tty.c 12 KB

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