2
0

curses.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #ifndef _WIN32
  26. #include <sys/ioctl.h>
  27. #include <termios.h>
  28. #endif
  29. #include "qemu-common.h"
  30. #include "ui/console.h"
  31. #include "ui/input.h"
  32. #include "sysemu/sysemu.h"
  33. #define FONT_HEIGHT 16
  34. #define FONT_WIDTH 8
  35. static DisplayChangeListener *dcl;
  36. static console_ch_t screen[160 * 100];
  37. static WINDOW *screenpad = NULL;
  38. static int width, height, gwidth, gheight, invalidate;
  39. static int px, py, sminx, sminy, smaxx, smaxy;
  40. static void curses_update(DisplayChangeListener *dcl,
  41. int x, int y, int w, int h)
  42. {
  43. chtype *line;
  44. line = ((chtype *) screen) + y * width;
  45. for (h += y; y < h; y ++, line += width)
  46. mvwaddchnstr(screenpad, y, 0, line, width);
  47. pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
  48. refresh();
  49. }
  50. static void curses_calc_pad(void)
  51. {
  52. if (qemu_console_is_fixedsize(NULL)) {
  53. width = gwidth;
  54. height = gheight;
  55. } else {
  56. width = COLS;
  57. height = LINES;
  58. }
  59. if (screenpad)
  60. delwin(screenpad);
  61. clear();
  62. refresh();
  63. screenpad = newpad(height, width);
  64. if (width > COLS) {
  65. px = (width - COLS) / 2;
  66. sminx = 0;
  67. smaxx = COLS;
  68. } else {
  69. px = 0;
  70. sminx = (COLS - width) / 2;
  71. smaxx = sminx + width;
  72. }
  73. if (height > LINES) {
  74. py = (height - LINES) / 2;
  75. sminy = 0;
  76. smaxy = LINES;
  77. } else {
  78. py = 0;
  79. sminy = (LINES - height) / 2;
  80. smaxy = sminy + height;
  81. }
  82. }
  83. static void curses_resize(DisplayChangeListener *dcl,
  84. int width, int height)
  85. {
  86. if (width == gwidth && height == gheight) {
  87. return;
  88. }
  89. gwidth = width;
  90. gheight = height;
  91. curses_calc_pad();
  92. }
  93. #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
  94. static volatile sig_atomic_t got_sigwinch;
  95. static void curses_winch_check(void)
  96. {
  97. struct winsize {
  98. unsigned short ws_row;
  99. unsigned short ws_col;
  100. unsigned short ws_xpixel; /* unused */
  101. unsigned short ws_ypixel; /* unused */
  102. } ws;
  103. if (!got_sigwinch) {
  104. return;
  105. }
  106. got_sigwinch = false;
  107. if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
  108. return;
  109. }
  110. resize_term(ws.ws_row, ws.ws_col);
  111. invalidate = 1;
  112. }
  113. static void curses_winch_handler(int signum)
  114. {
  115. got_sigwinch = true;
  116. }
  117. static void curses_winch_init(void)
  118. {
  119. struct sigaction old, winch = {
  120. .sa_handler = curses_winch_handler,
  121. };
  122. sigaction(SIGWINCH, &winch, &old);
  123. }
  124. #else
  125. static void curses_winch_check(void) {}
  126. static void curses_winch_init(void) {}
  127. #endif
  128. static void curses_cursor_position(DisplayChangeListener *dcl,
  129. int x, int y)
  130. {
  131. if (x >= 0) {
  132. x = sminx + x - px;
  133. y = sminy + y - py;
  134. if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
  135. move(y, x);
  136. curs_set(1);
  137. /* it seems that curs_set(1) must always be called before
  138. * curs_set(2) for the latter to have effect */
  139. if (!qemu_console_is_graphic(NULL)) {
  140. curs_set(2);
  141. }
  142. return;
  143. }
  144. }
  145. curs_set(0);
  146. }
  147. /* generic keyboard conversion */
  148. #include "curses_keys.h"
  149. static kbd_layout_t *kbd_layout = NULL;
  150. static void curses_refresh(DisplayChangeListener *dcl)
  151. {
  152. int chr, nextchr, keysym, keycode, keycode_alt;
  153. curses_winch_check();
  154. if (invalidate) {
  155. clear();
  156. refresh();
  157. curses_calc_pad();
  158. graphic_hw_invalidate(NULL);
  159. invalidate = 0;
  160. }
  161. graphic_hw_text_update(NULL, screen);
  162. nextchr = ERR;
  163. while (1) {
  164. /* while there are any pending key strokes to process */
  165. if (nextchr == ERR)
  166. chr = getch();
  167. else {
  168. chr = nextchr;
  169. nextchr = ERR;
  170. }
  171. if (chr == ERR)
  172. break;
  173. #ifdef KEY_RESIZE
  174. /* this shouldn't occur when we use a custom SIGWINCH handler */
  175. if (chr == KEY_RESIZE) {
  176. clear();
  177. refresh();
  178. curses_calc_pad();
  179. curses_update(dcl, 0, 0, width, height);
  180. continue;
  181. }
  182. #endif
  183. keycode = curses2keycode[chr];
  184. keycode_alt = 0;
  185. /* alt key */
  186. if (keycode == 1) {
  187. nextchr = getch();
  188. if (nextchr != ERR) {
  189. chr = nextchr;
  190. keycode_alt = ALT;
  191. keycode = curses2keycode[nextchr];
  192. nextchr = ERR;
  193. if (keycode != -1) {
  194. keycode |= ALT;
  195. /* process keys reserved for qemu */
  196. if (keycode >= QEMU_KEY_CONSOLE0 &&
  197. keycode < QEMU_KEY_CONSOLE0 + 9) {
  198. erase();
  199. wnoutrefresh(stdscr);
  200. console_select(keycode - QEMU_KEY_CONSOLE0);
  201. invalidate = 1;
  202. continue;
  203. }
  204. }
  205. }
  206. }
  207. if (kbd_layout) {
  208. keysym = -1;
  209. if (chr < CURSES_KEYS)
  210. keysym = curses2keysym[chr];
  211. if (keysym == -1) {
  212. if (chr < ' ') {
  213. keysym = chr + '@';
  214. if (keysym >= 'A' && keysym <= 'Z')
  215. keysym += 'a' - 'A';
  216. keysym |= KEYSYM_CNTRL;
  217. } else
  218. keysym = chr;
  219. }
  220. keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK);
  221. if (keycode == 0)
  222. continue;
  223. keycode |= (keysym & ~KEYSYM_MASK) >> 16;
  224. keycode |= keycode_alt;
  225. }
  226. if (keycode == -1)
  227. continue;
  228. if (qemu_console_is_graphic(NULL)) {
  229. /* since terminals don't know about key press and release
  230. * events, we need to emit both for each key received */
  231. if (keycode & SHIFT) {
  232. qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
  233. }
  234. if (keycode & CNTRL) {
  235. qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
  236. }
  237. if (keycode & ALT) {
  238. qemu_input_event_send_key_number(NULL, ALT_CODE, true);
  239. }
  240. if (keycode & ALTGR) {
  241. qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
  242. }
  243. qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
  244. qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
  245. if (keycode & ALTGR) {
  246. qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
  247. }
  248. if (keycode & ALT) {
  249. qemu_input_event_send_key_number(NULL, ALT_CODE, false);
  250. }
  251. if (keycode & CNTRL) {
  252. qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
  253. }
  254. if (keycode & SHIFT) {
  255. qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
  256. }
  257. } else {
  258. keysym = curses2qemu[chr];
  259. if (keysym == -1)
  260. keysym = chr;
  261. kbd_put_keysym(keysym);
  262. }
  263. }
  264. }
  265. static void curses_atexit(void)
  266. {
  267. endwin();
  268. }
  269. static void curses_setup(void)
  270. {
  271. int i, colour_default[8] = {
  272. COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
  273. COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE,
  274. };
  275. /* input as raw as possible, let everything be interpreted
  276. * by the guest system */
  277. initscr(); noecho(); intrflush(stdscr, FALSE);
  278. nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
  279. start_color(); raw(); scrollok(stdscr, FALSE);
  280. for (i = 0; i < 64; i ++)
  281. init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
  282. }
  283. static void curses_keyboard_setup(void)
  284. {
  285. #if defined(__APPLE__)
  286. /* always use generic keymaps */
  287. if (!keyboard_layout)
  288. keyboard_layout = "en-us";
  289. #endif
  290. if(keyboard_layout) {
  291. kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
  292. if (!kbd_layout)
  293. exit(1);
  294. }
  295. }
  296. static const DisplayChangeListenerOps dcl_ops = {
  297. .dpy_name = "curses",
  298. .dpy_text_update = curses_update,
  299. .dpy_text_resize = curses_resize,
  300. .dpy_refresh = curses_refresh,
  301. .dpy_text_cursor = curses_cursor_position,
  302. };
  303. void curses_display_init(DisplayState *ds, int full_screen)
  304. {
  305. #ifndef _WIN32
  306. if (!isatty(1)) {
  307. fprintf(stderr, "We need a terminal output\n");
  308. exit(1);
  309. }
  310. #endif
  311. curses_setup();
  312. curses_keyboard_setup();
  313. atexit(curses_atexit);
  314. curses_winch_init();
  315. dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
  316. dcl->ops = &dcl_ops;
  317. register_displaychangelistener(dcl);
  318. invalidate = 1;
  319. }