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. mc_tty_normalize_from_utf8
  13. tty_resize
  14. tty_init_xterm_support

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

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