1 /* $NetBSD: newwin.c,v 1.58 2020/07/14 04:39:39 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.58 2020/07/14 04:39:39 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 else 149 win->battr = 0; 150 win->nextp = win; 151 win->ch_off = 0; 152 win->orig = NULL; 153 win->reqy = nlines; 154 win->reqx = ncols; 155 156 #ifdef DEBUG 157 __CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off); 158 #endif 159 160 for (i = 0; i < maxy; i++) { 161 lp = win->alines[i]; 162 if (ispad) 163 lp->flags = __ISDIRTY; 164 else 165 lp->flags = 0; 166 for (sp = lp->line, j = 0; j < maxx; j++, sp++) { 167 sp->attr = 0; 168 #ifndef HAVE_WCHAR 169 sp->ch = win->bch; 170 #else 171 sp->ch = (wchar_t)btowc((int) win->bch); 172 sp->nsp = NULL; 173 SET_WCOL(*sp, 1); 174 #endif /* HAVE_WCHAR */ 175 } 176 lp->hash = __hash((char *)(void *)lp->line, 177 (size_t)(maxx * __LDATASIZE)); 178 } 179 return (win); 180 } 181 182 WINDOW * 183 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx) 184 { 185 186 return __subwin(orig, nlines, ncols, by, bx, FALSE); 187 } 188 189 static WINDOW * 190 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad) 191 { 192 int i; 193 __LINE *lp; 194 WINDOW *win; 195 int maxy, maxx; 196 197 #ifdef DEBUG 198 __CTRACE(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n", 199 orig, nlines, ncols, by, bx, ispad); 200 #endif 201 if (orig == NULL) 202 return NULL; 203 204 /* Make sure window fits inside the original one. */ 205 maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines; 206 maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols; 207 if (by < orig->begy || bx < orig->begx 208 || by + maxy > orig->maxy + orig->begy 209 || bx + maxx > orig->maxx + orig->begx) 210 return NULL; 211 if ((win = __makenew(_cursesi_screen, maxy, maxx, 212 by, bx, 1, ispad)) == NULL) 213 return NULL; 214 win->bch = orig->bch; 215 win->battr = orig->battr; 216 win->reqy = nlines; 217 win->reqx = ncols; 218 win->nextp = orig->nextp; 219 orig->nextp = win; 220 win->orig = orig; 221 222 /* Initialize flags here so that refresh can also use __set_subwin. */ 223 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) 224 lp->flags = 0; 225 __set_subwin(orig, win); 226 return win; 227 } 228 /* 229 * This code is shared with mvwin(). 230 */ 231 void 232 __set_subwin(WINDOW *orig, WINDOW *win) 233 { 234 int i; 235 __LINE *lp, *olp; 236 #ifdef HAVE_WCHAR 237 __LDATA *cp; 238 int j; 239 nschar_t *np; 240 #endif /* HAVE_WCHAR */ 241 242 win->ch_off = win->begx - orig->begx; 243 /* Point line pointers to line space. */ 244 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) { 245 win->alines[i] = lp; 246 olp = orig->alines[i + win->begy - orig->begy]; 247 #ifdef DEBUG 248 lp->sentinel = SENTINEL_VALUE; 249 #endif 250 lp->line = &olp->line[win->ch_off]; 251 lp->firstchp = &olp->firstch; 252 lp->lastchp = &olp->lastch; 253 #ifndef HAVE_WCHAR 254 lp->hash = __hash((char *)(void *)lp->line, 255 (size_t)(win->maxx * __LDATASIZE)); 256 #else 257 for (cp = lp->line, j = 0; j < win->maxx; j++, cp++) { 258 lp->hash = __hash_more( &cp->ch, 259 sizeof( wchar_t ), lp->hash ); 260 lp->hash = __hash_more( &cp->attr, 261 sizeof( wchar_t ), lp->hash ); 262 if ( cp->nsp ) { 263 np = cp->nsp; 264 while ( np ) { 265 lp->hash = __hash_more( &np->ch, 266 sizeof( wchar_t ), lp->hash ); 267 np = np->next; 268 } 269 } 270 } 271 #endif /* HAVE_WCHAR */ 272 } 273 274 #ifdef DEBUG 275 __CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n", 276 win->ch_off); 277 #endif 278 } 279 /* 280 * __makenew -- 281 * Set up a window buffer and returns a pointer to it. 282 */ 283 static WINDOW * 284 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub, 285 int ispad) 286 { 287 WINDOW *win; 288 __LINE *lp; 289 struct __winlist *wlp, *wlp2; 290 int i; 291 292 293 #ifdef DEBUG 294 __CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n", 295 nlines, ncols, by, bx); 296 #endif 297 if (nlines <= 0 || ncols <= 0) 298 return NULL; 299 300 if ((win = malloc(sizeof(WINDOW))) == NULL) 301 return NULL; 302 #ifdef DEBUG 303 __CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win); 304 #endif 305 win->fp = NULL; 306 win->buf = NULL; 307 win->buflen = 0; 308 309 /* Set up line pointer array and line space. */ 310 if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) { 311 free(win); 312 return NULL; 313 } 314 if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) { 315 free(win->alines); 316 free(win); 317 return NULL; 318 } 319 /* Don't allocate window and line space if it's a subwindow */ 320 if (sub) 321 win->wspace = NULL; 322 else { 323 /* 324 * Allocate window space in one chunk. 325 */ 326 if ((win->wspace = 327 malloc(ncols * nlines * sizeof(__LDATA))) == NULL) { 328 free(win->lspace); 329 free(win->alines); 330 free(win); 331 return NULL; 332 } 333 /* 334 * Append window to window list. 335 */ 336 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) { 337 free(win->wspace); 338 free(win->lspace); 339 free(win->alines); 340 free(win); 341 return NULL; 342 } 343 wlp->winp = win; 344 wlp->nextp = NULL; 345 if (screen->winlistp == NULL) 346 screen->winlistp = wlp; 347 else { 348 wlp2 = screen->winlistp; 349 while (wlp2->nextp != NULL) 350 wlp2 = wlp2->nextp; 351 wlp2->nextp = wlp; 352 } 353 /* 354 * Point line pointers to line space, and lines themselves into 355 * window space. 356 */ 357 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) { 358 win->alines[i] = lp; 359 lp->line = &win->wspace[i * ncols]; 360 #ifdef DEBUG 361 lp->sentinel = SENTINEL_VALUE; 362 #endif 363 lp->firstchp = &lp->firstch; 364 lp->lastchp = &lp->lastch; 365 if (ispad) { 366 lp->firstch = 0; 367 lp->lastch = ncols; 368 } else { 369 lp->firstch = ncols; 370 lp->lastch = 0; 371 } 372 } 373 } 374 #ifdef DEBUG 375 __CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols); 376 #endif 377 win->screen = screen; 378 win->cury = win->curx = 0; 379 win->maxy = nlines; 380 win->maxx = ncols; 381 win->reqy = nlines; 382 win->reqx = ncols; 383 384 win->begy = by; 385 win->begx = bx; 386 win->flags = (__IDLINE | __IDCHAR); 387 win->delay = -1; 388 win->wattr = 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