screen.c revision 1.14 1 /* $NetBSD: screen.c,v 1.14 2003/07/30 11:19:01 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)screen.c 8.2 (blymn) 11/27/2001";
40 #else
41 __RCSID("$NetBSD: screen.c,v 1.14 2003/07/30 11:19:01 dsl Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <stdlib.h>
46
47 #include "curses.h"
48 #include "curses_private.h"
49
50 /*
51 * set_term --
52 * Change the term to the given screen.
53 *
54 */
55 SCREEN *
56 set_term(SCREEN *new)
57 {
58 SCREEN *old_screen = _cursesi_screen;
59
60 if (_cursesi_screen != NULL) {
61 /* save changes made to the current screen... */
62 old_screen->echoit = __echoit;
63 old_screen->pfast = __pfast;
64 old_screen->rawmode = __rawmode;
65 old_screen->noqch = __noqch;
66 old_screen->COLS = COLS;
67 old_screen->LINES = LINES;
68 old_screen->COLORS = COLORS;
69 old_screen->COLOR_PAIRS = COLOR_PAIRS;
70 old_screen->GT = __GT;
71 old_screen->NONL = __NONL;
72 old_screen->UPPERCASE = __UPPERCASE;
73 }
74
75 _cursesi_screen = new;
76
77 __echoit = new->echoit;
78 __pfast = new->pfast;
79 __rawmode = new->rawmode;
80 __noqch = new->noqch;
81 COLS = new->COLS;
82 LINES = new->LINES;
83 COLORS = new->COLORS;
84 COLOR_PAIRS = new->COLOR_PAIRS;
85 __GT = new->GT;
86 __NONL = new->NONL;
87 __UPPERCASE = new->UPPERCASE;
88
89 _cursesi_resetterm(new);
90
91 curscr = new->curscr;
92 clearok(curscr, new->clearok);
93 stdscr = new->stdscr;
94 __virtscr = new->__virtscr;
95
96 _cursesi_reset_acs(new);
97
98 #ifdef DEBUG
99 __CTRACE("initscr: LINES = %d, COLS = %d\n", LINES, COLS);
100 #endif
101
102 return old_screen;
103 }
104
105 /*
106 * newterm --
107 * Set up a new screen.
108 *
109 */
110 SCREEN *
111 newterm(char *type, FILE *outfd, FILE *infd)
112 {
113 SCREEN *new_screen;
114 char *sp;
115
116 sp = type;
117 if ((type == NULL) && (sp = getenv("TERM")) == NULL)
118 return NULL;
119
120 if ((new_screen = (SCREEN *) malloc(sizeof(SCREEN))) == NULL)
121 return NULL;
122
123 new_screen->infd = infd;
124 new_screen->outfd = outfd;
125 new_screen->echoit = new_screen->nl = 1;
126 new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
127 new_screen->nca = A_NORMAL;
128 new_screen->color_type = COLOR_NONE;
129 new_screen->COLOR_PAIRS = 0;
130 new_screen->old_mode = 2;
131 new_screen->stdbuf = NULL;
132 new_screen->stdscr = NULL;
133 new_screen->curscr = NULL;
134 new_screen->__virtscr = NULL;
135 new_screen->curwin = 0;
136 new_screen->notty = FALSE;
137 new_screen->half_delay = FALSE;
138
139 if (_cursesi_gettmode(new_screen) == ERR)
140 goto error_exit;
141
142 if (_cursesi_setterm((char *)sp, new_screen) == ERR)
143 goto error_exit;
144
145 /* Need either homing or cursor motion for refreshes */
146 if (!new_screen->tc_ho && !new_screen->tc_cm)
147 goto error_exit;
148
149 new_screen->winlistp = NULL;
150
151 if ((new_screen->curscr = __newwin(new_screen, 0,
152 0, 0, 0, FALSE)) == ERR)
153 goto error_exit;
154
155 if ((new_screen->stdscr = __newwin(new_screen, 0,
156 0, 0, 0, FALSE)) == ERR) {
157 delwin(new_screen->curscr);
158 goto error_exit;
159 }
160
161 clearok(new_screen->stdscr, 1);
162
163 if ((new_screen->__virtscr = __newwin(new_screen, 0,
164 0, 0, 0, FALSE)) == ERR) {
165 delwin(new_screen->curscr);
166 delwin(new_screen->stdscr);
167 goto error_exit;
168 }
169
170 __init_getch(new_screen);
171
172 __init_acs(new_screen);
173
174 __set_stophandler();
175
176 /*
177 * bleh - it seems that apps expect the first newterm to set
178 * up the curses screen.... emulate this.
179 */
180 if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
181 set_term(new_screen);
182 }
183
184 #ifdef DEBUG
185 __CTRACE("newterm: LINES = %d, COLS = %d\n", LINES, COLS);
186 #endif
187 __startwin(new_screen);
188
189 return new_screen;
190
191 error_exit:
192 free(new_screen);
193 return NULL;
194 }
195
196 /*
197 * delscreen --
198 * Free resources used by the given screen and destroy it.
199 *
200 */
201 void
202 delscreen(SCREEN *screen)
203 {
204 struct __winlist *list;
205
206 /* free up the termcap entry stuff */
207 t_freent(screen->cursesi_genbuf);
208
209 /* walk the window list and kill all the parent windows */
210 while ((list = screen->winlistp)) {
211 delwin(list->winp);
212 if (list == screen->winlistp)
213 /* sanity - abort if window didn't remove itself */
214 break;
215 }
216
217 /* free the storage of the keymaps */
218 _cursesi_free_keymap(screen->base_keymap);
219
220 free(screen->stdbuf);
221 free(screen);
222 }
223