curses.c 12.0 KB
Newer Older
B
balrog 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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

31
#include "qemu-common.h"
32
#include "ui/console.h"
33
#include "ui/input.h"
34
#include "sysemu/sysemu.h"
35

B
balrog 已提交
36 37 38
#define FONT_HEIGHT 16
#define FONT_WIDTH 8

39
static DisplayChangeListener *dcl;
A
Anthony Liguori 已提交
40
static console_ch_t screen[160 * 100];
B
balrog 已提交
41 42 43 44
static WINDOW *screenpad = NULL;
static int width, height, gwidth, gheight, invalidate;
static int px, py, sminx, sminy, smaxx, smaxy;

45 46
chtype vga_to_curses[256];

47 48
static void curses_update(DisplayChangeListener *dcl,
                          int x, int y, int w, int h)
B
balrog 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
{
    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)
{
G
Gerd Hoffmann 已提交
62
    if (qemu_console_is_fixedsize(NULL)) {
B
balrog 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        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;
    }
}

99 100
static void curses_resize(DisplayChangeListener *dcl,
                          int width, int height)
B
balrog 已提交
101
{
102
    if (width == gwidth && height == gheight) {
B
balrog 已提交
103
        return;
104
    }
B
balrog 已提交
105

106 107
    gwidth = width;
    gheight = height;
B
balrog 已提交
108 109 110 111

    curses_calc_pad();
}

112 113 114
#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
static volatile sig_atomic_t got_sigwinch;
static void curses_winch_check(void)
B
balrog 已提交
115 116 117 118 119 120 121 122
{
    struct winsize {
        unsigned short ws_row;
        unsigned short ws_col;
        unsigned short ws_xpixel;   /* unused */
        unsigned short ws_ypixel;   /* unused */
    } ws;

123 124 125 126 127 128
    if (!got_sigwinch) {
        return;
    }
    got_sigwinch = false;

    if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
B
balrog 已提交
129
        return;
130
    }
B
balrog 已提交
131 132 133

    resize_term(ws.ws_row, ws.ws_col);
    invalidate = 1;
134
}
B
balrog 已提交
135

136 137 138
static void curses_winch_handler(int signum)
{
    got_sigwinch = true;
B
balrog 已提交
139
}
140 141 142 143 144 145 146 147 148 149 150

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) {}
B
balrog 已提交
151 152
#endif

153 154
static void curses_cursor_position(DisplayChangeListener *dcl,
                                   int x, int y)
B
balrog 已提交
155 156 157 158 159 160 161 162 163 164
{
    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 */
G
Gerd Hoffmann 已提交
165
            if (!qemu_console_is_graphic(NULL)) {
B
balrog 已提交
166
                curs_set(2);
G
Gerd Hoffmann 已提交
167
            }
B
balrog 已提交
168 169 170 171 172 173 174 175 176 177 178
            return;
        }
    }

    curs_set(0);
}

/* generic keyboard conversion */

#include "curses_keys.h"

A
Anthony Liguori 已提交
179
static kbd_layout_t *kbd_layout = NULL;
B
balrog 已提交
180

181
static void curses_refresh(DisplayChangeListener *dcl)
B
balrog 已提交
182
{
183
    int chr, nextchr, keysym, keycode, keycode_alt;
B
balrog 已提交
184

185 186
    curses_winch_check();

B
balrog 已提交
187 188 189 190
    if (invalidate) {
        clear();
        refresh();
        curses_calc_pad();
191
        graphic_hw_invalidate(NULL);
B
balrog 已提交
192 193 194
        invalidate = 0;
    }

195
    graphic_hw_text_update(NULL, screen);
B
balrog 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209

    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;

210
#ifdef KEY_RESIZE
B
balrog 已提交
211 212 213 214 215
        /* this shouldn't occur when we use a custom SIGWINCH handler */
        if (chr == KEY_RESIZE) {
            clear();
            refresh();
            curses_calc_pad();
216
            curses_update(dcl, 0, 0, width, height);
B
balrog 已提交
217 218
            continue;
        }
219
#endif
B
balrog 已提交
220 221

        keycode = curses2keycode[chr];
222
        keycode_alt = 0;
B
balrog 已提交
223 224 225 226 227 228

        /* alt key */
        if (keycode == 1) {
            nextchr = getch();

            if (nextchr != ERR) {
229 230
                chr = nextchr;
                keycode_alt = ALT;
B
balrog 已提交
231 232 233
                keycode = curses2keycode[nextchr];
                nextchr = ERR;

234 235
                if (keycode != -1) {
                    keycode |= ALT;
B
balrog 已提交
236

237 238 239 240 241 242
                    /* process keys reserved for qemu */
                    if (keycode >= QEMU_KEY_CONSOLE0 &&
                            keycode < QEMU_KEY_CONSOLE0 + 9) {
                        erase();
                        wnoutrefresh(stdscr);
                        console_select(keycode - QEMU_KEY_CONSOLE0);
B
balrog 已提交
243

244 245 246
                        invalidate = 1;
                        continue;
                    }
B
balrog 已提交
247 248 249 250
                }
            }
        }

251 252 253 254 255 256
        if (kbd_layout) {
            keysym = -1;
            if (chr < CURSES_KEYS)
                keysym = curses2keysym[chr];

            if (keysym == -1) {
257 258 259 260 261 262
                if (chr < ' ') {
                    keysym = chr + '@';
                    if (keysym >= 'A' && keysym <= 'Z')
                        keysym += 'a' - 'A';
                    keysym |= KEYSYM_CNTRL;
                } else
263 264
                    keysym = chr;
            }
B
balrog 已提交
265

266 267 268 269 270 271
            keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
            if (keycode == 0)
                continue;

            keycode |= (keysym & ~KEYSYM_MASK) >> 16;
            keycode |= keycode_alt;
B
balrog 已提交
272 273
        }

274 275 276
        if (keycode == -1)
            continue;

G
Gerd Hoffmann 已提交
277
        if (qemu_console_is_graphic(NULL)) {
B
balrog 已提交
278 279
            /* since terminals don't know about key press and release
             * events, we need to emit both for each key received */
280 281
            if (keycode & SHIFT) {
                qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
282
                qemu_input_event_send_key_delay(0);
283 284 285
            }
            if (keycode & CNTRL) {
                qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
286
                qemu_input_event_send_key_delay(0);
287 288 289
            }
            if (keycode & ALT) {
                qemu_input_event_send_key_number(NULL, ALT_CODE, true);
290
                qemu_input_event_send_key_delay(0);
291
            }
292
            if (keycode & ALTGR) {
293
                qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
294
                qemu_input_event_send_key_delay(0);
295
            }
296

297
            qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
298
            qemu_input_event_send_key_delay(0);
299
            qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
300
            qemu_input_event_send_key_delay(0);
301

302
            if (keycode & ALTGR) {
303
                qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
304
                qemu_input_event_send_key_delay(0);
305 306 307
            }
            if (keycode & ALT) {
                qemu_input_event_send_key_number(NULL, ALT_CODE, false);
308
                qemu_input_event_send_key_delay(0);
309 310 311
            }
            if (keycode & CNTRL) {
                qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
312
                qemu_input_event_send_key_delay(0);
313 314 315
            }
            if (keycode & SHIFT) {
                qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
316
                qemu_input_event_send_key_delay(0);
317
            }
B
balrog 已提交
318
        } else {
319
            keysym = curses2qemu[chr];
B
balrog 已提交
320 321 322 323 324 325 326 327
            if (keysym == -1)
                keysym = chr;

            kbd_put_keysym(keysym);
        }
    }
}

B
Blue Swirl 已提交
328
static void curses_atexit(void)
B
balrog 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
    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);

346
    for (i = 0; i < 64; i++) {
B
balrog 已提交
347
        init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
348 349 350 351 352
    }
    /* Set default color for more than 64. (monitor uses 0x74xx for example) */
    for (i = 64; i < COLOR_PAIRS; i++) {
        init_pair(i, COLOR_WHITE, COLOR_BLACK);
    }
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

    /*
     * Setup mapping for vga to curses line graphics.
     * FIXME: for better font, have to use ncursesw and setlocale()
     */
#if 0
    /* FIXME: map from where? */
    ACS_S1;
    ACS_S3;
    ACS_S7;
    ACS_S9;
#endif
    /* ACS_* is not constant. So, we can't initialize statically. */
    vga_to_curses['\0'] = ' ';
    vga_to_curses[0x04] = ACS_DIAMOND;
    vga_to_curses[0x0a] = ACS_RARROW;
    vga_to_curses[0x0b] = ACS_LARROW;
    vga_to_curses[0x18] = ACS_UARROW;
    vga_to_curses[0x19] = ACS_DARROW;
    vga_to_curses[0x9c] = ACS_STERLING;
    vga_to_curses[0xb0] = ACS_BOARD;
    vga_to_curses[0xb1] = ACS_CKBOARD;
    vga_to_curses[0xb3] = ACS_VLINE;
    vga_to_curses[0xb4] = ACS_RTEE;
    vga_to_curses[0xbf] = ACS_URCORNER;
    vga_to_curses[0xc0] = ACS_LLCORNER;
    vga_to_curses[0xc1] = ACS_BTEE;
    vga_to_curses[0xc2] = ACS_TTEE;
    vga_to_curses[0xc3] = ACS_LTEE;
    vga_to_curses[0xc4] = ACS_HLINE;
    vga_to_curses[0xc5] = ACS_PLUS;
    vga_to_curses[0xce] = ACS_LANTERN;
    vga_to_curses[0xd8] = ACS_NEQUAL;
    vga_to_curses[0xd9] = ACS_LRCORNER;
    vga_to_curses[0xda] = ACS_ULCORNER;
    vga_to_curses[0xdb] = ACS_BLOCK;
    vga_to_curses[0xe3] = ACS_PI;
    vga_to_curses[0xf1] = ACS_PLMINUS;
    vga_to_curses[0xf2] = ACS_GEQUAL;
    vga_to_curses[0xf3] = ACS_LEQUAL;
    vga_to_curses[0xf8] = ACS_DEGREE;
    vga_to_curses[0xfe] = ACS_BULLET;
B
balrog 已提交
395 396 397 398 399 400 401 402 403 404
}

static void curses_keyboard_setup(void)
{
#if defined(__APPLE__)
    /* always use generic keymaps */
    if (!keyboard_layout)
        keyboard_layout = "en-us";
#endif
    if(keyboard_layout) {
405
        kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
B
balrog 已提交
406 407 408 409 410
        if (!kbd_layout)
            exit(1);
    }
}

411 412 413 414 415 416 417 418
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,
};

B
balrog 已提交
419 420 421 422 423 424 425 426 427 428 429
void curses_display_init(DisplayState *ds, int full_screen)
{
#ifndef _WIN32
    if (!isatty(1)) {
        fprintf(stderr, "We need a terminal output\n");
        exit(1);
    }
#endif

    curses_setup();
    curses_keyboard_setup();
430
    atexit(curses_atexit);
B
balrog 已提交
431

432
    curses_winch_init();
B
balrog 已提交
433

434
    dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
435
    dcl->ops = &dcl_ops;
436
    register_displaychangelistener(dcl);
B
balrog 已提交
437 438 439

    invalidate = 1;
}