2 * QEMU curses/ncurses display driver
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)
108 #if defined(SIGWINCH) && defined(KEY_RESIZE)
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 */
190 /* this shouldn't occur when we use a custom SIGWINCH handler */
191 if (chr == KEY_RESIZE) {
195 curses_update(ds, 0, 0, width, height);
196 ds->width = FONT_WIDTH * width;
197 ds->height = FONT_HEIGHT * height;
202 keycode = curses2keycode[chr];
210 if (nextchr != ERR) {
211 keycode = curses2keycode[nextchr];
218 /* process keys reserved for qemu */
219 if (keycode >= QEMU_KEY_CONSOLE0 &&
220 keycode < QEMU_KEY_CONSOLE0 + 9) {
222 wnoutrefresh(stdscr);
223 console_select(keycode - QEMU_KEY_CONSOLE0);
231 if (kbd_layout && !(keycode & GREY)) {
232 keysym = keycode2keysym[keycode & KEY_MASK];
236 keycode &= ~KEY_MASK;
237 keycode |= keysym2scancode(kbd_layout, keysym);
240 if (is_graphic_console()) {
241 /* since terminals don't know about key press and release
242 * events, we need to emit both for each key received */
244 kbd_put_keycode(SHIFT_CODE);
246 kbd_put_keycode(CNTRL_CODE);
248 kbd_put_keycode(ALT_CODE);
250 kbd_put_keycode(GREY_CODE);
251 kbd_put_keycode(keycode & KEY_MASK);
253 kbd_put_keycode(GREY_CODE);
254 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
256 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
258 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
260 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
262 keysym = curses2keysym[chr];
266 kbd_put_keysym(keysym);
271 static void curses_cleanup(void *opaque)
276 static void curses_atexit(void)
278 curses_cleanup(NULL);
281 static void curses_setup(void)
283 int i, colour_default[8] = {
284 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
285 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
288 /* input as raw as possible, let everything be interpreted
289 * by the guest system */
290 initscr(); noecho(); intrflush(stdscr, FALSE);
291 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
292 start_color(); raw(); scrollok(stdscr, FALSE);
294 for (i = 0; i < 64; i ++)
295 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
298 static void curses_keyboard_setup(void)
300 int i, keycode, keysym;
302 #if defined(__APPLE__)
303 /* always use generic keymaps */
304 if (!keyboard_layout)
305 keyboard_layout = "en-us";
307 if(keyboard_layout) {
308 kbd_layout = init_keyboard_layout(keyboard_layout);
313 for (i = 0; i < CURSES_KEYS; i ++)
314 keycode2keysym[i] = -1;
316 for (i = 0; i < CURSES_KEYS; i ++) {
317 if (curses2keycode[i] == -1)
320 keycode = curses2keycode[i] & KEY_MASK;
321 if (keycode2keysym[keycode] >= 0)
324 for (keysym = 0; keysym < CURSES_KEYS; keysym ++)
325 if (curses2keycode[keysym] == keycode) {
326 keycode2keysym[keycode] = keysym;
330 if (keysym >= CURSES_KEYS)
331 keycode2keysym[keycode] = i;
335 void curses_display_init(DisplayState *ds, int full_screen)
339 fprintf(stderr, "We need a terminal output\n");
345 curses_keyboard_setup();
346 atexit(curses_atexit);
349 signal(SIGINT, SIG_DFL);
350 signal(SIGQUIT, SIG_DFL);
351 #if defined(SIGWINCH) && defined(KEY_RESIZE)
352 /* some curses implementations provide a handler, but we
353 * want to be sure this is handled regardless of the library */
354 signal(SIGWINCH, curses_winch_handler);
358 ds->data = (void *) screen;
363 ds->dpy_update = curses_update;
364 ds->dpy_resize = curses_resize;
365 ds->dpy_refresh = curses_refresh;
366 ds->dpy_text_cursor = curses_cursor_position;
370 /* Standard VGA initial text mode dimensions */
371 curses_resize(ds, 80, 25);