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>
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
] & 0xff;
79 chtype at
= line
[x
] & ~0xff;
80 ret
= getcchar(&vga_to_curses
[ch
], wch
, &attrs
, &colors
, NULL
);
81 if (ret
== ERR
|| wch
[0] == 0) {
85 setcchar(&curses_line
[x
], wch
, at
, 0, NULL
);
87 mvwadd_wchnstr(screenpad
, y
, 0, curses_line
, width
);
90 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
94 static void curses_calc_pad(void)
96 if (qemu_console_is_fixedsize(NULL
)) {
110 screenpad
= newpad(height
, width
);
113 px
= (width
- COLS
) / 2;
118 sminx
= (COLS
- width
) / 2;
119 smaxx
= sminx
+ width
;
122 if (height
> LINES
) {
123 py
= (height
- LINES
) / 2;
128 sminy
= (LINES
- height
) / 2;
129 smaxy
= sminy
+ height
;
133 static void curses_resize(DisplayChangeListener
*dcl
,
134 int width
, int height
)
136 if (width
== gwidth
&& height
== gheight
) {
146 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
147 static volatile sig_atomic_t got_sigwinch
;
148 static void curses_winch_check(void)
151 unsigned short ws_row
;
152 unsigned short ws_col
;
153 unsigned short ws_xpixel
; /* unused */
154 unsigned short ws_ypixel
; /* unused */
160 got_sigwinch
= false;
162 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
166 resize_term(ws
.ws_row
, ws
.ws_col
);
170 static void curses_winch_handler(int signum
)
175 static void curses_winch_init(void)
177 struct sigaction old
, winch
= {
178 .sa_handler
= curses_winch_handler
,
180 sigaction(SIGWINCH
, &winch
, &old
);
183 static void curses_winch_check(void) {}
184 static void curses_winch_init(void) {}
187 static void curses_cursor_position(DisplayChangeListener
*dcl
,
194 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
197 /* it seems that curs_set(1) must always be called before
198 * curs_set(2) for the latter to have effect */
199 if (!qemu_console_is_graphic(NULL
)) {
209 /* generic keyboard conversion */
211 #include "curses_keys.h"
213 static kbd_layout_t
*kbd_layout
= NULL
;
215 static wint_t console_getch(enum maybe_keycode
*maybe_keycode
)
218 switch (get_wch(&ret
)) {
220 *maybe_keycode
= CURSES_KEYCODE
;
223 *maybe_keycode
= CURSES_CHAR
;
234 static int curses2foo(const int _curses2foo
[], const int _curseskey2foo
[],
235 int chr
, enum maybe_keycode maybe_keycode
)
238 if (maybe_keycode
== CURSES_CHAR
) {
239 if (chr
< CURSES_CHARS
) {
240 ret
= _curses2foo
[chr
];
243 if (chr
< CURSES_KEYS
) {
244 ret
= _curseskey2foo
[chr
];
246 if (ret
== -1 && maybe_keycode
== CURSES_CHAR_OR_KEYCODE
&&
247 chr
< CURSES_CHARS
) {
248 ret
= _curses2foo
[chr
];
254 #define curses2keycode(chr, maybe_keycode) \
255 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
256 #define curses2keysym(chr, maybe_keycode) \
257 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
258 #define curses2qemu(chr, maybe_keycode) \
259 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
261 static void curses_refresh(DisplayChangeListener
*dcl
)
263 int chr
, keysym
, keycode
, keycode_alt
;
264 enum maybe_keycode maybe_keycode
;
266 curses_winch_check();
272 graphic_hw_invalidate(NULL
);
276 graphic_hw_text_update(NULL
, screen
);
279 /* while there are any pending key strokes to process */
280 chr
= console_getch(&maybe_keycode
);
286 /* this shouldn't occur when we use a custom SIGWINCH handler */
287 if (maybe_keycode
!= CURSES_CHAR
&& chr
== KEY_RESIZE
) {
291 curses_update(dcl
, 0, 0, width
, height
);
296 keycode
= curses2keycode(chr
, maybe_keycode
);
301 enum maybe_keycode next_maybe_keycode
;
302 int nextchr
= console_getch(&next_maybe_keycode
);
306 maybe_keycode
= next_maybe_keycode
;
308 keycode
= curses2keycode(chr
, maybe_keycode
);
313 /* process keys reserved for qemu */
314 if (keycode
>= QEMU_KEY_CONSOLE0
&&
315 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
317 wnoutrefresh(stdscr
);
318 console_select(keycode
- QEMU_KEY_CONSOLE0
);
328 keysym
= curses2keysym(chr
, maybe_keycode
);
333 if (keysym
>= 'A' && keysym
<= 'Z')
335 keysym
|= KEYSYM_CNTRL
;
340 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
,
345 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
346 keycode
|= keycode_alt
;
352 if (qemu_console_is_graphic(NULL
)) {
353 /* since terminals don't know about key press and release
354 * events, we need to emit both for each key received */
355 if (keycode
& SHIFT
) {
356 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
357 qemu_input_event_send_key_delay(0);
359 if (keycode
& CNTRL
) {
360 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
361 qemu_input_event_send_key_delay(0);
364 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
365 qemu_input_event_send_key_delay(0);
367 if (keycode
& ALTGR
) {
368 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
369 qemu_input_event_send_key_delay(0);
372 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
373 qemu_input_event_send_key_delay(0);
374 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
375 qemu_input_event_send_key_delay(0);
377 if (keycode
& ALTGR
) {
378 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
379 qemu_input_event_send_key_delay(0);
382 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
383 qemu_input_event_send_key_delay(0);
385 if (keycode
& CNTRL
) {
386 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
387 qemu_input_event_send_key_delay(0);
389 if (keycode
& SHIFT
) {
390 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
391 qemu_input_event_send_key_delay(0);
394 keysym
= curses2qemu(chr
, maybe_keycode
);
398 kbd_put_keysym(keysym
);
403 static void curses_atexit(void)
410 * - fch is the font glyph number
411 * - uch is the unicode value
412 * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
413 * - mbch is the native local-dependent multibyte representation
416 /* Setup wchar glyph for one UCS-2 char */
417 static void convert_ucs(unsigned char fch
, uint16_t uch
, iconv_t conv
)
419 char mbch
[MB_LEN_MAX
];
425 puch
= (char *) &uch
;
426 pmbch
= (char *) mbch
;
428 smbch
= sizeof(mbch
);
430 if (iconv(conv
, &puch
, &such
, &pmbch
, &smbch
) == (size_t) -1) {
431 fprintf(stderr
, "Could not convert 0x%04x "
432 "from UCS-2 to a multibyte character: %s\n",
433 uch
, strerror(errno
));
437 memset(&ps
, 0, sizeof(ps
));
438 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
439 fprintf(stderr
, "Could not convert 0x%04x "
440 "from a multibyte character to wchar_t: %s\n",
441 uch
, strerror(errno
));
446 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
449 /* Setup wchar glyph for one font character */
450 static void convert_font(unsigned char fch
, iconv_t conv
)
452 char mbch
[MB_LEN_MAX
];
458 pfch
= (char *) &fch
;
459 pmbch
= (char *) &mbch
;
461 smbch
= sizeof(mbch
);
463 if (iconv(conv
, &pfch
, &sfch
, &pmbch
, &smbch
) == (size_t) -1) {
464 fprintf(stderr
, "Could not convert font glyph 0x%02x "
465 "from %s to a multibyte character: %s\n",
466 fch
, font_charset
, strerror(errno
));
470 memset(&ps
, 0, sizeof(ps
));
471 if (mbrtowc(&wch
[0], mbch
, sizeof(mbch
) - smbch
, &ps
) == -1) {
472 fprintf(stderr
, "Could not convert font glyph 0x%02x "
473 "from a multibyte character to wchar_t: %s\n",
474 fch
, strerror(errno
));
479 setcchar(&vga_to_curses
[fch
], wch
, 0, 0, NULL
);
482 /* Convert one wchar to UCS-2 */
483 static uint16_t get_ucs(wchar_t wch
, iconv_t conv
)
485 char mbch
[MB_LEN_MAX
];
492 memset(&ps
, 0, sizeof(ps
));
493 ret
= wcrtomb(mbch
, wch
, &ps
);
495 fprintf(stderr
, "Could not convert 0x%04lx "
496 "from wchar_t to a multibyte character: %s\n",
497 (unsigned long)wch
, strerror(errno
));
501 pmbch
= (char *) mbch
;
502 puch
= (char *) &uch
;
506 if (iconv(conv
, &pmbch
, &smbch
, &puch
, &such
) == (size_t) -1) {
507 fprintf(stderr
, "Could not convert 0x%04lx "
508 "from a multibyte character to UCS-2 : %s\n",
509 (unsigned long)wch
, strerror(errno
));
517 * Setup mapping for vga to curses line graphics.
519 static void font_setup(void)
521 iconv_t ucs2_to_nativecharset
;
522 iconv_t nativecharset_to_ucs2
;
527 * Control characters are normally non-printable, but VGA does have
528 * well-known glyphs for them.
530 static uint16_t control_characters
[0x20] = {
565 ucs2_to_nativecharset
= iconv_open(nl_langinfo(CODESET
), "UCS-2");
566 if (ucs2_to_nativecharset
== (iconv_t
) -1) {
567 fprintf(stderr
, "Could not convert font glyphs from UCS-2: '%s'\n",
572 nativecharset_to_ucs2
= iconv_open("UCS-2", nl_langinfo(CODESET
));
573 if (nativecharset_to_ucs2
== (iconv_t
) -1) {
574 iconv_close(ucs2_to_nativecharset
);
575 fprintf(stderr
, "Could not convert font glyphs to UCS-2: '%s'\n",
580 font_conv
= iconv_open(nl_langinfo(CODESET
), font_charset
);
581 if (font_conv
== (iconv_t
) -1) {
582 iconv_close(ucs2_to_nativecharset
);
583 iconv_close(nativecharset_to_ucs2
);
584 fprintf(stderr
, "Could not convert font glyphs from %s: '%s'\n",
585 font_charset
, strerror(errno
));
589 /* Control characters */
590 for (i
= 0; i
<= 0x1F; i
++) {
591 convert_ucs(i
, control_characters
[i
], ucs2_to_nativecharset
);
594 for (i
= 0x20; i
<= 0xFF; i
++) {
595 convert_font(i
, font_conv
);
599 convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset
);
601 if (strcmp(nl_langinfo(CODESET
), "UTF-8")) {
602 /* Non-Unicode capable, use termcap equivalents for those available */
603 for (i
= 0; i
<= 0xFF; i
++) {
604 wchar_t wch
[CCHARW_MAX
];
609 ret
= getcchar(&vga_to_curses
[i
], wch
, &attr
, &color
, NULL
);
613 switch (get_ucs(wch
[0], nativecharset_to_ucs2
)) {
615 vga_to_curses
[i
] = *WACS_STERLING
;
618 vga_to_curses
[i
] = *WACS_BOARD
;
621 vga_to_curses
[i
] = *WACS_CKBOARD
;
624 vga_to_curses
[i
] = *WACS_VLINE
;
627 vga_to_curses
[i
] = *WACS_RTEE
;
630 vga_to_curses
[i
] = *WACS_URCORNER
;
633 vga_to_curses
[i
] = *WACS_LLCORNER
;
636 vga_to_curses
[i
] = *WACS_BTEE
;
639 vga_to_curses
[i
] = *WACS_TTEE
;
642 vga_to_curses
[i
] = *WACS_LTEE
;
645 vga_to_curses
[i
] = *WACS_HLINE
;
648 vga_to_curses
[i
] = *WACS_PLUS
;
651 vga_to_curses
[i
] = *WACS_LANTERN
;
654 vga_to_curses
[i
] = *WACS_NEQUAL
;
657 vga_to_curses
[i
] = *WACS_LRCORNER
;
660 vga_to_curses
[i
] = *WACS_ULCORNER
;
663 vga_to_curses
[i
] = *WACS_BLOCK
;
666 vga_to_curses
[i
] = *WACS_PI
;
669 vga_to_curses
[i
] = *WACS_PLMINUS
;
672 vga_to_curses
[i
] = *WACS_GEQUAL
;
675 vga_to_curses
[i
] = *WACS_LEQUAL
;
678 vga_to_curses
[i
] = *WACS_DEGREE
;
681 vga_to_curses
[i
] = *WACS_BULLET
;
684 vga_to_curses
[i
] = *WACS_DIAMOND
;
687 vga_to_curses
[i
] = *WACS_RARROW
;
690 vga_to_curses
[i
] = *WACS_LARROW
;
693 vga_to_curses
[i
] = *WACS_UARROW
;
696 vga_to_curses
[i
] = *WACS_DARROW
;
699 vga_to_curses
[i
] = *WACS_S1
;
702 vga_to_curses
[i
] = *WACS_S3
;
705 vga_to_curses
[i
] = *WACS_S7
;
708 vga_to_curses
[i
] = *WACS_S9
;
713 iconv_close(ucs2_to_nativecharset
);
714 iconv_close(nativecharset_to_ucs2
);
715 iconv_close(font_conv
);
718 static void curses_setup(void)
720 int i
, colour_default
[8] = {
721 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
722 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
723 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
724 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
725 [QEMU_COLOR_RED
] = COLOR_RED
,
726 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
727 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
728 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
731 /* input as raw as possible, let everything be interpreted
732 * by the guest system */
733 initscr(); noecho(); intrflush(stdscr
, FALSE
);
734 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
735 start_color(); raw(); scrollok(stdscr
, FALSE
);
738 /* Make color pair to match color format (3bits bg:3bits fg) */
739 for (i
= 0; i
< 64; i
++) {
740 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
742 /* Set default color for more than 64 for safety. */
743 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
744 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
750 static void curses_keyboard_setup(void)
752 #if defined(__APPLE__)
753 /* always use generic keymaps */
754 if (!keyboard_layout
)
755 keyboard_layout
= "en-us";
757 if(keyboard_layout
) {
758 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
,
763 static const DisplayChangeListenerOps dcl_ops
= {
764 .dpy_name
= "curses",
765 .dpy_text_update
= curses_update
,
766 .dpy_text_resize
= curses_resize
,
767 .dpy_refresh
= curses_refresh
,
768 .dpy_text_cursor
= curses_cursor_position
,
771 static void curses_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
775 fprintf(stderr
, "We need a terminal output\n");
780 setlocale(LC_CTYPE
, "");
781 if (opts
->u
.curses
.charset
) {
782 font_charset
= opts
->u
.curses
.charset
;
785 curses_keyboard_setup();
786 atexit(curses_atexit
);
790 dcl
= g_new0(DisplayChangeListener
, 1);
792 register_displaychangelistener(dcl
);
797 static QemuDisplay qemu_display_curses
= {
798 .type
= DISPLAY_TYPE_CURSES
,
799 .init
= curses_display_init
,
802 static void register_curses(void)
804 qemu_display_register(&qemu_display_curses
);
807 type_init(register_curses
);