blob: dbc3d5ec735c8b15df664ec607fb127e58fb7830 [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,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010045 int x, int y, int w, int h)
balrog4d3b6f62008-02-10 16:33:14 +000046{
47 chtype *line;
48
49 line = ((chtype *) screen) + y * width;
50 for (h += y; y < h; y ++, line += width)
51 mvwaddchnstr(screenpad, y, 0, line, width);
52
53 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
54 refresh();
55}
56
57static void curses_calc_pad(void)
58{
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +010059 if (qemu_console_is_fixedsize(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +000060 width = gwidth;
61 height = gheight;
62 } else {
63 width = COLS;
64 height = LINES;
65 }
66
67 if (screenpad)
68 delwin(screenpad);
69
70 clear();
71 refresh();
72
73 screenpad = newpad(height, width);
74
75 if (width > COLS) {
76 px = (width - COLS) / 2;
77 sminx = 0;
78 smaxx = COLS;
79 } else {
80 px = 0;
81 sminx = (COLS - width) / 2;
82 smaxx = sminx + width;
83 }
84
85 if (height > LINES) {
86 py = (height - LINES) / 2;
87 sminy = 0;
88 smaxy = LINES;
89 } else {
90 py = 0;
91 sminy = (LINES - height) / 2;
92 smaxy = sminy + height;
93 }
94}
95
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010096static void curses_resize(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010097 int width, int height)
balrog4d3b6f62008-02-10 16:33:14 +000098{
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +020099 if (width == gwidth && height == gheight) {
balrog4d3b6f62008-02-10 16:33:14 +0000100 return;
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200101 }
balrog4d3b6f62008-02-10 16:33:14 +0000102
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200103 gwidth = width;
104 gheight = height;
balrog4d3b6f62008-02-10 16:33:14 +0000105
106 curses_calc_pad();
107}
108
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100109#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
110static volatile sig_atomic_t got_sigwinch;
111static void curses_winch_check(void)
balrog4d3b6f62008-02-10 16:33:14 +0000112{
113 struct winsize {
114 unsigned short ws_row;
115 unsigned short ws_col;
116 unsigned short ws_xpixel; /* unused */
117 unsigned short ws_ypixel; /* unused */
118 } ws;
119
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100120 if (!got_sigwinch) {
balrog4d3b6f62008-02-10 16:33:14 +0000121 return;
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100122 }
123 got_sigwinch = false;
124
125 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
126 return;
127 }
balrog4d3b6f62008-02-10 16:33:14 +0000128
129 resize_term(ws.ws_row, ws.ws_col);
balrog4d3b6f62008-02-10 16:33:14 +0000130 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000131}
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100132
133static void curses_winch_handler(int signum)
134{
135 got_sigwinch = true;
136}
137
138static void curses_winch_init(void)
139{
140 struct sigaction old, winch = {
141 .sa_handler = curses_winch_handler,
142 };
143 sigaction(SIGWINCH, &winch, &old);
144}
145#else
146static void curses_winch_check(void) {}
147static void curses_winch_init(void) {}
balrog4d3b6f62008-02-10 16:33:14 +0000148#endif
149
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100150static void curses_cursor_position(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100151 int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000152{
153 if (x >= 0) {
154 x = sminx + x - px;
155 y = sminy + y - py;
156
157 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
158 move(y, x);
159 curs_set(1);
160 /* it seems that curs_set(1) must always be called before
161 * curs_set(2) for the latter to have effect */
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100162 if (!qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000163 curs_set(2);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100164 }
balrog4d3b6f62008-02-10 16:33:14 +0000165 return;
166 }
167 }
168
169 curs_set(0);
170}
171
172/* generic keyboard conversion */
173
174#include "curses_keys.h"
balrog4d3b6f62008-02-10 16:33:14 +0000175
Anthony Liguoric227f092009-10-01 16:12:16 -0500176static kbd_layout_t *kbd_layout = NULL;
balrog4d3b6f62008-02-10 16:33:14 +0000177
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100178static void curses_refresh(DisplayChangeListener *dcl)
balrog4d3b6f62008-02-10 16:33:14 +0000179{
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100180 int chr, nextchr, keysym, keycode, keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000181
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100182 curses_winch_check();
183
balrog4d3b6f62008-02-10 16:33:14 +0000184 if (invalidate) {
185 clear();
186 refresh();
187 curses_calc_pad();
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100188 graphic_hw_invalidate(NULL);
balrog4d3b6f62008-02-10 16:33:14 +0000189 invalidate = 0;
190 }
191
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100192 graphic_hw_text_update(NULL, screen);
balrog4d3b6f62008-02-10 16:33:14 +0000193
194 nextchr = ERR;
195 while (1) {
196 /* while there are any pending key strokes to process */
197 if (nextchr == ERR)
198 chr = getch();
199 else {
200 chr = nextchr;
201 nextchr = ERR;
202 }
203
204 if (chr == ERR)
205 break;
206
balrogb1314cf2008-02-22 18:21:28 +0000207#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000208 /* this shouldn't occur when we use a custom SIGWINCH handler */
209 if (chr == KEY_RESIZE) {
210 clear();
211 refresh();
212 curses_calc_pad();
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100213 curses_update(dcl, 0, 0, width, height);
balrog4d3b6f62008-02-10 16:33:14 +0000214 continue;
215 }
balrogb1314cf2008-02-22 18:21:28 +0000216#endif
balrog4d3b6f62008-02-10 16:33:14 +0000217
218 keycode = curses2keycode[chr];
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100219 keycode_alt = 0;
balrog4d3b6f62008-02-10 16:33:14 +0000220
221 /* alt key */
222 if (keycode == 1) {
223 nextchr = getch();
224
225 if (nextchr != ERR) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100226 chr = nextchr;
227 keycode_alt = ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000228 keycode = curses2keycode[nextchr];
229 nextchr = ERR;
balrog4d3b6f62008-02-10 16:33:14 +0000230
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100231 if (keycode != -1) {
232 keycode |= ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000233
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100234 /* process keys reserved for qemu */
235 if (keycode >= QEMU_KEY_CONSOLE0 &&
236 keycode < QEMU_KEY_CONSOLE0 + 9) {
237 erase();
238 wnoutrefresh(stdscr);
239 console_select(keycode - QEMU_KEY_CONSOLE0);
balrog4d3b6f62008-02-10 16:33:14 +0000240
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100241 invalidate = 1;
242 continue;
243 }
balrog4d3b6f62008-02-10 16:33:14 +0000244 }
245 }
246 }
247
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100248 if (kbd_layout) {
249 keysym = -1;
250 if (chr < CURSES_KEYS)
251 keysym = curses2keysym[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000252
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100253 if (keysym == -1) {
Samuel Thibaultd03703c2010-10-19 19:48:20 +0200254 if (chr < ' ') {
255 keysym = chr + '@';
256 if (keysym >= 'A' && keysym <= 'Z')
257 keysym += 'a' - 'A';
258 keysym |= KEYSYM_CNTRL;
259 } else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100260 keysym = chr;
261 }
262
263 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
264 if (keycode == 0)
265 continue;
266
267 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
268 keycode |= keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000269 }
270
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100271 if (keycode == -1)
272 continue;
273
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100274 if (qemu_console_is_graphic(NULL)) {
balrog4d3b6f62008-02-10 16:33:14 +0000275 /* since terminals don't know about key press and release
276 * events, we need to emit both for each key received */
277 if (keycode & SHIFT)
278 kbd_put_keycode(SHIFT_CODE);
279 if (keycode & CNTRL)
280 kbd_put_keycode(CNTRL_CODE);
281 if (keycode & ALT)
282 kbd_put_keycode(ALT_CODE);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100283 if (keycode & ALTGR) {
284 kbd_put_keycode(SCANCODE_EMUL0);
285 kbd_put_keycode(ALT_CODE);
286 }
balrog4d3b6f62008-02-10 16:33:14 +0000287 if (keycode & GREY)
288 kbd_put_keycode(GREY_CODE);
289 kbd_put_keycode(keycode & KEY_MASK);
290 if (keycode & GREY)
291 kbd_put_keycode(GREY_CODE);
292 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100293 if (keycode & ALTGR) {
294 kbd_put_keycode(SCANCODE_EMUL0);
295 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
296 }
balrog4d3b6f62008-02-10 16:33:14 +0000297 if (keycode & ALT)
298 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
299 if (keycode & CNTRL)
300 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
301 if (keycode & SHIFT)
302 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
303 } else {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100304 keysym = curses2qemu[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000305 if (keysym == -1)
306 keysym = chr;
307
308 kbd_put_keysym(keysym);
309 }
310 }
311}
312
Blue Swirlaaf12c22010-03-21 19:44:06 +0000313static void curses_atexit(void)
balrog4d3b6f62008-02-10 16:33:14 +0000314{
315 endwin();
316}
317
balrog4d3b6f62008-02-10 16:33:14 +0000318static void curses_setup(void)
319{
320 int i, colour_default[8] = {
321 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
322 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
323 };
324
325 /* input as raw as possible, let everything be interpreted
326 * by the guest system */
327 initscr(); noecho(); intrflush(stdscr, FALSE);
328 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
329 start_color(); raw(); scrollok(stdscr, FALSE);
330
331 for (i = 0; i < 64; i ++)
332 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
333}
334
335static void curses_keyboard_setup(void)
336{
balrog4d3b6f62008-02-10 16:33:14 +0000337#if defined(__APPLE__)
338 /* always use generic keymaps */
339 if (!keyboard_layout)
340 keyboard_layout = "en-us";
341#endif
342 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000343 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
balrog4d3b6f62008-02-10 16:33:14 +0000344 if (!kbd_layout)
345 exit(1);
346 }
balrog4d3b6f62008-02-10 16:33:14 +0000347}
348
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100349static const DisplayChangeListenerOps dcl_ops = {
350 .dpy_name = "curses",
351 .dpy_text_update = curses_update,
352 .dpy_text_resize = curses_resize,
353 .dpy_refresh = curses_refresh,
354 .dpy_text_cursor = curses_cursor_position,
355};
356
balrog4d3b6f62008-02-10 16:33:14 +0000357void curses_display_init(DisplayState *ds, int full_screen)
358{
359#ifndef _WIN32
360 if (!isatty(1)) {
361 fprintf(stderr, "We need a terminal output\n");
362 exit(1);
363 }
364#endif
365
366 curses_setup();
367 curses_keyboard_setup();
Anthony Liguori28695482010-03-21 14:13:02 -0500368 atexit(curses_atexit);
balrog4d3b6f62008-02-10 16:33:14 +0000369
Gerd Hoffmann032ac6f2013-11-22 15:35:03 +0100370 curses_winch_init();
balrog4d3b6f62008-02-10 16:33:14 +0000371
Anthony Liguori7267c092011-08-20 22:09:37 -0500372 dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100373 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +0200374 register_displaychangelistener(dcl);
balrog4d3b6f62008-02-10 16:33:14 +0000375
376 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000377}