blob: 5921767fbf069cc29d3cae3639cbfcb49d840831 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass84c7fb32016-01-18 19:52:17 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass84c7fb32016-01-18 19:52:17 -07004 */
5
6#ifndef __video_console_h
7#define __video_console_h
8
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +01009#include <video.h>
10
Simon Glassbac91502020-07-02 21:12:18 -060011struct video_priv;
12
Simon Glass52c10c52016-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
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +010018/*
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +010019 * The 16 colors supported by the console
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +010020 */
21enum color_idx {
22 VID_BLACK = 0,
23 VID_RED,
24 VID_GREEN,
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +010025 VID_BROWN,
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +010026 VID_BLUE,
27 VID_MAGENTA,
28 VID_CYAN,
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +010029 VID_LIGHT_GRAY,
30 VID_GRAY,
31 VID_LIGHT_RED,
32 VID_LIGTH_GREEN,
33 VID_YELLOW,
34 VID_LIGHT_BLUE,
35 VID_LIGHT_MAGENTA,
36 VID_LIGHT_CYAN,
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +010037 VID_WHITE,
38
39 VID_COLOR_COUNT
40};
41
Simon Glass84c7fb32016-01-18 19:52:17 -070042/**
43 * struct vidconsole_priv - uclass-private data about a console device
44 *
Simon Glass52c10c52016-01-14 18:10:37 -070045 * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
46 * method. Drivers may set up @xstart_frac if desired.
47 *
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +020048 * @sdev: stdio device, acting as an output sink
49 * @xcur_frac: Current X position, in fractional units (VID_TO_POS(x))
50 * @ycur: Current Y position in pixels (0=top)
51 * @rows: Number of text rows
52 * @cols: Number of text columns
53 * @x_charsize: Character width in pixels
54 * @y_charsize: Character height in pixels
Simon Glass52c10c52016-01-14 18:10:37 -070055 * @tab_width_frac: Tab width in fractional units
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +020056 * @xsize_frac: Width of the display in fractional units
Simon Glassa74451d2016-01-14 18:10:39 -070057 * @xstart_frac: Left margin for the text console in fractional units
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +020058 * @last_ch: Last character written to the text console on this line
59 * @escape: TRUE if currently accumulating an ANSI escape sequence
60 * @escape_len: Length of accumulated escape sequence so far
61 * @col_saved: Saved X position, in fractional units (VID_TO_POS(x))
62 * @row_saved: Saved Y position in pixels (0=top)
63 * @escape_buf: Buffer to accumulate escape sequence
Simon Glass84c7fb32016-01-18 19:52:17 -070064 */
65struct vidconsole_priv {
66 struct stdio_dev sdev;
Simon Glass52c10c52016-01-14 18:10:37 -070067 int xcur_frac;
68 int ycur;
Simon Glass84c7fb32016-01-18 19:52:17 -070069 int rows;
70 int cols;
Simon Glass52c10c52016-01-14 18:10:37 -070071 int x_charsize;
72 int y_charsize;
73 int tab_width_frac;
74 int xsize_frac;
Simon Glassa74451d2016-01-14 18:10:39 -070075 int xstart_frac;
Simon Glassafee7432016-01-14 18:10:40 -070076 int last_ch;
Rob Clark06e7a0d2017-09-13 18:12:21 -040077 /*
78 * ANSI escape sequences are accumulated character by character,
79 * starting after the ESC char (0x1b) until the entire sequence
80 * is consumed at which point it is acted upon.
81 */
82 int escape;
83 int escape_len;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +020084 int row_saved;
85 int col_saved;
Rob Clark06e7a0d2017-09-13 18:12:21 -040086 char escape_buf[32];
Simon Glass84c7fb32016-01-18 19:52:17 -070087};
88
89/**
90 * struct vidconsole_ops - Video console operations
91 *
92 * These operations work on either an absolute console position (measured
93 * in pixels) or a text row number (measured in rows, where each row consists
94 * of an entire line of text - typically 16 pixels).
95 */
96struct vidconsole_ops {
97 /**
98 * putc_xy() - write a single character to a position
99 *
100 * @dev: Device to write to
Simon Glass52c10c52016-01-14 18:10:37 -0700101 * @x_frac: Fractional pixel X position (0=left-most pixel) which
102 * is the X position multipled by VID_FRAC_DIV.
Simon Glass84c7fb32016-01-18 19:52:17 -0700103 * @y: Pixel Y position (0=top-most pixel)
104 * @ch: Character to write
Simon Glass52c10c52016-01-14 18:10:37 -0700105 * @return number of fractional pixels that the cursor should move,
106 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
107 * on error
Simon Glass84c7fb32016-01-18 19:52:17 -0700108 */
Simon Glass52c10c52016-01-14 18:10:37 -0700109 int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
Simon Glass84c7fb32016-01-18 19:52:17 -0700110
111 /**
112 * move_rows() - Move text rows from one place to another
113 *
114 * @dev: Device to adjust
115 * @rowdst: Destination text row (0=top)
116 * @rowsrc: Source start text row
117 * @count: Number of text rows to move
118 * @return 0 if OK, -ve on error
119 */
120 int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
121 uint count);
122
123 /**
124 * set_row() - Set the colour of a text row
125 *
126 * Every pixel contained within the text row is adjusted
127 *
128 * @dev: Device to adjust
129 * @row: Text row to adjust (0=top)
130 * @clr: Raw colour (pixel value) to write to each pixel
131 * @return 0 if OK, -ve on error
132 */
133 int (*set_row)(struct udevice *dev, uint row, int clr);
Simon Glassafee7432016-01-14 18:10:40 -0700134
135 /**
136 * entry_start() - Indicate that text entry is starting afresh
137 *
138 * Consoles which use proportional fonts need to track the position of
139 * each character output so that backspace will return to the correct
140 * place. This method signals to the console driver that a new entry
141 * line is being start (e.g. the user pressed return to start a new
142 * command). The driver can use this signal to empty its list of
143 * positions.
144 */
145 int (*entry_start)(struct udevice *dev);
Simon Glass33bd3b62016-01-14 18:10:41 -0700146
147 /**
148 * backspace() - Handle erasing the last character
149 *
150 * With proportional fonts the vidconsole uclass cannot itself erase
151 * the previous character. This optional method will be called when
152 * a backspace is needed. The driver should erase the previous
153 * character and update the cursor position (xcur_frac, ycur) to the
154 * start of the previous character.
155 *
156 * If not implement, default behaviour will work for fixed-width
157 * characters.
158 */
159 int (*backspace)(struct udevice *dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700160};
161
162/* Get a pointer to the driver operations for a video console device */
163#define vidconsole_get_ops(dev) ((struct vidconsole_ops *)(dev)->driver->ops)
164
165/**
166 * vidconsole_putc_xy() - write a single character to a position
167 *
168 * @dev: Device to write to
Simon Glass52c10c52016-01-14 18:10:37 -0700169 * @x_frac: Fractional pixel X position (0=left-most pixel) which
170 * is the X position multipled by VID_FRAC_DIV.
Simon Glass84c7fb32016-01-18 19:52:17 -0700171 * @y: Pixel Y position (0=top-most pixel)
172 * @ch: Character to write
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100173 * Return: number of fractional pixels that the cursor should move,
Simon Glass52c10c52016-01-14 18:10:37 -0700174 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
175 * on error
Simon Glass84c7fb32016-01-18 19:52:17 -0700176 */
177int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
178
179/**
180 * vidconsole_move_rows() - Move text rows from one place to another
181 *
182 * @dev: Device to adjust
183 * @rowdst: Destination text row (0=top)
184 * @rowsrc: Source start text row
185 * @count: Number of text rows to move
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100186 * Return: 0 if OK, -ve on error
Simon Glass84c7fb32016-01-18 19:52:17 -0700187 */
188int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
189 uint count);
190
191/**
192 * vidconsole_set_row() - Set the colour of a text row
193 *
194 * Every pixel contained within the text row is adjusted
195 *
196 * @dev: Device to adjust
197 * @row: Text row to adjust (0=top)
198 * @clr: Raw colour (pixel value) to write to each pixel
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100199 * Return: 0 if OK, -ve on error
Simon Glass84c7fb32016-01-18 19:52:17 -0700200 */
201int vidconsole_set_row(struct udevice *dev, uint row, int clr);
202
203/**
204 * vidconsole_put_char() - Output a character to the current console position
205 *
206 * Outputs a character to the console and advances the cursor. This function
207 * handles wrapping to new lines and scrolling the console. Special
208 * characters are handled also: \n, \r, \b and \t.
209 *
210 * The device always starts with the cursor at position 0,0 (top left). It
211 * can be adjusted manually using vidconsole_position_cursor().
212 *
213 * @dev: Device to adjust
214 * @ch: Character to write
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100215 * Return: 0 if OK, -ve on error
Simon Glass84c7fb32016-01-18 19:52:17 -0700216 */
217int vidconsole_put_char(struct udevice *dev, char ch);
218
219/**
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200220 * vidconsole_put_string() - Output a string to the current console position
221 *
222 * Outputs a string to the console and advances the cursor. This function
223 * handles wrapping to new lines and scrolling the console. Special
224 * characters are handled also: \n, \r, \b and \t.
225 *
226 * The device always starts with the cursor at position 0,0 (top left). It
227 * can be adjusted manually using vidconsole_position_cursor().
228 *
229 * @dev: Device to adjust
230 * @str: String to write
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100231 * Return: 0 if OK, -ve on error
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200232 */
233int vidconsole_put_string(struct udevice *dev, const char *str);
234
235/**
Simon Glass84c7fb32016-01-18 19:52:17 -0700236 * vidconsole_position_cursor() - Move the text cursor
237 *
238 * @dev: Device to adjust
239 * @col: New cursor text column
240 * @row: New cursor text row
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100241 * Return: 0 if OK, -ve on error
Simon Glass84c7fb32016-01-18 19:52:17 -0700242 */
243void vidconsole_position_cursor(struct udevice *dev, unsigned col,
244 unsigned row);
245
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100246/**
247 * vid_console_color() - convert a color code to a pixel's internal
248 * representation
249 *
250 * The caller has to guarantee that the color index is less than
251 * VID_COLOR_COUNT.
252 *
253 * @priv private data of the console device
254 * @idx color index
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100255 * Return: color value
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100256 */
257u32 vid_console_color(struct video_priv *priv, unsigned int idx);
258
Simon Glass31a7e232020-07-02 21:12:23 -0600259#ifdef CONFIG_VIDEO_COPY
260/**
261 * vidconsole_sync_copy() - Sync back to the copy framebuffer
262 *
263 * This ensures that the copy framebuffer has the same data as the framebuffer
264 * for a particular region. It should be called after the framebuffer is updated
265 *
266 * @from and @to can be in either order. The region between them is synced.
267 *
268 * @dev: Vidconsole device being updated
269 * @from: Start/end address within the framebuffer (->fb)
270 * @to: Other address within the frame buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100271 * Return: 0 if OK, -EFAULT if the start address is before the start of the
Simon Glass31a7e232020-07-02 21:12:23 -0600272 * frame buffer start
273 */
274int vidconsole_sync_copy(struct udevice *dev, void *from, void *to);
275
276/**
277 * vidconsole_memmove() - Perform a memmove() within the frame buffer
278 *
279 * This handles a memmove(), e.g. for scrolling. It also updates the copy
280 * framebuffer.
281 *
282 * @dev: Vidconsole device being updated
283 * @dst: Destination address within the framebuffer (->fb)
284 * @src: Source address within the framebuffer (->fb)
285 * @size: Number of bytes to transfer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100286 * Return: 0 if OK, -EFAULT if the start address is before the start of the
Simon Glass31a7e232020-07-02 21:12:23 -0600287 * frame buffer start
288 */
289int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
290 int size);
291#else
292static inline int vidconsole_sync_copy(struct udevice *dev, void *from,
293 void *to)
294{
295 return 0;
296}
297
298static inline int vidconsole_memmove(struct udevice *dev, void *dst,
299 const void *src, int size)
300{
301 memmove(dst, src, size);
302
303 return 0;
304}
305
306#endif
307
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100308#endif