forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
unikey.c
728 lines (694 loc) · 21.2 KB
/
unikey.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
/*
* ANSI to Unicode keyboard mapping
* Maps VT and ANSI keyboard sequences to unicode private use range.
*
* July 2022 Greg Haerr
*/
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "unikey.h"
#define ANSI_UTF8 0 /* =1 to decode UTF-9 in readansi() */
#define CHECK_MOUSE 1 /* =1 to validate ANSI mouse sequences */
#define DEBUG 1 /* =1 for keyname() */
#define ESC 27
static char scroll_reverse = 0; /* report reversed scroll wheel direction */
int kDoubleClickTime = 200;
#if DEBUG
#pragma GCC diagnostic ignored "-Wmissing-braces"
struct unikeyname unikeynames[] = {
kBackSpace, "kBackSpace",
kTab, "kTab",
kEscKey, "kEscKey",
kDel, "kDel",
kUpArrow, "kUpArrow",
kDownArrow, "kDownArrow",
kLeftArrow, "kLeftArrow",
kRightArrow, "kRightArrow",
kInsert, "kInsert",
kDelete, "kDelete",
kHome, "kHome",
kEnd, "kEnd",
kPageUp, "kPageUp",
kPageDown, "kPageDown",
kMouseLeftDown, "kMouseLeftDown",
kMouseMiddleDown, "kMouseMiddleDown",
kMouseRightDown, "kMouseRightDown",
kMouseLeftUp, "kMouseLeftUp",
kMouseMiddleUp, "kMouseMiddleUp",
kMouseRightUp, "kMouseRightUp",
kMouseLeftDrag, "kMouseLeftDrag",
kMouseMiddleDrag, "kMouseMiddleDrag",
kMouseRightDrag, "kMouseRightDrag",
kMouseWheelUp, "kMouseWheelUp",
kMouseWheelDown, "kMouseWheelDown",
kMouseMotion, "kMouseMotion",
kMouseLeftDoubleClick,"kMouseLeftDoubleClick",
kF1, "kF1",
kF2, "kF2",
kF3, "kF3",
kF4, "kF4",
kF5, "kF5",
kF6, "kF6",
kF7, "kF7",
kF8, "kF8",
kF9, "kF9",
kF10, "kF10",
kF11, "kF11",
kF12, "kF12",
kCtrlF1, "kCtrlF1",
kCtrlF2, "kCtrlF2",
kCtrlF3, "kCtrlF3",
kCtrlF4, "kCtrlF4",
kCtrlF5, "kCtrlF5",
kCtrlF6, "kCtrlF6",
kCtrlF7, "kCtrlF7",
kCtrlF8, "kCtrlF8",
kCtrlF9, "kCtrlF9",
kCtrlF10, "kCtrlF10",
kCtrlF11, "kCtrlF11",
kCtrlF12, "kCtrlF12",
kAltF1, "kAltF1",
kAltF2, "kAltF2",
kAltF3, "kAltF3",
kAltF4, "kAltF4",
kAltF5, "kAltF5",
kAltF6, "kAltF6",
kAltF7, "kAltF7",
kAltF8, "kAltF8",
kAltF9, "kAltF9",
kAltF10, "kAltF10",
kAltF11, "kAltF11",
kAltF12, "kAltF12",
kAltA, "kAltA",
kAltB, "kAltB",
kAltC, "kAltC",
kAltD, "kAltD",
kAltE, "kAltE",
kAltF, "kAltF",
kAltG, "kAltG",
kAltH, "kAltH",
kAltI, "kAltI",
kAltJ, "kAltJ",
kAltK, "kAltK",
kAltL, "kAltL",
kAltM, "kAltM",
kAltN, "kAltN",
kAltO, "kAltO",
kAltP, "kAltP",
kAltQ, "kAltQ",
kAltR, "kAltR",
kAltS, "kAltS",
kAltT, "kAltT",
kAltU, "kAltU",
kAltV, "kAltV",
kAltW, "kAltW",
kAltX, "kAltX",
kAltY, "kAltY",
kAltZ, "kAltZ",
kAlt0, "kAlt0",
kAlt1, "kAlt1",
kAlt2, "kAlt2",
kAlt3, "kAlt3",
kAlt4, "kAlt4",
kAlt5, "kAlt5",
kAlt6, "kAlt6",
kAlt7, "kAlt7",
kAlt8, "kAlt8",
kAlt9, "kAlt9",
0, 0
};
char *unikeyname(int k)
{
int i;
static char name[24];
for (i = 0; unikeynames[i].key; i++) {
if (unikeynames[i].key == k) {
strcpy(name, unikeynames[i].name);
return name;
}
}
return NULL;
}
#endif
static int small_atoi(const char *s)
{
int n = 0;
while (*s == ' ' || *s == '\t')
s++;
while ((unsigned) (*s - '0') <= 9u)
n = n * 10 + *s++ - '0';
return n;
}
/*
* Check and convert from single ANSI or UTF-8 keyboard sequence
* to unicode key or char value, including single byte ASCII values
* (0x00 <= ASCII <= 0x7f).
* Will also decode UTF-8 into single UCS-2 character.
* Returns -1 if not ASCII or ANSI keyboard sequence (e.g. mouse or DSR).
*/
int ansi_to_unikey(char *buf, int n)
{
if (n == 0)
return -1; /* invalid conversion */
if (n == 1) {
int c = buf[0] & 255; /* ASCII range */
return (c == 127)? 8: c; /* map DEL -> BS */
}
if (buf[0] == ESC) {
if (buf[1] == '[') {
if (n == 3) { /* xterm sequences */
switch (buf[2]) { /* ESC [ A etc */
case 'A': return kUpArrow;
case 'B': return kDownArrow;
case 'C': return kRightArrow;
case 'D': return kLeftArrow;
case 'F': return kEnd;
case 'H': return kHome;
}
} else if (n == 4 && buf[2] == '1') { /* ESC [ 1 P etc */
switch (buf[3]) {
case 'P': return kF1;
case 'Q': return kF2;
case 'R': return kF3;
case 'S': return kF4;
}
}
if (n > 3 && buf[n-1] == '~') { /* vt sequences */
switch (small_atoi(buf+2)) {
case 1: return kHome;
case 2: return kInsert;
case 3: return kDelete;
case 4: return kEnd;
case 5: return kPageUp;
case 6: return kPageDown;
case 7: return kHome;
case 8: return kEnd;
case 11: return kF1;
case 12: return kF2;
case 13: return kF3;
case 14: return kF4;
case 15: return kF5;
case 17: return kF6;
case 18: return kF7;
case 19: return kF8;
case 20: return kF9;
case 21: return kF10;
case 23: return kF11;
case 24: return kF12;
}
}
}
/* allow multi-keystroke sequences using ESC + character -> Alt/Fn key */
if (n == 2) {
if (buf[1] >= 'a' && buf[1] <= 'z') /* ESC {a-z} -> ALT-{A-Z} */
return buf[1] - 'a' + kAltA;
if (buf[1] >= '1' && buf[1] <= '9') /* ESC {1-9} -> Fn{1-9} */
return buf[1] - '1' + kF1;
if (buf[1] == '0')
return kF10;
}
}
return -1;
}
/* state machine conversion of UTF-8 byte sequence to Rune */
/* returns Rune or 0 on no data yet or -1 on invalid data */
int stream_to_rune(unsigned int ch)
{
static enum { kAscii, kUtf8 } state = kAscii;
static int a, n;
switch (state) {
case kAscii:
default:
if (ch >= 0300) {
a = ThomPikeByte(ch);
n = ThomPikeLen(ch) - 1;
state = kUtf8;
return 0; /* no data yet */
}
break;
case kUtf8:
if (!ThomPikeCont(ch)) {
state = kAscii;
return -1; /* bad data */
}
a = ThomPikeMerge(a, ch);
if (!--n) {
state = kAscii;
return a;
}
return 0; /* no data yet */
}
return ch;
}
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#ifdef COSMO
STATIC_YOINK("vga_console");
#endif
#ifndef COSMO
#define unreachable
/**
* Reads single keystroke or control sequence from character device.
*
* When reading ANSI UTF-8 text streams, characters and control codes
* are oftentimes encoded as multi-byte sequences. This function knows
* how long each sequence is, so that each read consumes a single thing
* from the underlying file descriptor, e.g.
*
* "a" ALFA
* "\316\261" ALPHA
* "\e[38;5;202m" ORANGERED
* "\e[A" UP
* "\e\e[A" ALT-UP
* "\001" CTRL-ALFA
* "\e\001" ALT-CTRL-ALFA
* "\eOP" PF1
* "\000" NUL
* "\e]rm -rf /\e\\" OSC
* "\302\233A" UP
* "\300\200" NUL
*
* This routine generalizes to ascii, utf-8, chorded modifier keys,
* function keys, color codes, c0/c1 control codes, cursor movement,
* mouse movement, etc.
*
* Userspace buffering isn't required, since ANSI escape sequences and
* UTF-8 are decoded without peeking. Noncanonical overlong encodings
* can cause the stream to go out of sync. This function recovers such
* events by ignoring continuation bytes at the beginning of each read.
*
* @param p is guaranteed to receive a NUL terminator if n>0
* @return number of bytes read (helps differentiate "\0" vs. "")
* @see examples/ttyinfo.c
* @see ANSI X3.64-1979
* @see ISO/IEC 6429
* @see FIPS-86
* @see ECMA-48
*/
int readansi(int fd, char *p, int n) {
//wint_t x = 0;
int rc;
int e, i, j;
unsigned char c;
enum { kAscii, kUtf8, kEsc, kCsi1, kCsi2, kSs, kNf, kStr, kStr2, kDone } t;
e = errno;
t = kAscii;
i = j = 0;
if (n) p[0] = 0;
do {
for (;;) {
if (n) {
/* TODO: possibly allow timeout after single ESC to return ESC */
rc = read(fd, &c, 1);
} else {
errno = EFAULT; //read(fd, 0, 0);
rc = -1;
}
if (rc == -1 && errno == EINTR) {
if (!i) {
return -1;
}
} else if (rc == -1) {
return -1;
} else if (!rc) {
if (!i) {
errno = e;
return 0;
} else {
errno = EILSEQ;
return -1;
}
} else {
break;
}
}
if (i + 1 < n) {
p[i] = c;
p[i + 1] = 0;
} else if (i < n) {
p[i] = 0;
}
++i;
switch (t) {
Whoopsie:
if (n) p[0] = c;
t = kAscii;
i = 1;
/* fallthrough */
case kAscii:
if (c < 0200) {
if (c == '\e') {
t = kEsc;
} else {
t = kDone;
}
} else if (c >= 0300) {
#if ANSI_UTF8
t = kUtf8;
x = ThomPikeByte(c);
j = ThomPikeLen(c) - 1;
#else
t = kDone;
#endif
} else {
/* ignore overlong sequences */
}
break;
#if ANSI_UTF8
case kUtf8:
if ((c & 0300) == 0200) {
x = ThomPikeMerge(x, c);
if (!--j) {
switch (x) {
case '\e':
t = kEsc; /* parsed but not canonicalized */
break;
case 0x9b:
t = kCsi1; /* unusual but legal */
break;
case 0x8e:
case 0x8f:
t = kSs; /* unusual but legal */
break;
case 0x90: /* DCS (Device Control String) */
case 0x98: /* SOS (Start of String) */
case 0x9d: /* OSC (Operating System Command) */
case 0x9e: /* PM (Privacy Message) */
case 0x9f: /* APC (Application Program Command) */
t = kStr;
break;
default:
t = kDone;
break;
}
}
} else {
goto Whoopsie; /* ignore underlong sequences if not eof */
}
break;
#endif
case kEsc:
if (0x20 <= c && c <= 0x2f) { /* Nf */
/*
* Almost no one uses ANSI Nf sequences
* They overlaps with alt+graphic keystrokes
* We care more about being able to type alt-/
*/
if (c == ' ' || c == '#') {
t = kNf;
} else {
t = kDone;
}
} else if (0x30 <= c && c <= 0x3f) { /* Fp */
t = kDone;
} else if (0x20 <= c && c <= 0x5F) { /* Fe */
switch (c) {
case '[':
t = kCsi1;
break;
case 'N': /* SS2 */
case 'O': /* SS3 */
t = kSs;
break;
case 'P': /* DCS (Device Control String) */
case 'X': /* SOS (Start of String) */
case ']': /* DCS (Operating System Command) */
case '^': /* PM (Privacy Message) */
case '_': /* DCS (Application Program Command) */
t = kStr;
break;
default:
t = kDone;
break;
}
} else if (0x60 <= c && c <= 0x7e) { /* Fs */
t = kDone;
} else if (c == '\e') {
if (i < 3) {
t = kEsc; /* alt chording */
} else {
t = kDone; /* esc mashing */
i = 1;
}
} else {
t = kDone;
}
break;
case kSs:
t = kDone;
break;
case kNf:
if (0x30 <= c && c <= 0x7e) {
t = kDone;
} else if (!(0x20 <= c && c <= 0x2f)) {
goto Whoopsie;
}
break;
case kCsi1:
if (0x20 <= c && c <= 0x2f) {
t = kCsi2;
} else if (c == '[' && (i == 3 || (i == 4 && p[1] == '\e'))) {
/* linux function keys */
} else if (0x40 <= c && c <= 0x7e) {
t = kDone;
} else if (!(0x30 <= c && c <= 0x3f)) {
goto Whoopsie;
}
break;
case kCsi2:
if (0x40 <= c && c <= 0x7e) {
t = kDone;
} else if (!(0x20 <= c && c <= 0x2f)) {
goto Whoopsie;
}
break;
case kStr:
switch (c) {
case '\a':
t = kDone;
break;
case '\e': /* ESC */
case 0302: /* C1 (UTF-8) */
t = kStr2;
break;
default:
break;
}
break;
case kStr2:
switch (c) {
case '\a':
t = kDone;
break;
case '\\': /* ST (ASCII) */
case 0234: /* ST (UTF-8) */
t = kDone;
break;
default:
t = kStr;
break;
}
break;
default:
unreachable;
}
} while (t != kDone);
errno = e;
return i;
}
#endif
static int startswith(const char *s, const char *prefix) {
for (;;) {
if (!*prefix) return 1;
if (!*s) return 0;
if (*s++ != *prefix++) return 0;
}
}
static int getparm(char *buf, int n)
{
while (n-- > 0) {
while (*buf != ';' && *buf)
buf++;
if (*buf)
buf++;
}
return small_atoi(buf);
}
/* xterm ansi mouse sequence status bits ESC [ < status;y;x {M|m} */
#define ANSI_MOUSE_BUTTON_MASK 0x03
#define ANSI_MOUSE_BUTTON_L 0x00
#define ANSI_MOUSE_BUTTON_M 0x01
#define ANSI_MOUSE_BUTTON_R 0x02
#define AMSI_MOUSE_MOD_MASK 0x1c
#define ANSI_MOUSE_SHIFT 0x04
#define ANSI_MOUSE_ALT 0x08
#define ANSI_MOUSE_CTRL 0x10
#define ANSI_MOUSE_MOTION 0x20
#define ANSI_MOUSE_WHEEL_MASK 0x40
#define AMSI_MOUSE_WHEEL_DOWN 0x01 /* otherwise up */
#define ANSI_MOUSE_BUTTON_UP 0x80 /* otherwise down */
const char *describemouseevent(int e) {
static char buf[32];
sprintf(buf, "%02x", e);
if (e & 0x10) strcat(buf, " ctrl");
if (e & 0x08) strcat(buf, " alt");
if (e & 0x04) strcat(buf, " shift");
if (e & 0x40) {
strcat(buf, " wheel");
if (e & 0x01) {
strcat(buf, " down");
} else {
strcat(buf, " up");
}
} else {
switch (e & 3) {
case 0:
strcat(buf, " left");
break;
case 1:
strcat(buf, " middle");
break;
case 2:
strcat(buf, " right");
break;
default:
break;
}
if (e & 0x20) {
strcat(buf, " motion");
} else if (e & 0x80) {
strcat(buf, " up");
} else {
strcat(buf, " down");
}
}
return buf;
}
/* convert ansi mouse event to unikey and modifiers */
static int mouse_to_unikey(int e, int *modkeys)
{
int status = 0;
int k;
if (e & 0x10) status |= kCtrl;
if (e & 0x08) status |= kAlt;
if (e & 0x04) status |= kShift;
if (e & 0x40) { /* wheel */
if (e & 0x01) {
k = scroll_reverse? kMouseWheelUp: kMouseWheelDown;
} else {
k = scroll_reverse? kMouseWheelDown: kMouseWheelUp;
}
} else {
if (e & 0x20) { /* motion */
e &= 3;
k = !e? kMouseLeftDrag: e==1? kMouseMiddleDrag: e==2? kMouseRightDrag: kMouseMotion;
} else if (e & 0x80) { /* up */
e &= 3;
k = !e? kMouseLeftUp: e==1? kMouseMiddleUp: e==2? kMouseRightUp: 0;
} else { /* down */
e &= 3;
k = !e? kMouseLeftDown: e==1? kMouseMiddleDown: e==2? kMouseRightDown: 0;
}
}
*modkeys = status;
return k;
}
int isdigit(int c)
{
return (unsigned) (c - '0') < 10u;
}
/*
* Check and convert ANSI mouse sequence to unicode mouse event.
* Format: ESC [ < status;y;x {m|M}
* Returns -1 if not ANSI mouse sequence.
*/
int ansi_to_unimouse(char *buf, int n, int *x, int *y, int *modkeys, int *status)
{
char *p;
int k;
struct timeval t;
long tick;
static long lasttick;
static int lastkey;
if (!startswith(buf, "\e[<") || (buf[n-1] != 'm' && buf[n-1] != 'M'))
return -1;
p = buf + 3;
if (!isdigit(*p)) return -1;
*status = getparm(p, 0);
*x = getparm(p, 1) - 1;
*y = getparm(p, 2) - 1;
#if CHECK_MOUSE
if (*status > 93) return -1;
if (*x < 0) return -1;
if (*y < 0) return -1;
if (getparm(p, 3) != 0) return -1;
#endif
*status |= (buf[n-1] == 'm') << 7;
k = mouse_to_unikey(*status, modkeys);
if (k == kMouseLeftDown || k == kMouseLeftUp) {
gettimeofday(&t, NULL);
tick = t.tv_sec * 1000 + t.tv_usec / 1000;
if (k == kMouseLeftDown && lastkey == kMouseLeftUp
&& (tick - lasttick) < kDoubleClickTime) {
k = kMouseLeftDoubleClick;
}
lasttick = tick;
}
lastkey = k;
//printf("mouse %s at %d×%d, %s\r\n",describemouseevent(*status), *x, *y, keyname(k));
return k;
}
/*
* Check and respond to ANSI DSR (device status report).
* Format: ESC [ rows; cols R
* Returns -1 if not ANSI DSR.
*/
int ansi_dsr(char *buf, int n, int *cols, int *rows)
{
char *p;
if (!startswith(buf, "\e[") || buf[n-1] != 'R')
return -1;
p = buf + 2;
*rows = getparm(p, 0);
*cols = getparm(p, 1);
//printf("DSR terminal size is %dx%d\r\n", *cols, *rows);
return 1;
}
#define WRITE(FD, SLIT) write(FD, SLIT, strlen(SLIT))
#define PROBE_DISPLAY_SIZE "\e7\e[9979;9979H\e[6n\e8"
/* probe display size - only uses DSR for now, for ELKS/UNIX compatibility */
int tty_getsize(int *cols, int *rows)
{
int n, x, y;
char buf[32];
WRITE(1, PROBE_DISPLAY_SIZE);
if ((n = readansi(0, buf, sizeof(buf))) != -1) {
if (ansi_dsr(buf, n, &x, &y) > 0) {
*cols = x;
*rows = y;
//printf("inband signaling says terminal size is %d×%d\r\n", x, y);
return 1;
}
}
return -1;
}