xref: /netbsd-src/lib/libcurses/touchwin.c (revision 6348e3f32a4431b45d07e1da16fd7b12725a45e7)
1 /*	$NetBSD: touchwin.c,v 1.35 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[] = "@(#)touchwin.c	8.2 (Berkeley) 5/4/94";
36 #else
37 __RCSID("$NetBSD: touchwin.c,v 1.35 2024/12/23 02:58:04 blymn Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include "curses.h"
42 #include "curses_private.h"
43 
44 static int _cursesi_touchline_force(WINDOW *, int, int, int, int);
45 
46 /*
47  * __sync --
48  *	To be called after each window change.
49  */
50 void
51 __sync(WINDOW *win)
52 {
53 	if (__predict_false(win == NULL))
54 		return;
55 
56 	if (win->flags & __IMMEDOK)
57 		wrefresh(win);
58 	if (win->flags & __SYNCOK)
59 		wsyncup(win);
60 }
61 
62 /*
63  * is_linetouched --
64  *	Indicate if line has been touched or not.
65  */
66 bool
67 is_linetouched(WINDOW *win, int line)
68 {
69 	if (__predict_false(win == NULL))
70 		return ERR;
71 
72 	if (line > win->maxy)
73 		return FALSE;
74 
75 	__CTRACE(__CTRACE_LINE, "is_linetouched: (%p, line %d, dirty %d)\n",
76 	    win, line, (win->alines[line]->flags & __ISDIRTY));
77 	return (win->alines[line]->flags & __ISDIRTY) != 0;
78 }
79 
80 /*
81  * touchline --
82  *	Touch count lines starting at start.  This is the SUS v2 compliant
83  *	version.
84  */
85 int
86 touchline(WINDOW *win, int start, int count)
87 {
88 	__CTRACE(__CTRACE_LINE, "touchline: (%p, %d, %d)\n", win, start, count);
89 	return wtouchln(win, start, count, 1);
90 }
91 
92 /*
93  * wredrawln --
94  *	Mark count lines starting at start as corrupted.  Implemented using
95  *	wtouchln().
96  */
97 int wredrawln(WINDOW *win, int start, int count)
98 {
99 	__CTRACE(__CTRACE_LINE, "wredrawln: (%p, %d, %d)\n", win, start, count);
100 	return wtouchln(win, start, count, 1);
101 }
102 
103 /*
104  * is_wintouched --
105  *	Check if the window has been touched.
106  */
107 bool
108 is_wintouched(WINDOW *win)
109 {
110 	int y, maxy;
111 
112 	__CTRACE(__CTRACE_LINE, "is_wintouched: (%p, maxy %d)\n", win,
113 	    win->maxy);
114 	if (__predict_false(win == NULL))
115 		return FALSE;
116 
117 	maxy = win->maxy;
118 	for (y = 0; y < maxy; y++) {
119 		if (is_linetouched(win, y) == TRUE)
120 			return TRUE;
121 	}
122 
123 	return FALSE;
124 }
125 
126 /*
127  * touchwin --
128  *	Make it look like the whole window has been changed.
129  */
130 int
131 touchwin(WINDOW *win)
132 {
133 	__CTRACE(__CTRACE_LINE, "touchwin: (%p)\n", win);
134 	return wtouchln(win, 0, win->maxy, 1);
135 }
136 
137 /*
138  * redrawwin --
139  *	Mark entire window as corrupted.  Implemented using wtouchln().
140  */
141 int
142 redrawwin(WINDOW *win)
143 {
144 	__CTRACE(__CTRACE_LINE, "redrawwin: (%p)\n", win);
145 	return wtouchln(win, 0, win->maxy, 1);
146 }
147 
148 /*
149  * untouchwin --
150  *	 Make it look like the window has not been changed.
151  */
152 int
153 untouchwin(WINDOW *win)
154 {
155 	__CTRACE(__CTRACE_LINE, "untouchwin: (%p)\n", win);
156 	return wtouchln(win, 0, win->maxy, 0);
157 }
158 
159 /*
160  * wtouchln --
161  *	If changed is 1 then touch n lines starting at line.  If changed
162  *	is 0 then mark the lines as unchanged.
163  */
164 int
165 wtouchln(WINDOW *win, int line, int n, int changed)
166 {
167 	int	y;
168 	__LINE	*wlp;
169 
170 	__CTRACE(__CTRACE_LINE, "wtouchln: (%p) %d, %d, %d\n",
171 	    win, line, n, changed);
172 	if (__predict_false(win == NULL))
173 		return FALSE;
174 
175 	if (line < 0 || win->maxy <= line)
176 		return ERR;
177 	if (n < 0)
178 		return ERR;
179 	if (n > win->maxy - line)
180 		n = win->maxy - line;
181 
182 	for (y = line; y < line + n; y++) {
183 		if (changed == 1)
184 			_cursesi_touchline_force(win, y, 0,
185 			    (int) win->maxx - 1, 1);
186 		else {
187 			wlp = win->alines[y];
188 			if (*wlp->firstchp >= win->ch_off &&
189 			    *wlp->firstchp < win->maxx + win->ch_off)
190 				*wlp->firstchp = win->maxx + win->ch_off;
191 			if (*wlp->lastchp >= win->ch_off &&
192 			    *wlp->lastchp < win->maxx + win->ch_off)
193 				*wlp->lastchp = win->ch_off;
194 			wlp->flags &= ~(__ISDIRTY | __ISFORCED);
195 		}
196 	}
197 
198 	return OK;
199 }
200 
201 /*
202  * Touch all the lines in a window.  If force is set to 1 then screen
203  * update optimisation will disabled to force the change out.
204  */
205 int
206 __touchwin(WINDOW *win, int force)
207 {
208 	int	 y, maxy;
209 
210 	__CTRACE(__CTRACE_LINE, "__touchwin: (%p)\n", win);
211 	maxy = win->maxy;
212 	for (y = 0; y < maxy; y++)
213 		_cursesi_touchline_force(win, y, 0, (int) win->maxx - 1,
214 		    force);
215 	return OK;
216 }
217 
218 int
219 __touchline(WINDOW *win, int y, int sx, int ex)
220 {
221 
222 	return _cursesi_touchline_force(win, y, sx, ex, 0);
223 }
224 
225 /*
226  * Touch line y on window win starting from column sx and ending at
227  * column ex.  If force is 1 then we mark this line as a forced update
228  * which will bypass screen optimisation in the refresh code to rewrite
229  * this line unconditionally (even if refresh thinks the screen matches
230  * what is in the virtscr)
231  */
232 static int
233 _cursesi_touchline_force(WINDOW *win, int y, int sx, int ex, int force)
234 {
235 
236 	__CTRACE(__CTRACE_LINE, "__touchline: (%p, %d, %d, %d, %d)\n",
237 	    win, y, sx, ex, force);
238 	__CTRACE(__CTRACE_LINE, "__touchline: first = %d, last = %d\n",
239 	    *win->alines[y]->firstchp, *win->alines[y]->lastchp);
240 	sx += win->ch_off;
241 	ex += win->ch_off;
242 	win->alines[y]->flags |= __ISDIRTY;
243 	if (force == 1)
244 		win->alines[y]->flags |= __ISFORCED;
245 	/* firstchp/lastchp are shared between parent window and sub-window. */
246 	if (*win->alines[y]->firstchp > sx)
247 		*win->alines[y]->firstchp = sx;
248 	if (*win->alines[y]->lastchp < ex)
249 		*win->alines[y]->lastchp = ex;
250 	__CTRACE(__CTRACE_LINE, "__touchline: first = %d, last = %d\n",
251 	    *win->alines[y]->firstchp, *win->alines[y]->lastchp);
252 	return OK;
253 }
254 
255 void
256 wsyncup(WINDOW *win)
257 {
258 
259 	if (__predict_false(win == NULL))
260 		return;
261 
262 	do {
263 		__touchwin(win, 0);
264 		win = win->orig;
265 	} while (win);
266 }
267 
268 void
269 wsyncdown(WINDOW *win)
270 {
271 	if (__predict_false(win == NULL))
272 		return;
273 
274 	WINDOW *w = win->orig;
275 
276 	while (w) {
277 		if (is_wintouched(w)) {
278 			__touchwin(win, 0);
279 			break;
280 		}
281 		w = w->orig;
282 	}
283 }
284