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>
32 #define resize_term resizeterm
35 #include "qemu-common.h"
39 #define FONT_HEIGHT 16
42 static console_ch_t screen
[160 * 100];
43 static WINDOW
*screenpad
= NULL
;
44 static int width
, height
, gwidth
, gheight
, invalidate
;
45 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
47 static void curses_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
51 line
= ((chtype
*) screen
) + y
* width
;
52 for (h
+= y
; y
< h
; y
++, line
+= width
)
53 mvwaddchnstr(screenpad
, y
, 0, line
, width
);
55 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
59 static void curses_calc_pad(void)
61 if (is_fixedsize_console()) {
75 screenpad
= newpad(height
, width
);
78 px
= (width
- COLS
) / 2;
83 sminx
= (COLS
- width
) / 2;
84 smaxx
= sminx
+ width
;
88 py
= (height
- LINES
) / 2;
93 sminy
= (LINES
- height
) / 2;
94 smaxy
= sminy
+ height
;
98 static void curses_resize(DisplayState
*ds
)
100 if (ds_get_width(ds
) == gwidth
&& ds_get_height(ds
) == gheight
)
103 gwidth
= ds_get_width(ds
);
104 gheight
= ds_get_height(ds
);
107 ds
->surface
->width
= width
* FONT_WIDTH
;
108 ds
->surface
->height
= height
* FONT_HEIGHT
;
112 #if defined(SIGWINCH) && defined(KEY_RESIZE)
113 static void curses_winch_handler(int signum
)
116 unsigned short ws_row
;
117 unsigned short ws_col
;
118 unsigned short ws_xpixel
; /* unused */
119 unsigned short ws_ypixel
; /* unused */
122 /* terminal size changed */
123 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1)
126 resize_term(ws
.ws_row
, ws
.ws_col
);
130 /* some systems require this */
131 signal(SIGWINCH
, curses_winch_handler
);
136 static void curses_cursor_position(DisplayState
*ds
, int x
, int y
)
142 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
145 /* it seems that curs_set(1) must always be called before
146 * curs_set(2) for the latter to have effect */
147 if (!is_graphic_console())
156 /* generic keyboard conversion */
158 #include "curses_keys.h"
160 static kbd_layout_t
*kbd_layout
= NULL
;
162 static void curses_refresh(DisplayState
*ds
)
164 int chr
, nextchr
, keysym
, keycode
, keycode_alt
;
170 ds
->surface
->width
= FONT_WIDTH
* width
;
171 ds
->surface
->height
= FONT_HEIGHT
* height
;
176 vga_hw_text_update(screen
);
180 /* while there are any pending key strokes to process */
192 /* this shouldn't occur when we use a custom SIGWINCH handler */
193 if (chr
== KEY_RESIZE
) {
197 curses_update(ds
, 0, 0, width
, height
);
198 ds
->surface
->width
= FONT_WIDTH
* width
;
199 ds
->surface
->height
= FONT_HEIGHT
* height
;
204 keycode
= curses2keycode
[chr
];
211 if (nextchr
!= ERR
) {
214 keycode
= curses2keycode
[nextchr
];
220 /* process keys reserved for qemu */
221 if (keycode
>= QEMU_KEY_CONSOLE0
&&
222 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
224 wnoutrefresh(stdscr
);
225 console_select(keycode
- QEMU_KEY_CONSOLE0
);
236 if (chr
< CURSES_KEYS
)
237 keysym
= curses2keysym
[chr
];
242 if (keysym
>= 'A' && keysym
<= 'Z')
244 keysym
|= KEYSYM_CNTRL
;
249 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
);
253 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
254 keycode
|= keycode_alt
;
260 if (is_graphic_console()) {
261 /* since terminals don't know about key press and release
262 * events, we need to emit both for each key received */
264 kbd_put_keycode(SHIFT_CODE
);
266 kbd_put_keycode(CNTRL_CODE
);
268 kbd_put_keycode(ALT_CODE
);
269 if (keycode
& ALTGR
) {
270 kbd_put_keycode(SCANCODE_EMUL0
);
271 kbd_put_keycode(ALT_CODE
);
274 kbd_put_keycode(GREY_CODE
);
275 kbd_put_keycode(keycode
& KEY_MASK
);
277 kbd_put_keycode(GREY_CODE
);
278 kbd_put_keycode((keycode
& KEY_MASK
) | KEY_RELEASE
);
279 if (keycode
& ALTGR
) {
280 kbd_put_keycode(SCANCODE_EMUL0
);
281 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
284 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
286 kbd_put_keycode(CNTRL_CODE
| KEY_RELEASE
);
288 kbd_put_keycode(SHIFT_CODE
| KEY_RELEASE
);
290 keysym
= curses2qemu
[chr
];
294 kbd_put_keysym(keysym
);
299 static void curses_atexit(void)
304 static void curses_setup(void)
306 int i
, colour_default
[8] = {
307 COLOR_BLACK
, COLOR_BLUE
, COLOR_GREEN
, COLOR_CYAN
,
308 COLOR_RED
, COLOR_MAGENTA
, COLOR_YELLOW
, COLOR_WHITE
,
311 /* input as raw as possible, let everything be interpreted
312 * by the guest system */
313 initscr(); noecho(); intrflush(stdscr
, FALSE
);
314 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
315 start_color(); raw(); scrollok(stdscr
, FALSE
);
317 for (i
= 0; i
< 64; i
++)
318 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
321 static void curses_keyboard_setup(void)
323 #if defined(__APPLE__)
324 /* always use generic keymaps */
325 if (!keyboard_layout
)
326 keyboard_layout
= "en-us";
328 if(keyboard_layout
) {
329 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
335 void curses_display_init(DisplayState
*ds
, int full_screen
)
337 DisplayChangeListener
*dcl
;
340 fprintf(stderr
, "We need a terminal output\n");
346 curses_keyboard_setup();
347 atexit(curses_atexit
);
350 #if defined(SIGWINCH) && defined(KEY_RESIZE)
351 /* some curses implementations provide a handler, but we
352 * want to be sure this is handled regardless of the library */
353 signal(SIGWINCH
, curses_winch_handler
);
357 dcl
= (DisplayChangeListener
*) qemu_mallocz(sizeof(DisplayChangeListener
));
358 dcl
->dpy_update
= curses_update
;
359 dcl
->dpy_resize
= curses_resize
;
360 dcl
->dpy_refresh
= curses_refresh
;
361 dcl
->dpy_text_cursor
= curses_cursor_position
;
362 register_displaychangelistener(ds
, dcl
);
363 qemu_free_displaysurface(ds
);
364 ds
->surface
= qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen
);