blob: 37954ce1b0460f70a36f63a956bfa673e704d955 [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
Fei Liab4f9312018-10-17 10:26:50 +020031#include "qapi/error.h"
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
Gerd Hoffmanne2f82e92017-09-27 12:38:11 +020037/* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
38#undef KEY_EVENT
39#include <curses.h>
40#undef KEY_EVENT
41
balrog4d3b6f62008-02-10 16:33:14 +000042#define FONT_HEIGHT 16
43#define FONT_WIDTH 8
44
Samuel Thibault459a7072019-03-04 22:05:32 +010045enum maybe_keycode {
46 CURSES_KEYCODE,
47 CURSES_CHAR,
48 CURSES_CHAR_OR_KEYCODE,
49};
50
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010051static DisplayChangeListener *dcl;
Anthony Liguoric227f092009-10-01 16:12:16 -050052static console_ch_t screen[160 * 100];
balrog4d3b6f62008-02-10 16:33:14 +000053static WINDOW *screenpad = NULL;
54static int width, height, gwidth, gheight, invalidate;
55static int px, py, sminx, sminy, smaxx, smaxy;
56
Gerd Hoffmanne2f82e92017-09-27 12:38:11 +020057static chtype vga_to_curses[256];
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +090058
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010059static void curses_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010060 int x, int y, int w, int h)
balrog4d3b6f62008-02-10 16:33:14 +000061{
Gerd Hoffmanne2f82e92017-09-27 12:38:11 +020062 console_ch_t *line;
63 chtype curses_line[width];
balrog4d3b6f62008-02-10 16:33:14 +000064
Gerd Hoffmanne2f82e92017-09-27 12:38:11 +020065 line = screen + y * width;
66 for (h += y; y < h; y ++, line += width) {
67 for (x = 0; x < width; x++) {
68 chtype ch = line[x] & 0xff;
69 chtype at = line[x] & ~0xff;
70 if (vga_to_curses[ch]) {
71 ch = vga_to_curses[ch];
72 }
73 curses_line[x] = ch | at;
74 }
75 mvwaddchnstr(screenpad, y, 0, curses_line, width);
76 }
balrog4d3b6f62008-02-10 16:33:14 +000077
78 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
79 refresh();
80}
81
82static void curses_calc_pad(void)
83{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +010084 if (qemu_console_is_fixedsize(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +000085 width = gwidth;
86 height = gheight;
87 } else {
88 width = COLS;
89 height = LINES;
90 }
91
92 if (screenpad)
93 delwin(screenpad);
94
95 clear();
96 refresh();
97
98 screenpad = newpad(height, width);
99
100 if (width > COLS) {
101 px = (width - COLS) / 2;
102 sminx = 0;
103 smaxx = COLS;
104 } else {
105 px = 0;
106 sminx = (COLS - width) / 2;
107 smaxx = sminx + width;
108 }
109
110 if (height > LINES) {
111 py = (height - LINES) / 2;
112 sminy = 0;
113 smaxy = LINES;
114 } else {
115 py = 0;
116 sminy = (LINES - height) / 2;
117 smaxy = sminy + height;
118 }
119}
120
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100121static void curses_resize(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100122 int width, int height)
balrog4d3b6f62008-02-10 16:33:14 +0000123{
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200124 if (width == gwidth && height == gheight) {
balrog4d3b6f62008-02-10 16:33:14 +0000125 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200126 }
balrog4d3b6f62008-02-10 16:33:14 +0000127
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200128 gwidth = width;
129 gheight = height;
balrog4d3b6f62008-02-10 16:33:14 +0000130
131 curses_calc_pad();
132}
133
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100134#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
135static volatile sig_atomic_t got_sigwinch;
136static void curses_winch_check(void)
balrog4d3b6f62008-02-10 16:33:14 +0000137{
138 struct winsize {
139 unsigned short ws_row;
140 unsigned short ws_col;
141 unsigned short ws_xpixel; /* unused */
142 unsigned short ws_ypixel; /* unused */
143 } ws;
144
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100145 if (!got_sigwinch) {
balrog4d3b6f62008-02-10 16:33:14 +0000146 return;
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100147 }
148 got_sigwinch = false;
149
150 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
151 return;
152 }
balrog4d3b6f62008-02-10 16:33:14 +0000153
154 resize_term(ws.ws_row, ws.ws_col);
balrog4d3b6f62008-02-10 16:33:14 +0000155 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000156}
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100157
158static void curses_winch_handler(int signum)
159{
160 got_sigwinch = true;
161}
162
163static void curses_winch_init(void)
164{
165 struct sigaction old, winch = {
166 .sa_handler = curses_winch_handler,
167 };
168 sigaction(SIGWINCH, &winch, &old);
169}
170#else
171static void curses_winch_check(void) {}
172static void curses_winch_init(void) {}
balrog4d3b6f62008-02-10 16:33:14 +0000173#endif
174
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100175static void curses_cursor_position(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100176 int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000177{
178 if (x >= 0) {
179 x = sminx + x - px;
180 y = sminy + y - py;
181
182 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
183 move(y, x);
184 curs_set(1);
185 /* it seems that curs_set(1) must always be called before
186 * curs_set(2) for the latter to have effect */
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100187 if (!qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000188 curs_set(2);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100189 }
balrog4d3b6f62008-02-10 16:33:14 +0000190 return;
191 }
192 }
193
194 curs_set(0);
195}
196
197/* generic keyboard conversion */
198
199#include "curses_keys.h"
balrog4d3b6f62008-02-10 16:33:14 +0000200
Anthony Liguoric227f092009-10-01 16:12:16 -0500201static kbd_layout_t *kbd_layout = NULL;
balrog4d3b6f62008-02-10 16:33:14 +0000202
Samuel Thibault459a7072019-03-04 22:05:32 +0100203static wint_t console_getch(enum maybe_keycode *maybe_keycode)
204{
205 wint_t ret;
206 switch (get_wch(&ret)) {
207 case KEY_CODE_YES:
208 *maybe_keycode = CURSES_KEYCODE;
209 break;
210 case OK:
211 *maybe_keycode = CURSES_CHAR;
212 break;
213 case ERR:
214 ret = -1;
215 break;
216 }
217 return ret;
218}
219
220static int curses2foo(const int _curses2foo[], const int _curseskey2foo[],
221 int chr, enum maybe_keycode maybe_keycode)
222{
223 int ret = -1;
224 if (maybe_keycode == CURSES_CHAR) {
225 if (chr < CURSES_CHARS) {
226 ret = _curses2foo[chr];
227 }
228 } else {
229 if (chr < CURSES_KEYS) {
230 ret = _curseskey2foo[chr];
231 }
232 if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE &&
233 chr < CURSES_CHARS) {
234 ret = _curses2foo[chr];
235 }
236 }
237 return ret;
238}
239
240#define curses2keycode(chr, maybe_keycode) \
241 curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
242#define curses2keysym(chr, maybe_keycode) \
243 curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
244#define curses2qemu(chr, maybe_keycode) \
245 curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
246
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100247static void curses_refresh(DisplayChangeListener *dcl)
balrog4d3b6f62008-02-10 16:33:14 +0000248{
Peter Maydell99a9ef42016-08-11 15:23:27 +0100249 int chr, keysym, keycode, keycode_alt;
Samuel Thibault459a7072019-03-04 22:05:32 +0100250 enum maybe_keycode maybe_keycode;
balrog4d3b6f62008-02-10 16:33:14 +0000251
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100252 curses_winch_check();
253
balrog4d3b6f62008-02-10 16:33:14 +0000254 if (invalidate) {
255 clear();
256 refresh();
257 curses_calc_pad();
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100258 graphic_hw_invalidate(NULL);
balrog4d3b6f62008-02-10 16:33:14 +0000259 invalidate = 0;
260 }
261
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100262 graphic_hw_text_update(NULL, screen);
balrog4d3b6f62008-02-10 16:33:14 +0000263
balrog4d3b6f62008-02-10 16:33:14 +0000264 while (1) {
265 /* while there are any pending key strokes to process */
Samuel Thibault459a7072019-03-04 22:05:32 +0100266 chr = console_getch(&maybe_keycode);
balrog4d3b6f62008-02-10 16:33:14 +0000267
Samuel Thibault459a7072019-03-04 22:05:32 +0100268 if (chr == -1)
balrog4d3b6f62008-02-10 16:33:14 +0000269 break;
270
balrogb1314cf2008-02-22 18:21:28 +0000271#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000272 /* this shouldn't occur when we use a custom SIGWINCH handler */
Samuel Thibault459a7072019-03-04 22:05:32 +0100273 if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) {
balrog4d3b6f62008-02-10 16:33:14 +0000274 clear();
275 refresh();
276 curses_calc_pad();
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100277 curses_update(dcl, 0, 0, width, height);
balrog4d3b6f62008-02-10 16:33:14 +0000278 continue;
279 }
balrogb1314cf2008-02-22 18:21:28 +0000280#endif
balrog4d3b6f62008-02-10 16:33:14 +0000281
Samuel Thibault459a7072019-03-04 22:05:32 +0100282 keycode = curses2keycode(chr, maybe_keycode);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100283 keycode_alt = 0;
balrog4d3b6f62008-02-10 16:33:14 +0000284
Samuel Thibault633786f2019-03-03 18:25:57 +0100285 /* alt or esc key */
balrog4d3b6f62008-02-10 16:33:14 +0000286 if (keycode == 1) {
Samuel Thibault459a7072019-03-04 22:05:32 +0100287 enum maybe_keycode next_maybe_keycode;
288 int nextchr = console_getch(&next_maybe_keycode);
balrog4d3b6f62008-02-10 16:33:14 +0000289
Samuel Thibault459a7072019-03-04 22:05:32 +0100290 if (nextchr != -1) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100291 chr = nextchr;
Samuel Thibault459a7072019-03-04 22:05:32 +0100292 maybe_keycode = next_maybe_keycode;
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100293 keycode_alt = ALT;
Samuel Thibault459a7072019-03-04 22:05:32 +0100294 keycode = curses2keycode(chr, maybe_keycode);
balrog4d3b6f62008-02-10 16:33:14 +0000295
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100296 if (keycode != -1) {
297 keycode |= ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000298
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100299 /* process keys reserved for qemu */
300 if (keycode >= QEMU_KEY_CONSOLE0 &&
301 keycode < QEMU_KEY_CONSOLE0 + 9) {
302 erase();
303 wnoutrefresh(stdscr);
304 console_select(keycode - QEMU_KEY_CONSOLE0);
balrog4d3b6f62008-02-10 16:33:14 +0000305
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100306 invalidate = 1;
307 continue;
308 }
balrog4d3b6f62008-02-10 16:33:14 +0000309 }
310 }
311 }
312
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100313 if (kbd_layout) {
Samuel Thibault459a7072019-03-04 22:05:32 +0100314 keysym = curses2keysym(chr, maybe_keycode);
balrog4d3b6f62008-02-10 16:33:14 +0000315
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100316 if (keysym == -1) {
Samuel Thibaultd03703c2010-10-19 19:48:20 +0200317 if (chr < ' ') {
318 keysym = chr + '@';
319 if (keysym >= 'A' && keysym <= 'Z')
320 keysym += 'a' - 'A';
321 keysym |= KEYSYM_CNTRL;
322 } else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100323 keysym = chr;
324 }
325
Gerd Hoffmannabb4f2c2018-02-22 08:05:13 +0100326 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
Gerd Hoffmann19c1b9f2019-01-22 10:28:14 +0100327 NULL, false);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100328 if (keycode == 0)
329 continue;
330
331 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
332 keycode |= keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000333 }
334
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100335 if (keycode == -1)
336 continue;
337
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100338 if (qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000339 /* since terminals don't know about key press and release
340 * events, we need to emit both for each key received */
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100341 if (keycode & SHIFT) {
342 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200343 qemu_input_event_send_key_delay(0);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100344 }
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100345 if (keycode & CNTRL) {
346 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200347 qemu_input_event_send_key_delay(0);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100348 }
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100349 if (keycode & ALT) {
350 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200351 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100352 }
353 if (keycode & ALTGR) {
354 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200355 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100356 }
357
Andrew Oatesf5c0ab12014-05-23 20:16:09 -0400358 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200359 qemu_input_event_send_key_delay(0);
Andrew Oatesf5c0ab12014-05-23 20:16:09 -0400360 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200361 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100362
363 if (keycode & ALTGR) {
364 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200365 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100366 }
367 if (keycode & ALT) {
368 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200369 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100370 }
371 if (keycode & CNTRL) {
372 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200373 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100374 }
375 if (keycode & SHIFT) {
376 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
Gerd Hoffmann5a165662014-05-28 13:05:04 +0200377 qemu_input_event_send_key_delay(0);
Gerd Hoffmanncd100322013-12-04 13:40:20 +0100378 }
balrog4d3b6f62008-02-10 16:33:14 +0000379 } else {
Samuel Thibault459a7072019-03-04 22:05:32 +0100380 keysym = curses2qemu(chr, maybe_keycode);
balrog4d3b6f62008-02-10 16:33:14 +0000381 if (keysym == -1)
382 keysym = chr;
383
384 kbd_put_keysym(keysym);
385 }
386 }
387}
388
Blue Swirlaaf12c22010-03-21 19:44:06 +0000389static void curses_atexit(void)
balrog4d3b6f62008-02-10 16:33:14 +0000390{
391 endwin();
392}
393
balrog4d3b6f62008-02-10 16:33:14 +0000394static void curses_setup(void)
395{
396 int i, colour_default[8] = {
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900397 [QEMU_COLOR_BLACK] = COLOR_BLACK,
398 [QEMU_COLOR_BLUE] = COLOR_BLUE,
399 [QEMU_COLOR_GREEN] = COLOR_GREEN,
400 [QEMU_COLOR_CYAN] = COLOR_CYAN,
401 [QEMU_COLOR_RED] = COLOR_RED,
402 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
403 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
404 [QEMU_COLOR_WHITE] = COLOR_WHITE,
balrog4d3b6f62008-02-10 16:33:14 +0000405 };
406
407 /* input as raw as possible, let everything be interpreted
408 * by the guest system */
409 initscr(); noecho(); intrflush(stdscr, FALSE);
410 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
411 start_color(); raw(); scrollok(stdscr, FALSE);
Samuel Thibault633786f2019-03-03 18:25:57 +0100412 set_escdelay(25);
balrog4d3b6f62008-02-10 16:33:14 +0000413
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900414 /* Make color pair to match color format (3bits bg:3bits fg) */
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900415 for (i = 0; i < 64; i++) {
balrog4d3b6f62008-02-10 16:33:14 +0000416 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900417 }
OGAWA Hirofumi40837332015-11-29 22:28:24 +0900418 /* Set default color for more than 64 for safety. */
OGAWA Hirofumi615220d2015-10-19 21:23:10 +0900419 for (i = 64; i < COLOR_PAIRS; i++) {
420 init_pair(i, COLOR_WHITE, COLOR_BLACK);
421 }
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900422
423 /*
424 * Setup mapping for vga to curses line graphics.
425 * FIXME: for better font, have to use ncursesw and setlocale()
426 */
427#if 0
428 /* FIXME: map from where? */
429 ACS_S1;
430 ACS_S3;
431 ACS_S7;
432 ACS_S9;
433#endif
434 /* ACS_* is not constant. So, we can't initialize statically. */
435 vga_to_curses['\0'] = ' ';
436 vga_to_curses[0x04] = ACS_DIAMOND;
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900437 vga_to_curses[0x18] = ACS_UARROW;
438 vga_to_curses[0x19] = ACS_DARROW;
Samuel Thibault697783a2016-10-15 21:53:04 +0200439 vga_to_curses[0x1a] = ACS_RARROW;
440 vga_to_curses[0x1b] = ACS_LARROW;
OGAWA Hirofumie2368dc2015-10-19 21:23:46 +0900441 vga_to_curses[0x9c] = ACS_STERLING;
442 vga_to_curses[0xb0] = ACS_BOARD;
443 vga_to_curses[0xb1] = ACS_CKBOARD;
444 vga_to_curses[0xb3] = ACS_VLINE;
445 vga_to_curses[0xb4] = ACS_RTEE;
446 vga_to_curses[0xbf] = ACS_URCORNER;
447 vga_to_curses[0xc0] = ACS_LLCORNER;
448 vga_to_curses[0xc1] = ACS_BTEE;
449 vga_to_curses[0xc2] = ACS_TTEE;
450 vga_to_curses[0xc3] = ACS_LTEE;
451 vga_to_curses[0xc4] = ACS_HLINE;
452 vga_to_curses[0xc5] = ACS_PLUS;
453 vga_to_curses[0xce] = ACS_LANTERN;
454 vga_to_curses[0xd8] = ACS_NEQUAL;
455 vga_to_curses[0xd9] = ACS_LRCORNER;
456 vga_to_curses[0xda] = ACS_ULCORNER;
457 vga_to_curses[0xdb] = ACS_BLOCK;
458 vga_to_curses[0xe3] = ACS_PI;
459 vga_to_curses[0xf1] = ACS_PLMINUS;
460 vga_to_curses[0xf2] = ACS_GEQUAL;
461 vga_to_curses[0xf3] = ACS_LEQUAL;
462 vga_to_curses[0xf8] = ACS_DEGREE;
463 vga_to_curses[0xfe] = ACS_BULLET;
balrog4d3b6f62008-02-10 16:33:14 +0000464}
465
466static void curses_keyboard_setup(void)
467{
balrog4d3b6f62008-02-10 16:33:14 +0000468#if defined(__APPLE__)
469 /* always use generic keymaps */
470 if (!keyboard_layout)
471 keyboard_layout = "en-us";
472#endif
473 if(keyboard_layout) {
Fei Liab4f9312018-10-17 10:26:50 +0200474 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
475 &error_fatal);
balrog4d3b6f62008-02-10 16:33:14 +0000476 }
balrog4d3b6f62008-02-10 16:33:14 +0000477}
478
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100479static const DisplayChangeListenerOps dcl_ops = {
480 .dpy_name = "curses",
481 .dpy_text_update = curses_update,
482 .dpy_text_resize = curses_resize,
483 .dpy_refresh = curses_refresh,
484 .dpy_text_cursor = curses_cursor_position,
485};
486
Gerd Hoffmannb0766612018-03-01 11:05:38 +0100487static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
balrog4d3b6f62008-02-10 16:33:14 +0000488{
489#ifndef _WIN32
490 if (!isatty(1)) {
491 fprintf(stderr, "We need a terminal output\n");
492 exit(1);
493 }
494#endif
495
496 curses_setup();
497 curses_keyboard_setup();
Anthony Liguori28695482010-03-21 14:13:02 -0500498 atexit(curses_atexit);
balrog4d3b6f62008-02-10 16:33:14 +0000499
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100500 curses_winch_init();
balrog4d3b6f62008-02-10 16:33:14 +0000501
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100502 dcl = g_new0(DisplayChangeListener, 1);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100503 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +0200504 register_displaychangelistener(dcl);
balrog4d3b6f62008-02-10 16:33:14 +0000505
506 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000507}
Gerd Hoffmannb0766612018-03-01 11:05:38 +0100508
509static QemuDisplay qemu_display_curses = {
510 .type = DISPLAY_TYPE_CURSES,
511 .init = curses_display_init,
512};
513
514static void register_curses(void)
515{
516 qemu_display_register(&qemu_display_curses);
517}
518
519type_init(register_curses);