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