4 Copyright (C) 1999-2001 Timo Sirainen
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.
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.
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
25 #include "mainwindows.h"
27 #if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)
36 # define COLOR_PAIRS 64
39 #if defined (TIOCGWINSZ) && defined (HAVE_CURSES_RESIZETERM)
40 # define USE_RESIZE_TERM
43 #ifndef _POSIX_VDISABLE
44 # define _POSIX_VDISABLE 0
53 TERM_WINDOW *root_window;
55 static int curs_x, curs_y;
56 static int freeze_refresh;
57 static struct termios old_tio;
59 static int init_curses(void)
61 char ansi_tab[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
68 cbreak(); noecho(); idlok(stdscr, 1);
69 #ifdef HAVE_CURSES_IDCOK
70 /*idcok(stdscr, 1); - disabled currently, causes redrawing problems with NetBSD */
72 intrflush(stdscr, FALSE); nodelay(stdscr, TRUE);
74 /* Disable INTR, QUIT, VDSUSP and SUSP keys */
75 if (tcgetattr(0, &old_tio) == 0) {
76 memcpy(&tio, &old_tio, sizeof(tio));
77 tio.c_cc[VINTR] = _POSIX_VDISABLE;
78 tio.c_cc[VQUIT] = _POSIX_VDISABLE;
80 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
83 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
85 tcsetattr(0, TCSADRAIN, &tio);
90 else if (term_use_colors)
91 term_use_colors = FALSE;
93 #ifdef HAVE_NCURSES_USE_DEFAULT_COLORS
94 /* this lets us to use the "default" background color for colors <= 7 so
95 background pixmaps etc. show up right */
98 for (num = 1; num < COLOR_PAIRS; num++)
99 init_pair(num, ansi_tab[num & 7], num <= 7 ? -1 : ansi_tab[num >> 3]);
101 init_pair(63, 0, -1); /* hm.. not THAT good idea, but probably more
102 people want dark grey than white on white.. */
104 for (num = 1; num < COLOR_PAIRS; num++)
105 init_pair(num, ansi_tab[num & 7], ansi_tab[num >> 3]);
113 static int term_init_int(void)
123 root_window = g_new0(TERM_WINDOW, 1);
124 root_window->win = stdscr;
131 static void term_deinit_int(void)
133 tcsetattr(0, TCSADRAIN, &old_tio);
136 g_free_and_null(root_window);
141 if (!term_init_int())
144 settings_add_int("lookandfeel", "default_color", 7);
149 void term_deinit(void)
151 term_common_deinit();
155 /* Resize terminal - if width or height is negative,
156 the new size is unknown and should be figured out somehow */
157 void term_resize(int width, int height)
159 #ifdef HAVE_CURSES_RESIZETERM
160 if (width < 0 || height < 0) {
164 #ifdef HAVE_CURSES_RESIZETERM
165 } else if (term_width != width || term_height != height) {
167 term_height = height;
168 resizeterm(term_height, term_width);
173 void term_resize_final(int width, int height)
175 #ifdef HAVE_CURSES_RESIZETERM
176 if (width < 0 || height < 0)
177 mainwindows_recreate();
179 mainwindows_recreate();
183 /* Returns TRUE if terminal has colors */
184 int term_has_colors(void)
189 /* Force the colors on any way you can */
190 void term_force_colors(int set)
192 /* don't do anything with curses */
196 void term_clear(void)
198 term_set_color(root_window, 0);
208 /* Create a new window in terminal */
209 TERM_WINDOW *term_window_create(int x, int y, int width, int height)
213 window = g_new0(TERM_WINDOW, 1);
214 window->x = x; window->y = y;
215 window->width = width; window->height = height;
216 window->win = newwin(height, width, y, x);
217 if (window->win == NULL)
218 g_error("newwin() failed: %d,%d %d,%d", x, y, width, height);
219 idlok(window->win, 1);
224 /* Destroy a terminal window */
225 void term_window_destroy(TERM_WINDOW *window)
231 /* Move/resize a window */
232 void term_window_move(TERM_WINDOW *window, int x, int y,
233 int width, int height)
235 /* some checks to make sure the window is visible in screen,
236 otherwise curses could get nasty and not show our window anymore. */
237 if (width < 1) width = 1;
238 if (height < 1) height = 1;
239 if (x+width > term_width) x = term_width-width;
240 if (y+height > term_height) y = term_height-height;
242 #ifdef HAVE_CURSES_WRESIZE
243 if (window->width != width || window->height != height)
244 wresize(window->win, height, width);
245 if (window->x != x || window->y != y)
246 mvwin(window->win, y, x);
248 if (window->width != width || window->height != height ||
249 window->x != x || window->y != y) {
251 window->win = newwin(height, width, y, x);
252 idlok(window->win, 1);
255 window->x = x; window->y = y;
256 window->width = width; window->height = height;
260 void term_window_clear(TERM_WINDOW *window)
265 /* Scroll window up/down */
266 void term_window_scroll(TERM_WINDOW *window, int count)
268 scrollok(window->win, TRUE);
269 wscrl(window->win, count);
270 scrollok(window->win, FALSE);
273 static int get_attr(int color)
277 if (!term_use_colors)
278 attr = (color & 0x70) ? A_REVERSE : 0;
279 else if (((color & 0x0f) == 8) && (color & ATTR_BOLD) == 0)
280 attr = (A_DIM | COLOR_PAIR(63));
281 else if ((color & 0x77) == 0)
284 if (color & ATTR_RESETFG) {
286 color |= settings_get_int("default_color");
288 attr = (COLOR_PAIR((color&7) + (color&0x70)/2));
291 if ((color & 0x08) || (color & ATTR_BOLD)) attr |= A_BOLD;
292 if (color & ATTR_BLINK) attr |= A_BLINK;
294 if (color & ATTR_UNDERLINE) attr |= A_UNDERLINE;
295 if (color & ATTR_REVERSE) attr |= A_REVERSE;
299 /* Change active color */
300 void term_set_color(TERM_WINDOW *window, int col)
302 wattrset(window->win, get_attr(col));
303 wbkgdset(window->win, ' ' | get_attr(col));
306 void term_move(TERM_WINDOW *window, int x, int y)
308 wmove(window->win, y, x);
311 void term_addch(TERM_WINDOW *window, int chr)
313 waddch(window->win, chr);
316 void term_add_unichar(TERM_WINDOW *window, unichar chr)
318 waddch(window->win, chr);
321 void term_addstr(TERM_WINDOW *window, const char *str)
323 waddstr(window->win, (const char *) str);
326 void term_clrtoeol(TERM_WINDOW *window)
328 wclrtoeol(window->win);
331 void term_move_cursor(int x, int y)
337 void term_refresh_freeze(void)
342 void term_refresh_thaw(void)
344 if (freeze_refresh > 0) {
346 if (freeze_refresh == 0) term_refresh(NULL);
350 void term_refresh(TERM_WINDOW *window)
353 wnoutrefresh(window->win);
355 if (freeze_refresh == 0) {
356 move(curs_y, curs_x);
357 wnoutrefresh(stdscr);
365 kill(getpid(), SIGSTOP);
370 void term_auto_detach(int set)
374 void term_set_input_type(int type)
378 int term_gets(unichar *buffer, int size)
382 for (count = 0; count < size; ) {
385 if (key == KEY_RESIZE)