blob: 2694e44f6ecf395f1aa257229722a98353f9d8fd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass83510762016-01-18 19:52:17 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass83510762016-01-18 19:52:17 -07004 */
5
6#ifndef __video_console_h
7#define __video_console_h
8
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +01009#include <video.h>
10
Simon Glass2d7c2682020-07-02 21:12:18 -060011struct video_priv;
12
Simon Glassf2661782016-01-14 18:10:37 -070013#define VID_FRAC_DIV 256
14
15#define VID_TO_PIXEL(x) ((x) / VID_FRAC_DIV)
16#define VID_TO_POS(x) ((x) * VID_FRAC_DIV)
17
Simon Glass83510762016-01-18 19:52:17 -070018/**
19 * struct vidconsole_priv - uclass-private data about a console device
20 *
Simon Glassf2661782016-01-14 18:10:37 -070021 * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
22 * method. Drivers may set up @xstart_frac if desired.
23 *
Heinrich Schuchardt662f3812018-09-19 21:31:48 +020024 * @sdev: stdio device, acting as an output sink
25 * @xcur_frac: Current X position, in fractional units (VID_TO_POS(x))
26 * @ycur: Current Y position in pixels (0=top)
27 * @rows: Number of text rows
28 * @cols: Number of text columns
29 * @x_charsize: Character width in pixels
30 * @y_charsize: Character height in pixels
Simon Glassf2661782016-01-14 18:10:37 -070031 * @tab_width_frac: Tab width in fractional units
Heinrich Schuchardt662f3812018-09-19 21:31:48 +020032 * @xsize_frac: Width of the display in fractional units
Simon Glassc5b77d02016-01-14 18:10:39 -070033 * @xstart_frac: Left margin for the text console in fractional units
Heinrich Schuchardt662f3812018-09-19 21:31:48 +020034 * @last_ch: Last character written to the text console on this line
35 * @escape: TRUE if currently accumulating an ANSI escape sequence
36 * @escape_len: Length of accumulated escape sequence so far
37 * @col_saved: Saved X position, in fractional units (VID_TO_POS(x))
38 * @row_saved: Saved Y position in pixels (0=top)
39 * @escape_buf: Buffer to accumulate escape sequence
Simon Glass83510762016-01-18 19:52:17 -070040 */
41struct vidconsole_priv {
42 struct stdio_dev sdev;
Simon Glassf2661782016-01-14 18:10:37 -070043 int xcur_frac;
44 int ycur;
Simon Glass83510762016-01-18 19:52:17 -070045 int rows;
46 int cols;
Simon Glassf2661782016-01-14 18:10:37 -070047 int x_charsize;
48 int y_charsize;
49 int tab_width_frac;
50 int xsize_frac;
Simon Glassc5b77d02016-01-14 18:10:39 -070051 int xstart_frac;
Simon Glass58c733a2016-01-14 18:10:40 -070052 int last_ch;
Rob Clarka085aa12017-09-13 18:12:21 -040053 /*
54 * ANSI escape sequences are accumulated character by character,
55 * starting after the ESC char (0x1b) until the entire sequence
56 * is consumed at which point it is acted upon.
57 */
58 int escape;
59 int escape_len;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +020060 int row_saved;
61 int col_saved;
Rob Clarka085aa12017-09-13 18:12:21 -040062 char escape_buf[32];
Simon Glass83510762016-01-18 19:52:17 -070063};
64
65/**
Simon Glass0e38bd82023-01-06 08:52:32 -060066 * struct vidfont_info - information about a font
67 *
68 * @name: Font name, e.g. nimbus_sans_l_regular
69 */
70struct vidfont_info {
71 const char *name;
72};
73
74/**
Simon Glass648a4992023-06-01 10:22:45 -060075 * struct vidconsole_colour - Holds colour information
76 *
77 * @colour_fg: Foreground colour (pixel value)
78 * @colour_bg: Background colour (pixel value)
79 */
80struct vidconsole_colour {
81 u32 colour_fg;
82 u32 colour_bg;
83};
84
85/**
Simon Glassb828ed72023-06-01 10:22:46 -060086 * struct vidconsole_bbox - Bounding box of text
87 *
88 * This describes the bounding box of something, measured in pixels. The x0/y0
89 * pair is inclusive; the x1/y2 pair is exclusive, meaning that it is one pixel
90 * beyond the extent of the object
91 *
92 * @valid: Values are valid (bounding box is known)
93 * @x0: left x position, in pixels from left side
94 * @y0: top y position, in pixels from top
95 * @x1: right x position + 1
96 * @y1: botton y position + 1
97 */
98struct vidconsole_bbox {
99 bool valid;
100 int x0;
101 int y0;
102 int x1;
103 int y1;
104};
105
106/**
Simon Glass83510762016-01-18 19:52:17 -0700107 * struct vidconsole_ops - Video console operations
108 *
109 * These operations work on either an absolute console position (measured
110 * in pixels) or a text row number (measured in rows, where each row consists
111 * of an entire line of text - typically 16 pixels).
112 */
113struct vidconsole_ops {
114 /**
115 * putc_xy() - write a single character to a position
116 *
117 * @dev: Device to write to
Simon Glassf2661782016-01-14 18:10:37 -0700118 * @x_frac: Fractional pixel X position (0=left-most pixel) which
119 * is the X position multipled by VID_FRAC_DIV.
Simon Glass83510762016-01-18 19:52:17 -0700120 * @y: Pixel Y position (0=top-most pixel)
121 * @ch: Character to write
Simon Glassf2661782016-01-14 18:10:37 -0700122 * @return number of fractional pixels that the cursor should move,
123 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
124 * on error
Simon Glass83510762016-01-18 19:52:17 -0700125 */
Simon Glassf2661782016-01-14 18:10:37 -0700126 int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
Simon Glass83510762016-01-18 19:52:17 -0700127
128 /**
129 * move_rows() - Move text rows from one place to another
130 *
131 * @dev: Device to adjust
132 * @rowdst: Destination text row (0=top)
133 * @rowsrc: Source start text row
134 * @count: Number of text rows to move
135 * @return 0 if OK, -ve on error
136 */
137 int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
138 uint count);
139
140 /**
141 * set_row() - Set the colour of a text row
142 *
143 * Every pixel contained within the text row is adjusted
144 *
145 * @dev: Device to adjust
146 * @row: Text row to adjust (0=top)
147 * @clr: Raw colour (pixel value) to write to each pixel
148 * @return 0 if OK, -ve on error
149 */
150 int (*set_row)(struct udevice *dev, uint row, int clr);
Simon Glass58c733a2016-01-14 18:10:40 -0700151
152 /**
153 * entry_start() - Indicate that text entry is starting afresh
154 *
Simon Glass0e38bd82023-01-06 08:52:32 -0600155 * @dev: Device to adjust
156 * Returns: 0 on success, -ve on error
157 *
Simon Glass58c733a2016-01-14 18:10:40 -0700158 * Consoles which use proportional fonts need to track the position of
159 * each character output so that backspace will return to the correct
160 * place. This method signals to the console driver that a new entry
161 * line is being start (e.g. the user pressed return to start a new
162 * command). The driver can use this signal to empty its list of
163 * positions.
164 */
165 int (*entry_start)(struct udevice *dev);
Simon Glass7b9f7e42016-01-14 18:10:41 -0700166
167 /**
168 * backspace() - Handle erasing the last character
169 *
Simon Glass0e38bd82023-01-06 08:52:32 -0600170 * @dev: Device to adjust
171 * Returns: 0 on success, -ve on error
172 *
Simon Glass7b9f7e42016-01-14 18:10:41 -0700173 * With proportional fonts the vidconsole uclass cannot itself erase
174 * the previous character. This optional method will be called when
175 * a backspace is needed. The driver should erase the previous
176 * character and update the cursor position (xcur_frac, ycur) to the
177 * start of the previous character.
178 *
179 * If not implement, default behaviour will work for fixed-width
180 * characters.
181 */
182 int (*backspace)(struct udevice *dev);
Simon Glass0e38bd82023-01-06 08:52:32 -0600183
184 /**
185 * get_font() - Obtain information about a font (optional)
186 *
187 * @dev: Device to check
188 * @seq: Font number to query (0=first, 1=second, etc.)
189 * @info: Returns font information on success
190 * Returns: 0 on success, -ENOENT if no such font
191 */
192 int (*get_font)(struct udevice *dev, int seq,
193 struct vidfont_info *info);
194
195 /**
Dzmitry Sankouski4f6e3482023-03-07 13:21:15 +0300196 * get_font_size() - get the current font name and size
197 *
198 * @dev: vidconsole device
199 * @sizep: Place to put the font size (nominal height in pixels)
200 * Returns: Current font name
201 */
202 const char *(*get_font_size)(struct udevice *dev, uint *sizep);
203
204 /**
Simon Glass0e38bd82023-01-06 08:52:32 -0600205 * select_font() - Select a particular font by name / size
206 *
207 * @dev: Device to adjust
208 * @name: Font name to use (NULL to use default)
209 * @size: Font size to use (0 to use default)
210 * Returns: 0 on success, -ENOENT if no such font
211 */
212 int (*select_font)(struct udevice *dev, const char *name, uint size);
Simon Glassb828ed72023-06-01 10:22:46 -0600213
214 /**
215 * measure() - Measure the bounds of some text
216 *
217 * @dev: Device to adjust
218 * @name: Font name to use (NULL to use default)
219 * @size: Font size to use (0 to use default)
220 * @text: Text to measure
221 * @bbox: Returns bounding box of text, assuming it is positioned
222 * at 0,0
223 * Returns: 0 on success, -ENOENT if no such font
224 */
225 int (*measure)(struct udevice *dev, const char *name, uint size,
226 const char *text, struct vidconsole_bbox *bbox);
Simon Glass83510762016-01-18 19:52:17 -0700227};
228
229/* Get a pointer to the driver operations for a video console device */
230#define vidconsole_get_ops(dev) ((struct vidconsole_ops *)(dev)->driver->ops)
231
232/**
Simon Glass0e38bd82023-01-06 08:52:32 -0600233 * vidconsole_get_font() - Obtain information about a font
234 *
235 * @dev: Device to check
236 * @seq: Font number to query (0=first, 1=second, etc.)
237 * @info: Returns font information on success
238 * Returns: 0 on success, -ENOENT if no such font, -ENOSYS if there is no such
239 * method
240 */
241int vidconsole_get_font(struct udevice *dev, int seq,
242 struct vidfont_info *info);
243
244/**
245 * vidconsole_select_font() - Select a particular font by name / size
246 *
247 * @dev: Device to adjust
248 * @name: Font name to use (NULL to use default)
249 * @size: Font size to use (0 to use default)
250 */
251int vidconsole_select_font(struct udevice *dev, const char *name, uint size);
252
Simon Glassb828ed72023-06-01 10:22:46 -0600253/*
254 * vidconsole_measure() - Measuring the bounding box of some text
255 *
256 * @dev: Console device to use
257 * @name: Font name, NULL for default
258 * @size: Font size, ignored if @name is NULL
259 * @text: Text to measure
260 * @bbox: Returns nounding box of text
261 * Returns: 0 if OK, -ve on error
262 */
263int vidconsole_measure(struct udevice *dev, const char *name, uint size,
264 const char *text, struct vidconsole_bbox *bbox);
265
Simon Glass0e38bd82023-01-06 08:52:32 -0600266/**
Simon Glass648a4992023-06-01 10:22:45 -0600267 * vidconsole_push_colour() - Temporarily change the font colour
268 *
269 * @dev: Device to adjust
270 * @fg: Foreground colour to select
271 * @bg: Background colour to select
272 * @old: Place to store the current colour, so it can be restored
273 */
274void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
275 enum colour_idx bg, struct vidconsole_colour *old);
276
277/**
278 * vidconsole_pop_colour() - Restore the original colour
279 *
280 * @dev: Device to adjust
281 * @old: Old colour to be restored
282 */
283void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old);
284
285/**
Simon Glass83510762016-01-18 19:52:17 -0700286 * vidconsole_putc_xy() - write a single character to a position
287 *
288 * @dev: Device to write to
Simon Glassf2661782016-01-14 18:10:37 -0700289 * @x_frac: Fractional pixel X position (0=left-most pixel) which
290 * is the X position multipled by VID_FRAC_DIV.
Simon Glass83510762016-01-18 19:52:17 -0700291 * @y: Pixel Y position (0=top-most pixel)
292 * @ch: Character to write
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100293 * Return: number of fractional pixels that the cursor should move,
Simon Glassf2661782016-01-14 18:10:37 -0700294 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
295 * on error
Simon Glass83510762016-01-18 19:52:17 -0700296 */
297int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
298
299/**
300 * vidconsole_move_rows() - Move text rows from one place to another
301 *
302 * @dev: Device to adjust
303 * @rowdst: Destination text row (0=top)
304 * @rowsrc: Source start text row
305 * @count: Number of text rows to move
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100306 * Return: 0 if OK, -ve on error
Simon Glass83510762016-01-18 19:52:17 -0700307 */
308int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
309 uint count);
310
311/**
312 * vidconsole_set_row() - Set the colour of a text row
313 *
314 * Every pixel contained within the text row is adjusted
315 *
316 * @dev: Device to adjust
317 * @row: Text row to adjust (0=top)
318 * @clr: Raw colour (pixel value) to write to each pixel
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100319 * Return: 0 if OK, -ve on error
Simon Glass83510762016-01-18 19:52:17 -0700320 */
321int vidconsole_set_row(struct udevice *dev, uint row, int clr);
322
323/**
324 * vidconsole_put_char() - Output a character to the current console position
325 *
326 * Outputs a character to the console and advances the cursor. This function
327 * handles wrapping to new lines and scrolling the console. Special
328 * characters are handled also: \n, \r, \b and \t.
329 *
330 * The device always starts with the cursor at position 0,0 (top left). It
331 * can be adjusted manually using vidconsole_position_cursor().
332 *
333 * @dev: Device to adjust
334 * @ch: Character to write
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100335 * Return: 0 if OK, -ve on error
Simon Glass83510762016-01-18 19:52:17 -0700336 */
337int vidconsole_put_char(struct udevice *dev, char ch);
338
339/**
Marek Vasute63168a2019-05-17 20:22:31 +0200340 * vidconsole_put_string() - Output a string to the current console position
341 *
342 * Outputs a string to the console and advances the cursor. This function
343 * handles wrapping to new lines and scrolling the console. Special
344 * characters are handled also: \n, \r, \b and \t.
345 *
346 * The device always starts with the cursor at position 0,0 (top left). It
347 * can be adjusted manually using vidconsole_position_cursor().
348 *
349 * @dev: Device to adjust
350 * @str: String to write
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100351 * Return: 0 if OK, -ve on error
Marek Vasute63168a2019-05-17 20:22:31 +0200352 */
353int vidconsole_put_string(struct udevice *dev, const char *str);
354
355/**
Simon Glass83510762016-01-18 19:52:17 -0700356 * vidconsole_position_cursor() - Move the text cursor
357 *
358 * @dev: Device to adjust
359 * @col: New cursor text column
360 * @row: New cursor text row
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100361 * Return: 0 if OK, -ve on error
Simon Glass83510762016-01-18 19:52:17 -0700362 */
363void vidconsole_position_cursor(struct udevice *dev, unsigned col,
364 unsigned row);
365
Simon Glass6b6dc0d2022-10-06 08:36:04 -0600366/**
Simon Glassa76b60f2023-03-10 12:47:21 -0800367 * vidconsole_clear_and_reset() - Clear the console and reset the cursor
368 *
369 * The cursor is placed at the start of the console
370 *
371 * @dev: vidconsole device to adjust
372 */
373int vidconsole_clear_and_reset(struct udevice *dev);
374
375/**
Simon Glass6b6dc0d2022-10-06 08:36:04 -0600376 * vidconsole_set_cursor_pos() - set cursor position
377 *
378 * The cursor is set to the new position and the start-of-line information is
379 * updated to the same position, so that a newline will return to @x
380 *
381 * @dev: video console device to update
382 * @x: x position from left in pixels
383 * @y: y position from top in pixels
384 */
385void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y);
386
Simon Glass57a847c2022-10-06 08:36:14 -0600387/**
388 * vidconsole_list_fonts() - List the available fonts
389 *
Simon Glass0e38bd82023-01-06 08:52:32 -0600390 * @dev: vidconsole device to check
Simon Glass57a847c2022-10-06 08:36:14 -0600391 *
Simon Glass0e38bd82023-01-06 08:52:32 -0600392 * This shows a list of fonts known by this vidconsole. The list is displayed on
393 * the console (not necessarily @dev but probably)
Simon Glass57a847c2022-10-06 08:36:14 -0600394 */
Simon Glass0e38bd82023-01-06 08:52:32 -0600395void vidconsole_list_fonts(struct udevice *dev);
Simon Glass57a847c2022-10-06 08:36:14 -0600396
Simon Glass430e1672022-10-06 08:36:16 -0600397/**
Simon Glass0e38bd82023-01-06 08:52:32 -0600398 * vidconsole_get_font_size() - get the current font name and size
Simon Glass430e1672022-10-06 08:36:16 -0600399 *
400 * @dev: vidconsole device
401 * @sizep: Place to put the font size (nominal height in pixels)
Dzmitry Sankouski4f6e3482023-03-07 13:21:15 +0300402 * @name: pointer to font name, a placeholder for result
403 * Return: 0 if OK, -ENOSYS if not implemented in driver
Simon Glass430e1672022-10-06 08:36:16 -0600404 */
Dzmitry Sankouski4f6e3482023-03-07 13:21:15 +0300405int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep);
Simon Glass430e1672022-10-06 08:36:16 -0600406
Simon Glass8c0b5d22020-07-02 21:12:23 -0600407#ifdef CONFIG_VIDEO_COPY
408/**
409 * vidconsole_sync_copy() - Sync back to the copy framebuffer
410 *
411 * This ensures that the copy framebuffer has the same data as the framebuffer
412 * for a particular region. It should be called after the framebuffer is updated
413 *
414 * @from and @to can be in either order. The region between them is synced.
415 *
416 * @dev: Vidconsole device being updated
417 * @from: Start/end address within the framebuffer (->fb)
418 * @to: Other address within the frame buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100419 * Return: 0 if OK, -EFAULT if the start address is before the start of the
Simon Glass8c0b5d22020-07-02 21:12:23 -0600420 * frame buffer start
421 */
422int vidconsole_sync_copy(struct udevice *dev, void *from, void *to);
423
424/**
425 * vidconsole_memmove() - Perform a memmove() within the frame buffer
426 *
427 * This handles a memmove(), e.g. for scrolling. It also updates the copy
428 * framebuffer.
429 *
430 * @dev: Vidconsole device being updated
431 * @dst: Destination address within the framebuffer (->fb)
432 * @src: Source address within the framebuffer (->fb)
433 * @size: Number of bytes to transfer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100434 * Return: 0 if OK, -EFAULT if the start address is before the start of the
Simon Glass8c0b5d22020-07-02 21:12:23 -0600435 * frame buffer start
436 */
437int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
438 int size);
439#else
Dzmitry Sankouski31547252023-03-07 13:21:11 +0300440
441#include <string.h>
442
Simon Glass8c0b5d22020-07-02 21:12:23 -0600443static inline int vidconsole_sync_copy(struct udevice *dev, void *from,
444 void *to)
445{
446 return 0;
447}
448
449static inline int vidconsole_memmove(struct udevice *dev, void *dst,
450 const void *src, int size)
451{
452 memmove(dst, src, size);
453
454 return 0;
455}
456
457#endif
458
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100459#endif