expo: Provide a way to iterate through all scene objects

For some operations it is necessary to process all objects in an expo.
Provide an iterator to handle this.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2023-08-14 16:40:23 -06:00 committed by Tom Rini
parent 34ecba1f76
commit f2eb6ad50a
4 changed files with 100 additions and 0 deletions

View file

@ -266,3 +266,18 @@ int expo_apply_theme(struct expo *exp, ofnode node)
return 0;
}
int expo_iter_scene_objs(struct expo *exp, expo_scene_obj_iterator iter,
void *priv)
{
struct scene *scn;
int ret;
list_for_each_entry(scn, &exp->scene_head, sibling) {
ret = scene_iter_objs(scn, iter, priv);
if (ret)
return log_msg_ret("wr", ret);
}
return 0;
}

View file

@ -681,3 +681,19 @@ int scene_set_open(struct scene *scn, uint id, bool open)
return 0;
}
int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter,
void *priv)
{
struct scene_obj *obj;
list_for_each_entry(obj, &scn->obj_head, sibling) {
int ret;
ret = iter(obj, priv);
if (ret)
return log_msg_ret("itr", ret);
}
return 0;
}

View file

@ -9,6 +9,8 @@
#ifndef __SCENE_INTERNAL_H
#define __SCENE_INTERNAL_H
typedef int (*expo_scene_obj_iterator)(struct scene_obj *obj, void *priv);
/**
* expo_lookup_scene_id() - Look up a scene ID
*
@ -198,4 +200,26 @@ int scene_menu_render_deps(struct scene *scn, struct scene_obj_menu *menu);
*/
int scene_menu_calc_dims(struct scene_obj_menu *menu);
/**
* scene_iter_objs() - Iterate through all scene objects
*
* @scn: Scene to process
* @iter: Iterator to call on each object
* @priv: Private data to pass to the iterator, in addition to the object
* Return: 0 if OK, -ve on error
*/
int scene_iter_objs(struct scene *scn, expo_scene_obj_iterator iter,
void *priv);
/**
* expo_iter_scene_objects() - Iterate through all scene objects
*
* @exp: Expo to process
* @iter: Iterator to call on each object
* @priv: Private data to pass to the iterator, in addition to the object
* Return: 0 if OK, -ve on error
*/
int expo_iter_scene_objs(struct expo *exp, expo_scene_obj_iterator iter,
void *priv);
#endif /* __SCENE_INTERNAL_H */

View file

@ -289,6 +289,33 @@ static int expo_object_attr(struct unit_test_state *uts)
}
BOOTSTD_TEST(expo_object_attr, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
/**
* struct test_iter_priv - private data for expo-iterator test
*
* @count: number of scene objects
* @menu_count: number of menus
* @fail_at: item ID at which to return an error
*/
struct test_iter_priv {
int count;
int menu_count;
int fail_at;
};
int h_test_iter(struct scene_obj *obj, void *vpriv)
{
struct test_iter_priv *priv = vpriv;
if (priv->fail_at == obj->id)
return -EINVAL;
priv->count++;
if (obj->type == SCENEOBJT_MENU)
priv->menu_count++;
return 0;
}
/* Check creating a scene with a menu */
static int expo_object_menu(struct unit_test_state *uts)
{
@ -296,6 +323,7 @@ static int expo_object_menu(struct unit_test_state *uts)
struct scene_menitem *item;
int id, label_id, desc_id, key_id, pointer_id, preview_id;
struct scene_obj_txt *ptr, *name1, *desc1, *key1, *tit, *prev1;
struct test_iter_priv priv;
struct scene *scn;
struct expo *exp;
ulong start_mem;
@ -382,6 +410,23 @@ static int expo_object_menu(struct unit_test_state *uts)
ut_asserteq(menu->obj.dim.y + 32, prev1->obj.dim.y);
ut_asserteq(true, prev1->obj.flags & SCENEOF_HIDE);
/* check iterating through scene items */
memset(&priv, '\0', sizeof(priv));
ut_assertok(expo_iter_scene_objs(exp, h_test_iter, &priv));
ut_asserteq(7, priv.count);
ut_asserteq(1, priv.menu_count);
/* check the iterator failing part way through iteration */
memset(&priv, '\0', sizeof(priv));
priv.fail_at = key_id;
ut_asserteq(-EINVAL, expo_iter_scene_objs(exp, h_test_iter, &priv));
/* 2 items (preview_id and the menuitem) are after key_id, 7 - 2 = 5 */
ut_asserteq(5, priv.count);
/* menu is first, so is still processed */
ut_asserteq(1, priv.menu_count);
expo_destroy(exp);
ut_assertok(ut_check_delta(start_mem));