blob: 22d55df71f63de1e90e657df51d205370ad71d8f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass83510762016-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 Glass83510762016-01-18 19:52:17 -07008 */
9
Patrick Delaunayb953ec22021-04-27 11:02:19 +020010#define LOG_CATEGORY UCLASS_VIDEO_CONSOLE
11
Simon Glass83510762016-01-18 19:52:17 -070012#include <common.h>
Simon Glass9899eef2023-10-01 19:13:19 -060013#include <abuf.h>
Simon Glass09140112020-05-10 11:40:03 -060014#include <command.h>
Simon Glass8b763df2020-07-02 21:12:14 -060015#include <console.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass83510762016-01-18 19:52:17 -070017#include <dm.h>
18#include <video.h>
19#include <video_console.h>
Heinrich Schuchardt5fba5322018-03-02 20:50:17 +010020#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glass8b763df2020-07-02 21:12:14 -060021#include <linux/ctype.h>
Simon Glass83510762016-01-18 19:52:17 -070022
Simon Glass83510762016-01-18 19:52:17 -070023int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
24{
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 Glass617d7b52023-10-01 19:13:20 -060051int vidconsole_entry_start(struct udevice *dev)
Simon Glass58c733a2016-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 Glass83510762016-01-18 19:52:17 -070060/* Move backwards one space */
Simon Glass7b9f7e42016-01-14 18:10:41 -070061static int vidconsole_back(struct udevice *dev)
Simon Glass83510762016-01-18 19:52:17 -070062{
63 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass7b9f7e42016-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 Glass83510762016-01-18 19:52:17 -070072
Simon Glassf2661782016-01-14 18:10:37 -070073 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassc5b77d02016-01-14 18:10:39 -070074 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glassf2661782016-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 Glass83510762016-01-18 19:52:17 -070080 }
Michal Simek9de731f2020-12-14 08:47:52 +010081 return video_sync(dev->parent, false);
Simon Glass83510762016-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 Jain86fbee62023-04-20 17:41:08 +053090 const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
Michal Simek9de731f2020-12-14 08:47:52 +010091 int i, ret;
Simon Glass83510762016-01-18 19:52:17 -070092
Simon Glassc5b77d02016-01-14 18:10:39 -070093 priv->xcur_frac = priv->xstart_frac;
Simon Glassf2661782016-01-14 18:10:37 -070094 priv->ycur += priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -070095
96 /* Check if we need to scroll the terminal */
Simon Glassf2661782016-01-14 18:10:37 -070097 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass83510762016-01-18 19:52:17 -070098 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
99 for (i = 0; i < rows; i++)
100 vidconsole_set_row(dev, priv->rows - i - 1,
101 vid_priv->colour_bg);
Simon Glassf2661782016-01-14 18:10:37 -0700102 priv->ycur -= rows * priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700103 }
Simon Glass58c733a2016-01-14 18:10:40 -0700104 priv->last_ch = 0;
105
Michal Simek9de731f2020-12-14 08:47:52 +0100106 ret = video_sync(dev->parent, false);
107 if (ret) {
108#ifdef DEBUG
109 console_puts_select_stderr(true, "[vc err: video_sync]");
110#endif
111 }
Simon Glass83510762016-01-18 19:52:17 -0700112}
113
Rob Clarka085aa12017-09-13 18:12:21 -0400114static char *parsenum(char *s, int *num)
115{
116 char *end;
117 *num = simple_strtol(s, &end, 10);
118 return end;
119}
120
Simon Glass6b6dc0d2022-10-06 08:36:04 -0600121void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
122{
123 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
124
125 priv->xcur_frac = VID_TO_POS(x);
126 priv->xstart_frac = priv->xcur_frac;
127 priv->ycur = y;
128}
129
Tom Rinif6546c72023-03-15 11:58:58 -0400130/**
131 * set_cursor_position() - set cursor position
132 *
133 * @priv: private data of the video console
134 * @row: new row
135 * @col: new column
136 */
137static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200138{
Tom Rinif6546c72023-03-15 11:58:58 -0400139 /*
140 * Ensure we stay in the bounds of the screen.
141 */
142 if (row >= priv->rows)
143 row = priv->rows - 1;
144 if (col >= priv->cols)
145 col = priv->cols - 1;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200146
Tom Rinif6546c72023-03-15 11:58:58 -0400147 priv->ycur = row * priv->y_charsize;
148 priv->xcur_frac = priv->xstart_frac +
149 VID_TO_POS(col * priv->x_charsize);
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200150}
151
152/**
153 * get_cursor_position() - get cursor position
154 *
155 * @priv: private data of the video console
156 * @row: row
157 * @col: column
158 */
159static void get_cursor_position(struct vidconsole_priv *priv,
160 int *row, int *col)
161{
162 *row = priv->ycur / priv->y_charsize;
163 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
164 priv->x_charsize;
165}
166
Rob Clarka085aa12017-09-13 18:12:21 -0400167/*
168 * Process a character while accumulating an escape string. Chars are
169 * accumulated into escape_buf until the end of escape sequence is
170 * found, at which point the sequence is parsed and processed.
171 */
172static void vidconsole_escape_char(struct udevice *dev, char ch)
173{
174 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
175
176 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
177 goto error;
178
179 /* Sanity checking for bogus ESC sequences: */
180 if (priv->escape_len >= sizeof(priv->escape_buf))
181 goto error;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200182 if (priv->escape_len == 0) {
183 switch (ch) {
184 case '7':
185 /* Save cursor position */
186 get_cursor_position(priv, &priv->row_saved,
187 &priv->col_saved);
188 priv->escape = 0;
189
190 return;
191 case '8': {
192 /* Restore cursor position */
193 int row = priv->row_saved;
194 int col = priv->col_saved;
195
Tom Rinif6546c72023-03-15 11:58:58 -0400196 set_cursor_position(priv, row, col);
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200197 priv->escape = 0;
198 return;
199 }
200 case '[':
201 break;
202 default:
203 goto error;
204 }
205 }
Rob Clarka085aa12017-09-13 18:12:21 -0400206
207 priv->escape_buf[priv->escape_len++] = ch;
208
209 /*
210 * Escape sequences are terminated by a letter, so keep
211 * accumulating until we get one:
212 */
213 if (!isalpha(ch))
214 return;
215
216 /*
217 * clear escape mode first, otherwise things will get highly
218 * surprising if you hit any debug prints that come back to
219 * this console.
220 */
221 priv->escape = 0;
222
223 switch (ch) {
Andre Przywara29c158b2019-03-23 01:29:57 +0000224 case 'A':
225 case 'B':
226 case 'C':
227 case 'D':
228 case 'E':
229 case 'F': {
230 int row, col, num;
231 char *s = priv->escape_buf;
232
233 /*
234 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
235 * Cursor left/right: [%dD, [%dC
236 */
237 s++; /* [ */
238 s = parsenum(s, &num);
239 if (num == 0) /* No digit in sequence ... */
240 num = 1; /* ... means "move by 1". */
241
242 get_cursor_position(priv, &row, &col);
243 if (ch == 'A' || ch == 'F')
244 row -= num;
245 if (ch == 'C')
246 col += num;
247 if (ch == 'D')
248 col -= num;
249 if (ch == 'B' || ch == 'E')
250 row += num;
251 if (ch == 'E' || ch == 'F')
252 col = 0;
253 if (col < 0)
254 col = 0;
255 if (row < 0)
256 row = 0;
257 /* Right and bottom overflows are handled in the callee. */
Tom Rinif6546c72023-03-15 11:58:58 -0400258 set_cursor_position(priv, row, col);
Andre Przywara29c158b2019-03-23 01:29:57 +0000259 break;
260 }
Rob Clarka085aa12017-09-13 18:12:21 -0400261 case 'H':
262 case 'f': {
263 int row, col;
264 char *s = priv->escape_buf;
265
266 /*
267 * Set cursor position: [%d;%df or [%d;%dH
268 */
269 s++; /* [ */
270 s = parsenum(s, &row);
271 s++; /* ; */
272 s = parsenum(s, &col);
273
Heinrich Schuchardt118f0202018-11-10 19:55:48 +0100274 /*
275 * Video origin is [0, 0], terminal origin is [1, 1].
276 */
277 if (row)
278 --row;
279 if (col)
280 --col;
281
Tom Rinif6546c72023-03-15 11:58:58 -0400282 set_cursor_position(priv, row, col);
Rob Clarka085aa12017-09-13 18:12:21 -0400283
284 break;
285 }
286 case 'J': {
287 int mode;
288
289 /*
290 * Clear part/all screen:
291 * [J or [0J - clear screen from cursor down
292 * [1J - clear screen from cursor up
293 * [2J - clear entire screen
294 *
295 * TODO we really only handle entire-screen case, others
296 * probably require some additions to video-uclass (and
297 * are not really needed yet by efi_console)
298 */
299 parsenum(priv->escape_buf + 1, &mode);
300
301 if (mode == 2) {
Michal Simek9de731f2020-12-14 08:47:52 +0100302 int ret;
303
Rob Clarka085aa12017-09-13 18:12:21 -0400304 video_clear(dev->parent);
Michal Simek9de731f2020-12-14 08:47:52 +0100305 ret = video_sync(dev->parent, false);
306 if (ret) {
307#ifdef DEBUG
308 console_puts_select_stderr(true, "[vc err: video_sync]");
309#endif
310 }
Rob Clarka085aa12017-09-13 18:12:21 -0400311 priv->ycur = 0;
312 priv->xcur_frac = priv->xstart_frac;
313 } else {
314 debug("unsupported clear mode: %d\n", mode);
315 }
316 break;
317 }
Andre Przywara44222942019-03-23 01:29:58 +0000318 case 'K': {
319 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
320 int mode;
321
322 /*
323 * Clear (parts of) current line
324 * [0K - clear line to end
325 * [2K - clear entire line
326 */
327 parsenum(priv->escape_buf + 1, &mode);
328
329 if (mode == 2) {
330 int row, col;
331
332 get_cursor_position(priv, &row, &col);
333 vidconsole_set_row(dev, row, vid_priv->colour_bg);
334 }
335 break;
336 }
Rob Clark703d8852017-09-13 18:12:22 -0400337 case 'm': {
338 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
339 char *s = priv->escape_buf;
340 char *end = &priv->escape_buf[priv->escape_len];
341
342 /*
343 * Set graphics mode: [%d;...;%dm
344 *
345 * Currently only supports the color attributes:
346 *
347 * Foreground Colors:
348 *
349 * 30 Black
350 * 31 Red
351 * 32 Green
352 * 33 Yellow
353 * 34 Blue
354 * 35 Magenta
355 * 36 Cyan
356 * 37 White
357 *
358 * Background Colors:
359 *
360 * 40 Black
361 * 41 Red
362 * 42 Green
363 * 43 Yellow
364 * 44 Blue
365 * 45 Magenta
366 * 46 Cyan
367 * 47 White
368 */
369
370 s++; /* [ */
371 while (s < end) {
372 int val;
373
374 s = parsenum(s, &val);
375 s++;
376
377 switch (val) {
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100378 case 0:
379 /* all attributes off */
Simon Glassb9f210a2018-11-06 15:21:36 -0700380 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100381 break;
382 case 1:
383 /* bold */
384 vid_priv->fg_col_idx |= 8;
Simon Glassa032e4b2022-10-06 08:36:03 -0600385 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100386 vid_priv, vid_priv->fg_col_idx);
387 break;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000388 case 7:
389 /* reverse video */
Simon Glassa032e4b2022-10-06 08:36:03 -0600390 vid_priv->colour_fg = video_index_to_colour(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000391 vid_priv, vid_priv->bg_col_idx);
Simon Glassa032e4b2022-10-06 08:36:03 -0600392 vid_priv->colour_bg = video_index_to_colour(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000393 vid_priv, vid_priv->fg_col_idx);
394 break;
Rob Clark703d8852017-09-13 18:12:22 -0400395 case 30 ... 37:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100396 /* foreground color */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100397 vid_priv->fg_col_idx &= ~7;
398 vid_priv->fg_col_idx |= val - 30;
Simon Glassa032e4b2022-10-06 08:36:03 -0600399 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100400 vid_priv, vid_priv->fg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400401 break;
402 case 40 ... 47:
Andre Przywaraeabb0722019-03-23 01:29:56 +0000403 /* background color, also mask the bold bit */
404 vid_priv->bg_col_idx &= ~0xf;
405 vid_priv->bg_col_idx |= val - 40;
Simon Glassa032e4b2022-10-06 08:36:03 -0600406 vid_priv->colour_bg = video_index_to_colour(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000407 vid_priv, vid_priv->bg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400408 break;
409 default:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100410 /* ignore unsupported SGR parameter */
Rob Clark703d8852017-09-13 18:12:22 -0400411 break;
412 }
413 }
414
415 break;
416 }
Rob Clarka085aa12017-09-13 18:12:21 -0400417 default:
418 debug("unrecognized escape sequence: %*s\n",
419 priv->escape_len, priv->escape_buf);
420 }
421
422 return;
423
424error:
425 /* something went wrong, just revert to normal mode: */
426 priv->escape = 0;
427}
428
Andre Przywara7035ec32019-03-23 01:29:59 +0000429/* Put that actual character on the screen (using the CP437 code page). */
430static int vidconsole_output_glyph(struct udevice *dev, char ch)
431{
432 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
433 int ret;
434
435 /*
436 * Failure of this function normally indicates an unsupported
437 * colour depth. Check this and return an error to help with
438 * diagnosis.
439 */
440 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
441 if (ret == -EAGAIN) {
442 vidconsole_newline(dev);
443 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
444 }
445 if (ret < 0)
446 return ret;
447 priv->xcur_frac += ret;
448 priv->last_ch = ch;
449 if (priv->xcur_frac >= priv->xsize_frac)
450 vidconsole_newline(dev);
451
452 return 0;
453}
454
Simon Glass83510762016-01-18 19:52:17 -0700455int vidconsole_put_char(struct udevice *dev, char ch)
456{
457 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
458 int ret;
459
Rob Clarka085aa12017-09-13 18:12:21 -0400460 if (priv->escape) {
461 vidconsole_escape_char(dev, ch);
462 return 0;
463 }
464
Simon Glass83510762016-01-18 19:52:17 -0700465 switch (ch) {
Rob Clarka085aa12017-09-13 18:12:21 -0400466 case '\x1b':
467 priv->escape_len = 0;
468 priv->escape = 1;
469 break;
Simon Glass5508f102016-01-14 18:10:38 -0700470 case '\a':
471 /* beep */
472 break;
Simon Glass83510762016-01-18 19:52:17 -0700473 case '\r':
Simon Glassc5b77d02016-01-14 18:10:39 -0700474 priv->xcur_frac = priv->xstart_frac;
Simon Glass83510762016-01-18 19:52:17 -0700475 break;
476 case '\n':
477 vidconsole_newline(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700478 vidconsole_entry_start(dev);
Simon Glass83510762016-01-18 19:52:17 -0700479 break;
480 case '\t': /* Tab (8 chars alignment) */
Simon Glassf2661782016-01-14 18:10:37 -0700481 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
482 + 1) * priv->tab_width_frac;
Simon Glass83510762016-01-18 19:52:17 -0700483
Simon Glassf2661782016-01-14 18:10:37 -0700484 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700485 vidconsole_newline(dev);
486 break;
487 case '\b':
488 vidconsole_back(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700489 priv->last_ch = 0;
Simon Glass83510762016-01-18 19:52:17 -0700490 break;
491 default:
Andre Przywara7035ec32019-03-23 01:29:59 +0000492 ret = vidconsole_output_glyph(dev, ch);
Simon Glassf2661782016-01-14 18:10:37 -0700493 if (ret < 0)
Simon Glass83510762016-01-18 19:52:17 -0700494 return ret;
Simon Glass83510762016-01-18 19:52:17 -0700495 break;
496 }
497
498 return 0;
499}
500
Marek Vasute63168a2019-05-17 20:22:31 +0200501int vidconsole_put_string(struct udevice *dev, const char *str)
502{
503 const char *s;
504 int ret;
505
506 for (s = str; *s; s++) {
507 ret = vidconsole_put_char(dev, *s);
508 if (ret)
509 return ret;
510 }
511
512 return 0;
513}
514
Simon Glass83510762016-01-18 19:52:17 -0700515static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
516{
517 struct udevice *dev = sdev->priv;
Simon Glass8b763df2020-07-02 21:12:14 -0600518 int ret;
Simon Glass83510762016-01-18 19:52:17 -0700519
Simon Glass8b763df2020-07-02 21:12:14 -0600520 ret = vidconsole_put_char(dev, ch);
521 if (ret) {
522#ifdef DEBUG
523 console_puts_select_stderr(true, "[vc err: putc]");
524#endif
525 }
Michal Simek9de731f2020-12-14 08:47:52 +0100526 ret = video_sync(dev->parent, false);
527 if (ret) {
528#ifdef DEBUG
529 console_puts_select_stderr(true, "[vc err: video_sync]");
530#endif
531 }
Simon Glass83510762016-01-18 19:52:17 -0700532}
533
534static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
535{
536 struct udevice *dev = sdev->priv;
Simon Glass8b763df2020-07-02 21:12:14 -0600537 int ret;
Simon Glass83510762016-01-18 19:52:17 -0700538
Simon Glass8b763df2020-07-02 21:12:14 -0600539 ret = vidconsole_put_string(dev, s);
540 if (ret) {
541#ifdef DEBUG
542 char str[30];
543
544 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
545 console_puts_select_stderr(true, str);
546#endif
547 }
Michal Simek9de731f2020-12-14 08:47:52 +0100548 ret = video_sync(dev->parent, false);
549 if (ret) {
550#ifdef DEBUG
551 console_puts_select_stderr(true, "[vc err: video_sync]");
552#endif
553 }
Simon Glass83510762016-01-18 19:52:17 -0700554}
555
Simon Glass0e38bd82023-01-06 08:52:32 -0600556void vidconsole_list_fonts(struct udevice *dev)
557{
558 struct vidfont_info info;
559 int ret, i;
560
561 for (i = 0, ret = 0; !ret; i++) {
562 ret = vidconsole_get_font(dev, i, &info);
563 if (!ret)
564 printf("%s\n", info.name);
565 }
566}
567
568int vidconsole_get_font(struct udevice *dev, int seq,
569 struct vidfont_info *info)
570{
571 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
572
573 if (!ops->get_font)
574 return -ENOSYS;
575
576 return ops->get_font(dev, seq, info);
577}
578
Dzmitry Sankouski4f6e3482023-03-07 13:21:15 +0300579int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
580{
581 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
582
583 if (!ops->get_font_size)
584 return -ENOSYS;
585
586 *name = ops->get_font_size(dev, sizep);
587 return 0;
588}
589
Simon Glass0e38bd82023-01-06 08:52:32 -0600590int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
591{
592 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
593
594 if (!ops->select_font)
595 return -ENOSYS;
596
597 return ops->select_font(dev, name, size);
598}
599
Simon Glassb828ed72023-06-01 10:22:46 -0600600int vidconsole_measure(struct udevice *dev, const char *name, uint size,
601 const char *text, struct vidconsole_bbox *bbox)
602{
603 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
604 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
605 int ret;
606
Bin Meng01c76f12023-08-03 17:32:41 +0800607 if (ops->measure) {
Simon Glassb828ed72023-06-01 10:22:46 -0600608 ret = ops->measure(dev, name, size, text, bbox);
609 if (ret != -ENOSYS)
610 return ret;
611 }
612
613 bbox->valid = true;
614 bbox->x0 = 0;
615 bbox->y0 = 0;
616 bbox->x1 = priv->x_charsize * strlen(text);
617 bbox->y1 = priv->y_charsize;
618
619 return 0;
620}
621
Simon Glass9e55d092023-10-01 19:13:18 -0600622int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
623 uint num_chars, struct vidconsole_bbox *bbox)
624{
625 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
626 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
627 int ret;
628
629 if (ops->measure) {
630 ret = ops->nominal(dev, name, size, num_chars, bbox);
631 if (ret != -ENOSYS)
632 return ret;
633 }
634
635 bbox->valid = true;
636 bbox->x0 = 0;
637 bbox->y0 = 0;
638 bbox->x1 = priv->x_charsize * num_chars;
639 bbox->y1 = priv->y_charsize;
640
641 return 0;
642}
643
Simon Glass9899eef2023-10-01 19:13:19 -0600644int vidconsole_entry_save(struct udevice *dev, struct abuf *buf)
645{
646 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
647 int ret;
648
649 if (ops->measure) {
650 ret = ops->entry_save(dev, buf);
651 if (ret != -ENOSYS)
652 return ret;
653 }
654
655 /* no data so make sure the buffer is empty */
656 abuf_realloc(buf, 0);
657
658 return 0;
659}
660
661int vidconsole_entry_restore(struct udevice *dev, struct abuf *buf)
662{
663 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
664 int ret;
665
666 if (ops->measure) {
667 ret = ops->entry_restore(dev, buf);
668 if (ret != -ENOSYS)
669 return ret;
670 }
671
672 return 0;
673}
674
Simon Glass37db20d2023-10-01 19:13:21 -0600675int vidconsole_set_cursor_visible(struct udevice *dev, bool visible,
676 uint x, uint y, uint index)
677{
678 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
679 int ret;
680
681 if (ops->set_cursor_visible) {
682 ret = ops->set_cursor_visible(dev, visible, x, y, index);
683 if (ret != -ENOSYS)
684 return ret;
685 }
686
687 return 0;
688}
689
Simon Glass648a4992023-06-01 10:22:45 -0600690void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
691 enum colour_idx bg, struct vidconsole_colour *old)
692{
693 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
694
695 old->colour_fg = vid_priv->colour_fg;
696 old->colour_bg = vid_priv->colour_bg;
697
698 vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
699 vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
700}
701
702void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
703{
704 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
705
706 vid_priv->colour_fg = old->colour_fg;
707 vid_priv->colour_bg = old->colour_bg;
708}
709
Simon Glass83510762016-01-18 19:52:17 -0700710/* Set up the number of rows and colours (rotated drivers override this) */
711static int vidconsole_pre_probe(struct udevice *dev)
712{
713 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
714 struct udevice *vid = dev->parent;
715 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
716
Simon Glassf2661782016-01-14 18:10:37 -0700717 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass83510762016-01-18 19:52:17 -0700718
719 return 0;
720}
721
722/* Register the device with stdio */
723static int vidconsole_post_probe(struct udevice *dev)
724{
725 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
726 struct stdio_dev *sdev = &priv->sdev;
Simon Glass83510762016-01-18 19:52:17 -0700727
Simon Glassf2661782016-01-14 18:10:37 -0700728 if (!priv->tab_width_frac)
729 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
730
Simon Glass8b85dfc2020-12-16 21:20:07 -0700731 if (dev_seq(dev)) {
Simon Glassf1a12472016-01-21 19:44:51 -0700732 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass8b85dfc2020-12-16 21:20:07 -0700733 dev_seq(dev));
Simon Glassf1a12472016-01-21 19:44:51 -0700734 } else {
735 strcpy(sdev->name, "vidconsole");
736 }
Simon Glassf2661782016-01-14 18:10:37 -0700737
Simon Glass83510762016-01-18 19:52:17 -0700738 sdev->flags = DEV_FLAGS_OUTPUT;
739 sdev->putc = vidconsole_putc;
740 sdev->puts = vidconsole_puts;
741 sdev->priv = dev;
Simon Glass83510762016-01-18 19:52:17 -0700742
Masahiro Yamada720873b2016-09-06 22:17:33 +0900743 return stdio_register(sdev);
Simon Glass83510762016-01-18 19:52:17 -0700744}
745
746UCLASS_DRIVER(vidconsole) = {
747 .id = UCLASS_VIDEO_CONSOLE,
748 .name = "vidconsole0",
749 .pre_probe = vidconsole_pre_probe,
750 .post_probe = vidconsole_post_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700751 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass83510762016-01-18 19:52:17 -0700752};
753
Simon Glass8c0b5d22020-07-02 21:12:23 -0600754#ifdef CONFIG_VIDEO_COPY
755int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
756{
757 struct udevice *vid = dev_get_parent(dev);
758
759 return video_sync_copy(vid, from, to);
760}
761
762int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
763 int size)
764{
765 memmove(dst, src, size);
766 return vidconsole_sync_copy(dev, dst, dst + size);
767}
768#endif
Simon Glassa76b60f2023-03-10 12:47:21 -0800769
770int vidconsole_clear_and_reset(struct udevice *dev)
771{
772 int ret;
773
774 ret = video_clear(dev_get_parent(dev));
775 if (ret)
776 return ret;
777 vidconsole_position_cursor(dev, 0, 0);
778
779 return 0;
780}
Tom Rinif6546c72023-03-15 11:58:58 -0400781
782void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
783{
784 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
785 struct udevice *vid_dev = dev->parent;
786 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
787 short x, y;
788
789 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
790 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
791 vidconsole_set_cursor_pos(dev, x, y);
792}