From: Masahisa Kojima <masahisa.kojima@linaro.org>
To: u-boot@lists.denx.de
Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Simon Glass <sjg@chromium.org>,
Takahiro Akashi <takahiro.akashi@linaro.org>,
Francois Ozog <francois.ozog@linaro.org>,
Mark Kettenis <mark.kettenis@xs4all.nl>,
Masahisa Kojima <masahisa.kojima@linaro.org>
Subject: [PATCH v4 08/11] bootmenu: factor out the user input handling
Date: Thu, 24 Mar 2022 22:54:40 +0900 [thread overview]
Message-ID: <20220324135443.1571-9-masahisa.kojima@linaro.org> (raw)
In-Reply-To: <20220324135443.1571-1-masahisa.kojima@linaro.org>
This commit moves the user input handling from cmd/bootmenu.c
to common/menu.c to reuse it from other modules.
Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Newly created in v4.
| 149 -------------------------------------------------
| 137 +++++++++++++++++++++++++++++++++++++++++++++
| 20 +++++++
3 files changed, 157 insertions(+), 149 deletions(-)
--git a/cmd/bootmenu.c b/cmd/bootmenu.c
index 947b3a49ff..d3591aa4a9 100644
--- a/cmd/bootmenu.c
+++ b/cmd/bootmenu.c
@@ -52,21 +52,6 @@ struct bootmenu_entry {
struct bootmenu_entry *next; /* next menu entry (num+1) */
};
-struct bootmenu_data {
- int delay; /* delay for autoboot */
- int active; /* active menu entry */
- int count; /* total count of menu entries */
- struct bootmenu_entry *first; /* first menu entry */
-};
-
-enum bootmenu_key {
- KEY_NONE = 0,
- KEY_UP,
- KEY_DOWN,
- KEY_SELECT,
- KEY_QUIT,
-};
-
static char *bootmenu_getoption(unsigned short int n)
{
char name[MAX_ENV_SIZE];
@@ -107,140 +92,6 @@ static void bootmenu_print_entry(void *data)
puts(ANSI_COLOR_RESET);
}
-static void bootmenu_autoboot_loop(struct bootmenu_data *menu,
- enum bootmenu_key *key, int *esc)
-{
- int i, c;
-
- if (menu->delay > 0) {
- /* flush input */
- while (tstc())
- getchar();
-
- printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
- printf(" Hit any key to stop autoboot: %2d ", menu->delay);
- }
-
- while (menu->delay > 0) {
- for (i = 0; i < 100; ++i) {
- if (!tstc()) {
- WATCHDOG_RESET();
- mdelay(10);
- continue;
- }
-
- menu->delay = -1;
- c = getchar();
-
- switch (c) {
- case '\e':
- *esc = 1;
- *key = KEY_NONE;
- break;
- case '\r':
- *key = KEY_SELECT;
- break;
- case 0x3: /* ^C */
- *key = KEY_QUIT;
- break;
- default:
- *key = KEY_NONE;
- break;
- }
-
- break;
- }
-
- if (menu->delay < 0)
- break;
-
- --menu->delay;
- printf("\b\b\b%2d ", menu->delay);
- }
-
- printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
- puts(ANSI_CLEAR_LINE);
-
- if (menu->delay == 0)
- *key = KEY_SELECT;
-}
-
-static void bootmenu_loop(struct bootmenu_data *menu,
- enum bootmenu_key *key, int *esc)
-{
- int c;
-
- if (*esc == 1) {
- if (tstc()) {
- c = getchar();
- } else {
- WATCHDOG_RESET();
- mdelay(10);
- if (tstc())
- c = getchar();
- else
- c = '\e';
- }
- } else {
- while (!tstc()) {
- WATCHDOG_RESET();
- mdelay(10);
- }
- c = getchar();
- }
-
- 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 {
- /* Alone ESC key was pressed */
- *key = KEY_QUIT;
- *esc = (c == '\e') ? 1 : 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;
-
- /* 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;
- }
-
- /* enter key was pressed */
- if (c == '\r')
- *key = KEY_SELECT;
-
- /* ^C was pressed */
- if (c == 0x3)
- *key = KEY_QUIT;
-}
-
static char *bootmenu_choice_entry(void *data)
{
struct bootmenu_data *menu = data;
--git a/common/menu.c b/common/menu.c
index b577d80b4f..6dcac035b7 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -4,11 +4,14 @@
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*/
+#include <ansi.h>
#include <common.h>
#include <cli.h>
#include <malloc.h>
#include <errno.h>
+#include <linux/delay.h>
#include <linux/list.h>
+#include <watchdog.h>
#include "menu.h"
@@ -418,3 +421,137 @@ int menu_destroy(struct menu *m)
return 1;
}
+
+void bootmenu_autoboot_loop(struct bootmenu_data *menu,
+ enum bootmenu_key *key, int *esc)
+{
+ int i, c;
+
+ if (menu->delay > 0) {
+ /* flush input */
+ while (tstc())
+ getchar();
+
+ printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
+ printf(" Hit any key to stop autoboot: %2d ", menu->delay);
+ }
+
+ while (menu->delay > 0) {
+ for (i = 0; i < 100; ++i) {
+ if (!tstc()) {
+ WATCHDOG_RESET();
+ mdelay(10);
+ continue;
+ }
+
+ menu->delay = -1;
+ c = getchar();
+
+ switch (c) {
+ case '\e':
+ *esc = 1;
+ *key = KEY_NONE;
+ break;
+ case '\r':
+ *key = KEY_SELECT;
+ break;
+ case 0x3: /* ^C */
+ *key = KEY_QUIT;
+ break;
+ default:
+ *key = KEY_NONE;
+ break;
+ }
+
+ break;
+ }
+
+ if (menu->delay < 0)
+ break;
+
+ --menu->delay;
+ printf("\b\b\b%2d ", menu->delay);
+ }
+
+ printf(ANSI_CURSOR_POSITION, menu->count + 5, 1);
+ puts(ANSI_CLEAR_LINE);
+
+ if (menu->delay == 0)
+ *key = KEY_SELECT;
+}
+
+void bootmenu_loop(struct bootmenu_data *menu,
+ enum bootmenu_key *key, int *esc)
+{
+ int c;
+
+ if (*esc == 1) {
+ if (tstc()) {
+ c = getchar();
+ } else {
+ WATCHDOG_RESET();
+ mdelay(10);
+ if (tstc())
+ c = getchar();
+ else
+ c = '\e';
+ }
+ } else {
+ while (!tstc()) {
+ WATCHDOG_RESET();
+ mdelay(10);
+ }
+ c = getchar();
+ }
+
+ 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 {
+ /* Alone ESC key was pressed */
+ *key = KEY_QUIT;
+ *esc = (c == '\e') ? 1 : 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;
+
+ /* 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;
+ }
+
+ /* enter key was pressed */
+ if (c == '\r')
+ *key = KEY_SELECT;
+
+ /* ^C was pressed */
+ if (c == 0x3)
+ *key = KEY_QUIT;
+}
--git a/include/menu.h b/include/menu.h
index ad5859437e..e74616cae8 100644
--- a/include/menu.h
+++ b/include/menu.h
@@ -35,4 +35,24 @@ int menu_default_choice(struct menu *m, void **choice);
*/
int menu_show(int bootdelay);
+struct bootmenu_data {
+ int delay; /* delay for autoboot */
+ int active; /* active menu entry */
+ int count; /* total count of menu entries */
+ struct bootmenu_entry *first; /* first menu entry */
+};
+
+enum bootmenu_key {
+ KEY_NONE = 0,
+ KEY_UP,
+ KEY_DOWN,
+ KEY_SELECT,
+ KEY_QUIT,
+};
+
+void bootmenu_autoboot_loop(struct bootmenu_data *menu,
+ enum bootmenu_key *key, int *esc);
+void bootmenu_loop(struct bootmenu_data *menu,
+ enum bootmenu_key *key, int *esc);
+
#endif /* __MENU_H__ */
--
2.17.1
next prev parent reply other threads:[~2022-03-24 13:56 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-24 13:54 [PATCH v4 00/11] enable menu-driven boot device selection Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 01/11] bootmenu: fix menu API error handling Masahisa Kojima
2022-03-30 8:55 ` Ilias Apalodimas
2022-03-24 13:54 ` [PATCH v4 02/11] lib/charset: add u16_strlcat() function Masahisa Kojima
2022-04-02 7:14 ` Heinrich Schuchardt
2022-04-04 14:50 ` Masahisa Kojima
2022-04-16 7:32 ` Heinrich Schuchardt
2022-04-18 7:47 ` Masahisa Kojima
2022-04-28 7:45 ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 03/11] test: unit test for u16_strlcat() Masahisa Kojima
2022-04-02 7:47 ` Heinrich Schuchardt
2022-04-04 14:54 ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 04/11] menu: always show the menu regardless of the number of entry Masahisa Kojima
2022-04-02 7:56 ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 05/11] efi_loader: export efi_locate_device_handle() Masahisa Kojima
2022-04-01 5:43 ` Ilias Apalodimas
2022-03-24 13:54 ` [PATCH v4 06/11] efi_loader: bootmgr: add booting from removable media Masahisa Kojima
2022-03-30 19:13 ` Ilias Apalodimas
2022-03-31 0:51 ` Masahisa Kojima
2022-03-31 6:25 ` Ilias Apalodimas
2022-04-02 6:12 ` Heinrich Schuchardt
2022-04-04 6:48 ` Masahisa Kojima
2022-04-04 21:54 ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 07/11] bootmenu: add UEFI and disto_boot entries Masahisa Kojima
2022-04-01 6:08 ` Ilias Apalodimas
2022-04-02 6:33 ` Heinrich Schuchardt
2022-04-04 8:10 ` Masahisa Kojima
2022-03-24 13:54 ` Masahisa Kojima [this message]
2022-03-24 13:54 ` [PATCH v4 09/11] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
2022-03-31 8:31 ` Ilias Apalodimas
2022-04-14 9:25 ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 10/11] bootmenu: add removable media entries Masahisa Kojima
2022-03-31 8:48 ` Ilias Apalodimas
2022-03-31 10:18 ` Masahisa Kojima
2022-04-04 22:00 ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 11/11] doc:bootmenu: add UEFI boot variable and distro boot support Masahisa Kojima
2022-04-02 5:51 ` Heinrich Schuchardt
2022-03-25 1:20 ` [PATCH v4 00/11] enable menu-driven boot device selection Takahiro Akashi
2022-03-25 6:57 ` Masahisa Kojima
2022-04-02 5:48 ` Heinrich Schuchardt
2022-04-04 6:10 ` Masahisa Kojima
2022-04-16 6:46 ` Heinrich Schuchardt
2022-04-28 7:35 ` Masahisa Kojima
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220324135443.1571-9-masahisa.kojima@linaro.org \
--to=masahisa.kojima@linaro.org \
--cc=francois.ozog@linaro.org \
--cc=ilias.apalodimas@linaro.org \
--cc=mark.kettenis@xs4all.nl \
--cc=sjg@chromium.org \
--cc=takahiro.akashi@linaro.org \
--cc=u-boot@lists.denx.de \
--cc=xypron.glpk@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).