]>
Commit | Line | Data |
---|---|---|
4d3b6f6e AZ |
1 | /* |
2 | * QEMU curses/ncurses display driver | |
3 | * | |
4 | * Copyright (c) 2005 Andrzej Zaborowski <[email protected]> | |
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 | */ | |
e16f4c87 | 24 | #include "qemu/osdep.h" |
4d3b6f6e AZ |
25 | |
26 | #ifndef _WIN32 | |
4d3b6f6e AZ |
27 | #include <sys/ioctl.h> |
28 | #include <termios.h> | |
29 | #endif | |
30 | ||
ab4f931e | 31 | #include "qapi/error.h" |
511d2b14 | 32 | #include "qemu-common.h" |
28ecbaee | 33 | #include "ui/console.h" |
cd100328 | 34 | #include "ui/input.h" |
9c17d615 | 35 | #include "sysemu/sysemu.h" |
511d2b14 | 36 | |
e2f82e92 GH |
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 | ||
4d3b6f6e AZ |
42 | #define FONT_HEIGHT 16 |
43 | #define FONT_WIDTH 8 | |
44 | ||
7c20b4a3 | 45 | static DisplayChangeListener *dcl; |
c227f099 | 46 | static console_ch_t screen[160 * 100]; |
4d3b6f6e AZ |
47 | static WINDOW *screenpad = NULL; |
48 | static int width, height, gwidth, gheight, invalidate; | |
49 | static int px, py, sminx, sminy, smaxx, smaxy; | |
50 | ||
e2f82e92 | 51 | static chtype vga_to_curses[256]; |
e2368dc9 | 52 | |
7c20b4a3 | 53 | static void curses_update(DisplayChangeListener *dcl, |
7c20b4a3 | 54 | int x, int y, int w, int h) |
4d3b6f6e | 55 | { |
e2f82e92 GH |
56 | console_ch_t *line; |
57 | chtype curses_line[width]; | |
4d3b6f6e | 58 | |
e2f82e92 GH |
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 | } | |
4d3b6f6e AZ |
71 | |
72 | pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1); | |
73 | refresh(); | |
74 | } | |
75 | ||
76 | static void curses_calc_pad(void) | |
77 | { | |
81c0d5a6 | 78 | if (qemu_console_is_fixedsize(NULL)) { |
4d3b6f6e AZ |
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 | ||
7c20b4a3 | 115 | static void curses_resize(DisplayChangeListener *dcl, |
7c20b4a3 | 116 | int width, int height) |
4d3b6f6e | 117 | { |
a93a4a22 | 118 | if (width == gwidth && height == gheight) { |
4d3b6f6e | 119 | return; |
a93a4a22 | 120 | } |
4d3b6f6e | 121 | |
a93a4a22 GH |
122 | gwidth = width; |
123 | gheight = height; | |
4d3b6f6e AZ |
124 | |
125 | curses_calc_pad(); | |
126 | } | |
127 | ||
032ac6f8 GH |
128 | #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE) |
129 | static volatile sig_atomic_t got_sigwinch; | |
130 | static void curses_winch_check(void) | |
4d3b6f6e AZ |
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 | ||
032ac6f8 GH |
139 | if (!got_sigwinch) { |
140 | return; | |
141 | } | |
142 | got_sigwinch = false; | |
143 | ||
144 | if (ioctl(1, TIOCGWINSZ, &ws) == -1) { | |
4d3b6f6e | 145 | return; |
032ac6f8 | 146 | } |
4d3b6f6e AZ |
147 | |
148 | resize_term(ws.ws_row, ws.ws_col); | |
4d3b6f6e | 149 | invalidate = 1; |
032ac6f8 | 150 | } |
4d3b6f6e | 151 | |
032ac6f8 GH |
152 | static void curses_winch_handler(int signum) |
153 | { | |
154 | got_sigwinch = true; | |
4d3b6f6e | 155 | } |
032ac6f8 GH |
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) {} | |
4d3b6f6e AZ |
167 | #endif |
168 | ||
7c20b4a3 | 169 | static void curses_cursor_position(DisplayChangeListener *dcl, |
7c20b4a3 | 170 | int x, int y) |
4d3b6f6e AZ |
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 */ | |
81c0d5a6 | 181 | if (!qemu_console_is_graphic(NULL)) { |
4d3b6f6e | 182 | curs_set(2); |
81c0d5a6 | 183 | } |
4d3b6f6e AZ |
184 | return; |
185 | } | |
186 | } | |
187 | ||
188 | curs_set(0); | |
189 | } | |
190 | ||
191 | /* generic keyboard conversion */ | |
192 | ||
193 | #include "curses_keys.h" | |
4d3b6f6e | 194 | |
c227f099 | 195 | static kbd_layout_t *kbd_layout = NULL; |
4d3b6f6e | 196 | |
bc2ed970 | 197 | static void curses_refresh(DisplayChangeListener *dcl) |
4d3b6f6e | 198 | { |
99a9ef44 | 199 | int chr, keysym, keycode, keycode_alt; |
4d3b6f6e | 200 | |
032ac6f8 GH |
201 | curses_winch_check(); |
202 | ||
4d3b6f6e AZ |
203 | if (invalidate) { |
204 | clear(); | |
205 | refresh(); | |
206 | curses_calc_pad(); | |
1dbfa005 | 207 | graphic_hw_invalidate(NULL); |
4d3b6f6e AZ |
208 | invalidate = 0; |
209 | } | |
210 | ||
1dbfa005 | 211 | graphic_hw_text_update(NULL, screen); |
4d3b6f6e | 212 | |
4d3b6f6e AZ |
213 | while (1) { |
214 | /* while there are any pending key strokes to process */ | |
99a9ef44 | 215 | chr = getch(); |
4d3b6f6e AZ |
216 | |
217 | if (chr == ERR) | |
218 | break; | |
219 | ||
b1314cf9 | 220 | #ifdef KEY_RESIZE |
4d3b6f6e AZ |
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(); | |
bc2ed970 | 226 | curses_update(dcl, 0, 0, width, height); |
4d3b6f6e AZ |
227 | continue; |
228 | } | |
b1314cf9 | 229 | #endif |
4d3b6f6e AZ |
230 | |
231 | keycode = curses2keycode[chr]; | |
44bb61c8 | 232 | keycode_alt = 0; |
4d3b6f6e AZ |
233 | |
234 | /* alt key */ | |
235 | if (keycode == 1) { | |
99a9ef44 | 236 | int nextchr = getch(); |
4d3b6f6e AZ |
237 | |
238 | if (nextchr != ERR) { | |
44bb61c8 ST |
239 | chr = nextchr; |
240 | keycode_alt = ALT; | |
99a9ef44 | 241 | keycode = curses2keycode[chr]; |
4d3b6f6e | 242 | |
44bb61c8 ST |
243 | if (keycode != -1) { |
244 | keycode |= ALT; | |
4d3b6f6e | 245 | |
44bb61c8 ST |
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); | |
4d3b6f6e | 252 | |
44bb61c8 ST |
253 | invalidate = 1; |
254 | continue; | |
255 | } | |
4d3b6f6e AZ |
256 | } |
257 | } | |
258 | } | |
259 | ||
44bb61c8 ST |
260 | if (kbd_layout) { |
261 | keysym = -1; | |
262 | if (chr < CURSES_KEYS) | |
263 | keysym = curses2keysym[chr]; | |
264 | ||
265 | if (keysym == -1) { | |
d03703c8 ST |
266 | if (chr < ' ') { |
267 | keysym = chr + '@'; | |
268 | if (keysym >= 'A' && keysym <= 'Z') | |
269 | keysym += 'a' - 'A'; | |
270 | keysym |= KEYSYM_CNTRL; | |
271 | } else | |
44bb61c8 ST |
272 | keysym = chr; |
273 | } | |
4d3b6f6e | 274 | |
abb4f2c9 GH |
275 | keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK, |
276 | false, false, false); | |
44bb61c8 ST |
277 | if (keycode == 0) |
278 | continue; | |
279 | ||
280 | keycode |= (keysym & ~KEYSYM_MASK) >> 16; | |
281 | keycode |= keycode_alt; | |
4d3b6f6e AZ |
282 | } |
283 | ||
44bb61c8 ST |
284 | if (keycode == -1) |
285 | continue; | |
286 | ||
81c0d5a6 | 287 | if (qemu_console_is_graphic(NULL)) { |
4d3b6f6e AZ |
288 | /* since terminals don't know about key press and release |
289 | * events, we need to emit both for each key received */ | |
cd100328 GH |
290 | if (keycode & SHIFT) { |
291 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, true); | |
5a165668 | 292 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
293 | } |
294 | if (keycode & CNTRL) { | |
295 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, true); | |
5a165668 | 296 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
297 | } |
298 | if (keycode & ALT) { | |
299 | qemu_input_event_send_key_number(NULL, ALT_CODE, true); | |
5a165668 | 300 | qemu_input_event_send_key_delay(0); |
cd100328 | 301 | } |
44bb61c8 | 302 | if (keycode & ALTGR) { |
cd100328 | 303 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true); |
5a165668 | 304 | qemu_input_event_send_key_delay(0); |
44bb61c8 | 305 | } |
cd100328 | 306 | |
f5c0ab13 | 307 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true); |
5a165668 | 308 | qemu_input_event_send_key_delay(0); |
f5c0ab13 | 309 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false); |
5a165668 | 310 | qemu_input_event_send_key_delay(0); |
cd100328 | 311 | |
44bb61c8 | 312 | if (keycode & ALTGR) { |
cd100328 | 313 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false); |
5a165668 | 314 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
315 | } |
316 | if (keycode & ALT) { | |
317 | qemu_input_event_send_key_number(NULL, ALT_CODE, false); | |
5a165668 | 318 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
319 | } |
320 | if (keycode & CNTRL) { | |
321 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, false); | |
5a165668 | 322 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
323 | } |
324 | if (keycode & SHIFT) { | |
325 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, false); | |
5a165668 | 326 | qemu_input_event_send_key_delay(0); |
44bb61c8 | 327 | } |
4d3b6f6e | 328 | } else { |
bba4e1b5 PM |
329 | keysym = -1; |
330 | if (chr < CURSES_KEYS) { | |
331 | keysym = curses2qemu[chr]; | |
332 | } | |
4d3b6f6e AZ |
333 | if (keysym == -1) |
334 | keysym = chr; | |
335 | ||
336 | kbd_put_keysym(keysym); | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
aaf12c25 | 341 | static void curses_atexit(void) |
4d3b6f6e AZ |
342 | { |
343 | endwin(); | |
344 | } | |
345 | ||
4d3b6f6e AZ |
346 | static void curses_setup(void) |
347 | { | |
348 | int i, colour_default[8] = { | |
4083733d OH |
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, | |
4d3b6f6e AZ |
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 | ||
4083733d | 365 | /* Make color pair to match color format (3bits bg:3bits fg) */ |
615220dd | 366 | for (i = 0; i < 64; i++) { |
4d3b6f6e | 367 | init_pair(i, colour_default[i & 7], colour_default[i >> 3]); |
615220dd | 368 | } |
4083733d | 369 | /* Set default color for more than 64 for safety. */ |
615220dd OH |
370 | for (i = 64; i < COLOR_PAIRS; i++) { |
371 | init_pair(i, COLOR_WHITE, COLOR_BLACK); | |
372 | } | |
e2368dc9 OH |
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; | |
e2368dc9 OH |
388 | vga_to_curses[0x18] = ACS_UARROW; |
389 | vga_to_curses[0x19] = ACS_DARROW; | |
697783a7 ST |
390 | vga_to_curses[0x1a] = ACS_RARROW; |
391 | vga_to_curses[0x1b] = ACS_LARROW; | |
e2368dc9 OH |
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; | |
4d3b6f6e AZ |
415 | } |
416 | ||
417 | static void curses_keyboard_setup(void) | |
418 | { | |
4d3b6f6e AZ |
419 | #if defined(__APPLE__) |
420 | /* always use generic keymaps */ | |
421 | if (!keyboard_layout) | |
422 | keyboard_layout = "en-us"; | |
423 | #endif | |
424 | if(keyboard_layout) { | |
ab4f931e FL |
425 | kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout, |
426 | &error_fatal); | |
4d3b6f6e | 427 | } |
4d3b6f6e AZ |
428 | } |
429 | ||
7c20b4a3 GH |
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 | ||
b0766612 | 438 | static void curses_display_init(DisplayState *ds, DisplayOptions *opts) |
4d3b6f6e AZ |
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(); | |
28695489 | 449 | atexit(curses_atexit); |
4d3b6f6e | 450 | |
032ac6f8 | 451 | curses_winch_init(); |
4d3b6f6e | 452 | |
fedf0d35 | 453 | dcl = g_new0(DisplayChangeListener, 1); |
7c20b4a3 | 454 | dcl->ops = &dcl_ops; |
5209089f | 455 | register_displaychangelistener(dcl); |
4d3b6f6e AZ |
456 | |
457 | invalidate = 1; | |
4d3b6f6e | 458 | } |
b0766612 GH |
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); |