1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.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 "ui/input.h"
34#include "sysemu/sysemu.h"
35
36#define FONT_HEIGHT 16
37#define FONT_WIDTH 8
38
39static DisplayChangeListener *dcl;
40static console_ch_t screen[160 * 100];
41static WINDOW *screenpad = NULL;
42static int width, height, gwidth, gheight, invalidate;
43static int px, py, sminx, sminy, smaxx, smaxy;
44
45chtype vga_to_curses[256];
46
47static void curses_update(DisplayChangeListener *dcl,
48 int x, int y, int w, int h)
49{
50 chtype *line;
51
52 line = ((chtype *) screen) + y * width;
53 for (h += y; y < h; y ++, line += width)
54 mvwaddchnstr(screenpad, y, 0, line, width);
55
56 pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
57 refresh();
58}
59
60static void curses_calc_pad(void)
61{
62 if (qemu_console_is_fixedsize(NULL)) {
63 width = gwidth;
64 height = gheight;
65 } else {
66 width = COLS;
67 height = LINES;
68 }
69
70 if (screenpad)
71 delwin(screenpad);
72
73 clear();
74 refresh();
75
76 screenpad = newpad(height, width);
77
78 if (width > COLS) {
79 px = (width - COLS) / 2;
80 sminx = 0;
81 smaxx = COLS;
82 } else {
83 px = 0;
84 sminx = (COLS - width) / 2;
85 smaxx = sminx + width;
86 }
87
88 if (height > LINES) {
89 py = (height - LINES) / 2;
90 sminy = 0;
91 smaxy = LINES;
92 } else {
93 py = 0;
94 sminy = (LINES - height) / 2;
95 smaxy = sminy + height;
96 }
97}
98
99static void curses_resize(DisplayChangeListener *dcl,
100 int width, int height)
101{
102 if (width == gwidth && height == gheight) {
103 return;
104 }
105
106 gwidth = width;
107 gheight = height;
108
109 curses_calc_pad();
110}
111
112#if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
113static volatile sig_atomic_t got_sigwinch;
114static void curses_winch_check(void)
115{
116 struct winsize {
117 unsigned short ws_row;
118 unsigned short ws_col;
119 unsigned short ws_xpixel;
120 unsigned short ws_ypixel;
121 } ws;
122
123 if (!got_sigwinch) {
124 return;
125 }
126 got_sigwinch = false;
127
128 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
129 return;
130 }
131
132 resize_term(ws.ws_row, ws.ws_col);
133 invalidate = 1;
134}
135
136static void curses_winch_handler(int signum)
137{
138 got_sigwinch = true;
139}
140
141static void curses_winch_init(void)
142{
143 struct sigaction old, winch = {
144 .sa_handler = curses_winch_handler,
145 };
146 sigaction(SIGWINCH, &winch, &old);
147}
148#else
149static void curses_winch_check(void) {}
150static void curses_winch_init(void) {}
151#endif
152
153static void curses_cursor_position(DisplayChangeListener *dcl,
154 int x, int y)
155{
156 if (x >= 0) {
157 x = sminx + x - px;
158 y = sminy + y - py;
159
160 if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
161 move(y, x);
162 curs_set(1);
163
164
165 if (!qemu_console_is_graphic(NULL)) {
166 curs_set(2);
167 }
168 return;
169 }
170 }
171
172 curs_set(0);
173}
174
175
176
177#include "curses_keys.h"
178
179static kbd_layout_t *kbd_layout = NULL;
180
181static void curses_refresh(DisplayChangeListener *dcl)
182{
183 int chr, keysym, keycode, keycode_alt;
184
185 curses_winch_check();
186
187 if (invalidate) {
188 clear();
189 refresh();
190 curses_calc_pad();
191 graphic_hw_invalidate(NULL);
192 invalidate = 0;
193 }
194
195 graphic_hw_text_update(NULL, screen);
196
197 while (1) {
198
199 chr = getch();
200
201 if (chr == ERR)
202 break;
203
204#ifdef KEY_RESIZE
205
206 if (chr == KEY_RESIZE) {
207 clear();
208 refresh();
209 curses_calc_pad();
210 curses_update(dcl, 0, 0, width, height);
211 continue;
212 }
213#endif
214
215 keycode = curses2keycode[chr];
216 keycode_alt = 0;
217
218
219 if (keycode == 1) {
220 int nextchr = getch();
221
222 if (nextchr != ERR) {
223 chr = nextchr;
224 keycode_alt = ALT;
225 keycode = curses2keycode[chr];
226
227 if (keycode != -1) {
228 keycode |= ALT;
229
230
231 if (keycode >= QEMU_KEY_CONSOLE0 &&
232 keycode < QEMU_KEY_CONSOLE0 + 9) {
233 erase();
234 wnoutrefresh(stdscr);
235 console_select(keycode - QEMU_KEY_CONSOLE0);
236
237 invalidate = 1;
238 continue;
239 }
240 }
241 }
242 }
243
244 if (kbd_layout) {
245 keysym = -1;
246 if (chr < CURSES_KEYS)
247 keysym = curses2keysym[chr];
248
249 if (keysym == -1) {
250 if (chr < ' ') {
251 keysym = chr + '@';
252 if (keysym >= 'A' && keysym <= 'Z')
253 keysym += 'a' - 'A';
254 keysym |= KEYSYM_CNTRL;
255 } else
256 keysym = chr;
257 }
258
259 keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
260 if (keycode == 0)
261 continue;
262
263 keycode |= (keysym & ~KEYSYM_MASK) >> 16;
264 keycode |= keycode_alt;
265 }
266
267 if (keycode == -1)
268 continue;
269
270 if (qemu_console_is_graphic(NULL)) {
271
272
273 if (keycode & SHIFT) {
274 qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
275 qemu_input_event_send_key_delay(0);
276 }
277 if (keycode & CNTRL) {
278 qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
279 qemu_input_event_send_key_delay(0);
280 }
281 if (keycode & ALT) {
282 qemu_input_event_send_key_number(NULL, ALT_CODE, true);
283 qemu_input_event_send_key_delay(0);
284 }
285 if (keycode & ALTGR) {
286 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
287 qemu_input_event_send_key_delay(0);
288 }
289
290 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
291 qemu_input_event_send_key_delay(0);
292 qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
293 qemu_input_event_send_key_delay(0);
294
295 if (keycode & ALTGR) {
296 qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
297 qemu_input_event_send_key_delay(0);
298 }
299 if (keycode & ALT) {
300 qemu_input_event_send_key_number(NULL, ALT_CODE, false);
301 qemu_input_event_send_key_delay(0);
302 }
303 if (keycode & CNTRL) {
304 qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
305 qemu_input_event_send_key_delay(0);
306 }
307 if (keycode & SHIFT) {
308 qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
309 qemu_input_event_send_key_delay(0);
310 }
311 } else {
312 keysym = -1;
313 if (chr < CURSES_KEYS) {
314 keysym = curses2qemu[chr];
315 }
316 if (keysym == -1)
317 keysym = chr;
318
319 kbd_put_keysym(keysym);
320 }
321 }
322}
323
324static void curses_atexit(void)
325{
326 endwin();
327}
328
329static void curses_setup(void)
330{
331 int i, colour_default[8] = {
332 [QEMU_COLOR_BLACK] = COLOR_BLACK,
333 [QEMU_COLOR_BLUE] = COLOR_BLUE,
334 [QEMU_COLOR_GREEN] = COLOR_GREEN,
335 [QEMU_COLOR_CYAN] = COLOR_CYAN,
336 [QEMU_COLOR_RED] = COLOR_RED,
337 [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
338 [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
339 [QEMU_COLOR_WHITE] = COLOR_WHITE,
340 };
341
342
343
344 initscr(); noecho(); intrflush(stdscr, FALSE);
345 nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
346 start_color(); raw(); scrollok(stdscr, FALSE);
347
348
349 for (i = 0; i < 64; i++) {
350 init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
351 }
352
353 for (i = 64; i < COLOR_PAIRS; i++) {
354 init_pair(i, COLOR_WHITE, COLOR_BLACK);
355 }
356
357
358
359
360
361#if 0
362
363 ACS_S1;
364 ACS_S3;
365 ACS_S7;
366 ACS_S9;
367#endif
368
369 vga_to_curses['\0'] = ' ';
370 vga_to_curses[0x04] = ACS_DIAMOND;
371 vga_to_curses[0x18] = ACS_UARROW;
372 vga_to_curses[0x19] = ACS_DARROW;
373 vga_to_curses[0x1a] = ACS_RARROW;
374 vga_to_curses[0x1b] = ACS_LARROW;
375 vga_to_curses[0x9c] = ACS_STERLING;
376 vga_to_curses[0xb0] = ACS_BOARD;
377 vga_to_curses[0xb1] = ACS_CKBOARD;
378 vga_to_curses[0xb3] = ACS_VLINE;
379 vga_to_curses[0xb4] = ACS_RTEE;
380 vga_to_curses[0xbf] = ACS_URCORNER;
381 vga_to_curses[0xc0] = ACS_LLCORNER;
382 vga_to_curses[0xc1] = ACS_BTEE;
383 vga_to_curses[0xc2] = ACS_TTEE;
384 vga_to_curses[0xc3] = ACS_LTEE;
385 vga_to_curses[0xc4] = ACS_HLINE;
386 vga_to_curses[0xc5] = ACS_PLUS;
387 vga_to_curses[0xce] = ACS_LANTERN;
388 vga_to_curses[0xd8] = ACS_NEQUAL;
389 vga_to_curses[0xd9] = ACS_LRCORNER;
390 vga_to_curses[0xda] = ACS_ULCORNER;
391 vga_to_curses[0xdb] = ACS_BLOCK;
392 vga_to_curses[0xe3] = ACS_PI;
393 vga_to_curses[0xf1] = ACS_PLMINUS;
394 vga_to_curses[0xf2] = ACS_GEQUAL;
395 vga_to_curses[0xf3] = ACS_LEQUAL;
396 vga_to_curses[0xf8] = ACS_DEGREE;
397 vga_to_curses[0xfe] = ACS_BULLET;
398}
399
400static void curses_keyboard_setup(void)
401{
402#if defined(__APPLE__)
403
404 if (!keyboard_layout)
405 keyboard_layout = "en-us";
406#endif
407 if(keyboard_layout) {
408 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
409 if (!kbd_layout)
410 exit(1);
411 }
412}
413
414static const DisplayChangeListenerOps dcl_ops = {
415 .dpy_name = "curses",
416 .dpy_text_update = curses_update,
417 .dpy_text_resize = curses_resize,
418 .dpy_refresh = curses_refresh,
419 .dpy_text_cursor = curses_cursor_position,
420};
421
422void curses_display_init(DisplayState *ds, int full_screen)
423{
424#ifndef _WIN32
425 if (!isatty(1)) {
426 fprintf(stderr, "We need a terminal output\n");
427 exit(1);
428 }
429#endif
430
431 curses_setup();
432 curses_keyboard_setup();
433 atexit(curses_atexit);
434
435 curses_winch_init();
436
437 dcl = g_new0(DisplayChangeListener, 1);
438 dcl->ops = &dcl_ops;
439 register_displaychangelistener(dcl);
440
441 invalidate = 1;
442}
443