blob: 5d06e51ff236b2acc0d7d40fd12c2dc171bc2bd5 [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 Glass84c7fb32016-01-18 19:52:17 -070012#include <common.h>
Simon Glass34b5c252023-10-01 19:13:19 -060013#include <abuf.h>
Janne Grunau5548c362024-03-16 22:50:19 +010014#include <charset.h>
Simon Glassed38aef2020-05-10 11:40:03 -060015#include <command.h>
Simon Glassf97beb72020-07-02 21:12:14 -060016#include <console.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070018#include <dm.h>
19#include <video.h>
20#include <video_console.h>
Heinrich Schuchardt2172fa42018-03-02 20:50:17 +010021#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glassf97beb72020-07-02 21:12:14 -060022#include <linux/ctype.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070023
Janne Grunau5548c362024-03-16 22:50:19 +010024int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, int ch)
Simon Glass84c7fb32016-01-18 19:52:17 -070025{
26 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
27
28 if (!ops->putc_xy)
29 return -ENOSYS;
30 return ops->putc_xy(dev, x, y, ch);
31}
32
33int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
34 uint count)
35{
36 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
37
38 if (!ops->move_rows)
39 return -ENOSYS;
40 return ops->move_rows(dev, rowdst, rowsrc, count);
41}
42
43int vidconsole_set_row(struct udevice *dev, uint row, int clr)
44{
45 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
46
47 if (!ops->set_row)
48 return -ENOSYS;
49 return ops->set_row(dev, row, clr);
50}
51
Simon Glass4446c4b2023-10-01 19:13:20 -060052int vidconsole_entry_start(struct udevice *dev)
Simon Glassafee7432016-01-14 18:10:40 -070053{
54 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
55
56 if (!ops->entry_start)
57 return -ENOSYS;
58 return ops->entry_start(dev);
59}
60
Simon Glass84c7fb32016-01-18 19:52:17 -070061/* Move backwards one space */
Simon Glass33bd3b62016-01-14 18:10:41 -070062static int vidconsole_back(struct udevice *dev)
Simon Glass84c7fb32016-01-18 19:52:17 -070063{
64 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass33bd3b62016-01-14 18:10:41 -070065 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
66 int ret;
67
68 if (ops->backspace) {
69 ret = ops->backspace(dev);
70 if (ret != -ENOSYS)
71 return ret;
72 }
Simon Glass84c7fb32016-01-18 19:52:17 -070073
Simon Glass52c10c52016-01-14 18:10:37 -070074 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassa74451d2016-01-14 18:10:39 -070075 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glass52c10c52016-01-14 18:10:37 -070076 priv->xcur_frac = (priv->cols - 1) *
77 VID_TO_POS(priv->x_charsize);
78 priv->ycur -= priv->y_charsize;
79 if (priv->ycur < 0)
80 priv->ycur = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -070081 }
Michal Simek632e3d42020-12-14 08:47:52 +010082 return video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -070083}
84
85/* Move to a newline, scrolling the display if necessary */
86static void vidconsole_newline(struct udevice *dev)
87{
88 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
89 struct udevice *vid_dev = dev->parent;
90 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Nikhil M Jain9e3301d2023-04-20 17:41:08 +053091 const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
Michal Simek632e3d42020-12-14 08:47:52 +010092 int i, ret;
Simon Glass84c7fb32016-01-18 19:52:17 -070093
Simon Glassa74451d2016-01-14 18:10:39 -070094 priv->xcur_frac = priv->xstart_frac;
Simon Glass52c10c52016-01-14 18:10:37 -070095 priv->ycur += priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -070096
97 /* Check if we need to scroll the terminal */
Simon Glass52c10c52016-01-14 18:10:37 -070098 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass84c7fb32016-01-18 19:52:17 -070099 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
100 for (i = 0; i < rows; i++)
101 vidconsole_set_row(dev, priv->rows - i - 1,
102 vid_priv->colour_bg);
Simon Glass52c10c52016-01-14 18:10:37 -0700103 priv->ycur -= rows * priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -0700104 }
Simon Glassafee7432016-01-14 18:10:40 -0700105 priv->last_ch = 0;
106
Michal Simek632e3d42020-12-14 08:47:52 +0100107 ret = video_sync(dev->parent, false);
108 if (ret) {
109#ifdef DEBUG
110 console_puts_select_stderr(true, "[vc err: video_sync]");
111#endif
112 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700113}
114
Rob Clark06e7a0d2017-09-13 18:12:21 -0400115static char *parsenum(char *s, int *num)
116{
117 char *end;
118 *num = simple_strtol(s, &end, 10);
119 return end;
120}
121
Simon Glassd622c5b2022-10-06 08:36:04 -0600122void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
123{
124 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
125
126 priv->xcur_frac = VID_TO_POS(x);
127 priv->xstart_frac = priv->xcur_frac;
128 priv->ycur = y;
Simon Glass2425d4f2024-01-04 08:10:37 -0700129 vidconsole_entry_start(dev);
Simon Glassd622c5b2022-10-06 08:36:04 -0600130}
131
Tom Rini2b99fd82023-03-15 11:58:58 -0400132/**
133 * set_cursor_position() - set cursor position
134 *
135 * @priv: private data of the video console
136 * @row: new row
137 * @col: new column
138 */
Simon Glass2425d4f2024-01-04 08:10:37 -0700139static void set_cursor_position(struct udevice *dev, int row, int col)
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200140{
Simon Glass2425d4f2024-01-04 08:10:37 -0700141 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
142
Tom Rini2b99fd82023-03-15 11:58:58 -0400143 /*
144 * Ensure we stay in the bounds of the screen.
145 */
146 if (row >= priv->rows)
147 row = priv->rows - 1;
148 if (col >= priv->cols)
149 col = priv->cols - 1;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200150
Simon Glass2425d4f2024-01-04 08:10:37 -0700151 vidconsole_position_cursor(dev, col, row);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200152}
153
154/**
155 * get_cursor_position() - get cursor position
156 *
157 * @priv: private data of the video console
158 * @row: row
159 * @col: column
160 */
161static void get_cursor_position(struct vidconsole_priv *priv,
162 int *row, int *col)
163{
164 *row = priv->ycur / priv->y_charsize;
165 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
166 priv->x_charsize;
167}
168
Rob Clark06e7a0d2017-09-13 18:12:21 -0400169/*
170 * Process a character while accumulating an escape string. Chars are
171 * accumulated into escape_buf until the end of escape sequence is
172 * found, at which point the sequence is parsed and processed.
173 */
174static void vidconsole_escape_char(struct udevice *dev, char ch)
175{
176 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
177
178 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
179 goto error;
180
181 /* Sanity checking for bogus ESC sequences: */
182 if (priv->escape_len >= sizeof(priv->escape_buf))
183 goto error;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200184 if (priv->escape_len == 0) {
185 switch (ch) {
186 case '7':
187 /* Save cursor position */
188 get_cursor_position(priv, &priv->row_saved,
189 &priv->col_saved);
190 priv->escape = 0;
191
192 return;
193 case '8': {
194 /* Restore cursor position */
195 int row = priv->row_saved;
196 int col = priv->col_saved;
197
Simon Glass2425d4f2024-01-04 08:10:37 -0700198 set_cursor_position(dev, row, col);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200199 priv->escape = 0;
200 return;
201 }
202 case '[':
203 break;
204 default:
205 goto error;
206 }
207 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400208
209 priv->escape_buf[priv->escape_len++] = ch;
210
211 /*
212 * Escape sequences are terminated by a letter, so keep
213 * accumulating until we get one:
214 */
215 if (!isalpha(ch))
216 return;
217
218 /*
219 * clear escape mode first, otherwise things will get highly
220 * surprising if you hit any debug prints that come back to
221 * this console.
222 */
223 priv->escape = 0;
224
225 switch (ch) {
Andre Przywarad4a294c2019-03-23 01:29:57 +0000226 case 'A':
227 case 'B':
228 case 'C':
229 case 'D':
230 case 'E':
231 case 'F': {
232 int row, col, num;
233 char *s = priv->escape_buf;
234
235 /*
236 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
237 * Cursor left/right: [%dD, [%dC
238 */
239 s++; /* [ */
240 s = parsenum(s, &num);
241 if (num == 0) /* No digit in sequence ... */
242 num = 1; /* ... means "move by 1". */
243
244 get_cursor_position(priv, &row, &col);
245 if (ch == 'A' || ch == 'F')
246 row -= num;
247 if (ch == 'C')
248 col += num;
249 if (ch == 'D')
250 col -= num;
251 if (ch == 'B' || ch == 'E')
252 row += num;
253 if (ch == 'E' || ch == 'F')
254 col = 0;
255 if (col < 0)
256 col = 0;
257 if (row < 0)
258 row = 0;
259 /* Right and bottom overflows are handled in the callee. */
Simon Glass2425d4f2024-01-04 08:10:37 -0700260 set_cursor_position(dev, row, col);
Andre Przywarad4a294c2019-03-23 01:29:57 +0000261 break;
262 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400263 case 'H':
264 case 'f': {
265 int row, col;
266 char *s = priv->escape_buf;
267
268 /*
269 * Set cursor position: [%d;%df or [%d;%dH
270 */
271 s++; /* [ */
272 s = parsenum(s, &row);
273 s++; /* ; */
274 s = parsenum(s, &col);
275
Heinrich Schuchardtc3c69302018-11-10 19:55:48 +0100276 /*
277 * Video origin is [0, 0], terminal origin is [1, 1].
278 */
279 if (row)
280 --row;
281 if (col)
282 --col;
283
Simon Glass2425d4f2024-01-04 08:10:37 -0700284 set_cursor_position(dev, row, col);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400285
286 break;
287 }
288 case 'J': {
289 int mode;
290
291 /*
292 * Clear part/all screen:
293 * [J or [0J - clear screen from cursor down
294 * [1J - clear screen from cursor up
295 * [2J - clear entire screen
296 *
297 * TODO we really only handle entire-screen case, others
298 * probably require some additions to video-uclass (and
299 * are not really needed yet by efi_console)
300 */
301 parsenum(priv->escape_buf + 1, &mode);
302
303 if (mode == 2) {
Michal Simek632e3d42020-12-14 08:47:52 +0100304 int ret;
305
Rob Clark06e7a0d2017-09-13 18:12:21 -0400306 video_clear(dev->parent);
Michal Simek632e3d42020-12-14 08:47:52 +0100307 ret = video_sync(dev->parent, false);
308 if (ret) {
309#ifdef DEBUG
310 console_puts_select_stderr(true, "[vc err: video_sync]");
311#endif
312 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400313 priv->ycur = 0;
314 priv->xcur_frac = priv->xstart_frac;
315 } else {
316 debug("unsupported clear mode: %d\n", mode);
317 }
318 break;
319 }
Andre Przywara918622a2019-03-23 01:29:58 +0000320 case 'K': {
321 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
322 int mode;
323
324 /*
325 * Clear (parts of) current line
326 * [0K - clear line to end
327 * [2K - clear entire line
328 */
329 parsenum(priv->escape_buf + 1, &mode);
330
331 if (mode == 2) {
332 int row, col;
333
334 get_cursor_position(priv, &row, &col);
335 vidconsole_set_row(dev, row, vid_priv->colour_bg);
336 }
337 break;
338 }
Rob Clark50509bb2017-09-13 18:12:22 -0400339 case 'm': {
340 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
341 char *s = priv->escape_buf;
342 char *end = &priv->escape_buf[priv->escape_len];
343
344 /*
345 * Set graphics mode: [%d;...;%dm
346 *
347 * Currently only supports the color attributes:
348 *
349 * Foreground Colors:
350 *
351 * 30 Black
352 * 31 Red
353 * 32 Green
354 * 33 Yellow
355 * 34 Blue
356 * 35 Magenta
357 * 36 Cyan
358 * 37 White
359 *
360 * Background Colors:
361 *
362 * 40 Black
363 * 41 Red
364 * 42 Green
365 * 43 Yellow
366 * 44 Blue
367 * 45 Magenta
368 * 46 Cyan
369 * 47 White
370 */
371
372 s++; /* [ */
373 while (s < end) {
374 int val;
375
376 s = parsenum(s, &val);
377 s++;
378
379 switch (val) {
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100380 case 0:
381 /* all attributes off */
Simon Glass2b063b82018-11-06 15:21:36 -0700382 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100383 break;
384 case 1:
385 /* bold */
386 vid_priv->fg_col_idx |= 8;
Simon Glass2a006332022-10-06 08:36:03 -0600387 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100388 vid_priv, vid_priv->fg_col_idx);
389 break;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000390 case 7:
391 /* reverse video */
Simon Glass2a006332022-10-06 08:36:03 -0600392 vid_priv->colour_fg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000393 vid_priv, vid_priv->bg_col_idx);
Simon Glass2a006332022-10-06 08:36:03 -0600394 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000395 vid_priv, vid_priv->fg_col_idx);
396 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400397 case 30 ... 37:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100398 /* foreground color */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100399 vid_priv->fg_col_idx &= ~7;
400 vid_priv->fg_col_idx |= val - 30;
Simon Glass2a006332022-10-06 08:36:03 -0600401 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100402 vid_priv, vid_priv->fg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400403 break;
404 case 40 ... 47:
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000405 /* background color, also mask the bold bit */
406 vid_priv->bg_col_idx &= ~0xf;
407 vid_priv->bg_col_idx |= val - 40;
Simon Glass2a006332022-10-06 08:36:03 -0600408 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000409 vid_priv, vid_priv->bg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400410 break;
411 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100412 /* ignore unsupported SGR parameter */
Rob Clark50509bb2017-09-13 18:12:22 -0400413 break;
414 }
415 }
416
417 break;
418 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400419 default:
420 debug("unrecognized escape sequence: %*s\n",
421 priv->escape_len, priv->escape_buf);
422 }
423
424 return;
425
426error:
427 /* something went wrong, just revert to normal mode: */
428 priv->escape = 0;
429}
430
Janne Grunau5548c362024-03-16 22:50:19 +0100431/* Put that actual character on the screen (using the UTF-32 code points). */
432static int vidconsole_output_glyph(struct udevice *dev, int ch)
Andre Przywarade86baf2019-03-23 01:29:59 +0000433{
434 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
435 int ret;
436
437 /*
438 * Failure of this function normally indicates an unsupported
439 * colour depth. Check this and return an error to help with
440 * diagnosis.
441 */
442 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
443 if (ret == -EAGAIN) {
444 vidconsole_newline(dev);
445 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
446 }
447 if (ret < 0)
448 return ret;
449 priv->xcur_frac += ret;
450 priv->last_ch = ch;
451 if (priv->xcur_frac >= priv->xsize_frac)
452 vidconsole_newline(dev);
453
454 return 0;
455}
456
Simon Glass84c7fb32016-01-18 19:52:17 -0700457int vidconsole_put_char(struct udevice *dev, char ch)
458{
459 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Janne Grunau5548c362024-03-16 22:50:19 +0100460 int cp, ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700461
Rob Clark06e7a0d2017-09-13 18:12:21 -0400462 if (priv->escape) {
463 vidconsole_escape_char(dev, ch);
464 return 0;
465 }
466
Simon Glass84c7fb32016-01-18 19:52:17 -0700467 switch (ch) {
Rob Clark06e7a0d2017-09-13 18:12:21 -0400468 case '\x1b':
469 priv->escape_len = 0;
470 priv->escape = 1;
471 break;
Simon Glass37b80202016-01-14 18:10:38 -0700472 case '\a':
473 /* beep */
474 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700475 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700476 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700477 break;
478 case '\n':
479 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700480 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700481 break;
482 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700483 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
484 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700485
Simon Glass52c10c52016-01-14 18:10:37 -0700486 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700487 vidconsole_newline(dev);
488 break;
489 case '\b':
490 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700491 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700492 break;
493 default:
Janne Grunau5548c362024-03-16 22:50:19 +0100494 if (CONFIG_IS_ENABLED(CHARSET)) {
495 cp = utf8_to_utf32_stream(ch, priv->utf8_buf);
496 if (cp == 0)
497 return 0;
498 } else {
499 cp = ch;
500 }
501 ret = vidconsole_output_glyph(dev, cp);
Simon Glass52c10c52016-01-14 18:10:37 -0700502 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700503 return ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700504 break;
505 }
506
507 return 0;
508}
509
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200510int vidconsole_put_string(struct udevice *dev, const char *str)
511{
512 const char *s;
513 int ret;
514
515 for (s = str; *s; s++) {
516 ret = vidconsole_put_char(dev, *s);
517 if (ret)
518 return ret;
519 }
520
521 return 0;
522}
523
Simon Glass84c7fb32016-01-18 19:52:17 -0700524static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
525{
526 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600527 int ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700528
Simon Glassf97beb72020-07-02 21:12:14 -0600529 ret = vidconsole_put_char(dev, ch);
530 if (ret) {
531#ifdef DEBUG
532 console_puts_select_stderr(true, "[vc err: putc]");
533#endif
534 }
Michal Simek632e3d42020-12-14 08:47:52 +0100535 ret = video_sync(dev->parent, false);
536 if (ret) {
537#ifdef DEBUG
538 console_puts_select_stderr(true, "[vc err: video_sync]");
539#endif
540 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700541}
542
543static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
544{
545 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600546 int ret;
547
548 ret = vidconsole_put_string(dev, s);
549 if (ret) {
550#ifdef DEBUG
551 char str[30];
Simon Glass84c7fb32016-01-18 19:52:17 -0700552
Simon Glassf97beb72020-07-02 21:12:14 -0600553 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
554 console_puts_select_stderr(true, str);
555#endif
556 }
Michal Simek632e3d42020-12-14 08:47:52 +0100557 ret = video_sync(dev->parent, false);
558 if (ret) {
559#ifdef DEBUG
560 console_puts_select_stderr(true, "[vc err: video_sync]");
561#endif
562 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700563}
564
Simon Glass3b175ba2023-01-06 08:52:32 -0600565void vidconsole_list_fonts(struct udevice *dev)
566{
567 struct vidfont_info info;
568 int ret, i;
569
570 for (i = 0, ret = 0; !ret; i++) {
571 ret = vidconsole_get_font(dev, i, &info);
572 if (!ret)
573 printf("%s\n", info.name);
574 }
575}
576
577int vidconsole_get_font(struct udevice *dev, int seq,
578 struct vidfont_info *info)
579{
580 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
581
582 if (!ops->get_font)
583 return -ENOSYS;
584
585 return ops->get_font(dev, seq, info);
586}
587
Dzmitry Sankouski86c6a532023-03-07 13:21:15 +0300588int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
589{
590 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
591
592 if (!ops->get_font_size)
593 return -ENOSYS;
594
595 *name = ops->get_font_size(dev, sizep);
596 return 0;
597}
598
Simon Glass3b175ba2023-01-06 08:52:32 -0600599int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
600{
601 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
602
603 if (!ops->select_font)
604 return -ENOSYS;
605
606 return ops->select_font(dev, name, size);
607}
608
Simon Glass5caf1252023-06-01 10:22:46 -0600609int vidconsole_measure(struct udevice *dev, const char *name, uint size,
610 const char *text, struct vidconsole_bbox *bbox)
611{
612 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
613 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
614 int ret;
615
Bin Meng31aef742023-08-03 17:32:41 +0800616 if (ops->measure) {
Simon Glass5caf1252023-06-01 10:22:46 -0600617 ret = ops->measure(dev, name, size, text, bbox);
618 if (ret != -ENOSYS)
619 return ret;
620 }
621
622 bbox->valid = true;
623 bbox->x0 = 0;
624 bbox->y0 = 0;
625 bbox->x1 = priv->x_charsize * strlen(text);
626 bbox->y1 = priv->y_charsize;
627
628 return 0;
629}
630
Simon Glass8b82e592023-10-01 19:13:18 -0600631int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
632 uint num_chars, struct vidconsole_bbox *bbox)
633{
634 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
635 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
636 int ret;
637
638 if (ops->measure) {
639 ret = ops->nominal(dev, name, size, num_chars, bbox);
640 if (ret != -ENOSYS)
641 return ret;
642 }
643
644 bbox->valid = true;
645 bbox->x0 = 0;
646 bbox->y0 = 0;
647 bbox->x1 = priv->x_charsize * num_chars;
648 bbox->y1 = priv->y_charsize;
649
650 return 0;
651}
652
Simon Glass34b5c252023-10-01 19:13:19 -0600653int vidconsole_entry_save(struct udevice *dev, struct abuf *buf)
654{
655 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
656 int ret;
657
658 if (ops->measure) {
659 ret = ops->entry_save(dev, buf);
660 if (ret != -ENOSYS)
661 return ret;
662 }
663
664 /* no data so make sure the buffer is empty */
665 abuf_realloc(buf, 0);
666
667 return 0;
668}
669
670int vidconsole_entry_restore(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_restore(dev, buf);
677 if (ret != -ENOSYS)
678 return ret;
679 }
680
681 return 0;
682}
683
Simon Glass377f79aa2023-10-01 19:13:21 -0600684int vidconsole_set_cursor_visible(struct udevice *dev, bool visible,
685 uint x, uint y, uint index)
686{
687 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
688 int ret;
689
690 if (ops->set_cursor_visible) {
691 ret = ops->set_cursor_visible(dev, visible, x, y, index);
692 if (ret != -ENOSYS)
693 return ret;
694 }
695
696 return 0;
697}
698
Simon Glassa73a8b82023-06-01 10:22:45 -0600699void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
700 enum colour_idx bg, struct vidconsole_colour *old)
701{
702 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
703
704 old->colour_fg = vid_priv->colour_fg;
705 old->colour_bg = vid_priv->colour_bg;
706
707 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
708 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
709}
710
711void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
712{
713 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
714
715 vid_priv->colour_fg = old->colour_fg;
716 vid_priv->colour_bg = old->colour_bg;
717}
718
Simon Glass84c7fb32016-01-18 19:52:17 -0700719/* Set up the number of rows and colours (rotated drivers override this) */
720static int vidconsole_pre_probe(struct udevice *dev)
721{
722 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
723 struct udevice *vid = dev->parent;
724 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
725
Simon Glass52c10c52016-01-14 18:10:37 -0700726 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700727
728 return 0;
729}
730
731/* Register the device with stdio */
732static int vidconsole_post_probe(struct udevice *dev)
733{
734 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
735 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700736
Simon Glass52c10c52016-01-14 18:10:37 -0700737 if (!priv->tab_width_frac)
738 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
739
Simon Glass75e534b2020-12-16 21:20:07 -0700740 if (dev_seq(dev)) {
Simon Glass798ff502016-01-21 19:44:51 -0700741 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass75e534b2020-12-16 21:20:07 -0700742 dev_seq(dev));
Simon Glass798ff502016-01-21 19:44:51 -0700743 } else {
744 strcpy(sdev->name, "vidconsole");
745 }
Simon Glass52c10c52016-01-14 18:10:37 -0700746
Simon Glass84c7fb32016-01-18 19:52:17 -0700747 sdev->flags = DEV_FLAGS_OUTPUT;
748 sdev->putc = vidconsole_putc;
749 sdev->puts = vidconsole_puts;
750 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700751
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900752 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700753}
754
755UCLASS_DRIVER(vidconsole) = {
756 .id = UCLASS_VIDEO_CONSOLE,
757 .name = "vidconsole0",
758 .pre_probe = vidconsole_pre_probe,
759 .post_probe = vidconsole_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700760 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass84c7fb32016-01-18 19:52:17 -0700761};
762
Simon Glass31a7e232020-07-02 21:12:23 -0600763#ifdef CONFIG_VIDEO_COPY
764int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
765{
766 struct udevice *vid = dev_get_parent(dev);
767
768 return video_sync_copy(vid, from, to);
769}
770
771int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
772 int size)
773{
774 memmove(dst, src, size);
775 return vidconsole_sync_copy(dev, dst, dst + size);
776}
777#endif
Simon Glass90679c62023-03-10 12:47:21 -0800778
779int vidconsole_clear_and_reset(struct udevice *dev)
780{
781 int ret;
782
783 ret = video_clear(dev_get_parent(dev));
784 if (ret)
785 return ret;
786 vidconsole_position_cursor(dev, 0, 0);
787
788 return 0;
789}
Tom Rini2b99fd82023-03-15 11:58:58 -0400790
791void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
792{
793 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
794 struct udevice *vid_dev = dev->parent;
795 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
796 short x, y;
797
798 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
799 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
800 vidconsole_set_cursor_pos(dev, x, y);
801}