module Xmobar.Plugins.Kbd(Kbd(..)) where
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
import Data.Char (toLower)
import Control.Monad (forever)
import Control.Applicative ((<|>))
import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras
import Xmobar.Run.Exec
import Xmobar.X11.Events (nextEvent')
import Xmobar.System.Kbd
noLaySymbols :: [String]
noLaySymbols :: [String]
noLaySymbols = [String
"group", String
"inet", String
"ctr", String
"compose", String
"pc", String
"ctrl", String
"terminate"]
splitLayout :: String -> [String]
splitLayout :: String -> [String]
splitLayout String
s
= (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
flt
([String] -> [String])
-> ([String] -> [String]) -> [String] -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
':'))
([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> String -> [String]
split (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'+') String
s
where
flt :: String -> Bool
flt String
"" = Bool
False
flt String
s' = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
s') [String]
noLaySymbols
split :: (Char -> Bool) -> String -> [String]
split :: (Char -> Bool) -> String -> [String]
split Char -> Bool
p String
s = case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Char -> Bool
p String
s of
(String
pref, Char
_:String
suf) -> String
pref String -> [String] -> [String]
forall a. a -> [a] -> [a]
: (Char -> Bool) -> String -> [String]
split Char -> Bool
p String
suf
(String
pref, String
"") -> [String
pref]
searchReplaceLayout :: KbdOpts -> String -> String
searchReplaceLayout :: KbdOpts -> String -> String
searchReplaceLayout KbdOpts
opts String
s = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
s (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ String -> KbdOpts -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
s KbdOpts
opts
getKbdLay :: Display -> KbdOpts -> IO String
getKbdLay :: Display -> KbdOpts -> IO String
getKbdLay Display
dpy KbdOpts
opts = do
[String]
lay <- String -> [String]
splitLayout (String -> [String]) -> IO String -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Display -> IO String
getLayoutStr Display
dpy
[String]
grps <- (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
2) ([String] -> [String]) -> IO [String] -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Display -> IO [String]
getGrpNames Display
dpy
Int
curLay <- Display -> IO Int
getKbdLayout Display
dpy
String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> IO String) -> String -> IO String
forall a b. (a -> b) -> a -> b
$ KbdOpts -> String -> String
searchReplaceLayout KbdOpts
opts
(String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"??"
(Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ ([String]
lay [String] -> Int -> Maybe String
forall a. [a] -> Int -> Maybe a
!!? Int
curLay) Maybe String -> Maybe String -> Maybe String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([String]
grps [String] -> Int -> Maybe String
forall a. [a] -> Int -> Maybe a
!!? Int
curLay)
(!!?) :: [a] -> Int -> Maybe a
!!? :: [a] -> Int -> Maybe a
(!!?) [] Int
_ = Maybe a
forall a. Maybe a
Nothing
(!!?) (a
x : [a]
_) Int
0 = a -> Maybe a
forall a. a -> Maybe a
Just a
x
(!!?) (a
_ : [a]
xs) Int
i = [a]
xs [a] -> Int -> Maybe a
forall a. [a] -> Int -> Maybe a
!!? (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
newtype Kbd = Kbd [(String, String)]
deriving (ReadPrec [Kbd]
ReadPrec Kbd
Int -> ReadS Kbd
ReadS [Kbd]
(Int -> ReadS Kbd)
-> ReadS [Kbd] -> ReadPrec Kbd -> ReadPrec [Kbd] -> Read Kbd
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Kbd]
$creadListPrec :: ReadPrec [Kbd]
readPrec :: ReadPrec Kbd
$creadPrec :: ReadPrec Kbd
readList :: ReadS [Kbd]
$creadList :: ReadS [Kbd]
readsPrec :: Int -> ReadS Kbd
$creadsPrec :: Int -> ReadS Kbd
Read, Int -> Kbd -> String -> String
[Kbd] -> String -> String
Kbd -> String
(Int -> Kbd -> String -> String)
-> (Kbd -> String) -> ([Kbd] -> String -> String) -> Show Kbd
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [Kbd] -> String -> String
$cshowList :: [Kbd] -> String -> String
show :: Kbd -> String
$cshow :: Kbd -> String
showsPrec :: Int -> Kbd -> String -> String
$cshowsPrec :: Int -> Kbd -> String -> String
Show)
instance Exec Kbd where
alias :: Kbd -> String
alias (Kbd KbdOpts
_) = String
"kbd"
start :: Kbd -> (String -> IO ()) -> IO ()
start (Kbd KbdOpts
opts) String -> IO ()
cb = do
Display
dpy <- String -> IO Display
openDisplay String
""
String -> IO ()
cb (String -> IO ()) -> IO String -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Display -> KbdOpts -> IO String
getKbdLay Display
dpy KbdOpts
opts
CUInt
_ <- Display -> CUInt -> CUInt -> CULong -> CULong -> IO CUInt
xkbSelectEventDetails Display
dpy CUInt
xkbUseCoreKbd CUInt
xkbStateNotify CULong
xkbAllStateComponentsMask CULong
xkbGroupStateMask
CUInt
_ <- Display -> CUInt -> CUInt -> CUInt -> IO CUInt
xkbSelectEvents Display
dpy CUInt
xkbUseCoreKbd CUInt
xkbNewKeyboardNotifyMask CUInt
xkbNewKeyboardNotifyMask
(XEventPtr -> IO Any) -> IO Any
forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent ((XEventPtr -> IO Any) -> IO Any)
-> (XEventPtr -> IO Any) -> IO Any
forall a b. (a -> b) -> a -> b
$ \XEventPtr
e -> IO () -> IO Any
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO Any) -> IO () -> IO Any
forall a b. (a -> b) -> a -> b
$ do
Display -> XEventPtr -> IO ()
nextEvent' Display
dpy XEventPtr
e
Event
_ <- XEventPtr -> IO Event
getEvent XEventPtr
e
String -> IO ()
cb (String -> IO ()) -> IO String -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Display -> KbdOpts -> IO String
getKbdLay Display
dpy KbdOpts
opts
Display -> IO ()
closeDisplay Display
dpy
() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()