tty.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. Interface to the terminal controlling library.
  3. Copyright (C) 2005-2019
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Roland Illig <roland.illig@gmx.de>, 2005.
  7. Andrew Borodin <aborodin@vmail.ru>, 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 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU 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, see <http://www.gnu.org/licenses/>.
  19. */
  20. /** \file tty.c
  21. * \brief Source: %interface to the terminal controlling library
  22. */
  23. #include <config.h>
  24. #include <errno.h>
  25. #include <signal.h>
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <string.h> /* memset() */
  29. #ifdef HAVE_SYS_SELECT_H
  30. #include <sys/select.h>
  31. #else
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #endif
  35. #include <unistd.h> /* exit() */
  36. #ifdef HAVE_SYS_IOCTL_H
  37. #include <sys/ioctl.h>
  38. #endif
  39. #include "lib/global.h"
  40. #include "lib/strutil.h"
  41. #include "tty.h"
  42. #include "tty-internal.h"
  43. #include "mouse.h" /* use_mouse_p */
  44. #include "win.h"
  45. /*** global variables ****************************************************************************/
  46. int mc_tty_frm[MC_TTY_FRM_MAX];
  47. /*** file scope macro definitions ****************************************************************/
  48. /*** file scope type declarations ****************************************************************/
  49. /*** file scope variables ************************************************************************/
  50. static SIG_ATOMIC_VOLATILE_T got_interrupt = 0;
  51. /*** file scope functions ************************************************************************/
  52. /* --------------------------------------------------------------------------------------------- */
  53. static void
  54. sigintr_handler (int signo)
  55. {
  56. (void) &signo;
  57. got_interrupt = 1;
  58. }
  59. /* --------------------------------------------------------------------------------------------- */
  60. /*** public functions ****************************************************************************/
  61. /* --------------------------------------------------------------------------------------------- */
  62. /**
  63. * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
  64. *
  65. * @param force_xterm Set forced the XTerm type
  66. *
  67. * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
  68. * rxvt*, Eterm or dtterm
  69. */
  70. gboolean
  71. tty_check_term (gboolean force_xterm)
  72. {
  73. const char *termvalue;
  74. const char *xdisplay;
  75. termvalue = getenv ("TERM");
  76. if (termvalue == NULL || *termvalue == '\0')
  77. {
  78. fputs (_("The TERM environment variable is unset!\n"), stderr);
  79. exit (EXIT_FAILURE);
  80. }
  81. xdisplay = getenv ("DISPLAY");
  82. if (xdisplay != NULL && *xdisplay == '\0')
  83. xdisplay = NULL;
  84. return force_xterm || strncmp (termvalue, "xterm", 5) == 0
  85. || strncmp (termvalue, "konsole", 7) == 0
  86. || strncmp (termvalue, "rxvt", 4) == 0
  87. || strcmp (termvalue, "Eterm") == 0
  88. || strcmp (termvalue, "dtterm") == 0
  89. || (strncmp (termvalue, "screen", 6) == 0 && xdisplay != NULL);
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. extern void
  93. tty_start_interrupt_key (void)
  94. {
  95. struct sigaction act;
  96. memset (&act, 0, sizeof (act));
  97. act.sa_handler = sigintr_handler;
  98. sigemptyset (&act.sa_mask);
  99. #ifdef SA_RESTART
  100. act.sa_flags = SA_RESTART;
  101. #endif /* SA_RESTART */
  102. sigaction (SIGINT, &act, NULL);
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. extern void
  106. tty_enable_interrupt_key (void)
  107. {
  108. struct sigaction act;
  109. memset (&act, 0, sizeof (act));
  110. act.sa_handler = sigintr_handler;
  111. sigemptyset (&act.sa_mask);
  112. sigaction (SIGINT, &act, NULL);
  113. got_interrupt = 0;
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. extern void
  117. tty_disable_interrupt_key (void)
  118. {
  119. struct sigaction act;
  120. memset (&act, 0, sizeof (act));
  121. act.sa_handler = SIG_IGN;
  122. sigemptyset (&act.sa_mask);
  123. sigaction (SIGINT, &act, NULL);
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. extern gboolean
  127. tty_got_interrupt (void)
  128. {
  129. gboolean rv;
  130. rv = (got_interrupt != 0);
  131. got_interrupt = 0;
  132. return rv;
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. gboolean
  136. tty_got_winch (void)
  137. {
  138. fd_set fdset;
  139. /* *INDENT-OFF* */
  140. /* instant timeout */
  141. struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 };
  142. /* *INDENT-ON* */
  143. int ok;
  144. FD_ZERO (&fdset);
  145. FD_SET (sigwinch_pipe[0], &fdset);
  146. while ((ok = select (sigwinch_pipe[0] + 1, &fdset, NULL, NULL, &timeout)) < 0)
  147. if (errno != EINTR)
  148. {
  149. perror (_("Cannot check SIGWINCH pipe"));
  150. exit (EXIT_FAILURE);
  151. }
  152. return (ok != 0 && FD_ISSET (sigwinch_pipe[0], &fdset));
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. void
  156. tty_flush_winch (void)
  157. {
  158. ssize_t n;
  159. /* merge all SIGWINCH events raised to this moment */
  160. do
  161. {
  162. char x[16];
  163. /* read multiple events at a time */
  164. n = read (sigwinch_pipe[0], &x, sizeof (x));
  165. }
  166. while (n > 0 || (n == -1 && errno == EINTR));
  167. }
  168. /* --------------------------------------------------------------------------------------------- */
  169. void
  170. tty_print_one_hline (gboolean single)
  171. {
  172. tty_print_alt_char (ACS_HLINE, single);
  173. }
  174. /* --------------------------------------------------------------------------------------------- */
  175. void
  176. tty_print_one_vline (gboolean single)
  177. {
  178. tty_print_alt_char (ACS_VLINE, single);
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. void
  182. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  183. {
  184. int y2, x2;
  185. if (ys <= 0 || xs <= 0)
  186. return;
  187. ys--;
  188. xs--;
  189. y2 = y + ys;
  190. x2 = x + xs;
  191. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  192. tty_draw_vline (y, x2, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  193. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  194. tty_draw_hline (y2, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  195. tty_gotoyx (y, x);
  196. tty_print_alt_char (ACS_ULCORNER, single);
  197. tty_gotoyx (y2, x);
  198. tty_print_alt_char (ACS_LLCORNER, single);
  199. tty_gotoyx (y, x2);
  200. tty_print_alt_char (ACS_URCORNER, single);
  201. tty_gotoyx (y2, x2);
  202. tty_print_alt_char (ACS_LRCORNER, single);
  203. }
  204. /* --------------------------------------------------------------------------------------------- */
  205. char *
  206. mc_tty_normalize_from_utf8 (const char *str)
  207. {
  208. GIConv conv;
  209. GString *buffer;
  210. const char *_system_codepage = str_detect_termencoding ();
  211. if (str_isutf8 (_system_codepage))
  212. return g_strdup (str);
  213. conv = g_iconv_open (_system_codepage, "UTF-8");
  214. if (conv == INVALID_CONV)
  215. return g_strdup (str);
  216. buffer = g_string_new ("");
  217. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  218. {
  219. g_string_free (buffer, TRUE);
  220. str_close_conv (conv);
  221. return g_strdup (str);
  222. }
  223. str_close_conv (conv);
  224. return g_string_free (buffer, FALSE);
  225. }
  226. /* --------------------------------------------------------------------------------------------- */
  227. /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
  228. int
  229. tty_resize (int fd)
  230. {
  231. #if defined TIOCSWINSZ
  232. struct winsize tty_size;
  233. tty_size.ws_row = LINES;
  234. tty_size.ws_col = COLS;
  235. tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  236. return ioctl (fd, TIOCSWINSZ, &tty_size);
  237. #else
  238. return 0;
  239. #endif
  240. }
  241. /* --------------------------------------------------------------------------------------------- */
  242. void
  243. tty_init_xterm_support (gboolean is_xterm)
  244. {
  245. const char *termvalue;
  246. termvalue = getenv ("TERM");
  247. /* Check mouse and ca capabilities */
  248. /* terminfo/termcap structures have been already initialized,
  249. in slang_init() or/and init_curses() */
  250. /* Check terminfo at first, then check termcap */
  251. xmouse_seq = tty_tgetstr ("kmous");
  252. if (xmouse_seq == NULL)
  253. xmouse_seq = tty_tgetstr ("Km");
  254. smcup = tty_tgetstr ("smcup");
  255. if (smcup == NULL)
  256. smcup = tty_tgetstr ("ti");
  257. rmcup = tty_tgetstr ("rmcup");
  258. if (rmcup == NULL)
  259. rmcup = tty_tgetstr ("te");
  260. if (strcmp (termvalue, "cygwin") == 0)
  261. {
  262. is_xterm = TRUE;
  263. use_mouse_p = MOUSE_DISABLED;
  264. }
  265. if (is_xterm)
  266. {
  267. /* Default to the standard xterm sequence */
  268. if (xmouse_seq == NULL)
  269. xmouse_seq = ESC_STR "[M";
  270. /* Enable mouse unless explicitly disabled by --nomouse */
  271. if (use_mouse_p != MOUSE_DISABLED)
  272. {
  273. if (mc_global.tty.old_mouse)
  274. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  275. else
  276. {
  277. /* FIXME: this dirty hack to set supported type of tracking the mouse */
  278. const char *color_term = getenv ("COLORTERM");
  279. if (strncmp (termvalue, "rxvt", 4) == 0 ||
  280. (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
  281. strcmp (termvalue, "Eterm") == 0)
  282. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  283. else
  284. use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
  285. }
  286. }
  287. }
  288. /* No termcap for SGR extended mouse (yet), hardcode it for now */
  289. if (xmouse_seq != NULL)
  290. xmouse_extended_seq = ESC_STR "[<";
  291. }
  292. /* --------------------------------------------------------------------------------------------- */