@@ -1,8 +1,8 @@
11module Graphics.Vty.Input.Mouse
22 ( requestSGRMouseEvents
33 , disableSGRMouseEvents
4− , classifySGRMouseEvent
5− , classifyNormalMouseEvent
4+ , isMouseEvent
5+ , classifyMouseEvent
66 ) where
77
88import Graphics.Vty.Input.Events
@@ -10,6 +10,7 @@ import Graphics.Vty.Input.Classify.Types
1010
1111import Control.Monad.Trans.Maybe
1212import Control.Monad.State
13+import Data.List (isPrefixOf)
1314import Data.Maybe (catMaybes)
1415import Data.Bits ((.&.))
1516
@@ -37,6 +38,22 @@ requestSGRMouseEvents = "\ESC[?1000h\ESC[?1006h"
3738disableSGRMouseEvents :: String
3839disableSGRMouseEvents = "\ESC[?1000l\ESC[?1006l"
3940
41+-- | Does the specified string begin with a mouse event?"
42+isMouseEvent :: String -> Bool
43+isMouseEvent s = isSGREvent s || isNormalEvent s
44+
45+isSGREvent :: String -> Bool
46+isSGREvent = isPrefixOf sgrPrefix
47+
48+sgrPrefix :: String
49+sgrPrefix = "\ESC[M"
50+
51+isNormalEvent :: String -> Bool
52+isNormalEvent = isPrefixOf normalPrefix
53+
54+normalPrefix :: String
55+normalPrefix = "\ESC[<"
56+
4057-- Modifier bits:
4158shiftBit :: Int
4259shiftBit = 4
@@ -63,6 +80,17 @@ rightButton = 2
6380hasBitSet :: Int -> Int -> Bool
6481hasBitSet val bit = val .&. bit > 0
6582
83+classifyMouseEvent :: String -> KClass
84+classifyMouseEvent s = runParser s $ do
85+ when (not $ isMouseEvent s) failParse
86+
87+ expectChar '\ESC'
88+ expectChar '['
89+ ty <- readChar
90+ case ty of
91+ '<' -> classifySGRMouseEvent
92+ 'M' -> classifyNormalMouseEvent
93+
6694-- Given a modifer/button value, determine which button was indicated
6795getSGRButton :: Int -> Parser Button
6896getSGRButton mods =
@@ -116,8 +144,8 @@ expectChar c = do
116144-- Attempt to classify a control sequence as a "normal" mouse event. To
117145-- get here we should have already read "\ESC[M" so that will not be
118146-- included in the string to be parsed.
119−classifyNormalMouseEvent :: [Char] -> KClass
120−classifyNormalMouseEvent s = runParser s $ do
147+classifyNormalMouseEvent :: Parser Event
148+classifyNormalMouseEvent = do
121149 statusChar <- readChar
122150 xCoordChar <- readChar
123151 yCoordChar <- readChar
@@ -137,8 +165,8 @@ classifyNormalMouseEvent s = runParser s $ do
137165-- Attempt to classify a control sequence as an SGR mouse event. To
138166-- get here we should have already read "\ESC[<" so that will not be
139167-- included in the string to be parsed.
140−classifySGRMouseEvent :: [Char] -> KClass
141−classifySGRMouseEvent s = runParser s $ do
168+classifySGRMouseEvent :: Parser Event
169+classifySGRMouseEvent = do
142170 mods <- readInt
143171 expectChar ';'
144172 xCoord <- readInt