Merge branch '2944_cleanup'
[midnight-commander.git] / lib / tty / tty.c
blob564837ef70825bee38ad85c85fed8af2972d5f4b
1 /*
2 Interface to the terminal controlling library.
4 Copyright (C) 2005, 2006, 2007, 2009, 2011
5 The 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 <unistd.h> /* exit() */
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
42 #include "lib/global.h"
43 #include "lib/strutil.h"
45 #include "tty.h"
46 #include "tty-internal.h"
47 #include "mouse.h" /* use_mouse_p */
48 #include "win.h"
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
54 database */
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 /* --------------------------------------------------------------------------------------------- */
70 static void
71 sigintr_handler (int signo)
73 (void) &signo;
74 got_interrupt = 1;
77 /* --------------------------------------------------------------------------------------------- */
78 /*** public functions ****************************************************************************/
79 /* --------------------------------------------------------------------------------------------- */
81 /**
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
89 gboolean
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);
98 exit (EXIT_FAILURE);
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 /* --------------------------------------------------------------------------------------------- */
109 extern void
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 /* --------------------------------------------------------------------------------------------- */
122 extern void
123 tty_enable_interrupt_key (void)
125 struct sigaction act;
127 act.sa_handler = sigintr_handler;
128 sigemptyset (&act.sa_mask);
129 act.sa_flags = 0;
130 sigaction (SIGINT, &act, NULL);
131 got_interrupt = 0;
134 /* --------------------------------------------------------------------------------------------- */
136 extern void
137 tty_disable_interrupt_key (void)
139 struct sigaction act;
141 act.sa_handler = SIG_IGN;
142 sigemptyset (&act.sa_mask);
143 act.sa_flags = 0;
144 sigaction (SIGINT, &act, NULL);
147 /* --------------------------------------------------------------------------------------------- */
149 extern gboolean
150 tty_got_interrupt (void)
152 gboolean rv;
154 rv = (got_interrupt != 0);
155 got_interrupt = 0;
156 return rv;
159 /* --------------------------------------------------------------------------------------------- */
161 void
162 tty_print_one_hline (gboolean single)
164 tty_print_alt_char (ACS_HLINE, single);
167 /* --------------------------------------------------------------------------------------------- */
169 void
170 tty_print_one_vline (gboolean single)
172 tty_print_alt_char (ACS_VLINE, single);
175 /* --------------------------------------------------------------------------------------------- */
177 void
178 tty_draw_box (int y, int x, int ys, int xs, gboolean single)
180 int y2, x2;
182 if (ys <= 0 || xs <= 0)
183 return;
185 ys--;
186 xs--;
188 y2 = y + ys;
189 x2 = x + xs;
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);
195 tty_gotoyx (y, x);
196 tty_print_alt_char (ACS_ULCORNER, single);
197 tty_gotoyx (y2, x);
198 tty_print_alt_char (ACS_LLCORNER, single);
199 tty_gotoyx (y, x2);
200 tty_print_alt_char (ACS_URCORNER, single);
201 tty_gotoyx (y2, x2);
202 tty_print_alt_char (ACS_LRCORNER, single);
205 /* --------------------------------------------------------------------------------------------- */
207 char *
208 mc_tty_normalize_from_utf8 (const char *str)
210 GIConv conv;
211 GString *buffer;
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 */
238 tty_resize (int fd)
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);
248 #else
249 return 0;
250 #endif
253 /* --------------------------------------------------------------------------------------------- */
255 void
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");
270 if (smcup == NULL)
271 smcup = tty_tgetstr ("ti");
272 rmcup = tty_tgetstr ("rmcup");
273 if (rmcup == NULL)
274 rmcup = tty_tgetstr ("te");
276 if (strcmp (termvalue, "cygwin") == 0)
278 is_xterm = TRUE;
279 use_mouse_p = MOUSE_DISABLED;
282 if (is_xterm)
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;
293 else
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;
301 else
302 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
308 /* --------------------------------------------------------------------------------------------- */