]>
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 | |
26 | #ifndef _WIN32 | |
4d3b6f6e AZ |
27 | #include <sys/ioctl.h> |
28 | #include <termios.h> | |
29 | #endif | |
2f8b7cd5 ST |
30 | #include <locale.h> |
31 | #include <wchar.h> | |
32 | #include <langinfo.h> | |
33 | #include <iconv.h> | |
4d3b6f6e | 34 | |
ab4f931e | 35 | #include "qapi/error.h" |
511d2b14 | 36 | #include "qemu-common.h" |
28ecbaee | 37 | #include "ui/console.h" |
cd100328 | 38 | #include "ui/input.h" |
9c17d615 | 39 | #include "sysemu/sysemu.h" |
511d2b14 | 40 | |
e2f82e92 GH |
41 | /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */ |
42 | #undef KEY_EVENT | |
43 | #include <curses.h> | |
44 | #undef KEY_EVENT | |
45 | ||
4d3b6f6e AZ |
46 | #define FONT_HEIGHT 16 |
47 | #define FONT_WIDTH 8 | |
48 | ||
459a707e ST |
49 | enum maybe_keycode { |
50 | CURSES_KEYCODE, | |
51 | CURSES_CHAR, | |
52 | CURSES_CHAR_OR_KEYCODE, | |
53 | }; | |
54 | ||
7c20b4a3 | 55 | static DisplayChangeListener *dcl; |
c227f099 | 56 | static console_ch_t screen[160 * 100]; |
4d3b6f6e AZ |
57 | static WINDOW *screenpad = NULL; |
58 | static int width, height, gwidth, gheight, invalidate; | |
59 | static int px, py, sminx, sminy, smaxx, smaxy; | |
60 | ||
2f8b7cd5 ST |
61 | static const char *font_charset = "CP437"; |
62 | static cchar_t vga_to_curses[256]; | |
e2368dc9 | 63 | |
7c20b4a3 | 64 | static void curses_update(DisplayChangeListener *dcl, |
7c20b4a3 | 65 | int x, int y, int w, int h) |
4d3b6f6e | 66 | { |
e2f82e92 | 67 | console_ch_t *line; |
2f8b7cd5 | 68 | cchar_t curses_line[width]; |
4d3b6f6e | 69 | |
e2f82e92 GH |
70 | line = screen + y * width; |
71 | for (h += y; y < h; y ++, line += width) { | |
72 | for (x = 0; x < width; x++) { | |
73 | chtype ch = line[x] & 0xff; | |
74 | chtype at = line[x] & ~0xff; | |
2f8b7cd5 ST |
75 | if (vga_to_curses[ch].chars[0]) { |
76 | curses_line[x] = vga_to_curses[ch]; | |
77 | } else { | |
a5489ae5 ST |
78 | curses_line[x] = (cchar_t) { |
79 | .chars[0] = ch, | |
80 | }; | |
e2f82e92 | 81 | } |
2f8b7cd5 | 82 | curses_line[x].attr |= at; |
e2f82e92 | 83 | } |
2f8b7cd5 | 84 | mvwadd_wchnstr(screenpad, y, 0, curses_line, width); |
e2f82e92 | 85 | } |
4d3b6f6e AZ |
86 | |
87 | pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1); | |
88 | refresh(); | |
89 | } | |
90 | ||
91 | static void curses_calc_pad(void) | |
92 | { | |
81c0d5a6 | 93 | if (qemu_console_is_fixedsize(NULL)) { |
4d3b6f6e AZ |
94 | width = gwidth; |
95 | height = gheight; | |
96 | } else { | |
97 | width = COLS; | |
98 | height = LINES; | |
99 | } | |
100 | ||
101 | if (screenpad) | |
102 | delwin(screenpad); | |
103 | ||
104 | clear(); | |
105 | refresh(); | |
106 | ||
107 | screenpad = newpad(height, width); | |
108 | ||
109 | if (width > COLS) { | |
110 | px = (width - COLS) / 2; | |
111 | sminx = 0; | |
112 | smaxx = COLS; | |
113 | } else { | |
114 | px = 0; | |
115 | sminx = (COLS - width) / 2; | |
116 | smaxx = sminx + width; | |
117 | } | |
118 | ||
119 | if (height > LINES) { | |
120 | py = (height - LINES) / 2; | |
121 | sminy = 0; | |
122 | smaxy = LINES; | |
123 | } else { | |
124 | py = 0; | |
125 | sminy = (LINES - height) / 2; | |
126 | smaxy = sminy + height; | |
127 | } | |
128 | } | |
129 | ||
7c20b4a3 | 130 | static void curses_resize(DisplayChangeListener *dcl, |
7c20b4a3 | 131 | int width, int height) |
4d3b6f6e | 132 | { |
a93a4a22 | 133 | if (width == gwidth && height == gheight) { |
4d3b6f6e | 134 | return; |
a93a4a22 | 135 | } |
4d3b6f6e | 136 | |
a93a4a22 GH |
137 | gwidth = width; |
138 | gheight = height; | |
4d3b6f6e AZ |
139 | |
140 | curses_calc_pad(); | |
141 | } | |
142 | ||
032ac6f8 GH |
143 | #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE) |
144 | static volatile sig_atomic_t got_sigwinch; | |
145 | static void curses_winch_check(void) | |
4d3b6f6e AZ |
146 | { |
147 | struct winsize { | |
148 | unsigned short ws_row; | |
149 | unsigned short ws_col; | |
150 | unsigned short ws_xpixel; /* unused */ | |
151 | unsigned short ws_ypixel; /* unused */ | |
152 | } ws; | |
153 | ||
032ac6f8 GH |
154 | if (!got_sigwinch) { |
155 | return; | |
156 | } | |
157 | got_sigwinch = false; | |
158 | ||
159 | if (ioctl(1, TIOCGWINSZ, &ws) == -1) { | |
4d3b6f6e | 160 | return; |
032ac6f8 | 161 | } |
4d3b6f6e AZ |
162 | |
163 | resize_term(ws.ws_row, ws.ws_col); | |
4d3b6f6e | 164 | invalidate = 1; |
032ac6f8 | 165 | } |
4d3b6f6e | 166 | |
032ac6f8 GH |
167 | static void curses_winch_handler(int signum) |
168 | { | |
169 | got_sigwinch = true; | |
4d3b6f6e | 170 | } |
032ac6f8 GH |
171 | |
172 | static void curses_winch_init(void) | |
173 | { | |
174 | struct sigaction old, winch = { | |
175 | .sa_handler = curses_winch_handler, | |
176 | }; | |
177 | sigaction(SIGWINCH, &winch, &old); | |
178 | } | |
179 | #else | |
180 | static void curses_winch_check(void) {} | |
181 | static void curses_winch_init(void) {} | |
4d3b6f6e AZ |
182 | #endif |
183 | ||
7c20b4a3 | 184 | static void curses_cursor_position(DisplayChangeListener *dcl, |
7c20b4a3 | 185 | int x, int y) |
4d3b6f6e AZ |
186 | { |
187 | if (x >= 0) { | |
188 | x = sminx + x - px; | |
189 | y = sminy + y - py; | |
190 | ||
191 | if (x >= 0 && y >= 0 && x < COLS && y < LINES) { | |
192 | move(y, x); | |
193 | curs_set(1); | |
194 | /* it seems that curs_set(1) must always be called before | |
195 | * curs_set(2) for the latter to have effect */ | |
81c0d5a6 | 196 | if (!qemu_console_is_graphic(NULL)) { |
4d3b6f6e | 197 | curs_set(2); |
81c0d5a6 | 198 | } |
4d3b6f6e AZ |
199 | return; |
200 | } | |
201 | } | |
202 | ||
203 | curs_set(0); | |
204 | } | |
205 | ||
206 | /* generic keyboard conversion */ | |
207 | ||
208 | #include "curses_keys.h" | |
4d3b6f6e | 209 | |
c227f099 | 210 | static kbd_layout_t *kbd_layout = NULL; |
4d3b6f6e | 211 | |
459a707e ST |
212 | static wint_t console_getch(enum maybe_keycode *maybe_keycode) |
213 | { | |
214 | wint_t ret; | |
215 | switch (get_wch(&ret)) { | |
216 | case KEY_CODE_YES: | |
217 | *maybe_keycode = CURSES_KEYCODE; | |
218 | break; | |
219 | case OK: | |
220 | *maybe_keycode = CURSES_CHAR; | |
221 | break; | |
222 | case ERR: | |
223 | ret = -1; | |
224 | break; | |
225 | } | |
226 | return ret; | |
227 | } | |
228 | ||
229 | static int curses2foo(const int _curses2foo[], const int _curseskey2foo[], | |
230 | int chr, enum maybe_keycode maybe_keycode) | |
231 | { | |
232 | int ret = -1; | |
233 | if (maybe_keycode == CURSES_CHAR) { | |
234 | if (chr < CURSES_CHARS) { | |
235 | ret = _curses2foo[chr]; | |
236 | } | |
237 | } else { | |
238 | if (chr < CURSES_KEYS) { | |
239 | ret = _curseskey2foo[chr]; | |
240 | } | |
241 | if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE && | |
242 | chr < CURSES_CHARS) { | |
243 | ret = _curses2foo[chr]; | |
244 | } | |
245 | } | |
246 | return ret; | |
247 | } | |
248 | ||
249 | #define curses2keycode(chr, maybe_keycode) \ | |
250 | curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode) | |
251 | #define curses2keysym(chr, maybe_keycode) \ | |
252 | curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode) | |
253 | #define curses2qemu(chr, maybe_keycode) \ | |
254 | curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode) | |
255 | ||
bc2ed970 | 256 | static void curses_refresh(DisplayChangeListener *dcl) |
4d3b6f6e | 257 | { |
99a9ef44 | 258 | int chr, keysym, keycode, keycode_alt; |
459a707e | 259 | enum maybe_keycode maybe_keycode; |
4d3b6f6e | 260 | |
032ac6f8 GH |
261 | curses_winch_check(); |
262 | ||
4d3b6f6e AZ |
263 | if (invalidate) { |
264 | clear(); | |
265 | refresh(); | |
266 | curses_calc_pad(); | |
1dbfa005 | 267 | graphic_hw_invalidate(NULL); |
4d3b6f6e AZ |
268 | invalidate = 0; |
269 | } | |
270 | ||
1dbfa005 | 271 | graphic_hw_text_update(NULL, screen); |
4d3b6f6e | 272 | |
4d3b6f6e AZ |
273 | while (1) { |
274 | /* while there are any pending key strokes to process */ | |
459a707e | 275 | chr = console_getch(&maybe_keycode); |
4d3b6f6e | 276 | |
459a707e | 277 | if (chr == -1) |
4d3b6f6e AZ |
278 | break; |
279 | ||
b1314cf9 | 280 | #ifdef KEY_RESIZE |
4d3b6f6e | 281 | /* this shouldn't occur when we use a custom SIGWINCH handler */ |
459a707e | 282 | if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) { |
4d3b6f6e AZ |
283 | clear(); |
284 | refresh(); | |
285 | curses_calc_pad(); | |
bc2ed970 | 286 | curses_update(dcl, 0, 0, width, height); |
4d3b6f6e AZ |
287 | continue; |
288 | } | |
b1314cf9 | 289 | #endif |
4d3b6f6e | 290 | |
459a707e | 291 | keycode = curses2keycode(chr, maybe_keycode); |
44bb61c8 | 292 | keycode_alt = 0; |
4d3b6f6e | 293 | |
633786fe | 294 | /* alt or esc key */ |
4d3b6f6e | 295 | if (keycode == 1) { |
459a707e ST |
296 | enum maybe_keycode next_maybe_keycode; |
297 | int nextchr = console_getch(&next_maybe_keycode); | |
4d3b6f6e | 298 | |
459a707e | 299 | if (nextchr != -1) { |
44bb61c8 | 300 | chr = nextchr; |
459a707e | 301 | maybe_keycode = next_maybe_keycode; |
44bb61c8 | 302 | keycode_alt = ALT; |
459a707e | 303 | keycode = curses2keycode(chr, maybe_keycode); |
4d3b6f6e | 304 | |
44bb61c8 ST |
305 | if (keycode != -1) { |
306 | keycode |= ALT; | |
4d3b6f6e | 307 | |
44bb61c8 ST |
308 | /* process keys reserved for qemu */ |
309 | if (keycode >= QEMU_KEY_CONSOLE0 && | |
310 | keycode < QEMU_KEY_CONSOLE0 + 9) { | |
311 | erase(); | |
312 | wnoutrefresh(stdscr); | |
313 | console_select(keycode - QEMU_KEY_CONSOLE0); | |
4d3b6f6e | 314 | |
44bb61c8 ST |
315 | invalidate = 1; |
316 | continue; | |
317 | } | |
4d3b6f6e AZ |
318 | } |
319 | } | |
320 | } | |
321 | ||
44bb61c8 | 322 | if (kbd_layout) { |
459a707e | 323 | keysym = curses2keysym(chr, maybe_keycode); |
44bb61c8 ST |
324 | |
325 | if (keysym == -1) { | |
d03703c8 ST |
326 | if (chr < ' ') { |
327 | keysym = chr + '@'; | |
328 | if (keysym >= 'A' && keysym <= 'Z') | |
329 | keysym += 'a' - 'A'; | |
330 | keysym |= KEYSYM_CNTRL; | |
331 | } else | |
44bb61c8 ST |
332 | keysym = chr; |
333 | } | |
4d3b6f6e | 334 | |
abb4f2c9 | 335 | keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK, |
19c1b9fd | 336 | NULL, false); |
44bb61c8 ST |
337 | if (keycode == 0) |
338 | continue; | |
339 | ||
340 | keycode |= (keysym & ~KEYSYM_MASK) >> 16; | |
341 | keycode |= keycode_alt; | |
4d3b6f6e AZ |
342 | } |
343 | ||
44bb61c8 ST |
344 | if (keycode == -1) |
345 | continue; | |
346 | ||
81c0d5a6 | 347 | if (qemu_console_is_graphic(NULL)) { |
4d3b6f6e AZ |
348 | /* since terminals don't know about key press and release |
349 | * events, we need to emit both for each key received */ | |
cd100328 GH |
350 | if (keycode & SHIFT) { |
351 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, true); | |
5a165668 | 352 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
353 | } |
354 | if (keycode & CNTRL) { | |
355 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, true); | |
5a165668 | 356 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
357 | } |
358 | if (keycode & ALT) { | |
359 | qemu_input_event_send_key_number(NULL, ALT_CODE, true); | |
5a165668 | 360 | qemu_input_event_send_key_delay(0); |
cd100328 | 361 | } |
44bb61c8 | 362 | if (keycode & ALTGR) { |
cd100328 | 363 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true); |
5a165668 | 364 | qemu_input_event_send_key_delay(0); |
44bb61c8 | 365 | } |
cd100328 | 366 | |
f5c0ab13 | 367 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true); |
5a165668 | 368 | qemu_input_event_send_key_delay(0); |
f5c0ab13 | 369 | qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false); |
5a165668 | 370 | qemu_input_event_send_key_delay(0); |
cd100328 | 371 | |
44bb61c8 | 372 | if (keycode & ALTGR) { |
cd100328 | 373 | qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false); |
5a165668 | 374 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
375 | } |
376 | if (keycode & ALT) { | |
377 | qemu_input_event_send_key_number(NULL, ALT_CODE, false); | |
5a165668 | 378 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
379 | } |
380 | if (keycode & CNTRL) { | |
381 | qemu_input_event_send_key_number(NULL, CNTRL_CODE, false); | |
5a165668 | 382 | qemu_input_event_send_key_delay(0); |
cd100328 GH |
383 | } |
384 | if (keycode & SHIFT) { | |
385 | qemu_input_event_send_key_number(NULL, SHIFT_CODE, false); | |
5a165668 | 386 | qemu_input_event_send_key_delay(0); |
44bb61c8 | 387 | } |
4d3b6f6e | 388 | } else { |
459a707e | 389 | keysym = curses2qemu(chr, maybe_keycode); |
4d3b6f6e AZ |
390 | if (keysym == -1) |
391 | keysym = chr; | |
392 | ||
393 | kbd_put_keysym(keysym); | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
aaf12c25 | 398 | static void curses_atexit(void) |
4d3b6f6e AZ |
399 | { |
400 | endwin(); | |
401 | } | |
402 | ||
b7b664a4 ST |
403 | /* |
404 | * In the following: | |
405 | * - fch is the font glyph number | |
406 | * - uch is the unicode value | |
407 | * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris) | |
408 | * - mbch is the native local-dependent multibyte representation | |
409 | */ | |
410 | ||
2f8b7cd5 | 411 | /* Setup wchar glyph for one UCS-2 char */ |
b7b664a4 | 412 | static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv) |
2f8b7cd5 | 413 | { |
b7b664a4 | 414 | char mbch[MB_LEN_MAX]; |
2f8b7cd5 | 415 | wchar_t wch; |
b7b664a4 ST |
416 | char *puch, *pmbch; |
417 | size_t such, smbch; | |
418 | mbstate_t ps; | |
419 | ||
420 | puch = (char *) &uch; | |
421 | pmbch = (char *) mbch; | |
422 | such = sizeof(uch); | |
423 | smbch = sizeof(mbch); | |
424 | ||
425 | if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) { | |
426 | fprintf(stderr, "Could not convert 0x%04x " | |
427 | "from UCS-2 to a multibyte character: %s\n", | |
428 | uch, strerror(errno)); | |
429 | return; | |
430 | } | |
2f8b7cd5 | 431 | |
b7b664a4 ST |
432 | memset(&ps, 0, sizeof(ps)); |
433 | if (mbrtowc(&wch, mbch, sizeof(mbch) - smbch, &ps) == -1) { | |
434 | fprintf(stderr, "Could not convert 0x%04x " | |
435 | "from a multibyte character to wchar_t: %s\n", | |
436 | uch, strerror(errno)); | |
437 | return; | |
2f8b7cd5 | 438 | } |
b7b664a4 | 439 | vga_to_curses[fch].chars[0] = wch; |
2f8b7cd5 ST |
440 | } |
441 | ||
442 | /* Setup wchar glyph for one font character */ | |
b7b664a4 | 443 | static void convert_font(unsigned char fch, iconv_t conv) |
2f8b7cd5 | 444 | { |
b7b664a4 | 445 | char mbch[MB_LEN_MAX]; |
2f8b7cd5 | 446 | wchar_t wch; |
b7b664a4 ST |
447 | char *pfch, *pmbch; |
448 | size_t sfch, smbch; | |
449 | mbstate_t ps; | |
450 | ||
451 | pfch = (char *) &fch; | |
452 | pmbch = (char *) &mbch; | |
453 | sfch = sizeof(fch); | |
454 | smbch = sizeof(mbch); | |
455 | ||
456 | if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) { | |
457 | fprintf(stderr, "Could not convert font glyph 0x%02x " | |
458 | "from %s to a multibyte character: %s\n", | |
459 | fch, font_charset, strerror(errno)); | |
460 | return; | |
461 | } | |
2f8b7cd5 | 462 | |
b7b664a4 ST |
463 | memset(&ps, 0, sizeof(ps)); |
464 | if (mbrtowc(&wch, mbch, sizeof(mbch) - smbch, &ps) == -1) { | |
465 | fprintf(stderr, "Could not convert font glyph 0x%02x " | |
466 | "from a multibyte character to wchar_t: %s\n", | |
467 | fch, strerror(errno)); | |
468 | return; | |
2f8b7cd5 | 469 | } |
b7b664a4 | 470 | vga_to_curses[fch].chars[0] = wch; |
2f8b7cd5 ST |
471 | } |
472 | ||
473 | /* Convert one wchar to UCS-2 */ | |
474 | static uint16_t get_ucs(wchar_t wch, iconv_t conv) | |
475 | { | |
b7b664a4 ST |
476 | char mbch[MB_LEN_MAX]; |
477 | uint16_t uch; | |
478 | char *pmbch, *puch; | |
479 | size_t smbch, such; | |
480 | mbstate_t ps; | |
481 | int ret; | |
482 | ||
483 | memset(&ps, 0, sizeof(ps)); | |
484 | ret = wcrtomb(mbch, wch, &ps); | |
485 | if (ret == -1) { | |
486 | fprintf(stderr, "Could not convert 0x%04x " | |
487 | "from wchar_t to a multibyte character: %s\n", | |
488 | wch, strerror(errno)); | |
489 | return 0xFFFD; | |
490 | } | |
491 | ||
492 | pmbch = (char *) mbch; | |
493 | puch = (char *) &uch; | |
494 | smbch = ret; | |
495 | such = sizeof(uch); | |
496 | ||
497 | if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) { | |
498 | fprintf(stderr, "Could not convert 0x%04x " | |
499 | "from a multibyte character to UCS-2 : %s\n", | |
500 | wch, strerror(errno)); | |
2f8b7cd5 ST |
501 | return 0xFFFD; |
502 | } | |
503 | ||
b7b664a4 | 504 | return uch; |
2f8b7cd5 ST |
505 | } |
506 | ||
507 | /* | |
508 | * Setup mapping for vga to curses line graphics. | |
509 | */ | |
510 | static void font_setup(void) | |
511 | { | |
b7b664a4 ST |
512 | iconv_t ucs2_to_nativecharset; |
513 | iconv_t nativecharset_to_ucs2; | |
514 | iconv_t font_conv; | |
515 | int i; | |
516 | ||
2f8b7cd5 ST |
517 | /* |
518 | * Control characters are normally non-printable, but VGA does have | |
519 | * well-known glyphs for them. | |
520 | */ | |
521 | static uint16_t control_characters[0x20] = { | |
522 | 0x0020, | |
523 | 0x263a, | |
524 | 0x263b, | |
525 | 0x2665, | |
526 | 0x2666, | |
527 | 0x2663, | |
528 | 0x2660, | |
529 | 0x2022, | |
530 | 0x25d8, | |
531 | 0x25cb, | |
532 | 0x25d9, | |
533 | 0x2642, | |
534 | 0x2640, | |
535 | 0x266a, | |
536 | 0x266b, | |
537 | 0x263c, | |
538 | 0x25ba, | |
539 | 0x25c4, | |
540 | 0x2195, | |
541 | 0x203c, | |
542 | 0x00b6, | |
543 | 0x00a7, | |
544 | 0x25ac, | |
545 | 0x21a8, | |
546 | 0x2191, | |
547 | 0x2193, | |
548 | 0x2192, | |
549 | 0x2190, | |
550 | 0x221f, | |
551 | 0x2194, | |
552 | 0x25b2, | |
553 | 0x25bc | |
554 | }; | |
555 | ||
b7b664a4 ST |
556 | ucs2_to_nativecharset = iconv_open(nl_langinfo(CODESET), "UCS-2"); |
557 | if (ucs2_to_nativecharset == (iconv_t) -1) { | |
2f8b7cd5 ST |
558 | fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n", |
559 | strerror(errno)); | |
560 | exit(1); | |
561 | } | |
562 | ||
b7b664a4 ST |
563 | nativecharset_to_ucs2 = iconv_open("UCS-2", nl_langinfo(CODESET)); |
564 | if (nativecharset_to_ucs2 == (iconv_t) -1) { | |
565 | iconv_close(ucs2_to_nativecharset); | |
2f8b7cd5 ST |
566 | fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n", |
567 | strerror(errno)); | |
568 | exit(1); | |
569 | } | |
570 | ||
b7b664a4 | 571 | font_conv = iconv_open(nl_langinfo(CODESET), font_charset); |
2f8b7cd5 | 572 | if (font_conv == (iconv_t) -1) { |
b7b664a4 ST |
573 | iconv_close(ucs2_to_nativecharset); |
574 | iconv_close(nativecharset_to_ucs2); | |
2f8b7cd5 ST |
575 | fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n", |
576 | font_charset, strerror(errno)); | |
577 | exit(1); | |
578 | } | |
579 | ||
580 | /* Control characters */ | |
581 | for (i = 0; i <= 0x1F; i++) { | |
b7b664a4 | 582 | convert_ucs(i, control_characters[i], ucs2_to_nativecharset); |
2f8b7cd5 ST |
583 | } |
584 | ||
585 | for (i = 0x20; i <= 0xFF; i++) { | |
586 | convert_font(i, font_conv); | |
587 | } | |
588 | ||
589 | /* DEL */ | |
b7b664a4 | 590 | convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset); |
2f8b7cd5 ST |
591 | |
592 | if (strcmp(nl_langinfo(CODESET), "UTF-8")) { | |
593 | /* Non-Unicode capable, use termcap equivalents for those available */ | |
594 | for (i = 0; i <= 0xFF; i++) { | |
b7b664a4 | 595 | switch (get_ucs(vga_to_curses[i].chars[0], nativecharset_to_ucs2)) { |
2f8b7cd5 ST |
596 | case 0x00a3: |
597 | vga_to_curses[i] = *WACS_STERLING; | |
598 | break; | |
599 | case 0x2591: | |
600 | vga_to_curses[i] = *WACS_BOARD; | |
601 | break; | |
602 | case 0x2592: | |
603 | vga_to_curses[i] = *WACS_CKBOARD; | |
604 | break; | |
605 | case 0x2502: | |
606 | vga_to_curses[i] = *WACS_VLINE; | |
607 | break; | |
608 | case 0x2524: | |
609 | vga_to_curses[i] = *WACS_RTEE; | |
610 | break; | |
611 | case 0x2510: | |
612 | vga_to_curses[i] = *WACS_URCORNER; | |
613 | break; | |
614 | case 0x2514: | |
615 | vga_to_curses[i] = *WACS_LLCORNER; | |
616 | break; | |
617 | case 0x2534: | |
618 | vga_to_curses[i] = *WACS_BTEE; | |
619 | break; | |
620 | case 0x252c: | |
621 | vga_to_curses[i] = *WACS_TTEE; | |
622 | break; | |
623 | case 0x251c: | |
624 | vga_to_curses[i] = *WACS_LTEE; | |
625 | break; | |
626 | case 0x2500: | |
627 | vga_to_curses[i] = *WACS_HLINE; | |
628 | break; | |
629 | case 0x253c: | |
630 | vga_to_curses[i] = *WACS_PLUS; | |
631 | break; | |
632 | case 0x256c: | |
633 | vga_to_curses[i] = *WACS_LANTERN; | |
634 | break; | |
635 | case 0x256a: | |
636 | vga_to_curses[i] = *WACS_NEQUAL; | |
637 | break; | |
638 | case 0x2518: | |
639 | vga_to_curses[i] = *WACS_LRCORNER; | |
640 | break; | |
641 | case 0x250c: | |
642 | vga_to_curses[i] = *WACS_ULCORNER; | |
643 | break; | |
644 | case 0x2588: | |
645 | vga_to_curses[i] = *WACS_BLOCK; | |
646 | break; | |
647 | case 0x03c0: | |
648 | vga_to_curses[i] = *WACS_PI; | |
649 | break; | |
650 | case 0x00b1: | |
651 | vga_to_curses[i] = *WACS_PLMINUS; | |
652 | break; | |
653 | case 0x2265: | |
654 | vga_to_curses[i] = *WACS_GEQUAL; | |
655 | break; | |
656 | case 0x2264: | |
657 | vga_to_curses[i] = *WACS_LEQUAL; | |
658 | break; | |
659 | case 0x00b0: | |
660 | vga_to_curses[i] = *WACS_DEGREE; | |
661 | break; | |
662 | case 0x25a0: | |
663 | vga_to_curses[i] = *WACS_BULLET; | |
664 | break; | |
665 | case 0x2666: | |
666 | vga_to_curses[i] = *WACS_DIAMOND; | |
667 | break; | |
668 | case 0x2192: | |
669 | vga_to_curses[i] = *WACS_RARROW; | |
670 | break; | |
671 | case 0x2190: | |
672 | vga_to_curses[i] = *WACS_LARROW; | |
673 | break; | |
674 | case 0x2191: | |
675 | vga_to_curses[i] = *WACS_UARROW; | |
676 | break; | |
677 | case 0x2193: | |
678 | vga_to_curses[i] = *WACS_DARROW; | |
679 | break; | |
680 | case 0x23ba: | |
681 | vga_to_curses[i] = *WACS_S1; | |
682 | break; | |
683 | case 0x23bb: | |
684 | vga_to_curses[i] = *WACS_S3; | |
685 | break; | |
686 | case 0x23bc: | |
687 | vga_to_curses[i] = *WACS_S7; | |
688 | break; | |
689 | case 0x23bd: | |
690 | vga_to_curses[i] = *WACS_S9; | |
691 | break; | |
692 | } | |
693 | } | |
694 | } | |
b7b664a4 ST |
695 | iconv_close(ucs2_to_nativecharset); |
696 | iconv_close(nativecharset_to_ucs2); | |
a9fda247 | 697 | iconv_close(font_conv); |
2f8b7cd5 ST |
698 | } |
699 | ||
4d3b6f6e AZ |
700 | static void curses_setup(void) |
701 | { | |
702 | int i, colour_default[8] = { | |
4083733d OH |
703 | [QEMU_COLOR_BLACK] = COLOR_BLACK, |
704 | [QEMU_COLOR_BLUE] = COLOR_BLUE, | |
705 | [QEMU_COLOR_GREEN] = COLOR_GREEN, | |
706 | [QEMU_COLOR_CYAN] = COLOR_CYAN, | |
707 | [QEMU_COLOR_RED] = COLOR_RED, | |
708 | [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA, | |
709 | [QEMU_COLOR_YELLOW] = COLOR_YELLOW, | |
710 | [QEMU_COLOR_WHITE] = COLOR_WHITE, | |
4d3b6f6e AZ |
711 | }; |
712 | ||
713 | /* input as raw as possible, let everything be interpreted | |
714 | * by the guest system */ | |
715 | initscr(); noecho(); intrflush(stdscr, FALSE); | |
716 | nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); | |
717 | start_color(); raw(); scrollok(stdscr, FALSE); | |
633786fe | 718 | set_escdelay(25); |
4d3b6f6e | 719 | |
4083733d | 720 | /* Make color pair to match color format (3bits bg:3bits fg) */ |
615220dd | 721 | for (i = 0; i < 64; i++) { |
4d3b6f6e | 722 | init_pair(i, colour_default[i & 7], colour_default[i >> 3]); |
615220dd | 723 | } |
4083733d | 724 | /* Set default color for more than 64 for safety. */ |
615220dd OH |
725 | for (i = 64; i < COLOR_PAIRS; i++) { |
726 | init_pair(i, COLOR_WHITE, COLOR_BLACK); | |
727 | } | |
e2368dc9 | 728 | |
2f8b7cd5 | 729 | font_setup(); |
4d3b6f6e AZ |
730 | } |
731 | ||
732 | static void curses_keyboard_setup(void) | |
733 | { | |
4d3b6f6e AZ |
734 | #if defined(__APPLE__) |
735 | /* always use generic keymaps */ | |
736 | if (!keyboard_layout) | |
737 | keyboard_layout = "en-us"; | |
738 | #endif | |
739 | if(keyboard_layout) { | |
ab4f931e FL |
740 | kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout, |
741 | &error_fatal); | |
4d3b6f6e | 742 | } |
4d3b6f6e AZ |
743 | } |
744 | ||
7c20b4a3 GH |
745 | static const DisplayChangeListenerOps dcl_ops = { |
746 | .dpy_name = "curses", | |
747 | .dpy_text_update = curses_update, | |
748 | .dpy_text_resize = curses_resize, | |
749 | .dpy_refresh = curses_refresh, | |
750 | .dpy_text_cursor = curses_cursor_position, | |
751 | }; | |
752 | ||
b0766612 | 753 | static void curses_display_init(DisplayState *ds, DisplayOptions *opts) |
4d3b6f6e AZ |
754 | { |
755 | #ifndef _WIN32 | |
756 | if (!isatty(1)) { | |
757 | fprintf(stderr, "We need a terminal output\n"); | |
758 | exit(1); | |
759 | } | |
760 | #endif | |
761 | ||
2f8b7cd5 ST |
762 | setlocale(LC_CTYPE, ""); |
763 | if (opts->u.curses.charset) { | |
764 | font_charset = opts->u.curses.charset; | |
765 | } | |
4d3b6f6e AZ |
766 | curses_setup(); |
767 | curses_keyboard_setup(); | |
28695489 | 768 | atexit(curses_atexit); |
4d3b6f6e | 769 | |
032ac6f8 | 770 | curses_winch_init(); |
4d3b6f6e | 771 | |
fedf0d35 | 772 | dcl = g_new0(DisplayChangeListener, 1); |
7c20b4a3 | 773 | dcl->ops = &dcl_ops; |
5209089f | 774 | register_displaychangelistener(dcl); |
4d3b6f6e AZ |
775 | |
776 | invalidate = 1; | |
4d3b6f6e | 777 | } |
b0766612 GH |
778 | |
779 | static QemuDisplay qemu_display_curses = { | |
780 | .type = DISPLAY_TYPE_CURSES, | |
781 | .init = curses_display_init, | |
782 | }; | |
783 | ||
784 | static void register_curses(void) | |
785 | { | |
786 | qemu_display_register(&qemu_display_curses); | |
787 | } | |
788 | ||
789 | type_init(register_curses); |