2 Interface to the terminal controlling library.
4 Copyright (C) 2005, 2006, 2007, 2009, 2011
5 The 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 <unistd.h> /* exit() */
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
42 #include "lib/global.h"
43 #include "lib/strutil.h"
46 #include "tty-internal.h"
47 #include "mouse.h" /* use_mouse_p */
50 /*** global variables ****************************************************************************/
52 /* If true program softkeys (HP terminals only) on startup and after every
53 command ran in the subshell to the description found in the termcap/terminfo
55 int reset_hp_softkeys
= 0;
57 int mc_tty_frm
[MC_TTY_FRM_MAX
];
59 /*** file scope macro definitions ****************************************************************/
61 /*** file scope type declarations ****************************************************************/
63 /*** file scope variables ************************************************************************/
65 static SIG_ATOMIC_VOLATILE_T got_interrupt
= 0;
67 /*** file scope functions ************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
71 sigintr_handler (int signo
)
77 /* --------------------------------------------------------------------------------------------- */
78 /*** public functions ****************************************************************************/
79 /* --------------------------------------------------------------------------------------------- */
82 * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
84 * @param force_xterm Set forced the XTerm type
86 * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
87 * rxvt*, Eterm or dtterm
90 tty_check_term (gboolean force_xterm
)
92 const char *termvalue
;
95 termvalue
= getenv ("TERM");
96 if (termvalue
== NULL
|| *termvalue
== '\0')
98 fputs (_("The TERM environment variable is unset!\n"), stderr
);
102 xdisplay
= getenv ("DISPLAY");
103 if (xdisplay
!= NULL
&& *xdisplay
== '\0')
106 return force_xterm
|| strncmp (termvalue
, "xterm", 5) == 0
107 || strncmp (termvalue
, "konsole", 7) == 0
108 || strncmp (termvalue
, "rxvt", 4) == 0
109 || strcmp (termvalue
, "Eterm") == 0
110 || strcmp (termvalue
, "dtterm") == 0
111 || (strncmp (termvalue
, "screen", 6) == 0 && xdisplay
!= NULL
);
114 /* --------------------------------------------------------------------------------------------- */
117 tty_start_interrupt_key (void)
119 struct sigaction act
;
121 act
.sa_handler
= sigintr_handler
;
122 sigemptyset (&act
.sa_mask
);
123 act
.sa_flags
= SA_RESTART
;
124 sigaction (SIGINT
, &act
, NULL
);
127 /* --------------------------------------------------------------------------------------------- */
130 tty_enable_interrupt_key (void)
132 struct sigaction act
;
134 act
.sa_handler
= sigintr_handler
;
135 sigemptyset (&act
.sa_mask
);
137 sigaction (SIGINT
, &act
, NULL
);
141 /* --------------------------------------------------------------------------------------------- */
144 tty_disable_interrupt_key (void)
146 struct sigaction act
;
148 act
.sa_handler
= SIG_IGN
;
149 sigemptyset (&act
.sa_mask
);
151 sigaction (SIGINT
, &act
, NULL
);
154 /* --------------------------------------------------------------------------------------------- */
157 tty_got_interrupt (void)
161 rv
= (got_interrupt
!= 0);
166 /* --------------------------------------------------------------------------------------------- */
169 tty_print_one_hline (gboolean single
)
171 tty_print_alt_char (ACS_HLINE
, single
);
174 /* --------------------------------------------------------------------------------------------- */
177 tty_print_one_vline (gboolean single
)
179 tty_print_alt_char (ACS_VLINE
, single
);
182 /* --------------------------------------------------------------------------------------------- */
185 tty_draw_box (int y
, int x
, int ys
, int xs
, gboolean single
)
189 if (ys
<= 0 || xs
<= 0)
198 tty_draw_vline (y
, x
, mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
], ys
);
199 tty_draw_vline (y
, x2
, mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
], ys
);
200 tty_draw_hline (y
, x
, mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
], xs
);
201 tty_draw_hline (y2
, x
, mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
], xs
);
203 tty_print_alt_char (ACS_ULCORNER
, single
);
205 tty_print_alt_char (ACS_LLCORNER
, single
);
207 tty_print_alt_char (ACS_URCORNER
, single
);
209 tty_print_alt_char (ACS_LRCORNER
, single
);
212 /* --------------------------------------------------------------------------------------------- */
215 mc_tty_normalize_from_utf8 (const char *str
)
219 const char *_system_codepage
= str_detect_termencoding ();
221 if (str_isutf8 (_system_codepage
))
222 return g_strdup (str
);
224 conv
= g_iconv_open (_system_codepage
, "UTF-8");
225 if (conv
== INVALID_CONV
)
226 return g_strdup (str
);
228 buffer
= g_string_new ("");
230 if (str_convert (conv
, str
, buffer
) == ESTR_FAILURE
)
232 g_string_free (buffer
, TRUE
);
233 str_close_conv (conv
);
234 return g_strdup (str
);
236 str_close_conv (conv
);
238 return g_string_free (buffer
, FALSE
);
241 /* --------------------------------------------------------------------------------------------- */
243 /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
247 #if defined TIOCSWINSZ
248 struct winsize tty_size
;
250 tty_size
.ws_row
= LINES
;
251 tty_size
.ws_col
= COLS
;
252 tty_size
.ws_xpixel
= tty_size
.ws_ypixel
= 0;
254 return ioctl (fd
, TIOCSWINSZ
, &tty_size
);
260 /* --------------------------------------------------------------------------------------------- */
263 tty_init_xterm_support (gboolean is_xterm
)
265 const char *termvalue
;
267 termvalue
= getenv ("TERM");
269 /* Check mouse and ca capabilities */
270 /* terminfo/termcap structures have been already initialized,
271 in slang_init() or/and init_curses() */
272 /* Check terminfo at first, then check termcap */
273 xmouse_seq
= tty_tgetstr ("kmous");
274 if (xmouse_seq
== NULL
)
275 xmouse_seq
= tty_tgetstr ("Km");
276 smcup
= tty_tgetstr ("smcup");
278 smcup
= tty_tgetstr ("ti");
279 rmcup
= tty_tgetstr ("rmcup");
281 rmcup
= tty_tgetstr ("te");
283 if (strcmp (termvalue
, "cygwin") == 0)
286 use_mouse_p
= MOUSE_DISABLED
;
291 /* Default to the standard xterm sequence */
292 if (xmouse_seq
== NULL
)
293 xmouse_seq
= ESC_STR
"[M";
295 /* Enable mouse unless explicitly disabled by --nomouse */
296 if (use_mouse_p
!= MOUSE_DISABLED
)
298 if (mc_global
.tty
.old_mouse
)
299 use_mouse_p
= MOUSE_XTERM_NORMAL_TRACKING
;
302 /* FIXME: this dirty hack to set supported type of tracking the mouse */
303 const char *color_term
= getenv ("COLORTERM");
304 if (strncmp (termvalue
, "rxvt", 4) == 0 ||
305 (color_term
!= NULL
&& strncmp (color_term
, "rxvt", 4) == 0) ||
306 strcmp (termvalue
, "Eterm") == 0)
307 use_mouse_p
= MOUSE_XTERM_NORMAL_TRACKING
;
309 use_mouse_p
= MOUSE_XTERM_BUTTON_EVENT_TRACKING
;
314 /* No termcap for SGR extended mouse (yet), hardcode it for now */
315 if (xmouse_seq
!= NULL
)
316 xmouse_extended_seq
= ESC_STR
"[<";
319 /* --------------------------------------------------------------------------------------------- */