1 /* Slang interface to the Midnight Commander
2 This emulates some features of ncurses on top of slang
4 Copyright (C) 1995, 1996 The Free Software Foundation.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include "mouse.h" /* Gpm_Event is required in key.h */
34 #include "key.h" /* define_sequence */
35 #include "main.h" /* extern: force_colors */
36 #include "win.h" /* do_exit_ca_mode */
41 /* Taken from S-Lang's slutty.c */
42 #ifdef ultrix /* Ultrix gets _POSIX_VDISABLE wrong! */
43 # define NULL_VALUE -1
45 # ifdef _POSIX_VDISABLE
46 # define NULL_VALUE _POSIX_VDISABLE
48 # define NULL_VALUE 255
52 /* Taken from S-Lang's sldisply.c file */
54 # define tgetstr(a,b) SLtt_tgetstr (a)
56 extern char *tgetstr(char *, char **);
63 /* Various saved termios settings that we control here */
64 static struct termios boot_mode
;
65 static struct termios new_mode
;
67 /* Set if we get an interrupt */
68 static int slinterrupt
;
70 /* Controls whether we should wait for input in getch */
71 static int no_slang_delay
;
73 /* {{{ Copied from ../slang/slgetkey.c, removed the DEC_8Bit_HACK, */
74 extern unsigned int SLang_Input_Buffer_Len
;
75 extern unsigned char SLang_Input_Buffer
[];
76 #if SLANG_VERSION >= 10000
77 extern unsigned int _SLsys_getkey (void);
78 extern int _SLsys_input_pending (int);
80 extern unsigned int SLsys_getkey (void);
81 extern int SLsys_input_pending (int);
84 /* Forward declarations */
85 static void load_terminfo_keys (void);
87 static unsigned int SLang_getkey2 (void)
92 if (SLang_Input_Buffer_Len
)
94 ch
= (unsigned int) *SLang_Input_Buffer
;
95 SLang_Input_Buffer_Len
--;
96 imax
= SLang_Input_Buffer_Len
;
98 memcpy ((char *) SLang_Input_Buffer
,
99 (char *) (SLang_Input_Buffer
+ 1), imax
);
102 #if SLANG_VERSION >= 10000
103 else return(_SLsys_getkey ());
105 else return(SLsys_getkey());
109 static int SLang_input_pending2 (int tsecs
)
114 if (SLang_Input_Buffer_Len
) return (int) SLang_Input_Buffer_Len
;
115 #if SLANG_VERSION >= 10000
116 n
= _SLsys_input_pending (tsecs
);
118 n
= SLsys_input_pending (tsecs
);
120 if (n
<= 0) return 0;
122 i
= SLang_getkey2 ();
123 if (i
== SLANG_GETKEY_ERROR
)
124 return 0; /* don't put crippled error codes into the input buffer */
125 c
= (unsigned char)i
;
126 SLang_ungetkey_string (&c
, 1);
133 slang_intr (int signo
)
139 enable_interrupt_key(void)
141 struct sigaction act
;
143 act
.sa_handler
= slang_intr
;
144 sigemptyset (&act
.sa_mask
);
146 sigaction (SIGINT
, &act
, NULL
);
151 disable_interrupt_key(void)
153 struct sigaction act
;
155 act
.sa_handler
= SIG_IGN
;
157 sigemptyset (&act
.sa_mask
);
158 sigaction (SIGINT
, &act
, NULL
);
171 /* Only done the first time */
175 extern int SLtt_Blink_Mode
;
176 extern int SLtt_Has_Alt_Charset
;
177 extern int force_ugly_line_drawing
;
178 struct sigaction act
, oact
;
180 SLtt_get_terminfo ();
183 * If the terminal in not in terminfo but begins with a well-known
184 * string such as "linux" or "xterm" S-Lang will go on, but the
185 * terminal size and several other variables won't be initialized
186 * (as of S-Lang 1.4.4). Detect it and abort. Also detect extremely
187 * small, large and negative screen dimensions.
189 if ((COLS
< 10) || (LINES
< 5) || (COLS
> 1024) || (LINES
> 1024)) {
191 _("Screen size %dx%d is not supported.\n"
192 "Check the TERM environment variable.\n"),
197 tcgetattr (fileno (stdin
), &boot_mode
);
198 /* 255 = ignore abort char; XCTRL('g') for abort char = ^g */
199 SLang_init_tty (XCTRL('c'), 1, 0);
201 /* If SLang uses fileno(stderr) for terminal input MC will hang
202 if we call SLang_getkey between calls to open_error_pipe and
203 close_error_pipe, e.g. when we do a growing view of an gzipped
205 if (SLang_TT_Read_FD
== fileno (stderr
))
206 SLang_TT_Read_FD
= fileno (stdin
);
208 if (force_ugly_line_drawing
)
209 SLtt_Has_Alt_Charset
= 0;
210 if (tcgetattr (SLang_TT_Read_FD
, &new_mode
) == 0) {
212 new_mode
.c_cc
[VDSUSP
] = NULL_VALUE
; /* to ignore ^Y */
215 new_mode
.c_cc
[VLNEXT
] = NULL_VALUE
; /* to ignore ^V */
217 tcsetattr (SLang_TT_Read_FD
, TCSADRAIN
, &new_mode
);
220 load_terminfo_keys ();
221 act
.sa_handler
= slang_intr
;
222 sigemptyset (&act
.sa_mask
);
223 act
.sa_flags
= SA_RESTART
;
224 sigaction (SIGINT
, &act
, &oact
);
229 slang_set_raw_mode (void)
231 tcsetattr (SLang_TT_Read_FD
, TCSANOW
, &new_mode
);
234 /* Done each time we come back from done mode */
236 slang_prog_mode (void)
238 tcsetattr (SLang_TT_Read_FD
, TCSANOW
, &new_mode
);
240 SLsmg_touch_lines (0, LINES
);
243 /* Called each time we want to shutdown slang screen manager */
245 slang_shell_mode (void)
247 tcsetattr (SLang_TT_Read_FD
, TCSANOW
, &boot_mode
);
251 slang_shutdown (void)
258 dealloc_color_pairs ();
260 /* Load the op capability to reset the colors to those that were
261 * active when the program was started up
263 op_cap
= SLtt_tgetstr ("op");
265 fprintf (stdout
, "%s", op_cap
);
270 /* HP Terminals have capabilities (pfkey, pfloc, pfx) to program function keys.
271 elm 2.4pl15 invoked with the -K option utilizes these softkeys and the
272 consequence is that function keys don't work in MC sometimes.
273 Unfortunately I don't now the one and only escape sequence to turn off
274 softkeys (elm uses three different capabilities to turn on softkeys and two
275 capabilities to turn them off).
276 Among other things elm uses the pair we already use in slang_keypad. That's
277 the reason why I call slang_reset_softkeys from slang_keypad. In lack of
278 something better the softkeys are programmed to their defaults from the
279 termcap/terminfo database.
280 The escape sequence to program the softkeys is taken from elm and it is
281 hardcoded because neither slang nor ncurses 4.1 know how to 'printf' this
286 slang_reset_softkeys (void)
290 static const char display
[] = " ";
293 for ( key
= 1; key
< 9; key
++ ) {
294 g_snprintf (tmp
, sizeof (tmp
), "k%d", key
);
295 send
= (char *) SLtt_tgetstr (tmp
);
297 g_snprintf(tmp
, sizeof (tmp
), "\033&f%dk%dd%dL%s%s", key
,
298 sizeof (display
) - 1, strlen(send
), display
, send
);
299 SLtt_write_string (tmp
);
304 /* keypad routines */
306 slang_keypad (int set
)
309 extern int reset_hp_softkeys
;
311 keypad_string
= (char *) SLtt_tgetstr (set
? "ks" : "ke");
313 SLtt_write_string (keypad_string
);
314 if (set
&& reset_hp_softkeys
)
315 slang_reset_softkeys ();
319 set_slang_delay (int v
)
325 hline (int ch
, int len
)
329 last_x
= SLsmg_get_column ();
330 last_y
= SLsmg_get_row ();
335 if (ch
== ACS_HLINE
){
336 SLsmg_draw_hline (len
);
341 move (last_y
, last_x
);
345 vline (int character
, int len
)
348 SLsmg_draw_vline (len
);
350 int last_x
, last_y
, pos
= 0;
352 last_x
= SLsmg_get_column ();
353 last_y
= SLsmg_get_row ();
356 move (last_y
+ pos
++, last_x
);
359 move (last_x
, last_y
);
363 int has_colors (void)
365 char *terminal
= getenv ("TERM");
366 char *cts
= color_terminal_string
, *s
;
370 SLtt_Use_Ansi_Colors
= 1;
371 if (NULL
!= getenv ("COLORTERM"))
372 SLtt_Use_Ansi_Colors
= 1;
374 /* We want to allow overwriding */
375 if (!disable_colors
){
376 /* check color_terminal_string */
381 while (*cts
== ' ' || *cts
== '\t') cts
++;
386 while (*cts
&& *cts
!= ',')
391 if (i
&& i
== strlen(terminal
) && strncmp(s
, terminal
, i
) == 0)
392 SLtt_Use_Ansi_Colors
= 1;
394 if (*cts
== ',') cts
++;
399 /* Setup emulated colors */
400 if (SLtt_Use_Ansi_Colors
){
402 mc_init_pair (A_REVERSE
, "black", "white");
403 mc_init_pair (A_BOLD
, "white", "black");
405 mc_init_pair (A_REVERSE
, "black", "lightgray");
406 mc_init_pair (A_BOLD
, "white", "black");
407 mc_init_pair (A_BOLD_REVERSE
, "white", "lightgray");
410 SLtt_set_mono (A_BOLD
, NULL
, SLTT_BOLD_MASK
);
411 SLtt_set_mono (A_REVERSE
, NULL
, SLTT_REV_MASK
);
412 SLtt_set_mono (A_BOLD
|A_REVERSE
, NULL
, SLTT_BOLD_MASK
| SLTT_REV_MASK
);
414 return SLtt_Use_Ansi_Colors
;
420 if (!SLtt_Use_Ansi_Colors
){
421 SLsmg_set_color (color
);
427 SLsmg_set_color (A_BOLD
);
429 SLsmg_set_color ((color
& (~A_BOLD
)) + 8);
433 if (color
== A_REVERSE
)
434 SLsmg_set_color (A_REVERSE
);
436 SLsmg_set_color (color
);
439 /* This table describes which capabilities we want and which values we
442 static const struct {
475 { KEY_BACKSPACE
, "kb" },
482 do_define_key (int code
, char *strcap
)
486 seq
= (char *) SLtt_tgetstr (strcap
);
488 define_sequence (code
, seq
, MCKEY_NOACTION
);
492 load_terminfo_keys (void)
496 for (i
= 0; key_table
[i
].key_code
; i
++)
497 do_define_key (key_table
[i
].key_code
, key_table
[i
].key_name
);
504 if (SLang_input_pending2 (0) == 0)
507 c
= SLang_getkey2 ();
508 if (c
== SLANG_GETKEY_ERROR
) {
509 fprintf (stderr
, "SLang_getkey returned SLANG_GETKEY_ERROR\n"
510 "Assuming EOF on stdin and exiting\n");
518 /* Non slang builds do not understand got_interrupt */
519 int got_interrupt (void)
523 #endif /* HAVE_SLANG */
528 if (!we_are_background
)