Clarify type cast in tgetsrt() calls.
[midnight-commander.git] / lib / tty / tty-ncurses.c
blob475ba66bb0b580655504da8da702f7469b4652fe
1 /*
2 Interface to the terminal controlling library.
3 Ncurses wrapper.
5 Copyright (C) 2005-2016
6 Free Software Foundation, Inc.
8 Written by:
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/>.
28 /** \file
29 * \brief Source: NCurses-based tty layer of Midnight-commander
32 #include <config.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <signal.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
39 #endif
40 #include <termios.h>
42 #include "lib/global.h"
43 #include "lib/strutil.h" /* str_term_form */
45 #ifndef WANT_TERM_H
46 #define WANT_TERM_H
47 #endif
49 #include "tty-internal.h" /* mc_tty_normalize_from_utf8() */
50 #include "tty.h"
51 #include "color-internal.h"
52 #include "key.h"
53 #include "mouse.h"
54 #include "win.h"
56 /* include at last !!! */
57 #ifdef WANT_TERM_H
58 #ifdef HAVE_NCURSES_TERM_H
59 #include <ncurses/term.h>
60 #else
61 #include <term.h>
62 #endif /* HAVE_NCURSES_TERM_H */
63 #endif /* WANT_TERM_H */
65 /*** global variables ****************************************************************************/
67 /*** file scope macro definitions ****************************************************************/
69 #if !defined(CTRL)
70 #define CTRL(x) ((x) & 0x1f)
71 #endif
73 #define yx_in_screen(y, x) \
74 (y >= 0 && y < LINES && x >= 0 && x < COLS)
76 /*** global variables ****************************************************************************/
78 /*** file scope type declarations ****************************************************************/
80 /*** file scope variables ************************************************************************/
82 /* ncurses supports cursor positions only within window */
83 /* We use our own cursor coordibates to support partially visible widgets */
84 static int mc_curs_row, mc_curs_col;
86 /*** file scope functions ************************************************************************/
87 /* --------------------------------------------------------------------------------------------- */
89 /* --------------------------------------------------------------------------------------------- */
91 static void
92 tty_setup_sigwinch (void (*handler) (int))
94 #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
95 struct sigaction act, oact;
97 memset (&act, 0, sizeof (act));
98 act.sa_handler = handler;
99 sigemptyset (&act.sa_mask);
100 #ifdef SA_RESTART
101 act.sa_flags = SA_RESTART;
102 #endif /* SA_RESTART */
103 sigaction (SIGWINCH, &act, &oact);
104 #endif /* SIGWINCH */
107 /* --------------------------------------------------------------------------------------------- */
109 static void
110 sigwinch_handler (int dummy)
112 (void) dummy;
114 mc_global.tty.winch_flag = 1;
117 /* --------------------------------------------------------------------------------------------- */
118 /*** public functions ****************************************************************************/
119 /* --------------------------------------------------------------------------------------------- */
122 mc_tty_normalize_lines_char (const char *ch)
124 char *str2;
125 int res;
127 struct mc_tty_lines_struct
129 const char *line;
130 int line_code;
131 } const lines_codes[] = {
132 {"\342\224\230", ACS_LRCORNER}, /* ┌ */
133 {"\342\224\224", ACS_LLCORNER}, /* └ */
134 {"\342\224\220", ACS_URCORNER}, /* ┐ */
135 {"\342\224\214", ACS_ULCORNER}, /* ┘ */
136 {"\342\224\234", ACS_LTEE}, /* ├ */
137 {"\342\224\244", ACS_RTEE}, /* ┤ */
138 {"\342\224\254", ACS_TTEE}, /* ┬ */
139 {"\342\224\264", ACS_BTEE}, /* ┴ */
140 {"\342\224\200", ACS_HLINE}, /* ─ */
141 {"\342\224\202", ACS_VLINE}, /* │ */
142 {"\342\224\274", ACS_PLUS}, /* ┼ */
144 {"\342\225\235", ACS_LRCORNER | A_BOLD}, /* ╔ */
145 {"\342\225\232", ACS_LLCORNER | A_BOLD}, /* ╚ */
146 {"\342\225\227", ACS_URCORNER | A_BOLD}, /* ╗ */
147 {"\342\225\224", ACS_ULCORNER | A_BOLD}, /* ╝ */
148 {"\342\225\237", ACS_LTEE | A_BOLD}, /* ╟ */
149 {"\342\225\242", ACS_RTEE | A_BOLD}, /* ╢ */
150 {"\342\225\244", ACS_TTEE | A_BOLD}, /* ╤ */
151 {"\342\225\247", ACS_BTEE | A_BOLD}, /* ╧ */
152 {"\342\225\220", ACS_HLINE | A_BOLD}, /* ═ */
153 {"\342\225\221", ACS_VLINE | A_BOLD}, /* ║ */
155 {NULL, 0}
158 if (ch == NULL)
159 return (int) ' ';
161 for (res = 0; lines_codes[res].line; res++)
163 if (strcmp (ch, lines_codes[res].line) == 0)
164 return lines_codes[res].line_code;
167 str2 = mc_tty_normalize_from_utf8 (ch);
168 res = g_utf8_get_char_validated (str2, -1);
170 if (res < 0)
171 res = (unsigned char) str2[0];
172 g_free (str2);
174 return res;
177 /* --------------------------------------------------------------------------------------------- */
179 void
180 tty_init (gboolean mouse_enable, gboolean is_xterm)
182 initscr ();
184 #ifdef HAVE_ESCDELAY
186 * If ncurses exports the ESCDELAY variable, it should be set to
187 * a low value, or you'll experience a delay in processing escape
188 * sequences that are recognized by mc (e.g. Esc-Esc). On the other
189 * hand, making ESCDELAY too small can result in some sequences
190 * (e.g. cursor arrows) being reported as separate keys under heavy
191 * processor load, and this can be a problem if mc hasn't learned
192 * them in the "Learn Keys" dialog. The value is in milliseconds.
194 ESCDELAY = 200;
195 #endif /* HAVE_ESCDELAY */
197 /* use Ctrl-g to generate SIGINT */
198 cur_term->Nttyb.c_cc[VINTR] = CTRL ('g'); /* ^g */
199 /* disable SIGQUIT to allow use Ctrl-\ key */
200 cur_term->Nttyb.c_cc[VQUIT] = NULL_VALUE;
201 tcsetattr (cur_term->Filedes, TCSANOW, &cur_term->Nttyb);
203 tty_start_interrupt_key ();
205 if (!mouse_enable)
206 use_mouse_p = MOUSE_DISABLED;
207 tty_init_xterm_support (is_xterm); /* do it before tty_enter_ca_mode() call */
208 tty_enter_ca_mode ();
209 tty_raw_mode ();
210 noecho ();
211 keypad (stdscr, TRUE);
212 nodelay (stdscr, FALSE);
214 tty_setup_sigwinch (sigwinch_handler);
217 /* --------------------------------------------------------------------------------------------- */
219 void
220 tty_shutdown (void)
222 disable_mouse ();
223 disable_bracketed_paste ();
224 tty_reset_shell_mode ();
225 tty_noraw_mode ();
226 tty_keypad (FALSE);
227 tty_reset_screen ();
228 tty_exit_ca_mode ();
231 /* --------------------------------------------------------------------------------------------- */
233 void
234 tty_enter_ca_mode (void)
236 if (mc_global.tty.xterm_flag && smcup != NULL)
238 fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h");
239 fflush (stdout);
243 /* --------------------------------------------------------------------------------------------- */
245 void
246 tty_exit_ca_mode (void)
248 if (mc_global.tty.xterm_flag && rmcup != NULL)
250 fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
251 fflush (stdout);
255 /* --------------------------------------------------------------------------------------------- */
257 void
258 tty_change_screen_size (void)
260 #if defined(TIOCGWINSZ) && NCURSES_VERSION_MAJOR >= 4
261 struct winsize winsz;
263 winsz.ws_col = winsz.ws_row = 0;
265 #ifndef NCURSES_VERSION
266 tty_noraw_mode ();
267 tty_reset_screen ();
268 #endif
270 /* Ioctl on the STDIN_FILENO */
271 ioctl (fileno (stdout), TIOCGWINSZ, &winsz);
272 if (winsz.ws_col != 0 && winsz.ws_row != 0)
274 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
275 resizeterm (winsz.ws_row, winsz.ws_col);
276 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
277 #else
278 COLS = winsz.ws_col;
279 LINES = winsz.ws_row;
280 #endif
282 #endif /* defined(TIOCGWINSZ) || NCURSES_VERSION_MAJOR >= 4 */
284 #ifdef ENABLE_SUBSHELL
285 if (mc_global.tty.use_subshell)
286 tty_resize (mc_global.tty.subshell_pty);
287 #endif
290 /* --------------------------------------------------------------------------------------------- */
292 void
293 tty_reset_prog_mode (void)
295 reset_prog_mode ();
298 /* --------------------------------------------------------------------------------------------- */
300 void
301 tty_reset_shell_mode (void)
303 reset_shell_mode ();
306 /* --------------------------------------------------------------------------------------------- */
308 void
309 tty_raw_mode (void)
311 raw (); /* FIXME: uneeded? */
312 cbreak ();
315 /* --------------------------------------------------------------------------------------------- */
317 void
318 tty_noraw_mode (void)
320 nocbreak (); /* FIXME: unneeded? */
321 noraw ();
324 /* --------------------------------------------------------------------------------------------- */
326 void
327 tty_noecho (void)
329 noecho ();
332 /* --------------------------------------------------------------------------------------------- */
335 tty_flush_input (void)
337 return flushinp ();
340 /* --------------------------------------------------------------------------------------------- */
342 void
343 tty_keypad (gboolean set)
345 keypad (stdscr, (bool) set);
348 /* --------------------------------------------------------------------------------------------- */
350 void
351 tty_nodelay (gboolean set)
353 nodelay (stdscr, (bool) set);
356 /* --------------------------------------------------------------------------------------------- */
359 tty_baudrate (void)
361 return baudrate ();
364 /* --------------------------------------------------------------------------------------------- */
367 tty_lowlevel_getch (void)
369 return getch ();
372 /* --------------------------------------------------------------------------------------------- */
375 tty_reset_screen (void)
377 return endwin ();
380 /* --------------------------------------------------------------------------------------------- */
382 void
383 tty_touch_screen (void)
385 touchwin (stdscr);
388 /* --------------------------------------------------------------------------------------------- */
390 void
391 tty_gotoyx (int y, int x)
393 mc_curs_row = y;
394 mc_curs_col = x;
396 if (y < 0)
397 y = 0;
398 if (y >= LINES)
399 y = LINES - 1;
401 if (x < 0)
402 x = 0;
403 if (x >= COLS)
404 x = COLS - 1;
406 move (y, x);
409 /* --------------------------------------------------------------------------------------------- */
411 void
412 tty_getyx (int *py, int *px)
414 *py = mc_curs_row;
415 *px = mc_curs_col;
418 /* --------------------------------------------------------------------------------------------- */
420 void
421 tty_draw_hline (int y, int x, int ch, int len)
423 int x1;
425 if (y < 0 || y >= LINES || x >= COLS)
426 return;
428 x1 = x;
430 if (x < 0)
432 len += x;
433 if (len <= 0)
434 return;
435 x = 0;
438 if ((chtype) ch == ACS_HLINE)
439 ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
441 move (y, x);
442 hline (ch, len);
443 move (y, x1);
445 mc_curs_row = y;
446 mc_curs_col = x1;
449 /* --------------------------------------------------------------------------------------------- */
451 void
452 tty_draw_vline (int y, int x, int ch, int len)
454 int y1;
456 if (x < 0 || x >= COLS || y >= LINES)
457 return;
459 y1 = y;
461 if (y < 0)
463 len += y;
464 if (len <= 0)
465 return;
466 y = 0;
469 if ((chtype) ch == ACS_VLINE)
470 ch = mc_tty_frm[MC_TTY_FRM_VERT];
472 move (y, x);
473 vline (ch, len);
474 move (y1, x);
476 mc_curs_row = y1;
477 mc_curs_col = x;
480 /* --------------------------------------------------------------------------------------------- */
482 void
483 tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
485 int i;
487 if (y < 0)
489 rows += y;
491 if (rows <= 0)
492 return;
494 y = 0;
497 if (x < 0)
499 cols += x;
501 if (cols <= 0)
502 return;
504 x = 0;
507 if (y + rows > LINES)
508 rows = LINES - y;
509 if (x + cols > COLS)
510 cols = COLS - x;
512 for (i = 0; i < rows; i++)
514 move (y + i, x);
515 hline (ch, cols);
518 move (y, x);
520 mc_curs_row = y;
521 mc_curs_col = x;
524 /* --------------------------------------------------------------------------------------------- */
526 void
527 tty_set_alt_charset (gboolean alt_charset)
529 (void) alt_charset;
532 /* --------------------------------------------------------------------------------------------- */
534 void
535 tty_display_8bit (gboolean what)
537 meta (stdscr, (int) what);
540 /* --------------------------------------------------------------------------------------------- */
542 void
543 tty_print_char (int c)
545 if (yx_in_screen (mc_curs_row, mc_curs_col))
546 addch (c);
547 mc_curs_col++;
550 /* --------------------------------------------------------------------------------------------- */
552 void
553 tty_print_anychar (int c)
555 if (mc_global.utf8_display || c > 255)
557 int res;
558 unsigned char str[UTF8_CHAR_LEN + 1];
560 res = g_unichar_to_utf8 (c, (char *) str);
561 if (res == 0)
563 if (yx_in_screen (mc_curs_row, mc_curs_col))
564 addch ('.');
565 mc_curs_col++;
567 else
569 const char *s;
571 str[res] = '\0';
572 s = str_term_form ((char *) str);
574 if (yx_in_screen (mc_curs_row, mc_curs_col))
575 addstr (s);
577 if (g_unichar_iswide (c))
578 mc_curs_col += 2;
579 else if (!g_unichar_iszerowidth (c))
580 mc_curs_col++;
583 else
585 if (yx_in_screen (mc_curs_row, mc_curs_col))
586 addch (c);
587 mc_curs_col++;
591 /* --------------------------------------------------------------------------------------------- */
593 void
594 tty_print_alt_char (int c, gboolean single)
596 if (yx_in_screen (mc_curs_row, mc_curs_col))
598 if ((chtype) c == ACS_VLINE)
599 c = mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT];
600 else if ((chtype) c == ACS_HLINE)
601 c = mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ];
602 else if ((chtype) c == ACS_LTEE)
603 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE];
604 else if ((chtype) c == ACS_RTEE)
605 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE];
606 else if ((chtype) c == ACS_ULCORNER)
607 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP];
608 else if ((chtype) c == ACS_LLCORNER)
609 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM];
610 else if ((chtype) c == ACS_URCORNER)
611 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP];
612 else if ((chtype) c == ACS_LRCORNER)
613 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM];
614 else if ((chtype) c == ACS_PLUS)
615 c = mc_tty_frm[MC_TTY_FRM_CROSS];
617 addch (c);
620 mc_curs_col++;
623 /* --------------------------------------------------------------------------------------------- */
625 void
626 tty_print_string (const char *s)
628 int len;
629 int start = 0;
631 s = str_term_form (s);
632 len = str_term_width1 (s);
634 /* line is upper or below the screen or entire line is before or after scrren */
635 if (mc_curs_row < 0 || mc_curs_row >= LINES || mc_curs_col + len <= 0 || mc_curs_col >= COLS)
637 mc_curs_col += len;
638 return;
641 /* skip invisible left part */
642 if (mc_curs_col < 0)
644 start = -mc_curs_col;
645 len += mc_curs_col;
646 mc_curs_col = 0;
649 mc_curs_col += len;
650 if (mc_curs_col >= COLS)
651 len = COLS - (mc_curs_col - len);
653 addstr (str_term_substring (s, start, len));
656 /* --------------------------------------------------------------------------------------------- */
658 void
659 tty_printf (const char *fmt, ...)
661 va_list args;
662 char buf[BUF_1K]; /* FIXME: is it enough? */
664 va_start (args, fmt);
665 g_vsnprintf (buf, sizeof (buf), fmt, args);
666 va_end (args);
667 tty_print_string (buf);
670 /* --------------------------------------------------------------------------------------------- */
672 char *
673 tty_tgetstr (const char *cap)
675 char *unused = NULL;
677 return tgetstr ((NCURSES_CONST char *) cap, &unused);
680 /* --------------------------------------------------------------------------------------------- */
682 void
683 tty_refresh (void)
685 refresh ();
686 doupdate ();
689 /* --------------------------------------------------------------------------------------------- */
691 void
692 tty_beep (void)
694 beep ();
697 /* --------------------------------------------------------------------------------------------- */