tty.c 12 KB

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