1 | /*
|
---|
2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <str.h>
|
---|
32 | #include <io/console.h>
|
---|
33 | #include <io/keycode.h>
|
---|
34 | #include <io/style.h>
|
---|
35 | #include <io/color.h>
|
---|
36 | #include <vfs/vfs.h>
|
---|
37 | #include <clipboard.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <assert.h>
|
---|
41 | #include <bool.h>
|
---|
42 |
|
---|
43 | #include "tinput.h"
|
---|
44 |
|
---|
45 | /** Seek direction */
|
---|
46 | typedef enum {
|
---|
47 | seek_backward = -1,
|
---|
48 | seek_forward = 1
|
---|
49 | } seek_dir_t;
|
---|
50 |
|
---|
51 | static void tinput_init(tinput_t *ti);
|
---|
52 | static void tinput_insert_string(tinput_t *ti, const char *str);
|
---|
53 | static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb);
|
---|
54 | static bool tinput_sel_active(tinput_t *ti);
|
---|
55 | static void tinput_sel_all(tinput_t *ti);
|
---|
56 | static void tinput_sel_delete(tinput_t *ti);
|
---|
57 | static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev);
|
---|
58 | static void tinput_key_shift(tinput_t *ti, console_event_t *ev);
|
---|
59 | static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev);
|
---|
60 | static void tinput_key_unmod(tinput_t *ti, console_event_t *ev);
|
---|
61 | static void tinput_pre_seek(tinput_t *ti, bool shift_held);
|
---|
62 | static void tinput_post_seek(tinput_t *ti, bool shift_held);
|
---|
63 |
|
---|
64 | /** Create a new text input field. */
|
---|
65 | tinput_t *tinput_new(void)
|
---|
66 | {
|
---|
67 | tinput_t *ti;
|
---|
68 |
|
---|
69 | ti = malloc(sizeof(tinput_t));
|
---|
70 | if (ti == NULL)
|
---|
71 | return NULL;
|
---|
72 |
|
---|
73 | tinput_init(ti);
|
---|
74 | return ti;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Destroy text input field. */
|
---|
78 | void tinput_destroy(tinput_t *ti)
|
---|
79 | {
|
---|
80 | free(ti);
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void tinput_display_tail(tinput_t *ti, int start, int pad)
|
---|
84 | {
|
---|
85 | static wchar_t dbuf[INPUT_MAX_SIZE + 1];
|
---|
86 | int sa, sb;
|
---|
87 | int i, p;
|
---|
88 |
|
---|
89 | tinput_sel_get_bounds(ti, &sa, &sb);
|
---|
90 |
|
---|
91 | console_goto(fphone(stdout), (ti->col0 + start) % ti->con_cols,
|
---|
92 | ti->row0 + (ti->col0 + start) / ti->con_cols);
|
---|
93 | console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
|
---|
94 |
|
---|
95 | p = start;
|
---|
96 | if (p < sa) {
|
---|
97 | memcpy(dbuf, ti->buffer + p, (sa - p) * sizeof(wchar_t));
|
---|
98 | dbuf[sa - p] = '\0';
|
---|
99 | printf("%ls", dbuf);
|
---|
100 | p = sa;
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (p < sb) {
|
---|
104 | fflush(stdout);
|
---|
105 | console_set_color(fphone(stdout), COLOR_BLACK, COLOR_RED, 0);
|
---|
106 | memcpy(dbuf, ti->buffer + p,
|
---|
107 | (sb - p) * sizeof(wchar_t));
|
---|
108 | dbuf[sb - p] = '\0';
|
---|
109 | printf("%ls", dbuf);
|
---|
110 | p = sb;
|
---|
111 | }
|
---|
112 |
|
---|
113 | fflush(stdout);
|
---|
114 | console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
|
---|
115 |
|
---|
116 | if (p < ti->nc) {
|
---|
117 | memcpy(dbuf, ti->buffer + p,
|
---|
118 | (ti->nc - p) * sizeof(wchar_t));
|
---|
119 | dbuf[ti->nc - p] = '\0';
|
---|
120 | printf("%ls", dbuf);
|
---|
121 | }
|
---|
122 |
|
---|
123 | for (i = 0; i < pad; ++i)
|
---|
124 | putchar(' ');
|
---|
125 | fflush(stdout);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static char *tinput_get_str(tinput_t *ti)
|
---|
129 | {
|
---|
130 | return wstr_to_astr(ti->buffer);
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void tinput_position_caret(tinput_t *ti)
|
---|
134 | {
|
---|
135 | console_goto(fphone(stdout), (ti->col0 + ti->pos) % ti->con_cols,
|
---|
136 | ti->row0 + (ti->col0 + ti->pos) / ti->con_cols);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Update row0 in case the screen could have scrolled. */
|
---|
140 | static void tinput_update_origin(tinput_t *ti)
|
---|
141 | {
|
---|
142 | int width, rows;
|
---|
143 |
|
---|
144 | width = ti->col0 + ti->nc;
|
---|
145 | rows = (width / ti->con_cols) + 1;
|
---|
146 |
|
---|
147 | /* Update row0 if the screen scrolled. */
|
---|
148 | if (ti->row0 + rows > ti->con_rows)
|
---|
149 | ti->row0 = ti->con_rows - rows;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void tinput_insert_char(tinput_t *ti, wchar_t c)
|
---|
153 | {
|
---|
154 | int i;
|
---|
155 | int new_width, new_height;
|
---|
156 |
|
---|
157 | if (ti->nc == INPUT_MAX_SIZE)
|
---|
158 | return;
|
---|
159 |
|
---|
160 | new_width = ti->col0 + ti->nc + 1;
|
---|
161 | if (new_width % ti->con_cols == 0) {
|
---|
162 | /* Advancing to new line. */
|
---|
163 | new_height = (new_width / ti->con_cols) + 1;
|
---|
164 | if (new_height >= ti->con_rows)
|
---|
165 | return; /* Disallow text longer than 1 page for now. */
|
---|
166 | }
|
---|
167 |
|
---|
168 | for (i = ti->nc; i > ti->pos; --i)
|
---|
169 | ti->buffer[i] = ti->buffer[i - 1];
|
---|
170 |
|
---|
171 | ti->buffer[ti->pos] = c;
|
---|
172 | ti->pos += 1;
|
---|
173 | ti->nc += 1;
|
---|
174 | ti->buffer[ti->nc] = '\0';
|
---|
175 | ti->sel_start = ti->pos;
|
---|
176 |
|
---|
177 | tinput_display_tail(ti, ti->pos - 1, 0);
|
---|
178 | tinput_update_origin(ti);
|
---|
179 | tinput_position_caret(ti);
|
---|
180 | }
|
---|
181 |
|
---|
182 | static void tinput_insert_string(tinput_t *ti, const char *str)
|
---|
183 | {
|
---|
184 | int i;
|
---|
185 | int new_width, new_height;
|
---|
186 | int ilen;
|
---|
187 | wchar_t c;
|
---|
188 | size_t off;
|
---|
189 |
|
---|
190 | ilen = min((ssize_t) str_length(str), INPUT_MAX_SIZE - ti->nc);
|
---|
191 | if (ilen == 0)
|
---|
192 | return;
|
---|
193 |
|
---|
194 | new_width = ti->col0 + ti->nc + ilen;
|
---|
195 | new_height = (new_width / ti->con_cols) + 1;
|
---|
196 | if (new_height >= ti->con_rows)
|
---|
197 | return; /* Disallow text longer than 1 page for now. */
|
---|
198 |
|
---|
199 | for (i = ti->nc - 1; i >= ti->pos; --i)
|
---|
200 | ti->buffer[i + ilen] = ti->buffer[i];
|
---|
201 |
|
---|
202 | off = 0; i = 0;
|
---|
203 | while (i < ilen) {
|
---|
204 | c = str_decode(str, &off, STR_NO_LIMIT);
|
---|
205 | if (c == '\0')
|
---|
206 | break;
|
---|
207 |
|
---|
208 | /* Filter out non-printable chars. */
|
---|
209 | if (c < 32)
|
---|
210 | c = 32;
|
---|
211 |
|
---|
212 | ti->buffer[ti->pos + i] = c;
|
---|
213 | ++i;
|
---|
214 | }
|
---|
215 |
|
---|
216 | ti->pos += ilen;
|
---|
217 | ti->nc += ilen;
|
---|
218 | ti->buffer[ti->nc] = '\0';
|
---|
219 | ti->sel_start = ti->pos;
|
---|
220 |
|
---|
221 | tinput_display_tail(ti, ti->pos - ilen, 0);
|
---|
222 | tinput_update_origin(ti);
|
---|
223 | tinput_position_caret(ti);
|
---|
224 | }
|
---|
225 |
|
---|
226 | static void tinput_backspace(tinput_t *ti)
|
---|
227 | {
|
---|
228 | int i;
|
---|
229 |
|
---|
230 | if (tinput_sel_active(ti)) {
|
---|
231 | tinput_sel_delete(ti);
|
---|
232 | return;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (ti->pos == 0)
|
---|
236 | return;
|
---|
237 |
|
---|
238 | for (i = ti->pos; i < ti->nc; ++i)
|
---|
239 | ti->buffer[i - 1] = ti->buffer[i];
|
---|
240 | ti->pos -= 1;
|
---|
241 | ti->nc -= 1;
|
---|
242 | ti->buffer[ti->nc] = '\0';
|
---|
243 | ti->sel_start = ti->pos;
|
---|
244 |
|
---|
245 | tinput_display_tail(ti, ti->pos, 1);
|
---|
246 | tinput_position_caret(ti);
|
---|
247 | }
|
---|
248 |
|
---|
249 | static void tinput_delete(tinput_t *ti)
|
---|
250 | {
|
---|
251 | if (tinput_sel_active(ti)) {
|
---|
252 | tinput_sel_delete(ti);
|
---|
253 | return;
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (ti->pos == ti->nc)
|
---|
257 | return;
|
---|
258 |
|
---|
259 | ti->pos += 1;
|
---|
260 | ti->sel_start = ti->pos;
|
---|
261 |
|
---|
262 | tinput_backspace(ti);
|
---|
263 | }
|
---|
264 |
|
---|
265 | static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
266 | {
|
---|
267 | tinput_pre_seek(ti, shift_held);
|
---|
268 |
|
---|
269 | if (dir == seek_forward) {
|
---|
270 | if (ti->pos < ti->nc)
|
---|
271 | ti->pos += 1;
|
---|
272 | } else {
|
---|
273 | if (ti->pos > 0)
|
---|
274 | ti->pos -= 1;
|
---|
275 | }
|
---|
276 |
|
---|
277 | tinput_post_seek(ti, shift_held);
|
---|
278 | }
|
---|
279 |
|
---|
280 | static void tinput_seek_word(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
281 | {
|
---|
282 | tinput_pre_seek(ti, shift_held);
|
---|
283 |
|
---|
284 | if (dir == seek_forward) {
|
---|
285 | if (ti->pos == ti->nc)
|
---|
286 | return;
|
---|
287 |
|
---|
288 | while (1) {
|
---|
289 | ti->pos += 1;
|
---|
290 |
|
---|
291 | if (ti->pos == ti->nc)
|
---|
292 | break;
|
---|
293 |
|
---|
294 | if (ti->buffer[ti->pos - 1] == ' ' &&
|
---|
295 | ti->buffer[ti->pos] != ' ')
|
---|
296 | break;
|
---|
297 | }
|
---|
298 | } else {
|
---|
299 | if (ti->pos == 0)
|
---|
300 | return;
|
---|
301 |
|
---|
302 | while (1) {
|
---|
303 | ti->pos -= 1;
|
---|
304 |
|
---|
305 | if (ti->pos == 0)
|
---|
306 | break;
|
---|
307 |
|
---|
308 | if (ti->buffer[ti->pos - 1] == ' ' &&
|
---|
309 | ti->buffer[ti->pos] != ' ')
|
---|
310 | break;
|
---|
311 | }
|
---|
312 |
|
---|
313 | }
|
---|
314 |
|
---|
315 | tinput_post_seek(ti, shift_held);
|
---|
316 | }
|
---|
317 |
|
---|
318 | static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
319 | {
|
---|
320 | tinput_pre_seek(ti, shift_held);
|
---|
321 |
|
---|
322 | if (dir == seek_forward) {
|
---|
323 | if (ti->pos + ti->con_cols <= ti->nc)
|
---|
324 | ti->pos = ti->pos + ti->con_cols;
|
---|
325 | } else {
|
---|
326 | if (ti->pos - ti->con_cols >= 0)
|
---|
327 | ti->pos = ti->pos - ti->con_cols;
|
---|
328 | }
|
---|
329 |
|
---|
330 | tinput_post_seek(ti, shift_held);
|
---|
331 | }
|
---|
332 |
|
---|
333 | static void tinput_seek_max(tinput_t *ti, seek_dir_t dir, bool shift_held)
|
---|
334 | {
|
---|
335 | tinput_pre_seek(ti, shift_held);
|
---|
336 |
|
---|
337 | if (dir == seek_backward)
|
---|
338 | ti->pos = 0;
|
---|
339 | else
|
---|
340 | ti->pos = ti->nc;
|
---|
341 |
|
---|
342 | tinput_post_seek(ti, shift_held);
|
---|
343 | }
|
---|
344 |
|
---|
345 | static void tinput_pre_seek(tinput_t *ti, bool shift_held)
|
---|
346 | {
|
---|
347 | if (tinput_sel_active(ti) && !shift_held) {
|
---|
348 | /* Unselect and redraw. */
|
---|
349 | ti->sel_start = ti->pos;
|
---|
350 | tinput_display_tail(ti, 0, 0);
|
---|
351 | tinput_position_caret(ti);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | static void tinput_post_seek(tinput_t *ti, bool shift_held)
|
---|
356 | {
|
---|
357 | if (shift_held) {
|
---|
358 | /* Selecting text. Need redraw. */
|
---|
359 | tinput_display_tail(ti, 0, 0);
|
---|
360 | } else {
|
---|
361 | /* Shift not held. Keep selection empty. */
|
---|
362 | ti->sel_start = ti->pos;
|
---|
363 | }
|
---|
364 | tinput_position_caret(ti);
|
---|
365 | }
|
---|
366 |
|
---|
367 | static void tinput_history_insert(tinput_t *ti, char *str)
|
---|
368 | {
|
---|
369 | int i;
|
---|
370 |
|
---|
371 | if (ti->hnum < HISTORY_LEN) {
|
---|
372 | ti->hnum += 1;
|
---|
373 | } else {
|
---|
374 | if (ti->history[HISTORY_LEN] != NULL)
|
---|
375 | free(ti->history[HISTORY_LEN]);
|
---|
376 | }
|
---|
377 |
|
---|
378 | for (i = ti->hnum; i > 1; --i)
|
---|
379 | ti->history[i] = ti->history[i - 1];
|
---|
380 |
|
---|
381 | ti->history[1] = str_dup(str);
|
---|
382 |
|
---|
383 | if (ti->history[0] != NULL) {
|
---|
384 | free(ti->history[0]);
|
---|
385 | ti->history[0] = NULL;
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | static void tinput_set_str(tinput_t *ti, char *str)
|
---|
390 | {
|
---|
391 | str_to_wstr(ti->buffer, INPUT_MAX_SIZE, str);
|
---|
392 | ti->nc = wstr_length(ti->buffer);
|
---|
393 | ti->pos = ti->nc;
|
---|
394 | ti->sel_start = ti->pos;
|
---|
395 | }
|
---|
396 |
|
---|
397 | static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb)
|
---|
398 | {
|
---|
399 | if (ti->sel_start < ti->pos) {
|
---|
400 | *sa = ti->sel_start;
|
---|
401 | *sb = ti->pos;
|
---|
402 | } else {
|
---|
403 | *sa = ti->pos;
|
---|
404 | *sb = ti->sel_start;
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | static bool tinput_sel_active(tinput_t *ti)
|
---|
409 | {
|
---|
410 | return ti->sel_start != ti->pos;
|
---|
411 | }
|
---|
412 |
|
---|
413 | static void tinput_sel_all(tinput_t *ti)
|
---|
414 | {
|
---|
415 | ti->sel_start = 0;
|
---|
416 | ti->pos = ti->nc;
|
---|
417 | tinput_display_tail(ti, 0, 0);
|
---|
418 | tinput_position_caret(ti);
|
---|
419 | }
|
---|
420 |
|
---|
421 | static void tinput_sel_delete(tinput_t *ti)
|
---|
422 | {
|
---|
423 | int sa, sb;
|
---|
424 |
|
---|
425 | tinput_sel_get_bounds(ti, &sa, &sb);
|
---|
426 | if (sa == sb)
|
---|
427 | return;
|
---|
428 |
|
---|
429 | memmove(ti->buffer + sa, ti->buffer + sb,
|
---|
430 | (ti->nc - sb) * sizeof(wchar_t));
|
---|
431 | ti->pos = ti->sel_start = sa;
|
---|
432 | ti->nc -= (sb - sa);
|
---|
433 | ti->buffer[ti->nc] = '\0';
|
---|
434 |
|
---|
435 | tinput_display_tail(ti, sa, sb - sa);
|
---|
436 | tinput_position_caret(ti);
|
---|
437 | }
|
---|
438 |
|
---|
439 | static void tinput_sel_copy_to_cb(tinput_t *ti)
|
---|
440 | {
|
---|
441 | int sa, sb;
|
---|
442 | char *str;
|
---|
443 |
|
---|
444 | tinput_sel_get_bounds(ti, &sa, &sb);
|
---|
445 |
|
---|
446 | if (sb < ti->nc) {
|
---|
447 | wchar_t tmp_c = ti->buffer[sb];
|
---|
448 | ti->buffer[sb] = '\0';
|
---|
449 | str = wstr_to_astr(ti->buffer + sa);
|
---|
450 | ti->buffer[sb] = tmp_c;
|
---|
451 | } else
|
---|
452 | str = wstr_to_astr(ti->buffer + sa);
|
---|
453 |
|
---|
454 | if (str == NULL)
|
---|
455 | goto error;
|
---|
456 |
|
---|
457 | if (clipboard_put_str(str) != EOK)
|
---|
458 | goto error;
|
---|
459 |
|
---|
460 | free(str);
|
---|
461 | return;
|
---|
462 | error:
|
---|
463 | return;
|
---|
464 | /* TODO: Give the user some warning. */
|
---|
465 | }
|
---|
466 |
|
---|
467 | static void tinput_paste_from_cb(tinput_t *ti)
|
---|
468 | {
|
---|
469 | char *str;
|
---|
470 | int rc;
|
---|
471 |
|
---|
472 | rc = clipboard_get_str(&str);
|
---|
473 | if (rc != EOK || str == NULL)
|
---|
474 | return; /* TODO: Give the user some warning. */
|
---|
475 |
|
---|
476 | tinput_insert_string(ti, str);
|
---|
477 | free(str);
|
---|
478 | }
|
---|
479 |
|
---|
480 | static void tinput_history_seek(tinput_t *ti, int offs)
|
---|
481 | {
|
---|
482 | int pad;
|
---|
483 |
|
---|
484 | if (ti->hpos + offs < 0 || ti->hpos + offs > ti->hnum)
|
---|
485 | return;
|
---|
486 |
|
---|
487 | if (ti->history[ti->hpos] != NULL) {
|
---|
488 | free(ti->history[ti->hpos]);
|
---|
489 | ti->history[ti->hpos] = NULL;
|
---|
490 | }
|
---|
491 |
|
---|
492 | ti->history[ti->hpos] = tinput_get_str(ti);
|
---|
493 | ti->hpos += offs;
|
---|
494 |
|
---|
495 | pad = ti->nc - str_length(ti->history[ti->hpos]);
|
---|
496 | if (pad < 0) pad = 0;
|
---|
497 |
|
---|
498 | tinput_set_str(ti, ti->history[ti->hpos]);
|
---|
499 | tinput_display_tail(ti, 0, pad);
|
---|
500 | tinput_update_origin(ti);
|
---|
501 | tinput_position_caret(ti);
|
---|
502 | }
|
---|
503 |
|
---|
504 | /** Initialize text input field.
|
---|
505 | *
|
---|
506 | * Must be called before using the field. It clears the history.
|
---|
507 | */
|
---|
508 | static void tinput_init(tinput_t *ti)
|
---|
509 | {
|
---|
510 | ti->hnum = 0;
|
---|
511 | ti->hpos = 0;
|
---|
512 | ti->history[0] = NULL;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /** Read in one line of input.
|
---|
516 | *
|
---|
517 | * @param ti Text input.
|
---|
518 | * @param dstr Place to save pointer to new string.
|
---|
519 | * @return EOK on success, ENOENT if user requested abort, EIO
|
---|
520 | * if communication with console failed.
|
---|
521 | */
|
---|
522 | int tinput_read(tinput_t *ti, char **dstr)
|
---|
523 | {
|
---|
524 | console_event_t ev;
|
---|
525 | char *str;
|
---|
526 |
|
---|
527 | fflush(stdout);
|
---|
528 |
|
---|
529 | if (console_get_size(fphone(stdin), &ti->con_cols, &ti->con_rows) != EOK)
|
---|
530 | return EIO;
|
---|
531 | if (console_get_pos(fphone(stdin), &ti->col0, &ti->row0) != EOK)
|
---|
532 | return EIO;
|
---|
533 |
|
---|
534 | ti->pos = ti->sel_start = 0;
|
---|
535 | ti->nc = 0;
|
---|
536 | ti->buffer[0] = '\0';
|
---|
537 | ti->done = false;
|
---|
538 | ti->exit_clui = false;
|
---|
539 |
|
---|
540 | while (!ti->done) {
|
---|
541 | fflush(stdout);
|
---|
542 | if (!console_get_event(fphone(stdin), &ev))
|
---|
543 | return EIO;
|
---|
544 |
|
---|
545 | if (ev.type != KEY_PRESS)
|
---|
546 | continue;
|
---|
547 |
|
---|
548 | if ((ev.mods & KM_CTRL) != 0 &&
|
---|
549 | (ev.mods & (KM_ALT | KM_SHIFT)) == 0) {
|
---|
550 | tinput_key_ctrl(ti, &ev);
|
---|
551 | }
|
---|
552 |
|
---|
553 | if ((ev.mods & KM_SHIFT) != 0 &&
|
---|
554 | (ev.mods & (KM_CTRL | KM_ALT)) == 0) {
|
---|
555 | tinput_key_shift(ti, &ev);
|
---|
556 | }
|
---|
557 |
|
---|
558 | if ((ev.mods & KM_CTRL) != 0 &&
|
---|
559 | (ev.mods & KM_SHIFT) != 0 &&
|
---|
560 | (ev.mods & KM_ALT) == 0) {
|
---|
561 | tinput_key_ctrl_shift(ti, &ev);
|
---|
562 | }
|
---|
563 |
|
---|
564 | if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
|
---|
565 | tinput_key_unmod(ti, &ev);
|
---|
566 | }
|
---|
567 |
|
---|
568 | if (ev.c >= ' ') {
|
---|
569 | tinput_sel_delete(ti);
|
---|
570 | tinput_insert_char(ti, ev.c);
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | if (ti->exit_clui)
|
---|
575 | return ENOENT;
|
---|
576 |
|
---|
577 | ti->pos = ti->nc;
|
---|
578 | tinput_position_caret(ti);
|
---|
579 | putchar('\n');
|
---|
580 |
|
---|
581 | str = tinput_get_str(ti);
|
---|
582 | if (str_cmp(str, "") != 0)
|
---|
583 | tinput_history_insert(ti, str);
|
---|
584 |
|
---|
585 | ti->hpos = 0;
|
---|
586 |
|
---|
587 | *dstr = str;
|
---|
588 | return EOK;
|
---|
589 | }
|
---|
590 |
|
---|
591 | static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev)
|
---|
592 | {
|
---|
593 | switch (ev->key) {
|
---|
594 | case KC_LEFT:
|
---|
595 | tinput_seek_word(ti, seek_backward, false);
|
---|
596 | break;
|
---|
597 | case KC_RIGHT:
|
---|
598 | tinput_seek_word(ti, seek_forward, false);
|
---|
599 | break;
|
---|
600 | case KC_UP:
|
---|
601 | tinput_seek_vertical(ti, seek_backward, false);
|
---|
602 | break;
|
---|
603 | case KC_DOWN:
|
---|
604 | tinput_seek_vertical(ti, seek_forward, false);
|
---|
605 | break;
|
---|
606 | case KC_X:
|
---|
607 | tinput_sel_copy_to_cb(ti);
|
---|
608 | tinput_sel_delete(ti);
|
---|
609 | break;
|
---|
610 | case KC_C:
|
---|
611 | tinput_sel_copy_to_cb(ti);
|
---|
612 | break;
|
---|
613 | case KC_V:
|
---|
614 | tinput_sel_delete(ti);
|
---|
615 | tinput_paste_from_cb(ti);
|
---|
616 | break;
|
---|
617 | case KC_A:
|
---|
618 | tinput_sel_all(ti);
|
---|
619 | break;
|
---|
620 | case KC_Q:
|
---|
621 | /* Signal libary client to quit interactive loop. */
|
---|
622 | ti->done = true;
|
---|
623 | ti->exit_clui = true;
|
---|
624 | break;
|
---|
625 | default:
|
---|
626 | break;
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev)
|
---|
631 | {
|
---|
632 | switch (ev->key) {
|
---|
633 | case KC_LEFT:
|
---|
634 | tinput_seek_word(ti, seek_backward, true);
|
---|
635 | break;
|
---|
636 | case KC_RIGHT:
|
---|
637 | tinput_seek_word(ti, seek_forward, true);
|
---|
638 | break;
|
---|
639 | case KC_UP:
|
---|
640 | tinput_seek_vertical(ti, seek_backward, true);
|
---|
641 | break;
|
---|
642 | case KC_DOWN:
|
---|
643 | tinput_seek_vertical(ti, seek_forward, true);
|
---|
644 | break;
|
---|
645 | default:
|
---|
646 | break;
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | static void tinput_key_shift(tinput_t *ti, console_event_t *ev)
|
---|
651 | {
|
---|
652 | switch (ev->key) {
|
---|
653 | case KC_LEFT:
|
---|
654 | tinput_seek_cell(ti, seek_backward, true);
|
---|
655 | break;
|
---|
656 | case KC_RIGHT:
|
---|
657 | tinput_seek_cell(ti, seek_forward, true);
|
---|
658 | break;
|
---|
659 | case KC_UP:
|
---|
660 | tinput_seek_vertical(ti, seek_backward, true);
|
---|
661 | break;
|
---|
662 | case KC_DOWN:
|
---|
663 | tinput_seek_vertical(ti, seek_forward, true);
|
---|
664 | break;
|
---|
665 | case KC_HOME:
|
---|
666 | tinput_seek_max(ti, seek_backward, true);
|
---|
667 | break;
|
---|
668 | case KC_END:
|
---|
669 | tinput_seek_max(ti, seek_forward, true);
|
---|
670 | break;
|
---|
671 | default:
|
---|
672 | break;
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | static void tinput_key_unmod(tinput_t *ti, console_event_t *ev)
|
---|
677 | {
|
---|
678 | switch (ev->key) {
|
---|
679 | case KC_ENTER:
|
---|
680 | case KC_NENTER:
|
---|
681 | ti->done = true;
|
---|
682 | break;
|
---|
683 | case KC_BACKSPACE:
|
---|
684 | tinput_backspace(ti);
|
---|
685 | break;
|
---|
686 | case KC_DELETE:
|
---|
687 | tinput_delete(ti);
|
---|
688 | break;
|
---|
689 | case KC_LEFT:
|
---|
690 | tinput_seek_cell(ti, seek_backward, false);
|
---|
691 | break;
|
---|
692 | case KC_RIGHT:
|
---|
693 | tinput_seek_cell(ti, seek_forward, false);
|
---|
694 | break;
|
---|
695 | case KC_HOME:
|
---|
696 | tinput_seek_max(ti, seek_backward, false);
|
---|
697 | break;
|
---|
698 | case KC_END:
|
---|
699 | tinput_seek_max(ti, seek_forward, false);
|
---|
700 | break;
|
---|
701 | case KC_UP:
|
---|
702 | tinput_history_seek(ti, +1);
|
---|
703 | break;
|
---|
704 | case KC_DOWN:
|
---|
705 | tinput_history_seek(ti, -1);
|
---|
706 | break;
|
---|
707 | default:
|
---|
708 | break;
|
---|
709 | }
|
---|
710 | }
|
---|