tty-ncurses.c 13 KB

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