* [PATCH 2/3 v3] menu: add auto select support
2010-09-17 13:45 [PATCH 1/3 v3] menu: shell fix do_menu_select Jean-Christophe PLAGNIOL-VILLARD
@ 2010-09-17 13:45 ` Jean-Christophe PLAGNIOL-VILLARD
2010-09-17 13:45 ` [PATCH 3/3 v3] Menu: add box style entry Jean-Christophe PLAGNIOL-VILLARD
1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2010-09-17 13:45 UTC (permalink / raw)
To: barebox
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>
---
commands/menu.c | 24 +++++++++++++++++++++---
| 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
| 4 ++++
3 files changed, 78 insertions(+), 4 deletions(-)
diff --git a/commands/menu.c b/commands/menu.c
index 7b8e8be..d2ac5cd 100644
--- a/commands/menu.c
+++ b/commands/menu.c
@@ -48,11 +48,12 @@ struct cmd_menu {
char *command;
char *submenu;
int num;
+ int auto_select;
#endif
};
#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)
#else
#define OPTS "m:ls"
@@ -212,7 +213,7 @@ static int do_menu_select(struct cmd_menu *cm)
#endif
/*
- * menu -s -m <menu>
+ * menu -s -m <menu> [-A <auto select delay>] [-d <display]
*/
static int do_menu_show(struct cmd_menu *cm)
{
@@ -223,6 +224,17 @@ static int do_menu_show(struct cmd_menu *cm)
else
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);
}
@@ -300,6 +312,7 @@ static int do_menu(struct command *cmdtp, int argc, char *argv[])
memset(&cm, 0, sizeof(struct cmd_menu));
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
cm.num = -EINVAL;
+ cm.auto_select = -EINVAL;
#endif
cm.action = action_show;
@@ -343,6 +356,9 @@ static int do_menu(struct command *cmdtp, int argc, char *argv[])
case 'n':
cm.num = simple_strtoul(optarg, NULL, 10);
break;
+ case 'A':
+ cm.auto_select = simple_strtoul(optarg, NULL, 10);
+ break;
#endif
default:
return 1;
@@ -398,7 +414,9 @@ static const __maybe_unused char cmd_menu_help[] =
"How to\n"
"\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"
"List menu\n"
" menu -l\n"
--git a/common/menu.c b/common/menu.c
index 7620d9e..d15f85a 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -29,6 +29,7 @@
#include <xfuncs.h>
#include <errno.h>
#include <readkey.h>
+#include <clock.h>
#include <linux/err.h>
static LIST_HEAD(menus);
@@ -49,6 +50,7 @@ void menu_free(struct menu *m)
return;
free(m->name);
free(m->display);
+ free(m->auto_display);
list_for_each_entry_safe(me, tmp, &m->entries, list)
menu_entry_free(me);
@@ -187,6 +189,16 @@ int menu_set_selected(struct menu *m, int num)
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)
{
struct menu_entry *me;
@@ -217,14 +229,54 @@ int menu_show(struct menu *m)
{
int ch;
int escape = 0;
+ int countdown;
+ int auto_display_len = 16;
+ uint64_t start, second;
if(!m || list_empty(&m->entries))
return -EINVAL;
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 {
- ch = getc();
+ if (m->auto_select >= 0)
+ ch = '\n';
+ else
+ ch = getc();
+
+ m->auto_select = -1;
+
switch(ch) {
case 0x1b:
escape = 1;
--git a/include/menu.h b/include/menu.h
index 22bfc23..cc9d0af 100644
--- a/include/menu.h
+++ b/include/menu.h
@@ -42,6 +42,9 @@ struct menu {
char *name;
char *display;
+ int auto_select;
+ char *auto_display;
+
struct list_head list;
struct list_head entries;
@@ -74,6 +77,7 @@ struct menu* menu_get_by_name(char *name);
int menu_show(struct menu *m);
int menu_set_selected_entry(struct menu *m, struct menu_entry* me);
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);
/*
--
1.7.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 3/3 v3] Menu: add box style entry
2010-09-17 13:45 [PATCH 1/3 v3] menu: shell fix do_menu_select Jean-Christophe PLAGNIOL-VILLARD
2010-09-17 13:45 ` [PATCH 2/3 v3] menu: add auto select support Jean-Christophe PLAGNIOL-VILLARD
@ 2010-09-17 13:45 ` Jean-Christophe PLAGNIOL-VILLARD
1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2010-09-17 13:45 UTC (permalink / raw)
To: barebox
this will allow you to create a box style entry
via C API by specifying the type as
struct menu_entry me = {
.display "test",
.type = MENU_ENTRY_BOX,
.box_state = 1,
.action = action_to_run,
}
and via shell
menu -e -a -m <menu> -c <command> [-R] [-b 0|1] -d <description>
menu -e -a -m <menu> -u submenu -d [-b 0|1] <description>
-b with 0 for not selected and 1 for selected
and -c for the command to run when changing state
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
commands/menu.c | 30 +++++++++++++++++++++++-------
| 38 ++++++++++++++++++++++++++++++++------
| 14 +++++++++++++-
3 files changed, 68 insertions(+), 14 deletions(-)
diff --git a/commands/menu.c b/commands/menu.c
index d2ac5cd..b9d6699 100644
--- a/commands/menu.c
+++ b/commands/menu.c
@@ -49,11 +49,13 @@ struct cmd_menu {
char *submenu;
int num;
int auto_select;
+ menu_entry_type type;
+ int box_state;
#endif
};
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
-#define OPTS "m:earlc:d:RsSn:u:A:"
+#define OPTS "m:earlc:d:RsSn:u:A:b:B:"
#define is_entry(x) ((x)->entry)
#else
#define OPTS "m:ls"
@@ -62,8 +64,8 @@ struct cmd_menu {
#if defined(CONFIG_CMD_MENU_MANAGEMENT)
/*
- * menu -e -a -m <menu> -c <command> [-R] -d <description>
- * menu -e -a -m <menu> -u submenu -d <description>
+ * menu -e -a -m <menu> -c <command> [-R] [-b 0|1 ] -d <description>
+ * menu -e -a -m <menu> -u submenu -d [-b 0|1] <description>
*/
static int do_menu_entry_add(struct cmd_menu *cm)
{
@@ -83,11 +85,15 @@ static int do_menu_entry_add(struct cmd_menu *cm)
if (cm->submenu)
me = menu_add_submenu(m, cm->submenu, cm->description);
else
- me = menu_add_command_entry(m, cm->description, cm->command);
+ me = menu_add_command_entry(m, cm->description, cm->command,
+ cm->type);
if (!me)
return PTR_ERR(me);
- me->non_re_ent = !cm->re_entrant;
+ me->box_state = cm->box_state > 0 ? 1 : 0;
+
+ if (!cm->submenu)
+ me->non_re_ent = !cm->re_entrant;
return 0;
}
@@ -358,6 +364,12 @@ static int do_menu(struct command *cmdtp, int argc, char *argv[])
break;
case 'A':
cm.auto_select = simple_strtoul(optarg, NULL, 10);
+ case 'b':
+ cm.type = MENU_ENTRY_BOX;
+ cm.box_state = simple_strtoul(optarg, NULL, 10);
+ break;
+ case 'B':
+ cm.command = optarg;
break;
#endif
default:
@@ -430,11 +442,15 @@ static const __maybe_unused char cmd_menu_help[] =
"\n"
"Add an entry\n"
" (-R for do no exit the menu after executing the command)\n"
-" menu -e -a -m <menu> -c <command> [-R] -d <description>\n"
+" (-b for box style 1 for selected)\n"
+" (and optional -c for the command to run when we change the state)\n"
+" menu -e -a -m <menu> -c <command> [-R] [-b 0|1] -d <description>\n"
"Add a submenu entry\n"
" (-R is not needed)\n"
-" menu -e -a -m <menu> -u <menu> -d <description>\n"
+" (-b for box style 1 for selected)\n"
+" (and -c is not needed)\n"
+" menu -e -a -m <menu> -u submenu -d [-b 0|1] <description>\n"
"\n"
"Remove an entry\n"
" menu -e -r -m <name> -n <num>\n"
--git a/common/menu.c b/common/menu.c
index d15f85a..8e3cb9b 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -149,13 +149,26 @@ void menu_entry_free(struct menu_entry *me)
me->free(me);
}
-static void print_menu_entry(struct menu *m, struct menu_entry *me, int reverse)
+static void print_menu_entry(struct menu *m, struct menu_entry *me,
+ int selected)
{
gotoXY(me->num + 1, 3);
- if (reverse)
- printf_reverse("%d: %-*s", me->num, m->width, me->display);
- else
- printf("%d: %-*s", me->num, m->width, me->display);
+ if (selected)
+ printf("\e[7m");
+
+ if (me->type == MENU_ENTRY_BOX) {
+ if (me->box_state)
+ puts("[*]");
+ else
+ puts("[ ]");
+ } else {
+ puts(" ");
+ }
+
+ printf(" %d: %-*s", me->num, m->width, me->display);
+
+ if (selected)
+ printf("\e[m");
}
int menu_set_selected_entry(struct menu *m, struct menu_entry* me)
@@ -306,6 +319,14 @@ int menu_show(struct menu *m)
}
print_menu_entry(m, m->selected, 1);
break;
+ case ' ':
+ if (m->selected->type != MENU_ENTRY_BOX)
+ break;
+ m->selected->box_state = !m->selected->box_state;
+ if (m->selected->action)
+ m->selected->action(m, m->selected);
+ print_menu_entry(m, m->selected, 1);
+ break;
case '\n':
case '\r':
clear();
@@ -335,6 +356,9 @@ static void menu_action_show(struct menu *m, struct menu_entry *me)
struct submenu *s = container_of(me, struct submenu, entry);
struct menu *sm;
+ if (me->type == MENU_ENTRY_BOX && !me->box_state)
+ return;
+
sm = menu_get_by_name(s->submenu);
if (sm)
menu_show(sm);
@@ -410,7 +434,8 @@ static void menu_command_free(struct menu_entry *me)
free(e);
}
-struct menu_entry *menu_add_command_entry(struct menu *m, char *display, char *command)
+struct menu_entry *menu_add_command_entry(struct menu *m, char *display,
+ char *command, menu_entry_type type)
{
struct action_entry *e = calloc(1, sizeof(*e));
int ret;
@@ -421,6 +446,7 @@ struct menu_entry *menu_add_command_entry(struct menu *m, char *display, char *c
e->command = strdup(command);
e->entry.action = menu_action_command;
e->entry.free = menu_command_free;
+ e->entry.type = type;
e->entry.display = strdup(display);
if (!e->entry.display || !e->command) {
--git a/include/menu.h b/include/menu.h
index cc9d0af..6e7b555 100644
--- a/include/menu.h
+++ b/include/menu.h
@@ -28,6 +28,11 @@
struct menu;
+typedef enum {
+ MENU_ENTRY_NORMAL = 0,
+ MENU_ENTRY_BOX,
+} menu_entry_type;
+
struct menu_entry {
int num;
char *display;
@@ -35,6 +40,12 @@ struct menu_entry {
void (*free)(struct menu_entry *me);
int non_re_ent;
+ /* MENU_ENTRY_BOX */
+ int box_state;
+ void (*box_action)(struct menu *m, struct menu_entry *me);
+
+ menu_entry_type type;
+
struct list_head list;
};
@@ -69,7 +80,8 @@ static inline struct menu* menu_alloc(void)
return m;
}
struct menu_entry *menu_add_submenu(struct menu *parent, char *submenu, char *display);
-struct menu_entry *menu_add_command_entry(struct menu *m, char *display, char *command);
+struct menu_entry *menu_add_command_entry(struct menu *m, char *display,
+ char *command, menu_entry_type type);
void menu_free(struct menu *m);
int menu_add(struct menu* m);
void menu_remove(struct menu *m);
--
1.7.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread