Fix use of uninitialized memory in sigaction structure.
[midnight-commander.git] / lib / tty / tty-ncurses.c
blobea4cbbd8f80a9d3c722d5e678a80a8ce6dd999cf
1 /*
2 Interface to the terminal controlling library.
3 Ncurses wrapper.
5 Copyright (C) 2005, 2006, 2007, 2009, 2011
6 The 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 "mouse.h"
53 #include "win.h"
55 /* include at last !!! */
56 #ifdef WANT_TERM_H
57 #ifdef HAVE_NCURSES_TERM_H
58 #include <ncurses/term.h>
59 #else
60 #include <term.h>
61 #endif /* HAVE_NCURSES_TERM_H */
62 #endif /* WANT_TERM_H */
64 /*** global variables ****************************************************************************/
66 /*** file scope macro definitions ****************************************************************/
68 #if defined(_AIX) && !defined(CTRL)
69 #define CTRL(x) ((x) & 0x1f)
70 #endif
72 #define yx_in_screen(y, x) \
73 (y >= 0 && y < LINES && x >= 0 && x < COLS)
75 /*** global variables ****************************************************************************/
77 /*** file scope type declarations ****************************************************************/
79 /*** file scope variables ************************************************************************/
81 /* ncurses supports cursor positions only within window */
82 /* We use our own cursor coordibates to support partially visible widgets */
83 static int mc_curs_row, mc_curs_col;
85 /*** file scope functions ************************************************************************/
86 /* --------------------------------------------------------------------------------------------- */
88 /* --------------------------------------------------------------------------------------------- */
90 static void
91 tty_setup_sigwinch (void (*handler) (int))
93 #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
94 struct sigaction act, oact;
96 memset (&act, 0, sizeof (act));
97 act.sa_handler = handler;
98 sigemptyset (&act.sa_mask);
99 #ifdef SA_RESTART
100 act.sa_flags = SA_RESTART;
101 #endif /* SA_RESTART */
102 sigaction (SIGWINCH, &act, &oact);
103 #endif /* SIGWINCH */
106 /* --------------------------------------------------------------------------------------------- */
108 static void
109 sigwinch_handler (int dummy)
111 (void) dummy;
113 mc_global.tty.winch_flag = 1;
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
121 mc_tty_normalize_lines_char (const char *ch)
123 char *str2;
124 int res;
126 struct mc_tty_lines_struct
128 const char *line;
129 int line_code;
130 } const lines_codes[] = {
131 {"\342\224\230", ACS_LRCORNER}, /* ┌ */
132 {"\342\224\224", ACS_LLCORNER}, /* └ */
133 {"\342\224\220", ACS_URCORNER}, /* ┐ */
134 {"\342\224\214", ACS_ULCORNER}, /* ┘ */
135 {"\342\224\234", ACS_LTEE}, /* ├ */
136 {"\342\224\244", ACS_RTEE}, /* ┤ */
137 {"\342\224\254", ACS_TTEE}, /* ┬ */
138 {"\342\224\264", ACS_BTEE}, /* ┴ */
139 {"\342\224\200", ACS_HLINE}, /* ─ */
140 {"\342\224\202", ACS_VLINE}, /* │ */
141 {"\342\224\274", ACS_PLUS}, /* ┼ */
143 {"\342\225\235", ACS_LRCORNER | A_BOLD}, /* ╔ */
144 {"\342\225\232", ACS_LLCORNER | A_BOLD}, /* ╚ */
145 {"\342\225\227", ACS_URCORNER | A_BOLD}, /* ╗ */
146 {"\342\225\224", ACS_ULCORNER | A_BOLD}, /* ╝ */
147 {"\342\225\237", ACS_LTEE | A_BOLD}, /* ╟ */
148 {"\342\225\242", ACS_RTEE | A_BOLD}, /* ╢ */
149 {"\342\225\244", ACS_TTEE | A_BOLD}, /* ╤ */
150 {"\342\225\247", ACS_BTEE | A_BOLD}, /* ╧ */
151 {"\342\225\220", ACS_HLINE | A_BOLD}, /* ═ */
152 {"\342\225\221", ACS_VLINE | A_BOLD}, /* ║ */
154 {NULL, 0}
157 if (ch == NULL)
158 return (int) ' ';
160 for (res = 0; lines_codes[res].line; res++)
162 if (strcmp (ch, lines_codes[res].line) == 0)
163 return lines_codes[res].line_code;
166 str2 = mc_tty_normalize_from_utf8 (ch);
167 res = g_utf8_get_char_validated (str2, -1);
169 if (res < 0)
170 res = (unsigned char) str2[0];
171 g_free (str2);
173 return res;
176 /* --------------------------------------------------------------------------------------------- */
178 void
179 tty_init (gboolean mouse_enable, gboolean is_xterm)
181 initscr ();
183 #ifdef HAVE_ESCDELAY
185 * If ncurses exports the ESCDELAY variable, it should be set to
186 * a low value, or you'll experience a delay in processing escape
187 * sequences that are recognized by mc (e.g. Esc-Esc). On the other
188 * hand, making ESCDELAY too small can result in some sequences
189 * (e.g. cursor arrows) being reported as separate keys under heavy
190 * processor load, and this can be a problem if mc hasn't learned
191 * them in the "Learn Keys" dialog. The value is in milliseconds.
193 ESCDELAY = 200;
194 #endif /* HAVE_ESCDELAY */
196 /* use Ctrl-g to generate SIGINT */
197 cur_term->Nttyb.c_cc[VINTR] = CTRL ('g'); /* ^g */
198 /* disable SIGQUIT to allow use Ctrl-\ key */
199 cur_term->Nttyb.c_cc[VQUIT] = NULL_VALUE;
200 tcsetattr (cur_term->Filedes, TCSANOW, &cur_term->Nttyb);
202 tty_start_interrupt_key ();
204 if (!mouse_enable)
205 use_mouse_p = MOUSE_DISABLED;
206 tty_init_xterm_support (is_xterm); /* do it before do_enter_ca_mode() call */
207 do_enter_ca_mode ();
208 tty_raw_mode ();
209 noecho ();
210 keypad (stdscr, TRUE);
211 nodelay (stdscr, FALSE);
213 tty_setup_sigwinch (sigwinch_handler);
216 /* --------------------------------------------------------------------------------------------- */
218 void
219 tty_shutdown (void)
221 disable_mouse ();
222 disable_bracketed_paste ();
223 tty_reset_shell_mode ();
224 tty_noraw_mode ();
225 tty_keypad (FALSE);
226 tty_reset_screen ();
227 do_exit_ca_mode ();
230 /* --------------------------------------------------------------------------------------------- */
232 void
233 tty_change_screen_size (void)
235 #if defined(TIOCGWINSZ) && NCURSES_VERSION_MAJOR >= 4
236 struct winsize winsz;
238 winsz.ws_col = winsz.ws_row = 0;
240 #ifndef NCURSES_VERSION
241 tty_noraw_mode ();
242 tty_reset_screen ();
243 #endif
245 /* Ioctl on the STDIN_FILENO */
246 ioctl (fileno (stdout), TIOCGWINSZ, &winsz);
247 if (winsz.ws_col != 0 && winsz.ws_row != 0)
249 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
250 resizeterm (winsz.ws_row, winsz.ws_col);
251 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
252 #else
253 COLS = winsz.ws_col;
254 LINES = winsz.ws_row;
255 #endif
257 #endif /* defined(TIOCGWINSZ) || NCURSES_VERSION_MAJOR >= 4 */
259 #ifdef ENABLE_SUBSHELL
260 if (mc_global.tty.use_subshell)
261 tty_resize (mc_global.tty.subshell_pty);
262 #endif
265 /* --------------------------------------------------------------------------------------------- */
267 void
268 tty_reset_prog_mode (void)
270 reset_prog_mode ();
273 /* --------------------------------------------------------------------------------------------- */
275 void
276 tty_reset_shell_mode (void)
278 reset_shell_mode ();
281 /* --------------------------------------------------------------------------------------------- */
283 void
284 tty_raw_mode (void)
286 raw (); /* FIXME: uneeded? */
287 cbreak ();
290 /* --------------------------------------------------------------------------------------------- */
292 void
293 tty_noraw_mode (void)
295 nocbreak (); /* FIXME: unneeded? */
296 noraw ();
299 /* --------------------------------------------------------------------------------------------- */
301 void
302 tty_noecho (void)
304 noecho ();
307 /* --------------------------------------------------------------------------------------------- */
310 tty_flush_input (void)
312 return flushinp ();
315 /* --------------------------------------------------------------------------------------------- */
317 void
318 tty_keypad (gboolean set)
320 keypad (stdscr, (bool) set);
323 /* --------------------------------------------------------------------------------------------- */
325 void
326 tty_nodelay (gboolean set)
328 nodelay (stdscr, (bool) set);
331 /* --------------------------------------------------------------------------------------------- */
334 tty_baudrate (void)
336 return baudrate ();
339 /* --------------------------------------------------------------------------------------------- */
342 tty_lowlevel_getch (void)
344 return getch ();
347 /* --------------------------------------------------------------------------------------------- */
350 tty_reset_screen (void)
352 return endwin ();
355 /* --------------------------------------------------------------------------------------------- */
357 void
358 tty_touch_screen (void)
360 touchwin (stdscr);
363 /* --------------------------------------------------------------------------------------------- */
365 void
366 tty_gotoyx (int y, int x)
368 mc_curs_row = y;
369 mc_curs_col = x;
371 if (y < 0)
372 y = 0;
373 if (y >= LINES)
374 y = LINES - 1;
376 if (x < 0)
377 x = 0;
378 if (x >= COLS)
379 x = COLS - 1;
381 move (y, x);
384 /* --------------------------------------------------------------------------------------------- */
386 void
387 tty_getyx (int *py, int *px)
389 *py = mc_curs_row;
390 *px = mc_curs_col;
393 /* --------------------------------------------------------------------------------------------- */
395 void
396 tty_draw_hline (int y, int x, int ch, int len)
398 int x1;
400 if (y < 0 || y >= LINES || x >= COLS)
401 return;
403 x1 = x;
405 if (x < 0)
407 len += x;
408 if (len <= 0)
409 return;
410 x = 0;
413 if ((chtype) ch == ACS_HLINE)
414 ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
416 move (y, x);
417 hline (ch, len);
418 move (y, x1);
420 mc_curs_row = y;
421 mc_curs_col = x1;
424 /* --------------------------------------------------------------------------------------------- */
426 void
427 tty_draw_vline (int y, int x, int ch, int len)
429 int y1;
431 if (x < 0 || x >= COLS || y >= LINES)
432 return;
434 y1 = y;
436 if (y < 0)
438 len += y;
439 if (len <= 0)
440 return;
441 y = 0;
444 if ((chtype) ch == ACS_VLINE)
445 ch = mc_tty_frm[MC_TTY_FRM_VERT];
447 move (y, x);
448 vline (ch, len);
449 move (y1, x);
451 mc_curs_row = y1;
452 mc_curs_col = x;
455 /* --------------------------------------------------------------------------------------------- */
457 void
458 tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
460 int i;
462 if (y < 0)
464 rows += y;
466 if (rows <= 0)
467 return;
469 y = 0;
472 if (x < 0)
474 cols += x;
476 if (cols <= 0)
477 return;
479 x = 0;
482 if (y + rows > LINES)
483 rows = LINES - y;
484 if (x + cols > COLS)
485 cols = COLS - x;
487 for (i = 0; i < rows; i++)
489 move (y + i, x);
490 hline (ch, cols);
493 move (y, x);
495 mc_curs_row = y;
496 mc_curs_col = x;
499 /* --------------------------------------------------------------------------------------------- */
501 void
502 tty_set_alt_charset (gboolean alt_charset)
504 (void) alt_charset;
507 /* --------------------------------------------------------------------------------------------- */
509 void
510 tty_display_8bit (gboolean what)
512 meta (stdscr, (int) what);
515 /* --------------------------------------------------------------------------------------------- */
517 void
518 tty_print_char (int c)
520 if (yx_in_screen (mc_curs_row, mc_curs_col))
521 addch (c);
522 mc_curs_col++;
525 /* --------------------------------------------------------------------------------------------- */
527 void
528 tty_print_anychar (int c)
530 if (mc_global.utf8_display || c > 255)
532 int res;
534 res = g_unichar_to_utf8 (c, (char *) str);
535 if (res == 0)
537 if (yx_in_screen (mc_curs_row, mc_curs_col))
538 addch ('.');
539 mc_curs_col++;
541 else
543 unsigned char str[UTF8_CHAR_LEN + 1];
544 const char *s;
546 str[res] = '\0';
547 s = str_term_form ((char *) str);
549 if (yx_in_screen (mc_curs_row, mc_curs_col))
550 addstr (s);
552 if (g_unichar_iswide (c))
553 mc_curs_col += 2;
554 else if (!g_unichar_iszerowidth (c))
555 mc_curs_col++;
558 else
560 if (yx_in_screen (mc_curs_row, mc_curs_col))
561 addch (c);
562 mc_curs_col++;
566 /* --------------------------------------------------------------------------------------------- */
568 void
569 tty_print_alt_char (int c, gboolean single)
571 if (yx_in_screen (mc_curs_row, mc_curs_col))
573 if ((chtype) c == ACS_VLINE)
574 c = mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT];
575 else if ((chtype) c == ACS_HLINE)
576 c = mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ];
577 else if ((chtype) c == ACS_LTEE)
578 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE];
579 else if ((chtype) c == ACS_RTEE)
580 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE];
581 else if ((chtype) c == ACS_ULCORNER)
582 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP];
583 else if ((chtype) c == ACS_LLCORNER)
584 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM];
585 else if ((chtype) c == ACS_URCORNER)
586 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP];
587 else if ((chtype) c == ACS_LRCORNER)
588 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM];
589 else if ((chtype) c == ACS_PLUS)
590 c = mc_tty_frm[MC_TTY_FRM_CROSS];
592 addch (c);
595 mc_curs_col++;
598 /* --------------------------------------------------------------------------------------------- */
600 void
601 tty_print_string (const char *s)
603 int len;
604 int start = 0;
606 s = str_term_form (s);
607 len = str_term_width1 (s);
609 /* line is upper or below the screen or entire line is before or after scrren */
610 if (mc_curs_row < 0 || mc_curs_row >= LINES || mc_curs_col + len <= 0 || mc_curs_col >= COLS)
612 mc_curs_col += len;
613 return;
616 /* skip invisible left part */
617 if (mc_curs_col < 0)
619 start = -mc_curs_col;
620 len += mc_curs_col;
621 mc_curs_col = 0;
624 mc_curs_col += len;
625 if (mc_curs_col >= COLS)
626 len = COLS - (mc_curs_col - len);
628 addstr (str_term_substring (s, start, len));
631 /* --------------------------------------------------------------------------------------------- */
633 void
634 tty_printf (const char *fmt, ...)
636 va_list args;
637 char buf[BUF_1K]; /* FIXME: is it enough? */
639 va_start (args, fmt);
640 g_vsnprintf (buf, sizeof (buf), fmt, args);
641 va_end (args);
642 tty_print_string (buf);
645 /* --------------------------------------------------------------------------------------------- */
647 char *
648 tty_tgetstr (const char *cap)
650 char *unused = NULL;
651 return tgetstr ((char *) cap, &unused);
654 /* --------------------------------------------------------------------------------------------- */
656 void
657 tty_refresh (void)
659 refresh ();
660 doupdate ();
663 /* --------------------------------------------------------------------------------------------- */
665 void
666 tty_beep (void)
668 beep ();
671 /* --------------------------------------------------------------------------------------------- */