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