Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * Copyright 2022 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #ifndef __SCENE_H |
| 8 | #define __SCENE_H |
| 9 | |
| 10 | #include <linux/list.h> |
| 11 | |
| 12 | struct udevice; |
| 13 | |
| 14 | /** |
| 15 | * enum expoact_type - types of actions reported by the expo |
| 16 | * |
| 17 | * @EXPOACT_NONE: no action |
| 18 | * @EXPOACT_POINT: menu item was highlighted (@id indicates which) |
| 19 | * @EXPOACT_SELECT: menu item was selected (@id indicates which) |
| 20 | * @EXPOACT_QUIT: request to exit the menu |
| 21 | */ |
| 22 | enum expoact_type { |
| 23 | EXPOACT_NONE, |
| 24 | EXPOACT_POINT, |
| 25 | EXPOACT_SELECT, |
| 26 | EXPOACT_QUIT, |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * struct expo_action - an action report by the expo |
| 31 | * |
| 32 | * @type: Action type (EXPOACT_NONE if there is no action) |
| 33 | * @select: Used for EXPOACT_POINT and EXPOACT_SELECT |
| 34 | * @id: ID number of the object affected. |
| 35 | */ |
| 36 | struct expo_action { |
| 37 | enum expoact_type type; |
| 38 | union { |
| 39 | struct { |
| 40 | int id; |
| 41 | } select; |
| 42 | }; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * struct expo - information about an expo |
| 47 | * |
| 48 | * A group of scenes which can be presented to the user, typically to obtain |
| 49 | * input or to make a selection. |
| 50 | * |
| 51 | * @name: Name of the expo (allocated) |
| 52 | * @display: Display to use (`UCLASS_VIDEO`), or NULL to use text mode |
Simon Glass | 67e2af1 | 2023-06-01 10:22:34 -0600 | [diff] [blame^] | 53 | * @cons: Console to use (`UCLASS_VIDEO_CONSOLE`), or NULL to use text mode |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 54 | * @scene_id: Current scene ID (0 if none) |
| 55 | * @next_id: Next ID number to use, for automatic allocation |
| 56 | * @action: Action selected by user. At present only one is supported, with the |
| 57 | * type set to EXPOACT_NONE if there is no action |
| 58 | * @text_mode: true to use text mode for the menu (no vidconsole) |
| 59 | * @priv: Private data for the controller |
| 60 | * @scene_head: List of scenes |
| 61 | * @str_head: list of strings |
| 62 | */ |
| 63 | struct expo { |
| 64 | char *name; |
| 65 | struct udevice *display; |
Simon Glass | 67e2af1 | 2023-06-01 10:22:34 -0600 | [diff] [blame^] | 66 | struct udevice *cons; |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 67 | uint scene_id; |
| 68 | uint next_id; |
| 69 | struct expo_action action; |
| 70 | bool text_mode; |
| 71 | void *priv; |
| 72 | struct list_head scene_head; |
| 73 | struct list_head str_head; |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * struct expo_string - a string that can be used in an expo |
| 78 | * |
| 79 | * @id: ID number of the string |
| 80 | * @str: String |
| 81 | * @sibling: Node to link this object to its siblings |
| 82 | */ |
| 83 | struct expo_string { |
| 84 | uint id; |
| 85 | const char *str; |
| 86 | struct list_head sibling; |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * struct scene - information about a scene in an expo |
| 91 | * |
| 92 | * A collection of text/image/menu items in an expo |
| 93 | * |
| 94 | * @expo: Expo this scene is part of |
| 95 | * @name: Name of the scene (allocated) |
| 96 | * @id: ID number of the scene |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 97 | * @title_id: String ID of title of the scene (allocated) |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 98 | * @sibling: Node to link this scene to its siblings |
| 99 | * @obj_head: List of objects in the scene |
| 100 | */ |
| 101 | struct scene { |
| 102 | struct expo *expo; |
| 103 | char *name; |
| 104 | uint id; |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 105 | uint title_id; |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 106 | struct list_head sibling; |
| 107 | struct list_head obj_head; |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * enum scene_obj_t - type of a scene object |
| 112 | * |
| 113 | * @SCENEOBJT_NONE: Used to indicate that the type does not matter |
| 114 | * @SCENEOBJT_IMAGE: Image data to render |
| 115 | * @SCENEOBJT_TEXT: Text line to render |
| 116 | * @SCENEOBJT_MENU: Menu containing items the user can select |
| 117 | */ |
| 118 | enum scene_obj_t { |
| 119 | SCENEOBJT_NONE = 0, |
| 120 | SCENEOBJT_IMAGE, |
| 121 | SCENEOBJT_TEXT, |
| 122 | SCENEOBJT_MENU, |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * struct scene_obj - information about an object in a scene |
| 127 | * |
| 128 | * @scene: Scene that this object relates to |
| 129 | * @name: Name of the object (allocated) |
| 130 | * @id: ID number of the object |
| 131 | * @type: Type of this object |
| 132 | * @x: x position, in pixels from left side |
| 133 | * @y: y position, in pixels from top |
| 134 | * @hide: true if the object should be hidden |
| 135 | * @sibling: Node to link this object to its siblings |
| 136 | */ |
| 137 | struct scene_obj { |
| 138 | struct scene *scene; |
| 139 | char *name; |
| 140 | uint id; |
| 141 | enum scene_obj_t type; |
| 142 | int x; |
| 143 | int y; |
| 144 | bool hide; |
| 145 | struct list_head sibling; |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * struct scene_obj_img - information about an image object in a scene |
| 150 | * |
| 151 | * This is a rectangular image which is blitted onto the display |
| 152 | * |
| 153 | * @obj: Basic object information |
| 154 | * @data: Image data in BMP format |
| 155 | */ |
| 156 | struct scene_obj_img { |
| 157 | struct scene_obj obj; |
| 158 | char *data; |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * struct scene_obj_txt - information about a text object in a scene |
| 163 | * |
| 164 | * This is a single-line text object |
| 165 | * |
| 166 | * @obj: Basic object information |
| 167 | * @str_id: ID of the text string to display |
| 168 | * @font_name: Name of font (allocated by caller) |
| 169 | * @font_size: Nominal size of font in pixels |
| 170 | */ |
| 171 | struct scene_obj_txt { |
| 172 | struct scene_obj obj; |
| 173 | uint str_id; |
| 174 | const char *font_name; |
| 175 | uint font_size; |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * struct scene_obj_menu - information about a menu object in a scene |
| 180 | * |
| 181 | * A menu has a number of items which can be selected by the user |
| 182 | * |
| 183 | * It also has: |
| 184 | * |
| 185 | * - a text/image object (@pointer_id) which points to the current item |
| 186 | * (@cur_item_id) |
| 187 | * |
| 188 | * - a preview object which shows an image related to the current item |
| 189 | * |
| 190 | * @obj: Basic object information |
| 191 | * @title_id: ID of the title text, or 0 if none |
| 192 | * @cur_item_id: ID of the current menu item, or 0 if none |
| 193 | * @pointer_id: ID of the object pointing to the current selection |
| 194 | * @item_head: List of items in the menu |
| 195 | */ |
| 196 | struct scene_obj_menu { |
| 197 | struct scene_obj obj; |
| 198 | uint title_id; |
| 199 | uint cur_item_id; |
| 200 | uint pointer_id; |
| 201 | struct list_head item_head; |
| 202 | }; |
| 203 | |
| 204 | /** |
| 205 | * enum scene_menuitem_flags_t - flags for menu items |
| 206 | * |
| 207 | * @SCENEMIF_GAP_BEFORE: Add a gap before this item |
| 208 | */ |
| 209 | enum scene_menuitem_flags_t { |
| 210 | SCENEMIF_GAP_BEFORE = 1 << 0, |
| 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * struct scene_menitem - a menu item in a menu |
| 215 | * |
| 216 | * A menu item has: |
| 217 | * |
| 218 | * - text object holding the name (short) and description (can be longer) |
| 219 | * - a text object holding the keypress |
| 220 | * |
| 221 | * @name: Name of the item (this is allocated by this call) |
| 222 | * @id: ID number of the object |
| 223 | * @key_id: ID of text object to use as the keypress to show |
| 224 | * @label_id: ID of text object to use as the label text |
| 225 | * @desc_id: ID of text object to use as the description text |
| 226 | * @preview_id: ID of the preview object, or 0 if none |
| 227 | * @flags: Flags for this item |
| 228 | * @sibling: Node to link this item to its siblings |
| 229 | */ |
| 230 | struct scene_menitem { |
| 231 | char *name; |
| 232 | uint id; |
| 233 | uint key_id; |
| 234 | uint label_id; |
| 235 | uint desc_id; |
| 236 | uint preview_id; |
| 237 | uint flags; |
| 238 | struct list_head sibling; |
| 239 | }; |
| 240 | |
| 241 | /** |
| 242 | * expo_new() - create a new expo |
| 243 | * |
| 244 | * Allocates a new expo |
| 245 | * |
| 246 | * @name: Name of expo (this is allocated by this call) |
| 247 | * @priv: Private data for the controller |
| 248 | * @expp: Returns a pointer to the new expo on success |
| 249 | * Returns: 0 if OK, -ENOMEM if out of memory |
| 250 | */ |
| 251 | int expo_new(const char *name, void *priv, struct expo **expp); |
| 252 | |
| 253 | /** |
| 254 | * expo_destroy() - Destroy an expo and free all its memory |
| 255 | * |
| 256 | * @exp: Expo to destroy |
| 257 | */ |
| 258 | void expo_destroy(struct expo *exp); |
| 259 | |
| 260 | /** |
| 261 | * expo_str() - add a new string to an expo |
| 262 | * |
| 263 | * @exp: Expo to update |
| 264 | * @name: Name to use (this is allocated by this call) |
| 265 | * @id: ID to use for the new object (0 to allocate one) |
| 266 | * @str: Pointer to text to display (allocated by caller) |
| 267 | * Returns: ID number for the object (typically @id), or -ve on error |
| 268 | */ |
| 269 | int expo_str(struct expo *exp, const char *name, uint id, const char *str); |
| 270 | |
| 271 | /** |
| 272 | * expo_get_str() - Get a string by ID |
| 273 | * |
| 274 | * @exp: Expo to use |
| 275 | * @id: String ID to look up |
| 276 | * @returns string, or NULL if not found |
| 277 | */ |
| 278 | const char *expo_get_str(struct expo *exp, uint id); |
| 279 | |
| 280 | /** |
| 281 | * expo_set_display() - set the display to use for a expo |
| 282 | * |
| 283 | * @exp: Expo to update |
| 284 | * @dev: Display to use (`UCLASS_VIDEO`), NULL to use text mode |
| 285 | * Returns: 0 (always) |
| 286 | */ |
| 287 | int expo_set_display(struct expo *exp, struct udevice *dev); |
| 288 | |
| 289 | /** |
| 290 | * expo_set_scene_id() - Set the current scene ID |
| 291 | * |
| 292 | * @exp: Expo to update |
| 293 | * @scene_id: New scene ID to use (0 to select no scene) |
| 294 | * Returns: 0 if OK, -ENOENT if there is no scene with that ID |
| 295 | */ |
| 296 | int expo_set_scene_id(struct expo *exp, uint scene_id); |
| 297 | |
| 298 | /** |
| 299 | * expo_render() - render the expo on the display / console |
| 300 | * |
| 301 | * @exp: Expo to render |
| 302 | * |
| 303 | * Returns: 0 if OK, -ECHILD if there is no current scene, -ENOENT if the |
| 304 | * current scene is not found, other error if something else goes wrong |
| 305 | */ |
| 306 | int expo_render(struct expo *exp); |
| 307 | |
| 308 | /** |
| 309 | * exp_set_text_mode() - Controls whether the expo renders in text mode |
| 310 | * |
| 311 | * @exp: Expo to update |
| 312 | * @text_mode: true to use text mode, false to use the console |
| 313 | */ |
| 314 | void exp_set_text_mode(struct expo *exp, bool text_mode); |
| 315 | |
| 316 | /** |
| 317 | * scene_new() - create a new scene in a expo |
| 318 | * |
| 319 | * The scene is given the ID @id which must be unique across all scenes, objects |
| 320 | * and items. The expo's @next_id is updated to at least @id + 1 |
| 321 | * |
| 322 | * @exp: Expo to update |
| 323 | * @name: Name to use (this is allocated by this call) |
| 324 | * @id: ID to use for the new scene (0 to allocate one) |
| 325 | * @scnp: Returns a pointer to the new scene on success |
| 326 | * Returns: ID number for the scene (typically @id), or -ve on error |
| 327 | */ |
| 328 | int scene_new(struct expo *exp, const char *name, uint id, struct scene **scnp); |
| 329 | |
| 330 | /** |
| 331 | * expo_lookup_scene_id() - Look up a scene by ID |
| 332 | * |
| 333 | * @exp: Expo to check |
| 334 | * @scene_id: Scene ID to look up |
| 335 | * @returns pointer to scene if found, else NULL |
| 336 | */ |
| 337 | struct scene *expo_lookup_scene_id(struct expo *exp, uint scene_id); |
| 338 | |
| 339 | /** |
| 340 | * scene_title_set() - set the scene title |
| 341 | * |
| 342 | * @scn: Scene to update |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 343 | * @title_id: Title ID to set |
| 344 | * Returns: 0 if OK |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 345 | */ |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 346 | int scene_title_set(struct scene *scn, uint title_id); |
Simon Glass | d8adbe9 | 2023-01-06 08:52:36 -0600 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * scene_obj_count() - Count the number of objects in a scene |
| 350 | * |
| 351 | * @scn: Scene to check |
| 352 | * Returns: number of objects in the scene, 0 if none |
| 353 | */ |
| 354 | int scene_obj_count(struct scene *scn); |
| 355 | |
| 356 | /** |
| 357 | * scene_img() - add a new image to a scene |
| 358 | * |
| 359 | * @scn: Scene to update |
| 360 | * @name: Name to use (this is allocated by this call) |
| 361 | * @id: ID to use for the new object (0 to allocate one) |
| 362 | * @data: Pointer to image data |
| 363 | * @imgp: If non-NULL, returns the new object |
| 364 | * Returns: ID number for the object (typically @id), or -ve on error |
| 365 | */ |
| 366 | int scene_img(struct scene *scn, const char *name, uint id, char *data, |
| 367 | struct scene_obj_img **imgp); |
| 368 | |
| 369 | /** |
| 370 | * scene_txt() - add a new text object to a scene |
| 371 | * |
| 372 | * @scn: Scene to update |
| 373 | * @name: Name to use (this is allocated by this call) |
| 374 | * @id: ID to use for the new object (0 to allocate one) |
| 375 | * @str_id: ID of the string to use |
| 376 | * @txtp: If non-NULL, returns the new object |
| 377 | * Returns: ID number for the object (typically @id), or -ve on error |
| 378 | */ |
| 379 | int scene_txt(struct scene *scn, const char *name, uint id, uint str_id, |
| 380 | struct scene_obj_txt **txtp); |
| 381 | |
| 382 | /** |
| 383 | * scene_txt_str() - add a new string to expr and text object to a scene |
| 384 | * |
| 385 | * @scn: Scene to update |
| 386 | * @name: Name to use (this is allocated by this call) |
| 387 | * @id: ID to use for the new object (0 to allocate one) |
| 388 | * @str_id: ID of the string to use |
| 389 | * @str: Pointer to text to display (allocated by caller) |
| 390 | * @txtp: If non-NULL, returns the new object |
| 391 | * Returns: ID number for the object (typically @id), or -ve on error |
| 392 | */ |
| 393 | int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id, |
| 394 | const char *str, struct scene_obj_txt **txtp); |
| 395 | |
| 396 | /** |
| 397 | * scene_menu() - create a menu |
| 398 | * |
| 399 | * @scn: Scene to update |
| 400 | * @name: Name to use (this is allocated by this call) |
| 401 | * @id: ID to use for the new object (0 to allocate one) |
| 402 | * @menup: If non-NULL, returns the new object |
| 403 | * Returns: ID number for the object (typically @id), or -ve on error |
| 404 | */ |
| 405 | int scene_menu(struct scene *scn, const char *name, uint id, |
| 406 | struct scene_obj_menu **menup); |
| 407 | |
| 408 | /** |
| 409 | * scene_txt_set_font() - Set the font for an object |
| 410 | * |
| 411 | * @scn: Scene to update |
| 412 | * @id: ID of object to update |
| 413 | * @font_name: Font name to use (allocated by caller) |
| 414 | * @font_size: Font size to use (nominal height in pixels) |
| 415 | */ |
| 416 | int scene_txt_set_font(struct scene *scn, uint id, const char *font_name, |
| 417 | uint font_size); |
| 418 | |
| 419 | /** |
| 420 | * scene_obj_set_pos() - Set the postion of an object |
| 421 | * |
| 422 | * @scn: Scene to update |
| 423 | * @id: ID of object to update |
| 424 | * @x: x position, in pixels from left side |
| 425 | * @y: y position, in pixels from top |
| 426 | * Returns: 0 if OK, -ENOENT if @id is invalid |
| 427 | */ |
| 428 | int scene_obj_set_pos(struct scene *scn, uint id, int x, int y); |
| 429 | |
| 430 | /** |
| 431 | * scene_obj_set_hide() - Set whether an object is hidden |
| 432 | * |
| 433 | * The update happens when the expo is next rendered. |
| 434 | * |
| 435 | * @scn: Scene to update |
| 436 | * @id: ID of object to update |
| 437 | * @hide: true to hide the object, false to show it |
| 438 | * Returns: 0 if OK, -ENOENT if @id is invalid |
| 439 | */ |
| 440 | int scene_obj_set_hide(struct scene *scn, uint id, bool hide); |
| 441 | |
| 442 | /** |
| 443 | * scene_menu_set_title() - Set the title of a menu |
| 444 | * |
| 445 | * @scn: Scene to update |
| 446 | * @id: ID of menu object to update |
| 447 | * @title_id: ID of text object to use as the title |
| 448 | * Returns: 0 if OK, -ENOENT if @id is invalid, -EINVAL if @title_id is invalid |
| 449 | */ |
| 450 | int scene_menu_set_title(struct scene *scn, uint id, uint title_id); |
| 451 | |
| 452 | /** |
| 453 | * scene_menu_set_pointer() - Set the item pointer for a menu |
| 454 | * |
| 455 | * This is a visual indicator of the current item, typically a ">" character |
| 456 | * which sits next to the current item and moves when the user presses the |
| 457 | * up/down arrow keys |
| 458 | * |
| 459 | * @scn: Scene to update |
| 460 | * @id: ID of menu object to update |
| 461 | * @cur_item_id: ID of text or image object to use as a pointer to the current |
| 462 | * item |
| 463 | * Returns: 0 if OK, -ENOENT if @id is invalid, -EINVAL if @cur_item_id is invalid |
| 464 | */ |
| 465 | int scene_menu_set_pointer(struct scene *scn, uint id, uint cur_item_id); |
| 466 | |
| 467 | /** |
| 468 | * scene_obj_get_hw() - Get width and height of an object in a scene |
| 469 | * |
| 470 | * @scn: Scene to check |
| 471 | * @id: ID of menu object to check |
| 472 | * @widthp: If non-NULL, returns width of object in pixels |
| 473 | * Returns: Height of object in pixels |
| 474 | */ |
| 475 | int scene_obj_get_hw(struct scene *scn, uint id, int *widthp); |
| 476 | |
| 477 | /** |
| 478 | * scene_menuitem() - Add an item to a menu |
| 479 | * |
| 480 | * @scn: Scene to update |
| 481 | * @menu_id: ID of menu object to update |
| 482 | * @name: Name to use (this is allocated by this call) |
| 483 | * @id: ID to use for the new object (0 to allocate one) |
| 484 | * @key_id: ID of text object to use as the keypress to show |
| 485 | * @label_id: ID of text object to use as the label text |
| 486 | * @desc_id: ID of text object to use as the description text |
| 487 | * @preview_id: ID of object to use as the preview (text or image) |
| 488 | * @flags: Flags for this item (enum scene_menuitem_flags_t) |
| 489 | * @itemp: If non-NULL, returns the new object |
| 490 | * Returns: ID number for the item (typically @id), or -ve on error |
| 491 | */ |
| 492 | int scene_menuitem(struct scene *scn, uint menu_id, const char *name, uint id, |
| 493 | uint key_id, uint label_id, uint desc_id, uint preview_id, |
| 494 | uint flags, struct scene_menitem **itemp); |
| 495 | |
| 496 | /** |
| 497 | * scene_arrange() - Arrange the scene to deal with object sizes |
| 498 | * |
| 499 | * Updates any menus in the scene so that their objects are in the right place. |
| 500 | * |
| 501 | * @scn: Scene to arrange |
| 502 | * Returns: 0 if OK, -ve on error |
| 503 | */ |
| 504 | int scene_arrange(struct scene *scn); |
| 505 | |
| 506 | /** |
| 507 | * expo_send_key() - set a keypress to the expo |
| 508 | * |
| 509 | * @exp: Expo to receive the key |
| 510 | * @key: Key to send (ASCII or enum bootmenu_key) |
| 511 | * Returns: 0 if OK, -ECHILD if there is no current scene |
| 512 | */ |
| 513 | int expo_send_key(struct expo *exp, int key); |
| 514 | |
| 515 | /** |
| 516 | * expo_action_get() - read user input from the expo |
| 517 | * |
| 518 | * @exp: Expo to check |
| 519 | * @act: Returns action |
| 520 | * Returns: 0 if OK, -EAGAIN if there was no action to return |
| 521 | */ |
| 522 | int expo_action_get(struct expo *exp, struct expo_action *act); |
| 523 | |
| 524 | #endif /*__SCENE_H */ |