blob: 23b1b81731bc5c079349d2c4f2bba144ee8d61c4 [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 Glassed38aef2020-05-10 11:40:03 -060013#include <command.h>
Simon Glassf97beb72020-07-02 21:12:14 -060014#include <console.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070016#include <dm.h>
17#include <video.h>
18#include <video_console.h>
Heinrich Schuchardt2172fa42018-03-02 20:50:17 +010019#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glassf97beb72020-07-02 21:12:14 -060020#include <linux/ctype.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070021
Simon Glass84c7fb32016-01-18 19:52:17 -070022int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
23{
24 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
25
26 if (!ops->putc_xy)
27 return -ENOSYS;
28 return ops->putc_xy(dev, x, y, ch);
29}
30
31int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
32 uint count)
33{
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
35
36 if (!ops->move_rows)
37 return -ENOSYS;
38 return ops->move_rows(dev, rowdst, rowsrc, count);
39}
40
41int vidconsole_set_row(struct udevice *dev, uint row, int clr)
42{
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44
45 if (!ops->set_row)
46 return -ENOSYS;
47 return ops->set_row(dev, row, clr);
48}
49
Simon Glassafee7432016-01-14 18:10:40 -070050static int vidconsole_entry_start(struct udevice *dev)
51{
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53
54 if (!ops->entry_start)
55 return -ENOSYS;
56 return ops->entry_start(dev);
57}
58
Simon Glass84c7fb32016-01-18 19:52:17 -070059/* Move backwards one space */
Simon Glass33bd3b62016-01-14 18:10:41 -070060static int vidconsole_back(struct udevice *dev)
Simon Glass84c7fb32016-01-18 19:52:17 -070061{
62 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass33bd3b62016-01-14 18:10:41 -070063 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
64 int ret;
65
66 if (ops->backspace) {
67 ret = ops->backspace(dev);
68 if (ret != -ENOSYS)
69 return ret;
70 }
Simon Glass84c7fb32016-01-18 19:52:17 -070071
Simon Glass52c10c52016-01-14 18:10:37 -070072 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassa74451d2016-01-14 18:10:39 -070073 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glass52c10c52016-01-14 18:10:37 -070074 priv->xcur_frac = (priv->cols - 1) *
75 VID_TO_POS(priv->x_charsize);
76 priv->ycur -= priv->y_charsize;
77 if (priv->ycur < 0)
78 priv->ycur = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -070079 }
Michal Simek632e3d42020-12-14 08:47:52 +010080 return video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -070081}
82
83/* Move to a newline, scrolling the display if necessary */
84static void vidconsole_newline(struct udevice *dev)
85{
86 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
87 struct udevice *vid_dev = dev->parent;
88 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Nikhil M Jain9e3301d2023-04-20 17:41:08 +053089 const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
Michal Simek632e3d42020-12-14 08:47:52 +010090 int i, ret;
Simon Glass84c7fb32016-01-18 19:52:17 -070091
Simon Glassa74451d2016-01-14 18:10:39 -070092 priv->xcur_frac = priv->xstart_frac;
Simon Glass52c10c52016-01-14 18:10:37 -070093 priv->ycur += priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -070094
95 /* Check if we need to scroll the terminal */
Simon Glass52c10c52016-01-14 18:10:37 -070096 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass84c7fb32016-01-18 19:52:17 -070097 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
98 for (i = 0; i < rows; i++)
99 vidconsole_set_row(dev, priv->rows - i - 1,
100 vid_priv->colour_bg);
Simon Glass52c10c52016-01-14 18:10:37 -0700101 priv->ycur -= rows * priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -0700102 }
Simon Glassafee7432016-01-14 18:10:40 -0700103 priv->last_ch = 0;
104
Michal Simek632e3d42020-12-14 08:47:52 +0100105 ret = video_sync(dev->parent, false);
106 if (ret) {
107#ifdef DEBUG
108 console_puts_select_stderr(true, "[vc err: video_sync]");
109#endif
110 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700111}
112
Rob Clark06e7a0d2017-09-13 18:12:21 -0400113static char *parsenum(char *s, int *num)
114{
115 char *end;
116 *num = simple_strtol(s, &end, 10);
117 return end;
118}
119
Simon Glassd622c5b2022-10-06 08:36:04 -0600120void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
121{
122 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
123
124 priv->xcur_frac = VID_TO_POS(x);
125 priv->xstart_frac = priv->xcur_frac;
126 priv->ycur = y;
127}
128
Tom Rini2b99fd82023-03-15 11:58:58 -0400129/**
130 * set_cursor_position() - set cursor position
131 *
132 * @priv: private data of the video console
133 * @row: new row
134 * @col: new column
135 */
136static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200137{
Tom Rini2b99fd82023-03-15 11:58:58 -0400138 /*
139 * Ensure we stay in the bounds of the screen.
140 */
141 if (row >= priv->rows)
142 row = priv->rows - 1;
143 if (col >= priv->cols)
144 col = priv->cols - 1;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200145
Tom Rini2b99fd82023-03-15 11:58:58 -0400146 priv->ycur = row * priv->y_charsize;
147 priv->xcur_frac = priv->xstart_frac +
148 VID_TO_POS(col * priv->x_charsize);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200149}
150
151/**
152 * get_cursor_position() - get cursor position
153 *
154 * @priv: private data of the video console
155 * @row: row
156 * @col: column
157 */
158static void get_cursor_position(struct vidconsole_priv *priv,
159 int *row, int *col)
160{
161 *row = priv->ycur / priv->y_charsize;
162 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
163 priv->x_charsize;
164}
165
Rob Clark06e7a0d2017-09-13 18:12:21 -0400166/*
167 * Process a character while accumulating an escape string. Chars are
168 * accumulated into escape_buf until the end of escape sequence is
169 * found, at which point the sequence is parsed and processed.
170 */
171static void vidconsole_escape_char(struct udevice *dev, char ch)
172{
173 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
174
175 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
176 goto error;
177
178 /* Sanity checking for bogus ESC sequences: */
179 if (priv->escape_len >= sizeof(priv->escape_buf))
180 goto error;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200181 if (priv->escape_len == 0) {
182 switch (ch) {
183 case '7':
184 /* Save cursor position */
185 get_cursor_position(priv, &priv->row_saved,
186 &priv->col_saved);
187 priv->escape = 0;
188
189 return;
190 case '8': {
191 /* Restore cursor position */
192 int row = priv->row_saved;
193 int col = priv->col_saved;
194
Tom Rini2b99fd82023-03-15 11:58:58 -0400195 set_cursor_position(priv, row, col);
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200196 priv->escape = 0;
197 return;
198 }
199 case '[':
200 break;
201 default:
202 goto error;
203 }
204 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400205
206 priv->escape_buf[priv->escape_len++] = ch;
207
208 /*
209 * Escape sequences are terminated by a letter, so keep
210 * accumulating until we get one:
211 */
212 if (!isalpha(ch))
213 return;
214
215 /*
216 * clear escape mode first, otherwise things will get highly
217 * surprising if you hit any debug prints that come back to
218 * this console.
219 */
220 priv->escape = 0;
221
222 switch (ch) {
Andre Przywarad4a294c2019-03-23 01:29:57 +0000223 case 'A':
224 case 'B':
225 case 'C':
226 case 'D':
227 case 'E':
228 case 'F': {
229 int row, col, num;
230 char *s = priv->escape_buf;
231
232 /*
233 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
234 * Cursor left/right: [%dD, [%dC
235 */
236 s++; /* [ */
237 s = parsenum(s, &num);
238 if (num == 0) /* No digit in sequence ... */
239 num = 1; /* ... means "move by 1". */
240
241 get_cursor_position(priv, &row, &col);
242 if (ch == 'A' || ch == 'F')
243 row -= num;
244 if (ch == 'C')
245 col += num;
246 if (ch == 'D')
247 col -= num;
248 if (ch == 'B' || ch == 'E')
249 row += num;
250 if (ch == 'E' || ch == 'F')
251 col = 0;
252 if (col < 0)
253 col = 0;
254 if (row < 0)
255 row = 0;
256 /* Right and bottom overflows are handled in the callee. */
Tom Rini2b99fd82023-03-15 11:58:58 -0400257 set_cursor_position(priv, row, col);
Andre Przywarad4a294c2019-03-23 01:29:57 +0000258 break;
259 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400260 case 'H':
261 case 'f': {
262 int row, col;
263 char *s = priv->escape_buf;
264
265 /*
266 * Set cursor position: [%d;%df or [%d;%dH
267 */
268 s++; /* [ */
269 s = parsenum(s, &row);
270 s++; /* ; */
271 s = parsenum(s, &col);
272
Heinrich Schuchardtc3c69302018-11-10 19:55:48 +0100273 /*
274 * Video origin is [0, 0], terminal origin is [1, 1].
275 */
276 if (row)
277 --row;
278 if (col)
279 --col;
280
Tom Rini2b99fd82023-03-15 11:58:58 -0400281 set_cursor_position(priv, row, col);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400282
283 break;
284 }
285 case 'J': {
286 int mode;
287
288 /*
289 * Clear part/all screen:
290 * [J or [0J - clear screen from cursor down
291 * [1J - clear screen from cursor up
292 * [2J - clear entire screen
293 *
294 * TODO we really only handle entire-screen case, others
295 * probably require some additions to video-uclass (and
296 * are not really needed yet by efi_console)
297 */
298 parsenum(priv->escape_buf + 1, &mode);
299
300 if (mode == 2) {
Michal Simek632e3d42020-12-14 08:47:52 +0100301 int ret;
302
Rob Clark06e7a0d2017-09-13 18:12:21 -0400303 video_clear(dev->parent);
Michal Simek632e3d42020-12-14 08:47:52 +0100304 ret = video_sync(dev->parent, false);
305 if (ret) {
306#ifdef DEBUG
307 console_puts_select_stderr(true, "[vc err: video_sync]");
308#endif
309 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400310 priv->ycur = 0;
311 priv->xcur_frac = priv->xstart_frac;
312 } else {
313 debug("unsupported clear mode: %d\n", mode);
314 }
315 break;
316 }
Andre Przywara918622a2019-03-23 01:29:58 +0000317 case 'K': {
318 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
319 int mode;
320
321 /*
322 * Clear (parts of) current line
323 * [0K - clear line to end
324 * [2K - clear entire line
325 */
326 parsenum(priv->escape_buf + 1, &mode);
327
328 if (mode == 2) {
329 int row, col;
330
331 get_cursor_position(priv, &row, &col);
332 vidconsole_set_row(dev, row, vid_priv->colour_bg);
333 }
334 break;
335 }
Rob Clark50509bb2017-09-13 18:12:22 -0400336 case 'm': {
337 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
338 char *s = priv->escape_buf;
339 char *end = &priv->escape_buf[priv->escape_len];
340
341 /*
342 * Set graphics mode: [%d;...;%dm
343 *
344 * Currently only supports the color attributes:
345 *
346 * Foreground Colors:
347 *
348 * 30 Black
349 * 31 Red
350 * 32 Green
351 * 33 Yellow
352 * 34 Blue
353 * 35 Magenta
354 * 36 Cyan
355 * 37 White
356 *
357 * Background Colors:
358 *
359 * 40 Black
360 * 41 Red
361 * 42 Green
362 * 43 Yellow
363 * 44 Blue
364 * 45 Magenta
365 * 46 Cyan
366 * 47 White
367 */
368
369 s++; /* [ */
370 while (s < end) {
371 int val;
372
373 s = parsenum(s, &val);
374 s++;
375
376 switch (val) {
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100377 case 0:
378 /* all attributes off */
Simon Glass2b063b82018-11-06 15:21:36 -0700379 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100380 break;
381 case 1:
382 /* bold */
383 vid_priv->fg_col_idx |= 8;
Simon Glass2a006332022-10-06 08:36:03 -0600384 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100385 vid_priv, vid_priv->fg_col_idx);
386 break;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000387 case 7:
388 /* reverse video */
Simon Glass2a006332022-10-06 08:36:03 -0600389 vid_priv->colour_fg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000390 vid_priv, vid_priv->bg_col_idx);
Simon Glass2a006332022-10-06 08:36:03 -0600391 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000392 vid_priv, vid_priv->fg_col_idx);
393 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400394 case 30 ... 37:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100395 /* foreground color */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100396 vid_priv->fg_col_idx &= ~7;
397 vid_priv->fg_col_idx |= val - 30;
Simon Glass2a006332022-10-06 08:36:03 -0600398 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100399 vid_priv, vid_priv->fg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400400 break;
401 case 40 ... 47:
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000402 /* background color, also mask the bold bit */
403 vid_priv->bg_col_idx &= ~0xf;
404 vid_priv->bg_col_idx |= val - 40;
Simon Glass2a006332022-10-06 08:36:03 -0600405 vid_priv->colour_bg = video_index_to_colour(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000406 vid_priv, vid_priv->bg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400407 break;
408 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100409 /* ignore unsupported SGR parameter */
Rob Clark50509bb2017-09-13 18:12:22 -0400410 break;
411 }
412 }
413
414 break;
415 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400416 default:
417 debug("unrecognized escape sequence: %*s\n",
418 priv->escape_len, priv->escape_buf);
419 }
420
421 return;
422
423error:
424 /* something went wrong, just revert to normal mode: */
425 priv->escape = 0;
426}
427
Andre Przywarade86baf2019-03-23 01:29:59 +0000428/* Put that actual character on the screen (using the CP437 code page). */
429static int vidconsole_output_glyph(struct udevice *dev, char ch)
430{
431 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
432 int ret;
433
434 /*
435 * Failure of this function normally indicates an unsupported
436 * colour depth. Check this and return an error to help with
437 * diagnosis.
438 */
439 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
440 if (ret == -EAGAIN) {
441 vidconsole_newline(dev);
442 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
443 }
444 if (ret < 0)
445 return ret;
446 priv->xcur_frac += ret;
447 priv->last_ch = ch;
448 if (priv->xcur_frac >= priv->xsize_frac)
449 vidconsole_newline(dev);
450
451 return 0;
452}
453
Simon Glass84c7fb32016-01-18 19:52:17 -0700454int vidconsole_put_char(struct udevice *dev, char ch)
455{
456 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
457 int ret;
458
Rob Clark06e7a0d2017-09-13 18:12:21 -0400459 if (priv->escape) {
460 vidconsole_escape_char(dev, ch);
461 return 0;
462 }
463
Simon Glass84c7fb32016-01-18 19:52:17 -0700464 switch (ch) {
Rob Clark06e7a0d2017-09-13 18:12:21 -0400465 case '\x1b':
466 priv->escape_len = 0;
467 priv->escape = 1;
468 break;
Simon Glass37b80202016-01-14 18:10:38 -0700469 case '\a':
470 /* beep */
471 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700472 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700473 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700474 break;
475 case '\n':
476 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700477 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700478 break;
479 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700480 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
481 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700482
Simon Glass52c10c52016-01-14 18:10:37 -0700483 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700484 vidconsole_newline(dev);
485 break;
486 case '\b':
487 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700488 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700489 break;
490 default:
Andre Przywarade86baf2019-03-23 01:29:59 +0000491 ret = vidconsole_output_glyph(dev, ch);
Simon Glass52c10c52016-01-14 18:10:37 -0700492 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700493 return ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700494 break;
495 }
496
497 return 0;
498}
499
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200500int vidconsole_put_string(struct udevice *dev, const char *str)
501{
502 const char *s;
503 int ret;
504
505 for (s = str; *s; s++) {
506 ret = vidconsole_put_char(dev, *s);
507 if (ret)
508 return ret;
509 }
510
511 return 0;
512}
513
Simon Glass84c7fb32016-01-18 19:52:17 -0700514static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
515{
516 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600517 int ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700518
Simon Glassf97beb72020-07-02 21:12:14 -0600519 ret = vidconsole_put_char(dev, ch);
520 if (ret) {
521#ifdef DEBUG
522 console_puts_select_stderr(true, "[vc err: putc]");
523#endif
524 }
Michal Simek632e3d42020-12-14 08:47:52 +0100525 ret = video_sync(dev->parent, false);
526 if (ret) {
527#ifdef DEBUG
528 console_puts_select_stderr(true, "[vc err: video_sync]");
529#endif
530 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700531}
532
533static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
534{
535 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600536 int ret;
537
538 ret = vidconsole_put_string(dev, s);
539 if (ret) {
540#ifdef DEBUG
541 char str[30];
Simon Glass84c7fb32016-01-18 19:52:17 -0700542
Simon Glassf97beb72020-07-02 21:12:14 -0600543 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
544 console_puts_select_stderr(true, str);
545#endif
546 }
Michal Simek632e3d42020-12-14 08:47:52 +0100547 ret = video_sync(dev->parent, false);
548 if (ret) {
549#ifdef DEBUG
550 console_puts_select_stderr(true, "[vc err: video_sync]");
551#endif
552 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700553}
554
Simon Glass3b175ba2023-01-06 08:52:32 -0600555void vidconsole_list_fonts(struct udevice *dev)
556{
557 struct vidfont_info info;
558 int ret, i;
559
560 for (i = 0, ret = 0; !ret; i++) {
561 ret = vidconsole_get_font(dev, i, &info);
562 if (!ret)
563 printf("%s\n", info.name);
564 }
565}
566
567int vidconsole_get_font(struct udevice *dev, int seq,
568 struct vidfont_info *info)
569{
570 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
571
572 if (!ops->get_font)
573 return -ENOSYS;
574
575 return ops->get_font(dev, seq, info);
576}
577
Dzmitry Sankouski86c6a532023-03-07 13:21:15 +0300578int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
579{
580 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
581
582 if (!ops->get_font_size)
583 return -ENOSYS;
584
585 *name = ops->get_font_size(dev, sizep);
586 return 0;
587}
588
Simon Glass3b175ba2023-01-06 08:52:32 -0600589int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
590{
591 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
592
593 if (!ops->select_font)
594 return -ENOSYS;
595
596 return ops->select_font(dev, name, size);
597}
598
Simon Glass5caf1252023-06-01 10:22:46 -0600599int vidconsole_measure(struct udevice *dev, const char *name, uint size,
600 const char *text, struct vidconsole_bbox *bbox)
601{
602 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
603 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
604 int ret;
605
Bin Meng31aef742023-08-03 17:32:41 +0800606 if (ops->measure) {
Simon Glass5caf1252023-06-01 10:22:46 -0600607 ret = ops->measure(dev, name, size, text, bbox);
608 if (ret != -ENOSYS)
609 return ret;
610 }
611
612 bbox->valid = true;
613 bbox->x0 = 0;
614 bbox->y0 = 0;
615 bbox->x1 = priv->x_charsize * strlen(text);
616 bbox->y1 = priv->y_charsize;
617
618 return 0;
619}
620
Simon Glass8b82e592023-10-01 19:13:18 -0600621int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
622 uint num_chars, struct vidconsole_bbox *bbox)
623{
624 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
625 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
626 int ret;
627
628 if (ops->measure) {
629 ret = ops->nominal(dev, name, size, num_chars, bbox);
630 if (ret != -ENOSYS)
631 return ret;
632 }
633
634 bbox->valid = true;
635 bbox->x0 = 0;
636 bbox->y0 = 0;
637 bbox->x1 = priv->x_charsize * num_chars;
638 bbox->y1 = priv->y_charsize;
639
640 return 0;
641}
642
Simon Glassa73a8b82023-06-01 10:22:45 -0600643void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
644 enum colour_idx bg, struct vidconsole_colour *old)
645{
646 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
647
648 old->colour_fg = vid_priv->colour_fg;
649 old->colour_bg = vid_priv->colour_bg;
650
651 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
652 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
653}
654
655void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
656{
657 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
658
659 vid_priv->colour_fg = old->colour_fg;
660 vid_priv->colour_bg = old->colour_bg;
661}
662
Simon Glass84c7fb32016-01-18 19:52:17 -0700663/* Set up the number of rows and colours (rotated drivers override this) */
664static int vidconsole_pre_probe(struct udevice *dev)
665{
666 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
667 struct udevice *vid = dev->parent;
668 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
669
Simon Glass52c10c52016-01-14 18:10:37 -0700670 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700671
672 return 0;
673}
674
675/* Register the device with stdio */
676static int vidconsole_post_probe(struct udevice *dev)
677{
678 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
679 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700680
Simon Glass52c10c52016-01-14 18:10:37 -0700681 if (!priv->tab_width_frac)
682 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
683
Simon Glass75e534b2020-12-16 21:20:07 -0700684 if (dev_seq(dev)) {
Simon Glass798ff502016-01-21 19:44:51 -0700685 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass75e534b2020-12-16 21:20:07 -0700686 dev_seq(dev));
Simon Glass798ff502016-01-21 19:44:51 -0700687 } else {
688 strcpy(sdev->name, "vidconsole");
689 }
Simon Glass52c10c52016-01-14 18:10:37 -0700690
Simon Glass84c7fb32016-01-18 19:52:17 -0700691 sdev->flags = DEV_FLAGS_OUTPUT;
692 sdev->putc = vidconsole_putc;
693 sdev->puts = vidconsole_puts;
694 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700695
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900696 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700697}
698
699UCLASS_DRIVER(vidconsole) = {
700 .id = UCLASS_VIDEO_CONSOLE,
701 .name = "vidconsole0",
702 .pre_probe = vidconsole_pre_probe,
703 .post_probe = vidconsole_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700704 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass84c7fb32016-01-18 19:52:17 -0700705};
706
Simon Glass31a7e232020-07-02 21:12:23 -0600707#ifdef CONFIG_VIDEO_COPY
708int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
709{
710 struct udevice *vid = dev_get_parent(dev);
711
712 return video_sync_copy(vid, from, to);
713}
714
715int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
716 int size)
717{
718 memmove(dst, src, size);
719 return vidconsole_sync_copy(dev, dst, dst + size);
720}
721#endif
Simon Glass90679c62023-03-10 12:47:21 -0800722
723int vidconsole_clear_and_reset(struct udevice *dev)
724{
725 int ret;
726
727 ret = video_clear(dev_get_parent(dev));
728 if (ret)
729 return ret;
730 vidconsole_position_cursor(dev, 0, 0);
731
732 return 0;
733}
Tom Rini2b99fd82023-03-15 11:58:58 -0400734
735void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
736{
737 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
738 struct udevice *vid_dev = dev->parent;
739 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
740 short x, y;
741
742 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
743 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
744 vidconsole_set_cursor_pos(dev, x, y);
745}