tty-ncurses.c 9.0 KB

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