summaryrefslogtreecommitdiffstats
path: root/common/sys/vtkbd.c
blob: 3ea4bed1e73b55e48f83198db4d1ed9cc713b7d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * vtkbd.c
 * Virtual Terminal Keyboard
 *
 * piaip's new re-implementation of xterm/VT100/220/ANSI key input 
 * escape sequence parser for BBS
 *
 * Author: Hung-Te Lin (piaip)
 * Create: Wed Sep 23 15:06:43 CST 2009
 * ---------------------------------------------------------------------------
 * Copyright (c) 2009 Hung-Te Lin <piaip@csie.org>
 * All rights reserved.
 * Distributed under BSD license (GPL compatible).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * ---------------------------------------------------------------------------
 * References:
 *  http://support.dell.com/support/edocs/systems/pe2650/en/ug/5g387ad0.htm
 *  http://aperiodic.net/phil/archives/Geekery/term-function-keys.html
 *  http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/rzaiw/rzaiwvt220opmode.htm
 *  http://www.rebol.com/docs/core23/rebolcore-18.html
 *  http://ascii-table.com/ansi-escape-sequences-vt-100.php
 *  http://web.mit.edu/gnu/doc/html/screen_10.html
 *  http://vt100.net/docs/vt220-rm/chapter3.html
 *  http://inwap.com/pdp10/ansicode.txt
 *  http://www.connectrf.com/Documents/vt220.html
 *  http://www.ibb.net/~anne/keyboard.html
 *  http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
 *  http://invisible-island.net/xterm/xterm.faq.html
 *  http://www.vim.org/htmldoc/term.html
 *  http://wiki.archlinux.org/index.php/Why_don%27t_my_Home_and_End_keys_work_in_terminals%3F
 *  http://yz.kiev.ua/www/etc/putty/Section3.5.html
 *  PuTTY Source < terminal.c, term_key() >
 *  Termcap
 * ---------------------------------------------------------------------------
 * * BS/DEL Rules
 *   The BackSpace, Erase(<X]), and Delete keys are special due to history...
 *    - on vt220,       BS=0x7F, Delete=ESC[3~  (screen/xterm/PuTTY/PCMan)
 *    - on vt100,       BS=0x7F
 *    - on vt100/xterm, BS=0x08, Delete=0x7F    (VMX/Windows/DOS telnet/KKMan)
 *   So we define 
 *      KEY_BS  = BACKSPACE/ERASE = 0x08, 0x7F
 *      KEY_DEL = DELETE          = ESC[3~
 *
 * * CR/LF  Rules
 *   The traditional newline maps to following combination...
 *   - UNIX, LF
 *   - Win,  CR+LF
 *   - Mac,  CR
 *   When it comes to terminal, most terminal sends CR or CR+LF for ENTER key.
 *   To simply processing, we treat CR+LF as single key stroke (CR).
 *   There were reports that some users getting double ENTERs if we take LF
 *   as ENTER, so we decided reject LF. We are not sure if there is any 
 *   clients sending LF only, but according the the compatibility test, 
 *   most modern clients send only CR (or CR+LF).
 *   So we define
 *      KEY_CR  = CR, CR+LF
 *      KEY_LF  = ignored.
 *
 * * Editing Keys (Home/End/Ins/Del/PgUp/PgDn, Find/Select/Ins/Prev/Remove/Next):
 *   Some old terminals use location mapping instead of mnemonic mapping:
 *     http://invisible-island.net/xterm/xterm.faq.html#xterm_keypad
 *   Well.... I decide to follow the mnemonic mapping. Unfortunately some
 *   terminals (may include gnome-terminal, as I've heard) may get into trouble.
 * ---------------------------------------------------------------------------
 * * The complete list to support:
 *   - Up/Down/Right/Left:          <Esc> [ <A-D>       | <Esc> O <A-D> (app)
 *   - Home/Ins/Del/End/PgUp/PgDn:  <Esc> [ <1~6> ~
 *   - Shift-TAB:                   <Esc> [ Z           | <Esc> [ 0 Z
 *   - F1~F4:                       <Esc> [ 1 <1234> ~  | <Esc> O <PQRS>  
 *   - F5:                          <Esc> [ 1 <5> ~ 
 *   - F6-F8:                       <Esc> [ 1 <789> ~
 *   - F9-F12:                      <Esc> [ 2 <0134> ~
 *   - Num 0-9 *+,-./=ENTER:        <Esc> O <pqrstuvwxyjklmnoXM>
 *   - (SCO) End/PgDn/Home/PgUp/Ins <Esc> [ <FGHIL>
 *   - (SCO) Del                    <0x7F>
 *   - (Xterm) HOME/END             <Esc> <[O> <HF>
     - (rxvt)  HOME/END             <Esc> [ <78> ~
 *   - (putty-rxvt) HOME            <Esc> [ H
 *   - (putty-rxvt) END             <Esc> O w
 *   - (Old Term?) Home/Ins/Del/End/PgUp/PgDn:  <Esc> [ <214536> ~  // not supported
 *
 *   Note: we don't support some rare terms like <Esc> O <TUVWXYZA> described 
 *   in Dell 2650 in order to prevent confusion. 
 *   Num pad is also always converted to digits.
 */

#include <assert.h>
#include <string.h>
#include "vtkbd.h"

/* VtkbdCtx.state */
typedef enum {
    VKSTATE_NORMAL = 0,
    VKSTATE_ESC,        // <Esc>
    VKSTATE_ESC_APP,    // <Esc> O
    VKSTATE_ESC_QUOTE,  // <Esc> [
    VKSTATE_ZERO,       // <Esc> [ 0    (wait Z)
    VKSTATE_ONE,        // <Esc> [ <1> 
    VKSTATE_TWO,        // <Esc> [ <2> 
    VKSTATE_TLIDE,      // <Esc> [ *    (wait ~, return esc_arg)
}   VKSTATES;

#define VKRAW_BS    0x08    // \b = Ctrl('H')
#define VKRAW_ERASE 0x7F    // <X]

/* the processor API */
int 
vtkbd_process(int c, VtkbdCtx *ctx)
{
    switch (ctx->state)
    {
        case VKSTATE_NORMAL:    // original state
            if (c == KEY_ESC)
            {
                ctx->state = VKSTATE_ESC;
                return KEY_INCOMPLETE;
            }

            // simple mappings
            switch (c) {
                // BS/ERASE/DEL Rules
                case VKRAW_BS:
                case VKRAW_ERASE:
                    return KEY_BS;
            }
            return c;

        case VKSTATE_ESC:       // <Esc>
            switch (c) {
                case '[':
                    ctx->state = VKSTATE_ESC_QUOTE;
                    return KEY_INCOMPLETE;

                case 'O':
                    ctx->state = VKSTATE_ESC_APP;
                    return KEY_INCOMPLETE;

                case '0':
                    ctx->state = VKSTATE_ZERO;
                    return KEY_INCOMPLETE;
            }

            // XXX should we map this into another section of KEY_ESC_* ?
            ctx->esc_arg = c;
            ctx->state = VKSTATE_NORMAL;
            return KEY_ESC;

        case VKSTATE_ZERO:  // <Esc> 0
            if (c != 'Z')
                break;

            ctx->state = VKSTATE_NORMAL;
            return KEY_STAB;

        case VKSTATE_ESC_APP:   // <Esc> O

            switch (c) {
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_UP + (c - 'A');

                    // SCO
                case 'H':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_HOME;
                case 'F':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_END;
                case 'G':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_PGDN;
                case 'I':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_PGUP;
                case 'L':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_INS;

                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_F1 + (c - 'P');

                    // rxvt style DELETE
                case 'w':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_DEL;

                    // Num pads: was always converted to NumLock=ON
                    // However we let 'w' map to DEL..
                    // XXX the 'w' may be used as Delete...
                case 'p': case 'q': case 'r': case 's':
                case 't': case 'u': case 'v': 
                // case 'w':
                case 'x': case 'y':
                    ctx->state = VKSTATE_NORMAL;
                    return '0' + (c - 'p');

                case 'M':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_ENTER;

                case 'X':
                    ctx->state = VKSTATE_NORMAL;
                    return '=';

                case 'j': case 'k': case 'l': case 'm': 
                case 'n': case 'o':
                    {
                        static const char *numx = "*+,-./";
                        assert( c >= 'j' && (c-'j') < strlen(numx));
                        ctx->state = VKSTATE_NORMAL;
                        return numx[c-'j'];
                    }
            }
            break;

        case VKSTATE_ESC_QUOTE:     // <Esc> [

            switch(c) {
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_UP + (c - 'A');

                    // SCO
                case 'H':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_HOME;
                case 'F':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_END;
                case 'G':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_PGDN;
                case 'I':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_PGUP;
                case 'L':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_INS;

                case '3':
                case '4':
                case '5':
                case '6':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_DEL + (c - '3');
                    return KEY_INCOMPLETE;

                case '7':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_HOME;
                    return KEY_INCOMPLETE;
                case '8':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_END;
                    return KEY_INCOMPLETE;

                case 'Z':
                    ctx->state = VKSTATE_NORMAL;
                    return KEY_STAB;

                case '1':
                    ctx->state = VKSTATE_ONE;
                    return KEY_INCOMPLETE;
                case '2':
                    ctx->state = VKSTATE_TWO;
                    return KEY_INCOMPLETE;
            }
            break;

        case VKSTATE_ONE:   // <Esc> [ 1
            if (c == '~')
            {
                ctx->state = VKSTATE_NORMAL;
                return KEY_HOME;
            }

            switch(c) {
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_F1 + c - '1'; // F1 .. F5
                    return KEY_INCOMPLETE;

                case '7':
                case '8':
                case '9':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_F6 + c - '7'; // F6 .. F8
                    return KEY_INCOMPLETE;
            }
            break;

        case VKSTATE_TWO:   // <Esc> [ 2
            if (c == '~')
            {
                ctx->state = VKSTATE_NORMAL;
                return KEY_INS;         // HOME+1
            }

            switch(c) {
                case '0':
                case '1':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_F9 + c - '0'; // F9 .. F10
                    return KEY_INCOMPLETE;

                case '3':
                case '4':
                    ctx->state = VKSTATE_TLIDE;
                    ctx->esc_arg = KEY_F11 + c - '3'; // F11 .. F12
                    return KEY_INCOMPLETE;
            }
            break;

        case VKSTATE_TLIDE:   // Esc [ <12> <0-9> ~
            if (c != '~')
                break;

            ctx->state = VKSTATE_NORMAL;
            return ctx->esc_arg;

        default:
            assert(!"unknown vkstate");
            break;
    }

    // what to do now?
    ctx->state = VKSTATE_NORMAL;
    return KEY_UNKNOWN;
}

ssize_t 
vtkbd_ignore_dbcs_evil_repeats(const unsigned char *buf, ssize_t len)
{
    // determine DBCS repeats by evil clients 
    // NOTE: this is usually invoked before vtkbd_process,
    // so we have to deal with the raw sequence.
    if (len == 2)
    {
    // XXX len==2 is dangerous. hope we are not in telnet IAC state...
    if (buf[0] != buf[1])
        return len;

        // targest here:
        //  - VKRAW_BS
        //  - VKRAW_ERASE
        //  - Ctrl('D')     (KKMan3 also treats Ctrl('D') as DBCS DEL)
    if (buf[0] == VKRAW_BS ||
        buf[0] == VKRAW_ERASE ||
        buf[0] == Ctrl('D'))
        return len/2;
    } 
    else if (len == 6)
    {
    // RIGHT:   KEY_ESC "OC" or KEY_ESC "[C"
    // LEFT:    KEY_ESC "OD" or KEY_ESC "[D"
    if (buf[2] != 'C' && buf[2] != 'D')
        return len;

    if ( buf[0] == KEY_ESC &&
        (buf[1] == '[' || buf[1] == 'O') &&
         buf[0] == buf[3] &&
         buf[1] == buf[4] &&
         buf[2] == buf[5])
        return len/2;
    } 
    else if (len == 8)
    {
    // DEL:     ESC_STR "[3~" // vt220
    if (buf[0] != KEY_ESC ||
            buf[2] != '3' ||
        buf[1] != '[' ||
        buf[3] != '~')
            return len;

        if( buf[4] == buf[0] &&
        buf[5] == buf[1] &&
        buf[6] == buf[2] &&
        buf[7] == buf[3])
        return len/2;
    }
    return len;
}

// vim:sw=4:sw=4:et