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>
37 #define FONT_HEIGHT 16
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(DisplayState *ds, 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 (is_graphic_console()) {
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(DisplayState *ds, int w, int h)
98 if (w == gwidth && h == gheight)
109 static void curses_winch_handler(int signum)
112 unsigned short ws_row;
113 unsigned short ws_col;
114 unsigned short ws_xpixel; /* unused */
115 unsigned short ws_ypixel; /* unused */
118 /* terminal size changed */
119 if (ioctl(1, TIOCGWINSZ, &ws) == -1)
122 resize_term(ws.ws_row, ws.ws_col);
126 /* some systems require this */
127 signal(SIGWINCH, curses_winch_handler);
132 static void curses_cursor_position(DisplayState *ds, int x, int y)
138 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
141 /* it seems that curs_set(1) must always be called before
142 * curs_set(2) for the latter to have effect */
143 if (!is_graphic_console())
152 /* generic keyboard conversion */
154 #include "curses_keys.h"
157 static kbd_layout_t *kbd_layout = 0;
158 static int keycode2keysym[CURSES_KEYS];
160 static void curses_refresh(DisplayState *ds)
162 int chr, nextchr, keysym, keycode;
168 ds->width = FONT_WIDTH * width;
169 ds->height = FONT_HEIGHT * height;
174 vga_hw_text_update(screen);
178 /* while there are any pending key strokes to process */
189 /* this shouldn't occur when we use a custom SIGWINCH handler */
190 if (chr == KEY_RESIZE) {
194 curses_update(ds, 0, 0, width, height);
195 ds->width = FONT_WIDTH * width;
196 ds->height = FONT_HEIGHT * height;
200 keycode = curses2keycode[chr];
208 if (nextchr != ERR) {
209 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);
229 if (kbd_layout && !(keycode & GREY)) {
230 keysym = keycode2keysym[keycode & KEY_MASK];
234 keycode &= ~KEY_MASK;
235 keycode |= keysym2scancode(kbd_layout, keysym);
238 if (is_graphic_console()) {
239 /* since terminals don't know about key press and release
240 * events, we need to emit both for each key received */
242 kbd_put_keycode(SHIFT_CODE);
244 kbd_put_keycode(CNTRL_CODE);
246 kbd_put_keycode(ALT_CODE);
248 kbd_put_keycode(GREY_CODE);
249 kbd_put_keycode(keycode & KEY_MASK);
251 kbd_put_keycode(GREY_CODE);
252 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
254 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
256 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
258 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
260 keysym = curses2keysym[chr];
264 kbd_put_keysym(keysym);
269 static void curses_cleanup(void *opaque)
274 static void curses_atexit(void)
276 curses_cleanup(NULL);
279 static void curses_setup(void)
281 int i, colour_default[8] = {
282 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
283 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
286 /* input as raw as possible, let everything be interpreted
287 * by the guest system */
288 initscr(); noecho(); intrflush(stdscr, FALSE);
289 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
290 start_color(); raw(); scrollok(stdscr, FALSE);
292 for (i = 0; i < 64; i ++)
293 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
296 static void curses_keyboard_setup(void)
298 int i, keycode, keysym;
300 #if defined(__APPLE__)
301 /* always use generic keymaps */
302 if (!keyboard_layout)
303 keyboard_layout = "en-us";
305 if(keyboard_layout) {
306 kbd_layout = init_keyboard_layout(keyboard_layout);
311 for (i = 0; i < CURSES_KEYS; i ++)
312 keycode2keysym[i] = -1;
314 for (i = 0; i < CURSES_KEYS; i ++) {
315 if (curses2keycode[i] == -1)
318 keycode = curses2keycode[i] & KEY_MASK;
319 if (keycode2keysym[keycode] >= 0)
322 for (keysym = 0; keysym < CURSES_KEYS; keysym ++)
323 if (curses2keycode[keysym] == keycode) {
324 keycode2keysym[keycode] = keysym;
328 if (keysym >= CURSES_KEYS)
329 keycode2keysym[keycode] = i;
333 void curses_display_init(DisplayState *ds, int full_screen)
337 fprintf(stderr, "We need a terminal output\n");
343 curses_keyboard_setup();
344 atexit(curses_atexit);
347 signal(SIGINT, SIG_DFL);
348 signal(SIGQUIT, SIG_DFL);
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 ds->data = (void *) screen;
361 ds->dpy_update = curses_update;
362 ds->dpy_resize = curses_resize;
363 ds->dpy_refresh = curses_refresh;
364 ds->dpy_text_cursor = curses_cursor_position;
368 /* Standard VGA initial text mode dimensions */
369 curses_resize(ds, 80, 25);