-
Notifications
You must be signed in to change notification settings - Fork 1
/
lineedit.c
479 lines (417 loc) · 12.3 KB
/
lineedit.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
/*
* mµOS - my micro OS
*
* Copyright (C)
* 2015 Christian Thäter <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <muos/serial.h>
#include <muos/io.h>
#include <muos/error.h>
#include <muos/lib/utf8.h>
#include <string.h>
#ifdef MUOS_LINEEDIT_CALLBACK
extern void MUOS_LINEEDIT_CALLBACK (const char*);
#endif
static uint8_t cursor;
static uint8_t used;
static char buffer[MUOS_LINEEDIT_BUFFER];
#ifdef MUOS_LINEEDIT_RECALL
static char recall;
#endif
//PLANNED: strategy on input errors, desync?
//PLANNED: full line overflow, because lineedit buffer > txqueue, needs some strategy (blocking_io/atomic_io)
//#if MUOS_LINEEDIT_BUFFER >= MUOS_SERIAL_TXQUEUE
//#error MUOS_SERIAL_TXQUEUE must be bigger than MUOS_LINEEDIT_BUFFER
//#endif
static uint8_t pending;
enum
{
UTF8_1 = 1,
UTF8_2,
UTF8_3,
UTF8_4,
UTF8DROP,
ESCAPE,
CSI,
DEL,
PGUP,
PGDOWN,
POS1,
END,
OVWR,
};
#ifdef MUOS_LINEEDIT_UTF8
static void
utf8del (void)
{
uint8_t len = muos_utf8size (buffer+cursor);
used -= len;
memmove (buffer+cursor, buffer+cursor+len, used-cursor+len);
}
static void
utf8line_redraw MUOS_IO_HWPARAM()
{
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("?25l\r"));
muos_output_cstrn MUOS_IO_HWARG(buffer, cursor);
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("s"));
muos_output_cstr MUOS_IO_HWARG(buffer+cursor);
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("K\x1b[u\x1b[?25h"));
}
#endif
void
muos_lineedit_echo MUOS_IO_HWPARAM(bool echo)
{
muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo = echo;
}
bool
muos_lineedit MUOS_IO_HWPARAM()
{
uint8_t data = muos_serial_rx_byte MUOS_IO_HWARG();
if (!muos_error_check (muos_error_rx_underflow))
{
#ifdef MUOS_LINEEDIT_UTF8
if (pending == UTF8DROP)
{
if (muos_utf8cont (data))
{
return true;
}
else
pending = 0;
}
#else
if (data > 127)
{
muos_output_char MUOS_IO_HWARG(7);
return true;
}
#endif
switch (pending<<8 | data)
{
case 0x1b:
// escape
pending = ESCAPE;
break;
case 0x0b:
// clear to end of line
if (used && cursor < used)
{
#ifdef MUOS_LINEEDIT_UTF8
//FIXME: delete from utfstart
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
utf8line_redraw MUOS_IO_HWARG();
#else
used = cursor;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("K"));
#endif
}
pending = 0;
break;
case ESCAPE<<8 | 0x5b:
pending = CSI;
break;
case CSI<<8 | 0x41:
// up
#ifdef MUOS_LINEEDIT_RECALL
if (recall)
{
*buffer = recall;
recall = 0;
cursor = used = strlen (buffer);
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_cstr MUOS_IO_HWARG(buffer);
}
#endif
#if 0 //PLANNED: history
#endif
pending = 0;
break;
case CSI<<8 | 0x42:
case 0x0a:
// down
#ifdef MUOS_LINEEDIT_RECALL
if (!recall)
{
recall = *buffer;
used = 0;
cursor = 0;
*buffer = 0;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("\r\x1b[K"));
}
#endif
#if 0 //PLANNED: history
#endif
pending = 0;
break;
case CSI<<8 | 0x43:
case 0x0c:
// right
if (cursor<used)
{
#ifdef MUOS_LINEEDIT_UTF8
cursor += muos_utf8size (buffer+cursor);
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
utf8line_redraw MUOS_IO_HWARG();
#else
++cursor;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_csi_char MUOS_IO_HWARG('C');
#endif
}
else
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG(7);
pending = 0;
break;
case CSI<<8 | 0x44:
case 0x08:
// left
if (cursor)
{
#ifdef MUOS_LINEEDIT_UTF8
cursor -= muos_utf8size (buffer+cursor-1);
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
utf8line_redraw MUOS_IO_HWARG();
#else
--cursor;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_csi_char MUOS_IO_HWARG('D');
#endif
}
else
muos_output_char MUOS_IO_HWARG(7);
pending = 0;
break;
case CSI<<8 | 0x33:
// del start
pending = DEL;
break;
case DEL<<8 | 0x7e:
// del
if (used && cursor < used)
{
#ifdef MUOS_LINEEDIT_UTF8
utf8del ();
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
utf8line_redraw MUOS_IO_HWARG();
#else
--used;
memmove (buffer+cursor, buffer+cursor+1, used-cursor+1);
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
{
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("s\x1b[?25l"));
muos_output_cstr MUOS_IO_HWARG(buffer+cursor);
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("K\x1b[u\x1b[?25h"));
}
#endif
}
else
{
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG(7);
}
pending = 0;
break;
case CSI<<8 | 0x35:
// pgup start
pending = PGUP;
break;
case PGUP<<8 | 0x7e:
// pgup
#if 0 //PLANNED: completion
#endif
pending = 0;
break;
case CSI<<8 | 0x36:
// pgdown start
pending = PGDOWN;
break;
case PGDOWN<<8 | 0x7e:
// pgdown
#if 0 //PLANNED: completion
#endif
pending = 0;
break;
case CSI<<8 | 0x31:
// pos1 start
pending = POS1;
break;
case POS1<<8 | 0x7e:
// pos1
pending = 0;
cursor = 0;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG('\r');
break;
case CSI<<8 | 0x34:
// end start
pending = END;
break;
case END<<8 | 0x7e:
// end
pending = 0;
cursor = used;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
{
#ifdef MUOS_LINEEDIT_UTF8
utf8line_redraw MUOS_IO_HWARG();
#else
muos_output_csi_cstr MUOS_IO_HWARG(NULL);
muos_output_uint16 MUOS_IO_HWARG(cursor+1);
muos_output_char MUOS_IO_HWARG('G');
#endif
}
break;
case CSI<<8 | 0x32:
// ovwr start
pending = OVWR;
break;
case OVWR<<8 | 0x7e:
// ovwr
muos_serial_status[MUOS_IO_HWINDEX].lineedit_ovwr ^= 1;
pending = 0;
break;
case 0x09:
// tab
#if 0 //PLANNED: completion
#endif
break;
case 0x0d:
// return
//TODO: push callback on bgq
//TODO: and suspend lineedit until the callback is finished (w/ wraper)
buffer[used] = 0;
#ifdef MUOS_LINEEDIT_CALLBACK
MUOS_LINEEDIT_CALLBACK (buffer);
#endif
#ifdef MUOS_LINEEDIT_RECALL
if (*buffer)
recall = *buffer;
#endif
used = 0;
cursor = 0;
*buffer = 0;
pending = 0;
break;
case 0x7f:
// backspace
if (cursor > 0)
{
#ifdef MUOS_LINEEDIT_UTF8
uint8_t len = muos_utf8size (buffer+cursor-1);
used -= len;
cursor -= len;
memmove (buffer+cursor, buffer+cursor+len, used-cursor+1);
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
utf8line_redraw MUOS_IO_HWARG();
#else
--used;
--cursor;
memmove (buffer+cursor, buffer+cursor+1, used-cursor+1);
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
{
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("D\x1b[s\x1b[?25l"));
muos_output_cstr MUOS_IO_HWARG(buffer+cursor);
muos_output_csi_fstr MUOS_IO_HWARG(MUOS_COALESCE_PSTR("K\x1b[u\x1b[?25h"));
}
#endif
}
else
{
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG(7);
}
break;
default:
//nonprintable
if (data < 32)
{
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG(7);
pending = 0;
return true;
}
#ifdef MUOS_LINEEDIT_UTF8
if (muos_utf8start (data))
{
pending = muos_utf8size ((const char*)&data);
}
else if (muos_utf8ascii (data))
{
pending = UTF8_1;
}
uint8_t ovwr_len = cursor < used && muos_serial_status[MUOS_IO_HWINDEX].lineedit_ovwr?muos_utf8size (buffer+cursor):0;
if (used+pending - ovwr_len >= MUOS_LINEEDIT_BUFFER)
{
if (pending > 1)
pending = UTF8DROP;
else
pending = 0;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG(7);
break;
}
if (ovwr_len && !muos_utf8cont (data))
utf8del ();
#ifdef MUOS_LINEEDIT_RECALL
recall = 0;
#endif
memmove (buffer+cursor+1, buffer+cursor, used-cursor+1);
buffer[cursor] = data;
++used;
++cursor;
if (pending == UTF8_1 && muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
utf8line_redraw MUOS_IO_HWARG();
--pending;
#else
pending = 0;
if (cursor < used && muos_serial_status[MUOS_IO_HWINDEX].lineedit_ovwr)
{
buffer[cursor] = data;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_char MUOS_IO_HWARG(buffer[cursor]);
++cursor;
}
else if (used < MUOS_LINEEDIT_BUFFER-1)
{
memmove (buffer+cursor+1, buffer+cursor, used-cursor+1);
buffer[cursor] = data;
if (muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
muos_output_cstr MUOS_IO_HWARG(buffer+cursor);
++used;
++cursor;
if (cursor != used && muos_serial_status[MUOS_IO_HWINDEX].lineedit_echo)
{
muos_output_csi_char MUOS_IO_HWARG('0');
muos_output_uint8 MUOS_IO_HWARG(used-cursor);
muos_output_char MUOS_IO_HWARG('D');
}
}
#endif
}
}
if (used)
{
muos_serial_status[MUOS_IO_HWINDEX].lineedit_pending = true;
}
else
{
muos_serial_status[MUOS_IO_HWINDEX].lineedit_pending = false;
}
return true;
}