blob: b47558956c1436b5cf26f4c77b9fa4d670a96b5b [file] [log] [blame]
balrog4d3b6f62008-02-10 16:33:14 +00001/*
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 */
Peter Maydelle16f4c82016-01-29 17:49:51 +000024#include "qemu/osdep.h"
balrog4d3b6f62008-02-10 16:33:14 +000025#include <curses.h>
26
27#ifndef _WIN32
balrog4d3b6f62008-02-10 16:33:14 +000028#include <sys/ioctl.h>
29#include <termios.h>
30#endif
31
blueswir1511d2b12009-03-07 15:32:56 +000032#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010033#include "ui/console.h"
Gerd Hoffmanncd100322013-12-04 13:40:20 +010034#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000036
balrog4d3b6f62008-02-10 16:33:14 +000037#define FONT_HEIGHT 16
38#define FONT_WIDTH 8
39
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010040static DisplayChangeListener *dcl;
Anthony Liguoric227f092009-10-01 16:12:16 -050041static console_ch_t screen[160 * 100];
balrog4d3b6f62008-02-10 16:33:14 +000042static WINDOW *screenpad = NULL;
43static int width, height, gwidth, gheight, invalidate;
44static int px, py, sminx, sminy, smaxx, smaxy;
45
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +090046chtype vga_to_curses[256];
47
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010048static void curses_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010049 int x, int y, int w, int h)
balrog4d3b6f62008-02-10 16:33:14 +000050{
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
61static void curses_calc_pad(void)
62{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +010063 if (qemu_console_is_fixedsize(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +000064 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
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100100static void curses_resize(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100101 int width, int height)
balrog4d3b6f62008-02-10 16:33:14 +0000102{
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200103 if (width == gwidth && height == gheight) {
balrog4d3b6f62008-02-10 16:33:14 +0000104 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200105 }
balrog4d3b6f62008-02-10 16:33:14 +0000106
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200107 gwidth = width;
108 gheight = height;
balrog4d3b6f62008-02-10 16:33:14 +0000109
110 curses_calc_pad();
111}
112
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100113#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
114static volatile sig_atomic_t got_sigwinch;
115static void curses_winch_check(void)
balrog4d3b6f62008-02-10 16:33:14 +0000116{
117 struct winsize {
118 unsigned short ws_row;
119 unsigned short ws_col;
120 unsigned short ws_xpixel; /* unused */
121 unsigned short ws_ypixel; /* unused */
122 } ws;
123
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100124 if (!got_sigwinch) {
balrog4d3b6f62008-02-10 16:33:14 +0000125 return;
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100126 }
127 got_sigwinch = false;
128
129 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
130 return;
131 }
balrog4d3b6f62008-02-10 16:33:14 +0000132
133 resize_term(ws.ws_row, ws.ws_col);
balrog4d3b6f62008-02-10 16:33:14 +0000134 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000135}
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100136
137static void curses_winch_handler(int signum)
138{
139 got_sigwinch = true;
140}
141
142static void curses_winch_init(void)
143{
144 struct sigaction old, winch = {
145 .sa_handler = curses_winch_handler,
146 };
147 sigaction(SIGWINCH, &winch, &old);
148}
149#else
150static void curses_winch_check(void) {}
151static void curses_winch_init(void) {}
balrog4d3b6f62008-02-10 16:33:14 +0000152#endif
153
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100154static void curses_cursor_position(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100155 int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000156{
157 if (x >= 0) {
158 x = sminx + x - px;
159 y = sminy + y - py;
160
161 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
162 move(y, x);
163 curs_set(1);
164 /* it seems that curs_set(1) must always be called before
165 * curs_set(2) for the latter to have effect */
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100166 if (!qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000167 curs_set(2);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100168 }
balrog4d3b6f62008-02-10 16:33:14 +0000169 return;
170 }
171 }
172
173 curs_set(0);
174}
175
176/* generic keyboard conversion */
177
178#include "curses_keys.h"
balrog4d3b6f62008-02-10 16:33:14 +0000179
Anthony Liguoric227f092009-10-01 16:12:16 -0500180static kbd_layout_t *kbd_layout = NULL;
balrog4d3b6f62008-02-10 16:33:14 +0000181
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100182static void curses_refresh(DisplayChangeListener *dcl)
balrog4d3b6f62008-02-10 16:33:14 +0000183{
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100184 int chr, nextchr, keysym, keycode, keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000185
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100186 curses_winch_check();
187
balrog4d3b6f62008-02-10 16:33:14 +0000188 if (invalidate) {
189 clear();
190 refresh();
191 curses_calc_pad();
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100192 graphic_hw_invalidate(NULL);
balrog4d3b6f62008-02-10 16:33:14 +0000193 invalidate = 0;
194 }
195
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100196 graphic_hw_text_update(NULL, screen);
balrog4d3b6f62008-02-10 16:33:14 +0000197
198 nextchr = ERR;
199 while (1) {
200 /* while there are any pending key strokes to process */
201 if (nextchr == ERR)
202 chr = getch();
203 else {
204 chr = nextchr;
205 nextchr = ERR;
206 }
207
208 if (chr == ERR)
209 break;
210
balrogb1314cf2008-02-22 18:21:28 +0000211#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000212 /* this shouldn't occur when we use a custom SIGWINCH handler */
213 if (chr == KEY_RESIZE) {
214 clear();
215 refresh();
216 curses_calc_pad();
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100217 curses_update(dcl, 0, 0, width, height);
balrog4d3b6f62008-02-10 16:33:14 +0000218 continue;
219 }
balrogb1314cf2008-02-22 18:21:28 +0000220#endif
balrog4d3b6f62008-02-10 16:33:14 +0000221
222 keycode = curses2keycode[chr];
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100223 keycode_alt = 0;
balrog4d3b6f62008-02-10 16:33:14 +0000224
225 /* alt key */
226 if (keycode == 1) {
227 nextchr = getch();
228
229 if (nextchr != ERR) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100230 chr = nextchr;
231 keycode_alt = ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000232 keycode = curses2keycode[nextchr];
233 nextchr = ERR;
balrog4d3b6f62008-02-10 16:33:14 +0000234
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100235 if (keycode != -1) {
236 keycode |= ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000237
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100238 /* process keys reserved for qemu */
239 if (keycode >= QEMU_KEY_CONSOLE0 &&
240 keycode < QEMU_KEY_CONSOLE0 + 9) {
241 erase();
242 wnoutrefresh(stdscr);
243 console_select(keycode - QEMU_KEY_CONSOLE0);
balrog4d3b6f62008-02-10 16:33:14 +0000244
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100245 invalidate = 1;
246 continue;
247 }
balrog4d3b6f62008-02-10 16:33:14 +0000248 }
249 }
250 }
251
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100252 if (kbd_layout) {
253 keysym = -1;
254 if (chr < CURSES_KEYS)
255 keysym = curses2keysym[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000256
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100257 if (keysym == -1) {
Samuel Thibaultd03703c2010-10-19 19:48:20 +0200258 if (chr < ' ') {
259 keysym = chr + '@';
260 if (keysym >= 'A' && keysym <= 'Z')
261 keysym += 'a' - 'A';
262 keysym |= KEYSYM_CNTRL;
263 } else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100264 keysym = chr;
265 }
266
267 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
268 if (keycode == 0)
269 continue;
270
271 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
272 keycode |= keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000273 }
274
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100275 if (keycode == -1)
276 continue;
277
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100278 if (qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000279 /* since terminals don't know about key press and release
280 * events, we need to emit both for each key received */
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100281 if (keycode & SHIFT) {
282 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200283 qemu_input_event_send_key_delay(0);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100284 }
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100285 if (keycode & CNTRL) {
286 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200287 qemu_input_event_send_key_delay(0);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100288 }
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100289 if (keycode & ALT) {
290 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200291 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100292 }
293 if (keycode & ALTGR) {
294 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200295 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100296 }
297
Andrew Oatesf5c0ab12014-05-23 20:16:09 -0400298 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200299 qemu_input_event_send_key_delay(0);
Andrew Oatesf5c0ab12014-05-23 20:16:09 -0400300 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200301 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100302
303 if (keycode & ALTGR) {
304 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200305 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100306 }
307 if (keycode & ALT) {
308 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200309 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100310 }
311 if (keycode & CNTRL) {
312 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200313 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100314 }
315 if (keycode & SHIFT) {
316 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200317 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100318 }
balrog4d3b6f62008-02-10 16:33:14 +0000319 } else {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100320 keysym = curses2qemu[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000321 if (keysym == -1)
322 keysym = chr;
323
324 kbd_put_keysym(keysym);
325 }
326 }
327}
328
Blue Swirlaaf12c22010-03-21 19:44:06 +0000329static void curses_atexit(void)
balrog4d3b6f62008-02-10 16:33:14 +0000330{
331 endwin();
332}
333
balrog4d3b6f62008-02-10 16:33:14 +0000334static void curses_setup(void)
335{
336 int i, colour_default[8] = {
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900337 [QEMU_COLOR_BLACK] = COLOR_BLACK,
338 [QEMU_COLOR_BLUE] = COLOR_BLUE,
339 [QEMU_COLOR_GREEN] = COLOR_GREEN,
340 [QEMU_COLOR_CYAN] = COLOR_CYAN,
341 [QEMU_COLOR_RED] = COLOR_RED,
342 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
343 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
344 [QEMU_COLOR_WHITE] = COLOR_WHITE,
balrog4d3b6f62008-02-10 16:33:14 +0000345 };
346
347 /* input as raw as possible, let everything be interpreted
348 * by the guest system */
349 initscr(); noecho(); intrflush(stdscr, FALSE);
350 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
351 start_color(); raw(); scrollok(stdscr, FALSE);
352
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900353 /* Make color pair to match color format (3bits bg:3bits fg) */
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900354 for (i = 0; i < 64; i++) {
balrog4d3b6f62008-02-10 16:33:14 +0000355 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900356 }
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900357 /* Set default color for more than 64 for safety. */
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900358 for (i = 64; i < COLOR_PAIRS; i++) {
359 init_pair(i, COLOR_WHITE, COLOR_BLACK);
360 }
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900361
362 /*
363 * Setup mapping for vga to curses line graphics.
364 * FIXME: for better font, have to use ncursesw and setlocale()
365 */
366#if 0
367 /* FIXME: map from where? */
368 ACS_S1;
369 ACS_S3;
370 ACS_S7;
371 ACS_S9;
372#endif
373 /* ACS_* is not constant. So, we can't initialize statically. */
374 vga_to_curses['\0'] = ' ';
375 vga_to_curses[0x04] = ACS_DIAMOND;
376 vga_to_curses[0x0a] = ACS_RARROW;
377 vga_to_curses[0x0b] = ACS_LARROW;
378 vga_to_curses[0x18] = ACS_UARROW;
379 vga_to_curses[0x19] = ACS_DARROW;
380 vga_to_curses[0x9c] = ACS_STERLING;
381 vga_to_curses[0xb0] = ACS_BOARD;
382 vga_to_curses[0xb1] = ACS_CKBOARD;
383 vga_to_curses[0xb3] = ACS_VLINE;
384 vga_to_curses[0xb4] = ACS_RTEE;
385 vga_to_curses[0xbf] = ACS_URCORNER;
386 vga_to_curses[0xc0] = ACS_LLCORNER;
387 vga_to_curses[0xc1] = ACS_BTEE;
388 vga_to_curses[0xc2] = ACS_TTEE;
389 vga_to_curses[0xc3] = ACS_LTEE;
390 vga_to_curses[0xc4] = ACS_HLINE;
391 vga_to_curses[0xc5] = ACS_PLUS;
392 vga_to_curses[0xce] = ACS_LANTERN;
393 vga_to_curses[0xd8] = ACS_NEQUAL;
394 vga_to_curses[0xd9] = ACS_LRCORNER;
395 vga_to_curses[0xda] = ACS_ULCORNER;
396 vga_to_curses[0xdb] = ACS_BLOCK;
397 vga_to_curses[0xe3] = ACS_PI;
398 vga_to_curses[0xf1] = ACS_PLMINUS;
399 vga_to_curses[0xf2] = ACS_GEQUAL;
400 vga_to_curses[0xf3] = ACS_LEQUAL;
401 vga_to_curses[0xf8] = ACS_DEGREE;
402 vga_to_curses[0xfe] = ACS_BULLET;
balrog4d3b6f62008-02-10 16:33:14 +0000403}
404
405static void curses_keyboard_setup(void)
406{
balrog4d3b6f62008-02-10 16:33:14 +0000407#if defined(__APPLE__)
408 /* always use generic keymaps */
409 if (!keyboard_layout)
410 keyboard_layout = "en-us";
411#endif
412 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000413 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
balrog4d3b6f62008-02-10 16:33:14 +0000414 if (!kbd_layout)
415 exit(1);
416 }
balrog4d3b6f62008-02-10 16:33:14 +0000417}
418
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100419static const DisplayChangeListenerOps dcl_ops = {
420 .dpy_name = "curses",
421 .dpy_text_update = curses_update,
422 .dpy_text_resize = curses_resize,
423 .dpy_refresh = curses_refresh,
424 .dpy_text_cursor = curses_cursor_position,
425};
426
balrog4d3b6f62008-02-10 16:33:14 +0000427void curses_display_init(DisplayState *ds, int full_screen)
428{
429#ifndef _WIN32
430 if (!isatty(1)) {
431 fprintf(stderr, "We need a terminal output\n");
432 exit(1);
433 }
434#endif
435
436 curses_setup();
437 curses_keyboard_setup();
Anthony Liguori28695482010-03-21 14:13:02 -0500438 atexit(curses_atexit);
balrog4d3b6f62008-02-10 16:33:14 +0000439
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100440 curses_winch_init();
balrog4d3b6f62008-02-10 16:33:14 +0000441
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100442 dcl = g_new0(DisplayChangeListener, 1);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100443 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +0200444 register_displaychangelistener(dcl);
balrog4d3b6f62008-02-10 16:33:14 +0000445
446 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000447}