-
Notifications
You must be signed in to change notification settings - Fork 9
/
Demo.hs
202 lines (155 loc) · 5.13 KB
/
Demo.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import Foreign.C.String
import Data.Word
import qualified Data.ByteString.Lazy as BS
import Control.Concurrent
import Control.Monad
import Data.Maybe
import qualified Graphics.XHB as X
import qualified Graphics.XHB.Gen.Xinerama as Xinerama
import System.IO
main = do
connectionM <- X.connect
case connectionM of
Nothing -> putStrLn "failed to get connection"
Just c -> demo c
demo :: X.Connection -> IO ()
demo c = do
handleError c
handleEvent c
-- send two requests
listReceipt <- X.listExtensions c
ssReceipt <- X.getScreenSaver c
-- create a window
wid <- X.newResource c
X.createWindow c $ demoCreateWindowReq c wid
X.mapWindow c wid
-- process first request
replyOrError <- X.getReply listReceipt
case replyOrError of
Left e -> putStrLn $ "error in extensions request" ++ showError e
Right listRep -> printExtReply listRep
-- process second request
replyOrError <- X.getReply ssReceipt
case replyOrError of
Left e -> putStrLn $ "error in screen saver request" ++ showError e
Right ssRep -> printSSRep ssRep
isActiveReceipt <- Xinerama.isActive c
replyOrError <- X.getReply isActiveReceipt
case replyOrError of
Left e -> putStrLn $ "error checking if xinerama is active" ++ showError e
Right isActiveReply -> printIsActive isActiveReply
putStrLn ""
putStrLn "Press any key to continue"
hSetBuffering stdin NoBuffering
getChar
putStrLn ""
printIsActive rep | rep == 0 = do
putStrLn ""
putStrLn "Xinerama is not active"
| otherwise = do
putStrLn ""
putStrLn "Xinerama is active!"
printExtReply :: X.ListExtensionsReply -> IO ()
printExtReply r =
let names = map strToString (X.names_ListExtensionsReply r)
in sequence_ $ map putStrLn $ "" : names
printSSRep :: X.GetScreenSaverReply -> IO ()
printSSRep r = sequence_ $ map putStrLn
[""
,"Screen saver info:"
," Interval: " ++ show (X.interval_GetScreenSaverReply r)
," Timeout: " ++ show (X.timeout_GetScreenSaverReply r)
]
-- this could be in a library somewhere
strToString :: X.STR -> String
strToString = map castCCharToChar . X.name_STR
-- | Create the data needed for the 'createWindow' request
demoCreateWindowReq :: X.Connection -> X.WINDOW -> X.CreateWindow
demoCreateWindowReq c w
= X.MkCreateWindow
0
w
(X.getRoot c)
0
0
100
100
5
X.WindowClassCopyFromParent
0
(X.toValueParam [(X.CWEventMask,X.toMask
[ X.EventMaskEnterWindow
, X.EventMaskLeaveWindow
, X.EventMaskFocusChange
]
)])
-- print errors from the queue
handleError :: X.Connection -> IO ()
handleError c = do
forkIO $ forever $ do
e <- X.waitForError c
putStrLn $ showError e
return ()
-- print events from the queue
handleEvent ::X.Connection -> IO ()
handleEvent c = do
forkIO $ forever $ do
e <- X.waitForEvent c
putStrLn $ showEvent e
return ()
showError :: X.SomeError -> String
showError serr = tryErrors hs showErrorBase serr
where hs = [ErrorHandler showWindowError
,ErrorHandler showUnknownError
]
-- show an error of type Window
showWindowError :: X.WindowError -> String
showWindowError err =
let badwindow = X.bad_value_WindowError err
in "WindowError: bad window: " ++ show badwindow
-- show an UnknownError
showUnknownError :: X.UnknownError -> String
showUnknownError (X.UnknownError bs) =
"UnknownError: " ++ (show . BS.unpack $ bs)
-- default case
showErrorBase :: String
showErrorBase = "Unhandled Error"
data ErrorHandler b = forall a . X.Error a => ErrorHandler (a -> b)
tryErrors :: [ErrorHandler b] -> b -> X.SomeError -> b
tryErrors hs z err = case foldr tryErr Nothing hs of
Nothing -> z
Just x -> x
where tryErr _ j@Just{} = j
tryErr h Nothing = case h of
ErrorHandler fn -> do
err' <- X.fromError err
return $ fn err'
showEvent :: X.SomeEvent -> String
showEvent = tryEvents hs showEventBase
where hs = [EventHandler showMotionNotify
,EventHandler showEnterNotify
,EventHandler showLeaveNotify
,EventHandler showFocusIn
,EventHandler showFocusOut
]
showMotionNotify :: X.MotionNotifyEvent -> String
showMotionNotify _ = "MotionNotify"
showEnterNotify :: X.EnterNotifyEvent -> String
showEnterNotify _ = "EnterNotify"
showLeaveNotify :: X.LeaveNotifyEvent -> String
showLeaveNotify _ = "LeaveNotify"
showFocusIn :: X.FocusInEvent -> String
showFocusIn _ = "FocusIn"
showFocusOut :: X.FocusOutEvent -> String
showFocusOut _ = "FocusOut"
showEventBase = "unhandled event"
data EventHandler b = forall a . X.Event a => EventHandler (a -> b)
tryEvents :: [EventHandler b] -> b -> X.SomeEvent -> b
tryEvents hs z ev = case foldr tryEv Nothing hs of
Nothing -> z
Just x -> x
where tryEv _ j@Just{} = j
tryEv h Nothing = case h of
EventHandler fn -> do
ev' <- X.fromEvent ev
return $ fn ev'