tty-ncurses.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. Interface to the terminal controlling library.
  3. Ncurses wrapper.
  4. Copyright (C) 2005, 2006, 2007, 2009, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2009.
  8. Ilia Maslakov <il.smind@gmail.com>, 2009.
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** \file
  22. * \brief Source: NCurses-based tty layer of Midnight-commander
  23. */
  24. #include <config.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <signal.h>
  28. #include "lib/global.h"
  29. #include "lib/strutil.h" /* str_term_form */
  30. #ifndef WANT_TERM_H
  31. #define WANT_TERM_H
  32. #endif
  33. #include "tty-internal.h" /* mc_tty_normalize_from_utf8() */
  34. #include "tty.h"
  35. #include "color-internal.h"
  36. #include "mouse.h"
  37. #include "win.h"
  38. /* include at last !!! */
  39. #ifdef WANT_TERM_H
  40. #ifdef HAVE_NCURSES_TERM_H
  41. #include <ncurses/term.h>
  42. #else
  43. #include <term.h>
  44. #endif /* HAVE_NCURSES_TERM_H */
  45. #endif /* WANT_TERM_H */
  46. /*** global variables ****************************************************************************/
  47. /*** file scope macro definitions ****************************************************************/
  48. #if defined(_AIX) && !defined(CTRL)
  49. #define CTRL(x) ((x) & 0x1f)
  50. #endif
  51. #define yx_in_screen(y, x) \
  52. (y >= 0 && y < LINES && x >= 0 && x < COLS)
  53. /*** global variables ****************************************************************************/
  54. /*** file scope type declarations ****************************************************************/
  55. /*** file scope variables ************************************************************************/
  56. /* ncurses supports cursor positions only within window */
  57. /* We use our own cursor coordibates to support partially visible widgets */
  58. static int mc_curs_row, mc_curs_col;
  59. /*** file scope functions ************************************************************************/
  60. /* --------------------------------------------------------------------------------------------- */
  61. /* --------------------------------------------------------------------------------------------- */
  62. static void
  63. tty_setup_sigwinch (void (*handler) (int))
  64. {
  65. #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
  66. struct sigaction act, oact;
  67. act.sa_handler = handler;
  68. sigemptyset (&act.sa_mask);
  69. act.sa_flags = 0;
  70. #ifdef SA_RESTART
  71. act.sa_flags |= SA_RESTART;
  72. #endif /* SA_RESTART */
  73. sigaction (SIGWINCH, &act, &oact);
  74. #endif /* SIGWINCH */
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. static void
  78. sigwinch_handler (int dummy)
  79. {
  80. (void) dummy;
  81. mc_global.tty.winch_flag = TRUE;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. /*** public functions ****************************************************************************/
  85. /* --------------------------------------------------------------------------------------------- */
  86. int
  87. mc_tty_normalize_lines_char (const char *ch)
  88. {
  89. char *str2;
  90. int res;
  91. struct mc_tty_lines_struct
  92. {
  93. const char *line;
  94. int line_code;
  95. } const lines_codes[] = {
  96. {"\342\224\230", ACS_LRCORNER}, /* ┌ */
  97. {"\342\224\224", ACS_LLCORNER}, /* └ */
  98. {"\342\224\220", ACS_URCORNER}, /* ┐ */
  99. {"\342\224\214", ACS_ULCORNER}, /* ┘ */
  100. {"\342\224\234", ACS_LTEE}, /* ├ */
  101. {"\342\224\244", ACS_RTEE}, /* ┤ */
  102. {"\342\224\254", ACS_TTEE}, /* ┬ */
  103. {"\342\224\264", ACS_BTEE}, /* ┴ */
  104. {"\342\224\200", ACS_HLINE}, /* ─ */
  105. {"\342\224\202", ACS_VLINE}, /* │ */
  106. {"\342\224\274", ACS_PLUS}, /* ┼ */
  107. {"\342\225\235", ACS_LRCORNER | A_BOLD}, /* ╔ */
  108. {"\342\225\232", ACS_LLCORNER | A_BOLD}, /* ╚ */
  109. {"\342\225\227", ACS_URCORNER | A_BOLD}, /* ╗ */
  110. {"\342\225\224", ACS_ULCORNER | A_BOLD}, /* ╝ */
  111. {"\342\225\237", ACS_LTEE | A_BOLD}, /* ╟ */
  112. {"\342\225\242", ACS_RTEE | A_BOLD}, /* ╢ */
  113. {"\342\225\244", ACS_TTEE | A_BOLD}, /* ╤ */
  114. {"\342\225\247", ACS_BTEE | A_BOLD}, /* ╧ */
  115. {"\342\225\220", ACS_HLINE | A_BOLD}, /* ═ */
  116. {"\342\225\221", ACS_VLINE | A_BOLD}, /* ║ */
  117. {NULL, 0}
  118. };
  119. if (ch == NULL)
  120. return (int) ' ';
  121. for (res = 0; lines_codes[res].line; res++)
  122. {
  123. if (strcmp (ch, lines_codes[res].line) == 0)
  124. return lines_codes[res].line_code;
  125. }
  126. str2 = mc_tty_normalize_from_utf8 (ch);
  127. res = g_utf8_get_char_validated (str2, -1);
  128. if (res < 0)
  129. res = (unsigned char) str2[0];
  130. g_free (str2);
  131. return res;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. void
  135. tty_init (gboolean mouse_enable, gboolean is_xterm)
  136. {
  137. initscr ();
  138. #ifdef HAVE_ESCDELAY
  139. /*
  140. * If ncurses exports the ESCDELAY variable, it should be set to
  141. * a low value, or you'll experience a delay in processing escape
  142. * sequences that are recognized by mc (e.g. Esc-Esc). On the other
  143. * hand, making ESCDELAY too small can result in some sequences
  144. * (e.g. cursor arrows) being reported as separate keys under heavy
  145. * processor load, and this can be a problem if mc hasn't learned
  146. * them in the "Learn Keys" dialog. The value is in milliseconds.
  147. */
  148. ESCDELAY = 200;
  149. #endif /* HAVE_ESCDELAY */
  150. /* use Ctrl-g to generate SIGINT */
  151. cur_term->Nttyb.c_cc[VINTR] = CTRL ('g'); /* ^g */
  152. /* disable SIGQUIT to allow use Ctrl-\ key */
  153. cur_term->Nttyb.c_cc[VQUIT] = NULL_VALUE;
  154. tcsetattr (cur_term->Filedes, TCSANOW, &cur_term->Nttyb);
  155. tty_start_interrupt_key ();
  156. if (!mouse_enable)
  157. use_mouse_p = MOUSE_DISABLED;
  158. tty_init_xterm_support (is_xterm); /* do it before do_enter_ca_mode() call */
  159. init_mouse ();
  160. do_enter_ca_mode ();
  161. tty_raw_mode ();
  162. noecho ();
  163. keypad (stdscr, TRUE);
  164. nodelay (stdscr, FALSE);
  165. tty_setup_sigwinch (sigwinch_handler);
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. void
  169. tty_shutdown (void)
  170. {
  171. disable_mouse ();
  172. tty_reset_shell_mode ();
  173. tty_noraw_mode ();
  174. tty_keypad (FALSE);
  175. tty_reset_screen ();
  176. do_exit_ca_mode ();
  177. }
  178. /* --------------------------------------------------------------------------------------------- */
  179. void
  180. tty_reset_prog_mode (void)
  181. {
  182. reset_prog_mode ();
  183. }
  184. /* --------------------------------------------------------------------------------------------- */
  185. void
  186. tty_reset_shell_mode (void)
  187. {
  188. reset_shell_mode ();
  189. }
  190. /* --------------------------------------------------------------------------------------------- */
  191. void
  192. tty_raw_mode (void)
  193. {
  194. raw (); /* FIXME: uneeded? */
  195. cbreak ();
  196. }
  197. /* --------------------------------------------------------------------------------------------- */
  198. void
  199. tty_noraw_mode (void)
  200. {
  201. nocbreak (); /* FIXME: unneeded? */
  202. noraw ();
  203. }
  204. /* --------------------------------------------------------------------------------------------- */
  205. void
  206. tty_noecho (void)
  207. {
  208. noecho ();
  209. }
  210. /* --------------------------------------------------------------------------------------------- */
  211. int
  212. tty_flush_input (void)
  213. {
  214. return flushinp ();
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. void
  218. tty_keypad (gboolean set)
  219. {
  220. keypad (stdscr, (bool) set);
  221. }
  222. /* --------------------------------------------------------------------------------------------- */
  223. void
  224. tty_nodelay (gboolean set)
  225. {
  226. nodelay (stdscr, (bool) set);
  227. }
  228. /* --------------------------------------------------------------------------------------------- */
  229. int
  230. tty_baudrate (void)
  231. {
  232. return baudrate ();
  233. }
  234. /* --------------------------------------------------------------------------------------------- */
  235. int
  236. tty_lowlevel_getch (void)
  237. {
  238. return getch ();
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. int
  242. tty_reset_screen (void)
  243. {
  244. return endwin ();
  245. }
  246. /* --------------------------------------------------------------------------------------------- */
  247. void
  248. tty_touch_screen (void)
  249. {
  250. touchwin (stdscr);
  251. }
  252. /* --------------------------------------------------------------------------------------------- */
  253. void
  254. tty_gotoyx (int y, int x)
  255. {
  256. mc_curs_row = y;
  257. mc_curs_col = x;
  258. if (y < 0)
  259. y = 0;
  260. if (y >= LINES)
  261. y = LINES - 1;
  262. if (x < 0)
  263. x = 0;
  264. if (x >= COLS)
  265. x = COLS - 1;
  266. move (y, x);
  267. }
  268. /* --------------------------------------------------------------------------------------------- */
  269. void
  270. tty_getyx (int *py, int *px)
  271. {
  272. *py = mc_curs_row;
  273. *px = mc_curs_col;
  274. }
  275. /* --------------------------------------------------------------------------------------------- */
  276. void
  277. tty_draw_hline (int y, int x, int ch, int len)
  278. {
  279. int x1;
  280. if (y < 0 || y >= LINES || x >= COLS)
  281. return;
  282. x1 = x;
  283. if (x < 0)
  284. {
  285. len += x;
  286. if (len <= 0)
  287. return;
  288. x = 0;
  289. }
  290. if ((chtype) ch == ACS_HLINE)
  291. ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
  292. move (y, x);
  293. hline (ch, len);
  294. move (y, x1);
  295. mc_curs_row = y;
  296. mc_curs_col = x1;
  297. }
  298. /* --------------------------------------------------------------------------------------------- */
  299. void
  300. tty_draw_vline (int y, int x, int ch, int len)
  301. {
  302. int y1;
  303. if (x < 0 || x >= COLS || y >= LINES)
  304. return;
  305. y1 = y;
  306. if (y < 0)
  307. {
  308. len += y;
  309. if (len <= 0)
  310. return;
  311. y = 0;
  312. }
  313. if ((chtype) ch == ACS_VLINE)
  314. ch = mc_tty_frm[MC_TTY_FRM_VERT];
  315. move (y, x);
  316. vline (ch, len);
  317. move (y1, x);
  318. mc_curs_row = y1;
  319. mc_curs_col = x;
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. void
  323. tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
  324. {
  325. int i;
  326. if (y < 0)
  327. {
  328. rows += y;
  329. if (rows <= 0)
  330. return;
  331. y = 0;
  332. }
  333. if (x < 0)
  334. {
  335. cols += x;
  336. if (cols <= 0)
  337. return;
  338. x = 0;
  339. }
  340. if (y + rows > LINES)
  341. rows = LINES - y;
  342. if (x + cols > COLS)
  343. cols = COLS - x;
  344. for (i = 0; i < rows; i++)
  345. {
  346. move (y + i, x);
  347. hline (ch, cols);
  348. }
  349. move (y, x);
  350. mc_curs_row = y;
  351. mc_curs_col = x;
  352. }
  353. /* --------------------------------------------------------------------------------------------- */
  354. void
  355. tty_set_alt_charset (gboolean alt_charset)
  356. {
  357. (void) alt_charset;
  358. }
  359. /* --------------------------------------------------------------------------------------------- */
  360. void
  361. tty_display_8bit (gboolean what)
  362. {
  363. meta (stdscr, (int) what);
  364. }
  365. /* --------------------------------------------------------------------------------------------- */
  366. void
  367. tty_print_char (int c)
  368. {
  369. if (yx_in_screen (mc_curs_row, mc_curs_col))
  370. addch (c);
  371. mc_curs_col++;
  372. }
  373. /* --------------------------------------------------------------------------------------------- */
  374. void
  375. tty_print_anychar (int c)
  376. {
  377. unsigned char str[6 + 1];
  378. if (mc_global.utf8_display || c > 255)
  379. {
  380. int res;
  381. res = g_unichar_to_utf8 (c, (char *) str);
  382. if (res == 0)
  383. {
  384. if (yx_in_screen (mc_curs_row, mc_curs_col))
  385. addch ('.');
  386. mc_curs_col++;
  387. }
  388. else
  389. {
  390. const char *s;
  391. str[res] = '\0';
  392. s = str_term_form ((char *) str);
  393. if (yx_in_screen (mc_curs_row, mc_curs_col))
  394. addstr (s);
  395. if (g_unichar_iswide (c))
  396. mc_curs_col += 2;
  397. else if (!g_unichar_iszerowidth (c))
  398. mc_curs_col++;
  399. }
  400. }
  401. else
  402. {
  403. if (yx_in_screen (mc_curs_row, mc_curs_col))
  404. addch (c);
  405. mc_curs_col++;
  406. }
  407. }
  408. /* --------------------------------------------------------------------------------------------- */
  409. void
  410. tty_print_alt_char (int c, gboolean single)
  411. {
  412. if (yx_in_screen (mc_curs_row, mc_curs_col))
  413. {
  414. if ((chtype) c == ACS_VLINE)
  415. c = mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT];
  416. else if ((chtype) c == ACS_HLINE)
  417. c = mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ];
  418. else if ((chtype) c == ACS_LTEE)
  419. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE];
  420. else if ((chtype) c == ACS_RTEE)
  421. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE];
  422. else if ((chtype) c == ACS_ULCORNER)
  423. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP];
  424. else if ((chtype) c == ACS_LLCORNER)
  425. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM];
  426. else if ((chtype) c == ACS_URCORNER)
  427. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP];
  428. else if ((chtype) c == ACS_LRCORNER)
  429. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM];
  430. else if ((chtype) c == ACS_PLUS)
  431. c = mc_tty_frm[MC_TTY_FRM_CROSS];
  432. addch (c);
  433. }
  434. mc_curs_col++;
  435. }
  436. /* --------------------------------------------------------------------------------------------- */
  437. void
  438. tty_print_string (const char *s)
  439. {
  440. int len;
  441. int start = 0;
  442. s = str_term_form (s);
  443. len = str_term_width1 (s);
  444. /* line is upper or below the screen or entire line is before or after scrren */
  445. if (mc_curs_row < 0 || mc_curs_row >= LINES || mc_curs_col + len <= 0 || mc_curs_col >= COLS)
  446. {
  447. mc_curs_col += len;
  448. return;
  449. }
  450. /* skip invisible left part */
  451. if (mc_curs_col < 0)
  452. {
  453. start = -mc_curs_col;
  454. len += mc_curs_col;
  455. mc_curs_col = 0;
  456. }
  457. mc_curs_col += len;
  458. if (mc_curs_col >= COLS)
  459. len = COLS - (mc_curs_col - len);
  460. addstr (str_term_substring (s, start, len));
  461. }
  462. /* --------------------------------------------------------------------------------------------- */
  463. void
  464. tty_printf (const char *fmt, ...)
  465. {
  466. va_list args;
  467. char buf[BUF_1K]; /* FIXME: is it enough? */
  468. va_start (args, fmt);
  469. g_vsnprintf (buf, sizeof (buf), fmt, args);
  470. va_end (args);
  471. tty_print_string (buf);
  472. }
  473. /* --------------------------------------------------------------------------------------------- */
  474. char *
  475. tty_tgetstr (const char *cap)
  476. {
  477. char *unused = NULL;
  478. return tgetstr ((char *) cap, &unused);
  479. }
  480. /* --------------------------------------------------------------------------------------------- */
  481. void
  482. tty_refresh (void)
  483. {
  484. refresh ();
  485. doupdate ();
  486. }
  487. /* --------------------------------------------------------------------------------------------- */
  488. void
  489. tty_beep (void)
  490. {
  491. beep ();
  492. }
  493. /* --------------------------------------------------------------------------------------------- */