tty-ncurses.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. /*** global variables ****************************************************************************/
  56. /*** file scope type declarations ****************************************************************/
  57. /*** file scope variables ************************************************************************/
  58. /*** file scope functions ************************************************************************/
  59. /* --------------------------------------------------------------------------------------------- */
  60. /* --------------------------------------------------------------------------------------------- */
  61. static void
  62. tty_setup_sigwinch (void (*handler) (int))
  63. {
  64. #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
  65. struct sigaction act, oact;
  66. act.sa_handler = handler;
  67. sigemptyset (&act.sa_mask);
  68. act.sa_flags = 0;
  69. #ifdef SA_RESTART
  70. act.sa_flags |= SA_RESTART;
  71. #endif /* SA_RESTART */
  72. sigaction (SIGWINCH, &act, &oact);
  73. #endif /* SIGWINCH */
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. static void
  77. sigwinch_handler (int dummy)
  78. {
  79. (void) dummy;
  80. mc_global.tty.winch_flag = 1;
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. /*** public functions ****************************************************************************/
  84. /* --------------------------------------------------------------------------------------------- */
  85. int
  86. mc_tty_normalize_lines_char (const char *ch)
  87. {
  88. char *str2;
  89. int res;
  90. struct mc_tty_lines_struct
  91. {
  92. const char *line;
  93. int line_code;
  94. } const lines_codes[] = {
  95. {"\342\224\230", ACS_LRCORNER}, /* ┌ */
  96. {"\342\224\224", ACS_LLCORNER}, /* └ */
  97. {"\342\224\220", ACS_URCORNER}, /* ┐ */
  98. {"\342\224\214", ACS_ULCORNER}, /* ┘ */
  99. {"\342\224\234", ACS_LTEE}, /* ├ */
  100. {"\342\224\244", ACS_RTEE}, /* ┤ */
  101. {"\342\224\254", ACS_TTEE}, /* ┬ */
  102. {"\342\224\264", ACS_BTEE}, /* ┴ */
  103. {"\342\224\200", ACS_HLINE}, /* ─ */
  104. {"\342\224\202", ACS_VLINE}, /* │ */
  105. {"\342\224\274", ACS_PLUS}, /* ┼ */
  106. {"\342\225\235", ACS_LRCORNER | A_BOLD}, /* ╔ */
  107. {"\342\225\232", ACS_LLCORNER | A_BOLD}, /* ╚ */
  108. {"\342\225\227", ACS_URCORNER | A_BOLD}, /* ╗ */
  109. {"\342\225\224", ACS_ULCORNER | A_BOLD}, /* ╝ */
  110. {"\342\225\237", ACS_LTEE | A_BOLD}, /* ╟ */
  111. {"\342\225\242", ACS_RTEE | A_BOLD}, /* ╢ */
  112. {"\342\225\244", ACS_TTEE | A_BOLD}, /* ╤ */
  113. {"\342\225\247", ACS_BTEE | A_BOLD}, /* ╧ */
  114. {"\342\225\220", ACS_HLINE | A_BOLD}, /* ═ */
  115. {"\342\225\221", ACS_VLINE | A_BOLD}, /* ║ */
  116. {NULL, 0}
  117. };
  118. if (ch == NULL)
  119. return (int) ' ';
  120. for (res = 0; lines_codes[res].line; res++)
  121. {
  122. if (strcmp (ch, lines_codes[res].line) == 0)
  123. return lines_codes[res].line_code;
  124. }
  125. str2 = mc_tty_normalize_from_utf8 (ch);
  126. res = g_utf8_get_char_validated (str2, -1);
  127. if (res < 0)
  128. res = (unsigned char) str2[0];
  129. g_free (str2);
  130. return res;
  131. }
  132. /* --------------------------------------------------------------------------------------------- */
  133. void
  134. tty_init (gboolean mouse_enable, gboolean is_xterm)
  135. {
  136. initscr ();
  137. #ifdef HAVE_ESCDELAY
  138. /*
  139. * If ncurses exports the ESCDELAY variable, it should be set to
  140. * a low value, or you'll experience a delay in processing escape
  141. * sequences that are recognized by mc (e.g. Esc-Esc). On the other
  142. * hand, making ESCDELAY too small can result in some sequences
  143. * (e.g. cursor arrows) being reported as separate keys under heavy
  144. * processor load, and this can be a problem if mc hasn't learned
  145. * them in the "Learn Keys" dialog. The value is in milliseconds.
  146. */
  147. ESCDELAY = 200;
  148. #endif /* HAVE_ESCDELAY */
  149. /* use Ctrl-g to generate SIGINT */
  150. cur_term->Nttyb.c_cc[VINTR] = CTRL ('g'); /* ^g */
  151. /* disable SIGQUIT to allow use Ctrl-\ key */
  152. cur_term->Nttyb.c_cc[VQUIT] = NULL_VALUE;
  153. tcsetattr (cur_term->Filedes, TCSANOW, &cur_term->Nttyb);
  154. tty_start_interrupt_key ();
  155. if (!mouse_enable)
  156. use_mouse_p = MOUSE_DISABLED;
  157. tty_init_xterm_support (is_xterm); /* do it before do_enter_ca_mode() call */
  158. init_mouse ();
  159. do_enter_ca_mode ();
  160. tty_raw_mode ();
  161. noecho ();
  162. keypad (stdscr, TRUE);
  163. nodelay (stdscr, FALSE);
  164. tty_setup_sigwinch (sigwinch_handler);
  165. }
  166. /* --------------------------------------------------------------------------------------------- */
  167. void
  168. tty_shutdown (void)
  169. {
  170. disable_mouse ();
  171. tty_reset_shell_mode ();
  172. tty_noraw_mode ();
  173. tty_keypad (FALSE);
  174. tty_reset_screen ();
  175. do_exit_ca_mode ();
  176. }
  177. /* --------------------------------------------------------------------------------------------- */
  178. void
  179. tty_change_screen_size (void)
  180. {
  181. #if defined(TIOCGWINSZ) && NCURSES_VERSION_MAJOR >= 4
  182. struct winsize winsz;
  183. winsz.ws_col = winsz.ws_row = 0;
  184. #ifndef NCURSES_VERSION
  185. tty_noraw_mode ();
  186. tty_reset_screen ();
  187. #endif
  188. /* Ioctl on the STDIN_FILENO */
  189. ioctl (fileno (stdout), TIOCGWINSZ, &winsz);
  190. if (winsz.ws_col != 0 && winsz.ws_row != 0)
  191. {
  192. #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
  193. resizeterm (winsz.ws_row, winsz.ws_col);
  194. clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
  195. #else
  196. COLS = winsz.ws_col;
  197. LINES = winsz.ws_row;
  198. #endif
  199. }
  200. #endif /* defined(TIOCGWINSZ) || NCURSES_VERSION_MAJOR >= 4 */
  201. #ifdef HAVE_SUBSHELL_SUPPORT
  202. if (mc_global.tty.use_subshell)
  203. tty_resize (mc_global.tty.subshell_pty);
  204. #endif
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. void
  208. tty_reset_prog_mode (void)
  209. {
  210. reset_prog_mode ();
  211. }
  212. /* --------------------------------------------------------------------------------------------- */
  213. void
  214. tty_reset_shell_mode (void)
  215. {
  216. reset_shell_mode ();
  217. }
  218. /* --------------------------------------------------------------------------------------------- */
  219. void
  220. tty_raw_mode (void)
  221. {
  222. raw (); /* FIXME: uneeded? */
  223. cbreak ();
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. void
  227. tty_noraw_mode (void)
  228. {
  229. nocbreak (); /* FIXME: unneeded? */
  230. noraw ();
  231. }
  232. /* --------------------------------------------------------------------------------------------- */
  233. void
  234. tty_noecho (void)
  235. {
  236. noecho ();
  237. }
  238. /* --------------------------------------------------------------------------------------------- */
  239. int
  240. tty_flush_input (void)
  241. {
  242. return flushinp ();
  243. }
  244. /* --------------------------------------------------------------------------------------------- */
  245. void
  246. tty_keypad (gboolean set)
  247. {
  248. keypad (stdscr, (bool) set);
  249. }
  250. /* --------------------------------------------------------------------------------------------- */
  251. void
  252. tty_nodelay (gboolean set)
  253. {
  254. nodelay (stdscr, (bool) set);
  255. }
  256. /* --------------------------------------------------------------------------------------------- */
  257. int
  258. tty_baudrate (void)
  259. {
  260. return baudrate ();
  261. }
  262. /* --------------------------------------------------------------------------------------------- */
  263. int
  264. tty_lowlevel_getch (void)
  265. {
  266. return getch ();
  267. }
  268. /* --------------------------------------------------------------------------------------------- */
  269. int
  270. tty_reset_screen (void)
  271. {
  272. return endwin ();
  273. }
  274. /* --------------------------------------------------------------------------------------------- */
  275. void
  276. tty_touch_screen (void)
  277. {
  278. touchwin (stdscr);
  279. }
  280. /* --------------------------------------------------------------------------------------------- */
  281. void
  282. tty_gotoyx (int y, int x)
  283. {
  284. move (y, x);
  285. }
  286. /* --------------------------------------------------------------------------------------------- */
  287. void
  288. tty_getyx (int *py, int *px)
  289. {
  290. getyx (stdscr, *py, *px);
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. /* if x < 0 or y < 0, draw line starting from current position */
  294. void
  295. tty_draw_hline (int y, int x, int ch, int len)
  296. {
  297. if ((chtype) ch == ACS_HLINE)
  298. ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
  299. if ((y >= 0) && (x >= 0))
  300. move (y, x);
  301. hline (ch, len);
  302. }
  303. /* --------------------------------------------------------------------------------------------- */
  304. /* if x < 0 or y < 0, draw line starting from current position */
  305. void
  306. tty_draw_vline (int y, int x, int ch, int len)
  307. {
  308. if ((chtype) ch == ACS_VLINE)
  309. ch = mc_tty_frm[MC_TTY_FRM_VERT];
  310. if ((y >= 0) && (x >= 0))
  311. move (y, x);
  312. vline (ch, len);
  313. }
  314. /* --------------------------------------------------------------------------------------------- */
  315. void
  316. tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
  317. {
  318. int i;
  319. if (y < 0)
  320. {
  321. rows += y;
  322. if (rows <= 0)
  323. return;
  324. y = 0;
  325. }
  326. if (x < 0)
  327. {
  328. cols += x;
  329. if (cols <= 0)
  330. return;
  331. x = 0;
  332. }
  333. if (y + rows > LINES)
  334. rows = LINES - y;
  335. if (x + cols > COLS)
  336. cols = COLS - x;
  337. for (i = 0; i < rows; i++)
  338. {
  339. move (y + i, x);
  340. hline (ch, cols);
  341. }
  342. move (y, x);
  343. }
  344. /* --------------------------------------------------------------------------------------------- */
  345. void
  346. tty_set_alt_charset (gboolean alt_charset)
  347. {
  348. (void) alt_charset;
  349. }
  350. /* --------------------------------------------------------------------------------------------- */
  351. void
  352. tty_display_8bit (gboolean what)
  353. {
  354. meta (stdscr, (int) what);
  355. }
  356. /* --------------------------------------------------------------------------------------------- */
  357. void
  358. tty_print_char (int c)
  359. {
  360. addch (c);
  361. }
  362. /* --------------------------------------------------------------------------------------------- */
  363. void
  364. tty_print_anychar (int c)
  365. {
  366. unsigned char str[6 + 1];
  367. if (mc_global.utf8_display || c > 255)
  368. {
  369. int res = g_unichar_to_utf8 (c, (char *) str);
  370. if (res == 0)
  371. {
  372. str[0] = '.';
  373. str[1] = '\0';
  374. }
  375. else
  376. str[res] = '\0';
  377. addstr (str_term_form ((char *) str));
  378. }
  379. else
  380. addch (c);
  381. }
  382. /* --------------------------------------------------------------------------------------------- */
  383. void
  384. tty_print_alt_char (int c, gboolean single)
  385. {
  386. if ((chtype) c == ACS_VLINE)
  387. c = mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT];
  388. else if ((chtype) c == ACS_HLINE)
  389. c = mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ];
  390. else if ((chtype) c == ACS_LTEE)
  391. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE];
  392. else if ((chtype) c == ACS_RTEE)
  393. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE];
  394. else if ((chtype) c == ACS_ULCORNER)
  395. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP];
  396. else if ((chtype) c == ACS_LLCORNER)
  397. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM];
  398. else if ((chtype) c == ACS_URCORNER)
  399. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP];
  400. else if ((chtype) c == ACS_LRCORNER)
  401. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM];
  402. else if ((chtype) c == ACS_PLUS)
  403. c = mc_tty_frm[MC_TTY_FRM_CROSS];
  404. addch (c);
  405. }
  406. /* --------------------------------------------------------------------------------------------- */
  407. void
  408. tty_print_string (const char *s)
  409. {
  410. addstr (str_term_form (s));
  411. }
  412. /* --------------------------------------------------------------------------------------------- */
  413. void
  414. tty_printf (const char *fmt, ...)
  415. {
  416. va_list args;
  417. va_start (args, fmt);
  418. vw_printw (stdscr, fmt, args);
  419. va_end (args);
  420. }
  421. /* --------------------------------------------------------------------------------------------- */
  422. char *
  423. tty_tgetstr (const char *cap)
  424. {
  425. char *unused = NULL;
  426. return tgetstr ((char *) cap, &unused);
  427. }
  428. /* --------------------------------------------------------------------------------------------- */
  429. void
  430. tty_refresh (void)
  431. {
  432. refresh ();
  433. doupdate ();
  434. }
  435. /* --------------------------------------------------------------------------------------------- */
  436. void
  437. tty_beep (void)
  438. {
  439. beep ();
  440. }
  441. /* --------------------------------------------------------------------------------------------- */