2
0

curses.c 9.8 KB

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