Ticket #2129: fix build with static GLib.
[midnight-commander.git] / lib / tty / tty.c
blobc803e1c75cb4d944610d9cffe1d64aaebe5a0b71
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;
93 const char *xdisplay;
95 termvalue = getenv ("TERM");
96 if (termvalue == NULL || *termvalue == '\0')
98 fputs (_("The TERM environment variable is unset!\n"), stderr);
99 exit (EXIT_FAILURE);
102 xdisplay = getenv ("DISPLAY");
103 if (xdisplay != NULL && *xdisplay == '\0')
104 xdisplay = NULL;
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 /* --------------------------------------------------------------------------------------------- */
116 extern void
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 /* --------------------------------------------------------------------------------------------- */
129 extern void
130 tty_enable_interrupt_key (void)
132 struct sigaction act;
134 act.sa_handler = sigintr_handler;
135 sigemptyset (&act.sa_mask);
136 act.sa_flags = 0;
137 sigaction (SIGINT, &act, NULL);
138 got_interrupt = 0;
141 /* --------------------------------------------------------------------------------------------- */
143 extern void
144 tty_disable_interrupt_key (void)
146 struct sigaction act;
148 act.sa_handler = SIG_IGN;
149 sigemptyset (&act.sa_mask);
150 act.sa_flags = 0;
151 sigaction (SIGINT, &act, NULL);
154 /* --------------------------------------------------------------------------------------------- */
156 extern gboolean
157 tty_got_interrupt (void)
159 gboolean rv;
161 rv = (got_interrupt != 0);
162 got_interrupt = 0;
163 return rv;
166 /* --------------------------------------------------------------------------------------------- */
168 void
169 tty_print_one_hline (gboolean single)
171 tty_print_alt_char (ACS_HLINE, single);
174 /* --------------------------------------------------------------------------------------------- */
176 void
177 tty_print_one_vline (gboolean single)
179 tty_print_alt_char (ACS_VLINE, single);
182 /* --------------------------------------------------------------------------------------------- */
184 void
185 tty_draw_box (int y, int x, int ys, int xs, gboolean single)
187 int y2, x2;
189 if (ys <= 0 || xs <= 0)
190 return;
192 ys--;
193 xs--;
195 y2 = y + ys;
196 x2 = x + xs;
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);
202 tty_gotoyx (y, x);
203 tty_print_alt_char (ACS_ULCORNER, single);
204 tty_gotoyx (y2, x);
205 tty_print_alt_char (ACS_LLCORNER, single);
206 tty_gotoyx (y, x2);
207 tty_print_alt_char (ACS_URCORNER, single);
208 tty_gotoyx (y2, x2);
209 tty_print_alt_char (ACS_LRCORNER, single);
212 /* --------------------------------------------------------------------------------------------- */
214 char *
215 mc_tty_normalize_from_utf8 (const char *str)
217 GIConv conv;
218 GString *buffer;
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 */
245 tty_resize (int fd)
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);
255 #else
256 return 0;
257 #endif
260 /* --------------------------------------------------------------------------------------------- */
262 void
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");
277 if (smcup == NULL)
278 smcup = tty_tgetstr ("ti");
279 rmcup = tty_tgetstr ("rmcup");
280 if (rmcup == NULL)
281 rmcup = tty_tgetstr ("te");
283 if (strcmp (termvalue, "cygwin") == 0)
285 is_xterm = TRUE;
286 use_mouse_p = MOUSE_DISABLED;
289 if (is_xterm)
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;
300 else
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;
308 else
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 /* --------------------------------------------------------------------------------------------- */