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