xref: /openbmc/u-boot/include/video_console.h (revision b592936d357f6c648f59ae0e3159149df3a942fb)
183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
283510766SSimon Glass /*
383510766SSimon Glass  * Copyright (c) 2015 Google, Inc
483510766SSimon Glass  */
583510766SSimon Glass 
683510766SSimon Glass #ifndef __video_console_h
783510766SSimon Glass #define __video_console_h
883510766SSimon Glass 
95c30fbb8SHeinrich Schuchardt #include <video.h>
105c30fbb8SHeinrich Schuchardt 
11f2661786SSimon Glass #define VID_FRAC_DIV	256
12f2661786SSimon Glass 
13f2661786SSimon Glass #define VID_TO_PIXEL(x)	((x) / VID_FRAC_DIV)
14f2661786SSimon Glass #define VID_TO_POS(x)	((x) * VID_FRAC_DIV)
15f2661786SSimon Glass 
165c30fbb8SHeinrich Schuchardt /*
179ffa4d12SHeinrich Schuchardt  * The 16 colors supported by the console
185c30fbb8SHeinrich Schuchardt  */
195c30fbb8SHeinrich Schuchardt enum color_idx {
205c30fbb8SHeinrich Schuchardt 	VID_BLACK = 0,
215c30fbb8SHeinrich Schuchardt 	VID_RED,
225c30fbb8SHeinrich Schuchardt 	VID_GREEN,
239ffa4d12SHeinrich Schuchardt 	VID_BROWN,
245c30fbb8SHeinrich Schuchardt 	VID_BLUE,
255c30fbb8SHeinrich Schuchardt 	VID_MAGENTA,
265c30fbb8SHeinrich Schuchardt 	VID_CYAN,
279ffa4d12SHeinrich Schuchardt 	VID_LIGHT_GRAY,
289ffa4d12SHeinrich Schuchardt 	VID_GRAY,
299ffa4d12SHeinrich Schuchardt 	VID_LIGHT_RED,
309ffa4d12SHeinrich Schuchardt 	VID_LIGTH_GREEN,
319ffa4d12SHeinrich Schuchardt 	VID_YELLOW,
329ffa4d12SHeinrich Schuchardt 	VID_LIGHT_BLUE,
339ffa4d12SHeinrich Schuchardt 	VID_LIGHT_MAGENTA,
349ffa4d12SHeinrich Schuchardt 	VID_LIGHT_CYAN,
355c30fbb8SHeinrich Schuchardt 	VID_WHITE,
365c30fbb8SHeinrich Schuchardt 
375c30fbb8SHeinrich Schuchardt 	VID_COLOR_COUNT
385c30fbb8SHeinrich Schuchardt };
395c30fbb8SHeinrich Schuchardt 
4083510766SSimon Glass /**
4183510766SSimon Glass  * struct vidconsole_priv - uclass-private data about a console device
4283510766SSimon Glass  *
43f2661786SSimon Glass  * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
44f2661786SSimon Glass  * method. Drivers may set up @xstart_frac if desired.
45f2661786SSimon Glass  *
4683510766SSimon Glass  * @sdev:		stdio device, acting as an output sink
47f2661786SSimon Glass  * @xcur_frac:		Current X position, in fractional units (VID_TO_POS(x))
485cb4860fSHeinrich Schuchardt  * @ycur:		Current Y position in pixels (0=top)
4983510766SSimon Glass  * @rows:		Number of text rows
5083510766SSimon Glass  * @cols:		Number of text columns
51f2661786SSimon Glass  * @x_charsize:		Character width in pixels
52f2661786SSimon Glass  * @y_charsize:		Character height in pixels
53f2661786SSimon Glass  * @tab_width_frac:	Tab width in fractional units
54f2661786SSimon Glass  * @xsize_frac:		Width of the display in fractional units
55c5b77d01SSimon Glass  * @xstart_frac:	Left margin for the text console in fractional units
5658c733a7SSimon Glass  * @last_ch:		Last character written to the text console on this line
57a085aa1fSRob Clark  * @escape:		TRUE if currently accumulating an ANSI escape sequence
58a085aa1fSRob Clark  * @escape_len:		Length of accumulated escape sequence so far
59*662f381aSHeinrich Schuchardt  * @col_saved:		Saved X position, in fractional units (VID_TO_POS(x))
60*662f381aSHeinrich Schuchardt  * @row_saved:		Saved Y position in pixels (0=top)
61a085aa1fSRob Clark  * @escape_buf:		Buffer to accumulate escape sequence
6283510766SSimon Glass  */
6383510766SSimon Glass struct vidconsole_priv {
6483510766SSimon Glass 	struct stdio_dev sdev;
65f2661786SSimon Glass 	int xcur_frac;
66f2661786SSimon Glass 	int ycur;
6783510766SSimon Glass 	int rows;
6883510766SSimon Glass 	int cols;
69f2661786SSimon Glass 	int x_charsize;
70f2661786SSimon Glass 	int y_charsize;
71f2661786SSimon Glass 	int tab_width_frac;
72f2661786SSimon Glass 	int xsize_frac;
73c5b77d01SSimon Glass 	int xstart_frac;
7458c733a7SSimon Glass 	int last_ch;
75a085aa1fSRob Clark 	/*
76a085aa1fSRob Clark 	 * ANSI escape sequences are accumulated character by character,
77a085aa1fSRob Clark 	 * starting after the ESC char (0x1b) until the entire sequence
78a085aa1fSRob Clark 	 * is consumed at which point it is acted upon.
79a085aa1fSRob Clark 	 */
80a085aa1fSRob Clark 	int escape;
81a085aa1fSRob Clark 	int escape_len;
82*662f381aSHeinrich Schuchardt 	int row_saved;
83*662f381aSHeinrich Schuchardt 	int col_saved;
84a085aa1fSRob Clark 	char escape_buf[32];
8583510766SSimon Glass };
8683510766SSimon Glass 
8783510766SSimon Glass /**
8883510766SSimon Glass  * struct vidconsole_ops - Video console operations
8983510766SSimon Glass  *
9083510766SSimon Glass  * These operations work on either an absolute console position (measured
9183510766SSimon Glass  * in pixels) or a text row number (measured in rows, where each row consists
9283510766SSimon Glass  * of an entire line of text - typically 16 pixels).
9383510766SSimon Glass  */
9483510766SSimon Glass struct vidconsole_ops {
9583510766SSimon Glass 	/**
9683510766SSimon Glass 	 * putc_xy() - write a single character to a position
9783510766SSimon Glass 	 *
9883510766SSimon Glass 	 * @dev:	Device to write to
99f2661786SSimon Glass 	 * @x_frac:	Fractional pixel X position (0=left-most pixel) which
100f2661786SSimon Glass 	 *		is the X position multipled by VID_FRAC_DIV.
10183510766SSimon Glass 	 * @y:		Pixel Y position (0=top-most pixel)
10283510766SSimon Glass 	 * @ch:		Character to write
103f2661786SSimon Glass 	 * @return number of fractional pixels that the cursor should move,
104f2661786SSimon Glass 	 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
105f2661786SSimon Glass 	 * on error
10683510766SSimon Glass 	 */
107f2661786SSimon Glass 	int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
10883510766SSimon Glass 
10983510766SSimon Glass 	/**
11083510766SSimon Glass 	 * move_rows() - Move text rows from one place to another
11183510766SSimon Glass 	 *
11283510766SSimon Glass 	 * @dev:	Device to adjust
11383510766SSimon Glass 	 * @rowdst:	Destination text row (0=top)
11483510766SSimon Glass 	 * @rowsrc:	Source start text row
11583510766SSimon Glass 	 * @count:	Number of text rows to move
11683510766SSimon Glass 	 * @return 0 if OK, -ve on error
11783510766SSimon Glass 	 */
11883510766SSimon Glass 	int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
11983510766SSimon Glass 			  uint count);
12083510766SSimon Glass 
12183510766SSimon Glass 	/**
12283510766SSimon Glass 	 * set_row() - Set the colour of a text row
12383510766SSimon Glass 	 *
12483510766SSimon Glass 	 * Every pixel contained within the text row is adjusted
12583510766SSimon Glass 	 *
12683510766SSimon Glass 	 * @dev:	Device to adjust
12783510766SSimon Glass 	 * @row:	Text row to adjust (0=top)
12883510766SSimon Glass 	 * @clr:	Raw colour (pixel value) to write to each pixel
12983510766SSimon Glass 	 * @return 0 if OK, -ve on error
13083510766SSimon Glass 	 */
13183510766SSimon Glass 	int (*set_row)(struct udevice *dev, uint row, int clr);
13258c733a7SSimon Glass 
13358c733a7SSimon Glass 	/**
13458c733a7SSimon Glass 	 * entry_start() - Indicate that text entry is starting afresh
13558c733a7SSimon Glass 	 *
13658c733a7SSimon Glass 	 * Consoles which use proportional fonts need to track the position of
13758c733a7SSimon Glass 	 * each character output so that backspace will return to the correct
13858c733a7SSimon Glass 	 * place. This method signals to the console driver that a new entry
13958c733a7SSimon Glass 	 * line is being start (e.g. the user pressed return to start a new
14058c733a7SSimon Glass 	 * command). The driver can use this signal to empty its list of
14158c733a7SSimon Glass 	 * positions.
14258c733a7SSimon Glass 	 */
14358c733a7SSimon Glass 	int (*entry_start)(struct udevice *dev);
1447b9f7e44SSimon Glass 
1457b9f7e44SSimon Glass 	/**
1467b9f7e44SSimon Glass 	 * backspace() - Handle erasing the last character
1477b9f7e44SSimon Glass 	 *
1487b9f7e44SSimon Glass 	 * With proportional fonts the vidconsole uclass cannot itself erase
1497b9f7e44SSimon Glass 	 * the previous character. This optional method will be called when
1507b9f7e44SSimon Glass 	 * a backspace is needed. The driver should erase the previous
1517b9f7e44SSimon Glass 	 * character and update the cursor position (xcur_frac, ycur) to the
1527b9f7e44SSimon Glass 	 * start of the previous character.
1537b9f7e44SSimon Glass 	 *
1547b9f7e44SSimon Glass 	 * If not implement, default behaviour will work for fixed-width
1557b9f7e44SSimon Glass 	 * characters.
1567b9f7e44SSimon Glass 	 */
1577b9f7e44SSimon Glass 	int (*backspace)(struct udevice *dev);
15883510766SSimon Glass };
15983510766SSimon Glass 
16083510766SSimon Glass /* Get a pointer to the driver operations for a video console device */
16183510766SSimon Glass #define vidconsole_get_ops(dev)  ((struct vidconsole_ops *)(dev)->driver->ops)
16283510766SSimon Glass 
16383510766SSimon Glass /**
16483510766SSimon Glass  * vidconsole_putc_xy() - write a single character to a position
16583510766SSimon Glass  *
16683510766SSimon Glass  * @dev:	Device to write to
167f2661786SSimon Glass  * @x_frac:	Fractional pixel X position (0=left-most pixel) which
168f2661786SSimon Glass  *		is the X position multipled by VID_FRAC_DIV.
16983510766SSimon Glass  * @y:		Pixel Y position (0=top-most pixel)
17083510766SSimon Glass  * @ch:		Character to write
171f2661786SSimon Glass  * @return number of fractional pixels that the cursor should move,
172f2661786SSimon Glass  * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
173f2661786SSimon Glass  * on error
17483510766SSimon Glass  */
17583510766SSimon Glass int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
17683510766SSimon Glass 
17783510766SSimon Glass /**
17883510766SSimon Glass  * vidconsole_move_rows() - Move text rows from one place to another
17983510766SSimon Glass  *
18083510766SSimon Glass  * @dev:	Device to adjust
18183510766SSimon Glass  * @rowdst:	Destination text row (0=top)
18283510766SSimon Glass  * @rowsrc:	Source start text row
18383510766SSimon Glass  * @count:	Number of text rows to move
18483510766SSimon Glass  * @return 0 if OK, -ve on error
18583510766SSimon Glass  */
18683510766SSimon Glass int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
18783510766SSimon Glass 			 uint count);
18883510766SSimon Glass 
18983510766SSimon Glass /**
19083510766SSimon Glass  * vidconsole_set_row() - Set the colour of a text row
19183510766SSimon Glass  *
19283510766SSimon Glass  * Every pixel contained within the text row is adjusted
19383510766SSimon Glass  *
19483510766SSimon Glass  * @dev:	Device to adjust
19583510766SSimon Glass  * @row:	Text row to adjust (0=top)
19683510766SSimon Glass  * @clr:	Raw colour (pixel value) to write to each pixel
19783510766SSimon Glass  * @return 0 if OK, -ve on error
19883510766SSimon Glass  */
19983510766SSimon Glass int vidconsole_set_row(struct udevice *dev, uint row, int clr);
20083510766SSimon Glass 
20183510766SSimon Glass /**
20283510766SSimon Glass  * vidconsole_put_char() - Output a character to the current console position
20383510766SSimon Glass  *
20483510766SSimon Glass  * Outputs a character to the console and advances the cursor. This function
20583510766SSimon Glass  * handles wrapping to new lines and scrolling the console. Special
20683510766SSimon Glass  * characters are handled also: \n, \r, \b and \t.
20783510766SSimon Glass  *
20883510766SSimon Glass  * The device always starts with the cursor at position 0,0 (top left). It
20983510766SSimon Glass  * can be adjusted manually using vidconsole_position_cursor().
21083510766SSimon Glass  *
21183510766SSimon Glass  * @dev:	Device to adjust
21283510766SSimon Glass  * @ch:		Character to write
21383510766SSimon Glass  * @return 0 if OK, -ve on error
21483510766SSimon Glass  */
21583510766SSimon Glass int vidconsole_put_char(struct udevice *dev, char ch);
21683510766SSimon Glass 
21783510766SSimon Glass /**
21883510766SSimon Glass  * vidconsole_position_cursor() - Move the text cursor
21983510766SSimon Glass  *
22083510766SSimon Glass  * @dev:	Device to adjust
22183510766SSimon Glass  * @col:	New cursor text column
22283510766SSimon Glass  * @row:	New cursor text row
22383510766SSimon Glass  * @return 0 if OK, -ve on error
22483510766SSimon Glass  */
22583510766SSimon Glass void vidconsole_position_cursor(struct udevice *dev, unsigned col,
22683510766SSimon Glass 				unsigned row);
22783510766SSimon Glass 
2285c30fbb8SHeinrich Schuchardt #ifdef CONFIG_DM_VIDEO
2295c30fbb8SHeinrich Schuchardt 
2305c30fbb8SHeinrich Schuchardt /**
2315c30fbb8SHeinrich Schuchardt  * vid_console_color() - convert a color code to a pixel's internal
2325c30fbb8SHeinrich Schuchardt  * representation
2335c30fbb8SHeinrich Schuchardt  *
2345c30fbb8SHeinrich Schuchardt  * The caller has to guarantee that the color index is less than
2355c30fbb8SHeinrich Schuchardt  * VID_COLOR_COUNT.
2365c30fbb8SHeinrich Schuchardt  *
2375c30fbb8SHeinrich Schuchardt  * @priv	private data of the console device
2385c30fbb8SHeinrich Schuchardt  * @idx		color index
2395c30fbb8SHeinrich Schuchardt  * @return	color value
2405c30fbb8SHeinrich Schuchardt  */
2415c30fbb8SHeinrich Schuchardt u32 vid_console_color(struct video_priv *priv, unsigned int idx);
2425c30fbb8SHeinrich Schuchardt 
2435c30fbb8SHeinrich Schuchardt #endif
2445c30fbb8SHeinrich Schuchardt 
24583510766SSimon Glass #endif
246