-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathin.c
2891 lines (2756 loc) · 90.2 KB
/
in.c
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "automaton.h"
#include "internal.h"
#include "unixsig.h"
#include "render.h"
#include "in.h"
// Notcurses takes over stdin, and if it is not connected to a terminal, also
// tries to make a connection to the controlling terminal. If such a connection
// is made, it will read from that source (in addition to stdin). We dump one or
// both into distinct buffers. We then try to lex structured elements out of
// the buffer(s). We can extract cursor location reports, mouse events, and
// UTF-8 characters. Completely extracted ones are placed in their appropriate
// queues, and removed from the depository buffer. We aim to consume the
// entirety of the deposit before going back to read more data, but let anyone
// blocking on data wake up as soon as we've processed any input.
//
// The primary goal is to react to terminal messages (mostly cursor location
// reports) as quickly as possible, and definitely not with unbounded latency,
// without unbounded allocation, and also without losing data. We'd furthermore
// like to reliably differentiate escapes and regular input, even when that
// latter contains escapes. Unbounded input will hopefully only be present when
// redirected from a file.
static sig_atomic_t cont_seen;
static sig_atomic_t resize_seen;
// called for SIGWINCH and SIGCONT, and causes block_on_input to return
void sigwinch_handler(int signo){
if(signo == SIGWINCH){
resize_seen = signo;
sigcont_seen_for_render = 1;
}else if(signo == SIGCONT){
cont_seen = signo;
sigcont_seen_for_render = 1;
}
}
typedef struct cursorloc {
int y, x; // 0-indexed cursor location
} cursorloc;
#ifndef __MINGW32__
typedef int ipipe;
#else
typedef HANDLE ipipe;
#endif
// local state for the input thread. don't put this large struct on the stack.
typedef struct inputctx {
// these two are not ringbuffers; we always move any leftover materia to the
// front of the queue (it ought be a handful of bytes at most).
unsigned char tbuf[BUFSIZ]; // only used if we have distinct terminal fd
unsigned char ibuf[BUFSIZ]; // might be intermingled bulk/control data
int stdinfd; // bulk in fd. always >= 0 (almost always 0). we do not
// own this descriptor, and must not close() it.
int termfd; // terminal fd: -1 with no controlling terminal, or
// if stdin is a terminal, or on MSFT Terminal.
#ifdef __MINGW32__
HANDLE stdinhandle; // handle to input terminal for MSFT Terminal
#endif
int lmargin, tmargin; // margins in use at left and top
int rmargin, bmargin; // margins in use at right and bottom
automaton amata;
int ibufvalid; // we mustn't read() if ibufvalid == sizeof(ibuf)
int tbufvalid; // only used if we have distinct terminal connection
uint8_t backspace; // backspace is usually not an escape sequence, but
// instead ^H or ^? or something. only escape sequences
// go into our automaton, so we handle this one
// out-of-band. set to non-zero; match with ctrl.
// ringbuffers for processed, structured input
cursorloc* csrs; // cursor reports are dumped here
ncinput* inputs; // processed input is dumped here
int coutstanding; // outstanding cursor location requests
int csize, isize; // total number of slots in csrs/inputs
int cvalid, ivalid; // population count of csrs/inputs
int cwrite, iwrite; // slot where we'll write the next csr/input;
// we cannot write if valid == size
int cread, iread; // slot from which clients read the next csr/input;
// they cannot read if valid == 0
pthread_mutex_t ilock; // lock for ncinput ringbuffer, also initial state
pthread_cond_t icond; // condvar for ncinput ringbuffer
pthread_mutex_t clock; // lock for csrs ringbuffer
pthread_cond_t ccond; // condvar for csrs ringbuffer
tinfo* ti; // link back to tinfo
pthread_t tid; // tid for input thread
unsigned midescape; // we're in the middle of a potential escape. we need
// to do a nonblocking read and try to complete it.
unsigned stdineof; // have we seen an EOF on stdin?
unsigned linesigs; // are line discipline signals active?
unsigned drain; // drain away bulk input?
ncsharedstats *stats; // stats shared with notcurses context
ipipe ipipes[2];
ipipe readypipes[2]; // pipes[0]: poll()able fd indicating the presence of user input
// initially, initdata is non-NULL and initdata_complete is NULL. once we
// get DA1, initdata_complete is non-NULL (it is the same value as
// initdata). once we complete reading the input payload that the DA1 arrived
// in, initdata is set to NULL, and we broadcast availability. once it has
// been taken, both become NULL.
struct initial_responses* initdata;
struct initial_responses* initdata_complete;
int kittykbd; // kitty keyboard protocol support level
bool failed; // error initializing input automaton, abort
} inputctx;
static inline void
inc_input_events(inputctx* ictx){
pthread_mutex_lock(&ictx->stats->lock);
++ictx->stats->s.input_events;
pthread_mutex_unlock(&ictx->stats->lock);
}
static inline void
inc_input_errors(inputctx* ictx){
pthread_mutex_lock(&ictx->stats->lock);
++ictx->stats->s.input_errors;
pthread_mutex_unlock(&ictx->stats->lock);
}
// load representations used by XTMODKEYS
static int
prep_xtmodkeys(inputctx* ictx){
// XTMODKEYS enables unambiguous representations of certain inputs. We
// enable XTMODKEYS where supported.
static const struct {
const char* esc;
uint32_t key;
unsigned modifiers;
} keys[] = {
{ .esc = "\x1b\x8", .key = NCKEY_BACKSPACE,
.modifiers = NCKEY_MOD_ALT, },
{ .esc = "\x1b[2P", .key = NCKEY_F01,
.modifiers = NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[5P", .key = NCKEY_F01,
.modifiers = NCKEY_MOD_CTRL, },
{ .esc = "\x1b[6P", .key = NCKEY_F01,
.modifiers = NCKEY_MOD_CTRL | NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[2Q", .key = NCKEY_F02,
.modifiers = NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[5Q", .key = NCKEY_F02,
.modifiers = NCKEY_MOD_CTRL, },
{ .esc = "\x1b[6Q", .key = NCKEY_F02,
.modifiers = NCKEY_MOD_CTRL | NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[2R", .key = NCKEY_F03,
.modifiers = NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[5R", .key = NCKEY_F03,
.modifiers = NCKEY_MOD_CTRL, },
{ .esc = "\x1b[6R", .key = NCKEY_F03,
.modifiers = NCKEY_MOD_CTRL | NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[2S", .key = NCKEY_F04,
.modifiers = NCKEY_MOD_SHIFT, },
{ .esc = "\x1b[5S", .key = NCKEY_F04,
.modifiers = NCKEY_MOD_CTRL, },
{ .esc = "\x1b[6S", .key = NCKEY_F04,
.modifiers = NCKEY_MOD_CTRL | NCKEY_MOD_SHIFT, },
{ .esc = NULL, .key = 0, },
}, *k;
for(k = keys ; k->esc ; ++k){
if(inputctx_add_input_escape(&ictx->amata, k->esc, k->key,
k->modifiers)){
return -1;
}
logdebug("added %u", k->key);
}
loginfo("added all xtmodkeys");
return 0;
}
// load all known special keys from terminfo, and build the input sequence trie
static int
prep_special_keys(inputctx* ictx){
#ifndef __MINGW32__
static const struct {
const char* tinfo;
uint32_t key;
bool shift, ctrl, alt;
} keys[] = {
// backspace (kbs) is handled seperately at the end
{ .tinfo = "kbeg", .key = NCKEY_BEGIN, },
{ .tinfo = "kcbt", .key = '\t', .shift = true, }, // "back-tab"
{ .tinfo = "kcub1", .key = NCKEY_LEFT, },
{ .tinfo = "kcuf1", .key = NCKEY_RIGHT, },
{ .tinfo = "kcuu1", .key = NCKEY_UP, },
{ .tinfo = "kcud1", .key = NCKEY_DOWN, },
{ .tinfo = "kdch1", .key = NCKEY_DEL, },
{ .tinfo = "kbs", .key = NCKEY_BACKSPACE, },
{ .tinfo = "kich1", .key = NCKEY_INS, },
{ .tinfo = "kend", .key = NCKEY_END, },
{ .tinfo = "khome", .key = NCKEY_HOME, },
{ .tinfo = "knp", .key = NCKEY_PGDOWN, },
{ .tinfo = "kpp", .key = NCKEY_PGUP, },
{ .tinfo = "kf0", .key = NCKEY_F01, },
{ .tinfo = "kf1", .key = NCKEY_F01, },
{ .tinfo = "kf2", .key = NCKEY_F02, },
{ .tinfo = "kf3", .key = NCKEY_F03, },
{ .tinfo = "kf4", .key = NCKEY_F04, },
{ .tinfo = "kf5", .key = NCKEY_F05, },
{ .tinfo = "kf6", .key = NCKEY_F06, },
{ .tinfo = "kf7", .key = NCKEY_F07, },
{ .tinfo = "kf8", .key = NCKEY_F08, },
{ .tinfo = "kf9", .key = NCKEY_F09, },
{ .tinfo = "kf10", .key = NCKEY_F10, },
{ .tinfo = "kf11", .key = NCKEY_F11, },
{ .tinfo = "kf12", .key = NCKEY_F12, },
{ .tinfo = "kf13", .key = NCKEY_F13, },
{ .tinfo = "kf14", .key = NCKEY_F14, },
{ .tinfo = "kf15", .key = NCKEY_F15, },
{ .tinfo = "kf16", .key = NCKEY_F16, },
{ .tinfo = "kf17", .key = NCKEY_F17, },
{ .tinfo = "kf18", .key = NCKEY_F18, },
{ .tinfo = "kf19", .key = NCKEY_F19, },
{ .tinfo = "kf20", .key = NCKEY_F20, },
{ .tinfo = "kf21", .key = NCKEY_F21, },
{ .tinfo = "kf22", .key = NCKEY_F22, },
{ .tinfo = "kf23", .key = NCKEY_F23, },
{ .tinfo = "kf24", .key = NCKEY_F24, },
{ .tinfo = "kf25", .key = NCKEY_F25, },
{ .tinfo = "kf26", .key = NCKEY_F26, },
{ .tinfo = "kf27", .key = NCKEY_F27, },
{ .tinfo = "kf28", .key = NCKEY_F28, },
{ .tinfo = "kf29", .key = NCKEY_F29, },
{ .tinfo = "kf30", .key = NCKEY_F30, },
{ .tinfo = "kf31", .key = NCKEY_F31, },
{ .tinfo = "kf32", .key = NCKEY_F32, },
{ .tinfo = "kf33", .key = NCKEY_F33, },
{ .tinfo = "kf34", .key = NCKEY_F34, },
{ .tinfo = "kf35", .key = NCKEY_F35, },
{ .tinfo = "kf36", .key = NCKEY_F36, },
{ .tinfo = "kf37", .key = NCKEY_F37, },
{ .tinfo = "kf38", .key = NCKEY_F38, },
{ .tinfo = "kf39", .key = NCKEY_F39, },
{ .tinfo = "kf40", .key = NCKEY_F40, },
{ .tinfo = "kf41", .key = NCKEY_F41, },
{ .tinfo = "kf42", .key = NCKEY_F42, },
{ .tinfo = "kf43", .key = NCKEY_F43, },
{ .tinfo = "kf44", .key = NCKEY_F44, },
{ .tinfo = "kf45", .key = NCKEY_F45, },
{ .tinfo = "kf46", .key = NCKEY_F46, },
{ .tinfo = "kf47", .key = NCKEY_F47, },
{ .tinfo = "kf48", .key = NCKEY_F48, },
{ .tinfo = "kf49", .key = NCKEY_F49, },
{ .tinfo = "kf50", .key = NCKEY_F50, },
{ .tinfo = "kf51", .key = NCKEY_F51, },
{ .tinfo = "kf52", .key = NCKEY_F52, },
{ .tinfo = "kf53", .key = NCKEY_F53, },
{ .tinfo = "kf54", .key = NCKEY_F54, },
{ .tinfo = "kf55", .key = NCKEY_F55, },
{ .tinfo = "kf56", .key = NCKEY_F56, },
{ .tinfo = "kf57", .key = NCKEY_F57, },
{ .tinfo = "kf58", .key = NCKEY_F58, },
{ .tinfo = "kf59", .key = NCKEY_F59, },
{ .tinfo = "kent", .key = NCKEY_ENTER, },
{ .tinfo = "kclr", .key = NCKEY_CLS, },
{ .tinfo = "kc1", .key = NCKEY_DLEFT, },
{ .tinfo = "kc3", .key = NCKEY_DRIGHT, },
{ .tinfo = "ka1", .key = NCKEY_ULEFT, },
{ .tinfo = "ka3", .key = NCKEY_URIGHT, },
{ .tinfo = "kb2", .key = NCKEY_CENTER, },
{ .tinfo = "kbeg", .key = NCKEY_BEGIN, },
{ .tinfo = "kcan", .key = NCKEY_CANCEL, },
{ .tinfo = "kclo", .key = NCKEY_CLOSE, },
{ .tinfo = "kcmd", .key = NCKEY_COMMAND, },
{ .tinfo = "kcpy", .key = NCKEY_COPY, },
{ .tinfo = "kext", .key = NCKEY_EXIT, },
{ .tinfo = "kprt", .key = NCKEY_PRINT, },
{ .tinfo = "krfr", .key = NCKEY_REFRESH, },
{ .tinfo = "kBEG", .key = NCKEY_BEGIN, .shift = 1, },
{ .tinfo = "kBEG3", .key = NCKEY_BEGIN, .alt = 1, },
{ .tinfo = "kBEG4", .key = NCKEY_BEGIN, .alt = 1, .shift = 1, },
{ .tinfo = "kBEG5", .key = NCKEY_BEGIN, .ctrl = 1, },
{ .tinfo = "kBEG6", .key = NCKEY_BEGIN, .ctrl = 1, .shift = 1, },
{ .tinfo = "kBEG7", .key = NCKEY_BEGIN, .alt = 1, .ctrl = 1, },
{ .tinfo = "kDC", .key = NCKEY_DEL, .shift = 1, },
{ .tinfo = "kDC3", .key = NCKEY_DEL, .alt = 1, },
{ .tinfo = "kDC4", .key = NCKEY_DEL, .alt = 1, .shift = 1, },
{ .tinfo = "kDC5", .key = NCKEY_DEL, .ctrl = 1, },
{ .tinfo = "kDC6", .key = NCKEY_DEL, .ctrl = 1, .shift = 1, },
{ .tinfo = "kDC7", .key = NCKEY_DEL, .alt = 1, .ctrl = 1, },
{ .tinfo = "kDN", .key = NCKEY_DOWN, .shift = 1, },
{ .tinfo = "kDN3", .key = NCKEY_DOWN, .alt = 1, },
{ .tinfo = "kDN4", .key = NCKEY_DOWN, .alt = 1, .shift = 1, },
{ .tinfo = "kDN5", .key = NCKEY_DOWN, .ctrl = 1, },
{ .tinfo = "kDN6", .key = NCKEY_DOWN, .ctrl = 1, .shift = 1, },
{ .tinfo = "kDN7", .key = NCKEY_DOWN, .alt = 1, .ctrl = 1, },
{ .tinfo = "kEND", .key = NCKEY_END, .shift = 1, },
{ .tinfo = "kEND3", .key = NCKEY_END, .alt = 1, },
{ .tinfo = "kEND4", .key = NCKEY_END, .alt = 1, .shift = 1, },
{ .tinfo = "kEND5", .key = NCKEY_END, .ctrl = 1, },
{ .tinfo = "kEND6", .key = NCKEY_END, .ctrl = 1, .shift = 1, },
{ .tinfo = "kEND7", .key = NCKEY_END, .alt = 1, .ctrl = 1, },
{ .tinfo = "kHOM", .key = NCKEY_HOME, .shift = 1, },
{ .tinfo = "kHOM3", .key = NCKEY_HOME, .alt = 1, },
{ .tinfo = "kHOM4", .key = NCKEY_HOME, .alt = 1, .shift = 1, },
{ .tinfo = "kHOM5", .key = NCKEY_HOME, .ctrl = 1, },
{ .tinfo = "kHOM6", .key = NCKEY_HOME, .ctrl = 1, .shift = 1, },
{ .tinfo = "kHOM7", .key = NCKEY_HOME, .alt = 1, .ctrl = 1, },
{ .tinfo = "kIC", .key = NCKEY_INS, .shift = 1, },
{ .tinfo = "kIC3", .key = NCKEY_INS, .alt = 1, },
{ .tinfo = "kIC4", .key = NCKEY_INS, .alt = 1, .shift = 1, },
{ .tinfo = "kIC5", .key = NCKEY_INS, .ctrl = 1, },
{ .tinfo = "kIC6", .key = NCKEY_INS, .ctrl = 1, .shift = 1, },
{ .tinfo = "kIC7", .key = NCKEY_INS, .alt = 1, .ctrl = 1, },
{ .tinfo = "kLFT", .key = NCKEY_LEFT, .shift = 1, },
{ .tinfo = "kLFT3", .key = NCKEY_LEFT, .alt = 1, },
{ .tinfo = "kLFT4", .key = NCKEY_LEFT, .alt = 1, .shift = 1, },
{ .tinfo = "kLFT5", .key = NCKEY_LEFT, .ctrl = 1, },
{ .tinfo = "kLFT6", .key = NCKEY_LEFT, .ctrl = 1, .shift = 1, },
{ .tinfo = "kLFT7", .key = NCKEY_LEFT, .alt = 1, .ctrl = 1, },
{ .tinfo = "kNXT", .key = NCKEY_PGDOWN, .shift = 1, },
{ .tinfo = "kNXT3", .key = NCKEY_PGDOWN, .alt = 1, },
{ .tinfo = "kNXT4", .key = NCKEY_PGDOWN, .alt = 1, .shift = 1, },
{ .tinfo = "kNXT5", .key = NCKEY_PGDOWN, .ctrl = 1, },
{ .tinfo = "kNXT6", .key = NCKEY_PGDOWN, .ctrl = 1, .shift = 1, },
{ .tinfo = "kNXT7", .key = NCKEY_PGDOWN, .alt = 1, .ctrl = 1, },
{ .tinfo = "kPRV", .key = NCKEY_PGUP, .shift = 1, },
{ .tinfo = "kPRV3", .key = NCKEY_PGUP, .alt = 1, },
{ .tinfo = "kPRV4", .key = NCKEY_PGUP, .alt = 1, .shift = 1, },
{ .tinfo = "kPRV5", .key = NCKEY_PGUP, .ctrl = 1, },
{ .tinfo = "kPRV6", .key = NCKEY_PGUP, .ctrl = 1, .shift = 1, },
{ .tinfo = "kPRV7", .key = NCKEY_PGUP, .alt = 1, .ctrl = 1, },
{ .tinfo = "kRIT", .key = NCKEY_RIGHT, .shift = 1, },
{ .tinfo = "kRIT3", .key = NCKEY_RIGHT, .alt = 1, },
{ .tinfo = "kRIT4", .key = NCKEY_RIGHT, .alt = 1, .shift = 1, },
{ .tinfo = "kRIT5", .key = NCKEY_RIGHT, .ctrl = 1, },
{ .tinfo = "kRIT6", .key = NCKEY_RIGHT, .ctrl = 1, .shift = 1, },
{ .tinfo = "kRIT7", .key = NCKEY_RIGHT, .alt = 1, .ctrl = 1, },
{ .tinfo = "kUP", .key = NCKEY_UP, .shift = 1, },
{ .tinfo = "kUP3", .key = NCKEY_UP, .alt = 1, },
{ .tinfo = "kUP4", .key = NCKEY_UP, .alt = 1, .shift = 1, },
{ .tinfo = "kUP5", .key = NCKEY_UP, .ctrl = 1, },
{ .tinfo = "kUP6", .key = NCKEY_UP, .ctrl = 1, .shift = 1, },
{ .tinfo = "kUP7", .key = NCKEY_UP, .alt = 1, .ctrl = 1, },
{ .tinfo = NULL, .key = 0, }
}, *k;
for(k = keys ; k->tinfo ; ++k){
char* seq = tigetstr(k->tinfo);
if(seq == NULL || seq == (char*)-1){
loginfo("no terminfo declaration for %s", k->tinfo);
continue;
}
if(seq[0] != NCKEY_ESC || strlen(seq) < 2){ // assume ESC prefix + content
logwarn("invalid escape: %s (0x%x)", k->tinfo, k->key);
continue;
}
unsigned modifiers = (k->shift ? NCKEY_MOD_SHIFT : 0)
| (k->alt ? NCKEY_MOD_ALT : 0)
| (k->ctrl ? NCKEY_MOD_CTRL : 0);
if(inputctx_add_input_escape(&ictx->amata, seq, k->key, modifiers)){
return -1;
}
logdebug("support for terminfo's %s: %s", k->tinfo, seq);
}
const char* bs = tigetstr("kbs");
if(bs == NULL){
logwarn("no backspace key was defined");
}else{
if(bs[0] == NCKEY_ESC){
if(inputctx_add_input_escape(&ictx->amata, bs, NCKEY_BACKSPACE, 0)){
return -1;
}
}else{
ictx->backspace = bs[0];
}
}
#else
(void)ictx;
#endif
return 0;
}
// starting from the current amata match point, match any necessary prefix, then
// extract the (possibly empty) content, then match the follow. as we are only
// called from a callback context, and know we've been properly matched, there
// is no error-checking per se (we do require prefix/follow matches, but if
// missed, we just return NULL). indicate empty prefix with "", not NULL.
// updates ictx->amata.matchstart to be pointing past the follow. follow ought
// not be NUL.
static char*
amata_next_kleene(automaton* amata, const char* prefix, char follow){
char c;
while( (c = *prefix++) ){
if(*amata->matchstart != c){
logerror("matchstart didn't match prefix (%c vs %c)", c, *amata->matchstart);
return NULL;
}
++amata->matchstart;
}
// prefix has been matched. mark start of string and find follow.
const unsigned char* start = amata->matchstart;
while(*amata->matchstart != follow){
++amata->matchstart;
}
char* ret = malloc(amata->matchstart - start + 1);
if(ret){
memcpy(ret, start, amata->matchstart - start);
ret[amata->matchstart - start] = '\0';
}
return ret;
}
// starting from the current amata match point, match any necessary prefix, then
// extract the numeric (possibly empty), then match the follow. as we are only
// called from a callback context, and know we've been properly matched, there
// is no error-checking per se (we do require prefix/follow matches, but if
// missed, we just return 0). indicate empty prefix with "", not NULL.
// updates ictx->amata.matchstart to be pointing past the follow. follow ought
// not be a digit nor NUL.
static unsigned
amata_next_numeric(automaton* amata, const char* prefix, char follow){
char c;
while( (c = *prefix++) ){
if(*amata->matchstart != c){
logerror("matchstart didn't match prefix (%c vs %c)", c, *amata->matchstart);
return 0;
}
++amata->matchstart;
}
// prefix has been matched
unsigned ret = 0;
while(isdigit(*amata->matchstart)){
int addend = *amata->matchstart - '0';
if((UINT_MAX - addend) / 10 < ret){
logerror("overflow: %u * 10 + %u > %u", ret, addend, UINT_MAX);
}
ret *= 10;
ret += addend;
++amata->matchstart;
}
char candidate = *amata->matchstart++;
if(candidate != follow){
logerror("didn't see follow (%c vs %c)", candidate, follow);
return 0;
}
return ret;
}
// same deal as amata_next_numeric, but returns a heap-allocated string.
// strings always end with ST ("x1b\\"). this one *does* return NULL on
// either a match failure or an alloc failure.
static char*
amata_next_string(automaton* amata, const char* prefix){
return amata_next_kleene(amata, prefix, '\x1b');
}
static inline void
send_synth_signal(int sig){
if(sig){
#ifndef __MINGW32__
raise(sig);
#endif
}
}
static void
mark_pipe_ready(ipipe pipes[static 2]){
char sig = 1;
#ifndef __MINGW32__
if(write(pipes[1], &sig, sizeof(sig)) != 1){
logwarn("error writing to pipe (%d) (%s)", pipes[1], strerror(errno));
#else
DWORD wrote;
if(!WriteFile(pipes[1], &sig, sizeof(sig), &wrote, NULL) || wrote != sizeof(sig)){
logwarn("error writing to pipe");
#endif
}else{
loginfo("wrote to readiness pipe");
}
}
// shove the assembled input |tni| into the input queue (if there's room, and
// we're not draining, and we haven't hit EOF). send any synthesized signal as
// the last thing we do. if Ctrl or Shift are among the modifiers, we replace
// any lowercase letter with its uppercase form, to maintain compatibility with
// other input methods.
//
// note that this w orks entirely off 'modifiers', not the obsolete
// shift/alt/ctrl booleans, which it neither sets nor tests!
static void
load_ncinput(inputctx* ictx, ncinput *tni){
int synth = 0;
if(tni->modifiers & (NCKEY_MOD_CTRL | NCKEY_MOD_SHIFT | NCKEY_MOD_CAPSLOCK)){
// when ctrl/shift are used with an ASCII (0..127) lowercase letter, always
// supply the capitalized form, to maintain compatibility among solutions.
if(tni->id < 0x7f){
if(islower(tni->id)){
tni->id = toupper(tni->id);
}
}
}
// if the kitty keyboard protocol is in use, any input without an explicit
// evtype can be safely considered a PRESS.
if(ictx->kittykbd){
if(tni->evtype == NCTYPE_UNKNOWN){
tni->evtype = NCTYPE_PRESS;
}
}
if(tni->modifiers == NCKEY_MOD_CTRL){ // exclude all other modifiers
if(ictx->linesigs){
if(tni->id == 'C'){
synth = SIGINT;
}else if(tni->id == 'Z'){
synth = SIGSTOP;
}else if(tni->id == '\\'){
synth = SIGQUIT;
}
}
}
inc_input_events(ictx);
if(ictx->drain || ictx->stdineof){
send_synth_signal(synth);
return;
}
pthread_mutex_lock(&ictx->ilock);
if(ictx->ivalid == ictx->isize){
pthread_mutex_unlock(&ictx->ilock);
logwarn("dropping input 0x%08x", tni->id);
inc_input_errors(ictx);
send_synth_signal(synth);
return;
}
ncinput* ni = ictx->inputs + ictx->iwrite;
memcpy(ni, tni, sizeof(*tni));
// perform final normalizations
if(ni->id == 0x7f || ni->id == 0x8){
ni->id = NCKEY_BACKSPACE;
}else if(ni->id == '\n' || ni->id == '\r'){
ni->id = NCKEY_ENTER;
}else if(ni->id == ictx->backspace){
ni->id = NCKEY_BACKSPACE;
}else if(ni->id > 0 && ni->id <= 26 && ni->id != '\t'){
ni->id = ni->id + 'A' - 1;
ni->modifiers |= NCKEY_MOD_CTRL;
}
if(++ictx->iwrite == ictx->isize){
ictx->iwrite = 0;
}
++ictx->ivalid;
// FIXME we don't always need to write here; write if ictx->ivalid was 0, and
// also write *from the client context* if we empty the input buffer there..?
mark_pipe_ready(ictx->readypipes);
pthread_mutex_unlock(&ictx->ilock);
pthread_cond_broadcast(&ictx->icond);
send_synth_signal(synth);
}
static void
pixelmouse_click(inputctx* ictx, ncinput* ni, long y, long x){
--x;
--y;
if(ictx->ti->cellpxy == 0 || ictx->ti->cellpxx == 0){
logerror("pixelmouse event without pixel info (%ld/%ld)", y, x);
inc_input_errors(ictx);
return;
}
ni->ypx = y % ictx->ti->cellpxy;
ni->xpx = x % ictx->ti->cellpxx;
y /= ictx->ti->cellpxy;
x /= ictx->ti->cellpxx;
x -= ictx->lmargin;
y -= ictx->tmargin;
// convert from 1- to 0-indexing, and account for margins
if(x < 0 || y < 0){ // click was in margins, drop it
logwarn("dropping click in margins %ld/%ld", y, x);
return;
}
if((unsigned)x >= ictx->ti->dimx - (ictx->rmargin + ictx->lmargin)){
logwarn("dropping click in margins %ld/%ld", y, x);
return;
}
if((unsigned)y >= ictx->ti->dimy - (ictx->bmargin + ictx->tmargin)){
logwarn("dropping click in margins %ld/%ld", y, x);
return;
}
ni->y = y;
ni->x = x;
load_ncinput(ictx, ni);
}
// ictx->numeric, ictx->p3, and ictx->p2 have the two parameters. we're using
// SGR (1006) mouse encoding, so use the final character to determine release
// ('M' for click, 'm' for release).
static void
mouse_click(inputctx* ictx, unsigned release, char follow){
unsigned mods = amata_next_numeric(&ictx->amata, "\x1b[<", ';');
long x = amata_next_numeric(&ictx->amata, "", ';');
long y = amata_next_numeric(&ictx->amata, "", follow);
ncinput tni = {
.ctrl = mods & 0x10,
.alt = mods & 0x08,
.shift = mods & 0x04,
};
tni.modifiers = (tni.shift ? NCKEY_MOD_SHIFT : 0)
| (tni.ctrl ? NCKEY_MOD_CTRL : 0)
| (tni.alt ? NCKEY_MOD_ALT : 0);
// SGR mouse reporting: lower two bits signify base button + {0, 1, 2} press
// and no button pressed/release/{3}. bit 5 indicates motion. bits 6 and 7
// select device groups: 64 is buttons 4--7, 128 is 8--11. a pure motion
// report (no button) is 35 (32 + 3 (no button pressed)) with (oddly enough)
// 'M' (i.e. release == true).
if(release){
tni.evtype = NCTYPE_RELEASE;
}else{
tni.evtype = NCTYPE_PRESS;
}
if(mods % 4 == 3){
tni.id = NCKEY_MOTION;
tni.evtype = NCTYPE_RELEASE;
}else{
if(mods < 64){
tni.id = NCKEY_BUTTON1 + (mods % 4);
}else if(mods >= 64 && mods < 128){
tni.id = NCKEY_BUTTON4 + (mods % 4);
}else if(mods >= 128 && mods < 192){
tni.id = NCKEY_BUTTON8 + (mods % 4);
}
}
if(ictx->ti->pixelmice){
if(ictx->ti->cellpxx == 0){
logerror("pixelmouse but no pixel info");
}
return pixelmouse_click(ictx, &tni, y, x);
}
x -= (1 + ictx->lmargin);
y -= (1 + ictx->tmargin);
// convert from 1- to 0-indexing, and account for margins
if(x < 0 || y < 0){ // click was in margins, drop it
logwarn("dropping click in margins %ld/%ld", y, x);
return;
}
if((unsigned)x >= ictx->ti->dimx - (ictx->rmargin + ictx->lmargin)){
logwarn("dropping click in margins %ld/%ld", y, x);
return;
}
if((unsigned)y >= ictx->ti->dimy - (ictx->bmargin + ictx->tmargin)){
logwarn("dropping click in margins %ld/%ld", y, x);
return;
}
tni.x = x;
tni.y = y;
tni.ypx = -1;
tni.xpx = -1;
load_ncinput(ictx, &tni);
}
static int
mouse_press_cb(inputctx* ictx){
mouse_click(ictx, 0, 'M');
return 2;
}
static int
mouse_release_cb(inputctx* ictx){
mouse_click(ictx, 1, 'm');
return 2;
}
static int
cursor_location_cb(inputctx* ictx){
unsigned y = amata_next_numeric(&ictx->amata, "\x1b[", ';') - 1;
unsigned x = amata_next_numeric(&ictx->amata, "", 'R') - 1;
// the first one doesn't go onto the queue; consume it here
pthread_mutex_lock(&ictx->clock);
--ictx->coutstanding;
if(ictx->initdata){
pthread_mutex_unlock(&ictx->clock);
ictx->initdata->cursory = y;
ictx->initdata->cursorx = x;
return 2;
}
if(ictx->cvalid == ictx->csize){
pthread_mutex_unlock(&ictx->clock);
logwarn("dropping cursor location report %u/%u", y, x);
inc_input_errors(ictx);
}else{
cursorloc* cloc = &ictx->csrs[ictx->cwrite];
if(++ictx->cwrite == ictx->csize){
ictx->cwrite = 0;
}
cloc->y = y;
cloc->x = x;
++ictx->cvalid;
pthread_mutex_unlock(&ictx->clock);
pthread_cond_broadcast(&ictx->ccond);
loginfo("cursor location: %u/%u", y, x);
}
return 2;
}
static int
geom_cb(inputctx* ictx){
unsigned kind = amata_next_numeric(&ictx->amata, "\x1b[", ';');
unsigned y = amata_next_numeric(&ictx->amata, "", ';');
unsigned x = amata_next_numeric(&ictx->amata, "", 't');
if(kind == 4){ // pixel geometry
if(ictx->initdata){
ictx->initdata->pixy = y;
ictx->initdata->pixx = x;
}
loginfo("pixel geom report %d/%d", y, x);
}else if(kind == 8){ // cell geometry
if(ictx->initdata){
ictx->initdata->dimy = y;
ictx->initdata->dimx = x;
}
loginfo("cell geom report %d/%d", y, x);
}else{
logerror("invalid geom report type: %d", kind);
return -1;
}
return 2;
}
static void
xtmodkey(inputctx* ictx, int val, int mods){
assert(mods >= 0);
assert(val > 0);
logdebug("v/m %d %d", val, mods);
ncinput tni = {
.id = val,
.evtype = NCTYPE_UNKNOWN,
};
if(mods == 2 || mods == 4 || mods == 6 || mods == 8 || mods == 10
|| mods == 12 || mods == 14 || mods == 16){
tni.shift = 1;
tni.modifiers |= NCKEY_MOD_SHIFT;
}
if(mods == 5 || mods == 6 || mods == 7 || mods == 8 ||
(mods >= 13 && mods <= 16)){
tni.ctrl = 1;
tni.modifiers |= NCKEY_MOD_CTRL;
}
if(mods == 3 || mods == 4 || mods == 7 || mods == 8 || mods == 11
|| mods == 12 || mods == 15 || mods == 16){
tni.alt = 1;
tni.modifiers |= NCKEY_MOD_ALT;
}
if(mods >= 9 && mods <= 16){
tni.modifiers |= NCKEY_MOD_META;
}
load_ncinput(ictx, &tni);
}
static uint32_t
kitty_functional(uint32_t val){
if(val >= 57344 && val <= 63743){
if(val >= 57376 && val <= 57398){
val = NCKEY_F13 + val - 57376;
}else if(val >= 57428 && val <= 57440){
val = NCKEY_MEDIA_PLAY + val - 57428;
}else if(val >= 57399 && val <= 57408){
val = '0' + val - 57399;
}else if(val >= 57441 && val <= 57454){ // up through NCKEY_L5SHIFT
val = NCKEY_LSHIFT + val - 57441;
}else switch(val){
case 57358: val = NCKEY_CAPS_LOCK; break;
case 57400: val = '1'; break;
case 57359: val = NCKEY_SCROLL_LOCK; break;
case 57360: val = NCKEY_NUM_LOCK; break;
case 57361: val = NCKEY_PRINT_SCREEN; break;
case 57362: val = NCKEY_PAUSE; break;
case 57363: val = NCKEY_MENU; break;
case 57409: val = '.'; break;
case 57410: val = '/'; break;
case 57411: val = '*'; break;
case 57412: val = '-'; break;
case 57413: val = '+'; break;
case 57414: val = NCKEY_ENTER; break;
case 57415: val = '='; break;
case 57416: val = NCKEY_SEPARATOR; break;
case 57417: val = NCKEY_LEFT; break;
case 57418: val = NCKEY_RIGHT; break;
case 57419: val = NCKEY_UP; break;
case 57420: val = NCKEY_DOWN; break;
case 57421: val = NCKEY_PGUP; break;
case 57422: val = NCKEY_PGDOWN; break;
case 57423: val = NCKEY_HOME; break;
case 57424: val = NCKEY_END; break;
case 57425: val = NCKEY_INS; break;
case 57426: val = NCKEY_DEL; break;
case 57427: val = NCKEY_BEGIN; break;
}
}else{
switch(val){
case 0xd: val = NCKEY_ENTER; break;
}
}
return val;
}
static void
kitty_kbd_txt(inputctx* ictx, int val, int mods, uint32_t *txt, int evtype){
assert(evtype >= 0);
assert(mods >= 0);
assert(val > 0);
logdebug("v/m/e %d %d %d", val, mods, evtype);
// "If the modifier field is not present in the escape code, its default value
// is 1 which means no modifiers."
if(mods == 0){
mods = 1;
}
ncinput tni = {
.id = kitty_functional(val),
.shift = mods && !!((mods - 1) & 0x1),
.alt = mods && !!((mods - 1) & 0x2),
.ctrl = mods && !!((mods - 1) & 0x4),
.modifiers = mods - 1,
};
switch(evtype){
case 0:
__attribute__ ((fallthrough));
case 1:
tni.evtype = NCTYPE_PRESS;
break;
case 2:
tni.evtype = NCTYPE_REPEAT;
break;
case 3:
tni.evtype = NCTYPE_RELEASE;
break;
default:
tni.evtype = NCTYPE_UNKNOWN;
break;
}
//note: if we don't set eff_text here, it will be set to .id later.
if(txt && txt[0]!=0){
for(int i=0 ; i<NCINPUT_MAX_EFF_TEXT_CODEPOINTS ; i++){
tni.eff_text[i] = txt[i];
}
}
load_ncinput(ictx, &tni);
}
static void
kitty_kbd(inputctx* ictx, int val, int mods, int evtype){
kitty_kbd_txt(ictx, val, mods, NULL, evtype);
}
static int
kitty_cb_simple(inputctx* ictx){
unsigned val = amata_next_numeric(&ictx->amata, "\x1b[", 'u');
val = kitty_functional(val);
kitty_kbd(ictx, val, 0, 0);
return 2;
}
static int
kitty_cb(inputctx* ictx){
unsigned val = amata_next_numeric(&ictx->amata, "\x1b[", ';');
unsigned mods = amata_next_numeric(&ictx->amata, "", 'u');
kitty_kbd(ictx, val, mods, 0);
return 2;
}
static int
kitty_cb_atxtn(inputctx* ictx, int n, int with_event){
uint32_t txt[5]={0};
unsigned val = amata_next_numeric(&ictx->amata, "\x1b[", ';');
unsigned ev = 0;
unsigned mods = 0;
if (with_event) {
mods = amata_next_numeric(&ictx->amata, "", ':');
ev = amata_next_numeric(&ictx->amata, "", ';');
} else {
mods = amata_next_numeric(&ictx->amata, "", ';');
}
for (int i = 0; i<n; i++) {
txt[i] = amata_next_numeric(&ictx->amata, "", (i==n-1)?'u':';');
}
kitty_kbd_txt(ictx, val, mods, txt, ev);
return 2;
}
static int
kitty_cb_atxt1(inputctx* ictx){
return kitty_cb_atxtn(ictx, 1, 0);
}
static int
kitty_cb_atxt2(inputctx* ictx){
return kitty_cb_atxtn(ictx, 2, 0);
}
static int
kitty_cb_atxt3(inputctx* ictx){
return kitty_cb_atxtn(ictx, 3, 0);
}
static int
kitty_cb_atxt4(inputctx* ictx){
return kitty_cb_atxtn(ictx, 4, 0);
}
static int
kitty_cb_complex_atxt1(inputctx* ictx){
return kitty_cb_atxtn(ictx, 1, 1);
}
static int
kitty_cb_complex_atxt2(inputctx* ictx){
return kitty_cb_atxtn(ictx, 2, 1);
}
static int
kitty_cb_complex_atxt3(inputctx* ictx){
return kitty_cb_atxtn(ictx, 3, 1);
}
static int
kitty_cb_complex_atxt4(inputctx* ictx){
return kitty_cb_atxtn(ictx, 4, 1);
}
static uint32_t
legacy_functional(uint32_t id){
switch(id){
case 2: id = NCKEY_INS; break;
case 3: id = NCKEY_DEL; break;
case 5: id = NCKEY_PGUP; break;
case 6: id = NCKEY_PGDOWN; break;
case 7: id = NCKEY_HOME; break;
case 8: id = NCKEY_END; break;
case 11: id = NCKEY_F01; break;
case 12: id = NCKEY_F02; break;
case 13: id = NCKEY_F03; break;
case 14: id = NCKEY_F04; break;
case 15: id = NCKEY_F05; break;
case 17: id = NCKEY_F06; break;
case 18: id = NCKEY_F07; break;
case 19: id = NCKEY_F08; break;
case 20: id = NCKEY_F09; break;
case 21: id = NCKEY_F10; break;
case 23: id = NCKEY_F11; break;
case 24: id = NCKEY_F12; break;
}
return id;
}
static int
simple_cb_begin(inputctx* ictx){
kitty_kbd(ictx, NCKEY_BEGIN, 0, 0);
return 2;
}
static int
kitty_cb_functional(inputctx* ictx){
unsigned val = amata_next_numeric(&ictx->amata, "\x1b[", ';');
unsigned mods = amata_next_numeric(&ictx->amata, "", ':');
unsigned ev = amata_next_numeric(&ictx->amata, "", '~');
uint32_t kval = kitty_functional(val);
if(kval == val){
kval = legacy_functional(val);
}
kitty_kbd(ictx, kval, mods, ev);
return 2;
}
static int
wezterm_cb(inputctx* ictx){
unsigned val = amata_next_numeric(&ictx->amata, "\x1b[", ';');
unsigned mods = amata_next_numeric(&ictx->amata, "", '~');
uint32_t kval = legacy_functional(val);
kitty_kbd(ictx, kval, mods, 0);
return 2;
}
static int
legacy_cb_f1(inputctx* ictx){
unsigned mods = amata_next_numeric(&ictx->amata, "\x1b[1;", 'P');
kitty_kbd(ictx, NCKEY_F01, mods, 0);
return 2;
}
static int
legacy_cb_f2(inputctx* ictx){
unsigned mods = amata_next_numeric(&ictx->amata, "\x1b[1;", 'Q');
kitty_kbd(ictx, NCKEY_F02, mods, 0);
return 2;
}
static int
legacy_cb_f4(inputctx* ictx){
unsigned mods = amata_next_numeric(&ictx->amata, "\x1b[1;", 'S');
kitty_kbd(ictx, NCKEY_F04, mods, 0);
return 2;
}
static int
kitty_cb_f1(inputctx* ictx){
unsigned mods = amata_next_numeric(&ictx->amata, "\x1b[1;", ':');
unsigned ev = amata_next_numeric(&ictx->amata, "", 'P');