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>
36 #include "qapi/error.h"
37 #include "qemu/module.h"
38 #include "ui/console.h"
40 #include "sysemu/sysemu.h"
42 /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
47 #define FONT_HEIGHT 16
53 CURSES_CHAR_OR_KEYCODE,
56 static DisplayChangeListener *dcl;
57 static console_ch_t screen[160 * 100];
58 static WINDOW *screenpad = NULL;
59 static int width, height, gwidth, gheight, invalidate;
60 static int px, py, sminx, sminy, smaxx, smaxy;
62 static const char *font_charset = "CP437";
63 static cchar_t vga_to_curses[256];
65 static void curses_update(DisplayChangeListener *dcl,
66 int x, int y, int w, int h)
69 cchar_t curses_line[width];
70 wchar_t wch[CCHARW_MAX];
75 line = screen + y * width;
76 for (h += y; y < h; y ++, line += width) {
77 for (x = 0; x < width; x++) {
78 chtype ch = line[x] & A_CHARTEXT;
79 chtype at = line[x] & A_ATTRIBUTES;
80 short color_pair = PAIR_NUMBER(line[x]);
82 ret = getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NULL);
83 if (ret == ERR || wch[0] == 0) {
87 setcchar(&curses_line[x], wch, at, color_pair, NULL);
89 mvwadd_wchnstr(screenpad, y, 0, curses_line, width);
92 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
96 static void curses_calc_pad(void)
98 if (qemu_console_is_fixedsize(NULL)) {
112 screenpad = newpad(height, width);
115 px = (width - COLS) / 2;
120 sminx = (COLS - width) / 2;
121 smaxx = sminx + width;
124 if (height > LINES) {
125 py = (height - LINES) / 2;
130 sminy = (LINES - height) / 2;
131 smaxy = sminy + height;
135 static void curses_resize(DisplayChangeListener *dcl,
136 int width, int height)
138 if (width == gwidth && height == gheight) {
148 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
149 static volatile sig_atomic_t got_sigwinch;
150 static void curses_winch_check(void)
153 unsigned short ws_row;
154 unsigned short ws_col;
155 unsigned short ws_xpixel; /* unused */
156 unsigned short ws_ypixel; /* unused */
162 got_sigwinch = false;
164 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
168 resize_term(ws.ws_row, ws.ws_col);
172 static void curses_winch_handler(int signum)
177 static void curses_winch_init(void)
179 struct sigaction old, winch = {
180 .sa_handler = curses_winch_handler,
182 sigaction(SIGWINCH, &winch, &old);
185 static void curses_winch_check(void) {}
186 static void curses_winch_init(void) {}
189 static void curses_cursor_position(DisplayChangeListener *dcl,
196 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
199 /* it seems that curs_set(1) must always be called before
200 * curs_set(2) for the latter to have effect */
201 if (!qemu_console_is_graphic(NULL)) {
211 /* generic keyboard conversion */
213 #include "curses_keys.h"
215 static kbd_layout_t *kbd_layout = NULL;
217 static wint_t console_getch(enum maybe_keycode *maybe_keycode)
220 switch (get_wch(&ret)) {
222 *maybe_keycode = CURSES_KEYCODE;
225 *maybe_keycode = CURSES_CHAR;
236 static int curses2foo(const int _curses2foo[], const int _curseskey2foo[],
237 int chr, enum maybe_keycode maybe_keycode)
240 if (maybe_keycode == CURSES_CHAR) {
241 if (chr < CURSES_CHARS) {
242 ret = _curses2foo[chr];
245 if (chr < CURSES_KEYS) {
246 ret = _curseskey2foo[chr];
248 if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE &&
249 chr < CURSES_CHARS) {
250 ret = _curses2foo[chr];
256 #define curses2keycode(chr, maybe_keycode) \
257 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
258 #define curses2keysym(chr, maybe_keycode) \
259 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
260 #define curses2qemu(chr, maybe_keycode) \
261 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
263 static void curses_refresh(DisplayChangeListener *dcl)
265 int chr, keysym, keycode, keycode_alt;
266 enum maybe_keycode maybe_keycode;
268 curses_winch_check();
274 graphic_hw_invalidate(NULL);
278 graphic_hw_text_update(NULL, screen);
281 /* while there are any pending key strokes to process */
282 chr = console_getch(&maybe_keycode);
288 /* this shouldn't occur when we use a custom SIGWINCH handler */
289 if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) {
293 curses_update(dcl, 0, 0, width, height);
298 keycode = curses2keycode(chr, maybe_keycode);
303 enum maybe_keycode next_maybe_keycode;
304 int nextchr = console_getch(&next_maybe_keycode);
308 maybe_keycode = next_maybe_keycode;
310 keycode = curses2keycode(chr, maybe_keycode);
315 /* process keys reserved for qemu */
316 if (keycode >= QEMU_KEY_CONSOLE0 &&
317 keycode < QEMU_KEY_CONSOLE0 + 9) {
319 wnoutrefresh(stdscr);
320 console_select(keycode - QEMU_KEY_CONSOLE0);
330 keysym = curses2keysym(chr, maybe_keycode);
335 if (keysym >= 'A' && keysym <= 'Z')
337 keysym |= KEYSYM_CNTRL;
342 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
347 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
348 keycode |= keycode_alt;
354 if (qemu_console_is_graphic(NULL)) {
355 /* since terminals don't know about key press and release
356 * events, we need to emit both for each key received */
357 if (keycode & SHIFT) {
358 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
359 qemu_input_event_send_key_delay(0);
361 if (keycode & CNTRL) {
362 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
363 qemu_input_event_send_key_delay(0);
366 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
367 qemu_input_event_send_key_delay(0);
369 if (keycode & ALTGR) {
370 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
371 qemu_input_event_send_key_delay(0);
374 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
375 qemu_input_event_send_key_delay(0);
376 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
377 qemu_input_event_send_key_delay(0);
379 if (keycode & ALTGR) {
380 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
381 qemu_input_event_send_key_delay(0);
384 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
385 qemu_input_event_send_key_delay(0);
387 if (keycode & CNTRL) {
388 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
389 qemu_input_event_send_key_delay(0);
391 if (keycode & SHIFT) {
392 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
393 qemu_input_event_send_key_delay(0);
396 keysym = curses2qemu(chr, maybe_keycode);
400 kbd_put_keysym(keysym);
405 static void curses_atexit(void)
412 * - fch is the font glyph number
413 * - uch is the unicode value
414 * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
415 * - mbch is the native local-dependent multibyte representation
418 /* Setup wchar glyph for one UCS-2 char */
419 static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv)
421 char mbch[MB_LEN_MAX];
427 puch = (char *) &uch;
428 pmbch = (char *) mbch;
430 smbch = sizeof(mbch);
432 if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) {
433 fprintf(stderr, "Could not convert 0x%04x "
434 "from UCS-2 to a multibyte character: %s\n",
435 uch, strerror(errno));
439 memset(&ps, 0, sizeof(ps));
440 if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
441 fprintf(stderr, "Could not convert 0x%04x "
442 "from a multibyte character to wchar_t: %s\n",
443 uch, strerror(errno));
448 setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
451 /* Setup wchar glyph for one font character */
452 static void convert_font(unsigned char fch, iconv_t conv)
454 char mbch[MB_LEN_MAX];
460 pfch = (char *) &fch;
461 pmbch = (char *) &mbch;
463 smbch = sizeof(mbch);
465 if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) {
466 fprintf(stderr, "Could not convert font glyph 0x%02x "
467 "from %s to a multibyte character: %s\n",
468 fch, font_charset, strerror(errno));
472 memset(&ps, 0, sizeof(ps));
473 if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
474 fprintf(stderr, "Could not convert font glyph 0x%02x "
475 "from a multibyte character to wchar_t: %s\n",
476 fch, strerror(errno));
481 setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
484 /* Convert one wchar to UCS-2 */
485 static uint16_t get_ucs(wchar_t wch, iconv_t conv)
487 char mbch[MB_LEN_MAX];
494 memset(&ps, 0, sizeof(ps));
495 ret = wcrtomb(mbch, wch, &ps);
497 fprintf(stderr, "Could not convert 0x%04lx "
498 "from wchar_t to a multibyte character: %s\n",
499 (unsigned long)wch, strerror(errno));
503 pmbch = (char *) mbch;
504 puch = (char *) &uch;
508 if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) {
509 fprintf(stderr, "Could not convert 0x%04lx "
510 "from a multibyte character to UCS-2 : %s\n",
511 (unsigned long)wch, strerror(errno));
519 * Setup mapping for vga to curses line graphics.
521 static void font_setup(void)
523 iconv_t ucs2_to_nativecharset;
524 iconv_t nativecharset_to_ucs2;
529 * Control characters are normally non-printable, but VGA does have
530 * well-known glyphs for them.
532 static uint16_t control_characters[0x20] = {
567 ucs2_to_nativecharset = iconv_open(nl_langinfo(CODESET), "UCS-2");
568 if (ucs2_to_nativecharset == (iconv_t) -1) {
569 fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n",
574 nativecharset_to_ucs2 = iconv_open("UCS-2", nl_langinfo(CODESET));
575 if (nativecharset_to_ucs2 == (iconv_t) -1) {
576 iconv_close(ucs2_to_nativecharset);
577 fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n",
582 font_conv = iconv_open(nl_langinfo(CODESET), font_charset);
583 if (font_conv == (iconv_t) -1) {
584 iconv_close(ucs2_to_nativecharset);
585 iconv_close(nativecharset_to_ucs2);
586 fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n",
587 font_charset, strerror(errno));
591 /* Control characters */
592 for (i = 0; i <= 0x1F; i++) {
593 convert_ucs(i, control_characters[i], ucs2_to_nativecharset);
596 for (i = 0x20; i <= 0xFF; i++) {
597 convert_font(i, font_conv);
601 convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset);
603 if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
604 /* Non-Unicode capable, use termcap equivalents for those available */
605 for (i = 0; i <= 0xFF; i++) {
606 wchar_t wch[CCHARW_MAX];
611 ret = getcchar(&vga_to_curses[i], wch, &attr, &color, NULL);
615 switch (get_ucs(wch[0], nativecharset_to_ucs2)) {
617 vga_to_curses[i] = *WACS_STERLING;
620 vga_to_curses[i] = *WACS_BOARD;
623 vga_to_curses[i] = *WACS_CKBOARD;
626 vga_to_curses[i] = *WACS_VLINE;
629 vga_to_curses[i] = *WACS_RTEE;
632 vga_to_curses[i] = *WACS_URCORNER;
635 vga_to_curses[i] = *WACS_LLCORNER;
638 vga_to_curses[i] = *WACS_BTEE;
641 vga_to_curses[i] = *WACS_TTEE;
644 vga_to_curses[i] = *WACS_LTEE;
647 vga_to_curses[i] = *WACS_HLINE;
650 vga_to_curses[i] = *WACS_PLUS;
653 vga_to_curses[i] = *WACS_LANTERN;
656 vga_to_curses[i] = *WACS_NEQUAL;
659 vga_to_curses[i] = *WACS_LRCORNER;
662 vga_to_curses[i] = *WACS_ULCORNER;
665 vga_to_curses[i] = *WACS_BLOCK;
668 vga_to_curses[i] = *WACS_PI;
671 vga_to_curses[i] = *WACS_PLMINUS;
674 vga_to_curses[i] = *WACS_GEQUAL;
677 vga_to_curses[i] = *WACS_LEQUAL;
680 vga_to_curses[i] = *WACS_DEGREE;
683 vga_to_curses[i] = *WACS_BULLET;
686 vga_to_curses[i] = *WACS_DIAMOND;
689 vga_to_curses[i] = *WACS_RARROW;
692 vga_to_curses[i] = *WACS_LARROW;
695 vga_to_curses[i] = *WACS_UARROW;
698 vga_to_curses[i] = *WACS_DARROW;
701 vga_to_curses[i] = *WACS_S1;
704 vga_to_curses[i] = *WACS_S3;
707 vga_to_curses[i] = *WACS_S7;
710 vga_to_curses[i] = *WACS_S9;
715 iconv_close(ucs2_to_nativecharset);
716 iconv_close(nativecharset_to_ucs2);
717 iconv_close(font_conv);
720 static void curses_setup(void)
722 int i, colour_default[8] = {
723 [QEMU_COLOR_BLACK] = COLOR_BLACK,
724 [QEMU_COLOR_BLUE] = COLOR_BLUE,
725 [QEMU_COLOR_GREEN] = COLOR_GREEN,
726 [QEMU_COLOR_CYAN] = COLOR_CYAN,
727 [QEMU_COLOR_RED] = COLOR_RED,
728 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
729 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
730 [QEMU_COLOR_WHITE] = COLOR_WHITE,
733 /* input as raw as possible, let everything be interpreted
734 * by the guest system */
735 initscr(); noecho(); intrflush(stdscr, FALSE);
736 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
737 start_color(); raw(); scrollok(stdscr, FALSE);
740 /* Make color pair to match color format (3bits bg:3bits fg) */
741 for (i = 0; i < 64; i++) {
742 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
744 /* Set default color for more than 64 for safety. */
745 for (i = 64; i < COLOR_PAIRS; i++) {
746 init_pair(i, COLOR_WHITE, COLOR_BLACK);
752 static void curses_keyboard_setup(void)
754 #if defined(__APPLE__)
755 /* always use generic keymaps */
756 if (!keyboard_layout)
757 keyboard_layout = "en-us";
759 if(keyboard_layout) {
760 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
765 static const DisplayChangeListenerOps dcl_ops = {
766 .dpy_name = "curses",
767 .dpy_text_update = curses_update,
768 .dpy_text_resize = curses_resize,
769 .dpy_refresh = curses_refresh,
770 .dpy_text_cursor = curses_cursor_position,
773 static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
777 fprintf(stderr, "We need a terminal output\n");
782 setlocale(LC_CTYPE, "");
783 if (opts->u.curses.charset) {
784 font_charset = opts->u.curses.charset;
787 curses_keyboard_setup();
788 atexit(curses_atexit);
792 dcl = g_new0(DisplayChangeListener, 1);
794 register_displaychangelistener(dcl);
799 static QemuDisplay qemu_display_curses = {
800 .type = DISPLAY_TYPE_CURSES,
801 .init = curses_display_init,
804 static void register_curses(void)
806 qemu_display_register(&qemu_display_curses);
809 type_init(register_curses);