Ticket #3710: don't parse "window-state-char" and "window-close-char" as colors.
[midnight-commander.git] / lib / tty / tty.c
bloba90580c66db9896f72eef4612ac9f7d629408a0e
1 /*
2 Interface to the terminal controlling library.
4 Copyright (C) 2005-2016
5 Free Software Foundation, Inc.
7 Written by:
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/>.
27 /** \file tty.c
28 * \brief Source: %interface to the terminal controlling library
31 #include <config.h>
33 #include <signal.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h> /* memset() */
37 #include <unistd.h> /* exit() */
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
43 #include "lib/global.h"
44 #include "lib/strutil.h"
46 #include "tty.h"
47 #include "tty-internal.h"
48 #include "mouse.h" /* use_mouse_p */
49 #include "win.h"
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 /* --------------------------------------------------------------------------------------------- */
66 static void
67 sigintr_handler (int signo)
69 (void) &signo;
70 got_interrupt = 1;
73 /* --------------------------------------------------------------------------------------------- */
74 /*** public functions ****************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
77 /**
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
85 gboolean
86 tty_check_term (gboolean force_xterm)
88 const char *termvalue;
89 const char *xdisplay;
91 termvalue = getenv ("TERM");
92 if (termvalue == NULL || *termvalue == '\0')
94 fputs (_("The TERM environment variable is unset!\n"), stderr);
95 exit (EXIT_FAILURE);
98 xdisplay = getenv ("DISPLAY");
99 if (xdisplay != NULL && *xdisplay == '\0')
100 xdisplay = NULL;
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 /* --------------------------------------------------------------------------------------------- */
112 extern void
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);
120 #ifdef SA_RESTART
121 act.sa_flags = SA_RESTART;
122 #endif /* SA_RESTART */
123 sigaction (SIGINT, &act, NULL);
126 /* --------------------------------------------------------------------------------------------- */
128 extern void
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);
137 got_interrupt = 0;
140 /* --------------------------------------------------------------------------------------------- */
142 extern void
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 /* --------------------------------------------------------------------------------------------- */
155 extern gboolean
156 tty_got_interrupt (void)
158 gboolean rv;
160 rv = (got_interrupt != 0);
161 got_interrupt = 0;
162 return rv;
165 /* --------------------------------------------------------------------------------------------- */
167 void
168 tty_print_one_hline (gboolean single)
170 tty_print_alt_char (ACS_HLINE, single);
173 /* --------------------------------------------------------------------------------------------- */
175 void
176 tty_print_one_vline (gboolean single)
178 tty_print_alt_char (ACS_VLINE, single);
181 /* --------------------------------------------------------------------------------------------- */
183 void
184 tty_draw_box (int y, int x, int ys, int xs, gboolean single)
186 int y2, x2;
188 if (ys <= 0 || xs <= 0)
189 return;
191 ys--;
192 xs--;
194 y2 = y + ys;
195 x2 = x + xs;
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);
201 tty_gotoyx (y, x);
202 tty_print_alt_char (ACS_ULCORNER, single);
203 tty_gotoyx (y2, x);
204 tty_print_alt_char (ACS_LLCORNER, single);
205 tty_gotoyx (y, x2);
206 tty_print_alt_char (ACS_URCORNER, single);
207 tty_gotoyx (y2, x2);
208 tty_print_alt_char (ACS_LRCORNER, single);
211 /* --------------------------------------------------------------------------------------------- */
213 char *
214 mc_tty_normalize_from_utf8 (const char *str)
216 GIConv conv;
217 GString *buffer;
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 */
244 tty_resize (int fd)
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);
254 #else
255 return 0;
256 #endif
259 /* --------------------------------------------------------------------------------------------- */
261 void
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");
276 if (smcup == NULL)
277 smcup = tty_tgetstr ("ti");
278 rmcup = tty_tgetstr ("rmcup");
279 if (rmcup == NULL)
280 rmcup = tty_tgetstr ("te");
282 if (strcmp (termvalue, "cygwin") == 0)
284 is_xterm = TRUE;
285 use_mouse_p = MOUSE_DISABLED;
288 if (is_xterm)
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;
299 else
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;
307 else
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 /* --------------------------------------------------------------------------------------------- */