From: Jeremy Kerr Date: Fri, 17 May 2013 02:02:41 +0000 (+0800) Subject: ui/ncurses: move getch() out of process_key callbacks X-Git-Tag: v1.0.0~596 X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=a610837ff38f5cc80bcbad465a80ab920e67927d ui/ncurses: move getch() out of process_key callbacks All process_key callbacks will want to query the key that was pressed, so do the getch() once in cui_process_key, and pass the result to the callback. Signed-off-by: Jeremy Kerr --- diff --git a/ui/ncurses/nc-boot-editor.c b/ui/ncurses/nc-boot-editor.c index 38d29f8..fb0bee8 100644 --- a/ui/ncurses/nc-boot-editor.c +++ b/ui/ncurses/nc-boot-editor.c @@ -196,81 +196,72 @@ static struct pb_boot_data *boot_editor_prepare_data( * Called from the cui via the scr:process_key method. */ -static void boot_editor_process_key(struct nc_scr *scr) +static void boot_editor_process_key(struct nc_scr *scr, int key) { struct boot_editor *boot_editor = boot_editor_from_scr(scr); struct pb_boot_data *bd; - while (1) { - int c = getch(); - - if (c == ERR) - return; - - /* DBGS("%d (%o)\n", c, c); */ - - switch (c) { - default: - boot_editor_move_cursor(boot_editor, c); - break; + switch (key) { + default: + boot_editor_move_cursor(boot_editor, key); + break; - /* hot keys */ - case 27: /* ESC */ - boot_editor->on_exit(boot_editor, - boot_editor_cancel, NULL); - nc_flush_keys(); - return; - case '\n': - case '\r': - form_driver(boot_editor->ncf, REQ_VALIDATION); - bd = boot_editor_prepare_data(boot_editor); - boot_editor->on_exit(boot_editor, - boot_editor_update, bd); - nc_flush_keys(); - return; - - /* insert mode */ - case KEY_IC: - boot_editor_insert_mode_tog(boot_editor); - break; + /* hot keys */ + case 27: /* ESC */ + boot_editor->on_exit(boot_editor, + boot_editor_cancel, NULL); + nc_flush_keys(); + return; + case '\n': + case '\r': + form_driver(boot_editor->ncf, REQ_VALIDATION); + bd = boot_editor_prepare_data(boot_editor); + boot_editor->on_exit(boot_editor, + boot_editor_update, bd); + nc_flush_keys(); + return; + + /* insert mode */ + case KEY_IC: + boot_editor_insert_mode_tog(boot_editor); + break; - /* form nav */ - case KEY_PPAGE: - boot_editor_move_field(boot_editor, REQ_FIRST_FIELD); - break; - case KEY_NPAGE: - boot_editor_move_field(boot_editor, REQ_LAST_FIELD); - break; - case KEY_DOWN: - boot_editor_move_field(boot_editor, REQ_NEXT_FIELD); - break; - case KEY_UP: - boot_editor_move_field(boot_editor, REQ_PREV_FIELD); - break; + /* form nav */ + case KEY_PPAGE: + boot_editor_move_field(boot_editor, REQ_FIRST_FIELD); + break; + case KEY_NPAGE: + boot_editor_move_field(boot_editor, REQ_LAST_FIELD); + break; + case KEY_DOWN: + boot_editor_move_field(boot_editor, REQ_NEXT_FIELD); + break; + case KEY_UP: + boot_editor_move_field(boot_editor, REQ_PREV_FIELD); + break; - /* field nav */ - case KEY_HOME: - boot_editor_move_cursor(boot_editor, REQ_BEG_FIELD); - break; - case KEY_END: - boot_editor_move_cursor(boot_editor, REQ_END_FIELD); - break; - case KEY_LEFT: - boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR); - break; - case KEY_RIGHT: - boot_editor_move_cursor(boot_editor, REQ_RIGHT_CHAR); - break; - case KEY_BACKSPACE: - if (boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR) - == E_OK) - boot_editor_move_cursor(boot_editor, - REQ_DEL_CHAR); - break; - case KEY_DC: - boot_editor_move_cursor(boot_editor, REQ_DEL_CHAR); - break; - } + /* field nav */ + case KEY_HOME: + boot_editor_move_cursor(boot_editor, REQ_BEG_FIELD); + break; + case KEY_END: + boot_editor_move_cursor(boot_editor, REQ_END_FIELD); + break; + case KEY_LEFT: + boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR); + break; + case KEY_RIGHT: + boot_editor_move_cursor(boot_editor, REQ_RIGHT_CHAR); + break; + case KEY_BACKSPACE: + if (boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR) + == E_OK) + boot_editor_move_cursor(boot_editor, + REQ_DEL_CHAR); + break; + case KEY_DC: + boot_editor_move_cursor(boot_editor, REQ_DEL_CHAR); + break; } } diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 167c2bb..3f8995e 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -210,7 +210,14 @@ static int cui_process_key(void *arg) assert(cui->current); ui_timer_disable(&cui->timer); - cui->current->process_key(cui->current); + for (;;) { + int c = getch(); + + if (c == ERR) + break; + + cui->current->process_key(cui->current, c); + } return 0; } diff --git a/ui/ncurses/nc-menu.c b/ui/ncurses/nc-menu.c index 87b58e2..0612d8a 100644 --- a/ui/ncurses/nc-menu.c +++ b/ui/ncurses/nc-menu.c @@ -191,67 +191,58 @@ static void pmenu_move_cursor(struct pmenu *menu, int req) * pmenu_process_key - Process a user keystroke. */ -static void pmenu_process_key(struct nc_scr *scr) +static void pmenu_process_key(struct nc_scr *scr, int key) { struct pmenu *menu = pmenu_from_scr(scr); struct pmenu_item *item = pmenu_find_selected(menu); nc_scr_status_free(&menu->scr); - while (1) { - int c = getch(); - - if (c == ERR) - return; - - DBGS("%d (%o)\n", c, c); - - if (menu->hot_key) - c = menu->hot_key(menu, item, c); - - switch (c) { - case 27: /* ESC */ - if (menu->on_exit) - menu->on_exit(menu); - nc_flush_keys(); - return; - - case KEY_PPAGE: - pmenu_move_cursor(menu, REQ_SCR_UPAGE); - break; - case KEY_NPAGE: - pmenu_move_cursor(menu, REQ_SCR_DPAGE); - break; - case KEY_HOME: - pmenu_move_cursor(menu, REQ_FIRST_ITEM); - break; - case KEY_END: - pmenu_move_cursor(menu, REQ_LAST_ITEM); - break; - case KEY_UP: - pmenu_move_cursor(menu, REQ_UP_ITEM); - break; - case KEY_DOWN: - pmenu_move_cursor(menu, REQ_DOWN_ITEM); - break; - case 'e': - if (item->on_edit) - item->on_edit(item); - break; - case 'o': - DBGS("on_open: %p\n", menu->on_open); - if (menu->on_open) - menu->on_open(menu); - break; - case '\n': - case '\r': - if (item->on_execute) - item->on_execute(item); - break; - default: - menu_driver(menu->ncm, c); - break; - } + if (menu->hot_key) + key = menu->hot_key(menu, item, key); + + switch (key) { + case 27: /* ESC */ + if (menu->on_exit) + menu->on_exit(menu); + nc_flush_keys(); + return; + + case KEY_PPAGE: + pmenu_move_cursor(menu, REQ_SCR_UPAGE); + break; + case KEY_NPAGE: + pmenu_move_cursor(menu, REQ_SCR_DPAGE); + break; + case KEY_HOME: + pmenu_move_cursor(menu, REQ_FIRST_ITEM); + break; + case KEY_END: + pmenu_move_cursor(menu, REQ_LAST_ITEM); + break; + case KEY_UP: + pmenu_move_cursor(menu, REQ_UP_ITEM); + break; + case KEY_DOWN: + pmenu_move_cursor(menu, REQ_DOWN_ITEM); + break; + case 'e': + if (item->on_edit) + item->on_edit(item); + break; + case 'o': + DBGS("on_open: %p\n", menu->on_open); + if (menu->on_open) + menu->on_open(menu); + break; + case '\n': + case '\r': + if (item->on_execute) + item->on_execute(item); + break; + default: + menu_driver(menu->ncm, key); + break; } } diff --git a/ui/ncurses/nc-scr.c b/ui/ncurses/nc-scr.c index 3966e95..062d34a 100644 --- a/ui/ncurses/nc-scr.c +++ b/ui/ncurses/nc-scr.c @@ -102,7 +102,7 @@ void nc_scr_status_printf(struct nc_scr *scr, const char *format, ...) int nc_scr_init(struct nc_scr *scr, enum pb_nc_sig sig, int begin_x, void *ui_ctx, - void (*process_key)(struct nc_scr *), + void (*process_key)(struct nc_scr *, int), int (*post)(struct nc_scr *), int (*unpost)(struct nc_scr *), void (*resize)(struct nc_scr *)) diff --git a/ui/ncurses/nc-scr.h b/ui/ncurses/nc-scr.h index 0658dd0..f18753a 100644 --- a/ui/ncurses/nc-scr.h +++ b/ui/ncurses/nc-scr.h @@ -75,13 +75,13 @@ struct nc_scr { void *ui_ctx; int (*post)(struct nc_scr *scr); int (*unpost)(struct nc_scr *scr); - void (*process_key)(struct nc_scr *scr); + void (*process_key)(struct nc_scr *scr, int key); void (*resize)(struct nc_scr *scr); }; int nc_scr_init(struct nc_scr *scr, enum pb_nc_sig sig, int begin_x, void *ui_ctx, - void (*process_key)(struct nc_scr *), + void (*process_key)(struct nc_scr *, int), int (*post)(struct nc_scr *), int (*unpost)(struct nc_scr *), void (*resize)(struct nc_scr *));