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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
{-# Language GeneralizedNewtypeDeriving, MultiParamTypeClasses,
FunctionalDependencies, FlexibleInstances, TypeFamilies #-}
module Internal.KeysM where
import Control.Arrow (second)
import Control.Monad (void)
import Control.Monad.State (State(..), modify', get, execState)
import XMonad
import Data.Map (Map)
import qualified Data.Map as Map
{- Module that defines a DSL for binding keys. -}
newtype KeysM l a = KeysM (State (XConfig l, Map (KeyMask, KeySym) (X ())) a)
deriving (Functor, Applicative, Monad)
newtype ButtonsM l a = ButtonsM (State (XConfig l, Map (KeyMask, Button) (Window -> X ())) a)
deriving (Functor, Applicative, Monad)
newtype BindingBuilder b a = BindingBuilder (State (KeyMask, [(KeyMask, b)]) a)
deriving (Functor, Applicative, Monad)
class HasConfig m where
getConfig :: m l (XConfig l)
class Bindable k where
type BindableValue k :: *
type BindableMonad k :: (* -> *) -> * -> *
bind :: k -> BindingBuilder (BindableValue k) a -> BindableMonad k l ()
instance Semigroup (KeysM l ()) where
(<>) = mappend
instance Monoid (KeysM l ()) where
mempty = return ()
mappend = (>>)
runKeys :: KeysM l a -> XConfig l -> Map (KeyMask, KeySym) (X ())
runKeys (KeysM stateM) config =
snd $ execState stateM (config, Map.empty)
runButtons :: ButtonsM l a -> XConfig l -> Map (KeyMask, Button) (Window -> X ())
runButtons (ButtonsM stateM) config =
snd $ execState stateM (config, Map.empty)
instance HasConfig KeysM where
getConfig = fst <$> KeysM get
instance HasConfig ButtonsM where
getConfig = fst <$> ButtonsM get
{- Generally it is assumed that the mod key shoud be pressed, but not always. -}
noMod :: f -> BindingBuilder f ()
noMod = rawMask 0
rawMask :: KeyMask -> f -> BindingBuilder f ()
rawMask m x = BindingBuilder $ modify' (second ((m, x):))
maskMod :: KeyMask -> f -> BindingBuilder f ()
maskMod mask action = do
modMask <- fst <$> BindingBuilder get
rawMask (modMask .|. mask) action
altMask :: KeyMask
altMask = mod1Mask
hyperMask :: KeyMask
hyperMask = mod3Mask
altgrMask :: KeyMask
altgrMask = mod2Mask
superMask :: KeyMask
superMask = mod4Mask
justMod :: f -> BindingBuilder f ()
justMod = maskMod 0
instance Bindable KeySym where
type BindableValue KeySym = X ()
type BindableMonad KeySym = KeysM
-- bind :: KeySym -> BindingBuilder (X x) a -> KeysM l ()
bind key (BindingBuilder stM) = do
m <- modMask <$> getConfig
let (_, values) = execState stM (m, [])
KeysM $ modify' $ second $
flip (<>) (Map.fromList (map (\(m, v) -> ((m, key), v)) values))
instance Bindable Button where
type BindableValue Button = Window -> X ()
type BindableMonad Button = ButtonsM
-- bind :: KeySym -> BindingBuilder (Window -> X ()) a -> ButtonsM l ()
bind button (BindingBuilder stM) = do
m <- modMask <$> getConfig
let (_, values) = execState stM (m, [])
ButtonsM $ modify' $ second $
flip (<>) (Map.fromList (map (\(m, v) -> ((m, button), v)) values))
shiftControlAltSuperHyperAltgrMod :: f -> BindingBuilder f ()
shiftControlAltSuperHyperAltgrMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. superMask .|. hyperMask .|. altgrMask)
shiftControlAltSuperHyperMod :: f -> BindingBuilder f ()
shiftControlAltSuperHyperMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. superMask .|. hyperMask)
shiftControlAltSuperAltgrMod :: f -> BindingBuilder f ()
shiftControlAltSuperAltgrMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. superMask .|. altgrMask)
shiftControlAltSuperMod :: f -> BindingBuilder f ()
shiftControlAltSuperMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. superMask)
shiftControlAltHyperAltgrMod :: f -> BindingBuilder f ()
shiftControlAltHyperAltgrMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. hyperMask .|. altgrMask)
shiftControlAltHyperMod :: f -> BindingBuilder f ()
shiftControlAltHyperMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. hyperMask)
shiftControlAltAltgrMod :: f -> BindingBuilder f ()
shiftControlAltAltgrMod =
maskMod (shiftMask .|. controlMask .|. altMask .|. altgrMask)
shiftControlAltMod :: f -> BindingBuilder f ()
shiftControlAltMod =
maskMod (shiftMask .|. controlMask .|. altMask)
shiftControlSuperHyperAltgrMod :: f -> BindingBuilder f ()
shiftControlSuperHyperAltgrMod =
maskMod (shiftMask .|. controlMask .|. superMask .|. hyperMask .|. altgrMask)
shiftControlSuperHyperMod :: f -> BindingBuilder f ()
shiftControlSuperHyperMod =
maskMod (shiftMask .|. controlMask .|. superMask .|. hyperMask)
shiftControlSuperAltgrMod :: f -> BindingBuilder f ()
shiftControlSuperAltgrMod =
maskMod (shiftMask .|. controlMask .|. superMask .|. altgrMask)
shiftControlSuperMod :: f -> BindingBuilder f ()
shiftControlSuperMod =
maskMod (shiftMask .|. controlMask .|. superMask)
shiftControlHyperAltgrMod :: f -> BindingBuilder f ()
shiftControlHyperAltgrMod =
maskMod (shiftMask .|. controlMask .|. hyperMask .|. altgrMask)
shiftControlHyperMod :: f -> BindingBuilder f ()
shiftControlHyperMod =
maskMod (shiftMask .|. controlMask .|. hyperMask)
shiftControlAltgrMod :: f -> BindingBuilder f ()
shiftControlAltgrMod =
maskMod (shiftMask .|. controlMask .|. altgrMask)
shiftControlMod :: f -> BindingBuilder f ()
shiftControlMod =
maskMod (shiftMask .|. controlMask)
shiftAltSuperHyperAltgrMod :: f -> BindingBuilder f ()
shiftAltSuperHyperAltgrMod =
maskMod (shiftMask .|. altMask .|. superMask .|. hyperMask .|. altgrMask)
shiftAltSuperHyperMod :: f -> BindingBuilder f ()
shiftAltSuperHyperMod =
maskMod (shiftMask .|. altMask .|. superMask .|. hyperMask)
shiftAltSuperAltgrMod :: f -> BindingBuilder f ()
shiftAltSuperAltgrMod =
maskMod (shiftMask .|. altMask .|. superMask .|. altgrMask)
shiftAltSuperMod :: f -> BindingBuilder f ()
shiftAltSuperMod =
maskMod (shiftMask .|. altMask .|. superMask)
shiftAltHyperAltgrMod :: f -> BindingBuilder f ()
shiftAltHyperAltgrMod =
maskMod (shiftMask .|. altMask .|. hyperMask .|. altgrMask)
shiftAltHyperMod :: f -> BindingBuilder f ()
shiftAltHyperMod =
maskMod (shiftMask .|. altMask .|. hyperMask)
shiftAltAltgrMod :: f -> BindingBuilder f ()
shiftAltAltgrMod =
maskMod (shiftMask .|. altMask .|. altgrMask)
shiftAltMod :: f -> BindingBuilder f ()
shiftAltMod =
maskMod (shiftMask .|. altMask)
shiftSuperHyperAltgrMod :: f -> BindingBuilder f ()
shiftSuperHyperAltgrMod =
maskMod (shiftMask .|. superMask .|. hyperMask .|. altgrMask)
shiftSuperHyperMod :: f -> BindingBuilder f ()
shiftSuperHyperMod =
maskMod (shiftMask .|. superMask .|. hyperMask)
shiftSuperAltgrMod :: f -> BindingBuilder f ()
shiftSuperAltgrMod =
maskMod (shiftMask .|. superMask .|. altgrMask)
shiftSuperMod :: f -> BindingBuilder f ()
shiftSuperMod =
maskMod (shiftMask .|. superMask)
shiftHyperAltgrMod :: f -> BindingBuilder f ()
shiftHyperAltgrMod =
maskMod (shiftMask .|. hyperMask .|. altgrMask)
shiftHyperMod :: f -> BindingBuilder f ()
shiftHyperMod =
maskMod (shiftMask .|. hyperMask)
shiftAltgrMod :: f -> BindingBuilder f ()
shiftAltgrMod =
maskMod (shiftMask .|. altgrMask)
shiftMod :: f -> BindingBuilder f ()
shiftMod = maskMod shiftMask
controlAltSuperHyperAltgrMod :: f -> BindingBuilder f ()
controlAltSuperHyperAltgrMod =
maskMod (controlMask .|. altMask .|. superMask .|. hyperMask .|. altgrMask)
controlAltSuperHyperMod :: f -> BindingBuilder f ()
controlAltSuperHyperMod =
maskMod (controlMask .|. altMask .|. superMask .|. hyperMask)
controlAltSuperAltgrMod :: f -> BindingBuilder f ()
controlAltSuperAltgrMod =
maskMod (controlMask .|. altMask .|. superMask .|. altgrMask)
controlAltSuperMod :: f -> BindingBuilder f ()
controlAltSuperMod =
maskMod (controlMask .|. altMask .|. superMask)
controlAltHyperAltgrMod :: f -> BindingBuilder f ()
controlAltHyperAltgrMod =
maskMod (controlMask .|. altMask .|. hyperMask .|. altgrMask)
controlAltHyperMod :: f -> BindingBuilder f ()
controlAltHyperMod =
maskMod (controlMask .|. altMask .|. hyperMask)
controlAltAltgrMod :: f -> BindingBuilder f ()
controlAltAltgrMod =
maskMod (controlMask .|. altMask .|. altgrMask)
controlAltMod :: f -> BindingBuilder f ()
controlAltMod =
maskMod (controlMask .|. altMask)
controlSuperHyperAltgrMod :: f -> BindingBuilder f ()
controlSuperHyperAltgrMod =
maskMod (controlMask .|. superMask .|. hyperMask .|. altgrMask)
controlSuperHyperMod :: f -> BindingBuilder f ()
controlSuperHyperMod =
maskMod (controlMask .|. superMask .|. hyperMask)
controlSuperAltgrMod :: f -> BindingBuilder f ()
controlSuperAltgrMod =
maskMod (controlMask .|. superMask .|. altgrMask)
controlSuperMod :: f -> BindingBuilder f ()
controlSuperMod =
maskMod (controlMask .|. superMask)
controlHyperAltgrMod :: f -> BindingBuilder f ()
controlHyperAltgrMod =
maskMod (controlMask .|. hyperMask .|. altgrMask)
controlHyperMod :: f -> BindingBuilder f ()
controlHyperMod =
maskMod (controlMask .|. hyperMask)
controlAltgrMod :: f -> BindingBuilder f ()
controlAltgrMod =
maskMod (controlMask .|. altgrMask)
controlMod :: f -> BindingBuilder f ()
controlMod = maskMod controlMask
altSuperHyperAltgrMod :: f -> BindingBuilder f ()
altSuperHyperAltgrMod =
maskMod (altMask .|. superMask .|. hyperMask .|. altgrMask)
altSuperHyperMod :: f -> BindingBuilder f ()
altSuperHyperMod =
maskMod (altMask .|. superMask .|. hyperMask)
altSuperAltgrMod :: f -> BindingBuilder f ()
altSuperAltgrMod =
maskMod (altMask .|. superMask .|. altgrMask)
altSuperMod :: f -> BindingBuilder f ()
altSuperMod =
maskMod (altMask .|. superMask)
altHyperAltgrMod :: f -> BindingBuilder f ()
altHyperAltgrMod =
maskMod (altMask .|. hyperMask .|. altgrMask)
altHyperMod :: f -> BindingBuilder f ()
altHyperMod =
maskMod (altMask .|. hyperMask)
altAltgrMod :: f -> BindingBuilder f ()
altAltgrMod =
maskMod (altMask .|. altgrMask)
altMod :: f -> BindingBuilder f ()
altMod = maskMod altMask
superHyperAltgrMod :: f -> BindingBuilder f ()
superHyperAltgrMod =
maskMod (superMask .|. hyperMask .|. altgrMask)
superHyperMod :: f -> BindingBuilder f ()
superHyperMod =
maskMod (superMask .|. hyperMask)
superAltgrMod :: f -> BindingBuilder f ()
superAltgrMod =
maskMod (superMask .|. altgrMask)
superMod :: f -> BindingBuilder f ()
superMod = maskMod superMask
hyperAltgrMod :: f -> BindingBuilder f ()
hyperAltgrMod =
maskMod (hyperMask .|. altgrMask)
hyperMod :: f -> BindingBuilder f ()
hyperMod = maskMod hyperMask
altgrMod :: f -> BindingBuilder f ()
altgrMod = maskMod altgrMask
{- Can combine two or more of the functions above to apply the same action to
- multiple masks. -}
(-|-) :: (f -> BindingBuilder f ()) ->
(f -> BindingBuilder f ()) ->
f -> BindingBuilder f ()
(-|-) fn1 fn2 f = fn1 f >> fn2 f
{- Meant for submapping, binds all alphanumeric charactes to (fn c). -}
mapNumbersAndAlpha :: KeyMask -> (Char -> X ()) -> KeysM l ()
mapNumbersAndAlpha km fn = do
mapNumbers km fn
mapAlpha km fn
{- Meant for submapping. This binds all numbers to (fn x) where x is the number
- pressed and fn is the function provided. -}
mapNumbers :: KeyMask -> (Char -> X ()) -> KeysM l ()
mapNumbers km fn = do
mapM_ (\(key, ch) -> bind key $ rawMask km (fn ch))
[ (xK_0, '0')
, (xK_1, '1')
, (xK_2, '2')
, (xK_3, '3')
, (xK_4, '4')
, (xK_5, '5')
, (xK_6, '6')
, (xK_7, '7')
, (xK_8, '8')
, (xK_9, '9')
-- Programmer Dvorak shifts the numbers so I have to map to their unshifted
-- form.
, (xK_bracketright, '6')
, (xK_exclam, '8')
, (xK_bracketleft, '7')
, (xK_braceleft, '5')
, (xK_braceright, '3')
, (xK_parenleft, '1')
, (xK_equal, '9')
, (xK_asterisk, '0')
, (xK_parenright, '2')
, (xK_plus, '4') ]
{- Meant for submapping. This binds all alpha charactes to (fn c) where c is the
- character pressed and fn is the function provided. -}
mapAlpha :: KeyMask -> (Char -> X ()) -> KeysM l ()
mapAlpha km fn =
mapM_ (\(key, ch) -> bind key $ rawMask km (fn ch)) [
(xK_a, 'a')
, (xK_b, 'b')
, (xK_c, 'c')
, (xK_d, 'd')
, (xK_e, 'e')
, (xK_f, 'f')
, (xK_g, 'g')
, (xK_h, 'h')
, (xK_i, 'i')
, (xK_j, 'j')
, (xK_k, 'k')
, (xK_l, 'l')
, (xK_m, 'm')
, (xK_n, 'n')
, (xK_o, 'o')
, (xK_p, 'p')
, (xK_q, 'q')
, (xK_r, 'r')
, (xK_s, 's')
, (xK_t, 't')
, (xK_u, 'u')
, (xK_v, 'v')
, (xK_w, 'w')
, (xK_x, 'x')
, (xK_y, 'y')
, (xK_z, 'z')
]
|