aboutsummaryrefslogtreecommitdiff
path: root/driver-csi.c
blob: bb4b0004084d6538e7eab3286992fb11ed84f5b2 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#include "termo.h"
#include "termo-internal.h"

#include <stdio.h>
#include <string.h>

// There are 64 basic codes 0x40 - 0x7F that end a key sequence,
// plus 0x24 ($) for shifted keys in rxvt-based terminals.

// The CSI/SS3 naming isn't completely appropriate, as that only really applies
// to the other direction of communication, that is from the application
// to the terminal.  What the terminal sends back doesn't have to conform to
// ECMA-48 (and indeed doesn't, as rxvt's 0x24 ($) is out of the range).

static int keyinfo_initialised = 0;
static struct keyinfo ss3s[96];
static char ss3_kpalts[96];

typedef struct
{
	termo_t *tk;
}
termo_csi_t;

typedef termo_result_t (*csi_handler_fn)
	(termo_t *tk, termo_key_t *key, int cmd, long *arg, int args);
static csi_handler_fn csi_handlers[96];

//
// Handler for CSI/SS3 cmd keys
//

static struct keyinfo csi_ss3s[96];

static termo_result_t
handle_csi_ss3_full (termo_t *tk,
	termo_key_t *key, int cmd, long *arg, int args)
{
	(void) tk;

	if (args > 1 && arg[1] != -1)
		key->modifiers = arg[1] - 1;
	else
		key->modifiers = 0;

	key->type       =   csi_ss3s[cmd - 0x20].type;
	key->code.sym   =   csi_ss3s[cmd - 0x20].sym;
	key->modifiers &= ~(csi_ss3s[cmd - 0x20].modifier_mask);
	key->modifiers |=   csi_ss3s[cmd - 0x20].modifier_set;

	if (key->code.sym == TERMO_SYM_UNKNOWN)
		return TERMO_RES_NONE;
	return TERMO_RES_KEY;
}

static void
register_csi_ss3_full (termo_type_t type, termo_sym_t sym,
	int modifier_set, int modifier_mask, unsigned char cmd)
{
	if (cmd < 0x20 || cmd >= 0x80)
		return;

	csi_ss3s[cmd - 0x20].type          = type;
	csi_ss3s[cmd - 0x20].sym           = sym;
	csi_ss3s[cmd - 0x20].modifier_set  = modifier_set;
	csi_ss3s[cmd - 0x20].modifier_mask = modifier_mask;

	csi_handlers[cmd - 0x20] = &handle_csi_ss3_full;
}

static void
register_csi_ss3 (termo_type_t type, termo_sym_t sym, unsigned char cmd)
{
	register_csi_ss3_full (type, sym, 0, 0, cmd);
}

//
// Handler for regular cursor key codes
//

static termo_result_t
handle_csi_cursor (termo_t *tk,
	termo_key_t *key, int cmd, long *arg, int args)
{
	(void) tk;

	// CSI arrow keys without arguments are usually Ctrl-modified,
	// and if not, as is the case with urxvt, they're specified in terminfo.
	// In addition to that, xterm can specify modifiers in an argument.
	key->type = TERMO_TYPE_KEYSYM;
	if (args > 1 && arg[1] != -1)
		key->modifiers = arg[1] - 1;
	else
		key->modifiers = TERMO_KEYMOD_CTRL;

	if (cmd == 'A') key->code.sym = TERMO_SYM_UP;
	if (cmd == 'B') key->code.sym = TERMO_SYM_DOWN;
	if (cmd == 'C') key->code.sym = TERMO_SYM_RIGHT;
	if (cmd == 'D') key->code.sym = TERMO_SYM_LEFT;
	return TERMO_RES_KEY;
}

//
// Handler for rxvt special cursor key codes
//

static termo_result_t
handle_csi_cursor_rxvt (termo_t *tk,
	termo_key_t *key, int cmd, long *arg, int args)
{
	(void) tk;
	(void) arg;
	(void) args;

	// CSI with small letter stands for Shift
	// SS3 with small letter stands for Ctrl but we don't handle that here
	key->type = TERMO_TYPE_KEYSYM;
	key->modifiers = TERMO_KEYMOD_SHIFT;

	if (cmd == 'a') key->code.sym = TERMO_SYM_UP;
	if (cmd == 'b') key->code.sym = TERMO_SYM_DOWN;
	if (cmd == 'c') key->code.sym = TERMO_SYM_RIGHT;
	if (cmd == 'd') key->code.sym = TERMO_SYM_LEFT;
	return TERMO_RES_KEY;
}

//
// Handler for rxvt SS3-only key combinations
//

static void
register_ss3 (termo_type_t type, termo_sym_t sym,
	int modifier_set, unsigned char cmd)
{
	if (cmd < 0x20 || cmd >= 0x80)
		return;

	ss3s[cmd - 0x20].type          = type;
	ss3s[cmd - 0x20].sym           = sym;
	ss3s[cmd - 0x20].modifier_set  = modifier_set;
	ss3s[cmd - 0x20].modifier_mask = 0;
}

//
// Handler for SS3 keys with kpad alternate representations
//

static void
register_ss3kpalt (termo_type_t type, termo_sym_t sym,
	unsigned char cmd, char kpalt)
{
	if (cmd < 0x20 || cmd >= 0x80)
		return;

	ss3s[cmd - 0x20].type          = type;
	ss3s[cmd - 0x20].sym           = sym;
	ss3s[cmd - 0x20].modifier_set  = 0;
	ss3s[cmd - 0x20].modifier_mask = 0;

	ss3_kpalts[cmd - 0x20] = kpalt;
}

//
// Handler for CSI number ~ function keys
//

// This value must be increased if more CSI function keys are added
static struct keyinfo csifuncs[35];
#define NCSIFUNCS ((long) (sizeof csifuncs / sizeof csifuncs[0]))

static termo_result_t
handle_csifunc (termo_t *tk, termo_key_t *key, int cmd, long *arg, int args)
{
	(void) cmd;

	if (args > 1 && arg[1] != -1)
		key->modifiers = arg[1] - 1;
	else
		key->modifiers = 0;
	key->type = TERMO_TYPE_KEYSYM;

	if (arg[0] == 27)
	{
		int mod = key->modifiers;
		(*tk->method.emit_codepoint) (tk, arg[2], key);
		key->modifiers |= mod;
	}
	else if (arg[0] >= 0 && arg[0] < NCSIFUNCS)
	{
		key->type       =   csifuncs[arg[0]].type;
		key->code.sym   =   csifuncs[arg[0]].sym;
		key->modifiers &= ~(csifuncs[arg[0]].modifier_mask);
		key->modifiers |=   csifuncs[arg[0]].modifier_set;
	}
	else
		key->code.sym = TERMO_SYM_UNKNOWN;

	if (key->code.sym == TERMO_SYM_UNKNOWN)
	{
#ifdef DEBUG
		fprintf (stderr, "CSI: Unknown function key %ld\n", arg[0]);
#endif
		return TERMO_RES_NONE;
	}

	return TERMO_RES_KEY;
}

static void
register_csifunc (termo_type_t type, termo_sym_t sym, int number)
{
	if (number >= NCSIFUNCS)
		return;

	csifuncs[number].type          = type;
	csifuncs[number].sym           = sym;
	csifuncs[number].modifier_set  = 0;
	csifuncs[number].modifier_mask = 0;

	csi_handlers['~' - 0x20] = &handle_csifunc;
}

//
// rxvt seems to emit these instead of ~ when holding various modifiers
//

static termo_result_t
handle_csi_rxvt (termo_t *tk,
	termo_key_t *key, int cmd, long *arg, int args)
{
	termo_result_t res;
	switch (cmd)
	{
	case '^':
		res = handle_csifunc (tk, key, cmd, arg, args);
		if (res == TERMO_RES_KEY)
			key->modifiers |= TERMO_KEYMOD_CTRL;
		return res;
	case '$':
		res = handle_csifunc (tk, key, cmd, arg, args);
		if (res == TERMO_RES_KEY)
			key->modifiers |= TERMO_KEYMOD_SHIFT;
		return res;
	case '@':
		res = handle_csifunc (tk, key, cmd, arg, args);
		if (res == TERMO_RES_KEY)
			key->modifiers |= TERMO_KEYMOD_CTRL | TERMO_KEYMOD_SHIFT;
		return res;
	default:
		return TERMO_RES_NONE;
	}
}

//
// Handler for CSI u extended Unicode keys
//

static termo_result_t
handle_csi_u (termo_t *tk, termo_key_t *key, int cmd, long *arg, int args)
{
	switch (cmd)
	{
	case 'u':
	{
		if (args > 1 && arg[1] != -1)
			key->modifiers = arg[1] - 1;
		else
			key->modifiers = 0;

		int mod = key->modifiers;
		key->type = TERMO_TYPE_KEYSYM;
		(*tk->method.emit_codepoint) (tk, arg[0], key);
		key->modifiers |= mod;
		return TERMO_RES_KEY;
	}
	default:
		return TERMO_RES_NONE;
	}
}

//
// Handler for CSI M / CSI m mouse events in SGR and rxvt encodings
// Note: This does not handle X10 encoding
//

static termo_result_t
handle_csi_m (termo_t *tk, termo_key_t *key, int cmd, long *arg, int args)
{
	(void) tk;

	int initial = cmd >> 8;
	cmd &= 0xff;

	switch (cmd)
	{
	case 'M':
	case 'm':
		break;
	default:
		return TERMO_RES_NONE;
	}

	if (!initial && args >= 3)
	{
		// rxvt protocol
		key->type = TERMO_TYPE_MOUSE;
		key->code.mouse.info = arg[0] - 0x20;

		key->modifiers = (key->code.mouse.info & 0x1c) >> 2;
		key->code.mouse.info &= ~0x1c;

		termo_key_set_linecol (key, arg[2] - 1, arg[1] - 1);
		return TERMO_RES_KEY;
	}

	if (initial == '<' && args >= 3)
	{
		// SGR protocol
		key->type = TERMO_TYPE_MOUSE;
		key->code.mouse.info = arg[0];

		key->modifiers = (key->code.mouse.info & 0x1c) >> 2;
		key->code.mouse.info &= ~0x1c;

		termo_key_set_linecol (key, arg[2] - 1, arg[1] - 1);

		if (cmd == 'm')  // release
			key->code.mouse.info |= 0x8000;
		return TERMO_RES_KEY;
	}
	return TERMO_RES_NONE;
}

//
// Handler for CSI I / CSI O focus events
//

static termo_result_t
handle_csi_IO (termo_t *tk, termo_key_t *key, int cmd, long *arg, int args)
{
	(void) tk;
	(void) arg;
	(void) args;

	switch (cmd &= 0xff)
	{
	case 'I':
		key->type = TERMO_TYPE_FOCUS;
		key->code.focused = true;
		return TERMO_RES_KEY;
	case 'O':
		key->type = TERMO_TYPE_FOCUS;
		key->code.focused = false;
		return TERMO_RES_KEY;
	default:
		return TERMO_RES_NONE;
	}
	return TERMO_RES_NONE;
}

termo_result_t
termo_interpret_mouse (termo_t *tk, const termo_key_t *key,
	termo_mouse_event_t *event, int *button, int *line, int *col)
{
	(void) tk;

	if (key->type != TERMO_TYPE_MOUSE)
		return TERMO_RES_NONE;

	int btn = 0;
	int code = key->code.mouse.info;
	int drag = code & 0x20;
	code &= ~0x3c;

	termo_mouse_event_t ev;
	switch (code)
	{
	case 0:
	case 1:
	case 2:
		ev = drag ? TERMO_MOUSE_DRAG : TERMO_MOUSE_PRESS;
		btn = code + 1;
		break;

	case 3:
		ev = TERMO_MOUSE_RELEASE;
		// no button hint
		break;

	case 64:
	case 65:
		ev = drag ? TERMO_MOUSE_DRAG : TERMO_MOUSE_PRESS;
		btn = code + 4 - 64;
		break;

	default:
		ev = TERMO_MOUSE_UNKNOWN;
	}

	if (key->code.mouse.info & 0x8000)
		ev = TERMO_MOUSE_RELEASE;

	if (event)
		*event = ev;
	if (button)
		*button = btn;

	termo_key_get_linecol (key, line, col);
	return TERMO_RES_KEY;
}

//
// Handler for CSI ? R position reports
// A plain CSI R with no arguments is probably actually <F3>
//

static termo_result_t
handle_csi_R (termo_t *tk, termo_key_t *key, int cmd, long *arg, int args)
{
	switch (cmd)
	{
	case 'R' | '?' << 8:
		if (args < 2)
			return TERMO_RES_NONE;

		key->type = TERMO_TYPE_POSITION;
		termo_key_set_linecol (key, arg[0] - 1, arg[1] - 1);
		return TERMO_RES_KEY;

	default:
		return handle_csi_ss3_full (tk, key, cmd, arg, args);
	}
}

termo_result_t
termo_interpret_position (termo_t *tk,
	const termo_key_t *key, int *line, int *col)
{
	(void) tk;

	if (key->type != TERMO_TYPE_POSITION)
		return TERMO_RES_NONE;

	termo_key_get_linecol (key, line, col);
	return TERMO_RES_KEY;
}

//
// Handler for CSI $y mode status reports
//

static termo_result_t
handle_csi_y (termo_t *tk, termo_key_t *key, int cmd, long *arg, int args)
{
	(void) tk;

	switch (cmd)
	{
	case 'y' | '$' << 16:
	case 'y' | '$' << 16 | '?' << 8:
		if (args < 2)
			return TERMO_RES_NONE;

		key->type = TERMO_TYPE_MODEREPORT;
		key->code.mode.initial = (cmd >> 8);
		key->code.mode.mode = arg[0];
		key->code.mode.value = arg[1];
		return TERMO_RES_KEY;

	default:
		return TERMO_RES_NONE;
	}
}

termo_result_t
termo_interpret_modereport (termo_t *tk,
	const termo_key_t *key, int *initial, int *mode, int *value)
{
	(void) tk;

	if (key->type != TERMO_TYPE_MODEREPORT)
		return TERMO_RES_NONE;

	if (initial)
		*initial = key->code.mode.initial;
	if (mode)
		*mode = key->code.mode.mode;
	if (value)
		*value = key->code.mode.value;
	return TERMO_RES_KEY;
}

#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])

static termo_result_t
parse_csi (termo_t *tk, size_t introlen, size_t *csi_len,
	long args[], size_t *nargs, unsigned long *commandp)
{
	// Specifically allowing the rxvt special character for shifted function
	// keys to end a CSI-like sequence, otherwise expecting ECMA-48-like input
	bool allow_dollar = true;

	size_t csi_end = introlen;
	while (csi_end < tk->buffcount)
	{
		unsigned char c = CHARAT (csi_end);
		if ((c >= 0x40 && c < 0x80) || (allow_dollar && c == '$'))
			break;

		// However just accepting the dollar as an end character would break
		// parsing DECRPM responses (mode reports).  We can work around this
		// ambiguity by making use of the fact that rxvt key sequences have
		// exactly one numeric argument and no initial byte.
		if (c < '0' || c > '9')
			allow_dollar = false;

		csi_end++;
	}

	if (csi_end >= tk->buffcount)
		return TERMO_RES_AGAIN;

	unsigned char cmd = CHARAT (csi_end);
	*commandp = cmd;

	char present = 0;
	int argi = 0;

	size_t p = introlen;

	// See if there is an initial byte
	if (CHARAT (p) >= '<' && CHARAT (p) <= '?')
	{
		*commandp |= (CHARAT (p) << 8);
		p++;
	}

	// Now attempt to parse out up number;number;... separated values
	for (; p < csi_end; p++)
	{
		unsigned char c = CHARAT (p);
		if (c >= '0' && c <= '9')
		{
			if (!present)
			{
				args[argi] = c - '0';
				present = 1;
			}
			else
				args[argi] = (args[argi] * 10) + c - '0';
		}
		else if (c == ';')
		{
			if (!present)
				args[argi] = -1;
			present = 0;
			argi++;

			if (argi > 16)
				break;
		}
		else if (c >= 0x20 && c <= 0x2f)
		{
			*commandp |= c << 16;
			break;
		}
	}

	if (present)
		argi++;

	*nargs = argi;
	*csi_len = csi_end + 1;
	return TERMO_RES_KEY;
}

termo_result_t
termo_interpret_csi (termo_t *tk, const termo_key_t *key,
	long args[], size_t *nargs, unsigned long *cmd)
{
	if (tk->hightide == 0)
		return TERMO_RES_NONE;
	if (key->type != TERMO_TYPE_UNKNOWN_CSI)
		return TERMO_RES_NONE;

	size_t dummy;
	return parse_csi (tk, 0, &dummy, args, nargs, cmd);
}

static int
register_keys (void)
{
	int i;
	for (i = 0; i < 96; i++)
	{
		csi_ss3s[i].sym = TERMO_SYM_UNKNOWN;
		ss3s[i].sym     = TERMO_SYM_UNKNOWN;
		ss3_kpalts[i] = 0;
	}
	for (i = 0; i < NCSIFUNCS; i++)
		csifuncs[i].sym = TERMO_SYM_UNKNOWN;

	// Cursor keys handling; there's some weird, weird stuff going on here:
	//
	// rxvt-based terminals should output SS3 A/B/C/D for C-S-arrow keys;
	// rxvt-unicode however only seems to output Shift codes.
	//
	// xterm, PuTTY, tmux output SS3 A/B/C/D for normal arrow keys when
	// terminfo start string has been written -- it gets eaten by the terminfo
	// driver then, usually.  To the contrary, CSI A/B/C/D is used for Ctrl.

	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_UP,    0, 'A');
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_DOWN,  0, 'B');
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_RIGHT, 0, 'C');
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_LEFT,  0, 'D');

	csi_handlers['A' - 0x20] = &handle_csi_cursor;
	csi_handlers['B' - 0x20] = &handle_csi_cursor;
	csi_handlers['C' - 0x20] = &handle_csi_cursor;
	csi_handlers['D' - 0x20] = &handle_csi_cursor;

	// Handle Shift-modified rxvt cursor keys (CSI a, CSI b, CSI c, CSI d)
	csi_handlers['a' - 0x20] = &handle_csi_cursor_rxvt;
	csi_handlers['b' - 0x20] = &handle_csi_cursor_rxvt;
	csi_handlers['c' - 0x20] = &handle_csi_cursor_rxvt;
	csi_handlers['d' - 0x20] = &handle_csi_cursor_rxvt;

	// Handle Ctrl-modified rxvt cursor keys (SS3 a, SS3 b, SS3 c, SS3 d)
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_UP,    TERMO_KEYMOD_CTRL, 'a');
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_DOWN,  TERMO_KEYMOD_CTRL, 'b');
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_RIGHT, TERMO_KEYMOD_CTRL, 'c');
	register_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_LEFT,  TERMO_KEYMOD_CTRL, 'd');

	// End of cursor keys handling

	register_csi_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_BEGIN, 'E');
	register_csi_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_END,   'F');
	register_csi_ss3 (TERMO_TYPE_KEYSYM, TERMO_SYM_HOME,  'H');
	register_csi_ss3 (TERMO_TYPE_FUNCTION, 1, 'P');
	register_csi_ss3 (TERMO_TYPE_FUNCTION, 2, 'Q');
	register_csi_ss3 (TERMO_TYPE_FUNCTION, 3, 'R');
	register_csi_ss3 (TERMO_TYPE_FUNCTION, 4, 'S');

	register_csi_ss3_full (TERMO_TYPE_KEYSYM, TERMO_SYM_TAB,
		TERMO_KEYMOD_SHIFT, TERMO_KEYMOD_SHIFT, 'Z');

	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPENTER,  'M', 0);
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPEQUALS, 'X', '=');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPMULT,   'j', '*');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPPLUS,   'k', '+');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPCOMMA,  'l', ',');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPMINUS,  'm', '-');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPPERIOD, 'n', '.');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KPDIV,    'o', '/');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP0,      'p', '0');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP1,      'q', '1');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP2,      'r', '2');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP3,      's', '3');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP4,      't', '4');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP5,      'u', '5');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP6,      'v', '6');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP7,      'w', '7');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP8,      'x', '8');
	register_ss3kpalt (TERMO_TYPE_KEYSYM, TERMO_SYM_KP9,      'y', '9');

	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_FIND,      1);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_INSERT,    2);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_DELETE,    3);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_SELECT,    4);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_PAGEUP,    5);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_PAGEDOWN,  6);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_HOME,      7);
	register_csifunc (TERMO_TYPE_KEYSYM, TERMO_SYM_END,       8);

	register_csifunc (TERMO_TYPE_FUNCTION, 1,  11);
	register_csifunc (TERMO_TYPE_FUNCTION, 2,  12);
	register_csifunc (TERMO_TYPE_FUNCTION, 3,  13);
	register_csifunc (TERMO_TYPE_FUNCTION, 4,  14);
	register_csifunc (TERMO_TYPE_FUNCTION, 5,  15);
	register_csifunc (TERMO_TYPE_FUNCTION, 6,  17);
	register_csifunc (TERMO_TYPE_FUNCTION, 7,  18);
	register_csifunc (TERMO_TYPE_FUNCTION, 8,  19);
	register_csifunc (TERMO_TYPE_FUNCTION, 9,  20);
	register_csifunc (TERMO_TYPE_FUNCTION, 10, 21);
	register_csifunc (TERMO_TYPE_FUNCTION, 11, 23);
	register_csifunc (TERMO_TYPE_FUNCTION, 12, 24);
	register_csifunc (TERMO_TYPE_FUNCTION, 13, 25);
	register_csifunc (TERMO_TYPE_FUNCTION, 14, 26);
	register_csifunc (TERMO_TYPE_FUNCTION, 15, 28);
	register_csifunc (TERMO_TYPE_FUNCTION, 16, 29);
	register_csifunc (TERMO_TYPE_FUNCTION, 17, 31);
	register_csifunc (TERMO_TYPE_FUNCTION, 18, 32);
	register_csifunc (TERMO_TYPE_FUNCTION, 19, 33);
	register_csifunc (TERMO_TYPE_FUNCTION, 20, 34);

	csi_handlers['u' - 0x20] = &handle_csi_u;

	csi_handlers['M' - 0x20] = &handle_csi_m;
	csi_handlers['m' - 0x20] = &handle_csi_m;

	csi_handlers['I' - 0x20] = &handle_csi_IO;
	csi_handlers['O' - 0x20] = &handle_csi_IO;

	csi_handlers['R' - 0x20] = &handle_csi_R;

	csi_handlers['y' - 0x20] = &handle_csi_y;

	csi_handlers['^' - 0x20] = &handle_csi_rxvt;
	csi_handlers['$' - 0x20] = &handle_csi_rxvt;
	csi_handlers['@' - 0x20] = &handle_csi_rxvt;

	keyinfo_initialised = 1;
	return 1;
}

static void *
new_driver (termo_t *tk, const char *term)
{
	(void) term;

	if (!keyinfo_initialised && !register_keys ())
		return NULL;

	termo_csi_t *csi = malloc (sizeof *csi);
	if (!csi)
		return NULL;

	csi->tk = tk;
	return csi;
}

static void
free_driver (void *info)
{
	termo_csi_t *csi = info;
	free (csi);
}

static termo_result_t
peekkey_csi (termo_t *tk, termo_csi_t *csi,
	size_t introlen, termo_key_t *key, int flags, size_t *nbytep)
{
	(void) csi;

	size_t csi_len;
	size_t args = 16;
	long arg[16];
	unsigned long cmd;

	termo_result_t ret = parse_csi (tk, introlen, &csi_len, arg, &args, &cmd);
	if (ret == TERMO_RES_AGAIN)
	{
		if (!(flags & PEEKKEY_FORCE))
			return TERMO_RES_AGAIN;

		(*tk->method.emit_codepoint) (tk, '[', key);
		key->modifiers |= TERMO_KEYMOD_ALT;
		*nbytep = introlen;
		return TERMO_RES_KEY;
	}

	// Mouse in X10 encoding consumes the next 3 bytes also (or more with 1005)
	if (cmd == 'M' && args < 3)
	{
		tk->buffstart += csi_len;
		tk->buffcount -= csi_len;

		termo_result_t mouse_result =
			(*tk->method.peekkey_mouse) (tk, key, nbytep);

		tk->buffstart -= csi_len;
		tk->buffcount += csi_len;

		if (mouse_result == TERMO_RES_KEY)
			*nbytep += csi_len;
		return mouse_result;
	}

	termo_result_t result = TERMO_RES_NONE;

	// We know from the logic above that cmd must be >= 0x20 and < 0x80
	if (csi_handlers[(cmd & 0xff) - 0x20])
		result = (*csi_handlers[(cmd & 0xff) - 0x20]) (tk, key, cmd, arg, args);

	if (result == TERMO_RES_NONE)
	{
#ifdef DEBUG
		switch (args)
		{
		case 1:
			fprintf (stderr, "CSI: Unknown arg1=%ld cmd=%c\n",
				arg[0], (char) cmd);
			break;
		case 2:
			fprintf (stderr, "CSI: Unknown arg1=%ld arg2=%ld cmd=%c\n",
				arg[0], arg[1], (char) cmd);
			break;
		case 3:
			fprintf (stderr, "CSI: Unknown arg1=%ld arg2=%ld arg3=%ld cmd=%c\n",
				arg[0], arg[1], arg[2], (char) cmd);
			break;
		default:
			fprintf (stderr, "CSI: Unknown arg1=%ld arg2=%ld arg3=%ld ... "
				"args=%zu cmd=%c\n", arg[0], arg[1], arg[2], args, (char) cmd);
			break;
		}
#endif
		key->type = TERMO_TYPE_UNKNOWN_CSI;
		key->code.number = cmd;

		tk->hightide = csi_len - introlen;
		*nbytep = introlen; // Do not yet eat the data bytes
		return TERMO_RES_KEY;
	}

	*nbytep = csi_len;
	return result;
}

static termo_result_t
peekkey_ss3 (termo_t *tk, termo_csi_t *csi, size_t introlen,
	termo_key_t *key, int flags, size_t *nbytep)
{
	(void) csi;

	if (tk->buffcount < introlen + 1)
	{
		if (!(flags & PEEKKEY_FORCE))
			return TERMO_RES_AGAIN;

		(*tk->method.emit_codepoint) (tk, 'O', key);
		key->modifiers |= TERMO_KEYMOD_ALT;
		*nbytep = tk->buffcount;
		return TERMO_RES_KEY;
	}

	unsigned char cmd = CHARAT (introlen);

	if (cmd < 0x40 || cmd >= 0x80)
		return TERMO_RES_NONE;

	// First go have a look at SS3-only sequences
	key->type      = ss3s[cmd - 0x20].type;
	key->code.sym  = ss3s[cmd - 0x20].sym;
	key->modifiers = ss3s[cmd - 0x20].modifier_set;

	// If that fails, try our mixed table (is there a reason for it?)
	if (key->code.sym == TERMO_SYM_UNKNOWN)
	{
		key->type      = csi_ss3s[cmd - 0x20].type;
		key->code.sym  = csi_ss3s[cmd - 0x20].sym;
		key->modifiers = csi_ss3s[cmd - 0x20].modifier_set;
	}
	// If we have a match for a keypad key but user wants to receive them
	// as characters instead, convert them
	else if ((tk->flags & TERMO_FLAG_CONVERTKP && ss3_kpalts[cmd - 0x20]))
	{
		key->type = TERMO_TYPE_KEY;
		key->code.codepoint = ss3_kpalts[cmd - 0x20];
		key->modifiers = 0;

		key->multibyte[0] = key->code.codepoint;
		key->multibyte[1] = 0;
	}

	if (key->code.sym == TERMO_SYM_UNKNOWN)
	{
#ifdef DEBUG
		fprintf (stderr, "CSI: Unknown SS3 %c (0x%02x)\n", (char) cmd, cmd);
#endif
		return TERMO_RES_NONE;
	}

	*nbytep = introlen + 1;
	return TERMO_RES_KEY;
}

static termo_result_t
peekkey (termo_t *tk, void *info,
	termo_key_t *key, int flags, size_t *nbytep)
{
	if (tk->buffcount == 0)
		return tk->is_closed ? TERMO_RES_EOF : TERMO_RES_NONE;

	termo_csi_t *csi = info;

	// Now we're sure at least 1 byte is valid
	unsigned char b0 = CHARAT (0);

	if (b0 == 0x1b && tk->buffcount == 1)
		return TERMO_RES_AGAIN;
	if (b0 == 0x1b && tk->buffcount > 1 && CHARAT (1) == '[')
		return peekkey_csi (tk, csi, 2, key, flags, nbytep);
	if (b0 == 0x1b && tk->buffcount > 1 && CHARAT (1) == 'O')
		return peekkey_ss3 (tk, csi, 2, key, flags, nbytep);
	if (b0 == 0x8f)
		return peekkey_ss3 (tk, csi, 1, key, flags, nbytep);
	if (b0 == 0x9b)
		return peekkey_csi (tk, csi, 1, key, flags, nbytep);
	return TERMO_RES_NONE;
}

termo_driver_t termo_driver_csi =
{
	.name        = "CSI",
	.new_driver  = new_driver,
	.free_driver = free_driver,
	.peekkey     = peekkey,
};