Changeset a35b458 in mainline for uspace/lib/clui/tinput.c
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/clui/tinput.c
r3061bc1 ra35b458 75 75 { 76 76 tinput_t *ti; 77 77 78 78 ti = calloc(1, sizeof(tinput_t)); 79 79 if (ti == NULL) 80 80 return NULL; 81 81 82 82 tinput_init(ti); 83 83 return ti; … … 107 107 if (!dbuf) 108 108 return; 109 109 110 110 size_t sa; 111 111 size_t sb; 112 112 tinput_sel_get_bounds(ti, &sa, &sb); 113 113 114 114 tinput_console_set_lpos(ti, ti->text_coord + start); 115 115 console_set_style(ti->console, STYLE_NORMAL); 116 116 117 117 size_t p = start; 118 118 if (p < sa) { … … 122 122 p = sa; 123 123 } 124 124 125 125 if (p < sb) { 126 126 console_flush(ti->console); 127 127 console_set_style(ti->console, STYLE_SELECTED); 128 128 129 129 memcpy(dbuf, ti->buffer + p, 130 130 (sb - p) * sizeof(wchar_t)); … … 133 133 p = sb; 134 134 } 135 135 136 136 console_flush(ti->console); 137 137 console_set_style(ti->console, STYLE_NORMAL); 138 138 139 139 if (p < ti->nc) { 140 140 memcpy(dbuf, ti->buffer + p, … … 143 143 printf("%ls", dbuf); 144 144 } 145 145 146 146 for (p = 0; p < pad; p++) 147 147 putchar(' '); 148 148 149 149 console_flush(ti->console); 150 150 … … 188 188 { 189 189 sysarg_t col0, row0; 190 190 191 191 if (console_get_pos(ti->console, &col0, &row0) != EOK) 192 192 return EIO; 193 193 194 194 ti->prompt_coord = row0 * ti->con_cols + col0; 195 195 ti->text_coord = ti->prompt_coord + str_length(ti->prompt); … … 206 206 if (ti->nc == INPUT_MAX_SIZE) 207 207 return; 208 208 209 209 unsigned new_width = LIN_TO_COL(ti, ti->text_coord) + ti->nc + 1; 210 210 if (new_width % ti->con_cols == 0) { … … 216 216 } 217 217 } 218 218 219 219 size_t i; 220 220 for (i = ti->nc; i > ti->pos; i--) 221 221 ti->buffer[i] = ti->buffer[i - 1]; 222 222 223 223 ti->buffer[ti->pos] = c; 224 224 ti->pos += 1; … … 226 226 ti->buffer[ti->nc] = '\0'; 227 227 ti->sel_start = ti->pos; 228 228 229 229 tinput_display_tail(ti, ti->pos - 1, 0); 230 230 tinput_update_origin(ti); … … 237 237 if (ilen == 0) 238 238 return; 239 239 240 240 unsigned new_width = LIN_TO_COL(ti, ti->text_coord) + ti->nc + ilen; 241 241 unsigned new_height = (new_width / ti->con_cols) + 1; … … 244 244 return; 245 245 } 246 246 247 247 if (ti->nc > 0) { 248 248 size_t i; … … 250 250 ti->buffer[i + ilen - 1] = ti->buffer[i - 1]; 251 251 } 252 252 253 253 size_t off = 0; 254 254 size_t i = 0; … … 257 257 if (c == '\0') 258 258 break; 259 259 260 260 /* Filter out non-printable chars. */ 261 261 if (c < 32) 262 262 c = 32; 263 263 264 264 ti->buffer[ti->pos + i] = c; 265 265 i++; 266 266 } 267 267 268 268 ti->pos += ilen; 269 269 ti->nc += ilen; 270 270 ti->buffer[ti->nc] = '\0'; 271 271 ti->sel_start = ti->pos; 272 272 273 273 tinput_display_tail(ti, ti->pos - ilen, 0); 274 274 tinput_update_origin(ti); … … 282 282 return; 283 283 } 284 284 285 285 if (ti->pos == 0) 286 286 return; 287 287 288 288 size_t i; 289 289 for (i = ti->pos; i < ti->nc; i++) 290 290 ti->buffer[i - 1] = ti->buffer[i]; 291 291 292 292 ti->pos -= 1; 293 293 ti->nc -= 1; 294 294 ti->buffer[ti->nc] = '\0'; 295 295 ti->sel_start = ti->pos; 296 296 297 297 tinput_display_tail(ti, ti->pos, 1); 298 298 tinput_position_caret(ti); … … 305 305 return; 306 306 } 307 307 308 308 if (ti->pos == ti->nc) 309 309 return; 310 310 311 311 ti->pos += 1; 312 312 ti->sel_start = ti->pos; 313 313 314 314 tinput_backspace(ti); 315 315 } … … 318 318 { 319 319 tinput_pre_seek(ti, shift_held); 320 320 321 321 if (dir == seek_forward) { 322 322 if (ti->pos < ti->nc) … … 326 326 ti->pos -= 1; 327 327 } 328 328 329 329 tinput_post_seek(ti, shift_held); 330 330 } … … 333 333 { 334 334 tinput_pre_seek(ti, shift_held); 335 335 336 336 if (dir == seek_forward) { 337 337 if (ti->pos == ti->nc) 338 338 return; 339 339 340 340 while (true) { 341 341 ti->pos += 1; 342 342 343 343 if (ti->pos == ti->nc) 344 344 break; 345 345 346 346 if ((ti->buffer[ti->pos - 1] == ' ') && 347 347 (ti->buffer[ti->pos] != ' ')) … … 351 351 if (ti->pos == 0) 352 352 return; 353 353 354 354 while (true) { 355 355 ti->pos -= 1; 356 356 357 357 if (ti->pos == 0) 358 358 break; 359 359 360 360 if (ti->buffer[ti->pos - 1] == ' ' && 361 361 ti->buffer[ti->pos] != ' ') 362 362 break; 363 363 } 364 365 } 366 364 365 } 366 367 367 tinput_post_seek(ti, shift_held); 368 368 } … … 371 371 { 372 372 tinput_pre_seek(ti, shift_held); 373 373 374 374 if (dir == seek_forward) { 375 375 if (ti->pos + ti->con_cols <= ti->nc) … … 379 379 ti->pos = ti->pos - ti->con_cols; 380 380 } 381 381 382 382 tinput_post_seek(ti, shift_held); 383 383 } … … 403 403 { 404 404 tinput_pre_seek(ti, shift_held); 405 405 406 406 if (dir == seek_backward) 407 407 ti->pos = 0; 408 408 else 409 409 ti->pos = ti->nc; 410 410 411 411 tinput_post_seek(ti, shift_held); 412 412 } … … 431 431 ti->sel_start = ti->pos; 432 432 } 433 433 434 434 tinput_position_caret(ti); 435 435 } … … 443 443 free(ti->history[HISTORY_LEN]); 444 444 } 445 445 446 446 size_t i; 447 447 for (i = ti->hnum; i > 1; i--) 448 448 ti->history[i] = ti->history[i - 1]; 449 449 450 450 ti->history[1] = str_dup(str); 451 451 452 452 if (ti->history[0] != NULL) { 453 453 free(ti->history[0]); … … 492 492 size_t sa; 493 493 size_t sb; 494 494 495 495 tinput_sel_get_bounds(ti, &sa, &sb); 496 496 if (sa == sb) 497 497 return; 498 498 499 499 memmove(ti->buffer + sa, ti->buffer + sb, 500 500 (ti->nc - sb) * sizeof(wchar_t)); 501 501 502 502 ti->pos = ti->sel_start = sa; 503 503 ti->nc -= (sb - sa); 504 504 ti->buffer[ti->nc] = '\0'; 505 505 506 506 tinput_display_tail(ti, sa, sb - sa); 507 507 tinput_position_caret(ti); … … 512 512 size_t sa; 513 513 size_t sb; 514 514 515 515 tinput_sel_get_bounds(ti, &sa, &sb); 516 516 517 517 char *str; 518 518 519 519 if (sb < ti->nc) { 520 520 wchar_t tmp_c = ti->buffer[sb]; … … 524 524 } else 525 525 str = wstr_to_astr(ti->buffer + sa); 526 526 527 527 if (str == NULL) 528 528 goto error; 529 529 530 530 if (clipboard_put_str(str) != EOK) 531 531 goto error; 532 532 533 533 free(str); 534 534 return; 535 535 536 536 error: 537 537 /* TODO: Give the user some kind of warning. */ … … 543 543 char *str; 544 544 errno_t rc = clipboard_get_str(&str); 545 545 546 546 if ((rc != EOK) || (str == NULL)) { 547 547 /* TODO: Give the user some kind of warning. */ 548 548 return; 549 549 } 550 550 551 551 tinput_insert_string(ti, str); 552 552 free(str); … … 562 562 return; 563 563 } 564 564 565 565 if (ti->history[ti->hpos] != NULL) { 566 566 free(ti->history[ti->hpos]); 567 567 ti->history[ti->hpos] = NULL; 568 568 } 569 569 570 570 ti->history[ti->hpos] = tinput_get_str(ti); 571 571 ti->hpos += offs; 572 572 573 573 int pad = (int) ti->nc - str_length(ti->history[ti->hpos]); 574 574 if (pad < 0) 575 575 pad = 0; 576 576 577 577 tinput_set_str(ti, ti->history[ti->hpos]); 578 578 tinput_display_tail(ti, 0, pad); … … 620 620 for (i = 0; i < cnum; i++) 621 621 max_width = max(max_width, str_width(compl[i])); 622 622 623 623 unsigned int cols = max(1, (ti->con_cols + 1) / (max_width + 1)); 624 624 unsigned int padding = 0; … … 628 628 unsigned int col_width = max_width + padding / cols; 629 629 unsigned int rows = cnum / cols + ((cnum % cols) != 0); 630 630 631 631 unsigned int row, col; 632 632 633 633 for (row = 0; row < rows; row++) { 634 634 unsigned int display_col = 0; … … 786 786 if (ti->prompt != NULL) 787 787 free(ti->prompt); 788 788 789 789 ti->prompt = str_dup(prompt); 790 790 if (ti->prompt == NULL) 791 791 return ENOMEM; 792 792 793 793 return EOK; 794 794 } … … 815 815 ((kev->mods & (KM_ALT | KM_SHIFT)) == 0)) 816 816 tinput_key_ctrl(ti, kev); 817 817 818 818 if (((kev->mods & KM_SHIFT) != 0) && 819 819 ((kev->mods & (KM_CTRL | KM_ALT)) == 0)) 820 820 tinput_key_shift(ti, kev); 821 821 822 822 if (((kev->mods & KM_CTRL) != 0) && 823 823 ((kev->mods & KM_SHIFT) != 0) && 824 824 ((kev->mods & KM_ALT) == 0)) 825 825 tinput_key_ctrl_shift(ti, kev); 826 826 827 827 if ((kev->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) 828 828 tinput_key_unmod(ti, kev); 829 829 830 830 if (kev->c >= ' ') { 831 831 tinput_sel_delete(ti); … … 868 868 if (console_get_size(ti->console, &ti->con_cols, &ti->con_rows) != EOK) 869 869 return EIO; 870 870 871 871 tinput_set_str(ti, istr); 872 872 … … 874 874 ti->done = false; 875 875 ti->exit_clui = false; 876 876 877 877 if (tinput_display(ti) != EOK) 878 878 return EIO; 879 879 880 880 while (!ti->done) { 881 881 console_flush(ti->console); 882 882 883 883 cons_event_t ev; 884 884 if (!console_get_event(ti->console, &ev)) 885 885 return EIO; 886 886 887 887 switch (ev.type) { 888 888 case CEV_KEY: … … 897 897 } 898 898 } 899 899 900 900 if (ti->exit_clui) 901 901 return ENOENT; 902 902 903 903 ti->pos = ti->nc; 904 904 tinput_position_caret(ti); 905 905 putchar('\n'); 906 906 907 907 char *str = tinput_get_str(ti); 908 908 if (str_cmp(str, "") != 0) 909 909 tinput_history_insert(ti, str); 910 910 911 911 ti->hpos = 0; 912 912 913 913 *dstr = str; 914 914 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.