src/termbox2.c

filter: Statementstmt 67.2% 287 / 427 Branchbran 54.1% 249 / 460 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.4% 536 / 887 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/termbox2.h ↑ Summary lib/Term/Fabulous/Termbox.xs »
1 /*
2 * termbox2.c - the single translation unit that compiles termbox2 in.
3 *
4 * perl.h stays out of this file on purpose: termbox2's implementation
5 * defines feature-test macros and hundreds of static helpers that have
6 * no business meeting Perl's macros.
7 *
8 * Besides the library this file holds the readers termbox2 lacks: Alt
9 * keys, SGR mouse reports and the kitty keyboard protocol's key
10 * reports. They run as termbox2's TB_FUNC_EXTRACT_PRE hook, before its
11 * own parsers, and read the raw input buffer, which is static to this
12 * translation unit. It also holds inline mode (tf_init_inline and its
13 * helpers) and the terminal queries, which reach into termbox2's
14 * internals the same way, and the end-of-input check of
15 * tf_input_ended(). The operating system is reached only through the
16 * platform layer at the end of termbox2.h (open_tty, init_fds,
17 * wait_input, now_ms), except in tf_input_ended() and
18 * tf_window_cell_size(), which ask it questions termbox2 never asks.
19 */
20 #define TB_IMPL
21 #include "tf_termbox.h"
22  
23 #ifdef __sun
24 #include <sys/filio.h> /* FIONREAD */
25 #endif
26  
27 59 int tf_cluster_width(const uint32_t *codepoints, size_t count) {
28 59 return tb_cluster_width((uint32_t *)codepoints, count);
29 }
30  
31 /* Any larger coordinate or button number is not a mouse report. */
32 #define TF_MOUSE_NUMBER_LIMIT 65535
33  
34 /*
35 * ESC followed by one key, both in the buffer at once: Alt plus that key.
36 * Alt+letter, Alt+Enter and Alt+umlaut arrive this way; ESC ESC, ESC [
37 * and ESC O begin other sequences and are left to termbox2, which
38 * then also delivers a lone ESC as the Escape key.
39 */
40 3 static int tf_extract_alt_key(struct tb_event *event, size_t *consumed) {
41 3 const unsigned char *buf = (const unsigned char *)global.in.buf;
42 3 size_t len = global.in.len;
43 unsigned char key_byte;
44 size_t key_len;
45  
46 3 ▶ if (len < 2) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return TB_ERR; 0 3 3
47 3 key_byte = buf[1];
48 3 ▶ if (key_byte == 0x1b || key_byte == '[' || key_byte == 'O') return TB_ERR;
Branch: 3 branches
BranchTrueFalseTotal
if (key_byte == 0x1b || key_byte == '[' || key_byte == 'O') return TB_ERR; 3 0 3
if (key_byte == 0x1b || key_byte == '[' || key_byte == 'O') return TB_ERR; 3 0 3
if (key_byte == 0x1b || key_byte == '[' || key_byte == 'O') return TB_ERR; 0 3 3
49 3 ▶ if (key_byte >= 0x80 && key_byte < 0xc0) return TB_ERR; /* a stray UTF-8 continuation byte */
Branch: 2 branches
BranchTrueFalseTotal
if (key_byte >= 0x80 && key_byte < 0xc0) return TB_ERR; /* a stray UTF-8 continuation byte */ 1 2 3
if (key_byte >= 0x80 && key_byte < 0xc0) return TB_ERR; /* a stray UTF-8 continuation byte */ 0 1 1
50  
51 3 ▶ key_len = key_byte < 0x80 ? 1 : (size_t)tb_utf8_char_length((char)key_byte);
Branch: 1 branch
BranchTrueFalseTotal
key_len = key_byte < 0x80 ? 1 : (size_t)tb_utf8_char_length((char)key_byte); 1 2 3
52 3 ▶ if (len < 1 + key_len) return TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 1 + key_len) return TB_ERR_NEED_MORE; 0 3 3
53  
54 3 event->type = TB_EVENT_KEY;
55 3 event->mod = TB_MOD_ALT;
56 3 ▶ if (key_byte < 0x20 || key_byte == 0x7f) {
Branch: 2 branches
BranchTrueFalseTotal
if (key_byte < 0x20 || key_byte == 0x7f) { 2 1 3
if (key_byte < 0x20 || key_byte == 0x7f) { 0 2 2
57 /* A control byte is its own key code, with Ctrl set, as termbox2 reports it. */
58 1 event->key = key_byte;
59 1 event->ch = 0;
60 1 event->mod |= TB_MOD_CTRL;
61 } else {
62 2 event->key = 0;
63 2 tb_utf8_char_to_unicode(&event->ch, (const char *)buf + 1);
64 }
65 3 *consumed = 1 + key_len;
66 3 return TB_OK;
67 }
68  
69 /* The key code of an SGR button number, after its modifier and motion bits are masked off. */
70 11 static uint16_t tf_mouse_key(int button, int moving, int released) {
71 11 int low = button & 3;
72  
73 11 ▶ if (button & 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (button & 64) { 4 7 11
74 4 ▶ if (low == 0) return TB_KEY_MOUSE_WHEEL_UP;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 0) return TB_KEY_MOUSE_WHEEL_UP; 1 3 4
75 3 ▶ if (low == 1) return TB_KEY_MOUSE_WHEEL_DOWN;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 1) return TB_KEY_MOUSE_WHEEL_DOWN; 1 2 3
76 2 ▶ if (low == 2) return TF_KEY_MOUSE_WHEEL_LEFT;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 2) return TF_KEY_MOUSE_WHEEL_LEFT; 1 1 2
77 1 return TF_KEY_MOUSE_WHEEL_RIGHT;
78 }
79 7 ▶ if (released) return TB_KEY_MOUSE_RELEASE;
Branch: 1 branch
BranchTrueFalseTotal
if (released) return TB_KEY_MOUSE_RELEASE; 2 5 7
80 5 ▶ if (low == 0) return TB_KEY_MOUSE_LEFT;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 0) return TB_KEY_MOUSE_LEFT; 3 2 5
81 2 ▶ if (low == 1) return TB_KEY_MOUSE_MIDDLE;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 1) return TB_KEY_MOUSE_MIDDLE; 0 2 2
82 2 ▶ if (low == 2) return TB_KEY_MOUSE_RIGHT;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 2) return TB_KEY_MOUSE_RIGHT; 0 2 2
83 2 ▶ return moving ? TF_KEY_MOUSE_MOVE : TB_KEY_MOUSE_RELEASE;
Branch: 1 branch
BranchTrueFalseTotal
return moving ? TF_KEY_MOUSE_MOVE : TB_KEY_MOUSE_RELEASE; 1 1 2
84 }
85  
86 /* The press key of the button an SGR release ("m") names. Button number
87 * 3 is the older "some button was released" form: 0, unknown. */
88 2 static uint32_t tf_released_button(int button) {
89 2 int low = button & 3;
90  
91 2 ▶ if (button & 64) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (button & 64) return 0; 0 2 2
92 2 ▶ if (low == 0) return TB_KEY_MOUSE_LEFT;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 0) return TB_KEY_MOUSE_LEFT; 1 1 2
93 1 ▶ if (low == 1) return TB_KEY_MOUSE_MIDDLE;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 1) return TB_KEY_MOUSE_MIDDLE; 0 1 1
94 1 ▶ if (low == 2) return TB_KEY_MOUSE_RIGHT;
Branch: 1 branch
BranchTrueFalseTotal
if (low == 2) return TB_KEY_MOUSE_RIGHT; 1 0 1
95 0 return 0;
96 }
97  
98 /*
99 * An SGR mouse report: ESC [ < button ; column ; row M, or m for a
100 * release. Bits 2, 3 and 4 of the button number are Shift, Alt and
101 * Ctrl, bit 5 is motion, bit 6 marks the wheel (0 up, 1 down, 2 left,
102 * 3 right); a button number of 3 with the motion bit is the pointer
103 * moving with no button held. A release keeps the button it names in
104 * the event's ch (see tf_released_button). Buttons 8 to 11 (bit 7) are left to
105 * termbox2. Other encodings (X10, urxvt) are left to it as well.
106 */
107 13 static int tf_extract_sgr_mouse(struct tb_event *event, size_t *consumed) {
108 13 const char *buf = global.in.buf;
109 13 size_t len = global.in.len;
110 13 int numbers[3] = {-1, -1, -1};
111 13 int count = 0;
112 13 char trail = 0;
113 size_t i;
114  
115 13 ▶ if (len < 3) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 3) return TB_ERR; 0 13 13
116 13 ▶ if (buf[2] != '<') return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[2] != '<') return TB_ERR; 0 13 13
117  
118 96 ▶ for (i = 3; i < len && trail == 0; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 3; i < len && trail == 0; i++) { 86 10 96
for (i = 3; i < len && trail == 0; i++) { 83 3 86
119 83 char c = buf[i];
120 83 ▶ if (c >= '0' && c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') { 83 0 83
if (c >= '0' && c <= '9') { 48 35 83
121 48 ▶ if (numbers[count] < 0) numbers[count] = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[count] < 0) numbers[count] = 0; 37 11 48
122 48 numbers[count] = numbers[count] * 10 + (c - '0');
123 48 ▶ if (numbers[count] > TF_MOUSE_NUMBER_LIMIT) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[count] > TF_MOUSE_NUMBER_LIMIT) return TB_ERR; 0 48 48
124 35 ▶ } else if (c == ';' && count < 2 && numbers[count] >= 0) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == ';' && count < 2 && numbers[count] >= 0) { 24 11 35
} else if (c == ';' && count < 2 && numbers[count] >= 0) { 24 0 24
} else if (c == ';' && count < 2 && numbers[count] >= 0) { 24 0 24
125 24 count++;
126 11 ▶ } else if ((c == 'M' || c == 'm') && count == 2 && numbers[2] >= 0) {
Branch: 4 branches
BranchTrueFalseTotal
} else if ((c == 'M' || c == 'm') && count == 2 && numbers[2] >= 0) { 2 9 11
} else if ((c == 'M' || c == 'm') && count == 2 && numbers[2] >= 0) { 2 0 2
} else if ((c == 'M' || c == 'm') && count == 2 && numbers[2] >= 0) { 11 0 11
} else if ((c == 'M' || c == 'm') && count == 2 && numbers[2] >= 0) { 11 0 11
127 11 trail = c;
128 } else {
129 0 return TB_ERR;
130 }
131 }
132 13 ▶ if (trail == 0) return TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (trail == 0) return TB_ERR_NEED_MORE; 2 11 13
133 11 ▶ if (numbers[0] & 128) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[0] & 128) return TB_ERR; 0 11 11
134  
135 11 event->type = TB_EVENT_MOUSE;
136 11 event->key = tf_mouse_key(numbers[0], numbers[0] & 32, trail == 'm');
137 11 ▶ event->ch = trail == 'm' ? tf_released_button(numbers[0]) : 0;
Branch: 1 branch
BranchTrueFalseTotal
event->ch = trail == 'm' ? tf_released_button(numbers[0]) : 0; 2 9 11
138 11 event->mod = 0;
139 11 ▶ if (numbers[0] & 4) event->mod |= TB_MOD_SHIFT;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[0] & 4) event->mod |= TB_MOD_SHIFT; 1 10 11
140 11 ▶ if (numbers[0] & 8) event->mod |= TB_MOD_ALT;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[0] & 8) event->mod |= TB_MOD_ALT; 0 11 11
141 11 ▶ if (numbers[0] & 16) event->mod |= TB_MOD_CTRL;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[0] & 16) event->mod |= TB_MOD_CTRL; 1 10 11
142 11 ▶ if (numbers[0] & 32) event->mod |= TB_MOD_MOTION;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[0] & 32) event->mod |= TB_MOD_MOTION; 2 9 11
143 11 event->x = numbers[1] > 0 ? numbers[1] - 1 : 0;
144 11 event->y = numbers[2] > 0 ? numbers[2] - 1 : 0;
145 11 *consumed = i;
146 11 return TB_OK;
147 }
148  
149 /* Any larger number is not a key report: the largest Unicode codepoint. */
150 #define TF_KEY_NUMBER_LIMIT 0x10ffff
151  
152 /* kitty's numbers for the keys of its functional key table that have
153 * no legacy encoding, and the private use area they come from. */
154 #define TF_KITTY_CAPS_LOCK 57358
155 #define TF_KITTY_MENU 57363
156 #define TF_KITTY_F13 57376
157 #define TF_KITTY_MUTE_VOLUME 57440
158 #define TF_KITTY_KP_BEGIN 57427
159 #define TF_PRIVATE_USE_FIRST 0xe000
160 #define TF_PRIVATE_USE_LAST 0xf8ff
161  
162 /*
163 * The numbers of a CSI key report, ESC [ code:shifted ; modifiers ...
164 * final, that matter here; -1 marks one that is absent. Further
165 * sub-fields (base layout key, event type) and fields (text) are read
166 * over and ignored.
167 */
168 struct tf_csi_key {
169 int code;
170 int shifted;
171 int modifiers;
172 char final;
173 };
174  
175 34 static void tf_store_csi_number(struct tf_csi_key *report, int field, int sub_field, int value) {
176 34 ▶ if (field == 0 && sub_field == 0) report->code = value;
Branch: 2 branches
BranchTrueFalseTotal
if (field == 0 && sub_field == 0) report->code = value; 21 13 34
if (field == 0 && sub_field == 0) report->code = value; 20 1 21
177 34 ▶ if (field == 0 && sub_field == 1) report->shifted = value;
Branch: 2 branches
BranchTrueFalseTotal
if (field == 0 && sub_field == 1) report->shifted = value; 21 13 34
if (field == 0 && sub_field == 1) report->shifted = value; 1 20 21
178 34 ▶ if (field == 1 && sub_field == 0) report->modifiers = value;
Branch: 2 branches
BranchTrueFalseTotal
if (field == 1 && sub_field == 0) report->modifiers = value; 13 21 34
if (field == 1 && sub_field == 0) report->modifiers = value; 13 0 13
179 34 }
180  
181 /*
182 * Reads ESC [ followed by decimal numbers, ':' and ';', up to its final
183 * byte. Any other byte, such as the private markers '<', '?' and '>',
184 * means it is not a key report.
185 */
186 20 static int tf_parse_csi_key(struct tf_csi_key *report, size_t *length) {
187 20 const char *buf = global.in.buf;
188 20 size_t len = global.in.len;
189 20 int field = 0;
190 20 int sub_field = 0;
191 20 int value = -1;
192 size_t i;
193  
194 20 report->code = -1;
195 20 report->shifted = -1;
196 20 report->modifiers = -1;
197 96 ▶ for (i = 2; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < len; i++) { 94 2 96
198 94 char c = buf[i];
199 94 ▶ if (c >= '0' && c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') { 94 0 94
if (c >= '0' && c <= '9') { 60 34 94
200 60 value = (value < 0 ? 0 : value) * 10 + (c - '0');
201 60 ▶ if (value > TF_KEY_NUMBER_LIMIT) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (value > TF_KEY_NUMBER_LIMIT) return TB_ERR; 0 60 60
202 60 continue;
203 }
204 34 tf_store_csi_number(report, field, sub_field, value);
205 34 value = -1;
206 34 ▶ if (c == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ':') { 1 33 34
207 1 sub_field++;
208 33 ▶ } else if (c == ';') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == ';') { 15 18 33
209 15 ▶ if (++field > 2) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (++field > 2) return TB_ERR; 0 15 15
210 15 sub_field = 0;
211 18 ▶ } else if (c >= 0x40 && c <= 0x7e) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c >= 0x40 && c <= 0x7e) { 18 0 18
} else if (c >= 0x40 && c <= 0x7e) { 18 0 18
212 18 report->final = c;
213 18 *length = i + 1;
214 18 return TB_OK;
215 } else {
216 0 return TB_ERR;
217 }
218 }
219 2 return TB_ERR_NEED_MORE;
220 }
221  
222 /* kitty sends 1 plus a bit field; the lock keys (Caps Lock, Num Lock) are dropped. */
223 17 static uint8_t tf_kitty_modifiers(int encoded) {
224 17 int bits = encoded > 0 ? encoded - 1 : 0;
225 17 uint8_t mod = 0;
226  
227 17 ▶ if (bits & 1) mod |= TB_MOD_SHIFT;
Branch: 1 branch
BranchTrueFalseTotal
if (bits & 1) mod |= TB_MOD_SHIFT; 4 13 17
228 17 ▶ if (bits & 2) mod |= TB_MOD_ALT;
Branch: 1 branch
BranchTrueFalseTotal
if (bits & 2) mod |= TB_MOD_ALT; 2 15 17
229 17 ▶ if (bits & 4) mod |= TB_MOD_CTRL;
Branch: 1 branch
BranchTrueFalseTotal
if (bits & 4) mod |= TB_MOD_CTRL; 7 10 17
230 17 ▶ if (bits & 8) mod |= TF_MOD_SUPER;
Branch: 1 branch
BranchTrueFalseTotal
if (bits & 8) mod |= TF_MOD_SUPER; 2 15 17
231 17 ▶ if (bits & 16) mod |= TF_MOD_HYPER;
Branch: 1 branch
BranchTrueFalseTotal
if (bits & 16) mod |= TF_MOD_HYPER; 0 17 17
232 17 ▶ if (bits & 32) mod |= TF_MOD_META;
Branch: 1 branch
BranchTrueFalseTotal
if (bits & 32) mod |= TF_MOD_META; 0 17 17
233 17 return mod;
234 }
235  
236 17 static int tf_key_event(struct tb_event *event, uint16_t key, uint32_t ch, uint8_t mod) {
237 17 event->type = TB_EVENT_KEY;
238 17 event->key = key;
239 17 event->ch = ch;
240 17 event->mod = mod;
241 17 return TB_OK;
242 }
243  
244 /* The TF_KEY_* code of a kitty functional key number, 0 for none. The
245 * codes run in the order of the numbers (see tf_termbox.h). */
246 14 static uint16_t tf_kitty_functional_key(int code) {
247 14 ▶ if (code >= TF_KITTY_CAPS_LOCK && code <= TF_KITTY_MENU) return (uint16_t)(TF_KEY_CAPS_LOCK - (code - TF_KITTY_CAPS_LOCK));
Branch: 2 branches
BranchTrueFalseTotal
if (code >= TF_KITTY_CAPS_LOCK && code <= TF_KITTY_MENU) return (uint16_t)(TF_KEY_CAPS_LOCK - (code - TF_KITTY_CAPS_LOCK)); 3 11 14
if (code >= TF_KITTY_CAPS_LOCK && code <= TF_KITTY_MENU) return (uint16_t)(TF_KEY_CAPS_LOCK - (code - TF_KITTY_CAPS_LOCK)); 0 3 3
248 14 ▶ if (code >= TF_KITTY_F13 && code <= TF_KITTY_MUTE_VOLUME) return (uint16_t)(TF_KEY_F13 - (code - TF_KITTY_F13));
Branch: 2 branches
BranchTrueFalseTotal
if (code >= TF_KITTY_F13 && code <= TF_KITTY_MUTE_VOLUME) return (uint16_t)(TF_KEY_F13 - (code - TF_KITTY_F13)); 3 11 14
if (code >= TF_KITTY_F13 && code <= TF_KITTY_MUTE_VOLUME) return (uint16_t)(TF_KEY_F13 - (code - TF_KITTY_F13)); 3 0 3
249 11 return 0;
250 }
251  
252 /* The key of CSI number ~, 0 for a number that is not a key. */
253 1 static uint16_t tf_tilde_key(int number) {
254 1 switch (number) {
255 0 case 2: return TB_KEY_INSERT;
256 1 case 3: return TB_KEY_DELETE;
257 0 case 5: return TB_KEY_PGUP;
258 0 case 6: return TB_KEY_PGDN;
259 0 case 7: return TB_KEY_HOME;
260 0 case 8: return TB_KEY_END;
261 0 case 11: return TB_KEY_F1;
262 0 case 12: return TB_KEY_F2;
263 0 case 13: return TB_KEY_F3;
264 0 case 14: return TB_KEY_F4;
265 0 case 15: return TB_KEY_F5;
266 0 case 17: return TB_KEY_F6;
267 0 case 18: return TB_KEY_F7;
268 0 case 19: return TB_KEY_F8;
269 0 case 20: return TB_KEY_F9;
270 0 case 21: return TB_KEY_F10;
271 0 case 23: return TB_KEY_F11;
272 0 case 24: return TB_KEY_F12;
273 0 case TF_KITTY_KP_BEGIN: return TF_KEY_KP_BEGIN;
274 }
275 0 return 0;
276 }
277  
278 /* The key of CSI 1 ; modifiers letter, 0 for a letter that is not a key.
279 * F3 has no letter: CSI 1 ; modifiers R is also a cursor position report. */
280 3 static uint16_t tf_letter_key(char final) {
281 3 switch (final) {
282 2 case 'A': return TB_KEY_ARROW_UP;
283 0 case 'B': return TB_KEY_ARROW_DOWN;
284 0 case 'C': return TB_KEY_ARROW_RIGHT;
285 0 case 'D': return TB_KEY_ARROW_LEFT;
286 1 case 'E': return TF_KEY_KP_BEGIN;
287 0 case 'F': return TB_KEY_END;
288 0 case 'H': return TB_KEY_HOME;
289 0 case 'P': return TB_KEY_F1;
290 0 case 'Q': return TB_KEY_F2;
291 0 case 'S': return TB_KEY_F4;
292 }
293 0 return 0;
294 }
295  
296 /*
297 * The legacy forms of the function keys, CSI number ; modifiers ~ and
298 * CSI 1 ; modifiers letter, when they carry modifiers: kitty adds Super,
299 * Hyper, Meta and the lock keys to the modifiers termbox2 knows. The
300 * unmodified forms are left to termbox2, which knows them from
301 * terminfo, except for the keypad's Begin key, which it has no code for.
302 */
303 4 static int tf_function_key_event(const struct tf_csi_key *report, struct tb_event *event) {
304 4 ▶ uint16_t key = report->final == '~' ? tf_tilde_key(report->code) : tf_letter_key(report->final);
Branch: 1 branch
BranchTrueFalseTotal
uint16_t key = report->final == '~' ? tf_tilde_key(report->code) : tf_letter_key(report->final); 1 3 4
305  
306 4 ▶ if (key == 0) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (key == 0) return TB_ERR; 0 4 4
307 4 ▶ if (report->final != '~' && report->code > 1) return TB_ERR;
Branch: 2 branches
BranchTrueFalseTotal
if (report->final != '~' && report->code > 1) return TB_ERR; 3 1 4
if (report->final != '~' && report->code > 1) return TB_ERR; 0 3 3
308 4 ▶ if (report->modifiers < 0 && key != TF_KEY_KP_BEGIN) return TB_ERR;
Branch: 2 branches
BranchTrueFalseTotal
if (report->modifiers < 0 && key != TF_KEY_KP_BEGIN) return TB_ERR; 2 2 4
if (report->modifiers < 0 && key != TF_KEY_KP_BEGIN) return TB_ERR; 1 1 2
309 3 return tf_key_event(event, key, 0, tf_kitty_modifiers(report->modifiers));
310 }
311  
312 /*
313 * Escape, Enter, Tab, Backspace and the other control codes. Without
314 * Ctrl they come as termbox2 reports their bytes: the byte as the key,
315 * with TB_MOD_CTRL on all but Escape, and Shift+Tab as TB_KEY_BACK_TAB.
316 * With Ctrl, which their bytes cannot carry, ch holds the byte instead
317 * and mod is exact.
318 */
319 4 static int tf_kitty_control_key(struct tb_event *event, int code, uint8_t mod) {
320 4 ▶ if (mod & TB_MOD_CTRL) return tf_key_event(event, 0, (uint32_t)code, mod);
Branch: 1 branch
BranchTrueFalseTotal
if (mod & TB_MOD_CTRL) return tf_key_event(event, 0, (uint32_t)code, mod); 1 3 4
321 3 ▶ if (code == TB_KEY_TAB && mod == TB_MOD_SHIFT) return tf_key_event(event, TB_KEY_BACK_TAB, 0, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (code == TB_KEY_TAB && mod == TB_MOD_SHIFT) return tf_key_event(event, TB_KEY_BACK_TAB, 0, 0); 1 2 3
if (code == TB_KEY_TAB && mod == TB_MOD_SHIFT) return tf_key_event(event, TB_KEY_BACK_TAB, 0, 0); 1 0 1
322 2 ▶ if (code == TB_KEY_ESC) return tf_key_event(event, TB_KEY_ESC, 0, mod);
Branch: 1 branch
BranchTrueFalseTotal
if (code == TB_KEY_ESC) return tf_key_event(event, TB_KEY_ESC, 0, mod); 1 1 2
323 1 return tf_key_event(event, (uint16_t)code, 0, mod | TB_MOD_CTRL);
324 }
325  
326 /*
327 * The control byte terminals send for Ctrl and this key, or -1 when it
328 * has none or its byte is also another key's: Ctrl+H is Backspace,
329 * Ctrl+I Tab, Ctrl+M Enter and Ctrl+[ Escape.
330 */
331 4 static int tf_control_byte(int code) {
332 4 ▶ if (code >= 'A' && code <= 'Z') code += 'a' - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (code >= 'A' && code <= 'Z') code += 'a' - 'A'; 4 0 4
if (code >= 'A' && code <= 'Z') code += 'a' - 'A'; 0 4 4
333 4 ▶ if (code == 'h' || code == 'i' || code == 'm') return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (code == 'h' || code == 'i' || code == 'm') return -1; 4 0 4
if (code == 'h' || code == 'i' || code == 'm') return -1; 3 1 4
if (code == 'h' || code == 'i' || code == 'm') return -1; 0 3 3
334 3 ▶ if (code >= 'a' && code <= 'z') return code - 'a' + 1;
Branch: 2 branches
BranchTrueFalseTotal
if (code >= 'a' && code <= 'z') return code - 'a' + 1; 3 0 3
if (code >= 'a' && code <= 'z') return code - 'a' + 1; 3 0 3
335 0 ▶ if (code == ' ') return 0x00;
Branch: 1 branch
BranchTrueFalseTotal
if (code == ' ') return 0x00; 0 0 0
336 0 ▶ if (code == '\\') return 0x1c;
Branch: 1 branch
BranchTrueFalseTotal
if (code == '\\') return 0x1c; 0 0 0
337 0 ▶ if (code == ']') return 0x1d;
Branch: 1 branch
BranchTrueFalseTotal
if (code == ']') return 0x1d; 0 0 0
338 0 return -1;
339 }
340  
341 /*
342 * A key that types text, reported because of its modifiers. With Ctrl,
343 * a key with a control byte of its own comes as termbox2 reports that
344 * byte, any other with its unshifted character in ch; Shift stays a
345 * modifier either way. Without Ctrl, ch is the character Shift makes,
346 * as legacy terminals send it after the Escape of Alt.
347 */
348 7 static int tf_kitty_text_key(struct tb_event *event, int code, int shifted, uint8_t mod) {
349 int control_byte;
350  
351 7 ▶ if (mod & TB_MOD_CTRL) {
Branch: 1 branch
BranchTrueFalseTotal
if (mod & TB_MOD_CTRL) { 4 3 7
352 4 control_byte = tf_control_byte(code);
353 4 ▶ if (control_byte >= 0) return tf_key_event(event, (uint16_t)control_byte, 0, mod);
Branch: 1 branch
BranchTrueFalseTotal
if (control_byte >= 0) return tf_key_event(event, (uint16_t)control_byte, 0, mod); 3 1 4
354 1 return tf_key_event(event, 0, (uint32_t)code, mod);
355 }
356 3 ▶ if (!(mod & TB_MOD_SHIFT)) return tf_key_event(event, 0, (uint32_t)code, mod);
Branch: 1 branch
BranchTrueFalseTotal
if (!(mod & TB_MOD_SHIFT)) return tf_key_event(event, 0, (uint32_t)code, mod); 2 1 3
357 1 ▶ if (shifted > 0) return tf_key_event(event, 0, (uint32_t)shifted, mod & ~TB_MOD_SHIFT);
Branch: 1 branch
BranchTrueFalseTotal
if (shifted > 0) return tf_key_event(event, 0, (uint32_t)shifted, mod & ~TB_MOD_SHIFT); 1 0 1
358 0 ▶ if (code >= 'a' && code <= 'z') return tf_key_event(event, 0, (uint32_t)(code - 'a' + 'A'), mod & ~TB_MOD_SHIFT);
Branch: 2 branches
BranchTrueFalseTotal
if (code >= 'a' && code <= 'z') return tf_key_event(event, 0, (uint32_t)(code - 'a' + 'A'), mod & ~TB_MOD_SHIFT); 0 0 0
if (code >= 'a' && code <= 'z') return tf_key_event(event, 0, (uint32_t)(code - 'a' + 'A'), mod & ~TB_MOD_SHIFT); 0 0 0
359 0 return tf_key_event(event, 0, (uint32_t)code, mod);
360 }
361  
362 /*
363 * A kitty key report, CSI code ; modifiers u. kitty sends it for keys
364 * the legacy encodings cannot tell apart once the program asked for it
365 * (see Term::Fabulous), and for keys that have no legacy encoding at
366 * all even before that. The keys of the private use area that are not
367 * in the functional key table, such as the modifier keys themselves,
368 * are not reported.
369 */
370 14 static int tf_kitty_key_event(const struct tf_csi_key *report, struct tb_event *event) {
371 14 int code = report->code;
372 14 uint8_t mod = tf_kitty_modifiers(report->modifiers);
373 uint16_t functional;
374  
375 14 ▶ if (code < 0) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (code < 0) return TB_ERR; 0 14 14
376 14 functional = tf_kitty_functional_key(code);
377 14 ▶ if (functional != 0) return tf_key_event(event, functional, 0, mod);
Branch: 1 branch
BranchTrueFalseTotal
if (functional != 0) return tf_key_event(event, functional, 0, mod); 3 11 14
378 11 ▶ if (code >= TF_PRIVATE_USE_FIRST && code <= TF_PRIVATE_USE_LAST) return TB_ERR;
Branch: 2 branches
BranchTrueFalseTotal
if (code >= TF_PRIVATE_USE_FIRST && code <= TF_PRIVATE_USE_LAST) return TB_ERR; 0 11 11
if (code >= TF_PRIVATE_USE_FIRST && code <= TF_PRIVATE_USE_LAST) return TB_ERR; 0 0 0
379 11 ▶ if (code < 0x20 || code == 0x7f) return tf_kitty_control_key(event, code, mod);
Branch: 2 branches
BranchTrueFalseTotal
if (code < 0x20 || code == 0x7f) return tf_kitty_control_key(event, code, mod); 7 4 11
if (code < 0x20 || code == 0x7f) return tf_kitty_control_key(event, code, mod); 0 7 7
380 7 return tf_kitty_text_key(event, code, report->shifted, mod);
381 }
382  
383 20 static int tf_extract_csi_key(struct tb_event *event, size_t *consumed) {
384 struct tf_csi_key report;
385 size_t length;
386 int rv;
387  
388 20 ▶ if_err_return(rv, tf_parse_csi_key(&report, &length));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tf_parse_csi_key(&report, &length)); 2 18 20
389 18 ▶ rv = report.final == 'u' ? tf_kitty_key_event(&report, event) : tf_function_key_event(&report, event);
Branch: 1 branch
BranchTrueFalseTotal
rv = report.final == 'u' ? tf_kitty_key_event(&report, event) : tf_function_key_event(&report, event); 14 4 18
390 18 ▶ if (rv == TB_OK) *consumed = length;
Branch: 1 branch
BranchTrueFalseTotal
if (rv == TB_OK) *consumed = length; 17 1 18
391 18 return rv;
392 }
393  
394 36 static int tf_extract_input(struct tb_event *event, size_t *consumed) {
395 36 const char *buf = global.in.buf;
396  
397 36 ▶ if (global.in.len < 2 || buf[0] != '\x1b') return TB_ERR;
Branch: 2 branches
BranchTrueFalseTotal
if (global.in.len < 2 || buf[0] != '\x1b') return TB_ERR; 36 0 36
if (global.in.len < 2 || buf[0] != '\x1b') return TB_ERR; 0 36 36
398 36 ▶ if (buf[1] != '[') return tf_extract_alt_key(event, consumed);
Branch: 1 branch
BranchTrueFalseTotal
if (buf[1] != '[') return tf_extract_alt_key(event, consumed); 3 33 36
399 33 ▶ if (global.in.len >= 3 && buf[2] == '<') return tf_extract_sgr_mouse(event, consumed);
Branch: 2 branches
BranchTrueFalseTotal
if (global.in.len >= 3 && buf[2] == '<') return tf_extract_sgr_mouse(event, consumed); 33 0 33
if (global.in.len >= 3 && buf[2] == '<') return tf_extract_sgr_mouse(event, consumed); 13 20 33
400 20 return tf_extract_csi_key(event, consumed);
401 }
402  
403 1 int tf_install_input_parser(void) {
404 1 return tb_set_func(TB_FUNC_EXTRACT_PRE, tf_extract_input);
405 }
406  
407 /*
408 * Inline mode. termbox2 has no switch for it, so tf_init_inline_rwfd()
409 * runs the steps of tb_init_rwfd() itself and empties the capabilities
410 * that take over the screen before anything is sent: the alternate
411 * screen, entered at the start and left at tb_shutdown(), and the clear
412 * that tb_init_rwfd(), every resize and tb_shutdown() send.
413 */
414 0 static void tf_drop_screen_takeover(void) {
415 0 global.caps[TB_CAP_ENTER_CA] = "";
416 0 global.caps[TB_CAP_EXIT_CA] = "";
417 0 global.caps[TB_CAP_CLEAR_SCREEN] = "";
418 0 }
419  
420 0 int tf_init_inline(void) {
421 int rv, rfd, wfd;
422  
423 0 ▶ if (global.initialized) return TB_ERR_INIT_ALREADY;
Branch: 1 branch
BranchTrueFalseTotal
if (global.initialized) return TB_ERR_INIT_ALREADY; 0 0 0
424 0 ▶ if_err_return(rv, open_tty("/dev/tty", &rfd, &wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, open_tty("/dev/tty", &rfd, &wfd)); 0 0 0
425 0 global.ttyfd_open = 1;
426 0 return tf_init_inline_rwfd(rfd, wfd);
427 }
428  
429 0 int tf_init_inline_rwfd(int rfd, int wfd) {
430 int rv;
431  
432 0 ▶ if (global.initialized) return TB_ERR_INIT_ALREADY;
Branch: 1 branch
BranchTrueFalseTotal
if (global.initialized) return TB_ERR_INIT_ALREADY; 0 0 0
433 0 tb_reset();
434  
435 do {
436 0 ▶ if_err_break(rv, init_fds(rfd, wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_fds(rfd, wfd)); 0 0 0
437 0 ▶ if_err_break(rv, init_term_attrs());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_term_attrs()); 0 0 0
438 0 ▶ if_err_break(rv, init_term_caps());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_term_caps()); 0 0 0
439 0 tf_drop_screen_takeover();
440 0 ▶ if_err_break(rv, init_cap_trie());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_cap_trie()); 0 0 0
441 0 ▶ if_err_break(rv, init_resize_handler());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_resize_handler()); 0 0 0
442 0 ▶ if_err_break(rv, send_init_escape_codes());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, send_init_escape_codes()); 0 0 0
443 0 ▶ if_err_break(rv, bytebuf_flush(&global.out, global.wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, bytebuf_flush(&global.out, global.wfd)); 0 0 0
444 0 ▶ if_err_break(rv, update_term_size());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, update_term_size()); 0 0 0
445 0 ▶ if_err_break(rv, init_cellbuf());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_cellbuf()); 0 0 0
446 0 global.initialized = 1;
447 } while (0);
448  
449 0 ▶ if (rv != TB_OK) tb_deinit();
Branch: 1 branch
BranchTrueFalseTotal
if (rv != TB_OK) tb_deinit(); 0 0 0
450 0 return rv;
451 }
452  
453 /* Any larger row or column is not a cursor position report. */
454 #define TF_CURSOR_NUMBER_LIMIT 65535
455  
456 /*
457 * Finds a cursor position report, ESC [ row ; column R, in the input
458 * buffer from offset `from` on: TB_OK with its offset, length and
459 * numbers, or TB_ERR while there is no complete one.
460 */
461 0 static int tf_find_cursor_report(size_t from, size_t *at, size_t *length, int *row, int *column) {
462 0 const char *buf = global.in.buf;
463 0 size_t len = global.in.len;
464 size_t start, i;
465  
466 0 ▶ for (start = from; start + 1 < len; start++) {
Branch: 1 branch
BranchTrueFalseTotal
for (start = from; start + 1 < len; start++) { 0 0 0
467 0 int numbers[2] = {0, 0};
468 0 int count = 0;
469 0 int digits = 0;
470  
471 0 ▶ if (buf[start] != '\x1b' || buf[start + 1] != '[') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (buf[start] != '\x1b' || buf[start + 1] != '[') continue; 0 0 0
if (buf[start] != '\x1b' || buf[start + 1] != '[') continue; 0 0 0
472 0 ▶ for (i = start + 2; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start + 2; i < len; i++) { 0 0 0
473 0 char c = buf[i];
474 0 ▶ if (c >= '0' && c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') { 0 0 0
if (c >= '0' && c <= '9') { 0 0 0
475 0 numbers[count] = numbers[count] * 10 + (c - '0');
476 0 ▶ if (numbers[count] > TF_CURSOR_NUMBER_LIMIT) break;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[count] > TF_CURSOR_NUMBER_LIMIT) break; 0 0 0
477 0 digits++;
478 0 ▶ } else if (c == ';' && count == 0 && digits > 0) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == ';' && count == 0 && digits > 0) { 0 0 0
} else if (c == ';' && count == 0 && digits > 0) { 0 0 0
} else if (c == ';' && count == 0 && digits > 0) { 0 0 0
479 0 count = 1;
480 0 digits = 0;
481 0 ▶ } else if (c == 'R' && count == 1 && digits > 0) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == 'R' && count == 1 && digits > 0) { 0 0 0
} else if (c == 'R' && count == 1 && digits > 0) { 0 0 0
} else if (c == 'R' && count == 1 && digits > 0) { 0 0 0
482 0 *at = start;
483 0 *length = i + 1 - start;
484 0 *row = numbers[0];
485 0 *column = numbers[1];
486 0 return TB_OK;
487 } else {
488 break;
489 }
490 }
491 }
492 0 return TB_ERR;
493 }
494  
495 /*
496 * Appends what the terminal sent to the input buffer, waiting for it no
497 * later than the deadline (a time of termbox2's now_ms clock):
498 * TB_ERR_NO_EVENT once the deadline has passed. A wait a signal
499 * interrupted reads nothing and returns TB_OK. A terminal at end of
500 * file wakes the wait with nothing to append; that is TB_ERR_READ with
501 * tb_last_errno 0, as a read of 0 bytes was before. A resize that
502 * arrives meanwhile stays pending for tb_peek_event.
503 */
504 7 static int tf_read_input_before(int64_t deadline) {
505 7 int64_t left = deadline - now_ms();
506 7 size_t before = global.in.len;
507 int rv;
508  
509 7 ▶ if (left <= 0) return TB_ERR_NO_EVENT;
Branch: 1 branch
BranchTrueFalseTotal
if (left <= 0) return TB_ERR_NO_EVENT; 0 7 7
510 7 rv = wait_input((int)left, NULL);
511 #ifndef _WIN32
512 7 ▶ if (rv == TB_ERR_POLL && global.last_errno == EINTR) return TB_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (rv == TB_ERR_POLL && global.last_errno == EINTR) return TB_OK; 0 7 7
if (rv == TB_ERR_POLL && global.last_errno == EINTR) return TB_OK; 0 0 0
513 #endif
514 7 ▶ if (rv == TB_OK && global.in.len == before) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv == TB_OK && global.in.len == before) { 5 2 7
if (rv == TB_OK && global.in.len == before) { 1 4 5
515 1 global.last_errno = 0;
516 1 return TB_ERR_READ;
517 }
518 6 return rv;
519 }
520  
521 /* Sends a question to the terminal and returns the time its answer must arrive by. */
522 7 static int tf_ask_terminal(const char *question, int timeout_ms, int64_t *deadline) {
523 int rv;
524  
525 7 ▶ if_err_return(rv, bytebuf_puts(&global.out, question));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, question)); 0 7 7
526 7 ▶ if_err_return(rv, bytebuf_flush(&global.out, global.wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_flush(&global.out, global.wfd)); 0 7 7
527 7 *deadline = now_ms() + timeout_ms;
528 7 return TB_OK;
529 }
530  
531 /* Takes an answer out of the input buffer, so that only keys stay queued. */
532 6 static void tf_drop_input(size_t at, size_t length) {
533 6 memmove(global.in.buf + at, global.in.buf + at + length, global.in.len - at - length);
534 6 global.in.len -= length;
535 6 global.in.buf[global.in.len] = '\0';
536 6 }
537  
538 0 int tf_cursor_position(int timeout_ms, int *x, int *y) {
539 int64_t deadline;
540 size_t from, at, length;
541 int rv, row, column;
542  
543 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
544 0 from = global.in.len; /* input from before the question cannot hold the answer */
545 0 ▶ if_err_return(rv, tf_ask_terminal("\x1b[6n", timeout_ms, &deadline));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tf_ask_terminal("\x1b[6n", timeout_ms, &deadline)); 0 0 0
546  
547 0 ▶ while (tf_find_cursor_report(from, &at, &length, &row, &column) != TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while (tf_find_cursor_report(from, &at, &length, &row, &column) != TB_OK) { 0 0 0
548 0 ▶ if_err_return(rv, tf_read_input_before(deadline));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tf_read_input_before(deadline)); 0 0 0
549 }
550 0 tf_drop_input(at, length);
551  
552 0 *x = column > 0 ? column - 1 : 0;
553 0 *y = row > 0 ? row - 1 : 0;
554 0 return TB_OK;
555 }
556  
557 /*
558 * Finds a private mode answer, ESC [ ? parameters final with the
559 * parameters made of digits and ';', in the input buffer from offset
560 * `from` on: TB_OK with its offset and length, or TB_ERR while there is
561 * no complete one. The kitty keyboard flags end in 'u', the primary
562 * device attributes in 'c'.
563 */
564 14 static int tf_find_private_answer(size_t from, char final, size_t *at, size_t *length) {
565 14 const char *buf = global.in.buf;
566 14 size_t len = global.in.len;
567 size_t start, i;
568  
569 32 ▶ for (start = from; start + 2 < len; start++) {
Branch: 1 branch
BranchTrueFalseTotal
for (start = from; start + 2 < len; start++) { 23 9 32
570 23 ▶ if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '?') continue;
Branch: 3 branches
BranchTrueFalseTotal
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '?') continue; 7 16 23
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '?') continue; 7 0 7
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '?') continue; 1 6 7
571 35 ▶ for (i = start + 3; i < len && ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == ';'); i++) {}
Branch: 4 branches
BranchTrueFalseTotal
for (i = start + 3; i < len && ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == ';'); i++) {} 35 0 35
for (i = start + 3; i < len && ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == ';'); i++) {} 35 0 35
for (i = start + 3; i < len && ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == ';'); i++) {} 22 13 35
for (i = start + 3; i < len && ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == ';'); i++) {} 7 6 13
572 6 ▶ if (i < len && buf[i] == final) {
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && buf[i] == final) { 6 0 6
if (i < len && buf[i] == final) { 5 1 6
573 5 *at = start;
574 5 *length = i + 1 - start;
575 5 return TB_OK;
576 }
577 }
578 9 return TB_ERR;
579 }
580  
581 4 int tf_kitty_keyboard_query(int timeout_ms, int *supported) {
582 int64_t deadline;
583 size_t from, at, length;
584 int rv, attributes_arrived;
585  
586 4 *supported = 0;
587 4 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 4 4
588 4 from = global.in.len; /* input from before the question cannot hold the answer */
589 4 ▶ if_err_return(rv, tf_ask_terminal("\x1b[?u\x1b[c", timeout_ms, &deadline));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tf_ask_terminal("\x1b[?u\x1b[c", timeout_ms, &deadline)); 4 0 4
590  
591 for (;;) {
592 6 attributes_arrived = tf_find_private_answer(from, 'c', &at, &length) == TB_OK;
593 6 ▶ if (attributes_arrived) break;
Branch: 1 branch
BranchTrueFalseTotal
if (attributes_arrived) break; 2 4 6
594 4 rv = tf_read_input_before(deadline);
595 4 ▶ if (rv == TB_ERR_NO_EVENT) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rv == TB_ERR_NO_EVENT) break; 1 3 4
596 3 ▶ if (rv != TB_OK) return rv;
Branch: 1 branch
BranchTrueFalseTotal
if (rv != TB_OK) return rv; 1 2 3
597 }
598 3 ▶ if (attributes_arrived) tf_drop_input(at, length);
Branch: 1 branch
BranchTrueFalseTotal
if (attributes_arrived) tf_drop_input(at, length); 2 1 3
599  
600 3 *supported = tf_find_private_answer(from, 'u', &at, &length) == TB_OK;
601 3 ▶ if (*supported) tf_drop_input(at, length);
Branch: 1 branch
BranchTrueFalseTotal
if (*supported) tf_drop_input(at, length); 1 2 3
602 3 ▶ return attributes_arrived || *supported ? TB_OK : TB_ERR_NO_EVENT;
Branch: 2 branches
BranchTrueFalseTotal
return attributes_arrived || *supported ? TB_OK : TB_ERR_NO_EVENT; 1 2 3
return attributes_arrived || *supported ? TB_OK : TB_ERR_NO_EVENT; 0 1 1
603 }
604  
605 /*
606 * Whether the parameters of a primary device attributes answer, the
607 * ESC [ ? 62;4;22 c at `at` with `length` bytes, list the number 4:
608 * the terminal shows sixel graphics.
609 */
610 2 static int tf_lists_sixel(size_t at, size_t length) {
611 2 const char *buf = global.in.buf;
612 size_t i;
613 2 int number = 0, digits = 0;
614  
615 17 ▶ for (i = at + 3; i < at + length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = at + 3; i < at + length; i++) { 16 1 17
616 16 ▶ if (buf[i] >= '0' && buf[i] <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] >= '0' && buf[i] <= '9') { 16 0 16
if (buf[i] >= '0' && buf[i] <= '9') { 10 6 16
617 10 ▶ if (number <= TF_CURSOR_NUMBER_LIMIT) number = number * 10 + (buf[i] - '0');
Branch: 1 branch
BranchTrueFalseTotal
if (number <= TF_CURSOR_NUMBER_LIMIT) number = number * 10 + (buf[i] - '0'); 10 0 10
618 10 digits++;
619 10 continue;
620 }
621 6 ▶ if (digits > 0 && number == 4) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (digits > 0 && number == 4) return 1; 6 0 6
if (digits > 0 && number == 4) return 1; 1 5 6
622 5 number = digits = 0;
623 }
624 1 return 0;
625 }
626  
627 /*
628 * Finds a cell size report, ESC [ 6 ; height ; width t, in the input
629 * buffer from offset `from` on: TB_OK with its offset, length and
630 * numbers, or TB_ERR while there is no complete one.
631 */
632 3 static int tf_find_cell_size_report(size_t from, size_t *at, size_t *length, int *height, int *width) {
633 3 const char *buf = global.in.buf;
634 3 size_t len = global.in.len;
635 size_t start, i;
636  
637 4 ▶ for (start = from; start + 3 < len; start++) {
Branch: 1 branch
BranchTrueFalseTotal
for (start = from; start + 3 < len; start++) { 2 2 4
638 2 int numbers[2] = {0, 0};
639 2 int count = 0;
640 2 int digits = 0;
641  
642 2 ▶ if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '6' || buf[start + 3] != ';') continue;
Branch: 4 branches
BranchTrueFalseTotal
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '6' || buf[start + 3] != ';') continue; 1 1 2
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '6' || buf[start + 3] != ';') continue; 1 0 1
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '6' || buf[start + 3] != ';') continue; 1 0 1
if (buf[start] != '\x1b' || buf[start + 1] != '[' || buf[start + 2] != '6' || buf[start + 3] != ';') continue; 0 1 1
643 6 ▶ for (i = start + 4; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start + 4; i < len; i++) { 6 0 6
644 6 char c = buf[i];
645 6 ▶ if (c >= '0' && c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') { 6 0 6
if (c >= '0' && c <= '9') { 4 2 6
646 4 numbers[count] = numbers[count] * 10 + (c - '0');
647 4 ▶ if (numbers[count] > TF_CURSOR_NUMBER_LIMIT) break;
Branch: 1 branch
BranchTrueFalseTotal
if (numbers[count] > TF_CURSOR_NUMBER_LIMIT) break; 0 4 4
648 4 digits++;
649 2 ▶ } else if (c == ';' && count == 0 && digits > 0) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == ';' && count == 0 && digits > 0) { 1 1 2
} else if (c == ';' && count == 0 && digits > 0) { 1 0 1
} else if (c == ';' && count == 0 && digits > 0) { 1 0 1
650 1 count = 1;
651 1 digits = 0;
652 1 ▶ } else if (c == 't' && count == 1 && digits > 0) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == 't' && count == 1 && digits > 0) { 1 0 1
} else if (c == 't' && count == 1 && digits > 0) { 1 0 1
} else if (c == 't' && count == 1 && digits > 0) { 1 0 1
653 1 *at = start;
654 1 *length = i + 1 - start;
655 1 *height = numbers[0];
656 1 *width = numbers[1];
657 1 return TB_OK;
658 } else {
659 break;
660 }
661 }
662 }
663 2 return TB_ERR;
664 }
665  
666 /*
667 * The cell size the tty's window size gives, 0 x 0 when it has no
668 * pixels. A Windows console knows no pixel size.
669 */
670 2 static void tf_window_cell_size(int *cell_width, int *cell_height) {
671 2 *cell_width = *cell_height = 0;
672 #ifndef _WIN32
673 struct winsize size;
674  
675 2 memset(&size, 0, sizeof(size));
676 2 ▶ if (global.ttyfd < 0 || ioctl(global.ttyfd, TIOCGWINSZ, &size) != 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (global.ttyfd < 0 || ioctl(global.ttyfd, TIOCGWINSZ, &size) != 0) return; 0 2 2
if (global.ttyfd < 0 || ioctl(global.ttyfd, TIOCGWINSZ, &size) != 0) return; 0 0 0
677 0 ▶ if (size.ws_col == 0 || size.ws_row == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (size.ws_col == 0 || size.ws_row == 0) return; 0 0 0
if (size.ws_col == 0 || size.ws_row == 0) return; 0 0 0
678 0 *cell_width = size.ws_xpixel / size.ws_col;
679 0 *cell_height = size.ws_ypixel / size.ws_row;
680 0 ▶ if (*cell_width == 0 || *cell_height == 0) *cell_width = *cell_height = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*cell_width == 0 || *cell_height == 0) *cell_width = *cell_height = 0; 0 0 0
if (*cell_width == 0 || *cell_height == 0) *cell_width = *cell_height = 0; 0 0 0
681 #endif
682 }
683  
684 3 int tf_sixel_query(int timeout_ms, int *supported, int *cell_width, int *cell_height) {
685 int64_t deadline;
686 size_t from, at, length;
687 int rv, attributes_arrived, reported_height, reported_width;
688  
689 3 *supported = 0;
690 3 *cell_width = 0;
691 3 *cell_height = 0;
692 3 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 3 3
693 3 from = global.in.len; /* input from before the question cannot hold the answer */
694 3 ▶ if_err_return(rv, tf_ask_terminal("\x1b[16t\x1b[c", timeout_ms, &deadline));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tf_ask_terminal("\x1b[16t\x1b[c", timeout_ms, &deadline)); 3 0 3
695  
696 for (;;) {
697 5 attributes_arrived = tf_find_private_answer(from, 'c', &at, &length) == TB_OK;
698 5 ▶ if (attributes_arrived) break;
Branch: 1 branch
BranchTrueFalseTotal
if (attributes_arrived) break; 2 3 5
699 3 rv = tf_read_input_before(deadline);
700 3 ▶ if (rv == TB_ERR_NO_EVENT) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rv == TB_ERR_NO_EVENT) break; 1 2 3
701 2 ▶ if (rv != TB_OK) return rv;
Branch: 1 branch
BranchTrueFalseTotal
if (rv != TB_OK) return rv; 0 2 2
702 }
703 3 ▶ if (attributes_arrived) {
Branch: 1 branch
BranchTrueFalseTotal
if (attributes_arrived) { 2 1 3
704 2 *supported = tf_lists_sixel(at, length);
705 2 tf_drop_input(at, length);
706 }
707  
708 3 ▶ if (tf_find_cell_size_report(from, &at, &length, &reported_height, &reported_width) == TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (tf_find_cell_size_report(from, &at, &length, &reported_height, &reported_width) == TB_OK) { 1 2 3
709 1 tf_drop_input(at, length);
710 1 ▶ if (reported_width > 0 && reported_height > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (reported_width > 0 && reported_height > 0) { 1 0 1
if (reported_width > 0 && reported_height > 0) { 1 0 1
711 1 *cell_width = reported_width;
712 1 *cell_height = reported_height;
713 }
714 }
715 3 ▶ if (*cell_width == 0) tf_window_cell_size(cell_width, cell_height);
Branch: 1 branch
BranchTrueFalseTotal
if (*cell_width == 0) tf_window_cell_size(cell_width, cell_height); 2 1 3
716 3 ▶ return attributes_arrived ? TB_OK : TB_ERR_NO_EVENT;
Branch: 1 branch
BranchTrueFalseTotal
return attributes_arrived ? TB_OK : TB_ERR_NO_EVENT; 2 1 3
717 }
718  
719 0 int tf_cells_differ(int x, int y, int width, int height, int *differ) {
720 struct tb_cell *back, *front;
721 int rv, column, row;
722 0 int right = x + width;
723 0 int bottom = y + height;
724  
725 0 *differ = 0;
726 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
727 0 ▶ if (x < 0) x = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x < 0) x = 0; 0 0 0
728 0 ▶ if (y < 0) y = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (y < 0) y = 0; 0 0 0
729 0 ▶ if (right > global.front.width) right = global.front.width;
Branch: 1 branch
BranchTrueFalseTotal
if (right > global.front.width) right = global.front.width; 0 0 0
730 0 ▶ if (bottom > global.front.height) bottom = global.front.height;
Branch: 1 branch
BranchTrueFalseTotal
if (bottom > global.front.height) bottom = global.front.height; 0 0 0
731  
732 0 ▶ for (row = y; row < bottom; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = y; row < bottom; row++) { 0 0 0
733 0 ▶ for (column = x; column < right; column++) {
Branch: 1 branch
BranchTrueFalseTotal
for (column = x; column < right; column++) { 0 0 0
734 0 ▶ if_err_return(rv, cellbuf_get(&global.back, column, row, &back));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.back, column, row, &back)); 0 0 0
735 0 ▶ if_err_return(rv, cellbuf_get(&global.front, column, row, &front));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.front, column, row, &front)); 0 0 0
736 0 ▶ if (cell_cmp(back, front) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cell_cmp(back, front) != 0) { 0 0 0
737 0 *differ = 1;
738 0 return TB_OK;
739 }
740 }
741 }
742 0 return TB_OK;
743 }
744  
745 0 int tf_invalidate_cells(int x, int y, int width, int height) {
746 struct tb_cell *front;
747 0 uint32_t invalid = (uint32_t)-1; /* what tb_present marks the hidden cells of a wide glyph with */
748 int rv, column, row;
749 0 int right = x + width;
750 0 int bottom = y + height;
751  
752 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
753 0 ▶ if (x < 0) x = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x < 0) x = 0; 0 0 0
754 0 ▶ if (y < 0) y = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (y < 0) y = 0; 0 0 0
755 0 ▶ if (right > global.front.width) right = global.front.width;
Branch: 1 branch
BranchTrueFalseTotal
if (right > global.front.width) right = global.front.width; 0 0 0
756 0 ▶ if (bottom > global.front.height) bottom = global.front.height;
Branch: 1 branch
BranchTrueFalseTotal
if (bottom > global.front.height) bottom = global.front.height; 0 0 0
757  
758 0 ▶ for (row = y; row < bottom; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = y; row < bottom; row++) { 0 0 0
759 0 ▶ for (column = x; column < right; column++) {
Branch: 1 branch
BranchTrueFalseTotal
for (column = x; column < right; column++) { 0 0 0
760 0 ▶ if_err_return(rv, cellbuf_get(&global.front, column, row, &front));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.front, column, row, &front)); 0 0 0
761 0 ▶ if_err_return(rv, cell_set(front, &invalid, 1, (uintattr_t)-1, (uintattr_t)-1));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cell_set(front, &invalid, 1, (uintattr_t)-1, (uintattr_t)-1)); 0 0 0
762 }
763 }
764 0 return TB_OK;
765 }
766  
767 0 int tf_reset_attrs(void) {
768 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
769 0 global.last_fg = ~global.fg;
770 0 global.last_bg = ~global.bg;
771 0 return bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0]);
772 }
773  
774 0 int tf_flush(void) {
775 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
776 0 return bytebuf_flush(&global.out, global.wfd);
777 }
778  
779 /*
780 * The end of the input. On POSIX, a descriptor that reports readable
781 * with no byte to read is at end of file; a terminal that hung up
782 * cannot even say how many bytes wait (EIO on Linux, ENXIO on FreeBSD).
783 * On Windows, a console is gone when it no longer answers, a pipe when
784 * its writer went away, a file at its end.
785 */
786 #ifndef _WIN32
787 4 int tf_input_ended(int fd) {
788 fd_set fds;
789 4 struct timeval no_wait = {0, 0};
790 int waiting;
791  
792 68 ▶ FD_ZERO(&fds);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&fds); 64 4 68
793 4 FD_SET(fd, &fds);
794 4 ▶ if (select(fd + 1, &fds, NULL, NULL, &no_wait) <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (select(fd + 1, &fds, NULL, NULL, &no_wait) <= 0) return 0; 1 3 4
795 3 ▶ if (ioctl(fd, FIONREAD, &waiting) != 0) return errno == EIO || errno == ENXIO;
Branch: 3 branches
BranchTrueFalseTotal
if (ioctl(fd, FIONREAD, &waiting) != 0) return errno == EIO || errno == ENXIO; 0 3 3
if (ioctl(fd, FIONREAD, &waiting) != 0) return errno == EIO || errno == ENXIO; 0 0 0
if (ioctl(fd, FIONREAD, &waiting) != 0) return errno == EIO || errno == ENXIO; 0 0 0
796 3 return waiting == 0;
797 }
798 #else
799 int tf_input_ended(int fd) {
800 HANDLE handle = (HANDLE)_get_osfhandle(fd);
801 DWORD count;
802 DWORD error;
803  
804 if (handle == INVALID_HANDLE_VALUE) return 1;
805 switch (GetFileType(handle)) {
806 case FILE_TYPE_PIPE:
807 if (PeekNamedPipe(handle, NULL, 0, NULL, &count, NULL)) return 0;
808 error = GetLastError();
809 return error == ERROR_BROKEN_PIPE || error == ERROR_PIPE_NOT_CONNECTED;
810 case FILE_TYPE_DISK:
811 return _eof(fd) == 1;
812 case FILE_TYPE_CHAR:
813 /* A character device that is no console (NUL, a serial port)
814 * answers ERROR_INVALID_HANDLE and never ends; a console that
815 * went away fails in another way. */
816 if (GetNumberOfConsoleInputEvents(handle, &count)) return 0;
817 return GetLastError() != ERROR_INVALID_HANDLE;
818 default:
819 return 0;
820 }
821 }
822 #endif
« src/termbox2.h ↑ Summary lib/Term/Fabulous/Termbox.xs »