summaryrefslogtreecommitdiff
path: root/common/menu.c
diff options
context:
space:
mode:
authorWeijie Gao <weijie.gao@mediatek.com>2024-10-29 17:47:16 +0800
committerTom Rini <trini@konsulko.com>2024-11-04 16:41:38 -0600
commitccdd7948e22f21d2add8f51c4918a2c576dc5e91 (patch)
treea7b72bad51f44e7bbd2d58f96f44d63660d9f30e /common/menu.c
parentddac69885efaffc68a71c1a159dc2b3ff4b9fda3 (diff)
menu: add support to check if menu needs to be reprinted
This patch adds a new callback named need_reprint for menu. The need_reprint will be called before printing the menu. If the callback exists and returns FALSE, menu printing will be canceled. This is very useful if the menu was not changed. It can save time for serial-based menu to handle more input data. Signed-off-by: Weijie Gao <weijie.gao@mediatek.com> Reviewed-by: Daniel Golle <daniel@makrotopia.org> Tested-by: Daniel Golle <daniel@makrotopia.org>
Diffstat (limited to 'common/menu.c')
-rw-r--r--common/menu.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/common/menu.c b/common/menu.c
index 48ab7f0f398..5a2126aa01a 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -43,6 +43,7 @@ struct menu {
void (*display_statusline)(struct menu *);
void (*item_data_print)(void *);
char *(*item_choice)(void *);
+ bool (*need_reprint)(void *);
void *item_choice_data;
struct list_head items;
int item_cnt;
@@ -117,6 +118,11 @@ static inline void *menu_item_destroy(struct menu *m,
*/
static inline void menu_display(struct menu *m)
{
+ if (m->need_reprint) {
+ if (!m->need_reprint(m->item_choice_data))
+ return;
+ }
+
if (m->title) {
puts(m->title);
putc('\n');
@@ -362,6 +368,9 @@ int menu_item_add(struct menu *m, char *item_key, void *item_data)
* item. Returns a key string corresponding to the chosen item or NULL if
* no item has been selected.
*
+ * need_reprint - If not NULL, will be called before printing the menu.
+ * Returning FALSE means the menu does not need reprint.
+ *
* item_choice_data - Will be passed as the argument to the item_choice function
*
* Returns a pointer to the menu if successful, or NULL if there is
@@ -371,6 +380,7 @@ struct menu *menu_create(char *title, int timeout, int prompt,
void (*display_statusline)(struct menu *),
void (*item_data_print)(void *),
char *(*item_choice)(void *),
+ bool (*need_reprint)(void *),
void *item_choice_data)
{
struct menu *m;
@@ -386,6 +396,7 @@ struct menu *menu_create(char *title, int timeout, int prompt,
m->display_statusline = display_statusline;
m->item_data_print = item_data_print;
m->item_choice = item_choice;
+ m->need_reprint = need_reprint;
m->item_choice_data = item_choice_data;
m->item_cnt = 0;