]> Git Repo - qemu.git/blob - ui/curses.c
Reduce curses escdelay from 1s to 25ms
[qemu.git] / ui / curses.c
1 /*
2  * QEMU curses/ncurses display driver
3  * 
4  * Copyright (c) 2005 Andrzej Zaborowski  <[email protected]>
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 #include "qemu/osdep.h"
25
26 #ifndef _WIN32
27 #include <sys/ioctl.h>
28 #include <termios.h>
29 #endif
30
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"
36
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
41
42 #define FONT_HEIGHT 16
43 #define FONT_WIDTH 8
44
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;
50
51 static chtype vga_to_curses[256];
52
53 static void curses_update(DisplayChangeListener *dcl,
54                           int x, int y, int w, int h)
55 {
56     console_ch_t *line;
57     chtype curses_line[width];
58
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];
66             }
67             curses_line[x] = ch | at;
68         }
69         mvwaddchnstr(screenpad, y, 0, curses_line, width);
70     }
71
72     pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
73     refresh();
74 }
75
76 static void curses_calc_pad(void)
77 {
78     if (qemu_console_is_fixedsize(NULL)) {
79         width = gwidth;
80         height = gheight;
81     } else {
82         width = COLS;
83         height = LINES;
84     }
85
86     if (screenpad)
87         delwin(screenpad);
88
89     clear();
90     refresh();
91
92     screenpad = newpad(height, width);
93
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;
102     }
103
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;
112     }
113 }
114
115 static void curses_resize(DisplayChangeListener *dcl,
116                           int width, int height)
117 {
118     if (width == gwidth && height == gheight) {
119         return;
120     }
121
122     gwidth = width;
123     gheight = height;
124
125     curses_calc_pad();
126 }
127
128 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
129 static volatile sig_atomic_t got_sigwinch;
130 static void curses_winch_check(void)
131 {
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;
138
139     if (!got_sigwinch) {
140         return;
141     }
142     got_sigwinch = false;
143
144     if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
145         return;
146     }
147
148     resize_term(ws.ws_row, ws.ws_col);
149     invalidate = 1;
150 }
151
152 static void curses_winch_handler(int signum)
153 {
154     got_sigwinch = true;
155 }
156
157 static void curses_winch_init(void)
158 {
159     struct sigaction old, winch = {
160         .sa_handler  = curses_winch_handler,
161     };
162     sigaction(SIGWINCH, &winch, &old);
163 }
164 #else
165 static void curses_winch_check(void) {}
166 static void curses_winch_init(void) {}
167 #endif
168
169 static void curses_cursor_position(DisplayChangeListener *dcl,
170                                    int x, int y)
171 {
172     if (x >= 0) {
173         x = sminx + x - px;
174         y = sminy + y - py;
175
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);
183             }
184             return;
185         }
186     }
187
188     curs_set(0);
189 }
190
191 /* generic keyboard conversion */
192
193 #include "curses_keys.h"
194
195 static kbd_layout_t *kbd_layout = NULL;
196
197 static void curses_refresh(DisplayChangeListener *dcl)
198 {
199     int chr, keysym, keycode, keycode_alt;
200
201     curses_winch_check();
202
203     if (invalidate) {
204         clear();
205         refresh();
206         curses_calc_pad();
207         graphic_hw_invalidate(NULL);
208         invalidate = 0;
209     }
210
211     graphic_hw_text_update(NULL, screen);
212
213     while (1) {
214         /* while there are any pending key strokes to process */
215         chr = getch();
216
217         if (chr == ERR)
218             break;
219
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;
228         }
229 #endif
230
231         keycode = curses2keycode[chr];
232         keycode_alt = 0;
233
234         /* alt or esc key */
235         if (keycode == 1) {
236             int nextchr = getch();
237
238             if (nextchr != ERR) {
239                 chr = nextchr;
240                 keycode_alt = ALT;
241                 keycode = curses2keycode[chr];
242
243                 if (keycode != -1) {
244                     keycode |= ALT;
245
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);
252
253                         invalidate = 1;
254                         continue;
255                     }
256                 }
257             }
258         }
259
260         if (kbd_layout) {
261             keysym = -1;
262             if (chr < CURSES_KEYS)
263                 keysym = curses2keysym[chr];
264
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;
273             }
274
275             keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
276                                       NULL, false);
277             if (keycode == 0)
278                 continue;
279
280             keycode |= (keysym & ~KEYSYM_MASK) >> 16;
281             keycode |= keycode_alt;
282         }
283
284         if (keycode == -1)
285             continue;
286
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);
293             }
294             if (keycode & CNTRL) {
295                 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
296                 qemu_input_event_send_key_delay(0);
297             }
298             if (keycode & ALT) {
299                 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
300                 qemu_input_event_send_key_delay(0);
301             }
302             if (keycode & ALTGR) {
303                 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
304                 qemu_input_event_send_key_delay(0);
305             }
306
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);
311
312             if (keycode & ALTGR) {
313                 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
314                 qemu_input_event_send_key_delay(0);
315             }
316             if (keycode & ALT) {
317                 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
318                 qemu_input_event_send_key_delay(0);
319             }
320             if (keycode & CNTRL) {
321                 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
322                 qemu_input_event_send_key_delay(0);
323             }
324             if (keycode & SHIFT) {
325                 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
326                 qemu_input_event_send_key_delay(0);
327             }
328         } else {
329             keysym = -1;
330             if (chr < CURSES_KEYS) {
331                 keysym = curses2qemu[chr];
332             }
333             if (keysym == -1)
334                 keysym = chr;
335
336             kbd_put_keysym(keysym);
337         }
338     }
339 }
340
341 static void curses_atexit(void)
342 {
343     endwin();
344 }
345
346 static void curses_setup(void)
347 {
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,
357     };
358
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);
364     set_escdelay(25);
365
366     /* Make color pair to match color format (3bits bg:3bits fg) */
367     for (i = 0; i < 64; i++) {
368         init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
369     }
370     /* Set default color for more than 64 for safety. */
371     for (i = 64; i < COLOR_PAIRS; i++) {
372         init_pair(i, COLOR_WHITE, COLOR_BLACK);
373     }
374
375     /*
376      * Setup mapping for vga to curses line graphics.
377      * FIXME: for better font, have to use ncursesw and setlocale()
378      */
379 #if 0
380     /* FIXME: map from where? */
381     ACS_S1;
382     ACS_S3;
383     ACS_S7;
384     ACS_S9;
385 #endif
386     /* ACS_* is not constant. So, we can't initialize statically. */
387     vga_to_curses['\0'] = ' ';
388     vga_to_curses[0x04] = ACS_DIAMOND;
389     vga_to_curses[0x18] = ACS_UARROW;
390     vga_to_curses[0x19] = ACS_DARROW;
391     vga_to_curses[0x1a] = ACS_RARROW;
392     vga_to_curses[0x1b] = ACS_LARROW;
393     vga_to_curses[0x9c] = ACS_STERLING;
394     vga_to_curses[0xb0] = ACS_BOARD;
395     vga_to_curses[0xb1] = ACS_CKBOARD;
396     vga_to_curses[0xb3] = ACS_VLINE;
397     vga_to_curses[0xb4] = ACS_RTEE;
398     vga_to_curses[0xbf] = ACS_URCORNER;
399     vga_to_curses[0xc0] = ACS_LLCORNER;
400     vga_to_curses[0xc1] = ACS_BTEE;
401     vga_to_curses[0xc2] = ACS_TTEE;
402     vga_to_curses[0xc3] = ACS_LTEE;
403     vga_to_curses[0xc4] = ACS_HLINE;
404     vga_to_curses[0xc5] = ACS_PLUS;
405     vga_to_curses[0xce] = ACS_LANTERN;
406     vga_to_curses[0xd8] = ACS_NEQUAL;
407     vga_to_curses[0xd9] = ACS_LRCORNER;
408     vga_to_curses[0xda] = ACS_ULCORNER;
409     vga_to_curses[0xdb] = ACS_BLOCK;
410     vga_to_curses[0xe3] = ACS_PI;
411     vga_to_curses[0xf1] = ACS_PLMINUS;
412     vga_to_curses[0xf2] = ACS_GEQUAL;
413     vga_to_curses[0xf3] = ACS_LEQUAL;
414     vga_to_curses[0xf8] = ACS_DEGREE;
415     vga_to_curses[0xfe] = ACS_BULLET;
416 }
417
418 static void curses_keyboard_setup(void)
419 {
420 #if defined(__APPLE__)
421     /* always use generic keymaps */
422     if (!keyboard_layout)
423         keyboard_layout = "en-us";
424 #endif
425     if(keyboard_layout) {
426         kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
427                                           &error_fatal);
428     }
429 }
430
431 static const DisplayChangeListenerOps dcl_ops = {
432     .dpy_name        = "curses",
433     .dpy_text_update = curses_update,
434     .dpy_text_resize = curses_resize,
435     .dpy_refresh     = curses_refresh,
436     .dpy_text_cursor = curses_cursor_position,
437 };
438
439 static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
440 {
441 #ifndef _WIN32
442     if (!isatty(1)) {
443         fprintf(stderr, "We need a terminal output\n");
444         exit(1);
445     }
446 #endif
447
448     curses_setup();
449     curses_keyboard_setup();
450     atexit(curses_atexit);
451
452     curses_winch_init();
453
454     dcl = g_new0(DisplayChangeListener, 1);
455     dcl->ops = &dcl_ops;
456     register_displaychangelistener(dcl);
457
458     invalidate = 1;
459 }
460
461 static QemuDisplay qemu_display_curses = {
462     .type       = DISPLAY_TYPE_CURSES,
463     .init       = curses_display_init,
464 };
465
466 static void register_curses(void)
467 {
468     qemu_display_register(&qemu_display_curses);
469 }
470
471 type_init(register_curses);
This page took 0.048747 seconds and 4 git commands to generate.