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[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 }