2008-02-10 19:33:14 +03: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 17:35:07 +03:00
|
|
|
|
2016-01-29 20:49:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2008-02-10 19:33:14 +03:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
2019-03-11 16:51:27 +03:00
|
|
|
#include <locale.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <langinfo.h>
|
|
|
|
#include <iconv.h>
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2018-10-17 11:26:50 +03:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2012-11-28 15:06:30 +04:00
|
|
|
#include "ui/console.h"
|
2013-12-04 16:40:20 +04:00
|
|
|
#include "ui/input.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
2009-03-07 18:32:56 +03:00
|
|
|
|
2017-09-27 13:38:11 +03: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 19:33:14 +03:00
|
|
|
#define FONT_HEIGHT 16
|
|
|
|
#define FONT_WIDTH 8
|
|
|
|
|
2019-03-05 00:05:32 +03:00
|
|
|
enum maybe_keycode {
|
|
|
|
CURSES_KEYCODE,
|
|
|
|
CURSES_CHAR,
|
|
|
|
CURSES_CHAR_OR_KEYCODE,
|
|
|
|
};
|
|
|
|
|
2012-11-13 17:51:41 +04:00
|
|
|
static DisplayChangeListener *dcl;
|
2009-10-02 01:12:16 +04:00
|
|
|
static console_ch_t screen[160 * 100];
|
2008-02-10 19:33:14 +03:00
|
|
|
static WINDOW *screenpad = NULL;
|
|
|
|
static int width, height, gwidth, gheight, invalidate;
|
|
|
|
static int px, py, sminx, sminy, smaxx, smaxy;
|
|
|
|
|
2019-03-11 16:51:27 +03:00
|
|
|
static const char *font_charset = "CP437";
|
|
|
|
static cchar_t vga_to_curses[256];
|
2015-10-19 15:23:46 +03:00
|
|
|
|
2012-11-13 17:51:41 +04:00
|
|
|
static void curses_update(DisplayChangeListener *dcl,
|
|
|
|
int x, int y, int w, int h)
|
2008-02-10 19:33:14 +03:00
|
|
|
{
|
2017-09-27 13:38:11 +03:00
|
|
|
console_ch_t *line;
|
2019-03-11 16:51:27 +03:00
|
|
|
cchar_t curses_line[width];
|
2019-04-27 21:33:07 +03:00
|
|
|
wchar_t wch[CCHARW_MAX];
|
|
|
|
attr_t attrs;
|
|
|
|
short colors;
|
|
|
|
int ret;
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2017-09-27 13:38:11 +03:00
|
|
|
line = screen + y * width;
|
|
|
|
for (h += y; y < h; y ++, line += width) {
|
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
chtype ch = line[x] & 0xff;
|
|
|
|
chtype at = line[x] & ~0xff;
|
2019-04-27 21:33:07 +03: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 13:38:11 +03:00
|
|
|
}
|
2019-04-27 21:33:07 +03:00
|
|
|
setcchar(&curses_line[x], wch, at, 0, NULL);
|
2017-09-27 13:38:11 +03:00
|
|
|
}
|
2019-03-11 16:51:27 +03:00
|
|
|
mvwadd_wchnstr(screenpad, y, 0, curses_line, width);
|
2017-09-27 13:38:11 +03:00
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
|
|
|
|
pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void curses_calc_pad(void)
|
|
|
|
{
|
2013-03-14 17:27:08 +04:00
|
|
|
if (qemu_console_is_fixedsize(NULL)) {
|
2008-02-10 19:33:14 +03: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 17:51:41 +04:00
|
|
|
static void curses_resize(DisplayChangeListener *dcl,
|
|
|
|
int width, int height)
|
2008-02-10 19:33:14 +03:00
|
|
|
{
|
2012-09-28 17:02:08 +04:00
|
|
|
if (width == gwidth && height == gheight) {
|
2008-02-10 19:33:14 +03:00
|
|
|
return;
|
2012-09-28 17:02:08 +04:00
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2012-09-28 17:02:08 +04:00
|
|
|
gwidth = width;
|
|
|
|
gheight = height;
|
2008-02-10 19:33:14 +03:00
|
|
|
|
|
|
|
curses_calc_pad();
|
|
|
|
}
|
|
|
|
|
2013-11-22 18:35:03 +04: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 19:33:14 +03: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 18:35:03 +04:00
|
|
|
if (!got_sigwinch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
got_sigwinch = false;
|
|
|
|
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
|
2008-02-10 19:33:14 +03:00
|
|
|
return;
|
2013-11-22 18:35:03 +04:00
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
|
|
|
|
resize_term(ws.ws_row, ws.ws_col);
|
|
|
|
invalidate = 1;
|
2013-11-22 18:35:03 +04:00
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2013-11-22 18:35:03 +04:00
|
|
|
static void curses_winch_handler(int signum)
|
|
|
|
{
|
|
|
|
got_sigwinch = true;
|
2008-02-10 19:33:14 +03:00
|
|
|
}
|
2013-11-22 18:35:03 +04: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 19:33:14 +03:00
|
|
|
#endif
|
|
|
|
|
2012-11-13 17:51:41 +04:00
|
|
|
static void curses_cursor_position(DisplayChangeListener *dcl,
|
|
|
|
int x, int y)
|
2008-02-10 19:33:14 +03: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 17:27:08 +04:00
|
|
|
if (!qemu_console_is_graphic(NULL)) {
|
2008-02-10 19:33:14 +03:00
|
|
|
curs_set(2);
|
2013-03-14 17:27:08 +04:00
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curs_set(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic keyboard conversion */
|
|
|
|
|
|
|
|
#include "curses_keys.h"
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static kbd_layout_t *kbd_layout = NULL;
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2019-03-05 00:05:32 +03: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;
|
|
|
|
}
|
|
|
|
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 16:03:04 +04:00
|
|
|
static void curses_refresh(DisplayChangeListener *dcl)
|
2008-02-10 19:33:14 +03:00
|
|
|
{
|
2016-08-11 17:23:27 +03:00
|
|
|
int chr, keysym, keycode, keycode_alt;
|
2019-03-05 00:05:32 +03:00
|
|
|
enum maybe_keycode maybe_keycode;
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2013-11-22 18:35:03 +04:00
|
|
|
curses_winch_check();
|
|
|
|
|
2008-02-10 19:33:14 +03:00
|
|
|
if (invalidate) {
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
curses_calc_pad();
|
2013-03-12 16:44:38 +04:00
|
|
|
graphic_hw_invalidate(NULL);
|
2008-02-10 19:33:14 +03:00
|
|
|
invalidate = 0;
|
|
|
|
}
|
|
|
|
|
2013-03-12 16:44:38 +04:00
|
|
|
graphic_hw_text_update(NULL, screen);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* while there are any pending key strokes to process */
|
2019-03-05 00:05:32 +03:00
|
|
|
chr = console_getch(&maybe_keycode);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2019-03-05 00:05:32 +03:00
|
|
|
if (chr == -1)
|
2008-02-10 19:33:14 +03:00
|
|
|
break;
|
|
|
|
|
2008-02-22 21:21:28 +03:00
|
|
|
#ifdef KEY_RESIZE
|
2008-02-10 19:33:14 +03:00
|
|
|
/* this shouldn't occur when we use a custom SIGWINCH handler */
|
2019-03-05 00:05:32 +03:00
|
|
|
if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) {
|
2008-02-10 19:33:14 +03:00
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
curses_calc_pad();
|
2013-03-01 16:03:04 +04:00
|
|
|
curses_update(dcl, 0, 0, width, height);
|
2008-02-10 19:33:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-22 21:21:28 +03:00
|
|
|
#endif
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2019-03-05 00:05:32 +03:00
|
|
|
keycode = curses2keycode(chr, maybe_keycode);
|
2010-02-28 23:03:00 +03:00
|
|
|
keycode_alt = 0;
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2019-03-03 20:25:57 +03:00
|
|
|
/* alt or esc key */
|
2008-02-10 19:33:14 +03:00
|
|
|
if (keycode == 1) {
|
2019-03-05 00:05:32 +03:00
|
|
|
enum maybe_keycode next_maybe_keycode;
|
|
|
|
int nextchr = console_getch(&next_maybe_keycode);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2019-03-05 00:05:32 +03:00
|
|
|
if (nextchr != -1) {
|
2010-02-28 23:03:00 +03:00
|
|
|
chr = nextchr;
|
2019-03-05 00:05:32 +03:00
|
|
|
maybe_keycode = next_maybe_keycode;
|
2010-02-28 23:03:00 +03:00
|
|
|
keycode_alt = ALT;
|
2019-03-05 00:05:32 +03:00
|
|
|
keycode = curses2keycode(chr, maybe_keycode);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2010-02-28 23:03:00 +03:00
|
|
|
if (keycode != -1) {
|
|
|
|
keycode |= ALT;
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2010-02-28 23:03:00 +03: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 19:33:14 +03:00
|
|
|
|
2010-02-28 23:03:00 +03:00
|
|
|
invalidate = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 23:03:00 +03:00
|
|
|
if (kbd_layout) {
|
2019-03-05 00:05:32 +03:00
|
|
|
keysym = curses2keysym(chr, maybe_keycode);
|
2010-02-28 23:03:00 +03:00
|
|
|
|
|
|
|
if (keysym == -1) {
|
2010-10-19 21:48:20 +04:00
|
|
|
if (chr < ' ') {
|
|
|
|
keysym = chr + '@';
|
|
|
|
if (keysym >= 'A' && keysym <= 'Z')
|
|
|
|
keysym += 'a' - 'A';
|
|
|
|
keysym |= KEYSYM_CNTRL;
|
|
|
|
} else
|
2010-02-28 23:03:00 +03:00
|
|
|
keysym = chr;
|
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2018-02-22 10:05:13 +03:00
|
|
|
keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
|
2019-01-22 12:28:14 +03:00
|
|
|
NULL, false);
|
2010-02-28 23:03:00 +03:00
|
|
|
if (keycode == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
keycode |= (keysym & ~KEYSYM_MASK) >> 16;
|
|
|
|
keycode |= keycode_alt;
|
2008-02-10 19:33:14 +03:00
|
|
|
}
|
|
|
|
|
2010-02-28 23:03:00 +03:00
|
|
|
if (keycode == -1)
|
|
|
|
continue;
|
|
|
|
|
2013-03-14 17:27:08 +04:00
|
|
|
if (qemu_console_is_graphic(NULL)) {
|
2008-02-10 19:33:14 +03:00
|
|
|
/* since terminals don't know about key press and release
|
|
|
|
* events, we need to emit both for each key received */
|
2013-12-04 16:40:20 +04:00
|
|
|
if (keycode & SHIFT) {
|
|
|
|
qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
}
|
|
|
|
if (keycode & CNTRL) {
|
|
|
|
qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
}
|
|
|
|
if (keycode & ALT) {
|
|
|
|
qemu_input_event_send_key_number(NULL, ALT_CODE, true);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
}
|
2010-02-28 23:03:00 +03:00
|
|
|
if (keycode & ALTGR) {
|
2013-12-04 16:40:20 +04:00
|
|
|
qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2010-02-28 23:03:00 +03:00
|
|
|
}
|
2013-12-04 16:40:20 +04:00
|
|
|
|
2014-05-24 04:16:09 +04:00
|
|
|
qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2014-05-24 04:16:09 +04:00
|
|
|
qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
|
2010-02-28 23:03:00 +03:00
|
|
|
if (keycode & ALTGR) {
|
2013-12-04 16:40:20 +04:00
|
|
|
qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
}
|
|
|
|
if (keycode & ALT) {
|
|
|
|
qemu_input_event_send_key_number(NULL, ALT_CODE, false);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
}
|
|
|
|
if (keycode & CNTRL) {
|
|
|
|
qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2013-12-04 16:40:20 +04:00
|
|
|
}
|
|
|
|
if (keycode & SHIFT) {
|
|
|
|
qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
|
2014-05-28 15:05:04 +04:00
|
|
|
qemu_input_event_send_key_delay(0);
|
2010-02-28 23:03:00 +03:00
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
} else {
|
2019-03-05 00:05:32 +03:00
|
|
|
keysym = curses2qemu(chr, maybe_keycode);
|
2008-02-10 19:33:14 +03:00
|
|
|
if (keysym == -1)
|
|
|
|
keysym = chr;
|
|
|
|
|
|
|
|
kbd_put_keysym(keysym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 22:44:06 +03:00
|
|
|
static void curses_atexit(void)
|
2008-02-10 19:33:14 +03:00
|
|
|
{
|
|
|
|
endwin();
|
|
|
|
}
|
|
|
|
|
2019-04-27 21:33:06 +03: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 16:51:27 +03:00
|
|
|
/* Setup wchar glyph for one UCS-2 char */
|
2019-04-27 21:33:06 +03:00
|
|
|
static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv)
|
2019-03-11 16:51:27 +03:00
|
|
|
{
|
2019-04-27 21:33:06 +03:00
|
|
|
char mbch[MB_LEN_MAX];
|
2019-04-27 21:33:07 +03:00
|
|
|
wchar_t wch[2];
|
2019-04-27 21:33:06 +03: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 16:51:27 +03:00
|
|
|
|
2019-04-27 21:33:06 +03:00
|
|
|
memset(&ps, 0, sizeof(ps));
|
2019-04-27 21:33:07 +03:00
|
|
|
if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
|
2019-04-27 21:33:06 +03:00
|
|
|
fprintf(stderr, "Could not convert 0x%04x "
|
|
|
|
"from a multibyte character to wchar_t: %s\n",
|
|
|
|
uch, strerror(errno));
|
|
|
|
return;
|
2019-03-11 16:51:27 +03:00
|
|
|
}
|
2019-04-27 21:33:07 +03:00
|
|
|
|
|
|
|
wch[1] = 0;
|
|
|
|
setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
|
2019-03-11 16:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup wchar glyph for one font character */
|
2019-04-27 21:33:06 +03:00
|
|
|
static void convert_font(unsigned char fch, iconv_t conv)
|
2019-03-11 16:51:27 +03:00
|
|
|
{
|
2019-04-27 21:33:06 +03:00
|
|
|
char mbch[MB_LEN_MAX];
|
2019-04-27 21:33:07 +03:00
|
|
|
wchar_t wch[2];
|
2019-04-27 21:33:06 +03: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 16:51:27 +03:00
|
|
|
|
2019-04-27 21:33:06 +03:00
|
|
|
memset(&ps, 0, sizeof(ps));
|
2019-04-27 21:33:07 +03:00
|
|
|
if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
|
2019-04-27 21:33:06 +03: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 16:51:27 +03:00
|
|
|
}
|
2019-04-27 21:33:07 +03:00
|
|
|
|
|
|
|
wch[1] = 0;
|
|
|
|
setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
|
2019-03-11 16:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert one wchar to UCS-2 */
|
|
|
|
static uint16_t get_ucs(wchar_t wch, iconv_t conv)
|
|
|
|
{
|
2019-04-27 21:33:06 +03: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 17:25:40 +03:00
|
|
|
fprintf(stderr, "Could not convert 0x%04lx "
|
2019-04-27 21:33:06 +03:00
|
|
|
"from wchar_t to a multibyte character: %s\n",
|
2019-05-27 17:25:40 +03:00
|
|
|
(unsigned long)wch, strerror(errno));
|
2019-04-27 21:33:06 +03: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 17:25:40 +03:00
|
|
|
fprintf(stderr, "Could not convert 0x%04lx "
|
2019-04-27 21:33:06 +03:00
|
|
|
"from a multibyte character to UCS-2 : %s\n",
|
2019-05-27 17:25:40 +03:00
|
|
|
(unsigned long)wch, strerror(errno));
|
2019-03-11 16:51:27 +03:00
|
|
|
return 0xFFFD;
|
|
|
|
}
|
|
|
|
|
2019-04-27 21:33:06 +03:00
|
|
|
return uch;
|
2019-03-11 16:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup mapping for vga to curses line graphics.
|
|
|
|
*/
|
|
|
|
static void font_setup(void)
|
|
|
|
{
|
2019-04-27 21:33:06 +03:00
|
|
|
iconv_t ucs2_to_nativecharset;
|
|
|
|
iconv_t nativecharset_to_ucs2;
|
|
|
|
iconv_t font_conv;
|
|
|
|
int i;
|
|
|
|
|
2019-03-11 16:51:27 +03:00
|
|
|
/*
|
|
|
|
* Control characters are normally non-printable, but VGA does have
|
|
|
|
* well-known glyphs for them.
|
|
|
|
*/
|
|
|
|
static uint16_t control_characters[0x20] = {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-04-27 21:33:06 +03:00
|
|
|
ucs2_to_nativecharset = iconv_open(nl_langinfo(CODESET), "UCS-2");
|
|
|
|
if (ucs2_to_nativecharset == (iconv_t) -1) {
|
2019-03-11 16:51:27 +03:00
|
|
|
fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-27 21:33:06 +03:00
|
|
|
nativecharset_to_ucs2 = iconv_open("UCS-2", nl_langinfo(CODESET));
|
|
|
|
if (nativecharset_to_ucs2 == (iconv_t) -1) {
|
|
|
|
iconv_close(ucs2_to_nativecharset);
|
2019-03-11 16:51:27 +03:00
|
|
|
fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-27 21:33:06 +03:00
|
|
|
font_conv = iconv_open(nl_langinfo(CODESET), font_charset);
|
2019-03-11 16:51:27 +03:00
|
|
|
if (font_conv == (iconv_t) -1) {
|
2019-04-27 21:33:06 +03:00
|
|
|
iconv_close(ucs2_to_nativecharset);
|
|
|
|
iconv_close(nativecharset_to_ucs2);
|
2019-03-11 16:51:27 +03: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 21:33:06 +03:00
|
|
|
convert_ucs(i, control_characters[i], ucs2_to_nativecharset);
|
2019-03-11 16:51:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0x20; i <= 0xFF; i++) {
|
|
|
|
convert_font(i, font_conv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DEL */
|
2019-04-27 21:33:06 +03:00
|
|
|
convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset);
|
2019-03-11 16:51:27 +03:00
|
|
|
|
|
|
|
if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
|
|
|
|
/* Non-Unicode capable, use termcap equivalents for those available */
|
|
|
|
for (i = 0; i <= 0xFF; i++) {
|
2019-04-27 21:33:07 +03: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 16:51:27 +03: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 21:33:06 +03:00
|
|
|
iconv_close(ucs2_to_nativecharset);
|
|
|
|
iconv_close(nativecharset_to_ucs2);
|
2019-03-14 20:25:24 +03:00
|
|
|
iconv_close(font_conv);
|
2019-03-11 16:51:27 +03:00
|
|
|
}
|
|
|
|
|
2008-02-10 19:33:14 +03:00
|
|
|
static void curses_setup(void)
|
|
|
|
{
|
|
|
|
int i, colour_default[8] = {
|
2015-11-29 16:28:24 +03: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 19:33:14 +03: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 20:25:57 +03:00
|
|
|
set_escdelay(25);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2015-11-29 16:28:24 +03:00
|
|
|
/* Make color pair to match color format (3bits bg:3bits fg) */
|
2015-10-19 15:23:10 +03:00
|
|
|
for (i = 0; i < 64; i++) {
|
2008-02-10 19:33:14 +03:00
|
|
|
init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
|
2015-10-19 15:23:10 +03:00
|
|
|
}
|
2015-11-29 16:28:24 +03:00
|
|
|
/* Set default color for more than 64 for safety. */
|
2015-10-19 15:23:10 +03:00
|
|
|
for (i = 64; i < COLOR_PAIRS; i++) {
|
|
|
|
init_pair(i, COLOR_WHITE, COLOR_BLACK);
|
|
|
|
}
|
2015-10-19 15:23:46 +03:00
|
|
|
|
2019-03-11 16:51:27 +03:00
|
|
|
font_setup();
|
2008-02-10 19:33:14 +03: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 11:26:50 +03:00
|
|
|
kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
|
|
|
|
&error_fatal);
|
2008-02-10 19:33:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-13 17:51:41 +04: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 13:05:38 +03:00
|
|
|
static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
|
2008-02-10 19:33:14 +03:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (!isatty(1)) {
|
|
|
|
fprintf(stderr, "We need a terminal output\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-11 16:51:27 +03:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
if (opts->u.curses.charset) {
|
|
|
|
font_charset = opts->u.curses.charset;
|
|
|
|
}
|
2008-02-10 19:33:14 +03:00
|
|
|
curses_setup();
|
|
|
|
curses_keyboard_setup();
|
2010-03-21 22:13:02 +03:00
|
|
|
atexit(curses_atexit);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2013-11-22 18:35:03 +04:00
|
|
|
curses_winch_init();
|
2008-02-10 19:33:14 +03:00
|
|
|
|
2015-11-03 19:12:03 +03:00
|
|
|
dcl = g_new0(DisplayChangeListener, 1);
|
2012-11-13 17:51:41 +04:00
|
|
|
dcl->ops = &dcl_ops;
|
2013-04-23 17:44:31 +04:00
|
|
|
register_displaychangelistener(dcl);
|
2008-02-10 19:33:14 +03:00
|
|
|
|
|
|
|
invalidate = 1;
|
|
|
|
}
|
2018-03-01 13:05:38 +03: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);
|