]> git.proxmox.com Git - mirror_qemu.git/blame - ui/curses.c
input/curses: add kbd delay between keydown and keyup events
[mirror_qemu.git] / ui / curses.c
CommitLineData
4d3b6f6e
AZ
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 */
4d3b6f6e
AZ
24#include <curses.h>
25
26#ifndef _WIN32
4d3b6f6e
AZ
27#include <sys/ioctl.h>
28#include <termios.h>
29#endif
30
511d2b14 31#include "qemu-common.h"
28ecbaee 32#include "ui/console.h"
cd100328 33#include "ui/input.h"
9c17d615 34#include "sysemu/sysemu.h"
511d2b14 35
4d3b6f6e
AZ
36#define FONT_HEIGHT 16
37#define FONT_WIDTH 8
38
7c20b4a3 39static DisplayChangeListener *dcl;
c227f099 40static console_ch_t screen[160 * 100];
4d3b6f6e
AZ
41static WINDOW *screenpad = NULL;
42static int width, height, gwidth, gheight, invalidate;
43static int px, py, sminx, sminy, smaxx, smaxy;
44
7c20b4a3 45static void curses_update(DisplayChangeListener *dcl,
7c20b4a3 46 int x, int y, int w, int h)
4d3b6f6e
AZ
47{
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{
81c0d5a6 60 if (qemu_console_is_fixedsize(NULL)) {
4d3b6f6e
AZ
61 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
7c20b4a3 97static void curses_resize(DisplayChangeListener *dcl,
7c20b4a3 98 int width, int height)
4d3b6f6e 99{
a93a4a22 100 if (width == gwidth && height == gheight) {
4d3b6f6e 101 return;
a93a4a22 102 }
4d3b6f6e 103
a93a4a22
GH
104 gwidth = width;
105 gheight = height;
4d3b6f6e
AZ
106
107 curses_calc_pad();
108}
109
032ac6f8
GH
110#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
111static volatile sig_atomic_t got_sigwinch;
112static void curses_winch_check(void)
4d3b6f6e
AZ
113{
114 struct winsize {
115 unsigned short ws_row;
116 unsigned short ws_col;
117 unsigned short ws_xpixel; /* unused */
118 unsigned short ws_ypixel; /* unused */
119 } ws;
120
032ac6f8
GH
121 if (!got_sigwinch) {
122 return;
123 }
124 got_sigwinch = false;
125
126 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
4d3b6f6e 127 return;
032ac6f8 128 }
4d3b6f6e
AZ
129
130 resize_term(ws.ws_row, ws.ws_col);
4d3b6f6e 131 invalidate = 1;
032ac6f8 132}
4d3b6f6e 133
032ac6f8
GH
134static void curses_winch_handler(int signum)
135{
136 got_sigwinch = true;
4d3b6f6e 137}
032ac6f8
GH
138
139static void curses_winch_init(void)
140{
141 struct sigaction old, winch = {
142 .sa_handler = curses_winch_handler,
143 };
144 sigaction(SIGWINCH, &winch, &old);
145}
146#else
147static void curses_winch_check(void) {}
148static void curses_winch_init(void) {}
4d3b6f6e
AZ
149#endif
150
7c20b4a3 151static void curses_cursor_position(DisplayChangeListener *dcl,
7c20b4a3 152 int x, int y)
4d3b6f6e
AZ
153{
154 if (x >= 0) {
155 x = sminx + x - px;
156 y = sminy + y - py;
157
158 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
159 move(y, x);
160 curs_set(1);
161 /* it seems that curs_set(1) must always be called before
162 * curs_set(2) for the latter to have effect */
81c0d5a6 163 if (!qemu_console_is_graphic(NULL)) {
4d3b6f6e 164 curs_set(2);
81c0d5a6 165 }
4d3b6f6e
AZ
166 return;
167 }
168 }
169
170 curs_set(0);
171}
172
173/* generic keyboard conversion */
174
175#include "curses_keys.h"
4d3b6f6e 176
c227f099 177static kbd_layout_t *kbd_layout = NULL;
4d3b6f6e 178
bc2ed970 179static void curses_refresh(DisplayChangeListener *dcl)
4d3b6f6e 180{
44bb61c8 181 int chr, nextchr, keysym, keycode, keycode_alt;
4d3b6f6e 182
032ac6f8
GH
183 curses_winch_check();
184
4d3b6f6e
AZ
185 if (invalidate) {
186 clear();
187 refresh();
188 curses_calc_pad();
1dbfa005 189 graphic_hw_invalidate(NULL);
4d3b6f6e
AZ
190 invalidate = 0;
191 }
192
1dbfa005 193 graphic_hw_text_update(NULL, screen);
4d3b6f6e
AZ
194
195 nextchr = ERR;
196 while (1) {
197 /* while there are any pending key strokes to process */
198 if (nextchr == ERR)
199 chr = getch();
200 else {
201 chr = nextchr;
202 nextchr = ERR;
203 }
204
205 if (chr == ERR)
206 break;
207
b1314cf9 208#ifdef KEY_RESIZE
4d3b6f6e
AZ
209 /* this shouldn't occur when we use a custom SIGWINCH handler */
210 if (chr == KEY_RESIZE) {
211 clear();
212 refresh();
213 curses_calc_pad();
bc2ed970 214 curses_update(dcl, 0, 0, width, height);
4d3b6f6e
AZ
215 continue;
216 }
b1314cf9 217#endif
4d3b6f6e
AZ
218
219 keycode = curses2keycode[chr];
44bb61c8 220 keycode_alt = 0;
4d3b6f6e
AZ
221
222 /* alt key */
223 if (keycode == 1) {
224 nextchr = getch();
225
226 if (nextchr != ERR) {
44bb61c8
ST
227 chr = nextchr;
228 keycode_alt = ALT;
4d3b6f6e
AZ
229 keycode = curses2keycode[nextchr];
230 nextchr = ERR;
4d3b6f6e 231
44bb61c8
ST
232 if (keycode != -1) {
233 keycode |= ALT;
4d3b6f6e 234
44bb61c8
ST
235 /* process keys reserved for qemu */
236 if (keycode >= QEMU_KEY_CONSOLE0 &&
237 keycode < QEMU_KEY_CONSOLE0 + 9) {
238 erase();
239 wnoutrefresh(stdscr);
240 console_select(keycode - QEMU_KEY_CONSOLE0);
4d3b6f6e 241
44bb61c8
ST
242 invalidate = 1;
243 continue;
244 }
4d3b6f6e
AZ
245 }
246 }
247 }
248
44bb61c8
ST
249 if (kbd_layout) {
250 keysym = -1;
251 if (chr < CURSES_KEYS)
252 keysym = curses2keysym[chr];
253
254 if (keysym == -1) {
d03703c8
ST
255 if (chr < ' ') {
256 keysym = chr + '@';
257 if (keysym >= 'A' && keysym <= 'Z')
258 keysym += 'a' - 'A';
259 keysym |= KEYSYM_CNTRL;
260 } else
44bb61c8
ST
261 keysym = chr;
262 }
4d3b6f6e 263
44bb61c8
ST
264 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
265 if (keycode == 0)
266 continue;
267
268 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
269 keycode |= keycode_alt;
4d3b6f6e
AZ
270 }
271
44bb61c8
ST
272 if (keycode == -1)
273 continue;
274
81c0d5a6 275 if (qemu_console_is_graphic(NULL)) {
4d3b6f6e
AZ
276 /* since terminals don't know about key press and release
277 * events, we need to emit both for each key received */
cd100328
GH
278 if (keycode & SHIFT) {
279 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
5a165668 280 qemu_input_event_send_key_delay(0);
cd100328
GH
281 }
282 if (keycode & CNTRL) {
283 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
5a165668 284 qemu_input_event_send_key_delay(0);
cd100328
GH
285 }
286 if (keycode & ALT) {
287 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
5a165668 288 qemu_input_event_send_key_delay(0);
cd100328 289 }
44bb61c8 290 if (keycode & ALTGR) {
cd100328 291 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
5a165668 292 qemu_input_event_send_key_delay(0);
44bb61c8 293 }
cd100328 294
f5c0ab13 295 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
5a165668 296 qemu_input_event_send_key_delay(0);
f5c0ab13 297 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
5a165668 298 qemu_input_event_send_key_delay(0);
cd100328 299
44bb61c8 300 if (keycode & ALTGR) {
cd100328 301 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
5a165668 302 qemu_input_event_send_key_delay(0);
cd100328
GH
303 }
304 if (keycode & ALT) {
305 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
5a165668 306 qemu_input_event_send_key_delay(0);
cd100328
GH
307 }
308 if (keycode & CNTRL) {
309 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
5a165668 310 qemu_input_event_send_key_delay(0);
cd100328
GH
311 }
312 if (keycode & SHIFT) {
313 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
5a165668 314 qemu_input_event_send_key_delay(0);
44bb61c8 315 }
4d3b6f6e 316 } else {
44bb61c8 317 keysym = curses2qemu[chr];
4d3b6f6e
AZ
318 if (keysym == -1)
319 keysym = chr;
320
321 kbd_put_keysym(keysym);
322 }
323 }
324}
325
aaf12c25 326static void curses_atexit(void)
4d3b6f6e
AZ
327{
328 endwin();
329}
330
4d3b6f6e
AZ
331static void curses_setup(void)
332{
333 int i, colour_default[8] = {
334 COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
335 COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
336 };
337
338 /* input as raw as possible, let everything be interpreted
339 * by the guest system */
340 initscr(); noecho(); intrflush(stdscr, FALSE);
341 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
342 start_color(); raw(); scrollok(stdscr, FALSE);
343
344 for (i = 0; i < 64; i ++)
345 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
346}
347
348static void curses_keyboard_setup(void)
349{
4d3b6f6e
AZ
350#if defined(__APPLE__)
351 /* always use generic keymaps */
352 if (!keyboard_layout)
353 keyboard_layout = "en-us";
354#endif
355 if(keyboard_layout) {
0483755a 356 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
4d3b6f6e
AZ
357 if (!kbd_layout)
358 exit(1);
359 }
4d3b6f6e
AZ
360}
361
7c20b4a3
GH
362static const DisplayChangeListenerOps dcl_ops = {
363 .dpy_name = "curses",
364 .dpy_text_update = curses_update,
365 .dpy_text_resize = curses_resize,
366 .dpy_refresh = curses_refresh,
367 .dpy_text_cursor = curses_cursor_position,
368};
369
4d3b6f6e
AZ
370void curses_display_init(DisplayState *ds, int full_screen)
371{
372#ifndef _WIN32
373 if (!isatty(1)) {
374 fprintf(stderr, "We need a terminal output\n");
375 exit(1);
376 }
377#endif
378
379 curses_setup();
380 curses_keyboard_setup();
28695489 381 atexit(curses_atexit);
4d3b6f6e 382
032ac6f8 383 curses_winch_init();
4d3b6f6e 384
7267c094 385 dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
7c20b4a3 386 dcl->ops = &dcl_ops;
5209089f 387 register_displaychangelistener(dcl);
4d3b6f6e
AZ
388
389 invalidate = 1;
4d3b6f6e 390}