-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmonadctl.hs
executable file
·91 lines (84 loc) · 2.42 KB
/
xmonadctl.hs
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
-- xmonadctl to communicate with the xmonad config via XMonad.Hooks.ServerMode
-- This config was originally copied from https://hackage.haskell.org/package/xmonad-contrib-0.16/docs/XMonad-Hooks-ServerMode.html but has been altered since
import Control.Monad (unless)
import Graphics.X11.Xlib (
allocaXEvent,
clientMessage,
defaultScreen,
internAtom,
openDisplay,
rootWindow,
sendEvent,
structureNotifyMask,
sync,
)
import Graphics.X11.Xlib.Extras (
currentTime,
setClientMessageEvent,
setEventType,
)
import System.Environment (
getArgs,
getProgName,
)
import System.IO (
hPutStrLn,
isEOF,
stderr,
)
main :: IO ()
main = parse True "XMONAD_COMMAND" =<< getArgs
parse :: Bool -> String -> [String] -> IO ()
parse input addr args = case args of
["--"]
| input -> repl addr
| otherwise -> return ()
("--" : xs) -> sendAll addr xs
("-a" : a : xs) -> parse input a xs
("-h" : _) -> showHelp
("--help" : _) -> showHelp
("-?" : _) -> showHelp
(a@('-' : _) : _) -> hPutStrLn stderr ("Unknown option " ++ a)
(x : xs) -> sendCommand addr x >> parse False addr xs
[]
| input -> showHelp
| otherwise -> return ()
repl :: String -> IO ()
repl addr = do
e <- isEOF
unless
e
( do
l <- getLine
sendCommand addr l
repl addr
)
sendAll :: String -> [String] -> IO ()
sendAll addr ss = foldr (\a b -> sendCommand addr a >> b) (return ()) ss
sendCommand :: String -> String -> IO ()
sendCommand addr s = do
d <- openDisplay ""
rw <- rootWindow d $ defaultScreen d
a <- internAtom d addr False
m <- internAtom d s False
allocaXEvent $ \e -> do
setEventType e clientMessage
setClientMessageEvent e rw a 32 m currentTime
sendEvent d rw False structureNotifyMask e
sync d False
showHelp :: IO ()
showHelp = do
pn <- getProgName
putStrLn
( "Send commands to a running instance of xmonad. xmonad.hs must be configured with XMonad.Hooks.ServerMode to work.\n"
++ "-a atom name can be used at any point in the command line arguments to change which atom it is sending on.\n"
++ "If sent with '--' or only -a atom arguments, it will read commands from stdin.\n"
++ "Ex:\n"
++ pn
++ " cmd1 cmd2\n"
++ pn
++ " -a XMONAD_COMMAND cmd1 cmd2 cmd3 -a XMONAD_PRINT hello world\n"
++ pn
++ " -a XMONAD_PRINT # will read data from stdin.\n"
++ "The atom defaults to XMONAD_COMMAND."
)