cppcheck: reduce variable scope.
[midnight-commander.git] / lib / tty / tty-ncurses.c
blob5bf8612f1581eb3533153a4cd50af225e375338c
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 act.sa_handler = handler;
97 sigemptyset (&act.sa_mask);
98 #ifdef SA_RESTART
99 act.sa_flags = SA_RESTART;
100 #else
101 act.sa_flags = 0;
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 do_enter_ca_mode() call */
208 do_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 do_exit_ca_mode ();
231 /* --------------------------------------------------------------------------------------------- */
233 void
234 tty_change_screen_size (void)
236 #if defined(TIOCGWINSZ) && NCURSES_VERSION_MAJOR >= 4
237 struct winsize winsz;
239 winsz.ws_col = winsz.ws_row = 0;
241 #ifndef NCURSES_VERSION
242 tty_noraw_mode ();
243 tty_reset_screen ();
244 #endif
246 /* Ioctl on the STDIN_FILENO */
247 ioctl (fileno (stdout), TIOCGWINSZ, &winsz);
248 if (winsz.ws_col != 0 && winsz.ws_row != 0)
250 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
251 resizeterm (winsz.ws_row, winsz.ws_col);
252 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
253 #else
254 COLS = winsz.ws_col;
255 LINES = winsz.ws_row;
256 #endif
258 #endif /* defined(TIOCGWINSZ) || NCURSES_VERSION_MAJOR >= 4 */
260 #ifdef ENABLE_SUBSHELL
261 if (mc_global.tty.use_subshell)
262 tty_resize (mc_global.tty.subshell_pty);
263 #endif
266 /* --------------------------------------------------------------------------------------------- */
268 void
269 tty_reset_prog_mode (void)
271 reset_prog_mode ();
274 /* --------------------------------------------------------------------------------------------- */
276 void
277 tty_reset_shell_mode (void)
279 reset_shell_mode ();
282 /* --------------------------------------------------------------------------------------------- */
284 void
285 tty_raw_mode (void)
287 raw (); /* FIXME: uneeded? */
288 cbreak ();
291 /* --------------------------------------------------------------------------------------------- */
293 void
294 tty_noraw_mode (void)
296 nocbreak (); /* FIXME: unneeded? */
297 noraw ();
300 /* --------------------------------------------------------------------------------------------- */
302 void
303 tty_noecho (void)
305 noecho ();
308 /* --------------------------------------------------------------------------------------------- */
311 tty_flush_input (void)
313 return flushinp ();
316 /* --------------------------------------------------------------------------------------------- */
318 void
319 tty_keypad (gboolean set)
321 keypad (stdscr, (bool) set);
324 /* --------------------------------------------------------------------------------------------- */
326 void
327 tty_nodelay (gboolean set)
329 nodelay (stdscr, (bool) set);
332 /* --------------------------------------------------------------------------------------------- */
335 tty_baudrate (void)
337 return baudrate ();
340 /* --------------------------------------------------------------------------------------------- */
343 tty_lowlevel_getch (void)
345 return getch ();
348 /* --------------------------------------------------------------------------------------------- */
351 tty_reset_screen (void)
353 return endwin ();
356 /* --------------------------------------------------------------------------------------------- */
358 void
359 tty_touch_screen (void)
361 touchwin (stdscr);
364 /* --------------------------------------------------------------------------------------------- */
366 void
367 tty_gotoyx (int y, int x)
369 mc_curs_row = y;
370 mc_curs_col = x;
372 if (y < 0)
373 y = 0;
374 if (y >= LINES)
375 y = LINES - 1;
377 if (x < 0)
378 x = 0;
379 if (x >= COLS)
380 x = COLS - 1;
382 move (y, x);
385 /* --------------------------------------------------------------------------------------------- */
387 void
388 tty_getyx (int *py, int *px)
390 *py = mc_curs_row;
391 *px = mc_curs_col;
394 /* --------------------------------------------------------------------------------------------- */
396 void
397 tty_draw_hline (int y, int x, int ch, int len)
399 int x1;
401 if (y < 0 || y >= LINES || x >= COLS)
402 return;
404 x1 = x;
406 if (x < 0)
408 len += x;
409 if (len <= 0)
410 return;
411 x = 0;
414 if ((chtype) ch == ACS_HLINE)
415 ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
417 move (y, x);
418 hline (ch, len);
419 move (y, x1);
421 mc_curs_row = y;
422 mc_curs_col = x1;
425 /* --------------------------------------------------------------------------------------------- */
427 void
428 tty_draw_vline (int y, int x, int ch, int len)
430 int y1;
432 if (x < 0 || x >= COLS || y >= LINES)
433 return;
435 y1 = y;
437 if (y < 0)
439 len += y;
440 if (len <= 0)
441 return;
442 y = 0;
445 if ((chtype) ch == ACS_VLINE)
446 ch = mc_tty_frm[MC_TTY_FRM_VERT];
448 move (y, x);
449 vline (ch, len);
450 move (y1, x);
452 mc_curs_row = y1;
453 mc_curs_col = x;
456 /* --------------------------------------------------------------------------------------------- */
458 void
459 tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
461 int i;
463 if (y < 0)
465 rows += y;
467 if (rows <= 0)
468 return;
470 y = 0;
473 if (x < 0)
475 cols += x;
477 if (cols <= 0)
478 return;
480 x = 0;
483 if (y + rows > LINES)
484 rows = LINES - y;
485 if (x + cols > COLS)
486 cols = COLS - x;
488 for (i = 0; i < rows; i++)
490 move (y + i, x);
491 hline (ch, cols);
494 move (y, x);
496 mc_curs_row = y;
497 mc_curs_col = x;
500 /* --------------------------------------------------------------------------------------------- */
502 void
503 tty_set_alt_charset (gboolean alt_charset)
505 (void) alt_charset;
508 /* --------------------------------------------------------------------------------------------- */
510 void
511 tty_display_8bit (gboolean what)
513 meta (stdscr, (int) what);
516 /* --------------------------------------------------------------------------------------------- */
518 void
519 tty_print_char (int c)
521 if (yx_in_screen (mc_curs_row, mc_curs_col))
522 addch (c);
523 mc_curs_col++;
526 /* --------------------------------------------------------------------------------------------- */
528 void
529 tty_print_anychar (int c)
531 if (mc_global.utf8_display || c > 255)
533 int res;
535 res = g_unichar_to_utf8 (c, (char *) str);
536 if (res == 0)
538 if (yx_in_screen (mc_curs_row, mc_curs_col))
539 addch ('.');
540 mc_curs_col++;
542 else
544 unsigned char str[UTF8_CHAR_LEN + 1];
545 const char *s;
547 str[res] = '\0';
548 s = str_term_form ((char *) str);
550 if (yx_in_screen (mc_curs_row, mc_curs_col))
551 addstr (s);
553 if (g_unichar_iswide (c))
554 mc_curs_col += 2;
555 else if (!g_unichar_iszerowidth (c))
556 mc_curs_col++;
559 else
561 if (yx_in_screen (mc_curs_row, mc_curs_col))
562 addch (c);
563 mc_curs_col++;
567 /* --------------------------------------------------------------------------------------------- */
569 void
570 tty_print_alt_char (int c, gboolean single)
572 if (yx_in_screen (mc_curs_row, mc_curs_col))
574 if ((chtype) c == ACS_VLINE)
575 c = mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT];
576 else if ((chtype) c == ACS_HLINE)
577 c = mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ];
578 else if ((chtype) c == ACS_LTEE)
579 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE];
580 else if ((chtype) c == ACS_RTEE)
581 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE];
582 else if ((chtype) c == ACS_ULCORNER)
583 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP];
584 else if ((chtype) c == ACS_LLCORNER)
585 c = mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM];
586 else if ((chtype) c == ACS_URCORNER)
587 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP];
588 else if ((chtype) c == ACS_LRCORNER)
589 c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM];
590 else if ((chtype) c == ACS_PLUS)
591 c = mc_tty_frm[MC_TTY_FRM_CROSS];
593 addch (c);
596 mc_curs_col++;
599 /* --------------------------------------------------------------------------------------------- */
601 void
602 tty_print_string (const char *s)
604 int len;
605 int start = 0;
607 s = str_term_form (s);
608 len = str_term_width1 (s);
610 /* line is upper or below the screen or entire line is before or after scrren */
611 if (mc_curs_row < 0 || mc_curs_row >= LINES || mc_curs_col + len <= 0 || mc_curs_col >= COLS)
613 mc_curs_col += len;
614 return;
617 /* skip invisible left part */
618 if (mc_curs_col < 0)
620 start = -mc_curs_col;
621 len += mc_curs_col;
622 mc_curs_col = 0;
625 mc_curs_col += len;
626 if (mc_curs_col >= COLS)
627 len = COLS - (mc_curs_col - len);
629 addstr (str_term_substring (s, start, len));
632 /* --------------------------------------------------------------------------------------------- */
634 void
635 tty_printf (const char *fmt, ...)
637 va_list args;
638 char buf[BUF_1K]; /* FIXME: is it enough? */
640 va_start (args, fmt);
641 g_vsnprintf (buf, sizeof (buf), fmt, args);
642 va_end (args);
643 tty_print_string (buf);
646 /* --------------------------------------------------------------------------------------------- */
648 char *
649 tty_tgetstr (const char *cap)
651 char *unused = NULL;
652 return tgetstr ((char *) cap, &unused);
655 /* --------------------------------------------------------------------------------------------- */
657 void
658 tty_refresh (void)
660 refresh ();
661 doupdate ();
664 /* --------------------------------------------------------------------------------------------- */
666 void
667 tty_beep (void)
669 beep ();
672 /* --------------------------------------------------------------------------------------------- */