virtio-net: support RSC v4/v6 tcp traffic for Windows HCK
[qemu/ar7.git] / ui / curses.c
blobf4e7a12f74a91f6142fe23f799446c601b604217
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.
24 #include "qemu/osdep.h"
26 #ifndef _WIN32
27 #include <sys/ioctl.h>
28 #include <termios.h>
29 #endif
31 #include "qapi/error.h"
32 #include "qemu-common.h"
33 #include "ui/console.h"
34 #include "ui/input.h"
35 #include "sysemu/sysemu.h"
37 /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
38 #undef KEY_EVENT
39 #include <curses.h>
40 #undef KEY_EVENT
42 #define FONT_HEIGHT 16
43 #define FONT_WIDTH 8
45 static DisplayChangeListener *dcl;
46 static console_ch_t screen[160 * 100];
47 static WINDOW *screenpad = NULL;
48 static int width, height, gwidth, gheight, invalidate;
49 static int px, py, sminx, sminy, smaxx, smaxy;
51 static chtype vga_to_curses[256];
53 static void curses_update(DisplayChangeListener *dcl,
54 int x, int y, int w, int h)
56 console_ch_t *line;
57 chtype curses_line[width];
59 line = screen + y * width;
60 for (h += y; y < h; y ++, line += width) {
61 for (x = 0; x < width; x++) {
62 chtype ch = line[x] & 0xff;
63 chtype at = line[x] & ~0xff;
64 if (vga_to_curses[ch]) {
65 ch = vga_to_curses[ch];
67 curses_line[x] = ch | at;
69 mvwaddchnstr(screenpad, y, 0, curses_line, width);
72 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
73 refresh();
76 static void curses_calc_pad(void)
78 if (qemu_console_is_fixedsize(NULL)) {
79 width = gwidth;
80 height = gheight;
81 } else {
82 width = COLS;
83 height = LINES;
86 if (screenpad)
87 delwin(screenpad);
89 clear();
90 refresh();
92 screenpad = newpad(height, width);
94 if (width > COLS) {
95 px = (width - COLS) / 2;
96 sminx = 0;
97 smaxx = COLS;
98 } else {
99 px = 0;
100 sminx = (COLS - width) / 2;
101 smaxx = sminx + width;
104 if (height > LINES) {
105 py = (height - LINES) / 2;
106 sminy = 0;
107 smaxy = LINES;
108 } else {
109 py = 0;
110 sminy = (LINES - height) / 2;
111 smaxy = sminy + height;
115 static void curses_resize(DisplayChangeListener *dcl,
116 int width, int height)
118 if (width == gwidth && height == gheight) {
119 return;
122 gwidth = width;
123 gheight = height;
125 curses_calc_pad();
128 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
129 static volatile sig_atomic_t got_sigwinch;
130 static void curses_winch_check(void)
132 struct winsize {
133 unsigned short ws_row;
134 unsigned short ws_col;
135 unsigned short ws_xpixel; /* unused */
136 unsigned short ws_ypixel; /* unused */
137 } ws;
139 if (!got_sigwinch) {
140 return;
142 got_sigwinch = false;
144 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
145 return;
148 resize_term(ws.ws_row, ws.ws_col);
149 invalidate = 1;
152 static void curses_winch_handler(int signum)
154 got_sigwinch = true;
157 static void curses_winch_init(void)
159 struct sigaction old, winch = {
160 .sa_handler = curses_winch_handler,
162 sigaction(SIGWINCH, &winch, &old);
164 #else
165 static void curses_winch_check(void) {}
166 static void curses_winch_init(void) {}
167 #endif
169 static void curses_cursor_position(DisplayChangeListener *dcl,
170 int x, int y)
172 if (x >= 0) {
173 x = sminx + x - px;
174 y = sminy + y - py;
176 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
177 move(y, x);
178 curs_set(1);
179 /* it seems that curs_set(1) must always be called before
180 * curs_set(2) for the latter to have effect */
181 if (!qemu_console_is_graphic(NULL)) {
182 curs_set(2);
184 return;
188 curs_set(0);
191 /* generic keyboard conversion */
193 #include "curses_keys.h"
195 static kbd_layout_t *kbd_layout = NULL;
197 static void curses_refresh(DisplayChangeListener *dcl)
199 int chr, keysym, keycode, keycode_alt;
201 curses_winch_check();
203 if (invalidate) {
204 clear();
205 refresh();
206 curses_calc_pad();
207 graphic_hw_invalidate(NULL);
208 invalidate = 0;
211 graphic_hw_text_update(NULL, screen);
213 while (1) {
214 /* while there are any pending key strokes to process */
215 chr = getch();
217 if (chr == ERR)
218 break;
220 #ifdef KEY_RESIZE
221 /* this shouldn't occur when we use a custom SIGWINCH handler */
222 if (chr == KEY_RESIZE) {
223 clear();
224 refresh();
225 curses_calc_pad();
226 curses_update(dcl, 0, 0, width, height);
227 continue;
229 #endif
231 keycode = curses2keycode[chr];
232 keycode_alt = 0;
234 /* alt key */
235 if (keycode == 1) {
236 int nextchr = getch();
238 if (nextchr != ERR) {
239 chr = nextchr;
240 keycode_alt = ALT;
241 keycode = curses2keycode[chr];
243 if (keycode != -1) {
244 keycode |= ALT;
246 /* process keys reserved for qemu */
247 if (keycode >= QEMU_KEY_CONSOLE0 &&
248 keycode < QEMU_KEY_CONSOLE0 + 9) {
249 erase();
250 wnoutrefresh(stdscr);
251 console_select(keycode - QEMU_KEY_CONSOLE0);
253 invalidate = 1;
254 continue;
260 if (kbd_layout) {
261 keysym = -1;
262 if (chr < CURSES_KEYS)
263 keysym = curses2keysym[chr];
265 if (keysym == -1) {
266 if (chr < ' ') {
267 keysym = chr + '@';
268 if (keysym >= 'A' && keysym <= 'Z')
269 keysym += 'a' - 'A';
270 keysym |= KEYSYM_CNTRL;
271 } else
272 keysym = chr;
275 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
276 false, false, false);
277 if (keycode == 0)
278 continue;
280 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
281 keycode |= keycode_alt;
284 if (keycode == -1)
285 continue;
287 if (qemu_console_is_graphic(NULL)) {
288 /* since terminals don't know about key press and release
289 * events, we need to emit both for each key received */
290 if (keycode & SHIFT) {
291 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
292 qemu_input_event_send_key_delay(0);
294 if (keycode & CNTRL) {
295 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
296 qemu_input_event_send_key_delay(0);
298 if (keycode & ALT) {
299 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
300 qemu_input_event_send_key_delay(0);
302 if (keycode & ALTGR) {
303 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
304 qemu_input_event_send_key_delay(0);
307 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
308 qemu_input_event_send_key_delay(0);
309 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
310 qemu_input_event_send_key_delay(0);
312 if (keycode & ALTGR) {
313 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
314 qemu_input_event_send_key_delay(0);
316 if (keycode & ALT) {
317 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
318 qemu_input_event_send_key_delay(0);
320 if (keycode & CNTRL) {
321 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
322 qemu_input_event_send_key_delay(0);
324 if (keycode & SHIFT) {
325 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
326 qemu_input_event_send_key_delay(0);
328 } else {
329 keysym = -1;
330 if (chr < CURSES_KEYS) {
331 keysym = curses2qemu[chr];
333 if (keysym == -1)
334 keysym = chr;
336 kbd_put_keysym(keysym);
341 static void curses_atexit(void)
343 endwin();
346 static void curses_setup(void)
348 int i, colour_default[8] = {
349 [QEMU_COLOR_BLACK] = COLOR_BLACK,
350 [QEMU_COLOR_BLUE] = COLOR_BLUE,
351 [QEMU_COLOR_GREEN] = COLOR_GREEN,
352 [QEMU_COLOR_CYAN] = COLOR_CYAN,
353 [QEMU_COLOR_RED] = COLOR_RED,
354 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
355 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
356 [QEMU_COLOR_WHITE] = COLOR_WHITE,
359 /* input as raw as possible, let everything be interpreted
360 * by the guest system */
361 initscr(); noecho(); intrflush(stdscr, FALSE);
362 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
363 start_color(); raw(); scrollok(stdscr, FALSE);
365 /* Make color pair to match color format (3bits bg:3bits fg) */
366 for (i = 0; i < 64; i++) {
367 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
369 /* Set default color for more than 64 for safety. */
370 for (i = 64; i < COLOR_PAIRS; i++) {
371 init_pair(i, COLOR_WHITE, COLOR_BLACK);
375 * Setup mapping for vga to curses line graphics.
376 * FIXME: for better font, have to use ncursesw and setlocale()
378 #if 0
379 /* FIXME: map from where? */
380 ACS_S1;
381 ACS_S3;
382 ACS_S7;
383 ACS_S9;
384 #endif
385 /* ACS_* is not constant. So, we can't initialize statically. */
386 vga_to_curses['\0'] = ' ';
387 vga_to_curses[0x04] = ACS_DIAMOND;
388 vga_to_curses[0x18] = ACS_UARROW;
389 vga_to_curses[0x19] = ACS_DARROW;
390 vga_to_curses[0x1a] = ACS_RARROW;
391 vga_to_curses[0x1b] = ACS_LARROW;
392 vga_to_curses[0x9c] = ACS_STERLING;
393 vga_to_curses[0xb0] = ACS_BOARD;
394 vga_to_curses[0xb1] = ACS_CKBOARD;
395 vga_to_curses[0xb3] = ACS_VLINE;
396 vga_to_curses[0xb4] = ACS_RTEE;
397 vga_to_curses[0xbf] = ACS_URCORNER;
398 vga_to_curses[0xc0] = ACS_LLCORNER;
399 vga_to_curses[0xc1] = ACS_BTEE;
400 vga_to_curses[0xc2] = ACS_TTEE;
401 vga_to_curses[0xc3] = ACS_LTEE;
402 vga_to_curses[0xc4] = ACS_HLINE;
403 vga_to_curses[0xc5] = ACS_PLUS;
404 vga_to_curses[0xce] = ACS_LANTERN;
405 vga_to_curses[0xd8] = ACS_NEQUAL;
406 vga_to_curses[0xd9] = ACS_LRCORNER;
407 vga_to_curses[0xda] = ACS_ULCORNER;
408 vga_to_curses[0xdb] = ACS_BLOCK;
409 vga_to_curses[0xe3] = ACS_PI;
410 vga_to_curses[0xf1] = ACS_PLMINUS;
411 vga_to_curses[0xf2] = ACS_GEQUAL;
412 vga_to_curses[0xf3] = ACS_LEQUAL;
413 vga_to_curses[0xf8] = ACS_DEGREE;
414 vga_to_curses[0xfe] = ACS_BULLET;
417 static void curses_keyboard_setup(void)
419 #if defined(__APPLE__)
420 /* always use generic keymaps */
421 if (!keyboard_layout)
422 keyboard_layout = "en-us";
423 #endif
424 if(keyboard_layout) {
425 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
426 &error_fatal);
430 static const DisplayChangeListenerOps dcl_ops = {
431 .dpy_name = "curses",
432 .dpy_text_update = curses_update,
433 .dpy_text_resize = curses_resize,
434 .dpy_refresh = curses_refresh,
435 .dpy_text_cursor = curses_cursor_position,
438 static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
440 #ifndef _WIN32
441 if (!isatty(1)) {
442 fprintf(stderr, "We need a terminal output\n");
443 exit(1);
445 #endif
447 curses_setup();
448 curses_keyboard_setup();
449 atexit(curses_atexit);
451 curses_winch_init();
453 dcl = g_new0(DisplayChangeListener, 1);
454 dcl->ops = &dcl_ops;
455 register_displaychangelistener(dcl);
457 invalidate = 1;
460 static QemuDisplay qemu_display_curses = {
461 .type = DISPLAY_TYPE_CURSES,
462 .init = curses_display_init,
465 static void register_curses(void)
467 qemu_display_register(&qemu_display_curses);
470 type_init(register_curses);