menu: add auto select support
this will allow to automaticaly run an entry if the user do no choice this is usefull for boot menu as example with menu -s -m boot -A 3 -d "Auto Boot in" Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
3212e8462e
commit
7923392c56
|
@ -48,11 +48,12 @@ struct cmd_menu {
|
||||||
char *command;
|
char *command;
|
||||||
char *submenu;
|
char *submenu;
|
||||||
int num;
|
int num;
|
||||||
|
int auto_select;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
|
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
|
||||||
#define OPTS "m:earlc:d:RsSn:u:"
|
#define OPTS "m:earlc:d:RsSn:u:A:"
|
||||||
#define is_entry(x) ((x)->entry)
|
#define is_entry(x) ((x)->entry)
|
||||||
#else
|
#else
|
||||||
#define OPTS "m:ls"
|
#define OPTS "m:ls"
|
||||||
|
@ -212,7 +213,7 @@ static int do_menu_select(struct cmd_menu *cm)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* menu -s -m <menu>
|
* menu -s -m <menu> [-A <auto select delay>] [-d <display]
|
||||||
*/
|
*/
|
||||||
static int do_menu_show(struct cmd_menu *cm)
|
static int do_menu_show(struct cmd_menu *cm)
|
||||||
{
|
{
|
||||||
|
@ -223,6 +224,17 @@ static int do_menu_show(struct cmd_menu *cm)
|
||||||
else
|
else
|
||||||
m = menu_get_by_name("boot");
|
m = menu_get_by_name("boot");
|
||||||
|
|
||||||
|
if (!m)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (cm->auto_select != -EINVAL) {
|
||||||
|
menu_set_auto_select(m, cm->auto_select);
|
||||||
|
|
||||||
|
free(m->auto_display);
|
||||||
|
|
||||||
|
m->auto_display = strdup(cm->description);
|
||||||
|
}
|
||||||
|
|
||||||
return menu_show(m);
|
return menu_show(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,6 +312,7 @@ static int do_menu(struct command *cmdtp, int argc, char *argv[])
|
||||||
memset(&cm, 0, sizeof(struct cmd_menu));
|
memset(&cm, 0, sizeof(struct cmd_menu));
|
||||||
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
|
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
|
||||||
cm.num = -EINVAL;
|
cm.num = -EINVAL;
|
||||||
|
cm.auto_select = -EINVAL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
cm.action = action_show;
|
cm.action = action_show;
|
||||||
|
@ -343,6 +356,9 @@ static int do_menu(struct command *cmdtp, int argc, char *argv[])
|
||||||
case 'n':
|
case 'n':
|
||||||
cm.num = simple_strtoul(optarg, NULL, 10);
|
cm.num = simple_strtoul(optarg, NULL, 10);
|
||||||
break;
|
break;
|
||||||
|
case 'A':
|
||||||
|
cm.auto_select = simple_strtoul(optarg, NULL, 10);
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -398,7 +414,9 @@ static const __maybe_unused char cmd_menu_help[] =
|
||||||
"How to\n"
|
"How to\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Show menu\n"
|
"Show menu\n"
|
||||||
" menu -s -m <menu>\n"
|
" (-A auto select delay)\n"
|
||||||
|
" (-d auto select description)\n"
|
||||||
|
" menu -s -m <menu> [-A delay] [-d auto_display]\n"
|
||||||
"\n"
|
"\n"
|
||||||
"List menu\n"
|
"List menu\n"
|
||||||
" menu -l\n"
|
" menu -l\n"
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <xfuncs.h>
|
#include <xfuncs.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <readkey.h>
|
#include <readkey.h>
|
||||||
|
#include <clock.h>
|
||||||
#include <linux/err.h>
|
#include <linux/err.h>
|
||||||
|
|
||||||
static LIST_HEAD(menus);
|
static LIST_HEAD(menus);
|
||||||
|
@ -49,6 +50,7 @@ void menu_free(struct menu *m)
|
||||||
return;
|
return;
|
||||||
free(m->name);
|
free(m->name);
|
||||||
free(m->display);
|
free(m->display);
|
||||||
|
free(m->auto_display);
|
||||||
|
|
||||||
list_for_each_entry_safe(me, tmp, &m->entries, list)
|
list_for_each_entry_safe(me, tmp, &m->entries, list)
|
||||||
menu_entry_free(me);
|
menu_entry_free(me);
|
||||||
|
@ -187,6 +189,16 @@ int menu_set_selected(struct menu *m, int num)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int menu_set_auto_select(struct menu *m, int delay)
|
||||||
|
{
|
||||||
|
if (!m)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
m->auto_select = delay;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void print_menu(struct menu *m)
|
static void print_menu(struct menu *m)
|
||||||
{
|
{
|
||||||
struct menu_entry *me;
|
struct menu_entry *me;
|
||||||
|
@ -217,14 +229,54 @@ int menu_show(struct menu *m)
|
||||||
{
|
{
|
||||||
int ch;
|
int ch;
|
||||||
int escape = 0;
|
int escape = 0;
|
||||||
|
int countdown;
|
||||||
|
int auto_display_len = 16;
|
||||||
|
uint64_t start, second;
|
||||||
|
|
||||||
if(!m || list_empty(&m->entries))
|
if(!m || list_empty(&m->entries))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
print_menu(m);
|
print_menu(m);
|
||||||
|
|
||||||
|
countdown = m->auto_select;
|
||||||
|
if (m->auto_select >= 0) {
|
||||||
|
gotoXY(m->nb_entries + 2, 3);
|
||||||
|
if (!m->auto_display) {
|
||||||
|
printf("Auto Select in");
|
||||||
|
} else {
|
||||||
|
auto_display_len = strlen(m->auto_display);
|
||||||
|
printf(m->auto_display);
|
||||||
|
}
|
||||||
|
printf(" %2d", countdown--);
|
||||||
|
}
|
||||||
|
|
||||||
|
start = get_time_ns();
|
||||||
|
second = start;
|
||||||
|
while (m->auto_select > 0 && !is_timeout(start, m->auto_select * SECOND)) {
|
||||||
|
if (tstc()) {
|
||||||
|
m->auto_select = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_timeout(second, SECOND)) {
|
||||||
|
printf("\b\b%2d", countdown--);
|
||||||
|
second += SECOND;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gotoXY(m->nb_entries + 2, 3);
|
||||||
|
printf("%*c", auto_display_len + 4, ' ');
|
||||||
|
|
||||||
|
gotoXY(m->selected->num + 1, 3);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ch = getc();
|
if (m->auto_select >= 0)
|
||||||
|
ch = '\n';
|
||||||
|
else
|
||||||
|
ch = getc();
|
||||||
|
|
||||||
|
m->auto_select = -1;
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case 0x1b:
|
case 0x1b:
|
||||||
escape = 1;
|
escape = 1;
|
||||||
|
|
|
@ -42,6 +42,9 @@ struct menu {
|
||||||
char *name;
|
char *name;
|
||||||
char *display;
|
char *display;
|
||||||
|
|
||||||
|
int auto_select;
|
||||||
|
char *auto_display;
|
||||||
|
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
struct list_head entries;
|
struct list_head entries;
|
||||||
|
|
||||||
|
@ -74,6 +77,7 @@ struct menu* menu_get_by_name(char *name);
|
||||||
int menu_show(struct menu *m);
|
int menu_show(struct menu *m);
|
||||||
int menu_set_selected_entry(struct menu *m, struct menu_entry* me);
|
int menu_set_selected_entry(struct menu *m, struct menu_entry* me);
|
||||||
int menu_set_selected(struct menu *m, int num);
|
int menu_set_selected(struct menu *m, int num);
|
||||||
|
int menu_set_auto_select(struct menu *m, int delay);
|
||||||
struct menu* menu_get_menus(void);
|
struct menu* menu_get_menus(void);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue