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