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