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