Changes in uspace/lib/clui/tinput.c [be61b8f:a2bdcf87] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/clui/tinput.c
rbe61b8f ra2bdcf87 40 40 #include <errno.h> 41 41 #include <assert.h> 42 #include < bool.h>42 #include <stdbool.h> 43 43 #include <tinput.h> 44 44 45 45 #define LIN_TO_COL(ti, lpos) ((lpos) % ((ti)->con_cols)) 46 46 #define LIN_TO_ROW(ti, lpos) ((lpos) / ((ti)->con_cols)) 47 #define LIN_POS(ti, col, row) ((col) + (row) * (ti)->con_cols) 47 48 48 49 /** Seek direction */ … … 104 105 static void tinput_display_tail(tinput_t *ti, size_t start, size_t pad) 105 106 { 106 wchar_t dbuf[INPUT_MAX_SIZE + 1]; 107 wchar_t *dbuf = malloc((INPUT_MAX_SIZE + 1) * sizeof(wchar_t)); 108 if (!dbuf) 109 return; 107 110 108 111 size_t sa; … … 146 149 147 150 console_flush(ti->console); 151 152 free(dbuf); 148 153 } 149 154 … … 379 384 } 380 385 386 static void tinput_seek_scrpos(tinput_t *ti, int col, int line, bool shift_held) 387 { 388 unsigned lpos; 389 tinput_pre_seek(ti, shift_held); 390 391 lpos = LIN_POS(ti, col, line); 392 393 if (lpos > ti->text_coord) 394 ti->pos = lpos - ti->text_coord; 395 else 396 ti->pos = 0; 397 if (ti->pos > ti->nc) 398 ti->pos = ti->nc; 399 400 tinput_post_seek(ti, shift_held); 401 } 402 381 403 static void tinput_seek_max(tinput_t *ti, seek_dir_t dir, bool shift_held) 382 404 { … … 595 617 { 596 618 unsigned int i; 597 /* Determine the maximum length of the completion in chars */598 size_t max_ length = 0;619 /* Determine the maximum width of the completion in chars */ 620 size_t max_width = 0; 599 621 for (i = 0; i < cnum; i++) 600 max_length = max(max_length, str_length(compl[i])); 601 602 unsigned int cols = max(1, (ti->con_cols + 1) / (max_length + 1)); 603 unsigned int col_width = ti->con_cols / cols; 622 max_width = max(max_width, str_width(compl[i])); 623 624 unsigned int cols = max(1, (ti->con_cols + 1) / (max_width + 1)); 625 unsigned int padding = 0; 626 if ((cols * max_width) + (cols - 1) < ti->con_cols) { 627 padding = ti->con_cols - (cols * max_width) - (cols - 1); 628 } 629 unsigned int col_width = max_width + padding / cols; 604 630 unsigned int rows = cnum / cols + ((cnum % cols) != 0); 605 631 … … 607 633 608 634 for (row = 0; row < rows; row++) { 609 bool wlc = false;635 unsigned int display_col = 0; 610 636 for (col = 0; col < cols; col++) { 611 637 size_t compl_idx = col * rows + row; 612 638 if (compl_idx >= cnum) 613 639 break; 614 if (col) 640 if (col) { 615 641 printf(" "); 642 display_col++; 643 } 616 644 printf("%s", compl[compl_idx]); 617 size_t compl_len = str_length(compl[compl_idx]); 618 if (col == cols -1) { 619 wlc = (compl_len == max_length); 620 } 621 else { 622 for (i = compl_len; i < col_width; i++) { 645 size_t compl_width = str_width(compl[compl_idx]); 646 display_col += compl_width; 647 if (col < cols - 1) { 648 for (i = compl_width; i < col_width; i++) { 623 649 printf(" "); 650 display_col++; 624 651 } 625 652 } 626 653 } 627 if (!wlc) printf("\n"); 628 } 654 if ((display_col % ti->con_cols) > 0) 655 printf("\n"); 656 } 657 fflush(stdout); 629 658 } 630 659 … … 774 803 { 775 804 ti->compl_ops = compl_ops; 805 } 806 807 /** Handle key press event. */ 808 static void tinput_key_press(tinput_t *ti, kbd_event_t *kev) 809 { 810 if (kev->key == KC_LSHIFT) 811 ti->lshift_held = true; 812 if (kev->key == KC_RSHIFT) 813 ti->rshift_held = true; 814 815 if (((kev->mods & KM_CTRL) != 0) && 816 ((kev->mods & (KM_ALT | KM_SHIFT)) == 0)) 817 tinput_key_ctrl(ti, kev); 818 819 if (((kev->mods & KM_SHIFT) != 0) && 820 ((kev->mods & (KM_CTRL | KM_ALT)) == 0)) 821 tinput_key_shift(ti, kev); 822 823 if (((kev->mods & KM_CTRL) != 0) && 824 ((kev->mods & KM_SHIFT) != 0) && 825 ((kev->mods & KM_ALT) == 0)) 826 tinput_key_ctrl_shift(ti, kev); 827 828 if ((kev->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) 829 tinput_key_unmod(ti, kev); 830 831 if (kev->c >= ' ') { 832 tinput_sel_delete(ti); 833 tinput_insert_char(ti, kev->c); 834 } 835 } 836 837 /** Handle key release event. */ 838 static void tinput_key_release(tinput_t *ti, kbd_event_t *kev) 839 { 840 if (kev->key == KC_LSHIFT) 841 ti->lshift_held = false; 842 if (kev->key == KC_RSHIFT) 843 ti->rshift_held = false; 844 } 845 846 /** Position event */ 847 static void tinput_pos(tinput_t *ti, pos_event_t *ev) 848 { 849 if (ev->type == POS_PRESS) { 850 tinput_seek_scrpos(ti, ev->hpos, ev->vpos, 851 ti->lshift_held || ti->rshift_held); 852 } 776 853 } 777 854 … … 805 882 console_flush(ti->console); 806 883 807 kbd_event_t ev;808 if (!console_get_ kbd_event(ti->console, &ev))884 cons_event_t ev; 885 if (!console_get_event(ti->console, &ev)) 809 886 return EIO; 810 887 811 if (ev.type != KEY_PRESS) 812 continue; 813 814 if (((ev.mods & KM_CTRL) != 0) && 815 ((ev.mods & (KM_ALT | KM_SHIFT)) == 0)) 816 tinput_key_ctrl(ti, &ev); 817 818 if (((ev.mods & KM_SHIFT) != 0) && 819 ((ev.mods & (KM_CTRL | KM_ALT)) == 0)) 820 tinput_key_shift(ti, &ev); 821 822 if (((ev.mods & KM_CTRL) != 0) && 823 ((ev.mods & KM_SHIFT) != 0) && 824 ((ev.mods & KM_ALT) == 0)) 825 tinput_key_ctrl_shift(ti, &ev); 826 827 if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) 828 tinput_key_unmod(ti, &ev); 829 830 if (ev.c >= ' ') { 831 tinput_sel_delete(ti); 832 tinput_insert_char(ti, ev.c); 888 switch (ev.type) { 889 case CEV_KEY: 890 if (ev.ev.key.type == KEY_PRESS) 891 tinput_key_press(ti, &ev.ev.key); 892 else 893 tinput_key_release(ti, &ev.ev.key); 894 break; 895 case CEV_POS: 896 tinput_pos(ti, &ev.ev.pos); 897 break; 833 898 } 834 899 }
Note:
See TracChangeset
for help on using the changeset viewer.