blob: fa329bd1b37fdb1021dd5c0f66d22eb1e3771ccf [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 Glassf97beb72020-07-02 21:12:14 -0600535 int ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700536
Simon Glassf97beb72020-07-02 21:12:14 -0600537 ret = vidconsole_put_char(dev, ch);
538 if (ret) {
539#ifdef DEBUG
540 console_puts_select_stderr(true, "[vc err: putc]");
541#endif
542 }
Michal Simek632e3d42020-12-14 08:47:52 +0100543 ret = video_sync(dev->parent, false);
544 if (ret) {
545#ifdef DEBUG
546 console_puts_select_stderr(true, "[vc err: video_sync]");
547#endif
548 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700549}
550
551static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
552{
553 struct udevice *dev = sdev->priv;
Simon Glassf97beb72020-07-02 21:12:14 -0600554 int ret;
555
556 ret = vidconsole_put_string(dev, s);
557 if (ret) {
558#ifdef DEBUG
559 char str[30];
Simon Glass84c7fb32016-01-18 19:52:17 -0700560
Simon Glassf97beb72020-07-02 21:12:14 -0600561 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
562 console_puts_select_stderr(true, str);
563#endif
564 }
Michal Simek632e3d42020-12-14 08:47:52 +0100565 ret = video_sync(dev->parent, false);
566 if (ret) {
567#ifdef DEBUG
568 console_puts_select_stderr(true, "[vc err: video_sync]");
569#endif
570 }
Simon Glass84c7fb32016-01-18 19:52:17 -0700571}
572
Simon Glass3b175ba2023-01-06 08:52:32 -0600573void vidconsole_list_fonts(struct udevice *dev)
574{
575 struct vidfont_info info;
576 int ret, i;
577
578 for (i = 0, ret = 0; !ret; i++) {
579 ret = vidconsole_get_font(dev, i, &info);
580 if (!ret)
581 printf("%s\n", info.name);
582 }
583}
584
585int vidconsole_get_font(struct udevice *dev, int seq,
586 struct vidfont_info *info)
587{
588 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
589
590 if (!ops->get_font)
591 return -ENOSYS;
592
593 return ops->get_font(dev, seq, info);
594}
595
Dzmitry Sankouski86c6a532023-03-07 13:21:15 +0300596int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
597{
598 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
599
600 if (!ops->get_font_size)
601 return -ENOSYS;
602
603 *name = ops->get_font_size(dev, sizep);
604 return 0;
605}
606
Simon Glass3b175ba2023-01-06 08:52:32 -0600607int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
608{
609 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
610
611 if (!ops->select_font)
612 return -ENOSYS;
613
614 return ops->select_font(dev, name, size);
615}
616
Simon Glass5caf1252023-06-01 10:22:46 -0600617int vidconsole_measure(struct udevice *dev, const char *name, uint size,
Simon Glass3aa33582025-04-02 06:29:39 +1300618 const char *text, int limit,
619 struct vidconsole_bbox *bbox, struct alist *lines)
Simon Glass5caf1252023-06-01 10:22:46 -0600620{
621 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
622 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
623 int ret;
624
Bin Meng31aef742023-08-03 17:32:41 +0800625 if (ops->measure) {
Simon Glass95bcad42025-04-02 06:29:36 +1300626 if (lines)
627 alist_empty(lines);
Simon Glass3aa33582025-04-02 06:29:39 +1300628 ret = ops->measure(dev, name, size, text, limit, bbox, lines);
Simon Glass5caf1252023-06-01 10:22:46 -0600629 if (ret != -ENOSYS)
630 return ret;
631 }
632
633 bbox->valid = true;
634 bbox->x0 = 0;
635 bbox->y0 = 0;
636 bbox->x1 = priv->x_charsize * strlen(text);
637 bbox->y1 = priv->y_charsize;
638
639 return 0;
640}
641
Simon Glass8b82e592023-10-01 19:13:18 -0600642int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
643 uint num_chars, struct vidconsole_bbox *bbox)
644{
645 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
646 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
647 int ret;
648
649 if (ops->measure) {
650 ret = ops->nominal(dev, name, size, num_chars, bbox);
651 if (ret != -ENOSYS)
652 return ret;
653 }
654
655 bbox->valid = true;
656 bbox->x0 = 0;
657 bbox->y0 = 0;
658 bbox->x1 = priv->x_charsize * num_chars;
659 bbox->y1 = priv->y_charsize;
660
661 return 0;
662}
663
Simon Glass34b5c252023-10-01 19:13:19 -0600664int vidconsole_entry_save(struct udevice *dev, struct abuf *buf)
665{
666 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
667 int ret;
668
669 if (ops->measure) {
670 ret = ops->entry_save(dev, buf);
671 if (ret != -ENOSYS)
672 return ret;
673 }
674
675 /* no data so make sure the buffer is empty */
676 abuf_realloc(buf, 0);
677
678 return 0;
679}
680
681int vidconsole_entry_restore(struct udevice *dev, struct abuf *buf)
682{
683 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
684 int ret;
685
686 if (ops->measure) {
687 ret = ops->entry_restore(dev, buf);
688 if (ret != -ENOSYS)
689 return ret;
690 }
691
692 return 0;
693}
694
Simon Glass377f79aa2023-10-01 19:13:21 -0600695int vidconsole_set_cursor_visible(struct udevice *dev, bool visible,
696 uint x, uint y, uint index)
697{
698 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
699 int ret;
700
701 if (ops->set_cursor_visible) {
702 ret = ops->set_cursor_visible(dev, visible, x, y, index);
703 if (ret != -ENOSYS)
704 return ret;
705 }
706
707 return 0;
708}
709
Simon Glassa73a8b82023-06-01 10:22:45 -0600710void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
711 enum colour_idx bg, struct vidconsole_colour *old)
712{
713 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
714
715 old->colour_fg = vid_priv->colour_fg;
716 old->colour_bg = vid_priv->colour_bg;
717
718 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
719 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
720}
721
722void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
723{
724 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
725
726 vid_priv->colour_fg = old->colour_fg;
727 vid_priv->colour_bg = old->colour_bg;
728}
729
Simon Glass84c7fb32016-01-18 19:52:17 -0700730/* Set up the number of rows and colours (rotated drivers override this) */
731static int vidconsole_pre_probe(struct udevice *dev)
732{
733 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
734 struct udevice *vid = dev->parent;
735 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
736
Simon Glass52c10c52016-01-14 18:10:37 -0700737 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700738
739 return 0;
740}
741
742/* Register the device with stdio */
743static int vidconsole_post_probe(struct udevice *dev)
744{
745 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
746 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700747
Simon Glass52c10c52016-01-14 18:10:37 -0700748 if (!priv->tab_width_frac)
749 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
750
Simon Glass75e534b2020-12-16 21:20:07 -0700751 if (dev_seq(dev)) {
Simon Glass798ff502016-01-21 19:44:51 -0700752 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass75e534b2020-12-16 21:20:07 -0700753 dev_seq(dev));
Simon Glass798ff502016-01-21 19:44:51 -0700754 } else {
755 strcpy(sdev->name, "vidconsole");
756 }
Simon Glass52c10c52016-01-14 18:10:37 -0700757
Simon Glass84c7fb32016-01-18 19:52:17 -0700758 sdev->flags = DEV_FLAGS_OUTPUT;
759 sdev->putc = vidconsole_putc;
760 sdev->puts = vidconsole_puts;
761 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700762
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900763 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700764}
765
766UCLASS_DRIVER(vidconsole) = {
767 .id = UCLASS_VIDEO_CONSOLE,
768 .name = "vidconsole0",
769 .pre_probe = vidconsole_pre_probe,
770 .post_probe = vidconsole_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700771 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass84c7fb32016-01-18 19:52:17 -0700772};
773
Simon Glass90679c62023-03-10 12:47:21 -0800774int vidconsole_clear_and_reset(struct udevice *dev)
775{
776 int ret;
777
778 ret = video_clear(dev_get_parent(dev));
779 if (ret)
780 return ret;
781 vidconsole_position_cursor(dev, 0, 0);
782
783 return 0;
784}
Tom Rini2b99fd82023-03-15 11:58:58 -0400785
786void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
787{
788 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
789 struct udevice *vid_dev = dev->parent;
790 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
791 short x, y;
792
793 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
794 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
795 vidconsole_set_cursor_pos(dev, x, y);
796}