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
25 #include "qemu/osdep.h"
28 #include <sys/ioctl.h>
35 #include "qapi/error.h"
36 #include "qemu/module.h"
37 #include "ui/console.h"
39 #include "sysemu/sysemu.h"
41 /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
46 #define FONT_HEIGHT 16
52 CURSES_CHAR_OR_KEYCODE,
55 static DisplayChangeListener *dcl;
56 static console_ch_t *screen;
57 static WINDOW *screenpad = NULL;
58 static int width, height, gwidth, gheight, invalidate;
59 static int px, py, sminx, sminy, smaxx, smaxy;
61 static const char *font_charset = "CP437";
62 static cchar_t *vga_to_curses;
64 static void curses_update(DisplayChangeListener *dcl,
65 int x, int y, int w, int h)
68 cchar_t curses_line[width];
69 wchar_t wch[CCHARW_MAX];
74 line = screen + y * width;
75 for (h += y; y < h; y ++, line += width) {
76 for (x = 0; x < width; x++) {
77 chtype ch = line[x] & A_CHARTEXT;
78 chtype at = line[x] & A_ATTRIBUTES;
79 short color_pair = PAIR_NUMBER(line[x]);
81 ret = getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NULL);
82 if (ret == ERR || wch[0] == 0) {
86 setcchar(&curses_line[x], wch, at, color_pair, NULL);
88 mvwadd_wchnstr(screenpad, y, 0, curses_line, width);
91 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
95 static void curses_calc_pad(void)
97 if (qemu_console_is_fixedsize(NULL)) {
111 screenpad = newpad(height, width);
114 px = (width - COLS) / 2;
119 sminx = (COLS - width) / 2;
120 smaxx = sminx + width;
123 if (height > LINES) {
124 py = (height - LINES) / 2;
129 sminy = (LINES - height) / 2;
130 smaxy = sminy + height;
134 static void curses_resize(DisplayChangeListener *dcl,
135 int width, int height)
137 if (width == gwidth && height == gheight) {
147 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
148 static volatile sig_atomic_t got_sigwinch;
149 static void curses_winch_check(void)
152 unsigned short ws_row;
153 unsigned short ws_col;
154 unsigned short ws_xpixel; /* unused */
155 unsigned short ws_ypixel; /* unused */
161 got_sigwinch = false;
163 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
167 resize_term(ws.ws_row, ws.ws_col);
171 static void curses_winch_handler(int signum)
176 static void curses_winch_init(void)
178 struct sigaction old, winch = {
179 .sa_handler = curses_winch_handler,
181 sigaction(SIGWINCH, &winch, &old);
184 static void curses_winch_check(void) {}
185 static void curses_winch_init(void) {}
188 static void curses_cursor_position(DisplayChangeListener *dcl,
195 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
198 /* it seems that curs_set(1) must always be called before
199 * curs_set(2) for the latter to have effect */
200 if (!qemu_console_is_graphic(NULL)) {
210 /* generic keyboard conversion */
212 #include "curses_keys.h"
214 static kbd_layout_t *kbd_layout = NULL;
216 static wint_t console_getch(enum maybe_keycode *maybe_keycode)
219 switch (get_wch(&ret)) {
221 *maybe_keycode = CURSES_KEYCODE;
224 *maybe_keycode = CURSES_CHAR;
235 static int curses2foo(const int _curses2foo[], const int _curseskey2foo[],
236 int chr, enum maybe_keycode maybe_keycode)
239 if (maybe_keycode == CURSES_CHAR) {
240 if (chr < CURSES_CHARS) {
241 ret = _curses2foo[chr];
244 if (chr < CURSES_KEYS) {
245 ret = _curseskey2foo[chr];
247 if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE &&
248 chr < CURSES_CHARS) {
249 ret = _curses2foo[chr];
255 #define curses2keycode(chr, maybe_keycode) \
256 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
257 #define curses2keysym(chr, maybe_keycode) \
258 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
259 #define curses2qemu(chr, maybe_keycode) \
260 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
262 static void curses_refresh(DisplayChangeListener *dcl)
264 int chr, keysym, keycode, keycode_alt;
265 enum maybe_keycode maybe_keycode = CURSES_KEYCODE;
267 curses_winch_check();
273 graphic_hw_invalidate(NULL);
277 graphic_hw_text_update(NULL, screen);
280 /* while there are any pending key strokes to process */
281 chr = console_getch(&maybe_keycode);
287 /* this shouldn't occur when we use a custom SIGWINCH handler */
288 if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) {
292 curses_update(dcl, 0, 0, width, height);
297 keycode = curses2keycode(chr, maybe_keycode);
302 enum maybe_keycode next_maybe_keycode = CURSES_KEYCODE;
303 int nextchr = console_getch(&next_maybe_keycode);
307 maybe_keycode = next_maybe_keycode;
309 keycode = curses2keycode(chr, maybe_keycode);
314 /* process keys reserved for qemu */
315 if (keycode >= QEMU_KEY_CONSOLE0 &&
316 keycode < QEMU_KEY_CONSOLE0 + 9) {
318 wnoutrefresh(stdscr);
319 console_select(keycode - QEMU_KEY_CONSOLE0);
329 keysym = curses2keysym(chr, maybe_keycode);
334 if (keysym >= 'A' && keysym <= 'Z')
336 keysym |= KEYSYM_CNTRL;
341 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
346 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
347 keycode |= keycode_alt;
353 if (qemu_console_is_graphic(NULL)) {
354 /* since terminals don't know about key press and release
355 * events, we need to emit both for each key received */
356 if (keycode & SHIFT) {
357 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
358 qemu_input_event_send_key_delay(0);
360 if (keycode & CNTRL) {
361 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
362 qemu_input_event_send_key_delay(0);
365 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
366 qemu_input_event_send_key_delay(0);
368 if (keycode & ALTGR) {
369 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
370 qemu_input_event_send_key_delay(0);
373 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
374 qemu_input_event_send_key_delay(0);
375 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
376 qemu_input_event_send_key_delay(0);
378 if (keycode & ALTGR) {
379 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
380 qemu_input_event_send_key_delay(0);
383 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
384 qemu_input_event_send_key_delay(0);
386 if (keycode & CNTRL) {
387 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
388 qemu_input_event_send_key_delay(0);
390 if (keycode & SHIFT) {
391 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
392 qemu_input_event_send_key_delay(0);
395 keysym = curses2qemu(chr, maybe_keycode);
399 kbd_put_keysym(keysym);
404 static void curses_atexit(void)
407 g_free(vga_to_curses);
413 * - fch is the font glyph number
414 * - uch is the unicode value
415 * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
416 * - mbch is the native local-dependent multibyte representation
419 /* Setup wchar glyph for one UCS-2 char */
420 static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv)
422 char mbch[MB_LEN_MAX];
428 puch = (char *) &uch;
429 pmbch = (char *) mbch;
431 smbch = sizeof(mbch);
433 if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) {
434 fprintf(stderr, "Could not convert 0x%04x "
435 "from UCS-2 to a multibyte character: %s\n",
436 uch, strerror(errno));
440 memset(&ps, 0, sizeof(ps));
441 if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
442 fprintf(stderr, "Could not convert 0x%04x "
443 "from a multibyte character to wchar_t: %s\n",
444 uch, strerror(errno));
449 setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
452 /* Setup wchar glyph for one font character */
453 static void convert_font(unsigned char fch, iconv_t conv)
455 char mbch[MB_LEN_MAX];
461 pfch = (char *) &fch;
462 pmbch = (char *) &mbch;
464 smbch = sizeof(mbch);
466 if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) {
467 fprintf(stderr, "Could not convert font glyph 0x%02x "
468 "from %s to a multibyte character: %s\n",
469 fch, font_charset, strerror(errno));
473 memset(&ps, 0, sizeof(ps));
474 if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
475 fprintf(stderr, "Could not convert font glyph 0x%02x "
476 "from a multibyte character to wchar_t: %s\n",
477 fch, strerror(errno));
482 setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
485 /* Convert one wchar to UCS-2 */
486 static uint16_t get_ucs(wchar_t wch, iconv_t conv)
488 char mbch[MB_LEN_MAX];
495 memset(&ps, 0, sizeof(ps));
496 ret = wcrtomb(mbch, wch, &ps);
498 fprintf(stderr, "Could not convert 0x%04lx "
499 "from wchar_t to a multibyte character: %s\n",
500 (unsigned long)wch, strerror(errno));
504 pmbch = (char *) mbch;
505 puch = (char *) &uch;
509 if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) {
510 fprintf(stderr, "Could not convert 0x%04lx "
511 "from a multibyte character to UCS-2 : %s\n",
512 (unsigned long)wch, strerror(errno));
520 * Setup mapping for vga to curses line graphics.
522 static void font_setup(void)
524 iconv_t ucs2_to_nativecharset;
525 iconv_t nativecharset_to_ucs2;
528 g_autofree gchar *local_codeset = g_get_codeset();
531 * Control characters are normally non-printable, but VGA does have
532 * well-known glyphs for them.
534 static const uint16_t control_characters[0x20] = {
569 ucs2_to_nativecharset = iconv_open(local_codeset, "UCS-2");
570 if (ucs2_to_nativecharset == (iconv_t) -1) {
571 fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n",
576 nativecharset_to_ucs2 = iconv_open("UCS-2", local_codeset);
577 if (nativecharset_to_ucs2 == (iconv_t) -1) {
578 iconv_close(ucs2_to_nativecharset);
579 fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n",
584 font_conv = iconv_open(local_codeset, font_charset);
585 if (font_conv == (iconv_t) -1) {
586 iconv_close(ucs2_to_nativecharset);
587 iconv_close(nativecharset_to_ucs2);
588 fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n",
589 font_charset, strerror(errno));
593 /* Control characters */
594 for (i = 0; i <= 0x1F; i++) {
595 convert_ucs(i, control_characters[i], ucs2_to_nativecharset);
598 for (i = 0x20; i <= 0xFF; i++) {
599 convert_font(i, font_conv);
603 convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset);
605 if (strcmp(local_codeset, "UTF-8")) {
606 /* Non-Unicode capable, use termcap equivalents for those available */
607 for (i = 0; i <= 0xFF; i++) {
608 wchar_t wch[CCHARW_MAX];
613 ret = getcchar(&vga_to_curses[i], wch, &attr, &color, NULL);
617 switch (get_ucs(wch[0], nativecharset_to_ucs2)) {
619 vga_to_curses[i] = *WACS_STERLING;
622 vga_to_curses[i] = *WACS_BOARD;
625 vga_to_curses[i] = *WACS_CKBOARD;
628 vga_to_curses[i] = *WACS_VLINE;
631 vga_to_curses[i] = *WACS_RTEE;
634 vga_to_curses[i] = *WACS_URCORNER;
637 vga_to_curses[i] = *WACS_LLCORNER;
640 vga_to_curses[i] = *WACS_BTEE;
643 vga_to_curses[i] = *WACS_TTEE;
646 vga_to_curses[i] = *WACS_LTEE;
649 vga_to_curses[i] = *WACS_HLINE;
652 vga_to_curses[i] = *WACS_PLUS;
655 vga_to_curses[i] = *WACS_LANTERN;
658 vga_to_curses[i] = *WACS_NEQUAL;
661 vga_to_curses[i] = *WACS_LRCORNER;
664 vga_to_curses[i] = *WACS_ULCORNER;
667 vga_to_curses[i] = *WACS_BLOCK;
670 vga_to_curses[i] = *WACS_PI;
673 vga_to_curses[i] = *WACS_PLMINUS;
676 vga_to_curses[i] = *WACS_GEQUAL;
679 vga_to_curses[i] = *WACS_LEQUAL;
682 vga_to_curses[i] = *WACS_DEGREE;
685 vga_to_curses[i] = *WACS_BULLET;
688 vga_to_curses[i] = *WACS_DIAMOND;
691 vga_to_curses[i] = *WACS_RARROW;
694 vga_to_curses[i] = *WACS_LARROW;
697 vga_to_curses[i] = *WACS_UARROW;
700 vga_to_curses[i] = *WACS_DARROW;
703 vga_to_curses[i] = *WACS_S1;
706 vga_to_curses[i] = *WACS_S3;
709 vga_to_curses[i] = *WACS_S7;
712 vga_to_curses[i] = *WACS_S9;
717 iconv_close(ucs2_to_nativecharset);
718 iconv_close(nativecharset_to_ucs2);
719 iconv_close(font_conv);
722 static void curses_setup(void)
724 int i, colour_default[8] = {
725 [QEMU_COLOR_BLACK] = COLOR_BLACK,
726 [QEMU_COLOR_BLUE] = COLOR_BLUE,
727 [QEMU_COLOR_GREEN] = COLOR_GREEN,
728 [QEMU_COLOR_CYAN] = COLOR_CYAN,
729 [QEMU_COLOR_RED] = COLOR_RED,
730 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
731 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
732 [QEMU_COLOR_WHITE] = COLOR_WHITE,
735 /* input as raw as possible, let everything be interpreted
736 * by the guest system */
737 initscr(); noecho(); intrflush(stdscr, FALSE);
738 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
739 start_color(); raw(); scrollok(stdscr, FALSE);
742 /* Make color pair to match color format (3bits bg:3bits fg) */
743 for (i = 0; i < 64; i++) {
744 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
746 /* Set default color for more than 64 for safety. */
747 for (i = 64; i < COLOR_PAIRS; i++) {
748 init_pair(i, COLOR_WHITE, COLOR_BLACK);
754 static void curses_keyboard_setup(void)
756 #if defined(__APPLE__)
757 /* always use generic keymaps */
758 if (!keyboard_layout)
759 keyboard_layout = "en-us";
761 if(keyboard_layout) {
762 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
767 static const DisplayChangeListenerOps dcl_ops = {
768 .dpy_name = "curses",
769 .dpy_text_update = curses_update,
770 .dpy_text_resize = curses_resize,
771 .dpy_refresh = curses_refresh,
772 .dpy_text_cursor = curses_cursor_position,
775 static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
779 fprintf(stderr, "We need a terminal output\n");
784 setlocale(LC_CTYPE, "");
785 if (opts->u.curses.charset) {
786 font_charset = opts->u.curses.charset;
788 screen = g_new0(console_ch_t, 160 * 100);
789 vga_to_curses = g_new0(cchar_t, 256);
791 curses_keyboard_setup();
792 atexit(curses_atexit);
796 dcl = g_new0(DisplayChangeListener, 1);
798 register_displaychangelistener(dcl);
803 static QemuDisplay qemu_display_curses = {
804 .type = DISPLAY_TYPE_CURSES,
805 .init = curses_display_init,
808 static void register_curses(void)
810 qemu_display_register(&qemu_display_curses);
813 type_init(register_curses);