on master help sign in
start refactoring input layer for an easy to understand mapping of input bytes to events
commit4f9fc10169038e0777e9054e81a344c45abb1f33 authorCorey O'Connor <corey@ooyala.com> dateSun 1 Sep 2013, 01:08 (13 years ago) parent2b7c82a
5 files changed+64−45
src/Graphics/Vty.hs+1−1 src/Graphics/Vty/LLInput.hs → src/Graphics/Vty/Input.hs+7−43 src/Graphics/Vty/Input/Data.hs+47 test/VerifyUsingMockInput.hs+7 vty.cabal+2−1
src/Graphics/Vty.hs+1−1
@@ -23,7 +23,7 @@ module Graphics.Vty ( Vty(..)
2323import Graphics.Vty.Terminal
2424import Graphics.Vty.Picture
2525import Graphics.Vty.DisplayRegion
26−import Graphics.Vty.LLInput
26+import Graphics.Vty.Input
2727
2828import Data.IORef
2929
src/Graphics/Vty/LLInput.hs → src/Graphics/Vty/Input.hsrenamed+7−43
@@ -2,14 +2,13 @@
22-- Copyright 2009-2010 Corey O'Connor
33{-# OPTIONS_GHC -Wall #-}
44{-# LANGUAGE ForeignFunctionInterface #-}
5−module Graphics.Vty.LLInput ( Key(..)
6− , Modifier(..)
7− , Button(..)
8− , Event(..)
9− , initTermInput
10− )
5+module Graphics.Vty.Input ( module Graphics.Vty.Input.Data
6+ , initTermInput
7+ )
118 where
129
10+import Graphics.Vty.Input.Data
11+
1312import Data.Char
1413import Data.Maybe ( mapMaybe
1514 )
@@ -36,30 +35,6 @@ import System.Posix.IO ( stdInput
3635
3736import Foreign ( alloca, poke, peek, Ptr )
3837
39−-- |Representations of non-modifier keys.
40−data Key = KEsc | KFun Int | KBackTab | KPrtScr | KPause | KASCII Char | KBS | KIns
41− | KHome | KPageUp | KDel | KEnd | KPageDown | KBegin | KNP5 | KUp | KMenu
42− | KLeft | KDown | KRight | KEnter
43− deriving (Eq,Show,Ord)
44−
45−-- |Modifier keys. Key codes are interpreted such that users are more likely to
46−-- have Meta than Alt; for instance on the PC Linux console, 'MMeta' will
47−-- generally correspond to the physical Alt key.
48−data Modifier = MShift | MCtrl | MMeta | MAlt
49− deriving (Eq,Show,Ord)
50−
51−-- |Mouse buttons. Not yet used.
52−data Button = BLeft | BMiddle | BRight
53− deriving (Eq,Show,Ord)
54−
55−-- |Generic events.
56−data Event = EvKey Key [Modifier] | EvMouse Int Int Button [Modifier]
57− | EvResize Int Int
58− deriving (Eq,Show,Ord)
59−
60−data KClass = Valid Key [Modifier] | Invalid | Prefix | MisPfx Key [Modifier] [Char]
61− deriving(Show)
62−
6338-- | Set up the terminal for input. Returns a function which reads key
6439-- events, and a function for shutting down the terminal access.
6540initTermInput :: Int -> Terminal -> IO (IO Event, IO ())
@@ -148,19 +123,8 @@ initTermInput escDelay terminal = do
148123
149124 classifyTab = compile (caps_classify_table : ansi_classify_table)
150125
151− caps_tabls = [("khome", (KHome, [])),
152− ("kend", (KEnd, [])),
153− ("cbt", (KBackTab, [])),
154− ("kcud1", (KDown, [])),
155− ("kcuu1", (KUp, [])),
156− ("kcuf1", (KRight, [])),
157− ("kcub1", (KLeft, [])),
158−
159− ("kLFT", (KLeft, [MShift])),
160− ("kRIT", (KRight, [MShift]))
161− ]
162−
163− caps_classify_table = [(x,y) | (Just x,y) <- map (first (getCapability terminal . tiGetStr)) $ caps_tabls]
126+ caps_classify_table
127+ = map_to_legacy_table [(x,y) | (Just x,y) <- map (first (getCapability terminal . tiGetStr)) $ caps_table]
164128
165129 ansi_classify_table :: [[([Char], (Key, [Modifier]))]]
166130 ansi_classify_table =
src/Graphics/Vty/Input/Data.hsnew file+47
@@ -0,0 +1,47 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+{-# LANGUAGE ForeignFunctionInterface #-}
3+-- Copyright Corey O'Connor
4+module Graphics.Vty.Input.Data where
5+
6+-- |Representations of non-modifier keys.
7+data Key = KEsc | KFun Int | KBackTab | KPrtScr | KPause | KASCII Char | KBS | KIns
8+ | KHome | KPageUp | KDel | KEnd | KPageDown | KBegin | KNP5 | KUp | KMenu
9+ | KLeft | KDown | KRight | KEnter
10+ deriving (Eq,Show,Ord)
11+
12+-- |Modifier keys. Key codes are interpreted such that users are more likely to
13+-- have Meta than Alt; for instance on the PC Linux console, 'MMeta' will
14+-- generally correspond to the physical Alt key.
15+data Modifier = MShift | MCtrl | MMeta | MAlt
16+ deriving (Eq,Show,Ord)
17+
18+-- |Mouse buttons. Not yet used.
19+data Button = BLeft | BMiddle | BRight
20+ deriving (Eq,Show,Ord)
21+
22+-- |Generic events.
23+data Event = EvKey Key [Modifier] | EvMouse Int Int Button [Modifier]
24+ | EvResize Int Int
25+ deriving (Eq,Show,Ord)
26+
27+data KClass = Valid Key [Modifier] | Invalid | Prefix | MisPfx Key [Modifier] [Char]
28+ deriving(Show)
29+
30+map_to_legacy_table :: [(String, Event)] -> [(String, (Key, [Modifier]))]
31+map_to_legacy_table = map f
32+ where f (s, EvKey k mods) = (s, (k, mods))
33+ f _ = error "no mapping for mouse or resize events"
34+
35+caps_table :: [(String, Event)]
36+caps_table =
37+ [ ("khome", EvKey KHome [])
38+ , ("kend", EvKey KEnd [])
39+ , ("cbt", EvKey KBackTab [])
40+ , ("kcud1", EvKey KDown [])
41+ , ("kcuu1", EvKey KUp [])
42+ , ("kcuf1", EvKey KRight [])
43+ , ("kcub1", EvKey KLeft [])
44+ , ("kLFT", EvKey KLeft [MShift])
45+ , ("kRIT", EvKey KRight [MShift])
46+ ]
47+
test/VerifyUsingMockInput.hs+7
@@ -13,5 +13,12 @@ import Control.Monad
1313import System.Posix.Env
1414import System.IO
1515
16+data InputSpec
17+ = KeySeq String -- | input sequence encoded as a string. Regardless, the input is read a byte at a time.
18+ | Delay Int -- | millisecond delay
19+
20+type ExpectedSpec = [Event]
21+
1622tests :: IO [Test]
1723tests = return []
24+
vty.cabal+2−1
@@ -59,7 +59,8 @@ library
5959 Graphics.Vty.Image
6060 Graphics.Vty.Inline
6161 Graphics.Vty.Inline.Unsafe
62− Graphics.Vty.LLInput
62+ Graphics.Vty.Input
63+ Graphics.Vty.Input.Data
6364 Graphics.Vty.Picture
6465 Graphics.Vty.Terminal
6566 Graphics.Text.Width