menu: Make use of CLI character processing

Avoid duplicating some of the escape-sequence processing here and use the
CLI function instead.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2023-01-06 08:52:26 -06:00
committed by Tom Rini
parent 86cc3c5215
commit 32bab0eae5
6 changed files with 58 additions and 82 deletions

View File

@@ -140,10 +140,11 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar)
* sequence
*/
if (!ichar) {
if (cch->emit_upto) {
if (cch->emitting) {
if (cch->emit_upto < cch->esc_len)
return cch->esc_save[cch->emit_upto++];
cch->emit_upto = 0;
cch->emitting = false;
}
return 0;
} else if (ichar == -ETIMEDOUT) {
@@ -174,18 +175,21 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar)
case ESC_SAVE:
/* save this character and return nothing */
cch->esc_save[cch->esc_len++] = ichar;
return 0;
ichar = 0;
break;
case ESC_REJECT:
/*
* invalid escape sequence, start returning the
* characters in it
*/
cch->esc_save[cch->esc_len++] = ichar;
return cch->esc_save[cch->emit_upto++];
ichar = cch->esc_save[cch->emit_upto++];
cch->emitting = true;
break;
case ESC_CONVERTED:
/* valid escape sequence, return the resulting char */
cch->esc_len = 0;
return ichar;
break;
}
}

View File

@@ -15,6 +15,8 @@
#include "menu.h"
#define ansi 0
/*
* Internally, each item in a menu is represented by a struct menu_item.
*
@@ -425,15 +427,19 @@ int menu_destroy(struct menu *m)
return 1;
}
enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc)
enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
struct cli_ch_state *cch)
{
enum bootmenu_key key = BKEY_NONE;
int i, c;
while (menu->delay > 0) {
printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
if (ansi)
printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
printf("Hit any key to stop autoboot: %d ", menu->delay);
for (i = 0; i < 100; ++i) {
int ichar;
if (!tstc()) {
schedule();
mdelay(10);
@@ -443,12 +449,13 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc)
menu->delay = -1;
c = getchar();
switch (c) {
case '\e':
*esc = 1;
ichar = cli_ch_process(cch, c);
switch (ichar) {
case '\0':
key = BKEY_NONE;
break;
case '\r':
case '\n':
key = BKEY_SELECT;
break;
case 0x3: /* ^C */
@@ -458,7 +465,6 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc)
key = BKEY_NONE;
break;
}
break;
}
@@ -468,7 +474,8 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc)
--menu->delay;
}
printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
if (ansi)
printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
if (menu->delay == 0)
key = BKEY_SELECT;
@@ -476,79 +483,32 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc)
return key;
}
enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc)
enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
struct cli_ch_state *cch)
{
enum bootmenu_key key = BKEY_NONE;
int c;
if (*esc == 1) {
if (tstc()) {
c = cli_ch_process(cch, 0);
if (!c) {
while (!c && !tstc()) {
schedule();
mdelay(10);
c = cli_ch_process(cch, -ETIMEDOUT);
}
if (!c) {
c = getchar();
} else {
schedule();
mdelay(10);
if (tstc())
c = getchar();
else
c = '\e';
c = cli_ch_process(cch, c);
}
} else {
while (!tstc()) {
schedule();
mdelay(10);
}
c = getchar();
}
switch (*esc) {
case 0:
/* First char of ANSI escape sequence '\e' */
if (c == '\e') {
*esc = 1;
key = BKEY_NONE;
}
break;
case 1:
/* Second char of ANSI '[' */
if (c == '[') {
*esc = 2;
key = BKEY_NONE;
} else {
/* Alone ESC key was pressed */
key = BKEY_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 = BKEY_NONE;
break;
}
*esc = 0;
/* ANSI 'A' - key up was pressed */
if (c == 'A')
key = BKEY_UP;
/* ANSI 'B' - key down was pressed */
else if (c == 'B')
key = BKEY_DOWN;
/* other key was pressed */
else
key = BKEY_NONE;
break;
}
switch (c) {
case '\r':
case '\n':
/* enter key was pressed */
key = BKEY_SELECT;
break;
case CTL_CH('c'):
case '\e':
/* ^C was pressed */
key = BKEY_QUIT;
break;