]> git.proxmox.com Git - qemu.git/blame - ui/curses.c
fdc-test: split test_media_change() test, so insert part can be reused
[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
128ab2ff
BS
31#ifdef __OpenBSD__
32#define resize_term resizeterm
33#endif
34
511d2b14
BS
35#include "qemu-common.h"
36#include "console.h"
37#include "sysemu.h"
38
4d3b6f6e
AZ
39#define FONT_HEIGHT 16
40#define FONT_WIDTH 8
41
c227f099 42static console_ch_t screen[160 * 100];
4d3b6f6e
AZ
43static WINDOW *screenpad = NULL;
44static int width, height, gwidth, gheight, invalidate;
45static int px, py, sminx, sminy, smaxx, smaxy;
46
47static void curses_update(DisplayState *ds, int x, int y, int w, int h)
48{
49 chtype *line;
50
51 line = ((chtype *) screen) + y * width;
52 for (h += y; y < h; y ++, line += width)
53 mvwaddchnstr(screenpad, y, 0, line, width);
54
55 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
56 refresh();
57}
58
59static void curses_calc_pad(void)
60{
c21bbcfa 61 if (is_fixedsize_console()) {
4d3b6f6e
AZ
62 width = gwidth;
63 height = gheight;
64 } else {
65 width = COLS;
66 height = LINES;
67 }
68
69 if (screenpad)
70 delwin(screenpad);
71
72 clear();
73 refresh();
74
75 screenpad = newpad(height, width);
76
77 if (width > COLS) {
78 px = (width - COLS) / 2;
79 sminx = 0;
80 smaxx = COLS;
81 } else {
82 px = 0;
83 sminx = (COLS - width) / 2;
84 smaxx = sminx + width;
85 }
86
87 if (height > LINES) {
88 py = (height - LINES) / 2;
89 sminy = 0;
90 smaxy = LINES;
91 } else {
92 py = 0;
93 sminy = (LINES - height) / 2;
94 smaxy = sminy + height;
95 }
96}
97
a93a4a22 98static void curses_resize(DisplayState *ds, 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
110#ifndef _WIN32
b1314cf9 111#if defined(SIGWINCH) && defined(KEY_RESIZE)
4d3b6f6e
AZ
112static void curses_winch_handler(int signum)
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
121 /* terminal size changed */
122 if (ioctl(1, TIOCGWINSZ, &ws) == -1)
123 return;
124
125 resize_term(ws.ws_row, ws.ws_col);
126 curses_calc_pad();
127 invalidate = 1;
128
129 /* some systems require this */
130 signal(SIGWINCH, curses_winch_handler);
131}
132#endif
133#endif
134
135static void curses_cursor_position(DisplayState *ds, int x, int y)
136{
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"
4d3b6f6e 158
c227f099 159static kbd_layout_t *kbd_layout = NULL;
4d3b6f6e
AZ
160
161static void curses_refresh(DisplayState *ds)
162{
44bb61c8 163 int chr, nextchr, keysym, keycode, keycode_alt;
4d3b6f6e
AZ
164
165 if (invalidate) {
166 clear();
167 refresh();
168 curses_calc_pad();
4d3b6f6e
AZ
169 vga_hw_invalidate();
170 invalidate = 0;
171 }
172
173 vga_hw_text_update(screen);
174
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
b1314cf9 188#ifdef KEY_RESIZE
4d3b6f6e
AZ
189 /* this shouldn't occur when we use a custom SIGWINCH handler */
190 if (chr == KEY_RESIZE) {
191 clear();
192 refresh();
193 curses_calc_pad();
194 curses_update(ds, 0, 0, width, height);
4d3b6f6e
AZ
195 continue;
196 }
b1314cf9 197#endif
4d3b6f6e
AZ
198
199 keycode = curses2keycode[chr];
44bb61c8 200 keycode_alt = 0;
4d3b6f6e
AZ
201
202 /* alt key */
203 if (keycode == 1) {
204 nextchr = getch();
205
206 if (nextchr != ERR) {
44bb61c8
ST
207 chr = nextchr;
208 keycode_alt = ALT;
4d3b6f6e
AZ
209 keycode = curses2keycode[nextchr];
210 nextchr = ERR;
4d3b6f6e 211
44bb61c8
ST
212 if (keycode != -1) {
213 keycode |= ALT;
4d3b6f6e 214
44bb61c8
ST
215 /* 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);
4d3b6f6e 221
44bb61c8
ST
222 invalidate = 1;
223 continue;
224 }
4d3b6f6e
AZ
225 }
226 }
227 }
228
44bb61c8
ST
229 if (kbd_layout) {
230 keysym = -1;
231 if (chr < CURSES_KEYS)
232 keysym = curses2keysym[chr];
233
234 if (keysym == -1) {
d03703c8
ST
235 if (chr < ' ') {
236 keysym = chr + '@';
237 if (keysym >= 'A' && keysym <= 'Z')
238 keysym += 'a' - 'A';
239 keysym |= KEYSYM_CNTRL;
240 } else
44bb61c8
ST
241 keysym = chr;
242 }
4d3b6f6e 243
44bb61c8
ST
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;
4d3b6f6e
AZ
250 }
251
44bb61c8
ST
252 if (keycode == -1)
253 continue;
254
4d3b6f6e
AZ
255 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);
44bb61c8
ST
264 if (keycode & ALTGR) {
265 kbd_put_keycode(SCANCODE_EMUL0);
266 kbd_put_keycode(ALT_CODE);
267 }
4d3b6f6e
AZ
268 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);
44bb61c8
ST
274 if (keycode & ALTGR) {
275 kbd_put_keycode(SCANCODE_EMUL0);
276 kbd_put_keycode(ALT_CODE | KEY_RELEASE);
277 }
4d3b6f6e
AZ
278 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 {
44bb61c8 285 keysym = curses2qemu[chr];
4d3b6f6e
AZ
286 if (keysym == -1)
287 keysym = chr;
288
289 kbd_put_keysym(keysym);
290 }
291 }
292}
293
aaf12c25 294static void curses_atexit(void)
4d3b6f6e
AZ
295{
296 endwin();
297}
298
4d3b6f6e
AZ
299static 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{
4d3b6f6e
AZ
318#if defined(__APPLE__)
319 /* always use generic keymaps */
320 if (!keyboard_layout)
321 keyboard_layout = "en-us";
322#endif
323 if(keyboard_layout) {
0483755a 324 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
4d3b6f6e
AZ
325 if (!kbd_layout)
326 exit(1);
327 }
4d3b6f6e
AZ
328}
329
330void curses_display_init(DisplayState *ds, int full_screen)
331{
7d957bd8 332 DisplayChangeListener *dcl;
4d3b6f6e
AZ
333#ifndef _WIN32
334 if (!isatty(1)) {
335 fprintf(stderr, "We need a terminal output\n");
336 exit(1);
337 }
338#endif
339
340 curses_setup();
341 curses_keyboard_setup();
28695489 342 atexit(curses_atexit);
4d3b6f6e
AZ
343
344#ifndef _WIN32
b1314cf9 345#if defined(SIGWINCH) && defined(KEY_RESIZE)
4d3b6f6e
AZ
346 /* some curses implementations provide a handler, but we
347 * want to be sure this is handled regardless of the library */
348 signal(SIGWINCH, curses_winch_handler);
349#endif
350#endif
351
7267c094 352 dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
a93a4a22
GH
353 dcl->dpy_text_update = curses_update;
354 dcl->dpy_text_resize = curses_resize;
7d957bd8
AL
355 dcl->dpy_refresh = curses_refresh;
356 dcl->dpy_text_cursor = curses_cursor_position;
357 register_displaychangelistener(ds, dcl);
4d3b6f6e
AZ
358
359 invalidate = 1;
4d3b6f6e 360}