kvm: qemu: LSI SCSI and e1000 unregister callbacks
[kvm-userspace.git] / qemu / curses.c
blob87aa9b36b644ff511dfae1f73819feb2a772f613
1 /*
2 * QEMU curses/ncurses display driver
3 *
4 * Copyright (c) 2005 Andrzej Zaborowski <balrog@zabor.org>
5 *
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
22 * THE SOFTWARE.
25 #include "qemu-common.h"
26 #include "console.h"
27 #include "sysemu.h"
29 #include <curses.h>
31 #ifndef _WIN32
32 #include <signal.h>
33 #include <sys/ioctl.h>
34 #include <termios.h>
35 #endif
37 #define FONT_HEIGHT 16
38 #define FONT_WIDTH 8
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)
47 chtype *line;
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);
54 refresh();
57 static void curses_calc_pad(void)
59 if (is_graphic_console()) {
60 width = gwidth;
61 height = gheight;
62 } else {
63 width = COLS;
64 height = LINES;
67 if (screenpad)
68 delwin(screenpad);
70 clear();
71 refresh();
73 screenpad = newpad(height, width);
75 if (width > COLS) {
76 px = (width - COLS) / 2;
77 sminx = 0;
78 smaxx = COLS;
79 } else {
80 px = 0;
81 sminx = (COLS - width) / 2;
82 smaxx = sminx + width;
85 if (height > LINES) {
86 py = (height - LINES) / 2;
87 sminy = 0;
88 smaxy = LINES;
89 } else {
90 py = 0;
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)
99 return;
101 gwidth = w;
102 gheight = h;
104 curses_calc_pad();
107 #ifndef _WIN32
108 #if defined(SIGWINCH) && defined(KEY_RESIZE)
109 static void curses_winch_handler(int signum)
111 struct winsize {
112 unsigned short ws_row;
113 unsigned short ws_col;
114 unsigned short ws_xpixel; /* unused */
115 unsigned short ws_ypixel; /* unused */
116 } ws;
118 /* terminal size changed */
119 if (ioctl(1, TIOCGWINSZ, &ws) == -1)
120 return;
122 resize_term(ws.ws_row, ws.ws_col);
123 curses_calc_pad();
124 invalidate = 1;
126 /* some systems require this */
127 signal(SIGWINCH, curses_winch_handler);
129 #endif
130 #endif
132 static void curses_cursor_position(DisplayState *ds, int x, int y)
134 if (x >= 0) {
135 x = sminx + x - px;
136 y = sminy + y - py;
138 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
139 move(y, x);
140 curs_set(1);
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())
144 curs_set(2);
145 return;
149 curs_set(0);
152 /* generic keyboard conversion */
154 #include "curses_keys.h"
155 #include "keymaps.c"
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;
164 if (invalidate) {
165 clear();
166 refresh();
167 curses_calc_pad();
168 ds->width = FONT_WIDTH * width;
169 ds->height = FONT_HEIGHT * height;
170 vga_hw_invalidate();
171 invalidate = 0;
174 vga_hw_text_update(screen);
176 nextchr = ERR;
177 while (1) {
178 /* while there are any pending key strokes to process */
179 if (nextchr == ERR)
180 chr = getch();
181 else {
182 chr = nextchr;
183 nextchr = ERR;
186 if (chr == ERR)
187 break;
189 #ifdef KEY_RESIZE
190 /* this shouldn't occur when we use a custom SIGWINCH handler */
191 if (chr == KEY_RESIZE) {
192 clear();
193 refresh();
194 curses_calc_pad();
195 curses_update(ds, 0, 0, width, height);
196 ds->width = FONT_WIDTH * width;
197 ds->height = FONT_HEIGHT * height;
198 continue;
200 #endif
202 keycode = curses2keycode[chr];
203 if (keycode == -1)
204 continue;
206 /* alt key */
207 if (keycode == 1) {
208 nextchr = getch();
210 if (nextchr != ERR) {
211 keycode = curses2keycode[nextchr];
212 nextchr = ERR;
213 if (keycode == -1)
214 continue;
216 keycode |= ALT;
218 /* process keys reserved for qemu */
219 if (keycode >= QEMU_KEY_CONSOLE0 &&
220 keycode < QEMU_KEY_CONSOLE0 + 9) {
221 erase();
222 wnoutrefresh(stdscr);
223 console_select(keycode - QEMU_KEY_CONSOLE0);
225 invalidate = 1;
226 continue;
231 if (kbd_layout && !(keycode & GREY)) {
232 keysym = keycode2keysym[keycode & KEY_MASK];
233 if (keysym == -1)
234 keysym = chr;
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 */
243 if (keycode & SHIFT)
244 kbd_put_keycode(SHIFT_CODE);
245 if (keycode & CNTRL)
246 kbd_put_keycode(CNTRL_CODE);
247 if (keycode & ALT)
248 kbd_put_keycode(ALT_CODE);
249 if (keycode & GREY)
250 kbd_put_keycode(GREY_CODE);
251 kbd_put_keycode(keycode & KEY_MASK);
252 if (keycode & GREY)
253 kbd_put_keycode(GREY_CODE);
254 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
255 if (keycode & ALT)
256 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
257 if (keycode & CNTRL)
258 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
259 if (keycode & SHIFT)
260 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
261 } else {
262 keysym = curses2keysym[chr];
263 if (keysym == -1)
264 keysym = chr;
266 kbd_put_keysym(keysym);
271 static void curses_cleanup(void *opaque)
273 endwin();
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";
306 #endif
307 if(keyboard_layout) {
308 kbd_layout = init_keyboard_layout(keyboard_layout);
309 if (!kbd_layout)
310 exit(1);
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)
318 continue;
320 keycode = curses2keycode[i] & KEY_MASK;
321 if (keycode2keysym[keycode] >= 0)
322 continue;
324 for (keysym = 0; keysym < CURSES_KEYS; keysym ++)
325 if (curses2keycode[keysym] == keycode) {
326 keycode2keysym[keycode] = keysym;
327 break;
330 if (keysym >= CURSES_KEYS)
331 keycode2keysym[keycode] = i;
335 void curses_display_init(DisplayState *ds, int full_screen)
337 #ifndef _WIN32
338 if (!isatty(1)) {
339 fprintf(stderr, "We need a terminal output\n");
340 exit(1);
342 #endif
344 curses_setup();
345 curses_keyboard_setup();
346 atexit(curses_atexit);
348 #ifndef _WIN32
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);
355 #endif
356 #endif
358 ds->data = (void *) screen;
359 ds->linesize = 0;
360 ds->depth = 0;
361 ds->width = 640;
362 ds->height = 400;
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;
368 invalidate = 1;
370 /* Standard VGA initial text mode dimensions */
371 curses_resize(ds, 80, 25);