curses.c 9.4 KB

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