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
24 #include "qemu/osdep.h"
27 #include <sys/ioctl.h>
35 #include "qapi/error.h"
36 #include "qemu-common.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
[160 * 100];
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
[256];
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
] & 0xff;
78 chtype at
= line
[x
] & ~0xff;
79 ret
= getcchar(&vga_to_curses
[ch
], wch
, &attrs
, &colors
, NULL
);
80 if (ret
== ERR
|| wch
[0] == 0) {
84 setcchar(&curses_line
[x
], wch
, at
, 0, NULL
);
86 mvwadd_wchnstr(screenpad
, y
, 0, curses_line
, width
);
89 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
93 static void curses_calc_pad(void)
95 if (qemu_console_is_fixedsize(NULL
)) {
109 screenpad
= newpad(height
, width
);
112 px
= (width
- COLS
) / 2;
117 sminx
= (COLS
- width
) / 2;
118 smaxx
= sminx
+ width
;
121 if (height
> LINES
) {
122 py
= (height
- LINES
) / 2;
127 sminy
= (LINES
- height
) / 2;
128 smaxy
= sminy
+ height
;
132 static void curses_resize(DisplayChangeListener
*dcl
,
133 int width
, int height
)
135 if (width
== gwidth
&& height
== gheight
) {
145 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
146 static volatile sig_atomic_t got_sigwinch
;
147 static void curses_winch_check(void)
150 unsigned short ws_row
;
151 unsigned short ws_col
;
152 unsigned short ws_xpixel
; /* unused */
153 unsigned short ws_ypixel
; /* unused */
159 got_sigwinch
= false;
161 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
165 resize_term(ws
.ws_row
, ws
.ws_col
);
169 static void curses_winch_handler(int signum
)
174 static void curses_winch_init(void)
176 struct sigaction old
, winch
= {
177 .sa_handler
= curses_winch_handler
,
179 sigaction(SIGWINCH
, &winch
, &old
);
182 static void curses_winch_check(void) {}
183 static void curses_winch_init(void) {}
186 static void curses_cursor_position(DisplayChangeListener
*dcl
,
193 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
196 /* it seems that curs_set(1) must always be called before
197 * curs_set(2) for the latter to have effect */
198 if (!qemu_console_is_graphic(NULL
)) {
208 /* generic keyboard conversion */
210 #include "curses_keys.h"
212 static kbd_layout_t
*kbd_layout
= NULL
;
214 static wint_t console_getch(enum maybe_keycode
*maybe_keycode
)
217 switch (get_wch(&ret
)) {
219 *maybe_keycode
= CURSES_KEYCODE
;
222 *maybe_keycode
= CURSES_CHAR
;
231 static int curses2foo(const int _curses2foo
[], const int _curseskey2foo
[],
232 int chr
, enum maybe_keycode maybe_keycode
)
235 if (maybe_keycode
== CURSES_CHAR
) {
236 if (chr
< CURSES_CHARS
) {
237 ret
= _curses2foo
[chr
];
240 if (chr
< CURSES_KEYS
) {
241 ret
= _curseskey2foo
[chr
];
243 if (ret
== -1 && maybe_keycode
== CURSES_CHAR_OR_KEYCODE
&&
244 chr
< CURSES_CHARS
) {
245 ret
= _curses2foo
[chr
];
251 #define curses2keycode(chr, maybe_keycode) \
252 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
253 #define curses2keysym(chr, maybe_keycode) \
254 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
255 #define curses2qemu(chr, maybe_keycode) \
256 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
258 static void curses_refresh(DisplayChangeListener
*dcl
)
260 int chr
, keysym
, keycode
, keycode_alt
;
261 enum maybe_keycode maybe_keycode
;
263 curses_winch_check();
269 graphic_hw_invalidate(NULL
);
273 graphic_hw_text_update(NULL
, screen
);
276 /* while there are any pending key strokes to process */
277 chr
= console_getch(&maybe_keycode
);
283 /* this shouldn't occur when we use a custom SIGWINCH handler */
284 if (maybe_keycode
!= CURSES_CHAR
&& chr
== KEY_RESIZE
) {
288 curses_update(dcl
, 0, 0, width
, height
);
293 keycode
= curses2keycode(chr
, maybe_keycode
);
298 enum maybe_keycode next_maybe_keycode
;
299 int nextchr
= console_getch(&next_maybe_keycode
);
303 maybe_keycode
= next_maybe_keycode
;
305 keycode
= curses2keycode(chr
, maybe_keycode
);
310 /* process keys reserved for qemu */
311 if (keycode
>= QEMU_KEY_CONSOLE0
&&
312 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
314 wnoutrefresh(stdscr
);
315 console_select(keycode
- QEMU_KEY_CONSOLE0
);
325 keysym
= curses2keysym(chr
, maybe_keycode
);
330 if (keysym
>= 'A' && keysym
<= 'Z')
332 keysym
|= KEYSYM_CNTRL
;
337 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
,
342 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
343 keycode
|= keycode_alt
;
349 if (qemu_console_is_graphic(NULL
)) {
350 /* since terminals don't know about key press and release
351 * events, we need to emit both for each key received */
352 if (keycode
& SHIFT
) {
353 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
354 qemu_input_event_send_key_delay(0);
356 if (keycode
& CNTRL
) {
357 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
358 qemu_input_event_send_key_delay(0);
361 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
362 qemu_input_event_send_key_delay(0);
364 if (keycode
& ALTGR
) {
365 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
366 qemu_input_event_send_key_delay(0);
369 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
370 qemu_input_event_send_key_delay(0);
371 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
372 qemu_input_event_send_key_delay(0);
374 if (keycode
& ALTGR
) {
375 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
376 qemu_input_event_send_key_delay(0);
379 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
380 qemu_input_event_send_key_delay(0);
382 if (keycode
& CNTRL
) {
383 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
384 qemu_input_event_send_key_delay(0);
386 if (keycode
& SHIFT
) {
387 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
388 qemu_input_event_send_key_delay(0);
391 keysym
= curses2qemu(chr
, maybe_keycode
);
395 kbd_put_keysym(keysym
);
400 static void curses_atexit(void)
407 * - fch is the font glyph number
408 * - uch is the unicode value
409 * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
410 * - mbch is the native local-dependent multibyte representation
413 /* Setup wchar glyph for one UCS-2 char */
414 static void convert_ucs(unsigned char fch
, uint16_t uch
, iconv_t conv
)
416 char mbch
[MB_LEN_MAX
];
422 puch
= (char *) &uch
;
423 pmbch
= (char *) mbch
;
425 smbch
= sizeof(mbch
);
427 if (iconv(conv
, &puch
, &such
, &pmbch
, &smbch
) == (size_t) -1) {
428 fprintf(stderr
, "Could not convert 0x%04x "
429 "from UCS-2 to a multibyte character: %s\n",
430 uch
, strerror(errno
));
434 memset(&ps
, 0, sizeof(ps
));
435 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
436 fprintf(stderr
, "Could not convert 0x%04x "
437 "from a multibyte character to wchar_t: %s\n",
438 uch
, strerror(errno
));
443 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
446 /* Setup wchar glyph for one font character */
447 static void convert_font(unsigned char fch
, iconv_t conv
)
449 char mbch
[MB_LEN_MAX
];
455 pfch
= (char *) &fch
;
456 pmbch
= (char *) &mbch
;
458 smbch
= sizeof(mbch
);
460 if (iconv(conv
, &pfch
, &sfch
, &pmbch
, &smbch
) == (size_t) -1) {
461 fprintf(stderr
, "Could not convert font glyph 0x%02x "
462 "from %s to a multibyte character: %s\n",
463 fch
, font_charset
, strerror(errno
));
467 memset(&ps
, 0, sizeof(ps
));
468 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
469 fprintf(stderr
, "Could not convert font glyph 0x%02x "
470 "from a multibyte character to wchar_t: %s\n",
471 fch
, strerror(errno
));
476 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
479 /* Convert one wchar to UCS-2 */
480 static uint16_t get_ucs(wchar_t wch
, iconv_t conv
)
482 char mbch
[MB_LEN_MAX
];
489 memset(&ps
, 0, sizeof(ps
));
490 ret
= wcrtomb(mbch
, wch
, &ps
);
492 fprintf(stderr
, "Could not convert 0x%04x "
493 "from wchar_t to a multibyte character: %s\n",
494 wch
, strerror(errno
));
498 pmbch
= (char *) mbch
;
499 puch
= (char *) &uch
;
503 if (iconv(conv
, &pmbch
, &smbch
, &puch
, &such
) == (size_t) -1) {
504 fprintf(stderr
, "Could not convert 0x%04x "
505 "from a multibyte character to UCS-2 : %s\n",
506 wch
, strerror(errno
));
514 * Setup mapping for vga to curses line graphics.
516 static void font_setup(void)
518 iconv_t ucs2_to_nativecharset
;
519 iconv_t nativecharset_to_ucs2
;
524 * Control characters are normally non-printable, but VGA does have
525 * well-known glyphs for them.
527 static uint16_t control_characters
[0x20] = {
562 ucs2_to_nativecharset
= iconv_open(nl_langinfo(CODESET
), "UCS-2");
563 if (ucs2_to_nativecharset
== (iconv_t
) -1) {
564 fprintf(stderr
, "Could not convert font glyphs from UCS-2: '%s'\n",
569 nativecharset_to_ucs2
= iconv_open("UCS-2", nl_langinfo(CODESET
));
570 if (nativecharset_to_ucs2
== (iconv_t
) -1) {
571 iconv_close(ucs2_to_nativecharset
);
572 fprintf(stderr
, "Could not convert font glyphs to UCS-2: '%s'\n",
577 font_conv
= iconv_open(nl_langinfo(CODESET
), font_charset
);
578 if (font_conv
== (iconv_t
) -1) {
579 iconv_close(ucs2_to_nativecharset
);
580 iconv_close(nativecharset_to_ucs2
);
581 fprintf(stderr
, "Could not convert font glyphs from %s: '%s'\n",
582 font_charset
, strerror(errno
));
586 /* Control characters */
587 for (i
= 0; i
<= 0x1F; i
++) {
588 convert_ucs(i
, control_characters
[i
], ucs2_to_nativecharset
);
591 for (i
= 0x20; i
<= 0xFF; i
++) {
592 convert_font(i
, font_conv
);
596 convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset
);
598 if (strcmp(nl_langinfo(CODESET
), "UTF-8")) {
599 /* Non-Unicode capable, use termcap equivalents for those available */
600 for (i
= 0; i
<= 0xFF; i
++) {
601 wchar_t wch
[CCHARW_MAX
];
606 ret
= getcchar(&vga_to_curses
[i
], wch
, &attr
, &color
, NULL
);
610 switch (get_ucs(wch
[0], nativecharset_to_ucs2
)) {
612 vga_to_curses
[i
] = *WACS_STERLING
;
615 vga_to_curses
[i
] = *WACS_BOARD
;
618 vga_to_curses
[i
] = *WACS_CKBOARD
;
621 vga_to_curses
[i
] = *WACS_VLINE
;
624 vga_to_curses
[i
] = *WACS_RTEE
;
627 vga_to_curses
[i
] = *WACS_URCORNER
;
630 vga_to_curses
[i
] = *WACS_LLCORNER
;
633 vga_to_curses
[i
] = *WACS_BTEE
;
636 vga_to_curses
[i
] = *WACS_TTEE
;
639 vga_to_curses
[i
] = *WACS_LTEE
;
642 vga_to_curses
[i
] = *WACS_HLINE
;
645 vga_to_curses
[i
] = *WACS_PLUS
;
648 vga_to_curses
[i
] = *WACS_LANTERN
;
651 vga_to_curses
[i
] = *WACS_NEQUAL
;
654 vga_to_curses
[i
] = *WACS_LRCORNER
;
657 vga_to_curses
[i
] = *WACS_ULCORNER
;
660 vga_to_curses
[i
] = *WACS_BLOCK
;
663 vga_to_curses
[i
] = *WACS_PI
;
666 vga_to_curses
[i
] = *WACS_PLMINUS
;
669 vga_to_curses
[i
] = *WACS_GEQUAL
;
672 vga_to_curses
[i
] = *WACS_LEQUAL
;
675 vga_to_curses
[i
] = *WACS_DEGREE
;
678 vga_to_curses
[i
] = *WACS_BULLET
;
681 vga_to_curses
[i
] = *WACS_DIAMOND
;
684 vga_to_curses
[i
] = *WACS_RARROW
;
687 vga_to_curses
[i
] = *WACS_LARROW
;
690 vga_to_curses
[i
] = *WACS_UARROW
;
693 vga_to_curses
[i
] = *WACS_DARROW
;
696 vga_to_curses
[i
] = *WACS_S1
;
699 vga_to_curses
[i
] = *WACS_S3
;
702 vga_to_curses
[i
] = *WACS_S7
;
705 vga_to_curses
[i
] = *WACS_S9
;
710 iconv_close(ucs2_to_nativecharset
);
711 iconv_close(nativecharset_to_ucs2
);
712 iconv_close(font_conv
);
715 static void curses_setup(void)
717 int i
, colour_default
[8] = {
718 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
719 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
720 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
721 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
722 [QEMU_COLOR_RED
] = COLOR_RED
,
723 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
724 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
725 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
728 /* input as raw as possible, let everything be interpreted
729 * by the guest system */
730 initscr(); noecho(); intrflush(stdscr
, FALSE
);
731 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
732 start_color(); raw(); scrollok(stdscr
, FALSE
);
735 /* Make color pair to match color format (3bits bg:3bits fg) */
736 for (i
= 0; i
< 64; i
++) {
737 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
739 /* Set default color for more than 64 for safety. */
740 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
741 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
747 static void curses_keyboard_setup(void)
749 #if defined(__APPLE__)
750 /* always use generic keymaps */
751 if (!keyboard_layout
)
752 keyboard_layout
= "en-us";
754 if(keyboard_layout
) {
755 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
,
760 static const DisplayChangeListenerOps dcl_ops
= {
761 .dpy_name
= "curses",
762 .dpy_text_update
= curses_update
,
763 .dpy_text_resize
= curses_resize
,
764 .dpy_refresh
= curses_refresh
,
765 .dpy_text_cursor
= curses_cursor_position
,
768 static void curses_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
772 fprintf(stderr
, "We need a terminal output\n");
777 setlocale(LC_CTYPE
, "");
778 if (opts
->u
.curses
.charset
) {
779 font_charset
= opts
->u
.curses
.charset
;
782 curses_keyboard_setup();
783 atexit(curses_atexit
);
787 dcl
= g_new0(DisplayChangeListener
, 1);
789 register_displaychangelistener(dcl
);
794 static QemuDisplay qemu_display_curses
= {
795 .type
= DISPLAY_TYPE_CURSES
,
796 .init
= curses_display_init
,
799 static void register_curses(void)
801 qemu_display_register(&qemu_display_curses
);
804 type_init(register_curses
);