[PATCH v2 1/2] menu: Add a function to set the default by matching the item data

Simon Glass sjg at chromium.org
Wed Feb 5 18:59:08 CET 2020


Hi Schrempf,

On Wed, 5 Feb 2020 at 08:12, Schrempf Frieder
<frieder.schrempf at kontron.de> wrote:
>
> From: Frieder Schrempf <frieder.schrempf at kontron.de>
>
> In order to make it possible to auto select a default entry by
> matching the data of the menu entries by an external matching
> function, we add some helpers and expose the
> menu_set_default_by_item_data_match() function.
>
> Signed-off-by: Frieder Schrempf <frieder.schrempf at kontron.de>
> ---
> Changes in v2:
> * Keep the menu structs private and instead only expose one additional
>   function, that sets the default by calling an external matching
>   function on each entry.
> * Change the title and commit message to reflect the changes.
> ---
>  common/menu.c  | 40 ++++++++++++++++++++++++++++++++++++++++
>  include/menu.h |  3 +++
>  2 files changed, 43 insertions(+)
>
> diff --git a/common/menu.c b/common/menu.c
> index 7b66d199a9..3833b8a237 100644
> --- a/common/menu.c
> +++ b/common/menu.c
> @@ -160,6 +160,46 @@ static inline struct menu_item *menu_item_by_key(struct menu *m,
>         return menu_items_iter(m, menu_item_key_match, item_key);
>  }
>
> +/*
> + * Find the first matching item, if any exists by calling a matching function
> + * on the items data field.
> + */
> +static inline struct menu_item *menu_item_by_matching_fn(struct menu *m,
> +                       int match(void *, void *), void * extra)
> +{
> +       struct list_head *pos, *n;
> +       struct menu_item *item;
> +       int ret;
> +
> +       list_for_each_safe(pos, n, &m->items) {
> +               item = list_entry(pos, struct menu_item, list);
> +               if (item->key) {
> +                       ret = match(item->data, extra);
> +                       if (ret == 1)
> +                               return item;
> +               }
> +       }
> +
> +       return NULL;
> +}
> +
> +/*
> + * Select the menus default entry based on matching the data field of the menu
> + * items by calling a matching function.
> + */
> +int menu_set_default_by_item_data_match(struct menu *m,
> +                       int match(void *, void *), void *extra, char **key)
> +{
> +       struct menu_item *item = menu_item_by_matching_fn(m, match, extra);
> +
> +       if (!item)
> +               return -ENOENT;
> +
> +       *key = item->key;
> +       m->default_item = item;
> +       return 0;
> +}
> +
>  /*
>   * Set *choice to point to the default item's data, if any default item was
>   * set, and returns 1. If no default item was set, returns -ENOENT.
> diff --git a/include/menu.h b/include/menu.h
> index 2d227c20bd..0a8b41a905 100644
> --- a/include/menu.h
> +++ b/include/menu.h
> @@ -18,6 +18,9 @@ int menu_item_add(struct menu *m, char *item_key, void *item_data);
>  int menu_destroy(struct menu *m);
>  void menu_display_statusline(struct menu *m);
>  int menu_default_choice(struct menu *m, void **choice);

Please add a full function comment here, including the args/return for match.

> +int menu_set_default_by_item_data_match(struct menu *m,
> +                                       int match(void *, void *), void *extra,
> +                                       char **key);
>
>  /**
>   * menu_show() Show a boot menu
> --
> 2.17.1

Regards,
Simon


More information about the U-Boot mailing list