Statistics
| Branch: | Revision:

root / ui / curses.c @ 7fee199c

History | View | Annotate | Download (9.9 kB)

1 4d3b6f6e balrog
/*
2 4d3b6f6e balrog
 * QEMU curses/ncurses display driver
3 4d3b6f6e balrog
 * 
4 4d3b6f6e balrog
 * Copyright (c) 2005 Andrzej Zaborowski  <balrog@zabor.org>
5 4d3b6f6e balrog
 * 
6 4d3b6f6e balrog
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 4d3b6f6e balrog
 * of this software and associated documentation files (the "Software"), to deal
8 4d3b6f6e balrog
 * in the Software without restriction, including without limitation the rights
9 4d3b6f6e balrog
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 4d3b6f6e balrog
 * copies of the Software, and to permit persons to whom the Software is
11 4d3b6f6e balrog
 * furnished to do so, subject to the following conditions:
12 4d3b6f6e balrog
 *
13 4d3b6f6e balrog
 * The above copyright notice and this permission notice shall be included in
14 4d3b6f6e balrog
 * all copies or substantial portions of the Software.
15 4d3b6f6e balrog
 *
16 4d3b6f6e balrog
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 4d3b6f6e balrog
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 4d3b6f6e balrog
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 4d3b6f6e balrog
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 4d3b6f6e balrog
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 4d3b6f6e balrog
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 4d3b6f6e balrog
 * THE SOFTWARE.
23 4d3b6f6e balrog
 */
24 4d3b6f6e balrog
#include <curses.h>
25 4d3b6f6e balrog
26 4d3b6f6e balrog
#ifndef _WIN32
27 4d3b6f6e balrog
#include <sys/ioctl.h>
28 4d3b6f6e balrog
#include <termios.h>
29 4d3b6f6e balrog
#endif
30 4d3b6f6e balrog
31 128ab2ff blueswir1
#ifdef __OpenBSD__
32 128ab2ff blueswir1
#define resize_term resizeterm
33 128ab2ff blueswir1
#endif
34 128ab2ff blueswir1
35 511d2b14 blueswir1
#include "qemu-common.h"
36 511d2b14 blueswir1
#include "console.h"
37 511d2b14 blueswir1
#include "sysemu.h"
38 511d2b14 blueswir1
39 4d3b6f6e balrog
#define FONT_HEIGHT 16
40 4d3b6f6e balrog
#define FONT_WIDTH 8
41 4d3b6f6e balrog
42 c227f099 Anthony Liguori
static console_ch_t screen[160 * 100];
43 4d3b6f6e balrog
static WINDOW *screenpad = NULL;
44 4d3b6f6e balrog
static int width, height, gwidth, gheight, invalidate;
45 4d3b6f6e balrog
static int px, py, sminx, sminy, smaxx, smaxy;
46 4d3b6f6e balrog
47 4d3b6f6e balrog
static void curses_update(DisplayState *ds, int x, int y, int w, int h)
48 4d3b6f6e balrog
{
49 4d3b6f6e balrog
    chtype *line;
50 4d3b6f6e balrog
51 4d3b6f6e balrog
    line = ((chtype *) screen) + y * width;
52 4d3b6f6e balrog
    for (h += y; y < h; y ++, line += width)
53 4d3b6f6e balrog
        mvwaddchnstr(screenpad, y, 0, line, width);
54 4d3b6f6e balrog
55 4d3b6f6e balrog
    pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
56 4d3b6f6e balrog
    refresh();
57 4d3b6f6e balrog
}
58 4d3b6f6e balrog
59 4d3b6f6e balrog
static void curses_calc_pad(void)
60 4d3b6f6e balrog
{
61 c21bbcfa balrog
    if (is_fixedsize_console()) {
62 4d3b6f6e balrog
        width = gwidth;
63 4d3b6f6e balrog
        height = gheight;
64 4d3b6f6e balrog
    } else {
65 4d3b6f6e balrog
        width = COLS;
66 4d3b6f6e balrog
        height = LINES;
67 4d3b6f6e balrog
    }
68 4d3b6f6e balrog
69 4d3b6f6e balrog
    if (screenpad)
70 4d3b6f6e balrog
        delwin(screenpad);
71 4d3b6f6e balrog
72 4d3b6f6e balrog
    clear();
73 4d3b6f6e balrog
    refresh();
74 4d3b6f6e balrog
75 4d3b6f6e balrog
    screenpad = newpad(height, width);
76 4d3b6f6e balrog
77 4d3b6f6e balrog
    if (width > COLS) {
78 4d3b6f6e balrog
        px = (width - COLS) / 2;
79 4d3b6f6e balrog
        sminx = 0;
80 4d3b6f6e balrog
        smaxx = COLS;
81 4d3b6f6e balrog
    } else {
82 4d3b6f6e balrog
        px = 0;
83 4d3b6f6e balrog
        sminx = (COLS - width) / 2;
84 4d3b6f6e balrog
        smaxx = sminx + width;
85 4d3b6f6e balrog
    }
86 4d3b6f6e balrog
87 4d3b6f6e balrog
    if (height > LINES) {
88 4d3b6f6e balrog
        py = (height - LINES) / 2;
89 4d3b6f6e balrog
        sminy = 0;
90 4d3b6f6e balrog
        smaxy = LINES;
91 4d3b6f6e balrog
    } else {
92 4d3b6f6e balrog
        py = 0;
93 4d3b6f6e balrog
        sminy = (LINES - height) / 2;
94 4d3b6f6e balrog
        smaxy = sminy + height;
95 4d3b6f6e balrog
    }
96 4d3b6f6e balrog
}
97 4d3b6f6e balrog
98 7d957bd8 aliguori
static void curses_resize(DisplayState *ds)
99 4d3b6f6e balrog
{
100 7d957bd8 aliguori
    if (ds_get_width(ds) == gwidth && ds_get_height(ds) == gheight)
101 4d3b6f6e balrog
        return;
102 4d3b6f6e balrog
103 7d957bd8 aliguori
    gwidth = ds_get_width(ds);
104 7d957bd8 aliguori
    gheight = ds_get_height(ds);
105 4d3b6f6e balrog
106 4d3b6f6e balrog
    curses_calc_pad();
107 68f00996 aliguori
    ds->surface->width = width * FONT_WIDTH;
108 68f00996 aliguori
    ds->surface->height = height * FONT_HEIGHT;
109 4d3b6f6e balrog
}
110 4d3b6f6e balrog
111 4d3b6f6e balrog
#ifndef _WIN32
112 b1314cf9 balrog
#if defined(SIGWINCH) && defined(KEY_RESIZE)
113 4d3b6f6e balrog
static void curses_winch_handler(int signum)
114 4d3b6f6e balrog
{
115 4d3b6f6e balrog
    struct winsize {
116 4d3b6f6e balrog
        unsigned short ws_row;
117 4d3b6f6e balrog
        unsigned short ws_col;
118 4d3b6f6e balrog
        unsigned short ws_xpixel;   /* unused */
119 4d3b6f6e balrog
        unsigned short ws_ypixel;   /* unused */
120 4d3b6f6e balrog
    } ws;
121 4d3b6f6e balrog
122 4d3b6f6e balrog
    /* terminal size changed */
123 4d3b6f6e balrog
    if (ioctl(1, TIOCGWINSZ, &ws) == -1)
124 4d3b6f6e balrog
        return;
125 4d3b6f6e balrog
126 4d3b6f6e balrog
    resize_term(ws.ws_row, ws.ws_col);
127 4d3b6f6e balrog
    curses_calc_pad();
128 4d3b6f6e balrog
    invalidate = 1;
129 4d3b6f6e balrog
130 4d3b6f6e balrog
    /* some systems require this */
131 4d3b6f6e balrog
    signal(SIGWINCH, curses_winch_handler);
132 4d3b6f6e balrog
}
133 4d3b6f6e balrog
#endif
134 4d3b6f6e balrog
#endif
135 4d3b6f6e balrog
136 4d3b6f6e balrog
static void curses_cursor_position(DisplayState *ds, int x, int y)
137 4d3b6f6e balrog
{
138 4d3b6f6e balrog
    if (x >= 0) {
139 4d3b6f6e balrog
        x = sminx + x - px;
140 4d3b6f6e balrog
        y = sminy + y - py;
141 4d3b6f6e balrog
142 4d3b6f6e balrog
        if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
143 4d3b6f6e balrog
            move(y, x);
144 4d3b6f6e balrog
            curs_set(1);
145 4d3b6f6e balrog
            /* it seems that curs_set(1) must always be called before
146 4d3b6f6e balrog
             * curs_set(2) for the latter to have effect */
147 4d3b6f6e balrog
            if (!is_graphic_console())
148 4d3b6f6e balrog
                curs_set(2);
149 4d3b6f6e balrog
            return;
150 4d3b6f6e balrog
        }
151 4d3b6f6e balrog
    }
152 4d3b6f6e balrog
153 4d3b6f6e balrog
    curs_set(0);
154 4d3b6f6e balrog
}
155 4d3b6f6e balrog
156 4d3b6f6e balrog
/* generic keyboard conversion */
157 4d3b6f6e balrog
158 4d3b6f6e balrog
#include "curses_keys.h"
159 4d3b6f6e balrog
160 c227f099 Anthony Liguori
static kbd_layout_t *kbd_layout = NULL;
161 4d3b6f6e balrog
162 4d3b6f6e balrog
static void curses_refresh(DisplayState *ds)
163 4d3b6f6e balrog
{
164 44bb61c8 Samuel Thibault
    int chr, nextchr, keysym, keycode, keycode_alt;
165 4d3b6f6e balrog
166 4d3b6f6e balrog
    if (invalidate) {
167 4d3b6f6e balrog
        clear();
168 4d3b6f6e balrog
        refresh();
169 4d3b6f6e balrog
        curses_calc_pad();
170 7d957bd8 aliguori
        ds->surface->width = FONT_WIDTH * width;
171 7d957bd8 aliguori
        ds->surface->height = FONT_HEIGHT * height;
172 4d3b6f6e balrog
        vga_hw_invalidate();
173 4d3b6f6e balrog
        invalidate = 0;
174 4d3b6f6e balrog
    }
175 4d3b6f6e balrog
176 4d3b6f6e balrog
    vga_hw_text_update(screen);
177 4d3b6f6e balrog
178 4d3b6f6e balrog
    nextchr = ERR;
179 4d3b6f6e balrog
    while (1) {
180 4d3b6f6e balrog
        /* while there are any pending key strokes to process */
181 4d3b6f6e balrog
        if (nextchr == ERR)
182 4d3b6f6e balrog
            chr = getch();
183 4d3b6f6e balrog
        else {
184 4d3b6f6e balrog
            chr = nextchr;
185 4d3b6f6e balrog
            nextchr = ERR;
186 4d3b6f6e balrog
        }
187 4d3b6f6e balrog
188 4d3b6f6e balrog
        if (chr == ERR)
189 4d3b6f6e balrog
            break;
190 4d3b6f6e balrog
191 b1314cf9 balrog
#ifdef KEY_RESIZE
192 4d3b6f6e balrog
        /* this shouldn't occur when we use a custom SIGWINCH handler */
193 4d3b6f6e balrog
        if (chr == KEY_RESIZE) {
194 4d3b6f6e balrog
            clear();
195 4d3b6f6e balrog
            refresh();
196 4d3b6f6e balrog
            curses_calc_pad();
197 4d3b6f6e balrog
            curses_update(ds, 0, 0, width, height);
198 7d957bd8 aliguori
            ds->surface->width = FONT_WIDTH * width;
199 7d957bd8 aliguori
            ds->surface->height = FONT_HEIGHT * height;
200 4d3b6f6e balrog
            continue;
201 4d3b6f6e balrog
        }
202 b1314cf9 balrog
#endif
203 4d3b6f6e balrog
204 4d3b6f6e balrog
        keycode = curses2keycode[chr];
205 44bb61c8 Samuel Thibault
        keycode_alt = 0;
206 4d3b6f6e balrog
207 4d3b6f6e balrog
        /* alt key */
208 4d3b6f6e balrog
        if (keycode == 1) {
209 4d3b6f6e balrog
            nextchr = getch();
210 4d3b6f6e balrog
211 4d3b6f6e balrog
            if (nextchr != ERR) {
212 44bb61c8 Samuel Thibault
                chr = nextchr;
213 44bb61c8 Samuel Thibault
                keycode_alt = ALT;
214 4d3b6f6e balrog
                keycode = curses2keycode[nextchr];
215 4d3b6f6e balrog
                nextchr = ERR;
216 4d3b6f6e balrog
217 44bb61c8 Samuel Thibault
                if (keycode != -1) {
218 44bb61c8 Samuel Thibault
                    keycode |= ALT;
219 4d3b6f6e balrog
220 44bb61c8 Samuel Thibault
                    /* process keys reserved for qemu */
221 44bb61c8 Samuel Thibault
                    if (keycode >= QEMU_KEY_CONSOLE0 &&
222 44bb61c8 Samuel Thibault
                            keycode < QEMU_KEY_CONSOLE0 + 9) {
223 44bb61c8 Samuel Thibault
                        erase();
224 44bb61c8 Samuel Thibault
                        wnoutrefresh(stdscr);
225 44bb61c8 Samuel Thibault
                        console_select(keycode - QEMU_KEY_CONSOLE0);
226 4d3b6f6e balrog
227 44bb61c8 Samuel Thibault
                        invalidate = 1;
228 44bb61c8 Samuel Thibault
                        continue;
229 44bb61c8 Samuel Thibault
                    }
230 4d3b6f6e balrog
                }
231 4d3b6f6e balrog
            }
232 4d3b6f6e balrog
        }
233 4d3b6f6e balrog
234 44bb61c8 Samuel Thibault
        if (kbd_layout) {
235 44bb61c8 Samuel Thibault
            keysym = -1;
236 44bb61c8 Samuel Thibault
            if (chr < CURSES_KEYS)
237 44bb61c8 Samuel Thibault
                keysym = curses2keysym[chr];
238 44bb61c8 Samuel Thibault
239 44bb61c8 Samuel Thibault
            if (keysym == -1) {
240 d03703c8 Samuel Thibault
                if (chr < ' ') {
241 d03703c8 Samuel Thibault
                    keysym = chr + '@';
242 d03703c8 Samuel Thibault
                    if (keysym >= 'A' && keysym <= 'Z')
243 d03703c8 Samuel Thibault
                        keysym += 'a' - 'A';
244 d03703c8 Samuel Thibault
                    keysym |= KEYSYM_CNTRL;
245 d03703c8 Samuel Thibault
                } else
246 44bb61c8 Samuel Thibault
                    keysym = chr;
247 44bb61c8 Samuel Thibault
            }
248 4d3b6f6e balrog
249 44bb61c8 Samuel Thibault
            keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
250 44bb61c8 Samuel Thibault
            if (keycode == 0)
251 44bb61c8 Samuel Thibault
                continue;
252 44bb61c8 Samuel Thibault
253 44bb61c8 Samuel Thibault
            keycode |= (keysym & ~KEYSYM_MASK) >> 16;
254 44bb61c8 Samuel Thibault
            keycode |= keycode_alt;
255 4d3b6f6e balrog
        }
256 4d3b6f6e balrog
257 44bb61c8 Samuel Thibault
        if (keycode == -1)
258 44bb61c8 Samuel Thibault
            continue;
259 44bb61c8 Samuel Thibault
260 4d3b6f6e balrog
        if (is_graphic_console()) {
261 4d3b6f6e balrog
            /* since terminals don't know about key press and release
262 4d3b6f6e balrog
             * events, we need to emit both for each key received */
263 4d3b6f6e balrog
            if (keycode & SHIFT)
264 4d3b6f6e balrog
                kbd_put_keycode(SHIFT_CODE);
265 4d3b6f6e balrog
            if (keycode & CNTRL)
266 4d3b6f6e balrog
                kbd_put_keycode(CNTRL_CODE);
267 4d3b6f6e balrog
            if (keycode & ALT)
268 4d3b6f6e balrog
                kbd_put_keycode(ALT_CODE);
269 44bb61c8 Samuel Thibault
            if (keycode & ALTGR) {
270 44bb61c8 Samuel Thibault
                kbd_put_keycode(SCANCODE_EMUL0);
271 44bb61c8 Samuel Thibault
                kbd_put_keycode(ALT_CODE);
272 44bb61c8 Samuel Thibault
            }
273 4d3b6f6e balrog
            if (keycode & GREY)
274 4d3b6f6e balrog
                kbd_put_keycode(GREY_CODE);
275 4d3b6f6e balrog
            kbd_put_keycode(keycode & KEY_MASK);
276 4d3b6f6e balrog
            if (keycode & GREY)
277 4d3b6f6e balrog
                kbd_put_keycode(GREY_CODE);
278 4d3b6f6e balrog
            kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
279 44bb61c8 Samuel Thibault
            if (keycode & ALTGR) {
280 44bb61c8 Samuel Thibault
                kbd_put_keycode(SCANCODE_EMUL0);
281 44bb61c8 Samuel Thibault
                kbd_put_keycode(ALT_CODE | KEY_RELEASE);
282 44bb61c8 Samuel Thibault
            }
283 4d3b6f6e balrog
            if (keycode & ALT)
284 4d3b6f6e balrog
                kbd_put_keycode(ALT_CODE | KEY_RELEASE);
285 4d3b6f6e balrog
            if (keycode & CNTRL)
286 4d3b6f6e balrog
                kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
287 4d3b6f6e balrog
            if (keycode & SHIFT)
288 4d3b6f6e balrog
                kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
289 4d3b6f6e balrog
        } else {
290 44bb61c8 Samuel Thibault
            keysym = curses2qemu[chr];
291 4d3b6f6e balrog
            if (keysym == -1)
292 4d3b6f6e balrog
                keysym = chr;
293 4d3b6f6e balrog
294 4d3b6f6e balrog
            kbd_put_keysym(keysym);
295 4d3b6f6e balrog
        }
296 4d3b6f6e balrog
    }
297 4d3b6f6e balrog
}
298 4d3b6f6e balrog
299 aaf12c25 Blue Swirl
static void curses_atexit(void)
300 4d3b6f6e balrog
{
301 4d3b6f6e balrog
    endwin();
302 4d3b6f6e balrog
}
303 4d3b6f6e balrog
304 4d3b6f6e balrog
static void curses_setup(void)
305 4d3b6f6e balrog
{
306 4d3b6f6e balrog
    int i, colour_default[8] = {
307 4d3b6f6e balrog
        COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
308 4d3b6f6e balrog
        COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
309 4d3b6f6e balrog
    };
310 4d3b6f6e balrog
311 4d3b6f6e balrog
    /* input as raw as possible, let everything be interpreted
312 4d3b6f6e balrog
     * by the guest system */
313 4d3b6f6e balrog
    initscr(); noecho(); intrflush(stdscr, FALSE);
314 4d3b6f6e balrog
    nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
315 4d3b6f6e balrog
    start_color(); raw(); scrollok(stdscr, FALSE);
316 4d3b6f6e balrog
317 4d3b6f6e balrog
    for (i = 0; i < 64; i ++)
318 4d3b6f6e balrog
        init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
319 4d3b6f6e balrog
}
320 4d3b6f6e balrog
321 4d3b6f6e balrog
static void curses_keyboard_setup(void)
322 4d3b6f6e balrog
{
323 4d3b6f6e balrog
#if defined(__APPLE__)
324 4d3b6f6e balrog
    /* always use generic keymaps */
325 4d3b6f6e balrog
    if (!keyboard_layout)
326 4d3b6f6e balrog
        keyboard_layout = "en-us";
327 4d3b6f6e balrog
#endif
328 4d3b6f6e balrog
    if(keyboard_layout) {
329 0483755a aliguori
        kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
330 4d3b6f6e balrog
        if (!kbd_layout)
331 4d3b6f6e balrog
            exit(1);
332 4d3b6f6e balrog
    }
333 4d3b6f6e balrog
}
334 4d3b6f6e balrog
335 4d3b6f6e balrog
void curses_display_init(DisplayState *ds, int full_screen)
336 4d3b6f6e balrog
{
337 7d957bd8 aliguori
    DisplayChangeListener *dcl;
338 4d3b6f6e balrog
#ifndef _WIN32
339 4d3b6f6e balrog
    if (!isatty(1)) {
340 4d3b6f6e balrog
        fprintf(stderr, "We need a terminal output\n");
341 4d3b6f6e balrog
        exit(1);
342 4d3b6f6e balrog
    }
343 4d3b6f6e balrog
#endif
344 4d3b6f6e balrog
345 4d3b6f6e balrog
    curses_setup();
346 4d3b6f6e balrog
    curses_keyboard_setup();
347 28695489 Anthony Liguori
    atexit(curses_atexit);
348 4d3b6f6e balrog
349 4d3b6f6e balrog
#ifndef _WIN32
350 b1314cf9 balrog
#if defined(SIGWINCH) && defined(KEY_RESIZE)
351 4d3b6f6e balrog
    /* some curses implementations provide a handler, but we
352 4d3b6f6e balrog
     * want to be sure this is handled regardless of the library */
353 4d3b6f6e balrog
    signal(SIGWINCH, curses_winch_handler);
354 4d3b6f6e balrog
#endif
355 4d3b6f6e balrog
#endif
356 4d3b6f6e balrog
357 7d957bd8 aliguori
    dcl = (DisplayChangeListener *) qemu_mallocz(sizeof(DisplayChangeListener));
358 7d957bd8 aliguori
    dcl->dpy_update = curses_update;
359 7d957bd8 aliguori
    dcl->dpy_resize = curses_resize;
360 7d957bd8 aliguori
    dcl->dpy_refresh = curses_refresh;
361 7d957bd8 aliguori
    dcl->dpy_text_cursor = curses_cursor_position;
362 7d957bd8 aliguori
    register_displaychangelistener(ds, dcl);
363 7b5d76da aliguori
    qemu_free_displaysurface(ds);
364 68f00996 aliguori
    ds->surface = qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen);
365 4d3b6f6e balrog
366 4d3b6f6e balrog
    invalidate = 1;
367 4d3b6f6e balrog
}