1 /* $NetBSD: touchwin.c,v 1.32 2020/07/03 23:28:51 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[] = "@(#)touchwin.c 8.2 (Berkeley) 5/4/94"; 36 #else 37 __RCSID("$NetBSD: touchwin.c,v 1.32 2020/07/03 23:28:51 uwe 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 #ifdef DEBUG 71 __CTRACE(__CTRACE_LINE, "is_linetouched: (%p, line %d, dirty %d)\n", 72 win, line, (win->alines[line]->flags & __ISDIRTY)); 73 #endif 74 return (win->alines[line]->flags & __ISDIRTY) != 0; 75 } 76 77 /* 78 * touchline -- 79 * Touch count lines starting at start. This is the SUS v2 compliant 80 * version. 81 */ 82 int 83 touchline(WINDOW *win, int start, int count) 84 { 85 #ifdef DEBUG 86 __CTRACE(__CTRACE_LINE, "touchline: (%p, %d, %d)\n", win, start, count); 87 #endif 88 return wtouchln(win, start, count, 1); 89 } 90 91 /* 92 * wredrawln -- 93 * Mark count lines starting at start as corrupted. Implemented using 94 * wtouchln(). 95 */ 96 int wredrawln(WINDOW *win, int start, int count) 97 { 98 #ifdef DEBUG 99 __CTRACE(__CTRACE_LINE, "wredrawln: (%p, %d, %d)\n", win, start, count); 100 #endif 101 return wtouchln(win, start, count, 1); 102 } 103 104 /* 105 * is_wintouched -- 106 * Check if the window has been touched. 107 */ 108 bool 109 is_wintouched(WINDOW *win) 110 { 111 int y, maxy; 112 113 #ifdef DEBUG 114 __CTRACE(__CTRACE_LINE, "is_wintouched: (%p, maxy %d)\n", win, 115 win->maxy); 116 #endif 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 #ifdef DEBUG 134 __CTRACE(__CTRACE_LINE, "touchwin: (%p)\n", win); 135 #endif 136 return wtouchln(win, 0, win->maxy, 1); 137 } 138 139 /* 140 * redrawwin -- 141 * Mark entire window as corrupted. Implemented using wtouchln(). 142 */ 143 int 144 redrawwin(WINDOW *win) 145 { 146 #ifdef DEBUG 147 __CTRACE(__CTRACE_LINE, "redrawwin: (%p)\n", win); 148 #endif 149 return wtouchln(win, 0, win->maxy, 1); 150 } 151 152 /* 153 * untouchwin -- 154 * Make it look like the window has not been changed. 155 */ 156 int 157 untouchwin(WINDOW *win) 158 { 159 #ifdef DEBUG 160 __CTRACE(__CTRACE_LINE, "untouchwin: (%p)\n", win); 161 #endif 162 return wtouchln(win, 0, win->maxy, 0); 163 } 164 165 /* 166 * wtouchln -- 167 * If changed is 1 then touch n lines starting at line. If changed 168 * is 0 then mark the lines as unchanged. 169 */ 170 int 171 wtouchln(WINDOW *win, int line, int n, int changed) 172 { 173 int y; 174 __LINE *wlp; 175 176 #ifdef DEBUG 177 __CTRACE(__CTRACE_LINE, "wtouchln: (%p) %d, %d, %d\n", 178 win, line, n, changed); 179 #endif 180 if (line < 0 || win->maxy <= line) 181 return ERR; 182 if (n < 0) 183 return ERR; 184 if (n > win->maxy - line) 185 n = win->maxy - line; 186 187 for (y = line; y < line + n; y++) { 188 if (changed == 1) 189 _cursesi_touchline_force(win, y, 0, 190 (int) win->maxx - 1, 1); 191 else { 192 wlp = win->alines[y]; 193 if (*wlp->firstchp >= win->ch_off && 194 *wlp->firstchp < win->maxx + win->ch_off) 195 *wlp->firstchp = win->maxx + win->ch_off; 196 if (*wlp->lastchp >= win->ch_off && 197 *wlp->lastchp < win->maxx + win->ch_off) 198 *wlp->lastchp = win->ch_off; 199 wlp->flags &= ~(__ISDIRTY | __ISFORCED); 200 } 201 } 202 203 return OK; 204 } 205 206 int 207 __touchwin(WINDOW *win) 208 { 209 int y, maxy; 210 211 #ifdef DEBUG 212 __CTRACE(__CTRACE_LINE, "__touchwin: (%p)\n", win); 213 #endif 214 maxy = win->maxy; 215 for (y = 0; y < maxy; y++) 216 __touchline(win, y, 0, (int) win->maxx - 1); 217 return OK; 218 } 219 220 int 221 __touchline(WINDOW *win, int y, int sx, int ex) 222 { 223 224 return _cursesi_touchline_force(win, y, sx, ex, 0); 225 } 226 227 /* 228 * Touch line y on window win starting from column sx and ending at 229 * column ex. If force is 1 then we mark this line as a forced update 230 * which will bypass screen optimisation in the refresh code to rewrite 231 * this line unconditionally (even if refresh thinks the screen matches 232 * what is in the virtscr) 233 */ 234 static int 235 _cursesi_touchline_force(WINDOW *win, int y, int sx, int ex, int force) 236 { 237 238 #ifdef DEBUG 239 __CTRACE(__CTRACE_LINE, "__touchline: (%p, %d, %d, %d, %d)\n", 240 win, y, sx, ex, force); 241 __CTRACE(__CTRACE_LINE, "__touchline: first = %d, last = %d\n", 242 *win->alines[y]->firstchp, *win->alines[y]->lastchp); 243 #endif 244 sx += win->ch_off; 245 ex += win->ch_off; 246 win->alines[y]->flags |= __ISDIRTY; 247 if (force == 1) 248 win->alines[y]->flags |= __ISFORCED; 249 /* firstchp/lastchp are shared between parent window and sub-window. */ 250 if (*win->alines[y]->firstchp > sx) 251 *win->alines[y]->firstchp = sx; 252 if (*win->alines[y]->lastchp < ex) 253 *win->alines[y]->lastchp = ex; 254 #ifdef DEBUG 255 __CTRACE(__CTRACE_LINE, "__touchline: first = %d, last = %d\n", 256 *win->alines[y]->firstchp, *win->alines[y]->lastchp); 257 #endif 258 return OK; 259 } 260 261 void 262 wsyncup(WINDOW *win) 263 { 264 265 do { 266 __touchwin(win); 267 win = win->orig; 268 } while (win); 269 } 270 271 void 272 wsyncdown(WINDOW *win) 273 { 274 WINDOW *w = win->orig; 275 276 while (w) { 277 if (is_wintouched(w)) { 278 __touchwin(win); 279 break; 280 } 281 w = w->orig; 282 } 283 } 284