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
25 #include "qemu-common.h"
33 #include <sys/ioctl.h>
38 #define resize_term resizeterm
41 #define FONT_HEIGHT 16
44 static console_ch_t screen
[160 * 100];
45 static WINDOW
*screenpad
= NULL
;
46 static int width
, height
, gwidth
, gheight
, invalidate
;
47 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
49 static void curses_update(DisplayState
*ds
, 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 (is_fixedsize_console()) {
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(DisplayState
*ds
)
102 if (ds_get_width(ds
) == gwidth
&& ds_get_height(ds
) == gheight
)
105 gwidth
= ds_get_width(ds
);
106 gheight
= ds_get_height(ds
);
109 ds
->surface
->width
= width
* FONT_WIDTH
;
110 ds
->surface
->height
= height
* FONT_HEIGHT
;
114 #if defined(SIGWINCH) && defined(KEY_RESIZE)
115 static void curses_winch_handler(int signum
)
118 unsigned short ws_row
;
119 unsigned short ws_col
;
120 unsigned short ws_xpixel
; /* unused */
121 unsigned short ws_ypixel
; /* unused */
124 /* terminal size changed */
125 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1)
128 resize_term(ws
.ws_row
, ws
.ws_col
);
132 /* some systems require this */
133 signal(SIGWINCH
, curses_winch_handler
);
138 static void curses_cursor_position(DisplayState
*ds
, int x
, int y
)
144 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
147 /* it seems that curs_set(1) must always be called before
148 * curs_set(2) for the latter to have effect */
149 if (!is_graphic_console())
158 /* generic keyboard conversion */
160 #include "curses_keys.h"
162 static kbd_layout_t
*kbd_layout
= 0;
163 static int keycode2keysym
[CURSES_KEYS
];
165 static void curses_refresh(DisplayState
*ds
)
167 int chr
, nextchr
, keysym
, keycode
;
173 ds
->surface
->width
= FONT_WIDTH
* width
;
174 ds
->surface
->height
= FONT_HEIGHT
* height
;
179 vga_hw_text_update(screen
);
183 /* while there are any pending key strokes to process */
195 /* this shouldn't occur when we use a custom SIGWINCH handler */
196 if (chr
== KEY_RESIZE
) {
200 curses_update(ds
, 0, 0, width
, height
);
201 ds
->surface
->width
= FONT_WIDTH
* width
;
202 ds
->surface
->height
= FONT_HEIGHT
* height
;
207 keycode
= curses2keycode
[chr
];
215 if (nextchr
!= ERR
) {
216 keycode
= curses2keycode
[nextchr
];
223 /* process keys reserved for qemu */
224 if (keycode
>= QEMU_KEY_CONSOLE0
&&
225 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
227 wnoutrefresh(stdscr
);
228 console_select(keycode
- QEMU_KEY_CONSOLE0
);
236 if (kbd_layout
&& !(keycode
& GREY
)) {
237 keysym
= keycode2keysym
[keycode
& KEY_MASK
];
241 keycode
&= ~KEY_MASK
;
242 keycode
|= keysym2scancode(kbd_layout
, keysym
);
245 if (is_graphic_console()) {
246 /* since terminals don't know about key press and release
247 * events, we need to emit both for each key received */
249 kbd_put_keycode(SHIFT_CODE
);
251 kbd_put_keycode(CNTRL_CODE
);
253 kbd_put_keycode(ALT_CODE
);
255 kbd_put_keycode(GREY_CODE
);
256 kbd_put_keycode(keycode
& KEY_MASK
);
258 kbd_put_keycode(GREY_CODE
);
259 kbd_put_keycode((keycode
& KEY_MASK
) | KEY_RELEASE
);
261 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
263 kbd_put_keycode(CNTRL_CODE
| KEY_RELEASE
);
265 kbd_put_keycode(SHIFT_CODE
| KEY_RELEASE
);
267 keysym
= curses2keysym
[chr
];
271 kbd_put_keysym(keysym
);
276 static void curses_cleanup(void *opaque
)
281 static void curses_atexit(void)
283 curses_cleanup(NULL
);
286 static void curses_setup(void)
288 int i
, colour_default
[8] = {
289 COLOR_BLACK
, COLOR_BLUE
, COLOR_GREEN
, COLOR_CYAN
,
290 COLOR_RED
, COLOR_MAGENTA
, COLOR_YELLOW
, COLOR_WHITE
,
293 /* input as raw as possible, let everything be interpreted
294 * by the guest system */
295 initscr(); noecho(); intrflush(stdscr
, FALSE
);
296 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
297 start_color(); raw(); scrollok(stdscr
, FALSE
);
299 for (i
= 0; i
< 64; i
++)
300 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
303 static void curses_keyboard_setup(void)
305 int i
, keycode
, keysym
;
307 #if defined(__APPLE__)
308 /* always use generic keymaps */
309 if (!keyboard_layout
)
310 keyboard_layout
= "en-us";
312 if(keyboard_layout
) {
313 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
318 for (i
= 0; i
< CURSES_KEYS
; i
++)
319 keycode2keysym
[i
] = -1;
321 for (i
= 0; i
< CURSES_KEYS
; i
++) {
322 if (curses2keycode
[i
] == -1)
325 keycode
= curses2keycode
[i
] & KEY_MASK
;
326 if (keycode2keysym
[keycode
] >= 0)
329 for (keysym
= 0; keysym
< CURSES_KEYS
; keysym
++)
330 if (curses2keycode
[keysym
] == keycode
) {
331 keycode2keysym
[keycode
] = keysym
;
335 if (keysym
>= CURSES_KEYS
)
336 keycode2keysym
[keycode
] = i
;
340 void curses_display_init(DisplayState
*ds
, int full_screen
)
342 DisplayChangeListener
*dcl
;
345 fprintf(stderr
, "We need a terminal output\n");
351 curses_keyboard_setup();
352 atexit(curses_atexit
);
355 #if defined(SIGWINCH) && defined(KEY_RESIZE)
356 /* some curses implementations provide a handler, but we
357 * want to be sure this is handled regardless of the library */
358 signal(SIGWINCH
, curses_winch_handler
);
362 dcl
= (DisplayChangeListener
*) qemu_mallocz(sizeof(DisplayChangeListener
));
363 dcl
->dpy_update
= curses_update
;
364 dcl
->dpy_resize
= curses_resize
;
365 dcl
->dpy_refresh
= curses_refresh
;
366 dcl
->dpy_text_cursor
= curses_cursor_position
;
367 register_displaychangelistener(ds
, dcl
);
368 qemu_free_displaysurface(ds
->surface
);
369 ds
->surface
= qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen
);
373 /* Standard VGA initial text mode dimensions */