refresh.c revision 1.50 1 1.50 jdc /* $NetBSD: refresh.c,v 1.50 2002/12/23 12:17:55 jdc Exp $ */
2 1.8 mikel
3 1.1 cgd /*
4 1.7 cgd * Copyright (c) 1981, 1993, 1994
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.8 mikel #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 mikel #if 0
39 1.7 cgd static char sccsid[] = "@(#)refresh.c 8.7 (Berkeley) 8/13/94";
40 1.8 mikel #else
41 1.50 jdc __RCSID("$NetBSD: refresh.c,v 1.50 2002/12/23 12:17:55 jdc Exp $");
42 1.8 mikel #endif
43 1.11 mrg #endif /* not lint */
44 1.1 cgd
45 1.38 matt #include <stdlib.h>
46 1.4 mycroft #include <string.h>
47 1.1 cgd
48 1.7 cgd #include "curses.h"
49 1.15 blymn #include "curses_private.h"
50 1.7 cgd
51 1.4 mycroft static void domvcur __P((int, int, int, int));
52 1.23 jdc static int makech __P((int));
53 1.23 jdc static void quickch __P((void));
54 1.23 jdc static void scrolln __P((int, int, int, int, int));
55 1.1 cgd
56 1.17 blymn #ifndef _CURSES_USE_MACROS
57 1.17 blymn
58 1.17 blymn /*
59 1.17 blymn * refresh --
60 1.17 blymn * Make the current screen look like "stdscr" over the area covered by
61 1.17 blymn * stdscr.
62 1.17 blymn */
63 1.17 blymn int
64 1.17 blymn refresh(void)
65 1.17 blymn {
66 1.17 blymn return wrefresh(stdscr);
67 1.17 blymn }
68 1.17 blymn
69 1.17 blymn #endif
70 1.17 blymn
71 1.4 mycroft /*
72 1.23 jdc * wnoutrefresh --
73 1.23 jdc * Add the contents of "win" to the virtual window.
74 1.23 jdc */
75 1.23 jdc int
76 1.23 jdc wnoutrefresh(WINDOW *win)
77 1.23 jdc {
78 1.50 jdc #ifdef DEBUG
79 1.50 jdc __CTRACE("wnoutrefresh: win %0.2o\n", win);
80 1.50 jdc #endif
81 1.50 jdc
82 1.50 jdc return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
83 1.50 jdc win->begx, win->maxy, win->maxx);
84 1.44 blymn }
85 1.44 blymn
86 1.50 jdc /*
87 1.50 jdc * pnoutrefresh --
88 1.50 jdc * Add the contents of "pad" to the virtual window.
89 1.50 jdc */
90 1.50 jdc int
91 1.50 jdc pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
92 1.50 jdc int smaxy, int smaxx)
93 1.50 jdc {
94 1.50 jdc int pmaxy, pmaxx;
95 1.50 jdc
96 1.50 jdc #ifdef DEBUG
97 1.50 jdc __CTRACE("pnoutrefresh: pad %0.2o, flags 0x%08x\n", pad, pad->flags);
98 1.50 jdc __CTRACE("pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n", pbegy, pbegx,
99 1.50 jdc sbegy, sbegx, smaxy, smaxx);
100 1.50 jdc #endif
101 1.50 jdc
102 1.50 jdc /* SUS says if these are negative, they should be treated as zero */
103 1.50 jdc if (pbegy < 0)
104 1.50 jdc pbegy = 0;
105 1.50 jdc if (pbegx < 0)
106 1.50 jdc pbegx = 0;
107 1.50 jdc if (sbegy < 0)
108 1.50 jdc sbegy = 0;
109 1.50 jdc if (sbegx < 0)
110 1.50 jdc sbegx = 0;
111 1.50 jdc
112 1.50 jdc /* Calculate rectangle on pad - used bu _cursesi_wnoutrefresh */
113 1.50 jdc pmaxy = pbegy + smaxy - sbegy + 1;
114 1.50 jdc pmaxx = pbegx + smaxx - sbegx + 1;
115 1.50 jdc
116 1.50 jdc /* Check rectangle fits in pad */
117 1.50 jdc if (pmaxy > pad->maxy - pad->begy)
118 1.50 jdc pmaxy = pad->maxy - pad->begy;
119 1.50 jdc if (pmaxx > pad->maxx - pad->begx)
120 1.50 jdc pmaxx = pad->maxx - pad->begx;
121 1.50 jdc
122 1.50 jdc if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
123 1.50 jdc return ERR;
124 1.50 jdc
125 1.50 jdc return _cursesi_wnoutrefresh(_cursesi_screen, pad,
126 1.50 jdc pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
127 1.50 jdc pad->begx + sbegx, pmaxy, pmaxx);
128 1.50 jdc }
129 1.44 blymn
130 1.44 blymn /*
131 1.44 blymn * _cursesi_wnoutrefresh --
132 1.44 blymn * Does the grunt work for wnoutrefresh to the given screen.
133 1.50 jdc * Copies the part of the window given by the rectangle
134 1.50 jdc * (begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
135 1.44 blymn */
136 1.44 blymn int
137 1.50 jdc _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
138 1.50 jdc int wbegy, int wbegx, int maxy, int maxx)
139 1.44 blymn {
140 1.45 blymn
141 1.50 jdc short wy, wx, y_off, x_off;
142 1.33 mycroft __LINE *wlp, *vlp;
143 1.23 jdc
144 1.23 jdc #ifdef DEBUG
145 1.23 jdc __CTRACE("wnoutrefresh: win %0.2o, flags 0x%08x\n", win, win->flags);
146 1.50 jdc __CTRACE("wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n", begy, begx,
147 1.50 jdc wbegy, wbegx, maxy, maxx);
148 1.23 jdc #endif
149 1.23 jdc
150 1.44 blymn if (screen->curwin)
151 1.23 jdc return(OK);
152 1.50 jdc
153 1.50 jdc /* Check that cursor position on "win" is valid for "__virtscr" */
154 1.50 jdc if (win->cury + wbegy - begy < screen->__virtscr->maxy &&
155 1.50 jdc win->cury + wbegy - begy >= 0 && win->cury < maxy - begy)
156 1.50 jdc screen->__virtscr->cury = win->cury + wbegy - begy;
157 1.50 jdc if (win->curx + wbegx - begx < screen->__virtscr->maxx &&
158 1.50 jdc win->curx + wbegx - begx >= 0 && win->curx < maxx - begx)
159 1.50 jdc screen->__virtscr->curx = win->curx + wbegx - begx;
160 1.23 jdc
161 1.23 jdc /* Copy the window flags from "win" to "__virtscr" */
162 1.33 mycroft if (win->flags & __CLEAROK) {
163 1.50 jdc if (win->flags & __FULLWIN) {
164 1.44 blymn screen->__virtscr->flags |= __CLEAROK;
165 1.50 jdc werase(screen->__virtscr);
166 1.50 jdc }
167 1.23 jdc win->flags &= ~__CLEAROK;
168 1.33 mycroft }
169 1.44 blymn screen->__virtscr->flags &= ~__LEAVEOK;
170 1.44 blymn screen->__virtscr->flags |= win->flags;
171 1.23 jdc
172 1.50 jdc for (wy = begy, y_off = wbegy; wy < maxy &&
173 1.50 jdc y_off < screen->__virtscr->maxy; wy++, y_off++) {
174 1.50 jdc wlp = win->lines[wy];
175 1.23 jdc #ifdef DEBUG
176 1.23 jdc __CTRACE("wnoutrefresh: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
177 1.33 mycroft *wlp->firstchp, *wlp->lastchp, wlp->flags);
178 1.23 jdc #endif
179 1.35 mycroft if ((wlp->flags & __ISDIRTY) == 0)
180 1.33 mycroft continue;
181 1.50 jdc vlp = screen->__virtscr->lines[y_off];
182 1.33 mycroft
183 1.50 jdc if (*wlp->firstchp < maxx + win->ch_off &&
184 1.33 mycroft *wlp->lastchp >= win->ch_off) {
185 1.23 jdc /* Copy line from "win" to "__virtscr". */
186 1.50 jdc for (wx = begx + *wlp->firstchp - win->ch_off,
187 1.50 jdc x_off = wbegx + *wlp->firstchp - win->ch_off;
188 1.50 jdc wx <= *wlp->lastchp && wx < maxx &&
189 1.44 blymn x_off < screen->__virtscr->maxx; wx++, x_off++) {
190 1.33 mycroft vlp->line[x_off].attr = wlp->line[wx].attr;
191 1.36 jdc if (wlp->line[wx].attr & __COLOR)
192 1.33 mycroft vlp->line[x_off].attr |=
193 1.36 jdc wlp->line[wx].battr & ~__COLOR;
194 1.36 jdc else
195 1.36 jdc vlp->line[x_off].attr |=
196 1.36 jdc wlp->line[wx].battr;
197 1.33 mycroft if (wlp->line[wx].ch == ' ' &&
198 1.33 mycroft wlp->line[wx].bch != ' ')
199 1.33 mycroft vlp->line[x_off].ch
200 1.33 mycroft = wlp->line[wx].bch;
201 1.23 jdc else
202 1.33 mycroft vlp->line[x_off].ch
203 1.33 mycroft = wlp->line[wx].ch;
204 1.23 jdc }
205 1.23 jdc
206 1.23 jdc /* Set flags on "__virtscr" and unset on "win". */
207 1.33 mycroft if (wlp->flags & __ISPASTEOL)
208 1.33 mycroft vlp->flags |= __ISPASTEOL;
209 1.23 jdc else
210 1.33 mycroft vlp->flags &= ~__ISPASTEOL;
211 1.33 mycroft if (wlp->flags & __ISDIRTY)
212 1.33 mycroft vlp->flags |= __ISDIRTY;
213 1.23 jdc
214 1.23 jdc #ifdef DEBUG
215 1.23 jdc __CTRACE("win: firstch = %d, lastch = %d\n",
216 1.33 mycroft *wlp->firstchp, *wlp->lastchp);
217 1.23 jdc #endif
218 1.23 jdc /* Set change pointers on "__virtscr". */
219 1.33 mycroft if (*vlp->firstchp >
220 1.50 jdc *wlp->firstchp + wbegx - win->ch_off)
221 1.33 mycroft *vlp->firstchp =
222 1.50 jdc *wlp->firstchp + wbegx - win->ch_off;
223 1.33 mycroft if (*vlp->lastchp <
224 1.50 jdc *wlp->lastchp + wbegx - win->ch_off)
225 1.33 mycroft *vlp->lastchp =
226 1.50 jdc *wlp->lastchp + wbegx - win->ch_off;
227 1.23 jdc #ifdef DEBUG
228 1.23 jdc __CTRACE("__virtscr: firstch = %d, lastch = %d\n",
229 1.33 mycroft *vlp->firstchp, *vlp->lastchp);
230 1.23 jdc #endif
231 1.50 jdc /*
232 1.50 jdc * Unset change pointers only if a window, as a pad
233 1.50 jdc * can be displayed again without any of the contents
234 1.50 jdc * changing.
235 1.50 jdc */
236 1.50 jdc if (!(win->flags & __ISPAD)) {
237 1.50 jdc /* Set change pointers on "win". */
238 1.50 jdc if (*wlp->firstchp >= win->ch_off)
239 1.50 jdc *wlp->firstchp = maxx + win->ch_off;
240 1.50 jdc if (*wlp->lastchp < maxx + win->ch_off)
241 1.50 jdc *wlp->lastchp = win->ch_off;
242 1.50 jdc if (*wlp->lastchp < *wlp->firstchp) {
243 1.23 jdc #ifdef DEBUG
244 1.50 jdc __CTRACE("wnoutrefresh: line %d notdirty\n", wy);
245 1.23 jdc #endif
246 1.50 jdc wlp->flags &= ~__ISDIRTY;
247 1.50 jdc }
248 1.23 jdc }
249 1.23 jdc }
250 1.23 jdc }
251 1.23 jdc
252 1.23 jdc return (OK);
253 1.23 jdc }
254 1.23 jdc
255 1.23 jdc /*
256 1.4 mycroft * wrefresh --
257 1.50 jdc * Make the current screen look like "win" over the area covered by
258 1.4 mycroft * win.
259 1.4 mycroft */
260 1.4 mycroft int
261 1.17 blymn wrefresh(WINDOW *win)
262 1.1 cgd {
263 1.44 blymn int retval;
264 1.45 blymn
265 1.50 jdc #ifdef DEBUG
266 1.50 jdc __CTRACE("wrefresh: win %0.2o\n", win);
267 1.50 jdc #endif
268 1.50 jdc
269 1.44 blymn _cursesi_screen->curwin = (win == _cursesi_screen->curscr);
270 1.43 blymn if (!_cursesi_screen->curwin)
271 1.50 jdc retval = _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0,
272 1.50 jdc win->begy, win->begx, win->maxy, win->maxx);
273 1.23 jdc else
274 1.23 jdc retval = OK;
275 1.23 jdc if (retval == OK) {
276 1.23 jdc retval = doupdate();
277 1.24 jdc if (!win->flags & __LEAVEOK) {
278 1.24 jdc win->cury = max(0, curscr->cury - win->begy);
279 1.24 jdc win->curx = max(0, curscr->curx - win->begx);
280 1.24 jdc }
281 1.23 jdc }
282 1.43 blymn _cursesi_screen->curwin = 0;
283 1.50 jdc return(retval);
284 1.50 jdc }
285 1.50 jdc
286 1.50 jdc /*
287 1.50 jdc * prefresh --
288 1.50 jdc * Make the current screen look like "pad" over the area coverd by
289 1.50 jdc * the specified area of pad.
290 1.50 jdc */
291 1.50 jdc int
292 1.50 jdc prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
293 1.50 jdc int smaxy, int smaxx)
294 1.50 jdc {
295 1.50 jdc int retval;
296 1.50 jdc
297 1.50 jdc #ifdef DEBUG
298 1.50 jdc __CTRACE("prefresh: pad %0.2o, flags 0x%08x\n", pad, pad->flags);
299 1.50 jdc #endif
300 1.50 jdc
301 1.50 jdc /* Use pnoutrefresh() to avoid duplicating code here */
302 1.50 jdc retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
303 1.50 jdc if (retval == OK) {
304 1.50 jdc retval = doupdate();
305 1.50 jdc if (!pad->flags & __LEAVEOK) {
306 1.50 jdc pad->cury = max(0, curscr->cury - pad->begy);
307 1.50 jdc pad->curx = max(0, curscr->curx - pad->begx);
308 1.50 jdc }
309 1.50 jdc }
310 1.23 jdc return(retval);
311 1.23 jdc }
312 1.23 jdc
313 1.23 jdc /*
314 1.23 jdc * doupdate --
315 1.23 jdc * Make the current screen look like the virtual window "__virtscr".
316 1.23 jdc */
317 1.23 jdc int
318 1.23 jdc doupdate(void)
319 1.23 jdc {
320 1.23 jdc WINDOW *win;
321 1.23 jdc __LINE *wlp;
322 1.23 jdc short wy;
323 1.23 jdc int dnum;
324 1.9 phil
325 1.9 phil /* Check if we need to restart ... */
326 1.43 blymn if (_cursesi_screen->endwin)
327 1.9 phil __restartwin();
328 1.7 cgd
329 1.43 blymn if (_cursesi_screen->curwin)
330 1.23 jdc win = curscr;
331 1.23 jdc else
332 1.44 blymn win = _cursesi_screen->__virtscr;
333 1.23 jdc
334 1.4 mycroft /* Initialize loop parameters. */
335 1.43 blymn _cursesi_screen->ly = curscr->cury;
336 1.43 blymn _cursesi_screen->lx = curscr->curx;
337 1.1 cgd wy = 0;
338 1.1 cgd
339 1.43 blymn if (!_cursesi_screen->curwin)
340 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
341 1.5 cgd wlp = win->lines[wy];
342 1.5 cgd if (wlp->flags & __ISDIRTY)
343 1.12 christos wlp->hash = __hash((char *)(void *)wlp->line,
344 1.48 blymn (size_t) (win->maxx * __LDATASIZE));
345 1.5 cgd }
346 1.5 cgd
347 1.43 blymn if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
348 1.43 blymn _cursesi_screen->curwin) {
349 1.23 jdc if (curscr->wattr & __COLOR)
350 1.23 jdc __unsetattr(0);
351 1.41 jdc tputs(__tc_cl, 0, __cputchar);
352 1.43 blymn _cursesi_screen->ly = 0;
353 1.43 blymn _cursesi_screen->lx = 0;
354 1.43 blymn if (!_cursesi_screen->curwin) {
355 1.23 jdc curscr->flags &= ~__CLEAROK;
356 1.23 jdc curscr->cury = 0;
357 1.23 jdc curscr->curx = 0;
358 1.23 jdc werase(curscr);
359 1.1 cgd }
360 1.23 jdc __touchwin(win);
361 1.5 cgd win->flags &= ~__CLEAROK;
362 1.1 cgd }
363 1.41 jdc if (!__CA) {
364 1.5 cgd if (win->curx != 0)
365 1.39 itojun __cputchar('\n');
366 1.43 blymn if (!_cursesi_screen->curwin)
367 1.1 cgd werase(curscr);
368 1.1 cgd }
369 1.4 mycroft #ifdef DEBUG
370 1.43 blymn __CTRACE("doupdate: (%0.2o): curwin = %d\n", win,
371 1.43 blymn _cursesi_screen->curwin);
372 1.23 jdc __CTRACE("doupdate: \tfirstch\tlastch\n");
373 1.5 cgd #endif
374 1.5 cgd
375 1.43 blymn if (!_cursesi_screen->curwin) {
376 1.5 cgd /*
377 1.5 cgd * Invoke quickch() only if more than a quarter of the lines
378 1.5 cgd * in the window are dirty.
379 1.5 cgd */
380 1.5 cgd for (wy = 0, dnum = 0; wy < win->maxy; wy++)
381 1.35 mycroft if (win->lines[wy]->flags & __ISDIRTY)
382 1.5 cgd dnum++;
383 1.5 cgd if (!__noqch && dnum > (int) win->maxy / 4)
384 1.23 jdc quickch();
385 1.5 cgd }
386 1.5 cgd
387 1.5 cgd #ifdef DEBUG
388 1.11 mrg {
389 1.11 mrg int i, j;
390 1.11 mrg
391 1.5 cgd __CTRACE("#####################################\n");
392 1.5 cgd for (i = 0; i < curscr->maxy; i++) {
393 1.5 cgd __CTRACE("C: %d:", i);
394 1.5 cgd __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
395 1.7 cgd for (j = 0; j < curscr->maxx; j++)
396 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
397 1.5 cgd __CTRACE("\n");
398 1.14 simonb __CTRACE(" attr:");
399 1.7 cgd for (j = 0; j < curscr->maxx; j++)
400 1.43 blymn __CTRACE(" %x",
401 1.43 blymn curscr->lines[i]->line[j].attr);
402 1.5 cgd __CTRACE("\n");
403 1.5 cgd __CTRACE("W: %d:", i);
404 1.23 jdc __CTRACE(" 0x%x \n", win->lines[i]->hash);
405 1.23 jdc __CTRACE(" 0x%x ", win->lines[i]->flags);
406 1.23 jdc for (j = 0; j < win->maxx; j++)
407 1.23 jdc __CTRACE("%c", win->lines[i]->line[j].ch);
408 1.23 jdc __CTRACE("\n");
409 1.23 jdc __CTRACE(" attr:");
410 1.23 jdc for (j = 0; j < win->maxx; j++)
411 1.23 jdc __CTRACE(" %x",
412 1.23 jdc win->lines[i]->line[j].attr);
413 1.23 jdc __CTRACE("\n");
414 1.5 cgd }
415 1.11 mrg }
416 1.11 mrg #endif /* DEBUG */
417 1.5 cgd
418 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
419 1.33 mycroft wlp = win->lines[wy];
420 1.23 jdc /* XXX: remove this debug */
421 1.4 mycroft #ifdef DEBUG
422 1.23 jdc __CTRACE("doupdate: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
423 1.33 mycroft *wlp->firstchp, *wlp->lastchp, wlp->flags);
424 1.4 mycroft #endif
425 1.43 blymn if (!_cursesi_screen->curwin)
426 1.33 mycroft curscr->lines[wy]->hash = wlp->hash;
427 1.35 mycroft if (wlp->flags & __ISDIRTY) {
428 1.23 jdc if (makech(wy) == ERR)
429 1.4 mycroft return (ERR);
430 1.1 cgd else {
431 1.33 mycroft if (*wlp->firstchp >= 0)
432 1.33 mycroft *wlp->firstchp = win->maxx;
433 1.33 mycroft if (*wlp->lastchp < win->maxx)
434 1.33 mycroft *wlp->lastchp = 0;
435 1.33 mycroft if (*wlp->lastchp < *wlp->firstchp) {
436 1.5 cgd #ifdef DEBUG
437 1.23 jdc __CTRACE("doupdate: line %d notdirty\n", wy);
438 1.5 cgd #endif
439 1.33 mycroft wlp->flags &= ~__ISDIRTY;
440 1.5 cgd }
441 1.1 cgd }
442 1.5 cgd
443 1.5 cgd }
444 1.4 mycroft #ifdef DEBUG
445 1.33 mycroft __CTRACE("\t%d\t%d\n", *wlp->firstchp, *wlp->lastchp);
446 1.4 mycroft #endif
447 1.1 cgd }
448 1.7 cgd
449 1.5 cgd #ifdef DEBUG
450 1.43 blymn __CTRACE("doupdate: ly=%d, lx=%d\n", _cursesi_screen->ly,
451 1.43 blymn _cursesi_screen->lx);
452 1.5 cgd #endif
453 1.1 cgd
454 1.43 blymn if (_cursesi_screen->curwin)
455 1.43 blymn domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
456 1.43 blymn (int) win->cury, (int) win->curx);
457 1.1 cgd else {
458 1.5 cgd if (win->flags & __LEAVEOK) {
459 1.43 blymn curscr->cury = _cursesi_screen->ly;
460 1.43 blymn curscr->curx = _cursesi_screen->lx;
461 1.4 mycroft } else {
462 1.43 blymn domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
463 1.43 blymn win->cury, win->curx);
464 1.23 jdc curscr->cury = win->cury;
465 1.23 jdc curscr->curx = win->curx;
466 1.1 cgd }
467 1.1 cgd }
468 1.23 jdc
469 1.28 mycroft /* Don't leave the screen with attributes set. */
470 1.28 mycroft __unsetattr(0);
471 1.43 blymn (void) fflush(_cursesi_screen->outfd);
472 1.28 mycroft return (OK);
473 1.1 cgd }
474 1.1 cgd
475 1.1 cgd /*
476 1.4 mycroft * makech --
477 1.4 mycroft * Make a change on the screen.
478 1.1 cgd */
479 1.4 mycroft static int
480 1.23 jdc makech(wy)
481 1.11 mrg int wy;
482 1.1 cgd {
483 1.23 jdc WINDOW *win;
484 1.34 mycroft static __LDATA blank = {' ', 0, ' ', 0};
485 1.7 cgd __LDATA *nsp, *csp, *cp, *cep;
486 1.11 mrg int clsp, nlsp; /* Last space in lines. */
487 1.23 jdc int lch, wx;
488 1.35 mycroft char *ce;
489 1.23 jdc attr_t lspc; /* Last space colour */
490 1.27 mycroft attr_t off, on;
491 1.5 cgd
492 1.8 mikel #ifdef __GNUC__
493 1.23 jdc nlsp = lspc = 0; /* XXX gcc -Wuninitialized */
494 1.8 mikel #endif
495 1.43 blymn if (_cursesi_screen->curwin)
496 1.23 jdc win = curscr;
497 1.23 jdc else
498 1.23 jdc win = __virtscr;
499 1.5 cgd /* Is the cursor still on the end of the last line? */
500 1.40 itojun if (wy > 0 && curscr->lines[wy - 1]->flags & __ISPASTEOL) {
501 1.43 blymn domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
502 1.43 blymn _cursesi_screen->ly + 1, 0);
503 1.43 blymn _cursesi_screen->ly++;
504 1.43 blymn _cursesi_screen->lx = 0;
505 1.5 cgd }
506 1.23 jdc wx = *win->lines[wy]->firstchp;
507 1.5 cgd if (wx < 0)
508 1.5 cgd wx = 0;
509 1.11 mrg else
510 1.11 mrg if (wx >= win->maxx)
511 1.11 mrg return (OK);
512 1.23 jdc lch = *win->lines[wy]->lastchp;
513 1.1 cgd if (lch < 0)
514 1.4 mycroft return (OK);
515 1.11 mrg else
516 1.11 mrg if (lch >= (int) win->maxx)
517 1.11 mrg lch = win->maxx - 1;
518 1.1 cgd
519 1.43 blymn if (_cursesi_screen->curwin)
520 1.5 cgd csp = ␣
521 1.1 cgd else
522 1.23 jdc csp = &curscr->lines[wy]->line[wx];
523 1.1 cgd
524 1.5 cgd nsp = &win->lines[wy]->line[wx];
525 1.43 blymn if (__tc_ce && !_cursesi_screen->curwin) {
526 1.20 jdc cp = &win->lines[wy]->line[win->maxx - 1];
527 1.23 jdc lspc = cp->attr & __COLOR;
528 1.23 jdc while (cp->ch == ' ' && cp->attr == lspc)
529 1.20 jdc if (cp-- <= win->lines[wy]->line)
530 1.1 cgd break;
531 1.5 cgd nlsp = cp - win->lines[wy]->line;
532 1.42 blymn if (nlsp < 0)
533 1.42 blymn nlsp = 0;
534 1.1 cgd }
535 1.43 blymn if (!_cursesi_screen->curwin)
536 1.41 jdc ce = __tc_ce;
537 1.1 cgd else
538 1.4 mycroft ce = NULL;
539 1.1 cgd
540 1.1 cgd while (wx <= lch) {
541 1.35 mycroft if (memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
542 1.4 mycroft if (wx <= lch) {
543 1.5 cgd while (wx <= lch &&
544 1.7 cgd memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
545 1.7 cgd nsp++;
546 1.43 blymn if (!_cursesi_screen->curwin)
547 1.7 cgd ++csp;
548 1.7 cgd ++wx;
549 1.7 cgd }
550 1.4 mycroft continue;
551 1.4 mycroft }
552 1.4 mycroft break;
553 1.4 mycroft }
554 1.43 blymn domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
555 1.5 cgd
556 1.4 mycroft #ifdef DEBUG
557 1.35 mycroft __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d\n",
558 1.43 blymn wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
559 1.4 mycroft #endif
560 1.43 blymn _cursesi_screen->ly = wy;
561 1.43 blymn _cursesi_screen->lx = wx;
562 1.35 mycroft while (memcmp(nsp, csp, sizeof(__LDATA)) != 0 && wx <= lch) {
563 1.7 cgd if (ce != NULL &&
564 1.23 jdc wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
565 1.4 mycroft /* Check for clear to end-of-line. */
566 1.5 cgd cep = &curscr->lines[wy]->line[win->maxx - 1];
567 1.23 jdc while (cep->ch == ' ' && cep->attr == lspc)
568 1.5 cgd if (cep-- <= csp)
569 1.4 mycroft break;
570 1.7 cgd clsp = cep - curscr->lines[wy]->line -
571 1.11 mrg win->begx * __LDATASIZE;
572 1.4 mycroft #ifdef DEBUG
573 1.20 jdc __CTRACE("makech: clsp = %d, nlsp = %d\n",
574 1.20 jdc clsp, nlsp);
575 1.4 mycroft #endif
576 1.41 jdc if (((clsp - nlsp >= strlen(__tc_ce) &&
577 1.20 jdc clsp < win->maxx * __LDATASIZE) ||
578 1.20 jdc wy == win->maxy - 1) &&
579 1.23 jdc (!(lspc & __COLOR) ||
580 1.41 jdc ((lspc & __COLOR) && __tc_ut))) {
581 1.23 jdc __unsetattr(0);
582 1.47 jdc if (__using_color &&
583 1.47 jdc ((lspc & __COLOR) !=
584 1.47 jdc (curscr->wattr & __COLOR)))
585 1.47 jdc __set_color(curscr, lspc &
586 1.47 jdc __COLOR);
587 1.41 jdc tputs(__tc_ce, 0, __cputchar);
588 1.43 blymn _cursesi_screen->lx = wx + win->begx;
589 1.5 cgd while (wx++ <= clsp) {
590 1.5 cgd csp->ch = ' ';
591 1.23 jdc csp->attr = lspc;
592 1.5 cgd csp++;
593 1.5 cgd }
594 1.4 mycroft return (OK);
595 1.1 cgd }
596 1.4 mycroft ce = NULL;
597 1.4 mycroft }
598 1.4 mycroft
599 1.47 jdc #ifdef DEBUG
600 1.47 jdc __CTRACE("makech: have attributes %08x, need attributes %08x\n", curscr->wattr, nsp->attr);
601 1.47 jdc #endif
602 1.27 mycroft
603 1.27 mycroft off = ~nsp->attr & curscr->wattr;
604 1.16 jdc
605 1.16 jdc /*
606 1.11 mrg * Unset attributes as appropriate. Unset first
607 1.11 mrg * so that the relevant attributes can be reset
608 1.16 jdc * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
609 1.16 jdc * 'mp' and 'mr'). Check to see if we also turn off
610 1.27 mycroft * standout, attributes and colour.
611 1.11 mrg */
612 1.41 jdc if (off & __TERMATTR && __tc_me != NULL) {
613 1.41 jdc tputs(__tc_me, 0, __cputchar);
614 1.41 jdc curscr->wattr &= __mask_me;
615 1.41 jdc off &= __mask_me;
616 1.11 mrg }
617 1.11 mrg
618 1.11 mrg /*
619 1.11 mrg * Exit underscore mode if appropriate.
620 1.27 mycroft * Check to see if we also turn off standout,
621 1.27 mycroft * attributes and colour.
622 1.11 mrg */
623 1.41 jdc if (off & __UNDERSCORE && __tc_ue != NULL) {
624 1.41 jdc tputs(__tc_ue, 0, __cputchar);
625 1.41 jdc curscr->wattr &= __mask_ue;
626 1.41 jdc off &= __mask_ue;
627 1.11 mrg }
628 1.11 mrg
629 1.11 mrg /*
630 1.27 mycroft * Exit standout mode as appropriate.
631 1.27 mycroft * Check to see if we also turn off underscore,
632 1.27 mycroft * attributes and colour.
633 1.7 cgd * XXX
634 1.41 jdc * Should use uc if so/se not available.
635 1.7 cgd */
636 1.41 jdc if (off & __STANDOUT && __tc_se != NULL) {
637 1.41 jdc tputs(__tc_se, 0, __cputchar);
638 1.41 jdc curscr->wattr &= __mask_se;
639 1.41 jdc off &= __mask_se;
640 1.27 mycroft }
641 1.27 mycroft
642 1.41 jdc if (off & __ALTCHARSET && __tc_ae != NULL) {
643 1.41 jdc tputs(__tc_ae, 0, __cputchar);
644 1.27 mycroft curscr->wattr &= ~__ALTCHARSET;
645 1.27 mycroft }
646 1.27 mycroft
647 1.47 jdc /* Set/change colour as appropriate. */
648 1.47 jdc if (__using_color)
649 1.47 jdc __set_color(curscr, nsp->attr & __COLOR);
650 1.47 jdc
651 1.27 mycroft on = nsp->attr & ~curscr->wattr;
652 1.27 mycroft
653 1.27 mycroft /*
654 1.27 mycroft * Enter standout mode if appropriate.
655 1.27 mycroft */
656 1.41 jdc if (on & __STANDOUT && __tc_so != NULL && __tc_se
657 1.41 jdc != NULL) {
658 1.41 jdc tputs(__tc_so, 0, __cputchar);
659 1.27 mycroft curscr->wattr |= __STANDOUT;
660 1.11 mrg }
661 1.11 mrg
662 1.11 mrg /*
663 1.11 mrg * Enter underscore mode if appropriate.
664 1.11 mrg * XXX
665 1.41 jdc * Should use uc if us/ue not available.
666 1.11 mrg */
667 1.41 jdc if (on & __UNDERSCORE && __tc_us != NULL &&
668 1.41 jdc __tc_ue != NULL) {
669 1.41 jdc tputs(__tc_us, 0, __cputchar);
670 1.15 blymn curscr->wattr |= __UNDERSCORE;
671 1.11 mrg }
672 1.11 mrg
673 1.11 mrg /*
674 1.11 mrg * Set other attributes as appropriate.
675 1.11 mrg */
676 1.41 jdc if (__tc_me != NULL) {
677 1.41 jdc if (on & __BLINK && __tc_mb != NULL) {
678 1.41 jdc tputs(__tc_mb, 0, __cputchar);
679 1.27 mycroft curscr->wattr |= __BLINK;
680 1.27 mycroft }
681 1.41 jdc if (on & __BOLD && __tc_md != NULL) {
682 1.41 jdc tputs(__tc_md, 0, __cputchar);
683 1.27 mycroft curscr->wattr |= __BOLD;
684 1.27 mycroft }
685 1.41 jdc if (on & __DIM && __tc_mh != NULL) {
686 1.41 jdc tputs(__tc_mh, 0, __cputchar);
687 1.27 mycroft curscr->wattr |= __DIM;
688 1.27 mycroft }
689 1.41 jdc if (on & __BLANK && __tc_mk != NULL) {
690 1.41 jdc tputs(__tc_mk, 0, __cputchar);
691 1.27 mycroft curscr->wattr |= __BLANK;
692 1.27 mycroft }
693 1.41 jdc if (on & __PROTECT && __tc_mp != NULL) {
694 1.41 jdc tputs(__tc_mp, 0, __cputchar);
695 1.27 mycroft curscr->wattr |= __PROTECT;
696 1.27 mycroft }
697 1.41 jdc if (on & __REVERSE && __tc_mr != NULL) {
698 1.41 jdc tputs(__tc_mr, 0, __cputchar);
699 1.27 mycroft curscr->wattr |= __REVERSE;
700 1.27 mycroft }
701 1.15 blymn }
702 1.15 blymn
703 1.15 blymn /* Enter/exit altcharset mode as appropriate. */
704 1.41 jdc if (on & __ALTCHARSET && __tc_as != NULL &&
705 1.41 jdc __tc_ae != NULL) {
706 1.41 jdc tputs(__tc_as, 0, __cputchar);
707 1.27 mycroft curscr->wattr |= __ALTCHARSET;
708 1.11 mrg }
709 1.4 mycroft
710 1.4 mycroft wx++;
711 1.23 jdc if (wx >= win->maxx &&
712 1.43 blymn wy == win->maxy - 1 && !_cursesi_screen->curwin)
713 1.5 cgd if (win->flags & __SCROLLOK) {
714 1.28 mycroft if (win->flags & __ENDLINE)
715 1.23 jdc __unsetattr(1);
716 1.5 cgd if (!(win->flags & __SCROLLWIN)) {
717 1.43 blymn if (!_cursesi_screen->curwin) {
718 1.23 jdc csp->attr = nsp->attr;
719 1.39 itojun __cputchar((int)
720 1.23 jdc (csp->ch =
721 1.23 jdc nsp->ch));
722 1.5 cgd } else
723 1.39 itojun __cputchar((int) nsp->ch);
724 1.5 cgd }
725 1.23 jdc if (wx < curscr->maxx) {
726 1.43 blymn domvcur(_cursesi_screen->ly, wx,
727 1.23 jdc (int) (win->maxy - 1),
728 1.23 jdc (int) (win->maxx - 1));
729 1.5 cgd }
730 1.43 blymn _cursesi_screen->ly = win->maxy - 1;
731 1.43 blymn _cursesi_screen->lx = win->maxx - 1;
732 1.4 mycroft return (OK);
733 1.7 cgd }
734 1.7 cgd if (wx < win->maxx || wy < win->maxy - 1 ||
735 1.5 cgd !(win->flags & __SCROLLWIN)) {
736 1.43 blymn if (!_cursesi_screen->curwin) {
737 1.23 jdc csp->attr = nsp->attr;
738 1.39 itojun __cputchar((int) (csp->ch = nsp->ch));
739 1.5 cgd csp++;
740 1.7 cgd } else
741 1.39 itojun __cputchar((int) nsp->ch);
742 1.5 cgd }
743 1.4 mycroft #ifdef DEBUG
744 1.5 cgd __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
745 1.4 mycroft #endif
746 1.41 jdc if (__tc_uc && ((nsp->attr & __STANDOUT) ||
747 1.11 mrg (nsp->attr & __UNDERSCORE))) {
748 1.39 itojun __cputchar('\b');
749 1.41 jdc tputs(__tc_uc, 0, __cputchar);
750 1.1 cgd }
751 1.4 mycroft nsp++;
752 1.4 mycroft #ifdef DEBUG
753 1.43 blymn __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, _cursesi_screen->lx);
754 1.4 mycroft #endif
755 1.5 cgd }
756 1.43 blymn if (_cursesi_screen->lx == wx) /* If no change. */
757 1.4 mycroft break;
758 1.43 blymn _cursesi_screen->lx = wx;
759 1.43 blymn if (_cursesi_screen->lx >= COLS && __tc_am)
760 1.43 blymn _cursesi_screen->lx = COLS - 1;
761 1.11 mrg else
762 1.11 mrg if (wx >= win->maxx) {
763 1.43 blymn domvcur(_cursesi_screen->ly,
764 1.43 blymn _cursesi_screen->lx,
765 1.43 blymn _cursesi_screen->ly,
766 1.43 blymn (int) (win->maxx - 1));
767 1.43 blymn _cursesi_screen->lx = win->maxx - 1;
768 1.11 mrg }
769 1.4 mycroft #ifdef DEBUG
770 1.43 blymn __CTRACE("makech: 3: wx = %d, lx = %d\n", wx,
771 1.43 blymn _cursesi_screen->lx);
772 1.4 mycroft #endif
773 1.1 cgd }
774 1.7 cgd
775 1.4 mycroft return (OK);
776 1.1 cgd }
777 1.1 cgd
778 1.1 cgd /*
779 1.4 mycroft * domvcur --
780 1.19 jdc * Do a mvcur, leaving attributes if necessary.
781 1.1 cgd */
782 1.4 mycroft static void
783 1.1 cgd domvcur(oy, ox, ny, nx)
784 1.11 mrg int oy, ox, ny, nx;
785 1.4 mycroft {
786 1.23 jdc __unsetattr(1);
787 1.5 cgd __mvcur(oy, ox, ny, nx, 1);
788 1.5 cgd }
789 1.5 cgd
790 1.5 cgd /*
791 1.5 cgd * Quickch() attempts to detect a pattern in the change of the window
792 1.7 cgd * in order to optimize the change, e.g., scroll n lines as opposed to
793 1.5 cgd * repainting the screen line by line.
794 1.5 cgd */
795 1.5 cgd
796 1.46 christos static __LDATA buf[128];
797 1.46 christos static u_int last_hash;
798 1.46 christos static size_t last_hash_len;
799 1.46 christos #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
800 1.46 christos
801 1.5 cgd static void
802 1.23 jdc quickch(void)
803 1.5 cgd {
804 1.23 jdc #define THRESH (int) __virtscr->maxy / 4
805 1.5 cgd
806 1.10 perry __LINE *clp, *tmp1, *tmp2;
807 1.11 mrg int bsize, curs, curw, starts, startw, i, j;
808 1.11 mrg int n, target, cur_period, bot, top, sc_region;
809 1.11 mrg u_int blank_hash;
810 1.22 jdc attr_t bcolor;
811 1.5 cgd
812 1.8 mikel #ifdef __GNUC__
813 1.11 mrg curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
814 1.8 mikel #endif
815 1.7 cgd /*
816 1.5 cgd * Find how many lines from the top of the screen are unchanged.
817 1.5 cgd */
818 1.23 jdc for (top = 0; top < __virtscr->maxy; top++)
819 1.35 mycroft if (__virtscr->lines[top]->flags & __ISDIRTY &&
820 1.35 mycroft (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
821 1.35 mycroft memcmp(__virtscr->lines[top]->line,
822 1.32 mycroft curscr->lines[top]->line,
823 1.35 mycroft (size_t) __virtscr->maxx * __LDATASIZE) != 0))
824 1.5 cgd break;
825 1.5 cgd else
826 1.23 jdc __virtscr->lines[top]->flags &= ~__ISDIRTY;
827 1.11 mrg /*
828 1.11 mrg * Find how many lines from bottom of screen are unchanged.
829 1.11 mrg */
830 1.23 jdc for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
831 1.35 mycroft if (__virtscr->lines[bot]->flags & __ISDIRTY &&
832 1.35 mycroft (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
833 1.35 mycroft memcmp(__virtscr->lines[bot]->line,
834 1.32 mycroft curscr->lines[bot]->line,
835 1.35 mycroft (size_t) __virtscr->maxx * __LDATASIZE) != 0))
836 1.5 cgd break;
837 1.5 cgd else
838 1.23 jdc __virtscr->lines[bot]->flags &= ~__ISDIRTY;
839 1.23 jdc
840 1.23 jdc /*
841 1.23 jdc * Work round an xterm bug where inserting lines causes all the
842 1.23 jdc * inserted lines to be covered with the background colour we
843 1.23 jdc * set on the first line (even if we unset it for subsequent
844 1.23 jdc * lines).
845 1.23 jdc */
846 1.23 jdc bcolor = __virtscr->lines[min(top,
847 1.23 jdc __virtscr->maxy - 1)]->line[0].attr & __COLOR;
848 1.23 jdc for (i = top + 1, j = 0; i < bot; i++) {
849 1.23 jdc if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
850 1.23 jdc bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
851 1.23 jdc attr & __COLOR;
852 1.23 jdc j = i - top;
853 1.23 jdc } else
854 1.23 jdc break;
855 1.23 jdc }
856 1.23 jdc top += j;
857 1.5 cgd
858 1.5 cgd #ifdef NO_JERKINESS
859 1.5 cgd /*
860 1.5 cgd * If we have a bottom unchanged region return. Scrolling the
861 1.5 cgd * bottom region up and then back down causes a screen jitter.
862 1.5 cgd * This will increase the number of characters sent to the screen
863 1.5 cgd * but it looks better.
864 1.5 cgd */
865 1.23 jdc if (bot < __virtscr->maxy - 1)
866 1.5 cgd return;
867 1.11 mrg #endif /* NO_JERKINESS */
868 1.5 cgd
869 1.5 cgd /*
870 1.5 cgd * Search for the largest block of text not changed.
871 1.5 cgd * Invariants of the loop:
872 1.23 jdc * - Startw is the index of the beginning of the examined block in
873 1.23 jdc * __virtscr.
874 1.11 mrg * - Starts is the index of the beginning of the examined block in
875 1.23 jdc * curscr.
876 1.7 cgd * - Curw is the index of one past the end of the exmined block in
877 1.23 jdc * __virtscr.
878 1.23 jdc * - Curs is the index of one past the end of the exmined block in
879 1.5 cgd * curscr.
880 1.5 cgd * - bsize is the current size of the examined block.
881 1.11 mrg */
882 1.23 jdc
883 1.5 cgd for (bsize = bot - top; bsize >= THRESH; bsize--) {
884 1.5 cgd for (startw = top; startw <= bot - bsize; startw++)
885 1.7 cgd for (starts = top; starts <= bot - bsize;
886 1.11 mrg starts++) {
887 1.5 cgd for (curw = startw, curs = starts;
888 1.11 mrg curs < starts + bsize; curw++, curs++)
889 1.35 mycroft if (__virtscr->lines[curw]->hash !=
890 1.35 mycroft curscr->lines[curs]->hash)
891 1.32 mycroft break;
892 1.32 mycroft if (curs != starts + bsize)
893 1.32 mycroft continue;
894 1.32 mycroft for (curw = startw, curs = starts;
895 1.32 mycroft curs < starts + bsize; curw++, curs++)
896 1.32 mycroft if (memcmp(__virtscr->lines[curw]->line,
897 1.32 mycroft curscr->lines[curs]->line,
898 1.32 mycroft (size_t) __virtscr->maxx *
899 1.32 mycroft __LDATASIZE) != 0)
900 1.5 cgd break;
901 1.5 cgd if (curs == starts + bsize)
902 1.5 cgd goto done;
903 1.5 cgd }
904 1.5 cgd }
905 1.11 mrg done:
906 1.22 jdc
907 1.5 cgd /* Did not find anything */
908 1.7 cgd if (bsize < THRESH)
909 1.5 cgd return;
910 1.5 cgd
911 1.5 cgd #ifdef DEBUG
912 1.7 cgd __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
913 1.11 mrg bsize, starts, startw, curw, curs, top, bot);
914 1.5 cgd #endif
915 1.5 cgd
916 1.7 cgd /*
917 1.7 cgd * Make sure that there is no overlap between the bottom and top
918 1.5 cgd * regions and the middle scrolled block.
919 1.5 cgd */
920 1.5 cgd if (bot < curs)
921 1.5 cgd bot = curs - 1;
922 1.5 cgd if (top > starts)
923 1.5 cgd top = starts;
924 1.5 cgd
925 1.5 cgd n = startw - starts;
926 1.5 cgd
927 1.5 cgd #ifdef DEBUG
928 1.11 mrg __CTRACE("#####################################\n");
929 1.11 mrg for (i = 0; i < curscr->maxy; i++) {
930 1.11 mrg __CTRACE("C: %d:", i);
931 1.11 mrg __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
932 1.11 mrg for (j = 0; j < curscr->maxx; j++)
933 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
934 1.11 mrg __CTRACE("\n");
935 1.14 simonb __CTRACE(" attr:");
936 1.11 mrg for (j = 0; j < curscr->maxx; j++)
937 1.14 simonb __CTRACE(" %x", curscr->lines[i]->line[j].attr);
938 1.11 mrg __CTRACE("\n");
939 1.11 mrg __CTRACE("W: %d:", i);
940 1.23 jdc __CTRACE(" 0x%x \n", __virtscr->lines[i]->hash);
941 1.23 jdc __CTRACE(" 0x%x ", __virtscr->lines[i]->flags);
942 1.23 jdc for (j = 0; j < __virtscr->maxx; j++)
943 1.23 jdc __CTRACE("%c", __virtscr->lines[i]->line[j].ch);
944 1.11 mrg __CTRACE("\n");
945 1.14 simonb __CTRACE(" attr:");
946 1.23 jdc for (j = 0; j < __virtscr->maxx; j++)
947 1.23 jdc __CTRACE(" %x", __virtscr->lines[i]->line[j].attr);
948 1.11 mrg __CTRACE("\n");
949 1.11 mrg }
950 1.7 cgd #endif
951 1.7 cgd
952 1.46 christos if (buf[0].ch != ' ') {
953 1.46 christos for (i = 0; i < BLANKSIZE; i++) {
954 1.46 christos buf[i].ch = ' ';
955 1.46 christos buf[i].bch = ' ';
956 1.46 christos buf[i].attr = 0;
957 1.46 christos buf[i].battr = 0;
958 1.46 christos }
959 1.5 cgd }
960 1.46 christos
961 1.46 christos if (__virtscr->maxx != last_hash_len) {
962 1.46 christos blank_hash = 0;
963 1.46 christos for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
964 1.46 christos blank_hash = __hash_more((char *)(void *)buf, sizeof(buf),
965 1.46 christos blank_hash);
966 1.46 christos }
967 1.46 christos blank_hash = __hash_more((char *)(void *)buf,
968 1.46 christos i * sizeof(buf[0]), blank_hash);
969 1.46 christos /* cache result in static data - screen width doesn't change often */
970 1.46 christos last_hash_len = __virtscr->maxx;
971 1.46 christos last_hash = blank_hash;
972 1.46 christos } else
973 1.46 christos blank_hash = last_hash;
974 1.5 cgd
975 1.5 cgd /*
976 1.5 cgd * Perform the rotation to maintain the consistency of curscr.
977 1.5 cgd * This is hairy since we are doing an *in place* rotation.
978 1.5 cgd * Invariants of the loop:
979 1.5 cgd * - I is the index of the current line.
980 1.5 cgd * - Target is the index of the target of line i.
981 1.5 cgd * - Tmp1 points to current line (i).
982 1.5 cgd * - Tmp2 and points to target line (target);
983 1.7 cgd * - Cur_period is the index of the end of the current period.
984 1.5 cgd * (see below).
985 1.5 cgd *
986 1.5 cgd * There are 2 major issues here that make this rotation non-trivial:
987 1.5 cgd * 1. Scrolling in a scrolling region bounded by the top
988 1.5 cgd * and bottom regions determined (whose size is sc_region).
989 1.7 cgd * 2. As a result of the use of the mod function, there may be a
990 1.5 cgd * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
991 1.5 cgd * 0 to 2, which then causes all odd lines not to be rotated.
992 1.7 cgd * To remedy this, an index of the end ( = beginning) of the
993 1.7 cgd * current 'period' is kept, cur_period, and when it is reached,
994 1.7 cgd * the next period is started from cur_period + 1 which is
995 1.5 cgd * guaranteed not to have been reached since that would mean that
996 1.5 cgd * all records would have been reached. (think about it...).
997 1.7 cgd *
998 1.5 cgd * Lines in the rotation can have 3 attributes which are marked on the
999 1.5 cgd * line so that curscr is consistent with the visual screen.
1000 1.5 cgd * 1. Not dirty -- lines inside the scrolled block, top region or
1001 1.5 cgd * bottom region.
1002 1.7 cgd * 2. Blank lines -- lines in the differential of the scrolling
1003 1.7 cgd * region adjacent to top and bot regions
1004 1.5 cgd * depending on scrolling direction.
1005 1.5 cgd * 3. Dirty line -- all other lines are marked dirty.
1006 1.5 cgd */
1007 1.5 cgd sc_region = bot - top + 1;
1008 1.5 cgd i = top;
1009 1.5 cgd tmp1 = curscr->lines[top];
1010 1.5 cgd cur_period = top;
1011 1.5 cgd for (j = top; j <= bot; j++) {
1012 1.5 cgd target = (i - top + n + sc_region) % sc_region + top;
1013 1.5 cgd tmp2 = curscr->lines[target];
1014 1.5 cgd curscr->lines[target] = tmp1;
1015 1.5 cgd /* Mark block as clean and blank out scrolled lines. */
1016 1.5 cgd clp = curscr->lines[target];
1017 1.5 cgd #ifdef DEBUG
1018 1.5 cgd __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
1019 1.11 mrg n, startw, curw, i, target);
1020 1.5 cgd #endif
1021 1.7 cgd if ((target >= startw && target < curw) || target < top
1022 1.5 cgd || target > bot) {
1023 1.5 cgd #ifdef DEBUG
1024 1.22 jdc __CTRACE("-- notdirty\n");
1025 1.5 cgd #endif
1026 1.23 jdc __virtscr->lines[target]->flags &= ~__ISDIRTY;
1027 1.11 mrg } else
1028 1.11 mrg if ((n > 0 && target >= top && target < top + n) ||
1029 1.11 mrg (n < 0 && target <= bot && target > bot + n)) {
1030 1.11 mrg if (clp->hash != blank_hash || memcmp(clp->line,
1031 1.46 christos clp->line + 1, (__virtscr->maxx - 1) *
1032 1.46 christos __LDATASIZE) || memcmp(clp->line, buf,
1033 1.46 christos __LDATASIZE)) {
1034 1.46 christos for (i = __virtscr->maxx; i > BLANKSIZE;
1035 1.46 christos i -= BLANKSIZE) {
1036 1.46 christos (void)memcpy(clp->line + i -
1037 1.46 christos BLANKSIZE, buf, sizeof(buf));
1038 1.46 christos }
1039 1.46 christos (void)memcpy(clp->line , buf, i *
1040 1.46 christos sizeof(buf[0]));
1041 1.11 mrg #ifdef DEBUG
1042 1.22 jdc __CTRACE("-- blanked out: dirty\n");
1043 1.11 mrg #endif
1044 1.11 mrg clp->hash = blank_hash;
1045 1.35 mycroft __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1046 1.11 mrg } else {
1047 1.5 cgd #ifdef DEBUG
1048 1.22 jdc __CTRACE(" -- blank line already: dirty\n");
1049 1.5 cgd #endif
1050 1.35 mycroft __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1051 1.11 mrg }
1052 1.5 cgd } else {
1053 1.5 cgd #ifdef DEBUG
1054 1.22 jdc __CTRACE(" -- dirty\n");
1055 1.5 cgd #endif
1056 1.35 mycroft __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1057 1.5 cgd }
1058 1.5 cgd if (target == cur_period) {
1059 1.5 cgd i = target + 1;
1060 1.5 cgd tmp1 = curscr->lines[i];
1061 1.5 cgd cur_period = i;
1062 1.5 cgd } else {
1063 1.5 cgd tmp1 = tmp2;
1064 1.5 cgd i = target;
1065 1.5 cgd }
1066 1.5 cgd }
1067 1.5 cgd #ifdef DEBUG
1068 1.11 mrg __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1069 1.11 mrg for (i = 0; i < curscr->maxy; i++) {
1070 1.11 mrg __CTRACE("C: %d:", i);
1071 1.11 mrg for (j = 0; j < curscr->maxx; j++)
1072 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
1073 1.11 mrg __CTRACE("\n");
1074 1.11 mrg __CTRACE("W: %d:", i);
1075 1.23 jdc for (j = 0; j < __virtscr->maxx; j++)
1076 1.23 jdc __CTRACE("%c", __virtscr->lines[i]->line[j].ch);
1077 1.11 mrg __CTRACE("\n");
1078 1.11 mrg }
1079 1.5 cgd #endif
1080 1.23 jdc if (n != 0)
1081 1.23 jdc scrolln(starts, startw, curs, bot, top);
1082 1.5 cgd }
1083 1.5 cgd
1084 1.5 cgd /*
1085 1.7 cgd * scrolln --
1086 1.7 cgd * Scroll n lines, where n is starts - startw.
1087 1.5 cgd */
1088 1.11 mrg static void /* ARGSUSED */
1089 1.23 jdc scrolln(starts, startw, curs, bot, top)
1090 1.11 mrg int starts, startw, curs, bot, top;
1091 1.5 cgd {
1092 1.11 mrg int i, oy, ox, n;
1093 1.5 cgd
1094 1.5 cgd oy = curscr->cury;
1095 1.5 cgd ox = curscr->curx;
1096 1.5 cgd n = starts - startw;
1097 1.5 cgd
1098 1.7 cgd /*
1099 1.7 cgd * XXX
1100 1.7 cgd * The initial tests that set __noqch don't let us reach here unless
1101 1.41 jdc * we have either cs + ho + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
1102 1.7 cgd * scrolling can only shift the entire scrolling region, not just a
1103 1.7 cgd * part of it, which means that the quickch() routine is going to be
1104 1.41 jdc * sadly disappointed in us if we don't have cs as well.
1105 1.7 cgd *
1106 1.41 jdc * If cs, ho and SF/sf are set, can use the scrolling region. Because
1107 1.41 jdc * the cursor position after cs is undefined, we need ho which gives us
1108 1.7 cgd * the ability to move to somewhere without knowledge of the current
1109 1.7 cgd * location of the cursor. Still call __mvcur() anyway, to update its
1110 1.7 cgd * idea of where the cursor is.
1111 1.7 cgd *
1112 1.7 cgd * When the scrolling region has been set, the cursor has to be at the
1113 1.7 cgd * last line of the region to make the scroll happen.
1114 1.7 cgd *
1115 1.7 cgd * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1116 1.41 jdc * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
1117 1.7 cgd * SF/SR. So, if we're scrolling almost all of the screen, try and use
1118 1.7 cgd * AL/DL, otherwise use the scrolling region. The "almost all" is a
1119 1.7 cgd * shameless hack for vi.
1120 1.7 cgd */
1121 1.5 cgd if (n > 0) {
1122 1.41 jdc if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SF != NULL ||
1123 1.41 jdc ((__tc_AL == NULL || __tc_DL == NULL ||
1124 1.41 jdc top > 3 || bot + 3 < __virtscr->maxy) &&
1125 1.41 jdc __tc_sf != NULL))) {
1126 1.41 jdc tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
1127 1.7 cgd __mvcur(oy, ox, 0, 0, 1);
1128 1.41 jdc tputs(__tc_ho, 0, __cputchar);
1129 1.7 cgd __mvcur(0, 0, bot, 0, 1);
1130 1.41 jdc if (__tc_SF != NULL)
1131 1.41 jdc tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
1132 1.7 cgd else
1133 1.7 cgd for (i = 0; i < n; i++)
1134 1.41 jdc tputs(__tc_sf, 0, __cputchar);
1135 1.41 jdc tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
1136 1.41 jdc __cputchar);
1137 1.7 cgd __mvcur(bot, 0, 0, 0, 1);
1138 1.41 jdc tputs(__tc_ho, 0, __cputchar);
1139 1.7 cgd __mvcur(0, 0, oy, ox, 1);
1140 1.7 cgd return;
1141 1.7 cgd }
1142 1.7 cgd
1143 1.7 cgd /* Scroll up the block. */
1144 1.41 jdc if (__tc_SF != NULL && top == 0) {
1145 1.7 cgd __mvcur(oy, ox, bot, 0, 1);
1146 1.41 jdc tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
1147 1.7 cgd } else
1148 1.41 jdc if (__tc_DL != NULL) {
1149 1.11 mrg __mvcur(oy, ox, top, 0, 1);
1150 1.41 jdc tputs(__tscroll(__tc_DL, n, 0), 0, __cputchar);
1151 1.11 mrg } else
1152 1.41 jdc if (__tc_dl != NULL) {
1153 1.11 mrg __mvcur(oy, ox, top, 0, 1);
1154 1.11 mrg for (i = 0; i < n; i++)
1155 1.41 jdc tputs(__tc_dl, 0, __cputchar);
1156 1.11 mrg } else
1157 1.41 jdc if (__tc_sf != NULL && top == 0) {
1158 1.11 mrg __mvcur(oy, ox, bot, 0, 1);
1159 1.11 mrg for (i = 0; i < n; i++)
1160 1.41 jdc tputs(__tc_sf, 0,
1161 1.41 jdc __cputchar);
1162 1.11 mrg } else
1163 1.11 mrg abort();
1164 1.5 cgd
1165 1.7 cgd /* Push down the bottom region. */
1166 1.5 cgd __mvcur(top, 0, bot - n + 1, 0, 1);
1167 1.41 jdc if (__tc_AL != NULL)
1168 1.41 jdc tputs(__tscroll(__tc_AL, n, 0), 0, __cputchar);
1169 1.5 cgd else
1170 1.41 jdc if (__tc_al != NULL)
1171 1.11 mrg for (i = 0; i < n; i++)
1172 1.41 jdc tputs(__tc_al, 0, __cputchar);
1173 1.11 mrg else
1174 1.11 mrg abort();
1175 1.5 cgd __mvcur(bot - n + 1, 0, oy, ox, 1);
1176 1.5 cgd } else {
1177 1.7 cgd /*
1178 1.7 cgd * !!!
1179 1.7 cgd * n < 0
1180 1.7 cgd *
1181 1.41 jdc * If cs, ho and SR/sr are set, can use the scrolling region.
1182 1.7 cgd * See the above comments for details.
1183 1.7 cgd */
1184 1.41 jdc if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SR != NULL ||
1185 1.41 jdc ((__tc_AL == NULL || __tc_DL == NULL || top > 3 ||
1186 1.41 jdc bot + 3 < __virtscr->maxy) && __tc_sr != NULL))) {
1187 1.41 jdc tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
1188 1.7 cgd __mvcur(oy, ox, 0, 0, 1);
1189 1.41 jdc tputs(__tc_ho, 0, __cputchar);
1190 1.7 cgd __mvcur(0, 0, top, 0, 1);
1191 1.7 cgd
1192 1.41 jdc if (__tc_SR != NULL)
1193 1.41 jdc tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
1194 1.7 cgd else
1195 1.7 cgd for (i = n; i < 0; i++)
1196 1.41 jdc tputs(__tc_sr, 0, __cputchar);
1197 1.41 jdc tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
1198 1.41 jdc __cputchar);
1199 1.7 cgd __mvcur(top, 0, 0, 0, 1);
1200 1.41 jdc tputs(__tc_ho, 0, __cputchar);
1201 1.7 cgd __mvcur(0, 0, oy, ox, 1);
1202 1.7 cgd return;
1203 1.7 cgd }
1204 1.7 cgd
1205 1.7 cgd /* Preserve the bottom lines. */
1206 1.7 cgd __mvcur(oy, ox, bot + n + 1, 0, 1);
1207 1.41 jdc if (__tc_SR != NULL && bot == __virtscr->maxy)
1208 1.41 jdc tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
1209 1.5 cgd else
1210 1.41 jdc if (__tc_DL != NULL)
1211 1.41 jdc tputs(__tscroll(__tc_DL, -n, 0), 0, __cputchar);
1212 1.11 mrg else
1213 1.41 jdc if (__tc_dl != NULL)
1214 1.11 mrg for (i = n; i < 0; i++)
1215 1.41 jdc tputs(__tc_dl, 0, __cputchar);
1216 1.11 mrg else
1217 1.41 jdc if (__tc_sr != NULL &&
1218 1.41 jdc bot == __virtscr->maxy)
1219 1.11 mrg for (i = n; i < 0; i++)
1220 1.41 jdc tputs(__tc_sr, 0,
1221 1.41 jdc __cputchar);
1222 1.11 mrg else
1223 1.11 mrg abort();
1224 1.7 cgd
1225 1.7 cgd /* Scroll the block down. */
1226 1.5 cgd __mvcur(bot + n + 1, 0, top, 0, 1);
1227 1.41 jdc if (__tc_AL != NULL)
1228 1.41 jdc tputs(__tscroll(__tc_AL, -n, 0), 0, __cputchar);
1229 1.5 cgd else
1230 1.41 jdc if (__tc_al != NULL)
1231 1.11 mrg for (i = n; i < 0; i++)
1232 1.41 jdc tputs(__tc_al, 0, __cputchar);
1233 1.11 mrg else
1234 1.11 mrg abort();
1235 1.5 cgd __mvcur(top, 0, oy, ox, 1);
1236 1.19 jdc }
1237 1.19 jdc }
1238 1.19 jdc
1239 1.19 jdc /*
1240 1.23 jdc * __unsetattr --
1241 1.19 jdc * Unset attributes on curscr. Leave standout, attribute and colour
1242 1.41 jdc * modes if necessary (!ms). Always leave altcharset (xterm at least
1243 1.19 jdc * ignores a cursor move if we don't).
1244 1.19 jdc */
1245 1.23 jdc void /* ARGSUSED */
1246 1.23 jdc __unsetattr(int checkms)
1247 1.19 jdc {
1248 1.19 jdc int isms;
1249 1.19 jdc
1250 1.19 jdc if (checkms)
1251 1.41 jdc if (!__tc_ms) {
1252 1.19 jdc isms = 1;
1253 1.19 jdc } else {
1254 1.19 jdc isms = 0;
1255 1.19 jdc }
1256 1.19 jdc else
1257 1.19 jdc isms = 1;
1258 1.20 jdc #ifdef DEBUG
1259 1.41 jdc __CTRACE("__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
1260 1.41 jdc checkms, __tc_ms ? "TRUE" : "FALSE", curscr->wattr);
1261 1.20 jdc #endif
1262 1.19 jdc
1263 1.20 jdc /*
1264 1.41 jdc * Don't leave the screen in standout mode (check against ms). Check
1265 1.27 mycroft * to see if we also turn off underscore, attributes and colour.
1266 1.20 jdc */
1267 1.19 jdc if (curscr->wattr & __STANDOUT && isms) {
1268 1.41 jdc tputs(__tc_se, 0, __cputchar);
1269 1.41 jdc curscr->wattr &= __mask_se;
1270 1.19 jdc }
1271 1.20 jdc /*
1272 1.41 jdc * Don't leave the screen in underscore mode (check against ms).
1273 1.26 jdc * Check to see if we also turn off attributes. Assume that we
1274 1.26 jdc * also turn off colour.
1275 1.20 jdc */
1276 1.19 jdc if (curscr->wattr & __UNDERSCORE && isms) {
1277 1.41 jdc tputs(__tc_ue, 0, __cputchar);
1278 1.41 jdc curscr->wattr &= __mask_ue;
1279 1.19 jdc }
1280 1.20 jdc /*
1281 1.41 jdc * Don't leave the screen with attributes set (check against ms).
1282 1.26 jdc * Assume that also turn off colour.
1283 1.20 jdc */
1284 1.20 jdc if (curscr->wattr & __TERMATTR && isms) {
1285 1.41 jdc tputs(__tc_me, 0, __cputchar);
1286 1.41 jdc curscr->wattr &= __mask_me;
1287 1.19 jdc }
1288 1.41 jdc /* Don't leave the screen with altcharset set (don't check ms). */
1289 1.19 jdc if (curscr->wattr & __ALTCHARSET) {
1290 1.41 jdc tputs(__tc_ae, 0, __cputchar);
1291 1.19 jdc curscr->wattr &= ~__ALTCHARSET;
1292 1.19 jdc }
1293 1.41 jdc /* Don't leave the screen with colour set (check against ms). */
1294 1.47 jdc if (__using_color && isms)
1295 1.47 jdc __unset_color(curscr);
1296 1.1 cgd }
1297