From 798f0fc54926093a074a9cbb011de833a0614c3a Mon Sep 17 00:00:00 2001 From: Frank Wunderlich Date: Fri, 28 Dec 2018 17:56:19 +0100 Subject: [PATCH 1/2] bootmenu: added key-input (1-9,a-f) --- cmd/bootmenu.c | 145 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 51 deletions(-) diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index 3dc2c854ac..fd6ba6c8af 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -40,7 +40,22 @@ struct bootmenu_data { enum bootmenu_key { KEY_NONE = 0, - KEY_UP, + KEY_1, + KEY_2, + KEY_3, + KEY_4, + KEY_5, + KEY_6, + KEY_7, + KEY_8, + KEY_9, + KEY_a, + KEY_b, + KEY_c, + KEY_d, + KEY_e, + KEY_f, + KEY_UP = 20, KEY_DOWN, KEY_SELECT, }; @@ -78,6 +93,23 @@ static void bootmenu_print_entry(void *data) puts(ANSI_COLOR_RESET); } +bool get_bootmenu_key(enum bootmenu_key *key, int c) +{ + /* ANSI '1~9' - was pressed */ + if (c <= '9' && c >= '1' ) + { + *key = c-48; + return true; + }else + /* ANSI 'a~f' - was pressed */ + if (c <= 'f' && c >= 'a' ) + { + *key = c-87; + return true; + } + return false; +} + static void bootmenu_autoboot_loop(struct bootmenu_data *menu, enum bootmenu_key *key, int *esc) { @@ -98,23 +130,23 @@ static void bootmenu_autoboot_loop(struct bootmenu_data *menu, menu->delay = -1; c = getc(); - - switch (c) { - case '\e': - *esc = 1; - *key = KEY_NONE; - break; - case '\r': - *key = KEY_SELECT; - break; - default: - *key = KEY_NONE; - break; + if (!get_bootmenu_key(key,c)) + { + switch (c) { + case '\e': + *esc = 1; + *key = KEY_NONE; + break; + case '\r': + *key = KEY_SELECT; + break; + default: + *key = KEY_NONE; + break; + } } - break; } - if (menu->delay < 0) break; @@ -141,47 +173,49 @@ static void bootmenu_loop(struct bootmenu_data *menu, c = getc(); - switch (*esc) { - case 0: - /* First char of ANSI escape sequence '\e' */ - if (c == '\e') { - *esc = 1; - *key = KEY_NONE; - } - break; - case 1: - /* Second char of ANSI '[' */ - if (c == '[') { - *esc = 2; - *key = KEY_NONE; - } else { - *esc = 0; - } - break; - case 2: - case 3: - /* Third char of ANSI (number '1') - optional */ - if (*esc == 2 && c == '1') { - *esc = 3; + if (!get_bootmenu_key(key,c)) + { + switch (*esc) { + case 0: + /* First char of ANSI escape sequence '\e' */ + if (c == '\e') { + *esc = 1; + *key = KEY_NONE; + } + break; + case 1: + /* Second char of ANSI '[' */ + if (c == '[') { + *esc = 2; *key = KEY_NONE; + } else { + *esc = 0; + } break; - } + case 2: + case 3: + /* Third char of ANSI (number '1') - optional */ + if (*esc == 2 && c == '1') { + *esc = 3; + *key = KEY_NONE; + break; + } - *esc = 0; + *esc = 0; - /* ANSI 'A' - key up was pressed */ - if (c == 'A') - *key = KEY_UP; - /* ANSI 'B' - key down was pressed */ - else if (c == 'B') - *key = KEY_DOWN; - /* other key was pressed */ - else - *key = KEY_NONE; + /* ANSI 'A' - key up was pressed */ + if (c == 'A') + *key = KEY_UP; + /* ANSI 'B' - key down was pressed */ + else if (c == 'B') + *key = KEY_DOWN; + /* other key was pressed */ + else + *key = KEY_NONE; - break; + break; + } } - /* enter key was pressed */ if (c == '\r') *key = KEY_SELECT; @@ -204,6 +238,14 @@ static char *bootmenu_choice_entry(void *data) bootmenu_loop(menu, &key, &esc); } + if (key < KEY_UP && key > KEY_NONE) + { + menu->active = key-1; + return NULL; + } + else + { + switch (key) { case KEY_UP: if (menu->active > 0) @@ -223,6 +265,7 @@ static char *bootmenu_choice_entry(void *data) default: break; } + } } /* never happens */ @@ -467,7 +510,7 @@ void menu_display_statusline(struct menu *m) printf(ANSI_CURSOR_POSITION, menu->count + 5, 1); puts(ANSI_CLEAR_LINE); printf(ANSI_CURSOR_POSITION, menu->count + 6, 1); - puts(" Press UP/DOWN to move, ENTER to select"); + puts(" Press UP/DOWN to move or Press 1~9,a~f to choose, ENTER to select"); puts(ANSI_CLEAR_LINE_TO_END); printf(ANSI_CURSOR_POSITION, menu->count + 7, 1); puts(ANSI_CLEAR_LINE); -- 2.25.1