| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * QEMU curses/ncurses display driver | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Copyright (c) 2005 Andrzej Zaborowski  <balrog@zabor.org> | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 
					
						
							|  |  |  |  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-29 17:49:51 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  | #include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #include <termios.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | #include <locale.h>
 | 
					
						
							|  |  |  | #include <wchar.h>
 | 
					
						
							|  |  |  | #include <iconv.h>
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 10:26:50 +02:00
										 |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2012-11-28 12:06:30 +01:00
										 |  |  | #include "ui/console.h"
 | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  | #include "ui/input.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:04 +01:00
										 |  |  | #include "sysemu/sysemu.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-07 15:32:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 21:53:55 +01:00
										 |  |  | #if defined(__APPLE__) || defined(__OpenBSD__)
 | 
					
						
							|  |  |  | #define _XOPEN_SOURCE_EXTENDED 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 12:38:11 +02:00
										 |  |  | /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */ | 
					
						
							|  |  |  | #undef KEY_EVENT
 | 
					
						
							|  |  |  | #include <curses.h>
 | 
					
						
							|  |  |  | #undef KEY_EVENT
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | #define FONT_HEIGHT 16
 | 
					
						
							|  |  |  | #define FONT_WIDTH 8
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  | enum maybe_keycode { | 
					
						
							|  |  |  |     CURSES_KEYCODE, | 
					
						
							|  |  |  |     CURSES_CHAR, | 
					
						
							|  |  |  |     CURSES_CHAR_OR_KEYCODE, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-13 14:51:41 +01:00
										 |  |  | static DisplayChangeListener *dcl; | 
					
						
							| 
									
										
										
										
											2020-03-05 13:45:24 +01:00
										 |  |  | static console_ch_t *screen; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | static WINDOW *screenpad = NULL; | 
					
						
							|  |  |  | static int width, height, gwidth, gheight, invalidate; | 
					
						
							|  |  |  | static int px, py, sminx, sminy, smaxx, smaxy; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | static const char *font_charset = "CP437"; | 
					
						
							| 
									
										
										
										
											2020-03-05 13:45:24 +01:00
										 |  |  | static cchar_t *vga_to_curses; | 
					
						
							| 
									
										
										
										
											2015-10-19 21:23:46 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-13 14:51:41 +01:00
										 |  |  | static void curses_update(DisplayChangeListener *dcl, | 
					
						
							|  |  |  |                           int x, int y, int w, int h) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-27 12:38:11 +02:00
										 |  |  |     console_ch_t *line; | 
					
						
							| 
									
										
										
										
											2022-08-19 16:39:30 +01:00
										 |  |  |     g_autofree cchar_t *curses_line = g_new(cchar_t, width); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |     wchar_t wch[CCHARW_MAX]; | 
					
						
							|  |  |  |     attr_t attrs; | 
					
						
							|  |  |  |     short colors; | 
					
						
							|  |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 12:38:11 +02:00
										 |  |  |     line = screen + y * width; | 
					
						
							|  |  |  |     for (h += y; y < h; y ++, line += width) { | 
					
						
							|  |  |  |         for (x = 0; x < width; x++) { | 
					
						
							| 
									
										
										
										
											2019-10-03 23:53:37 -04:00
										 |  |  |             chtype ch = line[x] & A_CHARTEXT; | 
					
						
							|  |  |  |             chtype at = line[x] & A_ATTRIBUTES; | 
					
						
							| 
									
										
										
										
											2019-10-03 23:53:38 -04:00
										 |  |  |             short color_pair = PAIR_NUMBER(line[x]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |             ret = getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NULL); | 
					
						
							|  |  |  |             if (ret == ERR || wch[0] == 0) { | 
					
						
							|  |  |  |                 wch[0] = ch; | 
					
						
							|  |  |  |                 wch[1] = 0; | 
					
						
							| 
									
										
										
										
											2017-09-27 12:38:11 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-10-03 23:53:38 -04:00
										 |  |  |             setcchar(&curses_line[x], wch, at, color_pair, NULL); | 
					
						
							| 
									
										
										
										
											2017-09-27 12:38:11 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |         mvwadd_wchnstr(screenpad, y, 0, curses_line, width); | 
					
						
							| 
									
										
										
										
											2017-09-27 12:38:11 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1); | 
					
						
							|  |  |  |     refresh(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void curses_calc_pad(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-14 14:27:08 +01:00
										 |  |  |     if (qemu_console_is_fixedsize(NULL)) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         width = gwidth; | 
					
						
							|  |  |  |         height = gheight; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         width = COLS; | 
					
						
							|  |  |  |         height = LINES; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (screenpad) | 
					
						
							|  |  |  |         delwin(screenpad); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     clear(); | 
					
						
							|  |  |  |     refresh(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     screenpad = newpad(height, width); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (width > COLS) { | 
					
						
							|  |  |  |         px = (width - COLS) / 2; | 
					
						
							|  |  |  |         sminx = 0; | 
					
						
							|  |  |  |         smaxx = COLS; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         px = 0; | 
					
						
							|  |  |  |         sminx = (COLS - width) / 2; | 
					
						
							|  |  |  |         smaxx = sminx + width; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (height > LINES) { | 
					
						
							|  |  |  |         py = (height - LINES) / 2; | 
					
						
							|  |  |  |         sminy = 0; | 
					
						
							|  |  |  |         smaxy = LINES; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         py = 0; | 
					
						
							|  |  |  |         sminy = (LINES - height) / 2; | 
					
						
							|  |  |  |         smaxy = sminy + height; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-13 14:51:41 +01:00
										 |  |  | static void curses_resize(DisplayChangeListener *dcl, | 
					
						
							|  |  |  |                           int width, int height) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-09-28 15:02:08 +02:00
										 |  |  |     if (width == gwidth && height == gheight) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2012-09-28 15:02:08 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-28 15:02:08 +02:00
										 |  |  |     gwidth = width; | 
					
						
							|  |  |  |     gheight = height; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     curses_calc_pad(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  | #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
 | 
					
						
							|  |  |  | static volatile sig_atomic_t got_sigwinch; | 
					
						
							|  |  |  | static void curses_winch_check(void) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     struct winsize { | 
					
						
							|  |  |  |         unsigned short ws_row; | 
					
						
							|  |  |  |         unsigned short ws_col; | 
					
						
							|  |  |  |         unsigned short ws_xpixel;   /* unused */ | 
					
						
							|  |  |  |         unsigned short ws_ypixel;   /* unused */ | 
					
						
							|  |  |  |     } ws; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  |     if (!got_sigwinch) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     got_sigwinch = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ioctl(1, TIOCGWINSZ, &ws) == -1) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resize_term(ws.ws_row, ws.ws_col); | 
					
						
							|  |  |  |     invalidate = 1; | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  | static void curses_winch_handler(int signum) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     got_sigwinch = true; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void curses_winch_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct sigaction old, winch = { | 
					
						
							|  |  |  |         .sa_handler  = curses_winch_handler, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     sigaction(SIGWINCH, &winch, &old); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | static void curses_winch_check(void) {} | 
					
						
							|  |  |  | static void curses_winch_init(void) {} | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-13 14:51:41 +01:00
										 |  |  | static void curses_cursor_position(DisplayChangeListener *dcl, | 
					
						
							|  |  |  |                                    int x, int y) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (x >= 0) { | 
					
						
							|  |  |  |         x = sminx + x - px; | 
					
						
							|  |  |  |         y = sminy + y - py; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (x >= 0 && y >= 0 && x < COLS && y < LINES) { | 
					
						
							|  |  |  |             move(y, x); | 
					
						
							|  |  |  |             curs_set(1); | 
					
						
							|  |  |  |             /* it seems that curs_set(1) must always be called before
 | 
					
						
							|  |  |  |              * curs_set(2) for the latter to have effect */ | 
					
						
							| 
									
										
										
										
											2013-03-14 14:27:08 +01:00
										 |  |  |             if (!qemu_console_is_graphic(NULL)) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |                 curs_set(2); | 
					
						
							| 
									
										
										
										
											2013-03-14 14:27:08 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     curs_set(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* generic keyboard conversion */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "curses_keys.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-01 16:12:16 -05:00
										 |  |  | static kbd_layout_t *kbd_layout = NULL; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  | static wint_t console_getch(enum maybe_keycode *maybe_keycode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     wint_t ret; | 
					
						
							|  |  |  |     switch (get_wch(&ret)) { | 
					
						
							|  |  |  |     case KEY_CODE_YES: | 
					
						
							|  |  |  |         *maybe_keycode = CURSES_KEYCODE; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case OK: | 
					
						
							|  |  |  |         *maybe_keycode = CURSES_CHAR; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case ERR: | 
					
						
							|  |  |  |         ret = -1; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-07-18 14:01:04 +02:00
										 |  |  |     default: | 
					
						
							|  |  |  |         abort(); | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int curses2foo(const int _curses2foo[], const int _curseskey2foo[], | 
					
						
							|  |  |  |                       int chr, enum maybe_keycode maybe_keycode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = -1; | 
					
						
							|  |  |  |     if (maybe_keycode == CURSES_CHAR) { | 
					
						
							|  |  |  |         if (chr < CURSES_CHARS) { | 
					
						
							|  |  |  |             ret = _curses2foo[chr]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (chr < CURSES_KEYS) { | 
					
						
							|  |  |  |             ret = _curseskey2foo[chr]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE && | 
					
						
							|  |  |  |             chr < CURSES_CHARS) { | 
					
						
							|  |  |  |             ret = _curses2foo[chr]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define curses2keycode(chr, maybe_keycode) \
 | 
					
						
							|  |  |  |     curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode) | 
					
						
							|  |  |  | #define curses2keysym(chr, maybe_keycode) \
 | 
					
						
							|  |  |  |     curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode) | 
					
						
							|  |  |  | #define curses2qemu(chr, maybe_keycode) \
 | 
					
						
							|  |  |  |     curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-01 13:03:04 +01:00
										 |  |  | static void curses_refresh(DisplayChangeListener *dcl) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-11 15:23:27 +01:00
										 |  |  |     int chr, keysym, keycode, keycode_alt; | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:46 +08:00
										 |  |  |     enum maybe_keycode maybe_keycode = CURSES_KEYCODE; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  |     curses_winch_check(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |     if (invalidate) { | 
					
						
							|  |  |  |         clear(); | 
					
						
							|  |  |  |         refresh(); | 
					
						
							|  |  |  |         curses_calc_pad(); | 
					
						
							| 
									
										
										
										
											2013-03-12 13:44:38 +01:00
										 |  |  |         graphic_hw_invalidate(NULL); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         invalidate = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-12 13:44:38 +01:00
										 |  |  |     graphic_hw_text_update(NULL, screen); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     while (1) { | 
					
						
							|  |  |  |         /* while there are any pending key strokes to process */ | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |         chr = console_getch(&maybe_keycode); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |         if (chr == -1) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-22 18:21:28 +00:00
										 |  |  | #ifdef KEY_RESIZE
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         /* this shouldn't occur when we use a custom SIGWINCH handler */ | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |         if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |             clear(); | 
					
						
							|  |  |  |             refresh(); | 
					
						
							|  |  |  |             curses_calc_pad(); | 
					
						
							| 
									
										
										
										
											2013-03-01 13:03:04 +01:00
										 |  |  |             curses_update(dcl, 0, 0, width, height); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-02-22 18:21:28 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |         keycode = curses2keycode(chr, maybe_keycode); | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |         keycode_alt = 0; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-03 18:25:57 +01:00
										 |  |  |         /* alt or esc key */ | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         if (keycode == 1) { | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:46 +08:00
										 |  |  |             enum maybe_keycode next_maybe_keycode = CURSES_KEYCODE; | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |             int nextchr = console_getch(&next_maybe_keycode); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |             if (nextchr != -1) { | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |                 chr = nextchr; | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |                 maybe_keycode = next_maybe_keycode; | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |                 keycode_alt = ALT; | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |                 keycode = curses2keycode(chr, maybe_keycode); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |                 if (keycode != -1) { | 
					
						
							|  |  |  |                     keycode |= ALT; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |                     /* process keys reserved for qemu */ | 
					
						
							|  |  |  |                     if (keycode >= QEMU_KEY_CONSOLE0 && | 
					
						
							|  |  |  |                             keycode < QEMU_KEY_CONSOLE0 + 9) { | 
					
						
							|  |  |  |                         erase(); | 
					
						
							|  |  |  |                         wnoutrefresh(stdscr); | 
					
						
							|  |  |  |                         console_select(keycode - QEMU_KEY_CONSOLE0); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |                         invalidate = 1; | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |         if (kbd_layout) { | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |             keysym = curses2keysym(chr, maybe_keycode); | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (keysym == -1) { | 
					
						
							| 
									
										
										
										
											2010-10-19 19:48:20 +02:00
										 |  |  |                 if (chr < ' ') { | 
					
						
							|  |  |  |                     keysym = chr + '@'; | 
					
						
							|  |  |  |                     if (keysym >= 'A' && keysym <= 'Z') | 
					
						
							|  |  |  |                         keysym += 'a' - 'A'; | 
					
						
							|  |  |  |                     keysym |= KEYSYM_CNTRL; | 
					
						
							|  |  |  |                 } else | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |                     keysym = chr; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-22 08:05:13 +01:00
										 |  |  |             keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK, | 
					
						
							| 
									
										
										
										
											2019-01-22 10:28:14 +01:00
										 |  |  |                                       NULL, false); | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |             if (keycode == 0) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             keycode |= (keysym & ~KEYSYM_MASK) >> 16; | 
					
						
							|  |  |  |             keycode |= keycode_alt; | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |         if (keycode == -1) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 14:27:08 +01:00
										 |  |  |         if (qemu_console_is_graphic(NULL)) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |             /* since terminals don't know about key press and release
 | 
					
						
							|  |  |  |              * events, we need to emit both for each key received */ | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             if (keycode & SHIFT) { | 
					
						
							|  |  |  |                 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (keycode & CNTRL) { | 
					
						
							|  |  |  |                 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (keycode & ALT) { | 
					
						
							|  |  |  |                 qemu_input_event_send_key_number(NULL, ALT_CODE, true); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |             if (keycode & ALTGR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |                 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-23 20:16:09 -04:00
										 |  |  |             qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |             qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2014-05-23 20:16:09 -04:00
										 |  |  |             qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |             qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |             if (keycode & ALTGR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |                 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (keycode & ALT) { | 
					
						
							|  |  |  |                 qemu_input_event_send_key_number(NULL, ALT_CODE, false); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (keycode & CNTRL) { | 
					
						
							|  |  |  |                 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2013-12-04 13:40:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (keycode & SHIFT) { | 
					
						
							|  |  |  |                 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false); | 
					
						
							| 
									
										
										
										
											2014-05-28 13:05:04 +02:00
										 |  |  |                 qemu_input_event_send_key_delay(0); | 
					
						
							| 
									
										
										
										
											2010-02-28 21:03:00 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-03-04 22:05:32 +01:00
										 |  |  |             keysym = curses2qemu(chr, maybe_keycode); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |             if (keysym == -1) | 
					
						
							|  |  |  |                 keysym = chr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             kbd_put_keysym(keysym); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-21 19:44:06 +00:00
										 |  |  | static void curses_atexit(void) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     endwin(); | 
					
						
							| 
									
										
										
										
											2020-03-05 13:45:24 +01:00
										 |  |  |     g_free(vga_to_curses); | 
					
						
							|  |  |  |     g_free(screen); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * In the following: | 
					
						
							|  |  |  |  * - fch is the font glyph number | 
					
						
							|  |  |  |  * - uch is the unicode value | 
					
						
							|  |  |  |  * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris) | 
					
						
							|  |  |  |  * - mbch is the native local-dependent multibyte representation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | /* Setup wchar glyph for one UCS-2 char */ | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  | static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv) | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     char mbch[MB_LEN_MAX]; | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |     wchar_t wch[2]; | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     char *puch, *pmbch; | 
					
						
							|  |  |  |     size_t such, smbch; | 
					
						
							|  |  |  |     mbstate_t ps; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     puch = (char *) &uch; | 
					
						
							|  |  |  |     pmbch = (char *) mbch; | 
					
						
							|  |  |  |     such = sizeof(uch); | 
					
						
							|  |  |  |     smbch = sizeof(mbch); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) { | 
					
						
							|  |  |  |         fprintf(stderr, "Could not convert 0x%04x " | 
					
						
							|  |  |  |                         "from UCS-2 to a multibyte character: %s\n", | 
					
						
							|  |  |  |                         uch, strerror(errno)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     memset(&ps, 0, sizeof(ps)); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |     if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |         fprintf(stderr, "Could not convert 0x%04x " | 
					
						
							|  |  |  |                         "from a multibyte character to wchar_t: %s\n", | 
					
						
							|  |  |  |                         uch, strerror(errno)); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     wch[1] = 0; | 
					
						
							|  |  |  |     setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Setup wchar glyph for one font character */ | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  | static void convert_font(unsigned char fch, iconv_t conv) | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     char mbch[MB_LEN_MAX]; | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |     wchar_t wch[2]; | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     char *pfch, *pmbch; | 
					
						
							|  |  |  |     size_t sfch, smbch; | 
					
						
							|  |  |  |     mbstate_t ps; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pfch = (char *) &fch; | 
					
						
							|  |  |  |     pmbch = (char *) &mbch; | 
					
						
							|  |  |  |     sfch = sizeof(fch); | 
					
						
							|  |  |  |     smbch = sizeof(mbch); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) { | 
					
						
							|  |  |  |         fprintf(stderr, "Could not convert font glyph 0x%02x " | 
					
						
							|  |  |  |                         "from %s to a multibyte character: %s\n", | 
					
						
							|  |  |  |                         fch, font_charset, strerror(errno)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     memset(&ps, 0, sizeof(ps)); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |     if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |         fprintf(stderr, "Could not convert font glyph 0x%02x " | 
					
						
							|  |  |  |                         "from a multibyte character to wchar_t: %s\n", | 
					
						
							|  |  |  |                         fch, strerror(errno)); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     wch[1] = 0; | 
					
						
							|  |  |  |     setcchar(&vga_to_curses[fch], wch, 0, 0, NULL); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Convert one wchar to UCS-2 */ | 
					
						
							|  |  |  | static uint16_t get_ucs(wchar_t wch, iconv_t conv) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     char mbch[MB_LEN_MAX]; | 
					
						
							|  |  |  |     uint16_t uch; | 
					
						
							|  |  |  |     char *pmbch, *puch; | 
					
						
							|  |  |  |     size_t smbch, such; | 
					
						
							|  |  |  |     mbstate_t ps; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memset(&ps, 0, sizeof(ps)); | 
					
						
							|  |  |  |     ret = wcrtomb(mbch, wch, &ps); | 
					
						
							|  |  |  |     if (ret == -1) { | 
					
						
							| 
									
										
										
										
											2019-05-27 16:25:40 +02:00
										 |  |  |         fprintf(stderr, "Could not convert 0x%04lx " | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |                         "from wchar_t to a multibyte character: %s\n", | 
					
						
							| 
									
										
										
										
											2019-05-27 16:25:40 +02:00
										 |  |  |                         (unsigned long)wch, strerror(errno)); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |         return 0xFFFD; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pmbch = (char *) mbch; | 
					
						
							|  |  |  |     puch = (char *) &uch; | 
					
						
							|  |  |  |     smbch = ret; | 
					
						
							|  |  |  |     such = sizeof(uch); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) { | 
					
						
							| 
									
										
										
										
											2019-05-27 16:25:40 +02:00
										 |  |  |         fprintf(stderr, "Could not convert 0x%04lx " | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |                         "from a multibyte character to UCS-2 : %s\n", | 
					
						
							| 
									
										
										
										
											2019-05-27 16:25:40 +02:00
										 |  |  |                         (unsigned long)wch, strerror(errno)); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |         return 0xFFFD; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     return uch; | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Setup mapping for vga to curses line graphics. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void font_setup(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     iconv_t ucs2_to_nativecharset; | 
					
						
							|  |  |  |     iconv_t nativecharset_to_ucs2; | 
					
						
							|  |  |  |     iconv_t font_conv; | 
					
						
							|  |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:45 +08:00
										 |  |  |     g_autofree gchar *local_codeset = g_get_codeset(); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Control characters are normally non-printable, but VGA does have | 
					
						
							|  |  |  |      * well-known glyphs for them. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-05 13:45:23 +01:00
										 |  |  |     static const uint16_t control_characters[0x20] = { | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |       0x0020, | 
					
						
							|  |  |  |       0x263a, | 
					
						
							|  |  |  |       0x263b, | 
					
						
							|  |  |  |       0x2665, | 
					
						
							|  |  |  |       0x2666, | 
					
						
							|  |  |  |       0x2663, | 
					
						
							|  |  |  |       0x2660, | 
					
						
							|  |  |  |       0x2022, | 
					
						
							|  |  |  |       0x25d8, | 
					
						
							|  |  |  |       0x25cb, | 
					
						
							|  |  |  |       0x25d9, | 
					
						
							|  |  |  |       0x2642, | 
					
						
							|  |  |  |       0x2640, | 
					
						
							|  |  |  |       0x266a, | 
					
						
							|  |  |  |       0x266b, | 
					
						
							|  |  |  |       0x263c, | 
					
						
							|  |  |  |       0x25ba, | 
					
						
							|  |  |  |       0x25c4, | 
					
						
							|  |  |  |       0x2195, | 
					
						
							|  |  |  |       0x203c, | 
					
						
							|  |  |  |       0x00b6, | 
					
						
							|  |  |  |       0x00a7, | 
					
						
							|  |  |  |       0x25ac, | 
					
						
							|  |  |  |       0x21a8, | 
					
						
							|  |  |  |       0x2191, | 
					
						
							|  |  |  |       0x2193, | 
					
						
							|  |  |  |       0x2192, | 
					
						
							|  |  |  |       0x2190, | 
					
						
							|  |  |  |       0x221f, | 
					
						
							|  |  |  |       0x2194, | 
					
						
							|  |  |  |       0x25b2, | 
					
						
							|  |  |  |       0x25bc | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:45 +08:00
										 |  |  |     ucs2_to_nativecharset = iconv_open(local_codeset, "UCS-2"); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     if (ucs2_to_nativecharset == (iconv_t) -1) { | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |         fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n", | 
					
						
							|  |  |  |                         strerror(errno)); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:45 +08:00
										 |  |  |     nativecharset_to_ucs2 = iconv_open("UCS-2", local_codeset); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     if (nativecharset_to_ucs2 == (iconv_t) -1) { | 
					
						
							|  |  |  |         iconv_close(ucs2_to_nativecharset); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |         fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n", | 
					
						
							|  |  |  |                         strerror(errno)); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:45 +08:00
										 |  |  |     font_conv = iconv_open(local_codeset, font_charset); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     if (font_conv == (iconv_t) -1) { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |         iconv_close(ucs2_to_nativecharset); | 
					
						
							|  |  |  |         iconv_close(nativecharset_to_ucs2); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |         fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n", | 
					
						
							|  |  |  |                         font_charset, strerror(errno)); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Control characters */ | 
					
						
							|  |  |  |     for (i = 0; i <= 0x1F; i++) { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |         convert_ucs(i, control_characters[i], ucs2_to_nativecharset); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0x20; i <= 0xFF; i++) { | 
					
						
							|  |  |  |         convert_font(i, font_conv); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* DEL */ | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 07:43:45 +08:00
										 |  |  |     if (strcmp(local_codeset, "UTF-8")) { | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |         /* Non-Unicode capable, use termcap equivalents for those available */ | 
					
						
							|  |  |  |         for (i = 0; i <= 0xFF; i++) { | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:07 +02:00
										 |  |  |             wchar_t wch[CCHARW_MAX]; | 
					
						
							|  |  |  |             attr_t attr; | 
					
						
							|  |  |  |             short color; | 
					
						
							|  |  |  |             int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ret = getcchar(&vga_to_curses[i], wch, &attr, &color, NULL); | 
					
						
							|  |  |  |             if (ret == ERR) | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             switch (get_ucs(wch[0], nativecharset_to_ucs2)) { | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |             case 0x00a3: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_STERLING; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2591: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_BOARD; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2592: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_CKBOARD; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2502: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_VLINE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2524: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_RTEE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2510: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_URCORNER; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2514: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_LLCORNER; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2534: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_BTEE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x252c: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_TTEE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x251c: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_LTEE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2500: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_HLINE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x253c: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_PLUS; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x256c: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_LANTERN; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x256a: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_NEQUAL; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2518: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_LRCORNER; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x250c: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_ULCORNER; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2588: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_BLOCK; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x03c0: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_PI; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x00b1: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_PLMINUS; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2265: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_GEQUAL; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2264: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_LEQUAL; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x00b0: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_DEGREE; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x25a0: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_BULLET; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2666: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_DIAMOND; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2192: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_RARROW; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2190: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_LARROW; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2191: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_UARROW; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x2193: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_DARROW; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x23ba: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_S1; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x23bb: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_S3; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x23bc: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_S7; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 0x23bd: | 
					
						
							|  |  |  |                 vga_to_curses[i] = *WACS_S9; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-27 20:33:06 +02:00
										 |  |  |     iconv_close(ucs2_to_nativecharset); | 
					
						
							|  |  |  |     iconv_close(nativecharset_to_ucs2); | 
					
						
							| 
									
										
										
										
											2019-03-14 18:25:24 +01:00
										 |  |  |     iconv_close(font_conv); | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | static void curses_setup(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i, colour_default[8] = { | 
					
						
							| 
									
										
										
										
											2015-11-29 22:28:24 +09:00
										 |  |  |         [QEMU_COLOR_BLACK]   = COLOR_BLACK, | 
					
						
							|  |  |  |         [QEMU_COLOR_BLUE]    = COLOR_BLUE, | 
					
						
							|  |  |  |         [QEMU_COLOR_GREEN]   = COLOR_GREEN, | 
					
						
							|  |  |  |         [QEMU_COLOR_CYAN]    = COLOR_CYAN, | 
					
						
							|  |  |  |         [QEMU_COLOR_RED]     = COLOR_RED, | 
					
						
							|  |  |  |         [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA, | 
					
						
							|  |  |  |         [QEMU_COLOR_YELLOW]  = COLOR_YELLOW, | 
					
						
							|  |  |  |         [QEMU_COLOR_WHITE]   = COLOR_WHITE, | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* input as raw as possible, let everything be interpreted
 | 
					
						
							|  |  |  |      * by the guest system */ | 
					
						
							|  |  |  |     initscr(); noecho(); intrflush(stdscr, FALSE); | 
					
						
							|  |  |  |     nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); | 
					
						
							|  |  |  |     start_color(); raw(); scrollok(stdscr, FALSE); | 
					
						
							| 
									
										
										
										
											2019-03-03 18:25:57 +01:00
										 |  |  |     set_escdelay(25); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-29 22:28:24 +09:00
										 |  |  |     /* Make color pair to match color format (3bits bg:3bits fg) */ | 
					
						
							| 
									
										
										
										
											2015-10-19 21:23:10 +09:00
										 |  |  |     for (i = 0; i < 64; i++) { | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |         init_pair(i, colour_default[i & 7], colour_default[i >> 3]); | 
					
						
							| 
									
										
										
										
											2015-10-19 21:23:10 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-11-29 22:28:24 +09:00
										 |  |  |     /* Set default color for more than 64 for safety. */ | 
					
						
							| 
									
										
										
										
											2015-10-19 21:23:10 +09:00
										 |  |  |     for (i = 64; i < COLOR_PAIRS; i++) { | 
					
						
							|  |  |  |         init_pair(i, COLOR_WHITE, COLOR_BLACK); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-19 21:23:46 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     font_setup(); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void curses_keyboard_setup(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if defined(__APPLE__)
 | 
					
						
							|  |  |  |     /* always use generic keymaps */ | 
					
						
							|  |  |  |     if (!keyboard_layout) | 
					
						
							|  |  |  |         keyboard_layout = "en-us"; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     if(keyboard_layout) { | 
					
						
							| 
									
										
										
										
											2018-10-17 10:26:50 +02:00
										 |  |  |         kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout, | 
					
						
							|  |  |  |                                           &error_fatal); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-13 14:51:41 +01:00
										 |  |  | static const DisplayChangeListenerOps dcl_ops = { | 
					
						
							|  |  |  |     .dpy_name        = "curses", | 
					
						
							|  |  |  |     .dpy_text_update = curses_update, | 
					
						
							|  |  |  |     .dpy_text_resize = curses_resize, | 
					
						
							|  |  |  |     .dpy_refresh     = curses_refresh, | 
					
						
							|  |  |  |     .dpy_text_cursor = curses_cursor_position, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 11:05:38 +01:00
										 |  |  | static void curses_display_init(DisplayState *ds, DisplayOptions *opts) | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | { | 
					
						
							|  |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  |     if (!isatty(1)) { | 
					
						
							|  |  |  |         fprintf(stderr, "We need a terminal output\n"); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-11 14:51:27 +01:00
										 |  |  |     setlocale(LC_CTYPE, ""); | 
					
						
							|  |  |  |     if (opts->u.curses.charset) { | 
					
						
							|  |  |  |         font_charset = opts->u.curses.charset; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-05 13:45:24 +01:00
										 |  |  |     screen = g_new0(console_ch_t, 160 * 100); | 
					
						
							|  |  |  |     vga_to_curses = g_new0(cchar_t, 256); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  |     curses_setup(); | 
					
						
							|  |  |  |     curses_keyboard_setup(); | 
					
						
							| 
									
										
										
										
											2010-03-21 14:13:02 -05:00
										 |  |  |     atexit(curses_atexit); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 15:35:03 +01:00
										 |  |  |     curses_winch_init(); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 17:12:03 +01:00
										 |  |  |     dcl = g_new0(DisplayChangeListener, 1); | 
					
						
							| 
									
										
										
										
											2012-11-13 14:51:41 +01:00
										 |  |  |     dcl->ops = &dcl_ops; | 
					
						
							| 
									
										
										
										
											2013-04-23 15:44:31 +02:00
										 |  |  |     register_displaychangelistener(dcl); | 
					
						
							| 
									
										
										
										
											2008-02-10 16:33:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     invalidate = 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-01 11:05:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static QemuDisplay qemu_display_curses = { | 
					
						
							|  |  |  |     .type       = DISPLAY_TYPE_CURSES, | 
					
						
							|  |  |  |     .init       = curses_display_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void register_curses(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qemu_display_register(&qemu_display_curses); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type_init(register_curses); |