blob: 434e1cfa4dbc9a68cec4095a6c03f70a25e877d1 [file] [log] [blame]
balrog4d3b6f62008-02-10 16:33:14 +00001/*
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
blueswir1128ab2f2008-08-15 18:33:42 +000037#ifdef __OpenBSD__
38#define resize_term resizeterm
39#endif
40
balrog4d3b6f62008-02-10 16:33:14 +000041#define FONT_HEIGHT 16
42#define FONT_WIDTH 8
43
44static console_ch_t screen[160 * 100];
45static WINDOW *screenpad = NULL;
46static int width, height, gwidth, gheight, invalidate;
47static int px, py, sminx, sminy, smaxx, smaxy;
48
49static 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
61static void curses_calc_pad(void)
62{
balrogc21bbcf2008-09-24 03:32:33 +000063 if (is_fixedsize_console()) {
balrog4d3b6f62008-02-10 16:33:14 +000064 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
aliguori7d957bd2009-01-15 22:14:11 +0000100static void curses_resize(DisplayState *ds)
balrog4d3b6f62008-02-10 16:33:14 +0000101{
aliguori7d957bd2009-01-15 22:14:11 +0000102 if (ds_get_width(ds) == gwidth && ds_get_height(ds) == gheight)
balrog4d3b6f62008-02-10 16:33:14 +0000103 return;
104
aliguori7d957bd2009-01-15 22:14:11 +0000105 gwidth = ds_get_width(ds);
106 gheight = ds_get_height(ds);
balrog4d3b6f62008-02-10 16:33:14 +0000107
108 curses_calc_pad();
aliguori68f00992009-01-21 18:59:12 +0000109 ds->surface->width = width * FONT_WIDTH;
110 ds->surface->height = height * FONT_HEIGHT;
balrog4d3b6f62008-02-10 16:33:14 +0000111}
112
113#ifndef _WIN32
balrogb1314cf2008-02-22 18:21:28 +0000114#if defined(SIGWINCH) && defined(KEY_RESIZE)
balrog4d3b6f62008-02-10 16:33:14 +0000115static 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
138static 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"
balrog4d3b6f62008-02-10 16:33:14 +0000161
162static kbd_layout_t *kbd_layout = 0;
163static int keycode2keysym[CURSES_KEYS];
164
165static void curses_refresh(DisplayState *ds)
166{
167 int chr, nextchr, keysym, keycode;
168
169 if (invalidate) {
170 clear();
171 refresh();
172 curses_calc_pad();
aliguori7d957bd2009-01-15 22:14:11 +0000173 ds->surface->width = FONT_WIDTH * width;
174 ds->surface->height = FONT_HEIGHT * height;
balrog4d3b6f62008-02-10 16:33:14 +0000175 vga_hw_invalidate();
176 invalidate = 0;
177 }
178
179 vga_hw_text_update(screen);
180
181 nextchr = ERR;
182 while (1) {
183 /* while there are any pending key strokes to process */
184 if (nextchr == ERR)
185 chr = getch();
186 else {
187 chr = nextchr;
188 nextchr = ERR;
189 }
190
191 if (chr == ERR)
192 break;
193
balrogb1314cf2008-02-22 18:21:28 +0000194#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000195 /* this shouldn't occur when we use a custom SIGWINCH handler */
196 if (chr == KEY_RESIZE) {
197 clear();
198 refresh();
199 curses_calc_pad();
200 curses_update(ds, 0, 0, width, height);
aliguori7d957bd2009-01-15 22:14:11 +0000201 ds->surface->width = FONT_WIDTH * width;
202 ds->surface->height = FONT_HEIGHT * height;
balrog4d3b6f62008-02-10 16:33:14 +0000203 continue;
204 }
balrogb1314cf2008-02-22 18:21:28 +0000205#endif
balrog4d3b6f62008-02-10 16:33:14 +0000206
207 keycode = curses2keycode[chr];
208 if (keycode == -1)
209 continue;
210
211 /* alt key */
212 if (keycode == 1) {
213 nextchr = getch();
214
215 if (nextchr != ERR) {
216 keycode = curses2keycode[nextchr];
217 nextchr = ERR;
218 if (keycode == -1)
219 continue;
220
221 keycode |= ALT;
222
223 /* process keys reserved for qemu */
224 if (keycode >= QEMU_KEY_CONSOLE0 &&
225 keycode < QEMU_KEY_CONSOLE0 + 9) {
226 erase();
227 wnoutrefresh(stdscr);
228 console_select(keycode - QEMU_KEY_CONSOLE0);
229
230 invalidate = 1;
231 continue;
232 }
233 }
234 }
235
236 if (kbd_layout && !(keycode & GREY)) {
237 keysym = keycode2keysym[keycode & KEY_MASK];
238 if (keysym == -1)
239 keysym = chr;
240
241 keycode &= ~KEY_MASK;
242 keycode |= keysym2scancode(kbd_layout, keysym);
243 }
244
245 if (is_graphic_console()) {
246 /* since terminals don't know about key press and release
247 * events, we need to emit both for each key received */
248 if (keycode & SHIFT)
249 kbd_put_keycode(SHIFT_CODE);
250 if (keycode & CNTRL)
251 kbd_put_keycode(CNTRL_CODE);
252 if (keycode & ALT)
253 kbd_put_keycode(ALT_CODE);
254 if (keycode & GREY)
255 kbd_put_keycode(GREY_CODE);
256 kbd_put_keycode(keycode & KEY_MASK);
257 if (keycode & GREY)
258 kbd_put_keycode(GREY_CODE);
259 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
260 if (keycode & ALT)
261 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
262 if (keycode & CNTRL)
263 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
264 if (keycode & SHIFT)
265 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
266 } else {
267 keysym = curses2keysym[chr];
268 if (keysym == -1)
269 keysym = chr;
270
271 kbd_put_keysym(keysym);
272 }
273 }
274}
275
276static void curses_cleanup(void *opaque)
277{
278 endwin();
279}
280
281static void curses_atexit(void)
282{
283 curses_cleanup(NULL);
284}
285
286static void curses_setup(void)
287{
288 int i, colour_default[8] = {
289 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
290 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
291 };
292
293 /* input as raw as possible, let everything be interpreted
294 * by the guest system */
295 initscr(); noecho(); intrflush(stdscr, FALSE);
296 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
297 start_color(); raw(); scrollok(stdscr, FALSE);
298
299 for (i = 0; i < 64; i ++)
300 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
301}
302
303static void curses_keyboard_setup(void)
304{
305 int i, keycode, keysym;
306
307#if defined(__APPLE__)
308 /* always use generic keymaps */
309 if (!keyboard_layout)
310 keyboard_layout = "en-us";
311#endif
312 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000313 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
balrog4d3b6f62008-02-10 16:33:14 +0000314 if (!kbd_layout)
315 exit(1);
316 }
317
318 for (i = 0; i < CURSES_KEYS; i ++)
319 keycode2keysym[i] = -1;
320
321 for (i = 0; i < CURSES_KEYS; i ++) {
322 if (curses2keycode[i] == -1)
323 continue;
324
325 keycode = curses2keycode[i] & KEY_MASK;
326 if (keycode2keysym[keycode] >= 0)
327 continue;
328
329 for (keysym = 0; keysym < CURSES_KEYS; keysym ++)
330 if (curses2keycode[keysym] == keycode) {
331 keycode2keysym[keycode] = keysym;
332 break;
333 }
334
335 if (keysym >= CURSES_KEYS)
336 keycode2keysym[keycode] = i;
337 }
338}
339
340void curses_display_init(DisplayState *ds, int full_screen)
341{
aliguori7d957bd2009-01-15 22:14:11 +0000342 DisplayChangeListener *dcl;
balrog4d3b6f62008-02-10 16:33:14 +0000343#ifndef _WIN32
344 if (!isatty(1)) {
345 fprintf(stderr, "We need a terminal output\n");
346 exit(1);
347 }
348#endif
349
350 curses_setup();
351 curses_keyboard_setup();
352 atexit(curses_atexit);
353
354#ifndef _WIN32
balrogb1314cf2008-02-22 18:21:28 +0000355#if defined(SIGWINCH) && defined(KEY_RESIZE)
balrog4d3b6f62008-02-10 16:33:14 +0000356 /* some curses implementations provide a handler, but we
357 * want to be sure this is handled regardless of the library */
358 signal(SIGWINCH, curses_winch_handler);
359#endif
360#endif
361
aliguori7d957bd2009-01-15 22:14:11 +0000362 dcl = (DisplayChangeListener *) qemu_mallocz(sizeof(DisplayChangeListener));
aliguori7d957bd2009-01-15 22:14:11 +0000363 dcl->dpy_update = curses_update;
364 dcl->dpy_resize = curses_resize;
365 dcl->dpy_refresh = curses_refresh;
366 dcl->dpy_text_cursor = curses_cursor_position;
367 register_displaychangelistener(ds, dcl);
368 qemu_free_displaysurface(ds->surface);
aliguori68f00992009-01-21 18:59:12 +0000369 ds->surface = qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen);
balrog4d3b6f62008-02-10 16:33:14 +0000370
371 invalidate = 1;
372
373 /* Standard VGA initial text mode dimensions */
aliguori7d957bd2009-01-15 22:14:11 +0000374 curses_resize(ds);
balrog4d3b6f62008-02-10 16:33:14 +0000375}