tty.c 11 KB

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