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 | */ |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 25 | |
| 26 | #ifndef _WIN32 |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
| 28 | #include <termios.h> |
| 29 | #endif |
| 30 | |
Fei Li | ab4f931 | 2018-10-17 10:26:50 +0200 | [diff] [blame] | 31 | #include "qapi/error.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 32 | #include "qemu-common.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 33 | #include "ui/console.h" |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 34 | #include "ui/input.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/sysemu.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 36 | |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 37 | /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */ |
| 38 | #undef KEY_EVENT |
| 39 | #include <curses.h> |
| 40 | #undef KEY_EVENT |
| 41 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 42 | #define FONT_HEIGHT 16 |
| 43 | #define FONT_WIDTH 8 |
| 44 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 45 | static DisplayChangeListener *dcl; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 46 | static console_ch_t screen[160 * 100]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 47 | static WINDOW *screenpad = NULL; |
| 48 | static int width, height, gwidth, gheight, invalidate; |
| 49 | static int px, py, sminx, sminy, smaxx, smaxy; |
| 50 | |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 51 | static chtype vga_to_curses[256]; |
OGAWA Hirofumi | e2368dc | 2015-10-19 21:23:46 +0900 | [diff] [blame] | 52 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 53 | static void curses_update(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 54 | int x, int y, int w, int h) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 55 | { |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 56 | console_ch_t *line; |
| 57 | chtype curses_line[width]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 58 | |
Gerd Hoffmann | e2f82e9 | 2017-09-27 12:38:11 +0200 | [diff] [blame] | 59 | line = screen + y * width; |
| 60 | for (h += y; y < h; y ++, line += width) { |
| 61 | for (x = 0; x < width; x++) { |
| 62 | chtype ch = line[x] & 0xff; |
| 63 | chtype at = line[x] & ~0xff; |
| 64 | if (vga_to_curses[ch]) { |
| 65 | ch = vga_to_curses[ch]; |
| 66 | } |
| 67 | curses_line[x] = ch | at; |
| 68 | } |
| 69 | mvwaddchnstr(screenpad, y, 0, curses_line, width); |
| 70 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 71 | |
| 72 | pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1); |
| 73 | refresh(); |
| 74 | } |
| 75 | |
| 76 | static void curses_calc_pad(void) |
| 77 | { |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 78 | if (qemu_console_is_fixedsize(NULL)) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 79 | width = gwidth; |
| 80 | height = gheight; |
| 81 | } else { |
| 82 | width = COLS; |
| 83 | height = LINES; |
| 84 | } |
| 85 | |
| 86 | if (screenpad) |
| 87 | delwin(screenpad); |
| 88 | |
| 89 | clear(); |
| 90 | refresh(); |
| 91 | |
| 92 | screenpad = newpad(height, width); |
| 93 | |
| 94 | if (width > COLS) { |
| 95 | px = (width - COLS) / 2; |
| 96 | sminx = 0; |
| 97 | smaxx = COLS; |
| 98 | } else { |
| 99 | px = 0; |
| 100 | sminx = (COLS - width) / 2; |
| 101 | smaxx = sminx + width; |
| 102 | } |
| 103 | |
| 104 | if (height > LINES) { |
| 105 | py = (height - LINES) / 2; |
| 106 | sminy = 0; |
| 107 | smaxy = LINES; |
| 108 | } else { |
| 109 | py = 0; |
| 110 | sminy = (LINES - height) / 2; |
| 111 | smaxy = sminy + height; |
| 112 | } |
| 113 | } |
| 114 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 115 | static void curses_resize(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 116 | int width, int height) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 117 | { |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 118 | if (width == gwidth && height == gheight) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 119 | return; |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 120 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 121 | |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 122 | gwidth = width; |
| 123 | gheight = height; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 124 | |
| 125 | curses_calc_pad(); |
| 126 | } |
| 127 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 128 | #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE) |
| 129 | static volatile sig_atomic_t got_sigwinch; |
| 130 | static void curses_winch_check(void) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 131 | { |
| 132 | struct winsize { |
| 133 | unsigned short ws_row; |
| 134 | unsigned short ws_col; |
| 135 | unsigned short ws_xpixel; /* unused */ |
| 136 | unsigned short ws_ypixel; /* unused */ |
| 137 | } ws; |
| 138 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 139 | if (!got_sigwinch) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 140 | return; |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 141 | } |
| 142 | got_sigwinch = false; |
| 143 | |
| 144 | if (ioctl(1, TIOCGWINSZ, &ws) == -1) { |
| 145 | return; |
| 146 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 147 | |
| 148 | resize_term(ws.ws_row, ws.ws_col); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 149 | invalidate = 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 150 | } |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 151 | |
| 152 | static void curses_winch_handler(int signum) |
| 153 | { |
| 154 | got_sigwinch = true; |
| 155 | } |
| 156 | |
| 157 | static void curses_winch_init(void) |
| 158 | { |
| 159 | struct sigaction old, winch = { |
| 160 | .sa_handler = curses_winch_handler, |
| 161 | }; |
| 162 | sigaction(SIGWINCH, &winch, &old); |
| 163 | } |
| 164 | #else |
| 165 | static void curses_winch_check(void) {} |
| 166 | static void curses_winch_init(void) {} |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 169 | static void curses_cursor_position(DisplayChangeListener *dcl, |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 170 | int x, int y) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 171 | { |
| 172 | if (x >= 0) { |
| 173 | x = sminx + x - px; |
| 174 | y = sminy + y - py; |
| 175 | |
| 176 | if (x >= 0 && y >= 0 && x < COLS && y < LINES) { |
| 177 | move(y, x); |
| 178 | curs_set(1); |
| 179 | /* it seems that curs_set(1) must always be called before |
| 180 | * curs_set(2) for the latter to have effect */ |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 181 | if (!qemu_console_is_graphic(NULL)) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 182 | curs_set(2); |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 183 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | curs_set(0); |
| 189 | } |
| 190 | |
| 191 | /* generic keyboard conversion */ |
| 192 | |
| 193 | #include "curses_keys.h" |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 194 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 195 | static kbd_layout_t *kbd_layout = NULL; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 196 | |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 197 | static void curses_refresh(DisplayChangeListener *dcl) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 198 | { |
Peter Maydell | 99a9ef4 | 2016-08-11 15:23:27 +0100 | [diff] [blame] | 199 | int chr, keysym, keycode, keycode_alt; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 200 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 201 | curses_winch_check(); |
| 202 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 203 | if (invalidate) { |
| 204 | clear(); |
| 205 | refresh(); |
| 206 | curses_calc_pad(); |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 207 | graphic_hw_invalidate(NULL); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 208 | invalidate = 0; |
| 209 | } |
| 210 | |
Gerd Hoffmann | 1dbfa00 | 2013-03-12 13:44:38 +0100 | [diff] [blame] | 211 | graphic_hw_text_update(NULL, screen); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 212 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 213 | while (1) { |
| 214 | /* while there are any pending key strokes to process */ |
Peter Maydell | 99a9ef4 | 2016-08-11 15:23:27 +0100 | [diff] [blame] | 215 | chr = getch(); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 216 | |
| 217 | if (chr == ERR) |
| 218 | break; |
| 219 | |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 220 | #ifdef KEY_RESIZE |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 221 | /* this shouldn't occur when we use a custom SIGWINCH handler */ |
| 222 | if (chr == KEY_RESIZE) { |
| 223 | clear(); |
| 224 | refresh(); |
| 225 | curses_calc_pad(); |
Gerd Hoffmann | bc2ed97 | 2013-03-01 13:03:04 +0100 | [diff] [blame] | 226 | curses_update(dcl, 0, 0, width, height); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 227 | continue; |
| 228 | } |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 229 | #endif |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 230 | |
| 231 | keycode = curses2keycode[chr]; |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 232 | keycode_alt = 0; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 233 | |
| 234 | /* alt key */ |
| 235 | if (keycode == 1) { |
Peter Maydell | 99a9ef4 | 2016-08-11 15:23:27 +0100 | [diff] [blame] | 236 | int nextchr = getch(); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 237 | |
| 238 | if (nextchr != ERR) { |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 239 | chr = nextchr; |
| 240 | keycode_alt = ALT; |
Peter Maydell | 99a9ef4 | 2016-08-11 15:23:27 +0100 | [diff] [blame] | 241 | keycode = curses2keycode[chr]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 242 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 243 | if (keycode != -1) { |
| 244 | keycode |= ALT; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 245 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 246 | /* process keys reserved for qemu */ |
| 247 | if (keycode >= QEMU_KEY_CONSOLE0 && |
| 248 | keycode < QEMU_KEY_CONSOLE0 + 9) { |
| 249 | erase(); |
| 250 | wnoutrefresh(stdscr); |
| 251 | console_select(keycode - QEMU_KEY_CONSOLE0); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 252 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 253 | invalidate = 1; |
| 254 | continue; |
| 255 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 260 | if (kbd_layout) { |
| 261 | keysym = -1; |
| 262 | if (chr < CURSES_KEYS) |
| 263 | keysym = curses2keysym[chr]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 264 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 265 | if (keysym == -1) { |
Samuel Thibault | d03703c | 2010-10-19 19:48:20 +0200 | [diff] [blame] | 266 | if (chr < ' ') { |
| 267 | keysym = chr + '@'; |
| 268 | if (keysym >= 'A' && keysym <= 'Z') |
| 269 | keysym += 'a' - 'A'; |
| 270 | keysym |= KEYSYM_CNTRL; |
| 271 | } else |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 272 | keysym = chr; |
| 273 | } |
| 274 | |
Gerd Hoffmann | abb4f2c | 2018-02-22 08:05:13 +0100 | [diff] [blame] | 275 | keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK, |
| 276 | false, false, false); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 277 | if (keycode == 0) |
| 278 | continue; |
| 279 | |
| 280 | keycode |= (keysym & ~KEYSYM_MASK) >> 16; |
| 281 | keycode |= keycode_alt; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 284 | if (keycode == -1) |
| 285 | continue; |
| 286 | |
Gerd Hoffmann | 81c0d5a | 2013-03-14 14:27:08 +0100 | [diff] [blame] | 287 | if (qemu_console_is_graphic(NULL)) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 288 | /* since terminals don't know about key press and release |
| 289 | * events, we need to emit both for each key received */ |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 290 | if (keycode & SHIFT) { |
| 291 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 292 | qemu_input_event_send_key_delay(0); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 293 | } |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 294 | if (keycode & CNTRL) { |
| 295 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 296 | qemu_input_event_send_key_delay(0); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 297 | } |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 298 | if (keycode & ALT) { |
| 299 | qemu_input_event_send_key_number(NULL, ALT_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 300 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 301 | } |
| 302 | if (keycode & ALTGR) { |
| 303 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 304 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Andrew Oates | f5c0ab1 | 2014-05-23 20:16:09 -0400 | [diff] [blame] | 307 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 308 | qemu_input_event_send_key_delay(0); |
Andrew Oates | f5c0ab1 | 2014-05-23 20:16:09 -0400 | [diff] [blame] | 309 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 310 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 311 | |
| 312 | if (keycode & ALTGR) { |
| 313 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 314 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 315 | } |
| 316 | if (keycode & ALT) { |
| 317 | qemu_input_event_send_key_number(NULL, ALT_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 318 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 319 | } |
| 320 | if (keycode & CNTRL) { |
| 321 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 322 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 323 | } |
| 324 | if (keycode & SHIFT) { |
| 325 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, false); |
Gerd Hoffmann | 5a16566 | 2014-05-28 13:05:04 +0200 | [diff] [blame] | 326 | qemu_input_event_send_key_delay(0); |
Gerd Hoffmann | cd10032 | 2013-12-04 13:40:20 +0100 | [diff] [blame] | 327 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 328 | } else { |
Peter Maydell | bba4e1b | 2016-08-11 15:23:26 +0100 | [diff] [blame] | 329 | keysym = -1; |
| 330 | if (chr < CURSES_KEYS) { |
| 331 | keysym = curses2qemu[chr]; |
| 332 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 333 | if (keysym == -1) |
| 334 | keysym = chr; |
| 335 | |
| 336 | kbd_put_keysym(keysym); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
Blue Swirl | aaf12c2 | 2010-03-21 19:44:06 +0000 | [diff] [blame] | 341 | static void curses_atexit(void) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 342 | { |
| 343 | endwin(); |
| 344 | } |
| 345 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 346 | static void curses_setup(void) |
| 347 | { |
| 348 | int i, colour_default[8] = { |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 349 | [QEMU_COLOR_BLACK] = COLOR_BLACK, |
| 350 | [QEMU_COLOR_BLUE] = COLOR_BLUE, |
| 351 | [QEMU_COLOR_GREEN] = COLOR_GREEN, |
| 352 | [QEMU_COLOR_CYAN] = COLOR_CYAN, |
| 353 | [QEMU_COLOR_RED] = COLOR_RED, |
| 354 | [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA, |
| 355 | [QEMU_COLOR_YELLOW] = COLOR_YELLOW, |
| 356 | [QEMU_COLOR_WHITE] = COLOR_WHITE, |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | /* input as raw as possible, let everything be interpreted |
| 360 | * by the guest system */ |
| 361 | initscr(); noecho(); intrflush(stdscr, FALSE); |
| 362 | nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); |
| 363 | start_color(); raw(); scrollok(stdscr, FALSE); |
| 364 | |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 365 | /* Make color pair to match color format (3bits bg:3bits fg) */ |
OGAWA Hirofumi | 615220d | 2015-10-19 21:23:10 +0900 | [diff] [blame] | 366 | for (i = 0; i < 64; i++) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 367 | init_pair(i, colour_default[i & 7], colour_default[i >> 3]); |
OGAWA Hirofumi | 615220d | 2015-10-19 21:23:10 +0900 | [diff] [blame] | 368 | } |
OGAWA Hirofumi | 4083733 | 2015-11-29 22:28:24 +0900 | [diff] [blame] | 369 | /* Set default color for more than 64 for safety. */ |
OGAWA Hirofumi | 615220d | 2015-10-19 21:23:10 +0900 | [diff] [blame] | 370 | for (i = 64; i < COLOR_PAIRS; i++) { |
| 371 | init_pair(i, COLOR_WHITE, COLOR_BLACK); |
| 372 | } |
OGAWA Hirofumi | e2368dc | 2015-10-19 21:23:46 +0900 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * Setup mapping for vga to curses line graphics. |
| 376 | * FIXME: for better font, have to use ncursesw and setlocale() |
| 377 | */ |
| 378 | #if 0 |
| 379 | /* FIXME: map from where? */ |
| 380 | ACS_S1; |
| 381 | ACS_S3; |
| 382 | ACS_S7; |
| 383 | ACS_S9; |
| 384 | #endif |
| 385 | /* ACS_* is not constant. So, we can't initialize statically. */ |
| 386 | vga_to_curses['\0'] = ' '; |
| 387 | vga_to_curses[0x04] = ACS_DIAMOND; |
OGAWA Hirofumi | e2368dc | 2015-10-19 21:23:46 +0900 | [diff] [blame] | 388 | vga_to_curses[0x18] = ACS_UARROW; |
| 389 | vga_to_curses[0x19] = ACS_DARROW; |
Samuel Thibault | 697783a | 2016-10-15 21:53:04 +0200 | [diff] [blame] | 390 | vga_to_curses[0x1a] = ACS_RARROW; |
| 391 | vga_to_curses[0x1b] = ACS_LARROW; |
OGAWA Hirofumi | e2368dc | 2015-10-19 21:23:46 +0900 | [diff] [blame] | 392 | vga_to_curses[0x9c] = ACS_STERLING; |
| 393 | vga_to_curses[0xb0] = ACS_BOARD; |
| 394 | vga_to_curses[0xb1] = ACS_CKBOARD; |
| 395 | vga_to_curses[0xb3] = ACS_VLINE; |
| 396 | vga_to_curses[0xb4] = ACS_RTEE; |
| 397 | vga_to_curses[0xbf] = ACS_URCORNER; |
| 398 | vga_to_curses[0xc0] = ACS_LLCORNER; |
| 399 | vga_to_curses[0xc1] = ACS_BTEE; |
| 400 | vga_to_curses[0xc2] = ACS_TTEE; |
| 401 | vga_to_curses[0xc3] = ACS_LTEE; |
| 402 | vga_to_curses[0xc4] = ACS_HLINE; |
| 403 | vga_to_curses[0xc5] = ACS_PLUS; |
| 404 | vga_to_curses[0xce] = ACS_LANTERN; |
| 405 | vga_to_curses[0xd8] = ACS_NEQUAL; |
| 406 | vga_to_curses[0xd9] = ACS_LRCORNER; |
| 407 | vga_to_curses[0xda] = ACS_ULCORNER; |
| 408 | vga_to_curses[0xdb] = ACS_BLOCK; |
| 409 | vga_to_curses[0xe3] = ACS_PI; |
| 410 | vga_to_curses[0xf1] = ACS_PLMINUS; |
| 411 | vga_to_curses[0xf2] = ACS_GEQUAL; |
| 412 | vga_to_curses[0xf3] = ACS_LEQUAL; |
| 413 | vga_to_curses[0xf8] = ACS_DEGREE; |
| 414 | vga_to_curses[0xfe] = ACS_BULLET; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static void curses_keyboard_setup(void) |
| 418 | { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 419 | #if defined(__APPLE__) |
| 420 | /* always use generic keymaps */ |
| 421 | if (!keyboard_layout) |
| 422 | keyboard_layout = "en-us"; |
| 423 | #endif |
| 424 | if(keyboard_layout) { |
Fei Li | ab4f931 | 2018-10-17 10:26:50 +0200 | [diff] [blame] | 425 | kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout, |
| 426 | &error_fatal); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 427 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 430 | static const DisplayChangeListenerOps dcl_ops = { |
| 431 | .dpy_name = "curses", |
| 432 | .dpy_text_update = curses_update, |
| 433 | .dpy_text_resize = curses_resize, |
| 434 | .dpy_refresh = curses_refresh, |
| 435 | .dpy_text_cursor = curses_cursor_position, |
| 436 | }; |
| 437 | |
Gerd Hoffmann | b076661 | 2018-03-01 11:05:38 +0100 | [diff] [blame] | 438 | static void curses_display_init(DisplayState *ds, DisplayOptions *opts) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 439 | { |
| 440 | #ifndef _WIN32 |
| 441 | if (!isatty(1)) { |
| 442 | fprintf(stderr, "We need a terminal output\n"); |
| 443 | exit(1); |
| 444 | } |
| 445 | #endif |
| 446 | |
| 447 | curses_setup(); |
| 448 | curses_keyboard_setup(); |
Anthony Liguori | 2869548 | 2010-03-21 14:13:02 -0500 | [diff] [blame] | 449 | atexit(curses_atexit); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 450 | |
Gerd Hoffmann | 032ac6f | 2013-11-22 15:35:03 +0100 | [diff] [blame] | 451 | curses_winch_init(); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 452 | |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 453 | dcl = g_new0(DisplayChangeListener, 1); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 454 | dcl->ops = &dcl_ops; |
Gerd Hoffmann | 5209089 | 2013-04-23 15:44:31 +0200 | [diff] [blame] | 455 | register_displaychangelistener(dcl); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 456 | |
| 457 | invalidate = 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 458 | } |
Gerd Hoffmann | b076661 | 2018-03-01 11:05:38 +0100 | [diff] [blame] | 459 | |
| 460 | static QemuDisplay qemu_display_curses = { |
| 461 | .type = DISPLAY_TYPE_CURSES, |
| 462 | .init = curses_display_init, |
| 463 | }; |
| 464 | |
| 465 | static void register_curses(void) |
| 466 | { |
| 467 | qemu_display_register(&qemu_display_curses); |
| 468 | } |
| 469 | |
| 470 | type_init(register_curses); |