module System.Taffybar.Support.PagerHints (
pagerHints
) where
import Codec.Binary.UTF8.String (encode)
import Control.Monad
import Data.Monoid
import Foreign.C.Types (CInt)
import XMonad
import qualified XMonad.StackSet as W
xLayoutProp :: X Atom
xLayoutProp :: X Window
xLayoutProp = String -> X Window
getAtom String
"_XMONAD_CURRENT_LAYOUT"
xVisibleProp :: X Atom
xVisibleProp :: X Window
xVisibleProp = String -> X Window
getAtom String
"_XMONAD_VISIBLE_WORKSPACES"
pagerHints :: XConfig a -> XConfig a
XConfig a
c = XConfig a
c { handleEventHook :: Event -> X All
handleEventHook = XConfig a -> Event -> X All
forall (l :: * -> *). XConfig l -> Event -> X All
handleEventHook XConfig a
c (Event -> X All) -> (Event -> X All) -> Event -> X All
forall {a}. Monoid a => a -> a -> a
+++ Event -> X All
pagerHintsEventHook
, logHook :: X ()
logHook = XConfig a -> X ()
forall (l :: * -> *). XConfig l -> X ()
logHook XConfig a
c X () -> X () -> X ()
forall {a}. Monoid a => a -> a -> a
+++ X ()
pagerHintsLogHook }
where a
x +++ :: a -> a -> a
+++ a
y = a
x a -> a -> a
forall {a}. Monoid a => a -> a -> a
`mappend` a
y
pagerHintsLogHook :: X ()
= do
(WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet
(String -> X ()
setCurrentLayout (String -> X ()) -> (WindowSet -> String) -> WindowSet -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Layout Window -> String
forall (layout :: * -> *) a.
LayoutClass layout a =>
layout a -> String
description (Layout Window -> String)
-> (WindowSet -> Layout Window) -> WindowSet -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workspace String (Layout Window) Window -> Layout Window
forall i l a. Workspace i l a -> l
W.layout (Workspace String (Layout Window) Window -> Layout Window)
-> (WindowSet -> Workspace String (Layout Window) Window)
-> WindowSet
-> Layout Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace (Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window)
-> (WindowSet
-> Screen String (Layout Window) Window ScreenId ScreenDetail)
-> WindowSet
-> Workspace String (Layout Window) Window
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> Screen String (Layout Window) Window ScreenId ScreenDetail
forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current)
(WindowSet -> X ()) -> X ()
forall a. (WindowSet -> X a) -> X a
withWindowSet
([String] -> X ()
setVisibleWorkspaces ([String] -> X ()) -> (WindowSet -> [String]) -> WindowSet -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Screen String (Layout Window) Window ScreenId ScreenDetail
-> String)
-> [Screen String (Layout Window) Window ScreenId ScreenDetail]
-> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Workspace String (Layout Window) Window -> String
forall i l a. Workspace i l a -> i
W.tag (Workspace String (Layout Window) Window -> String)
-> (Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window)
-> Screen String (Layout Window) Window ScreenId ScreenDetail
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Screen String (Layout Window) Window ScreenId ScreenDetail
-> Workspace String (Layout Window) Window
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace) ([Screen String (Layout Window) Window ScreenId ScreenDetail]
-> [String])
-> (WindowSet
-> [Screen String (Layout Window) Window ScreenId ScreenDetail])
-> WindowSet
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowSet
-> [Screen String (Layout Window) Window ScreenId ScreenDetail]
forall i l a sid sd. StackSet i l a sid sd -> [Screen i l a sid sd]
W.visible)
setCurrentLayout :: String -> X ()
setCurrentLayout :: String -> X ()
setCurrentLayout String
l = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
Window
r <- (XConf -> Window) -> X Window
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Window
theRoot
Window
a <- X Window
xLayoutProp
Window
c <- String -> X Window
getAtom String
"UTF8_STRING"
let l' :: [CChar]
l' = (Word8 -> CChar) -> [Word8] -> [CChar]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> CChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral (String -> [Word8]
encode String
l)
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Window -> Window -> Window -> CInt -> [CChar] -> IO ()
changeProperty8 Display
dpy Window
r Window
a Window
c CInt
propModeReplace [CChar]
l'
setVisibleWorkspaces :: [String] -> X ()
setVisibleWorkspaces :: [String] -> X ()
setVisibleWorkspaces [String]
vis = (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
dpy -> do
Window
r <- (XConf -> Window) -> X Window
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Window
theRoot
Window
a <- X Window
xVisibleProp
Window
c <- String -> X Window
getAtom String
"UTF8_STRING"
let vis' :: [CChar]
vis' = (Word8 -> CChar) -> [Word8] -> [CChar]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> CChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Word8] -> [CChar]) -> [Word8] -> [CChar]
forall a b. (a -> b) -> a -> b
$ (String -> [Word8]) -> [String] -> [Word8]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (([Word8] -> [Word8] -> [Word8]
forall a. [a] -> [a] -> [a]
++[Word8
0]) ([Word8] -> [Word8]) -> (String -> [Word8]) -> String -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [Word8]
encode) [String]
vis
IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Window -> Window -> Window -> CInt -> [CChar] -> IO ()
changeProperty8 Display
dpy Window
r Window
a Window
c CInt
propModeReplace [CChar]
vis'
pagerHintsEventHook :: Event -> X All
ClientMessageEvent {
ev_message_type :: Event -> Window
ev_message_type = Window
mt,
ev_data :: Event -> [CInt]
ev_data = [CInt]
d
} = (WindowSet -> X All) -> X All
forall a. (WindowSet -> X a) -> X a
withWindowSet ((WindowSet -> X All) -> X All) -> (WindowSet -> X All) -> X All
forall a b. (a -> b) -> a -> b
$ \WindowSet
_ -> do
Window
a <- X Window
xLayoutProp
Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Window
mt Window -> Window -> Bool
forall a. Eq a => a -> a -> Bool
== Window
a) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ [CInt] -> X ()
sendLayoutMessage [CInt]
d
All -> X All
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
pagerHintsEventHook Event
_ = All -> X All
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> All
All Bool
True)
sendLayoutMessage :: [CInt] -> X ()
sendLayoutMessage :: [CInt] -> X ()
sendLayoutMessage [CInt]
evData = case [CInt]
evData of
[] -> () -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
CInt
x:[CInt]
_ -> if CInt
x CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
< CInt
0
then ChangeLayout -> X ()
forall a. Message a => a -> X ()
sendMessage ChangeLayout
FirstLayout
else ChangeLayout -> X ()
forall a. Message a => a -> X ()
sendMessage ChangeLayout
NextLayout