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