2 Interface to the terminal controlling library.
5 Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
8 Andrew Borodin <aborodin@vmail.ru>, 2009.
9 Ilia Maslakov <il.smind@gmail.com>, 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 2 of the
16 License, or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be
19 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 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, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 * \brief Source: NCurses-based tty layer of Midnight-commander
39 #include "lib/global.h"
40 #include "lib/strutil.h" /* str_term_form */
48 #include "tty-internal.h" /* slow_tty */
50 #include "color-internal.h"
53 /* include at last !!! */
55 #ifdef HAVE_NCURSES_TERM_H
56 # include <ncurses/term.h>
59 #endif /* HAVE_NCURSES_TERM_H */
60 #endif /* WANT_TERM_H */
62 /*** global variables **************************************************/
64 /*** file scope macro definitions **************************************/
66 #if defined(_AIX) && !defined(CTRL)
67 # define CTRL(x) ((x) & 0x1f)
70 /*** global variables **************************************************/
72 /*** file scope type declarations **************************************/
74 /*** file scope variables **********************************************/
76 /*** file scope functions **********************************************/
78 /* --------------------------------------------------------------------------------------------- */
79 /*** public functions **************************************************/
80 /* --------------------------------------------------------------------------------------------- */
83 mc_tty_normalize_lines_char (const char *ch
)
88 struct mc_tty_lines_struct
92 } const lines_codes
[] = {
93 {"\342\224\230", ACS_LRCORNER
}, /* ┌ */
94 {"\342\224\224", ACS_LLCORNER
}, /* └ */
95 {"\342\224\220", ACS_URCORNER
}, /* ┐ */
96 {"\342\224\214", ACS_ULCORNER
}, /* ┘ */
97 {"\342\224\234", ACS_LTEE
}, /* ├ */
98 {"\342\224\244", ACS_RTEE
}, /* ┤ */
99 {"\342\224\254", ACS_TTEE
}, /* ┬ */
100 {"\342\224\264", ACS_BTEE
}, /* ┴ */
101 {"\342\224\200", ACS_HLINE
}, /* ─ */
102 {"\342\224\202", ACS_VLINE
}, /* │ */
103 {"\342\224\274", ACS_PLUS
}, /* ┼ */
105 {"\342\225\235", ACS_LRCORNER
| A_BOLD
}, /* ╔ */
106 {"\342\225\232", ACS_LLCORNER
| A_BOLD
}, /* ╚ */
107 {"\342\225\227", ACS_URCORNER
| A_BOLD
}, /* ╗ */
108 {"\342\225\224", ACS_ULCORNER
| A_BOLD
}, /* ╝ */
109 {"\342\225\237", ACS_LTEE
| A_BOLD
}, /* ╟ */
110 {"\342\225\242", ACS_RTEE
| A_BOLD
}, /* ╢ */
111 {"\342\225\244", ACS_TTEE
| A_BOLD
}, /* ╤ */
112 {"\342\225\247", ACS_BTEE
| A_BOLD
}, /* ╧ */
113 {"\342\225\220", ACS_HLINE
| A_BOLD
}, /* ═ */
114 {"\342\225\221", ACS_VLINE
| A_BOLD
}, /* ║ */
122 for (res
= 0; lines_codes
[res
].line
; res
++)
124 if (strcmp (ch
, lines_codes
[res
].line
) == 0)
125 return lines_codes
[res
].line_code
;
128 str2
= mc_tty_normalize_from_utf8 (ch
);
129 res
= g_utf8_get_char_validated (str2
, -1);
132 res
= (unsigned char) str2
[0];
138 /* --------------------------------------------------------------------------------------------- */
142 tty_init (gboolean slow
, gboolean ugly_lines
)
151 * If ncurses exports the ESCDELAY variable, it should be set to
152 * a low value, or you'll experience a delay in processing escape
153 * sequences that are recognized by mc (e.g. Esc-Esc). On the other
154 * hand, making ESCDELAY too small can result in some sequences
155 * (e.g. cursor arrows) being reported as separate keys under heavy
156 * processor load, and this can be a problem if mc hasn't learned
157 * them in the "Learn Keys" dialog. The value is in milliseconds.
160 #endif /* HAVE_ESCDELAY */
162 /* use Ctrl-g to generate SIGINT */
163 cur_term
->Nttyb
.c_cc
[VINTR
] = CTRL ('g'); /* ^g */
164 /* disable SIGQUIT to allow use Ctrl-\ key */
165 cur_term
->Nttyb
.c_cc
[VQUIT
] = NULL_VALUE
;
166 tcsetattr (cur_term
->Filedes
, TCSANOW
, &cur_term
->Nttyb
);
168 tty_start_interrupt_key ();
173 keypad (stdscr
, TRUE
);
174 nodelay (stdscr
, FALSE
);
184 tty_reset_prog_mode (void)
190 tty_reset_shell_mode (void)
198 raw (); /* FIXME: uneeded? */
203 tty_noraw_mode (void)
205 nocbreak (); /* FIXME: unneeded? */
216 tty_flush_input (void)
222 tty_keypad (gboolean set
)
224 keypad (stdscr
, (bool) set
);
228 tty_nodelay (gboolean set
)
230 nodelay (stdscr
, (bool) set
);
240 tty_lowlevel_getch (void)
246 tty_reset_screen (void)
252 tty_touch_screen (void)
258 tty_gotoyx (int y
, int x
)
264 tty_getyx (int *py
, int *px
)
266 getyx (stdscr
, *py
, *px
);
269 /* if x < 0 or y < 0, draw line starting from current position */
271 tty_draw_hline (int y
, int x
, int ch
, int len
)
273 if ((chtype
) ch
== ACS_HLINE
)
274 ch
= mc_tty_frm
[MC_TTY_FRM_HORIZ
];
276 if ((y
>= 0) && (x
>= 0))
281 /* if x < 0 or y < 0, draw line starting from current position */
283 tty_draw_vline (int y
, int x
, int ch
, int len
)
285 if ((chtype
) ch
== ACS_VLINE
)
286 ch
= mc_tty_frm
[MC_TTY_FRM_VERT
];
288 if ((y
>= 0) && (x
>= 0))
294 tty_fill_region (int y
, int x
, int rows
, int cols
, unsigned char ch
)
318 if (y
+ rows
> LINES
)
323 for (i
= 0; i
< rows
; i
++)
333 tty_set_alt_charset (gboolean alt_charset
)
339 tty_display_8bit (gboolean what
)
341 meta (stdscr
, (int) what
);
345 tty_print_char (int c
)
351 tty_print_anychar (int c
)
353 unsigned char str
[6 + 1];
355 if (utf8_display
|| c
> 255)
357 int res
= g_unichar_to_utf8 (c
, (char *) str
);
365 addstr (str_term_form ((char *) str
));
373 tty_print_alt_char (int c
, gboolean single
)
375 if ((chtype
) c
== ACS_VLINE
)
376 c
= mc_tty_frm
[single
? MC_TTY_FRM_VERT
: MC_TTY_FRM_DVERT
];
377 else if ((chtype
) c
== ACS_HLINE
)
378 c
= mc_tty_frm
[single
? MC_TTY_FRM_HORIZ
: MC_TTY_FRM_DHORIZ
];
379 else if ((chtype
) c
== ACS_LTEE
)
380 c
= mc_tty_frm
[single
? MC_TTY_FRM_LEFTMIDDLE
: MC_TTY_FRM_DLEFTMIDDLE
];
381 else if ((chtype
) c
== ACS_RTEE
)
382 c
= mc_tty_frm
[single
? MC_TTY_FRM_RIGHTMIDDLE
: MC_TTY_FRM_DRIGHTMIDDLE
];
383 else if ((chtype
) c
== ACS_ULCORNER
)
384 c
= mc_tty_frm
[single
? MC_TTY_FRM_LEFTTOP
: MC_TTY_FRM_DLEFTTOP
];
385 else if ((chtype
) c
== ACS_LLCORNER
)
386 c
= mc_tty_frm
[single
? MC_TTY_FRM_LEFTBOTTOM
: MC_TTY_FRM_DLEFTBOTTOM
];
387 else if ((chtype
) c
== ACS_URCORNER
)
388 c
= mc_tty_frm
[single
? MC_TTY_FRM_RIGHTTOP
: MC_TTY_FRM_DRIGHTTOP
];
389 else if ((chtype
) c
== ACS_LRCORNER
)
390 c
= mc_tty_frm
[single
? MC_TTY_FRM_RIGHTBOTTOM
: MC_TTY_FRM_DRIGHTBOTTOM
];
391 else if ((chtype
) c
== ACS_PLUS
)
392 c
= mc_tty_frm
[MC_TTY_FRM_CROSS
];
398 tty_print_string (const char *s
)
400 addstr (str_term_form (s
));
404 tty_printf (const char *fmt
, ...)
408 va_start (args
, fmt
);
409 vw_printw (stdscr
, fmt
, args
);
414 tty_tgetstr (const char *cap
)
417 return tgetstr ((char *) cap
, &unused
);
428 tty_setup_sigwinch (void (*handler
) (int))
430 #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
431 struct sigaction act
, oact
;
432 act
.sa_handler
= handler
;
433 sigemptyset (&act
.sa_mask
);
436 act
.sa_flags
|= SA_RESTART
;
437 #endif /* SA_RESTART */
438 sigaction (SIGWINCH
, &act
, &oact
);
439 #endif /* SIGWINCH */