2 Interface to the terminal controlling library.
5 Copyright (C) 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
9 Andrew Borodin <aborodin@vmail.ru>, 2009.
10 Ilia Maslakov <il.smind@gmail.com>, 2009.
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: NCurses-based tty layer of Midnight-commander
38 #include "lib/global.h"
39 #include "lib/strutil.h" /* str_term_form */
45 #include "tty-internal.h" /* mc_tty_normalize_from_utf8() */
47 #include "color-internal.h"
51 /* include at last !!! */
53 #ifdef HAVE_NCURSES_TERM_H
54 #include <ncurses/term.h>
57 #endif /* HAVE_NCURSES_TERM_H */
58 #endif /* WANT_TERM_H */
60 /*** global variables ****************************************************************************/
62 /*** file scope macro definitions ****************************************************************/
64 #if defined(_AIX) && !defined(CTRL)
65 #define CTRL(x) ((x) & 0x1f)
68 /*** global variables ****************************************************************************/
70 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
74 /*** file scope functions ************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
77 /* --------------------------------------------------------------------------------------------- */
80 tty_setup_sigwinch (void (*handler
) (int))
82 #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
83 struct sigaction act
, oact
;
84 act
.sa_handler
= handler
;
85 sigemptyset (&act
.sa_mask
);
88 act
.sa_flags
|= SA_RESTART
;
89 #endif /* SA_RESTART */
90 sigaction (SIGWINCH
, &act
, &oact
);
94 /* --------------------------------------------------------------------------------------------- */
97 sigwinch_handler (int dummy
)
101 mc_global
.tty
.winch_flag
= TRUE
;
104 /* --------------------------------------------------------------------------------------------- */
105 /*** public functions ****************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
109 mc_tty_normalize_lines_char (const char *ch
)
114 struct mc_tty_lines_struct
118 } const lines_codes
[] = {
119 {"\342\224\230", ACS_LRCORNER
}, /* ┌ */
120 {"\342\224\224", ACS_LLCORNER
}, /* └ */
121 {"\342\224\220", ACS_URCORNER
}, /* ┐ */
122 {"\342\224\214", ACS_ULCORNER
}, /* ┘ */
123 {"\342\224\234", ACS_LTEE
}, /* ├ */
124 {"\342\224\244", ACS_RTEE
}, /* ┤ */
125 {"\342\224\254", ACS_TTEE
}, /* ┬ */
126 {"\342\224\264", ACS_BTEE
}, /* ┴ */
127 {"\342\224\200", ACS_HLINE
}, /* ─ */
128 {"\342\224\202", ACS_VLINE
}, /* │ */
129 {"\342\224\274", ACS_PLUS
}, /* ┼ */
131 {"\342\225\235", ACS_LRCORNER
| A_BOLD
}, /* ╔ */
132 {"\342\225\232", ACS_LLCORNER
| A_BOLD
}, /* ╚ */
133 {"\342\225\227", ACS_URCORNER
| A_BOLD
}, /* ╗ */
134 {"\342\225\224", ACS_ULCORNER
| A_BOLD
}, /* ╝ */
135 {"\342\225\237", ACS_LTEE
| A_BOLD
}, /* ╟ */
136 {"\342\225\242", ACS_RTEE
| A_BOLD
}, /* ╢ */
137 {"\342\225\244", ACS_TTEE
| A_BOLD
}, /* ╤ */
138 {"\342\225\247", ACS_BTEE
| A_BOLD
}, /* ╧ */
139 {"\342\225\220", ACS_HLINE
| A_BOLD
}, /* ═ */
140 {"\342\225\221", ACS_VLINE
| A_BOLD
}, /* ║ */
148 for (res
= 0; lines_codes
[res
].line
; res
++)
150 if (strcmp (ch
, lines_codes
[res
].line
) == 0)
151 return lines_codes
[res
].line_code
;
154 str2
= mc_tty_normalize_from_utf8 (ch
);
155 res
= g_utf8_get_char_validated (str2
, -1);
158 res
= (unsigned char) str2
[0];
164 /* --------------------------------------------------------------------------------------------- */
167 tty_init (gboolean mouse_enable
, gboolean is_xterm
)
173 * If ncurses exports the ESCDELAY variable, it should be set to
174 * a low value, or you'll experience a delay in processing escape
175 * sequences that are recognized by mc (e.g. Esc-Esc). On the other
176 * hand, making ESCDELAY too small can result in some sequences
177 * (e.g. cursor arrows) being reported as separate keys under heavy
178 * processor load, and this can be a problem if mc hasn't learned
179 * them in the "Learn Keys" dialog. The value is in milliseconds.
182 #endif /* HAVE_ESCDELAY */
184 /* use Ctrl-g to generate SIGINT */
185 cur_term
->Nttyb
.c_cc
[VINTR
] = CTRL ('g'); /* ^g */
186 /* disable SIGQUIT to allow use Ctrl-\ key */
187 cur_term
->Nttyb
.c_cc
[VQUIT
] = NULL_VALUE
;
188 tcsetattr (cur_term
->Filedes
, TCSANOW
, &cur_term
->Nttyb
);
190 tty_start_interrupt_key ();
193 use_mouse_p
= MOUSE_DISABLED
;
194 tty_init_xterm_support (is_xterm
); /* do it before do_enter_ca_mode() call */
199 keypad (stdscr
, TRUE
);
200 nodelay (stdscr
, FALSE
);
202 tty_setup_sigwinch (sigwinch_handler
);
205 /* --------------------------------------------------------------------------------------------- */
211 tty_reset_shell_mode ();
218 /* --------------------------------------------------------------------------------------------- */
221 tty_reset_prog_mode (void)
226 /* --------------------------------------------------------------------------------------------- */
229 tty_reset_shell_mode (void)
234 /* --------------------------------------------------------------------------------------------- */
239 raw (); /* FIXME: uneeded? */
243 /* --------------------------------------------------------------------------------------------- */
246 tty_noraw_mode (void)
248 nocbreak (); /* FIXME: unneeded? */
252 /* --------------------------------------------------------------------------------------------- */
260 /* --------------------------------------------------------------------------------------------- */
263 tty_flush_input (void)
268 /* --------------------------------------------------------------------------------------------- */
271 tty_keypad (gboolean set
)
273 keypad (stdscr
, (bool) set
);
276 /* --------------------------------------------------------------------------------------------- */
279 tty_nodelay (gboolean set
)
281 nodelay (stdscr
, (bool) set
);
284 /* --------------------------------------------------------------------------------------------- */
292 /* --------------------------------------------------------------------------------------------- */
295 tty_lowlevel_getch (void)
300 /* --------------------------------------------------------------------------------------------- */
303 tty_reset_screen (void)
308 /* --------------------------------------------------------------------------------------------- */
311 tty_touch_screen (void)
316 /* --------------------------------------------------------------------------------------------- */
319 tty_gotoyx (int y
, int x
)
324 /* --------------------------------------------------------------------------------------------- */
327 tty_getyx (int *py
, int *px
)
329 getyx (stdscr
, *py
, *px
);
332 /* --------------------------------------------------------------------------------------------- */
333 /* if x < 0 or y < 0, draw line starting from current position */
336 tty_draw_hline (int y
, int x
, int ch
, int len
)
338 if ((chtype
) ch
== ACS_HLINE
)
339 ch
= mc_tty_frm
[MC_TTY_FRM_HORIZ
];
341 if ((y
>= 0) && (x
>= 0))
346 /* --------------------------------------------------------------------------------------------- */
347 /* if x < 0 or y < 0, draw line starting from current position */
350 tty_draw_vline (int y
, int x
, int ch
, int len
)
352 if ((chtype
) ch
== ACS_VLINE
)
353 ch
= mc_tty_frm
[MC_TTY_FRM_VERT
];
355 if ((y
>= 0) && (x
>= 0))
360 /* --------------------------------------------------------------------------------------------- */
363 tty_fill_region (int y
, int x
, int rows
, int cols
, unsigned char ch
)
387 if (y
+ rows
> LINES
)
392 for (i
= 0; i
< rows
; i
++)
401 /* --------------------------------------------------------------------------------------------- */
404 tty_set_alt_charset (gboolean alt_charset
)
409 /* --------------------------------------------------------------------------------------------- */
412 tty_display_8bit (gboolean what
)
414 meta (stdscr
, (int) what
);
417 /* --------------------------------------------------------------------------------------------- */
420 tty_print_char (int c
)
425 /* --------------------------------------------------------------------------------------------- */
428 tty_print_anychar (int c
)
430 unsigned char str
[6 + 1];
432 if (mc_global
.utf8_display
|| c
> 255)
434 int res
= g_unichar_to_utf8 (c
, (char *) str
);
442 addstr (str_term_form ((char *) str
));
449 /* --------------------------------------------------------------------------------------------- */
452 tty_print_alt_char (int c
, gboolean single
)
454 if ((chtype
) c
== ACS_VLINE
)
455 c
= mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
];
456 else if ((chtype
) c
== ACS_HLINE
)
457 c
= mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
];
458 else if ((chtype
) c
== ACS_LTEE
)
459 c
= mc_tty_frm
[single
? MC_TTY_FRM_LEFTMIDDLE
: MC_TTY_FRM_DLEFTMIDDLE
];
460 else if ((chtype
) c
== ACS_RTEE
)
461 c
= mc_tty_frm
[single
? MC_TTY_FRM_RIGHTMIDDLE
: MC_TTY_FRM_DRIGHTMIDDLE
];
462 else if ((chtype
) c
== ACS_ULCORNER
)
463 c
= mc_tty_frm
[single
? MC_TTY_FRM_LEFTTOP
: MC_TTY_FRM_DLEFTTOP
];
464 else if ((chtype
) c
== ACS_LLCORNER
)
465 c
= mc_tty_frm
[single
? MC_TTY_FRM_LEFTBOTTOM
: MC_TTY_FRM_DLEFTBOTTOM
];
466 else if ((chtype
) c
== ACS_URCORNER
)
467 c
= mc_tty_frm
[single
? MC_TTY_FRM_RIGHTTOP
: MC_TTY_FRM_DRIGHTTOP
];
468 else if ((chtype
) c
== ACS_LRCORNER
)
469 c
= mc_tty_frm
[single
? MC_TTY_FRM_RIGHTBOTTOM
: MC_TTY_FRM_DRIGHTBOTTOM
];
470 else if ((chtype
) c
== ACS_PLUS
)
471 c
= mc_tty_frm
[MC_TTY_FRM_CROSS
];
476 /* --------------------------------------------------------------------------------------------- */
479 tty_print_string (const char *s
)
481 addstr (str_term_form (s
));
484 /* --------------------------------------------------------------------------------------------- */
487 tty_printf (const char *fmt
, ...)
491 va_start (args
, fmt
);
492 vw_printw (stdscr
, fmt
, args
);
496 /* --------------------------------------------------------------------------------------------- */
499 tty_tgetstr (const char *cap
)
502 return tgetstr ((char *) cap
, &unused
);
505 /* --------------------------------------------------------------------------------------------- */
514 /* --------------------------------------------------------------------------------------------- */
522 /* --------------------------------------------------------------------------------------------- */