getName :: Window -> X NamedWindow
getName w = withDisplay $ \d -> do
    -- TODO, this code is ugly and convoluted -- clean it up
    let getIt = bracket getProp (xFree . tp_value) (fmap (`NW` w) . copy)

        getProp = (internAtom d "_NET_WM_NAME" False >>= getTextProperty d w)
                      `E.catch` \(SomeException _) -> getTextProperty d w wM_NAME

        copy prop = fromMaybe "" . listToMaybe <$> wcTextPropertyToTextList d prop

    io $ getIt `E.catch` \(SomeException _) ->  (`NW` w) . resName <$> getClassHint d w

-- | Get 'NamedWindow' using 'wM_CLASS'
getName :: Window -> X NamedWindow
getName w = withDisplay $ \d -> do
    -- TODO, this code is ugly and convoluted -- clean it up
    let getIt = bracket getProp (xFree . tp_value) (fmap (`NW` w) . copy)

        getProp = (internAtom d "_NET_WM_NAME" False >>= getTextProperty d w)
                      `E.catch` \(SomeException _) -> getTextProperty d w wM_NAME

        copy prop = fromMaybe "" . listToMaybe <$> wcTextPropertyToTextList d prop

    io $ getIt `E.catch` \(SomeException _) ->  (`NW` w) . resName <$> getClassHint d w

-- | Get 'NamedWindow' using 'wM_CLASS'
getName w = withDisplay $ \d -> do
    -- TODO, this code is ugly and convoluted -- clean it up
    let getIt = bracket getProp (xFree . tp_value) (fmap (`NW` w) . copy)

        getProp = (internAtom d "_NET_WM_NAME" False >>= getTextProperty d w)
                      `E.catch` \(SomeException _) -> getTextProperty d w wM_NAME

        copy prop = fromMaybe "" . listToMaybe <$> wcTextPropertyToTextList d prop

    io $ getIt `E.catch` \(SomeException _) ->  (`NW` w) . resName <$> getClassHint d w

-- | Get 'NamedWindow' using 'wM_CLASS'