1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2015 Google, Inc
4  */
5 
6 #ifndef __video_console_h
7 #define __video_console_h
8 
9 #include <video.h>
10 
11 #define VID_FRAC_DIV	256
12 
13 #define VID_TO_PIXEL(x)	((x) / VID_FRAC_DIV)
14 #define VID_TO_POS(x)	((x) * VID_FRAC_DIV)
15 
16 /*
17  * The 16 colors supported by the console
18  */
19 enum color_idx {
20 	VID_BLACK = 0,
21 	VID_RED,
22 	VID_GREEN,
23 	VID_BROWN,
24 	VID_BLUE,
25 	VID_MAGENTA,
26 	VID_CYAN,
27 	VID_LIGHT_GRAY,
28 	VID_GRAY,
29 	VID_LIGHT_RED,
30 	VID_LIGTH_GREEN,
31 	VID_YELLOW,
32 	VID_LIGHT_BLUE,
33 	VID_LIGHT_MAGENTA,
34 	VID_LIGHT_CYAN,
35 	VID_WHITE,
36 
37 	VID_COLOR_COUNT
38 };
39 
40 /**
41  * struct vidconsole_priv - uclass-private data about a console device
42  *
43  * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
44  * method. Drivers may set up @xstart_frac if desired.
45  *
46  * @sdev:		stdio device, acting as an output sink
47  * @xcur_frac:		Current X position, in fractional units (VID_TO_POS(x))
48  * @ycur:		Current Y position in pixels (0=top)
49  * @rows:		Number of text rows
50  * @cols:		Number of text columns
51  * @x_charsize:		Character width in pixels
52  * @y_charsize:		Character height in pixels
53  * @tab_width_frac:	Tab width in fractional units
54  * @xsize_frac:		Width of the display in fractional units
55  * @xstart_frac:	Left margin for the text console in fractional units
56  * @last_ch:		Last character written to the text console on this line
57  * @escape:		TRUE if currently accumulating an ANSI escape sequence
58  * @escape_len:		Length of accumulated escape sequence so far
59  * @col_saved:		Saved X position, in fractional units (VID_TO_POS(x))
60  * @row_saved:		Saved Y position in pixels (0=top)
61  * @escape_buf:		Buffer to accumulate escape sequence
62  */
63 struct vidconsole_priv {
64 	struct stdio_dev sdev;
65 	int xcur_frac;
66 	int ycur;
67 	int rows;
68 	int cols;
69 	int x_charsize;
70 	int y_charsize;
71 	int tab_width_frac;
72 	int xsize_frac;
73 	int xstart_frac;
74 	int last_ch;
75 	/*
76 	 * ANSI escape sequences are accumulated character by character,
77 	 * starting after the ESC char (0x1b) until the entire sequence
78 	 * is consumed at which point it is acted upon.
79 	 */
80 	int escape;
81 	int escape_len;
82 	int row_saved;
83 	int col_saved;
84 	char escape_buf[32];
85 };
86 
87 /**
88  * struct vidconsole_ops - Video console operations
89  *
90  * These operations work on either an absolute console position (measured
91  * in pixels) or a text row number (measured in rows, where each row consists
92  * of an entire line of text - typically 16 pixels).
93  */
94 struct vidconsole_ops {
95 	/**
96 	 * putc_xy() - write a single character to a position
97 	 *
98 	 * @dev:	Device to write to
99 	 * @x_frac:	Fractional pixel X position (0=left-most pixel) which
100 	 *		is the X position multipled by VID_FRAC_DIV.
101 	 * @y:		Pixel Y position (0=top-most pixel)
102 	 * @ch:		Character to write
103 	 * @return number of fractional pixels that the cursor should move,
104 	 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
105 	 * on error
106 	 */
107 	int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
108 
109 	/**
110 	 * move_rows() - Move text rows from one place to another
111 	 *
112 	 * @dev:	Device to adjust
113 	 * @rowdst:	Destination text row (0=top)
114 	 * @rowsrc:	Source start text row
115 	 * @count:	Number of text rows to move
116 	 * @return 0 if OK, -ve on error
117 	 */
118 	int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
119 			  uint count);
120 
121 	/**
122 	 * set_row() - Set the colour of a text row
123 	 *
124 	 * Every pixel contained within the text row is adjusted
125 	 *
126 	 * @dev:	Device to adjust
127 	 * @row:	Text row to adjust (0=top)
128 	 * @clr:	Raw colour (pixel value) to write to each pixel
129 	 * @return 0 if OK, -ve on error
130 	 */
131 	int (*set_row)(struct udevice *dev, uint row, int clr);
132 
133 	/**
134 	 * entry_start() - Indicate that text entry is starting afresh
135 	 *
136 	 * Consoles which use proportional fonts need to track the position of
137 	 * each character output so that backspace will return to the correct
138 	 * place. This method signals to the console driver that a new entry
139 	 * line is being start (e.g. the user pressed return to start a new
140 	 * command). The driver can use this signal to empty its list of
141 	 * positions.
142 	 */
143 	int (*entry_start)(struct udevice *dev);
144 
145 	/**
146 	 * backspace() - Handle erasing the last character
147 	 *
148 	 * With proportional fonts the vidconsole uclass cannot itself erase
149 	 * the previous character. This optional method will be called when
150 	 * a backspace is needed. The driver should erase the previous
151 	 * character and update the cursor position (xcur_frac, ycur) to the
152 	 * start of the previous character.
153 	 *
154 	 * If not implement, default behaviour will work for fixed-width
155 	 * characters.
156 	 */
157 	int (*backspace)(struct udevice *dev);
158 };
159 
160 /* Get a pointer to the driver operations for a video console device */
161 #define vidconsole_get_ops(dev)  ((struct vidconsole_ops *)(dev)->driver->ops)
162 
163 /**
164  * vidconsole_putc_xy() - write a single character to a position
165  *
166  * @dev:	Device to write to
167  * @x_frac:	Fractional pixel X position (0=left-most pixel) which
168  *		is the X position multipled by VID_FRAC_DIV.
169  * @y:		Pixel Y position (0=top-most pixel)
170  * @ch:		Character to write
171  * @return number of fractional pixels that the cursor should move,
172  * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
173  * on error
174  */
175 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
176 
177 /**
178  * vidconsole_move_rows() - Move text rows from one place to another
179  *
180  * @dev:	Device to adjust
181  * @rowdst:	Destination text row (0=top)
182  * @rowsrc:	Source start text row
183  * @count:	Number of text rows to move
184  * @return 0 if OK, -ve on error
185  */
186 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
187 			 uint count);
188 
189 /**
190  * vidconsole_set_row() - Set the colour of a text row
191  *
192  * Every pixel contained within the text row is adjusted
193  *
194  * @dev:	Device to adjust
195  * @row:	Text row to adjust (0=top)
196  * @clr:	Raw colour (pixel value) to write to each pixel
197  * @return 0 if OK, -ve on error
198  */
199 int vidconsole_set_row(struct udevice *dev, uint row, int clr);
200 
201 /**
202  * vidconsole_put_char() - Output a character to the current console position
203  *
204  * Outputs a character to the console and advances the cursor. This function
205  * handles wrapping to new lines and scrolling the console. Special
206  * characters are handled also: \n, \r, \b and \t.
207  *
208  * The device always starts with the cursor at position 0,0 (top left). It
209  * can be adjusted manually using vidconsole_position_cursor().
210  *
211  * @dev:	Device to adjust
212  * @ch:		Character to write
213  * @return 0 if OK, -ve on error
214  */
215 int vidconsole_put_char(struct udevice *dev, char ch);
216 
217 /**
218  * vidconsole_position_cursor() - Move the text cursor
219  *
220  * @dev:	Device to adjust
221  * @col:	New cursor text column
222  * @row:	New cursor text row
223  * @return 0 if OK, -ve on error
224  */
225 void vidconsole_position_cursor(struct udevice *dev, unsigned col,
226 				unsigned row);
227 
228 #ifdef CONFIG_DM_VIDEO
229 
230 /**
231  * vid_console_color() - convert a color code to a pixel's internal
232  * representation
233  *
234  * The caller has to guarantee that the color index is less than
235  * VID_COLOR_COUNT.
236  *
237  * @priv	private data of the console device
238  * @idx		color index
239  * @return	color value
240  */
241 u32 vid_console_color(struct video_priv *priv, unsigned int idx);
242 
243 #endif
244 
245 #endif
246