Ticket #1648: implemented single-line boxes.
[kaloumi3.git] / lib / tty / tty.c
blob77876f5bf092654783f8da70bb8fbf2a7d73fc00
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 "lib/global.h"
38 #include "lib/strutil.h"
40 #include "tty.h"
41 #include "tty-internal.h"
44 /*** global variables **************************************************/
46 /* If true program softkeys (HP terminals only) on startup and after every
47 command ran in the subshell to the description found in the termcap/terminfo
48 database */
49 int reset_hp_softkeys = 0;
51 /* If true lines are drown by spaces */
52 gboolean slow_tty = FALSE;
54 /* If true use +, -, | for line drawing */
55 gboolean ugly_line_drawing = FALSE;
57 int mc_tty_ugly_frm[MC_TTY_FRM_MAX];
59 /*** file scope macro definitions **************************************/
61 /*** file scope type declarations **************************************/
63 /*** file scope variables **********************************************/
65 static volatile sig_atomic_t got_interrupt = 0;
67 /*** file scope functions **********************************************/
69 static void
70 sigintr_handler (int signo)
72 (void) &signo;
73 got_interrupt = 1;
76 /*** public functions **************************************************/
78 extern gboolean
79 tty_is_slow (void)
81 return slow_tty;
84 extern void
85 tty_start_interrupt_key (void)
87 struct sigaction act;
89 act.sa_handler = sigintr_handler;
90 sigemptyset (&act.sa_mask);
91 act.sa_flags = SA_RESTART;
92 sigaction (SIGINT, &act, NULL);
95 extern void
96 tty_enable_interrupt_key (void)
98 struct sigaction act;
100 act.sa_handler = sigintr_handler;
101 sigemptyset (&act.sa_mask);
102 act.sa_flags = 0;
103 sigaction (SIGINT, &act, NULL);
104 got_interrupt = 0;
107 extern void
108 tty_disable_interrupt_key (void)
110 struct sigaction act;
112 act.sa_handler = SIG_IGN;
113 sigemptyset (&act.sa_mask);
114 act.sa_flags = 0;
115 sigaction (SIGINT, &act, NULL);
118 extern gboolean
119 tty_got_interrupt (void)
121 gboolean rv;
123 rv = (got_interrupt != 0);
124 got_interrupt = 0;
125 return rv;
128 void
129 tty_print_one_hline (void)
131 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_thinhoriz], FALSE);
134 void
135 tty_print_one_vline (void)
137 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_thinvert], FALSE);
140 void
141 tty_draw_box (int y, int x, int ys, int xs, gboolean single)
143 if (single) {
144 tty_draw_vline (y, x, mc_tty_ugly_frm[MC_TTY_FRM_grpvert], ys);
145 tty_draw_vline (y, x + xs - 1, mc_tty_ugly_frm[MC_TTY_FRM_grpvert], ys);
146 tty_draw_hline (y, x, mc_tty_ugly_frm[MC_TTY_FRM_grphoriz], xs);
147 tty_draw_hline (y + ys - 1, x, mc_tty_ugly_frm[MC_TTY_FRM_grphoriz], xs);
148 tty_gotoyx (y, x);
149 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_grplefttop], single);
150 tty_gotoyx (y + ys - 1, x);
151 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_grpleftbottom], single);
152 tty_gotoyx (y, x + xs - 1);
153 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_grprighttop], single);
154 tty_gotoyx (y + ys - 1, x + xs - 1);
155 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_grprightbottom], single);
156 } else {
157 tty_draw_vline (y, x, mc_tty_ugly_frm[MC_TTY_FRM_vert], ys);
158 tty_draw_vline (y, x + xs - 1, mc_tty_ugly_frm[MC_TTY_FRM_vert], ys);
159 tty_draw_hline (y, x, mc_tty_ugly_frm[MC_TTY_FRM_horiz], xs);
160 tty_draw_hline (y + ys - 1, x, mc_tty_ugly_frm[MC_TTY_FRM_horiz], xs);
161 tty_gotoyx (y, x);
162 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_lefttop], single);
163 tty_gotoyx (y + ys - 1, x);
164 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_leftbottom], single);
165 tty_gotoyx (y, x + xs - 1);
166 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_righttop], single);
167 tty_gotoyx (y + ys - 1, x + xs - 1);
168 tty_print_alt_char (mc_tty_ugly_frm[MC_TTY_FRM_rightbottom], single);
172 char *
173 mc_tty_normalize_from_utf8 (const char *str)
175 GIConv conv;
176 GString *buffer;
177 const char *_system_codepage = str_detect_termencoding ();
179 if (str_isutf8 (_system_codepage))
180 return g_strdup (str);
182 conv = g_iconv_open (_system_codepage, "UTF-8");
183 if (conv == INVALID_CONV)
184 return g_strdup (str);
186 buffer = g_string_new ("");
188 if (str_convert (conv, str, buffer) == ESTR_FAILURE) {
189 g_string_free (buffer, TRUE);
190 str_close_conv (conv);
191 return g_strdup (str);
193 str_close_conv (conv);
195 return g_string_free (buffer, FALSE);