tty-ncurses.c 13 KB

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