blob: ed9e65c7f077dc0e5ac912174a457dcd40c33966 [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{
balrogc21bbcf2008-09-24 03:32:33 +000059 if (is_fixedsize_console()) {
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
109#ifndef _WIN32
balrogb1314cf2008-02-22 18:21:28 +0000110#if defined(SIGWINCH) && defined(KEY_RESIZE)
balrog4d3b6f62008-02-10 16:33:14 +0000111static void curses_winch_handler(int signum)
112{
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
120 /* terminal size changed */
121 if (ioctl(1, TIOCGWINSZ, &ws) == -1)
122 return;
123
124 resize_term(ws.ws_row, ws.ws_col);
125 curses_calc_pad();
126 invalidate = 1;
127
128 /* some systems require this */
129 signal(SIGWINCH, curses_winch_handler);
130}
131#endif
132#endif
133
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100134static void curses_cursor_position(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100135 int x, int y)
balrog4d3b6f62008-02-10 16:33:14 +0000136{
137 if (x >= 0) {
138 x = sminx + x - px;
139 y = sminy + y - py;
140
141 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
142 move(y, x);
143 curs_set(1);
144 /* it seems that curs_set(1) must always be called before
145 * curs_set(2) for the latter to have effect */
146 if (!is_graphic_console())
147 curs_set(2);
148 return;
149 }
150 }
151
152 curs_set(0);
153}
154
155/* generic keyboard conversion */
156
157#include "curses_keys.h"
balrog4d3b6f62008-02-10 16:33:14 +0000158
Anthony Liguoric227f092009-10-01 16:12:16 -0500159static kbd_layout_t *kbd_layout = NULL;
balrog4d3b6f62008-02-10 16:33:14 +0000160
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100161static void curses_refresh(DisplayChangeListener *dcl)
balrog4d3b6f62008-02-10 16:33:14 +0000162{
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100163 int chr, nextchr, keysym, keycode, keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000164
165 if (invalidate) {
166 clear();
167 refresh();
168 curses_calc_pad();
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100169 graphic_hw_invalidate(NULL);
balrog4d3b6f62008-02-10 16:33:14 +0000170 invalidate = 0;
171 }
172
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100173 graphic_hw_text_update(NULL, screen);
balrog4d3b6f62008-02-10 16:33:14 +0000174
175 nextchr = ERR;
176 while (1) {
177 /* while there are any pending key strokes to process */
178 if (nextchr == ERR)
179 chr = getch();
180 else {
181 chr = nextchr;
182 nextchr = ERR;
183 }
184
185 if (chr == ERR)
186 break;
187
balrogb1314cf2008-02-22 18:21:28 +0000188#ifdef KEY_RESIZE
balrog4d3b6f62008-02-10 16:33:14 +0000189 /* this shouldn't occur when we use a custom SIGWINCH handler */
190 if (chr == KEY_RESIZE) {
191 clear();
192 refresh();
193 curses_calc_pad();
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100194 curses_update(dcl, 0, 0, width, height);
balrog4d3b6f62008-02-10 16:33:14 +0000195 continue;
196 }
balrogb1314cf2008-02-22 18:21:28 +0000197#endif
balrog4d3b6f62008-02-10 16:33:14 +0000198
199 keycode = curses2keycode[chr];
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100200 keycode_alt = 0;
balrog4d3b6f62008-02-10 16:33:14 +0000201
202 /* alt key */
203 if (keycode == 1) {
204 nextchr = getch();
205
206 if (nextchr != ERR) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100207 chr = nextchr;
208 keycode_alt = ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000209 keycode = curses2keycode[nextchr];
210 nextchr = ERR;
balrog4d3b6f62008-02-10 16:33:14 +0000211
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100212 if (keycode != -1) {
213 keycode |= ALT;
balrog4d3b6f62008-02-10 16:33:14 +0000214
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100215 /* process keys reserved for qemu */
216 if (keycode >= QEMU_KEY_CONSOLE0 &&
217 keycode < QEMU_KEY_CONSOLE0 + 9) {
218 erase();
219 wnoutrefresh(stdscr);
220 console_select(keycode - QEMU_KEY_CONSOLE0);
balrog4d3b6f62008-02-10 16:33:14 +0000221
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100222 invalidate = 1;
223 continue;
224 }
balrog4d3b6f62008-02-10 16:33:14 +0000225 }
226 }
227 }
228
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100229 if (kbd_layout) {
230 keysym = -1;
231 if (chr < CURSES_KEYS)
232 keysym = curses2keysym[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000233
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100234 if (keysym == -1) {
Samuel Thibaultd03703c2010-10-19 19:48:20 +0200235 if (chr < ' ') {
236 keysym = chr + '@';
237 if (keysym >= 'A' && keysym <= 'Z')
238 keysym += 'a' - 'A';
239 keysym |= KEYSYM_CNTRL;
240 } else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100241 keysym = chr;
242 }
243
244 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
245 if (keycode == 0)
246 continue;
247
248 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
249 keycode |= keycode_alt;
balrog4d3b6f62008-02-10 16:33:14 +0000250 }
251
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100252 if (keycode == -1)
253 continue;
254
balrog4d3b6f62008-02-10 16:33:14 +0000255 if (is_graphic_console()) {
256 /* since terminals don't know about key press and release
257 * events, we need to emit both for each key received */
258 if (keycode & SHIFT)
259 kbd_put_keycode(SHIFT_CODE);
260 if (keycode & CNTRL)
261 kbd_put_keycode(CNTRL_CODE);
262 if (keycode & ALT)
263 kbd_put_keycode(ALT_CODE);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100264 if (keycode & ALTGR) {
265 kbd_put_keycode(SCANCODE_EMUL0);
266 kbd_put_keycode(ALT_CODE);
267 }
balrog4d3b6f62008-02-10 16:33:14 +0000268 if (keycode & GREY)
269 kbd_put_keycode(GREY_CODE);
270 kbd_put_keycode(keycode & KEY_MASK);
271 if (keycode & GREY)
272 kbd_put_keycode(GREY_CODE);
273 kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100274 if (keycode & ALTGR) {
275 kbd_put_keycode(SCANCODE_EMUL0);
276 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
277 }
balrog4d3b6f62008-02-10 16:33:14 +0000278 if (keycode & ALT)
279 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
280 if (keycode & CNTRL)
281 kbd_put_keycode(CNTRL_CODE | KEY_RELEASE);
282 if (keycode & SHIFT)
283 kbd_put_keycode(SHIFT_CODE | KEY_RELEASE);
284 } else {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100285 keysym = curses2qemu[chr];
balrog4d3b6f62008-02-10 16:33:14 +0000286 if (keysym == -1)
287 keysym = chr;
288
289 kbd_put_keysym(keysym);
290 }
291 }
292}
293
Blue Swirlaaf12c22010-03-21 19:44:06 +0000294static void curses_atexit(void)
balrog4d3b6f62008-02-10 16:33:14 +0000295{
296 endwin();
297}
298
balrog4d3b6f62008-02-10 16:33:14 +0000299static void curses_setup(void)
300{
301 int i, colour_default[8] = {
302 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
303 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
304 };
305
306 /* input as raw as possible, let everything be interpreted
307 * by the guest system */
308 initscr(); noecho(); intrflush(stdscr, FALSE);
309 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
310 start_color(); raw(); scrollok(stdscr, FALSE);
311
312 for (i = 0; i < 64; i ++)
313 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
314}
315
316static void curses_keyboard_setup(void)
317{
balrog4d3b6f62008-02-10 16:33:14 +0000318#if defined(__APPLE__)
319 /* always use generic keymaps */
320 if (!keyboard_layout)
321 keyboard_layout = "en-us";
322#endif
323 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000324 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
balrog4d3b6f62008-02-10 16:33:14 +0000325 if (!kbd_layout)
326 exit(1);
327 }
balrog4d3b6f62008-02-10 16:33:14 +0000328}
329
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100330static const DisplayChangeListenerOps dcl_ops = {
331 .dpy_name = "curses",
332 .dpy_text_update = curses_update,
333 .dpy_text_resize = curses_resize,
334 .dpy_refresh = curses_refresh,
335 .dpy_text_cursor = curses_cursor_position,
336};
337
balrog4d3b6f62008-02-10 16:33:14 +0000338void curses_display_init(DisplayState *ds, int full_screen)
339{
340#ifndef _WIN32
341 if (!isatty(1)) {
342 fprintf(stderr, "We need a terminal output\n");
343 exit(1);
344 }
345#endif
346
347 curses_setup();
348 curses_keyboard_setup();
Anthony Liguori28695482010-03-21 14:13:02 -0500349 atexit(curses_atexit);
balrog4d3b6f62008-02-10 16:33:14 +0000350
351#ifndef _WIN32
balrogb1314cf2008-02-22 18:21:28 +0000352#if defined(SIGWINCH) && defined(KEY_RESIZE)
balrog4d3b6f62008-02-10 16:33:14 +0000353 /* some curses implementations provide a handler, but we
354 * want to be sure this is handled regardless of the library */
355 signal(SIGWINCH, curses_winch_handler);
356#endif
357#endif
358
Anthony Liguori7267c092011-08-20 22:09:37 -0500359 dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100360 dcl->ops = &dcl_ops;
aliguori7d957bd2009-01-15 22:14:11 +0000361 register_displaychangelistener(ds, dcl);
balrog4d3b6f62008-02-10 16:33:14 +0000362
363 invalidate = 1;
balrog4d3b6f62008-02-10 16:33:14 +0000364}