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