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