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
;
94 termvalue
= getenv ("TERM");
95 if (termvalue
== NULL
|| *termvalue
== '\0')
97 fputs (_("The TERM environment variable is unset!\n"), stderr
);
101 return force_xterm
|| strncmp (termvalue
, "xterm", 5) == 0
102 || strncmp (termvalue
, "konsole", 7) == 0
103 || strncmp (termvalue
, "rxvt", 4) == 0
104 || strcmp (termvalue
, "Eterm") == 0 || strcmp (termvalue
, "dtterm") == 0;
107 /* --------------------------------------------------------------------------------------------- */
110 tty_start_interrupt_key (void)
112 struct sigaction act
;
114 act
.sa_handler
= sigintr_handler
;
115 sigemptyset (&act
.sa_mask
);
116 act
.sa_flags
= SA_RESTART
;
117 sigaction (SIGINT
, &act
, NULL
);
120 /* --------------------------------------------------------------------------------------------- */
123 tty_enable_interrupt_key (void)
125 struct sigaction act
;
127 act
.sa_handler
= sigintr_handler
;
128 sigemptyset (&act
.sa_mask
);
130 sigaction (SIGINT
, &act
, NULL
);
134 /* --------------------------------------------------------------------------------------------- */
137 tty_disable_interrupt_key (void)
139 struct sigaction act
;
141 act
.sa_handler
= SIG_IGN
;
142 sigemptyset (&act
.sa_mask
);
144 sigaction (SIGINT
, &act
, NULL
);
147 /* --------------------------------------------------------------------------------------------- */
150 tty_got_interrupt (void)
154 rv
= (got_interrupt
!= 0);
159 /* --------------------------------------------------------------------------------------------- */
162 tty_print_one_hline (gboolean single
)
164 tty_print_alt_char (ACS_HLINE
, single
);
167 /* --------------------------------------------------------------------------------------------- */
170 tty_print_one_vline (gboolean single
)
172 tty_print_alt_char (ACS_VLINE
, single
);
175 /* --------------------------------------------------------------------------------------------- */
178 tty_draw_box (int y
, int x
, int ys
, int xs
, gboolean single
)
182 if (ys
<= 0 || xs
<= 0)
191 tty_draw_vline (y
, x
, mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
], ys
);
192 tty_draw_vline (y
, x2
, mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
], ys
);
193 tty_draw_hline (y
, x
, mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
], xs
);
194 tty_draw_hline (y2
, x
, mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
], xs
);
196 tty_print_alt_char (ACS_ULCORNER
, single
);
198 tty_print_alt_char (ACS_LLCORNER
, single
);
200 tty_print_alt_char (ACS_URCORNER
, single
);
202 tty_print_alt_char (ACS_LRCORNER
, single
);
205 /* --------------------------------------------------------------------------------------------- */
208 mc_tty_normalize_from_utf8 (const char *str
)
212 const char *_system_codepage
= str_detect_termencoding ();
214 if (str_isutf8 (_system_codepage
))
215 return g_strdup (str
);
217 conv
= g_iconv_open (_system_codepage
, "UTF-8");
218 if (conv
== INVALID_CONV
)
219 return g_strdup (str
);
221 buffer
= g_string_new ("");
223 if (str_convert (conv
, str
, buffer
) == ESTR_FAILURE
)
225 g_string_free (buffer
, TRUE
);
226 str_close_conv (conv
);
227 return g_strdup (str
);
229 str_close_conv (conv
);
231 return g_string_free (buffer
, FALSE
);
234 /* --------------------------------------------------------------------------------------------- */
236 /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
240 #if defined TIOCSWINSZ
241 struct winsize tty_size
;
243 tty_size
.ws_row
= LINES
;
244 tty_size
.ws_col
= COLS
;
245 tty_size
.ws_xpixel
= tty_size
.ws_ypixel
= 0;
247 return ioctl (fd
, TIOCSWINSZ
, &tty_size
);
253 /* --------------------------------------------------------------------------------------------- */
256 tty_init_xterm_support (gboolean is_xterm
)
258 const char *termvalue
;
260 termvalue
= getenv ("TERM");
262 /* Check mouse and ca capabilities */
263 /* terminfo/termcap structures have been already initialized,
264 in slang_init() or/and init_curses() */
265 /* Check terminfo at first, then check termcap */
266 xmouse_seq
= tty_tgetstr ("kmous");
267 if (xmouse_seq
== NULL
)
268 xmouse_seq
= tty_tgetstr ("Km");
269 smcup
= tty_tgetstr ("smcup");
271 smcup
= tty_tgetstr ("ti");
272 rmcup
= tty_tgetstr ("rmcup");
274 rmcup
= tty_tgetstr ("te");
276 if (strcmp (termvalue
, "cygwin") == 0)
279 use_mouse_p
= MOUSE_DISABLED
;
284 /* Default to the standard xterm sequence */
285 if (xmouse_seq
== NULL
)
286 xmouse_seq
= ESC_STR
"[M";
288 /* Enable mouse unless explicitly disabled by --nomouse */
289 if (use_mouse_p
!= MOUSE_DISABLED
)
291 if (mc_global
.tty
.old_mouse
)
292 use_mouse_p
= MOUSE_XTERM_NORMAL_TRACKING
;
295 /* FIXME: this dirty hack to set supported type of tracking the mouse */
296 const char *color_term
= getenv ("COLORTERM");
297 if (strncmp (termvalue
, "rxvt", 4) == 0 ||
298 (color_term
!= NULL
&& strncmp (color_term
, "rxvt", 4) == 0) ||
299 strcmp (termvalue
, "Eterm") == 0)
300 use_mouse_p
= MOUSE_XTERM_NORMAL_TRACKING
;
302 use_mouse_p
= MOUSE_XTERM_BUTTON_EVENT_TRACKING
;
308 /* --------------------------------------------------------------------------------------------- */