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>
31 #include "qapi/error.h"
32 #include "qemu-common.h"
33 #include "ui/console.h"
35 #include "sysemu/sysemu.h"
37 /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
42 #define FONT_HEIGHT 16
48 CURSES_CHAR_OR_KEYCODE
,
51 static DisplayChangeListener
*dcl
;
52 static console_ch_t screen
[160 * 100];
53 static WINDOW
*screenpad
= NULL
;
54 static int width
, height
, gwidth
, gheight
, invalidate
;
55 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
57 static chtype vga_to_curses
[256];
59 static void curses_update(DisplayChangeListener
*dcl
,
60 int x
, int y
, int w
, int h
)
63 chtype curses_line
[width
];
65 line
= screen
+ y
* width
;
66 for (h
+= y
; y
< h
; y
++, line
+= width
) {
67 for (x
= 0; x
< width
; x
++) {
68 chtype ch
= line
[x
] & 0xff;
69 chtype at
= line
[x
] & ~0xff;
70 if (vga_to_curses
[ch
]) {
71 ch
= vga_to_curses
[ch
];
73 curses_line
[x
] = ch
| at
;
75 mvwaddchnstr(screenpad
, y
, 0, curses_line
, width
);
78 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
82 static void curses_calc_pad(void)
84 if (qemu_console_is_fixedsize(NULL
)) {
98 screenpad
= newpad(height
, width
);
101 px
= (width
- COLS
) / 2;
106 sminx
= (COLS
- width
) / 2;
107 smaxx
= sminx
+ width
;
110 if (height
> LINES
) {
111 py
= (height
- LINES
) / 2;
116 sminy
= (LINES
- height
) / 2;
117 smaxy
= sminy
+ height
;
121 static void curses_resize(DisplayChangeListener
*dcl
,
122 int width
, int height
)
124 if (width
== gwidth
&& height
== gheight
) {
134 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
135 static volatile sig_atomic_t got_sigwinch
;
136 static void curses_winch_check(void)
139 unsigned short ws_row
;
140 unsigned short ws_col
;
141 unsigned short ws_xpixel
; /* unused */
142 unsigned short ws_ypixel
; /* unused */
148 got_sigwinch
= false;
150 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
154 resize_term(ws
.ws_row
, ws
.ws_col
);
158 static void curses_winch_handler(int signum
)
163 static void curses_winch_init(void)
165 struct sigaction old
, winch
= {
166 .sa_handler
= curses_winch_handler
,
168 sigaction(SIGWINCH
, &winch
, &old
);
171 static void curses_winch_check(void) {}
172 static void curses_winch_init(void) {}
175 static void curses_cursor_position(DisplayChangeListener
*dcl
,
182 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
185 /* it seems that curs_set(1) must always be called before
186 * curs_set(2) for the latter to have effect */
187 if (!qemu_console_is_graphic(NULL
)) {
197 /* generic keyboard conversion */
199 #include "curses_keys.h"
201 static kbd_layout_t
*kbd_layout
= NULL
;
203 static wint_t console_getch(enum maybe_keycode
*maybe_keycode
)
206 switch (get_wch(&ret
)) {
208 *maybe_keycode
= CURSES_KEYCODE
;
211 *maybe_keycode
= CURSES_CHAR
;
220 static int curses2foo(const int _curses2foo
[], const int _curseskey2foo
[],
221 int chr
, enum maybe_keycode maybe_keycode
)
224 if (maybe_keycode
== CURSES_CHAR
) {
225 if (chr
< CURSES_CHARS
) {
226 ret
= _curses2foo
[chr
];
229 if (chr
< CURSES_KEYS
) {
230 ret
= _curseskey2foo
[chr
];
232 if (ret
== -1 && maybe_keycode
== CURSES_CHAR_OR_KEYCODE
&&
233 chr
< CURSES_CHARS
) {
234 ret
= _curses2foo
[chr
];
240 #define curses2keycode(chr, maybe_keycode) \
241 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
242 #define curses2keysym(chr, maybe_keycode) \
243 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
244 #define curses2qemu(chr, maybe_keycode) \
245 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
247 static void curses_refresh(DisplayChangeListener
*dcl
)
249 int chr
, keysym
, keycode
, keycode_alt
;
250 enum maybe_keycode maybe_keycode
;
252 curses_winch_check();
258 graphic_hw_invalidate(NULL
);
262 graphic_hw_text_update(NULL
, screen
);
265 /* while there are any pending key strokes to process */
266 chr
= console_getch(&maybe_keycode
);
272 /* this shouldn't occur when we use a custom SIGWINCH handler */
273 if (maybe_keycode
!= CURSES_CHAR
&& chr
== KEY_RESIZE
) {
277 curses_update(dcl
, 0, 0, width
, height
);
282 keycode
= curses2keycode(chr
, maybe_keycode
);
287 enum maybe_keycode next_maybe_keycode
;
288 int nextchr
= console_getch(&next_maybe_keycode
);
292 maybe_keycode
= next_maybe_keycode
;
294 keycode
= curses2keycode(chr
, maybe_keycode
);
299 /* process keys reserved for qemu */
300 if (keycode
>= QEMU_KEY_CONSOLE0
&&
301 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
303 wnoutrefresh(stdscr
);
304 console_select(keycode
- QEMU_KEY_CONSOLE0
);
314 keysym
= curses2keysym(chr
, maybe_keycode
);
319 if (keysym
>= 'A' && keysym
<= 'Z')
321 keysym
|= KEYSYM_CNTRL
;
326 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
,
331 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
332 keycode
|= keycode_alt
;
338 if (qemu_console_is_graphic(NULL
)) {
339 /* since terminals don't know about key press and release
340 * events, we need to emit both for each key received */
341 if (keycode
& SHIFT
) {
342 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
343 qemu_input_event_send_key_delay(0);
345 if (keycode
& CNTRL
) {
346 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
347 qemu_input_event_send_key_delay(0);
350 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
351 qemu_input_event_send_key_delay(0);
353 if (keycode
& ALTGR
) {
354 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
355 qemu_input_event_send_key_delay(0);
358 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
359 qemu_input_event_send_key_delay(0);
360 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
361 qemu_input_event_send_key_delay(0);
363 if (keycode
& ALTGR
) {
364 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
365 qemu_input_event_send_key_delay(0);
368 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
369 qemu_input_event_send_key_delay(0);
371 if (keycode
& CNTRL
) {
372 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
373 qemu_input_event_send_key_delay(0);
375 if (keycode
& SHIFT
) {
376 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
377 qemu_input_event_send_key_delay(0);
380 keysym
= curses2qemu(chr
, maybe_keycode
);
384 kbd_put_keysym(keysym
);
389 static void curses_atexit(void)
394 static void curses_setup(void)
396 int i
, colour_default
[8] = {
397 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
398 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
399 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
400 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
401 [QEMU_COLOR_RED
] = COLOR_RED
,
402 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
403 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
404 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
407 /* input as raw as possible, let everything be interpreted
408 * by the guest system */
409 initscr(); noecho(); intrflush(stdscr
, FALSE
);
410 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
411 start_color(); raw(); scrollok(stdscr
, FALSE
);
414 /* Make color pair to match color format (3bits bg:3bits fg) */
415 for (i
= 0; i
< 64; i
++) {
416 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
418 /* Set default color for more than 64 for safety. */
419 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
420 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
424 * Setup mapping for vga to curses line graphics.
425 * FIXME: for better font, have to use ncursesw and setlocale()
428 /* FIXME: map from where? */
434 /* ACS_* is not constant. So, we can't initialize statically. */
435 vga_to_curses
['\0'] = ' ';
436 vga_to_curses
[0x04] = ACS_DIAMOND
;
437 vga_to_curses
[0x18] = ACS_UARROW
;
438 vga_to_curses
[0x19] = ACS_DARROW
;
439 vga_to_curses
[0x1a] = ACS_RARROW
;
440 vga_to_curses
[0x1b] = ACS_LARROW
;
441 vga_to_curses
[0x9c] = ACS_STERLING
;
442 vga_to_curses
[0xb0] = ACS_BOARD
;
443 vga_to_curses
[0xb1] = ACS_CKBOARD
;
444 vga_to_curses
[0xb3] = ACS_VLINE
;
445 vga_to_curses
[0xb4] = ACS_RTEE
;
446 vga_to_curses
[0xbf] = ACS_URCORNER
;
447 vga_to_curses
[0xc0] = ACS_LLCORNER
;
448 vga_to_curses
[0xc1] = ACS_BTEE
;
449 vga_to_curses
[0xc2] = ACS_TTEE
;
450 vga_to_curses
[0xc3] = ACS_LTEE
;
451 vga_to_curses
[0xc4] = ACS_HLINE
;
452 vga_to_curses
[0xc5] = ACS_PLUS
;
453 vga_to_curses
[0xce] = ACS_LANTERN
;
454 vga_to_curses
[0xd8] = ACS_NEQUAL
;
455 vga_to_curses
[0xd9] = ACS_LRCORNER
;
456 vga_to_curses
[0xda] = ACS_ULCORNER
;
457 vga_to_curses
[0xdb] = ACS_BLOCK
;
458 vga_to_curses
[0xe3] = ACS_PI
;
459 vga_to_curses
[0xf1] = ACS_PLMINUS
;
460 vga_to_curses
[0xf2] = ACS_GEQUAL
;
461 vga_to_curses
[0xf3] = ACS_LEQUAL
;
462 vga_to_curses
[0xf8] = ACS_DEGREE
;
463 vga_to_curses
[0xfe] = ACS_BULLET
;
466 static void curses_keyboard_setup(void)
468 #if defined(__APPLE__)
469 /* always use generic keymaps */
470 if (!keyboard_layout
)
471 keyboard_layout
= "en-us";
473 if(keyboard_layout
) {
474 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
,
479 static const DisplayChangeListenerOps dcl_ops
= {
480 .dpy_name
= "curses",
481 .dpy_text_update
= curses_update
,
482 .dpy_text_resize
= curses_resize
,
483 .dpy_refresh
= curses_refresh
,
484 .dpy_text_cursor
= curses_cursor_position
,
487 static void curses_display_init(DisplayState
*ds
, DisplayOptions
*opts
)
491 fprintf(stderr
, "We need a terminal output\n");
497 curses_keyboard_setup();
498 atexit(curses_atexit
);
502 dcl
= g_new0(DisplayChangeListener
, 1);
504 register_displaychangelistener(dcl
);
509 static QemuDisplay qemu_display_curses
= {
510 .type
= DISPLAY_TYPE_CURSES
,
511 .init
= curses_display_init
,
514 static void register_curses(void)
516 qemu_display_register(&qemu_display_curses
);
519 type_init(register_curses
);