balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU curses/ncurses display driver |
| 3 | * |
| 4 | * Copyright (c) 2005 Andrzej Zaborowski <balrog@zabor.org> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 26 | |
| 27 | #ifndef _WIN32 |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
| 29 | #include <termios.h> |
| 30 | #endif |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 31 | #include <locale.h> |
| 32 | #include <wchar.h> |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 33 | #include <iconv.h> |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 34 | |
Fei Li | ab4f931 | 2018-10-17 10:26:50 +0200 | [diff] [blame] | 35 | #include "qapi/error.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 36 | #include "qemu/module.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 37 | #include "ui/console.h" |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 38 | #include "ui/input.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 39 | #include "sysemu/sysemu.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 40 | |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 41 | /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */ |
| 42 | #undef KEY_EVENT |
| 43 | #include <curses.h> |
| 44 | #undef KEY_EVENT |
| 45 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 46 | #define FONT_HEIGHT 16 |
| 47 | #define FONT_WIDTH 8 |
| 48 | |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 49 | enum maybe_keycode { |
| 50 | CURSES_KEYCODE, |
| 51 | CURSES_CHAR, |
| 52 | CURSES_CHAR_OR_KEYCODE, |
| 53 | }; |
| 54 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 55 | static DisplayChangeListener *dcl; |
Philippe Mathieu-Daudé | 76c51fc | 2020-03-05 13:45:24 +0100 | [diff] [blame] | 56 | static console_ch_t *screen; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 57 | static WINDOW *screenpad = NULL; |
| 58 | static int width, height, gwidth, gheight, invalidate; |
| 59 | static int px, py, sminx, sminy, smaxx, smaxy; |
| 60 | |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 61 | static const char *font_charset = "CP437"; |
Philippe Mathieu-Daudé | 76c51fc | 2020-03-05 13:45:24 +0100 | [diff] [blame] | 62 | static cchar_t *vga_to_curses; |
OGAWA Hirofumi | e2368dc | 2015-10-19 21:23:46 +0900 | [diff] [blame] | 63 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 64 | static void curses_update(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 65 | int x, int y, int w, int h) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 66 | { |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 67 | console_ch_t *line; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 68 | cchar_t curses_line[width]; |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 69 | wchar_t wch[CCHARW_MAX]; |
| 70 | attr_t attrs; |
| 71 | short colors; |
| 72 | int ret; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 73 | |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 74 | line = screen + y * width; |
| 75 | for (h += y; y < h; y ++, line += width) { |
| 76 | for (x = 0; x < width; x++) { |
Matthew Kilgore | cd54ea4 | 2019-10-03 23:53:37 -0400 | [diff] [blame] | 77 | chtype ch = line[x] & A_CHARTEXT; |
| 78 | chtype at = line[x] & A_ATTRIBUTES; |
Matthew Kilgore | 30f5a9d | 2019-10-03 23:53:38 -0400 | [diff] [blame] | 79 | short color_pair = PAIR_NUMBER(line[x]); |
| 80 | |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 81 | ret = getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NULL); |
| 82 | if (ret == ERR || wch[0] == 0) { |
| 83 | wch[0] = ch; |
| 84 | wch[1] = 0; |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 85 | } |
Matthew Kilgore | 30f5a9d | 2019-10-03 23:53:38 -0400 | [diff] [blame] | 86 | setcchar(&curses_line[x], wch, at, color_pair, NULL); |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 87 | } |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 88 | mvwadd_wchnstr(screenpad, y, 0, curses_line, width); |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 89 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 90 | |
| 91 | pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1); |
| 92 | refresh(); |
| 93 | } |
| 94 | |
| 95 | static void curses_calc_pad(void) |
| 96 | { |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 97 | if (qemu_console_is_fixedsize(NULL)) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 98 | width = gwidth; |
| 99 | height = gheight; |
| 100 | } else { |
| 101 | width = COLS; |
| 102 | height = LINES; |
| 103 | } |
| 104 | |
| 105 | if (screenpad) |
| 106 | delwin(screenpad); |
| 107 | |
| 108 | clear(); |
| 109 | refresh(); |
| 110 | |
| 111 | screenpad = newpad(height, width); |
| 112 | |
| 113 | if (width > COLS) { |
| 114 | px = (width - COLS) / 2; |
| 115 | sminx = 0; |
| 116 | smaxx = COLS; |
| 117 | } else { |
| 118 | px = 0; |
| 119 | sminx = (COLS - width) / 2; |
| 120 | smaxx = sminx + width; |
| 121 | } |
| 122 | |
| 123 | if (height > LINES) { |
| 124 | py = (height - LINES) / 2; |
| 125 | sminy = 0; |
| 126 | smaxy = LINES; |
| 127 | } else { |
| 128 | py = 0; |
| 129 | sminy = (LINES - height) / 2; |
| 130 | smaxy = sminy + height; |
| 131 | } |
| 132 | } |
| 133 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 134 | static void curses_resize(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 135 | int width, int height) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 136 | { |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 137 | if (width == gwidth && height == gheight) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 138 | return; |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 139 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 140 | |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 141 | gwidth = width; |
| 142 | gheight = height; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 143 | |
| 144 | curses_calc_pad(); |
| 145 | } |
| 146 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 147 | #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE) |
| 148 | static volatile sig_atomic_t got_sigwinch; |
| 149 | static void curses_winch_check(void) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 150 | { |
| 151 | struct winsize { |
| 152 | unsigned short ws_row; |
| 153 | unsigned short ws_col; |
| 154 | unsigned short ws_xpixel; /* unused */ |
| 155 | unsigned short ws_ypixel; /* unused */ |
| 156 | } ws; |
| 157 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 158 | if (!got_sigwinch) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 159 | return; |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 160 | } |
| 161 | got_sigwinch = false; |
| 162 | |
| 163 | if (ioctl(1, TIOCGWINSZ, &ws) == -1) { |
| 164 | return; |
| 165 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 166 | |
| 167 | resize_term(ws.ws_row, ws.ws_col); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 168 | invalidate = 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 169 | } |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 170 | |
| 171 | static void curses_winch_handler(int signum) |
| 172 | { |
| 173 | got_sigwinch = true; |
| 174 | } |
| 175 | |
| 176 | static void curses_winch_init(void) |
| 177 | { |
| 178 | struct sigaction old, winch = { |
| 179 | .sa_handler = curses_winch_handler, |
| 180 | }; |
| 181 | sigaction(SIGWINCH, &winch, &old); |
| 182 | } |
| 183 | #else |
| 184 | static void curses_winch_check(void) {} |
| 185 | static void curses_winch_init(void) {} |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 188 | static void curses_cursor_position(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 189 | int x, int y) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 190 | { |
| 191 | if (x >= 0) { |
| 192 | x = sminx + x - px; |
| 193 | y = sminy + y - py; |
| 194 | |
| 195 | if (x >= 0 && y >= 0 && x < COLS && y < LINES) { |
| 196 | move(y, x); |
| 197 | curs_set(1); |
| 198 | /* it seems that curs_set(1) must always be called before |
| 199 | * curs_set(2) for the latter to have effect */ |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 200 | if (!qemu_console_is_graphic(NULL)) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 201 | curs_set(2); |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 202 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | curs_set(0); |
| 208 | } |
| 209 | |
| 210 | /* generic keyboard conversion */ |
| 211 | |
| 212 | #include "curses_keys.h" |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 213 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 214 | static kbd_layout_t *kbd_layout = NULL; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 215 | |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 216 | static wint_t console_getch(enum maybe_keycode *maybe_keycode) |
| 217 | { |
| 218 | wint_t ret; |
| 219 | switch (get_wch(&ret)) { |
| 220 | case KEY_CODE_YES: |
| 221 | *maybe_keycode = CURSES_KEYCODE; |
| 222 | break; |
| 223 | case OK: |
| 224 | *maybe_keycode = CURSES_CHAR; |
| 225 | break; |
| 226 | case ERR: |
| 227 | ret = -1; |
| 228 | break; |
Paolo Bonzini | 68097ed | 2019-07-18 14:01:04 +0200 | [diff] [blame] | 229 | default: |
| 230 | abort(); |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 231 | } |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static int curses2foo(const int _curses2foo[], const int _curseskey2foo[], |
| 236 | int chr, enum maybe_keycode maybe_keycode) |
| 237 | { |
| 238 | int ret = -1; |
| 239 | if (maybe_keycode == CURSES_CHAR) { |
| 240 | if (chr < CURSES_CHARS) { |
| 241 | ret = _curses2foo[chr]; |
| 242 | } |
| 243 | } else { |
| 244 | if (chr < CURSES_KEYS) { |
| 245 | ret = _curseskey2foo[chr]; |
| 246 | } |
| 247 | if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE && |
| 248 | chr < CURSES_CHARS) { |
| 249 | ret = _curses2foo[chr]; |
| 250 | } |
| 251 | } |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | #define curses2keycode(chr, maybe_keycode) \ |
| 256 | curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode) |
| 257 | #define curses2keysym(chr, maybe_keycode) \ |
| 258 | curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode) |
| 259 | #define curses2qemu(chr, maybe_keycode) \ |
| 260 | curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode) |
| 261 | |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 262 | static void curses_refresh(DisplayChangeListener *dcl) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 263 | { |
Peter Maydell | 99a9ef4 | 2016-08-11 15:23:27 +0100 | [diff] [blame] | 264 | int chr, keysym, keycode, keycode_alt; |
Yonggang Luo | 65f5279 | 2020-10-13 07:43:46 +0800 | [diff] [blame] | 265 | enum maybe_keycode maybe_keycode = CURSES_KEYCODE; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 266 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 267 | curses_winch_check(); |
| 268 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 269 | if (invalidate) { |
| 270 | clear(); |
| 271 | refresh(); |
| 272 | curses_calc_pad(); |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 273 | graphic_hw_invalidate(NULL); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 274 | invalidate = 0; |
| 275 | } |
| 276 | |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 277 | graphic_hw_text_update(NULL, screen); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 278 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 279 | while (1) { |
| 280 | /* while there are any pending key strokes to process */ |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 281 | chr = console_getch(&maybe_keycode); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 282 | |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 283 | if (chr == -1) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 284 | break; |
| 285 | |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 286 | #ifdef KEY_RESIZE |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 287 | /* this shouldn't occur when we use a custom SIGWINCH handler */ |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 288 | if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 289 | clear(); |
| 290 | refresh(); |
| 291 | curses_calc_pad(); |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 292 | curses_update(dcl, 0, 0, width, height); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 293 | continue; |
| 294 | } |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 295 | #endif |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 296 | |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 297 | keycode = curses2keycode(chr, maybe_keycode); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 298 | keycode_alt = 0; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 299 | |
Samuel Thibault | 633786f | 2019-03-03 18:25:57 +0100 | [diff] [blame] | 300 | /* alt or esc key */ |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 301 | if (keycode == 1) { |
Yonggang Luo | 65f5279 | 2020-10-13 07:43:46 +0800 | [diff] [blame] | 302 | enum maybe_keycode next_maybe_keycode = CURSES_KEYCODE; |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 303 | int nextchr = console_getch(&next_maybe_keycode); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 304 | |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 305 | if (nextchr != -1) { |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 306 | chr = nextchr; |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 307 | maybe_keycode = next_maybe_keycode; |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 308 | keycode_alt = ALT; |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 309 | keycode = curses2keycode(chr, maybe_keycode); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 310 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 311 | if (keycode != -1) { |
| 312 | keycode |= ALT; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 313 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 314 | /* process keys reserved for qemu */ |
| 315 | if (keycode >= QEMU_KEY_CONSOLE0 && |
| 316 | keycode < QEMU_KEY_CONSOLE0 + 9) { |
| 317 | erase(); |
| 318 | wnoutrefresh(stdscr); |
| 319 | console_select(keycode - QEMU_KEY_CONSOLE0); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 320 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 321 | invalidate = 1; |
| 322 | continue; |
| 323 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 328 | if (kbd_layout) { |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 329 | keysym = curses2keysym(chr, maybe_keycode); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 330 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 331 | if (keysym == -1) { |
Samuel Thibault | d03703c | 2010-10-19 19:48:20 +0200 | [diff] [blame] | 332 | if (chr < ' ') { |
| 333 | keysym = chr + '@'; |
| 334 | if (keysym >= 'A' && keysym <= 'Z') |
| 335 | keysym += 'a' - 'A'; |
| 336 | keysym |= KEYSYM_CNTRL; |
| 337 | } else |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 338 | keysym = chr; |
| 339 | } |
| 340 | |
Gerd Hoffmann | abb4f2c | 2018-02-22 08:05:13 +0100 | [diff] [blame] | 341 | keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK, |
Gerd Hoffmann | 19c1b9f | 2019-01-22 10:28:14 +0100 | [diff] [blame] | 342 | NULL, false); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 343 | if (keycode == 0) |
| 344 | continue; |
| 345 | |
| 346 | keycode |= (keysym & ~KEYSYM_MASK) >> 16; |
| 347 | keycode |= keycode_alt; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 350 | if (keycode == -1) |
| 351 | continue; |
| 352 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 353 | if (qemu_console_is_graphic(NULL)) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 354 | /* since terminals don't know about key press and release |
| 355 | * events, we need to emit both for each key received */ |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 356 | if (keycode & SHIFT) { |
| 357 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 358 | qemu_input_event_send_key_delay(0); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 359 | } |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 360 | if (keycode & CNTRL) { |
| 361 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 362 | qemu_input_event_send_key_delay(0); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 363 | } |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 364 | if (keycode & ALT) { |
| 365 | qemu_input_event_send_key_number(NULL, ALT_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 366 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 367 | } |
| 368 | if (keycode & ALTGR) { |
| 369 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 370 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Andrew Oates | f5c0ab1 | 2014-05-23 20:16:09 -0400 | [diff] [blame] | 373 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 374 | qemu_input_event_send_key_delay(0); |
Andrew Oates | f5c0ab1 | 2014-05-23 20:16:09 -0400 | [diff] [blame] | 375 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 376 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 377 | |
| 378 | if (keycode & ALTGR) { |
| 379 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 380 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 381 | } |
| 382 | if (keycode & ALT) { |
| 383 | qemu_input_event_send_key_number(NULL, ALT_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 384 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 385 | } |
| 386 | if (keycode & CNTRL) { |
| 387 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 388 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 389 | } |
| 390 | if (keycode & SHIFT) { |
| 391 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 392 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 393 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 394 | } else { |
Samuel Thibault | 459a707 | 2019-03-04 22:05:32 +0100 | [diff] [blame] | 395 | keysym = curses2qemu(chr, maybe_keycode); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 396 | if (keysym == -1) |
| 397 | keysym = chr; |
| 398 | |
| 399 | kbd_put_keysym(keysym); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Blue Swirl | aaf12c2 | 2010-03-21 19:44:06 +0000 | [diff] [blame] | 404 | static void curses_atexit(void) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 405 | { |
| 406 | endwin(); |
Philippe Mathieu-Daudé | 76c51fc | 2020-03-05 13:45:24 +0100 | [diff] [blame] | 407 | g_free(vga_to_curses); |
| 408 | g_free(screen); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 411 | /* |
| 412 | * In the following: |
| 413 | * - fch is the font glyph number |
| 414 | * - uch is the unicode value |
| 415 | * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris) |
| 416 | * - mbch is the native local-dependent multibyte representation |
| 417 | */ |
| 418 | |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 419 | /* Setup wchar glyph for one UCS-2 char */ |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 420 | static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv) |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 421 | { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 422 | char mbch[MB_LEN_MAX]; |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 423 | wchar_t wch[2]; |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 424 | char *puch, *pmbch; |
| 425 | size_t such, smbch; |
| 426 | mbstate_t ps; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 427 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 428 | puch = (char *) &uch; |
| 429 | pmbch = (char *) mbch; |
| 430 | such = sizeof(uch); |
| 431 | smbch = sizeof(mbch); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 432 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 433 | if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) { |
| 434 | fprintf(stderr, "Could not convert 0x%04x " |
| 435 | "from UCS-2 to a multibyte character: %s\n", |
| 436 | uch, strerror(errno)); |
| 437 | return; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 438 | } |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 439 | |
| 440 | memset(&ps, 0, sizeof(ps)); |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 441 | if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 442 | fprintf(stderr, "Could not convert 0x%04x " |
| 443 | "from a multibyte character to wchar_t: %s\n", |
| 444 | uch, strerror(errno)); |
| 445 | return; |
| 446 | } |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 447 | |
| 448 | wch[1] = 0; |
| 449 | setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Setup wchar glyph for one font character */ |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 453 | static void convert_font(unsigned char fch, iconv_t conv) |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 454 | { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 455 | char mbch[MB_LEN_MAX]; |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 456 | wchar_t wch[2]; |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 457 | char *pfch, *pmbch; |
| 458 | size_t sfch, smbch; |
| 459 | mbstate_t ps; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 460 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 461 | pfch = (char *) &fch; |
| 462 | pmbch = (char *) &mbch; |
| 463 | sfch = sizeof(fch); |
| 464 | smbch = sizeof(mbch); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 465 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 466 | if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) { |
| 467 | fprintf(stderr, "Could not convert font glyph 0x%02x " |
| 468 | "from %s to a multibyte character: %s\n", |
| 469 | fch, font_charset, strerror(errno)); |
| 470 | return; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 471 | } |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 472 | |
| 473 | memset(&ps, 0, sizeof(ps)); |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 474 | if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 475 | fprintf(stderr, "Could not convert font glyph 0x%02x " |
| 476 | "from a multibyte character to wchar_t: %s\n", |
| 477 | fch, strerror(errno)); |
| 478 | return; |
| 479 | } |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 480 | |
| 481 | wch[1] = 0; |
| 482 | setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* Convert one wchar to UCS-2 */ |
| 486 | static uint16_t get_ucs(wchar_t wch, iconv_t conv) |
| 487 | { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 488 | char mbch[MB_LEN_MAX]; |
| 489 | uint16_t uch; |
| 490 | char *pmbch, *puch; |
| 491 | size_t smbch, such; |
| 492 | mbstate_t ps; |
| 493 | int ret; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 494 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 495 | memset(&ps, 0, sizeof(ps)); |
| 496 | ret = wcrtomb(mbch, wch, &ps); |
| 497 | if (ret == -1) { |
Max Reitz | dc3c871 | 2019-05-27 16:25:40 +0200 | [diff] [blame] | 498 | fprintf(stderr, "Could not convert 0x%04lx " |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 499 | "from wchar_t to a multibyte character: %s\n", |
Max Reitz | dc3c871 | 2019-05-27 16:25:40 +0200 | [diff] [blame] | 500 | (unsigned long)wch, strerror(errno)); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 501 | return 0xFFFD; |
| 502 | } |
| 503 | |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 504 | pmbch = (char *) mbch; |
| 505 | puch = (char *) &uch; |
| 506 | smbch = ret; |
| 507 | such = sizeof(uch); |
| 508 | |
| 509 | if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) { |
Max Reitz | dc3c871 | 2019-05-27 16:25:40 +0200 | [diff] [blame] | 510 | fprintf(stderr, "Could not convert 0x%04lx " |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 511 | "from a multibyte character to UCS-2 : %s\n", |
Max Reitz | dc3c871 | 2019-05-27 16:25:40 +0200 | [diff] [blame] | 512 | (unsigned long)wch, strerror(errno)); |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 513 | return 0xFFFD; |
| 514 | } |
| 515 | |
| 516 | return uch; |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Setup mapping for vga to curses line graphics. |
| 521 | */ |
| 522 | static void font_setup(void) |
| 523 | { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 524 | iconv_t ucs2_to_nativecharset; |
| 525 | iconv_t nativecharset_to_ucs2; |
| 526 | iconv_t font_conv; |
| 527 | int i; |
Yonggang Luo | 80d3ab6 | 2020-10-13 07:43:45 +0800 | [diff] [blame] | 528 | g_autofree gchar *local_codeset = g_get_codeset(); |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 529 | |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 530 | /* |
| 531 | * Control characters are normally non-printable, but VGA does have |
| 532 | * well-known glyphs for them. |
| 533 | */ |
Philippe Mathieu-Daudé | 80e8c2e | 2020-03-05 13:45:23 +0100 | [diff] [blame] | 534 | static const uint16_t control_characters[0x20] = { |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 535 | 0x0020, |
| 536 | 0x263a, |
| 537 | 0x263b, |
| 538 | 0x2665, |
| 539 | 0x2666, |
| 540 | 0x2663, |
| 541 | 0x2660, |
| 542 | 0x2022, |
| 543 | 0x25d8, |
| 544 | 0x25cb, |
| 545 | 0x25d9, |
| 546 | 0x2642, |
| 547 | 0x2640, |
| 548 | 0x266a, |
| 549 | 0x266b, |
| 550 | 0x263c, |
| 551 | 0x25ba, |
| 552 | 0x25c4, |
| 553 | 0x2195, |
| 554 | 0x203c, |
| 555 | 0x00b6, |
| 556 | 0x00a7, |
| 557 | 0x25ac, |
| 558 | 0x21a8, |
| 559 | 0x2191, |
| 560 | 0x2193, |
| 561 | 0x2192, |
| 562 | 0x2190, |
| 563 | 0x221f, |
| 564 | 0x2194, |
| 565 | 0x25b2, |
| 566 | 0x25bc |
| 567 | }; |
| 568 | |
Yonggang Luo | 80d3ab6 | 2020-10-13 07:43:45 +0800 | [diff] [blame] | 569 | ucs2_to_nativecharset = iconv_open(local_codeset, "UCS-2"); |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 570 | if (ucs2_to_nativecharset == (iconv_t) -1) { |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 571 | fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n", |
| 572 | strerror(errno)); |
| 573 | exit(1); |
| 574 | } |
| 575 | |
Yonggang Luo | 80d3ab6 | 2020-10-13 07:43:45 +0800 | [diff] [blame] | 576 | nativecharset_to_ucs2 = iconv_open("UCS-2", local_codeset); |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 577 | if (nativecharset_to_ucs2 == (iconv_t) -1) { |
| 578 | iconv_close(ucs2_to_nativecharset); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 579 | fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n", |
| 580 | strerror(errno)); |
| 581 | exit(1); |
| 582 | } |
| 583 | |
Yonggang Luo | 80d3ab6 | 2020-10-13 07:43:45 +0800 | [diff] [blame] | 584 | font_conv = iconv_open(local_codeset, font_charset); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 585 | if (font_conv == (iconv_t) -1) { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 586 | iconv_close(ucs2_to_nativecharset); |
| 587 | iconv_close(nativecharset_to_ucs2); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 588 | fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n", |
| 589 | font_charset, strerror(errno)); |
| 590 | exit(1); |
| 591 | } |
| 592 | |
| 593 | /* Control characters */ |
| 594 | for (i = 0; i <= 0x1F; i++) { |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 595 | convert_ucs(i, control_characters[i], ucs2_to_nativecharset); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | for (i = 0x20; i <= 0xFF; i++) { |
| 599 | convert_font(i, font_conv); |
| 600 | } |
| 601 | |
| 602 | /* DEL */ |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 603 | convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 604 | |
Yonggang Luo | 80d3ab6 | 2020-10-13 07:43:45 +0800 | [diff] [blame] | 605 | if (strcmp(local_codeset, "UTF-8")) { |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 606 | /* Non-Unicode capable, use termcap equivalents for those available */ |
| 607 | for (i = 0; i <= 0xFF; i++) { |
Samuel Thibault | 962cf8f | 2019-04-27 20:33:07 +0200 | [diff] [blame] | 608 | wchar_t wch[CCHARW_MAX]; |
| 609 | attr_t attr; |
| 610 | short color; |
| 611 | int ret; |
| 612 | |
| 613 | ret = getcchar(&vga_to_curses[i], wch, &attr, &color, NULL); |
| 614 | if (ret == ERR) |
| 615 | continue; |
| 616 | |
| 617 | switch (get_ucs(wch[0], nativecharset_to_ucs2)) { |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 618 | case 0x00a3: |
| 619 | vga_to_curses[i] = *WACS_STERLING; |
| 620 | break; |
| 621 | case 0x2591: |
| 622 | vga_to_curses[i] = *WACS_BOARD; |
| 623 | break; |
| 624 | case 0x2592: |
| 625 | vga_to_curses[i] = *WACS_CKBOARD; |
| 626 | break; |
| 627 | case 0x2502: |
| 628 | vga_to_curses[i] = *WACS_VLINE; |
| 629 | break; |
| 630 | case 0x2524: |
| 631 | vga_to_curses[i] = *WACS_RTEE; |
| 632 | break; |
| 633 | case 0x2510: |
| 634 | vga_to_curses[i] = *WACS_URCORNER; |
| 635 | break; |
| 636 | case 0x2514: |
| 637 | vga_to_curses[i] = *WACS_LLCORNER; |
| 638 | break; |
| 639 | case 0x2534: |
| 640 | vga_to_curses[i] = *WACS_BTEE; |
| 641 | break; |
| 642 | case 0x252c: |
| 643 | vga_to_curses[i] = *WACS_TTEE; |
| 644 | break; |
| 645 | case 0x251c: |
| 646 | vga_to_curses[i] = *WACS_LTEE; |
| 647 | break; |
| 648 | case 0x2500: |
| 649 | vga_to_curses[i] = *WACS_HLINE; |
| 650 | break; |
| 651 | case 0x253c: |
| 652 | vga_to_curses[i] = *WACS_PLUS; |
| 653 | break; |
| 654 | case 0x256c: |
| 655 | vga_to_curses[i] = *WACS_LANTERN; |
| 656 | break; |
| 657 | case 0x256a: |
| 658 | vga_to_curses[i] = *WACS_NEQUAL; |
| 659 | break; |
| 660 | case 0x2518: |
| 661 | vga_to_curses[i] = *WACS_LRCORNER; |
| 662 | break; |
| 663 | case 0x250c: |
| 664 | vga_to_curses[i] = *WACS_ULCORNER; |
| 665 | break; |
| 666 | case 0x2588: |
| 667 | vga_to_curses[i] = *WACS_BLOCK; |
| 668 | break; |
| 669 | case 0x03c0: |
| 670 | vga_to_curses[i] = *WACS_PI; |
| 671 | break; |
| 672 | case 0x00b1: |
| 673 | vga_to_curses[i] = *WACS_PLMINUS; |
| 674 | break; |
| 675 | case 0x2265: |
| 676 | vga_to_curses[i] = *WACS_GEQUAL; |
| 677 | break; |
| 678 | case 0x2264: |
| 679 | vga_to_curses[i] = *WACS_LEQUAL; |
| 680 | break; |
| 681 | case 0x00b0: |
| 682 | vga_to_curses[i] = *WACS_DEGREE; |
| 683 | break; |
| 684 | case 0x25a0: |
| 685 | vga_to_curses[i] = *WACS_BULLET; |
| 686 | break; |
| 687 | case 0x2666: |
| 688 | vga_to_curses[i] = *WACS_DIAMOND; |
| 689 | break; |
| 690 | case 0x2192: |
| 691 | vga_to_curses[i] = *WACS_RARROW; |
| 692 | break; |
| 693 | case 0x2190: |
| 694 | vga_to_curses[i] = *WACS_LARROW; |
| 695 | break; |
| 696 | case 0x2191: |
| 697 | vga_to_curses[i] = *WACS_UARROW; |
| 698 | break; |
| 699 | case 0x2193: |
| 700 | vga_to_curses[i] = *WACS_DARROW; |
| 701 | break; |
| 702 | case 0x23ba: |
| 703 | vga_to_curses[i] = *WACS_S1; |
| 704 | break; |
| 705 | case 0x23bb: |
| 706 | vga_to_curses[i] = *WACS_S3; |
| 707 | break; |
| 708 | case 0x23bc: |
| 709 | vga_to_curses[i] = *WACS_S7; |
| 710 | break; |
| 711 | case 0x23bd: |
| 712 | vga_to_curses[i] = *WACS_S9; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | } |
Samuel Thibault | b7b664a | 2019-04-27 20:33:06 +0200 | [diff] [blame] | 717 | iconv_close(ucs2_to_nativecharset); |
| 718 | iconv_close(nativecharset_to_ucs2); |
Samuel Thibault | a9fda24 | 2019-03-14 18:25:24 +0100 | [diff] [blame] | 719 | iconv_close(font_conv); |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 720 | } |
| 721 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 722 | static void curses_setup(void) |
| 723 | { |
| 724 | int i, colour_default[8] = { |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 725 | [QEMU_COLOR_BLACK] = COLOR_BLACK, |
| 726 | [QEMU_COLOR_BLUE] = COLOR_BLUE, |
| 727 | [QEMU_COLOR_GREEN] = COLOR_GREEN, |
| 728 | [QEMU_COLOR_CYAN] = COLOR_CYAN, |
| 729 | [QEMU_COLOR_RED] = COLOR_RED, |
| 730 | [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA, |
| 731 | [QEMU_COLOR_YELLOW] = COLOR_YELLOW, |
| 732 | [QEMU_COLOR_WHITE] = COLOR_WHITE, |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 733 | }; |
| 734 | |
| 735 | /* input as raw as possible, let everything be interpreted |
| 736 | * by the guest system */ |
| 737 | initscr(); noecho(); intrflush(stdscr, FALSE); |
| 738 | nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); |
| 739 | start_color(); raw(); scrollok(stdscr, FALSE); |
Samuel Thibault | 633786f | 2019-03-03 18:25:57 +0100 | [diff] [blame] | 740 | set_escdelay(25); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 741 | |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 742 | /* Make color pair to match color format (3bits bg:3bits fg) */ |
OGAWA Hirofumi | 615220d | 2015-10-19 21:23:10 +0900 | [diff] [blame] | 743 | for (i = 0; i < 64; i++) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 744 | init_pair(i, colour_default[i & 7], colour_default[i >> 3]); |
OGAWA Hirofumi | 615220d | 2015-10-19 21:23:10 +0900 | [diff] [blame] | 745 | } |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 746 | /* Set default color for more than 64 for safety. */ |
OGAWA Hirofumi | 615220d | 2015-10-19 21:23:10 +0900 | [diff] [blame] | 747 | for (i = 64; i < COLOR_PAIRS; i++) { |
| 748 | init_pair(i, COLOR_WHITE, COLOR_BLACK); |
| 749 | } |
OGAWA Hirofumi | e2368dc | 2015-10-19 21:23:46 +0900 | [diff] [blame] | 750 | |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 751 | font_setup(); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static void curses_keyboard_setup(void) |
| 755 | { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 756 | #if defined(__APPLE__) |
| 757 | /* always use generic keymaps */ |
| 758 | if (!keyboard_layout) |
| 759 | keyboard_layout = "en-us"; |
| 760 | #endif |
| 761 | if(keyboard_layout) { |
Fei Li | ab4f931 | 2018-10-17 10:26:50 +0200 | [diff] [blame] | 762 | kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout, |
| 763 | &error_fatal); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 764 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 767 | static const DisplayChangeListenerOps dcl_ops = { |
| 768 | .dpy_name = "curses", |
| 769 | .dpy_text_update = curses_update, |
| 770 | .dpy_text_resize = curses_resize, |
| 771 | .dpy_refresh = curses_refresh, |
| 772 | .dpy_text_cursor = curses_cursor_position, |
| 773 | }; |
| 774 | |
Gerd Hoffmann | b076661 | 2018-03-01 11:05:38 +0100 | [diff] [blame] | 775 | static void curses_display_init(DisplayState *ds, DisplayOptions *opts) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 776 | { |
| 777 | #ifndef _WIN32 |
| 778 | if (!isatty(1)) { |
| 779 | fprintf(stderr, "We need a terminal output\n"); |
| 780 | exit(1); |
| 781 | } |
| 782 | #endif |
| 783 | |
Samuel Thibault | 2f8b7cd | 2019-03-11 14:51:27 +0100 | [diff] [blame] | 784 | setlocale(LC_CTYPE, ""); |
| 785 | if (opts->u.curses.charset) { |
| 786 | font_charset = opts->u.curses.charset; |
| 787 | } |
Philippe Mathieu-Daudé | 76c51fc | 2020-03-05 13:45:24 +0100 | [diff] [blame] | 788 | screen = g_new0(console_ch_t, 160 * 100); |
| 789 | vga_to_curses = g_new0(cchar_t, 256); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 790 | curses_setup(); |
| 791 | curses_keyboard_setup(); |
Anthony Liguori | 2869548 | 2010-03-21 14:13:02 -0500 | [diff] [blame] | 792 | atexit(curses_atexit); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 793 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 794 | curses_winch_init(); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 795 | |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 796 | dcl = g_new0(DisplayChangeListener, 1); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 797 | dcl->ops = &dcl_ops; |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 798 | register_displaychangelistener(dcl); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 799 | |
| 800 | invalidate = 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 801 | } |
Gerd Hoffmann | b076661 | 2018-03-01 11:05:38 +0100 | [diff] [blame] | 802 | |
| 803 | static QemuDisplay qemu_display_curses = { |
| 804 | .type = DISPLAY_TYPE_CURSES, |
| 805 | .init = curses_display_init, |
| 806 | }; |
| 807 | |
| 808 | static void register_curses(void) |
| 809 | { |
| 810 | qemu_display_register(&qemu_display_curses); |
| 811 | } |
| 812 | |
| 813 | type_init(register_curses); |