2 * QEMU curses/ncurses display driver
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
27 #include <sys/ioctl.h>
31 #include "qapi/error.h"
32 #include "qemu-common.h"
33 #include "ui/console.h"
35 #include "sysemu/sysemu.h"
37 /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
42 #define FONT_HEIGHT 16
45 static DisplayChangeListener *dcl;
46 static console_ch_t screen[160 * 100];
47 static WINDOW *screenpad = NULL;
48 static int width, height, gwidth, gheight, invalidate;
49 static int px, py, sminx, sminy, smaxx, smaxy;
51 static chtype vga_to_curses[256];
53 static void curses_update(DisplayChangeListener *dcl,
54 int x, int y, int w, int h)
57 chtype curses_line[width];
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];
67 curses_line[x] = ch | at;
69 mvwaddchnstr(screenpad, y, 0, curses_line, width);
72 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
76 static void curses_calc_pad(void)
78 if (qemu_console_is_fixedsize(NULL)) {
92 screenpad = newpad(height, width);
95 px = (width - COLS) / 2;
100 sminx = (COLS - width) / 2;
101 smaxx = sminx + width;
104 if (height > LINES) {
105 py = (height - LINES) / 2;
110 sminy = (LINES - height) / 2;
111 smaxy = sminy + height;
115 static void curses_resize(DisplayChangeListener *dcl,
116 int width, int height)
118 if (width == gwidth && height == gheight) {
128 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
129 static volatile sig_atomic_t got_sigwinch;
130 static void curses_winch_check(void)
133 unsigned short ws_row;
134 unsigned short ws_col;
135 unsigned short ws_xpixel; /* unused */
136 unsigned short ws_ypixel; /* unused */
142 got_sigwinch = false;
144 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
148 resize_term(ws.ws_row, ws.ws_col);
152 static void curses_winch_handler(int signum)
157 static void curses_winch_init(void)
159 struct sigaction old, winch = {
160 .sa_handler = curses_winch_handler,
162 sigaction(SIGWINCH, &winch, &old);
165 static void curses_winch_check(void) {}
166 static void curses_winch_init(void) {}
169 static void curses_cursor_position(DisplayChangeListener *dcl,
176 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
179 /* it seems that curs_set(1) must always be called before
180 * curs_set(2) for the latter to have effect */
181 if (!qemu_console_is_graphic(NULL)) {
191 /* generic keyboard conversion */
193 #include "curses_keys.h"
195 static kbd_layout_t *kbd_layout = NULL;
197 static void curses_refresh(DisplayChangeListener *dcl)
199 int chr, keysym, keycode, keycode_alt;
201 curses_winch_check();
207 graphic_hw_invalidate(NULL);
211 graphic_hw_text_update(NULL, screen);
214 /* while there are any pending key strokes to process */
221 /* this shouldn't occur when we use a custom SIGWINCH handler */
222 if (chr == KEY_RESIZE) {
226 curses_update(dcl, 0, 0, width, height);
231 keycode = curses2keycode[chr];
236 int nextchr = getch();
238 if (nextchr != ERR) {
241 keycode = curses2keycode[chr];
246 /* process keys reserved for qemu */
247 if (keycode >= QEMU_KEY_CONSOLE0 &&
248 keycode < QEMU_KEY_CONSOLE0 + 9) {
250 wnoutrefresh(stdscr);
251 console_select(keycode - QEMU_KEY_CONSOLE0);
262 if (chr < CURSES_KEYS)
263 keysym = curses2keysym[chr];
268 if (keysym >= 'A' && keysym <= 'Z')
270 keysym |= KEYSYM_CNTRL;
275 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
280 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
281 keycode |= keycode_alt;
287 if (qemu_console_is_graphic(NULL)) {
288 /* since terminals don't know about key press and release
289 * events, we need to emit both for each key received */
290 if (keycode & SHIFT) {
291 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
292 qemu_input_event_send_key_delay(0);
294 if (keycode & CNTRL) {
295 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
296 qemu_input_event_send_key_delay(0);
299 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
300 qemu_input_event_send_key_delay(0);
302 if (keycode & ALTGR) {
303 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
304 qemu_input_event_send_key_delay(0);
307 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
308 qemu_input_event_send_key_delay(0);
309 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
310 qemu_input_event_send_key_delay(0);
312 if (keycode & ALTGR) {
313 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
314 qemu_input_event_send_key_delay(0);
317 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
318 qemu_input_event_send_key_delay(0);
320 if (keycode & CNTRL) {
321 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
322 qemu_input_event_send_key_delay(0);
324 if (keycode & SHIFT) {
325 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
326 qemu_input_event_send_key_delay(0);
330 if (chr < CURSES_KEYS) {
331 keysym = curses2qemu[chr];
336 kbd_put_keysym(keysym);
341 static void curses_atexit(void)
346 static void curses_setup(void)
348 int i, colour_default[8] = {
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,
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);
366 /* Make color pair to match color format (3bits bg:3bits fg) */
367 for (i = 0; i < 64; i++) {
368 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
370 /* Set default color for more than 64 for safety. */
371 for (i = 64; i < COLOR_PAIRS; i++) {
372 init_pair(i, COLOR_WHITE, COLOR_BLACK);
376 * Setup mapping for vga to curses line graphics.
377 * FIXME: for better font, have to use ncursesw and setlocale()
380 /* FIXME: map from where? */
386 /* ACS_* is not constant. So, we can't initialize statically. */
387 vga_to_curses['\0'] = ' ';
388 vga_to_curses[0x04] = ACS_DIAMOND;
389 vga_to_curses[0x18] = ACS_UARROW;
390 vga_to_curses[0x19] = ACS_DARROW;
391 vga_to_curses[0x1a] = ACS_RARROW;
392 vga_to_curses[0x1b] = ACS_LARROW;
393 vga_to_curses[0x9c] = ACS_STERLING;
394 vga_to_curses[0xb0] = ACS_BOARD;
395 vga_to_curses[0xb1] = ACS_CKBOARD;
396 vga_to_curses[0xb3] = ACS_VLINE;
397 vga_to_curses[0xb4] = ACS_RTEE;
398 vga_to_curses[0xbf] = ACS_URCORNER;
399 vga_to_curses[0xc0] = ACS_LLCORNER;
400 vga_to_curses[0xc1] = ACS_BTEE;
401 vga_to_curses[0xc2] = ACS_TTEE;
402 vga_to_curses[0xc3] = ACS_LTEE;
403 vga_to_curses[0xc4] = ACS_HLINE;
404 vga_to_curses[0xc5] = ACS_PLUS;
405 vga_to_curses[0xce] = ACS_LANTERN;
406 vga_to_curses[0xd8] = ACS_NEQUAL;
407 vga_to_curses[0xd9] = ACS_LRCORNER;
408 vga_to_curses[0xda] = ACS_ULCORNER;
409 vga_to_curses[0xdb] = ACS_BLOCK;
410 vga_to_curses[0xe3] = ACS_PI;
411 vga_to_curses[0xf1] = ACS_PLMINUS;
412 vga_to_curses[0xf2] = ACS_GEQUAL;
413 vga_to_curses[0xf3] = ACS_LEQUAL;
414 vga_to_curses[0xf8] = ACS_DEGREE;
415 vga_to_curses[0xfe] = ACS_BULLET;
418 static void curses_keyboard_setup(void)
420 #if defined(__APPLE__)
421 /* always use generic keymaps */
422 if (!keyboard_layout)
423 keyboard_layout = "en-us";
425 if(keyboard_layout) {
426 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
431 static const DisplayChangeListenerOps dcl_ops = {
432 .dpy_name = "curses",
433 .dpy_text_update = curses_update,
434 .dpy_text_resize = curses_resize,
435 .dpy_refresh = curses_refresh,
436 .dpy_text_cursor = curses_cursor_position,
439 static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
443 fprintf(stderr, "We need a terminal output\n");
449 curses_keyboard_setup();
450 atexit(curses_atexit);
454 dcl = g_new0(DisplayChangeListener, 1);
456 register_displaychangelistener(dcl);
461 static QemuDisplay qemu_display_curses = {
462 .type = DISPLAY_TYPE_CURSES,
463 .init = curses_display_init,
466 static void register_curses(void)
468 qemu_display_register(&qemu_display_curses);
471 type_init(register_curses);