1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright 2022 Google LLC
11 #include <dm/ofnode_decl.h>
12 #include <linux/list.h>
19 * enum expoact_type - types of actions reported by the expo
21 * @EXPOACT_NONE: no action
22 * @EXPOACT_POINT_OBJ: object was highlighted (@id indicates which)
23 * @EXPOACT_POINT_ITEM: menu item was highlighted (@id indicates which)
24 * @EXPOACT_SELECT: menu item was selected (@id indicates which)
25 * @EXPOACT_OPEN: menu was opened, so an item can be selected (@id indicates
27 * @EXPOACT_CLOSE: menu was closed (@id indicates which menu object)
28 * @EXPOACT_QUIT: request to exit the menu
41 * struct expo_action - an action report by the expo
43 * @type: Action type (EXPOACT_NONE if there is no action)
44 * @select: Used for EXPOACT_POINT_ITEM and EXPOACT_SELECT
45 * @id: ID number of the object affected.
48 enum expoact_type type;
57 * struct expo_theme - theme for the expo
59 * @font_size: Default font size for all text
60 * @menu_inset: Inset width (on each side and top/bottom) for menu items
61 * @menuitem_gap_y: Gap between menu items in pixels
70 * struct expo - information about an expo
72 * A group of scenes which can be presented to the user, typically to obtain
73 * input or to make a selection.
75 * @name: Name of the expo (allocated)
76 * @display: Display to use (`UCLASS_VIDEO`), or NULL to use text mode
77 * @cons: Console to use (`UCLASS_VIDEO_CONSOLE`), or NULL to use text mode
78 * @scene_id: Current scene ID (0 if none)
79 * @next_id: Next ID number to use, for automatic allocation
80 * @action: Action selected by user. At present only one is supported, with the
81 * type set to EXPOACT_NONE if there is no action
82 * @text_mode: true to use text mode for the menu (no vidconsole)
83 * @popup: true to use popup menus, instead of showing all items
84 * @priv: Private data for the controller
85 * @theme: Information about fonts styles, etc.
86 * @scene_head: List of scenes
87 * @str_head: list of strings
91 struct udevice *display;
95 struct expo_action action;
99 struct expo_theme theme;
100 struct list_head scene_head;
101 struct list_head str_head;
105 * struct expo_string - a string that can be used in an expo
107 * @id: ID number of the string
109 * @sibling: Node to link this object to its siblings
114 struct list_head sibling;
118 * struct scene - information about a scene in an expo
120 * A collection of text/image/menu items in an expo
122 * @expo: Expo this scene is part of
123 * @name: Name of the scene (allocated)
124 * @id: ID number of the scene
125 * @title_id: String ID of title of the scene (allocated)
126 * @highlight_id: ID of highlighted object, if any
127 * @cls: cread state to use for input
128 * @buf: Buffer for input
129 * @entry_save: Buffer to hold vidconsole text-entry information
130 * @sibling: Node to link this scene to its siblings
131 * @obj_head: List of objects in the scene
139 struct cli_line_state cls;
141 struct abuf entry_save;
142 struct list_head sibling;
143 struct list_head obj_head;
147 * enum scene_obj_t - type of a scene object
149 * @SCENEOBJT_NONE: Used to indicate that the type does not matter
150 * @SCENEOBJT_IMAGE: Image data to render
151 * @SCENEOBJT_TEXT: Text line to render
152 * @SCENEOBJT_MENU: Menu containing items the user can select
153 * @SCENEOBJT_TEXTLINE: Line of text the user can edit
160 /* types from here on can be highlighted */
166 * struct scene_dim - Dimensions of an object
168 * @x: x position, in pixels from left side
169 * @y: y position, in pixels from top
170 * @w: width, in pixels
171 * @h: height, in pixels
181 * enum scene_obj_flags_t - flags for objects
183 * @SCENEOF_HIDE: object should be hidden
184 * @SCENEOF_POINT: object should be highlighted
185 * @SCENEOF_OPEN: object should be opened (e.g. menu is opened so that an option
188 enum scene_obj_flags_t {
189 SCENEOF_HIDE = 1 << 0,
190 SCENEOF_POINT = 1 << 1,
191 SCENEOF_OPEN = 1 << 2,
195 /* Maximum number of characters allowed in an line editor */
196 EXPO_MAX_CHARS = 250,
200 * struct scene_obj - information about an object in a scene
202 * @scene: Scene that this object relates to
203 * @name: Name of the object (allocated)
204 * @id: ID number of the object
205 * @type: Type of this object
206 * @dim: Dimensions for this object
207 * @flags: Flags for this object
208 * @bit_length: Number of bits used for this object in CMOS RAM
209 * @start_bit: Start bit to use for this object in CMOS RAM
210 * @sibling: Node to link this object to its siblings
216 enum scene_obj_t type;
217 struct scene_dim dim;
221 struct list_head sibling;
224 /* object can be highlighted when moving around expo */
225 static inline bool scene_obj_can_highlight(const struct scene_obj *obj)
227 return obj->type >= SCENEOBJT_MENU;
231 * struct scene_obj_img - information about an image object in a scene
233 * This is a rectangular image which is blitted onto the display
235 * @obj: Basic object information
236 * @data: Image data in BMP format
238 struct scene_obj_img {
239 struct scene_obj obj;
244 * struct scene_obj_txt - information about a text object in a scene
246 * This is a single-line text object
248 * @obj: Basic object information
249 * @str_id: ID of the text string to display
250 * @font_name: Name of font (allocated by caller)
251 * @font_size: Nominal size of font in pixels
253 struct scene_obj_txt {
254 struct scene_obj obj;
256 const char *font_name;
261 * struct scene_obj_menu - information about a menu object in a scene
263 * A menu has a number of items which can be selected by the user
267 * - a text/image object (@pointer_id) which points to the current item
270 * - a preview object which shows an image related to the current item
272 * @obj: Basic object information
273 * @title_id: ID of the title text, or 0 if none
274 * @cur_item_id: ID of the current menu item, or 0 if none
275 * @pointer_id: ID of the object pointing to the current selection
276 * @item_head: List of items in the menu
278 struct scene_obj_menu {
279 struct scene_obj obj;
283 struct list_head item_head;
287 * enum scene_menuitem_flags_t - flags for menu items
289 * @SCENEMIF_GAP_BEFORE: Add a gap before this item
291 enum scene_menuitem_flags_t {
292 SCENEMIF_GAP_BEFORE = 1 << 0,
296 * struct scene_menitem - a menu item in a menu
300 * - text object holding the name (short) and description (can be longer)
301 * - a text object holding the keypress
303 * @name: Name of the item (this is allocated by this call)
304 * @id: ID number of the object
305 * @key_id: ID of text object to use as the keypress to show
306 * @label_id: ID of text object to use as the label text
307 * @desc_id: ID of text object to use as the description text
308 * @preview_id: ID of the preview object, or 0 if none
309 * @flags: Flags for this item
310 * @sibling: Node to link this item to its siblings
312 struct scene_menitem {
320 struct list_head sibling;
324 * struct scene_obj_textline - information about a textline in a scene
326 * A textline has a prompt and a line of editable text
328 * @obj: Basic object information
329 * @label_id: ID of the label text, or 0 if none
330 * @edit_id: ID of the editable text
331 * @max_chars: Maximum number of characters allowed
332 * @buf: Text buffer containing current text
333 * @pos: Cursor position
335 struct scene_obj_textline {
336 struct scene_obj obj;
345 * expo_new() - create a new expo
347 * Allocates a new expo
349 * @name: Name of expo (this is allocated by this call)
350 * @priv: Private data for the controller
351 * @expp: Returns a pointer to the new expo on success
352 * Returns: 0 if OK, -ENOMEM if out of memory
354 int expo_new(const char *name, void *priv, struct expo **expp);
357 * expo_destroy() - Destroy an expo and free all its memory
359 * @exp: Expo to destroy
361 void expo_destroy(struct expo *exp);
364 * expo_set_dynamic_start() - Set the start of the 'dynamic' IDs
366 * It is common for a set of 'static' IDs to be used to refer to objects in the
367 * expo. These typically use an enum so that they are defined in sequential
370 * Dynamic IDs (for objects not in the enum) are intended to be used for
371 * objects to which the code does not need to refer. These are ideally located
372 * above the static IDs.
374 * Use this function to set the start of the dynamic range, making sure that the
375 * value is higher than all the statically allocated IDs.
377 * @exp: Expo to update
378 * @dyn_start: Start ID that expo should use for dynamic allocation
380 void expo_set_dynamic_start(struct expo *exp, uint dyn_start);
383 * expo_str() - add a new string to an expo
385 * @exp: Expo 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: Pointer to text to display (allocated by caller)
389 * Returns: ID number for the object (typically @id), or -ve on error
391 int expo_str(struct expo *exp, const char *name, uint id, const char *str);
394 * expo_get_str() - Get a string by ID
397 * @id: String ID to look up
398 * @returns string, or NULL if not found
400 const char *expo_get_str(struct expo *exp, uint id);
403 * expo_set_display() - set the display to use for a expo
405 * @exp: Expo to update
406 * @dev: Display to use (`UCLASS_VIDEO`), NULL to use text mode
407 * Returns: 0 (always)
409 int expo_set_display(struct expo *exp, struct udevice *dev);
412 * expo_calc_dims() - Calculate the dimensions of the objects
414 * Updates the width and height of all objects based on their contents
416 * @exp: Expo to update
417 * Returns 0 if OK, -ENOTSUPP if there is no graphical console
419 int expo_calc_dims(struct expo *exp);
422 * expo_set_scene_id() - Set the current scene ID
424 * @exp: Expo to update
425 * @scene_id: New scene ID to use (0 to select no scene)
426 * Returns: 0 if OK, -ENOENT if there is no scene with that ID
428 int expo_set_scene_id(struct expo *exp, uint scene_id);
431 * expo_first_scene_id() - Get the ID of the first scene
433 * @exp: Expo to check
434 * Returns: Scene ID of first scene, or -ENOENT if there are no scenes
436 int expo_first_scene_id(struct expo *exp);
439 * expo_render() - render the expo on the display / console
441 * @exp: Expo to render
443 * Returns: 0 if OK, -ECHILD if there is no current scene, -ENOENT if the
444 * current scene is not found, other error if something else goes wrong
446 int expo_render(struct expo *exp);
449 * expo_set_text_mode() - Controls whether the expo renders in text mode
451 * @exp: Expo to update
452 * @text_mode: true to use text mode, false to use the console
454 void expo_set_text_mode(struct expo *exp, bool text_mode);
457 * scene_new() - create a new scene in a expo
459 * The scene is given the ID @id which must be unique across all scenes, objects
460 * and items. The expo's @next_id is updated to at least @id + 1
462 * @exp: Expo to update
463 * @name: Name to use (this is allocated by this call)
464 * @id: ID to use for the new scene (0 to allocate one)
465 * @scnp: Returns a pointer to the new scene on success
466 * Returns: ID number for the scene (typically @id), or -ve on error
468 int scene_new(struct expo *exp, const char *name, uint id, struct scene **scnp);
471 * expo_lookup_scene_id() - Look up a scene by ID
473 * @exp: Expo to check
474 * @scene_id: Scene ID to look up
475 * @returns pointer to scene if found, else NULL
477 struct scene *expo_lookup_scene_id(struct expo *exp, uint scene_id);
480 * scene_highlight_first() - Highlight the first item in a scene
482 * This highlights the first item, so that the user can see that it is pointed
485 * @scn: Scene to update
487 void scene_highlight_first(struct scene *scn);
490 * scene_set_highlight_id() - Set the object which is highlighted
492 * Sets a new object to highlight in the scene
494 * @scn: Scene to update
495 * @id: ID of object to highlight
497 void scene_set_highlight_id(struct scene *scn, uint id);
500 * scene_set_open() - Set whether an item is open or not
502 * @scn: Scene to update
503 * @id: ID of object to update
504 * @open: true to open the object, false to close it
505 * Returns: 0 if OK, -ENOENT if @id is invalid
507 int scene_set_open(struct scene *scn, uint id, bool open);
510 * scene_title_set() - set the scene title
512 * @scn: Scene to update
513 * @title_id: Title ID to set
516 int scene_title_set(struct scene *scn, uint title_id);
519 * scene_obj_count() - Count the number of objects in a scene
521 * @scn: Scene to check
522 * Returns: number of objects in the scene, 0 if none
524 int scene_obj_count(struct scene *scn);
527 * scene_img() - add a new image to a scene
529 * @scn: Scene to update
530 * @name: Name to use (this is allocated by this call)
531 * @id: ID to use for the new object (0 to allocate one)
532 * @data: Pointer to image data
533 * @imgp: If non-NULL, returns the new object
534 * Returns: ID number for the object (typically @id), or -ve on error
536 int scene_img(struct scene *scn, const char *name, uint id, char *data,
537 struct scene_obj_img **imgp);
540 * scene_txt() - add a new text object to a scene
542 * @scn: Scene to update
543 * @name: Name to use (this is allocated by this call)
544 * @id: ID to use for the new object (0 to allocate one)
545 * @str_id: ID of the string to use
546 * @txtp: If non-NULL, returns the new object
547 * Returns: ID number for the object (typically @id), or -ve on error
549 int scene_txt(struct scene *scn, const char *name, uint id, uint str_id,
550 struct scene_obj_txt **txtp);
553 * scene_txt_str() - add a new string to expo and text object to a scene
555 * @scn: Scene to update
556 * @name: Name to use (this is allocated by this call)
557 * @id: ID to use for the new object (0 to allocate one)
558 * @str_id: ID of the string to use
559 * @str: Pointer to text to display (allocated by caller)
560 * @txtp: If non-NULL, returns the new object
561 * Returns: ID number for the object (typically @id), or -ve on error
563 int scene_txt_str(struct scene *scn, const char *name, uint id, uint str_id,
564 const char *str, struct scene_obj_txt **txtp);
567 * scene_menu() - create a menu
569 * @scn: Scene to update
570 * @name: Name to use (this is allocated by this call)
571 * @id: ID to use for the new object (0 to allocate one)
572 * @menup: If non-NULL, returns the new object
573 * Returns: ID number for the object (typically @id), or -ve on error
575 int scene_menu(struct scene *scn, const char *name, uint id,
576 struct scene_obj_menu **menup);
579 * scene_textline() - create a textline
581 * @scn: Scene to update
582 * @name: Name to use (this is allocated by this call)
583 * @id: ID to use for the new object (0 to allocate one)
584 * @max_chars: Maximum length of the textline in characters
585 * @tlinep: If non-NULL, returns the new object
586 * Returns: ID number for the object (typically @id), or -ve on error
588 int scene_textline(struct scene *scn, const char *name, uint id, uint max_chars,
589 struct scene_obj_textline **tlinep);
592 * scene_txt_set_font() - Set the font for an object
594 * @scn: Scene to update
595 * @id: ID of object to update
596 * @font_name: Font name to use (allocated by caller)
597 * @font_size: Font size to use (nominal height in pixels)
599 int scene_txt_set_font(struct scene *scn, uint id, const char *font_name,
603 * scene_obj_set_pos() - Set the postion of an object
605 * @scn: Scene to update
606 * @id: ID of object to update
607 * @x: x position, in pixels from left side
608 * @y: y position, in pixels from top
609 * Returns: 0 if OK, -ENOENT if @id is invalid
611 int scene_obj_set_pos(struct scene *scn, uint id, int x, int y);
614 * scene_obj_set_size() - Set the size of an object
616 * @scn: Scene to update
617 * @id: ID of object to update
618 * @w: width in pixels
619 * @h: height in pixels
620 * Returns: 0 if OK, -ENOENT if @id is invalid
622 int scene_obj_set_size(struct scene *scn, uint id, int w, int h);
625 * scene_obj_set_hide() - Set whether an object is hidden
627 * The update happens when the expo is next rendered.
629 * @scn: Scene to update
630 * @id: ID of object to update
631 * @hide: true to hide the object, false to show it
632 * Returns: 0 if OK, -ENOENT if @id is invalid
634 int scene_obj_set_hide(struct scene *scn, uint id, bool hide);
637 * scene_menu_set_title() - Set the title of a menu
639 * @scn: Scene to update
640 * @id: ID of menu object to update
641 * @title_id: ID of text object to use as the title
642 * Returns: 0 if OK, -ENOENT if @id is invalid, -EINVAL if @title_id is invalid
644 int scene_menu_set_title(struct scene *scn, uint id, uint title_id);
647 * scene_menu_set_pointer() - Set the item pointer for a menu
649 * This is a visual indicator of the current item, typically a ">" character
650 * which sits next to the current item and moves when the user presses the
653 * @scn: Scene to update
654 * @id: ID of menu object to update
655 * @cur_item_id: ID of text or image object to use as a pointer to the current
657 * Returns: 0 if OK, -ENOENT if @id is invalid, -EINVAL if @cur_item_id is invalid
659 int scene_menu_set_pointer(struct scene *scn, uint id, uint cur_item_id);
662 * scene_obj_get_hw() - Get width and height of an object in a scene
664 * @scn: Scene to check
665 * @id: ID of menu object to check
666 * @widthp: If non-NULL, returns width of object in pixels
667 * Returns: Height of object in pixels
669 int scene_obj_get_hw(struct scene *scn, uint id, int *widthp);
672 * scene_menuitem() - Add an item to a menu
674 * @scn: Scene to update
675 * @menu_id: ID of menu object to update
676 * @name: Name to use (this is allocated by this call)
677 * @id: ID to use for the new object (0 to allocate one)
678 * @key_id: ID of text object to use as the keypress to show
679 * @label_id: ID of text object to use as the label text
680 * @desc_id: ID of text object to use as the description text
681 * @preview_id: ID of object to use as the preview (text or image)
682 * @flags: Flags for this item (enum scene_menuitem_flags_t)
683 * @itemp: If non-NULL, returns the new object
684 * Returns: ID number for the item (typically @id), or -ve on error
686 int scene_menuitem(struct scene *scn, uint menu_id, const char *name, uint id,
687 uint key_id, uint label_id, uint desc_id, uint preview_id,
688 uint flags, struct scene_menitem **itemp);
691 * scene_arrange() - Arrange the scene to deal with object sizes
693 * Updates any menus in the scene so that their objects are in the right place.
695 * @scn: Scene to arrange
696 * Returns: 0 if OK, -ve on error
698 int scene_arrange(struct scene *scn);
701 * expo_send_key() - set a keypress to the expo
703 * @exp: Expo to receive the key
704 * @key: Key to send (ASCII or enum bootmenu_key)
705 * Returns: 0 if OK, -ECHILD if there is no current scene
707 int expo_send_key(struct expo *exp, int key);
710 * expo_action_get() - read user input from the expo
712 * @exp: Expo to check
713 * @act: Returns action
714 * Returns: 0 if OK, -EAGAIN if there was no action to return
716 int expo_action_get(struct expo *exp, struct expo_action *act);
719 * expo_apply_theme() - Apply a theme to an expo
721 * @exp: Expo to update
722 * @node: Node containing the theme
724 int expo_apply_theme(struct expo *exp, ofnode node);
727 * expo_build() - Build an expo from an FDT description
729 * Build a complete expo from a description in the provided devicetree.
731 * See doc/develop/expo.rst for a description of the format
733 * @root: Root node for expo description
734 * @expp: Returns the new expo
735 * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
736 * error, -ENOENT if there is a references to a non-existent string
738 int expo_build(ofnode root, struct expo **expp);