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