ui/ncurses: move getch() out of process_key callbacks
authorJeremy Kerr <jk@ozlabs.org>
Fri, 17 May 2013 02:02:41 +0000 (10:02 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 21 May 2013 07:29:43 +0000 (15:29 +0800)
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 <jk@ozlabs.org>
ui/ncurses/nc-boot-editor.c
ui/ncurses/nc-cui.c
ui/ncurses/nc-menu.c
ui/ncurses/nc-scr.c
ui/ncurses/nc-scr.h

index 38d29f826d5556b6ad03b6734f94b6751f1dac9c..fb0bee8ef526a9d4c6ab853072388ff13b793cef 100644 (file)
@@ -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;
        }
 }
 
index 167c2bb4eee4e13c996fc462ae2b56628ffe64a0..3f8995eba809c92b46417e2c70be529dbab2fdc5 100644 (file)
@@ -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;
 }
index 87b58e2d28f3ccaefa7a62755ae96991f9fb1768..0612d8a74bb11d88444f16b098268d78468ba9ed 100644 (file)
@@ -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;
        }
 }
 
index 3966e956aac504037663a6a046a6859282ea0f88..062d34a7b177d835b1b81e5befc5b2fa95a37249 100644 (file)
@@ -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 *))
index 0658dd06139844bf2c90887d4b21a25f28d5767c..f18753a4a74c128a3d04ffffb38bcb4c308936d1 100644 (file)
@@ -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 *));