root/lib/tty/tty.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. sigintr_handler
  2. tty_check_term
  3. tty_start_interrupt_key
  4. tty_enable_interrupt_key
  5. tty_disable_interrupt_key
  6. tty_got_interrupt
  7. tty_got_winch
  8. tty_flush_winch
  9. tty_print_one_hline
  10. tty_print_one_vline
  11. tty_draw_box
  12. tty_draw_box_shadow
  13. mc_tty_normalize_from_utf8
  14. tty_resize
  15. tty_clear_screen
  16. tty_init_xterm_support

   1 /*
   2    Interface to the terminal controlling library.
   3 
   4    Copyright (C) 2005-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Roland Illig <roland.illig@gmx.de>, 2005.
   9    Andrew Borodin <aborodin@vmail.ru>, 2009.
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 /** \file tty.c
  28  *  \brief Source: %interface to the terminal controlling library
  29  */
  30 
  31 #include <config.h>
  32 
  33 #include <errno.h>
  34 #include <signal.h>
  35 #include <stdarg.h>
  36 #include <stdlib.h>
  37 #include <string.h>             /* memset() */
  38 
  39 #ifdef HAVE_SYS_SELECT_H
  40 #include <sys/select.h>
  41 #else
  42 #include <sys/time.h>
  43 #include <sys/types.h>
  44 #endif
  45 #include <unistd.h>             /* exit() */
  46 
  47 #ifdef HAVE_SYS_IOCTL_H
  48 #include <sys/ioctl.h>
  49 #endif
  50 
  51 /* In some systems (like Solaris 11.4 SPARC), TIOCSWINSZ is defined in termios.h */
  52 #include <termios.h>
  53 
  54 #include "lib/global.h"
  55 #include "lib/strutil.h"
  56 #include "lib/util.h"
  57 
  58 #include "tty.h"
  59 #include "tty-internal.h"
  60 #include "color.h"              /* tty_set_normal_attrs() */
  61 #include "mouse.h"              /* use_mouse_p */
  62 #include "win.h"
  63 
  64 /*** global variables ****************************************************************************/
  65 
  66 int mc_tty_frm[MC_TTY_FRM_MAX];
  67 
  68 /*** file scope macro definitions ****************************************************************/
  69 
  70 /*** file scope type declarations ****************************************************************/
  71 
  72 /*** forward declarations (file scope functions) *************************************************/
  73 
  74 /*** file scope variables ************************************************************************/
  75 
  76 static SIG_ATOMIC_VOLATILE_T got_interrupt = 0;
  77 
  78 /* --------------------------------------------------------------------------------------------- */
  79 /*** file scope functions ************************************************************************/
  80 /* --------------------------------------------------------------------------------------------- */
  81 
  82 static void
  83 sigintr_handler (int signo)
     /* [previous][next][first][last][top][bottom][index][help]  */
  84 {
  85     (void) &signo;
  86     got_interrupt = 1;
  87 }
  88 
  89 /* --------------------------------------------------------------------------------------------- */
  90 /*** public functions ****************************************************************************/
  91 /* --------------------------------------------------------------------------------------------- */
  92 
  93 /**
  94  * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
  95  *
  96  * @param force_xterm Set forced the XTerm type
  97  *
  98  * @return true if @param force_xterm is true or value of $TERM is one of following:
  99  *         term*
 100  *         konsole*
 101  *         rxvt*
 102  *         Eterm
 103  *         dtterm
 104  *         alacritty*
 105  *         foot*
 106  *         screen*
 107  *         tmux*
 108  *         contour*
 109  */
 110 gboolean
 111 tty_check_term (gboolean force_xterm)
     /* [previous][next][first][last][top][bottom][index][help]  */
 112 {
 113     const char *termvalue;
 114 
 115     termvalue = getenv ("TERM");
 116     if (termvalue == NULL || *termvalue == '\0')
 117     {
 118         fputs (_("The TERM environment variable is unset!\n"), stderr);
 119         exit (EXIT_FAILURE);
 120     }
 121 
 122     /* *INDENT-OFF* */
 123     return force_xterm
 124         || strncmp (termvalue, "xterm", 5) == 0
 125         || strncmp (termvalue, "konsole", 7) == 0
 126         || strncmp (termvalue, "rxvt", 4) == 0
 127         || strcmp (termvalue, "Eterm") == 0
 128         || strcmp (termvalue, "dtterm") == 0
 129         || strncmp (termvalue, "alacritty", 9) == 0
 130         || strncmp (termvalue, "foot", 4) == 0
 131         || strncmp (termvalue, "screen", 6) == 0
 132         || strncmp (termvalue, "tmux", 4) == 0
 133         || strncmp (termvalue, "contour", 7) == 0;
 134     /* *INDENT-ON* */
 135 }
 136 
 137 /* --------------------------------------------------------------------------------------------- */
 138 
 139 extern void
 140 tty_start_interrupt_key (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 141 {
 142     struct sigaction act;
 143 
 144     memset (&act, 0, sizeof (act));
 145     act.sa_handler = sigintr_handler;
 146     sigemptyset (&act.sa_mask);
 147 #ifdef SA_RESTART
 148     act.sa_flags = SA_RESTART;
 149 #endif /* SA_RESTART */
 150     my_sigaction (SIGINT, &act, NULL);
 151 }
 152 
 153 /* --------------------------------------------------------------------------------------------- */
 154 
 155 extern void
 156 tty_enable_interrupt_key (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 157 {
 158     struct sigaction act;
 159 
 160     memset (&act, 0, sizeof (act));
 161     act.sa_handler = sigintr_handler;
 162     sigemptyset (&act.sa_mask);
 163     my_sigaction (SIGINT, &act, NULL);
 164     got_interrupt = 0;
 165 }
 166 
 167 /* --------------------------------------------------------------------------------------------- */
 168 
 169 extern void
 170 tty_disable_interrupt_key (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 171 {
 172     struct sigaction act;
 173 
 174     memset (&act, 0, sizeof (act));
 175     act.sa_handler = SIG_IGN;
 176     sigemptyset (&act.sa_mask);
 177     my_sigaction (SIGINT, &act, NULL);
 178 }
 179 
 180 /* --------------------------------------------------------------------------------------------- */
 181 
 182 extern gboolean
 183 tty_got_interrupt (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 184 {
 185     gboolean rv;
 186 
 187     rv = (got_interrupt != 0);
 188     got_interrupt = 0;
 189     return rv;
 190 }
 191 
 192 /* --------------------------------------------------------------------------------------------- */
 193 
 194 gboolean
 195 tty_got_winch (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 196 {
 197     fd_set fdset;
 198     /* *INDENT-OFF* */
 199     /* instant timeout */
 200     struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 };
 201     /* *INDENT-ON* */
 202     int ok;
 203 
 204     FD_ZERO (&fdset);
 205     FD_SET (sigwinch_pipe[0], &fdset);
 206 
 207     while ((ok = select (sigwinch_pipe[0] + 1, &fdset, NULL, NULL, &timeout)) < 0)
 208         if (errno != EINTR)
 209         {
 210             perror (_("Cannot check SIGWINCH pipe"));
 211             exit (EXIT_FAILURE);
 212         }
 213 
 214     return (ok != 0 && FD_ISSET (sigwinch_pipe[0], &fdset));
 215 }
 216 
 217 /* --------------------------------------------------------------------------------------------- */
 218 
 219 void
 220 tty_flush_winch (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 221 {
 222     ssize_t n;
 223 
 224     /* merge all SIGWINCH events raised to this moment */
 225     do
 226     {
 227         char x[16];
 228 
 229         /* read multiple events at a time  */
 230         n = read (sigwinch_pipe[0], &x, sizeof (x));
 231     }
 232     while (n > 0 || (n == -1 && errno == EINTR));
 233 }
 234 
 235 /* --------------------------------------------------------------------------------------------- */
 236 
 237 void
 238 tty_print_one_hline (gboolean single)
     /* [previous][next][first][last][top][bottom][index][help]  */
 239 {
 240     tty_print_alt_char (ACS_HLINE, single);
 241 }
 242 
 243 /* --------------------------------------------------------------------------------------------- */
 244 
 245 void
 246 tty_print_one_vline (gboolean single)
     /* [previous][next][first][last][top][bottom][index][help]  */
 247 {
 248     tty_print_alt_char (ACS_VLINE, single);
 249 }
 250 
 251 /* --------------------------------------------------------------------------------------------- */
 252 
 253 void
 254 tty_draw_box (int y, int x, int ys, int xs, gboolean single)
     /* [previous][next][first][last][top][bottom][index][help]  */
 255 {
 256     int y2, x2;
 257 
 258     if (ys <= 0 || xs <= 0)
 259         return;
 260 
 261     ys--;
 262     xs--;
 263 
 264     y2 = y + ys;
 265     x2 = x + xs;
 266 
 267     tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
 268     tty_draw_vline (y, x2, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
 269     tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
 270     tty_draw_hline (y2, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
 271     tty_gotoyx (y, x);
 272     tty_print_alt_char (ACS_ULCORNER, single);
 273     tty_gotoyx (y2, x);
 274     tty_print_alt_char (ACS_LLCORNER, single);
 275     tty_gotoyx (y, x2);
 276     tty_print_alt_char (ACS_URCORNER, single);
 277     tty_gotoyx (y2, x2);
 278     tty_print_alt_char (ACS_LRCORNER, single);
 279 }
 280 
 281 /* --------------------------------------------------------------------------------------------- */
 282 
 283 void
 284 tty_draw_box_shadow (int y, int x, int rows, int cols, int shadow_color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 285 {
 286     /* draw right shadow */
 287     tty_colorize_area (y + 1, x + cols, rows - 1, 2, shadow_color);
 288     /* draw bottom shadow */
 289     tty_colorize_area (y + rows, x + 2, 1, cols, shadow_color);
 290 }
 291 
 292 /* --------------------------------------------------------------------------------------------- */
 293 
 294 char *
 295 mc_tty_normalize_from_utf8 (const char *str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 296 {
 297     GIConv conv;
 298     GString *buffer;
 299     const char *_system_codepage = str_detect_termencoding ();
 300 
 301     if (str_isutf8 (_system_codepage))
 302         return g_strdup (str);
 303 
 304     conv = g_iconv_open (_system_codepage, "UTF-8");
 305     if (conv == INVALID_CONV)
 306         return g_strdup (str);
 307 
 308     buffer = g_string_new ("");
 309 
 310     if (str_convert (conv, str, buffer) == ESTR_FAILURE)
 311     {
 312         g_string_free (buffer, TRUE);
 313         str_close_conv (conv);
 314         return g_strdup (str);
 315     }
 316     str_close_conv (conv);
 317 
 318     return g_string_free (buffer, FALSE);
 319 }
 320 
 321 /* --------------------------------------------------------------------------------------------- */
 322 
 323 /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
 324 int
 325 tty_resize (int fd)
     /* [previous][next][first][last][top][bottom][index][help]  */
 326 {
 327 #if defined TIOCSWINSZ
 328     struct winsize tty_size;
 329 
 330     tty_size.ws_row = LINES;
 331     tty_size.ws_col = COLS;
 332     tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
 333 
 334     return ioctl (fd, TIOCSWINSZ, &tty_size);
 335 #else
 336     return 0;
 337 #endif
 338 }
 339 
 340 /* --------------------------------------------------------------------------------------------- */
 341 
 342 /** Clear screen */
 343 void
 344 tty_clear_screen (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 345 {
 346     tty_set_normal_attrs ();
 347     tty_fill_region (0, 0, LINES, COLS, ' ');
 348     tty_refresh ();
 349 }
 350 
 351 /* --------------------------------------------------------------------------------------------- */
 352 
 353 void
 354 tty_init_xterm_support (gboolean is_xterm)
     /* [previous][next][first][last][top][bottom][index][help]  */
 355 {
 356     const char *termvalue;
 357 
 358     termvalue = getenv ("TERM");
 359 
 360     /* Check mouse and ca capabilities */
 361     /* terminfo/termcap structures have been already initialized,
 362        in slang_init() or/and init_curses()  */
 363     /* Check terminfo at first, then check termcap */
 364     xmouse_seq = tty_tgetstr ("kmous");
 365     if (xmouse_seq == NULL)
 366         xmouse_seq = tty_tgetstr ("Km");
 367     smcup = tty_tgetstr ("smcup");
 368     if (smcup == NULL)
 369         smcup = tty_tgetstr ("ti");
 370     rmcup = tty_tgetstr ("rmcup");
 371     if (rmcup == NULL)
 372         rmcup = tty_tgetstr ("te");
 373 
 374     if (strcmp (termvalue, "cygwin") == 0)
 375     {
 376         is_xterm = TRUE;
 377         use_mouse_p = MOUSE_DISABLED;
 378     }
 379 
 380     if (is_xterm)
 381     {
 382         /* Default to the standard xterm sequence */
 383         if (xmouse_seq == NULL)
 384             xmouse_seq = ESC_STR "[M";
 385 
 386         /* Enable mouse unless explicitly disabled by --nomouse */
 387         if (use_mouse_p != MOUSE_DISABLED)
 388         {
 389             if (mc_global.tty.old_mouse)
 390                 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
 391             else
 392             {
 393                 /* FIXME: this dirty hack to set supported type of tracking the mouse */
 394                 const char *color_term = getenv ("COLORTERM");
 395                 if (strncmp (termvalue, "rxvt", 4) == 0 ||
 396                     (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
 397                     strcmp (termvalue, "Eterm") == 0)
 398                     use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
 399                 else
 400                     use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
 401             }
 402         }
 403     }
 404 
 405     /* There's only one termcap entry "kmous", typically containing "\E[M" or "\E[<".
 406      * We need the former in xmouse_seq, the latter in xmouse_extended_seq.
 407      * See tickets 2956, 3954, and 4063 for details. */
 408     if (xmouse_seq != NULL)
 409     {
 410         if (strcmp (xmouse_seq, ESC_STR "[<") == 0)
 411             xmouse_seq = ESC_STR "[M";
 412 
 413         xmouse_extended_seq = ESC_STR "[<";
 414     }
 415 }
 416 
 417 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */