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"
33 #include "sysemu/sysemu.h"
35 #define FONT_HEIGHT 16
38 static DisplayChangeListener
*dcl
;
39 static console_ch_t screen
[160 * 100];
40 static WINDOW
*screenpad
= NULL
;
41 static int width
, height
, gwidth
, gheight
, invalidate
;
42 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
44 static void curses_update(DisplayChangeListener
*dcl
,
45 int x
, int y
, int w
, int h
)
49 line
= ((chtype
*) screen
) + y
* width
;
50 for (h
+= y
; y
< h
; y
++, line
+= width
)
51 mvwaddchnstr(screenpad
, y
, 0, line
, width
);
53 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
57 static void curses_calc_pad(void)
59 if (qemu_console_is_fixedsize(NULL
)) {
73 screenpad
= newpad(height
, width
);
76 px
= (width
- COLS
) / 2;
81 sminx
= (COLS
- width
) / 2;
82 smaxx
= sminx
+ width
;
86 py
= (height
- LINES
) / 2;
91 sminy
= (LINES
- height
) / 2;
92 smaxy
= sminy
+ height
;
96 static void curses_resize(DisplayChangeListener
*dcl
,
97 int width
, int height
)
99 if (width
== gwidth
&& height
== gheight
) {
110 #if defined(SIGWINCH) && defined(KEY_RESIZE)
111 static void curses_winch_handler(int signum
)
114 unsigned short ws_row
;
115 unsigned short ws_col
;
116 unsigned short ws_xpixel
; /* unused */
117 unsigned short ws_ypixel
; /* unused */
120 /* terminal size changed */
121 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1)
124 resize_term(ws
.ws_row
, ws
.ws_col
);
128 /* some systems require this */
129 signal(SIGWINCH
, curses_winch_handler
);
134 static void curses_cursor_position(DisplayChangeListener
*dcl
,
141 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
144 /* it seems that curs_set(1) must always be called before
145 * curs_set(2) for the latter to have effect */
146 if (!qemu_console_is_graphic(NULL
)) {
156 /* generic keyboard conversion */
158 #include "curses_keys.h"
160 static kbd_layout_t
*kbd_layout
= NULL
;
162 static void curses_refresh(DisplayChangeListener
*dcl
)
164 int chr
, nextchr
, keysym
, keycode
, keycode_alt
;
170 graphic_hw_invalidate(NULL
);
174 graphic_hw_text_update(NULL
, screen
);
178 /* while there are any pending key strokes to process */
190 /* this shouldn't occur when we use a custom SIGWINCH handler */
191 if (chr
== KEY_RESIZE
) {
195 curses_update(dcl
, 0, 0, width
, height
);
200 keycode
= curses2keycode
[chr
];
207 if (nextchr
!= ERR
) {
210 keycode
= curses2keycode
[nextchr
];
216 /* process keys reserved for qemu */
217 if (keycode
>= QEMU_KEY_CONSOLE0
&&
218 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
220 wnoutrefresh(stdscr
);
221 console_select(keycode
- QEMU_KEY_CONSOLE0
);
232 if (chr
< CURSES_KEYS
)
233 keysym
= curses2keysym
[chr
];
238 if (keysym
>= 'A' && keysym
<= 'Z')
240 keysym
|= KEYSYM_CNTRL
;
245 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
);
249 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
250 keycode
|= keycode_alt
;
256 if (qemu_console_is_graphic(NULL
)) {
257 /* since terminals don't know about key press and release
258 * events, we need to emit both for each key received */
260 kbd_put_keycode(SHIFT_CODE
);
262 kbd_put_keycode(CNTRL_CODE
);
264 kbd_put_keycode(ALT_CODE
);
265 if (keycode
& ALTGR
) {
266 kbd_put_keycode(SCANCODE_EMUL0
);
267 kbd_put_keycode(ALT_CODE
);
270 kbd_put_keycode(GREY_CODE
);
271 kbd_put_keycode(keycode
& KEY_MASK
);
273 kbd_put_keycode(GREY_CODE
);
274 kbd_put_keycode((keycode
& KEY_MASK
) | KEY_RELEASE
);
275 if (keycode
& ALTGR
) {
276 kbd_put_keycode(SCANCODE_EMUL0
);
277 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
280 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
282 kbd_put_keycode(CNTRL_CODE
| KEY_RELEASE
);
284 kbd_put_keycode(SHIFT_CODE
| KEY_RELEASE
);
286 keysym
= curses2qemu
[chr
];
290 kbd_put_keysym(keysym
);
295 static void curses_atexit(void)
300 static void curses_setup(void)
302 int i
, colour_default
[8] = {
303 COLOR_BLACK
, COLOR_BLUE
, COLOR_GREEN
, COLOR_CYAN
,
304 COLOR_RED
, COLOR_MAGENTA
, COLOR_YELLOW
, COLOR_WHITE
,
307 /* input as raw as possible, let everything be interpreted
308 * by the guest system */
309 initscr(); noecho(); intrflush(stdscr
, FALSE
);
310 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
311 start_color(); raw(); scrollok(stdscr
, FALSE
);
313 for (i
= 0; i
< 64; i
++)
314 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
317 static void curses_keyboard_setup(void)
319 #if defined(__APPLE__)
320 /* always use generic keymaps */
321 if (!keyboard_layout
)
322 keyboard_layout
= "en-us";
324 if(keyboard_layout
) {
325 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
331 static const DisplayChangeListenerOps dcl_ops
= {
332 .dpy_name
= "curses",
333 .dpy_text_update
= curses_update
,
334 .dpy_text_resize
= curses_resize
,
335 .dpy_refresh
= curses_refresh
,
336 .dpy_text_cursor
= curses_cursor_position
,
339 void curses_display_init(DisplayState
*ds
, int full_screen
)
343 fprintf(stderr
, "We need a terminal output\n");
349 curses_keyboard_setup();
350 atexit(curses_atexit
);
353 #if defined(SIGWINCH) && defined(KEY_RESIZE)
354 /* some curses implementations provide a handler, but we
355 * want to be sure this is handled regardless of the library */
356 signal(SIGWINCH
, curses_winch_handler
);
360 dcl
= (DisplayChangeListener
*) g_malloc0(sizeof(DisplayChangeListener
));
362 register_displaychangelistener(dcl
);