1 /* $NetBSD: newwin.c,v 1.59 2021/05/15 11:06:07 uwe 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.59 2021/05/15 11:06:07 uwe 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 for (cp = lp->line, j = 0; j < win->maxx; j++, cp++) { 256 lp->hash = __hash_more( &cp->ch, 257 sizeof( wchar_t ), lp->hash ); 258 lp->hash = __hash_more( &cp->attr, 259 sizeof( wchar_t ), lp->hash ); 260 if ( cp->nsp ) { 261 np = cp->nsp; 262 while ( np ) { 263 lp->hash = __hash_more( &np->ch, 264 sizeof( wchar_t ), lp->hash ); 265 np = np->next; 266 } 267 } 268 } 269 #endif /* HAVE_WCHAR */ 270 } 271 272 #ifdef DEBUG 273 __CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n", 274 win->ch_off); 275 #endif 276 } 277 /* 278 * __makenew -- 279 * Set up a window buffer and returns a pointer to it. 280 */ 281 static WINDOW * 282 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub, 283 int ispad) 284 { 285 WINDOW *win; 286 __LINE *lp; 287 struct __winlist *wlp, *wlp2; 288 int i; 289 290 291 #ifdef DEBUG 292 __CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n", 293 nlines, ncols, by, bx); 294 #endif 295 if (nlines <= 0 || ncols <= 0) 296 return NULL; 297 298 if ((win = malloc(sizeof(WINDOW))) == NULL) 299 return NULL; 300 #ifdef DEBUG 301 __CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win); 302 #endif 303 win->fp = NULL; 304 win->buf = NULL; 305 win->buflen = 0; 306 307 /* Set up line pointer array and line space. */ 308 if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) { 309 free(win); 310 return NULL; 311 } 312 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) { 313 free(win->alines); 314 free(win); 315 return NULL; 316 } 317 /* Don't allocate window and line space if it's a subwindow */ 318 if (sub) 319 win->wspace = NULL; 320 else { 321 /* 322 * Allocate window space in one chunk. 323 */ 324 if ((win->wspace = 325 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) { 326 free(win->lspace); 327 free(win->alines); 328 free(win); 329 return NULL; 330 } 331 /* 332 * Append window to window list. 333 */ 334 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) { 335 free(win->wspace); 336 free(win->lspace); 337 free(win->alines); 338 free(win); 339 return NULL; 340 } 341 wlp->winp = win; 342 wlp->nextp = NULL; 343 if (screen->winlistp == NULL) 344 screen->winlistp = wlp; 345 else { 346 wlp2 = screen->winlistp; 347 while (wlp2->nextp != NULL) 348 wlp2 = wlp2->nextp; 349 wlp2->nextp = wlp; 350 } 351 /* 352 * Point line pointers to line space, and lines themselves into 353 * window space. 354 */ 355 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) { 356 win->alines[i] = lp; 357 lp->line = &win->wspace[i * ncols]; 358 #ifdef DEBUG 359 lp->sentinel = SENTINEL_VALUE; 360 #endif 361 lp->firstchp = &lp->firstch; 362 lp->lastchp = &lp->lastch; 363 if (ispad) { 364 lp->firstch = 0; 365 lp->lastch = ncols; 366 } else { 367 lp->firstch = ncols; 368 lp->lastch = 0; 369 } 370 } 371 } 372 #ifdef DEBUG 373 __CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols); 374 #endif 375 win->screen = screen; 376 win->cury = win->curx = 0; 377 win->maxy = nlines; 378 win->maxx = ncols; 379 win->reqy = nlines; 380 win->reqx = ncols; 381 382 win->begy = by; 383 win->begx = bx; 384 win->flags = (__IDLINE | __IDCHAR); 385 win->delay = -1; 386 win->wattr = 0; 387 win->battr = 0; 388 #ifdef HAVE_WCHAR 389 win->bnsp = NULL; 390 SET_BGWCOL(*win, 1); 391 #endif /* HAVE_WCHAR */ 392 win->scr_t = 0; 393 win->scr_b = win->maxy - 1; 394 if (ispad) { 395 win->flags |= __ISPAD; 396 win->pbegy = 0; 397 win->pbegx = 0; 398 win->sbegy = 0; 399 win->sbegx = 0; 400 win->smaxy = 0; 401 win->smaxx = 0; 402 } else 403 __swflags(win); 404 #ifdef DEBUG 405 __CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr); 406 __CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags); 407 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy); 408 __CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx); 409 __CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy); 410 __CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx); 411 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t); 412 __CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b); 413 #endif 414 return win; 415 } 416 417 void 418 __swflags(WINDOW *win) 419 { 420 TERMINAL *term = win->screen->term; 421 422 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK); 423 if (win->begx + win->maxx == win->screen->COLS && 424 !(win->flags & __ISPAD)) 425 { 426 win->flags |= __ENDLINE; 427 if (win->begx == 0 && 428 win->maxy == win->screen->LINES && 429 win->begy == 0) 430 win->flags |= __FULLWIN; 431 if (win->begy + win->maxy == win->screen->LINES && 432 t_auto_right_margin(term) && 433 !(t_insert_character(term) != NULL || 434 t_parm_ich(term) != NULL || 435 (t_enter_insert_mode(term) != NULL && 436 t_exit_insert_mode(term) != NULL))) 437 win->flags |= __SCROLLWIN; 438 } 439 } 440 441 /* 442 * is_pad -- 443 * Return true if window was created by newpad. 444 */ 445 bool 446 is_pad(const WINDOW *win) 447 { 448 449 return win->flags & __ISPAD ? true : false; 450 } 451