xref: /netbsd/lib/libcurses/newwin.c (revision b496d403)
1 /*	$NetBSD: newwin.c,v 1.67 2022/05/03 07:25:34 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)newwin.c	8.3 (Berkeley) 7/27/94";
36 #else
37 __RCSID("$NetBSD: newwin.c,v 1.67 2022/05/03 07:25:34 blymn Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include <stdlib.h>
42 
43 #include "curses.h"
44 #include "curses_private.h"
45 
46 
47 static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
48 			 int bx, int sub, int ispad);
49 static WINDOW *__subwin(WINDOW *orig, int nlines, int ncols, int by, int bx,
50 			 int ispad);
51 
52 /*
53  * derwin --
54  *      Create a new window in the same manner as subwin but (by, bx)
55  *      are relative to the origin of window orig instead of absolute.
56  */
57 WINDOW *
derwin(WINDOW * orig,int nlines,int ncols,int by,int bx)58 derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
59 {
60 
61 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
62 	    FALSE);
63 }
64 
65 /*
66  * subpad --
67  *      Create a new pad in the same manner as subwin but (by, bx)
68  *      are relative to the origin of window orig instead of absolute.
69  */
70 WINDOW *
subpad(WINDOW * orig,int nlines,int ncols,int by,int bx)71 subpad(WINDOW *orig, int nlines, int ncols, int by, int bx)
72 {
73 
74 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
75 	    TRUE);
76 }
77 
78 /*
79  * dupwin --
80  *      Create a copy of the given window.
81  */
82 WINDOW *
dupwin(WINDOW * win)83 dupwin(WINDOW *win)
84 {
85 	WINDOW *new_one;
86 
87 	if ((new_one = __newwin(_cursesi_screen, win->maxy, win->maxx,
88 				win->begy, win->begx, FALSE,
89 				win == stdscr ? TRUE : FALSE)) == NULL)
90 		return NULL;
91 
92 	overwrite(win, new_one);
93 	return new_one;
94 }
95 
96 /*
97  * newwin --
98  *	Allocate space for and set up defaults for a new window.
99  */
100 WINDOW *
newwin(int nlines,int ncols,int by,int bx)101 newwin(int nlines, int ncols, int by, int bx)
102 {
103 
104 	return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE, FALSE);
105 }
106 
107 /*
108  * newpad --
109  *	Allocate space for and set up defaults for a new pad.
110  */
111 WINDOW *
newpad(int nlines,int ncols)112 newpad(int nlines, int ncols)
113 {
114 
115 	if (nlines < 1 || ncols < 1)
116 		return NULL;
117 	return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE, FALSE);
118 }
119 
120 WINDOW *
__newwin(SCREEN * screen,int nlines,int ncols,int by,int bx,int ispad,int isstdscr)121 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad,
122     int isstdscr)
123 {
124 	WINDOW *win;
125 	__LINE *lp;
126 	int     i, j;
127 	int	ry, maxy, maxx;
128 	__LDATA *sp;
129 
130 	if (by < 0 || bx < 0)
131 		return NULL;
132 
133 	if (isstdscr) {
134 		ry = __rippedlines(screen, -1);
135 		by += __rippedlines(screen, 1);
136 	} else
137 		ry = 0;
138 
139 	maxy = nlines > 0 ? nlines : LINES - by - ry + nlines;
140 	maxx = ncols > 0 ? ncols : COLS - bx + ncols;
141 
142 	if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL)
143 		return NULL;
144 
145 #ifdef HAVE_WCHAR
146 	win->bch = (wchar_t) btowc((int) ' ');
147 #else
148 	win->bch = ' ';
149 #endif
150 
151 	if (__using_color)
152 		win->battr |= __default_color;
153 	win->nextp = win;
154 	win->ch_off = 0;
155 	win->orig = NULL;
156 	win->reqy = nlines;
157 	win->reqx = ncols;
158 
159 	__CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off);
160 
161 	for (i = 0; i < maxy; i++) {
162 		lp = win->alines[i];
163 		if (ispad)
164 			lp->flags = __ISDIRTY;
165 		else
166 			lp->flags = 0;
167 		for (sp = lp->line, j = 0; j < maxx; j++, sp++) {
168 			sp->attr = 0;
169 			sp->cflags = CA_BACKGROUND;
170 			sp->ch = win->bch;
171 #ifdef HAVE_WCHAR
172 			sp->nsp = NULL;
173 			sp->wcols = 1;
174 #endif /* HAVE_WCHAR */
175 		}
176 		lp->hash = __hash_line(lp->line, maxx);
177 	}
178 	return (win);
179 }
180 
181 WINDOW *
subwin(WINDOW * orig,int nlines,int ncols,int by,int bx)182 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
183 {
184 
185 	return __subwin(orig, nlines, ncols, by, bx, FALSE);
186 }
187 
188 static WINDOW *
__subwin(WINDOW * orig,int nlines,int ncols,int by,int bx,int ispad)189 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad)
190 {
191 	int     i;
192 	__LINE *lp;
193 	WINDOW *win;
194 	int	maxy, maxx;
195 
196 	__CTRACE(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n",
197 	    orig, nlines, ncols, by, bx, ispad);
198 	if (orig == NULL)
199 		return NULL;
200 
201 	/* Make sure window fits inside the original one. */
202 	maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines;
203 	maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols;
204 	if (by < orig->begy || bx < orig->begx
205 	    || by + maxy > orig->maxy + orig->begy
206 	    || bx + maxx > orig->maxx + orig->begx)
207 		return NULL;
208 	if ((win = __makenew(_cursesi_screen, maxy, maxx,
209 			     by, bx, 1, ispad)) == NULL)
210 		return NULL;
211 	win->bch = orig->bch;
212 	win->battr = orig->battr;
213 	win->reqy = nlines;
214 	win->reqx = ncols;
215 	win->nextp = orig->nextp;
216 	orig->nextp = win;
217 	win->orig = orig;
218 
219 	/* Initialize flags here so that refresh can also use __set_subwin. */
220 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
221 		lp->flags = 0;
222 	__set_subwin(orig, win);
223 	return win;
224 }
225 /*
226  * This code is shared with mvwin().
227  */
228 void
__set_subwin(WINDOW * orig,WINDOW * win)229 __set_subwin(WINDOW *orig, WINDOW *win)
230 {
231 	int     i;
232 	__LINE *lp, *olp;
233 
234 	win->ch_off = win->begx - orig->begx;
235 	/* Point line pointers to line space. */
236 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
237 		win->alines[i] = lp;
238 		olp = orig->alines[i + win->begy - orig->begy];
239 #ifdef DEBUG
240 		lp->sentinel = SENTINEL_VALUE;
241 #endif
242 		lp->line = &olp->line[win->ch_off];
243 		lp->firstchp = &olp->firstch;
244 		lp->lastchp = &olp->lastch;
245 		lp->hash = __hash_line(lp->line, win->maxx);
246 	}
247 
248 	__CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n",
249 	    win->ch_off);
250 }
251 /*
252  * __makenew --
253  *	Set up a window buffer and returns a pointer to it.
254  */
255 static WINDOW *
__makenew(SCREEN * screen,int nlines,int ncols,int by,int bx,int sub,int ispad)256 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub,
257 	int ispad)
258 {
259 	WINDOW			*win;
260 	__LINE			*lp;
261 	struct __winlist	*wlp, *wlp2;
262 	int			 i;
263 
264 
265 	__CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n",
266 	    nlines, ncols, by, bx);
267 	if (nlines <= 0 || ncols <= 0)
268 		return NULL;
269 
270 	if ((win = malloc(sizeof(WINDOW))) == NULL)
271 		return NULL;
272 	__CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win);
273 	win->fp = NULL;
274 	win->buf = NULL;
275 	win->buflen = 0;
276 
277 	/* Set up line pointer array and line space. */
278 	if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) {
279 		free(win);
280 		return NULL;
281 	}
282 	if ((win->lspace = calloc(nlines, sizeof(__LINE))) == NULL) {
283 		free(win->alines);
284 		free(win);
285 		return NULL;
286 	}
287 	/* Don't allocate window and line space if it's a subwindow */
288 	if (sub)
289 		win->wspace = NULL;
290 	else {
291 		/*
292 		 * Allocate window space in one chunk.
293 		 */
294 		if ((win->wspace =
295 			calloc(ncols * nlines, sizeof(__LDATA))) == NULL) {
296 			free(win->lspace);
297 			free(win->alines);
298 			free(win);
299 			return NULL;
300 		}
301 		/*
302 		 * Append window to window list.
303 		 */
304 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
305 			free(win->wspace);
306 			free(win->lspace);
307 			free(win->alines);
308 			free(win);
309 			return NULL;
310 		}
311 		wlp->winp = win;
312 		wlp->nextp = NULL;
313 		if (screen->winlistp == NULL)
314 			screen->winlistp = wlp;
315 		else {
316 			wlp2 = screen->winlistp;
317 			while (wlp2->nextp != NULL)
318 				wlp2 = wlp2->nextp;
319 			wlp2->nextp = wlp;
320 		}
321 		/*
322 		 * Point line pointers to line space, and lines themselves into
323 		 * window space.
324 		 */
325 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
326 			win->alines[i] = lp;
327 			lp->line = &win->wspace[i * ncols];
328 #ifdef DEBUG
329 			lp->sentinel = SENTINEL_VALUE;
330 #endif
331 			lp->firstchp = &lp->firstch;
332 			lp->lastchp = &lp->lastch;
333 			if (ispad) {
334 				lp->firstch = 0;
335 				lp->lastch = ncols;
336 			} else {
337 				lp->firstch = ncols;
338 				lp->lastch = 0;
339 			}
340 		}
341 	}
342 	__CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols);
343 	win->screen = screen;
344 	win->cury = win->curx = 0;
345 	win->maxy = nlines;
346 	win->maxx = ncols;
347 	win->reqy = nlines;
348 	win->reqx = ncols;
349 
350 	win->begy = by;
351 	win->begx = bx;
352 	win->flags = (__IDLINE | __IDCHAR);
353 	win->delay = -1;
354 	win->wattr = 0;
355 	win->battr = 0;
356 #ifdef HAVE_WCHAR
357 	win->bnsp = NULL;
358 	win->wcols = 1;
359 #endif /* HAVE_WCHAR */
360 	win->scr_t = 0;
361 	win->scr_b = win->maxy - 1;
362 	if (ispad) {
363 		win->flags |= __ISPAD;
364 		win->pbegy = 0;
365 		win->pbegx = 0;
366 		win->sbegy = 0;
367 		win->sbegx = 0;
368 		win->smaxy = 0;
369 		win->smaxx = 0;
370 	} else
371 		__swflags(win);
372 	__CTRACE(__CTRACE_WINDOW, "makenew: sub = %d\n", sub);
373 	__CTRACE(__CTRACE_WINDOW, "makenew: ispad = %d\n", ispad);
374 	__CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr);
375 	__CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags);
376 	__CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy);
377 	__CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx);
378 	__CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy);
379 	__CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx);
380 	__CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t);
381 	__CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b);
382 	return win;
383 }
384 
385 void
__swflags(WINDOW * win)386 __swflags(WINDOW *win)
387 {
388 	TERMINAL *term = win->screen->term;
389 
390 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
391 	if (win->begx + win->maxx == win->screen->COLS &&
392 	    !(win->flags & __ISPAD))
393 	{
394 		win->flags |= __ENDLINE;
395 		if (win->begx == 0 &&
396 		    win->maxy == win->screen->LINES &&
397 		    win->begy == 0)
398 			win->flags |= __FULLWIN;
399 		if (win->begy + win->maxy == win->screen->LINES &&
400 		    t_auto_right_margin(term) &&
401 		    !(t_insert_character(term) != NULL ||
402 		    t_parm_ich(term) != NULL ||
403 		    (t_enter_insert_mode(term) != NULL &&
404 		     t_exit_insert_mode(term) != NULL)))
405 			win->flags |= __SCROLLWIN;
406 	}
407 }
408 
409 /*
410  * is_pad --
411  *	Return true if window was created by newpad.
412  */
413 bool
is_pad(const WINDOW * win)414 is_pad(const WINDOW *win)
415 {
416 
417 	return win->flags & __ISPAD ? true : false;
418 }
419