tty-ncurses.c 16 KB

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