Merge branch '1396_with_search_engine'
[midnight-commander.git] / src / tty / tty.c
blob3d31b515b4d376bb06140afd431b02e25069eb4a
1 /*
2 Interface to the terminal controlling library.
4 Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
6 Written by:
7 Roland Illig <roland.illig@gmx.de>, 2005.
8 Andrew Borodin <aborodin@vmail.ru>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 /** \file tty.c
29 * \brief Source: %interface to the terminal controlling library
32 #include <config.h>
34 #include <signal.h>
35 #include <stdarg.h>
37 #include "../../src/global.h"
39 #include "../../src/tty/tty.h"
40 #include "../../src/tty/tty-internal.h"
42 #include "../../src/strutil.h"
44 /*** global variables **************************************************/
46 /* If true lines are drown by spaces */
47 gboolean slow_tty = FALSE;
49 /* If true use +, -, | for line drawing */
50 gboolean ugly_line_drawing = FALSE;
52 int mc_tty_ugly_frm[MC_TTY_FRM_MAX];
54 /*** file scope macro definitions **************************************/
56 /*** file scope type declarations **************************************/
58 /*** file scope variables **********************************************/
60 static volatile sig_atomic_t got_interrupt = 0;
62 /*** file scope functions **********************************************/
64 static void
65 sigintr_handler (int signo)
67 (void) &signo;
68 got_interrupt = 1;
71 /*** public functions **************************************************/
73 extern gboolean
74 tty_is_slow (void)
76 return slow_tty;
79 extern void
80 tty_start_interrupt_key (void)
82 struct sigaction act;
84 act.sa_handler = sigintr_handler;
85 sigemptyset (&act.sa_mask);
86 act.sa_flags = SA_RESTART;
87 sigaction (SIGINT, &act, NULL);
90 extern void
91 tty_enable_interrupt_key (void)
93 struct sigaction act;
95 act.sa_handler = sigintr_handler;
96 sigemptyset (&act.sa_mask);
97 act.sa_flags = 0;
98 sigaction (SIGINT, &act, NULL);
99 got_interrupt = 0;
102 extern void
103 tty_disable_interrupt_key (void)
105 struct sigaction act;
107 act.sa_handler = SIG_IGN;
108 sigemptyset (&act.sa_mask);
109 act.sa_flags = 0;
110 sigaction (SIGINT, &act, NULL);
113 extern gboolean
114 tty_got_interrupt (void)
116 gboolean rv;
118 rv = (got_interrupt != 0);
119 got_interrupt = 0;
120 return rv;
123 void
124 tty_print_one_hline (void)
126 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_thinhoriz]);
129 void
130 tty_print_one_vline (void)
132 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_thinvert]);
135 void
136 tty_draw_box (int y, int x, int ys, int xs)
138 tty_draw_vline (y, x, mc_tty_ugly_frm[MC_TTY_FRM_vert], ys);
139 tty_draw_vline (y, x + xs - 1, mc_tty_ugly_frm[MC_TTY_FRM_vert], ys);
140 tty_draw_hline (y, x, mc_tty_ugly_frm[MC_TTY_FRM_horiz], xs);
141 tty_draw_hline (y + ys - 1, x, mc_tty_ugly_frm[MC_TTY_FRM_horiz], xs);
142 tty_gotoyx (y, x);
143 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_lefttop]);
144 tty_gotoyx (y + ys - 1, x);
145 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_leftbottom]);
146 tty_gotoyx (y, x + xs - 1);
147 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_righttop]);
148 tty_gotoyx (y + ys - 1, x + xs - 1);
149 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_rightbottom]);
152 char *
153 mc_tty_normalize_from_utf8 (const char *str)
155 GIConv conv;
156 GString *buffer;
157 const char *_system_codepage = str_detect_termencoding ();
159 if (str_isutf8 (_system_codepage))
160 return g_strdup (str);
162 conv = g_iconv_open (_system_codepage, "UTF-8");
163 if (conv == INVALID_CONV)
164 return g_strdup (str);
166 buffer = g_string_new ("");
168 if (str_convert (conv, str, buffer) == ESTR_FAILURE) {
169 g_string_free (buffer, TRUE);
170 str_close_conv (conv);
171 return g_strdup (str);
173 str_close_conv (conv);
175 return g_string_free (buffer, FALSE);