xref: /netbsd-src/lib/libcurses/screen.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: screen.c,v 1.1 2001/12/02 09:14:22 blymn 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.1 2001/12/02 09:14:22 blymn 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;
59 
60 	if (_cursesi_screen != NULL) {
61 		old_screen = _cursesi_screen;
62 
63 		  /* save changes made to the current screen... */
64 		old_screen->echoit = __echoit;
65 		old_screen->pfast = __pfast;
66 		old_screen->rawmode = __rawmode;
67 		old_screen->noqch = __noqch;
68 		old_screen->nca = __nca;
69 		old_screen->winlistp = __winlistp;
70 		old_screen->COLS = COLS;
71 		old_screen->LINES = LINES;
72 		old_screen->COLORS = COLORS;
73 		old_screen->COLOR_PAIRS = COLOR_PAIRS;
74 		old_screen->GT = __GT;
75 		old_screen->NONL = __NONL;
76 		old_screen->UPPERCASE = __UPPERCASE;
77 	}
78 
79 	_cursesi_screen = new;
80 
81 	__echoit = new->echoit;
82         __pfast = new->pfast;
83 	__rawmode = new->rawmode;
84 	__noqch = new->noqch;
85 	__nca = new->nca;
86 	COLS = new->COLS;
87 	LINES = new->LINES;
88 	COLORS = new->COLORS;
89 	COLOR_PAIRS = new->COLOR_PAIRS;
90 	__GT = new->GT;
91 	__NONL = new->NONL;
92 	__UPPERCASE = new->UPPERCASE;
93 
94 	_cursesi_resetterm(new);
95 
96 	__winlistp = new->winlistp;
97 
98 	curscr = new->curscr;
99 	clearok(curscr, new->clearok);
100 	stdscr = new->stdscr;
101 	__virtscr = new->__virtscr;
102 
103 	_cursesi_reset_acs(new);
104 
105 #ifdef DEBUG
106 	__CTRACE("initscr: LINES = %d, COLS = %d\n", LINES, COLS);
107 #endif
108 
109 	return old_screen;
110 }
111 
112 /*
113  * newterm --
114  *      Set up a new screen.
115  *
116  */
117 SCREEN *
118 newterm(char *type, FILE *outfd, FILE *infd)
119 {
120 	SCREEN *new_screen;
121 	char *sp;
122 
123 	sp = type;
124 	if ((type == NULL) && (sp = getenv("TERM")) == NULL)
125 		return NULL;
126 
127 	if ((new_screen = (SCREEN *) malloc(sizeof(SCREEN))) == NULL)
128 		return NULL;
129 
130 	new_screen->infd = infd;
131 	new_screen->outfd = outfd;
132 	new_screen->echoit = 1;
133         new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
134 	new_screen->nca = A_NORMAL;
135 	new_screen->color_type = COLOR_NONE;
136 	new_screen->old_mode = 2;
137 	new_screen->stdbuf = NULL;
138 	new_screen->stdscr = NULL;
139 	new_screen->curscr = NULL;
140 	new_screen->__virtscr = NULL;
141 	new_screen->curwin = 0;
142 
143 	if (_cursesi_gettmode(new_screen) == ERR)
144 		goto error_exit;
145 
146 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
147 		goto error_exit;
148 
149 	/* Need either homing or cursor motion for refreshes */
150 	if (!new_screen->tc_ho && !new_screen->tc_cm)
151 		goto error_exit;
152 
153 	new_screen->winlistp = NULL;
154 
155 	if ((new_screen->curscr = __newwin(new_screen, new_screen->LINES,
156 					   new_screen->COLS, 0, 0)) == ERR)
157 		goto error_exit;
158 
159 	clearok(new_screen->curscr, 1);
160 
161 	if ((new_screen->stdscr = __newwin(new_screen, new_screen->LINES,
162 					   new_screen->COLS, 0, 0)) == ERR) {
163 		delwin(new_screen->curscr);
164 		goto error_exit;
165 	}
166 
167 	if ((new_screen->__virtscr = __newwin(new_screen, new_screen->LINES,
168 					    new_screen->COLS, 0, 0)) == ERR) {
169 		delwin(new_screen->curscr);
170 		delwin(new_screen->stdscr);
171 		goto error_exit;
172 	}
173 
174 	__init_getch(new_screen);
175 
176 	__init_acs(new_screen);
177 
178 	__set_stophandler();
179 
180 	  /*
181 	   * bleh - it seems that apps expect the first newterm to set
182 	   * up the curses screen.... emulate this.
183 	   */
184 	if (_cursesi_screen == NULL) {
185 		__echoit = new_screen->echoit;
186 		__pfast = new_screen->pfast;
187 		__rawmode = new_screen->rawmode;
188 		__noqch = new_screen->noqch;
189 		__nca = new_screen->nca;
190 		COLS = new_screen->COLS;
191 		LINES = new_screen->LINES;
192 		COLORS = new_screen->COLORS;
193 		COLOR_PAIRS = new_screen->COLOR_PAIRS;
194 		__GT = new_screen->GT;
195 		__NONL = new_screen->NONL;
196 		__UPPERCASE = new_screen->UPPERCASE;
197 		set_term(new_screen);
198 	}
199 
200 #ifdef DEBUG
201 	__CTRACE("newterm: LINES = %d, COLS = %d\n", LINES, COLS);
202 #endif
203 	__startwin(new_screen);
204 
205 	return new_screen;
206 
207   error_exit:
208 	free(new_screen);
209 	return NULL;
210 }
211 
212 /*
213  * delscreen --
214  *   Free resources used by the given screen and destroy it.
215  *
216  */
217 void
218 delscreen(SCREEN *screen)
219 {
220         struct __winlist *list, *np;
221 
222 	  /* free up the termcap entry stuff */
223 	t_freent(screen->cursesi_genbuf);
224 
225 	  /* walk the window list and kill all the parent windows */
226 	for (list = screen->winlistp; list != NULL; list = list->nextp) {
227 		if (list->winp->orig == NULL)
228 			delwin(list->winp);
229 	}
230 
231 	  /* free the windows list structures */
232 	for (list = screen->winlistp; list;) {
233 		np = list->nextp;
234 		free(list);
235 		list = np;
236 	}
237 
238 	  /* free the storage of the keymaps */
239 	_cursesi_free_keymap(screen->base_keymap);
240 
241 	free(screen->stdbuf);
242 	free(screen);
243 }
244