]> git.proxmox.com Git - mirror_qemu.git/blame - ui/curses.c
cocoa: switch over to new display registry
[mirror_qemu.git] / ui / curses.c
CommitLineData
4d3b6f6e
AZ
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 */
e16f4c87 24#include "qemu/osdep.h"
4d3b6f6e
AZ
25
26#ifndef _WIN32
4d3b6f6e
AZ
27#include <sys/ioctl.h>
28#include <termios.h>
29#endif
30
511d2b14 31#include "qemu-common.h"
28ecbaee 32#include "ui/console.h"
cd100328 33#include "ui/input.h"
9c17d615 34#include "sysemu/sysemu.h"
511d2b14 35
e2f82e92
GH
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
4d3b6f6e
AZ
41#define FONT_HEIGHT 16
42#define FONT_WIDTH 8
43
7c20b4a3 44static DisplayChangeListener *dcl;
c227f099 45static console_ch_t screen[160 * 100];
4d3b6f6e
AZ
46static WINDOW *screenpad = NULL;
47static int width, height, gwidth, gheight, invalidate;
48static int px, py, sminx, sminy, smaxx, smaxy;
49
e2f82e92 50static chtype vga_to_curses[256];
e2368dc9 51
7c20b4a3 52static void curses_update(DisplayChangeListener *dcl,
7c20b4a3 53 int x, int y, int w, int h)
4d3b6f6e 54{
e2f82e92
GH
55 console_ch_t *line;
56 chtype curses_line[width];
4d3b6f6e 57
e2f82e92
GH
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 }
4d3b6f6e
AZ
70
71 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
72 refresh();
73}
74
75static void curses_calc_pad(void)
76{
81c0d5a6 77 if (qemu_console_is_fixedsize(NULL)) {
4d3b6f6e
AZ
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
7c20b4a3 114static void curses_resize(DisplayChangeListener *dcl,
7c20b4a3 115 int width, int height)
4d3b6f6e 116{
a93a4a22 117 if (width == gwidth && height == gheight) {
4d3b6f6e 118 return;
a93a4a22 119 }
4d3b6f6e 120
a93a4a22
GH
121 gwidth = width;
122 gheight = height;
4d3b6f6e
AZ
123
124 curses_calc_pad();
125}
126
032ac6f8
GH
127#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
128static volatile sig_atomic_t got_sigwinch;
129static void curses_winch_check(void)
4d3b6f6e
AZ
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
032ac6f8
GH
138 if (!got_sigwinch) {
139 return;
140 }
141 got_sigwinch = false;
142
143 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
4d3b6f6e 144 return;
032ac6f8 145 }
4d3b6f6e
AZ
146
147 resize_term(ws.ws_row, ws.ws_col);
4d3b6f6e 148 invalidate = 1;
032ac6f8 149}
4d3b6f6e 150
032ac6f8
GH
151static void curses_winch_handler(int signum)
152{
153 got_sigwinch = true;
4d3b6f6e 154}
032ac6f8
GH
155
156static 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
164static void curses_winch_check(void) {}
165static void curses_winch_init(void) {}
4d3b6f6e
AZ
166#endif
167
7c20b4a3 168static void curses_cursor_position(DisplayChangeListener *dcl,
7c20b4a3 169 int x, int y)
4d3b6f6e
AZ
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 */
81c0d5a6 180 if (!qemu_console_is_graphic(NULL)) {
4d3b6f6e 181 curs_set(2);
81c0d5a6 182 }
4d3b6f6e
AZ
183 return;
184 }
185 }
186
187 curs_set(0);
188}
189
190/* generic keyboard conversion */
191
192#include "curses_keys.h"
4d3b6f6e 193
c227f099 194static kbd_layout_t *kbd_layout = NULL;
4d3b6f6e 195
bc2ed970 196static void curses_refresh(DisplayChangeListener *dcl)
4d3b6f6e 197{
99a9ef44 198 int chr, keysym, keycode, keycode_alt;
4d3b6f6e 199
032ac6f8
GH
200 curses_winch_check();
201
4d3b6f6e
AZ
202 if (invalidate) {
203 clear();
204 refresh();
205 curses_calc_pad();
1dbfa005 206 graphic_hw_invalidate(NULL);
4d3b6f6e
AZ
207 invalidate = 0;
208 }
209
1dbfa005 210 graphic_hw_text_update(NULL, screen);
4d3b6f6e 211
4d3b6f6e
AZ
212 while (1) {
213 /* while there are any pending key strokes to process */
99a9ef44 214 chr = getch();
4d3b6f6e
AZ
215
216 if (chr == ERR)
217 break;
218
b1314cf9 219#ifdef KEY_RESIZE
4d3b6f6e
AZ
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();
bc2ed970 225 curses_update(dcl, 0, 0, width, height);
4d3b6f6e
AZ
226 continue;
227 }
b1314cf9 228#endif
4d3b6f6e
AZ
229
230 keycode = curses2keycode[chr];
44bb61c8 231 keycode_alt = 0;
4d3b6f6e
AZ
232
233 /* alt key */
234 if (keycode == 1) {
99a9ef44 235 int nextchr = getch();
4d3b6f6e
AZ
236
237 if (nextchr != ERR) {
44bb61c8
ST
238 chr = nextchr;
239 keycode_alt = ALT;
99a9ef44 240 keycode = curses2keycode[chr];
4d3b6f6e 241
44bb61c8
ST
242 if (keycode != -1) {
243 keycode |= ALT;
4d3b6f6e 244
44bb61c8
ST
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);
4d3b6f6e 251
44bb61c8
ST
252 invalidate = 1;
253 continue;
254 }
4d3b6f6e
AZ
255 }
256 }
257 }
258
44bb61c8
ST
259 if (kbd_layout) {
260 keysym = -1;
261 if (chr < CURSES_KEYS)
262 keysym = curses2keysym[chr];
263
264 if (keysym == -1) {
d03703c8
ST
265 if (chr < ' ') {
266 keysym = chr + '@';
267 if (keysym >= 'A' && keysym <= 'Z')
268 keysym += 'a' - 'A';
269 keysym |= KEYSYM_CNTRL;
270 } else
44bb61c8
ST
271 keysym = chr;
272 }
4d3b6f6e 273
abb4f2c9
GH
274 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
275 false, false, false);
44bb61c8
ST
276 if (keycode == 0)
277 continue;
278
279 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
280 keycode |= keycode_alt;
4d3b6f6e
AZ
281 }
282
44bb61c8
ST
283 if (keycode == -1)
284 continue;
285
81c0d5a6 286 if (qemu_console_is_graphic(NULL)) {
4d3b6f6e
AZ
287 /* since terminals don't know about key press and release
288 * events, we need to emit both for each key received */
cd100328
GH
289 if (keycode & SHIFT) {
290 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
5a165668 291 qemu_input_event_send_key_delay(0);
cd100328
GH
292 }
293 if (keycode & CNTRL) {
294 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
5a165668 295 qemu_input_event_send_key_delay(0);
cd100328
GH
296 }
297 if (keycode & ALT) {
298 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
5a165668 299 qemu_input_event_send_key_delay(0);
cd100328 300 }
44bb61c8 301 if (keycode & ALTGR) {
cd100328 302 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
5a165668 303 qemu_input_event_send_key_delay(0);
44bb61c8 304 }
cd100328 305
f5c0ab13 306 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
5a165668 307 qemu_input_event_send_key_delay(0);
f5c0ab13 308 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
5a165668 309 qemu_input_event_send_key_delay(0);
cd100328 310
44bb61c8 311 if (keycode & ALTGR) {
cd100328 312 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
5a165668 313 qemu_input_event_send_key_delay(0);
cd100328
GH
314 }
315 if (keycode & ALT) {
316 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
5a165668 317 qemu_input_event_send_key_delay(0);
cd100328
GH
318 }
319 if (keycode & CNTRL) {
320 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
5a165668 321 qemu_input_event_send_key_delay(0);
cd100328
GH
322 }
323 if (keycode & SHIFT) {
324 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
5a165668 325 qemu_input_event_send_key_delay(0);
44bb61c8 326 }
4d3b6f6e 327 } else {
bba4e1b5
PM
328 keysym = -1;
329 if (chr < CURSES_KEYS) {
330 keysym = curses2qemu[chr];
331 }
4d3b6f6e
AZ
332 if (keysym == -1)
333 keysym = chr;
334
335 kbd_put_keysym(keysym);
336 }
337 }
338}
339
aaf12c25 340static void curses_atexit(void)
4d3b6f6e
AZ
341{
342 endwin();
343}
344
4d3b6f6e
AZ
345static void curses_setup(void)
346{
347 int i, colour_default[8] = {
4083733d
OH
348 [QEMU_COLOR_BLACK] = COLOR_BLACK,
349 [QEMU_COLOR_BLUE] = COLOR_BLUE,
350 [QEMU_COLOR_GREEN] = COLOR_GREEN,
351 [QEMU_COLOR_CYAN] = COLOR_CYAN,
352 [QEMU_COLOR_RED] = COLOR_RED,
353 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
354 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
355 [QEMU_COLOR_WHITE] = COLOR_WHITE,
4d3b6f6e
AZ
356 };
357
358 /* input as raw as possible, let everything be interpreted
359 * by the guest system */
360 initscr(); noecho(); intrflush(stdscr, FALSE);
361 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
362 start_color(); raw(); scrollok(stdscr, FALSE);
363
4083733d 364 /* Make color pair to match color format (3bits bg:3bits fg) */
615220dd 365 for (i = 0; i < 64; i++) {
4d3b6f6e 366 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
615220dd 367 }
4083733d 368 /* Set default color for more than 64 for safety. */
615220dd
OH
369 for (i = 64; i < COLOR_PAIRS; i++) {
370 init_pair(i, COLOR_WHITE, COLOR_BLACK);
371 }
e2368dc9
OH
372
373 /*
374 * Setup mapping for vga to curses line graphics.
375 * FIXME: for better font, have to use ncursesw and setlocale()
376 */
377#if 0
378 /* FIXME: map from where? */
379 ACS_S1;
380 ACS_S3;
381 ACS_S7;
382 ACS_S9;
383#endif
384 /* ACS_* is not constant. So, we can't initialize statically. */
385 vga_to_curses['\0'] = ' ';
386 vga_to_curses[0x04] = ACS_DIAMOND;
e2368dc9
OH
387 vga_to_curses[0x18] = ACS_UARROW;
388 vga_to_curses[0x19] = ACS_DARROW;
697783a7
ST
389 vga_to_curses[0x1a] = ACS_RARROW;
390 vga_to_curses[0x1b] = ACS_LARROW;
e2368dc9
OH
391 vga_to_curses[0x9c] = ACS_STERLING;
392 vga_to_curses[0xb0] = ACS_BOARD;
393 vga_to_curses[0xb1] = ACS_CKBOARD;
394 vga_to_curses[0xb3] = ACS_VLINE;
395 vga_to_curses[0xb4] = ACS_RTEE;
396 vga_to_curses[0xbf] = ACS_URCORNER;
397 vga_to_curses[0xc0] = ACS_LLCORNER;
398 vga_to_curses[0xc1] = ACS_BTEE;
399 vga_to_curses[0xc2] = ACS_TTEE;
400 vga_to_curses[0xc3] = ACS_LTEE;
401 vga_to_curses[0xc4] = ACS_HLINE;
402 vga_to_curses[0xc5] = ACS_PLUS;
403 vga_to_curses[0xce] = ACS_LANTERN;
404 vga_to_curses[0xd8] = ACS_NEQUAL;
405 vga_to_curses[0xd9] = ACS_LRCORNER;
406 vga_to_curses[0xda] = ACS_ULCORNER;
407 vga_to_curses[0xdb] = ACS_BLOCK;
408 vga_to_curses[0xe3] = ACS_PI;
409 vga_to_curses[0xf1] = ACS_PLMINUS;
410 vga_to_curses[0xf2] = ACS_GEQUAL;
411 vga_to_curses[0xf3] = ACS_LEQUAL;
412 vga_to_curses[0xf8] = ACS_DEGREE;
413 vga_to_curses[0xfe] = ACS_BULLET;
4d3b6f6e
AZ
414}
415
416static void curses_keyboard_setup(void)
417{
4d3b6f6e
AZ
418#if defined(__APPLE__)
419 /* always use generic keymaps */
420 if (!keyboard_layout)
421 keyboard_layout = "en-us";
422#endif
423 if(keyboard_layout) {
0483755a 424 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
4d3b6f6e
AZ
425 if (!kbd_layout)
426 exit(1);
427 }
4d3b6f6e
AZ
428}
429
7c20b4a3
GH
430static const DisplayChangeListenerOps dcl_ops = {
431 .dpy_name = "curses",
432 .dpy_text_update = curses_update,
433 .dpy_text_resize = curses_resize,
434 .dpy_refresh = curses_refresh,
435 .dpy_text_cursor = curses_cursor_position,
436};
437
14f130fa 438void curses_display_init(DisplayState *ds, DisplayOptions *opts)
4d3b6f6e
AZ
439{
440#ifndef _WIN32
441 if (!isatty(1)) {
442 fprintf(stderr, "We need a terminal output\n");
443 exit(1);
444 }
445#endif
446
447 curses_setup();
448 curses_keyboard_setup();
28695489 449 atexit(curses_atexit);
4d3b6f6e 450
032ac6f8 451 curses_winch_init();
4d3b6f6e 452
fedf0d35 453 dcl = g_new0(DisplayChangeListener, 1);
7c20b4a3 454 dcl->ops = &dcl_ops;
5209089f 455 register_displaychangelistener(dcl);
4d3b6f6e
AZ
456
457 invalidate = 1;
4d3b6f6e 458}