{-# LINE 1 "src/Xmobar/System/Kbd.hsc" #-}
{-# LANGUAGE ScopedTypeVariables, ForeignFunctionInterface, MultiParamTypeClasses, DeriveDataTypeable, FlexibleInstances, PatternGuards #-}
-----------------------------------------------------------------------------
-- |
-- 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.System.Kbd where

import Control.Monad ((<=<))

import Foreign
import Foreign.C.Types
import Foreign.C.String

import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras (none)





--
-- Definition for XkbStaceRec and getKbdLayout taken from
-- XMonad.Layout.XKBLayout
--
data XkbStateRec = XkbStateRec {
    XkbStateRec -> CUChar
group :: CUChar,
    XkbStateRec -> CUChar
locked_group :: CUChar,
    XkbStateRec -> CUShort
base_group :: CUShort,
    XkbStateRec -> CUShort
latched_group :: CUShort,
    XkbStateRec -> CUChar
mods :: CUChar,
    XkbStateRec -> CUChar
base_mods :: CUChar,
    XkbStateRec -> CUChar
latched_mods :: CUChar,
    XkbStateRec -> CUChar
locked_mods :: CUChar,
    XkbStateRec -> CUChar
compat_state :: CUChar,
    XkbStateRec -> CUChar
grab_mods :: CUChar,
    XkbStateRec -> CUChar
compat_grab_mods :: CUChar,
    XkbStateRec -> CUChar
lookup_mods :: CUChar,
    XkbStateRec -> CUChar
compat_lookup_mods :: CUChar,
    XkbStateRec -> CUShort
ptr_buttons :: CUShort
}

instance Storable XkbStateRec where
    sizeOf :: XkbStateRec -> Int
sizeOf XkbStateRec
_ = ((Int
18))
{-# LINE 54 "src/Xmobar/System/Kbd.hsc" #-}
    alignment _ = alignment (undefined :: CUShort)
    poke :: Ptr XkbStateRec -> XkbStateRec -> IO ()
poke Ptr XkbStateRec
_ XkbStateRec
_ = IO ()
forall a. HasCallStack => a
undefined
    peek :: Ptr XkbStateRec -> IO XkbStateRec
peek Ptr XkbStateRec
ptr = do
        r_group <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
0)) Ptr XkbStateRec
ptr
{-# LINE 58 "src/Xmobar/System/Kbd.hsc" #-}
        r_locked_group <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
1)) ptr
{-# LINE 59 "src/Xmobar/System/Kbd.hsc" #-}
        r_base_group <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUShort
forall b. Ptr b -> Int -> IO CUShort
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
2)) ptr
{-# LINE 60 "src/Xmobar/System/Kbd.hsc" #-}
        r_latched_group <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUShort
forall b. Ptr b -> Int -> IO CUShort
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
4)) ptr
{-# LINE 61 "src/Xmobar/System/Kbd.hsc" #-}
        r_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
6)) ptr
{-# LINE 62 "src/Xmobar/System/Kbd.hsc" #-}
        r_base_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
7)) ptr
{-# LINE 63 "src/Xmobar/System/Kbd.hsc" #-}
        r_latched_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
8)) ptr
{-# LINE 64 "src/Xmobar/System/Kbd.hsc" #-}
        r_locked_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
9)) ptr
{-# LINE 65 "src/Xmobar/System/Kbd.hsc" #-}
        r_compat_state <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
10)) ptr
{-# LINE 66 "src/Xmobar/System/Kbd.hsc" #-}
        r_grab_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
11)) ptr
{-# LINE 67 "src/Xmobar/System/Kbd.hsc" #-}
        r_compat_grab_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
12)) ptr
{-# LINE 68 "src/Xmobar/System/Kbd.hsc" #-}
        r_lookup_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
13)) ptr
{-# LINE 69 "src/Xmobar/System/Kbd.hsc" #-}
        r_compat_lookup_mods <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
14)) ptr
{-# LINE 70 "src/Xmobar/System/Kbd.hsc" #-}
        r_ptr_buttons <- ((\Ptr XkbStateRec
hsc_ptr -> Ptr XkbStateRec -> Int -> IO CUShort
forall b. Ptr b -> Int -> IO CUShort
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbStateRec
hsc_ptr Int
16)) ptr
{-# LINE 71 "src/Xmobar/System/Kbd.hsc" #-}
        return XkbStateRec {
            group = r_group,
            locked_group = r_locked_group,
            base_group = r_base_group,
            latched_group = r_latched_group,
            mods = r_mods,
            base_mods = r_base_mods,
            latched_mods = r_latched_mods,
            locked_mods = r_locked_mods,
            compat_state = r_compat_state,
            grab_mods = r_grab_mods,
            compat_grab_mods = r_compat_grab_mods,
            lookup_mods = r_lookup_mods,
            compat_lookup_mods = r_compat_lookup_mods,
            ptr_buttons = r_ptr_buttons
        }

foreign import ccall unsafe "X11/XKBlib.h XkbGetState"
    xkbGetState :: Display -> CUInt -> Ptr XkbStateRec -> IO CInt


getKbdLayout :: Display -> IO Int
getKbdLayout :: Display -> IO Int
getKbdLayout Display
d = (Ptr XkbStateRec -> IO Int) -> IO Int
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr XkbStateRec -> IO Int) -> IO Int)
-> (Ptr XkbStateRec -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Ptr XkbStateRec
stRecPtr -> do
    Display -> CUInt -> Ptr XkbStateRec -> IO Status
xkbGetState Display
d CUInt
0x100 Ptr XkbStateRec
stRecPtr
    st <- Ptr XkbStateRec -> IO XkbStateRec
forall a. Storable a => Ptr a -> IO a
peek Ptr XkbStateRec
stRecPtr
    return $ fromIntegral (group st)

data XkbKeyNameRec = XkbKeyNameRec {
    XkbKeyNameRec -> Ptr CChar
name :: Ptr CChar -- array
}

--
-- the t_ before alias is just because of name collisions
--
data XkbKeyAliasRec = XkbKeyAliasRec {
    XkbKeyAliasRec -> Ptr CChar
real  :: Ptr CChar, -- array
    XkbKeyAliasRec -> Ptr CChar
t_alias :: Ptr CChar  -- array
}

--
-- the t_ before geometry is just because of name collisions
--
data XkbNamesRec = XkbNamesRec {
    XkbNamesRec -> Atom
keycodes :: Atom,
    XkbNamesRec -> Atom
t_geometry :: Atom,
    XkbNamesRec -> Atom
symbols :: Atom,
    XkbNamesRec -> Atom
types :: Atom,
    XkbNamesRec -> Atom
compat :: Atom,
    XkbNamesRec -> [Atom]
vmods :: [Atom], -- Atom              vmods[XkbNumVirtualMods];
    XkbNamesRec -> [Atom]
indicators :: [Atom], -- Atom              indicators[XkbNumIndicators];
    XkbNamesRec -> [Atom]
groups :: [Atom], -- Atom              groups[XkbNumKbdGroups];
    XkbNamesRec -> Ptr XkbKeyNameRec
keys :: Ptr XkbKeyNameRec,
    XkbNamesRec -> Ptr CChar
key_aliases :: Ptr CChar, -- dont care XkbKeyAliasRec,
    XkbNamesRec -> Ptr Atom
radio_groups :: Ptr Atom,
    XkbNamesRec -> Atom
phys_symbols :: Atom,
    XkbNamesRec -> CUChar
num_keys :: CUChar,
    XkbNamesRec -> CUChar
num_key_aliases :: CUChar,
    XkbNamesRec -> CUShort
num_rg :: CUShort
}

--
-- the t_ before map, indicators and compat are just because of name collisions
--
data XkbDescRec = XkbDescRec {
    XkbDescRec -> Ptr CChar
t_dpy :: Ptr CChar, -- struct _XDisplay* ; don't care
    XkbDescRec -> CUShort
flags :: CUShort,
    XkbDescRec -> CUShort
device_spec :: CUShort,
    XkbDescRec -> KeyCode
min_key_code :: KeyCode,
    XkbDescRec -> KeyCode
max_key_code :: KeyCode,
    XkbDescRec -> Ptr CChar
ctrls :: Ptr CChar, -- XkbControlsPtr ;  dont' care
    XkbDescRec -> Ptr CChar
server :: Ptr CChar, -- XkbServerMapPtr ;  dont' care
    XkbDescRec -> Ptr CChar
t_map :: Ptr CChar, --XkbClientMapPtr ;  dont' care
    XkbDescRec -> Ptr CChar
t_indicators :: Ptr CChar, -- XkbIndicatorPtr ;  dont' care
    XkbDescRec -> Ptr XkbNamesRec
names :: Ptr XkbNamesRec, -- array
    XkbDescRec -> Ptr CChar
t_compat :: Ptr CChar, -- XkbCompatMap ;  dont' care
    XkbDescRec -> Ptr CChar
geom :: Ptr CChar -- XkbGeometryPtr ;  dont' care

}

instance Storable XkbKeyNameRec where
    sizeOf :: XkbKeyNameRec -> Int
sizeOf XkbKeyNameRec
_ = ((Int
4))
{-# LINE 152 "src/Xmobar/System/Kbd.hsc" #-}
    alignment _ = alignment (undefined :: CUShort)
    poke :: Ptr XkbKeyNameRec -> XkbKeyNameRec -> IO ()
poke Ptr XkbKeyNameRec
_ XkbKeyNameRec
_ = IO ()
forall a. HasCallStack => a
undefined
    peek :: Ptr XkbKeyNameRec -> IO XkbKeyNameRec
peek Ptr XkbKeyNameRec
ptr = do
        r_name <- ((\Ptr XkbKeyNameRec
hsc_ptr -> Ptr XkbKeyNameRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbKeyNameRec
hsc_ptr Int
0)) Ptr XkbKeyNameRec
ptr
{-# LINE 156 "src/Xmobar/System/Kbd.hsc" #-}

        return XkbKeyNameRec {
            name = r_name
        }

instance Storable XkbKeyAliasRec where
    sizeOf :: XkbKeyAliasRec -> Int
sizeOf XkbKeyAliasRec
_ = ((Int
8))
{-# LINE 163 "src/Xmobar/System/Kbd.hsc" #-}
    alignment _ = alignment (undefined :: CUShort)
    poke :: Ptr XkbKeyAliasRec -> XkbKeyAliasRec -> IO ()
poke Ptr XkbKeyAliasRec
_ XkbKeyAliasRec
_ = IO ()
forall a. HasCallStack => a
undefined
    peek :: Ptr XkbKeyAliasRec -> IO XkbKeyAliasRec
peek Ptr XkbKeyAliasRec
ptr = do
        r_real <- ((\Ptr XkbKeyAliasRec
hsc_ptr -> Ptr XkbKeyAliasRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbKeyAliasRec
hsc_ptr Int
0)) Ptr XkbKeyAliasRec
ptr
{-# LINE 167 "src/Xmobar/System/Kbd.hsc" #-}
        r_alias <- ((\Ptr XkbKeyAliasRec
hsc_ptr -> Ptr XkbKeyAliasRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbKeyAliasRec
hsc_ptr Int
4)) ptr
{-# LINE 168 "src/Xmobar/System/Kbd.hsc" #-}

        return XkbKeyAliasRec {
            real = r_real,
            t_alias = r_alias
        }

instance Storable XkbNamesRec where
    sizeOf :: XkbNamesRec -> Int
sizeOf XkbNamesRec
_ = ((Int
496))
{-# LINE 176 "src/Xmobar/System/Kbd.hsc" #-}
    alignment _ = alignment (undefined :: CUShort)
    poke :: Ptr XkbNamesRec -> XkbNamesRec -> IO ()
poke Ptr XkbNamesRec
_ XkbNamesRec
_ = IO ()
forall a. HasCallStack => a
undefined
    peek :: Ptr XkbNamesRec -> IO XkbNamesRec
peek Ptr XkbNamesRec
ptr = do
        r_keycodes <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO Atom
forall b. Ptr b -> Int -> IO Atom
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
0)) Ptr XkbNamesRec
ptr
{-# LINE 180 "src/Xmobar/System/Kbd.hsc" #-}
        r_geometry <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO Atom
forall b. Ptr b -> Int -> IO Atom
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
8)) ptr
{-# LINE 181 "src/Xmobar/System/Kbd.hsc" #-}
        r_symbols <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO Atom
forall b. Ptr b -> Int -> IO Atom
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
16)) ptr
{-# LINE 182 "src/Xmobar/System/Kbd.hsc" #-}
        r_types <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO Atom
forall b. Ptr b -> Int -> IO Atom
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
24)) ptr
{-# LINE 183 "src/Xmobar/System/Kbd.hsc" #-}
        r_compat <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO Atom
forall b. Ptr b -> Int -> IO Atom
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
32)) ptr
{-# LINE 184 "src/Xmobar/System/Kbd.hsc" #-}
        r_vmods <- peekArray (16) $ ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec
hsc_ptr Ptr XkbNamesRec -> Int -> Ptr Atom
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
40)) ptr
{-# LINE 185 "src/Xmobar/System/Kbd.hsc" #-}
        r_indicators <- peekArray (32) $ ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec
hsc_ptr Ptr XkbNamesRec -> Int -> Ptr Atom
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
168)) ptr
{-# LINE 186 "src/Xmobar/System/Kbd.hsc" #-}
        r_groups <- peekArray (4) $ ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec
hsc_ptr Ptr XkbNamesRec -> Int -> Ptr Atom
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
424)) ptr
{-# LINE 187 "src/Xmobar/System/Kbd.hsc" #-}
        r_keys <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO (Ptr XkbKeyNameRec)
forall b. Ptr b -> Int -> IO (Ptr XkbKeyNameRec)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
456)) ptr
{-# LINE 188 "src/Xmobar/System/Kbd.hsc" #-}
        r_key_aliases <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
464)) ptr
{-# LINE 189 "src/Xmobar/System/Kbd.hsc" #-}
        r_radio_groups <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO (Ptr Atom)
forall b. Ptr b -> Int -> IO (Ptr Atom)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
472)) ptr
{-# LINE 190 "src/Xmobar/System/Kbd.hsc" #-}
        r_phys_symbols <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO Atom
forall b. Ptr b -> Int -> IO Atom
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
480)) ptr
{-# LINE 191 "src/Xmobar/System/Kbd.hsc" #-}
        r_num_keys <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
488)) ptr
{-# LINE 192 "src/Xmobar/System/Kbd.hsc" #-}
        r_num_key_aliases <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO CUChar
forall b. Ptr b -> Int -> IO CUChar
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
489)) ptr
{-# LINE 193 "src/Xmobar/System/Kbd.hsc" #-}
        r_num_rg <- ((\Ptr XkbNamesRec
hsc_ptr -> Ptr XkbNamesRec -> Int -> IO CUShort
forall b. Ptr b -> Int -> IO CUShort
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbNamesRec
hsc_ptr Int
490)) ptr
{-# LINE 194 "src/Xmobar/System/Kbd.hsc" #-}

        return XkbNamesRec {
            keycodes = r_keycodes,
            t_geometry = r_geometry,
            symbols = r_symbols,
            types = r_types,
            compat = r_compat,
            vmods = r_vmods,
            indicators = r_indicators,
            groups = r_groups,
            keys = r_keys,
            key_aliases = r_key_aliases,
            radio_groups = r_radio_groups,
            phys_symbols = r_phys_symbols,
            num_keys = r_num_keys,
            num_key_aliases = r_num_key_aliases,
            num_rg = r_num_rg
       }

instance Storable XkbDescRec where
    sizeOf :: XkbDescRec -> Int
sizeOf XkbDescRec
_ = ((Int
72))
{-# LINE 215 "src/Xmobar/System/Kbd.hsc" #-}
    alignment _ = alignment (undefined :: CUShort)
    poke :: Ptr XkbDescRec -> XkbDescRec -> IO ()
poke Ptr XkbDescRec
_ XkbDescRec
_ = IO ()
forall a. HasCallStack => a
undefined
    peek :: Ptr XkbDescRec -> IO XkbDescRec
peek Ptr XkbDescRec
ptr = do
        r_dpy <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
0)) Ptr XkbDescRec
ptr
{-# LINE 219 "src/Xmobar/System/Kbd.hsc" #-}
        r_flags <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO CUShort
forall b. Ptr b -> Int -> IO CUShort
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
8)) ptr
{-# LINE 220 "src/Xmobar/System/Kbd.hsc" #-}
        r_device_spec <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO CUShort
forall b. Ptr b -> Int -> IO CUShort
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
10)) ptr
{-# LINE 221 "src/Xmobar/System/Kbd.hsc" #-}
        r_min_key_code <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO KeyCode
forall b. Ptr b -> Int -> IO KeyCode
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
12)) ptr
{-# LINE 222 "src/Xmobar/System/Kbd.hsc" #-}
        r_max_key_code <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO KeyCode
forall b. Ptr b -> Int -> IO KeyCode
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
13)) ptr
{-# LINE 223 "src/Xmobar/System/Kbd.hsc" #-}
        r_ctrls <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
16)) ptr
{-# LINE 224 "src/Xmobar/System/Kbd.hsc" #-}
        r_server <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
24)) ptr
{-# LINE 225 "src/Xmobar/System/Kbd.hsc" #-}
        r_map <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
32)) ptr
{-# LINE 226 "src/Xmobar/System/Kbd.hsc" #-}
        r_indicators <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
40)) ptr
{-# LINE 227 "src/Xmobar/System/Kbd.hsc" #-}
        r_names <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr XkbNamesRec)
forall b. Ptr b -> Int -> IO (Ptr XkbNamesRec)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
48)) ptr
{-# LINE 228 "src/Xmobar/System/Kbd.hsc" #-}
        r_compat <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
56)) ptr
{-# LINE 229 "src/Xmobar/System/Kbd.hsc" #-}
        r_geom <- ((\Ptr XkbDescRec
hsc_ptr -> Ptr XkbDescRec -> Int -> IO (Ptr CChar)
forall b. Ptr b -> Int -> IO (Ptr CChar)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr XkbDescRec
hsc_ptr Int
64)) ptr
{-# LINE 230 "src/Xmobar/System/Kbd.hsc" #-}

        return XkbDescRec {
            t_dpy = r_dpy,
            flags = r_flags,
            device_spec = r_device_spec,
            min_key_code = r_min_key_code,
            max_key_code = r_max_key_code,
            ctrls = r_ctrls,
            server = r_server,
            t_map = r_map,
            t_indicators = r_indicators,
            names = r_names,
            t_compat = r_compat,
            geom = r_geom
        }

--
-- C bindings
--

foreign import ccall unsafe "X11/XKBlib.h XkbAllocKeyboard"
    xkbAllocKeyboard :: IO (Ptr XkbDescRec)

foreign import ccall unsafe "X11/XKBlib.h XkbGetNames"
    xkbGetNames :: Display -> CUInt -> (Ptr XkbDescRec)  -> IO Status

foreign import ccall unsafe "X11/XKBlib.h XGetAtomName"
    xGetAtomName :: Display -> Atom -> IO CString

foreign import ccall unsafe "X11/XKBlib.h XkbFreeNames"
    xkbFreeNames :: (Ptr XkbDescRec) -> CUInt -> CInt -> IO ()

foreign import ccall unsafe "X11/XKBlib.h XkbFreeKeyboard"
    xkbFreeKeyboard :: (Ptr XkbDescRec) -> CUInt -> CInt -> IO ()

foreign import ccall unsafe "X11/XKBlib.h XkbSelectEventDetails"
    xkbSelectEventDetails :: Display -> CUInt -> CUInt -> CULong -> CULong -> IO CUInt

foreign import ccall unsafe "X11/XKBlib.h XkbSelectEvents"
    xkbSelectEvents :: Display -> CUInt -> CUInt -> CUInt -> IO CUInt


xkbUseCoreKbd :: CUInt
xkbUseCoreKbd :: CUInt
xkbUseCoreKbd = CUInt
256
{-# LINE 274 "src/Xmobar/System/Kbd.hsc" #-}

xkbStateNotify :: CUInt
xkbStateNotify :: CUInt
xkbStateNotify = CUInt
2
{-# LINE 277 "src/Xmobar/System/Kbd.hsc" #-}

xkbIndicatorStateNotify :: CUInt
xkbIndicatorStateNotify :: CUInt
xkbIndicatorStateNotify = CUInt
4
{-# LINE 280 "src/Xmobar/System/Kbd.hsc" #-}

xkbMapNotify :: CUInt
xkbMapNotify :: CUInt
xkbMapNotify = CUInt
1
{-# LINE 283 "src/Xmobar/System/Kbd.hsc" #-}

xkbMapNotifyMask :: CUInt
xkbMapNotifyMask :: CUInt
xkbMapNotifyMask = CUInt
2
{-# LINE 286 "src/Xmobar/System/Kbd.hsc" #-}

xkbNewKeyboardNotifyMask :: CUInt
xkbNewKeyboardNotifyMask :: CUInt
xkbNewKeyboardNotifyMask  = CUInt
1
{-# LINE 289 "src/Xmobar/System/Kbd.hsc" #-}

xkbAllStateComponentsMask :: CULong
xkbAllStateComponentsMask :: CULong
xkbAllStateComponentsMask = CULong
16383
{-# LINE 292 "src/Xmobar/System/Kbd.hsc" #-}

xkbGroupStateMask :: CULong
xkbGroupStateMask :: CULong
xkbGroupStateMask = CULong
16
{-# LINE 295 "src/Xmobar/System/Kbd.hsc" #-}

xkbSymbolsNameMask :: CUInt
xkbSymbolsNameMask :: CUInt
xkbSymbolsNameMask = CUInt
4
{-# LINE 298 "src/Xmobar/System/Kbd.hsc" #-}

xkbGroupNamesMask :: CUInt
xkbGroupNamesMask :: CUInt
xkbGroupNamesMask = CUInt
4096
{-# LINE 301 "src/Xmobar/System/Kbd.hsc" #-}

type KbdOpts = [(String, String)]

getLayoutStr :: Display -> IO String
getLayoutStr :: Display -> IO String
getLayoutStr Display
dpy =  do
        kbdDescPtr <- IO (Ptr XkbDescRec)
xkbAllocKeyboard
        status <- xkbGetNames dpy xkbSymbolsNameMask kbdDescPtr
        str <- getLayoutStr' status dpy kbdDescPtr
        xkbFreeNames kbdDescPtr xkbSymbolsNameMask 1
        xkbFreeKeyboard kbdDescPtr 0 1
        return str

getLayoutStr' :: Status -> Display -> (Ptr XkbDescRec) -> IO String
getLayoutStr' :: Status -> Display -> Ptr XkbDescRec -> IO String
getLayoutStr' Status
st Display
dpy Ptr XkbDescRec
kbdDescPtr =
        if Status
st Status -> Status -> Bool
forall a. Eq a => a -> a -> Bool
== Status
0 then -- Success
            do
            kbdDesc <- Ptr XkbDescRec -> IO XkbDescRec
forall a. Storable a => Ptr a -> IO a
peek Ptr XkbDescRec
kbdDescPtr
            nameArray <- peek (names kbdDesc)
            atom <- xGetAtomName dpy (symbols nameArray)
            str <- peekCString atom
            return str
        else -- Behaviour on error
            do
                String -> IO String
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return String
"Error while requesting layout!"

getGrpNames :: Display -> IO [String]
getGrpNames :: Display -> IO [String]
getGrpNames Display
dpy =  do
        kbdDescPtr <- IO (Ptr XkbDescRec)
xkbAllocKeyboard
        status <- xkbGetNames dpy xkbGroupNamesMask kbdDescPtr
        str <- getGrpNames' status dpy kbdDescPtr
        xkbFreeNames kbdDescPtr xkbGroupNamesMask 1
        xkbFreeKeyboard kbdDescPtr 0 1
        return str

getGrpNames' :: Status -> Display -> (Ptr XkbDescRec) -> IO [String]
getGrpNames' :: Status -> Display -> Ptr XkbDescRec -> IO [String]
getGrpNames' Status
st Display
dpy Ptr XkbDescRec
kbdDescPtr =
        if Status
st Status -> Status -> Bool
forall a. Eq a => a -> a -> Bool
== Status
0 then -- Success
            do
            kbdDesc <- Ptr XkbDescRec -> IO XkbDescRec
forall a. Storable a => Ptr a -> IO a
peek Ptr XkbDescRec
kbdDescPtr
            nameArray <- peek (names kbdDesc)
            let grpsArr = XkbNamesRec -> [Atom]
groups XkbNamesRec
nameArray
            let grps = (Atom -> Bool) -> [Atom] -> [Atom]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Atom -> Atom -> Bool
forall a. Eq a => a -> a -> Bool
/=Atom
none) [Atom]
grpsArr
            mapM (peekCString <=< xGetAtomName dpy) grps
        else [String] -> IO [String]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return [String
"Error while requesting layout!"]