Statistics
| Branch: | Revision:

root / curses.c @ 0d84be5b

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