tty-ncurses.c 13 KB

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