Commit 4d3b6f6e authored by balrog's avatar balrog

Add an ncurses UI.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3976 c046a42c-6fe2-441c-8c8c-71466251a162
parent c0be16d3
......@@ -99,6 +99,9 @@ OBJS+=$(addprefix audio/, $(AUDIO_OBJS))
ifdef CONFIG_SDL
OBJS+=sdl.o x_keymap.o
endif
ifdef CONFIG_CURSES
OBJS+=curses.o
endif
OBJS+=vnc.o d3des.o
ifdef CONFIG_COCOA
......@@ -122,6 +125,9 @@ sdl.o: sdl.c keymaps.c sdl_keysym.h
vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h d3des.c d3des.h
$(CC) $(CFLAGS) $(CPPFLAGS) $(CONFIG_VNC_TLS_CFLAGS) -c -o $@ $<
curses.o: curses.c keymaps.c curses_keys.h
$(CC) $(CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) -c -o $@ $<
audio/sdlaudio.o: audio/sdlaudio.c
$(CC) $(CFLAGS) $(CPPFLAGS) $(SDL_CFLAGS) -c -o $@ $<
......
......@@ -647,7 +647,7 @@ main.o: CFLAGS+=-p
endif
$(QEMU_PROG): $(OBJS) ../libqemu_common.a libqemu.a
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) $(SDL_LIBS) $(COCOA_LIBS)
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) $(SDL_LIBS) $(COCOA_LIBS) $(CURSES_LIBS)
endif # !CONFIG_USER_ONLY
......
......@@ -108,6 +108,7 @@ linux_user="no"
darwin_user="no"
build_docs="no"
uname_release=""
curses="yes"
# OS specific
targetos=`uname -s`
......@@ -323,6 +324,8 @@ for opt do
;;
--disable-werror) werror="no"
;;
--disable-curses) curses="no"
;;
*) echo "ERROR: unknown option $opt"; show_help="yes"
;;
esac
......@@ -669,6 +672,20 @@ EOF
fi
fi
##########################################
# curses probe
if test "$curses" = "yes" ; then
curses=no
cat > $TMPC << EOF
#include <curses.h>
int main(void) { return curses_version(); }
EOF
if $cc -o $TMPE $TMPC -lcurses 2> /dev/null ; then
curses=yes
fi
fi # test "$curses"
# Check if tools are available to build documentation.
if [ -x "`which texi2html 2>/dev/null`" ] && \
[ -x "`which pod2man 2>/dev/null`" ]; then
......@@ -720,6 +737,7 @@ echo "SDL support $sdl"
if test "$sdl" != "no" ; then
echo "SDL static link $sdl_static"
fi
echo "curses support $curses"
echo "mingw32 support $mingw32"
echo "Adlib support $adlib"
echo "AC97 support $ac97"
......@@ -974,8 +992,13 @@ if test "$sdl1" = "yes" ; then
fi
fi
if test "$cocoa" = "yes" ; then
echo "#define CONFIG_COCOA 1" >> $config_h
echo "CONFIG_COCOA=yes" >> $config_mak
echo "#define CONFIG_COCOA 1" >> $config_h
echo "CONFIG_COCOA=yes" >> $config_mak
fi
if test "$curses" = "yes" ; then
echo "#define CONFIG_CURSES 1" >> $config_h
echo "CONFIG_CURSES=yes" >> $config_mak
echo "CURSES_LIBS=-lcurses" >> $config_mak
fi
# XXX: suppress that
......@@ -1040,7 +1063,8 @@ if test "$target_user_only" = "no" -a "$check_gfx" = "yes" \
-a "$sdl" = "no" -a "$cocoa" = "no" ; then
echo "ERROR: QEMU requires SDL or Cocoa for graphical output"
echo "To build QEMU without graphical output configure with --disable-gfx-check"
echo "Note that this will disable all output from the virtual graphics card."
echo "Note that this will disable all output from the virtual graphics card"
echo "except through VNC or curses."
exit 1;
fi
......
......@@ -121,6 +121,7 @@ struct TextConsole {
vga_hw_update_ptr hw_update;
vga_hw_invalidate_ptr hw_invalidate;
vga_hw_screen_dump_ptr hw_screen_dump;
vga_hw_text_update_ptr hw_text_update;
void *hw;
int g_width, g_height;
......@@ -135,6 +136,7 @@ struct TextConsole {
TextAttributes t_attrib_default; /* default text attributes */
TextAttributes t_attrib; /* currently active text attributes */
TextCell *cells;
int text_x[2], text_y[2], cursor_invalidate;
enum TTYState state;
int esc_params[MAX_ESC_PARAMS];
......@@ -171,6 +173,12 @@ void vga_hw_screen_dump(const char *filename)
consoles[0]->hw_screen_dump(consoles[0]->hw, filename);
}
void vga_hw_text_update(console_ch_t *chardata)
{
if (active_console && active_console->hw_text_update)
active_console->hw_text_update(active_console->hw, chardata);
}
/* convert a RGBA color to a color index usable in graphic primitives */
static unsigned int vga_get_color(DisplayState *ds, unsigned int rgba)
{
......@@ -515,12 +523,25 @@ static void text_console_resize(TextConsole *s)
s->cells = cells;
}
static inline void text_update_xy(TextConsole *s, int x, int y)
{
s->text_x[0] = MIN(s->text_x[0], x);
s->text_x[1] = MAX(s->text_x[1], x);
s->text_y[0] = MIN(s->text_y[0], y);
s->text_y[1] = MAX(s->text_y[1], y);
}
static void update_xy(TextConsole *s, int x, int y)
{
TextCell *c;
int y1, y2;
if (s == active_console) {
if (!s->ds->depth) {
text_update_xy(s, x, y);
return;
}
y1 = (s->y_base + y) % s->total_height;
y2 = y1 - s->y_displayed;
if (y2 < 0)
......@@ -542,6 +563,12 @@ static void console_show_cursor(TextConsole *s, int show)
if (s == active_console) {
int x = s->x;
if (!s->ds->depth) {
s->cursor_invalidate = 1;
return;
}
if (x >= s->width) {
x = s->width - 1;
}
......@@ -571,6 +598,14 @@ static void console_refresh(TextConsole *s)
if (s != active_console)
return;
if (!s->ds->depth) {
s->text_x[0] = 0;
s->text_y[0] = 0;
s->text_x[1] = s->width - 1;
s->text_y[1] = s->height - 1;
s->cursor_invalidate = 1;
return;
}
vga_fill_rect(s->ds, 0, 0, s->ds->width, s->ds->height,
color_table[0][COLOR_BLACK]);
......@@ -648,6 +683,14 @@ static void console_put_lf(TextConsole *s)
c++;
}
if (s == active_console && s->y_displayed == s->y_base) {
if (!s->ds->depth) {
s->text_x[0] = 0;
s->text_y[0] = 0;
s->text_x[1] = s->width - 1;
s->text_y[1] = s->height - 1;
return;
}
vga_bitblt(s->ds, 0, FONT_HEIGHT, 0, 0,
s->width * FONT_WIDTH,
(s->height - 1) * FONT_HEIGHT);
......@@ -998,21 +1041,7 @@ void console_select(unsigned int index)
s = consoles[index];
if (s) {
active_console = s;
if (s->console_type != GRAPHIC_CONSOLE) {
if (s->g_width != s->ds->width ||
s->g_height != s->ds->height) {
if (s->console_type == TEXT_CONSOLE_FIXED_SIZE) {
dpy_resize(s->ds, s->g_width, s->g_height);
} else {
s->g_width = s->ds->width;
s->g_height = s->ds->height;
text_console_resize(s);
}
}
console_refresh(s);
} else {
vga_hw_invalidate();
}
vga_hw_invalidate();
}
}
......@@ -1116,6 +1145,52 @@ void kbd_put_keysym(int keysym)
}
}
static void text_console_invalidate(void *opaque)
{
TextConsole *s = (TextConsole *) opaque;
if (s->console_type != GRAPHIC_CONSOLE) {
if (s->g_width != s->ds->width ||
s->g_height != s->ds->height) {
if (s->console_type == TEXT_CONSOLE_FIXED_SIZE)
dpy_resize(s->ds, s->g_width, s->g_height);
else {
s->g_width = s->ds->width;
s->g_height = s->ds->height;
text_console_resize(s);
}
}
}
console_refresh(s);
}
static void text_console_update(void *opaque, console_ch_t *chardata)
{
TextConsole *s = (TextConsole *) opaque;
int i, j, src;
if (s->text_x[0] <= s->text_x[1]) {
src = (s->y_base + s->text_y[0]) * s->width;
chardata += s->text_y[0] * s->width;
for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
for (j = 0; j < s->width; j ++, src ++)
console_write_ch(chardata ++, s->cells[src].ch |
(s->cells[src].t_attrib.fgcol << 12) |
(s->cells[src].t_attrib.bgcol << 8) |
(s->cells[src].t_attrib.bold << 21));
dpy_update(s->ds, s->text_x[0], s->text_y[0],
s->text_x[1] - s->text_x[0], i - s->text_y[0]);
s->text_x[0] = s->width;
s->text_y[0] = s->height;
s->text_x[1] = 0;
s->text_y[1] = 0;
}
if (s->cursor_invalidate) {
dpy_cursor(s->ds, s->x, s->y);
s->cursor_invalidate = 0;
}
}
static TextConsole *new_console(DisplayState *ds, console_type_t console_type)
{
TextConsole *s;
......@@ -1150,6 +1225,7 @@ static TextConsole *new_console(DisplayState *ds, console_type_t console_type)
TextConsole *graphic_console_init(DisplayState *ds, vga_hw_update_ptr update,
vga_hw_invalidate_ptr invalidate,
vga_hw_screen_dump_ptr screen_dump,
vga_hw_text_update_ptr text_update,
void *opaque)
{
TextConsole *s;
......@@ -1160,13 +1236,14 @@ TextConsole *graphic_console_init(DisplayState *ds, vga_hw_update_ptr update,
s->hw_update = update;
s->hw_invalidate = invalidate;
s->hw_screen_dump = screen_dump;
s->hw_text_update = text_update;
s->hw = opaque;
return s;
}
int is_graphic_console(void)
{
return active_console->console_type == GRAPHIC_CONSOLE;
return active_console && active_console->console_type == GRAPHIC_CONSOLE;
}
void console_color_init(DisplayState *ds)
......@@ -1234,6 +1311,10 @@ CharDriverState *text_console_init(DisplayState *ds, const char *p)
s->g_width = width;
s->g_height = height;
s->hw_invalidate = text_console_invalidate;
s->hw_text_update = text_console_update;
s->hw = s;
/* Set text attribute defaults */
s->t_attrib_default.bold = 0;
s->t_attrib_default.uline = 0;
......
......@@ -79,6 +79,7 @@ struct DisplayState {
int dst_x, int dst_y, int w, int h);
void (*dpy_fill)(struct DisplayState *s, int x, int y,
int w, int h, uint32_t c);
void (*dpy_text_cursor)(struct DisplayState *s, int x, int y);
void (*mouse_set)(int x, int y, int on);
void (*cursor_define)(int width, int height, int bpp, int hot_x, int hot_y,
uint8_t *image, uint8_t *mask);
......@@ -94,17 +95,32 @@ static inline void dpy_resize(DisplayState *s, int w, int h)
s->dpy_resize(s, w, h);
}
static inline void dpy_cursor(DisplayState *s, int x, int y)
{
if (s->dpy_text_cursor)
s->dpy_text_cursor(s, x, y);
}
typedef unsigned long console_ch_t;
static inline void console_write_ch(console_ch_t *dest, uint32_t ch)
{
cpu_to_le32wu((uint32_t *) dest, ch);
}
typedef void (*vga_hw_update_ptr)(void *);
typedef void (*vga_hw_invalidate_ptr)(void *);
typedef void (*vga_hw_screen_dump_ptr)(void *, const char *);
typedef void (*vga_hw_text_update_ptr)(void *, console_ch_t *);
TextConsole *graphic_console_init(DisplayState *ds, vga_hw_update_ptr update,
vga_hw_invalidate_ptr invalidate,
vga_hw_screen_dump_ptr screen_dump,
vga_hw_text_update_ptr text_update,
void *opaque);
void vga_hw_update(void);
void vga_hw_invalidate(void);
void vga_hw_screen_dump(const char *filename);
void vga_hw_text_update(console_ch_t *chardata);
int is_graphic_console(void);
CharDriverState *text_console_init(DisplayState *ds, const char *p);
......@@ -124,6 +140,9 @@ int vnc_display_open(DisplayState *ds, const char *display);
int vnc_display_password(DisplayState *ds, const char *password);
void do_info_vnc(void);
/* curses.c */
void curses_display_init(DisplayState *ds, int full_screen);
/* x_keymap.c */
extern uint8_t _translate_keycode(const int key);
......
/*
* 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 "qemu-common.h"
#include "console.h"
#include "sysemu.h"
#include <curses.h>
#ifndef _WIN32
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
#endif
#define FONT_HEIGHT 16
#define FONT_WIDTH 8
static console_ch_t screen[160 * 100];
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)
{
if (is_graphic_console()) {
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;
}
}
static void curses_resize(DisplayState *ds, int w, int h)
{
if (w == gwidth && h == gheight)
return;
gwidth = w;
gheight = h;
curses_calc_pad();
}
#ifndef _WIN32
#ifdef SIGWINCH
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"
#include "keymaps.c"
static kbd_layout_t *kbd_layout = 0;
static int keycode2keysym[CURSES_KEYS];
static void curses_refresh(DisplayState *ds)
{
int chr, nextchr, keysym, keycode;
if (invalidate) {
clear();
refresh();
curses_calc_pad();
ds->width = FONT_WIDTH * width;
ds->height = FONT_HEIGHT * height;
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;
/* 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);
ds->width = FONT_WIDTH * width;
ds->height = FONT_HEIGHT * height;
continue;
}
keycode = curses2keycode[chr];
if (keycode == -1)
continue;
/* alt key */
if (keycode == 1) {
nextchr = getch();
if (nextchr != ERR) {
keycode = curses2keycode[nextchr];
nextchr = ERR;
if (keycode == -1)
continue;
keycode |= ALT;
/* process keys reserved for qemu */
if (keycode >= QEMU_KEY_CONSOLE0 &&
keycode < QEMU_KEY_CONSOLE0 + 9) {
erase();
wnoutrefresh(stdscr);
console_select(keycode - QEMU_KEY_CONSOLE0);
invalidate = 1;
continue;
}
}
}
if (kbd_layout && !(keycode & GREY)) {
keysym = keycode2keysym[keycode & KEY_MASK];
if (keysym == -1)
keysym = chr;
keycode &= ~KEY_MASK;
keycode |= keysym2scancode(kbd_layout, keysym);
}
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);
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);
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 {
keysym = curses2keysym[chr];
if (keysym == -1)
keysym = chr;
kbd_put_keysym(keysym);
}
}
}
static void curses_cleanup(void *opaque)
{
endwin();
}
static void curses_atexit(void)
{