tty.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. Interface to the terminal controlling library.
  3. Copyright (C) 2005-2021
  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. /* In some systems (like Solaris 11.4 SPARC), TIOCSWINSZ is defined in termios.h */
  40. #include <termios.h>
  41. #include "lib/global.h"
  42. #include "lib/strutil.h"
  43. #include "tty.h"
  44. #include "tty-internal.h"
  45. #include "color.h" /* tty_set_normal_attrs() */
  46. #include "mouse.h" /* use_mouse_p */
  47. #include "win.h"
  48. /*** global variables ****************************************************************************/
  49. int mc_tty_frm[MC_TTY_FRM_MAX];
  50. /*** file scope macro definitions ****************************************************************/
  51. /*** file scope type declarations ****************************************************************/
  52. /*** file scope variables ************************************************************************/
  53. static SIG_ATOMIC_VOLATILE_T got_interrupt = 0;
  54. /*** file scope functions ************************************************************************/
  55. /* --------------------------------------------------------------------------------------------- */
  56. static void
  57. sigintr_handler (int signo)
  58. {
  59. (void) &signo;
  60. got_interrupt = 1;
  61. }
  62. /* --------------------------------------------------------------------------------------------- */
  63. /*** public functions ****************************************************************************/
  64. /* --------------------------------------------------------------------------------------------- */
  65. /**
  66. * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
  67. *
  68. * @param force_xterm Set forced the XTerm type
  69. *
  70. * @return true if @param force_xterm is true or value of $TERM is one of following:
  71. * term*
  72. * konsole*
  73. * rxvt*
  74. * Eterm
  75. * dtterm
  76. * alacritty*
  77. * foot*
  78. * screen* (and DISPLAY environment variable is set).
  79. */
  80. gboolean
  81. tty_check_term (gboolean force_xterm)
  82. {
  83. const char *termvalue;
  84. const char *xdisplay;
  85. termvalue = getenv ("TERM");
  86. if (termvalue == NULL || *termvalue == '\0')
  87. {
  88. fputs (_("The TERM environment variable is unset!\n"), stderr);
  89. exit (EXIT_FAILURE);
  90. }
  91. xdisplay = getenv ("DISPLAY");
  92. if (xdisplay != NULL && *xdisplay == '\0')
  93. xdisplay = NULL;
  94. return force_xterm || strncmp (termvalue, "xterm", 5) == 0
  95. || strncmp (termvalue, "konsole", 7) == 0
  96. || strncmp (termvalue, "rxvt", 4) == 0
  97. || strcmp (termvalue, "Eterm") == 0
  98. || strcmp (termvalue, "dtterm") == 0
  99. || strncmp (termvalue, "alacritty", 9) == 0
  100. || strncmp (termvalue, "foot", 4) == 0
  101. || (strncmp (termvalue, "screen", 6) == 0 && xdisplay != NULL);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. extern void
  105. tty_start_interrupt_key (void)
  106. {
  107. struct sigaction act;
  108. memset (&act, 0, sizeof (act));
  109. act.sa_handler = sigintr_handler;
  110. sigemptyset (&act.sa_mask);
  111. #ifdef SA_RESTART
  112. act.sa_flags = SA_RESTART;
  113. #endif /* SA_RESTART */
  114. sigaction (SIGINT, &act, NULL);
  115. }
  116. /* --------------------------------------------------------------------------------------------- */
  117. extern void
  118. tty_enable_interrupt_key (void)
  119. {
  120. struct sigaction act;
  121. memset (&act, 0, sizeof (act));
  122. act.sa_handler = sigintr_handler;
  123. sigemptyset (&act.sa_mask);
  124. sigaction (SIGINT, &act, NULL);
  125. got_interrupt = 0;
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. extern void
  129. tty_disable_interrupt_key (void)
  130. {
  131. struct sigaction act;
  132. memset (&act, 0, sizeof (act));
  133. act.sa_handler = SIG_IGN;
  134. sigemptyset (&act.sa_mask);
  135. sigaction (SIGINT, &act, NULL);
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. extern gboolean
  139. tty_got_interrupt (void)
  140. {
  141. gboolean rv;
  142. rv = (got_interrupt != 0);
  143. got_interrupt = 0;
  144. return rv;
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. gboolean
  148. tty_got_winch (void)
  149. {
  150. fd_set fdset;
  151. /* *INDENT-OFF* */
  152. /* instant timeout */
  153. struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 };
  154. /* *INDENT-ON* */
  155. int ok;
  156. FD_ZERO (&fdset);
  157. FD_SET (sigwinch_pipe[0], &fdset);
  158. while ((ok = select (sigwinch_pipe[0] + 1, &fdset, NULL, NULL, &timeout)) < 0)
  159. if (errno != EINTR)
  160. {
  161. perror (_("Cannot check SIGWINCH pipe"));
  162. exit (EXIT_FAILURE);
  163. }
  164. return (ok != 0 && FD_ISSET (sigwinch_pipe[0], &fdset));
  165. }
  166. /* --------------------------------------------------------------------------------------------- */
  167. void
  168. tty_flush_winch (void)
  169. {
  170. ssize_t n;
  171. /* merge all SIGWINCH events raised to this moment */
  172. do
  173. {
  174. char x[16];
  175. /* read multiple events at a time */
  176. n = read (sigwinch_pipe[0], &x, sizeof (x));
  177. }
  178. while (n > 0 || (n == -1 && errno == EINTR));
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. void
  182. tty_print_one_hline (gboolean single)
  183. {
  184. tty_print_alt_char (ACS_HLINE, single);
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. void
  188. tty_print_one_vline (gboolean single)
  189. {
  190. tty_print_alt_char (ACS_VLINE, single);
  191. }
  192. /* --------------------------------------------------------------------------------------------- */
  193. void
  194. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  195. {
  196. int y2, x2;
  197. if (ys <= 0 || xs <= 0)
  198. return;
  199. ys--;
  200. xs--;
  201. y2 = y + ys;
  202. x2 = x + xs;
  203. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  204. tty_draw_vline (y, x2, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  205. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  206. tty_draw_hline (y2, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  207. tty_gotoyx (y, x);
  208. tty_print_alt_char (ACS_ULCORNER, single);
  209. tty_gotoyx (y2, x);
  210. tty_print_alt_char (ACS_LLCORNER, single);
  211. tty_gotoyx (y, x2);
  212. tty_print_alt_char (ACS_URCORNER, single);
  213. tty_gotoyx (y2, x2);
  214. tty_print_alt_char (ACS_LRCORNER, single);
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. void
  218. tty_draw_box_shadow (int y, int x, int rows, int cols, int shadow_color)
  219. {
  220. /* draw right shadow */
  221. tty_colorize_area (y + 1, x + cols, rows - 1, 2, shadow_color);
  222. /* draw bottom shadow */
  223. tty_colorize_area (y + rows, x + 2, 1, cols, shadow_color);
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. char *
  227. mc_tty_normalize_from_utf8 (const char *str)
  228. {
  229. GIConv conv;
  230. GString *buffer;
  231. const char *_system_codepage = str_detect_termencoding ();
  232. if (str_isutf8 (_system_codepage))
  233. return g_strdup (str);
  234. conv = g_iconv_open (_system_codepage, "UTF-8");
  235. if (conv == INVALID_CONV)
  236. return g_strdup (str);
  237. buffer = g_string_new ("");
  238. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  239. {
  240. g_string_free (buffer, TRUE);
  241. str_close_conv (conv);
  242. return g_strdup (str);
  243. }
  244. str_close_conv (conv);
  245. return g_string_free (buffer, FALSE);
  246. }
  247. /* --------------------------------------------------------------------------------------------- */
  248. /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
  249. int
  250. tty_resize (int fd)
  251. {
  252. #if defined TIOCSWINSZ
  253. struct winsize tty_size;
  254. tty_size.ws_row = LINES;
  255. tty_size.ws_col = COLS;
  256. tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  257. return ioctl (fd, TIOCSWINSZ, &tty_size);
  258. #else
  259. return 0;
  260. #endif
  261. }
  262. /* --------------------------------------------------------------------------------------------- */
  263. /** Clear screen */
  264. void
  265. tty_clear_screen (void)
  266. {
  267. tty_set_normal_attrs ();
  268. tty_fill_region (0, 0, LINES, COLS, ' ');
  269. tty_refresh ();
  270. }
  271. /* --------------------------------------------------------------------------------------------- */
  272. void
  273. tty_init_xterm_support (gboolean is_xterm)
  274. {
  275. const char *termvalue;
  276. termvalue = getenv ("TERM");
  277. /* Check mouse and ca capabilities */
  278. /* terminfo/termcap structures have been already initialized,
  279. in slang_init() or/and init_curses() */
  280. /* Check terminfo at first, then check termcap */
  281. xmouse_seq = tty_tgetstr ("kmous");
  282. if (xmouse_seq == NULL)
  283. xmouse_seq = tty_tgetstr ("Km");
  284. smcup = tty_tgetstr ("smcup");
  285. if (smcup == NULL)
  286. smcup = tty_tgetstr ("ti");
  287. rmcup = tty_tgetstr ("rmcup");
  288. if (rmcup == NULL)
  289. rmcup = tty_tgetstr ("te");
  290. if (strcmp (termvalue, "cygwin") == 0)
  291. {
  292. is_xterm = TRUE;
  293. use_mouse_p = MOUSE_DISABLED;
  294. }
  295. if (is_xterm)
  296. {
  297. /* Default to the standard xterm sequence */
  298. if (xmouse_seq == NULL)
  299. xmouse_seq = ESC_STR "[M";
  300. /* Enable mouse unless explicitly disabled by --nomouse */
  301. if (use_mouse_p != MOUSE_DISABLED)
  302. {
  303. if (mc_global.tty.old_mouse)
  304. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  305. else
  306. {
  307. /* FIXME: this dirty hack to set supported type of tracking the mouse */
  308. const char *color_term = getenv ("COLORTERM");
  309. if (strncmp (termvalue, "rxvt", 4) == 0 ||
  310. (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
  311. strcmp (termvalue, "Eterm") == 0)
  312. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  313. else
  314. use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
  315. }
  316. }
  317. }
  318. /* There's only one termcap entry "kmous", typically containing "\E[M" or "\E[<".
  319. * We need the former in xmouse_seq, the latter in xmouse_extended_seq.
  320. * See tickets 2956, 3954, and 4063 for details. */
  321. if (xmouse_seq != NULL)
  322. {
  323. if (strcmp (xmouse_seq, ESC_STR "[<") == 0)
  324. xmouse_seq = ESC_STR "[M";
  325. xmouse_extended_seq = ESC_STR "[<";
  326. }
  327. }
  328. /* --------------------------------------------------------------------------------------------- */