source: mainline/uspace/lib/clui/tinput.c@ 4122410

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4122410 was 4122410, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve Doxygen documentaion

This is stil WiP. A number of libraries, drivers and services were
converted to using a more hierarchical and decentralized scheme when it
comes to specifying to which doxygen group they belong.

  • Property mode set to 100644
File size: 22.1 KB
Line 
1/*
2 * Copyright (c) 2011 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/** @addtogroup libclui
30 * @{
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <str.h>
36#include <io/console.h>
37#include <io/keycode.h>
38#include <io/style.h>
39#include <io/color.h>
40#include <vfs/vfs.h>
41#include <clipboard.h>
42#include <macros.h>
43#include <errno.h>
44#include <assert.h>
45#include <stdbool.h>
46#include <tinput.h>
47
48#define LIN_TO_COL(ti, lpos) ((lpos) % ((ti)->con_cols))
49#define LIN_TO_ROW(ti, lpos) ((lpos) / ((ti)->con_cols))
50#define LIN_POS(ti, col, row) ((col) + (row) * (ti)->con_cols)
51
52/** Seek direction */
53typedef enum {
54 seek_backward = -1,
55 seek_forward = 1
56} seek_dir_t;
57
58static void tinput_init(tinput_t *);
59static void tinput_insert_string(tinput_t *, const char *);
60static void tinput_sel_get_bounds(tinput_t *, size_t *, size_t *);
61static bool tinput_sel_active(tinput_t *);
62static void tinput_sel_all(tinput_t *);
63static void tinput_sel_delete(tinput_t *);
64static void tinput_key_ctrl(tinput_t *, kbd_event_t *);
65static void tinput_key_shift(tinput_t *, kbd_event_t *);
66static void tinput_key_ctrl_shift(tinput_t *, kbd_event_t *);
67static void tinput_key_unmod(tinput_t *, kbd_event_t *);
68static void tinput_pre_seek(tinput_t *, bool);
69static void tinput_post_seek(tinput_t *, bool);
70
71static void tinput_console_set_lpos(tinput_t *ti, unsigned lpos)
72{
73 console_set_pos(ti->console, LIN_TO_COL(ti, lpos),
74 LIN_TO_ROW(ti, lpos));
75}
76
77/** Create a new text input field. */
78tinput_t *tinput_new(void)
79{
80 tinput_t *ti;
81
82 ti = calloc(1, sizeof(tinput_t));
83 if (ti == NULL)
84 return NULL;
85
86 tinput_init(ti);
87 return ti;
88}
89
90/** Destroy text input field. */
91void tinput_destroy(tinput_t *ti)
92{
93 if (ti->prompt != NULL)
94 free(ti->prompt);
95 free(ti);
96}
97
98static void tinput_display_prompt(tinput_t *ti)
99{
100 tinput_console_set_lpos(ti, ti->prompt_coord);
101
102 console_set_style(ti->console, STYLE_EMPHASIS);
103 printf("%s", ti->prompt);
104 console_flush(ti->console);
105 console_set_style(ti->console, STYLE_NORMAL);
106}
107
108static void tinput_display_tail(tinput_t *ti, size_t start, size_t pad)
109{
110 wchar_t *dbuf = malloc((INPUT_MAX_SIZE + 1) * sizeof(wchar_t));
111 if (!dbuf)
112 return;
113
114 size_t sa;
115 size_t sb;
116 tinput_sel_get_bounds(ti, &sa, &sb);
117
118 tinput_console_set_lpos(ti, ti->text_coord + start);
119 console_set_style(ti->console, STYLE_NORMAL);
120
121 size_t p = start;
122 if (p < sa) {
123 memcpy(dbuf, ti->buffer + p, (sa - p) * sizeof(wchar_t));
124 dbuf[sa - p] = '\0';
125 printf("%ls", dbuf);
126 p = sa;
127 }
128
129 if (p < sb) {
130 console_flush(ti->console);
131 console_set_style(ti->console, STYLE_SELECTED);
132
133 memcpy(dbuf, ti->buffer + p,
134 (sb - p) * sizeof(wchar_t));
135 dbuf[sb - p] = '\0';
136 printf("%ls", dbuf);
137 p = sb;
138 }
139
140 console_flush(ti->console);
141 console_set_style(ti->console, STYLE_NORMAL);
142
143 if (p < ti->nc) {
144 memcpy(dbuf, ti->buffer + p,
145 (ti->nc - p) * sizeof(wchar_t));
146 dbuf[ti->nc - p] = '\0';
147 printf("%ls", dbuf);
148 }
149
150 for (p = 0; p < pad; p++)
151 putwchar(' ');
152
153 console_flush(ti->console);
154
155 free(dbuf);
156}
157
158static char *tinput_get_str(tinput_t *ti)
159{
160 return wstr_to_astr(ti->buffer);
161}
162
163static void tinput_position_caret(tinput_t *ti)
164{
165 tinput_console_set_lpos(ti, ti->text_coord + ti->pos);
166}
167
168/** Update text_coord, prompt_coord in case the screen could have scrolled. */
169static void tinput_update_origin(tinput_t *ti)
170{
171 unsigned end_coord = ti->text_coord + ti->nc;
172 unsigned end_row = LIN_TO_ROW(ti, end_coord);
173
174 unsigned scroll_rows;
175
176 /* Update coords if the screen scrolled. */
177 if (end_row >= ti->con_rows) {
178 scroll_rows = end_row - ti->con_rows + 1;
179 ti->text_coord -= ti->con_cols * scroll_rows;
180 ti->prompt_coord -= ti->con_cols * scroll_rows;
181 }
182}
183
184static void tinput_jump_after(tinput_t *ti)
185{
186 tinput_console_set_lpos(ti, ti->text_coord + ti->nc);
187 console_flush(ti->console);
188 putwchar('\n');
189}
190
191static errno_t tinput_display(tinput_t *ti)
192{
193 sysarg_t col0, row0;
194
195 if (console_get_pos(ti->console, &col0, &row0) != EOK)
196 return EIO;
197
198 ti->prompt_coord = row0 * ti->con_cols + col0;
199 ti->text_coord = ti->prompt_coord + str_length(ti->prompt);
200
201 tinput_display_prompt(ti);
202 tinput_display_tail(ti, 0, 0);
203 tinput_position_caret(ti);
204
205 return EOK;
206}
207
208static void tinput_insert_char(tinput_t *ti, wchar_t c)
209{
210 if (ti->nc == INPUT_MAX_SIZE)
211 return;
212
213 unsigned new_width = LIN_TO_COL(ti, ti->text_coord) + ti->nc + 1;
214 if (new_width % ti->con_cols == 0) {
215 /* Advancing to new line. */
216 sysarg_t new_height = (new_width / ti->con_cols) + 1;
217 if (new_height >= ti->con_rows) {
218 /* Disallow text longer than 1 page for now. */
219 return;
220 }
221 }
222
223 size_t i;
224 for (i = ti->nc; i > ti->pos; i--)
225 ti->buffer[i] = ti->buffer[i - 1];
226
227 ti->buffer[ti->pos] = c;
228 ti->pos += 1;
229 ti->nc += 1;
230 ti->buffer[ti->nc] = '\0';
231 ti->sel_start = ti->pos;
232
233 tinput_display_tail(ti, ti->pos - 1, 0);
234 tinput_update_origin(ti);
235 tinput_position_caret(ti);
236}
237
238static void tinput_insert_string(tinput_t *ti, const char *str)
239{
240 size_t ilen = min(str_length(str), INPUT_MAX_SIZE - ti->nc);
241 if (ilen == 0)
242 return;
243
244 unsigned new_width = LIN_TO_COL(ti, ti->text_coord) + ti->nc + ilen;
245 unsigned new_height = (new_width / ti->con_cols) + 1;
246 if (new_height >= ti->con_rows) {
247 /* Disallow text longer than 1 page for now. */
248 return;
249 }
250
251 if (ti->nc > 0) {
252 size_t i;
253 for (i = ti->nc; i > ti->pos; i--)
254 ti->buffer[i + ilen - 1] = ti->buffer[i - 1];
255 }
256
257 size_t off = 0;
258 size_t i = 0;
259 while (i < ilen) {
260 wchar_t c = str_decode(str, &off, STR_NO_LIMIT);
261 if (c == '\0')
262 break;
263
264 /* Filter out non-printable chars. */
265 if (c < 32)
266 c = 32;
267
268 ti->buffer[ti->pos + i] = c;
269 i++;
270 }
271
272 ti->pos += ilen;
273 ti->nc += ilen;
274 ti->buffer[ti->nc] = '\0';
275 ti->sel_start = ti->pos;
276
277 tinput_display_tail(ti, ti->pos - ilen, 0);
278 tinput_update_origin(ti);
279 tinput_position_caret(ti);
280}
281
282static void tinput_backspace(tinput_t *ti)
283{
284 if (tinput_sel_active(ti)) {
285 tinput_sel_delete(ti);
286 return;
287 }
288
289 if (ti->pos == 0)
290 return;
291
292 size_t i;
293 for (i = ti->pos; i < ti->nc; i++)
294 ti->buffer[i - 1] = ti->buffer[i];
295
296 ti->pos -= 1;
297 ti->nc -= 1;
298 ti->buffer[ti->nc] = '\0';
299 ti->sel_start = ti->pos;
300
301 tinput_display_tail(ti, ti->pos, 1);
302 tinput_position_caret(ti);
303}
304
305static void tinput_delete(tinput_t *ti)
306{
307 if (tinput_sel_active(ti)) {
308 tinput_sel_delete(ti);
309 return;
310 }
311
312 if (ti->pos == ti->nc)
313 return;
314
315 ti->pos += 1;
316 ti->sel_start = ti->pos;
317
318 tinput_backspace(ti);
319}
320
321static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir, bool shift_held)
322{
323 tinput_pre_seek(ti, shift_held);
324
325 if (dir == seek_forward) {
326 if (ti->pos < ti->nc)
327 ti->pos += 1;
328 } else {
329 if (ti->pos > 0)
330 ti->pos -= 1;
331 }
332
333 tinput_post_seek(ti, shift_held);
334}
335
336static void tinput_seek_word(tinput_t *ti, seek_dir_t dir, bool shift_held)
337{
338 tinput_pre_seek(ti, shift_held);
339
340 if (dir == seek_forward) {
341 if (ti->pos == ti->nc)
342 return;
343
344 while (true) {
345 ti->pos += 1;
346
347 if (ti->pos == ti->nc)
348 break;
349
350 if ((ti->buffer[ti->pos - 1] == ' ') &&
351 (ti->buffer[ti->pos] != ' '))
352 break;
353 }
354 } else {
355 if (ti->pos == 0)
356 return;
357
358 while (true) {
359 ti->pos -= 1;
360
361 if (ti->pos == 0)
362 break;
363
364 if (ti->buffer[ti->pos - 1] == ' ' &&
365 ti->buffer[ti->pos] != ' ')
366 break;
367 }
368
369 }
370
371 tinput_post_seek(ti, shift_held);
372}
373
374static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir, bool shift_held)
375{
376 tinput_pre_seek(ti, shift_held);
377
378 if (dir == seek_forward) {
379 if (ti->pos + ti->con_cols <= ti->nc)
380 ti->pos = ti->pos + ti->con_cols;
381 } else {
382 if (ti->pos >= ti->con_cols)
383 ti->pos = ti->pos - ti->con_cols;
384 }
385
386 tinput_post_seek(ti, shift_held);
387}
388
389static void tinput_seek_scrpos(tinput_t *ti, int col, int line, bool shift_held)
390{
391 unsigned lpos;
392 tinput_pre_seek(ti, shift_held);
393
394 lpos = LIN_POS(ti, col, line);
395
396 if (lpos > ti->text_coord)
397 ti->pos = lpos - ti->text_coord;
398 else
399 ti->pos = 0;
400 if (ti->pos > ti->nc)
401 ti->pos = ti->nc;
402
403 tinput_post_seek(ti, shift_held);
404}
405
406static void tinput_seek_max(tinput_t *ti, seek_dir_t dir, bool shift_held)
407{
408 tinput_pre_seek(ti, shift_held);
409
410 if (dir == seek_backward)
411 ti->pos = 0;
412 else
413 ti->pos = ti->nc;
414
415 tinput_post_seek(ti, shift_held);
416}
417
418static void tinput_pre_seek(tinput_t *ti, bool shift_held)
419{
420 if ((tinput_sel_active(ti)) && (!shift_held)) {
421 /* Unselect and redraw. */
422 ti->sel_start = ti->pos;
423 tinput_display_tail(ti, 0, 0);
424 tinput_position_caret(ti);
425 }
426}
427
428static void tinput_post_seek(tinput_t *ti, bool shift_held)
429{
430 if (shift_held) {
431 /* Selecting text. Need redraw. */
432 tinput_display_tail(ti, 0, 0);
433 } else {
434 /* Shift not held. Keep selection empty. */
435 ti->sel_start = ti->pos;
436 }
437
438 tinput_position_caret(ti);
439}
440
441static void tinput_history_insert(tinput_t *ti, char *str)
442{
443 if (ti->hnum < HISTORY_LEN) {
444 ti->hnum += 1;
445 } else {
446 if (ti->history[HISTORY_LEN] != NULL)
447 free(ti->history[HISTORY_LEN]);
448 }
449
450 size_t i;
451 for (i = ti->hnum; i > 1; i--)
452 ti->history[i] = ti->history[i - 1];
453
454 ti->history[1] = str_dup(str);
455
456 if (ti->history[0] != NULL) {
457 free(ti->history[0]);
458 ti->history[0] = NULL;
459 }
460}
461
462static void tinput_set_str(tinput_t *ti, const char *str)
463{
464 str_to_wstr(ti->buffer, INPUT_MAX_SIZE, str);
465 ti->nc = wstr_length(ti->buffer);
466 ti->pos = ti->nc;
467 ti->sel_start = ti->pos;
468}
469
470static void tinput_sel_get_bounds(tinput_t *ti, size_t *sa, size_t *sb)
471{
472 if (ti->sel_start < ti->pos) {
473 *sa = ti->sel_start;
474 *sb = ti->pos;
475 } else {
476 *sa = ti->pos;
477 *sb = ti->sel_start;
478 }
479}
480
481static bool tinput_sel_active(tinput_t *ti)
482{
483 return (ti->sel_start != ti->pos);
484}
485
486static void tinput_sel_all(tinput_t *ti)
487{
488 ti->sel_start = 0;
489 ti->pos = ti->nc;
490 tinput_display_tail(ti, 0, 0);
491 tinput_position_caret(ti);
492}
493
494static void tinput_sel_delete(tinput_t *ti)
495{
496 size_t sa;
497 size_t sb;
498
499 tinput_sel_get_bounds(ti, &sa, &sb);
500 if (sa == sb)
501 return;
502
503 memmove(ti->buffer + sa, ti->buffer + sb,
504 (ti->nc - sb) * sizeof(wchar_t));
505
506 ti->pos = ti->sel_start = sa;
507 ti->nc -= (sb - sa);
508 ti->buffer[ti->nc] = '\0';
509
510 tinput_display_tail(ti, sa, sb - sa);
511 tinput_position_caret(ti);
512}
513
514static void tinput_sel_copy_to_cb(tinput_t *ti)
515{
516 size_t sa;
517 size_t sb;
518
519 tinput_sel_get_bounds(ti, &sa, &sb);
520
521 char *str;
522
523 if (sb < ti->nc) {
524 wchar_t tmp_c = ti->buffer[sb];
525 ti->buffer[sb] = '\0';
526 str = wstr_to_astr(ti->buffer + sa);
527 ti->buffer[sb] = tmp_c;
528 } else
529 str = wstr_to_astr(ti->buffer + sa);
530
531 if (str == NULL)
532 goto error;
533
534 if (clipboard_put_str(str) != EOK)
535 goto error;
536
537 free(str);
538 return;
539
540error:
541 /* TODO: Give the user some kind of warning. */
542 return;
543}
544
545static void tinput_paste_from_cb(tinput_t *ti)
546{
547 char *str;
548 errno_t rc = clipboard_get_str(&str);
549
550 if ((rc != EOK) || (str == NULL)) {
551 /* TODO: Give the user some kind of warning. */
552 return;
553 }
554
555 tinput_insert_string(ti, str);
556 free(str);
557}
558
559static void tinput_history_seek(tinput_t *ti, int offs)
560{
561 if (offs >= 0) {
562 if (ti->hpos + offs > ti->hnum)
563 return;
564 } else {
565 if (ti->hpos < (size_t) -offs)
566 return;
567 }
568
569 if (ti->history[ti->hpos] != NULL) {
570 free(ti->history[ti->hpos]);
571 ti->history[ti->hpos] = NULL;
572 }
573
574 ti->history[ti->hpos] = tinput_get_str(ti);
575 ti->hpos += offs;
576
577 int pad = (int) ti->nc - str_length(ti->history[ti->hpos]);
578 if (pad < 0)
579 pad = 0;
580
581 tinput_set_str(ti, ti->history[ti->hpos]);
582 tinput_display_tail(ti, 0, pad);
583 tinput_update_origin(ti);
584 tinput_position_caret(ti);
585}
586
587/** Compare two entries in array of completions. */
588static int compl_cmp(const void *va, const void *vb)
589{
590 const char *a = *(const char **) va;
591 const char *b = *(const char **) vb;
592
593 return str_cmp(a, b);
594}
595
596static size_t common_pref_len(const char *a, const char *b)
597{
598 size_t i;
599 size_t a_off, b_off;
600 wchar_t ca, cb;
601
602 i = 0;
603 a_off = 0;
604 b_off = 0;
605
606 while (true) {
607 ca = str_decode(a, &a_off, STR_NO_LIMIT);
608 cb = str_decode(b, &b_off, STR_NO_LIMIT);
609
610 if (ca == '\0' || cb == '\0' || ca != cb)
611 break;
612 ++i;
613 }
614
615 return i;
616}
617
618/* Print a list of completions */
619static void tinput_show_completions(tinput_t *ti, char **compl, size_t cnum)
620{
621 unsigned int i;
622 /* Determine the maximum width of the completion in chars */
623 size_t max_width = 0;
624 for (i = 0; i < cnum; i++)
625 max_width = max(max_width, str_width(compl[i]));
626
627 unsigned int cols = max(1, (ti->con_cols + 1) / (max_width + 1));
628 unsigned int padding = 0;
629 if (cols * max_width + (cols - 1) < ti->con_cols) {
630 padding = ti->con_cols - cols * max_width - (cols - 1);
631 }
632 unsigned int col_width = max_width + padding / cols;
633 unsigned int rows = cnum / cols + ((cnum % cols) != 0);
634
635 unsigned int row, col;
636
637 for (row = 0; row < rows; row++) {
638 unsigned int display_col = 0;
639 for (col = 0; col < cols; col++) {
640 size_t compl_idx = col * rows + row;
641 if (compl_idx >= cnum)
642 break;
643 if (col) {
644 printf(" ");
645 display_col++;
646 }
647 printf("%s", compl[compl_idx]);
648 size_t compl_width = str_width(compl[compl_idx]);
649 display_col += compl_width;
650 if (col < cols - 1) {
651 for (i = compl_width; i < col_width; i++) {
652 printf(" ");
653 display_col++;
654 }
655 }
656 }
657 if ((display_col % ti->con_cols) > 0)
658 printf("\n");
659 }
660 fflush(stdout);
661}
662
663
664static void tinput_text_complete(tinput_t *ti)
665{
666 void *state;
667 size_t cstart;
668 char *ctmp;
669 char **compl; /* Array of completions */
670 size_t compl_len; /* Current length of @c compl array */
671 size_t cnum;
672 size_t i;
673 errno_t rc;
674
675 if (ti->compl_ops == NULL)
676 return;
677
678 /*
679 * Obtain list of all possible completions (growing array).
680 */
681
682 rc = (*ti->compl_ops->init)(ti->buffer, ti->pos, &cstart, &state);
683 if (rc != EOK)
684 return;
685
686 cnum = 0;
687
688 compl_len = 1;
689 compl = malloc(compl_len * sizeof(char *));
690 if (compl == NULL) {
691 printf("Error: Out of memory.\n");
692 return;
693 }
694
695 while (true) {
696 rc = (*ti->compl_ops->get_next)(state, &ctmp);
697 if (rc != EOK)
698 break;
699
700 if (cnum >= compl_len) {
701 /* Extend array */
702 compl_len = 2 * compl_len;
703 compl = realloc(compl, compl_len * sizeof(char *));
704 if (compl == NULL) {
705 printf("Error: Out of memory.\n");
706 break;
707 }
708 }
709
710 compl[cnum] = str_dup(ctmp);
711 if (compl[cnum] == NULL) {
712 printf("Error: Out of memory.\n");
713 break;
714 }
715 cnum++;
716 }
717
718 (*ti->compl_ops->fini)(state);
719
720 if (cnum > 1) {
721 /*
722 * More than one match. Determine maximum common prefix.
723 */
724 size_t cplen;
725
726 cplen = str_length(compl[0]);
727 for (i = 1; i < cnum; i++)
728 cplen = min(cplen, common_pref_len(compl[0], compl[i]));
729
730 /* Compute how many bytes we should skip. */
731 size_t istart = str_lsize(compl[0], ti->pos - cstart);
732
733 if (cplen > istart) {
734 /* Insert common prefix. */
735
736 /* Copy remainder of common prefix. */
737 char *cpref = str_ndup(compl[0] + istart,
738 str_lsize(compl[0], cplen - istart));
739
740 /* Insert it. */
741 tinput_insert_string(ti, cpref);
742 free(cpref);
743 } else {
744 /* No common prefix. Sort and display all entries. */
745
746 qsort(compl, cnum, sizeof(char *), compl_cmp);
747
748 tinput_jump_after(ti);
749 tinput_show_completions(ti, compl, cnum);
750 tinput_display(ti);
751 }
752 } else if (cnum == 1) {
753 /*
754 * We have exactly one match. Insert it.
755 */
756
757 /* Compute how many bytes of completion string we should skip. */
758 size_t istart = str_lsize(compl[0], ti->pos - cstart);
759
760 /* Insert remainder of completion string at current position. */
761 tinput_insert_string(ti, compl[0] + istart);
762 }
763
764 for (i = 0; i < cnum; i++)
765 free(compl[i]);
766 free(compl);
767}
768
769/** Initialize text input field.
770 *
771 * Must be called before using the field. It clears the history.
772 */
773static void tinput_init(tinput_t *ti)
774{
775 ti->console = console_init(stdin, stdout);
776 ti->hnum = 0;
777 ti->hpos = 0;
778 ti->history[0] = NULL;
779}
780
781/** Set prompt string.
782 *
783 * @param ti Text input
784 * @param prompt Prompt string
785 *
786 * @return EOK on success, ENOMEM if out of memory.
787 */
788errno_t tinput_set_prompt(tinput_t *ti, const char *prompt)
789{
790 if (ti->prompt != NULL)
791 free(ti->prompt);
792
793 ti->prompt = str_dup(prompt);
794 if (ti->prompt == NULL)
795 return ENOMEM;
796
797 return EOK;
798}
799
800/** Set completion ops.
801 *
802 * Set pointer to completion ops structure that will be used for text
803 * completion.
804 */
805void tinput_set_compl_ops(tinput_t *ti, tinput_compl_ops_t *compl_ops)
806{
807 ti->compl_ops = compl_ops;
808}
809
810/** Handle key press event. */
811static void tinput_key_press(tinput_t *ti, kbd_event_t *kev)
812{
813 if (kev->key == KC_LSHIFT)
814 ti->lshift_held = true;
815 if (kev->key == KC_RSHIFT)
816 ti->rshift_held = true;
817
818 if (((kev->mods & KM_CTRL) != 0) &&
819 ((kev->mods & (KM_ALT | KM_SHIFT)) == 0))
820 tinput_key_ctrl(ti, kev);
821
822 if (((kev->mods & KM_SHIFT) != 0) &&
823 ((kev->mods & (KM_CTRL | KM_ALT)) == 0))
824 tinput_key_shift(ti, kev);
825
826 if (((kev->mods & KM_CTRL) != 0) &&
827 ((kev->mods & KM_SHIFT) != 0) &&
828 ((kev->mods & KM_ALT) == 0))
829 tinput_key_ctrl_shift(ti, kev);
830
831 if ((kev->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0)
832 tinput_key_unmod(ti, kev);
833
834 if (kev->c >= ' ') {
835 tinput_sel_delete(ti);
836 tinput_insert_char(ti, kev->c);
837 }
838}
839
840/** Handle key release event. */
841static void tinput_key_release(tinput_t *ti, kbd_event_t *kev)
842{
843 if (kev->key == KC_LSHIFT)
844 ti->lshift_held = false;
845 if (kev->key == KC_RSHIFT)
846 ti->rshift_held = false;
847}
848
849/** Position event */
850static void tinput_pos(tinput_t *ti, pos_event_t *ev)
851{
852 if (ev->type == POS_PRESS) {
853 tinput_seek_scrpos(ti, ev->hpos, ev->vpos,
854 ti->lshift_held || ti->rshift_held);
855 }
856}
857
858/** Read in one line of input with initial text provided.
859 *
860 * @param ti Text input
861 * @param istr Initial string
862 * @param dstr Place to save pointer to new string
863 *
864 * @return EOK on success
865 * @return ENOENT if user requested abort
866 * @return EIO if communication with console failed
867 *
868 */
869errno_t tinput_read_i(tinput_t *ti, const char *istr, char **dstr)
870{
871 console_flush(ti->console);
872 if (console_get_size(ti->console, &ti->con_cols, &ti->con_rows) != EOK)
873 return EIO;
874
875 tinput_set_str(ti, istr);
876
877 ti->sel_start = 0;
878 ti->done = false;
879 ti->exit_clui = false;
880
881 if (tinput_display(ti) != EOK)
882 return EIO;
883
884 while (!ti->done) {
885 console_flush(ti->console);
886
887 cons_event_t ev;
888 if (!console_get_event(ti->console, &ev))
889 return EIO;
890
891 switch (ev.type) {
892 case CEV_KEY:
893 if (ev.ev.key.type == KEY_PRESS)
894 tinput_key_press(ti, &ev.ev.key);
895 else
896 tinput_key_release(ti, &ev.ev.key);
897 break;
898 case CEV_POS:
899 tinput_pos(ti, &ev.ev.pos);
900 break;
901 }
902 }
903
904 if (ti->exit_clui)
905 return ENOENT;
906
907 ti->pos = ti->nc;
908 tinput_position_caret(ti);
909 putchar('\n');
910
911 char *str = tinput_get_str(ti);
912 if (str_cmp(str, "") != 0)
913 tinput_history_insert(ti, str);
914
915 ti->hpos = 0;
916
917 *dstr = str;
918 return EOK;
919}
920
921/** Read in one line of input.
922 *
923 * @param ti Text input
924 * @param dstr Place to save pointer to new string.
925 *
926 * @return EOK on success
927 * @return ENOENT if user requested abort
928 * @return EIO if communication with console failed
929 *
930 */
931errno_t tinput_read(tinput_t *ti, char **dstr)
932{
933 return tinput_read_i(ti, "", dstr);
934}
935
936static void tinput_key_ctrl(tinput_t *ti, kbd_event_t *ev)
937{
938 switch (ev->key) {
939 case KC_LEFT:
940 tinput_seek_word(ti, seek_backward, false);
941 break;
942 case KC_RIGHT:
943 tinput_seek_word(ti, seek_forward, false);
944 break;
945 case KC_UP:
946 tinput_seek_vertical(ti, seek_backward, false);
947 break;
948 case KC_DOWN:
949 tinput_seek_vertical(ti, seek_forward, false);
950 break;
951 case KC_X:
952 tinput_sel_copy_to_cb(ti);
953 tinput_sel_delete(ti);
954 break;
955 case KC_C:
956 tinput_sel_copy_to_cb(ti);
957 break;
958 case KC_V:
959 tinput_sel_delete(ti);
960 tinput_paste_from_cb(ti);
961 break;
962 case KC_A:
963 tinput_sel_all(ti);
964 break;
965 case KC_Q:
966 /* Signal libary client to quit interactive loop. */
967 ti->done = true;
968 ti->exit_clui = true;
969 break;
970 default:
971 break;
972 }
973}
974
975static void tinput_key_ctrl_shift(tinput_t *ti, kbd_event_t *ev)
976{
977 switch (ev->key) {
978 case KC_LEFT:
979 tinput_seek_word(ti, seek_backward, true);
980 break;
981 case KC_RIGHT:
982 tinput_seek_word(ti, seek_forward, true);
983 break;
984 case KC_UP:
985 tinput_seek_vertical(ti, seek_backward, true);
986 break;
987 case KC_DOWN:
988 tinput_seek_vertical(ti, seek_forward, true);
989 break;
990 default:
991 break;
992 }
993}
994
995static void tinput_key_shift(tinput_t *ti, kbd_event_t *ev)
996{
997 switch (ev->key) {
998 case KC_LEFT:
999 tinput_seek_cell(ti, seek_backward, true);
1000 break;
1001 case KC_RIGHT:
1002 tinput_seek_cell(ti, seek_forward, true);
1003 break;
1004 case KC_UP:
1005 tinput_seek_vertical(ti, seek_backward, true);
1006 break;
1007 case KC_DOWN:
1008 tinput_seek_vertical(ti, seek_forward, true);
1009 break;
1010 case KC_HOME:
1011 tinput_seek_max(ti, seek_backward, true);
1012 break;
1013 case KC_END:
1014 tinput_seek_max(ti, seek_forward, true);
1015 break;
1016 default:
1017 break;
1018 }
1019}
1020
1021static void tinput_key_unmod(tinput_t *ti, kbd_event_t *ev)
1022{
1023 switch (ev->key) {
1024 case KC_ENTER:
1025 case KC_NENTER:
1026 ti->done = true;
1027 break;
1028 case KC_BACKSPACE:
1029 tinput_backspace(ti);
1030 break;
1031 case KC_DELETE:
1032 tinput_delete(ti);
1033 break;
1034 case KC_LEFT:
1035 tinput_seek_cell(ti, seek_backward, false);
1036 break;
1037 case KC_RIGHT:
1038 tinput_seek_cell(ti, seek_forward, false);
1039 break;
1040 case KC_HOME:
1041 tinput_seek_max(ti, seek_backward, false);
1042 break;
1043 case KC_END:
1044 tinput_seek_max(ti, seek_forward, false);
1045 break;
1046 case KC_UP:
1047 tinput_history_seek(ti, 1);
1048 break;
1049 case KC_DOWN:
1050 tinput_history_seek(ti, -1);
1051 break;
1052 case KC_TAB:
1053 tinput_text_complete(ti);
1054 break;
1055 default:
1056 break;
1057 }
1058}
1059
1060/**
1061 * @}
1062 */
Note: See TracBrowser for help on using the repository browser.