1 /* $NetBSD: newwin.c,v 1.60 2021/06/01 00:59:01 mcf 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.60 2021/06/01 00:59:01 mcf 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, 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 * 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 * 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 * 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 win->bch = ' '; 146 if (__using_color) 147 win->battr |= __default_color; 148 win->nextp = win; 149 win->ch_off = 0; 150 win->orig = NULL; 151 win->reqy = nlines; 152 win->reqx = ncols; 153 154 #ifdef DEBUG 155 __CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off); 156 #endif 157 158 for (i = 0; i < maxy; i++) { 159 lp = win->alines[i]; 160 if (ispad) 161 lp->flags = __ISDIRTY; 162 else 163 lp->flags = 0; 164 for (sp = lp->line, j = 0; j < maxx; j++, sp++) { 165 sp->attr = 0; 166 #ifndef HAVE_WCHAR 167 sp->ch = win->bch; 168 #else 169 sp->ch = (wchar_t)btowc((int) win->bch); 170 sp->nsp = NULL; 171 SET_WCOL(*sp, 1); 172 #endif /* HAVE_WCHAR */ 173 } 174 lp->hash = __hash((char *)(void *)lp->line, 175 (size_t)(maxx * __LDATASIZE)); 176 } 177 return (win); 178 } 179 180 WINDOW * 181 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx) 182 { 183 184 return __subwin(orig, nlines, ncols, by, bx, FALSE); 185 } 186 187 static WINDOW * 188 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad) 189 { 190 int i; 191 __LINE *lp; 192 WINDOW *win; 193 int maxy, maxx; 194 195 #ifdef DEBUG 196 __CTRACE(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n", 197 orig, nlines, ncols, by, bx, ispad); 198 #endif 199 if (orig == NULL) 200 return NULL; 201 202 /* Make sure window fits inside the original one. */ 203 maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines; 204 maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols; 205 if (by < orig->begy || bx < orig->begx 206 || by + maxy > orig->maxy + orig->begy 207 || bx + maxx > orig->maxx + orig->begx) 208 return NULL; 209 if ((win = __makenew(_cursesi_screen, maxy, maxx, 210 by, bx, 1, ispad)) == NULL) 211 return NULL; 212 win->bch = orig->bch; 213 win->battr = orig->battr; 214 win->reqy = nlines; 215 win->reqx = ncols; 216 win->nextp = orig->nextp; 217 orig->nextp = win; 218 win->orig = orig; 219 220 /* Initialize flags here so that refresh can also use __set_subwin. */ 221 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) 222 lp->flags = 0; 223 __set_subwin(orig, win); 224 return win; 225 } 226 /* 227 * This code is shared with mvwin(). 228 */ 229 void 230 __set_subwin(WINDOW *orig, WINDOW *win) 231 { 232 int i; 233 __LINE *lp, *olp; 234 #ifdef HAVE_WCHAR 235 __LDATA *cp; 236 int j; 237 nschar_t *np; 238 #endif /* HAVE_WCHAR */ 239 240 win->ch_off = win->begx - orig->begx; 241 /* Point line pointers to line space. */ 242 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) { 243 win->alines[i] = lp; 244 olp = orig->alines[i + win->begy - orig->begy]; 245 #ifdef DEBUG 246 lp->sentinel = SENTINEL_VALUE; 247 #endif 248 lp->line = &olp->line[win->ch_off]; 249 lp->firstchp = &olp->firstch; 250 lp->lastchp = &olp->lastch; 251 #ifndef HAVE_WCHAR 252 lp->hash = __hash((char *)(void *)lp->line, 253 (size_t)(win->maxx * __LDATASIZE)); 254 #else 255 lp->hash = 0; 256 for (cp = lp->line, j = 0; j < win->maxx; j++, cp++) { 257 lp->hash = __hash_more( &cp->ch, 258 sizeof( wchar_t ), lp->hash ); 259 lp->hash = __hash_more( &cp->attr, 260 sizeof( wchar_t ), lp->hash ); 261 if ( cp->nsp ) { 262 np = cp->nsp; 263 while ( np ) { 264 lp->hash = __hash_more( &np->ch, 265 sizeof( wchar_t ), lp->hash ); 266 np = np->next; 267 } 268 } 269 } 270 #endif /* HAVE_WCHAR */ 271 } 272 273 #ifdef DEBUG 274 __CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n", 275 win->ch_off); 276 #endif 277 } 278 /* 279 * __makenew -- 280 * Set up a window buffer and returns a pointer to it. 281 */ 282 static WINDOW * 283 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub, 284 int ispad) 285 { 286 WINDOW *win; 287 __LINE *lp; 288 struct __winlist *wlp, *wlp2; 289 int i; 290 291 292 #ifdef DEBUG 293 __CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n", 294 nlines, ncols, by, bx); 295 #endif 296 if (nlines <= 0 || ncols <= 0) 297 return NULL; 298 299 if ((win = malloc(sizeof(WINDOW))) == NULL) 300 return NULL; 301 #ifdef DEBUG 302 __CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win); 303 #endif 304 win->fp = NULL; 305 win->buf = NULL; 306 win->buflen = 0; 307 308 /* Set up line pointer array and line space. */ 309 if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) { 310 free(win); 311 return NULL; 312 } 313 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) { 314 free(win->alines); 315 free(win); 316 return NULL; 317 } 318 /* Don't allocate window and line space if it's a subwindow */ 319 if (sub) 320 win->wspace = NULL; 321 else { 322 /* 323 * Allocate window space in one chunk. 324 */ 325 if ((win->wspace = 326 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) { 327 free(win->lspace); 328 free(win->alines); 329 free(win); 330 return NULL; 331 } 332 /* 333 * Append window to window list. 334 */ 335 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) { 336 free(win->wspace); 337 free(win->lspace); 338 free(win->alines); 339 free(win); 340 return NULL; 341 } 342 wlp->winp = win; 343 wlp->nextp = NULL; 344 if (screen->winlistp == NULL) 345 screen->winlistp = wlp; 346 else { 347 wlp2 = screen->winlistp; 348 while (wlp2->nextp != NULL) 349 wlp2 = wlp2->nextp; 350 wlp2->nextp = wlp; 351 } 352 /* 353 * Point line pointers to line space, and lines themselves into 354 * window space. 355 */ 356 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) { 357 win->alines[i] = lp; 358 lp->line = &win->wspace[i * ncols]; 359 #ifdef DEBUG 360 lp->sentinel = SENTINEL_VALUE; 361 #endif 362 lp->firstchp = &lp->firstch; 363 lp->lastchp = &lp->lastch; 364 if (ispad) { 365 lp->firstch = 0; 366 lp->lastch = ncols; 367 } else { 368 lp->firstch = ncols; 369 lp->lastch = 0; 370 } 371 } 372 } 373 #ifdef DEBUG 374 __CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols); 375 #endif 376 win->screen = screen; 377 win->cury = win->curx = 0; 378 win->maxy = nlines; 379 win->maxx = ncols; 380 win->reqy = nlines; 381 win->reqx = ncols; 382 383 win->begy = by; 384 win->begx = bx; 385 win->flags = (__IDLINE | __IDCHAR); 386 win->delay = -1; 387 win->wattr = 0; 388 win->battr = 0; 389 #ifdef HAVE_WCHAR 390 win->bnsp = NULL; 391 SET_BGWCOL(*win, 1); 392 #endif /* HAVE_WCHAR */ 393 win->scr_t = 0; 394 win->scr_b = win->maxy - 1; 395 if (ispad) { 396 win->flags |= __ISPAD; 397 win->pbegy = 0; 398 win->pbegx = 0; 399 win->sbegy = 0; 400 win->sbegx = 0; 401 win->smaxy = 0; 402 win->smaxx = 0; 403 } else 404 __swflags(win); 405 #ifdef DEBUG 406 __CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr); 407 __CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags); 408 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy); 409 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx); 410 __CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy); 411 __CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx); 412 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t); 413 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b); 414 #endif 415 return win; 416 } 417 418 void 419 __swflags(WINDOW *win) 420 { 421 TERMINAL *term = win->screen->term; 422 423 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK); 424 if (win->begx + win->maxx == win->screen->COLS && 425 !(win->flags & __ISPAD)) 426 { 427 win->flags |= __ENDLINE; 428 if (win->begx == 0 && 429 win->maxy == win->screen->LINES && 430 win->begy == 0) 431 win->flags |= __FULLWIN; 432 if (win->begy + win->maxy == win->screen->LINES && 433 t_auto_right_margin(term) && 434 !(t_insert_character(term) != NULL || 435 t_parm_ich(term) != NULL || 436 (t_enter_insert_mode(term) != NULL && 437 t_exit_insert_mode(term) != NULL))) 438 win->flags |= __SCROLLWIN; 439 } 440 } 441 442 /* 443 * is_pad -- 444 * Return true if window was created by newpad. 445 */ 446 bool 447 is_pad(const WINDOW *win) 448 { 449 450 return win->flags & __ISPAD ? true : false; 451 } 452