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
|
{-# LANGUAGE DataKinds #-}
module Wetterhorn.Dsl.Input
( InputM,
InputEvent (..),
InputProxy (..),
NoProxy,
withProxies,
forwardEvent,
forwardKey,
whenKeyEvent,
whenButtonEvent,
useInputHandler,
unwrap,
filterEvent,
isPressEvent,
nextInputEventThat,
replayEvents,
isKeyEvent,
nextInputPressEvent,
continue,
nextInputEvent,
getModifierState,
)
where
import Control.Concurrent (threadDelay)
import Control.Monad
import Control.Monad.Cont (MonadCont)
import Control.Monad.Loops (andM)
import Control.Monad.RWS
( MonadIO (liftIO),
MonadReader (ask),
MonadState (get),
MonadTrans (lift),
RWST,
execRWST,
gets,
modify,
)
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Maybe (MaybeT (runMaybeT))
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Proxy
import Data.Word (Word32)
import qualified Wetterhorn.Core.ButtonEvent as ButtonEvent
import qualified Wetterhorn.Core.KeyEvent as KeyEvent
import Wetterhorn.Core.W (W (..))
import qualified Wetterhorn.Core.W as W
import Wetterhorn.Foreign.WlRoots (guardNull, wlrKeyboardGetModifiers, wlrSeatGetKeyboard, wlrSeatKeyboardNotifyKey, wlrSeatSetKeyboard)
class InputProxy (spy :: k) where
onKeyEvent :: Proxy spy -> InputEvent -> MaybeT W InputEvent
instance (InputProxy h, InputProxy t) => InputProxy (h ': t) where
onKeyEvent _ = onKeyEvent (Proxy :: Proxy h) <=< onKeyEvent (Proxy :: Proxy t)
instance InputProxy '[] where
onKeyEvent _ = return
data NoProxy
instance InputProxy NoProxy where
onKeyEvent _ = return
instance (InputProxy s1, InputProxy s2) => InputProxy (s1, s2) where
onKeyEvent proxy = onKeyEvent (fmap fst proxy) <=< onKeyEvent (fmap snd proxy)
-- | Union of event types.
data InputEvent
= InputButtonEvent ButtonEvent.ButtonEvent
| InputKeyEvent KeyEvent.KeyEvent
-- | Context for the input.
newtype InputContext spy = InputContext
{ -- | Top of the input routine. Used in "continue" statement.
inputTop :: InputM spy ()
}
newtype InputState spy = InputState
{ inputSource :: InputM spy InputEvent
}
-- | Input monad for handling all kinds of input.
newtype InputM spy a = InputM (ContT () (RWST (InputContext spy) () (InputState spy) W) a)
deriving (Monad, Functor, Applicative, MonadCont, MonadIO)
instance MonadFail (InputM spy) where
fail _ = continue
-- | Lifts a W action to an InputM action.
instance W.Wlike (InputM spy) where
liftW = InputM . lift . lift
-- | Resets the input handler to the top.
continue :: InputM spy a
continue = do
(InputContext {inputTop = (InputM top)}) <- InputM ask
InputM $ shiftT (\_ -> resetT top)
-- | Forwards the given key event to the focused window.
forwardKey :: KeyEvent.KeyEvent -> W ()
forwardKey keyEvent = do
seatPtr <- W.getSeat
W.wio $ do
wlrSeatSetKeyboard
seatPtr
(KeyEvent.device keyEvent)
wlrSeatKeyboardNotifyKey
seatPtr
(KeyEvent.timeMs keyEvent)
(KeyEvent.keycode keyEvent)
( case KeyEvent.state keyEvent of
KeyEvent.KeyReleased -> 0
_ -> 1
)
-- | Executes a function if the input event is a key event. If it is not a key
-- event, then nothing happens.
whenKeyEvent :: (Monad m) => InputEvent -> (KeyEvent.KeyEvent -> m ()) -> m ()
whenKeyEvent (InputKeyEvent ke) = ($ ke)
whenKeyEvent _ = const (return ())
-- | Executes a function in the input event is a button event. If it is not a
-- button event, then nothing happens.
whenButtonEvent ::
(Monad m) => InputEvent -> (ButtonEvent.ButtonEvent -> m ()) -> m ()
whenButtonEvent (InputButtonEvent be) = ($ be)
whenButtonEvent _ = const (return ())
-- | Forwards the given input event to focused window.
forwardEvent :: (W.Wlike m) => InputEvent -> m ()
forwardEvent = \case
InputKeyEvent kv -> W.liftW $ forwardKey kv
InputButtonEvent _ -> return ()
-- | "Unwraps" a maybe. If the maybe is present, the handler proceeds. If the
-- maybe is not present, the handler restarts execution from the top.
unwrap :: Maybe a -> InputM spy a
unwrap (Just val) = return val
unwrap Nothing = continue
-- | Runs the series of events from the top as if they were input.
replayEvents :: [InputEvent] -> InputM spy ()
replayEvents events = do
ioref <- liftIO (newIORef events)
(InputM oldInput) <- InputM $ gets inputSource
let newInput =
InputM $
shiftT
( \thingToDo -> do
r <- liftIO (readIORef ioref)
case r of
[] -> do
modify $ \st -> st {inputSource = InputM oldInput}
a <- oldInput
lift (thingToDo a)
(a : as) -> do
liftIO (writeIORef ioref as)
lift (thingToDo a)
)
InputM $ modify $ \st -> st {inputSource = newInput}
where
delay to act = liftIO (threadDelay to) >> act
-- | Call in the reset handler with the InputM handler you wolud like to use.
useInputHandler :: (InputProxy spy) => InputM spy () -> W ()
useInputHandler (forever -> top@(InputM ctop)) = do
void $ execRWST (runContT ctop return) (InputContext top) (InputState useSeatEvents)
-- | Returns the next input event that's either a kep press or a button press.
nextInputPressEvent :: InputM spy InputEvent
nextInputPressEvent = nextInputEventThat (andM [isPressEvent, not . modifierKey])
modifierKey :: InputEvent -> Bool
modifierKey (InputKeyEvent (KeyEvent.KeyEvent {codepoint = '\NUL'})) = True
modifierKey _ = False
nextInputEventThat :: (InputEvent -> Bool) -> InputM spy InputEvent
nextInputEventThat fn =
nextInputEvent
>>= ( \ie ->
if fn ie
then return ie
else forwardEvent ie >> nextInputEventThat fn
)
isKeyEvent :: InputEvent -> Bool
isKeyEvent (InputKeyEvent _) = True
isKeyEvent _ = False
isPressEvent :: InputEvent -> Bool
isPressEvent (InputButtonEvent be)
| ButtonEvent.state be == ButtonEvent.ButtonPressed =
True
isPressEvent (InputKeyEvent ke)
| KeyEvent.state ke == KeyEvent.KeyPressed =
True
isPressEvent _ = False
-- | Returns the event only if it matches the filter. If it does not match the
-- filter, execution resets to the top.
filterEvent :: (InputEvent -> Bool) -> InputEvent -> InputM spy InputEvent
filterEvent fn ev | fn ev = return ev
filterEvent _ _ = continue
getModifierState :: W Word32
getModifierState = do
seat <- W.getSeat
keyboard <- W.wio $ wlrSeatGetKeyboard seat
maybe (return 0) (W.wio . wlrKeyboardGetModifiers) (guardNull keyboard)
nextInputEvent :: InputM spy InputEvent
nextInputEvent = join $ InputM $ gets inputSource
withProxies :: Proxy spy -> InputM spy a -> InputM spy a
withProxies _ = id
-- | Gets the next input event.
useSeatEvents :: forall spy. (InputProxy spy) => InputM spy InputEvent
useSeatEvents =
InputM $
shiftT
( \thingToDo -> do
putButtonHandler $ \be -> do
runSpies thingToDo (InputButtonEvent be)
putKeyHandler $ \ke -> do
runSpies thingToDo (InputKeyEvent ke)
)
where
runSpies fn ev = do
evM <- lift $ runMaybeT (onKeyEvent (Proxy :: Proxy spy) ev)
mapM_
( \ev' -> do
clearButtonHandler
clearKeyHandler
fn ev'
)
evM
clearButtonHandler =
lift $
modify $ \st ->
st
{ W.currentHooks =
(W.currentHooks st)
{ W.buttonHook = const (return ())
}
}
clearKeyHandler =
lift $
modify $ \st ->
st
{ W.currentHooks =
(W.currentHooks st)
{ W.keyHook = const (return ())
}
}
putButtonHandler h = lift $ do
(r, s) <- (,) <$> ask <*> get
lift $
modify $ \st ->
st
{ W.currentHooks =
(W.currentHooks st)
{ W.buttonHook = \be -> void (execRWST (h be) r s)
}
}
putKeyHandler h = lift $ do
(r, s) <- (,) <$> ask <*> get
lift $
modify $ \st ->
st
{ W.currentHooks =
(W.currentHooks st)
{ W.keyHook = \ke -> void (execRWST (h ke) r s)
}
}
|