Changeset 7e0cb78 in mainline
- Timestamp:
- 2009-12-05T15:52:24Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ed372da
- Parents:
- 0b772f5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/input.c
r0b772f5 r7e0cb78 36 36 #include <io/keycode.h> 37 37 #include <io/style.h> 38 #include <io/color.h> 38 39 #include <vfs/vfs.h> 39 40 #include <errno.h> … … 56 57 int nc; 57 58 int pos; 59 int sel_start; 58 60 59 61 char *history[1 + HISTORY_LEN]; 60 62 int hnum; 61 63 int hpos; 64 bool done; 62 65 } tinput_t; 63 66 … … 70 73 71 74 static char *tinput_read(tinput_t *ti); 75 static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb); 76 static bool tinput_sel_active(tinput_t *ti); 77 static void tinput_sel_delete(tinput_t *ti); 78 static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev); 79 static void tinput_key_shift(tinput_t *ti, console_event_t *ev); 80 static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev); 81 static void tinput_key_unmod(tinput_t *ti, console_event_t *ev); 82 static void tinput_pre_seek(tinput_t *ti, bool shift_held); 83 static void tinput_post_seek(tinput_t *ti, bool shift_held); 72 84 73 85 /* Tokenizes input from console, sees if the first word is a built-in, if so … … 123 135 static void tinput_display_tail(tinput_t *ti, int start, int pad) 124 136 { 125 int i; 137 static wchar_t dbuf[INPUT_MAX + 1]; 138 int sa, sb; 139 int i, p; 140 141 tinput_sel_get_bounds(ti, &sa, &sb); 126 142 127 143 console_goto(fphone(stdout), (ti->col0 + start) % ti->con_cols, 128 144 ti->row0 + (ti->col0 + start) / ti->con_cols); 129 printf("%ls", ti->buffer + start); 145 console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0); 146 147 p = start; 148 if (p < sa) { 149 memcpy(dbuf, ti->buffer + p, (sa - p) * sizeof(wchar_t)); 150 dbuf[sa - p] = '\0'; 151 printf("%ls", dbuf); 152 p = sa; 153 } 154 155 if (p < sb) { 156 fflush(stdout); 157 console_set_color(fphone(stdout), COLOR_BLACK, COLOR_RED, 0); 158 memcpy(dbuf, ti->buffer + p, 159 (sb - p) * sizeof(wchar_t)); 160 dbuf[sb - p] = '\0'; 161 printf("%ls", dbuf); 162 p = sb; 163 } 164 165 fflush(stdout); 166 console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0); 167 168 if (p < ti->nc) { 169 memcpy(dbuf, ti->buffer + p, 170 (ti->nc - p) * sizeof(wchar_t)); 171 dbuf[ti->nc - p] = '\0'; 172 printf("%ls", dbuf); 173 } 174 130 175 for (i = 0; i < pad; ++i) 131 176 putchar(' '); … … 180 225 ti->nc += 1; 181 226 ti->buffer[ti->nc] = '\0'; 227 ti->sel_start = ti->pos; 182 228 183 229 tinput_display_tail(ti, ti->pos - 1, 0); … … 190 236 int i; 191 237 238 if (tinput_sel_active(ti)) { 239 tinput_sel_delete(ti); 240 return; 241 } 242 192 243 if (ti->pos == 0) 193 244 return; … … 198 249 ti->nc -= 1; 199 250 ti->buffer[ti->nc] = '\0'; 251 ti->sel_start = ti->pos; 200 252 201 253 tinput_display_tail(ti, ti->pos, 1); … … 205 257 static void tinput_delete(tinput_t *ti) 206 258 { 259 if (tinput_sel_active(ti)) { 260 tinput_sel_delete(ti); 261 return; 262 } 263 207 264 if (ti->pos == ti->nc) 208 265 return; 209 266 210 267 ti->pos += 1; 268 ti->sel_start = ti->pos; 269 211 270 tinput_backspace(ti); 212 271 } 213 272 214 static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir) 215 { 273 static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir, bool shift_held) 274 { 275 tinput_pre_seek(ti, shift_held); 276 216 277 if (dir == seek_forward) { 217 278 if (ti->pos < ti->nc) … … 222 283 } 223 284 224 tinput_position_caret(ti); 225 } 226 227 static void tinput_seek_word(tinput_t *ti, seek_dir_t dir) 228 { 285 tinput_post_seek(ti, shift_held); 286 } 287 288 static void tinput_seek_word(tinput_t *ti, seek_dir_t dir, bool shift_held) 289 { 290 tinput_pre_seek(ti, shift_held); 291 229 292 if (dir == seek_forward) { 230 293 if (ti->pos == ti->nc) … … 258 321 } 259 322 260 tinput_position_caret(ti); 261 } 262 263 static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir) 264 { 323 tinput_post_seek(ti, shift_held); 324 } 325 326 static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir, bool shift_held) 327 { 328 tinput_pre_seek(ti, shift_held); 329 265 330 if (dir == seek_forward) { 266 331 if (ti->pos + ti->con_cols <= ti->nc) … … 271 336 } 272 337 273 tinput_position_caret(ti); 274 } 275 276 static void tinput_seek_max(tinput_t *ti, seek_dir_t dir) 277 { 338 tinput_post_seek(ti, shift_held); 339 } 340 341 static void tinput_seek_max(tinput_t *ti, seek_dir_t dir, bool shift_held) 342 { 343 tinput_pre_seek(ti, shift_held); 344 278 345 if (dir == seek_backward) 279 346 ti->pos = 0; … … 281 348 ti->pos = ti->nc; 282 349 350 tinput_post_seek(ti, shift_held); 351 } 352 353 static void tinput_pre_seek(tinput_t *ti, bool shift_held) 354 { 355 if (tinput_sel_active(ti) && !shift_held) { 356 /* Unselect and redraw. */ 357 ti->sel_start = ti->pos; 358 tinput_display_tail(ti, 0, 0); 359 tinput_position_caret(ti); 360 } 361 } 362 363 static void tinput_post_seek(tinput_t *ti, bool shift_held) 364 { 365 if (shift_held) { 366 /* Selecting text. Need redraw. */ 367 tinput_display_tail(ti, 0, 0); 368 } else { 369 /* Shift not held. Keep selection empty. */ 370 ti->sel_start = ti->pos; 371 } 283 372 tinput_position_caret(ti); 284 373 } … … 311 400 ti->nc = wstr_length(ti->buffer); 312 401 ti->pos = ti->nc; 402 ti->sel_start = ti->pos; 403 } 404 405 static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb) 406 { 407 if (ti->sel_start < ti->pos) { 408 *sa = ti->sel_start; 409 *sb = ti->pos; 410 } else { 411 *sa = ti->pos; 412 *sb = ti->sel_start; 413 } 414 } 415 416 static bool tinput_sel_active(tinput_t *ti) 417 { 418 return ti->sel_start != ti->pos; 419 } 420 421 static void tinput_sel_delete(tinput_t *ti) 422 { 423 int sa, sb; 424 425 tinput_sel_get_bounds(ti, &sa, &sb); 426 if (sa == sb) 427 return; 428 429 memmove(ti->buffer + sa, ti->buffer + sb, 430 (ti->nc - sb) * sizeof(wchar_t)); 431 ti->pos = ti->sel_start = sa; 432 ti->nc -= (sb - sa); 433 ti->buffer[ti->nc] = '\0'; 434 435 tinput_display_tail(ti, sa, sb - sa); 436 tinput_position_caret(ti); 313 437 } 314 438 … … 356 480 return NULL; 357 481 358 ti->pos = 0;482 ti->pos = ti->sel_start = 0; 359 483 ti->nc = 0; 360 484 ti->buffer[0] = '\0'; 361 362 while (true) { 485 ti->done = false; 486 487 while (!ti->done) { 363 488 fflush(stdout); 364 489 if (!console_get_event(fphone(stdin), &ev)) 365 490 return NULL; 366 491 367 492 if (ev.type != KEY_PRESS) 368 493 continue; … … 370 495 if ((ev.mods & KM_CTRL) != 0 && 371 496 (ev.mods & (KM_ALT | KM_SHIFT)) == 0) { 372 switch (ev.key) { 373 case KC_LEFT: 374 tinput_seek_word(ti, seek_backward); 375 break; 376 case KC_RIGHT: 377 tinput_seek_word(ti, seek_forward); 378 break; 379 case KC_UP: 380 tinput_seek_vertical(ti, seek_backward); 381 break; 382 case KC_DOWN: 383 tinput_seek_vertical(ti, seek_forward); 384 break; 385 } 497 tinput_key_ctrl(ti, &ev); 386 498 } 387 499 500 if ((ev.mods & KM_SHIFT) != 0 && 501 (ev.mods & (KM_CTRL | KM_ALT)) == 0) { 502 tinput_key_shift(ti, &ev); 503 } 504 505 if ((ev.mods & KM_CTRL) != 0 && 506 (ev.mods & KM_SHIFT) != 0 && 507 (ev.mods & KM_ALT) == 0) { 508 tinput_key_ctrl_shift(ti, &ev); 509 } 510 388 511 if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) { 389 switch (ev.key) { 390 case KC_ENTER: 391 case KC_NENTER: 392 goto done; 393 case KC_BACKSPACE: 394 tinput_backspace(ti); 395 break; 396 case KC_DELETE: 397 tinput_delete(ti); 398 break; 399 case KC_LEFT: 400 tinput_seek_cell(ti, seek_backward); 401 break; 402 case KC_RIGHT: 403 tinput_seek_cell(ti, seek_forward); 404 break; 405 case KC_HOME: 406 tinput_seek_max(ti, seek_backward); 407 break; 408 case KC_END: 409 tinput_seek_max(ti, seek_forward); 410 break; 411 case KC_UP: 412 tinput_history_seek(ti, +1); 413 break; 414 case KC_DOWN: 415 tinput_history_seek(ti, -1); 416 break; 417 } 512 tinput_key_unmod(ti, &ev); 418 513 } 419 514 420 515 if (ev.c >= ' ') { 516 tinput_sel_delete(ti); 421 517 tinput_insert_char(ti, ev.c); 422 518 } 423 519 } 424 520 425 done:426 521 ti->pos = ti->nc; 427 522 tinput_position_caret(ti); … … 436 531 return str; 437 532 } 533 534 static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev) 535 { 536 switch (ev->key) { 537 case KC_LEFT: 538 tinput_seek_word(ti, seek_backward, false); 539 break; 540 case KC_RIGHT: 541 tinput_seek_word(ti, seek_forward, false); 542 break; 543 case KC_UP: 544 tinput_seek_vertical(ti, seek_backward, false); 545 break; 546 case KC_DOWN: 547 tinput_seek_vertical(ti, seek_forward, false); 548 break; 549 default: 550 break; 551 } 552 } 553 554 static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev) 555 { 556 switch (ev->key) { 557 case KC_LEFT: 558 tinput_seek_word(ti, seek_backward, true); 559 break; 560 case KC_RIGHT: 561 tinput_seek_word(ti, seek_forward, true); 562 break; 563 case KC_UP: 564 tinput_seek_vertical(ti, seek_backward, true); 565 break; 566 case KC_DOWN: 567 tinput_seek_vertical(ti, seek_forward, true); 568 break; 569 default: 570 break; 571 } 572 } 573 574 static void tinput_key_shift(tinput_t *ti, console_event_t *ev) 575 { 576 switch (ev->key) { 577 case KC_LEFT: 578 tinput_seek_cell(ti, seek_backward, true); 579 break; 580 case KC_RIGHT: 581 tinput_seek_cell(ti, seek_forward, true); 582 break; 583 case KC_UP: 584 tinput_seek_vertical(ti, seek_backward, true); 585 break; 586 case KC_DOWN: 587 tinput_seek_vertical(ti, seek_forward, true); 588 break; 589 case KC_HOME: 590 tinput_seek_max(ti, seek_backward, true); 591 break; 592 case KC_END: 593 tinput_seek_max(ti, seek_forward, true); 594 break; 595 default: 596 break; 597 } 598 } 599 600 static void tinput_key_unmod(tinput_t *ti, console_event_t *ev) 601 { 602 switch (ev->key) { 603 case KC_ENTER: 604 case KC_NENTER: 605 ti->done = true; 606 break; 607 case KC_BACKSPACE: 608 tinput_backspace(ti); 609 break; 610 case KC_DELETE: 611 tinput_delete(ti); 612 break; 613 case KC_LEFT: 614 tinput_seek_cell(ti, seek_backward, false); 615 break; 616 case KC_RIGHT: 617 tinput_seek_cell(ti, seek_forward, false); 618 break; 619 case KC_HOME: 620 tinput_seek_max(ti, seek_backward, false); 621 break; 622 case KC_END: 623 tinput_seek_max(ti, seek_forward, false); 624 break; 625 case KC_UP: 626 tinput_history_seek(ti, +1); 627 break; 628 case KC_DOWN: 629 tinput_history_seek(ti, -1); 630 break; 631 default: 632 break; 633 } 634 } 635 438 636 439 637 void get_input(cliuser_t *usr)
Note:
See TracChangeset
for help on using the changeset viewer.