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.
|
|
|
|
*/
|
|
|
|
#include <curses.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-07 15:32:56 +00:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "console.h"
|
|
|
|
#include "sysemu.h"
|
|
|
|
|
2008-02-10 16:33:14 +00:00
|
|
|
#define FONT_HEIGHT 16
|
|
|
|
#define FONT_WIDTH 8
|
|
|
|
|
2009-10-01 21:12:16 +00:00
|
|
|
static console_ch_t screen[160 * 100];
|
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;
|
|
|
|
|
|
|
|
static void curses_update(DisplayState *ds, int x, int y, int w, int h)
|
|
|
|
{
|
|
|
|
chtype *line;
|
|
|
|
|
|
|
|
line = ((chtype *) screen) + y * width;
|
|
|
|
for (h += y; y < h; y ++, line += width)
|
|
|
|
mvwaddchnstr(screenpad, y, 0, line, width);
|
|
|
|
|
|
|
|
pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void curses_calc_pad(void)
|
|
|
|
{
|
2008-09-24 03:32:33 +00:00
|
|
|
if (is_fixedsize_console()) {
|
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-09-28 13:02:08 +00:00
|
|
|
static void curses_resize(DisplayState *ds, int width, int height)
|
2008-02-10 16:33:14 +00:00
|
|
|
{
|
2012-09-28 13:02:08 +00:00
|
|
|
if (width == gwidth && height == gheight) {
|
2008-02-10 16:33:14 +00:00
|
|
|
return;
|
2012-09-28 13:02:08 +00:00
|
|
|
}
|
2008-02-10 16:33:14 +00:00
|
|
|
|
2012-09-28 13:02:08 +00:00
|
|
|
gwidth = width;
|
|
|
|
gheight = height;
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
curses_calc_pad();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2008-02-22 18:21:28 +00:00
|
|
|
#if defined(SIGWINCH) && defined(KEY_RESIZE)
|
2008-02-10 16:33:14 +00:00
|
|
|
static void curses_winch_handler(int signum)
|
|
|
|
{
|
|
|
|
struct winsize {
|
|
|
|
unsigned short ws_row;
|
|
|
|
unsigned short ws_col;
|
|
|
|
unsigned short ws_xpixel; /* unused */
|
|
|
|
unsigned short ws_ypixel; /* unused */
|
|
|
|
} ws;
|
|
|
|
|
|
|
|
/* terminal size changed */
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
resize_term(ws.ws_row, ws.ws_col);
|
|
|
|
curses_calc_pad();
|
|
|
|
invalidate = 1;
|
|
|
|
|
|
|
|
/* some systems require this */
|
|
|
|
signal(SIGWINCH, curses_winch_handler);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void curses_cursor_position(DisplayState *ds, int x, int y)
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
if (!is_graphic_console())
|
|
|
|
curs_set(2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curs_set(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic keyboard conversion */
|
|
|
|
|
|
|
|
#include "curses_keys.h"
|
|
|
|
|
2009-10-01 21:12:16 +00:00
|
|
|
static kbd_layout_t *kbd_layout = NULL;
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
static void curses_refresh(DisplayState *ds)
|
|
|
|
{
|
2010-02-28 20:03:00 +00:00
|
|
|
int chr, nextchr, keysym, keycode, keycode_alt;
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
if (invalidate) {
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
curses_calc_pad();
|
|
|
|
vga_hw_invalidate();
|
|
|
|
invalidate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
vga_hw_text_update(screen);
|
|
|
|
|
|
|
|
nextchr = ERR;
|
|
|
|
while (1) {
|
|
|
|
/* while there are any pending key strokes to process */
|
|
|
|
if (nextchr == ERR)
|
|
|
|
chr = getch();
|
|
|
|
else {
|
|
|
|
chr = nextchr;
|
|
|
|
nextchr = ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chr == ERR)
|
|
|
|
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 */
|
|
|
|
if (chr == KEY_RESIZE) {
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
curses_calc_pad();
|
|
|
|
curses_update(ds, 0, 0, width, height);
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-22 18:21:28 +00:00
|
|
|
#endif
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
keycode = curses2keycode[chr];
|
2010-02-28 20:03:00 +00:00
|
|
|
keycode_alt = 0;
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
/* alt key */
|
|
|
|
if (keycode == 1) {
|
|
|
|
nextchr = getch();
|
|
|
|
|
|
|
|
if (nextchr != ERR) {
|
2010-02-28 20:03:00 +00:00
|
|
|
chr = nextchr;
|
|
|
|
keycode_alt = ALT;
|
2008-02-10 16:33:14 +00:00
|
|
|
keycode = curses2keycode[nextchr];
|
|
|
|
nextchr = ERR;
|
|
|
|
|
2010-02-28 20:03:00 +00:00
|
|
|
if (keycode != -1) {
|
|
|
|
keycode |= ALT;
|
2008-02-10 16:33:14 +00:00
|
|
|
|
2010-02-28 20:03:00 +00: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 20:03:00 +00:00
|
|
|
invalidate = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-10 16:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 20:03:00 +00:00
|
|
|
if (kbd_layout) {
|
|
|
|
keysym = -1;
|
|
|
|
if (chr < CURSES_KEYS)
|
|
|
|
keysym = curses2keysym[chr];
|
|
|
|
|
|
|
|
if (keysym == -1) {
|
2010-10-19 17:48:20 +00:00
|
|
|
if (chr < ' ') {
|
|
|
|
keysym = chr + '@';
|
|
|
|
if (keysym >= 'A' && keysym <= 'Z')
|
|
|
|
keysym += 'a' - 'A';
|
|
|
|
keysym |= KEYSYM_CNTRL;
|
|
|
|
} else
|
2010-02-28 20:03:00 +00:00
|
|
|
keysym = chr;
|
|
|
|
}
|
2008-02-10 16:33:14 +00:00
|
|
|
|
2010-02-28 20:03:00 +00:00
|
|
|
keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
|
|
|
|
if (keycode == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
keycode |= (keysym & ~KEYSYM_MASK) >> 16;
|
|
|
|
keycode |= keycode_alt;
|
2008-02-10 16:33:14 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 20:03:00 +00:00
|
|
|
if (keycode == -1)
|
|
|
|
continue;
|
|
|
|
|
2008-02-10 16:33:14 +00:00
|
|
|
if (is_graphic_console()) {
|
|
|
|
/* since terminals don't know about key press and release
|
|
|
|
* events, we need to emit both for each key received */
|
|
|
|
if (keycode & SHIFT)
|
|
|
|
kbd_put_keycode(SHIFT_CODE);
|
|
|
|
if (keycode & CNTRL)
|
|
|
|
kbd_put_keycode(CNTRL_CODE);
|
|
|
|
if (keycode & ALT)
|
|
|
|
kbd_put_keycode(ALT_CODE);
|
2010-02-28 20:03:00 +00:00
|
|
|
if (keycode & ALTGR) {
|
|
|
|
kbd_put_keycode(SCANCODE_EMUL0);
|
|
|
|
kbd_put_keycode(ALT_CODE);
|
|
|
|
}
|
2008-02-10 16:33:14 +00:00
|
|
|
if (keycode & GREY)
|
|
|
|
kbd_put_keycode(GREY_CODE);
|
|
|
|
kbd_put_keycode(keycode & KEY_MASK);
|
|
|
|
if (keycode & GREY)
|
|
|
|
kbd_put_keycode(GREY_CODE);
|
|
|
|
kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
|
2010-02-28 20:03:00 +00:00
|
|
|
if (keycode & ALTGR) {
|
|
|
|
kbd_put_keycode(SCANCODE_EMUL0);
|
|
|
|
kbd_put_keycode(ALT_CODE | KEY_RELEASE);
|
|
|
|
}
|
2008-02-10 16:33:14 +00:00
|
|
|
if (keycode & ALT)
|
|
|
|
kbd_put_keycode(ALT_CODE | KEY_RELEASE);
|
|
|
|
if (keycode & CNTRL)
|
|
|
|
kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
|
|
|
|
if (keycode & SHIFT)
|
|
|
|
kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
|
|
|
|
} else {
|
2010-02-28 20:03:00 +00:00
|
|
|
keysym = curses2qemu[chr];
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void curses_setup(void)
|
|
|
|
{
|
|
|
|
int i, colour_default[8] = {
|
|
|
|
COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
|
|
|
|
COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
for (i = 0; i < 64; i ++)
|
|
|
|
init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void curses_keyboard_setup(void)
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
/* always use generic keymaps */
|
|
|
|
if (!keyboard_layout)
|
|
|
|
keyboard_layout = "en-us";
|
|
|
|
#endif
|
|
|
|
if(keyboard_layout) {
|
2009-03-06 20:27:10 +00:00
|
|
|
kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
|
2008-02-10 16:33:14 +00:00
|
|
|
if (!kbd_layout)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void curses_display_init(DisplayState *ds, int full_screen)
|
|
|
|
{
|
2009-01-15 22:14:11 +00:00
|
|
|
DisplayChangeListener *dcl;
|
2008-02-10 16:33:14 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (!isatty(1)) {
|
|
|
|
fprintf(stderr, "We need a terminal output\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
curses_setup();
|
|
|
|
curses_keyboard_setup();
|
2010-03-21 19:13:02 +00:00
|
|
|
atexit(curses_atexit);
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2008-02-22 18:21:28 +00:00
|
|
|
#if defined(SIGWINCH) && defined(KEY_RESIZE)
|
2008-02-10 16:33:14 +00:00
|
|
|
/* some curses implementations provide a handler, but we
|
|
|
|
* want to be sure this is handled regardless of the library */
|
|
|
|
signal(SIGWINCH, curses_winch_handler);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
|
2012-09-28 13:02:08 +00:00
|
|
|
dcl->dpy_text_update = curses_update;
|
|
|
|
dcl->dpy_text_resize = curses_resize;
|
2009-01-15 22:14:11 +00:00
|
|
|
dcl->dpy_refresh = curses_refresh;
|
|
|
|
dcl->dpy_text_cursor = curses_cursor_position;
|
|
|
|
register_displaychangelistener(ds, dcl);
|
2008-02-10 16:33:14 +00:00
|
|
|
|
|
|
|
invalidate = 1;
|
|
|
|
}
|