blob: 76914cc3d78efc3067c83cf500e48b2b01609ab5 (
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
-----------------------------------------------------------------------------
-- |
-- Module : Plugins.Kbd
-- Copyright : (c) Martin Perner
-- License : BSD-style (see LICENSE)
--
-- Maintainer : Martin Perner <martin@perner.cc>
-- Stability : unstable
-- Portability : unportable
--
-- A keyboard layout indicator for Xmobar
--
-----------------------------------------------------------------------------
module Xmobar.Plugins.Kbd(Kbd(..)) where
import Data.List (isPrefixOf, findIndex)
import Data.Maybe (fromJust)
import Control.Monad (forever)
import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras
import Xmobar.Commands
import Xmobar.Utils (nextEvent')
import Xmobar.System.Kbd
-- 'Bad' prefixes of layouts
noLaySymbols :: [String]
noLaySymbols = ["group", "inet", "ctr", "pc", "ctrl"]
-- splits the layout string into the actual layouts
splitLayout :: String -> [String]
splitLayout s = splitLayout' noLaySymbols $ split s '+'
splitLayout' :: [String] -> [String] -> [String]
-- end of recursion, remove empty strings
splitLayout' [] s = map (takeWhile (\x -> x /= ':')) $ filter (\x -> length x > 0) s
-- remove current string if it has a 'bad' prefix
splitLayout' bad s = splitLayout' (tail bad) [x | x <- s, not $ isPrefixOf (head bad) x]
-- split String at each Char
split :: String -> Char -> [String]
split [] _ = [""]
split (c:cs) delim
| c == delim = "" : rest
| otherwise = (c : head rest) : tail rest
where
rest = split cs delim
-- replaces input string if on search list (exact match) with corresponding
-- element on replacement list.
--
-- if not found, return string unchanged
searchReplaceLayout :: KbdOpts -> String -> String
searchReplaceLayout opts s = let c = findIndex (\x -> fst x == s) opts in
case c of
Nothing -> s
x -> let i = (fromJust x) in
snd $ opts!!i
-- returns the active layout
getKbdLay :: Display -> KbdOpts -> IO String
getKbdLay dpy opts = do
lay <- getLayoutStr dpy
curLay <- getKbdLayout dpy
return $ searchReplaceLayout opts $ (splitLayout lay)!!(curLay)
data Kbd = Kbd [(String, String)]
deriving (Read, Show)
instance Exec Kbd where
alias (Kbd _) = "kbd"
start (Kbd opts) cb = do
dpy <- openDisplay ""
-- initial set of layout
cb =<< getKbdLay dpy opts
-- enable listing for
-- group changes
_ <- xkbSelectEventDetails dpy xkbUseCoreKbd xkbStateNotify xkbAllStateComponentsMask xkbGroupStateMask
-- layout/geometry changes
_ <- xkbSelectEvents dpy xkbUseCoreKbd xkbNewKeyboardNotifyMask xkbNewKeyboardNotifyMask
allocaXEvent $ \e -> forever $ do
nextEvent' dpy e
_ <- getEvent e
cb =<< getKbdLay dpy opts
closeDisplay dpy
return ()
|