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

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