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"
28 #include <sys/ioctl.h>
32 #include "qemu-common.h"
33 #include "ui/console.h"
35 #include "sysemu/sysemu.h"
37 #define FONT_HEIGHT 16
40 static DisplayChangeListener
*dcl
;
41 static console_ch_t screen
[160 * 100];
42 static WINDOW
*screenpad
= NULL
;
43 static int width
, height
, gwidth
, gheight
, invalidate
;
44 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
46 chtype vga_to_curses
[256];
48 static void curses_update(DisplayChangeListener
*dcl
,
49 int x
, int y
, int w
, int h
)
53 line
= ((chtype
*) screen
) + y
* width
;
54 for (h
+= y
; y
< h
; y
++, line
+= width
)
55 mvwaddchnstr(screenpad
, y
, 0, line
, width
);
57 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
61 static void curses_calc_pad(void)
63 if (qemu_console_is_fixedsize(NULL
)) {
77 screenpad
= newpad(height
, width
);
80 px
= (width
- COLS
) / 2;
85 sminx
= (COLS
- width
) / 2;
86 smaxx
= sminx
+ width
;
90 py
= (height
- LINES
) / 2;
95 sminy
= (LINES
- height
) / 2;
96 smaxy
= sminy
+ height
;
100 static void curses_resize(DisplayChangeListener
*dcl
,
101 int width
, int height
)
103 if (width
== gwidth
&& height
== gheight
) {
113 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
114 static volatile sig_atomic_t got_sigwinch
;
115 static void curses_winch_check(void)
118 unsigned short ws_row
;
119 unsigned short ws_col
;
120 unsigned short ws_xpixel
; /* unused */
121 unsigned short ws_ypixel
; /* unused */
127 got_sigwinch
= false;
129 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
133 resize_term(ws
.ws_row
, ws
.ws_col
);
137 static void curses_winch_handler(int signum
)
142 static void curses_winch_init(void)
144 struct sigaction old
, winch
= {
145 .sa_handler
= curses_winch_handler
,
147 sigaction(SIGWINCH
, &winch
, &old
);
150 static void curses_winch_check(void) {}
151 static void curses_winch_init(void) {}
154 static void curses_cursor_position(DisplayChangeListener
*dcl
,
161 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
164 /* it seems that curs_set(1) must always be called before
165 * curs_set(2) for the latter to have effect */
166 if (!qemu_console_is_graphic(NULL
)) {
176 /* generic keyboard conversion */
178 #include "curses_keys.h"
180 static kbd_layout_t
*kbd_layout
= NULL
;
182 static void curses_refresh(DisplayChangeListener
*dcl
)
184 int chr
, nextchr
, keysym
, keycode
, keycode_alt
;
186 curses_winch_check();
192 graphic_hw_invalidate(NULL
);
196 graphic_hw_text_update(NULL
, screen
);
200 /* while there are any pending key strokes to process */
212 /* this shouldn't occur when we use a custom SIGWINCH handler */
213 if (chr
== KEY_RESIZE
) {
217 curses_update(dcl
, 0, 0, width
, height
);
222 keycode
= curses2keycode
[chr
];
229 if (nextchr
!= ERR
) {
232 keycode
= curses2keycode
[nextchr
];
238 /* process keys reserved for qemu */
239 if (keycode
>= QEMU_KEY_CONSOLE0
&&
240 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
242 wnoutrefresh(stdscr
);
243 console_select(keycode
- QEMU_KEY_CONSOLE0
);
254 if (chr
< CURSES_KEYS
)
255 keysym
= curses2keysym
[chr
];
260 if (keysym
>= 'A' && keysym
<= 'Z')
262 keysym
|= KEYSYM_CNTRL
;
267 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
);
271 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
272 keycode
|= keycode_alt
;
278 if (qemu_console_is_graphic(NULL
)) {
279 /* since terminals don't know about key press and release
280 * events, we need to emit both for each key received */
281 if (keycode
& SHIFT
) {
282 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
283 qemu_input_event_send_key_delay(0);
285 if (keycode
& CNTRL
) {
286 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
287 qemu_input_event_send_key_delay(0);
290 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
291 qemu_input_event_send_key_delay(0);
293 if (keycode
& ALTGR
) {
294 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
295 qemu_input_event_send_key_delay(0);
298 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
299 qemu_input_event_send_key_delay(0);
300 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
301 qemu_input_event_send_key_delay(0);
303 if (keycode
& ALTGR
) {
304 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
305 qemu_input_event_send_key_delay(0);
308 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
309 qemu_input_event_send_key_delay(0);
311 if (keycode
& CNTRL
) {
312 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
313 qemu_input_event_send_key_delay(0);
315 if (keycode
& SHIFT
) {
316 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
317 qemu_input_event_send_key_delay(0);
320 keysym
= curses2qemu
[chr
];
324 kbd_put_keysym(keysym
);
329 static void curses_atexit(void)
334 static void curses_setup(void)
336 int i
, colour_default
[8] = {
337 [QEMU_COLOR_BLACK
] = COLOR_BLACK
,
338 [QEMU_COLOR_BLUE
] = COLOR_BLUE
,
339 [QEMU_COLOR_GREEN
] = COLOR_GREEN
,
340 [QEMU_COLOR_CYAN
] = COLOR_CYAN
,
341 [QEMU_COLOR_RED
] = COLOR_RED
,
342 [QEMU_COLOR_MAGENTA
] = COLOR_MAGENTA
,
343 [QEMU_COLOR_YELLOW
] = COLOR_YELLOW
,
344 [QEMU_COLOR_WHITE
] = COLOR_WHITE
,
347 /* input as raw as possible, let everything be interpreted
348 * by the guest system */
349 initscr(); noecho(); intrflush(stdscr
, FALSE
);
350 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
351 start_color(); raw(); scrollok(stdscr
, FALSE
);
353 /* Make color pair to match color format (3bits bg:3bits fg) */
354 for (i
= 0; i
< 64; i
++) {
355 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
357 /* Set default color for more than 64 for safety. */
358 for (i
= 64; i
< COLOR_PAIRS
; i
++) {
359 init_pair(i
, COLOR_WHITE
, COLOR_BLACK
);
363 * Setup mapping for vga to curses line graphics.
364 * FIXME: for better font, have to use ncursesw and setlocale()
367 /* FIXME: map from where? */
373 /* ACS_* is not constant. So, we can't initialize statically. */
374 vga_to_curses
['\0'] = ' ';
375 vga_to_curses
[0x04] = ACS_DIAMOND
;
376 vga_to_curses
[0x0a] = ACS_RARROW
;
377 vga_to_curses
[0x0b] = ACS_LARROW
;
378 vga_to_curses
[0x18] = ACS_UARROW
;
379 vga_to_curses
[0x19] = ACS_DARROW
;
380 vga_to_curses
[0x9c] = ACS_STERLING
;
381 vga_to_curses
[0xb0] = ACS_BOARD
;
382 vga_to_curses
[0xb1] = ACS_CKBOARD
;
383 vga_to_curses
[0xb3] = ACS_VLINE
;
384 vga_to_curses
[0xb4] = ACS_RTEE
;
385 vga_to_curses
[0xbf] = ACS_URCORNER
;
386 vga_to_curses
[0xc0] = ACS_LLCORNER
;
387 vga_to_curses
[0xc1] = ACS_BTEE
;
388 vga_to_curses
[0xc2] = ACS_TTEE
;
389 vga_to_curses
[0xc3] = ACS_LTEE
;
390 vga_to_curses
[0xc4] = ACS_HLINE
;
391 vga_to_curses
[0xc5] = ACS_PLUS
;
392 vga_to_curses
[0xce] = ACS_LANTERN
;
393 vga_to_curses
[0xd8] = ACS_NEQUAL
;
394 vga_to_curses
[0xd9] = ACS_LRCORNER
;
395 vga_to_curses
[0xda] = ACS_ULCORNER
;
396 vga_to_curses
[0xdb] = ACS_BLOCK
;
397 vga_to_curses
[0xe3] = ACS_PI
;
398 vga_to_curses
[0xf1] = ACS_PLMINUS
;
399 vga_to_curses
[0xf2] = ACS_GEQUAL
;
400 vga_to_curses
[0xf3] = ACS_LEQUAL
;
401 vga_to_curses
[0xf8] = ACS_DEGREE
;
402 vga_to_curses
[0xfe] = ACS_BULLET
;
405 static void curses_keyboard_setup(void)
407 #if defined(__APPLE__)
408 /* always use generic keymaps */
409 if (!keyboard_layout
)
410 keyboard_layout
= "en-us";
412 if(keyboard_layout
) {
413 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
419 static const DisplayChangeListenerOps dcl_ops
= {
420 .dpy_name
= "curses",
421 .dpy_text_update
= curses_update
,
422 .dpy_text_resize
= curses_resize
,
423 .dpy_refresh
= curses_refresh
,
424 .dpy_text_cursor
= curses_cursor_position
,
427 void curses_display_init(DisplayState
*ds
, int full_screen
)
431 fprintf(stderr
, "We need a terminal output\n");
437 curses_keyboard_setup();
438 atexit(curses_atexit
);
442 dcl
= g_new0(DisplayChangeListener
, 1);
444 register_displaychangelistener(dcl
);