summaryrefslogtreecommitdiff
path: root/src/fe-text/screen.c
blob: 288f8e027e05e06557fb47f4104461e5aa315577 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 screen.c : irssi

    Copyright (C) 1999-2000 Timo Sirainen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "signals.h"
#include "misc.h"
#include "settings.h"
#include "commands.h"

#include "screen.h"
#include "gui-readline.h"
#include "mainwindows.h"

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <signal.h>

#if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
#  include <ncurses.h>
#else
#  include <curses.h>
#endif

#ifndef COLOR_PAIRS
#  define COLOR_PAIRS 64
#endif

#if defined (TIOCGWINSZ) && defined (HAVE_CURSES_RESIZETERM)
#  define USE_RESIZE_TERM
#endif


#define RESIZE_TIMEOUT 500 /* how often to check if the terminal has been resized */
#define MIN_SCREEN_WIDTH 20

struct _SCREEN_WINDOW {
        WINDOW *win;
};

SCREEN_WINDOW *screen_root;
int screen_width, screen_height;

static int scrx, scry;
static int use_colors;
static int freeze_refresh;

static int init_screen_int(void);
static void deinit_screen_int(void);

#ifdef SIGWINCH
static int resize_timeout_tag;
#endif

static int resize_needed;

static int resize_timeout(void)
{
#ifdef USE_RESIZE_TERM
	struct winsize ws;
#endif

	if (!resize_needed)
		return TRUE;

        resize_needed = FALSE;

#ifdef USE_RESIZE_TERM

	/* Get new window size */
	if (ioctl(0, TIOCGWINSZ, &ws) < 0)
		return TRUE;

	if (ws.ws_row == LINES && ws.ws_col == COLS) {
		/* Same size, abort. */
		return TRUE;
	}

	if (ws.ws_col < MIN_SCREEN_WIDTH)
		ws.ws_col = MIN_SCREEN_WIDTH;

	/* Resize curses terminal */
	resizeterm(ws.ws_row, ws.ws_col);
#else
	deinit_screen_int();
	init_screen_int();
	mainwindows_recreate();
#endif

	screen_width = COLS;
	screen_height = LINES;
	mainwindows_resize(COLS, LINES);

	return TRUE;
}

#ifdef SIGWINCH
static void sig_winch(int p)
{
        resize_needed = TRUE;
}
#endif

static void cmd_resize(void)
{
	resize_needed = TRUE;
	resize_timeout();
}

static void read_settings(void)
{
	int old_colors = use_colors;

	use_colors = settings_get_bool("colors");
	if (use_colors && !has_colors())
		use_colors = FALSE;

	if (use_colors != old_colors)
		irssi_redraw();
}

static int init_curses(void)
{
	char ansi_tab[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
	int num;
#if !defined (WIN32) && defined(SIGWINCH)
	struct sigaction act;
#endif

	if (!initscr())
		return FALSE;

	if (COLS < MIN_SCREEN_WIDTH)
		COLS = MIN_SCREEN_WIDTH;

#ifdef SIGWINCH
	sigemptyset (&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = sig_winch;
	sigaction(SIGWINCH, &act, NULL);
#endif
	raw(); noecho(); idlok(stdscr, 1);
#ifdef HAVE_CURSES_IDCOK
	/*idcok(stdscr, 1); - disabled currently, causes redrawing problems with NetBSD */
#endif
	intrflush(stdscr, FALSE); nodelay(stdscr, TRUE);

	if (has_colors())
		start_color();
	else if (use_colors)
                use_colors = FALSE;

#ifdef HAVE_NCURSES_USE_DEFAULT_COLORS
	/* this lets us to use the "default" background color for colors <= 7 so
	   background pixmaps etc. show up right */
	use_default_colors();

	for (num = 1; num < COLOR_PAIRS; num++)
		init_pair(num, ansi_tab[num & 7], num <= 7 ? -1 : ansi_tab[num >> 3]);

	init_pair(63, 0, -1); /* hm.. not THAT good idea, but probably more
	                         people want dark grey than white on white.. */
#else
	for (num = 1; num < COLOR_PAIRS; num++)
		init_pair(num, ansi_tab[num & 7], ansi_tab[num >> 3]);
	init_pair(63, 0, 0);
#endif

	clear();
	return TRUE;
}

static int init_screen_int(void)
{
	int ret;

	ret = init_curses();
	if (!ret) return 0;

	use_colors = settings_get_bool("colors");

	scrx = scry = 0;
	freeze_refresh = 0;

	screen_root = g_new0(SCREEN_WINDOW, 1);
        screen_root->win = stdscr;

	screen_width = COLS;
	screen_height = LINES;
        return ret;
}

static void deinit_screen_int(void)
{
	endwin();
	g_free_and_null(screen_root);
}

/* Initialize screen, detect screen length */
int init_screen(void)
{
	settings_add_bool("lookandfeel", "colors", TRUE);

	signal_add("beep", (SIGNAL_FUNC) beep);
	signal_add("setup changed", (SIGNAL_FUNC) read_settings);
	command_bind("resize", NULL, (SIGNAL_FUNC) cmd_resize);

#ifdef SIGWINCH
	resize_timeout_tag = g_timeout_add(RESIZE_TIMEOUT,
					   (GSourceFunc) resize_timeout, NULL);
#endif
	return init_screen_int();
}

/* Deinitialize screen */
void deinit_screen(void)
{
	deinit_screen_int();

#ifdef SIGWINCH
	g_source_remove(resize_timeout_tag);
#endif

	command_unbind("resize", (SIGNAL_FUNC) cmd_resize);
	signal_remove("beep", (SIGNAL_FUNC) beep);
	signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}

int screen_has_colors(void)
{
        return has_colors();
}

void screen_clear(void)
{
        clear();
}

SCREEN_WINDOW *screen_window_create(int x, int y, int width, int height)
{
        SCREEN_WINDOW *scrwin;

	scrwin = g_new0(SCREEN_WINDOW, 1);
	scrwin->win = newwin(height, width, y, x);
	idlok(scrwin->win, 1);

        return scrwin;
}

void screen_window_destroy(SCREEN_WINDOW *window)
{
	delwin(window->win);
        g_free(window);
}

void screen_window_clear(SCREEN_WINDOW *window)
{
        werase(window->win);
}

void screen_window_move(SCREEN_WINDOW *window, int x, int y,
			int width, int height)
{
#ifdef HAVE_CURSES_WRESIZE
	wresize(window->win, height, width);
	mvwin(window->win, y, x);
#else
	delwin(window->win);
	window->win = newwin(height, width, y, x);
	idlok(window->win, 1);
#endif
}

void screen_window_scroll(SCREEN_WINDOW *window, int count)
{
	scrollok(window->win, TRUE);
	wscrl(window->win, count);
	scrollok(window->win, FALSE);
}

void screen_set_color(SCREEN_WINDOW *window, int col)
{
	int attr;

	if (!use_colors)
		attr = (col & 0x70) ? A_REVERSE : 0;
	else if (col & ATTR_COLOR8)
                attr = (A_DIM | COLOR_PAIR(63));
	else if ((col & 0x77) == 0)
		attr = A_NORMAL;
	else
		attr = (COLOR_PAIR((col&7) + (col&0x70)/2));

	if (col & 0x08) attr |= A_BOLD;
	if (col & 0x80) attr |= A_BLINK;

	if (col & ATTR_UNDERLINE) attr |= A_UNDERLINE;
	if (col & ATTR_REVERSE) attr |= A_REVERSE;

	wattrset(window->win, attr);
}

void screen_set_bg(SCREEN_WINDOW *window, int col)
{
	int attr;

	if (!use_colors)
		attr = (col & 0x70) ? A_REVERSE : 0;
	else {
		attr = (col == 8) ?
			(A_DIM | COLOR_PAIR(63)) :
			(COLOR_PAIR((col&7) + (col&0x70)/2));
	}

	if (col & 0x08) attr |= A_BOLD;
	if (col & 0x80) attr |= A_BLINK;

	wbkgdset(window->win, ' ' | attr);
}

void screen_move(SCREEN_WINDOW *window, int x, int y)
{
        wmove(window->win, y, x);
}

void screen_addch(SCREEN_WINDOW *window, int chr)
{
        waddch(window->win, chr);
}

void screen_addstr(SCREEN_WINDOW *window, char *str)
{
        waddstr(window->win, str);
}

void screen_clrtoeol(SCREEN_WINDOW *window)
{
        wclrtoeol(window->win);
}

void screen_move_cursor(int x, int y)
{
	scry = y;
	scrx = x;
}

void screen_refresh_freeze(void)
{
	freeze_refresh++;
}

void screen_refresh_thaw(void)
{
	if (freeze_refresh > 0) {
		freeze_refresh--;
		if (freeze_refresh == 0) screen_refresh(NULL);
	}
}

void screen_refresh(SCREEN_WINDOW *window)
{
	if (window != NULL)
		wnoutrefresh(window->win);

	if (freeze_refresh == 0) {
		move(scry, scrx);
		wnoutrefresh(stdscr);
		doupdate();
	}
}

int screen_getch(void)
{
	int key;

	key = getch();
	if (key == ERR)
		return -1;

#ifdef KEY_RESIZE
	if (key == KEY_RESIZE)
                return -1;
#endif

	return key;
}