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 */
|
---|
53 | typedef enum {
|
---|
54 | seek_backward = -1,
|
---|
55 | seek_forward = 1
|
---|
56 | } seek_dir_t;
|
---|
57 |
|
---|
58 | static void tinput_init(tinput_t *);
|
---|
59 | static void tinput_insert_string(tinput_t *, const char *);
|
---|
60 | static void tinput_sel_get_bounds(tinput_t *, size_t *, size_t *);
|
---|
61 | static bool tinput_sel_active(tinput_t *);
|
---|
62 | static void tinput_sel_all(tinput_t *);
|
---|
63 | static void tinput_sel_delete(tinput_t *);
|
---|
64 | static void tinput_key_ctrl(tinput_t *, kbd_event_t *);
|
---|
65 | static void tinput_key_shift(tinput_t *, kbd_event_t *);
|
---|
66 | static void tinput_key_ctrl_shift(tinput_t *, kbd_event_t *);
|
---|
67 | static void tinput_key_unmod(tinput_t *, kbd_event_t *);
|
---|
68 | static void tinput_pre_seek(tinput_t *, bool);
|
---|
69 | static void tinput_post_seek(tinput_t *, bool);
|
---|
70 |
|
---|
71 | static 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. */
|
---|
78 | tinput_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. */
|
---|
91 | void tinput_destroy(tinput_t *ti)
|
---|
92 | {
|
---|
93 | if (ti->prompt != NULL)
|
---|
94 | free(ti->prompt);
|
---|
95 | free(ti);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static 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 |
|
---|
108 | static 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 |
|
---|
158 | static char *tinput_get_str(tinput_t *ti)
|
---|
159 | {
|
---|
160 | return wstr_to_astr(ti->buffer);
|
---|
161 | }
|
---|
162 |
|
---|
163 | static 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. */
|
---|
169 | static 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 |
|
---|
184 | static 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 |
|
---|
191 | static 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 |
|
---|
208 | static 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 |
|
---|
238 | static 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 |
|
---|
282 | static 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 |
|
---|
305 | static 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 |
|
---|
321 | static 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 |
|
---|
336 | static 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 |
|
---|
374 | static 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 |
|
---|
389 | static 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 |
|
---|
406 | static 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 |
|
---|
418 | static 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 |
|
---|
428 | static 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 |
|
---|
441 | static 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 |
|
---|
462 | static 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 |
|
---|
470 | static 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 |
|
---|
481 | static bool tinput_sel_active(tinput_t *ti)
|
---|
482 | {
|
---|
483 | return (ti->sel_start != ti->pos);
|
---|
484 | }
|
---|
485 |
|
---|
486 | static 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 |
|
---|
494 | static 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 |
|
---|
514 | static 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 |
|
---|
540 | error:
|
---|
541 | /* TODO: Give the user some kind of warning. */
|
---|
542 | return;
|
---|
543 | }
|
---|
544 |
|
---|
545 | static 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 |
|
---|
559 | static 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. */
|
---|
588 | static 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 |
|
---|
596 | static 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 */
|
---|
619 | static 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 | static void tinput_text_complete(tinput_t *ti)
|
---|
664 | {
|
---|
665 | void *state;
|
---|
666 | size_t cstart;
|
---|
667 | char *ctmp;
|
---|
668 | char **compl; /* Array of completions */
|
---|
669 | size_t compl_len; /* Current length of @c compl array */
|
---|
670 | size_t cnum;
|
---|
671 | size_t i;
|
---|
672 | errno_t rc;
|
---|
673 |
|
---|
674 | if (ti->compl_ops == NULL)
|
---|
675 | return;
|
---|
676 |
|
---|
677 | /*
|
---|
678 | * Obtain list of all possible completions (growing array).
|
---|
679 | */
|
---|
680 |
|
---|
681 | rc = (*ti->compl_ops->init)(ti->buffer, ti->pos, &cstart, &state);
|
---|
682 | if (rc != EOK)
|
---|
683 | return;
|
---|
684 |
|
---|
685 | cnum = 0;
|
---|
686 |
|
---|
687 | compl_len = 1;
|
---|
688 | compl = malloc(compl_len * sizeof(char *));
|
---|
689 | if (compl == NULL) {
|
---|
690 | printf("Error: Out of memory.\n");
|
---|
691 | return;
|
---|
692 | }
|
---|
693 |
|
---|
694 | while (true) {
|
---|
695 | rc = (*ti->compl_ops->get_next)(state, &ctmp);
|
---|
696 | if (rc != EOK)
|
---|
697 | break;
|
---|
698 |
|
---|
699 | if (cnum >= compl_len) {
|
---|
700 | /* Extend array */
|
---|
701 | compl_len = 2 * compl_len;
|
---|
702 | compl = realloc(compl, compl_len * sizeof(char *));
|
---|
703 | if (compl == NULL) {
|
---|
704 | printf("Error: Out of memory.\n");
|
---|
705 | break;
|
---|
706 | }
|
---|
707 | }
|
---|
708 |
|
---|
709 | compl[cnum] = str_dup(ctmp);
|
---|
710 | if (compl[cnum] == NULL) {
|
---|
711 | printf("Error: Out of memory.\n");
|
---|
712 | break;
|
---|
713 | }
|
---|
714 | cnum++;
|
---|
715 | }
|
---|
716 |
|
---|
717 | (*ti->compl_ops->fini)(state);
|
---|
718 |
|
---|
719 | if (cnum > 1) {
|
---|
720 | /*
|
---|
721 | * More than one match. Determine maximum common prefix.
|
---|
722 | */
|
---|
723 | size_t cplen;
|
---|
724 |
|
---|
725 | cplen = str_length(compl[0]);
|
---|
726 | for (i = 1; i < cnum; i++)
|
---|
727 | cplen = min(cplen, common_pref_len(compl[0], compl[i]));
|
---|
728 |
|
---|
729 | /* Compute how many bytes we should skip. */
|
---|
730 | size_t istart = str_lsize(compl[0], ti->pos - cstart);
|
---|
731 |
|
---|
732 | if (cplen > istart) {
|
---|
733 | /* Insert common prefix. */
|
---|
734 |
|
---|
735 | /* Copy remainder of common prefix. */
|
---|
736 | char *cpref = str_ndup(compl[0] + istart,
|
---|
737 | str_lsize(compl[0], cplen - istart));
|
---|
738 |
|
---|
739 | /* Insert it. */
|
---|
740 | tinput_insert_string(ti, cpref);
|
---|
741 | free(cpref);
|
---|
742 | } else {
|
---|
743 | /* No common prefix. Sort and display all entries. */
|
---|
744 |
|
---|
745 | qsort(compl, cnum, sizeof(char *), compl_cmp);
|
---|
746 |
|
---|
747 | tinput_jump_after(ti);
|
---|
748 | tinput_show_completions(ti, compl, cnum);
|
---|
749 | tinput_display(ti);
|
---|
750 | }
|
---|
751 | } else if (cnum == 1) {
|
---|
752 | /*
|
---|
753 | * We have exactly one match. Insert it.
|
---|
754 | */
|
---|
755 |
|
---|
756 | /* Compute how many bytes of completion string we should skip. */
|
---|
757 | size_t istart = str_lsize(compl[0], ti->pos - cstart);
|
---|
758 |
|
---|
759 | /* Insert remainder of completion string at current position. */
|
---|
760 | tinput_insert_string(ti, compl[0] + istart);
|
---|
761 | }
|
---|
762 |
|
---|
763 | for (i = 0; i < cnum; i++)
|
---|
764 | free(compl[i]);
|
---|
765 | free(compl);
|
---|
766 | }
|
---|
767 |
|
---|
768 | /** Initialize text input field.
|
---|
769 | *
|
---|
770 | * Must be called before using the field. It clears the history.
|
---|
771 | */
|
---|
772 | static void tinput_init(tinput_t *ti)
|
---|
773 | {
|
---|
774 | ti->console = console_init(stdin, stdout);
|
---|
775 | ti->hnum = 0;
|
---|
776 | ti->hpos = 0;
|
---|
777 | ti->history[0] = NULL;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /** Set prompt string.
|
---|
781 | *
|
---|
782 | * @param ti Text input
|
---|
783 | * @param prompt Prompt string
|
---|
784 | *
|
---|
785 | * @return EOK on success, ENOMEM if out of memory.
|
---|
786 | */
|
---|
787 | errno_t tinput_set_prompt(tinput_t *ti, const char *prompt)
|
---|
788 | {
|
---|
789 | if (ti->prompt != NULL)
|
---|
790 | free(ti->prompt);
|
---|
791 |
|
---|
792 | ti->prompt = str_dup(prompt);
|
---|
793 | if (ti->prompt == NULL)
|
---|
794 | return ENOMEM;
|
---|
795 |
|
---|
796 | return EOK;
|
---|
797 | }
|
---|
798 |
|
---|
799 | /** Set completion ops.
|
---|
800 | *
|
---|
801 | * Set pointer to completion ops structure that will be used for text
|
---|
802 | * completion.
|
---|
803 | */
|
---|
804 | void tinput_set_compl_ops(tinput_t *ti, tinput_compl_ops_t *compl_ops)
|
---|
805 | {
|
---|
806 | ti->compl_ops = compl_ops;
|
---|
807 | }
|
---|
808 |
|
---|
809 | /** Handle key press event. */
|
---|
810 | static void tinput_key_press(tinput_t *ti, kbd_event_t *kev)
|
---|
811 | {
|
---|
812 | if (kev->key == KC_LSHIFT)
|
---|
813 | ti->lshift_held = true;
|
---|
814 | if (kev->key == KC_RSHIFT)
|
---|
815 | ti->rshift_held = true;
|
---|
816 |
|
---|
817 | if (((kev->mods & KM_CTRL) != 0) &&
|
---|
818 | ((kev->mods & (KM_ALT | KM_SHIFT)) == 0))
|
---|
819 | tinput_key_ctrl(ti, kev);
|
---|
820 |
|
---|
821 | if (((kev->mods & KM_SHIFT) != 0) &&
|
---|
822 | ((kev->mods & (KM_CTRL | KM_ALT)) == 0))
|
---|
823 | tinput_key_shift(ti, kev);
|
---|
824 |
|
---|
825 | if (((kev->mods & KM_CTRL) != 0) &&
|
---|
826 | ((kev->mods & KM_SHIFT) != 0) &&
|
---|
827 | ((kev->mods & KM_ALT) == 0))
|
---|
828 | tinput_key_ctrl_shift(ti, kev);
|
---|
829 |
|
---|
830 | if ((kev->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0)
|
---|
831 | tinput_key_unmod(ti, kev);
|
---|
832 |
|
---|
833 | if (kev->c >= ' ') {
|
---|
834 | tinput_sel_delete(ti);
|
---|
835 | tinput_insert_char(ti, kev->c);
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | /** Handle key release event. */
|
---|
840 | static void tinput_key_release(tinput_t *ti, kbd_event_t *kev)
|
---|
841 | {
|
---|
842 | if (kev->key == KC_LSHIFT)
|
---|
843 | ti->lshift_held = false;
|
---|
844 | if (kev->key == KC_RSHIFT)
|
---|
845 | ti->rshift_held = false;
|
---|
846 | }
|
---|
847 |
|
---|
848 | /** Position event */
|
---|
849 | static void tinput_pos(tinput_t *ti, pos_event_t *ev)
|
---|
850 | {
|
---|
851 | if (ev->type == POS_PRESS) {
|
---|
852 | tinput_seek_scrpos(ti, ev->hpos, ev->vpos,
|
---|
853 | ti->lshift_held || ti->rshift_held);
|
---|
854 | }
|
---|
855 | }
|
---|
856 |
|
---|
857 | /** Read in one line of input with initial text provided.
|
---|
858 | *
|
---|
859 | * @param ti Text input
|
---|
860 | * @param istr Initial string
|
---|
861 | * @param dstr Place to save pointer to new string
|
---|
862 | *
|
---|
863 | * @return EOK on success
|
---|
864 | * @return ENOENT if user requested abort
|
---|
865 | * @return EIO if communication with console failed
|
---|
866 | *
|
---|
867 | */
|
---|
868 | errno_t tinput_read_i(tinput_t *ti, const char *istr, char **dstr)
|
---|
869 | {
|
---|
870 | console_flush(ti->console);
|
---|
871 | if (console_get_size(ti->console, &ti->con_cols, &ti->con_rows) != EOK)
|
---|
872 | return EIO;
|
---|
873 |
|
---|
874 | tinput_set_str(ti, istr);
|
---|
875 |
|
---|
876 | ti->sel_start = 0;
|
---|
877 | ti->done = false;
|
---|
878 | ti->exit_clui = false;
|
---|
879 |
|
---|
880 | if (tinput_display(ti) != EOK)
|
---|
881 | return EIO;
|
---|
882 |
|
---|
883 | while (!ti->done) {
|
---|
884 | console_flush(ti->console);
|
---|
885 |
|
---|
886 | cons_event_t ev;
|
---|
887 | if (!console_get_event(ti->console, &ev))
|
---|
888 | return EIO;
|
---|
889 |
|
---|
890 | switch (ev.type) {
|
---|
891 | case CEV_KEY:
|
---|
892 | if (ev.ev.key.type == KEY_PRESS)
|
---|
893 | tinput_key_press(ti, &ev.ev.key);
|
---|
894 | else
|
---|
895 | tinput_key_release(ti, &ev.ev.key);
|
---|
896 | break;
|
---|
897 | case CEV_POS:
|
---|
898 | tinput_pos(ti, &ev.ev.pos);
|
---|
899 | break;
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 | if (ti->exit_clui)
|
---|
904 | return ENOENT;
|
---|
905 |
|
---|
906 | ti->pos = ti->nc;
|
---|
907 | tinput_position_caret(ti);
|
---|
908 | putchar('\n');
|
---|
909 |
|
---|
910 | char *str = tinput_get_str(ti);
|
---|
911 | if (str_cmp(str, "") != 0)
|
---|
912 | tinput_history_insert(ti, str);
|
---|
913 |
|
---|
914 | ti->hpos = 0;
|
---|
915 |
|
---|
916 | *dstr = str;
|
---|
917 | return EOK;
|
---|
918 | }
|
---|
919 |
|
---|
920 | /** Read in one line of input.
|
---|
921 | *
|
---|
922 | * @param ti Text input
|
---|
923 | * @param dstr Place to save pointer to new string.
|
---|
924 | *
|
---|
925 | * @return EOK on success
|
---|
926 | * @return ENOENT if user requested abort
|
---|
927 | * @return EIO if communication with console failed
|
---|
928 | *
|
---|
929 | */
|
---|
930 | errno_t tinput_read(tinput_t *ti, char **dstr)
|
---|
931 | {
|
---|
932 | return tinput_read_i(ti, "", dstr);
|
---|
933 | }
|
---|
934 |
|
---|
935 | static void tinput_key_ctrl(tinput_t *ti, kbd_event_t *ev)
|
---|
936 | {
|
---|
937 | switch (ev->key) {
|
---|
938 | case KC_LEFT:
|
---|
939 | tinput_seek_word(ti, seek_backward, false);
|
---|
940 | break;
|
---|
941 | case KC_RIGHT:
|
---|
942 | tinput_seek_word(ti, seek_forward, false);
|
---|
943 | break;
|
---|
944 | case KC_UP:
|
---|
945 | tinput_seek_vertical(ti, seek_backward, false);
|
---|
946 | break;
|
---|
947 | case KC_DOWN:
|
---|
948 | tinput_seek_vertical(ti, seek_forward, false);
|
---|
949 | break;
|
---|
950 | case KC_X:
|
---|
951 | tinput_sel_copy_to_cb(ti);
|
---|
952 | tinput_sel_delete(ti);
|
---|
953 | break;
|
---|
954 | case KC_C:
|
---|
955 | tinput_sel_copy_to_cb(ti);
|
---|
956 | break;
|
---|
957 | case KC_V:
|
---|
958 | tinput_sel_delete(ti);
|
---|
959 | tinput_paste_from_cb(ti);
|
---|
960 | break;
|
---|
961 | case KC_A:
|
---|
962 | tinput_sel_all(ti);
|
---|
963 | break;
|
---|
964 | case KC_Q:
|
---|
965 | /* Signal libary client to quit interactive loop. */
|
---|
966 | ti->done = true;
|
---|
967 | ti->exit_clui = true;
|
---|
968 | break;
|
---|
969 | default:
|
---|
970 | break;
|
---|
971 | }
|
---|
972 | }
|
---|
973 |
|
---|
974 | static void tinput_key_ctrl_shift(tinput_t *ti, kbd_event_t *ev)
|
---|
975 | {
|
---|
976 | switch (ev->key) {
|
---|
977 | case KC_LEFT:
|
---|
978 | tinput_seek_word(ti, seek_backward, true);
|
---|
979 | break;
|
---|
980 | case KC_RIGHT:
|
---|
981 | tinput_seek_word(ti, seek_forward, true);
|
---|
982 | break;
|
---|
983 | case KC_UP:
|
---|
984 | tinput_seek_vertical(ti, seek_backward, true);
|
---|
985 | break;
|
---|
986 | case KC_DOWN:
|
---|
987 | tinput_seek_vertical(ti, seek_forward, true);
|
---|
988 | break;
|
---|
989 | default:
|
---|
990 | break;
|
---|
991 | }
|
---|
992 | }
|
---|
993 |
|
---|
994 | static void tinput_key_shift(tinput_t *ti, kbd_event_t *ev)
|
---|
995 | {
|
---|
996 | switch (ev->key) {
|
---|
997 | case KC_LEFT:
|
---|
998 | tinput_seek_cell(ti, seek_backward, true);
|
---|
999 | break;
|
---|
1000 | case KC_RIGHT:
|
---|
1001 | tinput_seek_cell(ti, seek_forward, true);
|
---|
1002 | break;
|
---|
1003 | case KC_UP:
|
---|
1004 | tinput_seek_vertical(ti, seek_backward, true);
|
---|
1005 | break;
|
---|
1006 | case KC_DOWN:
|
---|
1007 | tinput_seek_vertical(ti, seek_forward, true);
|
---|
1008 | break;
|
---|
1009 | case KC_HOME:
|
---|
1010 | tinput_seek_max(ti, seek_backward, true);
|
---|
1011 | break;
|
---|
1012 | case KC_END:
|
---|
1013 | tinput_seek_max(ti, seek_forward, true);
|
---|
1014 | break;
|
---|
1015 | default:
|
---|
1016 | break;
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | static void tinput_key_unmod(tinput_t *ti, kbd_event_t *ev)
|
---|
1021 | {
|
---|
1022 | switch (ev->key) {
|
---|
1023 | case KC_ENTER:
|
---|
1024 | case KC_NENTER:
|
---|
1025 | ti->done = true;
|
---|
1026 | break;
|
---|
1027 | case KC_BACKSPACE:
|
---|
1028 | tinput_backspace(ti);
|
---|
1029 | break;
|
---|
1030 | case KC_DELETE:
|
---|
1031 | tinput_delete(ti);
|
---|
1032 | break;
|
---|
1033 | case KC_LEFT:
|
---|
1034 | tinput_seek_cell(ti, seek_backward, false);
|
---|
1035 | break;
|
---|
1036 | case KC_RIGHT:
|
---|
1037 | tinput_seek_cell(ti, seek_forward, false);
|
---|
1038 | break;
|
---|
1039 | case KC_HOME:
|
---|
1040 | tinput_seek_max(ti, seek_backward, false);
|
---|
1041 | break;
|
---|
1042 | case KC_END:
|
---|
1043 | tinput_seek_max(ti, seek_forward, false);
|
---|
1044 | break;
|
---|
1045 | case KC_UP:
|
---|
1046 | tinput_history_seek(ti, 1);
|
---|
1047 | break;
|
---|
1048 | case KC_DOWN:
|
---|
1049 | tinput_history_seek(ti, -1);
|
---|
1050 | break;
|
---|
1051 | case KC_TAB:
|
---|
1052 | tinput_text_complete(ti);
|
---|
1053 | break;
|
---|
1054 | default:
|
---|
1055 | break;
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * @}
|
---|
1061 | */
|
---|