Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / fe-text / term.c
1 /*
2  term.c : irssi
3
4     Copyright (C) 2001 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "settings.h"
25
26 #include "term.h"
27 #include "mainwindows.h"
28
29 #ifdef HAVE_NL_LANGINFO
30 #  include <langinfo.h>
31 #endif
32
33 #ifdef HAVE_SYS_IOCTL_H
34 #  include <sys/ioctl.h>
35 #endif
36 #include <signal.h>
37 #include <termios.h>
38
39 #define MIN_SCREEN_WIDTH 20
40
41 int term_width, term_height, term_detached;
42
43 int term_use_colors;
44 int term_type;
45
46 static int force_colors;
47 static int resize_dirty;
48
49 int term_get_size(int *width, int *height)
50 {
51 #ifdef TIOCGWINSZ
52         struct winsize ws;
53
54         /* Get new window size */
55         if (ioctl(0, TIOCGWINSZ, &ws) < 0)
56                 return FALSE;
57
58         if (ws.ws_row == 0 && ws.ws_col == 0)
59                 return FALSE;
60
61         *width = ws.ws_col;
62         *height = ws.ws_row;
63
64         if (*width < MIN_SCREEN_WIDTH)
65                 *width = MIN_SCREEN_WIDTH;
66         if (*height < 1)
67                 *height = 1;
68         return TRUE;
69 #else
70         return FALSE;
71 #endif
72 }
73
74 /* Resize the terminal if needed */
75 void term_resize_dirty(void)
76 {
77         int width, height;
78
79         if (!resize_dirty)
80                 return;
81
82         resize_dirty = FALSE;
83
84         if (!term_get_size(&width, &height))
85                 width = height = -1;
86
87         if (height != term_height || width != term_width) {
88                 term_resize(width, height);
89                 mainwindows_resize(term_width, term_height);
90                 term_resize_final(width, height);
91         }
92 }
93
94 #ifdef SIGWINCH
95 static void sig_winch(int p)
96 {
97         irssi_set_dirty();
98         resize_dirty = TRUE;
99 }
100 #endif
101
102 static void cmd_resize(void)
103 {
104         resize_dirty = TRUE;
105         term_resize_dirty();
106 }
107
108 static void cmd_redraw(void)
109 {
110         irssi_redraw();
111 }
112
113 static void read_settings(void)
114 {
115         const char *str;
116         int old_colors = term_use_colors;
117         int old_type = term_type;
118
119         term_auto_detach(settings_get_bool("term_auto_detach"));
120
121         /* set terminal type */
122         str = settings_get_str("term_charset");
123         if (g_strcasecmp(str, "utf-8") == 0)
124                 term_type = TERM_TYPE_UTF8;
125         else if (g_strcasecmp(str, "big5") == 0)
126                 term_type = TERM_TYPE_BIG5;
127         else
128                 term_type = TERM_TYPE_8BIT;
129
130         if (old_type != term_type)
131                 term_set_input_type(term_type);
132
133         /* change color stuff */
134         if (force_colors != settings_get_bool("term_force_colors")) {
135                 force_colors = settings_get_bool("term_force_colors");
136                 term_force_colors(force_colors);
137         }
138
139         term_use_colors = settings_get_bool("colors") &&
140                 (force_colors || term_has_colors());
141
142         if (term_use_colors != old_colors)
143                 irssi_redraw();
144 }
145
146 void term_common_init(void)
147 {
148 #ifdef SIGWINCH
149         struct sigaction act;
150 #endif
151         settings_add_bool("lookandfeel", "colors", TRUE);
152         settings_add_bool("lookandfeel", "term_force_colors", FALSE);
153         settings_add_bool("lookandfeel", "term_auto_detach", FALSE);
154         settings_add_bool("lookandfeel", "mirc_blink_fix", FALSE);
155
156         force_colors = FALSE;
157         term_use_colors = term_has_colors() && settings_get_bool("colors");
158         read_settings();
159
160 #if defined (HAVE_NL_LANGINFO) && defined(CODESET)
161         if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
162                 term_type = TERM_TYPE_UTF8;
163                 term_set_input_type(TERM_TYPE_UTF8);
164         }
165 #endif
166
167         signal_add("beep", (SIGNAL_FUNC) term_beep);
168         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
169         command_bind("resize", NULL, (SIGNAL_FUNC) cmd_resize);
170         command_bind("redraw", NULL, (SIGNAL_FUNC) cmd_redraw);
171
172 #ifdef SIGWINCH
173         sigemptyset (&act.sa_mask);
174         act.sa_flags = 0;
175         act.sa_handler = sig_winch;
176         sigaction(SIGWINCH, &act, NULL);
177 #endif
178 }
179
180 void term_common_deinit(void)
181 {
182         command_unbind("resize", (SIGNAL_FUNC) cmd_resize);
183         command_unbind("redraw", (SIGNAL_FUNC) cmd_redraw);
184         signal_remove("beep", (SIGNAL_FUNC) term_beep);
185         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
186 }