2 Interface to the terminal controlling library.
4 Copyright (C) 2005-2017
5 Free Software Foundation, Inc.
8 Roland Illig <roland.illig@gmx.de>, 2005.
9 Andrew Borodin <aborodin@vmail.ru>, 2009.
11 This file is part of the Midnight Commander.
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.
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.
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/>.
28 * \brief Source: %interface to the terminal controlling library
36 #include <string.h> /* memset() */
37 #include <unistd.h> /* exit() */
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
43 #include "lib/global.h"
44 #include "lib/strutil.h"
47 #include "tty-internal.h"
48 #include "mouse.h" /* use_mouse_p */
51 /*** global variables ****************************************************************************/
53 int mc_tty_frm
[MC_TTY_FRM_MAX
];
55 /*** file scope macro definitions ****************************************************************/
57 /*** file scope type declarations ****************************************************************/
59 /*** file scope variables ************************************************************************/
61 static SIG_ATOMIC_VOLATILE_T got_interrupt
= 0;
63 /*** file scope functions ************************************************************************/
64 /* --------------------------------------------------------------------------------------------- */
67 sigintr_handler (int signo
)
73 /* --------------------------------------------------------------------------------------------- */
74 /*** public functions ****************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
78 * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
80 * @param force_xterm Set forced the XTerm type
82 * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
83 * rxvt*, Eterm or dtterm
86 tty_check_term (gboolean force_xterm
)
88 const char *termvalue
;
91 termvalue
= getenv ("TERM");
92 if (termvalue
== NULL
|| *termvalue
== '\0')
94 fputs (_("The TERM environment variable is unset!\n"), stderr
);
98 xdisplay
= getenv ("DISPLAY");
99 if (xdisplay
!= NULL
&& *xdisplay
== '\0')
102 return force_xterm
|| strncmp (termvalue
, "xterm", 5) == 0
103 || strncmp (termvalue
, "konsole", 7) == 0
104 || strncmp (termvalue
, "rxvt", 4) == 0
105 || strcmp (termvalue
, "Eterm") == 0
106 || strcmp (termvalue
, "dtterm") == 0
107 || (strncmp (termvalue
, "screen", 6) == 0 && xdisplay
!= NULL
);
110 /* --------------------------------------------------------------------------------------------- */
113 tty_start_interrupt_key (void)
115 struct sigaction act
;
117 memset (&act
, 0, sizeof (act
));
118 act
.sa_handler
= sigintr_handler
;
119 sigemptyset (&act
.sa_mask
);
121 act
.sa_flags
= SA_RESTART
;
122 #endif /* SA_RESTART */
123 sigaction (SIGINT
, &act
, NULL
);
126 /* --------------------------------------------------------------------------------------------- */
129 tty_enable_interrupt_key (void)
131 struct sigaction act
;
133 memset (&act
, 0, sizeof (act
));
134 act
.sa_handler
= sigintr_handler
;
135 sigemptyset (&act
.sa_mask
);
136 sigaction (SIGINT
, &act
, NULL
);
140 /* --------------------------------------------------------------------------------------------- */
143 tty_disable_interrupt_key (void)
145 struct sigaction act
;
147 memset (&act
, 0, sizeof (act
));
148 act
.sa_handler
= SIG_IGN
;
149 sigemptyset (&act
.sa_mask
);
150 sigaction (SIGINT
, &act
, NULL
);
153 /* --------------------------------------------------------------------------------------------- */
156 tty_got_interrupt (void)
160 rv
= (got_interrupt
!= 0);
165 /* --------------------------------------------------------------------------------------------- */
168 tty_print_one_hline (gboolean single
)
170 tty_print_alt_char (ACS_HLINE
, single
);
173 /* --------------------------------------------------------------------------------------------- */
176 tty_print_one_vline (gboolean single
)
178 tty_print_alt_char (ACS_VLINE
, single
);
181 /* --------------------------------------------------------------------------------------------- */
184 tty_draw_box (int y
, int x
, int ys
, int xs
, gboolean single
)
188 if (ys
<= 0 || xs
<= 0)
197 tty_draw_vline (y
, x
, mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
], ys
);
198 tty_draw_vline (y
, x2
, mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
], ys
);
199 tty_draw_hline (y
, x
, mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
], xs
);
200 tty_draw_hline (y2
, x
, mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
], xs
);
202 tty_print_alt_char (ACS_ULCORNER
, single
);
204 tty_print_alt_char (ACS_LLCORNER
, single
);
206 tty_print_alt_char (ACS_URCORNER
, single
);
208 tty_print_alt_char (ACS_LRCORNER
, single
);
211 /* --------------------------------------------------------------------------------------------- */
214 mc_tty_normalize_from_utf8 (const char *str
)
218 const char *_system_codepage
= str_detect_termencoding ();
220 if (str_isutf8 (_system_codepage
))
221 return g_strdup (str
);
223 conv
= g_iconv_open (_system_codepage
, "UTF-8");
224 if (conv
== INVALID_CONV
)
225 return g_strdup (str
);
227 buffer
= g_string_new ("");
229 if (str_convert (conv
, str
, buffer
) == ESTR_FAILURE
)
231 g_string_free (buffer
, TRUE
);
232 str_close_conv (conv
);
233 return g_strdup (str
);
235 str_close_conv (conv
);
237 return g_string_free (buffer
, FALSE
);
240 /* --------------------------------------------------------------------------------------------- */
242 /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
246 #if defined TIOCSWINSZ
247 struct winsize tty_size
;
249 tty_size
.ws_row
= LINES
;
250 tty_size
.ws_col
= COLS
;
251 tty_size
.ws_xpixel
= tty_size
.ws_ypixel
= 0;
253 return ioctl (fd
, TIOCSWINSZ
, &tty_size
);
259 /* --------------------------------------------------------------------------------------------- */
262 tty_init_xterm_support (gboolean is_xterm
)
264 const char *termvalue
;
266 termvalue
= getenv ("TERM");
268 /* Check mouse and ca capabilities */
269 /* terminfo/termcap structures have been already initialized,
270 in slang_init() or/and init_curses() */
271 /* Check terminfo at first, then check termcap */
272 xmouse_seq
= tty_tgetstr ("kmous");
273 if (xmouse_seq
== NULL
)
274 xmouse_seq
= tty_tgetstr ("Km");
275 smcup
= tty_tgetstr ("smcup");
277 smcup
= tty_tgetstr ("ti");
278 rmcup
= tty_tgetstr ("rmcup");
280 rmcup
= tty_tgetstr ("te");
282 if (strcmp (termvalue
, "cygwin") == 0)
285 use_mouse_p
= MOUSE_DISABLED
;
290 /* Default to the standard xterm sequence */
291 if (xmouse_seq
== NULL
)
292 xmouse_seq
= ESC_STR
"[M";
294 /* Enable mouse unless explicitly disabled by --nomouse */
295 if (use_mouse_p
!= MOUSE_DISABLED
)
297 if (mc_global
.tty
.old_mouse
)
298 use_mouse_p
= MOUSE_XTERM_NORMAL_TRACKING
;
301 /* FIXME: this dirty hack to set supported type of tracking the mouse */
302 const char *color_term
= getenv ("COLORTERM");
303 if (strncmp (termvalue
, "rxvt", 4) == 0 ||
304 (color_term
!= NULL
&& strncmp (color_term
, "rxvt", 4) == 0) ||
305 strcmp (termvalue
, "Eterm") == 0)
306 use_mouse_p
= MOUSE_XTERM_NORMAL_TRACKING
;
308 use_mouse_p
= MOUSE_XTERM_BUTTON_EVENT_TRACKING
;
313 /* No termcap for SGR extended mouse (yet), hardcode it for now */
314 if (xmouse_seq
!= NULL
)
315 xmouse_extended_seq
= ESC_STR
"[<";
318 /* --------------------------------------------------------------------------------------------- */