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