tty-ncurses.c 16 KB

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