1 /* $NetBSD: newwin.c,v 1.51 2017/09/18 10:18:13 roy 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.51 2017/09/18 10:18:13 roy 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 * 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 * 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 * 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)) == NULL) 89 return NULL; 90 91 overwrite(win, new_one); 92 return new_one; 93 } 94 95 /* 96 * newwin -- 97 * Allocate space for and set up defaults for a new window. 98 */ 99 WINDOW * 100 newwin(int nlines, int ncols, int by, int bx) 101 { 102 103 return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE); 104 } 105 106 /* 107 * newpad -- 108 * Allocate space for and set up defaults for a new pad. 109 */ 110 WINDOW * 111 newpad(int nlines, int ncols) 112 { 113 114 if (nlines < 1 || ncols < 1) 115 return NULL; 116 return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE); 117 } 118 119 WINDOW * 120 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad) 121 { 122 WINDOW *win; 123 __LINE *lp; 124 int i, j; 125 int maxy, maxx; 126 __LDATA *sp; 127 128 if (by < 0 || bx < 0) 129 return NULL; 130 131 maxy = nlines > 0 ? nlines : LINES - by + nlines; 132 maxx = ncols > 0 ? ncols : COLS - bx + ncols; 133 134 if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL) 135 return NULL; 136 137 win->bch = ' '; 138 if (__using_color) 139 win->battr = __default_color; 140 else 141 win->battr = 0; 142 win->nextp = win; 143 win->ch_off = 0; 144 win->orig = NULL; 145 win->reqy = nlines; 146 win->reqx = ncols; 147 148 #ifdef DEBUG 149 __CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off); 150 #endif 151 152 for (i = 0; i < maxy; i++) { 153 lp = win->alines[i]; 154 if (ispad) 155 lp->flags = __ISDIRTY; 156 else 157 lp->flags = 0; 158 for (sp = lp->line, j = 0; j < maxx; j++, sp++) { 159 sp->attr = 0; 160 #ifndef HAVE_WCHAR 161 sp->ch = win->bch; 162 #else 163 sp->ch = (wchar_t)btowc((int) win->bch); 164 sp->nsp = NULL; 165 SET_WCOL(*sp, 1); 166 #endif /* HAVE_WCHAR */ 167 } 168 lp->hash = __hash((char *)(void *)lp->line, 169 (size_t)(ncols * __LDATASIZE)); 170 } 171 return (win); 172 } 173 174 WINDOW * 175 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx) 176 { 177 178 return __subwin(orig, nlines, ncols, by, bx, FALSE); 179 } 180 181 static WINDOW * 182 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad) 183 { 184 int i; 185 __LINE *lp; 186 WINDOW *win; 187 int maxy, maxx; 188 189 #ifdef DEBUG 190 __CTRACE(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n", 191 orig, nlines, ncols, by, bx, ispad); 192 #endif 193 if (orig == NULL) 194 return NULL; 195 196 /* Make sure window fits inside the original one. */ 197 maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines; 198 maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols; 199 if (by < orig->begy || bx < orig->begx 200 || by + maxy > orig->maxy + orig->begy 201 || bx + maxx > orig->maxx + orig->begx) 202 return NULL; 203 if ((win = __makenew(_cursesi_screen, maxy, maxx, 204 by, bx, 1, ispad)) == NULL) 205 return NULL; 206 win->bch = orig->bch; 207 win->battr = orig->battr; 208 win->reqy = nlines; 209 win->reqx = ncols; 210 win->nextp = orig->nextp; 211 orig->nextp = win; 212 win->orig = orig; 213 214 /* Initialize flags here so that refresh can also use __set_subwin. */ 215 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) 216 lp->flags = 0; 217 __set_subwin(orig, win); 218 return win; 219 } 220 /* 221 * This code is shared with mvwin(). 222 */ 223 void 224 __set_subwin(WINDOW *orig, WINDOW *win) 225 { 226 int i; 227 __LINE *lp, *olp; 228 #ifdef HAVE_WCHAR 229 __LDATA *cp; 230 int j; 231 nschar_t *np; 232 #endif /* HAVE_WCHAR */ 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 #ifndef HAVE_WCHAR 246 lp->hash = __hash((char *)(void *)lp->line, 247 (size_t)(win->maxx * __LDATASIZE)); 248 #else 249 for (cp = lp->line, j = 0; j < win->maxx; j++, cp++) { 250 lp->hash = __hash_more( &cp->ch, 251 sizeof( wchar_t ), lp->hash ); 252 lp->hash = __hash_more( &cp->attr, 253 sizeof( wchar_t ), lp->hash ); 254 if ( cp->nsp ) { 255 np = cp->nsp; 256 while ( np ) { 257 lp->hash = __hash_more( &np->ch, 258 sizeof( wchar_t ), lp->hash ); 259 np = np->next; 260 } 261 } 262 } 263 #endif /* HAVE_WCHAR */ 264 } 265 266 #ifdef DEBUG 267 __CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n", 268 win->ch_off); 269 #endif 270 } 271 /* 272 * __makenew -- 273 * Set up a window buffer and returns a pointer to it. 274 */ 275 static WINDOW * 276 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub, 277 int ispad) 278 { 279 WINDOW *win; 280 __LINE *lp; 281 struct __winlist *wlp, *wlp2; 282 int i; 283 284 285 #ifdef DEBUG 286 __CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n", 287 nlines, ncols, by, bx); 288 #endif 289 if (nlines <= 0 || ncols <= 0) 290 return NULL; 291 292 if ((win = malloc(sizeof(WINDOW))) == NULL) 293 return NULL; 294 #ifdef DEBUG 295 __CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win); 296 #endif 297 win->fp = NULL; 298 299 /* Set up line pointer array and line space. */ 300 if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) { 301 free(win); 302 return NULL; 303 } 304 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) { 305 free(win->alines); 306 free(win); 307 return NULL; 308 } 309 /* Don't allocate window and line space if it's a subwindow */ 310 if (sub) 311 win->wspace = NULL; 312 else { 313 /* 314 * Allocate window space in one chunk. 315 */ 316 if ((win->wspace = 317 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) { 318 free(win->lspace); 319 free(win->alines); 320 free(win); 321 return NULL; 322 } 323 /* 324 * Append window to window list. 325 */ 326 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) { 327 free(win->wspace); 328 free(win->lspace); 329 free(win->alines); 330 free(win); 331 return NULL; 332 } 333 wlp->winp = win; 334 wlp->nextp = NULL; 335 if (screen->winlistp == NULL) 336 screen->winlistp = wlp; 337 else { 338 wlp2 = screen->winlistp; 339 while (wlp2->nextp != NULL) 340 wlp2 = wlp2->nextp; 341 wlp2->nextp = wlp; 342 } 343 /* 344 * Point line pointers to line space, and lines themselves into 345 * window space. 346 */ 347 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) { 348 win->alines[i] = lp; 349 lp->line = &win->wspace[i * ncols]; 350 #ifdef DEBUG 351 lp->sentinel = SENTINEL_VALUE; 352 #endif 353 lp->firstchp = &lp->firstch; 354 lp->lastchp = &lp->lastch; 355 if (ispad) { 356 lp->firstch = 0; 357 lp->lastch = ncols; 358 } else { 359 lp->firstch = ncols; 360 lp->lastch = 0; 361 } 362 } 363 } 364 #ifdef DEBUG 365 __CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols); 366 #endif 367 win->screen = screen; 368 win->cury = win->curx = 0; 369 win->maxy = nlines; 370 win->maxx = ncols; 371 win->reqy = nlines; 372 win->reqx = ncols; 373 374 win->begy = by; 375 win->begx = bx; 376 win->flags = (__IDLINE | __IDCHAR); 377 win->delay = -1; 378 win->wattr = 0; 379 #ifdef HAVE_WCHAR 380 win->bnsp = NULL; 381 SET_BGWCOL(*win, 1); 382 #endif /* HAVE_WCHAR */ 383 win->scr_t = 0; 384 win->scr_b = win->maxy - 1; 385 if (ispad) { 386 win->flags |= __ISPAD; 387 win->pbegy = 0; 388 win->pbegx = 0; 389 win->sbegy = 0; 390 win->sbegx = 0; 391 win->smaxy = 0; 392 win->smaxx = 0; 393 } else 394 __swflags(win); 395 #ifdef DEBUG 396 __CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr); 397 __CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags); 398 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy); 399 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx); 400 __CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy); 401 __CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx); 402 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t); 403 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b); 404 #endif 405 return win; 406 } 407 408 void 409 __swflags(WINDOW *win) 410 { 411 412 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK); 413 if (win->begx + win->maxx == COLS && !(win->flags & __ISPAD)) { 414 win->flags |= __ENDLINE; 415 if (win->begx == 0 && win->maxy == LINES && win->begy == 0) 416 win->flags |= __FULLWIN; 417 if (win->begy + win->maxy == LINES) 418 win->flags |= __SCROLLWIN; 419 } 420 } 421 422 /* 423 * is_pad -- 424 * Return true if window was created by newpad. 425 */ 426 bool 427 is_pad(const WINDOW *win) 428 { 429 430 return win->flags & __ISPAD ? true : false; 431 } 432