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
|
module Scanner
( scan
, Scan(..)
) where
import Data.Bits ((.&.), testBit)
import Data.Char (isDigit, ord)
import Data.List (unsnoc)
import System.IO (Handle, hGetChar, hLookAhead, hWaitForInput)
timeout :: Int
timeout = 1
data Scan =
ScanKey String
| ScanMouse
{ mouseButton :: Int
, mousePressed :: Bool -- True == pressed, False == released
, mouseShift :: Bool
, mouseMeta :: Bool
, mouseControl :: Bool
, mouseMotion :: Bool
, mouseX :: Int
, mouseY :: Int
}
deriving Show
-- | Read one key, mouse event, or terminal reply.
--
-- Escape sequences that end early or contain unexpected characters are not
-- an error: what was read of them is returned as a 'ScanKey', and the
-- character that did not fit is left for the next 'scan'.
scan :: Handle -> IO Scan
scan h =
hGetChar h >>= \case
'\ESC' -> scanESC h
c -> return $ ScanKey [c]
scanESC :: Handle -> IO Scan
scanESC h =
hWaitGetChar timeout h >>= \case
Nothing -> return $ ScanKey "\ESC"
Just c
| c == '[' -> -- 05/11
scanCS h
| c == '\ESC' -> -- 01/11
-- XXX M-F1 and other crazy chords may cause
-- \ESC\ESC... on wu, so we just recurse here...
scanESC h
| c == 'O' -> -- 04/15
-- XXX Non-CSI SS3
-- XXX finalByte is maybe calles somehow else here, but it's
-- the same range
ScanKey . reverse <$> zeroOrOne h finalByte ['O','\ESC']
| otherwise ->
return $ ScanKey ['\ESC',c]
scanCS :: Handle -> IO Scan
scanCS h = do
params <- zeroOrMore h parameterByte ['[', '\ESC']
inters <- zeroOrMore h intermediateByte params
s <- reverse <$> zeroOrOne h finalByte inters
case s of
"\ESC[" -> return $ ScanKey "\ESC" -- TODO move this to scanESC
"\ESC[M" -> scanX10MousePayload h
'\ESC' : '[' : '<' : payload | Just m <- sgrMouse payload -> return m
_ -> return $ ScanKey s
scanX10MousePayload :: Handle -> IO Scan
scanX10MousePayload h =
upTo 3 >>= \payload ->
case map ord payload of
[b, x, y] ->
let pressed = (b .&. 3) /= 3 in
return (decodeMouseEvent (b - 32) (x - 32) (y - 32) pressed)
_ ->
return $ ScanKey ("\ESC[M" <> payload)
where
upTo :: Int -> IO [Char]
upTo 0 = return []
upTo n =
hWaitGetChar timeout h >>= \case
Just c -> (c :) <$> upTo (n - 1)
Nothing -> return []
-- | Decode the part of an SGR mouse report after @\\ESC[<@, e.g. @0;10;20M@.
sgrMouse :: String -> Maybe Scan
sgrMouse payload = do
(params, final) <- unsnoc payload
pressed <- case final of
'M' -> Just True
'm' -> Just False
_ -> Nothing
[b, x, y] <- traverse number (split ';' params)
return (decodeMouseEvent b x y pressed)
where
number s | not (null s), all isDigit s = Just (read s)
number _ = Nothing
split c s = case break (== c) s of
(a, _ : rest) -> a : split c rest
(a, []) -> [a]
zeroOrMore :: Handle -> (Char -> Bool) -> [Char] -> IO [Char]
zeroOrMore h p buf =
hWaitLookAhead timeout h >>= \case
Nothing -> return buf
Just c
| p c ->
hGetChar h {-drop c-} >> zeroOrMore h p (c:buf)
| otherwise ->
return buf
zeroOrOne :: Handle -> (Char -> Bool) -> [Char] -> IO [Char]
zeroOrOne h p buf =
hWaitLookAhead timeout h >>= \case
Just c | p c -> hGetChar h >> return (c:buf)
_ -> return buf
parameterByte :: Char -> Bool
parameterByte = between '0' '?' -- 03/00 03/15
intermediateByte :: Char -> Bool
intermediateByte = between ' ' '/' -- 02/00 02/15
finalByte :: Char -> Bool
finalByte = between '@' '~' -- 04/00 07/14
between :: Ord a => a -> a -> (a -> Bool)
between lo hi = \ x -> lo <= x && x <= hi
hWaitGetChar :: Int -> Handle -> IO (Maybe Char)
hWaitGetChar t h = do
ready <- hWaitForInput h t
if ready
then hGetChar h >>= return . Just
else return Nothing
hWaitLookAhead :: Int -> Handle -> IO (Maybe Char)
hWaitLookAhead t h = do
ready <- hWaitForInput h t
if ready
then hLookAhead h >>= return . Just
else return Nothing
decodeMouseEvent :: Int -> Int -> Int -> Bool -> Scan
decodeMouseEvent b x y pressed =
let
button = case (b .&. 192, b .&. 3) of
(0, 3) -> 0 -- X10 release
(0, n) -> n + 1 -- buttons 1 to 3
(64, n) -> n + 4 -- wheel up, down, left, right
(128, n) -> n + 8 -- buttons 8 to 11
(_, n) -> n + 12 -- unassigned
in
ScanMouse
{ mouseButton = button
, mousePressed = pressed
, mouseShift = testBit b 2
, mouseMeta = testBit b 3
, mouseControl = testBit b 4
, mouseMotion = testBit b 5
, mouseX = x
, mouseY = y
}
|