root / curses.c @ 8c5e95d8
History | View | Annotate | Download (9.5 kB)
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 |
*/
|
24 |
|
25 |
#include "qemu-common.h" |
26 |
#include "console.h" |
27 |
#include "sysemu.h" |
28 |
|
29 |
#include <curses.h> |
30 |
|
31 |
#ifndef _WIN32
|
32 |
#include <signal.h> |
33 |
#include <sys/ioctl.h> |
34 |
#include <termios.h> |
35 |
#endif
|
36 |
|
37 |
#define FONT_HEIGHT 16 |
38 |
#define FONT_WIDTH 8 |
39 |
|
40 |
static console_ch_t screen[160 * 100]; |
41 |
static WINDOW *screenpad = NULL; |
42 |
static int width, height, gwidth, gheight, invalidate; |
43 |
static int px, py, sminx, sminy, smaxx, smaxy; |
44 |
|
45 |
static void curses_update(DisplayState *ds, int x, int y, int w, int h) |
46 |
{ |
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 |
|
57 |
static void curses_calc_pad(void) |
58 |
{ |
59 |
if (is_graphic_console()) {
|
60 |
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 |
|
96 |
static void curses_resize(DisplayState *ds, int w, int h) |
97 |
{ |
98 |
if (w == gwidth && h == gheight)
|
99 |
return;
|
100 |
|
101 |
gwidth = w; |
102 |
gheight = h; |
103 |
|
104 |
curses_calc_pad(); |
105 |
} |
106 |
|
107 |
#ifndef _WIN32
|
108 |
#if defined(SIGWINCH) && defined(KEY_RESIZE)
|
109 |
static void curses_winch_handler(int signum) |
110 |
{ |
111 |
struct winsize {
|
112 |
unsigned short ws_row; |
113 |
unsigned short ws_col; |
114 |
unsigned short ws_xpixel; /* unused */ |
115 |
unsigned short ws_ypixel; /* unused */ |
116 |
} ws; |
117 |
|
118 |
/* terminal size changed */
|
119 |
if (ioctl(1, TIOCGWINSZ, &ws) == -1) |
120 |
return;
|
121 |
|
122 |
resize_term(ws.ws_row, ws.ws_col); |
123 |
curses_calc_pad(); |
124 |
invalidate = 1;
|
125 |
|
126 |
/* some systems require this */
|
127 |
signal(SIGWINCH, curses_winch_handler); |
128 |
} |
129 |
#endif
|
130 |
#endif
|
131 |
|
132 |
static void curses_cursor_position(DisplayState *ds, int x, int y) |
133 |
{ |
134 |
if (x >= 0) { |
135 |
x = sminx + x - px; |
136 |
y = sminy + y - py; |
137 |
|
138 |
if (x >= 0 && y >= 0 && x < COLS && y < LINES) { |
139 |
move(y, x); |
140 |
curs_set(1);
|
141 |
/* it seems that curs_set(1) must always be called before
|
142 |
* curs_set(2) for the latter to have effect */
|
143 |
if (!is_graphic_console())
|
144 |
curs_set(2);
|
145 |
return;
|
146 |
} |
147 |
} |
148 |
|
149 |
curs_set(0);
|
150 |
} |
151 |
|
152 |
/* generic keyboard conversion */
|
153 |
|
154 |
#include "curses_keys.h" |
155 |
#include "keymaps.c" |
156 |
|
157 |
static kbd_layout_t *kbd_layout = 0; |
158 |
static int keycode2keysym[CURSES_KEYS]; |
159 |
|
160 |
static void curses_refresh(DisplayState *ds) |
161 |
{ |
162 |
int chr, nextchr, keysym, keycode;
|
163 |
|
164 |
if (invalidate) {
|
165 |
clear(); |
166 |
refresh(); |
167 |
curses_calc_pad(); |
168 |
ds->width = FONT_WIDTH * width; |
169 |
ds->height = FONT_HEIGHT * height; |
170 |
vga_hw_invalidate(); |
171 |
invalidate = 0;
|
172 |
} |
173 |
|
174 |
vga_hw_text_update(screen); |
175 |
|
176 |
nextchr = ERR; |
177 |
while (1) { |
178 |
/* while there are any pending key strokes to process */
|
179 |
if (nextchr == ERR)
|
180 |
chr = getch(); |
181 |
else {
|
182 |
chr = nextchr; |
183 |
nextchr = ERR; |
184 |
} |
185 |
|
186 |
if (chr == ERR)
|
187 |
break;
|
188 |
|
189 |
#ifdef KEY_RESIZE
|
190 |
/* this shouldn't occur when we use a custom SIGWINCH handler */
|
191 |
if (chr == KEY_RESIZE) {
|
192 |
clear(); |
193 |
refresh(); |
194 |
curses_calc_pad(); |
195 |
curses_update(ds, 0, 0, width, height); |
196 |
ds->width = FONT_WIDTH * width; |
197 |
ds->height = FONT_HEIGHT * height; |
198 |
continue;
|
199 |
} |
200 |
#endif
|
201 |
|
202 |
keycode = curses2keycode[chr]; |
203 |
if (keycode == -1) |
204 |
continue;
|
205 |
|
206 |
/* alt key */
|
207 |
if (keycode == 1) { |
208 |
nextchr = getch(); |
209 |
|
210 |
if (nextchr != ERR) {
|
211 |
keycode = curses2keycode[nextchr]; |
212 |
nextchr = ERR; |
213 |
if (keycode == -1) |
214 |
continue;
|
215 |
|
216 |
keycode |= ALT; |
217 |
|
218 |
/* process keys reserved for qemu */
|
219 |
if (keycode >= QEMU_KEY_CONSOLE0 &&
|
220 |
keycode < QEMU_KEY_CONSOLE0 + 9) {
|
221 |
erase(); |
222 |
wnoutrefresh(stdscr); |
223 |
console_select(keycode - QEMU_KEY_CONSOLE0); |
224 |
|
225 |
invalidate = 1;
|
226 |
continue;
|
227 |
} |
228 |
} |
229 |
} |
230 |
|
231 |
if (kbd_layout && !(keycode & GREY)) {
|
232 |
keysym = keycode2keysym[keycode & KEY_MASK]; |
233 |
if (keysym == -1) |
234 |
keysym = chr; |
235 |
|
236 |
keycode &= ~KEY_MASK; |
237 |
keycode |= keysym2scancode(kbd_layout, keysym); |
238 |
} |
239 |
|
240 |
if (is_graphic_console()) {
|
241 |
/* since terminals don't know about key press and release
|
242 |
* events, we need to emit both for each key received */
|
243 |
if (keycode & SHIFT)
|
244 |
kbd_put_keycode(SHIFT_CODE); |
245 |
if (keycode & CNTRL)
|
246 |
kbd_put_keycode(CNTRL_CODE); |
247 |
if (keycode & ALT)
|
248 |
kbd_put_keycode(ALT_CODE); |
249 |
if (keycode & GREY)
|
250 |
kbd_put_keycode(GREY_CODE); |
251 |
kbd_put_keycode(keycode & KEY_MASK); |
252 |
if (keycode & GREY)
|
253 |
kbd_put_keycode(GREY_CODE); |
254 |
kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE); |
255 |
if (keycode & ALT)
|
256 |
kbd_put_keycode(ALT_CODE | KEY_RELEASE); |
257 |
if (keycode & CNTRL)
|
258 |
kbd_put_keycode(CNTRL_CODE | KEY_RELEASE); |
259 |
if (keycode & SHIFT)
|
260 |
kbd_put_keycode(SHIFT_CODE | KEY_RELEASE); |
261 |
} else {
|
262 |
keysym = curses2keysym[chr]; |
263 |
if (keysym == -1) |
264 |
keysym = chr; |
265 |
|
266 |
kbd_put_keysym(keysym); |
267 |
} |
268 |
} |
269 |
} |
270 |
|
271 |
static void curses_cleanup(void *opaque) |
272 |
{ |
273 |
endwin(); |
274 |
} |
275 |
|
276 |
static void curses_atexit(void) |
277 |
{ |
278 |
curses_cleanup(NULL);
|
279 |
} |
280 |
|
281 |
static void curses_setup(void) |
282 |
{ |
283 |
int i, colour_default[8] = { |
284 |
COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN, |
285 |
COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE, |
286 |
}; |
287 |
|
288 |
/* input as raw as possible, let everything be interpreted
|
289 |
* by the guest system */
|
290 |
initscr(); noecho(); intrflush(stdscr, FALSE); |
291 |
nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); |
292 |
start_color(); raw(); scrollok(stdscr, FALSE); |
293 |
|
294 |
for (i = 0; i < 64; i ++) |
295 |
init_pair(i, colour_default[i & 7], colour_default[i >> 3]); |
296 |
} |
297 |
|
298 |
static void curses_keyboard_setup(void) |
299 |
{ |
300 |
int i, keycode, keysym;
|
301 |
|
302 |
#if defined(__APPLE__)
|
303 |
/* always use generic keymaps */
|
304 |
if (!keyboard_layout)
|
305 |
keyboard_layout = "en-us";
|
306 |
#endif
|
307 |
if(keyboard_layout) {
|
308 |
kbd_layout = init_keyboard_layout(keyboard_layout); |
309 |
if (!kbd_layout)
|
310 |
exit(1);
|
311 |
} |
312 |
|
313 |
for (i = 0; i < CURSES_KEYS; i ++) |
314 |
keycode2keysym[i] = -1;
|
315 |
|
316 |
for (i = 0; i < CURSES_KEYS; i ++) { |
317 |
if (curses2keycode[i] == -1) |
318 |
continue;
|
319 |
|
320 |
keycode = curses2keycode[i] & KEY_MASK; |
321 |
if (keycode2keysym[keycode] >= 0) |
322 |
continue;
|
323 |
|
324 |
for (keysym = 0; keysym < CURSES_KEYS; keysym ++) |
325 |
if (curses2keycode[keysym] == keycode) {
|
326 |
keycode2keysym[keycode] = keysym; |
327 |
break;
|
328 |
} |
329 |
|
330 |
if (keysym >= CURSES_KEYS)
|
331 |
keycode2keysym[keycode] = i; |
332 |
} |
333 |
} |
334 |
|
335 |
void curses_display_init(DisplayState *ds, int full_screen) |
336 |
{ |
337 |
#ifndef _WIN32
|
338 |
if (!isatty(1)) { |
339 |
fprintf(stderr, "We need a terminal output\n");
|
340 |
exit(1);
|
341 |
} |
342 |
#endif
|
343 |
|
344 |
curses_setup(); |
345 |
curses_keyboard_setup(); |
346 |
atexit(curses_atexit); |
347 |
|
348 |
#ifndef _WIN32
|
349 |
signal(SIGINT, SIG_DFL); |
350 |
signal(SIGQUIT, SIG_DFL); |
351 |
#if defined(SIGWINCH) && defined(KEY_RESIZE)
|
352 |
/* some curses implementations provide a handler, but we
|
353 |
* want to be sure this is handled regardless of the library */
|
354 |
signal(SIGWINCH, curses_winch_handler); |
355 |
#endif
|
356 |
#endif
|
357 |
|
358 |
ds->data = (void *) screen;
|
359 |
ds->linesize = 0;
|
360 |
ds->depth = 0;
|
361 |
ds->width = 640;
|
362 |
ds->height = 400;
|
363 |
ds->dpy_update = curses_update; |
364 |
ds->dpy_resize = curses_resize; |
365 |
ds->dpy_refresh = curses_refresh; |
366 |
ds->dpy_text_cursor = curses_cursor_position; |
367 |
|
368 |
invalidate = 1;
|
369 |
|
370 |
/* Standard VGA initial text mode dimensions */
|
371 |
curses_resize(ds, 80, 25); |
372 |
} |