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