Handle terminating signals (Gerd Hoffmann)
[qemu] / curses.c
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:
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
25 #include "qemu-common.h"
26 #include "console.h"
27 #include "sysemu.h"
28
29 #include <curses.h>
30
31 #ifndef _WIN32
32 #include <signal.h>
33 #include <sys/ioctl.h>
34 #include <termios.h>
35 #endif
36
37 #ifdef __OpenBSD__
38 #define resize_term resizeterm
39 #endif
40
41 #define FONT_HEIGHT 16
42 #define FONT_WIDTH 8
43
44 static console_ch_t screen[160 * 100];
45 static WINDOW *screenpad = NULL;
46 static int width, height, gwidth, gheight, invalidate;
47 static int px, py, sminx, sminy, smaxx, smaxy;
48
49 static void curses_update(DisplayState *ds, int x, int y, int w, int h)
50 {
51     chtype *line;
52
53     line = ((chtype *) screen) + y * width;
54     for (h += y; y < h; y ++, line += width)
55         mvwaddchnstr(screenpad, y, 0, line, width);
56
57     pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
58     refresh();
59 }
60
61 static void curses_calc_pad(void)
62 {
63     if (is_graphic_console()) {
64         width = gwidth;
65         height = gheight;
66     } else {
67         width = COLS;
68         height = LINES;
69     }
70
71     if (screenpad)
72         delwin(screenpad);
73
74     clear();
75     refresh();
76
77     screenpad = newpad(height, width);
78
79     if (width > COLS) {
80         px = (width - COLS) / 2;
81         sminx = 0;
82         smaxx = COLS;
83     } else {
84         px = 0;
85         sminx = (COLS - width) / 2;
86         smaxx = sminx + width;
87     }
88
89     if (height > LINES) {
90         py = (height - LINES) / 2;
91         sminy = 0;
92         smaxy = LINES;
93     } else {
94         py = 0;
95         sminy = (LINES - height) / 2;
96         smaxy = sminy + height;
97     }
98 }
99
100 static void curses_resize(DisplayState *ds, int w, int h)
101 {
102     if (w == gwidth && h == gheight)
103         return;
104
105     gwidth = w;
106     gheight = h;
107
108     curses_calc_pad();
109 }
110
111 #ifndef _WIN32
112 #if defined(SIGWINCH) && defined(KEY_RESIZE)
113 static void curses_winch_handler(int signum)
114 {
115     struct winsize {
116         unsigned short ws_row;
117         unsigned short ws_col;
118         unsigned short ws_xpixel;   /* unused */
119         unsigned short ws_ypixel;   /* unused */
120     } ws;
121
122     /* terminal size changed */
123     if (ioctl(1, TIOCGWINSZ, &ws) == -1)
124         return;
125
126     resize_term(ws.ws_row, ws.ws_col);
127     curses_calc_pad();
128     invalidate = 1;
129
130     /* some systems require this */
131     signal(SIGWINCH, curses_winch_handler);
132 }
133 #endif
134 #endif
135
136 static void curses_cursor_position(DisplayState *ds, int x, int y)
137 {
138     if (x >= 0) {
139         x = sminx + x - px;
140         y = sminy + y - py;
141
142         if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
143             move(y, x);
144             curs_set(1);
145             /* it seems that curs_set(1) must always be called before
146              * curs_set(2) for the latter to have effect */
147             if (!is_graphic_console())
148                 curs_set(2);
149             return;
150         }
151     }
152
153     curs_set(0);
154 }
155
156 /* generic keyboard conversion */
157
158 #include "curses_keys.h"
159 #include "keymaps.c"
160
161 static kbd_layout_t *kbd_layout = 0;
162 static int keycode2keysym[CURSES_KEYS];
163
164 static void curses_refresh(DisplayState *ds)
165 {
166     int chr, nextchr, keysym, keycode;
167
168     if (invalidate) {
169         clear();
170         refresh();
171         curses_calc_pad();
172         ds->width = FONT_WIDTH * width;
173         ds->height = FONT_HEIGHT * height;
174         vga_hw_invalidate();
175         invalidate = 0;
176     }
177
178     vga_hw_text_update(screen);
179
180     nextchr = ERR;
181     while (1) {
182         /* while there are any pending key strokes to process */
183         if (nextchr == ERR)
184             chr = getch();
185         else {
186             chr = nextchr;
187             nextchr = ERR;
188         }
189
190         if (chr == ERR)
191             break;
192
193 #ifdef KEY_RESIZE
194         /* this shouldn't occur when we use a custom SIGWINCH handler */
195         if (chr == KEY_RESIZE) {
196             clear();
197             refresh();
198             curses_calc_pad();
199             curses_update(ds, 0, 0, width, height);
200             ds->width = FONT_WIDTH * width;
201             ds->height = FONT_HEIGHT * height;
202             continue;
203         }
204 #endif
205
206         keycode = curses2keycode[chr];
207         if (keycode == -1)
208             continue;
209
210         /* alt key */
211         if (keycode == 1) {
212             nextchr = getch();
213
214             if (nextchr != ERR) {
215                 keycode = curses2keycode[nextchr];
216                 nextchr = ERR;
217                 if (keycode == -1)
218                     continue;
219
220                 keycode |= ALT;
221
222                 /* process keys reserved for qemu */
223                 if (keycode >= QEMU_KEY_CONSOLE0 &&
224                         keycode < QEMU_KEY_CONSOLE0 + 9) {
225                     erase();
226                     wnoutrefresh(stdscr);
227                     console_select(keycode - QEMU_KEY_CONSOLE0);
228
229                     invalidate = 1;
230                     continue;
231                 }
232             }
233         }
234
235         if (kbd_layout && !(keycode & GREY)) {
236             keysym = keycode2keysym[keycode & KEY_MASK];
237             if (keysym == -1)
238                 keysym = chr;
239
240             keycode &= ~KEY_MASK;
241             keycode |= keysym2scancode(kbd_layout, keysym);
242         }
243
244         if (is_graphic_console()) {
245             /* since terminals don't know about key press and release
246              * events, we need to emit both for each key received */
247             if (keycode & SHIFT)
248                 kbd_put_keycode(SHIFT_CODE);
249             if (keycode & CNTRL)
250                 kbd_put_keycode(CNTRL_CODE);
251             if (keycode & ALT)
252                 kbd_put_keycode(ALT_CODE);
253             if (keycode & GREY)
254                 kbd_put_keycode(GREY_CODE);
255             kbd_put_keycode(keycode & KEY_MASK);
256             if (keycode & GREY)
257                 kbd_put_keycode(GREY_CODE);
258             kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
259             if (keycode & ALT)
260                 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
261             if (keycode & CNTRL)
262                 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
263             if (keycode & SHIFT)
264                 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
265         } else {
266             keysym = curses2keysym[chr];
267             if (keysym == -1)
268                 keysym = chr;
269
270             kbd_put_keysym(keysym);
271         }
272     }
273 }
274
275 static void curses_cleanup(void *opaque) 
276 {
277     endwin();
278 }
279
280 static void curses_atexit(void)
281 {
282     curses_cleanup(NULL);
283 }
284
285 static void curses_setup(void)
286 {
287     int i, colour_default[8] = {
288         COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
289         COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
290     };
291
292     /* input as raw as possible, let everything be interpreted
293      * by the guest system */
294     initscr(); noecho(); intrflush(stdscr, FALSE);
295     nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
296     start_color(); raw(); scrollok(stdscr, FALSE);
297
298     for (i = 0; i < 64; i ++)
299         init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
300 }
301
302 static void curses_keyboard_setup(void)
303 {
304     int i, keycode, keysym;
305
306 #if defined(__APPLE__)
307     /* always use generic keymaps */
308     if (!keyboard_layout)
309         keyboard_layout = "en-us";
310 #endif
311     if(keyboard_layout) {
312         kbd_layout = init_keyboard_layout(keyboard_layout);
313         if (!kbd_layout)
314             exit(1);
315     }
316
317     for (i = 0; i < CURSES_KEYS; i ++)
318         keycode2keysym[i] = -1;
319
320     for (i = 0; i < CURSES_KEYS; i ++) {
321         if (curses2keycode[i] == -1)
322             continue;
323
324         keycode = curses2keycode[i] & KEY_MASK;
325         if (keycode2keysym[keycode] >= 0)
326             continue;
327
328         for (keysym = 0; keysym < CURSES_KEYS; keysym ++)
329             if (curses2keycode[keysym] == keycode) {
330                 keycode2keysym[keycode] = keysym;
331                 break;
332             }
333
334         if (keysym >= CURSES_KEYS)
335             keycode2keysym[keycode] = i;
336     }
337 }
338
339 void curses_display_init(DisplayState *ds, int full_screen)
340 {
341 #ifndef _WIN32
342     if (!isatty(1)) {
343         fprintf(stderr, "We need a terminal output\n");
344         exit(1);
345     }
346 #endif
347
348     curses_setup();
349     curses_keyboard_setup();
350     atexit(curses_atexit);
351
352 #ifndef _WIN32
353 #if defined(SIGWINCH) && defined(KEY_RESIZE)
354     /* some curses implementations provide a handler, but we
355      * want to be sure this is handled regardless of the library */
356     signal(SIGWINCH, curses_winch_handler);
357 #endif
358 #endif
359
360     ds->data = (void *) screen;
361     ds->linesize = 0;
362     ds->depth = 0;
363     ds->width = 640;
364     ds->height = 400;
365     ds->dpy_update = curses_update;
366     ds->dpy_resize = curses_resize;
367     ds->dpy_refresh = curses_refresh;
368     ds->dpy_text_cursor = curses_cursor_position;
369
370     invalidate = 1;
371
372     /* Standard VGA initial text mode dimensions */
373     curses_resize(ds, 80, 25);
374 }