blob: 03cefdf470b24e47f75e6bf8cb6bacea42803f66 [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
26#ifndef _WIN32
balrog4d3b6f62008-02-10 16:33:14 +000027#include <sys/ioctl.h>
28#include <termios.h>
29#endif
30
blueswir1511d2b12009-03-07 15:32:56 +000031#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010032#include "ui/console.h"
Gerd Hoffmanncd100322013-12-04 13:40:20 +010033#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000035
balrog4d3b6f62008-02-10 16:33:14 +000036#define FONT_HEIGHT 16
37#define FONT_WIDTH 8
38
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010039static DisplayChangeListener *dcl;
Anthony Liguoric227f092009-10-01 16:12:16 -050040static console_ch_t screen[160 * 100];
balrog4d3b6f62008-02-10 16:33:14 +000041static WINDOW *screenpad = NULL;
42static int width, height, gwidth, gheight, invalidate;
43static int px, py, sminx, sminy, smaxx, smaxy;
44
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +090045chtype vga_to_curses[256];
46
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010047static void curses_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010048 int x, int y, int w, int h)
balrog4d3b6f62008-02-10 16:33:14 +000049{
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
60static void curses_calc_pad(void)
61{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +010062 if (qemu_console_is_fixedsize(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +000063 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
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010099static void curses_resize(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100100 int width, int height)
balrog4d3b6f62008-02-10 16:33:14 +0000101{
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200102 if (width == gwidth && height == gheight) {
balrog4d3b6f62008-02-10 16:33:14 +0000103 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200104 }
balrog4d3b6f62008-02-10 16:33:14 +0000105
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200106 gwidth = width;
107 gheight = height;
balrog4d3b6f62008-02-10 16:33:14 +0000108
109 curses_calc_pad();
110}
111
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100112#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
113static volatile sig_atomic_t got_sigwinch;
114static void curses_winch_check(void)
balrog4d3b6f62008-02-10 16:33:14 +0000115{
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
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100123 if (!got_sigwinch) {
balrog4d3b6f62008-02-10 16:33:14 +0000124 return;
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100125 }
126 got_sigwinch = false;
127
128 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
129 return;
130 }
balrog4d3b6f62008-02-10 16:33:14 +0000131
132 resize_term(ws.ws_row, ws.ws_col);
balrog4d3b6f62008-02-10 16:33:14 +0000133 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000134}
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100135
136static void curses_winch_handler(int signum)
137{
138 got_sigwinch = true;
139}
140
141static 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
149static void curses_winch_check(void) {}
150static void curses_winch_init(void) {}
balrog4d3b6f62008-02-10 16:33:14 +0000151#endif
152
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100153static void curses_cursor_position(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100154 int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000155{
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 */
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100165 if (!qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000166 curs_set(2);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100167 }
balrog4d3b6f62008-02-10 16:33:14 +0000168 return;
169 }
170 }
171
172 curs_set(0);
173}
174
175/* generic keyboard conversion */
176
177#include "curses_keys.h"
balrog4d3b6f62008-02-10 16:33:14 +0000178
Anthony Liguoric227f092009-10-01 16:12:16 -0500179static kbd_layout_t *kbd_layout = NULL;
balrog4d3b6f62008-02-10 16:33:14 +0000180
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100181static void curses_refresh(DisplayChangeListener *dcl)
balrog4d3b6f62008-02-10 16:33:14 +0000182{
Peter Maydell99a9ef42016-08-11 15:23:27 +0100183 int chr, keysym, keycode, keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000184
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100185 curses_winch_check();
186
balrog4d3b6f62008-02-10 16:33:14 +0000187 if (invalidate) {
188 clear();
189 refresh();
190 curses_calc_pad();
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100191 graphic_hw_invalidate(NULL);
balrog4d3b6f62008-02-10 16:33:14 +0000192 invalidate = 0;
193 }
194
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100195 graphic_hw_text_update(NULL, screen);
balrog4d3b6f62008-02-10 16:33:14 +0000196
balrog4d3b6f62008-02-10 16:33:14 +0000197 while (1) {
198 /* while there are any pending key strokes to process */
Peter Maydell99a9ef42016-08-11 15:23:27 +0100199 chr = getch();
balrog4d3b6f62008-02-10 16:33:14 +0000200
201 if (chr == ERR)
202 break;
203
balrogb1314cf2008-02-22 18:21:28 +0000204#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000205 /* this shouldn't occur when we use a custom SIGWINCH handler */
206 if (chr == KEY_RESIZE) {
207 clear();
208 refresh();
209 curses_calc_pad();
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100210 curses_update(dcl, 0, 0, width, height);
balrog4d3b6f62008-02-10 16:33:14 +0000211 continue;
212 }
balrogb1314cf2008-02-22 18:21:28 +0000213#endif
balrog4d3b6f62008-02-10 16:33:14 +0000214
215 keycode = curses2keycode[chr];
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100216 keycode_alt = 0;
balrog4d3b6f62008-02-10 16:33:14 +0000217
218 /* alt key */
219 if (keycode == 1) {
Peter Maydell99a9ef42016-08-11 15:23:27 +0100220 int nextchr = getch();
balrog4d3b6f62008-02-10 16:33:14 +0000221
222 if (nextchr != ERR) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100223 chr = nextchr;
224 keycode_alt = ALT;
Peter Maydell99a9ef42016-08-11 15:23:27 +0100225 keycode = curses2keycode[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000226
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100227 if (keycode != -1) {
228 keycode |= ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000229
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100230 /* process keys reserved for qemu */
231 if (keycode >= QEMU_KEY_CONSOLE0 &&
232 keycode < QEMU_KEY_CONSOLE0 + 9) {
233 erase();
234 wnoutrefresh(stdscr);
235 console_select(keycode - QEMU_KEY_CONSOLE0);
balrog4d3b6f62008-02-10 16:33:14 +0000236
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100237 invalidate = 1;
238 continue;
239 }
balrog4d3b6f62008-02-10 16:33:14 +0000240 }
241 }
242 }
243
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100244 if (kbd_layout) {
245 keysym = -1;
246 if (chr < CURSES_KEYS)
247 keysym = curses2keysym[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000248
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100249 if (keysym == -1) {
Samuel Thibaultd03703c2010-10-19 19:48:20 +0200250 if (chr < ' ') {
251 keysym = chr + '@';
252 if (keysym >= 'A' && keysym <= 'Z')
253 keysym += 'a' - 'A';
254 keysym |= KEYSYM_CNTRL;
255 } else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100256 keysym = chr;
257 }
258
259 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
260 if (keycode == 0)
261 continue;
262
263 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
264 keycode |= keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000265 }
266
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100267 if (keycode == -1)
268 continue;
269
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100270 if (qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000271 /* since terminals don't know about key press and release
272 * events, we need to emit both for each key received */
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100273 if (keycode & SHIFT) {
274 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200275 qemu_input_event_send_key_delay(0);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100276 }
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100277 if (keycode & CNTRL) {
278 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200279 qemu_input_event_send_key_delay(0);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100280 }
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100281 if (keycode & ALT) {
282 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200283 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100284 }
285 if (keycode & ALTGR) {
286 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200287 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100288 }
289
Andrew Oatesf5c0ab12014-05-23 20:16:09 -0400290 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200291 qemu_input_event_send_key_delay(0);
Andrew Oatesf5c0ab12014-05-23 20:16:09 -0400292 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200293 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100294
295 if (keycode & ALTGR) {
296 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200297 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100298 }
299 if (keycode & ALT) {
300 qemu_input_event_send_key_number(NULL, ALT_CODE, 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 & CNTRL) {
304 qemu_input_event_send_key_number(NULL, CNTRL_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 & SHIFT) {
308 qemu_input_event_send_key_number(NULL, SHIFT_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 }
balrog4d3b6f62008-02-10 16:33:14 +0000311 } else {
Peter Maydellbba4e1b2016-08-11 15:23:26 +0100312 keysym = -1;
313 if (chr < CURSES_KEYS) {
314 keysym = curses2qemu[chr];
315 }
balrog4d3b6f62008-02-10 16:33:14 +0000316 if (keysym == -1)
317 keysym = chr;
318
319 kbd_put_keysym(keysym);
320 }
321 }
322}
323
Blue Swirlaaf12c22010-03-21 19:44:06 +0000324static void curses_atexit(void)
balrog4d3b6f62008-02-10 16:33:14 +0000325{
326 endwin();
327}
328
balrog4d3b6f62008-02-10 16:33:14 +0000329static void curses_setup(void)
330{
331 int i, colour_default[8] = {
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900332 [QEMU_COLOR_BLACK] = COLOR_BLACK,
333 [QEMU_COLOR_BLUE] = COLOR_BLUE,
334 [QEMU_COLOR_GREEN] = COLOR_GREEN,
335 [QEMU_COLOR_CYAN] = COLOR_CYAN,
336 [QEMU_COLOR_RED] = COLOR_RED,
337 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
338 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
339 [QEMU_COLOR_WHITE] = COLOR_WHITE,
balrog4d3b6f62008-02-10 16:33:14 +0000340 };
341
342 /* input as raw as possible, let everything be interpreted
343 * by the guest system */
344 initscr(); noecho(); intrflush(stdscr, FALSE);
345 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
346 start_color(); raw(); scrollok(stdscr, FALSE);
347
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900348 /* Make color pair to match color format (3bits bg:3bits fg) */
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900349 for (i = 0; i < 64; i++) {
balrog4d3b6f62008-02-10 16:33:14 +0000350 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900351 }
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900352 /* Set default color for more than 64 for safety. */
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900353 for (i = 64; i < COLOR_PAIRS; i++) {
354 init_pair(i, COLOR_WHITE, COLOR_BLACK);
355 }
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900356
357 /*
358 * Setup mapping for vga to curses line graphics.
359 * FIXME: for better font, have to use ncursesw and setlocale()
360 */
361#if 0
362 /* FIXME: map from where? */
363 ACS_S1;
364 ACS_S3;
365 ACS_S7;
366 ACS_S9;
367#endif
368 /* ACS_* is not constant. So, we can't initialize statically. */
369 vga_to_curses['\0'] = ' ';
370 vga_to_curses[0x04] = ACS_DIAMOND;
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900371 vga_to_curses[0x18] = ACS_UARROW;
372 vga_to_curses[0x19] = ACS_DARROW;
Samuel Thibault697783a2016-10-15 21:53:04 +0200373 vga_to_curses[0x1a] = ACS_RARROW;
374 vga_to_curses[0x1b] = ACS_LARROW;
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900375 vga_to_curses[0x9c] = ACS_STERLING;
376 vga_to_curses[0xb0] = ACS_BOARD;
377 vga_to_curses[0xb1] = ACS_CKBOARD;
378 vga_to_curses[0xb3] = ACS_VLINE;
379 vga_to_curses[0xb4] = ACS_RTEE;
380 vga_to_curses[0xbf] = ACS_URCORNER;
381 vga_to_curses[0xc0] = ACS_LLCORNER;
382 vga_to_curses[0xc1] = ACS_BTEE;
383 vga_to_curses[0xc2] = ACS_TTEE;
384 vga_to_curses[0xc3] = ACS_LTEE;
385 vga_to_curses[0xc4] = ACS_HLINE;
386 vga_to_curses[0xc5] = ACS_PLUS;
387 vga_to_curses[0xce] = ACS_LANTERN;
388 vga_to_curses[0xd8] = ACS_NEQUAL;
389 vga_to_curses[0xd9] = ACS_LRCORNER;
390 vga_to_curses[0xda] = ACS_ULCORNER;
391 vga_to_curses[0xdb] = ACS_BLOCK;
392 vga_to_curses[0xe3] = ACS_PI;
393 vga_to_curses[0xf1] = ACS_PLMINUS;
394 vga_to_curses[0xf2] = ACS_GEQUAL;
395 vga_to_curses[0xf3] = ACS_LEQUAL;
396 vga_to_curses[0xf8] = ACS_DEGREE;
397 vga_to_curses[0xfe] = ACS_BULLET;
balrog4d3b6f62008-02-10 16:33:14 +0000398}
399
400static void curses_keyboard_setup(void)
401{
balrog4d3b6f62008-02-10 16:33:14 +0000402#if defined(__APPLE__)
403 /* always use generic keymaps */
404 if (!keyboard_layout)
405 keyboard_layout = "en-us";
406#endif
407 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000408 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
balrog4d3b6f62008-02-10 16:33:14 +0000409 if (!kbd_layout)
410 exit(1);
411 }
balrog4d3b6f62008-02-10 16:33:14 +0000412}
413
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100414static const DisplayChangeListenerOps dcl_ops = {
415 .dpy_name = "curses",
416 .dpy_text_update = curses_update,
417 .dpy_text_resize = curses_resize,
418 .dpy_refresh = curses_refresh,
419 .dpy_text_cursor = curses_cursor_position,
420};
421
balrog4d3b6f62008-02-10 16:33:14 +0000422void curses_display_init(DisplayState *ds, int full_screen)
423{
424#ifndef _WIN32
425 if (!isatty(1)) {
426 fprintf(stderr, "We need a terminal output\n");
427 exit(1);
428 }
429#endif
430
431 curses_setup();
432 curses_keyboard_setup();
Anthony Liguori28695482010-03-21 14:13:02 -0500433 atexit(curses_atexit);
balrog4d3b6f62008-02-10 16:33:14 +0000434
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100435 curses_winch_init();
balrog4d3b6f62008-02-10 16:33:14 +0000436
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100437 dcl = g_new0(DisplayChangeListener, 1);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100438 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +0200439 register_displaychangelistener(dcl);
balrog4d3b6f62008-02-10 16:33:14 +0000440
441 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000442}