blob: ca9856ccef5c5c3f7cab7baf21d413b8ee3cc8a7 [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 */
balrog4d3b6f62008-02-10 16:33:14 +000024#include <curses.h>
25
26#ifndef _WIN32
balrog4d3b6f62008-02-10 16:33:14 +000027#include <sys/ioctl.h>
28#include <termios.h>
29#endif
30
blueswir1511d2b12009-03-07 15:32:56 +000031#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010032#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000034
balrog4d3b6f62008-02-10 16:33:14 +000035#define FONT_HEIGHT 16
36#define FONT_WIDTH 8
37
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010038static DisplayChangeListener *dcl;
Anthony Liguoric227f092009-10-01 16:12:16 -050039static console_ch_t screen[160 * 100];
balrog4d3b6f62008-02-10 16:33:14 +000040static WINDOW *screenpad = NULL;
41static int width, height, gwidth, gheight, invalidate;
42static int px, py, sminx, sminy, smaxx, smaxy;
43
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010044static void curses_update(DisplayChangeListener *dcl,
45 DisplayState *ds,
46 int x, int y, int w, int h)
balrog4d3b6f62008-02-10 16:33:14 +000047{
48 chtype *line;
49
50 line = ((chtype *) screen) + y * width;
51 for (h += y; y < h; y ++, line += width)
52 mvwaddchnstr(screenpad, y, 0, line, width);
53
54 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
55 refresh();
56}
57
58static void curses_calc_pad(void)
59{
balrogc21bbcf2008-09-24 03:32:33 +000060 if (is_fixedsize_console()) {
balrog4d3b6f62008-02-10 16:33:14 +000061 width = gwidth;
62 height = gheight;
63 } else {
64 width = COLS;
65 height = LINES;
66 }
67
68 if (screenpad)
69 delwin(screenpad);
70
71 clear();
72 refresh();
73
74 screenpad = newpad(height, width);
75
76 if (width > COLS) {
77 px = (width - COLS) / 2;
78 sminx = 0;
79 smaxx = COLS;
80 } else {
81 px = 0;
82 sminx = (COLS - width) / 2;
83 smaxx = sminx + width;
84 }
85
86 if (height > LINES) {
87 py = (height - LINES) / 2;
88 sminy = 0;
89 smaxy = LINES;
90 } else {
91 py = 0;
92 sminy = (LINES - height) / 2;
93 smaxy = sminy + height;
94 }
95}
96
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010097static void curses_resize(DisplayChangeListener *dcl,
98 DisplayState *ds,
99 int width, int height)
balrog4d3b6f62008-02-10 16:33:14 +0000100{
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200101 if (width == gwidth && height == gheight) {
balrog4d3b6f62008-02-10 16:33:14 +0000102 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200103 }
balrog4d3b6f62008-02-10 16:33:14 +0000104
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200105 gwidth = width;
106 gheight = height;
balrog4d3b6f62008-02-10 16:33:14 +0000107
108 curses_calc_pad();
109}
110
111#ifndef _WIN32
balrogb1314cf2008-02-22 18:21:28 +0000112#if defined(SIGWINCH) && defined(KEY_RESIZE)
balrog4d3b6f62008-02-10 16:33:14 +0000113static void curses_winch_handler(int signum)
114{
115 struct winsize {
116 unsigned short ws_row;
117 unsigned short ws_col;
118 unsigned short ws_xpixel; /* unused */
119 unsigned short ws_ypixel; /* unused */
120 } ws;
121
122 /* terminal size changed */
123 if (ioctl(1, TIOCGWINSZ, &ws) == -1)
124 return;
125
126 resize_term(ws.ws_row, ws.ws_col);
127 curses_calc_pad();
128 invalidate = 1;
129
130 /* some systems require this */
131 signal(SIGWINCH, curses_winch_handler);
132}
133#endif
134#endif
135
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100136static void curses_cursor_position(DisplayChangeListener *dcl,
137 DisplayState *ds,
138 int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000139{
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
Anthony Liguoric227f092009-10-01 16:12:16 -0500162static kbd_layout_t *kbd_layout = NULL;
balrog4d3b6f62008-02-10 16:33:14 +0000163
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100164static void curses_refresh(DisplayChangeListener *dcl,
165 DisplayState *ds)
balrog4d3b6f62008-02-10 16:33:14 +0000166{
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100167 int chr, nextchr, keysym, keycode, keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000168
169 if (invalidate) {
170 clear();
171 refresh();
172 curses_calc_pad();
balrog4d3b6f62008-02-10 16:33:14 +0000173 vga_hw_invalidate();
174 invalidate = 0;
175 }
176
177 vga_hw_text_update(screen);
178
179 nextchr = ERR;
180 while (1) {
181 /* while there are any pending key strokes to process */
182 if (nextchr == ERR)
183 chr = getch();
184 else {
185 chr = nextchr;
186 nextchr = ERR;
187 }
188
189 if (chr == ERR)
190 break;
191
balrogb1314cf2008-02-22 18:21:28 +0000192#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000193 /* this shouldn't occur when we use a custom SIGWINCH handler */
194 if (chr == KEY_RESIZE) {
195 clear();
196 refresh();
197 curses_calc_pad();
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100198 curses_update(dcl, ds, 0, 0, width, height);
balrog4d3b6f62008-02-10 16:33:14 +0000199 continue;
200 }
balrogb1314cf2008-02-22 18:21:28 +0000201#endif
balrog4d3b6f62008-02-10 16:33:14 +0000202
203 keycode = curses2keycode[chr];
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100204 keycode_alt = 0;
balrog4d3b6f62008-02-10 16:33:14 +0000205
206 /* alt key */
207 if (keycode == 1) {
208 nextchr = getch();
209
210 if (nextchr != ERR) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100211 chr = nextchr;
212 keycode_alt = ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000213 keycode = curses2keycode[nextchr];
214 nextchr = ERR;
balrog4d3b6f62008-02-10 16:33:14 +0000215
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100216 if (keycode != -1) {
217 keycode |= ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000218
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100219 /* process keys reserved for qemu */
220 if (keycode >= QEMU_KEY_CONSOLE0 &&
221 keycode < QEMU_KEY_CONSOLE0 + 9) {
222 erase();
223 wnoutrefresh(stdscr);
224 console_select(keycode - QEMU_KEY_CONSOLE0);
balrog4d3b6f62008-02-10 16:33:14 +0000225
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100226 invalidate = 1;
227 continue;
228 }
balrog4d3b6f62008-02-10 16:33:14 +0000229 }
230 }
231 }
232
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100233 if (kbd_layout) {
234 keysym = -1;
235 if (chr < CURSES_KEYS)
236 keysym = curses2keysym[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000237
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100238 if (keysym == -1) {
Samuel Thibaultd03703c2010-10-19 19:48:20 +0200239 if (chr < ' ') {
240 keysym = chr + '@';
241 if (keysym >= 'A' && keysym <= 'Z')
242 keysym += 'a' - 'A';
243 keysym |= KEYSYM_CNTRL;
244 } else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100245 keysym = chr;
246 }
247
248 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
249 if (keycode == 0)
250 continue;
251
252 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
253 keycode |= keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000254 }
255
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100256 if (keycode == -1)
257 continue;
258
balrog4d3b6f62008-02-10 16:33:14 +0000259 if (is_graphic_console()) {
260 /* since terminals don't know about key press and release
261 * events, we need to emit both for each key received */
262 if (keycode & SHIFT)
263 kbd_put_keycode(SHIFT_CODE);
264 if (keycode & CNTRL)
265 kbd_put_keycode(CNTRL_CODE);
266 if (keycode & ALT)
267 kbd_put_keycode(ALT_CODE);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100268 if (keycode & ALTGR) {
269 kbd_put_keycode(SCANCODE_EMUL0);
270 kbd_put_keycode(ALT_CODE);
271 }
balrog4d3b6f62008-02-10 16:33:14 +0000272 if (keycode & GREY)
273 kbd_put_keycode(GREY_CODE);
274 kbd_put_keycode(keycode & KEY_MASK);
275 if (keycode & GREY)
276 kbd_put_keycode(GREY_CODE);
277 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100278 if (keycode & ALTGR) {
279 kbd_put_keycode(SCANCODE_EMUL0);
280 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
281 }
balrog4d3b6f62008-02-10 16:33:14 +0000282 if (keycode & ALT)
283 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
284 if (keycode & CNTRL)
285 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
286 if (keycode & SHIFT)
287 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
288 } else {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100289 keysym = curses2qemu[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000290 if (keysym == -1)
291 keysym = chr;
292
293 kbd_put_keysym(keysym);
294 }
295 }
296}
297
Blue Swirlaaf12c22010-03-21 19:44:06 +0000298static void curses_atexit(void)
balrog4d3b6f62008-02-10 16:33:14 +0000299{
300 endwin();
301}
302
balrog4d3b6f62008-02-10 16:33:14 +0000303static void curses_setup(void)
304{
305 int i, colour_default[8] = {
306 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
307 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
308 };
309
310 /* input as raw as possible, let everything be interpreted
311 * by the guest system */
312 initscr(); noecho(); intrflush(stdscr, FALSE);
313 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
314 start_color(); raw(); scrollok(stdscr, FALSE);
315
316 for (i = 0; i < 64; i ++)
317 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
318}
319
320static void curses_keyboard_setup(void)
321{
balrog4d3b6f62008-02-10 16:33:14 +0000322#if defined(__APPLE__)
323 /* always use generic keymaps */
324 if (!keyboard_layout)
325 keyboard_layout = "en-us";
326#endif
327 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000328 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
balrog4d3b6f62008-02-10 16:33:14 +0000329 if (!kbd_layout)
330 exit(1);
331 }
balrog4d3b6f62008-02-10 16:33:14 +0000332}
333
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100334static const DisplayChangeListenerOps dcl_ops = {
335 .dpy_name = "curses",
336 .dpy_text_update = curses_update,
337 .dpy_text_resize = curses_resize,
338 .dpy_refresh = curses_refresh,
339 .dpy_text_cursor = curses_cursor_position,
340};
341
balrog4d3b6f62008-02-10 16:33:14 +0000342void curses_display_init(DisplayState *ds, int full_screen)
343{
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();
Anthony Liguori28695482010-03-21 14:13:02 -0500353 atexit(curses_atexit);
balrog4d3b6f62008-02-10 16:33:14 +0000354
355#ifndef _WIN32
balrogb1314cf2008-02-22 18:21:28 +0000356#if defined(SIGWINCH) && defined(KEY_RESIZE)
balrog4d3b6f62008-02-10 16:33:14 +0000357 /* 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
Anthony Liguori7267c092011-08-20 22:09:37 -0500363 dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100364 dcl->ops = &dcl_ops;
aliguori7d957bd2009-01-15 22:14:11 +0000365 register_displaychangelistener(ds, dcl);
balrog4d3b6f62008-02-10 16:33:14 +0000366
367 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000368}