about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cannot_write_tests_for4
-rw-r--r--termbox/input.inl43
2 files changed, 31 insertions, 16 deletions
diff --git a/cannot_write_tests_for b/cannot_write_tests_for
index a77ef91b..8a678b37 100644
--- a/cannot_write_tests_for
+++ b/cannot_write_tests_for
@@ -3,3 +3,7 @@
 3. hide/show screen
 4. more touch event types
 5. sandbox isolation
+
+termbox issues are implementation-specific and not worth testing:
+  whether we clear junk from other processes
+  latency in interpreting low-level escape characters
diff --git a/termbox/input.inl b/termbox/input.inl
index 97ea1190..0088cd91 100644
--- a/termbox/input.inl
+++ b/termbox/input.inl
@@ -21,6 +21,9 @@ static bool starts_with(const char *s1, int len, const char *s2)
 // convert escape sequence to event, and return consumed bytes on success (failure == 0)
 static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
 {
+  static int parse_attempts = 0;
+  static const int MAX_PARSE_ATTEMPTS = 2;
+
 //?   int x = 0;
 //?   FOO("-- %d\n", len);
 //?   for (x = 0; x < len; ++x) {
@@ -48,6 +51,7 @@ static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
       event->key = TB_KEY_MOUSE_RELEASE;
       break;
     default:
+      parse_attempts = 0;
       return -6;
     }
     event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default
@@ -56,6 +60,7 @@ static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
     event->x = (uint8_t)buf[4] - 1 - 32;
     event->y = (uint8_t)buf[5] - 1 - 32;
 
+    parse_attempts = 0;
     return 6;
   }
 
@@ -66,6 +71,7 @@ static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
     if (starts_with(buf, len, keys[i])) {
       event->ch = 0;
       event->key = 0xFFFF-i;
+      parse_attempts = 0;
       return strlen(keys[i]);
     }
   }
@@ -73,35 +79,48 @@ static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
   if (starts_with(buf, len, "\033[200~")) {
     event->ch = 0;
     event->key = TB_KEY_START_PASTE;
+    parse_attempts = 0;
     return strlen("\033[200~");
   }
   if (starts_with(buf, len, "\033[201~")) {
     event->ch = 0;
     event->key = TB_KEY_END_PASTE;
+    parse_attempts = 0;
     return strlen("\033[201~");
   }
   if (starts_with(buf, len, "\033[1;5A")) {
     event->ch = 0;
     event->key = TB_KEY_CTRL_ARROW_UP;
+    parse_attempts = 0;
     return strlen("\033[1;5A");
   }
   if (starts_with(buf, len, "\033[1;5B")) {
     event->ch = 0;
     event->key = TB_KEY_CTRL_ARROW_DOWN;
+    parse_attempts = 0;
     return strlen("\033[1;5B");
   }
   if (starts_with(buf, len, "\033[1;5C")) {
     event->ch = 0;
     event->key = TB_KEY_CTRL_ARROW_RIGHT;
+    parse_attempts = 0;
     return strlen("\033[1;5C");
   }
   if (starts_with(buf, len, "\033[1;5D")) {
     event->ch = 0;
     event->key = TB_KEY_CTRL_ARROW_LEFT;
+    parse_attempts = 0;
     return strlen("\033[1;5D");
   }
 
-  return 0;
+  // no escape sequence recognized? wait a bit in case our buffer is incomplete
+  ++parse_attempts;
+  if (parse_attempts < MAX_PARSE_ATTEMPTS) return 0;
+  // still nothing? give up and consume just the esc
+  event->ch = 0;
+  event->key = TB_KEY_ESC;
+  parse_attempts = 0;
+  return 1;
 }
 
 static bool extract_event(struct tb_event *event, struct bytebuffer *inbuf)
@@ -118,23 +137,15 @@ static bool extract_event(struct tb_event *event, struct bytebuffer *inbuf)
 //?   }
   if (buf[0] == '\033') {
     int n = parse_escape_seq(event, buf, len);
+    if (n == 0) return false;
 //?     FOO("parsed: %u %u %u %u\n", n, (unsigned int)event->type, (unsigned int)event->key, event->ch);
-    if (n != 0) {
-      bool success = true;
-      if (n < 0) {
-        success = false;
-        n = -n;
-      }
-      bytebuffer_truncate(inbuf, n);
-      return success;
-    } else {
-//?       FOO("escape sequence\n");
-      // it's not escape sequence; assume it's esc
-      event->ch = 0;
-      event->key = TB_KEY_ESC;
-      bytebuffer_truncate(inbuf, 1);
-      return true;
+    bool success = true;
+    if (n < 0) {
+      success = false;
+      n = -n;
     }
+    bytebuffer_truncate(inbuf, n);
+    return success;
   }
 
   // if we're here, this is not an escape sequence and not an alt sequence
itle='Blame the previous revision' href='/akkartik/mu/blame/baremetal/504test-screen.mu?h=hlt&id=117a710a9d04c7176ce192cdb26134d2828478f9'>^
8f34dfd1 ^
966daf14 ^


a51bc7a1 ^
8f34dfd1 ^


74f1512f ^
8f34dfd1 ^





74f1512f ^
8f34dfd1 ^
cec5ef31 ^
966daf14 ^





8f34dfd1 ^
966daf14 ^


a51bc7a1 ^
8f34dfd1 ^


74f1512f ^
8f34dfd1 ^







cec5ef31 ^
966daf14 ^







1d7aae96 ^





bfcf5c72 ^
1d7aae96 ^


















































cec5ef31 ^
fdc3df04 ^
1d7aae96 ^






















cec5ef31 ^
1d7aae96 ^







a99775b4 ^








bfcf5c72 ^
a99775b4 ^

























cec5ef31 ^
a99775b4 ^














cec5ef31 ^
a99775b4 ^






a51bc7a1 ^



08c55cb2 ^
1d7aae96 ^


a99775b4 ^
a51bc7a1 ^
966daf14 ^
a51bc7a1 ^


74f1512f ^
1d7aae96 ^


a99775b4 ^
a51bc7a1 ^
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