2 * QEMU curses/ncurses display driver
4 * Copyright (c) 2005 Andrzej Zaborowski <balrog@zabor.org>
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 #if defined(__APPLE__) || defined(__OpenBSD__)
42 #define _XOPEN_SOURCE_EXTENDED 1
45 /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
50 #define FONT_HEIGHT 16
56 CURSES_CHAR_OR_KEYCODE
,
59 static DisplayChangeListener
*dcl
;
60 static console_ch_t
*screen
;
61 static WINDOW
*screenpad
= NULL
;
62 static int width
, height
, gwidth
, gheight
, invalidate
;
63 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
65 static const char *font_charset
= "CP437";
66 static cchar_t
*vga_to_curses
;
68 static void curses_update(DisplayChangeListener
*dcl
,
69 int x
, int y
, int w
, int h
)
72 g_autofree cchar_t
*curses_line
= g_new(cchar_t
, width
);
73 wchar_t wch
[CCHARW_MAX
];
78 line
= screen
+ y
* width
;
79 for (h
+= y
; y
< h
; y
++, line
+= width
) {
80 for (x
= 0; x
< width
; x
++) {
81 chtype ch
= line
[x
] & A_CHARTEXT
;
82 chtype at
= line
[x
] & A_ATTRIBUTES
;
83 short color_pair
= PAIR_NUMBER(line
[x
]);
85 ret
= getcchar(&vga_to_curses
[ch
], wch
, &attrs
, &colors
, NULL
);
86 if (ret
== ERR
|| wch
[0] == 0) {
90 setcchar(&curses_line
[x
], wch
, at
, color_pair
, NULL
);
92 mvwadd_wchnstr(screenpad
, y
, 0, curses_line
, width
);
95 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
99 static void curses_calc_pad(void)
101 if (qemu_console_is_fixedsize(dcl
->con
)) {
115 screenpad
= newpad(height
, width
);
118 px
= (width
- COLS
) / 2;
123 sminx
= (COLS
- width
) / 2;
124 smaxx
= sminx
+ width
;
127 if (height
> LINES
) {
128 py
= (height
- LINES
) / 2;
133 sminy
= (LINES
- height
) / 2;
134 smaxy
= sminy
+ height
;
138 static void curses_resize(DisplayChangeListener
*dcl
,
139 int width
, int height
)
141 if (width
== gwidth
&& height
== gheight
) {
151 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
152 static volatile sig_atomic_t got_sigwinch
;
153 static void curses_winch_check(void)
156 unsigned short ws_row
;
157 unsigned short ws_col
;
158 unsigned short ws_xpixel
; /* unused */
159 unsigned short ws_ypixel
; /* unused */
165 got_sigwinch
= false;
167 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
171 resize_term(ws
.ws_row
, ws
.ws_col
);
175 static void curses_winch_handler(int signum
)
180 static void curses_winch_init(void)
182 struct sigaction old
, winch
= {
183 .sa_handler
= curses_winch_handler
,
185 sigaction(SIGWINCH
, &winch
, &old
);
188 static void curses_winch_check(void) {}
189 static void curses_winch_init(void) {}
192 static void curses_cursor_position(DisplayChangeListener
*dcl
,
199 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
202 /* it seems that curs_set(1) must always be called before
203 * curs_set(2) for the latter to have effect */
204 if (!qemu_console_is_graphic(dcl
->con
)) {
214 /* generic keyboard conversion */
216 #include "curses_keys.h"
218 static kbd_layout_t
*kbd_layout
= NULL
;
220 static wint_t console_getch(enum maybe_keycode
*maybe_keycode
)
223 switch (get_wch(&ret
)) {
225 *maybe_keycode
= CURSES_KEYCODE
;
228 *maybe_keycode
= CURSES_CHAR
;
239 static int curses2foo(const int _curses2foo
[], const int _curseskey2foo
[],
240 int chr
, enum maybe_keycode maybe_keycode
)
243 if (maybe_keycode
== CURSES_CHAR
) {
244 if (chr
< CURSES_CHARS
) {
245 ret
= _curses2foo
[chr
];
248 if (chr
< CURSES_KEYS
) {
249 ret
= _curseskey2foo
[chr
];
251 if (ret
== -1 && maybe_keycode
== CURSES_CHAR_OR_KEYCODE
&&
252 chr
< CURSES_CHARS
) {
253 ret
= _curses2foo
[chr
];
259 #define curses2keycode(chr, maybe_keycode) \
260 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
261 #define curses2keysym(chr, maybe_keycode) \
262 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
263 #define curses2qemu(chr, maybe_keycode) \
264 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
266 static void curses_refresh(DisplayChangeListener
*dcl
)
268 int chr
, keysym
, keycode
, keycode_alt
;
269 enum maybe_keycode maybe_keycode
= CURSES_KEYCODE
;
271 curses_winch_check();
277 graphic_hw_invalidate(dcl
->con
);
281 graphic_hw_text_update(dcl
->con
, screen
);
284 /* while there are any pending key strokes to process */
285 chr
= console_getch(&maybe_keycode
);
291 /* this shouldn't occur when we use a custom SIGWINCH handler */
292 if (maybe_keycode
!= CURSES_CHAR
&& chr
== KEY_RESIZE
) {
296 curses_update(dcl
, 0, 0, width
, height
);
301 keycode
= curses2keycode(chr
, maybe_keycode
);
306 enum maybe_keycode next_maybe_keycode
= CURSES_KEYCODE
;
307 int nextchr
= console_getch(&next_maybe_keycode
);
311 maybe_keycode
= next_maybe_keycode
;
313 keycode
= curses2keycode(chr
, maybe_keycode
);
318 /* process keys reserved for qemu */
319 if (keycode
>= QEMU_KEY_CONSOLE0
&&
320 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
321 QemuConsole
*con
= qemu_console_lookup_by_index(keycode
- QEMU_KEY_CONSOLE0
);
324 wnoutrefresh(stdscr
);
325 unregister_displaychangelistener(dcl
);
327 register_displaychangelistener(dcl
);
338 keysym
= curses2keysym(chr
, maybe_keycode
);
343 if (keysym
>= 'A' && keysym
<= 'Z')
345 keysym
|= KEYSYM_CNTRL
;
350 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
,
355 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
356 keycode
|= keycode_alt
;
362 if (qemu_console_is_graphic(dcl
->con
)) {
363 /* since terminals don't know about key press and release
364 * events, we need to emit both for each key received */
365 if (keycode
& SHIFT
) {
366 qemu_input_event_send_key_number(dcl
->con
, SHIFT_CODE
, true);
367 qemu_input_event_send_key_delay(0);
369 if (keycode
& CNTRL
) {
370 qemu_input_event_send_key_number(dcl
->con
, CNTRL_CODE
, true);
371 qemu_input_event_send_key_delay(0);
374 qemu_input_event_send_key_number(dcl
->con
, ALT_CODE
, true);
375 qemu_input_event_send_key_delay(0);
377 if (keycode
& ALTGR
) {
378 qemu_input_event_send_key_number(dcl
->con
, GREY
| ALT_CODE
, true);
379 qemu_input_event_send_key_delay(0);
382 qemu_input_event_send_key_number(dcl
->con
, keycode
& KEY_MASK
, true);
383 qemu_input_event_send_key_delay(0);
384 qemu_input_event_send_key_number(dcl
->con
, keycode
& KEY_MASK
, false);
385 qemu_input_event_send_key_delay(0);
387 if (keycode
& ALTGR
) {
388 qemu_input_event_send_key_number(dcl
->con
, GREY
| ALT_CODE
, false);
389 qemu_input_event_send_key_delay(0);
392 qemu_input_event_send_key_number(dcl
->con
, ALT_CODE
, false);
393 qemu_input_event_send_key_delay(0);
395 if (keycode
& CNTRL
) {
396 qemu_input_event_send_key_number(dcl
->con
, CNTRL_CODE
, false);
397 qemu_input_event_send_key_delay(0);
399 if (keycode
& SHIFT
) {
400 qemu_input_event_send_key_number(dcl
->con
, SHIFT_CODE
, false);
401 qemu_input_event_send_key_delay(0);
404 keysym
= curses2qemu(chr
, maybe_keycode
);
408 qemu_text_console_put_keysym(QEMU_TEXT_CONSOLE(dcl
->con
), keysym
);
413 static void curses_atexit(void)
416 g_free(vga_to_curses
);
422 * - fch is the font glyph number
423 * - uch is the unicode value
424 * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
425 * - mbch is the native local-dependent multibyte representation
428 /* Setup wchar glyph for one UCS-2 char */
429 static void convert_ucs(unsigned char fch
, uint16_t uch
, iconv_t conv
)
431 char mbch
[MB_LEN_MAX
];
437 puch
= (char *) &uch
;
438 pmbch
= (char *) mbch
;
440 smbch
= sizeof(mbch
);
442 if (iconv(conv
, &puch
, &such
, &pmbch
, &smbch
) == (size_t) -1) {
443 fprintf(stderr
, "Could not convert 0x%04x "
444 "from UCS-2 to a multibyte character: %s\n",
445 uch
, strerror(errno
));
449 memset(&ps
, 0, sizeof(ps
));
450 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
451 fprintf(stderr
, "Could not convert 0x%04x "
452 "from a multibyte character to wchar_t: %s\n",
453 uch
, strerror(errno
));
458 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
461 /* Setup wchar glyph for one font character */
462 static void convert_font(unsigned char fch
, iconv_t conv
)
464 char mbch
[MB_LEN_MAX
];
470 pfch
= (char *) &fch
;
471 pmbch
= (char *) &mbch
;
473 smbch
= sizeof(mbch
);
475 if (iconv(conv
, &pfch
, &sfch
, &pmbch
, &smbch
) == (size_t) -1) {
476 fprintf(stderr
, "Could not convert font glyph 0x%02x "
477 "from %s to a multibyte character: %s\n",
478 fch
, font_charset
, strerror(errno
));
482 memset(&ps
, 0, sizeof(ps
));
483 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
484 fprintf(stderr
, "Could not convert font glyph 0x%02x "
485 "from a multibyte character to wchar_t: %s\n",
486 fch
, strerror(errno
));
491 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
494 /* Convert one wchar to UCS-2 */
495 static uint16_t get_ucs(wchar_t wch
, iconv_t conv
)
497 char mbch
[MB_LEN_MAX
];
504 memset(&ps
, 0, sizeof(ps
));
505 ret
= wcrtomb(mbch
, wch
, &ps
);
507 fprintf(stderr
, "Could not convert 0x%04lx "
508 "from wchar_t to a multibyte character: %s\n",
509 (unsigned long)wch
, strerror(errno
));
513 pmbch
= (char *) mbch
;
514 puch
= (char *) &uch
;
518 if (iconv(conv
, &pmbch
, &smbch
, &puch
, &such
) == (size_t) -1) {
519 fprintf(stderr
, "Could not convert 0x%04lx "
520 "from a multibyte character to UCS-2 : %s\n",
521 (unsigned long)wch
, strerror(errno
));
529 * Setup mapping for vga to curses line graphics.
531 static void font_setup(void)
533 iconv_t ucs2_to_nativecharset
;
534 iconv_t nativecharset_to_ucs2
;
537 g_autofree gchar
*local_codeset
= g_get_codeset();
540 * Control characters are normally non-printable, but VGA does have
541 * well-known glyphs for them.
543 static const uint16_t control_characters
[0x20] = {
578 ucs2_to_nativecharset
= iconv_open(local_codeset
, "UCS-2");
579 if (ucs2_to_nativecharset
== (iconv_t
) -1) {
580 fprintf(stderr
, "Could not convert font glyphs from UCS-2: '%s'\n",
585 nativecharset_to_ucs2
= iconv_open("UCS-2", local_codeset
);
586 if (nativecharset_to_ucs2
== (iconv_t
) -1) {
587 iconv_close(ucs2_to_nativecharset
);
588 fprintf(stderr
, "Could not convert font glyphs to UCS-2: '%s'\n",
593 font_conv
= iconv_open(local_codeset
, font_charset
);
594 if (font_conv
== (iconv_t
) -1) {
595 iconv_close(ucs2_to_nativecharset
);
596 iconv_close(nativecharset_to_ucs2
);
597 fprintf(stderr
, "Could not convert font glyphs from %s: '%s'\n",
598 font_charset
, strerror(errno
));
602 /* Control characters */
603 for (i
= 0; i
<= 0x1F; i
++) {
604 convert_ucs(i
, control_characters
[i
], ucs2_to_nativecharset
);
607 for (i
= 0x20; i
<= 0xFF; i
++) {
608 convert_font(i
, font_conv
);
612 convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset
);
614 if (strcmp(local_codeset
, "UTF-8")) {
615 /* Non-Unicode capable, use termcap equivalents for those available */
616 for (i
= 0; i
<= 0xFF; i
++) {
617 wchar_t wch
[CCHARW_MAX
];
622 ret
= getcchar(&vga_to_curses
[i
], wch
, &attr
, &color
, NULL
);
626 switch (get_ucs(wch
[0], nativecharset_to_ucs2
)) {
628 vga_to_curses
[i
] = *WACS_STERLING
;
631 vga_to_curses
[i
] = *WACS_BOARD
;
634 vga_to_curses
[i
] = *WACS_CKBOARD
;
637 vga_to_curses
[i
] = *WACS_VLINE
;
640 vga_to_curses
[i
] = *WACS_RTEE
;
643 vga_to_curses
[i
] = *WACS_URCORNER
;
646 vga_to_curses
[i
] = *WACS_LLCORNER
;
649 vga_to_curses
[i
] = *WACS_BTEE
;
652 vga_to_curses
[i
] = *WACS_TTEE
;
655 vga_to_curses
[i
] = *WACS_LTEE
;
658 vga_to_curses
[i
] = *WACS_HLINE
;
661 vga_to_curses
[i
] = *WACS_PLUS
;
664 vga_to_curses
[i
] = *WACS_LANTERN
;
667 vga_to_curses
[i
] = *WACS_NEQUAL
;
670 vga_to_curses
[i
] = *WACS_LRCORNER
;
673 vga_to_curses
[i
] = *WACS_ULCORNER
;
676 vga_to_curses
[i
] = *WACS_BLOCK
;
679 vga_to_curses
[i
] = *WACS_PI
;
682 vga_to_curses
[i
] = *WACS_PLMINUS
;
685 vga_to_curses
[i
] = *WACS_GEQUAL
;
688 vga_to_curses
[i
] = *WACS_LEQUAL
;
691 vga_to_curses
[i
] = *WACS_DEGREE
;
694 vga_to_curses
[i
] = *WACS_BULLET
;
697 vga_to_curses
[i
] = *WACS_DIAMOND
;
700 vga_to_curses
[i
] = *WACS_RARROW
;
703 vga_to_curses
[i
] = *WACS_LARROW
;
706 vga_to_curses
[i
] = *WACS_UARROW
;
709 vga_to_curses
[i
] = *WACS_DARROW
;
712 vga_to_curses
[i
] = *WACS_S1
;
715 vga_to_curses
[i
] = *WACS_S3
;
718 vga_to_curses
[i
] = *WACS_S7
;
721 vga_to_curses
[i
] = *WACS_S9
;
726 iconv_close(ucs2_to_nativecharset
);
727 iconv_close(nativecharset_to_ucs2
);
728 iconv_close(font_conv
);
731 static void curses_setup(void)
733 int i
, colour_default
[8] = {
734 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
735 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
736 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
737 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
738 [QEMU_COLOR_RED
] = COLOR_RED
,
739 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
740 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
741 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
744 /* input as raw as possible, let everything be interpreted
745 * by the guest system */
746 initscr(); noecho(); intrflush(stdscr
, FALSE
);
747 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
748 start_color(); raw(); scrollok(stdscr
, FALSE
);
751 /* Make color pair to match color format (3bits bg:3bits fg) */
752 for (i
= 0; i
< 64; i
++) {
753 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
755 /* Set default color for more than 64 for safety. */
756 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
757 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
763 static void curses_keyboard_setup(void)
765 #if defined(__APPLE__)
766 /* always use generic keymaps */
767 if (!keyboard_layout
)
768 keyboard_layout
= "en-us";
770 if(keyboard_layout
) {
771 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
,
776 static const DisplayChangeListenerOps dcl_ops
= {
777 .dpy_name
= "curses",
778 .dpy_text_update
= curses_update
,
779 .dpy_text_resize
= curses_resize
,
780 .dpy_refresh
= curses_refresh
,
781 .dpy_text_cursor
= curses_cursor_position
,
784 static void curses_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
788 fprintf(stderr
, "We need a terminal output\n");
793 setlocale(LC_CTYPE
, "");
794 if (opts
->u
.curses
.charset
) {
795 font_charset
= opts
->u
.curses
.charset
;
797 screen
= g_new0(console_ch_t
, 160 * 100);
798 vga_to_curses
= g_new0(cchar_t
, 256);
800 curses_keyboard_setup();
801 atexit(curses_atexit
);
805 dcl
= g_new0(DisplayChangeListener
, 1);
806 dcl
->con
= qemu_console_lookup_default();
808 register_displaychangelistener(dcl
);
813 static QemuDisplay qemu_display_curses
= {
814 .type
= DISPLAY_TYPE_CURSES
,
815 .init
= curses_display_init
,
818 static void register_curses(void)
820 qemu_display_register(&qemu_display_curses
);
823 type_init(register_curses
);