Statistics
| Branch: | Revision:

root / ui / curses.c @ 26ca8c06

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