Merged with Irssi 0.8.4 from irssi.org CVS.
[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 <locale.h>
31 #  include <langinfo.h>
32 #endif
33
34 #ifdef HAVE_SYS_IOCTL_H
35 #  include <sys/ioctl.h>
36 #endif
37 #include <signal.h>
38 #include <termios.h>
39
40 #define MIN_SCREEN_WIDTH 20
41
42 int term_width, term_height, term_detached;
43
44 int term_use_colors;
45 int term_type;
46
47 static int force_colors;
48 static int resize_dirty;
49
50 int term_get_size(int *width, int *height)
51 {
52 #ifdef TIOCGWINSZ
53         struct winsize ws;
54
55         /* Get new window size */
56         if (ioctl(0, TIOCGWINSZ, &ws) < 0)
57                 return FALSE;
58
59         *width = ws.ws_col;
60         *height = ws.ws_row;
61
62         if (*width < MIN_SCREEN_WIDTH)
63                 *width = MIN_SCREEN_WIDTH;
64         if (*height < 1)
65                 *height = 1;
66         return TRUE;
67 #else
68         return FALSE;
69 #endif
70 }
71
72 /* Resize the terminal if needed */
73 void term_resize_dirty(void)
74 {
75         int width, height;
76
77         if (!resize_dirty)
78                 return;
79
80         resize_dirty = FALSE;
81
82         if (!term_get_size(&width, &height))
83                 width = height = -1;
84
85         if (height != term_height || width != term_width) {
86                 term_resize(width, height);
87                 mainwindows_resize(term_width, term_height);
88                 term_resize_final(width, height);
89         }
90 }
91
92 #ifdef SIGWINCH
93 static void sig_winch(int p)
94 {
95         irssi_set_dirty();
96         resize_dirty = TRUE;
97 }
98 #endif
99
100 static void cmd_resize(void)
101 {
102         resize_dirty = TRUE;
103         term_resize_dirty();
104 }
105
106 static void read_settings(void)
107 {
108         const char *str;
109         int old_colors = term_use_colors;
110         int old_type = term_type;
111
112         term_auto_detach(settings_get_bool("term_auto_detach"));
113
114         /* set terminal type */
115         str = settings_get_str("term_type");
116         if (g_strcasecmp(str, "utf-8") == 0)
117                 term_type = TERM_TYPE_UTF8;
118         else if (g_strcasecmp(str, "big5") == 0)
119                 term_type = TERM_TYPE_BIG5;
120         else
121                 term_type = TERM_TYPE_8BIT;
122
123         if (old_type != term_type)
124                 term_set_input_type(term_type);
125
126         /* change color stuff */
127         if (force_colors != settings_get_bool("term_force_colors")) {
128                 force_colors = settings_get_bool("term_force_colors");
129                 term_force_colors(force_colors);
130         }
131
132         term_use_colors = settings_get_bool("colors") &&
133                 (force_colors || term_has_colors());
134
135         if (term_use_colors != old_colors)
136                 irssi_redraw();
137 }
138
139 void term_common_init(void)
140 {
141 #ifdef SIGWINCH
142         struct sigaction act;
143 #endif
144         settings_add_bool("lookandfeel", "colors", TRUE);
145         settings_add_bool("lookandfeel", "term_force_colors", FALSE);
146         settings_add_bool("lookandfeel", "term_auto_detach", FALSE);
147         settings_add_bool("lookandfeel", "mirc_blink_fix", FALSE);
148         settings_add_str("lookandfeel", "term_type", "8bit");
149
150         force_colors = FALSE;
151         term_use_colors = term_has_colors() && settings_get_bool("colors");
152         read_settings();
153
154 #if defined (HAVE_NL_LANGINFO) && defined(CODESET)
155         setlocale(LC_CTYPE, "");
156         if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
157                 term_type = TERM_TYPE_UTF8;
158                 term_set_input_type(TERM_TYPE_UTF8);
159         }
160 #endif
161
162         signal_add("beep", (SIGNAL_FUNC) term_beep);
163         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
164         command_bind("resize", NULL, (SIGNAL_FUNC) cmd_resize);
165
166 #ifdef SIGWINCH
167         sigemptyset (&act.sa_mask);
168         act.sa_flags = 0;
169         act.sa_handler = sig_winch;
170         sigaction(SIGWINCH, &act, NULL);
171 #endif
172 }
173
174 void term_common_deinit(void)
175 {
176         command_unbind("resize", (SIGNAL_FUNC) cmd_resize);
177         signal_remove("beep", (SIGNAL_FUNC) term_beep);
178         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
179 }