Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Implementation of a expo, a collection of scenes providing menu options |
| 4 | * |
| 5 | * Copyright 2022 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
Simon Glass | c98cb51 | 2023-06-01 10:22:43 -0600 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EXPO |
| 10 | |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <expo.h> |
| 14 | #include <malloc.h> |
| 15 | #include <video.h> |
| 16 | #include "scene_internal.h" |
| 17 | |
| 18 | int expo_new(const char *name, void *priv, struct expo **expp) |
| 19 | { |
| 20 | struct expo *exp; |
| 21 | |
| 22 | exp = calloc(1, sizeof(struct expo)); |
| 23 | if (!exp) |
| 24 | return log_msg_ret("expo", -ENOMEM); |
| 25 | exp->name = strdup(name); |
| 26 | if (!exp->name) { |
| 27 | free(exp); |
| 28 | return log_msg_ret("name", -ENOMEM); |
| 29 | } |
| 30 | exp->priv = priv; |
| 31 | INIT_LIST_HEAD(&exp->scene_head); |
| 32 | INIT_LIST_HEAD(&exp->str_head); |
| 33 | |
| 34 | *expp = exp; |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static void estr_destroy(struct expo_string *estr) |
| 40 | { |
| 41 | free(estr); |
| 42 | } |
| 43 | |
| 44 | void expo_destroy(struct expo *exp) |
| 45 | { |
| 46 | struct scene *scn, *next; |
| 47 | struct expo_string *estr, *enext; |
| 48 | |
| 49 | list_for_each_entry_safe(scn, next, &exp->scene_head, sibling) |
| 50 | scene_destroy(scn); |
| 51 | |
| 52 | list_for_each_entry_safe(estr, enext, &exp->str_head, sibling) |
| 53 | estr_destroy(estr); |
| 54 | |
| 55 | free(exp->name); |
| 56 | free(exp); |
| 57 | } |
| 58 | |
Simon Glass | 9af3415 | 2023-06-01 10:22:47 -0600 | [diff] [blame] | 59 | uint resolve_id(struct expo *exp, uint id) |
| 60 | { |
| 61 | if (!id) |
| 62 | id = exp->next_id++; |
| 63 | else if (id >= exp->next_id) |
| 64 | exp->next_id = id + 1; |
| 65 | |
| 66 | return id; |
| 67 | } |
| 68 | |
| 69 | void expo_set_dynamic_start(struct expo *exp, uint dyn_start) |
| 70 | { |
| 71 | exp->next_id = dyn_start; |
| 72 | } |
| 73 | |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 74 | int expo_str(struct expo *exp, const char *name, uint id, const char *str) |
| 75 | { |
| 76 | struct expo_string *estr; |
| 77 | |
| 78 | estr = calloc(1, sizeof(struct expo_string)); |
| 79 | if (!estr) |
| 80 | return log_msg_ret("obj", -ENOMEM); |
| 81 | |
| 82 | estr->id = resolve_id(exp, id); |
| 83 | estr->str = str; |
| 84 | list_add_tail(&estr->sibling, &exp->str_head); |
| 85 | |
| 86 | return estr->id; |
| 87 | } |
| 88 | |
| 89 | const char *expo_get_str(struct expo *exp, uint id) |
| 90 | { |
| 91 | struct expo_string *estr; |
| 92 | |
| 93 | list_for_each_entry(estr, &exp->str_head, sibling) { |
| 94 | if (estr->id == id) |
| 95 | return estr->str; |
| 96 | } |
| 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | int expo_set_display(struct expo *exp, struct udevice *dev) |
| 102 | { |
Simon Glass | 42b1849 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 103 | struct udevice *cons; |
| 104 | int ret; |
| 105 | |
| 106 | ret = device_find_first_child_by_uclass(dev, UCLASS_VIDEO_CONSOLE, |
| 107 | &cons); |
| 108 | if (ret) |
| 109 | return log_msg_ret("con", ret); |
| 110 | |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 111 | exp->display = dev; |
Simon Glass | 42b1849 | 2023-06-01 10:22:34 -0600 | [diff] [blame] | 112 | exp->cons = cons; |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Simon Glass | 699b0ac | 2023-06-01 10:22:52 -0600 | [diff] [blame^] | 117 | int expo_calc_dims(struct expo *exp) |
| 118 | { |
| 119 | struct scene *scn; |
| 120 | int ret; |
| 121 | |
| 122 | if (!exp->cons) |
| 123 | return log_msg_ret("dim", -ENOTSUPP); |
| 124 | |
| 125 | list_for_each_entry(scn, &exp->scene_head, sibling) { |
| 126 | /* |
| 127 | * Do the menus last so that all the menus' text objects |
| 128 | * are dimensioned |
| 129 | */ |
| 130 | ret = scene_calc_dims(scn, false); |
| 131 | if (ret) |
| 132 | return log_msg_ret("scn", ret); |
| 133 | ret = scene_calc_dims(scn, true); |
| 134 | if (ret) |
| 135 | return log_msg_ret("scn", ret); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Simon Glass | 5904d95 | 2023-06-01 10:22:37 -0600 | [diff] [blame] | 141 | void expo_set_text_mode(struct expo *exp, bool text_mode) |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 142 | { |
| 143 | exp->text_mode = text_mode; |
| 144 | } |
| 145 | |
| 146 | struct scene *expo_lookup_scene_id(struct expo *exp, uint scene_id) |
| 147 | { |
| 148 | struct scene *scn; |
| 149 | |
| 150 | list_for_each_entry(scn, &exp->scene_head, sibling) { |
| 151 | if (scn->id == scene_id) |
| 152 | return scn; |
| 153 | } |
| 154 | |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | int expo_set_scene_id(struct expo *exp, uint scene_id) |
| 159 | { |
Simon Glass | 14a86a5 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 160 | struct scene *scn; |
| 161 | int ret; |
| 162 | |
| 163 | scn = expo_lookup_scene_id(exp, scene_id); |
| 164 | if (!scn) |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 165 | return log_msg_ret("id", -ENOENT); |
Simon Glass | 14a86a5 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 166 | ret = scene_arrange(scn); |
| 167 | if (ret) |
| 168 | return log_msg_ret("arr", ret); |
| 169 | |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 170 | exp->scene_id = scene_id; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int expo_render(struct expo *exp) |
| 176 | { |
| 177 | struct udevice *dev = exp->display; |
| 178 | struct video_priv *vid_priv = dev_get_uclass_priv(dev); |
| 179 | struct scene *scn = NULL; |
| 180 | u32 colour; |
| 181 | int ret; |
| 182 | |
| 183 | colour = video_index_to_colour(vid_priv, VID_WHITE); |
| 184 | ret = video_fill(dev, colour); |
| 185 | if (ret) |
| 186 | return log_msg_ret("fill", ret); |
| 187 | |
| 188 | if (exp->scene_id) { |
| 189 | scn = expo_lookup_scene_id(exp, exp->scene_id); |
| 190 | if (!scn) |
| 191 | return log_msg_ret("scn", -ENOENT); |
| 192 | |
| 193 | ret = scene_render(scn); |
| 194 | if (ret) |
| 195 | return log_msg_ret("ren", ret); |
| 196 | } |
| 197 | |
| 198 | video_sync(dev, true); |
| 199 | |
| 200 | return scn ? 0 : -ECHILD; |
| 201 | } |
| 202 | |
| 203 | int expo_send_key(struct expo *exp, int key) |
| 204 | { |
| 205 | struct scene *scn = NULL; |
| 206 | |
| 207 | if (exp->scene_id) { |
| 208 | int ret; |
| 209 | |
| 210 | scn = expo_lookup_scene_id(exp, exp->scene_id); |
| 211 | if (!scn) |
| 212 | return log_msg_ret("scn", -ENOENT); |
| 213 | |
| 214 | ret = scene_send_key(scn, key, &exp->action); |
| 215 | if (ret) |
| 216 | return log_msg_ret("key", ret); |
Simon Glass | 14a86a5 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 217 | |
| 218 | /* arrange it to get any changes */ |
| 219 | ret = scene_arrange(scn); |
| 220 | if (ret) |
| 221 | return log_msg_ret("arr", ret); |
Simon Glass | 87c6f8a | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return scn ? 0 : -ECHILD; |
| 225 | } |
| 226 | |
| 227 | int expo_action_get(struct expo *exp, struct expo_action *act) |
| 228 | { |
| 229 | *act = exp->action; |
| 230 | exp->action.type = EXPOACT_NONE; |
| 231 | |
| 232 | return act->type == EXPOACT_NONE ? -EAGAIN : 0; |
| 233 | } |