@@ -1,4 +1,24 @@
1-- Copyright 2009-2010 Corey O'Connor
1-- Copyright 2009-2010 Corey O'Connor
3+-- There are two input modes:
7+-- Which is better? Well, depends on if you want to enter Unicode as UTF-8 or not.
9+-- * 7 bit mode: UTF-8 can be input unambiguiously. Requires an escape delay to differentiate ESC
10+-- from extended control characters. EG: F1
11+-- * 8 bit mode: UTF-8 cannot be input. Does not require an escape delay.
13+-- By default 7 bit mode is used. If 8 bit moe is requested then vty will request 8 bit mode from
14+-- the terminal. However, if the terminal does not report a capability that uses 8 bit encoding the
15+-- mode will fall back to 7 bit.
17+-- By default, the escape delay is 10000 microseconds. Which is assumed to be well above the
18+-- sampling rate required to detect a keyup for a person typing 200 wpm.
20+-- 0. set raw mode with min == 0 and time == 0
2module Graphics.Vty.Input ( Key(..)
22module Graphics.Vty.Input ( Key(..)
3 , Modifier(..)
23 , Modifier(..)
4 , Button(..)
24 , Button(..)
@@ -13,12 +33,15 @@ import Graphics.Vty.Input.Internal
13import Graphics.Vty.Input.Terminfo
33import Graphics.Vty.Input.Terminfo
15import Data.Char
35import Data.Char
16import Data.Word
37import Data.Word
18import Control.Monad (when, void)
39import Control.Monad (when, void)
19import Control.Concurrent
40import Control.Concurrent
20import Control.Exception
41import Control.Exception
43+import qualified GHC.Event as GHC
22import System.Console.Terminfo
45import System.Console.Terminfo
24import System.Posix.Signals.Exts
47import System.Posix.Signals.Exts
@@ -41,11 +64,12 @@ initTermInput escDelay terminal = do
41 eventChannel <- newChan
64 eventChannel <- newChan
42 inputChannel <- newChan
65 inputChannel <- newChan
43 hadInput <- newEmptyMVar
66 hadInput <- newEmptyMVar
44− oattr <- getTerminalAttributes stdInput
67+ attr <- getTerminalAttributes stdInput
45− let nattr = foldl withoutMode oattr [ StartStopOutput, KeyboardInterrupts
69+ let attr' = foldl withoutMode attr [ StartStopOutput, KeyboardInterrupts
46− , EnableEcho, ProcessInput, ExtendedFunctions
70+ , EnableEcho, ProcessInput, ExtendedFunctions
48− setTerminalAttributes stdInput nattr Immediately
72+ setTerminalAttributes stdInput attr' Immediately
49 set_term_timing
73 set_term_timing
50 let finishAtomicInput = writeChan inputChannel '\xFFFE'
74 let finishAtomicInput = writeChan inputChannel '\xFFFE'
51 inputThread :: IO ()
75 inputThread :: IO ()
@@ -77,8 +101,7 @@ initTermInput escDelay terminal = do
77 threadDelay escDelay -- microseconds
101 threadDelay escDelay -- microseconds
78 hadNoInput <- isEmptyMVar hadInput -- no input yet?
102 hadNoInput <- isEmptyMVar hadInput -- no input yet?
79 -- TODO(corey): there is a race between here and the inputThread.
103 -- TODO(corey): there is a race between here and the inputThread.
81− finishAtomicInput
104+ when hadNoInput finishAtomicInput
84 caps_legacy_table = map_to_legacy_table $ caps_classify_table terminal keys_from_caps_table
107 caps_legacy_table = map_to_legacy_table $ caps_classify_table terminal keys_from_caps_table
@@ -90,7 +113,7 @@ initTermInput escDelay terminal = do
90 noInputThreadId <- forkIO $ noInputThread
113 noInputThreadId <- forkIO $ noInputThread
91 let pokeIO = Catch $ do
114 let pokeIO = Catch $ do
92 let e = error "(getsize in input layer)"
115 let e = error "(getsize in input layer)"
93− setTerminalAttributes stdInput nattr Immediately
116+ setTerminalAttributes stdInput attr' Immediately
94 writeChan eventChannel (EvResize e e)
117 writeChan eventChannel (EvResize e e)
95 _ <- installHandler windowChange pokeIO Nothing
118 _ <- installHandler windowChange pokeIO Nothing
96 _ <- installHandler continueProcess pokeIO Nothing
119 _ <- installHandler continueProcess pokeIO Nothing
@@ -101,6 +124,6 @@ initTermInput escDelay terminal = do
101 killThread noInputThreadId
124 killThread noInputThreadId
102 _ <- installHandler windowChange Ignore Nothing
125 _ <- installHandler windowChange Ignore Nothing
103 _ <- installHandler continueProcess Ignore Nothing
126 _ <- installHandler continueProcess Ignore Nothing
104− setTerminalAttributes stdInput oattr Immediately
127+ setTerminalAttributes stdInput attr Immediately
105 return (readChan eventChannel, uninit)
128 return (readChan eventChannel, uninit)