on master help sign in
expand input documentation
commite361a749205ebc143dc93dbdff30d9234759ba95 authorCorey O'Connor <coreyoconnor@gmail.com> dateSun 27 Apr 2014, 11:27 (13 years ago) parent03cc712
2 files changed+110−18
src/Graphics/Vty/Config.hs+8−2 src/Graphics/Vty/Input.hs+102−16
src/Graphics/Vty/Config.hs+8−2
@@ -10,7 +10,9 @@
1010-- Each line of the input config is processed individually. Lines that fail to parse are ignored.
1111-- Later entries take precedence over earlier.
1212--
13−-- * Classify Table Overrides - @map@
13+-- = Classify Table Overrides
14+--
15+-- == @map@
1416--
1517-- Directive format:
1618--
@@ -62,10 +64,14 @@ data Config = Config
6264 { specifiedEscPeriod :: Maybe Int
6365 -- | Debug information is appended to the file.
6466 , debugLog :: Maybe FilePath
67+ -- | The (input byte, output event) pairs override VTY\'s built-in tables as well as terminfo.
68+ --
69+ -- See "Graphics.Vty.Config" module documentation for documentation of the @map@ directive.
6570 , inputOverrides :: ClassifyTable
6671 } deriving (Show, Eq)
6772
68−-- | AKA VTIME. The default is 100000 microseconds or 0.1 seconds.
73+-- | AKA VTIME. The default is 100000 microseconds or 0.1 seconds. Set using the
74+-- 'specifiedEscPeriod' field of 'Config'
6975singleEscPeriod :: Config -> Int
7076singleEscPeriod = maybe 100000 id . specifiedEscPeriod
7177
src/Graphics/Vty/Input.hs+102−16
@@ -1,34 +1,120 @@
11-- | The input layer for VTY. This provides methods for initializing an 'Input' structure which can
22-- then be used to read 'Event's from the terminal.
33--
4−-- Right, brain rot trying to figure this out exactly. So, as far as I can care:
4+-- The history of terminals has resulted in a broken input process. Some keys and combinations will
5+-- not reliably map to the expected events by any terminal program. Even those not using vty. There
6+-- is no 1:1 mapping from key events to bytes read from the terminal input device. In very limited
7+-- cases the terminal and vty's input process can be customized to resolve these issues.
58--
6−-- There are two input modes for control sequences:
9+-- See "Graphics.Vty.Config" for how to configure vty's input processing. Customizing terminfo and
10+-- the terminal is beyond the scope of this documentation.
11+--
12+-- = VTY's Implementation
13+--
14+-- One can get the brain rot trying to understand all this. So, as far as I can care...
15+--
16+-- There are two input modes:
717--
818-- 1. 7 bit
919--
1020-- 2. 8 bit
1121--
12−-- Which should be used depends on the need to enter Unicode as UTF-8 or not.
22+-- 7 bit input is the default and the expected in most use cases. This is what vty uses.
23+--
24+-- == 7 bit input encoding
25+--
26+-- Control key combinations are represented by masking the two high bits of the 7bit input. Back in
27+-- the day the control key actually grounded the two high bit wires: 6 and 7. This is why
28+-- control key combos map to single character events: The input bytes are identical. The input byte
29+-- is the bit encoding of the character with bits 6 and 7 masked. Bit 6 is set by shift. Bit 6 and
30+-- 7 are masked by control. EG:
31+--
32+-- * Control-I is 'i', `01101001`, has bit 6 and 7 masked to become `00001001`. Which is the ASCII
33+-- and UTF-8 encoding of the tab key.
34+--
35+-- * Control+Shift-C is 'C', `01000011`, with bit 6 and 7 set to zero which makes `0000011` and
36+-- is the "End of Text" code.
37+--
38+-- * Hypothesis: This is why capital-A, 'A', has value 65 in ASCII: This is the value 1 with bit 7
39+-- set and 6 unset.
40+--
41+-- * Hypothesis: Bit 6 is unset by upper case letters because, initially, there were only upper case
42+-- letters used and a 5 bit encoding.
43+--
44+-- == 8 bit encoding
45+--
46+-- The 8th bit was originally used for parity checking. Useless for emulators. Some terminal
47+-- emulators support a 8 bit input encoding. While this provides some advantages the actual usage is
48+-- low. Most systems use 7bit mode but recognize 8bit control characters when escaped. This is what
49+-- vty does.
50+--
51+-- == Escaped Control Keys
52+--
53+-- Using 7 bit input encoding the @ESC@ byte can signal the start of an encoded control key. To
54+-- differentiate a single @ESC@ eventfrom a control key the timing of the input is used.
55+--
56+-- 1. @ESC@ individually: @ESC@ byte; no bytes for 'singleEscPeriod'.
1357--
14−-- * 7 bit mode: UTF-8 can be input unambiguiously. The timing of ESC in the input sequence
15−-- determines whether the ESC is: Individual, part of a control key sequence, part of a meta key
16−-- combination.
58+-- 2. control keys that contain @ESC@ in their encoding: The @ESC byte; followed by more bytes read
59+-- within 'singleEscPeriod'. All bytes up until the next valid input block are passed to the
60+-- classifier.
1761--
18−-- * 8 bit mode: UTF-8 cannot be input unambiguously AFAIK. Does not require an escape delay.
62+-- If the current runtime is the threaded runtime then the terminal's @VMIN@ and @VTIME@ behavior
63+-- reliably implement the above rules. If the current runtime does not support forkOS then there is
64+-- currently no implementation.
1965--
20−-- vty assumes 7 bit mode. Which is the default AFAIK.
66+-- Vty used to emulate @VMIN@ and @VTIME@. This was a input loop which did tricky things with
67+-- non-blocking reads and timers. The implementation was not reliable. A reliable implementation is
68+-- possible, but there are no plans to implement this.
69+--
70+-- == Unicode Input and Escaped Control Key Sequences
71+--
72+-- The input encoding determines how UTF-8 encoded characters are recognize.
73+--
74+-- * 7 bit mode: UTF-8 can be input unambiguiously. UTF-8 input is a superset of ASCII. UTF-8 does
75+-- not overlap escaped control key sequences. However, the escape key must be differentiated from
76+-- escaped control key sequences by the timing of the input bytes.
77+--
78+-- * 8 bit mode: UTF-8 cannot be input unambiguously. This does not require using the timing of
79+-- input bytes to differentiate the escape key. Many terminals do not support 8 bit mode.
80+--
81+-- == Terminfo
82+--
83+-- The terminfo system is used to determine how some keys are encoded. Terminfo is incomplete. In
84+-- some cases terminfo is incorrect. Vty assumes terminfo is correct but provides a mechanism to
85+-- override terminfo. See "Graphics.Vty.Config" specifically 'inputOverrides'.
86+--
87+-- == Terminal Input is Broken
88+--
89+-- Clearly terminal input has fundemental issues. There is no easy way to reliably resolve these
90+-- issues.
91+--
92+-- One resolution would be to ditch standard terminal interfaces entirely and just go directly to
93+-- scancodes. A reasonable option for vty if everybody used the linux kernel console. I hear GUIs
94+-- are popular these days. Sadly, GUI terminal emulators don't provide access to scancodes AFAIK.
95+--
96+-- All is lost? Not really. "Graphics.Vty.Config" supports customizing the input byte to event
97+-- mapping and xterm supports customizing the scancode to input byte mapping. With a lot of work a
98+-- user's system can be set up to encode all the key combos in an almost-sane manner.
99+--
100+-- There are other tricky work arounds that can be done. I have no interest in implementing most of
101+-- these. They are not really worth the time.
102+--
103+-- == Terminal Output is Also Broken
104+--
105+-- This isn't the only odd aspect of terminals due to historical aspects that no longer apply. EG:
106+-- Some terminfo capabilities specify millisecond delays. (Capabilities are how terminfo describes
107+-- the control sequence to output red, for instance) This is to account for the slow speed of
108+-- hardcopy teletype interfaces. Cause, uh, we totally still use those.
109+--
110+-- The output encoding of colors and attributes are also rife with issues.
21111--
22−-- 1. ESC individually: ESC byte, no bytes for 'singleEscPeriod'.
112+-- == See also
23113--
24−-- 2. control keys that contain ESC in their byte sequence: ESC byte. Bytes read within
25−-- 'singleEscPeriod' are part of the sequence up until the next valid input block.
114+-- * http://www.leonerd.org.uk/hacks/fixterms/
26115--
27−-- If the current runtime is the threaded runtime then the terminal's VMIN and VTIME parameters can
28−-- be applied to implement the above rules.
29−-- If the current runtime does not support forkOS then there is currently no implementation. Vty
30−-- used to implement a parser which did tricky things with non-blocking reads and timers. This could
31−-- be revived.
116+-- In my experience this cannot resolve the issues without changes to the terminal emulator and
117+-- device.
32118module Graphics.Vty.Input ( Key(..)
33119 , Modifier(..)
34120 , Button(..)