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