Statistics
| Branch: | Revision:

root / curses.c @ b42ec42d

History | View | Annotate | Download (9.9 kB)

1
/*
2
 * QEMU curses/ncurses display driver
3
 * 
4
 * Copyright (c) 2005 Andrzej Zaborowski  <balrog@zabor.org>
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#include "qemu-common.h"
26
#include "console.h"
27
#include "sysemu.h"
28

    
29
#include <curses.h>
30

    
31
#ifndef _WIN32
32
#include <signal.h>
33
#include <sys/ioctl.h>
34
#include <termios.h>
35
#endif
36

    
37
#ifdef __OpenBSD__
38
#define resize_term resizeterm
39
#endif
40

    
41
#define FONT_HEIGHT 16
42
#define FONT_WIDTH 8
43

    
44
static console_ch_t screen[160 * 100];
45
static WINDOW *screenpad = NULL;
46
static int width, height, gwidth, gheight, invalidate;
47
static int px, py, sminx, sminy, smaxx, smaxy;
48

    
49
static void curses_update(DisplayState *ds, int x, int y, int w, int h)
50
{
51
    chtype *line;
52

    
53
    line = ((chtype *) screen) + y * width;
54
    for (h += y; y < h; y ++, line += width)
55
        mvwaddchnstr(screenpad, y, 0, line, width);
56

    
57
    pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
58
    refresh();
59
}
60

    
61
static void curses_calc_pad(void)
62
{
63
    if (is_fixedsize_console()) {
64
        width = gwidth;
65
        height = gheight;
66
    } else {
67
        width = COLS;
68
        height = LINES;
69
    }
70

    
71
    if (screenpad)
72
        delwin(screenpad);
73

    
74
    clear();
75
    refresh();
76

    
77
    screenpad = newpad(height, width);
78

    
79
    if (width > COLS) {
80
        px = (width - COLS) / 2;
81
        sminx = 0;
82
        smaxx = COLS;
83
    } else {
84
        px = 0;
85
        sminx = (COLS - width) / 2;
86
        smaxx = sminx + width;
87
    }
88

    
89
    if (height > LINES) {
90
        py = (height - LINES) / 2;
91
        sminy = 0;
92
        smaxy = LINES;
93
    } else {
94
        py = 0;
95
        sminy = (LINES - height) / 2;
96
        smaxy = sminy + height;
97
    }
98
}
99

    
100
static void curses_resize(DisplayState *ds)
101
{
102
    if (ds_get_width(ds) == gwidth && ds_get_height(ds) == gheight)
103
        return;
104

    
105
    gwidth = ds_get_width(ds);
106
    gheight = ds_get_height(ds);
107

    
108
    curses_calc_pad();
109
    ds->surface->width = width * FONT_WIDTH;
110
    ds->surface->height = height * FONT_HEIGHT;
111
}
112

    
113
#ifndef _WIN32
114
#if defined(SIGWINCH) && defined(KEY_RESIZE)
115
static void curses_winch_handler(int signum)
116
{
117
    struct winsize {
118
        unsigned short ws_row;
119
        unsigned short ws_col;
120
        unsigned short ws_xpixel;   /* unused */
121
        unsigned short ws_ypixel;   /* unused */
122
    } ws;
123

    
124
    /* terminal size changed */
125
    if (ioctl(1, TIOCGWINSZ, &ws) == -1)
126
        return;
127

    
128
    resize_term(ws.ws_row, ws.ws_col);
129
    curses_calc_pad();
130
    invalidate = 1;
131

    
132
    /* some systems require this */
133
    signal(SIGWINCH, curses_winch_handler);
134
}
135
#endif
136
#endif
137

    
138
static void curses_cursor_position(DisplayState *ds, int x, int y)
139
{
140
    if (x >= 0) {
141
        x = sminx + x - px;
142
        y = sminy + y - py;
143

    
144
        if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
145
            move(y, x);
146
            curs_set(1);
147
            /* it seems that curs_set(1) must always be called before
148
             * curs_set(2) for the latter to have effect */
149
            if (!is_graphic_console())
150
                curs_set(2);
151
            return;
152
        }
153
    }
154

    
155
    curs_set(0);
156
}
157

    
158
/* generic keyboard conversion */
159

    
160
#include "curses_keys.h"
161
#include "keymaps.c"
162

    
163
static kbd_layout_t *kbd_layout = 0;
164
static int keycode2keysym[CURSES_KEYS];
165

    
166
static void curses_refresh(DisplayState *ds)
167
{
168
    int chr, nextchr, keysym, keycode;
169

    
170
    if (invalidate) {
171
        clear();
172
        refresh();
173
        curses_calc_pad();
174
        ds->surface->width = FONT_WIDTH * width;
175
        ds->surface->height = FONT_HEIGHT * height;
176
        vga_hw_invalidate();
177
        invalidate = 0;
178
    }
179

    
180
    vga_hw_text_update(screen);
181

    
182
    nextchr = ERR;
183
    while (1) {
184
        /* while there are any pending key strokes to process */
185
        if (nextchr == ERR)
186
            chr = getch();
187
        else {
188
            chr = nextchr;
189
            nextchr = ERR;
190
        }
191

    
192
        if (chr == ERR)
193
            break;
194

    
195
#ifdef KEY_RESIZE
196
        /* this shouldn't occur when we use a custom SIGWINCH handler */
197
        if (chr == KEY_RESIZE) {
198
            clear();
199
            refresh();
200
            curses_calc_pad();
201
            curses_update(ds, 0, 0, width, height);
202
            ds->surface->width = FONT_WIDTH * width;
203
            ds->surface->height = FONT_HEIGHT * height;
204
            continue;
205
        }
206
#endif
207

    
208
        keycode = curses2keycode[chr];
209
        if (keycode == -1)
210
            continue;
211

    
212
        /* alt key */
213
        if (keycode == 1) {
214
            nextchr = getch();
215

    
216
            if (nextchr != ERR) {
217
                keycode = curses2keycode[nextchr];
218
                nextchr = ERR;
219
                if (keycode == -1)
220
                    continue;
221

    
222
                keycode |= ALT;
223

    
224
                /* process keys reserved for qemu */
225
                if (keycode >= QEMU_KEY_CONSOLE0 &&
226
                        keycode < QEMU_KEY_CONSOLE0 + 9) {
227
                    erase();
228
                    wnoutrefresh(stdscr);
229
                    console_select(keycode - QEMU_KEY_CONSOLE0);
230

    
231
                    invalidate = 1;
232
                    continue;
233
                }
234
            }
235
        }
236

    
237
        if (kbd_layout && !(keycode & GREY)) {
238
            keysym = keycode2keysym[keycode & KEY_MASK];
239
            if (keysym == -1)
240
                keysym = chr;
241

    
242
            keycode &= ~KEY_MASK;
243
            keycode |= keysym2scancode(kbd_layout, keysym);
244
        }
245

    
246
        if (is_graphic_console()) {
247
            /* since terminals don't know about key press and release
248
             * events, we need to emit both for each key received */
249
            if (keycode & SHIFT)
250
                kbd_put_keycode(SHIFT_CODE);
251
            if (keycode & CNTRL)
252
                kbd_put_keycode(CNTRL_CODE);
253
            if (keycode & ALT)
254
                kbd_put_keycode(ALT_CODE);
255
            if (keycode & GREY)
256
                kbd_put_keycode(GREY_CODE);
257
            kbd_put_keycode(keycode & KEY_MASK);
258
            if (keycode & GREY)
259
                kbd_put_keycode(GREY_CODE);
260
            kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
261
            if (keycode & ALT)
262
                kbd_put_keycode(ALT_CODE | KEY_RELEASE);
263
            if (keycode & CNTRL)
264
                kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
265
            if (keycode & SHIFT)
266
                kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
267
        } else {
268
            keysym = curses2keysym[chr];
269
            if (keysym == -1)
270
                keysym = chr;
271

    
272
            kbd_put_keysym(keysym);
273
        }
274
    }
275
}
276

    
277
static void curses_cleanup(void *opaque) 
278
{
279
    endwin();
280
}
281

    
282
static void curses_atexit(void)
283
{
284
    curses_cleanup(NULL);
285
}
286

    
287
static void curses_setup(void)
288
{
289
    int i, colour_default[8] = {
290
        COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
291
        COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
292
    };
293

    
294
    /* input as raw as possible, let everything be interpreted
295
     * by the guest system */
296
    initscr(); noecho(); intrflush(stdscr, FALSE);
297
    nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
298
    start_color(); raw(); scrollok(stdscr, FALSE);
299

    
300
    for (i = 0; i < 64; i ++)
301
        init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
302
}
303

    
304
static void curses_keyboard_setup(void)
305
{
306
    int i, keycode, keysym;
307

    
308
#if defined(__APPLE__)
309
    /* always use generic keymaps */
310
    if (!keyboard_layout)
311
        keyboard_layout = "en-us";
312
#endif
313
    if(keyboard_layout) {
314
        kbd_layout = init_keyboard_layout(keyboard_layout);
315
        if (!kbd_layout)
316
            exit(1);
317
    }
318

    
319
    for (i = 0; i < CURSES_KEYS; i ++)
320
        keycode2keysym[i] = -1;
321

    
322
    for (i = 0; i < CURSES_KEYS; i ++) {
323
        if (curses2keycode[i] == -1)
324
            continue;
325

    
326
        keycode = curses2keycode[i] & KEY_MASK;
327
        if (keycode2keysym[keycode] >= 0)
328
            continue;
329

    
330
        for (keysym = 0; keysym < CURSES_KEYS; keysym ++)
331
            if (curses2keycode[keysym] == keycode) {
332
                keycode2keysym[keycode] = keysym;
333
                break;
334
            }
335

    
336
        if (keysym >= CURSES_KEYS)
337
            keycode2keysym[keycode] = i;
338
    }
339
}
340

    
341
void curses_display_init(DisplayState *ds, int full_screen)
342
{
343
    DisplayChangeListener *dcl;
344
#ifndef _WIN32
345
    if (!isatty(1)) {
346
        fprintf(stderr, "We need a terminal output\n");
347
        exit(1);
348
    }
349
#endif
350

    
351
    curses_setup();
352
    curses_keyboard_setup();
353
    atexit(curses_atexit);
354

    
355
#ifndef _WIN32
356
#if defined(SIGWINCH) && defined(KEY_RESIZE)
357
    /* some curses implementations provide a handler, but we
358
     * want to be sure this is handled regardless of the library */
359
    signal(SIGWINCH, curses_winch_handler);
360
#endif
361
#endif
362

    
363
    dcl = (DisplayChangeListener *) qemu_mallocz(sizeof(DisplayChangeListener));
364
    dcl->dpy_update = curses_update;
365
    dcl->dpy_resize = curses_resize;
366
    dcl->dpy_refresh = curses_refresh;
367
    dcl->dpy_text_cursor = curses_cursor_position;
368
    register_displaychangelistener(ds, dcl);
369
    qemu_free_displaysurface(ds->surface);
370
    ds->surface = qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen);
371

    
372
    invalidate = 1;
373

    
374
    /* Standard VGA initial text mode dimensions */
375
    curses_resize(ds);
376
}