Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 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 |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 10 | #define LOG_CATEGORY UCLASS_VIDEO_CONSOLE |
| 11 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 12 | #include <common.h> |
Simon Glass | 34b5c25 | 2023-10-01 19:13:19 -0600 | [diff] [blame] | 13 | #include <abuf.h> |
Janne Grunau | 5548c36 | 2024-03-16 22:50:19 +0100 | [diff] [blame] | 14 | #include <charset.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 15 | #include <command.h> |
Simon Glass | f97beb7 | 2020-07-02 21:12:14 -0600 | [diff] [blame] | 16 | #include <console.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 18 | #include <dm.h> |
| 19 | #include <video.h> |
| 20 | #include <video_console.h> |
Heinrich Schuchardt | 2172fa4 | 2018-03-02 20:50:17 +0100 | [diff] [blame] | 21 | #include <video_font.h> /* Bitmap font for code page 437 */ |
Simon Glass | f97beb7 | 2020-07-02 21:12:14 -0600 | [diff] [blame] | 22 | #include <linux/ctype.h> |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 23 | |
Janne Grunau | 5548c36 | 2024-03-16 22:50:19 +0100 | [diff] [blame] | 24 | int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, int ch) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 25 | { |
| 26 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 27 | |
| 28 | if (!ops->putc_xy) |
| 29 | return -ENOSYS; |
| 30 | return ops->putc_xy(dev, x, y, ch); |
| 31 | } |
| 32 | |
| 33 | int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc, |
| 34 | uint count) |
| 35 | { |
| 36 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 37 | |
| 38 | if (!ops->move_rows) |
| 39 | return -ENOSYS; |
| 40 | return ops->move_rows(dev, rowdst, rowsrc, count); |
| 41 | } |
| 42 | |
| 43 | int vidconsole_set_row(struct udevice *dev, uint row, int clr) |
| 44 | { |
| 45 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 46 | |
| 47 | if (!ops->set_row) |
| 48 | return -ENOSYS; |
| 49 | return ops->set_row(dev, row, clr); |
| 50 | } |
| 51 | |
Simon Glass | 4446c4b | 2023-10-01 19:13:20 -0600 | [diff] [blame] | 52 | int vidconsole_entry_start(struct udevice *dev) |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 53 | { |
| 54 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 55 | |
| 56 | if (!ops->entry_start) |
| 57 | return -ENOSYS; |
| 58 | return ops->entry_start(dev); |
| 59 | } |
| 60 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 61 | /* Move backwards one space */ |
Simon Glass | 33bd3b6 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 62 | static int vidconsole_back(struct udevice *dev) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 63 | { |
| 64 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 33bd3b6 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 65 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 66 | int ret; |
| 67 | |
| 68 | if (ops->backspace) { |
| 69 | ret = ops->backspace(dev); |
| 70 | if (ret != -ENOSYS) |
| 71 | return ret; |
| 72 | } |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 73 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 74 | priv->xcur_frac -= VID_TO_POS(priv->x_charsize); |
Simon Glass | a74451d | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 75 | if (priv->xcur_frac < priv->xstart_frac) { |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 76 | priv->xcur_frac = (priv->cols - 1) * |
| 77 | VID_TO_POS(priv->x_charsize); |
| 78 | priv->ycur -= priv->y_charsize; |
| 79 | if (priv->ycur < 0) |
| 80 | priv->ycur = 0; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 81 | } |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 82 | return video_sync(dev->parent, false); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Move to a newline, scrolling the display if necessary */ |
| 86 | static void vidconsole_newline(struct udevice *dev) |
| 87 | { |
| 88 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 89 | struct udevice *vid_dev = dev->parent; |
| 90 | struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev); |
Nikhil M Jain | 9e3301d | 2023-04-20 17:41:08 +0530 | [diff] [blame] | 91 | const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES); |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 92 | int i, ret; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 93 | |
Simon Glass | a74451d | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 94 | priv->xcur_frac = priv->xstart_frac; |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 95 | priv->ycur += priv->y_charsize; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 96 | |
| 97 | /* Check if we need to scroll the terminal */ |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 98 | if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) { |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 99 | vidconsole_move_rows(dev, 0, rows, priv->rows - rows); |
| 100 | for (i = 0; i < rows; i++) |
| 101 | vidconsole_set_row(dev, priv->rows - i - 1, |
| 102 | vid_priv->colour_bg); |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 103 | priv->ycur -= rows * priv->y_charsize; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 104 | } |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 105 | priv->last_ch = 0; |
| 106 | |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 107 | ret = video_sync(dev->parent, false); |
| 108 | if (ret) { |
| 109 | #ifdef DEBUG |
| 110 | console_puts_select_stderr(true, "[vc err: video_sync]"); |
| 111 | #endif |
| 112 | } |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 115 | static char *parsenum(char *s, int *num) |
| 116 | { |
| 117 | char *end; |
| 118 | *num = simple_strtol(s, &end, 10); |
| 119 | return end; |
| 120 | } |
| 121 | |
Simon Glass | d622c5b | 2022-10-06 08:36:04 -0600 | [diff] [blame] | 122 | void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y) |
| 123 | { |
| 124 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 125 | |
| 126 | priv->xcur_frac = VID_TO_POS(x); |
| 127 | priv->xstart_frac = priv->xcur_frac; |
| 128 | priv->ycur = y; |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 129 | vidconsole_entry_start(dev); |
Simon Glass | d622c5b | 2022-10-06 08:36:04 -0600 | [diff] [blame] | 130 | } |
| 131 | |
Tom Rini | 2b99fd8 | 2023-03-15 11:58:58 -0400 | [diff] [blame] | 132 | /** |
| 133 | * set_cursor_position() - set cursor position |
| 134 | * |
| 135 | * @priv: private data of the video console |
| 136 | * @row: new row |
| 137 | * @col: new column |
| 138 | */ |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 139 | static void set_cursor_position(struct udevice *dev, int row, int col) |
Heinrich Schuchardt | 9e933f1 | 2018-09-19 21:31:48 +0200 | [diff] [blame] | 140 | { |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 141 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 142 | |
Tom Rini | 2b99fd8 | 2023-03-15 11:58:58 -0400 | [diff] [blame] | 143 | /* |
| 144 | * Ensure we stay in the bounds of the screen. |
| 145 | */ |
| 146 | if (row >= priv->rows) |
| 147 | row = priv->rows - 1; |
| 148 | if (col >= priv->cols) |
| 149 | col = priv->cols - 1; |
Heinrich Schuchardt | 9e933f1 | 2018-09-19 21:31:48 +0200 | [diff] [blame] | 150 | |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 151 | vidconsole_position_cursor(dev, col, row); |
Heinrich Schuchardt | 9e933f1 | 2018-09-19 21:31:48 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * get_cursor_position() - get cursor position |
| 156 | * |
| 157 | * @priv: private data of the video console |
| 158 | * @row: row |
| 159 | * @col: column |
| 160 | */ |
| 161 | static void get_cursor_position(struct vidconsole_priv *priv, |
| 162 | int *row, int *col) |
| 163 | { |
| 164 | *row = priv->ycur / priv->y_charsize; |
| 165 | *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) / |
| 166 | priv->x_charsize; |
| 167 | } |
| 168 | |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 169 | /* |
| 170 | * Process a character while accumulating an escape string. Chars are |
| 171 | * accumulated into escape_buf until the end of escape sequence is |
| 172 | * found, at which point the sequence is parsed and processed. |
| 173 | */ |
| 174 | static void vidconsole_escape_char(struct udevice *dev, char ch) |
| 175 | { |
| 176 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 177 | |
| 178 | if (!IS_ENABLED(CONFIG_VIDEO_ANSI)) |
| 179 | goto error; |
| 180 | |
| 181 | /* Sanity checking for bogus ESC sequences: */ |
| 182 | if (priv->escape_len >= sizeof(priv->escape_buf)) |
| 183 | goto error; |
Heinrich Schuchardt | 9e933f1 | 2018-09-19 21:31:48 +0200 | [diff] [blame] | 184 | if (priv->escape_len == 0) { |
| 185 | switch (ch) { |
| 186 | case '7': |
| 187 | /* Save cursor position */ |
| 188 | get_cursor_position(priv, &priv->row_saved, |
| 189 | &priv->col_saved); |
| 190 | priv->escape = 0; |
| 191 | |
| 192 | return; |
| 193 | case '8': { |
| 194 | /* Restore cursor position */ |
| 195 | int row = priv->row_saved; |
| 196 | int col = priv->col_saved; |
| 197 | |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 198 | set_cursor_position(dev, row, col); |
Heinrich Schuchardt | 9e933f1 | 2018-09-19 21:31:48 +0200 | [diff] [blame] | 199 | priv->escape = 0; |
| 200 | return; |
| 201 | } |
| 202 | case '[': |
| 203 | break; |
| 204 | default: |
| 205 | goto error; |
| 206 | } |
| 207 | } |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 208 | |
| 209 | priv->escape_buf[priv->escape_len++] = ch; |
| 210 | |
| 211 | /* |
| 212 | * Escape sequences are terminated by a letter, so keep |
| 213 | * accumulating until we get one: |
| 214 | */ |
| 215 | if (!isalpha(ch)) |
| 216 | return; |
| 217 | |
| 218 | /* |
| 219 | * clear escape mode first, otherwise things will get highly |
| 220 | * surprising if you hit any debug prints that come back to |
| 221 | * this console. |
| 222 | */ |
| 223 | priv->escape = 0; |
| 224 | |
| 225 | switch (ch) { |
Andre Przywara | d4a294c | 2019-03-23 01:29:57 +0000 | [diff] [blame] | 226 | case 'A': |
| 227 | case 'B': |
| 228 | case 'C': |
| 229 | case 'D': |
| 230 | case 'E': |
| 231 | case 'F': { |
| 232 | int row, col, num; |
| 233 | char *s = priv->escape_buf; |
| 234 | |
| 235 | /* |
| 236 | * Cursor up/down: [%dA, [%dB, [%dE, [%dF |
| 237 | * Cursor left/right: [%dD, [%dC |
| 238 | */ |
| 239 | s++; /* [ */ |
| 240 | s = parsenum(s, &num); |
| 241 | if (num == 0) /* No digit in sequence ... */ |
| 242 | num = 1; /* ... means "move by 1". */ |
| 243 | |
| 244 | get_cursor_position(priv, &row, &col); |
| 245 | if (ch == 'A' || ch == 'F') |
| 246 | row -= num; |
| 247 | if (ch == 'C') |
| 248 | col += num; |
| 249 | if (ch == 'D') |
| 250 | col -= num; |
| 251 | if (ch == 'B' || ch == 'E') |
| 252 | row += num; |
| 253 | if (ch == 'E' || ch == 'F') |
| 254 | col = 0; |
| 255 | if (col < 0) |
| 256 | col = 0; |
| 257 | if (row < 0) |
| 258 | row = 0; |
| 259 | /* Right and bottom overflows are handled in the callee. */ |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 260 | set_cursor_position(dev, row, col); |
Andre Przywara | d4a294c | 2019-03-23 01:29:57 +0000 | [diff] [blame] | 261 | break; |
| 262 | } |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 263 | case 'H': |
| 264 | case 'f': { |
| 265 | int row, col; |
| 266 | char *s = priv->escape_buf; |
| 267 | |
| 268 | /* |
| 269 | * Set cursor position: [%d;%df or [%d;%dH |
| 270 | */ |
| 271 | s++; /* [ */ |
| 272 | s = parsenum(s, &row); |
| 273 | s++; /* ; */ |
| 274 | s = parsenum(s, &col); |
| 275 | |
Heinrich Schuchardt | c3c6930 | 2018-11-10 19:55:48 +0100 | [diff] [blame] | 276 | /* |
| 277 | * Video origin is [0, 0], terminal origin is [1, 1]. |
| 278 | */ |
| 279 | if (row) |
| 280 | --row; |
| 281 | if (col) |
| 282 | --col; |
| 283 | |
Simon Glass | 2425d4f | 2024-01-04 08:10:37 -0700 | [diff] [blame] | 284 | set_cursor_position(dev, row, col); |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 285 | |
| 286 | break; |
| 287 | } |
| 288 | case 'J': { |
| 289 | int mode; |
| 290 | |
| 291 | /* |
| 292 | * Clear part/all screen: |
| 293 | * [J or [0J - clear screen from cursor down |
| 294 | * [1J - clear screen from cursor up |
| 295 | * [2J - clear entire screen |
| 296 | * |
| 297 | * TODO we really only handle entire-screen case, others |
| 298 | * probably require some additions to video-uclass (and |
| 299 | * are not really needed yet by efi_console) |
| 300 | */ |
| 301 | parsenum(priv->escape_buf + 1, &mode); |
| 302 | |
| 303 | if (mode == 2) { |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 304 | int ret; |
| 305 | |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 306 | video_clear(dev->parent); |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 307 | ret = video_sync(dev->parent, false); |
| 308 | if (ret) { |
| 309 | #ifdef DEBUG |
| 310 | console_puts_select_stderr(true, "[vc err: video_sync]"); |
| 311 | #endif |
| 312 | } |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 313 | priv->ycur = 0; |
| 314 | priv->xcur_frac = priv->xstart_frac; |
| 315 | } else { |
| 316 | debug("unsupported clear mode: %d\n", mode); |
| 317 | } |
| 318 | break; |
| 319 | } |
Andre Przywara | 918622a | 2019-03-23 01:29:58 +0000 | [diff] [blame] | 320 | case 'K': { |
| 321 | struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent); |
| 322 | int mode; |
| 323 | |
| 324 | /* |
| 325 | * Clear (parts of) current line |
| 326 | * [0K - clear line to end |
| 327 | * [2K - clear entire line |
| 328 | */ |
| 329 | parsenum(priv->escape_buf + 1, &mode); |
| 330 | |
| 331 | if (mode == 2) { |
| 332 | int row, col; |
| 333 | |
| 334 | get_cursor_position(priv, &row, &col); |
| 335 | vidconsole_set_row(dev, row, vid_priv->colour_bg); |
| 336 | } |
| 337 | break; |
| 338 | } |
Rob Clark | 50509bb | 2017-09-13 18:12:22 -0400 | [diff] [blame] | 339 | case 'm': { |
| 340 | struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent); |
| 341 | char *s = priv->escape_buf; |
| 342 | char *end = &priv->escape_buf[priv->escape_len]; |
| 343 | |
| 344 | /* |
| 345 | * Set graphics mode: [%d;...;%dm |
| 346 | * |
| 347 | * Currently only supports the color attributes: |
| 348 | * |
| 349 | * Foreground Colors: |
| 350 | * |
| 351 | * 30 Black |
| 352 | * 31 Red |
| 353 | * 32 Green |
| 354 | * 33 Yellow |
| 355 | * 34 Blue |
| 356 | * 35 Magenta |
| 357 | * 36 Cyan |
| 358 | * 37 White |
| 359 | * |
| 360 | * Background Colors: |
| 361 | * |
| 362 | * 40 Black |
| 363 | * 41 Red |
| 364 | * 42 Green |
| 365 | * 43 Yellow |
| 366 | * 44 Blue |
| 367 | * 45 Magenta |
| 368 | * 46 Cyan |
| 369 | * 47 White |
| 370 | */ |
| 371 | |
| 372 | s++; /* [ */ |
| 373 | while (s < end) { |
| 374 | int val; |
| 375 | |
| 376 | s = parsenum(s, &val); |
| 377 | s++; |
| 378 | |
| 379 | switch (val) { |
Heinrich Schuchardt | 2a436db | 2018-02-08 21:47:12 +0100 | [diff] [blame] | 380 | case 0: |
| 381 | /* all attributes off */ |
Simon Glass | 2b063b8 | 2018-11-06 15:21:36 -0700 | [diff] [blame] | 382 | video_set_default_colors(dev->parent, false); |
Heinrich Schuchardt | 2a436db | 2018-02-08 21:47:12 +0100 | [diff] [blame] | 383 | break; |
| 384 | case 1: |
| 385 | /* bold */ |
| 386 | vid_priv->fg_col_idx |= 8; |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 387 | vid_priv->colour_fg = video_index_to_colour( |
Heinrich Schuchardt | 2a436db | 2018-02-08 21:47:12 +0100 | [diff] [blame] | 388 | vid_priv, vid_priv->fg_col_idx); |
| 389 | break; |
Andre Przywara | 4ed5bc8 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 390 | case 7: |
| 391 | /* reverse video */ |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 392 | vid_priv->colour_fg = video_index_to_colour( |
Andre Przywara | 4ed5bc8 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 393 | vid_priv, vid_priv->bg_col_idx); |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 394 | vid_priv->colour_bg = video_index_to_colour( |
Andre Przywara | 4ed5bc8 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 395 | vid_priv, vid_priv->fg_col_idx); |
| 396 | break; |
Rob Clark | 50509bb | 2017-09-13 18:12:22 -0400 | [diff] [blame] | 397 | case 30 ... 37: |
Heinrich Schuchardt | 290e1d8 | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 398 | /* foreground color */ |
Heinrich Schuchardt | 2a436db | 2018-02-08 21:47:12 +0100 | [diff] [blame] | 399 | vid_priv->fg_col_idx &= ~7; |
| 400 | vid_priv->fg_col_idx |= val - 30; |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 401 | vid_priv->colour_fg = video_index_to_colour( |
Heinrich Schuchardt | 2a436db | 2018-02-08 21:47:12 +0100 | [diff] [blame] | 402 | vid_priv, vid_priv->fg_col_idx); |
Rob Clark | 50509bb | 2017-09-13 18:12:22 -0400 | [diff] [blame] | 403 | break; |
| 404 | case 40 ... 47: |
Andre Przywara | 4ed5bc8 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 405 | /* background color, also mask the bold bit */ |
| 406 | vid_priv->bg_col_idx &= ~0xf; |
| 407 | vid_priv->bg_col_idx |= val - 40; |
Simon Glass | 2a00633 | 2022-10-06 08:36:03 -0600 | [diff] [blame] | 408 | vid_priv->colour_bg = video_index_to_colour( |
Andre Przywara | 4ed5bc8 | 2019-03-23 01:29:56 +0000 | [diff] [blame] | 409 | vid_priv, vid_priv->bg_col_idx); |
Rob Clark | 50509bb | 2017-09-13 18:12:22 -0400 | [diff] [blame] | 410 | break; |
| 411 | default: |
Heinrich Schuchardt | 290e1d8 | 2018-02-08 21:47:11 +0100 | [diff] [blame] | 412 | /* ignore unsupported SGR parameter */ |
Rob Clark | 50509bb | 2017-09-13 18:12:22 -0400 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | break; |
| 418 | } |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 419 | default: |
| 420 | debug("unrecognized escape sequence: %*s\n", |
| 421 | priv->escape_len, priv->escape_buf); |
| 422 | } |
| 423 | |
| 424 | return; |
| 425 | |
| 426 | error: |
| 427 | /* something went wrong, just revert to normal mode: */ |
| 428 | priv->escape = 0; |
| 429 | } |
| 430 | |
Janne Grunau | 5548c36 | 2024-03-16 22:50:19 +0100 | [diff] [blame] | 431 | /* Put that actual character on the screen (using the UTF-32 code points). */ |
| 432 | static int vidconsole_output_glyph(struct udevice *dev, int ch) |
Andre Przywara | de86baf | 2019-03-23 01:29:59 +0000 | [diff] [blame] | 433 | { |
| 434 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 435 | int ret; |
| 436 | |
| 437 | /* |
| 438 | * Failure of this function normally indicates an unsupported |
| 439 | * colour depth. Check this and return an error to help with |
| 440 | * diagnosis. |
| 441 | */ |
| 442 | ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch); |
| 443 | if (ret == -EAGAIN) { |
| 444 | vidconsole_newline(dev); |
| 445 | ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch); |
| 446 | } |
| 447 | if (ret < 0) |
| 448 | return ret; |
| 449 | priv->xcur_frac += ret; |
| 450 | priv->last_ch = ch; |
| 451 | if (priv->xcur_frac >= priv->xsize_frac) |
| 452 | vidconsole_newline(dev); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 457 | int vidconsole_put_char(struct udevice *dev, char ch) |
| 458 | { |
| 459 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
Janne Grunau | 5548c36 | 2024-03-16 22:50:19 +0100 | [diff] [blame] | 460 | int cp, ret; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 461 | |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 462 | if (priv->escape) { |
| 463 | vidconsole_escape_char(dev, ch); |
| 464 | return 0; |
| 465 | } |
| 466 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 467 | switch (ch) { |
Rob Clark | 06e7a0d | 2017-09-13 18:12:21 -0400 | [diff] [blame] | 468 | case '\x1b': |
| 469 | priv->escape_len = 0; |
| 470 | priv->escape = 1; |
| 471 | break; |
Simon Glass | 37b8020 | 2016-01-14 18:10:38 -0700 | [diff] [blame] | 472 | case '\a': |
| 473 | /* beep */ |
| 474 | break; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 475 | case '\r': |
Simon Glass | a74451d | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 476 | priv->xcur_frac = priv->xstart_frac; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 477 | break; |
| 478 | case '\n': |
| 479 | vidconsole_newline(dev); |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 480 | vidconsole_entry_start(dev); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 481 | break; |
| 482 | case '\t': /* Tab (8 chars alignment) */ |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 483 | priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac) |
| 484 | + 1) * priv->tab_width_frac; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 485 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 486 | if (priv->xcur_frac >= priv->xsize_frac) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 487 | vidconsole_newline(dev); |
| 488 | break; |
| 489 | case '\b': |
| 490 | vidconsole_back(dev); |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 491 | priv->last_ch = 0; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 492 | break; |
| 493 | default: |
Janne Grunau | 5548c36 | 2024-03-16 22:50:19 +0100 | [diff] [blame] | 494 | if (CONFIG_IS_ENABLED(CHARSET)) { |
| 495 | cp = utf8_to_utf32_stream(ch, priv->utf8_buf); |
| 496 | if (cp == 0) |
| 497 | return 0; |
| 498 | } else { |
| 499 | cp = ch; |
| 500 | } |
| 501 | ret = vidconsole_output_glyph(dev, cp); |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 502 | if (ret < 0) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 503 | return ret; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Marek Vasut | a89f9cb | 2019-05-17 20:22:31 +0200 | [diff] [blame] | 510 | int vidconsole_put_string(struct udevice *dev, const char *str) |
| 511 | { |
| 512 | const char *s; |
| 513 | int ret; |
| 514 | |
| 515 | for (s = str; *s; s++) { |
| 516 | ret = vidconsole_put_char(dev, *s); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 524 | static void vidconsole_putc(struct stdio_dev *sdev, const char ch) |
| 525 | { |
| 526 | struct udevice *dev = sdev->priv; |
Simon Glass | f97beb7 | 2020-07-02 21:12:14 -0600 | [diff] [blame] | 527 | int ret; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 528 | |
Simon Glass | f97beb7 | 2020-07-02 21:12:14 -0600 | [diff] [blame] | 529 | ret = vidconsole_put_char(dev, ch); |
| 530 | if (ret) { |
| 531 | #ifdef DEBUG |
| 532 | console_puts_select_stderr(true, "[vc err: putc]"); |
| 533 | #endif |
| 534 | } |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 535 | ret = video_sync(dev->parent, false); |
| 536 | if (ret) { |
| 537 | #ifdef DEBUG |
| 538 | console_puts_select_stderr(true, "[vc err: video_sync]"); |
| 539 | #endif |
| 540 | } |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void vidconsole_puts(struct stdio_dev *sdev, const char *s) |
| 544 | { |
| 545 | struct udevice *dev = sdev->priv; |
Simon Glass | f97beb7 | 2020-07-02 21:12:14 -0600 | [diff] [blame] | 546 | int ret; |
| 547 | |
| 548 | ret = vidconsole_put_string(dev, s); |
| 549 | if (ret) { |
| 550 | #ifdef DEBUG |
| 551 | char str[30]; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 552 | |
Simon Glass | f97beb7 | 2020-07-02 21:12:14 -0600 | [diff] [blame] | 553 | snprintf(str, sizeof(str), "[vc err: puts %d]", ret); |
| 554 | console_puts_select_stderr(true, str); |
| 555 | #endif |
| 556 | } |
Michal Simek | 632e3d4 | 2020-12-14 08:47:52 +0100 | [diff] [blame] | 557 | ret = video_sync(dev->parent, false); |
| 558 | if (ret) { |
| 559 | #ifdef DEBUG |
| 560 | console_puts_select_stderr(true, "[vc err: video_sync]"); |
| 561 | #endif |
| 562 | } |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Simon Glass | 3b175ba | 2023-01-06 08:52:32 -0600 | [diff] [blame] | 565 | void vidconsole_list_fonts(struct udevice *dev) |
| 566 | { |
| 567 | struct vidfont_info info; |
| 568 | int ret, i; |
| 569 | |
| 570 | for (i = 0, ret = 0; !ret; i++) { |
| 571 | ret = vidconsole_get_font(dev, i, &info); |
| 572 | if (!ret) |
| 573 | printf("%s\n", info.name); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | int vidconsole_get_font(struct udevice *dev, int seq, |
| 578 | struct vidfont_info *info) |
| 579 | { |
| 580 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 581 | |
| 582 | if (!ops->get_font) |
| 583 | return -ENOSYS; |
| 584 | |
| 585 | return ops->get_font(dev, seq, info); |
| 586 | } |
| 587 | |
Dzmitry Sankouski | 86c6a53 | 2023-03-07 13:21:15 +0300 | [diff] [blame] | 588 | int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep) |
| 589 | { |
| 590 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 591 | |
| 592 | if (!ops->get_font_size) |
| 593 | return -ENOSYS; |
| 594 | |
| 595 | *name = ops->get_font_size(dev, sizep); |
| 596 | return 0; |
| 597 | } |
| 598 | |
Simon Glass | 3b175ba | 2023-01-06 08:52:32 -0600 | [diff] [blame] | 599 | int vidconsole_select_font(struct udevice *dev, const char *name, uint size) |
| 600 | { |
| 601 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 602 | |
| 603 | if (!ops->select_font) |
| 604 | return -ENOSYS; |
| 605 | |
| 606 | return ops->select_font(dev, name, size); |
| 607 | } |
| 608 | |
Simon Glass | 5caf125 | 2023-06-01 10:22:46 -0600 | [diff] [blame] | 609 | int vidconsole_measure(struct udevice *dev, const char *name, uint size, |
| 610 | const char *text, struct vidconsole_bbox *bbox) |
| 611 | { |
| 612 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 613 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 614 | int ret; |
| 615 | |
Bin Meng | 31aef74 | 2023-08-03 17:32:41 +0800 | [diff] [blame] | 616 | if (ops->measure) { |
Simon Glass | 5caf125 | 2023-06-01 10:22:46 -0600 | [diff] [blame] | 617 | ret = ops->measure(dev, name, size, text, bbox); |
| 618 | if (ret != -ENOSYS) |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | bbox->valid = true; |
| 623 | bbox->x0 = 0; |
| 624 | bbox->y0 = 0; |
| 625 | bbox->x1 = priv->x_charsize * strlen(text); |
| 626 | bbox->y1 = priv->y_charsize; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
Simon Glass | 8b82e59 | 2023-10-01 19:13:18 -0600 | [diff] [blame] | 631 | int vidconsole_nominal(struct udevice *dev, const char *name, uint size, |
| 632 | uint num_chars, struct vidconsole_bbox *bbox) |
| 633 | { |
| 634 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 635 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 636 | int ret; |
| 637 | |
| 638 | if (ops->measure) { |
| 639 | ret = ops->nominal(dev, name, size, num_chars, bbox); |
| 640 | if (ret != -ENOSYS) |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | bbox->valid = true; |
| 645 | bbox->x0 = 0; |
| 646 | bbox->y0 = 0; |
| 647 | bbox->x1 = priv->x_charsize * num_chars; |
| 648 | bbox->y1 = priv->y_charsize; |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Simon Glass | 34b5c25 | 2023-10-01 19:13:19 -0600 | [diff] [blame] | 653 | int vidconsole_entry_save(struct udevice *dev, struct abuf *buf) |
| 654 | { |
| 655 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 656 | int ret; |
| 657 | |
| 658 | if (ops->measure) { |
| 659 | ret = ops->entry_save(dev, buf); |
| 660 | if (ret != -ENOSYS) |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | /* no data so make sure the buffer is empty */ |
| 665 | abuf_realloc(buf, 0); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | int vidconsole_entry_restore(struct udevice *dev, struct abuf *buf) |
| 671 | { |
| 672 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 673 | int ret; |
| 674 | |
| 675 | if (ops->measure) { |
| 676 | ret = ops->entry_restore(dev, buf); |
| 677 | if (ret != -ENOSYS) |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
Simon Glass | 377f79aa | 2023-10-01 19:13:21 -0600 | [diff] [blame] | 684 | int vidconsole_set_cursor_visible(struct udevice *dev, bool visible, |
| 685 | uint x, uint y, uint index) |
| 686 | { |
| 687 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 688 | int ret; |
| 689 | |
| 690 | if (ops->set_cursor_visible) { |
| 691 | ret = ops->set_cursor_visible(dev, visible, x, y, index); |
| 692 | if (ret != -ENOSYS) |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
Simon Glass | a73a8b8 | 2023-06-01 10:22:45 -0600 | [diff] [blame] | 699 | void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg, |
| 700 | enum colour_idx bg, struct vidconsole_colour *old) |
| 701 | { |
| 702 | struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent); |
| 703 | |
| 704 | old->colour_fg = vid_priv->colour_fg; |
| 705 | old->colour_bg = vid_priv->colour_bg; |
| 706 | |
| 707 | vid_priv->colour_fg = video_index_to_colour(vid_priv, fg); |
| 708 | vid_priv->colour_bg = video_index_to_colour(vid_priv, bg); |
| 709 | } |
| 710 | |
| 711 | void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old) |
| 712 | { |
| 713 | struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent); |
| 714 | |
| 715 | vid_priv->colour_fg = old->colour_fg; |
| 716 | vid_priv->colour_bg = old->colour_bg; |
| 717 | } |
| 718 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 719 | /* Set up the number of rows and colours (rotated drivers override this) */ |
| 720 | static int vidconsole_pre_probe(struct udevice *dev) |
| 721 | { |
| 722 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 723 | struct udevice *vid = dev->parent; |
| 724 | struct video_priv *vid_priv = dev_get_uclass_priv(vid); |
| 725 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 726 | priv->xsize_frac = VID_TO_POS(vid_priv->xsize); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* Register the device with stdio */ |
| 732 | static int vidconsole_post_probe(struct udevice *dev) |
| 733 | { |
| 734 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 735 | struct stdio_dev *sdev = &priv->sdev; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 736 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 737 | if (!priv->tab_width_frac) |
| 738 | priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8; |
| 739 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 740 | if (dev_seq(dev)) { |
Simon Glass | 798ff50 | 2016-01-21 19:44:51 -0700 | [diff] [blame] | 741 | snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 742 | dev_seq(dev)); |
Simon Glass | 798ff50 | 2016-01-21 19:44:51 -0700 | [diff] [blame] | 743 | } else { |
| 744 | strcpy(sdev->name, "vidconsole"); |
| 745 | } |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 746 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 747 | sdev->flags = DEV_FLAGS_OUTPUT; |
| 748 | sdev->putc = vidconsole_putc; |
| 749 | sdev->puts = vidconsole_puts; |
| 750 | sdev->priv = dev; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 751 | |
Masahiro Yamada | bf528cd | 2016-09-06 22:17:33 +0900 | [diff] [blame] | 752 | return stdio_register(sdev); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | UCLASS_DRIVER(vidconsole) = { |
| 756 | .id = UCLASS_VIDEO_CONSOLE, |
| 757 | .name = "vidconsole0", |
| 758 | .pre_probe = vidconsole_pre_probe, |
| 759 | .post_probe = vidconsole_post_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 760 | .per_device_auto = sizeof(struct vidconsole_priv), |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 761 | }; |
| 762 | |
Simon Glass | 31a7e23 | 2020-07-02 21:12:23 -0600 | [diff] [blame] | 763 | #ifdef CONFIG_VIDEO_COPY |
| 764 | int vidconsole_sync_copy(struct udevice *dev, void *from, void *to) |
| 765 | { |
| 766 | struct udevice *vid = dev_get_parent(dev); |
| 767 | |
| 768 | return video_sync_copy(vid, from, to); |
| 769 | } |
| 770 | |
| 771 | int vidconsole_memmove(struct udevice *dev, void *dst, const void *src, |
| 772 | int size) |
| 773 | { |
| 774 | memmove(dst, src, size); |
| 775 | return vidconsole_sync_copy(dev, dst, dst + size); |
| 776 | } |
| 777 | #endif |
Simon Glass | 90679c6 | 2023-03-10 12:47:21 -0800 | [diff] [blame] | 778 | |
| 779 | int vidconsole_clear_and_reset(struct udevice *dev) |
| 780 | { |
| 781 | int ret; |
| 782 | |
| 783 | ret = video_clear(dev_get_parent(dev)); |
| 784 | if (ret) |
| 785 | return ret; |
| 786 | vidconsole_position_cursor(dev, 0, 0); |
| 787 | |
| 788 | return 0; |
| 789 | } |
Tom Rini | 2b99fd8 | 2023-03-15 11:58:58 -0400 | [diff] [blame] | 790 | |
| 791 | void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row) |
| 792 | { |
| 793 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 794 | struct udevice *vid_dev = dev->parent; |
| 795 | struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev); |
| 796 | short x, y; |
| 797 | |
| 798 | x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1); |
| 799 | y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1); |
| 800 | vidconsole_set_cursor_pos(dev, x, y); |
| 801 | } |