summaryrefslogtreecommitdiffhomepage
path: root/src/lib/Xmobar/Plugins/Locks.hs
blob: 19bce20eae6f74d8857a3e9b0c79092841ad5ea0 (plain)
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
62
63
64
-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Locks
-- Copyright   :  (c) Patrick Chilton
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Patrick Chilton <chpatrick@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A plugin that displays the status of the lock keys.
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.Locks(Locks(..)) where

import Graphics.X11
import Data.List
import Data.Bits
import Control.Monad
import Graphics.X11.Xlib.Extras
import Xmobar.Run.Commands
import Xmobar.System.Kbd
import Xmobar.Utils (nextEvent')

data Locks = Locks
    deriving (Read, Show)

locks :: [ ( KeySym, String )]
locks = [ ( xK_Caps_Lock,   "CAPS"   )
        , ( xK_Num_Lock,    "NUM"    )
        , ( xK_Scroll_Lock, "SCROLL" )
        ]

run' :: Display -> Window -> IO String
run' d root = do
    modMap <- getModifierMapping d
    ( _, _, _, _, _, _, _, m ) <- queryPointer d root

    ls <- filterM ( \( ks, _ ) -> do
        kc <- keysymToKeycode d ks
        return $ case find (elem kc . snd) modMap of
            Nothing       -> False
            Just ( i, _ ) -> testBit m (fromIntegral i)
        ) locks

    return $ unwords $ map snd ls

instance Exec Locks where
    alias Locks = "locks"
    start Locks cb = do
        d <- openDisplay ""
        root <- rootWindow d (defaultScreen d)
        _ <- xkbSelectEventDetails d xkbUseCoreKbd xkbIndicatorStateNotify m m

        allocaXEvent $ \ep -> forever $ do
            cb =<< run' d root
            nextEvent' d ep
            getEvent ep

        closeDisplay d
        return ()
      where
        m = xkbAllStateComponentsMask