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
28 #include <sys/ioctl.h>
33 #define resize_term resizeterm
36 #include "qemu-common.h"
40 #define FONT_HEIGHT 16
43 static console_ch_t screen
[160 * 100];
44 static WINDOW
*screenpad
= NULL
;
45 static int width
, height
, gwidth
, gheight
, invalidate
;
46 static int px
, py
, sminx
, sminy
, smaxx
, smaxy
;
48 static void curses_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
52 line
= ((chtype
*) screen
) + y
* width
;
53 for (h
+= y
; y
< h
; y
++, line
+= width
)
54 mvwaddchnstr(screenpad
, y
, 0, line
, width
);
56 pnoutrefresh(screenpad
, py
, px
, sminy
, sminx
, smaxy
- 1, smaxx
- 1);
60 static void curses_calc_pad(void)
62 if (is_fixedsize_console()) {
76 screenpad
= newpad(height
, width
);
79 px
= (width
- COLS
) / 2;
84 sminx
= (COLS
- width
) / 2;
85 smaxx
= sminx
+ width
;
89 py
= (height
- LINES
) / 2;
94 sminy
= (LINES
- height
) / 2;
95 smaxy
= sminy
+ height
;
99 static void curses_resize(DisplayState
*ds
)
101 if (ds_get_width(ds
) == gwidth
&& ds_get_height(ds
) == gheight
)
104 gwidth
= ds_get_width(ds
);
105 gheight
= ds_get_height(ds
);
108 ds
->surface
->width
= width
* FONT_WIDTH
;
109 ds
->surface
->height
= height
* FONT_HEIGHT
;
113 #if defined(SIGWINCH) && defined(KEY_RESIZE)
114 static void curses_winch_handler(int signum
)
117 unsigned short ws_row
;
118 unsigned short ws_col
;
119 unsigned short ws_xpixel
; /* unused */
120 unsigned short ws_ypixel
; /* unused */
123 /* terminal size changed */
124 if (ioctl(1, TIOCGWINSZ
, &ws
) == -1)
127 resize_term(ws
.ws_row
, ws
.ws_col
);
131 /* some systems require this */
132 signal(SIGWINCH
, curses_winch_handler
);
137 static void curses_cursor_position(DisplayState
*ds
, int x
, int y
)
143 if (x
>= 0 && y
>= 0 && x
< COLS
&& y
< LINES
) {
146 /* it seems that curs_set(1) must always be called before
147 * curs_set(2) for the latter to have effect */
148 if (!is_graphic_console())
157 /* generic keyboard conversion */
159 #include "curses_keys.h"
161 static kbd_layout_t
*kbd_layout
= NULL
;
163 static void curses_refresh(DisplayState
*ds
)
165 int chr
, nextchr
, keysym
, keycode
, keycode_alt
;
171 ds
->surface
->width
= FONT_WIDTH
* width
;
172 ds
->surface
->height
= FONT_HEIGHT
* height
;
177 vga_hw_text_update(screen
);
181 /* while there are any pending key strokes to process */
193 /* this shouldn't occur when we use a custom SIGWINCH handler */
194 if (chr
== KEY_RESIZE
) {
198 curses_update(ds
, 0, 0, width
, height
);
199 ds
->surface
->width
= FONT_WIDTH
* width
;
200 ds
->surface
->height
= FONT_HEIGHT
* height
;
205 keycode
= curses2keycode
[chr
];
212 if (nextchr
!= ERR
) {
215 keycode
= curses2keycode
[nextchr
];
221 /* process keys reserved for qemu */
222 if (keycode
>= QEMU_KEY_CONSOLE0
&&
223 keycode
< QEMU_KEY_CONSOLE0
+ 9) {
225 wnoutrefresh(stdscr
);
226 console_select(keycode
- QEMU_KEY_CONSOLE0
);
237 if (chr
< CURSES_KEYS
)
238 keysym
= curses2keysym
[chr
];
242 keysym
= (chr
+ '@' - 'A' + 'a') | KEYSYM_CNTRL
;
247 keycode
= keysym2scancode(kbd_layout
, keysym
& KEYSYM_MASK
);
251 keycode
|= (keysym
& ~KEYSYM_MASK
) >> 16;
252 keycode
|= keycode_alt
;
258 if (is_graphic_console()) {
259 /* since terminals don't know about key press and release
260 * events, we need to emit both for each key received */
262 kbd_put_keycode(SHIFT_CODE
);
264 kbd_put_keycode(CNTRL_CODE
);
266 kbd_put_keycode(ALT_CODE
);
267 if (keycode
& ALTGR
) {
268 kbd_put_keycode(SCANCODE_EMUL0
);
269 kbd_put_keycode(ALT_CODE
);
272 kbd_put_keycode(GREY_CODE
);
273 kbd_put_keycode(keycode
& KEY_MASK
);
275 kbd_put_keycode(GREY_CODE
);
276 kbd_put_keycode((keycode
& KEY_MASK
) | KEY_RELEASE
);
277 if (keycode
& ALTGR
) {
278 kbd_put_keycode(SCANCODE_EMUL0
);
279 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
282 kbd_put_keycode(ALT_CODE
| KEY_RELEASE
);
284 kbd_put_keycode(CNTRL_CODE
| KEY_RELEASE
);
286 kbd_put_keycode(SHIFT_CODE
| KEY_RELEASE
);
288 keysym
= curses2qemu
[chr
];
292 kbd_put_keysym(keysym
);
297 static void curses_atexit(Notifier
*notifier
)
302 static void curses_setup(void)
304 int i
, colour_default
[8] = {
305 COLOR_BLACK
, COLOR_BLUE
, COLOR_GREEN
, COLOR_CYAN
,
306 COLOR_RED
, COLOR_MAGENTA
, COLOR_YELLOW
, COLOR_WHITE
,
309 /* input as raw as possible, let everything be interpreted
310 * by the guest system */
311 initscr(); noecho(); intrflush(stdscr
, FALSE
);
312 nodelay(stdscr
, TRUE
); nonl(); keypad(stdscr
, TRUE
);
313 start_color(); raw(); scrollok(stdscr
, FALSE
);
315 for (i
= 0; i
< 64; i
++)
316 init_pair(i
, colour_default
[i
& 7], colour_default
[i
>> 3]);
319 static void curses_keyboard_setup(void)
321 #if defined(__APPLE__)
322 /* always use generic keymaps */
323 if (!keyboard_layout
)
324 keyboard_layout
= "en-us";
326 if(keyboard_layout
) {
327 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
333 void curses_display_init(DisplayState
*ds
, int full_screen
)
335 DisplayChangeListener
*dcl
;
336 static Notifier notifier
= { .notify
= curses_atexit
};
339 fprintf(stderr
, "We need a terminal output\n");
345 curses_keyboard_setup();
346 exit_notifier_add(¬ifier
);
349 #if defined(SIGWINCH) && defined(KEY_RESIZE)
350 /* some curses implementations provide a handler, but we
351 * want to be sure this is handled regardless of the library */
352 signal(SIGWINCH
, curses_winch_handler
);
356 dcl
= (DisplayChangeListener
*) qemu_mallocz(sizeof(DisplayChangeListener
));
357 dcl
->dpy_update
= curses_update
;
358 dcl
->dpy_resize
= curses_resize
;
359 dcl
->dpy_refresh
= curses_refresh
;
360 dcl
->dpy_text_cursor
= curses_cursor_position
;
361 register_displaychangelistener(ds
, dcl
);
362 qemu_free_displaysurface(ds
);
363 ds
->surface
= qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen
);