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(NULL
)) {
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(NULL
)) {
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(NULL
);
281 graphic_hw_text_update(NULL
, 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) {
322 wnoutrefresh(stdscr
);
323 console_select(keycode
- QEMU_KEY_CONSOLE0
);
333 keysym
= curses2keysym(chr
, maybe_keycode
);
338 if (keysym
>= 'A' && keysym
<= 'Z')
340 keysym
|= KEYSYM_CNTRL
;
345 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
,
350 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
351 keycode
|= keycode_alt
;
357 if (qemu_console_is_graphic(NULL
)) {
358 /* since terminals don't know about key press and release
359 * events, we need to emit both for each key received */
360 if (keycode
& SHIFT
) {
361 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
362 qemu_input_event_send_key_delay(0);
364 if (keycode
& CNTRL
) {
365 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
366 qemu_input_event_send_key_delay(0);
369 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
370 qemu_input_event_send_key_delay(0);
372 if (keycode
& ALTGR
) {
373 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
374 qemu_input_event_send_key_delay(0);
377 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
378 qemu_input_event_send_key_delay(0);
379 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
380 qemu_input_event_send_key_delay(0);
382 if (keycode
& ALTGR
) {
383 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
384 qemu_input_event_send_key_delay(0);
387 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
388 qemu_input_event_send_key_delay(0);
390 if (keycode
& CNTRL
) {
391 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
392 qemu_input_event_send_key_delay(0);
394 if (keycode
& SHIFT
) {
395 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
396 qemu_input_event_send_key_delay(0);
399 keysym
= curses2qemu(chr
, maybe_keycode
);
403 qemu_text_console_put_keysym(NULL
, keysym
);
408 static void curses_atexit(void)
411 g_free(vga_to_curses
);
417 * - fch is the font glyph number
418 * - uch is the unicode value
419 * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
420 * - mbch is the native local-dependent multibyte representation
423 /* Setup wchar glyph for one UCS-2 char */
424 static void convert_ucs(unsigned char fch
, uint16_t uch
, iconv_t conv
)
426 char mbch
[MB_LEN_MAX
];
432 puch
= (char *) &uch
;
433 pmbch
= (char *) mbch
;
435 smbch
= sizeof(mbch
);
437 if (iconv(conv
, &puch
, &such
, &pmbch
, &smbch
) == (size_t) -1) {
438 fprintf(stderr
, "Could not convert 0x%04x "
439 "from UCS-2 to a multibyte character: %s\n",
440 uch
, strerror(errno
));
444 memset(&ps
, 0, sizeof(ps
));
445 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
446 fprintf(stderr
, "Could not convert 0x%04x "
447 "from a multibyte character to wchar_t: %s\n",
448 uch
, strerror(errno
));
453 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
456 /* Setup wchar glyph for one font character */
457 static void convert_font(unsigned char fch
, iconv_t conv
)
459 char mbch
[MB_LEN_MAX
];
465 pfch
= (char *) &fch
;
466 pmbch
= (char *) &mbch
;
468 smbch
= sizeof(mbch
);
470 if (iconv(conv
, &pfch
, &sfch
, &pmbch
, &smbch
) == (size_t) -1) {
471 fprintf(stderr
, "Could not convert font glyph 0x%02x "
472 "from %s to a multibyte character: %s\n",
473 fch
, font_charset
, strerror(errno
));
477 memset(&ps
, 0, sizeof(ps
));
478 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
479 fprintf(stderr
, "Could not convert font glyph 0x%02x "
480 "from a multibyte character to wchar_t: %s\n",
481 fch
, strerror(errno
));
486 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
489 /* Convert one wchar to UCS-2 */
490 static uint16_t get_ucs(wchar_t wch
, iconv_t conv
)
492 char mbch
[MB_LEN_MAX
];
499 memset(&ps
, 0, sizeof(ps
));
500 ret
= wcrtomb(mbch
, wch
, &ps
);
502 fprintf(stderr
, "Could not convert 0x%04lx "
503 "from wchar_t to a multibyte character: %s\n",
504 (unsigned long)wch
, strerror(errno
));
508 pmbch
= (char *) mbch
;
509 puch
= (char *) &uch
;
513 if (iconv(conv
, &pmbch
, &smbch
, &puch
, &such
) == (size_t) -1) {
514 fprintf(stderr
, "Could not convert 0x%04lx "
515 "from a multibyte character to UCS-2 : %s\n",
516 (unsigned long)wch
, strerror(errno
));
524 * Setup mapping for vga to curses line graphics.
526 static void font_setup(void)
528 iconv_t ucs2_to_nativecharset
;
529 iconv_t nativecharset_to_ucs2
;
532 g_autofree gchar
*local_codeset
= g_get_codeset();
535 * Control characters are normally non-printable, but VGA does have
536 * well-known glyphs for them.
538 static const uint16_t control_characters
[0x20] = {
573 ucs2_to_nativecharset
= iconv_open(local_codeset
, "UCS-2");
574 if (ucs2_to_nativecharset
== (iconv_t
) -1) {
575 fprintf(stderr
, "Could not convert font glyphs from UCS-2: '%s'\n",
580 nativecharset_to_ucs2
= iconv_open("UCS-2", local_codeset
);
581 if (nativecharset_to_ucs2
== (iconv_t
) -1) {
582 iconv_close(ucs2_to_nativecharset
);
583 fprintf(stderr
, "Could not convert font glyphs to UCS-2: '%s'\n",
588 font_conv
= iconv_open(local_codeset
, font_charset
);
589 if (font_conv
== (iconv_t
) -1) {
590 iconv_close(ucs2_to_nativecharset
);
591 iconv_close(nativecharset_to_ucs2
);
592 fprintf(stderr
, "Could not convert font glyphs from %s: '%s'\n",
593 font_charset
, strerror(errno
));
597 /* Control characters */
598 for (i
= 0; i
<= 0x1F; i
++) {
599 convert_ucs(i
, control_characters
[i
], ucs2_to_nativecharset
);
602 for (i
= 0x20; i
<= 0xFF; i
++) {
603 convert_font(i
, font_conv
);
607 convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset
);
609 if (strcmp(local_codeset
, "UTF-8")) {
610 /* Non-Unicode capable, use termcap equivalents for those available */
611 for (i
= 0; i
<= 0xFF; i
++) {
612 wchar_t wch
[CCHARW_MAX
];
617 ret
= getcchar(&vga_to_curses
[i
], wch
, &attr
, &color
, NULL
);
621 switch (get_ucs(wch
[0], nativecharset_to_ucs2
)) {
623 vga_to_curses
[i
] = *WACS_STERLING
;
626 vga_to_curses
[i
] = *WACS_BOARD
;
629 vga_to_curses
[i
] = *WACS_CKBOARD
;
632 vga_to_curses
[i
] = *WACS_VLINE
;
635 vga_to_curses
[i
] = *WACS_RTEE
;
638 vga_to_curses
[i
] = *WACS_URCORNER
;
641 vga_to_curses
[i
] = *WACS_LLCORNER
;
644 vga_to_curses
[i
] = *WACS_BTEE
;
647 vga_to_curses
[i
] = *WACS_TTEE
;
650 vga_to_curses
[i
] = *WACS_LTEE
;
653 vga_to_curses
[i
] = *WACS_HLINE
;
656 vga_to_curses
[i
] = *WACS_PLUS
;
659 vga_to_curses
[i
] = *WACS_LANTERN
;
662 vga_to_curses
[i
] = *WACS_NEQUAL
;
665 vga_to_curses
[i
] = *WACS_LRCORNER
;
668 vga_to_curses
[i
] = *WACS_ULCORNER
;
671 vga_to_curses
[i
] = *WACS_BLOCK
;
674 vga_to_curses
[i
] = *WACS_PI
;
677 vga_to_curses
[i
] = *WACS_PLMINUS
;
680 vga_to_curses
[i
] = *WACS_GEQUAL
;
683 vga_to_curses
[i
] = *WACS_LEQUAL
;
686 vga_to_curses
[i
] = *WACS_DEGREE
;
689 vga_to_curses
[i
] = *WACS_BULLET
;
692 vga_to_curses
[i
] = *WACS_DIAMOND
;
695 vga_to_curses
[i
] = *WACS_RARROW
;
698 vga_to_curses
[i
] = *WACS_LARROW
;
701 vga_to_curses
[i
] = *WACS_UARROW
;
704 vga_to_curses
[i
] = *WACS_DARROW
;
707 vga_to_curses
[i
] = *WACS_S1
;
710 vga_to_curses
[i
] = *WACS_S3
;
713 vga_to_curses
[i
] = *WACS_S7
;
716 vga_to_curses
[i
] = *WACS_S9
;
721 iconv_close(ucs2_to_nativecharset
);
722 iconv_close(nativecharset_to_ucs2
);
723 iconv_close(font_conv
);
726 static void curses_setup(void)
728 int i
, colour_default
[8] = {
729 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
730 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
731 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
732 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
733 [QEMU_COLOR_RED
] = COLOR_RED
,
734 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
735 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
736 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
739 /* input as raw as possible, let everything be interpreted
740 * by the guest system */
741 initscr(); noecho(); intrflush(stdscr
, FALSE
);
742 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
743 start_color(); raw(); scrollok(stdscr
, FALSE
);
746 /* Make color pair to match color format (3bits bg:3bits fg) */
747 for (i
= 0; i
< 64; i
++) {
748 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
750 /* Set default color for more than 64 for safety. */
751 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
752 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
758 static void curses_keyboard_setup(void)
760 #if defined(__APPLE__)
761 /* always use generic keymaps */
762 if (!keyboard_layout
)
763 keyboard_layout
= "en-us";
765 if(keyboard_layout
) {
766 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
,
771 static const DisplayChangeListenerOps dcl_ops
= {
772 .dpy_name
= "curses",
773 .dpy_text_update
= curses_update
,
774 .dpy_text_resize
= curses_resize
,
775 .dpy_refresh
= curses_refresh
,
776 .dpy_text_cursor
= curses_cursor_position
,
779 static void curses_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
783 fprintf(stderr
, "We need a terminal output\n");
788 setlocale(LC_CTYPE
, "");
789 if (opts
->u
.curses
.charset
) {
790 font_charset
= opts
->u
.curses
.charset
;
792 screen
= g_new0(console_ch_t
, 160 * 100);
793 vga_to_curses
= g_new0(cchar_t
, 256);
795 curses_keyboard_setup();
796 atexit(curses_atexit
);
800 dcl
= g_new0(DisplayChangeListener
, 1);
802 register_displaychangelistener(dcl
);
807 static QemuDisplay qemu_display_curses
= {
808 .type
= DISPLAY_TYPE_CURSES
,
809 .init
= curses_display_init
,
812 static void register_curses(void)
814 qemu_display_register(&qemu_display_curses
);
817 type_init(register_curses
);