updates
[silc.git] / apps / irssi / src / fe-text / screen.c
1 /*
2  screen.c : irssi
3
4     Copyright (C) 1999-2000 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 "misc.h"
24 #include "settings.h"
25
26 #include "screen.h"
27 #include "gui-readline.h"
28 #include "mainwindows.h"
29
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
32 #endif
33 #include <signal.h>
34
35 #ifndef COLOR_PAIRS
36 #define COLOR_PAIRS 64
37 #endif
38
39 #define MIN_SCREEN_WIDTH 20
40
41 static int scrx, scry;
42 static int use_colors;
43 static int freeze_refresh;
44 static int resized;
45
46 static int init_screen_int(void);
47 static void deinit_screen_int(void);
48
49 #ifdef SIGWINCH
50 static void sig_winch(int p)
51 {
52         resized = TRUE;
53 }
54 #endif
55
56 void screen_check_resizes(void)
57 {
58 #if defined (TIOCGWINSZ) && defined (HAVE_CURSES_RESIZETERM)
59         struct winsize ws;
60
61         /* Get new window size */
62         if (ioctl(0, TIOCGWINSZ, &ws) < 0)
63                 return;
64
65         if (ws.ws_row == LINES && ws.ws_col == COLS) {
66                 /* Same size, abort. */
67                 return;
68         }
69
70         if (ws.ws_col < MIN_SCREEN_WIDTH)
71                 ws.ws_col = MIN_SCREEN_WIDTH;
72
73         /* Resize curses terminal */
74         resizeterm(ws.ws_row, ws.ws_col);
75 #else
76         deinit_screen_int();
77         init_screen_int();
78         mainwindows_recreate();
79 #endif
80
81         mainwindows_resize(COLS, LINES);
82         resized = FALSE;
83 }
84
85 static void read_signals(void)
86 {
87 #ifndef WIN32
88         int signals[] = {
89                 SIGHUP, SIGINT, SIGQUIT, SIGTERM,
90                 SIGALRM, SIGUSR1, SIGUSR2
91         };
92         char *signames[] = {
93                 "hup", "int", "quit", "term",
94                 "alrm", "usr1", "usr2"
95         };
96
97         const char *ignores;
98         struct sigaction act;
99         int n;
100
101         ignores = settings_get_str("ignore_signals");
102
103         sigemptyset (&act.sa_mask);
104         act.sa_flags = 0;
105
106         for (n = 0; n < sizeof(signals)/sizeof(signals[0]); n++) {
107                 act.sa_handler = find_substr(ignores, signames[n]) ?
108                         SIG_IGN : SIG_DFL;
109                 sigaction(signals[n], &act, NULL);
110         }
111 #endif
112 }
113
114 static void read_settings(void)
115 {
116         int old_colors = use_colors;
117
118         use_colors = settings_get_bool("colors");
119         read_signals();
120         if (use_colors && !has_colors())
121                 use_colors = FALSE;
122
123         if (use_colors != old_colors)
124                 irssi_redraw();
125 }
126
127 static int init_curses(void)
128 {
129         char ansi_tab[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
130         int num;
131 #ifndef WIN32
132         struct sigaction act;
133 #endif
134
135         if (!initscr())
136                 return FALSE;
137
138         if (COLS < MIN_SCREEN_WIDTH)
139                 COLS = MIN_SCREEN_WIDTH;
140
141 #ifdef SIGWINCH
142         sigemptyset (&act.sa_mask);
143         act.sa_flags = 0;
144         act.sa_handler = sig_winch;
145         sigaction(SIGWINCH, &act, NULL);
146 #endif
147         raw(); noecho(); idlok(stdscr, 1);
148 #ifdef HAVE_CURSES_IDCOK
149         idcok(stdscr, 1);
150 #endif
151         intrflush(stdscr, FALSE); nodelay(stdscr, TRUE);
152
153         if (has_colors())
154                 start_color();
155         else if (use_colors)
156                 use_colors = FALSE;
157
158 #ifdef HAVE_NCURSES_USE_DEFAULT_COLORS
159         /* this lets us to use the "default" background color for colors <= 7 so
160            background pixmaps etc. show up right */
161         use_default_colors();
162
163         for (num = 1; num < COLOR_PAIRS; num++)
164                 init_pair(num, ansi_tab[num & 7], num <= 7 ? -1 : ansi_tab[num >> 3]);
165
166         init_pair(63, 0, -1); /* hm.. not THAT good idea, but probably more
167                                  people want dark grey than white on white.. */
168 #else
169         for (num = 1; num < COLOR_PAIRS; num++)
170                 init_pair(num, ansi_tab[num & 7], ansi_tab[num >> 3]);
171         init_pair(63, 0, 0);
172 #endif
173
174         clear();
175         return TRUE;
176 }
177
178 static int init_screen_int(void)
179 {
180         use_colors = settings_get_bool("colors");
181         read_signals();
182
183         scrx = scry = 0;
184         freeze_refresh = 0;
185
186         return init_curses();
187 }
188
189 static void deinit_screen_int(void)
190 {
191         endwin();
192 }
193
194 /* Initialize screen, detect screen length */
195 int init_screen(void)
196 {
197         settings_add_bool("lookandfeel", "colors", TRUE);
198         settings_add_str("misc", "ignore_signals", "");
199         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
200
201         return init_screen_int();
202 }
203
204 /* Deinitialize screen */
205 void deinit_screen(void)
206 {
207         deinit_screen_int();
208         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
209 }
210
211 void set_color(WINDOW *window, int col)
212 {
213         int attr;
214
215         if (!use_colors)
216                 attr = (col & 0x70) ? A_REVERSE : 0;
217         else if (col & ATTR_COLOR8)
218                 attr = (A_DIM | COLOR_PAIR(63));
219         else if ((col & 0x77) == 0)
220                 attr = A_NORMAL;
221         else
222                 attr = (COLOR_PAIR((col&7) + (col&0x70)/2));
223
224         if (col & 0x08) attr |= A_BOLD;
225         if (col & 0x80) attr |= A_BLINK;
226
227         if (col & ATTR_UNDERLINE) attr |= A_UNDERLINE;
228         if (col & ATTR_REVERSE) attr |= A_REVERSE;
229
230         wattrset(window, attr);
231 }
232
233 void set_bg(WINDOW *window, int col)
234 {
235         int attr;
236
237         if (!use_colors)
238                 attr = (col & 0x70) ? A_REVERSE : 0;
239         else {
240                 attr = (col == 8) ?
241                         (A_DIM | COLOR_PAIR(63)) :
242                         (COLOR_PAIR((col&7) + (col&0x70)/2));
243         }
244
245         if (col & 0x08) attr |= A_BOLD;
246         if (col & 0x80) attr |= A_BLINK;
247
248         wbkgdset(window, ' ' | attr);
249 }
250
251 void move_cursor(int y, int x)
252 {
253         scry = y;
254         scrx = x;
255 }
256
257 void screen_refresh_freeze(void)
258 {
259         freeze_refresh++;
260 }
261
262 void screen_refresh_thaw(void)
263 {
264         if (freeze_refresh > 0) {
265                 freeze_refresh--;
266                 if (freeze_refresh == 0) screen_refresh(NULL);
267         }
268 }
269
270 void screen_refresh(WINDOW *window)
271 {
272         if (window != NULL)
273                 wnoutrefresh(window);
274         if (freeze_refresh == 0) {
275                 move(scry, scrx);
276                 wnoutrefresh(stdscr);
277                 doupdate();
278         }
279 }