tty-ncurses.c 13 KB

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