expo: Create a struct for generic text attributes
In preparation for adding more text types, refactor the common fields into a new structure. This will allow common code to be used. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
@@ -271,7 +271,7 @@ static int get_cur_menuitem_text(const struct scene_obj_menu *menu,
|
||||
if (!txt)
|
||||
return log_msg_ret("txt", -ENOENT);
|
||||
|
||||
str = expo_get_str(scn->expo, txt->str_id);
|
||||
str = expo_get_str(scn->expo, txt->gen.str_id);
|
||||
if (!str)
|
||||
return log_msg_ret("str", -ENOENT);
|
||||
*strp = str;
|
||||
|
||||
65
boot/scene.c
65
boot/scene.c
@@ -142,6 +142,31 @@ int scene_img(struct scene *scn, const char *name, uint id, char *data,
|
||||
return img->obj.id;
|
||||
}
|
||||
|
||||
int scene_txt_generic_init(struct expo *exp, struct scene_txt_generic *gen,
|
||||
const char *name, uint str_id, const char *str)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (str) {
|
||||
ret = expo_str(exp, name, str_id, str);
|
||||
if (ret < 0)
|
||||
return log_msg_ret("str", ret);
|
||||
if (str_id && ret != str_id)
|
||||
return log_msg_ret("id", -EEXIST);
|
||||
str_id = ret;
|
||||
} else {
|
||||
ret = resolve_id(exp, str_id);
|
||||
if (ret < 0)
|
||||
return log_msg_ret("nst", ret);
|
||||
if (str_id && ret != str_id)
|
||||
return log_msg_ret("nid", -EEXIST);
|
||||
}
|
||||
|
||||
gen->str_id = str_id;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
|
||||
struct scene_obj_txt **txtp)
|
||||
{
|
||||
@@ -154,8 +179,9 @@ int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
|
||||
if (ret < 0)
|
||||
return log_msg_ret("obj", ret);
|
||||
|
||||
txt->str_id = str_id;
|
||||
|
||||
ret = scene_txt_generic_init(scn->expo, &txt->gen, name, str_id, NULL);
|
||||
if (ret)
|
||||
return log_msg_ret("stg", ret);
|
||||
if (txtp)
|
||||
*txtp = txt;
|
||||
|
||||
@@ -168,21 +194,15 @@ int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id,
|
||||
struct scene_obj_txt *txt;
|
||||
int ret;
|
||||
|
||||
ret = expo_str(scn->expo, name, str_id, str);
|
||||
if (ret < 0)
|
||||
return log_msg_ret("str", ret);
|
||||
if (str_id && ret != str_id)
|
||||
return log_msg_ret("id", -EEXIST);
|
||||
str_id = ret;
|
||||
|
||||
ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXT,
|
||||
sizeof(struct scene_obj_txt),
|
||||
(struct scene_obj **)&txt);
|
||||
if (ret < 0)
|
||||
return log_msg_ret("obj", ret);
|
||||
|
||||
txt->str_id = str_id;
|
||||
|
||||
ret = scene_txt_generic_init(scn->expo, &txt->gen, name, str_id, str);
|
||||
if (ret)
|
||||
return log_msg_ret("tsg", ret);
|
||||
if (txtp)
|
||||
*txtp = txt;
|
||||
|
||||
@@ -197,8 +217,8 @@ int scene_txt_set_font(struct scene *scn, uint id, const char *font_name,
|
||||
txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
|
||||
if (!txt)
|
||||
return log_msg_ret("find", -ENOENT);
|
||||
txt->font_name = font_name;
|
||||
txt->font_size = font_size;
|
||||
txt->gen.font_name = font_name;
|
||||
txt->gen.font_size = font_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -313,13 +333,13 @@ int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
|
||||
return height;
|
||||
}
|
||||
case SCENEOBJT_TEXT: {
|
||||
struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
|
||||
struct scene_txt_generic *gen = &((struct scene_obj_txt *)obj)->gen;
|
||||
struct expo *exp = scn->expo;
|
||||
struct vidconsole_bbox bbox;
|
||||
const char *str;
|
||||
int len, ret;
|
||||
|
||||
str = expo_get_str(exp, txt->str_id);
|
||||
str = expo_get_str(exp, gen->str_id);
|
||||
if (!str)
|
||||
return log_msg_ret("str", -ENOENT);
|
||||
len = strlen(str);
|
||||
@@ -331,8 +351,8 @@ int scene_obj_get_hw(struct scene *scn, uint id, int *widthp)
|
||||
return 16;
|
||||
}
|
||||
|
||||
ret = vidconsole_measure(scn->expo->cons, txt->font_name,
|
||||
txt->font_size, str, -1, &bbox, NULL);
|
||||
ret = vidconsole_measure(scn->expo->cons, gen->font_name,
|
||||
gen->font_size, str, -1, &bbox, NULL);
|
||||
if (ret)
|
||||
return log_msg_ret("mea", ret);
|
||||
if (widthp)
|
||||
@@ -424,22 +444,23 @@ static int scene_obj_render(struct scene_obj *obj, bool text_mode)
|
||||
break;
|
||||
}
|
||||
case SCENEOBJT_TEXT: {
|
||||
struct scene_obj_txt *txt = (struct scene_obj_txt *)obj;
|
||||
struct scene_txt_generic *gen =
|
||||
&((struct scene_obj_txt *)obj)->gen;
|
||||
const char *str;
|
||||
|
||||
if (!cons)
|
||||
return -ENOTSUPP;
|
||||
|
||||
if (txt->font_name || txt->font_size) {
|
||||
if (gen->font_name || gen->font_size) {
|
||||
ret = vidconsole_select_font(cons,
|
||||
txt->font_name,
|
||||
txt->font_size);
|
||||
gen->font_name,
|
||||
gen->font_size);
|
||||
} else {
|
||||
ret = vidconsole_select_font(cons, NULL, 0);
|
||||
}
|
||||
if (ret && ret != -ENOSYS)
|
||||
return log_msg_ret("font", ret);
|
||||
str = expo_get_str(exp, txt->str_id);
|
||||
str = expo_get_str(exp, gen->str_id);
|
||||
if (str) {
|
||||
struct video_priv *vid_priv;
|
||||
struct vidconsole_colour old;
|
||||
|
||||
@@ -359,7 +359,7 @@ static struct scene_menitem *scene_menu_find_key(struct scene *scn,
|
||||
|
||||
txt = scene_obj_find(scn, item->key_id, SCENEOBJT_TEXT);
|
||||
if (txt) {
|
||||
str = expo_get_str(scn->expo, txt->str_id);
|
||||
str = expo_get_str(scn->expo, txt->gen.str_id);
|
||||
if (str && *str == key)
|
||||
return item;
|
||||
}
|
||||
@@ -562,7 +562,7 @@ int scene_menu_display(struct scene_obj_menu *menu)
|
||||
if (!txt)
|
||||
return log_msg_ret("txt", -EINVAL);
|
||||
|
||||
str = expo_get_str(exp, txt->str_id);
|
||||
str = expo_get_str(exp, txt->gen.str_id);
|
||||
printf("%s\n\n", str);
|
||||
}
|
||||
|
||||
@@ -570,7 +570,7 @@ int scene_menu_display(struct scene_obj_menu *menu)
|
||||
return 0;
|
||||
|
||||
pointer = scene_obj_find(scn, menu->pointer_id, SCENEOBJT_TEXT);
|
||||
pstr = expo_get_str(scn->expo, pointer->str_id);
|
||||
pstr = expo_get_str(scn->expo, pointer->gen.str_id);
|
||||
|
||||
list_for_each_entry(item, &menu->item_head, sibling) {
|
||||
struct scene_obj_txt *key = NULL, *label = NULL;
|
||||
@@ -579,15 +579,15 @@ int scene_menu_display(struct scene_obj_menu *menu)
|
||||
|
||||
key = scene_obj_find(scn, item->key_id, SCENEOBJT_TEXT);
|
||||
if (key)
|
||||
kstr = expo_get_str(exp, key->str_id);
|
||||
kstr = expo_get_str(exp, key->gen.str_id);
|
||||
|
||||
label = scene_obj_find(scn, item->label_id, SCENEOBJT_TEXT);
|
||||
if (label)
|
||||
lstr = expo_get_str(exp, label->str_id);
|
||||
lstr = expo_get_str(exp, label->gen.str_id);
|
||||
|
||||
desc = scene_obj_find(scn, item->desc_id, SCENEOBJT_TEXT);
|
||||
if (desc)
|
||||
dstr = expo_get_str(exp, desc->str_id);
|
||||
dstr = expo_get_str(exp, desc->gen.str_id);
|
||||
|
||||
printf("%3s %3s %-10s %s\n",
|
||||
pointer && menu->cur_item_id == item->id ? pstr : "",
|
||||
|
||||
@@ -71,8 +71,8 @@ int scene_textline_calc_dims(struct scene_obj_textline *tline)
|
||||
if (!txt)
|
||||
return log_msg_ret("dim", -ENOENT);
|
||||
|
||||
ret = vidconsole_nominal(scn->expo->cons, txt->font_name,
|
||||
txt->font_size, tline->max_chars, &bbox);
|
||||
ret = vidconsole_nominal(scn->expo->cons, txt->gen.font_name,
|
||||
txt->gen.font_size, tline->max_chars, &bbox);
|
||||
if (ret)
|
||||
return log_msg_ret("nom", ret);
|
||||
|
||||
@@ -191,10 +191,10 @@ int scene_textline_render_deps(struct scene *scn,
|
||||
if (!txt)
|
||||
return log_msg_ret("cur", -ENOENT);
|
||||
|
||||
if (txt->font_name || txt->font_size) {
|
||||
if (txt->gen.font_name || txt->gen.font_size) {
|
||||
ret = vidconsole_select_font(cons,
|
||||
txt->font_name,
|
||||
txt->font_size);
|
||||
txt->gen.font_name,
|
||||
txt->gen.font_size);
|
||||
} else {
|
||||
ret = vidconsole_select_font(cons, NULL, 0);
|
||||
}
|
||||
|
||||
@@ -290,21 +290,30 @@ struct scene_obj_img {
|
||||
char *data;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct scene_txt_generic - Generic information common to text objects
|
||||
*
|
||||
* @str_id: ID of the text string to display
|
||||
* @font_name: Name of font (allocated by caller)
|
||||
* @font_size: Nominal size of font in pixels
|
||||
*/
|
||||
struct scene_txt_generic {
|
||||
uint str_id;
|
||||
const char *font_name;
|
||||
uint font_size;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct scene_obj_txt - information about a text object in a scene
|
||||
*
|
||||
* This is a single-line text object
|
||||
*
|
||||
* @obj: Basic object information
|
||||
* @str_id: ID of the text string to display
|
||||
* @font_name: Name of font (allocated by caller)
|
||||
* @font_size: Nominal size of font in pixels
|
||||
* @gen: Generic information common to all objects which show text
|
||||
*/
|
||||
struct scene_obj_txt {
|
||||
struct scene_obj obj;
|
||||
uint str_id;
|
||||
const char *font_name;
|
||||
uint font_size;
|
||||
struct scene_txt_generic gen;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -858,7 +858,7 @@ static int check_font(struct unit_test_state *uts, struct scene *scn, uint id,
|
||||
txt = scene_obj_find(scn, id, SCENEOBJT_TEXT);
|
||||
ut_assertnonnull(txt);
|
||||
|
||||
ut_asserteq(font_size, txt->font_size);
|
||||
ut_asserteq(font_size, txt->gen.font_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ static int cedit_base(struct unit_test_state *uts)
|
||||
|
||||
txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
|
||||
ut_assertnonnull(txt);
|
||||
ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id));
|
||||
ut_asserteq_str("AC Power", expo_get_str(exp, txt->gen.str_id));
|
||||
|
||||
ut_asserteq(ID_AC_ON, menu->cur_item_id);
|
||||
|
||||
|
||||
@@ -280,8 +280,8 @@ static int expo_object_attr(struct unit_test_state *uts)
|
||||
|
||||
strcpy(name, "font2");
|
||||
ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, name, 42));
|
||||
ut_asserteq_ptr(name, txt->font_name);
|
||||
ut_asserteq(42, txt->font_size);
|
||||
ut_asserteq_ptr(name, txt->gen.font_name);
|
||||
ut_asserteq(42, txt->gen.font_size);
|
||||
|
||||
ut_asserteq(-ENOENT, scene_txt_set_font(scn, OBJ_TEXT2, name, 42));
|
||||
|
||||
@@ -296,7 +296,7 @@ static int expo_object_attr(struct unit_test_state *uts)
|
||||
node = ofnode_path("/bootstd/theme");
|
||||
ut_assert(ofnode_valid(node));
|
||||
ut_assertok(expo_apply_theme(exp, node));
|
||||
ut_asserteq(30, txt->font_size);
|
||||
ut_asserteq(30, txt->gen.font_size);
|
||||
|
||||
expo_destroy(exp);
|
||||
|
||||
@@ -727,7 +727,7 @@ static int expo_test_build(struct unit_test_state *uts)
|
||||
ut_assertnonnull(scn);
|
||||
ut_asserteq_str("main", scn->name);
|
||||
ut_asserteq(ID_SCENE1, scn->id);
|
||||
ut_asserteq(ID_DYNAMIC_START + 1, scn->title_id);
|
||||
ut_asserteq(ID_DYNAMIC_START, scn->title_id);
|
||||
ut_asserteq(0, scn->highlight_id);
|
||||
|
||||
/* check the title */
|
||||
@@ -739,7 +739,8 @@ static int expo_test_build(struct unit_test_state *uts)
|
||||
ut_asserteq(scn->title_id, obj->id);
|
||||
ut_asserteq(SCENEOBJT_TEXT, obj->type);
|
||||
ut_asserteq(0, obj->flags);
|
||||
ut_asserteq_str("Test Configuration", expo_get_str(exp, txt->str_id));
|
||||
ut_asserteq_str("Test Configuration",
|
||||
expo_get_str(exp, txt->gen.str_id));
|
||||
|
||||
/* check the menu */
|
||||
menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_NONE);
|
||||
@@ -751,7 +752,7 @@ static int expo_test_build(struct unit_test_state *uts)
|
||||
ut_asserteq(0, obj->flags);
|
||||
|
||||
txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
|
||||
ut_asserteq_str("CPU speed", expo_get_str(exp, txt->str_id));
|
||||
ut_asserteq_str("CPU speed", expo_get_str(exp, txt->gen.str_id));
|
||||
|
||||
ut_asserteq(0, menu->cur_item_id);
|
||||
ut_asserteq(0, menu->pointer_id);
|
||||
@@ -768,7 +769,7 @@ static int expo_test_build(struct unit_test_state *uts)
|
||||
ut_asserteq(0, item->value);
|
||||
|
||||
txt = scene_obj_find(scn, item->label_id, SCENEOBJT_NONE);
|
||||
ut_asserteq_str("2 GHz", expo_get_str(exp, txt->str_id));
|
||||
ut_asserteq_str("2 GHz", expo_get_str(exp, txt->gen.str_id));
|
||||
|
||||
count = list_count_nodes(&menu->item_head);
|
||||
ut_asserteq(3, count);
|
||||
|
||||
Reference in New Issue
Block a user