curses.c 9.9 KB

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