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 | */ |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 24 | #include <curses.h> |
| 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 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 31 | #include "qemu-common.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 32 | #include "ui/console.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 33 | #include "sysemu/sysemu.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 34 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 35 | #define FONT_HEIGHT 16 |
| 36 | #define FONT_WIDTH 8 |
| 37 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 38 | static DisplayChangeListener *dcl; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 39 | static console_ch_t screen[160 * 100]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 40 | static WINDOW *screenpad = NULL; |
| 41 | static int width, height, gwidth, gheight, invalidate; |
| 42 | static int px, py, sminx, sminy, smaxx, smaxy; |
| 43 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 44 | static void curses_update(DisplayChangeListener *dcl, |
| 45 | DisplayState *ds, |
| 46 | int x, int y, int w, int h) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 47 | { |
| 48 | chtype *line; |
| 49 | |
| 50 | line = ((chtype *) screen) + y * width; |
| 51 | for (h += y; y < h; y ++, line += width) |
| 52 | mvwaddchnstr(screenpad, y, 0, line, width); |
| 53 | |
| 54 | pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1); |
| 55 | refresh(); |
| 56 | } |
| 57 | |
| 58 | static void curses_calc_pad(void) |
| 59 | { |
balrog | c21bbcf | 2008-09-24 03:32:33 +0000 | [diff] [blame] | 60 | if (is_fixedsize_console()) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 61 | width = gwidth; |
| 62 | height = gheight; |
| 63 | } else { |
| 64 | width = COLS; |
| 65 | height = LINES; |
| 66 | } |
| 67 | |
| 68 | if (screenpad) |
| 69 | delwin(screenpad); |
| 70 | |
| 71 | clear(); |
| 72 | refresh(); |
| 73 | |
| 74 | screenpad = newpad(height, width); |
| 75 | |
| 76 | if (width > COLS) { |
| 77 | px = (width - COLS) / 2; |
| 78 | sminx = 0; |
| 79 | smaxx = COLS; |
| 80 | } else { |
| 81 | px = 0; |
| 82 | sminx = (COLS - width) / 2; |
| 83 | smaxx = sminx + width; |
| 84 | } |
| 85 | |
| 86 | if (height > LINES) { |
| 87 | py = (height - LINES) / 2; |
| 88 | sminy = 0; |
| 89 | smaxy = LINES; |
| 90 | } else { |
| 91 | py = 0; |
| 92 | sminy = (LINES - height) / 2; |
| 93 | smaxy = sminy + height; |
| 94 | } |
| 95 | } |
| 96 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 97 | static void curses_resize(DisplayChangeListener *dcl, |
| 98 | DisplayState *ds, |
| 99 | int width, int height) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 100 | { |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 101 | if (width == gwidth && height == gheight) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 102 | return; |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 103 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 104 | |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 105 | gwidth = width; |
| 106 | gheight = height; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 107 | |
| 108 | curses_calc_pad(); |
| 109 | } |
| 110 | |
| 111 | #ifndef _WIN32 |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 112 | #if defined(SIGWINCH) && defined(KEY_RESIZE) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 113 | static void curses_winch_handler(int signum) |
| 114 | { |
| 115 | struct winsize { |
| 116 | unsigned short ws_row; |
| 117 | unsigned short ws_col; |
| 118 | unsigned short ws_xpixel; /* unused */ |
| 119 | unsigned short ws_ypixel; /* unused */ |
| 120 | } ws; |
| 121 | |
| 122 | /* terminal size changed */ |
| 123 | if (ioctl(1, TIOCGWINSZ, &ws) == -1) |
| 124 | return; |
| 125 | |
| 126 | resize_term(ws.ws_row, ws.ws_col); |
| 127 | curses_calc_pad(); |
| 128 | invalidate = 1; |
| 129 | |
| 130 | /* some systems require this */ |
| 131 | signal(SIGWINCH, curses_winch_handler); |
| 132 | } |
| 133 | #endif |
| 134 | #endif |
| 135 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 136 | static void curses_cursor_position(DisplayChangeListener *dcl, |
| 137 | DisplayState *ds, |
| 138 | int x, int y) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 139 | { |
| 140 | if (x >= 0) { |
| 141 | x = sminx + x - px; |
| 142 | y = sminy + y - py; |
| 143 | |
| 144 | if (x >= 0 && y >= 0 && x < COLS && y < LINES) { |
| 145 | move(y, x); |
| 146 | curs_set(1); |
| 147 | /* it seems that curs_set(1) must always be called before |
| 148 | * curs_set(2) for the latter to have effect */ |
| 149 | if (!is_graphic_console()) |
| 150 | curs_set(2); |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | curs_set(0); |
| 156 | } |
| 157 | |
| 158 | /* generic keyboard conversion */ |
| 159 | |
| 160 | #include "curses_keys.h" |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 161 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 162 | static kbd_layout_t *kbd_layout = NULL; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 163 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 164 | static void curses_refresh(DisplayChangeListener *dcl, |
| 165 | DisplayState *ds) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 166 | { |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 167 | int chr, nextchr, keysym, keycode, keycode_alt; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 168 | |
| 169 | if (invalidate) { |
| 170 | clear(); |
| 171 | refresh(); |
| 172 | curses_calc_pad(); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 173 | vga_hw_invalidate(); |
| 174 | invalidate = 0; |
| 175 | } |
| 176 | |
| 177 | vga_hw_text_update(screen); |
| 178 | |
| 179 | nextchr = ERR; |
| 180 | while (1) { |
| 181 | /* while there are any pending key strokes to process */ |
| 182 | if (nextchr == ERR) |
| 183 | chr = getch(); |
| 184 | else { |
| 185 | chr = nextchr; |
| 186 | nextchr = ERR; |
| 187 | } |
| 188 | |
| 189 | if (chr == ERR) |
| 190 | break; |
| 191 | |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 192 | #ifdef KEY_RESIZE |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 193 | /* this shouldn't occur when we use a custom SIGWINCH handler */ |
| 194 | if (chr == KEY_RESIZE) { |
| 195 | clear(); |
| 196 | refresh(); |
| 197 | curses_calc_pad(); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 198 | curses_update(dcl, ds, 0, 0, width, height); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 199 | continue; |
| 200 | } |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 201 | #endif |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 202 | |
| 203 | keycode = curses2keycode[chr]; |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 204 | keycode_alt = 0; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 205 | |
| 206 | /* alt key */ |
| 207 | if (keycode == 1) { |
| 208 | nextchr = getch(); |
| 209 | |
| 210 | if (nextchr != ERR) { |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 211 | chr = nextchr; |
| 212 | keycode_alt = ALT; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 213 | keycode = curses2keycode[nextchr]; |
| 214 | nextchr = ERR; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 215 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 216 | if (keycode != -1) { |
| 217 | keycode |= ALT; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 218 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 219 | /* process keys reserved for qemu */ |
| 220 | if (keycode >= QEMU_KEY_CONSOLE0 && |
| 221 | keycode < QEMU_KEY_CONSOLE0 + 9) { |
| 222 | erase(); |
| 223 | wnoutrefresh(stdscr); |
| 224 | console_select(keycode - QEMU_KEY_CONSOLE0); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 225 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 226 | invalidate = 1; |
| 227 | continue; |
| 228 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 233 | if (kbd_layout) { |
| 234 | keysym = -1; |
| 235 | if (chr < CURSES_KEYS) |
| 236 | keysym = curses2keysym[chr]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 237 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 238 | if (keysym == -1) { |
Samuel Thibault | d03703c | 2010-10-19 19:48:20 +0200 | [diff] [blame] | 239 | if (chr < ' ') { |
| 240 | keysym = chr + '@'; |
| 241 | if (keysym >= 'A' && keysym <= 'Z') |
| 242 | keysym += 'a' - 'A'; |
| 243 | keysym |= KEYSYM_CNTRL; |
| 244 | } else |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 245 | keysym = chr; |
| 246 | } |
| 247 | |
| 248 | keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK); |
| 249 | if (keycode == 0) |
| 250 | continue; |
| 251 | |
| 252 | keycode |= (keysym & ~KEYSYM_MASK) >> 16; |
| 253 | keycode |= keycode_alt; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 256 | if (keycode == -1) |
| 257 | continue; |
| 258 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 259 | if (is_graphic_console()) { |
| 260 | /* since terminals don't know about key press and release |
| 261 | * events, we need to emit both for each key received */ |
| 262 | if (keycode & SHIFT) |
| 263 | kbd_put_keycode(SHIFT_CODE); |
| 264 | if (keycode & CNTRL) |
| 265 | kbd_put_keycode(CNTRL_CODE); |
| 266 | if (keycode & ALT) |
| 267 | kbd_put_keycode(ALT_CODE); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 268 | if (keycode & ALTGR) { |
| 269 | kbd_put_keycode(SCANCODE_EMUL0); |
| 270 | kbd_put_keycode(ALT_CODE); |
| 271 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 272 | if (keycode & GREY) |
| 273 | kbd_put_keycode(GREY_CODE); |
| 274 | kbd_put_keycode(keycode & KEY_MASK); |
| 275 | if (keycode & GREY) |
| 276 | kbd_put_keycode(GREY_CODE); |
| 277 | kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE); |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 278 | if (keycode & ALTGR) { |
| 279 | kbd_put_keycode(SCANCODE_EMUL0); |
| 280 | kbd_put_keycode(ALT_CODE | KEY_RELEASE); |
| 281 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 282 | if (keycode & ALT) |
| 283 | kbd_put_keycode(ALT_CODE | KEY_RELEASE); |
| 284 | if (keycode & CNTRL) |
| 285 | kbd_put_keycode(CNTRL_CODE | KEY_RELEASE); |
| 286 | if (keycode & SHIFT) |
| 287 | kbd_put_keycode(SHIFT_CODE | KEY_RELEASE); |
| 288 | } else { |
Samuel Thibault | 44bb61c | 2010-02-28 21:03:00 +0100 | [diff] [blame] | 289 | keysym = curses2qemu[chr]; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 290 | if (keysym == -1) |
| 291 | keysym = chr; |
| 292 | |
| 293 | kbd_put_keysym(keysym); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Blue Swirl | aaf12c2 | 2010-03-21 19:44:06 +0000 | [diff] [blame] | 298 | static void curses_atexit(void) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 299 | { |
| 300 | endwin(); |
| 301 | } |
| 302 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 303 | static void curses_setup(void) |
| 304 | { |
| 305 | int i, colour_default[8] = { |
| 306 | COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN, |
| 307 | COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE, |
| 308 | }; |
| 309 | |
| 310 | /* input as raw as possible, let everything be interpreted |
| 311 | * by the guest system */ |
| 312 | initscr(); noecho(); intrflush(stdscr, FALSE); |
| 313 | nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); |
| 314 | start_color(); raw(); scrollok(stdscr, FALSE); |
| 315 | |
| 316 | for (i = 0; i < 64; i ++) |
| 317 | init_pair(i, colour_default[i & 7], colour_default[i >> 3]); |
| 318 | } |
| 319 | |
| 320 | static void curses_keyboard_setup(void) |
| 321 | { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 322 | #if defined(__APPLE__) |
| 323 | /* always use generic keymaps */ |
| 324 | if (!keyboard_layout) |
| 325 | keyboard_layout = "en-us"; |
| 326 | #endif |
| 327 | if(keyboard_layout) { |
aliguori | 0483755 | 2009-03-06 20:27:10 +0000 | [diff] [blame] | 328 | kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 329 | if (!kbd_layout) |
| 330 | exit(1); |
| 331 | } |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 334 | static const DisplayChangeListenerOps dcl_ops = { |
| 335 | .dpy_name = "curses", |
| 336 | .dpy_text_update = curses_update, |
| 337 | .dpy_text_resize = curses_resize, |
| 338 | .dpy_refresh = curses_refresh, |
| 339 | .dpy_text_cursor = curses_cursor_position, |
| 340 | }; |
| 341 | |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 342 | void curses_display_init(DisplayState *ds, int full_screen) |
| 343 | { |
| 344 | #ifndef _WIN32 |
| 345 | if (!isatty(1)) { |
| 346 | fprintf(stderr, "We need a terminal output\n"); |
| 347 | exit(1); |
| 348 | } |
| 349 | #endif |
| 350 | |
| 351 | curses_setup(); |
| 352 | curses_keyboard_setup(); |
Anthony Liguori | 2869548 | 2010-03-21 14:13:02 -0500 | [diff] [blame] | 353 | atexit(curses_atexit); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 354 | |
| 355 | #ifndef _WIN32 |
balrog | b1314cf | 2008-02-22 18:21:28 +0000 | [diff] [blame] | 356 | #if defined(SIGWINCH) && defined(KEY_RESIZE) |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 357 | /* some curses implementations provide a handler, but we |
| 358 | * want to be sure this is handled regardless of the library */ |
| 359 | signal(SIGWINCH, curses_winch_handler); |
| 360 | #endif |
| 361 | #endif |
| 362 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 363 | dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener)); |
Gerd Hoffmann | 7c20b4a | 2012-11-13 14:51:41 +0100 | [diff] [blame] | 364 | dcl->ops = &dcl_ops; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 365 | register_displaychangelistener(ds, dcl); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 366 | |
| 367 | invalidate = 1; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 368 | } |