2
0

curses.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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/osdep.h"
  25. #ifndef _WIN32
  26. #include <sys/ioctl.h>
  27. #include <termios.h>
  28. #endif
  29. #include <locale.h>
  30. #include <wchar.h>
  31. #include <iconv.h>
  32. #include "qapi/error.h"
  33. #include "qemu/module.h"
  34. #include "ui/console.h"
  35. #include "ui/input.h"
  36. #include "sysemu/sysemu.h"
  37. /* KEY_EVENT is defined in wincon.h and in curses.h. Avoid redefinition. */
  38. #undef KEY_EVENT
  39. #include <curses.h>
  40. #undef KEY_EVENT
  41. #define FONT_HEIGHT 16
  42. #define FONT_WIDTH 8
  43. enum maybe_keycode {
  44. CURSES_KEYCODE,
  45. CURSES_CHAR,
  46. CURSES_CHAR_OR_KEYCODE,
  47. };
  48. static DisplayChangeListener *dcl;
  49. static console_ch_t *screen;
  50. static WINDOW *screenpad = NULL;
  51. static int width, height, gwidth, gheight, invalidate;
  52. static int px, py, sminx, sminy, smaxx, smaxy;
  53. static const char *font_charset = "CP437";
  54. static cchar_t *vga_to_curses;
  55. static void curses_update(DisplayChangeListener *dcl,
  56. int x, int y, int w, int h)
  57. {
  58. console_ch_t *line;
  59. cchar_t curses_line[width];
  60. wchar_t wch[CCHARW_MAX];
  61. attr_t attrs;
  62. short colors;
  63. int ret;
  64. line = screen + y * width;
  65. for (h += y; y < h; y ++, line += width) {
  66. for (x = 0; x < width; x++) {
  67. chtype ch = line[x] & A_CHARTEXT;
  68. chtype at = line[x] & A_ATTRIBUTES;
  69. short color_pair = PAIR_NUMBER(line[x]);
  70. ret = getcchar(&vga_to_curses[ch], wch, &attrs, &colors, NULL);
  71. if (ret == ERR || wch[0] == 0) {
  72. wch[0] = ch;
  73. wch[1] = 0;
  74. }
  75. setcchar(&curses_line[x], wch, at, color_pair, NULL);
  76. }
  77. mvwadd_wchnstr(screenpad, y, 0, curses_line, width);
  78. }
  79. pnoutrefresh(screenpad, py, px, sminy, sminx, smaxy - 1, smaxx - 1);
  80. refresh();
  81. }
  82. static void curses_calc_pad(void)
  83. {
  84. if (qemu_console_is_fixedsize(NULL)) {
  85. width = gwidth;
  86. height = gheight;
  87. } else {
  88. width = COLS;
  89. height = LINES;
  90. }
  91. if (screenpad)
  92. delwin(screenpad);
  93. clear();
  94. refresh();
  95. screenpad = newpad(height, width);
  96. if (width > COLS) {
  97. px = (width - COLS) / 2;
  98. sminx = 0;
  99. smaxx = COLS;
  100. } else {
  101. px = 0;
  102. sminx = (COLS - width) / 2;
  103. smaxx = sminx + width;
  104. }
  105. if (height > LINES) {
  106. py = (height - LINES) / 2;
  107. sminy = 0;
  108. smaxy = LINES;
  109. } else {
  110. py = 0;
  111. sminy = (LINES - height) / 2;
  112. smaxy = sminy + height;
  113. }
  114. }
  115. static void curses_resize(DisplayChangeListener *dcl,
  116. int width, int height)
  117. {
  118. if (width == gwidth && height == gheight) {
  119. return;
  120. }
  121. gwidth = width;
  122. gheight = height;
  123. curses_calc_pad();
  124. }
  125. #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
  126. static volatile sig_atomic_t got_sigwinch;
  127. static void curses_winch_check(void)
  128. {
  129. struct winsize {
  130. unsigned short ws_row;
  131. unsigned short ws_col;
  132. unsigned short ws_xpixel; /* unused */
  133. unsigned short ws_ypixel; /* unused */
  134. } ws;
  135. if (!got_sigwinch) {
  136. return;
  137. }
  138. got_sigwinch = false;
  139. if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
  140. return;
  141. }
  142. resize_term(ws.ws_row, ws.ws_col);
  143. invalidate = 1;
  144. }
  145. static void curses_winch_handler(int signum)
  146. {
  147. got_sigwinch = true;
  148. }
  149. static void curses_winch_init(void)
  150. {
  151. struct sigaction old, winch = {
  152. .sa_handler = curses_winch_handler,
  153. };
  154. sigaction(SIGWINCH, &winch, &old);
  155. }
  156. #else
  157. static void curses_winch_check(void) {}
  158. static void curses_winch_init(void) {}
  159. #endif
  160. static void curses_cursor_position(DisplayChangeListener *dcl,
  161. int x, int y)
  162. {
  163. if (x >= 0) {
  164. x = sminx + x - px;
  165. y = sminy + y - py;
  166. if (x >= 0 && y >= 0 && x < COLS && y < LINES) {
  167. move(y, x);
  168. curs_set(1);
  169. /* it seems that curs_set(1) must always be called before
  170. * curs_set(2) for the latter to have effect */
  171. if (!qemu_console_is_graphic(NULL)) {
  172. curs_set(2);
  173. }
  174. return;
  175. }
  176. }
  177. curs_set(0);
  178. }
  179. /* generic keyboard conversion */
  180. #include "curses_keys.h"
  181. static kbd_layout_t *kbd_layout = NULL;
  182. static wint_t console_getch(enum maybe_keycode *maybe_keycode)
  183. {
  184. wint_t ret;
  185. switch (get_wch(&ret)) {
  186. case KEY_CODE_YES:
  187. *maybe_keycode = CURSES_KEYCODE;
  188. break;
  189. case OK:
  190. *maybe_keycode = CURSES_CHAR;
  191. break;
  192. case ERR:
  193. ret = -1;
  194. break;
  195. default:
  196. abort();
  197. }
  198. return ret;
  199. }
  200. static int curses2foo(const int _curses2foo[], const int _curseskey2foo[],
  201. int chr, enum maybe_keycode maybe_keycode)
  202. {
  203. int ret = -1;
  204. if (maybe_keycode == CURSES_CHAR) {
  205. if (chr < CURSES_CHARS) {
  206. ret = _curses2foo[chr];
  207. }
  208. } else {
  209. if (chr < CURSES_KEYS) {
  210. ret = _curseskey2foo[chr];
  211. }
  212. if (ret == -1 && maybe_keycode == CURSES_CHAR_OR_KEYCODE &&
  213. chr < CURSES_CHARS) {
  214. ret = _curses2foo[chr];
  215. }
  216. }
  217. return ret;
  218. }
  219. #define curses2keycode(chr, maybe_keycode) \
  220. curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
  221. #define curses2keysym(chr, maybe_keycode) \
  222. curses2foo(_curses2keysym, _curseskey2keysym, chr, maybe_keycode)
  223. #define curses2qemu(chr, maybe_keycode) \
  224. curses2foo(_curses2qemu, _curseskey2qemu, chr, maybe_keycode)
  225. static void curses_refresh(DisplayChangeListener *dcl)
  226. {
  227. int chr, keysym, keycode, keycode_alt;
  228. enum maybe_keycode maybe_keycode = CURSES_KEYCODE;
  229. curses_winch_check();
  230. if (invalidate) {
  231. clear();
  232. refresh();
  233. curses_calc_pad();
  234. graphic_hw_invalidate(NULL);
  235. invalidate = 0;
  236. }
  237. graphic_hw_text_update(NULL, screen);
  238. while (1) {
  239. /* while there are any pending key strokes to process */
  240. chr = console_getch(&maybe_keycode);
  241. if (chr == -1)
  242. break;
  243. #ifdef KEY_RESIZE
  244. /* this shouldn't occur when we use a custom SIGWINCH handler */
  245. if (maybe_keycode != CURSES_CHAR && chr == KEY_RESIZE) {
  246. clear();
  247. refresh();
  248. curses_calc_pad();
  249. curses_update(dcl, 0, 0, width, height);
  250. continue;
  251. }
  252. #endif
  253. keycode = curses2keycode(chr, maybe_keycode);
  254. keycode_alt = 0;
  255. /* alt or esc key */
  256. if (keycode == 1) {
  257. enum maybe_keycode next_maybe_keycode = CURSES_KEYCODE;
  258. int nextchr = console_getch(&next_maybe_keycode);
  259. if (nextchr != -1) {
  260. chr = nextchr;
  261. maybe_keycode = next_maybe_keycode;
  262. keycode_alt = ALT;
  263. keycode = curses2keycode(chr, maybe_keycode);
  264. if (keycode != -1) {
  265. keycode |= ALT;
  266. /* process keys reserved for qemu */
  267. if (keycode >= QEMU_KEY_CONSOLE0 &&
  268. keycode < QEMU_KEY_CONSOLE0 + 9) {
  269. erase();
  270. wnoutrefresh(stdscr);
  271. console_select(keycode - QEMU_KEY_CONSOLE0);
  272. invalidate = 1;
  273. continue;
  274. }
  275. }
  276. }
  277. }
  278. if (kbd_layout) {
  279. keysym = curses2keysym(chr, maybe_keycode);
  280. if (keysym == -1) {
  281. if (chr < ' ') {
  282. keysym = chr + '@';
  283. if (keysym >= 'A' && keysym <= 'Z')
  284. keysym += 'a' - 'A';
  285. keysym |= KEYSYM_CNTRL;
  286. } else
  287. keysym = chr;
  288. }
  289. keycode = keysym2scancode(kbd_layout, keysym & KEYSYM_MASK,
  290. NULL, false);
  291. if (keycode == 0)
  292. continue;
  293. keycode |= (keysym & ~KEYSYM_MASK) >> 16;
  294. keycode |= keycode_alt;
  295. }
  296. if (keycode == -1)
  297. continue;
  298. if (qemu_console_is_graphic(NULL)) {
  299. /* since terminals don't know about key press and release
  300. * events, we need to emit both for each key received */
  301. if (keycode & SHIFT) {
  302. qemu_input_event_send_key_number(NULL, SHIFT_CODE, true);
  303. qemu_input_event_send_key_delay(0);
  304. }
  305. if (keycode & CNTRL) {
  306. qemu_input_event_send_key_number(NULL, CNTRL_CODE, true);
  307. qemu_input_event_send_key_delay(0);
  308. }
  309. if (keycode & ALT) {
  310. qemu_input_event_send_key_number(NULL, ALT_CODE, true);
  311. qemu_input_event_send_key_delay(0);
  312. }
  313. if (keycode & ALTGR) {
  314. qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, true);
  315. qemu_input_event_send_key_delay(0);
  316. }
  317. qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, true);
  318. qemu_input_event_send_key_delay(0);
  319. qemu_input_event_send_key_number(NULL, keycode & KEY_MASK, false);
  320. qemu_input_event_send_key_delay(0);
  321. if (keycode & ALTGR) {
  322. qemu_input_event_send_key_number(NULL, GREY | ALT_CODE, false);
  323. qemu_input_event_send_key_delay(0);
  324. }
  325. if (keycode & ALT) {
  326. qemu_input_event_send_key_number(NULL, ALT_CODE, false);
  327. qemu_input_event_send_key_delay(0);
  328. }
  329. if (keycode & CNTRL) {
  330. qemu_input_event_send_key_number(NULL, CNTRL_CODE, false);
  331. qemu_input_event_send_key_delay(0);
  332. }
  333. if (keycode & SHIFT) {
  334. qemu_input_event_send_key_number(NULL, SHIFT_CODE, false);
  335. qemu_input_event_send_key_delay(0);
  336. }
  337. } else {
  338. keysym = curses2qemu(chr, maybe_keycode);
  339. if (keysym == -1)
  340. keysym = chr;
  341. kbd_put_keysym(keysym);
  342. }
  343. }
  344. }
  345. static void curses_atexit(void)
  346. {
  347. endwin();
  348. g_free(vga_to_curses);
  349. g_free(screen);
  350. }
  351. /*
  352. * In the following:
  353. * - fch is the font glyph number
  354. * - uch is the unicode value
  355. * - wch is the wchar_t value (may not be unicode, e.g. on BSD/solaris)
  356. * - mbch is the native local-dependent multibyte representation
  357. */
  358. /* Setup wchar glyph for one UCS-2 char */
  359. static void convert_ucs(unsigned char fch, uint16_t uch, iconv_t conv)
  360. {
  361. char mbch[MB_LEN_MAX];
  362. wchar_t wch[2];
  363. char *puch, *pmbch;
  364. size_t such, smbch;
  365. mbstate_t ps;
  366. puch = (char *) &uch;
  367. pmbch = (char *) mbch;
  368. such = sizeof(uch);
  369. smbch = sizeof(mbch);
  370. if (iconv(conv, &puch, &such, &pmbch, &smbch) == (size_t) -1) {
  371. fprintf(stderr, "Could not convert 0x%04x "
  372. "from UCS-2 to a multibyte character: %s\n",
  373. uch, strerror(errno));
  374. return;
  375. }
  376. memset(&ps, 0, sizeof(ps));
  377. if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
  378. fprintf(stderr, "Could not convert 0x%04x "
  379. "from a multibyte character to wchar_t: %s\n",
  380. uch, strerror(errno));
  381. return;
  382. }
  383. wch[1] = 0;
  384. setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
  385. }
  386. /* Setup wchar glyph for one font character */
  387. static void convert_font(unsigned char fch, iconv_t conv)
  388. {
  389. char mbch[MB_LEN_MAX];
  390. wchar_t wch[2];
  391. char *pfch, *pmbch;
  392. size_t sfch, smbch;
  393. mbstate_t ps;
  394. pfch = (char *) &fch;
  395. pmbch = (char *) &mbch;
  396. sfch = sizeof(fch);
  397. smbch = sizeof(mbch);
  398. if (iconv(conv, &pfch, &sfch, &pmbch, &smbch) == (size_t) -1) {
  399. fprintf(stderr, "Could not convert font glyph 0x%02x "
  400. "from %s to a multibyte character: %s\n",
  401. fch, font_charset, strerror(errno));
  402. return;
  403. }
  404. memset(&ps, 0, sizeof(ps));
  405. if (mbrtowc(&wch[0], mbch, sizeof(mbch) - smbch, &ps) == -1) {
  406. fprintf(stderr, "Could not convert font glyph 0x%02x "
  407. "from a multibyte character to wchar_t: %s\n",
  408. fch, strerror(errno));
  409. return;
  410. }
  411. wch[1] = 0;
  412. setcchar(&vga_to_curses[fch], wch, 0, 0, NULL);
  413. }
  414. /* Convert one wchar to UCS-2 */
  415. static uint16_t get_ucs(wchar_t wch, iconv_t conv)
  416. {
  417. char mbch[MB_LEN_MAX];
  418. uint16_t uch;
  419. char *pmbch, *puch;
  420. size_t smbch, such;
  421. mbstate_t ps;
  422. int ret;
  423. memset(&ps, 0, sizeof(ps));
  424. ret = wcrtomb(mbch, wch, &ps);
  425. if (ret == -1) {
  426. fprintf(stderr, "Could not convert 0x%04lx "
  427. "from wchar_t to a multibyte character: %s\n",
  428. (unsigned long)wch, strerror(errno));
  429. return 0xFFFD;
  430. }
  431. pmbch = (char *) mbch;
  432. puch = (char *) &uch;
  433. smbch = ret;
  434. such = sizeof(uch);
  435. if (iconv(conv, &pmbch, &smbch, &puch, &such) == (size_t) -1) {
  436. fprintf(stderr, "Could not convert 0x%04lx "
  437. "from a multibyte character to UCS-2 : %s\n",
  438. (unsigned long)wch, strerror(errno));
  439. return 0xFFFD;
  440. }
  441. return uch;
  442. }
  443. /*
  444. * Setup mapping for vga to curses line graphics.
  445. */
  446. static void font_setup(void)
  447. {
  448. iconv_t ucs2_to_nativecharset;
  449. iconv_t nativecharset_to_ucs2;
  450. iconv_t font_conv;
  451. int i;
  452. g_autofree gchar *local_codeset = g_get_codeset();
  453. /*
  454. * Control characters are normally non-printable, but VGA does have
  455. * well-known glyphs for them.
  456. */
  457. static const uint16_t control_characters[0x20] = {
  458. 0x0020,
  459. 0x263a,
  460. 0x263b,
  461. 0x2665,
  462. 0x2666,
  463. 0x2663,
  464. 0x2660,
  465. 0x2022,
  466. 0x25d8,
  467. 0x25cb,
  468. 0x25d9,
  469. 0x2642,
  470. 0x2640,
  471. 0x266a,
  472. 0x266b,
  473. 0x263c,
  474. 0x25ba,
  475. 0x25c4,
  476. 0x2195,
  477. 0x203c,
  478. 0x00b6,
  479. 0x00a7,
  480. 0x25ac,
  481. 0x21a8,
  482. 0x2191,
  483. 0x2193,
  484. 0x2192,
  485. 0x2190,
  486. 0x221f,
  487. 0x2194,
  488. 0x25b2,
  489. 0x25bc
  490. };
  491. ucs2_to_nativecharset = iconv_open(local_codeset, "UCS-2");
  492. if (ucs2_to_nativecharset == (iconv_t) -1) {
  493. fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n",
  494. strerror(errno));
  495. exit(1);
  496. }
  497. nativecharset_to_ucs2 = iconv_open("UCS-2", local_codeset);
  498. if (nativecharset_to_ucs2 == (iconv_t) -1) {
  499. iconv_close(ucs2_to_nativecharset);
  500. fprintf(stderr, "Could not convert font glyphs to UCS-2: '%s'\n",
  501. strerror(errno));
  502. exit(1);
  503. }
  504. font_conv = iconv_open(local_codeset, font_charset);
  505. if (font_conv == (iconv_t) -1) {
  506. iconv_close(ucs2_to_nativecharset);
  507. iconv_close(nativecharset_to_ucs2);
  508. fprintf(stderr, "Could not convert font glyphs from %s: '%s'\n",
  509. font_charset, strerror(errno));
  510. exit(1);
  511. }
  512. /* Control characters */
  513. for (i = 0; i <= 0x1F; i++) {
  514. convert_ucs(i, control_characters[i], ucs2_to_nativecharset);
  515. }
  516. for (i = 0x20; i <= 0xFF; i++) {
  517. convert_font(i, font_conv);
  518. }
  519. /* DEL */
  520. convert_ucs(0x7F, 0x2302, ucs2_to_nativecharset);
  521. if (strcmp(local_codeset, "UTF-8")) {
  522. /* Non-Unicode capable, use termcap equivalents for those available */
  523. for (i = 0; i <= 0xFF; i++) {
  524. wchar_t wch[CCHARW_MAX];
  525. attr_t attr;
  526. short color;
  527. int ret;
  528. ret = getcchar(&vga_to_curses[i], wch, &attr, &color, NULL);
  529. if (ret == ERR)
  530. continue;
  531. switch (get_ucs(wch[0], nativecharset_to_ucs2)) {
  532. case 0x00a3:
  533. vga_to_curses[i] = *WACS_STERLING;
  534. break;
  535. case 0x2591:
  536. vga_to_curses[i] = *WACS_BOARD;
  537. break;
  538. case 0x2592:
  539. vga_to_curses[i] = *WACS_CKBOARD;
  540. break;
  541. case 0x2502:
  542. vga_to_curses[i] = *WACS_VLINE;
  543. break;
  544. case 0x2524:
  545. vga_to_curses[i] = *WACS_RTEE;
  546. break;
  547. case 0x2510:
  548. vga_to_curses[i] = *WACS_URCORNER;
  549. break;
  550. case 0x2514:
  551. vga_to_curses[i] = *WACS_LLCORNER;
  552. break;
  553. case 0x2534:
  554. vga_to_curses[i] = *WACS_BTEE;
  555. break;
  556. case 0x252c:
  557. vga_to_curses[i] = *WACS_TTEE;
  558. break;
  559. case 0x251c:
  560. vga_to_curses[i] = *WACS_LTEE;
  561. break;
  562. case 0x2500:
  563. vga_to_curses[i] = *WACS_HLINE;
  564. break;
  565. case 0x253c:
  566. vga_to_curses[i] = *WACS_PLUS;
  567. break;
  568. case 0x256c:
  569. vga_to_curses[i] = *WACS_LANTERN;
  570. break;
  571. case 0x256a:
  572. vga_to_curses[i] = *WACS_NEQUAL;
  573. break;
  574. case 0x2518:
  575. vga_to_curses[i] = *WACS_LRCORNER;
  576. break;
  577. case 0x250c:
  578. vga_to_curses[i] = *WACS_ULCORNER;
  579. break;
  580. case 0x2588:
  581. vga_to_curses[i] = *WACS_BLOCK;
  582. break;
  583. case 0x03c0:
  584. vga_to_curses[i] = *WACS_PI;
  585. break;
  586. case 0x00b1:
  587. vga_to_curses[i] = *WACS_PLMINUS;
  588. break;
  589. case 0x2265:
  590. vga_to_curses[i] = *WACS_GEQUAL;
  591. break;
  592. case 0x2264:
  593. vga_to_curses[i] = *WACS_LEQUAL;
  594. break;
  595. case 0x00b0:
  596. vga_to_curses[i] = *WACS_DEGREE;
  597. break;
  598. case 0x25a0:
  599. vga_to_curses[i] = *WACS_BULLET;
  600. break;
  601. case 0x2666:
  602. vga_to_curses[i] = *WACS_DIAMOND;
  603. break;
  604. case 0x2192:
  605. vga_to_curses[i] = *WACS_RARROW;
  606. break;
  607. case 0x2190:
  608. vga_to_curses[i] = *WACS_LARROW;
  609. break;
  610. case 0x2191:
  611. vga_to_curses[i] = *WACS_UARROW;
  612. break;
  613. case 0x2193:
  614. vga_to_curses[i] = *WACS_DARROW;
  615. break;
  616. case 0x23ba:
  617. vga_to_curses[i] = *WACS_S1;
  618. break;
  619. case 0x23bb:
  620. vga_to_curses[i] = *WACS_S3;
  621. break;
  622. case 0x23bc:
  623. vga_to_curses[i] = *WACS_S7;
  624. break;
  625. case 0x23bd:
  626. vga_to_curses[i] = *WACS_S9;
  627. break;
  628. }
  629. }
  630. }
  631. iconv_close(ucs2_to_nativecharset);
  632. iconv_close(nativecharset_to_ucs2);
  633. iconv_close(font_conv);
  634. }
  635. static void curses_setup(void)
  636. {
  637. int i, colour_default[8] = {
  638. [QEMU_COLOR_BLACK] = COLOR_BLACK,
  639. [QEMU_COLOR_BLUE] = COLOR_BLUE,
  640. [QEMU_COLOR_GREEN] = COLOR_GREEN,
  641. [QEMU_COLOR_CYAN] = COLOR_CYAN,
  642. [QEMU_COLOR_RED] = COLOR_RED,
  643. [QEMU_COLOR_MAGENTA] = COLOR_MAGENTA,
  644. [QEMU_COLOR_YELLOW] = COLOR_YELLOW,
  645. [QEMU_COLOR_WHITE] = COLOR_WHITE,
  646. };
  647. /* input as raw as possible, let everything be interpreted
  648. * by the guest system */
  649. initscr(); noecho(); intrflush(stdscr, FALSE);
  650. nodelay(stdscr, TRUE); nonl(); keypad(stdscr, TRUE);
  651. start_color(); raw(); scrollok(stdscr, FALSE);
  652. set_escdelay(25);
  653. /* Make color pair to match color format (3bits bg:3bits fg) */
  654. for (i = 0; i < 64; i++) {
  655. init_pair(i, colour_default[i & 7], colour_default[i >> 3]);
  656. }
  657. /* Set default color for more than 64 for safety. */
  658. for (i = 64; i < COLOR_PAIRS; i++) {
  659. init_pair(i, COLOR_WHITE, COLOR_BLACK);
  660. }
  661. font_setup();
  662. }
  663. static void curses_keyboard_setup(void)
  664. {
  665. #if defined(__APPLE__)
  666. /* always use generic keymaps */
  667. if (!keyboard_layout)
  668. keyboard_layout = "en-us";
  669. #endif
  670. if(keyboard_layout) {
  671. kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
  672. &error_fatal);
  673. }
  674. }
  675. static const DisplayChangeListenerOps dcl_ops = {
  676. .dpy_name = "curses",
  677. .dpy_text_update = curses_update,
  678. .dpy_text_resize = curses_resize,
  679. .dpy_refresh = curses_refresh,
  680. .dpy_text_cursor = curses_cursor_position,
  681. };
  682. static void curses_display_init(DisplayState *ds, DisplayOptions *opts)
  683. {
  684. #ifndef _WIN32
  685. if (!isatty(1)) {
  686. fprintf(stderr, "We need a terminal output\n");
  687. exit(1);
  688. }
  689. #endif
  690. setlocale(LC_CTYPE, "");
  691. if (opts->u.curses.charset) {
  692. font_charset = opts->u.curses.charset;
  693. }
  694. screen = g_new0(console_ch_t, 160 * 100);
  695. vga_to_curses = g_new0(cchar_t, 256);
  696. curses_setup();
  697. curses_keyboard_setup();
  698. atexit(curses_atexit);
  699. curses_winch_init();
  700. dcl = g_new0(DisplayChangeListener, 1);
  701. dcl->ops = &dcl_ops;
  702. register_displaychangelistener(dcl);
  703. invalidate = 1;
  704. }
  705. static QemuDisplay qemu_display_curses = {
  706. .type = DISPLAY_TYPE_CURSES,
  707. .init = curses_display_init,
  708. };
  709. static void register_curses(void)
  710. {
  711. qemu_display_register(&qemu_display_curses);
  712. }
  713. type_init(register_curses);