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
27 #include <sys/ioctl.h>
31 #include "qemu-common.h"
32 #include "ui/console.h"
34 #include "sysemu/sysemu.h"
36 #define FONT_HEIGHT 16
39 static DisplayChangeListener
*dcl
;
40 static console_ch_t screen
[160 * 100];
41 static WINDOW
*screenpad
= NULL
;
42 static int width
, height
, gwidth
, gheight
, invalidate
;
43 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
45 static void curses_update(DisplayChangeListener
*dcl
,
46 int x
, int y
, int w
, int h
)
50 line
= ((chtype
*) screen
) + y
* width
;
51 for (h
+= y
; y
< h
; y
++, line
+= width
)
52 mvwaddchnstr(screenpad
, y
, 0, line
, width
);
54 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
58 static void curses_calc_pad(void)
60 if (qemu_console_is_fixedsize(NULL
)) {
74 screenpad
= newpad(height
, width
);
77 px
= (width
- COLS
) / 2;
82 sminx
= (COLS
- width
) / 2;
83 smaxx
= sminx
+ width
;
87 py
= (height
- LINES
) / 2;
92 sminy
= (LINES
- height
) / 2;
93 smaxy
= sminy
+ height
;
97 static void curses_resize(DisplayChangeListener
*dcl
,
98 int width
, int height
)
100 if (width
== gwidth
&& height
== gheight
) {
110 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
111 static volatile sig_atomic_t got_sigwinch
;
112 static void curses_winch_check(void)
115 unsigned short ws_row
;
116 unsigned short ws_col
;
117 unsigned short ws_xpixel
; /* unused */
118 unsigned short ws_ypixel
; /* unused */
124 got_sigwinch
= false;
126 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1) {
130 resize_term(ws
.ws_row
, ws
.ws_col
);
134 static void curses_winch_handler(int signum
)
139 static void curses_winch_init(void)
141 struct sigaction old
, winch
= {
142 .sa_handler
= curses_winch_handler
,
144 sigaction(SIGWINCH
, &winch
, &old
);
147 static void curses_winch_check(void) {}
148 static void curses_winch_init(void) {}
151 static void curses_cursor_position(DisplayChangeListener
*dcl
,
158 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
161 /* it seems that curs_set(1) must always be called before
162 * curs_set(2) for the latter to have effect */
163 if (!qemu_console_is_graphic(NULL
)) {
173 /* generic keyboard conversion */
175 #include "curses_keys.h"
177 static kbd_layout_t
*kbd_layout
= NULL
;
179 static void curses_refresh(DisplayChangeListener
*dcl
)
181 int chr
, nextchr
, keysym
, keycode
, keycode_alt
;
183 curses_winch_check();
189 graphic_hw_invalidate(NULL
);
193 graphic_hw_text_update(NULL
, screen
);
197 /* while there are any pending key strokes to process */
209 /* this shouldn't occur when we use a custom SIGWINCH handler */
210 if (chr
== KEY_RESIZE
) {
214 curses_update(dcl
, 0, 0, width
, height
);
219 keycode
= curses2keycode
[chr
];
226 if (nextchr
!= ERR
) {
229 keycode
= curses2keycode
[nextchr
];
235 /* process keys reserved for qemu */
236 if (keycode
>= QEMU_KEY_CONSOLE0
&&
237 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
239 wnoutrefresh(stdscr
);
240 console_select(keycode
- QEMU_KEY_CONSOLE0
);
251 if (chr
< CURSES_KEYS
)
252 keysym
= curses2keysym
[chr
];
257 if (keysym
>= 'A' && keysym
<= 'Z')
259 keysym
|= KEYSYM_CNTRL
;
264 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
);
268 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
269 keycode
|= keycode_alt
;
275 if (qemu_console_is_graphic(NULL
)) {
276 /* since terminals don't know about key press and release
277 * events, we need to emit both for each key received */
278 if (keycode
& SHIFT
) {
279 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, true);
280 qemu_input_event_send_key_delay(0);
282 if (keycode
& CNTRL
) {
283 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, true);
284 qemu_input_event_send_key_delay(0);
287 qemu_input_event_send_key_number(NULL
, ALT_CODE
, true);
288 qemu_input_event_send_key_delay(0);
290 if (keycode
& ALTGR
) {
291 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, true);
292 qemu_input_event_send_key_delay(0);
295 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, true);
296 qemu_input_event_send_key_delay(0);
297 qemu_input_event_send_key_number(NULL
, keycode
& KEY_MASK
, false);
298 qemu_input_event_send_key_delay(0);
300 if (keycode
& ALTGR
) {
301 qemu_input_event_send_key_number(NULL
, GREY
| ALT_CODE
, false);
302 qemu_input_event_send_key_delay(0);
305 qemu_input_event_send_key_number(NULL
, ALT_CODE
, false);
306 qemu_input_event_send_key_delay(0);
308 if (keycode
& CNTRL
) {
309 qemu_input_event_send_key_number(NULL
, CNTRL_CODE
, false);
310 qemu_input_event_send_key_delay(0);
312 if (keycode
& SHIFT
) {
313 qemu_input_event_send_key_number(NULL
, SHIFT_CODE
, false);
314 qemu_input_event_send_key_delay(0);
317 keysym
= curses2qemu
[chr
];
321 kbd_put_keysym(keysym
);
326 static void curses_atexit(void)
331 static void curses_setup(void)
333 int i
, colour_default
[8] = {
334 COLOR_BLACK
, COLOR_BLUE
, COLOR_GREEN
, COLOR_CYAN
,
335 COLOR_RED
, COLOR_MAGENTA
, COLOR_YELLOW
, COLOR_WHITE
,
338 /* input as raw as possible, let everything be interpreted
339 * by the guest system */
340 initscr(); noecho(); intrflush(stdscr
, FALSE
);
341 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
342 start_color(); raw(); scrollok(stdscr
, FALSE
);
344 for (i
= 0; i
< 64; i
++)
345 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
348 static void curses_keyboard_setup(void)
350 #if defined(__APPLE__)
351 /* always use generic keymaps */
352 if (!keyboard_layout
)
353 keyboard_layout
= "en-us";
355 if(keyboard_layout
) {
356 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
362 static const DisplayChangeListenerOps dcl_ops
= {
363 .dpy_name
= "curses",
364 .dpy_text_update
= curses_update
,
365 .dpy_text_resize
= curses_resize
,
366 .dpy_refresh
= curses_refresh
,
367 .dpy_text_cursor
= curses_cursor_position
,
370 void curses_display_init(DisplayState
*ds
, int full_screen
)
374 fprintf(stderr
, "We need a terminal output\n");
380 curses_keyboard_setup();
381 atexit(curses_atexit
);
385 dcl
= (DisplayChangeListener
*) g_malloc0(sizeof(DisplayChangeListener
));
387 register_displaychangelistener(dcl
);