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