blob: ebe96bf0c2f38299f549d6eb2773f1049012d9fc [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
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200511int vidconsole_put_string(struct udevice *dev, const char *str)
512{
513 const char *s;
514 int ret;
515
516 for (s = str; *s; s++) {
517 ret = vidconsole_put_char(dev, *s);
518 if (ret)
519 return ret;
520 }
521
522 return 0;
523}
524
Simon Glass84c7fb32016-01-18 19:52:17 -0700525static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
526{
527 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600528 int ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700529
Simon Glassf97beb72020-07-02 21:12:14 -0600530 ret = vidconsole_put_char(dev, ch);
531 if (ret) {
532#ifdef DEBUG
533 console_puts_select_stderr(true, "[vc err: putc]");
534#endif
535 }
Michal Simek632e3d42020-12-14 08:47:52 +0100536 ret = video_sync(dev->parent, false);
537 if (ret) {
538#ifdef DEBUG
539 console_puts_select_stderr(true, "[vc err: video_sync]");
540#endif
541 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700542}
543
544static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
545{
546 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600547 int ret;
548
549 ret = vidconsole_put_string(dev, s);
550 if (ret) {
551#ifdef DEBUG
552 char str[30];
Simon Glass84c7fb32016-01-18 19:52:17 -0700553
Simon Glassf97beb72020-07-02 21:12:14 -0600554 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
555 console_puts_select_stderr(true, str);
556#endif
557 }
Michal Simek632e3d42020-12-14 08:47:52 +0100558 ret = video_sync(dev->parent, false);
559 if (ret) {
560#ifdef DEBUG
561 console_puts_select_stderr(true, "[vc err: video_sync]");
562#endif
563 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700564}
565
Simon Glass3b175ba2023-01-06 08:52:32 -0600566void vidconsole_list_fonts(struct udevice *dev)
567{
568 struct vidfont_info info;
569 int ret, i;
570
571 for (i = 0, ret = 0; !ret; i++) {
572 ret = vidconsole_get_font(dev, i, &info);
573 if (!ret)
574 printf("%s\n", info.name);
575 }
576}
577
578int vidconsole_get_font(struct udevice *dev, int seq,
579 struct vidfont_info *info)
580{
581 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
582
583 if (!ops->get_font)
584 return -ENOSYS;
585
586 return ops->get_font(dev, seq, info);
587}
588
Dzmitry Sankouski86c6a532023-03-07 13:21:15 +0300589int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
590{
591 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
592
593 if (!ops->get_font_size)
594 return -ENOSYS;
595
596 *name = ops->get_font_size(dev, sizep);
597 return 0;
598}
599
Simon Glass3b175ba2023-01-06 08:52:32 -0600600int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
601{
602 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
603
604 if (!ops->select_font)
605 return -ENOSYS;
606
607 return ops->select_font(dev, name, size);
608}
609
Simon Glass5caf1252023-06-01 10:22:46 -0600610int vidconsole_measure(struct udevice *dev, const char *name, uint size,
611 const char *text, struct vidconsole_bbox *bbox)
612{
613 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
614 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
615 int ret;
616
Bin Meng31aef742023-08-03 17:32:41 +0800617 if (ops->measure) {
Simon Glass5caf1252023-06-01 10:22:46 -0600618 ret = ops->measure(dev, name, size, text, bbox);
619 if (ret != -ENOSYS)
620 return ret;
621 }
622
623 bbox->valid = true;
624 bbox->x0 = 0;
625 bbox->y0 = 0;
626 bbox->x1 = priv->x_charsize * strlen(text);
627 bbox->y1 = priv->y_charsize;
628
629 return 0;
630}
631
Simon Glass8b82e592023-10-01 19:13:18 -0600632int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
633 uint num_chars, struct vidconsole_bbox *bbox)
634{
635 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
636 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
637 int ret;
638
639 if (ops->measure) {
640 ret = ops->nominal(dev, name, size, num_chars, bbox);
641 if (ret != -ENOSYS)
642 return ret;
643 }
644
645 bbox->valid = true;
646 bbox->x0 = 0;
647 bbox->y0 = 0;
648 bbox->x1 = priv->x_charsize * num_chars;
649 bbox->y1 = priv->y_charsize;
650
651 return 0;
652}
653
Simon Glass34b5c252023-10-01 19:13:19 -0600654int vidconsole_entry_save(struct udevice *dev, struct abuf *buf)
655{
656 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
657 int ret;
658
659 if (ops->measure) {
660 ret = ops->entry_save(dev, buf);
661 if (ret != -ENOSYS)
662 return ret;
663 }
664
665 /* no data so make sure the buffer is empty */
666 abuf_realloc(buf, 0);
667
668 return 0;
669}
670
671int vidconsole_entry_restore(struct udevice *dev, struct abuf *buf)
672{
673 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
674 int ret;
675
676 if (ops->measure) {
677 ret = ops->entry_restore(dev, buf);
678 if (ret != -ENOSYS)
679 return ret;
680 }
681
682 return 0;
683}
684
Simon Glass377f79aa2023-10-01 19:13:21 -0600685int vidconsole_set_cursor_visible(struct udevice *dev, bool visible,
686 uint x, uint y, uint index)
687{
688 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
689 int ret;
690
691 if (ops->set_cursor_visible) {
692 ret = ops->set_cursor_visible(dev, visible, x, y, index);
693 if (ret != -ENOSYS)
694 return ret;
695 }
696
697 return 0;
698}
699
Simon Glassa73a8b82023-06-01 10:22:45 -0600700void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
701 enum colour_idx bg, struct vidconsole_colour *old)
702{
703 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
704
705 old->colour_fg = vid_priv->colour_fg;
706 old->colour_bg = vid_priv->colour_bg;
707
708 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
709 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
710}
711
712void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
713{
714 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
715
716 vid_priv->colour_fg = old->colour_fg;
717 vid_priv->colour_bg = old->colour_bg;
718}
719
Simon Glass84c7fb32016-01-18 19:52:17 -0700720/* Set up the number of rows and colours (rotated drivers override this) */
721static int vidconsole_pre_probe(struct udevice *dev)
722{
723 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
724 struct udevice *vid = dev->parent;
725 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
726
Simon Glass52c10c52016-01-14 18:10:37 -0700727 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700728
729 return 0;
730}
731
732/* Register the device with stdio */
733static int vidconsole_post_probe(struct udevice *dev)
734{
735 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
736 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700737
Simon Glass52c10c52016-01-14 18:10:37 -0700738 if (!priv->tab_width_frac)
739 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
740
Simon Glass75e534b2020-12-16 21:20:07 -0700741 if (dev_seq(dev)) {
Simon Glass798ff502016-01-21 19:44:51 -0700742 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass75e534b2020-12-16 21:20:07 -0700743 dev_seq(dev));
Simon Glass798ff502016-01-21 19:44:51 -0700744 } else {
745 strcpy(sdev->name, "vidconsole");
746 }
Simon Glass52c10c52016-01-14 18:10:37 -0700747
Simon Glass84c7fb32016-01-18 19:52:17 -0700748 sdev->flags = DEV_FLAGS_OUTPUT;
749 sdev->putc = vidconsole_putc;
750 sdev->puts = vidconsole_puts;
751 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700752
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900753 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700754}
755
756UCLASS_DRIVER(vidconsole) = {
757 .id = UCLASS_VIDEO_CONSOLE,
758 .name = "vidconsole0",
759 .pre_probe = vidconsole_pre_probe,
760 .post_probe = vidconsole_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700761 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass84c7fb32016-01-18 19:52:17 -0700762};
763
Simon Glass31a7e232020-07-02 21:12:23 -0600764#ifdef CONFIG_VIDEO_COPY
765int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
766{
767 struct udevice *vid = dev_get_parent(dev);
768
769 return video_sync_copy(vid, from, to);
770}
771
772int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
773 int size)
774{
775 memmove(dst, src, size);
776 return vidconsole_sync_copy(dev, dst, dst + size);
777}
778#endif
Simon Glass90679c62023-03-10 12:47:21 -0800779
780int 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}