/[irssi]/cuix/fe-text/term-curses.c
ViewVC logotype

Contents of /cuix/fe-text/term-curses.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1928 - (show annotations)
Sun Oct 28 19:20:59 2001 UTC (11 years, 6 months ago) by cras
Original Path: irssi/trunk/src/fe-text/term-curses.c
File MIME type: text/plain
File size: 8015 byte(s)
Use TCSADRAIN option when changing terminfo settings.

1 /*
2 term-curses.c : irssi
3
4 Copyright (C) 1999-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 "settings.h"
23
24 #include "term.h"
25 #include "mainwindows.h"
26
27 #if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
28 # include <ncurses.h>
29 #else
30 # include <curses.h>
31 #endif
32 #include <termios.h>
33 #include <signal.h>
34
35 #ifndef COLOR_PAIRS
36 # define COLOR_PAIRS 64
37 #endif
38
39 #if defined (TIOCGWINSZ) && defined (HAVE_CURSES_RESIZETERM)
40 # define USE_RESIZE_TERM
41 #endif
42
43 #ifndef _POSIX_VDISABLE
44 # define _POSIX_VDISABLE 0
45 #endif
46
47 struct _TERM_WINDOW {
48 int x, y;
49 int width, height;
50 WINDOW *win;
51 };
52
53 TERM_WINDOW *root_window;
54 int term_width, term_height;
55
56 static int curs_x, curs_y;
57 static int freeze_refresh;
58 static struct termios old_tio;
59
60 static int init_curses(void)
61 {
62 char ansi_tab[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
63 int num;
64 struct termios tio;
65
66 if (!initscr())
67 return FALSE;
68
69 cbreak(); noecho(); idlok(stdscr, 1);
70 #ifdef HAVE_CURSES_IDCOK
71 /*idcok(stdscr, 1); - disabled currently, causes redrawing problems with NetBSD */
72 #endif
73 intrflush(stdscr, FALSE); nodelay(stdscr, TRUE);
74
75 /* Disable INTR, QUIT, VDSUSP and SUSP keys */
76 if (tcgetattr(0, &old_tio) == 0) {
77 memcpy(&tio, &old_tio, sizeof(tio));
78 tio.c_cc[VINTR] = _POSIX_VDISABLE;
79 tio.c_cc[VQUIT] = _POSIX_VDISABLE;
80 #ifdef VDSUSP
81 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
82 #endif
83 #ifdef VSUSP
84 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
85 #endif
86 tcsetattr(0, TCSADRAIN, &tio);
87 }
88
89 if (has_colors())
90 start_color();
91 else if (term_use_colors)
92 term_use_colors = FALSE;
93
94 #ifdef HAVE_NCURSES_USE_DEFAULT_COLORS
95 /* this lets us to use the "default" background color for colors <= 7 so
96 background pixmaps etc. show up right */
97 use_default_colors();
98
99 for (num = 1; num < COLOR_PAIRS; num++)
100 init_pair(num, ansi_tab[num & 7], num <= 7 ? -1 : ansi_tab[num >> 3]);
101
102 init_pair(63, 0, -1); /* hm.. not THAT good idea, but probably more
103 people want dark grey than white on white.. */
104 #else
105 for (num = 1; num < COLOR_PAIRS; num++)
106 init_pair(num, ansi_tab[num & 7], ansi_tab[num >> 3]);
107 init_pair(63, 0, 0);
108 #endif
109
110 clear();
111 return TRUE;
112 }
113
114 static int term_init_int(void)
115 {
116 int ret;
117
118 ret = init_curses();
119 if (!ret) return 0;
120
121 curs_x = curs_y = 0;
122 freeze_refresh = 0;
123
124 root_window = g_new0(TERM_WINDOW, 1);
125 root_window->win = stdscr;
126
127 term_width = COLS;
128 term_height = LINES;
129 return ret;
130 }
131
132 static void term_deinit_int(void)
133 {
134 tcsetattr(0, TCSADRAIN, &old_tio);
135
136 endwin();
137 g_free_and_null(root_window);
138 }
139
140 int term_init(void)
141 {
142 if (!term_init_int())
143 return FALSE;
144
145 settings_add_int("lookandfeel", "default_color", 7);
146 term_common_init();
147 return TRUE;
148 }
149
150 void term_deinit(void)
151 {
152 term_common_deinit();
153 term_deinit_int();
154 }
155
156 /* Resize terminal - if width or height is negative,
157 the new size is unknown and should be figured out somehow */
158 void term_resize(int width, int height)
159 {
160 #ifdef HAVE_CURSES_RESIZETERM
161 if (width < 0 || height < 0) {
162 #endif
163 term_deinit_int();
164 term_init_int();
165 mainwindows_recreate();
166 #ifdef HAVE_CURSES_RESIZETERM
167 } else if (term_width != width || term_height != height) {
168 term_width = width;
169 term_height = height;
170 resizeterm(term_height, term_width);
171 }
172 #endif
173 }
174
175 /* Returns TRUE if terminal has colors */
176 int term_has_colors(void)
177 {
178 return has_colors();
179 }
180
181 /* Force the colors on any way you can */
182 void term_force_colors(int set)
183 {
184 /* don't do anything with curses */
185 }
186
187 /* Clear screen */
188 void term_clear(void)
189 {
190 clear();
191 }
192
193 /* Beep */
194 void term_beep(void)
195 {
196 beep();
197 }
198
199 /* Create a new window in terminal */
200 TERM_WINDOW *term_window_create(int x, int y, int width, int height)
201 {
202 TERM_WINDOW *window;
203
204 window = g_new0(TERM_WINDOW, 1);
205 window->x = x; window->y = y;
206 window->width = width; window->height = height;
207 window->win = newwin(height, width, y, x);
208 idlok(window->win, 1);
209
210 return window;
211 }
212
213 /* Destroy a terminal window */
214 void term_window_destroy(TERM_WINDOW *window)
215 {
216 delwin(window->win);
217 g_free(window);
218 }
219
220 /* Move/resize a window */
221 void term_window_move(TERM_WINDOW *window, int x, int y,
222 int width, int height)
223 {
224 /* some checks to make sure the window is visible in screen,
225 otherwise curses could get nasty and not show our window anymore. */
226 if (width < 1) width = 1;
227 if (height < 1) height = 1;
228 if (x+width > term_width) x = term_width-width;
229 if (y+height > term_height) y = term_height-height;
230
231 #ifdef HAVE_CURSES_WRESIZE
232 if (window->width != width || window->height != height)
233 wresize(window->win, height, width);
234 if (window->x != x || window->y != y)
235 mvwin(window->win, y, x);
236 #else
237 if (window->width != width || window->height != height ||
238 window->x != x || window->y != y) {
239 delwin(window->win);
240 window->win = newwin(height, width, y, x);
241 idlok(window->win, 1);
242 }
243 #endif
244 window->x = x; window->y = y;
245 window->width = width; window->height = height;
246 }
247
248 /* Clear window */
249 void term_window_clear(TERM_WINDOW *window)
250 {
251 werase(window->win);
252 }
253
254 /* Scroll window up/down */
255 void term_window_scroll(TERM_WINDOW *window, int count)
256 {
257 scrollok(window->win, TRUE);
258 wscrl(window->win, count);
259 scrollok(window->win, FALSE);
260 }
261
262 static int get_attr(int color)
263 {
264 int attr;
265
266 if (!term_use_colors)
267 attr = (color & 0x70) ? A_REVERSE : 0;
268 else if (((color & 0x0f) == 8) && (color & ATTR_BOLD) == 0)
269 attr = (A_DIM | COLOR_PAIR(63));
270 else if ((color & 0x77) == 0)
271 attr = A_NORMAL;
272 else {
273 if (color & ATTR_RESETFG) {
274 color &= ~0x0f;
275 color |= settings_get_int("default_color");
276 }
277 attr = (COLOR_PAIR((color&7) + (color&0x70)/2));
278 }
279
280 if ((color & 0x08) || (color & ATTR_BOLD)) attr |= A_BOLD;
281 if (color & ATTR_BLINK) attr |= A_BLINK;
282
283 if (color & ATTR_UNDERLINE) attr |= A_UNDERLINE;
284 if (color & ATTR_REVERSE) attr |= A_REVERSE;
285 return attr;
286 }
287
288 /* Change active color */
289 void term_set_color(TERM_WINDOW *window, int col)
290 {
291 wattrset(window->win, get_attr(col));
292 wbkgdset(window->win, ' ' | get_attr(col));
293 }
294
295 void term_move(TERM_WINDOW *window, int x, int y)
296 {
297 wmove(window->win, y, x);
298 }
299
300 void term_addch(TERM_WINDOW *window, int chr)
301 {
302 waddch(window->win, chr);
303 }
304
305 void term_addstr(TERM_WINDOW *window, char *str)
306 {
307 waddstr(window->win, str);
308 }
309
310 void term_clrtoeol(TERM_WINDOW *window)
311 {
312 wclrtoeol(window->win);
313 }
314
315 void term_move_cursor(int x, int y)
316 {
317 curs_x = x;
318 curs_y = y;
319 }
320
321 void term_refresh_freeze(void)
322 {
323 freeze_refresh++;
324 }
325
326 void term_refresh_thaw(void)
327 {
328 if (freeze_refresh > 0) {
329 freeze_refresh--;
330 if (freeze_refresh == 0) term_refresh(NULL);
331 }
332 }
333
334 void term_refresh(TERM_WINDOW *window)
335 {
336 if (window != NULL)
337 wnoutrefresh(window->win);
338
339 if (freeze_refresh == 0) {
340 move(curs_y, curs_x);
341 wnoutrefresh(stdscr);
342 doupdate();
343 }
344 }
345
346 void term_stop(void)
347 {
348 term_deinit_int();
349 kill(getpid(), SIGSTOP);
350 term_init_int();
351 irssi_redraw();
352 }
353
354 int term_getch(void)
355 {
356 int key;
357
358 key = getch();
359 if (key == ERR)
360 return -1;
361
362 #ifdef KEY_RESIZE
363 if (key == KEY_RESIZE)
364 return -1;
365 #endif
366
367 return key;
368 }

Properties

Name Value
cvs2svn:cvs-rev 1.2
svn:eol-style native
svn:keywords author date id revision
svn:mime-type text/plain

Irssi staff
ViewVC Help
Powered by ViewVC 1.1.13