xref: /netbsd-src/lib/libcurses/resize.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: resize.c,v 1.30 2018/11/02 04:17:39 blymn Exp $	*/
2 
3 /*
4  * Copyright (c) 2001
5  *	Brett Lymn.
6  *
7  * This code has been donated to The NetBSD Foundation by the Author.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)resize.c   blymn 2001/08/26";
35 #else
36 __RCSID("$NetBSD: resize.c,v 1.30 2018/11/02 04:17:39 blymn Exp $");
37 #endif
38 #endif				/* not lint */
39 
40 #include <stdlib.h>
41 
42 #include "curses.h"
43 #include "curses_private.h"
44 
45 static int __resizeterm(WINDOW *win, int nlines, int ncols);
46 static int __resizewin(WINDOW *win, int nlines, int ncols);
47 
48 /*
49  * wresize --
50  *	Resize the given window to the new size.
51  */
52 int
53 wresize(WINDOW *win, int req_nlines, int req_ncols)
54 {
55 	int	nlines = req_nlines;
56 	int	ncols = req_ncols;
57 
58 	if (win == NULL)
59 		return ERR;
60 
61 #ifdef	DEBUG
62 	__CTRACE(__CTRACE_WINDOW, "wresize: (%p, %d, %d)\n",
63 	    win, nlines, ncols);
64 #endif
65 	if (win->orig == NULL) {
66 		/* bound "our" windows by the screen size */
67 		if (win == curscr || win == __virtscr || win == stdscr) {
68 			if (nlines > LINES)
69 				nlines = LINES;
70 			if (nlines < 1)
71 				nlines = 1;
72 			if (ncols > COLS)
73 				ncols = COLS;
74 			if (ncols < 1)
75 				ncols = 1;
76 		} else {
77 			if (win->begy > LINES)
78 				win->begy = 0;
79 			if (win->begy + nlines > LINES)
80 				nlines = 0;
81 			if (nlines <= 0)
82 				nlines += LINES - win->begy;
83 			if (nlines < 1)
84 				nlines = 1;
85 			if (win->begx > COLS)
86 				win->begx = 0;
87 			if (win->begx + ncols > COLS)
88 				ncols = 0;
89 			if (ncols <= 0)
90 				ncols += COLS - win->begx;
91 			if (ncols < 1)
92 				ncols = 1;
93 		}
94 	} else {
95 		/* subwins must fit inside the parent - check this */
96 		if (win->begy > win->orig->begy + win->orig->maxy)
97 			win->begy = win->orig->begy + win->orig->maxy - 1;
98 		if (win->begy + nlines > win->orig->begy + win->orig->maxy)
99 			nlines = 0;
100 		if (nlines <= 0)
101 			nlines += win->orig->begy + win->orig->maxy - win->begy;
102 		if (nlines < 1)
103 			nlines = 1;
104 		if (win->begx > win->orig->begx + win->orig->maxx)
105 			win->begx = win->orig->begx + win->orig->maxx - 1;
106 		if (win->begx + ncols > win->orig->begx + win->orig->maxx)
107 			ncols = 0;
108 		if (ncols <= 0)
109 			ncols += win->orig->begx + win->orig->maxx - win->begx;
110 		if (ncols < 1)
111 			ncols = 1;
112 	}
113 
114 	if ((__resizewin(win, nlines, ncols)) == ERR)
115 		return ERR;
116 
117 	win->reqy = req_nlines;
118 	win->reqx = req_ncols;
119 
120 	/* If someone resizes curscr, we must also resize __virtscr */
121 	if (win == curscr) {
122 		if ((__resizewin(__virtscr, nlines, ncols)) == ERR)
123 			return ERR;
124 		__virtscr->reqy = req_nlines;
125 		__virtscr->reqx = req_ncols;
126 	}
127 
128 	return OK;
129 }
130 
131 /*
132  * is_term_resized --
133  *	Return true if the given dimensions do not match the
134  *	internal structures.
135  */
136 bool
137 is_term_resized(int nlines, int ncols)
138 {
139 
140 	return (nlines > 0 && ncols > 0 &&
141 	    (nlines != _cursesi_screen->LINES ||
142 	    ncols != _cursesi_screen->COLS));
143 }
144 
145 /*
146  * resizeterm --
147  *	Resize the terminal window, resizing the dependent windows.
148  *	Handles internal book-keeping.
149  */
150 int
151 resizeterm(int nlines, int ncols)
152 {
153 	int result;
154 
155 #ifdef	DEBUG
156 	__CTRACE(__CTRACE_WINDOW, "resizeterm: (%d, %d)\n", nlines, ncols);
157 #endif
158 
159 	/* Unconditionally inform application screen has been resized. */
160 	_cursesi_screen->resized = 1;
161 
162 	if (!is_term_resized(nlines, ncols))
163 		return OK;
164 
165 	result = resize_term(nlines, ncols);
166 
167 	/* Screen contents are unknown, libcurses is not libpanel, we don't
168 	 * know the correct draw order. */
169 	clearok(curscr, TRUE);
170 
171 	if (result == OK) {
172 		/* Redraw the soft label keys to the new size. */
173 		__slk_resize(_cursesi_screen, ncols);
174 		__slk_noutrefresh(_cursesi_screen);
175 	}
176 
177 	return result;
178 }
179 
180 /*
181  * resize_term --
182  *	Resize the terminal window, resizing the dependent windows.
183  */
184 int
185 resize_term(int nlines, int ncols)
186 {
187 	WINDOW *win;
188 	struct __winlist *list;
189 	int rlines;
190 
191 #ifdef	DEBUG
192 	__CTRACE(__CTRACE_WINDOW, "resize_term: (%d, %d)\n", nlines, ncols);
193 #endif
194 
195 	if (!is_term_resized(nlines, ncols))
196 		return OK;
197 
198 	if (__resizeterm(curscr, nlines, ncols) == ERR)
199 		return ERR;
200 	if (__resizeterm(__virtscr, nlines, ncols) == ERR)
201 		return ERR;
202 	rlines = nlines - __rippedlines(_cursesi_screen, 0);
203 	if (__resizeterm(stdscr, rlines, ncols) == ERR)
204 		return ERR;
205 
206 	_cursesi_screen->LINES = nlines;
207 	_cursesi_screen->COLS = ncols;
208 	LINES = nlines;
209 	COLS = ncols;
210 
211 	/* tweak the flags now that we have updated the LINES and COLS */
212 	for (list = _cursesi_screen->winlistp; list != NULL; list = list->nextp) {
213 		win = list->winp;
214 
215 		if (!(win->flags & __ISPAD))
216 			__swflags(win);
217 	}
218 
219 	/* Resize and re-position the ripped off windows. */
220 	if (__ripoffresize(_cursesi_screen) == ERR)
221 		return ERR;
222 
223 	return OK;
224 }
225 
226 /*
227  * __resizeterm
228  *	Setup window for resizing.
229  */
230 static int
231 __resizeterm(WINDOW *win, int nlines, int ncols)
232 {
233 	int newlines, newcols;
234 
235 	newlines = win->reqy;
236 	if (win->begy + newlines >= nlines)
237 		newlines = 0;
238 	if (newlines == 0)
239 		newlines = nlines - win->begy;
240 
241 	newcols = win->reqx;
242 	if (win->begx + newcols >= ncols)
243 		newcols = 0;
244 	if (newcols == 0)
245 		newcols = ncols - win->begx;
246 
247 	return __resizewin(win, newlines, newcols);
248 }
249 
250 /*
251  * __resizewin --
252  *	Resize the given window.
253  */
254 static int
255 __resizewin(WINDOW *win, int nlines, int ncols)
256 {
257 	__LINE			*lp, *olp, **newlines, *newlspace;
258 	__LDATA			*sp;
259 	__LDATA			*newwspace;
260 	int			 i, j;
261 	int			 y, x;
262 	WINDOW			*swin;
263 
264 #ifdef	DEBUG
265 	__CTRACE(__CTRACE_WINDOW, "resize: (%p, %d, %d)\n", win, nlines, ncols);
266 	__CTRACE(__CTRACE_WINDOW, "resize: win->wattr = %08x\n", win->wattr);
267 	__CTRACE(__CTRACE_WINDOW, "resize: win->flags = %#.4x\n", win->flags);
268 	__CTRACE(__CTRACE_WINDOW, "resize: win->maxy = %d\n", win->maxy);
269 	__CTRACE(__CTRACE_WINDOW, "resize: win->maxx = %d\n", win->maxx);
270 	__CTRACE(__CTRACE_WINDOW, "resize: win->begy = %d\n", win->begy);
271 	__CTRACE(__CTRACE_WINDOW, "resize: win->begx = %d\n", win->begx);
272 	__CTRACE(__CTRACE_WINDOW, "resize: win->scr_t = %d\n", win->scr_t);
273 	__CTRACE(__CTRACE_WINDOW, "resize: win->scr_b = %d\n", win->scr_b);
274 #endif
275 
276 	/*
277 	 * free up any non-spacing storage before we lose the
278 	 * pointers...
279 	 */
280 #ifdef HAVE_WCHAR
281 	__cursesi_win_free_nsp(win);
282 #endif
283 
284 	if (nlines <= 0 || ncols <= 0)
285 		nlines = ncols = 0;
286 	else {
287 		/* Reallocate line pointer array and line space. */
288 		newlines = realloc(win->alines, nlines * sizeof(__LINE *));
289 		if (newlines == NULL)
290 			return ERR;
291 		win->alines = newlines;
292 
293 		newlspace = realloc(win->lspace, nlines * sizeof(__LINE));
294 		if (newlspace == NULL)
295 			return ERR;
296 		win->lspace = newlspace;
297 	}
298 
299 	/* Don't allocate window and line space if it's a subwindow */
300 	if (win->orig == NULL) {
301 		/*
302 		 * Allocate window space in one chunk.
303 		 */
304 		if (ncols != 0) {
305 			newwspace = realloc(win->wspace,
306 					    ncols * nlines * sizeof(__LDATA));
307 			if (newwspace == NULL)
308 				return ERR;
309 			win->wspace = newwspace;
310 		}
311 
312 		/*
313 		 * Point line pointers to line space, and lines themselves into
314 		 * window space.
315 		 */
316 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
317 			win->alines[i] = lp;
318 			lp->line = &win->wspace[i * ncols];
319 #ifdef DEBUG
320 			lp->sentinel = SENTINEL_VALUE;
321 #endif
322 			lp->firstchp = &lp->firstch;
323 			lp->lastchp = &lp->lastch;
324 			lp->firstch = 0;
325 			lp->lastch = ncols - 1;
326 			lp->flags = __ISDIRTY;
327 		}
328 	} else {
329 
330 		win->ch_off = win->begx - win->orig->begx;
331 		  /* Point line pointers to line space. */
332 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
333 			win->alines[i] = lp;
334 			olp = win->orig->alines[i + win->begy - win->orig->begy];
335 			lp->line = &olp->line[win->ch_off];
336 #ifdef DEBUG
337 			lp->sentinel = SENTINEL_VALUE;
338 #endif
339 			lp->firstchp = &olp->firstch;
340 			lp->lastchp = &olp->lastch;
341 			lp->flags = __ISDIRTY;
342 		}
343 	}
344 
345 	win->cury = win->curx = 0;
346 	win->maxy = nlines;
347 	win->maxx = ncols;
348 	win->scr_b = win->maxy - 1;
349 	__swflags(win);
350 
351 	  /*
352 	   * we must zot the window contents otherwise lines may pick
353 	   * up attributes from the previous line when the window is
354 	   * made smaller.  The client will redraw the window anyway
355 	   * so this is no big deal.
356 	   */
357 	for (i = 0; i < win->maxy; i++) {
358 		lp = win->alines[i];
359 		for (sp = lp->line, j = 0; j < win->maxx; j++, sp++) {
360 			sp->attr = 0;
361 #ifndef HAVE_WCHAR
362 			sp->ch = win->bch;
363 #else
364 			sp->ch = (wchar_t)btowc((int)win->bch);
365 			sp->nsp = NULL;
366 			if (_cursesi_copy_nsp(win->bnsp, sp) == ERR)
367 				return ERR;
368 			SET_WCOL(*sp, 1);
369 #endif /* HAVE_WCHAR */
370 		}
371 		lp->hash = __hash((char *)(void *)lp->line,
372 				  (size_t)(ncols * __LDATASIZE));
373 	}
374 
375 #ifdef DEBUG
376 	__CTRACE(__CTRACE_WINDOW, "resize: win->wattr = %08x\n", win->wattr);
377 	__CTRACE(__CTRACE_WINDOW, "resize: win->flags = %#.4x\n", win->flags);
378 	__CTRACE(__CTRACE_WINDOW, "resize: win->maxy = %d\n", win->maxy);
379 	__CTRACE(__CTRACE_WINDOW, "resize: win->maxx = %d\n", win->maxx);
380 	__CTRACE(__CTRACE_WINDOW, "resize: win->begy = %d\n", win->begy);
381 	__CTRACE(__CTRACE_WINDOW, "resize: win->begx = %d\n", win->begx);
382 	__CTRACE(__CTRACE_WINDOW, "resize: win->scr_t = %d\n", win->scr_t);
383 	__CTRACE(__CTRACE_WINDOW, "resize: win->scr_b = %d\n", win->scr_b);
384 #endif
385 
386 	if (win->orig == NULL) {
387 		/* bound subwindows to new size and fixup their pointers */
388 		for (swin = win->nextp; swin != win; swin = swin->nextp) {
389 			y = swin->reqy;
390 			if (swin->begy > win->begy + win->maxy)
391 				swin->begy = win->begy + win->maxy - 1;
392 			if (swin->begy + y > win->begy + win->maxy)
393 				y = 0;
394 			if (y <= 0)
395 				y += win->begy + win->maxy - swin->begy;
396 			if (y < 1)
397 				y = 1;
398 			x = swin->reqx;
399 			if (swin->begx > win->begx + win->maxx)
400 				swin->begx = win->begx + win->maxx - 1;
401 			if (swin->begx + x > win->begx + win->maxx)
402 				x = 0;
403 			if (x <= 0)
404 				x += win->begy + win->maxx - swin->begx;
405 			if (x < 1)
406 				x = 1;
407 			__resizewin(swin, y, x);
408 		}
409 	}
410 
411 	return OK;
412 }
413