blob: 6ba62ec348e97a601113d0547b5e61ebd650ac67 [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 Glass2425d4f2024-01-04 08:10:37 -0700130 vidconsole_entry_start(dev);
Simon Glassd622c5b2022-10-06 08:36:04 -0600131}
132
Tom Rini2b99fd82023-03-15 11:58:58 -0400133/**
134 * set_cursor_position() - set cursor position
135 *
136 * @priv: private data of the video console
137 * @row: new row
138 * @col: new column
139 */
Simon Glass2425d4f2024-01-04 08:10:37 -0700140static void set_cursor_position(struct udevice *dev, int row, int col)
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200141{
Simon Glass2425d4f2024-01-04 08:10:37 -0700142 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
143
Tom Rini2b99fd82023-03-15 11:58:58 -0400144 /*
145 * Ensure we stay in the bounds of the screen.
146 */
147 if (row >= priv->rows)
148 row = priv->rows - 1;
149 if (col >= priv->cols)
150 col = priv->cols - 1;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200151
Simon Glass2425d4f2024-01-04 08:10:37 -0700152 vidconsole_position_cursor(dev, col, row);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200153}
154
155/**
156 * get_cursor_position() - get cursor position
157 *
158 * @priv: private data of the video console
159 * @row: row
160 * @col: column
161 */
162static void get_cursor_position(struct vidconsole_priv *priv,
163 int *row, int *col)
164{
165 *row = priv->ycur / priv->y_charsize;
166 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
167 priv->x_charsize;
168}
169
Rob Clark06e7a0d2017-09-13 18:12:21 -0400170/*
171 * Process a character while accumulating an escape string. Chars are
172 * accumulated into escape_buf until the end of escape sequence is
173 * found, at which point the sequence is parsed and processed.
174 */
175static void vidconsole_escape_char(struct udevice *dev, char ch)
176{
177 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
178
179 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
180 goto error;
181
182 /* Sanity checking for bogus ESC sequences: */
183 if (priv->escape_len >= sizeof(priv->escape_buf))
184 goto error;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200185 if (priv->escape_len == 0) {
186 switch (ch) {
187 case '7':
188 /* Save cursor position */
189 get_cursor_position(priv, &priv->row_saved,
190 &priv->col_saved);
191 priv->escape = 0;
192
193 return;
194 case '8': {
195 /* Restore cursor position */
196 int row = priv->row_saved;
197 int col = priv->col_saved;
198
Simon Glass2425d4f2024-01-04 08:10:37 -0700199 set_cursor_position(dev, row, col);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200200 priv->escape = 0;
201 return;
202 }
203 case '[':
204 break;
205 default:
206 goto error;
207 }
208 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400209
210 priv->escape_buf[priv->escape_len++] = ch;
211
212 /*
213 * Escape sequences are terminated by a letter, so keep
214 * accumulating until we get one:
215 */
216 if (!isalpha(ch))
217 return;
218
219 /*
220 * clear escape mode first, otherwise things will get highly
221 * surprising if you hit any debug prints that come back to
222 * this console.
223 */
224 priv->escape = 0;
225
226 switch (ch) {
Andre Przywarad4a294c2019-03-23 01:29:57 +0000227 case 'A':
228 case 'B':
229 case 'C':
230 case 'D':
231 case 'E':
232 case 'F': {
233 int row, col, num;
234 char *s = priv->escape_buf;
235
236 /*
237 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
238 * Cursor left/right: [%dD, [%dC
239 */
240 s++; /* [ */
241 s = parsenum(s, &num);
242 if (num == 0) /* No digit in sequence ... */
243 num = 1; /* ... means "move by 1". */
244
245 get_cursor_position(priv, &row, &col);
246 if (ch == 'A' || ch == 'F')
247 row -= num;
248 if (ch == 'C')
249 col += num;
250 if (ch == 'D')
251 col -= num;
252 if (ch == 'B' || ch == 'E')
253 row += num;
254 if (ch == 'E' || ch == 'F')
255 col = 0;
256 if (col < 0)
257 col = 0;
258 if (row < 0)
259 row = 0;
260 /* Right and bottom overflows are handled in the callee. */
Simon Glass2425d4f2024-01-04 08:10:37 -0700261 set_cursor_position(dev, row, col);
Andre Przywarad4a294c2019-03-23 01:29:57 +0000262 break;
263 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400264 case 'H':
265 case 'f': {
266 int row, col;
267 char *s = priv->escape_buf;
268
269 /*
270 * Set cursor position: [%d;%df or [%d;%dH
271 */
272 s++; /* [ */
273 s = parsenum(s, &row);
274 s++; /* ; */
275 s = parsenum(s, &col);
276
Heinrich Schuchardtc3c69302018-11-10 19:55:48 +0100277 /*
278 * Video origin is [0, 0], terminal origin is [1, 1].
279 */
280 if (row)
281 --row;
282 if (col)
283 --col;
284
Simon Glass2425d4f2024-01-04 08:10:37 -0700285 set_cursor_position(dev, row, col);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400286
287 break;
288 }
289 case 'J': {
290 int mode;
291
292 /*
293 * Clear part/all screen:
294 * [J or [0J - clear screen from cursor down
295 * [1J - clear screen from cursor up
296 * [2J - clear entire screen
297 *
298 * TODO we really only handle entire-screen case, others
299 * probably require some additions to video-uclass (and
300 * are not really needed yet by efi_console)
301 */
302 parsenum(priv->escape_buf + 1, &mode);
303
304 if (mode == 2) {
Michal Simek632e3d42020-12-14 08:47:52 +0100305 int ret;
306
Rob Clark06e7a0d2017-09-13 18:12:21 -0400307 video_clear(dev->parent);
Michal Simek632e3d42020-12-14 08:47:52 +0100308 ret = video_sync(dev->parent, false);
309 if (ret) {
310#ifdef DEBUG
311 console_puts_select_stderr(true, "[vc err: video_sync]");
312#endif
313 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400314 priv->ycur = 0;
315 priv->xcur_frac = priv->xstart_frac;
316 } else {
317 debug("unsupported clear mode: %d\n", mode);
318 }
319 break;
320 }
Andre Przywara918622a2019-03-23 01:29:58 +0000321 case 'K': {
322 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
323 int mode;
324
325 /*
326 * Clear (parts of) current line
327 * [0K - clear line to end
328 * [2K - clear entire line
329 */
330 parsenum(priv->escape_buf + 1, &mode);
331
332 if (mode == 2) {
333 int row, col;
334
335 get_cursor_position(priv, &row, &col);
336 vidconsole_set_row(dev, row, vid_priv->colour_bg);
337 }
338 break;
339 }
Rob Clark50509bb2017-09-13 18:12:22 -0400340 case 'm': {
341 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
342 char *s = priv->escape_buf;
343 char *end = &priv->escape_buf[priv->escape_len];
344
345 /*
346 * Set graphics mode: [%d;...;%dm
347 *
348 * Currently only supports the color attributes:
349 *
350 * Foreground Colors:
351 *
352 * 30 Black
353 * 31 Red
354 * 32 Green
355 * 33 Yellow
356 * 34 Blue
357 * 35 Magenta
358 * 36 Cyan
359 * 37 White
360 *
361 * Background Colors:
362 *
363 * 40 Black
364 * 41 Red
365 * 42 Green
366 * 43 Yellow
367 * 44 Blue
368 * 45 Magenta
369 * 46 Cyan
370 * 47 White
371 */
372
373 s++; /* [ */
374 while (s < end) {
375 int val;
376
377 s = parsenum(s, &val);
378 s++;
379
380 switch (val) {
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100381 case 0:
382 /* all attributes off */
Simon Glass2b063b82018-11-06 15:21:36 -0700383 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100384 break;
385 case 1:
386 /* bold */
387 vid_priv->fg_col_idx |= 8;
Simon Glass2a006332022-10-06 08:36:03 -0600388 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100389 vid_priv, vid_priv->fg_col_idx);
390 break;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000391 case 7:
392 /* reverse video */
Simon Glass2a006332022-10-06 08:36:03 -0600393 vid_priv->colour_fg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000394 vid_priv, vid_priv->bg_col_idx);
Simon Glass2a006332022-10-06 08:36:03 -0600395 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000396 vid_priv, vid_priv->fg_col_idx);
397 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400398 case 30 ... 37:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100399 /* foreground color */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100400 vid_priv->fg_col_idx &= ~7;
401 vid_priv->fg_col_idx |= val - 30;
Simon Glass2a006332022-10-06 08:36:03 -0600402 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100403 vid_priv, vid_priv->fg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400404 break;
405 case 40 ... 47:
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000406 /* background color, also mask the bold bit */
407 vid_priv->bg_col_idx &= ~0xf;
408 vid_priv->bg_col_idx |= val - 40;
Simon Glass2a006332022-10-06 08:36:03 -0600409 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000410 vid_priv, vid_priv->bg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400411 break;
412 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100413 /* ignore unsupported SGR parameter */
Rob Clark50509bb2017-09-13 18:12:22 -0400414 break;
415 }
416 }
417
418 break;
419 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400420 default:
421 debug("unrecognized escape sequence: %*s\n",
422 priv->escape_len, priv->escape_buf);
423 }
424
425 return;
426
427error:
428 /* something went wrong, just revert to normal mode: */
429 priv->escape = 0;
430}
431
Janne Grunau5548c362024-03-16 22:50:19 +0100432/* Put that actual character on the screen (using the UTF-32 code points). */
433static int vidconsole_output_glyph(struct udevice *dev, int ch)
Andre Przywarade86baf2019-03-23 01:29:59 +0000434{
435 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
436 int ret;
437
438 /*
439 * Failure of this function normally indicates an unsupported
440 * colour depth. Check this and return an error to help with
441 * diagnosis.
442 */
443 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
444 if (ret == -EAGAIN) {
445 vidconsole_newline(dev);
446 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
447 }
448 if (ret < 0)
449 return ret;
450 priv->xcur_frac += ret;
451 priv->last_ch = ch;
452 if (priv->xcur_frac >= priv->xsize_frac)
453 vidconsole_newline(dev);
454
455 return 0;
456}
457
Simon Glass84c7fb32016-01-18 19:52:17 -0700458int vidconsole_put_char(struct udevice *dev, char ch)
459{
460 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Janne Grunau5548c362024-03-16 22:50:19 +0100461 int cp, ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700462
Rob Clark06e7a0d2017-09-13 18:12:21 -0400463 if (priv->escape) {
464 vidconsole_escape_char(dev, ch);
465 return 0;
466 }
467
Simon Glass84c7fb32016-01-18 19:52:17 -0700468 switch (ch) {
Rob Clark06e7a0d2017-09-13 18:12:21 -0400469 case '\x1b':
470 priv->escape_len = 0;
471 priv->escape = 1;
472 break;
Simon Glass37b80202016-01-14 18:10:38 -0700473 case '\a':
474 /* beep */
475 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700476 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700477 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700478 break;
479 case '\n':
480 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700481 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700482 break;
483 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700484 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
485 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700486
Simon Glass52c10c52016-01-14 18:10:37 -0700487 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700488 vidconsole_newline(dev);
489 break;
490 case '\b':
491 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700492 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700493 break;
494 default:
Janne Grunau5548c362024-03-16 22:50:19 +0100495 if (CONFIG_IS_ENABLED(CHARSET)) {
496 cp = utf8_to_utf32_stream(ch, priv->utf8_buf);
497 if (cp == 0)
498 return 0;
499 } else {
500 cp = ch;
501 }
502 ret = vidconsole_output_glyph(dev, cp);
Simon Glass52c10c52016-01-14 18:10:37 -0700503 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700504 return ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700505 break;
506 }
507
508 return 0;
509}
510
Simon Glass6f5a8642025-04-02 06:29:40 +1300511int vidconsole_put_stringn(struct udevice *dev, const char *str, int maxlen)
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200512{
Simon Glass6f5a8642025-04-02 06:29:40 +1300513 const char *s, *end = NULL;
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200514 int ret;
515
Simon Glass6f5a8642025-04-02 06:29:40 +1300516 if (maxlen != -1)
517 end = str + maxlen;
518 for (s = str; *s && (maxlen == -1 || s < end); s++) {
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200519 ret = vidconsole_put_char(dev, *s);
520 if (ret)
521 return ret;
522 }
523
524 return 0;
525}
526
Simon Glass6f5a8642025-04-02 06:29:40 +1300527int vidconsole_put_string(struct udevice *dev, const char *str)
528{
529 return vidconsole_put_stringn(dev, str, -1);
530}
531
Simon Glass84c7fb32016-01-18 19:52:17 -0700532static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
533{
534 struct udevice *dev = sdev->priv;
Simon Glassbe4fbd42025-04-02 06:29:42 +1300535 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf97beb72020-07-02 21:12:14 -0600536 int ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700537
Simon Glassbe4fbd42025-04-02 06:29:42 +1300538 if (priv->quiet)
539 return;
Simon Glassf97beb72020-07-02 21:12:14 -0600540 ret = vidconsole_put_char(dev, ch);
541 if (ret) {
542#ifdef DEBUG
543 console_puts_select_stderr(true, "[vc err: putc]");
544#endif
545 }
Michal Simek632e3d42020-12-14 08:47:52 +0100546 ret = video_sync(dev->parent, false);
547 if (ret) {
548#ifdef DEBUG
549 console_puts_select_stderr(true, "[vc err: video_sync]");
550#endif
551 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700552}
553
554static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
555{
556 struct udevice *dev = sdev->priv;
Simon Glassbe4fbd42025-04-02 06:29:42 +1300557 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf97beb72020-07-02 21:12:14 -0600558 int ret;
559
Simon Glassbe4fbd42025-04-02 06:29:42 +1300560 if (priv->quiet)
561 return;
Simon Glassf97beb72020-07-02 21:12:14 -0600562 ret = vidconsole_put_string(dev, s);
563 if (ret) {
564#ifdef DEBUG
565 char str[30];
Simon Glass84c7fb32016-01-18 19:52:17 -0700566
Simon Glassf97beb72020-07-02 21:12:14 -0600567 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
568 console_puts_select_stderr(true, str);
569#endif
570 }
Michal Simek632e3d42020-12-14 08:47:52 +0100571 ret = video_sync(dev->parent, false);
572 if (ret) {
573#ifdef DEBUG
574 console_puts_select_stderr(true, "[vc err: video_sync]");
575#endif
576 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700577}
578
Simon Glass3b175ba2023-01-06 08:52:32 -0600579void vidconsole_list_fonts(struct udevice *dev)
580{
581 struct vidfont_info info;
582 int ret, i;
583
584 for (i = 0, ret = 0; !ret; i++) {
585 ret = vidconsole_get_font(dev, i, &info);
586 if (!ret)
587 printf("%s\n", info.name);
588 }
589}
590
591int vidconsole_get_font(struct udevice *dev, int seq,
592 struct vidfont_info *info)
593{
594 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
595
596 if (!ops->get_font)
597 return -ENOSYS;
598
599 return ops->get_font(dev, seq, info);
600}
601
Dzmitry Sankouski86c6a532023-03-07 13:21:15 +0300602int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
603{
604 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
605
606 if (!ops->get_font_size)
607 return -ENOSYS;
608
609 *name = ops->get_font_size(dev, sizep);
610 return 0;
611}
612
Simon Glass3b175ba2023-01-06 08:52:32 -0600613int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
614{
615 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
616
617 if (!ops->select_font)
618 return -ENOSYS;
619
620 return ops->select_font(dev, name, size);
621}
622
Simon Glass5caf1252023-06-01 10:22:46 -0600623int vidconsole_measure(struct udevice *dev, const char *name, uint size,
Simon Glass3aa33582025-04-02 06:29:39 +1300624 const char *text, int limit,
625 struct vidconsole_bbox *bbox, struct alist *lines)
Simon Glass5caf1252023-06-01 10:22:46 -0600626{
627 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
628 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
629 int ret;
630
Bin Meng31aef742023-08-03 17:32:41 +0800631 if (ops->measure) {
Simon Glass95bcad42025-04-02 06:29:36 +1300632 if (lines)
633 alist_empty(lines);
Simon Glass3aa33582025-04-02 06:29:39 +1300634 ret = ops->measure(dev, name, size, text, limit, bbox, lines);
Simon Glass5caf1252023-06-01 10:22:46 -0600635 if (ret != -ENOSYS)
636 return ret;
637 }
638
639 bbox->valid = true;
640 bbox->x0 = 0;
641 bbox->y0 = 0;
642 bbox->x1 = priv->x_charsize * strlen(text);
643 bbox->y1 = priv->y_charsize;
644
645 return 0;
646}
647
Simon Glass8b82e592023-10-01 19:13:18 -0600648int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
649 uint num_chars, struct vidconsole_bbox *bbox)
650{
651 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
652 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
653 int ret;
654
655 if (ops->measure) {
656 ret = ops->nominal(dev, name, size, num_chars, bbox);
657 if (ret != -ENOSYS)
658 return ret;
659 }
660
661 bbox->valid = true;
662 bbox->x0 = 0;
663 bbox->y0 = 0;
664 bbox->x1 = priv->x_charsize * num_chars;
665 bbox->y1 = priv->y_charsize;
666
667 return 0;
668}
669
Simon Glass34b5c252023-10-01 19:13:19 -0600670int vidconsole_entry_save(struct udevice *dev, struct abuf *buf)
671{
672 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
673 int ret;
674
675 if (ops->measure) {
676 ret = ops->entry_save(dev, buf);
677 if (ret != -ENOSYS)
678 return ret;
679 }
680
681 /* no data so make sure the buffer is empty */
682 abuf_realloc(buf, 0);
683
684 return 0;
685}
686
687int vidconsole_entry_restore(struct udevice *dev, struct abuf *buf)
688{
689 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
690 int ret;
691
692 if (ops->measure) {
693 ret = ops->entry_restore(dev, buf);
694 if (ret != -ENOSYS)
695 return ret;
696 }
697
698 return 0;
699}
700
Simon Glass377f79aa2023-10-01 19:13:21 -0600701int vidconsole_set_cursor_visible(struct udevice *dev, bool visible,
702 uint x, uint y, uint index)
703{
704 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
705 int ret;
706
707 if (ops->set_cursor_visible) {
708 ret = ops->set_cursor_visible(dev, visible, x, y, index);
709 if (ret != -ENOSYS)
710 return ret;
711 }
712
713 return 0;
714}
715
Simon Glassa73a8b82023-06-01 10:22:45 -0600716void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
717 enum colour_idx bg, struct vidconsole_colour *old)
718{
719 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
720
721 old->colour_fg = vid_priv->colour_fg;
722 old->colour_bg = vid_priv->colour_bg;
723
724 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
725 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
726}
727
728void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
729{
730 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
731
732 vid_priv->colour_fg = old->colour_fg;
733 vid_priv->colour_bg = old->colour_bg;
734}
735
Simon Glass84c7fb32016-01-18 19:52:17 -0700736/* Set up the number of rows and colours (rotated drivers override this) */
737static int vidconsole_pre_probe(struct udevice *dev)
738{
739 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
740 struct udevice *vid = dev->parent;
741 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
742
Simon Glass52c10c52016-01-14 18:10:37 -0700743 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700744
745 return 0;
746}
747
748/* Register the device with stdio */
749static int vidconsole_post_probe(struct udevice *dev)
750{
751 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
752 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700753
Simon Glass52c10c52016-01-14 18:10:37 -0700754 if (!priv->tab_width_frac)
755 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
756
Simon Glass75e534b2020-12-16 21:20:07 -0700757 if (dev_seq(dev)) {
Simon Glass798ff502016-01-21 19:44:51 -0700758 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass75e534b2020-12-16 21:20:07 -0700759 dev_seq(dev));
Simon Glass798ff502016-01-21 19:44:51 -0700760 } else {
761 strcpy(sdev->name, "vidconsole");
762 }
Simon Glass52c10c52016-01-14 18:10:37 -0700763
Simon Glass84c7fb32016-01-18 19:52:17 -0700764 sdev->flags = DEV_FLAGS_OUTPUT;
765 sdev->putc = vidconsole_putc;
766 sdev->puts = vidconsole_puts;
767 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700768
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900769 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700770}
771
772UCLASS_DRIVER(vidconsole) = {
773 .id = UCLASS_VIDEO_CONSOLE,
774 .name = "vidconsole0",
775 .pre_probe = vidconsole_pre_probe,
776 .post_probe = vidconsole_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700777 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass84c7fb32016-01-18 19:52:17 -0700778};
779
Simon Glass90679c62023-03-10 12:47:21 -0800780int vidconsole_clear_and_reset(struct udevice *dev)
781{
782 int ret;
783
784 ret = video_clear(dev_get_parent(dev));
785 if (ret)
786 return ret;
787 vidconsole_position_cursor(dev, 0, 0);
788
789 return 0;
790}
Tom Rini2b99fd82023-03-15 11:58:58 -0400791
792void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
793{
794 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
795 struct udevice *vid_dev = dev->parent;
796 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
797 short x, y;
798
799 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
800 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
801 vidconsole_set_cursor_pos(dev, x, y);
802}
Simon Glassbe4fbd42025-04-02 06:29:42 +1300803
804void vidconsole_set_quiet(struct udevice *dev, bool quiet)
805{
806 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
807
808 priv->quiet = quiet;
809}