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