aboutsummaryrefslogtreecommitdiffstats
path: root/XMonad/Util/NamedWindows.hs
blob: c29519eaedcc2108f88f1dced6e23bb2c0858112 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Util.NamedWindows
-- Copyright   :  (c) David Roundy <droundy@darcs.net>
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  David Roundy <droundy@darcs.net>
-- Stability   :  unstable
-- Portability :  unportable
--
-- This module allows you to associate the X titles of windows with
-- them.
--
-----------------------------------------------------------------------------

module XMonad.Util.NamedWindows (
                                   -- * Usage
                                   -- $usage
                                   NamedWindow,
                                   getName,
                                   withNamedWindow,
                                   unName
                                  ) where

import Prelude hiding ( catch )
import Control.Exception ( bracket, catch )

import qualified XMonad.StackSet as W ( peek )


import XMonad

-- $usage
-- See "XMonad.Layout.Tabbed" for an example of its use.


data NamedWindow = NW !String !Window
instance Eq NamedWindow where
    (NW s _) == (NW s' _) = s == s'
instance Ord NamedWindow where
    compare (NW s _) (NW s' _) = compare s s'
instance Show NamedWindow where
    show (NW n _) = n

getName :: Window -> X NamedWindow
getName w = withDisplay $ \d -> do
    let getIt = bracket getProp (xFree . tp_value) (fmap (`NW` w) . copy)

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

        copy prop = head `fmap` wcTextPropertyToTextList d prop

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

unName :: NamedWindow -> Window
unName (NW _ w) = w

withNamedWindow :: (NamedWindow -> X ()) -> X ()
withNamedWindow f = do ws <- gets windowset
                       whenJust (W.peek ws) $ \w -> getName w >>= f