]>
Commit | Line | Data |
---|---|---|
4d3b6f6e AZ |
1 | /* |
2 | * QEMU curses/ncurses display driver | |
3 | * | |
4 | * Copyright (c) 2005 Andrzej Zaborowski <[email protected]> | |
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 | #ifdef SIGWINCH | |
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 | /* 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); | |
195 | ds->width = FONT_WIDTH * width; | |
196 | ds->height = FONT_HEIGHT * height; | |
197 | continue; | |
198 | } | |
199 | ||
200 | keycode = curses2keycode[chr]; | |
201 | if (keycode == -1) | |
202 | continue; | |
203 | ||
204 | /* alt key */ | |
205 | if (keycode == 1) { | |
206 | nextchr = getch(); | |
207 | ||
208 | if (nextchr != ERR) { | |
209 | keycode = curses2keycode[nextchr]; | |
210 | nextchr = ERR; | |
211 | if (keycode == -1) | |
212 | continue; | |
213 | ||
214 | keycode |= ALT; | |
215 | ||
216 | /* process keys reserved for qemu */ | |
217 | if (keycode >= QEMU_KEY_CONSOLE0 && | |
218 | keycode < QEMU_KEY_CONSOLE0 + 9) { | |
219 | erase(); | |
220 | wnoutrefresh(stdscr); | |
221 | console_select(keycode - QEMU_KEY_CONSOLE0); | |
222 | ||
223 | invalidate = 1; | |
224 | continue; | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | if (kbd_layout && !(keycode & GREY)) { | |
230 | keysym = keycode2keysym[keycode & KEY_MASK]; | |
231 | if (keysym == -1) | |
232 | keysym = chr; | |
233 | ||
234 | keycode &= ~KEY_MASK; | |
235 | keycode |= keysym2scancode(kbd_layout, keysym); | |
236 | } | |
237 | ||
238 | if (is_graphic_console()) { | |
239 | /* since terminals don't know about key press and release | |
240 | * events, we need to emit both for each key received */ | |
241 | if (keycode & SHIFT) | |
242 | kbd_put_keycode(SHIFT_CODE); | |
243 | if (keycode & CNTRL) | |
244 | kbd_put_keycode(CNTRL_CODE); | |
245 | if (keycode & ALT) | |
246 | kbd_put_keycode(ALT_CODE); | |
247 | if (keycode & GREY) | |
248 | kbd_put_keycode(GREY_CODE); | |
249 | kbd_put_keycode(keycode & KEY_MASK); | |
250 | if (keycode & GREY) | |
251 | kbd_put_keycode(GREY_CODE); | |
252 | kbd_put_keycode((keycode & KEY_MASK) | KEY_RELEASE); | |
253 | if (keycode & ALT) | |
254 | kbd_put_keycode(ALT_CODE | KEY_RELEASE); | |
255 | if (keycode & CNTRL) | |
256 | kbd_put_keycode(CNTRL_CODE | KEY_RELEASE); | |
257 | if (keycode & SHIFT) | |
258 | kbd_put_keycode(SHIFT_CODE | KEY_RELEASE); | |
259 | } else { | |
260 | keysym = curses2keysym[chr]; | |
261 | if (keysym == -1) | |
262 | keysym = chr; | |
263 | ||
264 | kbd_put_keysym(keysym); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | static void curses_cleanup(void *opaque) | |
270 | { | |
271 | endwin(); | |
272 | } | |
273 | ||
274 | static void curses_atexit(void) | |
275 | { | |
276 | curses_cleanup(NULL); | |
277 | } | |
278 | ||
279 | static void curses_setup(void) | |
280 | { | |
281 | int i, colour_default[8] = { | |
282 | COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN, | |
283 | COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE, | |
284 | }; | |
285 | ||
286 | /* input as raw as possible, let everything be interpreted | |
287 | * by the guest system */ | |
288 | initscr(); noecho(); intrflush(stdscr, FALSE); | |
289 | nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE); | |
290 | start_color(); raw(); scrollok(stdscr, FALSE); | |
291 | ||
292 | for (i = 0; i < 64; i ++) | |
293 | init_pair(i, colour_default[i & 7], colour_default[i >> 3]); | |
294 | } | |
295 | ||
296 | static void curses_keyboard_setup(void) | |
297 | { | |
298 | int i, keycode, keysym; | |
299 | ||
300 | #if defined(__APPLE__) | |
301 | /* always use generic keymaps */ | |
302 | if (!keyboard_layout) | |
303 | keyboard_layout = "en-us"; | |
304 | #endif | |
305 | if(keyboard_layout) { | |
306 | kbd_layout = init_keyboard_layout(keyboard_layout); | |
307 | if (!kbd_layout) | |
308 | exit(1); | |
309 | } | |
310 | ||
311 | for (i = 0; i < CURSES_KEYS; i ++) | |
312 | keycode2keysym[i] = -1; | |
313 | ||
314 | for (i = 0; i < CURSES_KEYS; i ++) { | |
315 | if (curses2keycode[i] == -1) | |
316 | continue; | |
317 | ||
318 | keycode = curses2keycode[i] & KEY_MASK; | |
319 | if (keycode2keysym[keycode] >= 0) | |
320 | continue; | |
321 | ||
322 | for (keysym = 0; keysym < CURSES_KEYS; keysym ++) | |
323 | if (curses2keycode[keysym] == keycode) { | |
324 | keycode2keysym[keycode] = keysym; | |
325 | break; | |
326 | } | |
327 | ||
328 | if (keysym >= CURSES_KEYS) | |
329 | keycode2keysym[keycode] = i; | |
330 | } | |
331 | } | |
332 | ||
333 | void curses_display_init(DisplayState *ds, int full_screen) | |
334 | { | |
335 | #ifndef _WIN32 | |
336 | if (!isatty(1)) { | |
337 | fprintf(stderr, "We need a terminal output\n"); | |
338 | exit(1); | |
339 | } | |
340 | #endif | |
341 | ||
342 | curses_setup(); | |
343 | curses_keyboard_setup(); | |
344 | atexit(curses_atexit); | |
345 | ||
346 | #ifndef _WIN32 | |
347 | signal(SIGINT, SIG_DFL); | |
348 | signal(SIGQUIT, SIG_DFL); | |
349 | #ifdef SIGWINCH | |
350 | /* some curses implementations provide a handler, but we | |
351 | * want to be sure this is handled regardless of the library */ | |
352 | signal(SIGWINCH, curses_winch_handler); | |
353 | #endif | |
354 | #endif | |
355 | ||
356 | ds->data = (void *) screen; | |
357 | ds->linesize = 0; | |
358 | ds->depth = 0; | |
359 | ds->width = 640; | |
360 | ds->height = 400; | |
361 | ds->dpy_update = curses_update; | |
362 | ds->dpy_resize = curses_resize; | |
363 | ds->dpy_refresh = curses_refresh; | |
364 | ds->dpy_text_cursor = curses_cursor_position; | |
365 | ||
366 | invalidate = 1; | |
367 | ||
368 | /* Standard VGA initial text mode dimensions */ | |
369 | curses_resize(ds, 80, 25); | |
370 | } |