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