2
0

curses.c 23 KB

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