blob: f1b2d61bd8ff9b68941b0ec82767f3361fef758c [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
4 * (C) Copyright 2001-2015
5 * DENX Software Engineering -- wd@denx.de
6 * Compulab Ltd - http://compulab.co.il/
7 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
Simon Glass84c7fb32016-01-18 19:52:17 -07008 */
9
Patrick Delaunay81313352021-04-27 11:02:19 +020010#define LOG_CATEGORY UCLASS_VIDEO_CONSOLE
11
Simon Glass34b5c252023-10-01 19:13:19 -060012#include <abuf.h>
Janne Grunau5548c362024-03-16 22:50:19 +010013#include <charset.h>
Simon Glassed38aef2020-05-10 11:40:03 -060014#include <command.h>
Simon Glassf97beb72020-07-02 21:12:14 -060015#include <console.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070017#include <dm.h>
18#include <video.h>
19#include <video_console.h>
Heinrich Schuchardt2172fa42018-03-02 20:50:17 +010020#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glassf97beb72020-07-02 21:12:14 -060021#include <linux/ctype.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070022
Janne Grunau5548c362024-03-16 22:50:19 +010023int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, int ch)
Simon Glass84c7fb32016-01-18 19:52:17 -070024{
25 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
26
27 if (!ops->putc_xy)
28 return -ENOSYS;
29 return ops->putc_xy(dev, x, y, ch);
30}
31
32int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
33 uint count)
34{
35 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
36
37 if (!ops->move_rows)
38 return -ENOSYS;
39 return ops->move_rows(dev, rowdst, rowsrc, count);
40}
41
42int vidconsole_set_row(struct udevice *dev, uint row, int clr)
43{
44 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
45
46 if (!ops->set_row)
47 return -ENOSYS;
48 return ops->set_row(dev, row, clr);
49}
50
Simon Glass4446c4b2023-10-01 19:13:20 -060051int vidconsole_entry_start(struct udevice *dev)
Simon Glassafee7432016-01-14 18:10:40 -070052{
53 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
54
55 if (!ops->entry_start)
56 return -ENOSYS;
57 return ops->entry_start(dev);
58}
59
Simon Glass84c7fb32016-01-18 19:52:17 -070060/* Move backwards one space */
Simon Glass33bd3b62016-01-14 18:10:41 -070061static int vidconsole_back(struct udevice *dev)
Simon Glass84c7fb32016-01-18 19:52:17 -070062{
63 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass33bd3b62016-01-14 18:10:41 -070064 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
65 int ret;
66
67 if (ops->backspace) {
68 ret = ops->backspace(dev);
69 if (ret != -ENOSYS)
70 return ret;
71 }
Simon Glass84c7fb32016-01-18 19:52:17 -070072
Simon Glass52c10c52016-01-14 18:10:37 -070073 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassa74451d2016-01-14 18:10:39 -070074 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glass52c10c52016-01-14 18:10:37 -070075 priv->xcur_frac = (priv->cols - 1) *
76 VID_TO_POS(priv->x_charsize);
77 priv->ycur -= priv->y_charsize;
78 if (priv->ycur < 0)
79 priv->ycur = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -070080 }
Michal Simek632e3d42020-12-14 08:47:52 +010081 return video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -070082}
83
84/* Move to a newline, scrolling the display if necessary */
85static void vidconsole_newline(struct udevice *dev)
86{
87 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
88 struct udevice *vid_dev = dev->parent;
89 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Nikhil M Jain9e3301d2023-04-20 17:41:08 +053090 const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
Michal Simek632e3d42020-12-14 08:47:52 +010091 int i, ret;
Simon Glass84c7fb32016-01-18 19:52:17 -070092
Simon Glassa74451d2016-01-14 18:10:39 -070093 priv->xcur_frac = priv->xstart_frac;
Simon Glass52c10c52016-01-14 18:10:37 -070094 priv->ycur += priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -070095
96 /* Check if we need to scroll the terminal */
Simon Glassaea571d2024-10-14 16:31:54 -060097 if (vid_priv->rot % 2 ?
98 priv->ycur + priv->x_charsize > vid_priv->xsize :
99 priv->ycur + priv->y_charsize > vid_priv->ysize) {
Simon Glass84c7fb32016-01-18 19:52:17 -0700100 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
101 for (i = 0; i < rows; i++)
102 vidconsole_set_row(dev, priv->rows - i - 1,
103 vid_priv->colour_bg);
Simon Glass52c10c52016-01-14 18:10:37 -0700104 priv->ycur -= rows * priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -0700105 }
Simon Glassafee7432016-01-14 18:10:40 -0700106 priv->last_ch = 0;
107
Michal Simek632e3d42020-12-14 08:47:52 +0100108 ret = video_sync(dev->parent, false);
109 if (ret) {
110#ifdef DEBUG
111 console_puts_select_stderr(true, "[vc err: video_sync]");
112#endif
113 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700114}
115
Rob Clark06e7a0d2017-09-13 18:12:21 -0400116static char *parsenum(char *s, int *num)
117{
118 char *end;
119 *num = simple_strtol(s, &end, 10);
120 return end;
121}
122
Simon Glassd622c5b2022-10-06 08:36:04 -0600123void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
124{
125 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
126
127 priv->xcur_frac = VID_TO_POS(x);
128 priv->xstart_frac = priv->xcur_frac;
129 priv->ycur = y;
Simon Glassece5ab02025-04-02 06:29:44 +1300130
131 /* make sure not to kern against the previous character */
132 priv->last_ch = 0;
Simon Glass2425d4f2024-01-04 08:10:37 -0700133 vidconsole_entry_start(dev);
Simon Glassd622c5b2022-10-06 08:36:04 -0600134}
135
Tom Rini2b99fd82023-03-15 11:58:58 -0400136/**
137 * set_cursor_position() - set cursor position
138 *
139 * @priv: private data of the video console
140 * @row: new row
141 * @col: new column
142 */
Simon Glass2425d4f2024-01-04 08:10:37 -0700143static void set_cursor_position(struct udevice *dev, int row, int col)
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200144{
Simon Glass2425d4f2024-01-04 08:10:37 -0700145 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
146
Tom Rini2b99fd82023-03-15 11:58:58 -0400147 /*
148 * Ensure we stay in the bounds of the screen.
149 */
150 if (row >= priv->rows)
151 row = priv->rows - 1;
152 if (col >= priv->cols)
153 col = priv->cols - 1;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200154
Simon Glass2425d4f2024-01-04 08:10:37 -0700155 vidconsole_position_cursor(dev, col, row);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200156}
157
158/**
159 * get_cursor_position() - get cursor position
160 *
161 * @priv: private data of the video console
162 * @row: row
163 * @col: column
164 */
165static void get_cursor_position(struct vidconsole_priv *priv,
166 int *row, int *col)
167{
168 *row = priv->ycur / priv->y_charsize;
169 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
170 priv->x_charsize;
171}
172
Rob Clark06e7a0d2017-09-13 18:12:21 -0400173/*
174 * Process a character while accumulating an escape string. Chars are
175 * accumulated into escape_buf until the end of escape sequence is
176 * found, at which point the sequence is parsed and processed.
177 */
178static void vidconsole_escape_char(struct udevice *dev, char ch)
179{
180 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
181
182 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
183 goto error;
184
185 /* Sanity checking for bogus ESC sequences: */
186 if (priv->escape_len >= sizeof(priv->escape_buf))
187 goto error;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200188 if (priv->escape_len == 0) {
189 switch (ch) {
190 case '7':
191 /* Save cursor position */
192 get_cursor_position(priv, &priv->row_saved,
193 &priv->col_saved);
194 priv->escape = 0;
195
196 return;
197 case '8': {
198 /* Restore cursor position */
199 int row = priv->row_saved;
200 int col = priv->col_saved;
201
Simon Glass2425d4f2024-01-04 08:10:37 -0700202 set_cursor_position(dev, row, col);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200203 priv->escape = 0;
204 return;
205 }
206 case '[':
207 break;
208 default:
209 goto error;
210 }
211 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400212
213 priv->escape_buf[priv->escape_len++] = ch;
214
215 /*
216 * Escape sequences are terminated by a letter, so keep
217 * accumulating until we get one:
218 */
219 if (!isalpha(ch))
220 return;
221
222 /*
223 * clear escape mode first, otherwise things will get highly
224 * surprising if you hit any debug prints that come back to
225 * this console.
226 */
227 priv->escape = 0;
228
229 switch (ch) {
Andre Przywarad4a294c2019-03-23 01:29:57 +0000230 case 'A':
231 case 'B':
232 case 'C':
233 case 'D':
234 case 'E':
235 case 'F': {
236 int row, col, num;
237 char *s = priv->escape_buf;
238
239 /*
240 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
241 * Cursor left/right: [%dD, [%dC
242 */
243 s++; /* [ */
244 s = parsenum(s, &num);
245 if (num == 0) /* No digit in sequence ... */
246 num = 1; /* ... means "move by 1". */
247
248 get_cursor_position(priv, &row, &col);
249 if (ch == 'A' || ch == 'F')
250 row -= num;
251 if (ch == 'C')
252 col += num;
253 if (ch == 'D')
254 col -= num;
255 if (ch == 'B' || ch == 'E')
256 row += num;
257 if (ch == 'E' || ch == 'F')
258 col = 0;
259 if (col < 0)
260 col = 0;
261 if (row < 0)
262 row = 0;
263 /* Right and bottom overflows are handled in the callee. */
Simon Glass2425d4f2024-01-04 08:10:37 -0700264 set_cursor_position(dev, row, col);
Andre Przywarad4a294c2019-03-23 01:29:57 +0000265 break;
266 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400267 case 'H':
268 case 'f': {
269 int row, col;
270 char *s = priv->escape_buf;
271
272 /*
273 * Set cursor position: [%d;%df or [%d;%dH
274 */
275 s++; /* [ */
276 s = parsenum(s, &row);
277 s++; /* ; */
278 s = parsenum(s, &col);
279
Heinrich Schuchardtc3c69302018-11-10 19:55:48 +0100280 /*
281 * Video origin is [0, 0], terminal origin is [1, 1].
282 */
283 if (row)
284 --row;
285 if (col)
286 --col;
287
Simon Glass2425d4f2024-01-04 08:10:37 -0700288 set_cursor_position(dev, row, col);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400289
290 break;
291 }
292 case 'J': {
293 int mode;
294
295 /*
296 * Clear part/all screen:
297 * [J or [0J - clear screen from cursor down
298 * [1J - clear screen from cursor up
299 * [2J - clear entire screen
300 *
301 * TODO we really only handle entire-screen case, others
302 * probably require some additions to video-uclass (and
303 * are not really needed yet by efi_console)
304 */
305 parsenum(priv->escape_buf + 1, &mode);
306
307 if (mode == 2) {
Michal Simek632e3d42020-12-14 08:47:52 +0100308 int ret;
309
Rob Clark06e7a0d2017-09-13 18:12:21 -0400310 video_clear(dev->parent);
Michal Simek632e3d42020-12-14 08:47:52 +0100311 ret = video_sync(dev->parent, false);
312 if (ret) {
313#ifdef DEBUG
314 console_puts_select_stderr(true, "[vc err: video_sync]");
315#endif
316 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400317 priv->ycur = 0;
318 priv->xcur_frac = priv->xstart_frac;
319 } else {
320 debug("unsupported clear mode: %d\n", mode);
321 }
322 break;
323 }
Andre Przywara918622a2019-03-23 01:29:58 +0000324 case 'K': {
325 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
326 int mode;
327
328 /*
329 * Clear (parts of) current line
330 * [0K - clear line to end
331 * [2K - clear entire line
332 */
333 parsenum(priv->escape_buf + 1, &mode);
334
335 if (mode == 2) {
336 int row, col;
337
338 get_cursor_position(priv, &row, &col);
339 vidconsole_set_row(dev, row, vid_priv->colour_bg);
340 }
341 break;
342 }
Rob Clark50509bb2017-09-13 18:12:22 -0400343 case 'm': {
344 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
345 char *s = priv->escape_buf;
346 char *end = &priv->escape_buf[priv->escape_len];
347
348 /*
349 * Set graphics mode: [%d;...;%dm
350 *
351 * Currently only supports the color attributes:
352 *
353 * Foreground Colors:
354 *
355 * 30 Black
356 * 31 Red
357 * 32 Green
358 * 33 Yellow
359 * 34 Blue
360 * 35 Magenta
361 * 36 Cyan
362 * 37 White
363 *
364 * Background Colors:
365 *
366 * 40 Black
367 * 41 Red
368 * 42 Green
369 * 43 Yellow
370 * 44 Blue
371 * 45 Magenta
372 * 46 Cyan
373 * 47 White
374 */
375
376 s++; /* [ */
377 while (s < end) {
378 int val;
379
380 s = parsenum(s, &val);
381 s++;
382
383 switch (val) {
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100384 case 0:
385 /* all attributes off */
Simon Glass2b063b82018-11-06 15:21:36 -0700386 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100387 break;
388 case 1:
389 /* bold */
390 vid_priv->fg_col_idx |= 8;
Simon Glass2a006332022-10-06 08:36:03 -0600391 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100392 vid_priv, vid_priv->fg_col_idx);
393 break;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000394 case 7:
395 /* reverse video */
Simon Glass2a006332022-10-06 08:36:03 -0600396 vid_priv->colour_fg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000397 vid_priv, vid_priv->bg_col_idx);
Simon Glass2a006332022-10-06 08:36:03 -0600398 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000399 vid_priv, vid_priv->fg_col_idx);
400 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400401 case 30 ... 37:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100402 /* foreground color */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100403 vid_priv->fg_col_idx &= ~7;
404 vid_priv->fg_col_idx |= val - 30;
Simon Glass2a006332022-10-06 08:36:03 -0600405 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100406 vid_priv, vid_priv->fg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400407 break;
408 case 40 ... 47:
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000409 /* background color, also mask the bold bit */
410 vid_priv->bg_col_idx &= ~0xf;
411 vid_priv->bg_col_idx |= val - 40;
Simon Glass2a006332022-10-06 08:36:03 -0600412 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000413 vid_priv, vid_priv->bg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400414 break;
415 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100416 /* ignore unsupported SGR parameter */
Rob Clark50509bb2017-09-13 18:12:22 -0400417 break;
418 }
419 }
420
421 break;
422 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400423 default:
424 debug("unrecognized escape sequence: %*s\n",
425 priv->escape_len, priv->escape_buf);
426 }
427
428 return;
429
430error:
431 /* something went wrong, just revert to normal mode: */
432 priv->escape = 0;
433}
434
Janne Grunau5548c362024-03-16 22:50:19 +0100435/* Put that actual character on the screen (using the UTF-32 code points). */
436static int vidconsole_output_glyph(struct udevice *dev, int ch)
Andre Przywarade86baf2019-03-23 01:29:59 +0000437{
438 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
439 int ret;
440
441 /*
442 * Failure of this function normally indicates an unsupported
443 * colour depth. Check this and return an error to help with
444 * diagnosis.
445 */
446 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
447 if (ret == -EAGAIN) {
448 vidconsole_newline(dev);
449 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
450 }
451 if (ret < 0)
452 return ret;
453 priv->xcur_frac += ret;
454 priv->last_ch = ch;
455 if (priv->xcur_frac >= priv->xsize_frac)
456 vidconsole_newline(dev);
457
458 return 0;
459}
460
Simon Glass84c7fb32016-01-18 19:52:17 -0700461int vidconsole_put_char(struct udevice *dev, char ch)
462{
463 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Janne Grunau5548c362024-03-16 22:50:19 +0100464 int cp, ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700465
Rob Clark06e7a0d2017-09-13 18:12:21 -0400466 if (priv->escape) {
467 vidconsole_escape_char(dev, ch);
468 return 0;
469 }
470
Simon Glass84c7fb32016-01-18 19:52:17 -0700471 switch (ch) {
Rob Clark06e7a0d2017-09-13 18:12:21 -0400472 case '\x1b':
473 priv->escape_len = 0;
474 priv->escape = 1;
475 break;
Simon Glass37b80202016-01-14 18:10:38 -0700476 case '\a':
477 /* beep */
478 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700479 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700480 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700481 break;
482 case '\n':
483 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700484 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700485 break;
486 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700487 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
488 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700489
Simon Glass52c10c52016-01-14 18:10:37 -0700490 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700491 vidconsole_newline(dev);
492 break;
493 case '\b':
494 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700495 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700496 break;
497 default:
Janne Grunau5548c362024-03-16 22:50:19 +0100498 if (CONFIG_IS_ENABLED(CHARSET)) {
499 cp = utf8_to_utf32_stream(ch, priv->utf8_buf);
500 if (cp == 0)
501 return 0;
502 } else {
503 cp = ch;
504 }
505 ret = vidconsole_output_glyph(dev, cp);
Simon Glass52c10c52016-01-14 18:10:37 -0700506 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700507 return ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700508 break;
509 }
510
511 return 0;
512}
513
Simon Glass6f5a8642025-04-02 06:29:40 +1300514int vidconsole_put_stringn(struct udevice *dev, const char *str, int maxlen)
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200515{
Simon Glass6f5a8642025-04-02 06:29:40 +1300516 const char *s, *end = NULL;
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200517 int ret;
518
Simon Glass6f5a8642025-04-02 06:29:40 +1300519 if (maxlen != -1)
520 end = str + maxlen;
521 for (s = str; *s && (maxlen == -1 || s < end); s++) {
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200522 ret = vidconsole_put_char(dev, *s);
523 if (ret)
524 return ret;
525 }
526
527 return 0;
528}
529
Simon Glass6f5a8642025-04-02 06:29:40 +1300530int vidconsole_put_string(struct udevice *dev, const char *str)
531{
532 return vidconsole_put_stringn(dev, str, -1);
533}
534
Simon Glass84c7fb32016-01-18 19:52:17 -0700535static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
536{
537 struct udevice *dev = sdev->priv;
Simon Glassbe4fbd42025-04-02 06:29:42 +1300538 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf97beb72020-07-02 21:12:14 -0600539 int ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700540
Simon Glassbe4fbd42025-04-02 06:29:42 +1300541 if (priv->quiet)
542 return;
Simon Glassf97beb72020-07-02 21:12:14 -0600543 ret = vidconsole_put_char(dev, ch);
544 if (ret) {
545#ifdef DEBUG
546 console_puts_select_stderr(true, "[vc err: putc]");
547#endif
548 }
Michal Simek632e3d42020-12-14 08:47:52 +0100549 ret = video_sync(dev->parent, false);
550 if (ret) {
551#ifdef DEBUG
552 console_puts_select_stderr(true, "[vc err: video_sync]");
553#endif
554 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700555}
556
557static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
558{
559 struct udevice *dev = sdev->priv;
Simon Glassbe4fbd42025-04-02 06:29:42 +1300560 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf97beb72020-07-02 21:12:14 -0600561 int ret;
562
Simon Glassbe4fbd42025-04-02 06:29:42 +1300563 if (priv->quiet)
564 return;
Simon Glassf97beb72020-07-02 21:12:14 -0600565 ret = vidconsole_put_string(dev, s);
566 if (ret) {
567#ifdef DEBUG
568 char str[30];
Simon Glass84c7fb32016-01-18 19:52:17 -0700569
Simon Glassf97beb72020-07-02 21:12:14 -0600570 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
571 console_puts_select_stderr(true, str);
572#endif
573 }
Michal Simek632e3d42020-12-14 08:47:52 +0100574 ret = video_sync(dev->parent, false);
575 if (ret) {
576#ifdef DEBUG
577 console_puts_select_stderr(true, "[vc err: video_sync]");
578#endif
579 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700580}
581
Simon Glass3b175ba2023-01-06 08:52:32 -0600582void vidconsole_list_fonts(struct udevice *dev)
583{
584 struct vidfont_info info;
585 int ret, i;
586
587 for (i = 0, ret = 0; !ret; i++) {
588 ret = vidconsole_get_font(dev, i, &info);
589 if (!ret)
590 printf("%s\n", info.name);
591 }
592}
593
594int vidconsole_get_font(struct udevice *dev, int seq,
595 struct vidfont_info *info)
596{
597 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
598
599 if (!ops->get_font)
600 return -ENOSYS;
601
602 return ops->get_font(dev, seq, info);
603}
604
Dzmitry Sankouski86c6a532023-03-07 13:21:15 +0300605int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
606{
607 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
608
609 if (!ops->get_font_size)
610 return -ENOSYS;
611
612 *name = ops->get_font_size(dev, sizep);
613 return 0;
614}
615
Simon Glass3b175ba2023-01-06 08:52:32 -0600616int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
617{
618 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
619
620 if (!ops->select_font)
621 return -ENOSYS;
622
623 return ops->select_font(dev, name, size);
624}
625
Simon Glass5caf1252023-06-01 10:22:46 -0600626int vidconsole_measure(struct udevice *dev, const char *name, uint size,
Simon Glass3aa33582025-04-02 06:29:39 +1300627 const char *text, int limit,
628 struct vidconsole_bbox *bbox, struct alist *lines)
Simon Glass5caf1252023-06-01 10:22:46 -0600629{
630 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
631 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
632 int ret;
633
Bin Meng31aef742023-08-03 17:32:41 +0800634 if (ops->measure) {
Simon Glass95bcad42025-04-02 06:29:36 +1300635 if (lines)
636 alist_empty(lines);
Simon Glass3aa33582025-04-02 06:29:39 +1300637 ret = ops->measure(dev, name, size, text, limit, bbox, lines);
Simon Glass5caf1252023-06-01 10:22:46 -0600638 if (ret != -ENOSYS)
639 return ret;
640 }
641
642 bbox->valid = true;
643 bbox->x0 = 0;
644 bbox->y0 = 0;
645 bbox->x1 = priv->x_charsize * strlen(text);
646 bbox->y1 = priv->y_charsize;
647
648 return 0;
649}
650
Simon Glass8b82e592023-10-01 19:13:18 -0600651int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
652 uint num_chars, struct vidconsole_bbox *bbox)
653{
654 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
655 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
656 int ret;
657
658 if (ops->measure) {
659 ret = ops->nominal(dev, name, size, num_chars, bbox);
660 if (ret != -ENOSYS)
661 return ret;
662 }
663
664 bbox->valid = true;
665 bbox->x0 = 0;
666 bbox->y0 = 0;
667 bbox->x1 = priv->x_charsize * num_chars;
668 bbox->y1 = priv->y_charsize;
669
670 return 0;
671}
672
Simon Glass34b5c252023-10-01 19:13:19 -0600673int vidconsole_entry_save(struct udevice *dev, struct abuf *buf)
674{
675 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
676 int ret;
677
678 if (ops->measure) {
679 ret = ops->entry_save(dev, buf);
680 if (ret != -ENOSYS)
681 return ret;
682 }
683
684 /* no data so make sure the buffer is empty */
685 abuf_realloc(buf, 0);
686
687 return 0;
688}
689
690int vidconsole_entry_restore(struct udevice *dev, struct abuf *buf)
691{
692 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
693 int ret;
694
695 if (ops->measure) {
696 ret = ops->entry_restore(dev, buf);
697 if (ret != -ENOSYS)
698 return ret;
699 }
700
701 return 0;
702}
703
Simon Glass377f79aa2023-10-01 19:13:21 -0600704int vidconsole_set_cursor_visible(struct udevice *dev, bool visible,
705 uint x, uint y, uint index)
706{
707 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
708 int ret;
709
710 if (ops->set_cursor_visible) {
711 ret = ops->set_cursor_visible(dev, visible, x, y, index);
712 if (ret != -ENOSYS)
713 return ret;
714 }
715
716 return 0;
717}
718
Simon Glassa73a8b82023-06-01 10:22:45 -0600719void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
720 enum colour_idx bg, struct vidconsole_colour *old)
721{
722 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
723
724 old->colour_fg = vid_priv->colour_fg;
725 old->colour_bg = vid_priv->colour_bg;
726
727 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
728 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
729}
730
731void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
732{
733 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
734
735 vid_priv->colour_fg = old->colour_fg;
736 vid_priv->colour_bg = old->colour_bg;
737}
738
Simon Glass84c7fb32016-01-18 19:52:17 -0700739/* Set up the number of rows and colours (rotated drivers override this) */
740static int vidconsole_pre_probe(struct udevice *dev)
741{
742 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
743 struct udevice *vid = dev->parent;
744 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
745
Simon Glass52c10c52016-01-14 18:10:37 -0700746 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700747
748 return 0;
749}
750
751/* Register the device with stdio */
752static int vidconsole_post_probe(struct udevice *dev)
753{
754 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
755 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700756
Simon Glass52c10c52016-01-14 18:10:37 -0700757 if (!priv->tab_width_frac)
758 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
759
Simon Glass75e534b2020-12-16 21:20:07 -0700760 if (dev_seq(dev)) {
Simon Glass798ff502016-01-21 19:44:51 -0700761 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass75e534b2020-12-16 21:20:07 -0700762 dev_seq(dev));
Simon Glass798ff502016-01-21 19:44:51 -0700763 } else {
764 strcpy(sdev->name, "vidconsole");
765 }
Simon Glass52c10c52016-01-14 18:10:37 -0700766
Simon Glass84c7fb32016-01-18 19:52:17 -0700767 sdev->flags = DEV_FLAGS_OUTPUT;
768 sdev->putc = vidconsole_putc;
769 sdev->puts = vidconsole_puts;
770 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700771
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900772 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700773}
774
775UCLASS_DRIVER(vidconsole) = {
776 .id = UCLASS_VIDEO_CONSOLE,
777 .name = "vidconsole0",
778 .pre_probe = vidconsole_pre_probe,
779 .post_probe = vidconsole_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700780 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass84c7fb32016-01-18 19:52:17 -0700781};
782
Simon Glass90679c62023-03-10 12:47:21 -0800783int vidconsole_clear_and_reset(struct udevice *dev)
784{
785 int ret;
786
787 ret = video_clear(dev_get_parent(dev));
788 if (ret)
789 return ret;
790 vidconsole_position_cursor(dev, 0, 0);
791
792 return 0;
793}
Tom Rini2b99fd82023-03-15 11:58:58 -0400794
795void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
796{
797 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
798 struct udevice *vid_dev = dev->parent;
799 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
800 short x, y;
801
802 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
803 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
804 vidconsole_set_cursor_pos(dev, x, y);
805}
Simon Glassbe4fbd42025-04-02 06:29:42 +1300806
807void vidconsole_set_quiet(struct udevice *dev, bool quiet)
808{
809 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
810
811 priv->quiet = quiet;
812}