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

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