-
Notifications
You must be signed in to change notification settings - Fork 523
/
tty.c
318 lines (251 loc) · 9.05 KB
/
tty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Interface to the terminal controlling library.
Copyright (C) 2005-2017
Free Software Foundation, Inc.
Written by:
Roland Illig <roland.illig@gmx.de>, 2005.
Andrew Borodin <aborodin@vmail.ru>, 2009.
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file tty.c
* \brief Source: %interface to the terminal controlling library
*/
#include <config.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h> /* memset() */
#include <unistd.h> /* exit() */
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include "lib/global.h"
#include "lib/strutil.h"
#include "tty.h"
#include "tty-internal.h"
#include "mouse.h" /* use_mouse_p */
#include "win.h"
/*** global variables ****************************************************************************/
int mc_tty_frm[MC_TTY_FRM_MAX];
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
static SIG_ATOMIC_VOLATILE_T got_interrupt = 0;
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
sigintr_handler (int signo)
{
(void) &signo;
got_interrupt = 1;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/**
* Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
*
* @param force_xterm Set forced the XTerm type
*
* @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
* rxvt*, Eterm or dtterm
*/
gboolean
tty_check_term (gboolean force_xterm)
{
const char *termvalue;
const char *xdisplay;
termvalue = getenv ("TERM");
if (termvalue == NULL || *termvalue == '\0')
{
fputs (_("The TERM environment variable is unset!\n"), stderr);
exit (EXIT_FAILURE);
}
xdisplay = getenv ("DISPLAY");
if (xdisplay != NULL && *xdisplay == '\0')
xdisplay = NULL;
return force_xterm || strncmp (termvalue, "xterm", 5) == 0
|| strncmp (termvalue, "konsole", 7) == 0
|| strncmp (termvalue, "rxvt", 4) == 0
|| strcmp (termvalue, "Eterm") == 0
|| strcmp (termvalue, "dtterm") == 0
|| (strncmp (termvalue, "screen", 6) == 0 && xdisplay != NULL);
}
/* --------------------------------------------------------------------------------------------- */
extern void
tty_start_interrupt_key (void)
{
struct sigaction act;
memset (&act, 0, sizeof (act));
act.sa_handler = sigintr_handler;
sigemptyset (&act.sa_mask);
#ifdef SA_RESTART
act.sa_flags = SA_RESTART;
#endif /* SA_RESTART */
sigaction (SIGINT, &act, NULL);
}
/* --------------------------------------------------------------------------------------------- */
extern void
tty_enable_interrupt_key (void)
{
struct sigaction act;
memset (&act, 0, sizeof (act));
act.sa_handler = sigintr_handler;
sigemptyset (&act.sa_mask);
sigaction (SIGINT, &act, NULL);
got_interrupt = 0;
}
/* --------------------------------------------------------------------------------------------- */
extern void
tty_disable_interrupt_key (void)
{
struct sigaction act;
memset (&act, 0, sizeof (act));
act.sa_handler = SIG_IGN;
sigemptyset (&act.sa_mask);
sigaction (SIGINT, &act, NULL);
}
/* --------------------------------------------------------------------------------------------- */
extern gboolean
tty_got_interrupt (void)
{
gboolean rv;
rv = (got_interrupt != 0);
got_interrupt = 0;
return rv;
}
/* --------------------------------------------------------------------------------------------- */
void
tty_print_one_hline (gboolean single)
{
tty_print_alt_char (ACS_HLINE, single);
}
/* --------------------------------------------------------------------------------------------- */
void
tty_print_one_vline (gboolean single)
{
tty_print_alt_char (ACS_VLINE, single);
}
/* --------------------------------------------------------------------------------------------- */
void
tty_draw_box (int y, int x, int ys, int xs, gboolean single)
{
int y2, x2;
if (ys <= 0 || xs <= 0)
return;
ys--;
xs--;
y2 = y + ys;
x2 = x + xs;
tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
tty_draw_vline (y, x2, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
tty_draw_hline (y2, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
tty_gotoyx (y, x);
tty_print_alt_char (ACS_ULCORNER, single);
tty_gotoyx (y2, x);
tty_print_alt_char (ACS_LLCORNER, single);
tty_gotoyx (y, x2);
tty_print_alt_char (ACS_URCORNER, single);
tty_gotoyx (y2, x2);
tty_print_alt_char (ACS_LRCORNER, single);
}
/* --------------------------------------------------------------------------------------------- */
char *
mc_tty_normalize_from_utf8 (const char *str)
{
GIConv conv;
GString *buffer;
const char *_system_codepage = str_detect_termencoding ();
if (str_isutf8 (_system_codepage))
return g_strdup (str);
conv = g_iconv_open (_system_codepage, "UTF-8");
if (conv == INVALID_CONV)
return g_strdup (str);
buffer = g_string_new ("");
if (str_convert (conv, str, buffer) == ESTR_FAILURE)
{
g_string_free (buffer, TRUE);
str_close_conv (conv);
return g_strdup (str);
}
str_close_conv (conv);
return g_string_free (buffer, FALSE);
}
/* --------------------------------------------------------------------------------------------- */
/** Resize given terminal using TIOCSWINSZ, return ioctl() result */
int
tty_resize (int fd)
{
#if defined TIOCSWINSZ
struct winsize tty_size;
tty_size.ws_row = LINES;
tty_size.ws_col = COLS;
tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
return ioctl (fd, TIOCSWINSZ, &tty_size);
#else
return 0;
#endif
}
/* --------------------------------------------------------------------------------------------- */
void
tty_init_xterm_support (gboolean is_xterm)
{
const char *termvalue;
termvalue = getenv ("TERM");
/* Check mouse and ca capabilities */
/* terminfo/termcap structures have been already initialized,
in slang_init() or/and init_curses() */
/* Check terminfo at first, then check termcap */
xmouse_seq = tty_tgetstr ("kmous");
if (xmouse_seq == NULL)
xmouse_seq = tty_tgetstr ("Km");
smcup = tty_tgetstr ("smcup");
if (smcup == NULL)
smcup = tty_tgetstr ("ti");
rmcup = tty_tgetstr ("rmcup");
if (rmcup == NULL)
rmcup = tty_tgetstr ("te");
if (strcmp (termvalue, "cygwin") == 0)
{
is_xterm = TRUE;
use_mouse_p = MOUSE_DISABLED;
}
if (is_xterm)
{
/* Default to the standard xterm sequence */
if (xmouse_seq == NULL)
xmouse_seq = ESC_STR "[M";
/* Enable mouse unless explicitly disabled by --nomouse */
if (use_mouse_p != MOUSE_DISABLED)
{
if (mc_global.tty.old_mouse)
use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
else
{
/* FIXME: this dirty hack to set supported type of tracking the mouse */
const char *color_term = getenv ("COLORTERM");
if (strncmp (termvalue, "rxvt", 4) == 0 ||
(color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
strcmp (termvalue, "Eterm") == 0)
use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
else
use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
}
}
}
/* No termcap for SGR extended mouse (yet), hardcode it for now */
if (xmouse_seq != NULL)
xmouse_extended_seq = ESC_STR "[<";
}
/* --------------------------------------------------------------------------------------------- */