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
];
70 line
= screen
+ y
* width
;
71 for (h
+= y
; y
< h
; y
++, line
+= width
) {
72 for (x
= 0; x
< width
; x
++) {
73 chtype ch
= line
[x
] & 0xff;
74 chtype at
= line
[x
] & ~0xff;
75 if (vga_to_curses
[ch
].chars
[0]) {
76 curses_line
[x
] = vga_to_curses
[ch
];
78 curses_line
[x
] = (cchar_t
) {
82 curses_line
[x
].attr
|= at
;
84 mvwadd_wchnstr(screenpad
, y
, 0, curses_line
, width
);
87 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
91 static void curses_calc_pad(void)
93 if (qemu_console_is_fixedsize(NULL
)) {
107 screenpad
= newpad(height
, width
);
110 px
= (width
- COLS
) / 2;
115 sminx
= (COLS
- width
) / 2;
116 smaxx
= sminx
+ width
;
119 if (height
> LINES
) {
120 py
= (height
- LINES
) / 2;
125 sminy
= (LINES
- height
) / 2;
126 smaxy
= sminy
+ height
;
130 static void curses_resize(DisplayChangeListener
*dcl
,
131 int width
, int height
)
133 if (width
== gwidth
&& height
== gheight
) {
143 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
144 static volatile sig_atomic_t got_sigwinch
;
145 static void curses_winch_check(void)
148 unsigned short ws_row
;
149 unsigned short ws_col
;
150 unsigned short ws_xpixel
; /* unused */
151 unsigned short ws_ypixel
; /* unused */
157 got_sigwinch
= false;
159 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
163 resize_term(ws
.ws_row
, ws
.ws_col
);
167 static void curses_winch_handler(int signum
)
172 static void curses_winch_init(void)
174 struct sigaction old
, winch
= {
175 .sa_handler
= curses_winch_handler
,
177 sigaction(SIGWINCH
, &winch
, &old
);
180 static void curses_winch_check(void) {}
181 static void curses_winch_init(void) {}
184 static void curses_cursor_position(DisplayChangeListener
*dcl
,
191 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
194 /* it seems that curs_set(1) must always be called before
195 * curs_set(2) for the latter to have effect */
196 if (!qemu_console_is_graphic(NULL
)) {
206 /* generic keyboard conversion */
208 #include "curses_keys.h"
210 static kbd_layout_t
*kbd_layout
= NULL
;
212 static wint_t console_getch(enum maybe_keycode
*maybe_keycode
)
215 switch (get_wch(&ret
)) {
217 *maybe_keycode
= CURSES_KEYCODE
;
220 *maybe_keycode
= CURSES_CHAR
;
229 static int curses2foo(const int _curses2foo
[], const int _curseskey2foo
[],
230 int chr
, enum maybe_keycode maybe_keycode
)
233 if (maybe_keycode
== CURSES_CHAR
) {
234 if (chr
< CURSES_CHARS
) {
235 ret
= _curses2foo
[chr
];
238 if (chr
< CURSES_KEYS
) {
239 ret
= _curseskey2foo
[chr
];
241 if (ret
== -1 && maybe_keycode
== CURSES_CHAR_OR_KEYCODE
&&
242 chr
< CURSES_CHARS
) {
243 ret
= _curses2foo
[chr
];
249 #define curses2keycode(chr, maybe_keycode) \
250 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
251 #define curses2keysym(chr, maybe_keycode) \
252 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
253 #define curses2qemu(chr, maybe_keycode) \
254 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
256 static void curses_refresh(DisplayChangeListener
*dcl
)
258 int chr
, keysym
, keycode
, keycode_alt
;
259 enum maybe_keycode maybe_keycode
;
261 curses_winch_check();
267 graphic_hw_invalidate(NULL
);
271 graphic_hw_text_update(NULL
, screen
);
274 /* while there are any pending key strokes to process */
275 chr
= console_getch(&maybe_keycode
);
281 /* this shouldn't occur when we use a custom SIGWINCH handler */
282 if (maybe_keycode
!= CURSES_CHAR
&& chr
== KEY_RESIZE
) {
286 curses_update(dcl
, 0, 0, width
, height
);
291 keycode
= curses2keycode(chr
, maybe_keycode
);
296 enum maybe_keycode next_maybe_keycode
;
297 int nextchr
= console_getch(&next_maybe_keycode
);
301 maybe_keycode
= next_maybe_keycode
;
303 keycode
= curses2keycode(chr
, maybe_keycode
);
308 /* process keys reserved for qemu */
309 if (keycode
>= QEMU_KEY_CONSOLE0
&&
310 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
312 wnoutrefresh(stdscr
);
313 console_select(keycode
- QEMU_KEY_CONSOLE0
);
323 keysym
= curses2keysym(chr
, maybe_keycode
);
328 if (keysym
>= 'A' && keysym
<= 'Z')
330 keysym
|= KEYSYM_CNTRL
;
335 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
,
340 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
341 keycode
|= keycode_alt
;
347 if (qemu_console_is_graphic(NULL
)) {
348 /* since terminals don't know about key press and release
349 * events, we need to emit both for each key received */
350 if (keycode
& SHIFT
) {
351 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
352 qemu_input_event_send_key_delay(0);
354 if (keycode
& CNTRL
) {
355 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
356 qemu_input_event_send_key_delay(0);
359 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
360 qemu_input_event_send_key_delay(0);
362 if (keycode
& ALTGR
) {
363 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
364 qemu_input_event_send_key_delay(0);
367 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
368 qemu_input_event_send_key_delay(0);
369 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
370 qemu_input_event_send_key_delay(0);
372 if (keycode
& ALTGR
) {
373 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
374 qemu_input_event_send_key_delay(0);
377 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
378 qemu_input_event_send_key_delay(0);
380 if (keycode
& CNTRL
) {
381 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
382 qemu_input_event_send_key_delay(0);
384 if (keycode
& SHIFT
) {
385 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
386 qemu_input_event_send_key_delay(0);
389 keysym
= curses2qemu(chr
, maybe_keycode
);
393 kbd_put_keysym(keysym
);
398 static void curses_atexit(void)
403 /* Setup wchar glyph for one UCS-2 char */
404 static void convert_ucs(int glyph
, uint16_t ch
, iconv_t conv
)
411 pwch
= (char *) &wch
;
415 if (iconv(conv
, &pch
, &sch
, &pwch
, &swch
) == (size_t) -1) {
416 fprintf(stderr
, "Could not convert 0x%04x from UCS-2 to WCHAR_T: %s\n",
417 ch
, strerror(errno
));
419 vga_to_curses
[glyph
].chars
[0] = wch
;
423 /* Setup wchar glyph for one font character */
424 static void convert_font(unsigned char ch
, iconv_t conv
)
431 pwch
= (char *) &wch
;
435 if (iconv(conv
, &pch
, &sch
, &pwch
, &swch
) == (size_t) -1) {
436 fprintf(stderr
, "Could not convert 0x%02x from %s to WCHAR_T: %s\n",
437 ch
, font_charset
, strerror(errno
));
439 vga_to_curses
[ch
].chars
[0] = wch
;
443 /* Convert one wchar to UCS-2 */
444 static uint16_t get_ucs(wchar_t wch
, iconv_t conv
)
451 pwch
= (char *) &wch
;
455 if (iconv(conv
, &pwch
, &swch
, &pch
, &sch
) == (size_t) -1) {
456 fprintf(stderr
, "Could not convert 0x%02lx from WCHAR_T to UCS-2: %s\n",
457 (unsigned long)wch
, strerror(errno
));
465 * Setup mapping for vga to curses line graphics.
467 static void font_setup(void)
470 * Control characters are normally non-printable, but VGA does have
471 * well-known glyphs for them.
473 static uint16_t control_characters
[0x20] = {
508 iconv_t ucs_to_wchar_conv
;
509 iconv_t wchar_to_ucs_conv
;
513 ucs_to_wchar_conv
= iconv_open("WCHAR_T", "UCS-2");
514 if (ucs_to_wchar_conv
== (iconv_t
) -1) {
515 fprintf(stderr
, "Could not convert font glyphs from UCS-2: '%s'\n",
520 wchar_to_ucs_conv
= iconv_open("UCS-2", "WCHAR_T");
521 if (wchar_to_ucs_conv
== (iconv_t
) -1) {
522 iconv_close(ucs_to_wchar_conv
);
523 fprintf(stderr
, "Could not convert font glyphs to UCS-2: '%s'\n",
528 font_conv
= iconv_open("WCHAR_T", font_charset
);
529 if (font_conv
== (iconv_t
) -1) {
530 iconv_close(ucs_to_wchar_conv
);
531 iconv_close(wchar_to_ucs_conv
);
532 fprintf(stderr
, "Could not convert font glyphs from %s: '%s'\n",
533 font_charset
, strerror(errno
));
537 /* Control characters */
538 for (i
= 0; i
<= 0x1F; i
++) {
539 convert_ucs(i
, control_characters
[i
], ucs_to_wchar_conv
);
542 for (i
= 0x20; i
<= 0xFF; i
++) {
543 convert_font(i
, font_conv
);
547 convert_ucs(0x7F, 0x2302, ucs_to_wchar_conv
);
549 if (strcmp(nl_langinfo(CODESET
), "UTF-8")) {
550 /* Non-Unicode capable, use termcap equivalents for those available */
551 for (i
= 0; i
<= 0xFF; i
++) {
552 switch (get_ucs(vga_to_curses
[i
].chars
[0], wchar_to_ucs_conv
)) {
554 vga_to_curses
[i
] = *WACS_STERLING
;
557 vga_to_curses
[i
] = *WACS_BOARD
;
560 vga_to_curses
[i
] = *WACS_CKBOARD
;
563 vga_to_curses
[i
] = *WACS_VLINE
;
566 vga_to_curses
[i
] = *WACS_RTEE
;
569 vga_to_curses
[i
] = *WACS_URCORNER
;
572 vga_to_curses
[i
] = *WACS_LLCORNER
;
575 vga_to_curses
[i
] = *WACS_BTEE
;
578 vga_to_curses
[i
] = *WACS_TTEE
;
581 vga_to_curses
[i
] = *WACS_LTEE
;
584 vga_to_curses
[i
] = *WACS_HLINE
;
587 vga_to_curses
[i
] = *WACS_PLUS
;
590 vga_to_curses
[i
] = *WACS_LANTERN
;
593 vga_to_curses
[i
] = *WACS_NEQUAL
;
596 vga_to_curses
[i
] = *WACS_LRCORNER
;
599 vga_to_curses
[i
] = *WACS_ULCORNER
;
602 vga_to_curses
[i
] = *WACS_BLOCK
;
605 vga_to_curses
[i
] = *WACS_PI
;
608 vga_to_curses
[i
] = *WACS_PLMINUS
;
611 vga_to_curses
[i
] = *WACS_GEQUAL
;
614 vga_to_curses
[i
] = *WACS_LEQUAL
;
617 vga_to_curses
[i
] = *WACS_DEGREE
;
620 vga_to_curses
[i
] = *WACS_BULLET
;
623 vga_to_curses
[i
] = *WACS_DIAMOND
;
626 vga_to_curses
[i
] = *WACS_RARROW
;
629 vga_to_curses
[i
] = *WACS_LARROW
;
632 vga_to_curses
[i
] = *WACS_UARROW
;
635 vga_to_curses
[i
] = *WACS_DARROW
;
638 vga_to_curses
[i
] = *WACS_S1
;
641 vga_to_curses
[i
] = *WACS_S3
;
644 vga_to_curses
[i
] = *WACS_S7
;
647 vga_to_curses
[i
] = *WACS_S9
;
652 iconv_close(ucs_to_wchar_conv
);
653 iconv_close(wchar_to_ucs_conv
);
654 iconv_close(font_conv
);
657 static void curses_setup(void)
659 int i
, colour_default
[8] = {
660 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
661 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
662 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
663 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
664 [QEMU_COLOR_RED
] = COLOR_RED
,
665 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
666 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
667 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
670 /* input as raw as possible, let everything be interpreted
671 * by the guest system */
672 initscr(); noecho(); intrflush(stdscr
, FALSE
);
673 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
674 start_color(); raw(); scrollok(stdscr
, FALSE
);
677 /* Make color pair to match color format (3bits bg:3bits fg) */
678 for (i
= 0; i
< 64; i
++) {
679 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
681 /* Set default color for more than 64 for safety. */
682 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
683 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
689 static void curses_keyboard_setup(void)
691 #if defined(__APPLE__)
692 /* always use generic keymaps */
693 if (!keyboard_layout
)
694 keyboard_layout
= "en-us";
696 if(keyboard_layout
) {
697 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
,
702 static const DisplayChangeListenerOps dcl_ops
= {
703 .dpy_name
= "curses",
704 .dpy_text_update
= curses_update
,
705 .dpy_text_resize
= curses_resize
,
706 .dpy_refresh
= curses_refresh
,
707 .dpy_text_cursor
= curses_cursor_position
,
710 static void curses_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
714 fprintf(stderr
, "We need a terminal output\n");
719 setlocale(LC_CTYPE
, "");
720 if (opts
->u
.curses
.charset
) {
721 font_charset
= opts
->u
.curses
.charset
;
724 curses_keyboard_setup();
725 atexit(curses_atexit
);
729 dcl
= g_new0(DisplayChangeListener
, 1);
731 register_displaychangelistener(dcl
);
736 static QemuDisplay qemu_display_curses
= {
737 .type
= DISPLAY_TYPE_CURSES
,
738 .init
= curses_display_init
,
741 static void register_curses(void)
743 qemu_display_register(&qemu_display_curses
);
746 type_init(register_curses
);