From 03046ee6c7279da313e2e156ced4c350488be5ee Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 02:16:59 +0000 Subject: scan: return what was read instead of throwing scan threw on input it did not expect: an escape sequence that stopped early (Alt-O sends ESC O and nothing else, a slow link splits sequences) or that contained a character outside its syntax. In a program that scans in a thread of its own, that thread died and the program stopped reading the keyboard. Likewise, an X10 mouse report that stopped early blocked until three more characters came, swallowing the keys typed meanwhile. Now every character of a sequence is read with the timeout, and when a sequence stops early or the next character does not fit, what was read so far is returned as a ScanKey. The character that did not fit stays in the handle for the next scan. For example ESC O alone is ScanKey "\ESCO", which is what Alt-O sends anyway, and ESC [ 1 followed by a newline is ScanKey "\ESC[1" followed by ScanKey "\n". SGR mouse reports are CSI sequences whose parameters start with <, so they are read like any other CSI sequence and decoded afterwards. A malformed one is returned as a ScanKey. This drops expectChar and scanNumber. Errors of the handle itself still propagate, such as end of file, or a byte that the handle's encoding cannot decode. --- src/Scanner.hs | 113 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/Scanner.hs b/src/Scanner.hs index 24546be..3383920 100644 --- a/src/Scanner.hs +++ b/src/Scanner.hs @@ -4,7 +4,8 @@ module Scanner ) where import Data.Bits ((.&.), testBit) -import Data.Char (ord) +import Data.Char (isDigit, ord) +import Data.List (unsnoc) import System.IO (Handle, hGetChar, hLookAhead, hWaitForInput) @@ -27,6 +28,11 @@ data Scan = +-- | 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 @@ -46,67 +52,62 @@ scanESC h = -- XXX M-F1 and other crazy chords may cause -- \ESC\ESC... on wu, so we just recurse here... scanESC h - | c == 'O' -> do -- 04/15 + | c == 'O' -> -- 04/15 -- XXX Non-CSI SS3 -- XXX finalByte is maybe calles somehow else here, but it's -- the same range - final <- expectChar h finalByte - return (ScanKey (reverse (final : ['O','\ESC']))) + ScanKey . reverse <$> zeroOrOne h finalByte ['O','\ESC'] | otherwise -> return $ ScanKey ['\ESC',c] scanCS :: Handle -> IO Scan -scanCS h = - hWaitLookAhead timeout h >>= \case - Nothing -> return $ ScanKey "\ESC" -- TODO move this to scanESC - Just c - | c == '<' -> hGetChar h >> scanSGRMousePayload h - | c == 'M' -> hGetChar h >> scanX10MousePayload h - | otherwise -> scanOtherCSISequence h - - -scanX10MousePayload :: Handle -> IO Scan -scanX10MousePayload h = do - b <- ord <$> hGetChar h - x <- ord <$> hGetChar h - y <- ord <$> hGetChar h - let pressed = (b .&. 3) /= 3 - return (decodeMouseEvent (b - 32) (x - 32) (y - 32) pressed) - - -scanSGRMousePayload :: Handle -> IO Scan -scanSGRMousePayload h = do - b <- scanNumber h - _ <- expectChar h (==';') - x <- scanNumber h - _ <- expectChar h (==';') - y <- scanNumber h - m <- hGetChar h -- 'M' or 'm' - let pressed = (m == 'M') - return (decodeMouseEvent b x y pressed) - - -scanOtherCSISequence :: Handle -> IO Scan -scanOtherCSISequence h = do +scanCS h = do params <- zeroOrMore h parameterByte ['[', '\ESC'] inters <- zeroOrMore h intermediateByte params - final <- expectChar h finalByte - return (ScanKey (reverse (final : inters))) + 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 -scanNumber :: Handle -> IO Int -scanNumber h = go 0 +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 - go acc = - hWaitLookAhead timeout h >>= \case - Just c -> - if '0' <= c && c <= '9' - then hGetChar h >> go (acc * 10 + (ord c - ord '0')) - else return acc - - Nothing -> fail "timeout" - + 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] @@ -120,15 +121,11 @@ zeroOrMore h p buf = return buf -expectChar :: Handle -> (Char -> Bool) -> IO Char -expectChar h p = - hWaitGetChar timeout h >>= \case - Just c -> - if p c - then return c - else fail ("unexpected char: " ++ show c) - Nothing -> - fail "timeout" +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 -- cgit v1.3.1