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