@@ -1,8 +1,8 @@
1module Graphics.Vty.Input.Mouse
1module Graphics.Vty.Input.Mouse
2 ( requestSGRMouseEvents
2 ( requestSGRMouseEvents
3 , disableSGRMouseEvents
3 , disableSGRMouseEvents
4− , classifySGRMouseEvent
4+ , isMouseEvent
5− , classifyNormalMouseEvent
5+ , classifyMouseEvent
8import Graphics.Vty.Input.Events
8import Graphics.Vty.Input.Events
@@ -10,6 +10,7 @@ import Graphics.Vty.Input.Classify.Types
11import Control.Monad.Trans.Maybe
11import Control.Monad.Trans.Maybe
12import Control.Monad.State
12import Control.Monad.State
13+import Data.List (isPrefixOf)
13import Data.Maybe (catMaybes)
14import Data.Maybe (catMaybes)
14import Data.Bits ((.&.))
15import Data.Bits ((.&.))
@@ -37,6 +38,22 @@ requestSGRMouseEvents = "\ESC[?1000h\ESC[?1006h"
37disableSGRMouseEvents :: String
38disableSGRMouseEvents :: String
38disableSGRMouseEvents = "\ESC[?1000l\ESC[?1006l"
39disableSGRMouseEvents = "\ESC[?1000l\ESC[?1006l"
41+-- | Does the specified string begin with a mouse event?"
42+isMouseEvent :: String -> Bool
43+isMouseEvent s = isSGREvent s || isNormalEvent s
45+isSGREvent :: String -> Bool
46+isSGREvent = isPrefixOf sgrPrefix
51+isNormalEvent :: String -> Bool
52+isNormalEvent = isPrefixOf normalPrefix
54+normalPrefix :: String
55+normalPrefix = "\ESC[<"
40-- Modifier bits:
57-- Modifier bits:
41shiftBit :: Int
58shiftBit :: Int
42shiftBit = 4
59shiftBit = 4
@@ -63,6 +80,17 @@ rightButton = 2
63hasBitSet :: Int -> Int -> Bool
80hasBitSet :: Int -> Int -> Bool
64hasBitSet val bit = val .&. bit > 0
81hasBitSet val bit = val .&. bit > 0
83+classifyMouseEvent :: String -> KClass
84+classifyMouseEvent s = runParser s $ do
85+ when (not $ isMouseEvent s) failParse
91+ '<' -> classifySGRMouseEvent
92+ 'M' -> classifyNormalMouseEvent
66-- Given a modifer/button value, determine which button was indicated
94-- Given a modifer/button value, determine which button was indicated
67getSGRButton :: Int -> Parser Button
95getSGRButton :: Int -> Parser Button
68getSGRButton mods =
96getSGRButton mods =
@@ -116,8 +144,8 @@ expectChar c = do
116-- Attempt to classify a control sequence as a "normal" mouse event. To
144-- Attempt to classify a control sequence as a "normal" mouse event. To
117-- get here we should have already read "\ESC[M" so that will not be
145-- get here we should have already read "\ESC[M" so that will not be
118-- included in the string to be parsed.
146-- included in the string to be parsed.
119−classifyNormalMouseEvent :: [Char] -> KClass
147+classifyNormalMouseEvent :: Parser Event
120−classifyNormalMouseEvent s = runParser s $ do
148+classifyNormalMouseEvent = do
121 statusChar <- readChar
149 statusChar <- readChar
122 xCoordChar <- readChar
150 xCoordChar <- readChar
123 yCoordChar <- readChar
151 yCoordChar <- readChar
@@ -137,8 +165,8 @@ classifyNormalMouseEvent s = runParser s $ do
137-- Attempt to classify a control sequence as an SGR mouse event. To
165-- Attempt to classify a control sequence as an SGR mouse event. To
138-- get here we should have already read "\ESC[<" so that will not be
166-- get here we should have already read "\ESC[<" so that will not be
139-- included in the string to be parsed.
167-- included in the string to be parsed.
140−classifySGRMouseEvent :: [Char] -> KClass
168+classifySGRMouseEvent :: Parser Event
141−classifySGRMouseEvent s = runParser s $ do
169+classifySGRMouseEvent = do
142 mods <- readInt
170 mods <- readInt
143 expectChar ';'
171 expectChar ';'
144 xCoord <- readInt
172 xCoord <- readInt