2
0

curses.c 22 KB

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