]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/video/vidconsole-uclass.c
video/console: Implement relative cursor movement ANSI handling
[thirdparty/u-boot.git] / drivers / video / vidconsole-uclass.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
83510766
SG
2/*
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
83510766
SG
8 */
9
10#include <common.h>
a085aa1f 11#include <linux/ctype.h>
83510766
SG
12#include <dm.h>
13#include <video.h>
14#include <video_console.h>
5fba5329 15#include <video_font.h> /* Bitmap font for code page 437 */
83510766 16
5c30fbb8
HS
17/*
18 * Structure to describe a console color
19 */
20struct vid_rgb {
21 u32 r;
22 u32 g;
23 u32 b;
24};
25
83510766
SG
26/* By default we scroll by a single line */
27#ifndef CONFIG_CONSOLE_SCROLL_LINES
28#define CONFIG_CONSOLE_SCROLL_LINES 1
29#endif
30
31int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
32{
33 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
34
35 if (!ops->putc_xy)
36 return -ENOSYS;
37 return ops->putc_xy(dev, x, y, ch);
38}
39
40int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
41 uint count)
42{
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44
45 if (!ops->move_rows)
46 return -ENOSYS;
47 return ops->move_rows(dev, rowdst, rowsrc, count);
48}
49
50int vidconsole_set_row(struct udevice *dev, uint row, int clr)
51{
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53
54 if (!ops->set_row)
55 return -ENOSYS;
56 return ops->set_row(dev, row, clr);
57}
58
58c733a7
SG
59static int vidconsole_entry_start(struct udevice *dev)
60{
61 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
62
63 if (!ops->entry_start)
64 return -ENOSYS;
65 return ops->entry_start(dev);
66}
67
83510766 68/* Move backwards one space */
7b9f7e44 69static int vidconsole_back(struct udevice *dev)
83510766
SG
70{
71 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
7b9f7e44
SG
72 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
73 int ret;
74
75 if (ops->backspace) {
76 ret = ops->backspace(dev);
77 if (ret != -ENOSYS)
78 return ret;
79 }
83510766 80
f2661786 81 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
c5b77d01 82 if (priv->xcur_frac < priv->xstart_frac) {
f2661786
SG
83 priv->xcur_frac = (priv->cols - 1) *
84 VID_TO_POS(priv->x_charsize);
85 priv->ycur -= priv->y_charsize;
86 if (priv->ycur < 0)
87 priv->ycur = 0;
83510766 88 }
55d39911 89 video_sync(dev->parent, false);
7b9f7e44
SG
90
91 return 0;
83510766
SG
92}
93
94/* Move to a newline, scrolling the display if necessary */
95static void vidconsole_newline(struct udevice *dev)
96{
97 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
98 struct udevice *vid_dev = dev->parent;
99 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
100 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
101 int i;
102
c5b77d01 103 priv->xcur_frac = priv->xstart_frac;
f2661786 104 priv->ycur += priv->y_charsize;
83510766
SG
105
106 /* Check if we need to scroll the terminal */
f2661786 107 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
83510766
SG
108 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
109 for (i = 0; i < rows; i++)
110 vidconsole_set_row(dev, priv->rows - i - 1,
111 vid_priv->colour_bg);
f2661786 112 priv->ycur -= rows * priv->y_charsize;
83510766 113 }
58c733a7
SG
114 priv->last_ch = 0;
115
55d39911 116 video_sync(dev->parent, false);
83510766
SG
117}
118
5c30fbb8 119static const struct vid_rgb colors[VID_COLOR_COUNT] = {
703d885c 120 { 0x00, 0x00, 0x00 }, /* black */
9ffa4d12
HS
121 { 0xc0, 0x00, 0x00 }, /* red */
122 { 0x00, 0xc0, 0x00 }, /* green */
123 { 0xc0, 0x60, 0x00 }, /* brown */
124 { 0x00, 0x00, 0xc0 }, /* blue */
125 { 0xc0, 0x00, 0xc0 }, /* magenta */
126 { 0x00, 0xc0, 0xc0 }, /* cyan */
127 { 0xc0, 0xc0, 0xc0 }, /* light gray */
128 { 0x80, 0x80, 0x80 }, /* gray */
129 { 0xff, 0x00, 0x00 }, /* bright red */
130 { 0x00, 0xff, 0x00 }, /* bright green */
703d885c 131 { 0xff, 0xff, 0x00 }, /* yellow */
9ffa4d12
HS
132 { 0x00, 0x00, 0xff }, /* bright blue */
133 { 0xff, 0x00, 0xff }, /* bright magenta */
134 { 0x00, 0xff, 0xff }, /* bright cyan */
703d885c
RC
135 { 0xff, 0xff, 0xff }, /* white */
136};
137
5c30fbb8 138u32 vid_console_color(struct video_priv *priv, unsigned int idx)
703d885c
RC
139{
140 switch (priv->bpix) {
141 case VIDEO_BPP16:
5c30fbb8
HS
142 return ((colors[idx].r >> 3) << 11) |
143 ((colors[idx].g >> 2) << 5) |
144 ((colors[idx].b >> 3) << 0);
703d885c 145 case VIDEO_BPP32:
5c30fbb8
HS
146 return (colors[idx].r << 16) |
147 (colors[idx].g << 8) |
148 (colors[idx].b << 0);
703d885c 149 default:
5c30fbb8
HS
150 /*
151 * For unknown bit arrangements just support
152 * black and white.
153 */
154 if (idx)
155 return 0xffffff; /* white */
156 else
157 return 0x000000; /* black */
703d885c
RC
158 }
159}
160
a085aa1f
RC
161static char *parsenum(char *s, int *num)
162{
163 char *end;
164 *num = simple_strtol(s, &end, 10);
165 return end;
166}
167
662f381a
HS
168/**
169 * set_cursor_position() - set cursor position
170 *
171 * @priv: private data of the video console
172 * @row: new row
173 * @col: new column
174 */
175static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
176{
177 /*
178 * Ensure we stay in the bounds of the screen.
179 */
180 if (row >= priv->rows)
181 row = priv->rows - 1;
182 if (col >= priv->cols)
183 col = priv->cols - 1;
184
185 priv->ycur = row * priv->y_charsize;
186 priv->xcur_frac = priv->xstart_frac +
187 VID_TO_POS(col * priv->x_charsize);
188}
189
190/**
191 * get_cursor_position() - get cursor position
192 *
193 * @priv: private data of the video console
194 * @row: row
195 * @col: column
196 */
197static void get_cursor_position(struct vidconsole_priv *priv,
198 int *row, int *col)
199{
200 *row = priv->ycur / priv->y_charsize;
201 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
202 priv->x_charsize;
203}
204
a085aa1f
RC
205/*
206 * Process a character while accumulating an escape string. Chars are
207 * accumulated into escape_buf until the end of escape sequence is
208 * found, at which point the sequence is parsed and processed.
209 */
210static void vidconsole_escape_char(struct udevice *dev, char ch)
211{
212 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
213
214 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
215 goto error;
216
217 /* Sanity checking for bogus ESC sequences: */
218 if (priv->escape_len >= sizeof(priv->escape_buf))
219 goto error;
662f381a
HS
220 if (priv->escape_len == 0) {
221 switch (ch) {
222 case '7':
223 /* Save cursor position */
224 get_cursor_position(priv, &priv->row_saved,
225 &priv->col_saved);
226 priv->escape = 0;
227
228 return;
229 case '8': {
230 /* Restore cursor position */
231 int row = priv->row_saved;
232 int col = priv->col_saved;
233
234 set_cursor_position(priv, row, col);
235 priv->escape = 0;
236 return;
237 }
238 case '[':
239 break;
240 default:
241 goto error;
242 }
243 }
a085aa1f
RC
244
245 priv->escape_buf[priv->escape_len++] = ch;
246
247 /*
248 * Escape sequences are terminated by a letter, so keep
249 * accumulating until we get one:
250 */
251 if (!isalpha(ch))
252 return;
253
254 /*
255 * clear escape mode first, otherwise things will get highly
256 * surprising if you hit any debug prints that come back to
257 * this console.
258 */
259 priv->escape = 0;
260
261 switch (ch) {
29c158b9
AP
262 case 'A':
263 case 'B':
264 case 'C':
265 case 'D':
266 case 'E':
267 case 'F': {
268 int row, col, num;
269 char *s = priv->escape_buf;
270
271 /*
272 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
273 * Cursor left/right: [%dD, [%dC
274 */
275 s++; /* [ */
276 s = parsenum(s, &num);
277 if (num == 0) /* No digit in sequence ... */
278 num = 1; /* ... means "move by 1". */
279
280 get_cursor_position(priv, &row, &col);
281 if (ch == 'A' || ch == 'F')
282 row -= num;
283 if (ch == 'C')
284 col += num;
285 if (ch == 'D')
286 col -= num;
287 if (ch == 'B' || ch == 'E')
288 row += num;
289 if (ch == 'E' || ch == 'F')
290 col = 0;
291 if (col < 0)
292 col = 0;
293 if (row < 0)
294 row = 0;
295 /* Right and bottom overflows are handled in the callee. */
296 set_cursor_position(priv, row, col);
297 break;
298 }
a085aa1f
RC
299 case 'H':
300 case 'f': {
301 int row, col;
302 char *s = priv->escape_buf;
303
304 /*
305 * Set cursor position: [%d;%df or [%d;%dH
306 */
307 s++; /* [ */
308 s = parsenum(s, &row);
309 s++; /* ; */
310 s = parsenum(s, &col);
311
118f020d
HS
312 /*
313 * Video origin is [0, 0], terminal origin is [1, 1].
314 */
315 if (row)
316 --row;
317 if (col)
318 --col;
319
662f381a 320 set_cursor_position(priv, row, col);
a085aa1f
RC
321
322 break;
323 }
324 case 'J': {
325 int mode;
326
327 /*
328 * Clear part/all screen:
329 * [J or [0J - clear screen from cursor down
330 * [1J - clear screen from cursor up
331 * [2J - clear entire screen
332 *
333 * TODO we really only handle entire-screen case, others
334 * probably require some additions to video-uclass (and
335 * are not really needed yet by efi_console)
336 */
337 parsenum(priv->escape_buf + 1, &mode);
338
339 if (mode == 2) {
340 video_clear(dev->parent);
55d39911 341 video_sync(dev->parent, false);
a085aa1f
RC
342 priv->ycur = 0;
343 priv->xcur_frac = priv->xstart_frac;
344 } else {
345 debug("unsupported clear mode: %d\n", mode);
346 }
347 break;
348 }
703d885c
RC
349 case 'm': {
350 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
351 char *s = priv->escape_buf;
352 char *end = &priv->escape_buf[priv->escape_len];
353
354 /*
355 * Set graphics mode: [%d;...;%dm
356 *
357 * Currently only supports the color attributes:
358 *
359 * Foreground Colors:
360 *
361 * 30 Black
362 * 31 Red
363 * 32 Green
364 * 33 Yellow
365 * 34 Blue
366 * 35 Magenta
367 * 36 Cyan
368 * 37 White
369 *
370 * Background Colors:
371 *
372 * 40 Black
373 * 41 Red
374 * 42 Green
375 * 43 Yellow
376 * 44 Blue
377 * 45 Magenta
378 * 46 Cyan
379 * 47 White
380 */
381
382 s++; /* [ */
383 while (s < end) {
384 int val;
385
386 s = parsenum(s, &val);
387 s++;
388
389 switch (val) {
9ffa4d12
HS
390 case 0:
391 /* all attributes off */
b9f210a3 392 video_set_default_colors(dev->parent, false);
9ffa4d12
HS
393 break;
394 case 1:
395 /* bold */
396 vid_priv->fg_col_idx |= 8;
397 vid_priv->colour_fg = vid_console_color(
398 vid_priv, vid_priv->fg_col_idx);
399 break;
eabb0725
AP
400 case 7:
401 /* reverse video */
402 vid_priv->colour_fg = vid_console_color(
403 vid_priv, vid_priv->bg_col_idx);
404 vid_priv->colour_bg = vid_console_color(
405 vid_priv, vid_priv->fg_col_idx);
406 break;
703d885c 407 case 30 ... 37:
5c30fbb8 408 /* foreground color */
9ffa4d12
HS
409 vid_priv->fg_col_idx &= ~7;
410 vid_priv->fg_col_idx |= val - 30;
5c30fbb8 411 vid_priv->colour_fg = vid_console_color(
9ffa4d12 412 vid_priv, vid_priv->fg_col_idx);
703d885c
RC
413 break;
414 case 40 ... 47:
eabb0725
AP
415 /* background color, also mask the bold bit */
416 vid_priv->bg_col_idx &= ~0xf;
417 vid_priv->bg_col_idx |= val - 40;
5c30fbb8 418 vid_priv->colour_bg = vid_console_color(
eabb0725 419 vid_priv, vid_priv->bg_col_idx);
703d885c
RC
420 break;
421 default:
5c30fbb8 422 /* ignore unsupported SGR parameter */
703d885c
RC
423 break;
424 }
425 }
426
427 break;
428 }
a085aa1f
RC
429 default:
430 debug("unrecognized escape sequence: %*s\n",
431 priv->escape_len, priv->escape_buf);
432 }
433
434 return;
435
436error:
437 /* something went wrong, just revert to normal mode: */
438 priv->escape = 0;
439}
440
83510766
SG
441int vidconsole_put_char(struct udevice *dev, char ch)
442{
443 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
444 int ret;
445
a085aa1f
RC
446 if (priv->escape) {
447 vidconsole_escape_char(dev, ch);
448 return 0;
449 }
450
83510766 451 switch (ch) {
a085aa1f
RC
452 case '\x1b':
453 priv->escape_len = 0;
454 priv->escape = 1;
455 break;
5508f10a
SG
456 case '\a':
457 /* beep */
458 break;
83510766 459 case '\r':
c5b77d01 460 priv->xcur_frac = priv->xstart_frac;
83510766
SG
461 break;
462 case '\n':
463 vidconsole_newline(dev);
58c733a7 464 vidconsole_entry_start(dev);
83510766
SG
465 break;
466 case '\t': /* Tab (8 chars alignment) */
f2661786
SG
467 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
468 + 1) * priv->tab_width_frac;
83510766 469
f2661786 470 if (priv->xcur_frac >= priv->xsize_frac)
83510766
SG
471 vidconsole_newline(dev);
472 break;
473 case '\b':
474 vidconsole_back(dev);
58c733a7 475 priv->last_ch = 0;
83510766
SG
476 break;
477 default:
478 /*
479 * Failure of this function normally indicates an unsupported
480 * colour depth. Check this and return an error to help with
481 * diagnosis.
482 */
f2661786
SG
483 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
484 if (ret == -EAGAIN) {
485 vidconsole_newline(dev);
486 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
487 priv->ycur, ch);
488 }
489 if (ret < 0)
83510766 490 return ret;
f2661786 491 priv->xcur_frac += ret;
58c733a7 492 priv->last_ch = ch;
f2661786 493 if (priv->xcur_frac >= priv->xsize_frac)
83510766
SG
494 vidconsole_newline(dev);
495 break;
496 }
497
498 return 0;
499}
500
501static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
502{
503 struct udevice *dev = sdev->priv;
504
505 vidconsole_put_char(dev, ch);
55d39911 506 video_sync(dev->parent, false);
83510766
SG
507}
508
509static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
510{
511 struct udevice *dev = sdev->priv;
512
513 while (*s)
514 vidconsole_put_char(dev, *s++);
55d39911 515 video_sync(dev->parent, false);
83510766
SG
516}
517
518/* Set up the number of rows and colours (rotated drivers override this) */
519static int vidconsole_pre_probe(struct udevice *dev)
520{
521 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
522 struct udevice *vid = dev->parent;
523 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
524
f2661786 525 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
83510766
SG
526
527 return 0;
528}
529
530/* Register the device with stdio */
531static int vidconsole_post_probe(struct udevice *dev)
532{
533 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
534 struct stdio_dev *sdev = &priv->sdev;
83510766 535
f2661786
SG
536 if (!priv->tab_width_frac)
537 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
538
f1a1247d
SG
539 if (dev->seq) {
540 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
541 dev->seq);
542 } else {
543 strcpy(sdev->name, "vidconsole");
544 }
f2661786 545
83510766
SG
546 sdev->flags = DEV_FLAGS_OUTPUT;
547 sdev->putc = vidconsole_putc;
548 sdev->puts = vidconsole_puts;
549 sdev->priv = dev;
83510766 550
720873bf 551 return stdio_register(sdev);
83510766
SG
552}
553
554UCLASS_DRIVER(vidconsole) = {
555 .id = UCLASS_VIDEO_CONSOLE,
556 .name = "vidconsole0",
557 .pre_probe = vidconsole_pre_probe,
558 .post_probe = vidconsole_post_probe,
559 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
560};
561
562void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
563{
564 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
f2661786
SG
565 struct udevice *vid_dev = dev->parent;
566 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
83510766 567
9949ee87
SG
568 col *= priv->x_charsize;
569 row *= priv->y_charsize;
f2661786
SG
570 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
571 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
83510766
SG
572}
573
574static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
575 char *const argv[])
576{
577 unsigned int col, row;
578 struct udevice *dev;
579
580 if (argc != 3)
581 return CMD_RET_USAGE;
582
3f603cbb 583 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
83510766
SG
584 return CMD_RET_FAILURE;
585 col = simple_strtoul(argv[1], NULL, 10);
586 row = simple_strtoul(argv[2], NULL, 10);
587 vidconsole_position_cursor(dev, col, row);
588
589 return 0;
590}
591
592static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
593 char *const argv[])
594{
595 struct udevice *dev;
596 const char *s;
597
598 if (argc != 2)
599 return CMD_RET_USAGE;
600
3f603cbb 601 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
83510766
SG
602 return CMD_RET_FAILURE;
603 for (s = argv[1]; *s; s++)
604 vidconsole_put_char(dev, *s);
605
55d39911 606 video_sync(dev->parent, false);
889808da 607
83510766
SG
608 return 0;
609}
610
611U_BOOT_CMD(
612 setcurs, 3, 1, do_video_setcursor,
613 "set cursor position within screen",
614 " <col> <row> in character"
615);
616
617U_BOOT_CMD(
618 lcdputs, 2, 1, do_video_puts,
619 "print string on video framebuffer",
620 " <string>"
621);