refresh.c revision 1.19 1 /* $NetBSD: refresh.c,v 1.19 2000/04/16 09:55:16 jdc 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)refresh.c 8.7 (Berkeley) 8/13/94";
40 #else
41 __RCSID("$NetBSD: refresh.c,v 1.19 2000/04/16 09:55:16 jdc Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <string.h>
46
47 #include "curses.h"
48 #include "curses_private.h"
49
50 static int curwin;
51 static short ly, lx;
52
53 static void domvcur __P((int, int, int, int));
54 static int makech __P((WINDOW *, int));
55 static void quickch __P((WINDOW *));
56 static void scrolln __P((WINDOW *, int, int, int, int, int));
57 static void unsetattr __P((int));
58
59 #ifndef _CURSES_USE_MACROS
60
61 /*
62 * refresh --
63 * Make the current screen look like "stdscr" over the area covered by
64 * stdscr.
65 */
66 int
67 refresh(void)
68 {
69 return wrefresh(stdscr);
70 }
71
72 #endif
73
74 /*
75 * wrefresh --
76 * Make the current screen look like "win" over the area coverd by
77 * win.
78 */
79 int
80 wrefresh(WINDOW *win)
81 {
82 __LINE *wlp;
83 int retval;
84 short wy;
85 int dnum;
86
87 /* Check if we need to restart ... */
88 if (__endwin) {
89 __endwin = 0;
90 __restartwin();
91 }
92
93 /* Initialize loop parameters. */
94 ly = curscr->cury;
95 lx = curscr->curx;
96 wy = 0;
97 curwin = (win == curscr);
98
99 if (!curwin)
100 for (wy = 0; wy < win->maxy; wy++) {
101 wlp = win->lines[wy];
102 if (wlp->flags & __ISDIRTY)
103 wlp->hash = __hash((char *)(void *)wlp->line,
104 (int) (win->maxx * __LDATASIZE));
105 }
106
107 if (win->flags & __CLEAROK || curscr->flags & __CLEAROK || curwin) {
108 if ((win->flags & __FULLWIN) || curscr->flags & __CLEAROK) {
109 /* XXX: check for background colour and bce here */
110 tputs(CL, 0, __cputchar);
111 ly = 0;
112 lx = 0;
113 if (!curwin) {
114 curscr->flags &= ~__CLEAROK;
115 curscr->cury = 0;
116 curscr->curx = 0;
117 werase(curscr);
118 }
119 __touchwin(win);
120 }
121 win->flags &= ~__CLEAROK;
122 }
123 if (!CA) {
124 if (win->curx != 0)
125 putchar('\n');
126 if (!curwin)
127 werase(curscr);
128 }
129 #ifdef DEBUG
130 __CTRACE("wrefresh: (%0.2o): curwin = %d\n", win, curwin);
131 __CTRACE("wrefresh: \tfirstch\tlastch\n");
132 #endif
133
134 if ((win->flags & __FULLWIN) && !curwin) {
135 /*
136 * Invoke quickch() only if more than a quarter of the lines
137 * in the window are dirty.
138 */
139 for (wy = 0, dnum = 0; wy < win->maxy; wy++)
140 if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT))
141 dnum++;
142 if (!__noqch && dnum > (int) win->maxy / 4)
143 quickch(win);
144 }
145
146 #ifdef DEBUG
147 {
148 int i, j;
149
150 __CTRACE("#####################################\n");
151 for (i = 0; i < curscr->maxy; i++) {
152 __CTRACE("C: %d:", i);
153 __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
154 for (j = 0; j < curscr->maxx; j++)
155 __CTRACE("%c", curscr->lines[i]->line[j].ch);
156 __CTRACE("\n");
157 __CTRACE(" attr:");
158 for (j = 0; j < curscr->maxx; j++)
159 __CTRACE(" %x", curscr->lines[i]->line[j].attr);
160 __CTRACE("\n");
161 __CTRACE("W: %d:", i);
162 /* Handle small windows */
163 if (i >= win->begy && i < (win->begy + win->maxy)) {
164 __CTRACE(" 0x%x \n",
165 win->lines[i - win->begy]->hash);
166 __CTRACE(" 0x%x ",
167 win->lines[i - win->begy]->flags);
168 for (j = 0; j < win->maxx; j++)
169 __CTRACE("%c", win->lines[i - win
170 ->begy]->line[j].ch);
171 __CTRACE("\n");
172 __CTRACE(" attr:");
173 for (j = 0; j < win->maxx; j++)
174 __CTRACE(" %x", win->lines[i - win
175 ->begy]->line[j].attr);
176 __CTRACE("\n");
177 }
178 }
179 }
180 #endif /* DEBUG */
181
182 for (wy = 0; wy < win->maxy; wy++) {
183 #ifdef DEBUG
184 __CTRACE("wy %d\tf: %d\tl:%d\tflags %x\n",
185 wy, *win->lines[wy]->firstchp, *win->lines[wy]->lastchp,
186 win->lines[wy]->flags);
187 #endif
188 if (!curwin)
189 curscr->lines[wy]->hash = win->lines[wy]->hash;
190 if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT)) {
191 if (makech(win, wy) == ERR)
192 return (ERR);
193 else {
194 if (*win->lines[wy]->firstchp >= win->ch_off)
195 *win->lines[wy]->firstchp = win->maxx +
196 win->ch_off;
197 if (*win->lines[wy]->lastchp < win->maxx +
198 win->ch_off)
199 *win->lines[wy]->lastchp = win->ch_off;
200 if (*win->lines[wy]->lastchp <
201 *win->lines[wy]->firstchp) {
202 #ifdef DEBUG
203 __CTRACE("wrefresh: line %d notdirty \n", wy);
204 #endif
205 win->lines[wy]->flags &= ~__ISDIRTY;
206 }
207 }
208
209 }
210 #ifdef DEBUG
211 __CTRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
212 *win->lines[wy]->lastchp);
213 #endif
214 }
215
216 #ifdef DEBUG
217 __CTRACE("refresh: ly=%d, lx=%d\n", ly, lx);
218 #endif
219
220 if (win == curscr)
221 domvcur(ly, lx, (int) win->cury, (int) win->curx);
222 else {
223 if (win->flags & __LEAVEOK) {
224 curscr->cury = ly;
225 curscr->curx = lx;
226 ly -= win->begy;
227 lx -= win->begx;
228 if (ly >= 0 && ly < win->maxy && lx >= 0 &&
229 lx < win->maxx) {
230 win->cury = ly;
231 win->curx = lx;
232 } else
233 win->cury = win->curx = 0;
234 } else {
235 domvcur(ly, lx, (int) (win->cury + win->begy),
236 (int) (win->curx + win->begx));
237 curscr->cury = win->cury + win->begy;
238 curscr->curx = win->curx + win->begx;
239 }
240 }
241 retval = OK;
242
243 (void) fflush(stdout);
244 return (retval);
245 }
246
247 /*
248 * makech --
249 * Make a change on the screen.
250 */
251 static int
252 makech(win, wy)
253 WINDOW *win;
254 int wy;
255 {
256 static __LDATA blank = {' ', 0};
257 __LDATA *nsp, *csp, *cp, *cep;
258 u_int force;
259 int clsp, nlsp; /* Last space in lines. */
260 int lch, wx, y;
261 char *ce;
262
263 #ifdef __GNUC__
264 nlsp = 0; /* XXX gcc -Wuninitialized */
265 #endif
266 /* Is the cursor still on the end of the last line? */
267 if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
268 domvcur(ly, lx, ly + 1, 0);
269 ly++;
270 lx = 0;
271 }
272 wx = *win->lines[wy]->firstchp - win->ch_off;
273 if (wx < 0)
274 wx = 0;
275 else
276 if (wx >= win->maxx)
277 return (OK);
278 lch = *win->lines[wy]->lastchp - win->ch_off;
279 if (lch < 0)
280 return (OK);
281 else
282 if (lch >= (int) win->maxx)
283 lch = win->maxx - 1;
284 y = wy + win->begy;
285
286 if (curwin)
287 csp = ␣
288 else
289 csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
290
291 nsp = &win->lines[wy]->line[wx];
292 force = win->lines[wy]->flags & __FORCEPAINT;
293 win->lines[wy]->flags &= ~__FORCEPAINT;
294 if (CE && !curwin) {
295 for (cp = &win->lines[wy]->line[win->maxx - 1];
296 cp->ch == ' ' && cp->attr == 0; cp--)
297 if (cp <= win->lines[wy]->line)
298 break;
299 nlsp = cp - win->lines[wy]->line;
300 }
301 if (!curwin)
302 ce = CE;
303 else
304 ce = NULL;
305
306 if (force) {
307 if (CM)
308 tputs(tgoto(CM, lx, ly), 0, __cputchar);
309 else {
310 tputs(HO, 0, __cputchar);
311 __mvcur(0, 0, ly, lx, 1);
312 }
313 }
314
315 while (wx <= lch) {
316 if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
317 if (wx <= lch) {
318 while (wx <= lch &&
319 memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
320 nsp++;
321 if (!curwin)
322 ++csp;
323 ++wx;
324 }
325 continue;
326 }
327 break;
328 }
329 domvcur(ly, lx, y, (int) (wx + win->begx));
330
331 #ifdef DEBUG
332 __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
333 wx, ly, lx, y, wx + win->begx, force);
334 #endif
335 ly = y;
336 lx = wx + win->begx;
337 while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
338 && wx <= lch) {
339
340 if (ce != NULL &&
341 win->maxx + win->begx == curscr->maxx &&
342 wx >= nlsp && nsp->ch == ' ' && nsp->attr == 0) {
343 /* Check for clear to end-of-line. */
344 cep = &curscr->lines[wy]->line[win->maxx - 1];
345 while (cep->ch == ' ' && cep->attr == 0)
346 if (cep-- <= csp)
347 break;
348 clsp = cep - curscr->lines[wy]->line -
349 win->begx * __LDATASIZE;
350 #ifdef DEBUG
351 __CTRACE("makech: clsp = %d, nlsp = %d\n", clsp, nlsp);
352 #endif
353 /* XXX: check for background colour and bce here */
354 if ((clsp - nlsp >= strlen(CE)
355 && clsp < win->maxx * __LDATASIZE) ||
356 wy == win->maxy - 1) {
357 unsetattr(0);
358 tputs(CE, 0, __cputchar);
359 lx = wx + win->begx;
360 while (wx++ <= clsp) {
361 csp->ch = ' ';
362 csp->attr = 0;
363 csp++;
364 }
365 return (OK);
366 }
367 ce = NULL;
368 }
369
370 /*
371 * Unset colour if appropriate. Check to see
372 * if we also turn off standout, underscore and
373 * attributes.
374 */
375 if (!(nsp->attr & __COLOR) &&
376 (curscr->wattr & __COLOR)) {
377 if (OC != NULL && CC == NULL)
378 tputs(OC, 0, __cputchar);
379 if (OP != NULL) {
380 tputs(OP, 0, __cputchar);
381 if (SE != NULL && !strcmp(OP, SE))
382 curscr->wattr &= ~__STANDOUT;
383 if (UE != NULL && !strcmp(OP, UE))
384 curscr->wattr &= ~__UNDERSCORE;
385 if (ME != NULL && !strcmp(OP, ME))
386 curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
387 }
388 curscr->wattr &= ~__COLOR;
389 }
390
391 /*
392 * Unset attributes as appropriate. Unset first
393 * so that the relevant attributes can be reset
394 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
395 * 'mp' and 'mr'). Check to see if we also turn off
396 * standout, attributes and colour.
397 */
398 if ((!(nsp->attr & __BLINK) &&
399 curscr->wattr & __BLINK) ||
400 (!(nsp->attr & __BOLD) &&
401 curscr->wattr & __BOLD) ||
402 (!(nsp->attr & __DIM) &&
403 curscr->wattr & __DIM) ||
404 (!(nsp->attr & __BLANK) &&
405 curscr->wattr & __BLANK) ||
406 (!(nsp->attr & __PROTECT) &&
407 curscr->wattr & __PROTECT) ||
408 (!(nsp->attr & __REVERSE) &&
409 curscr->wattr & __REVERSE)) {
410 tputs(ME, 0, __cputchar);
411 curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET
412 | __COLOR;
413 if (SE != NULL && !strcmp(ME, SE))
414 curscr->wattr &= ~__STANDOUT;
415 if (UE != NULL && !strcmp(ME, UE))
416 curscr->wattr &= ~__UNDERSCORE;
417 if (OP != NULL && !strcmp(ME, OP))
418 curscr->wattr &= ~__COLOR;
419 }
420
421 /*
422 * Exit underscore mode if appropriate.
423 * Check to see if we also turn off standout,
424 * attributes and colour.
425 */
426 if (!(nsp->attr & __UNDERSCORE) &&
427 (curscr->wattr & __UNDERSCORE)) {
428 tputs(UE, 0, __cputchar);
429 curscr->wattr &= ~__UNDERSCORE;
430 if (SE != NULL && !strcmp(UE, SE))
431 curscr->wattr &= ~__STANDOUT;
432 if (ME != NULL && !strcmp(UE, ME))
433 curscr->wattr &= ~__ATTRIBUTES |
434 __ALTCHARSET | __COLOR;
435 if (OP != NULL && !strcmp(UE, OP))
436 curscr->wattr &= ~__COLOR;
437 }
438
439 /*
440 * Enter/exit standout mode as appropriate.
441 * Check to see if we also turn off underscore,
442 * attributes and colour.
443 * XXX
444 * Should use UC if SO/SE not available.
445 */
446 if (nsp->attr & __STANDOUT) {
447 if (!(curscr->wattr & __STANDOUT) &&
448 SO != NULL && SE != NULL) {
449 tputs(SO, 0, __cputchar);
450 curscr->wattr |= __STANDOUT;
451 }
452 } else {
453 if (curscr->wattr & __STANDOUT) {
454 tputs(SE, 0, __cputchar);
455 curscr->wattr &= ~__STANDOUT;
456 if (UE != NULL && !strcmp(SE, UE))
457 curscr->wattr &=
458 ~__UNDERSCORE;
459 if (ME != NULL && !strcmp(SE, ME))
460 curscr->wattr &= ~__ATTRIBUTES |
461 __ALTCHARSET | __COLOR;
462 if (OP != NULL && !strcmp(SE, OP))
463 curscr->wattr &= ~__COLOR;
464 }
465 }
466
467 /*
468 * Enter underscore mode if appropriate.
469 * XXX
470 * Should use UC if US/UE not available.
471 */
472 if (nsp->attr & __UNDERSCORE &&
473 !(curscr->wattr & __UNDERSCORE) &&
474 US != NULL && UE != NULL) {
475 tputs(US, 0, __cputchar);
476 curscr->wattr |= __UNDERSCORE;
477 }
478
479 /*
480 * Set other attributes as appropriate.
481 */
482 if (nsp->attr & __BLINK) {
483 if (!(curscr->wattr & __BLINK) &&
484 MB != NULL && ME != NULL) {
485 tputs(MB, 0, __cputchar);
486 curscr->wattr |= __BLINK;
487 }
488 }
489 if (nsp->attr & __BOLD) {
490 if (!(curscr->wattr & __BOLD) &&
491 MD != NULL && ME != NULL) {
492 tputs(MD, 0, __cputchar);
493 curscr->wattr |= __BOLD;
494 }
495 }
496 if (nsp->attr & __DIM) {
497 if (!(curscr->wattr & __DIM) &&
498 MH != NULL && ME != NULL) {
499 tputs(MH, 0, __cputchar);
500 curscr->wattr |= __DIM;
501 }
502 }
503 if (nsp->attr & __BLANK) {
504 if (!(curscr->wattr & __BLANK) &&
505 MK != NULL && ME != NULL) {
506 tputs(MK, 0, __cputchar);
507 curscr->wattr |= __BLANK;
508 }
509 }
510 if (nsp->attr & __PROTECT) {
511 if (!(curscr->wattr & __PROTECT) &&
512 MP != NULL && ME != NULL) {
513 tputs(MP, 0, __cputchar);
514 curscr->wattr |= __PROTECT;
515 }
516 }
517 if (nsp->attr & __REVERSE) {
518 if (!(curscr->wattr & __REVERSE) &&
519 MR != NULL && ME != NULL) {
520 tputs(MR, 0, __cputchar);
521 curscr->wattr |= __REVERSE;
522 }
523 }
524
525 /* Set/change colour as appropriate. */
526 if (nsp->attr & __COLOR) {
527 if ((!(curscr->wattr & __COLOR) &&
528 cO != NULL && (OC != NULL || OP != NULL)) ||
529 ((nsp->attr & __COLOR) !=
530 (curscr->wattr & __COLOR))) {
531 __set_color(nsp->attr);
532 curscr->wattr &= ~__COLOR;
533 curscr->wattr |= nsp->attr & __COLOR;
534 }
535 }
536
537 /* Enter/exit altcharset mode as appropriate. */
538 if (nsp->attr & __ALTCHARSET) {
539 if (!(curscr->wattr & __ALTCHARSET) &&
540 AS != NULL && AE != NULL) {
541 tputs(AS, 0, __cputchar);
542 curscr->wattr |= __ALTCHARSET;
543 }
544 } else {
545 if (curscr->wattr & __ALTCHARSET) {
546 tputs(AE, 0, __cputchar);
547 curscr->wattr &= ~__ALTCHARSET;
548 }
549 }
550
551 wx++;
552 if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
553 if (win->flags & __SCROLLOK) {
554 if (win->flags & __ENDLINE){
555 unsetattr(1);
556 }
557 if (!(win->flags & __SCROLLWIN)) {
558 if (!curwin) {
559 csp->attr = nsp->attr;
560 putchar((int) (csp->ch = nsp->ch));
561
562 } else
563 putchar((int) nsp->ch);
564 }
565 if (wx + win->begx < curscr->maxx) {
566 domvcur(ly, (int) (wx + win->begx),
567 (int) (win->begy + win->maxy - 1),
568 (int) (win->begx + win->maxx - 1));
569 }
570 ly = win->begy + win->maxy - 1;
571 lx = win->begx + win->maxx - 1;
572 return (OK);
573 }
574 if (wx < win->maxx || wy < win->maxy - 1 ||
575 !(win->flags & __SCROLLWIN)) {
576 if (!curwin) {
577 csp->attr = nsp->attr;
578 putchar((int) (csp->ch = nsp->ch));
579 csp++;
580 } else
581 putchar((int) nsp->ch);
582 }
583 #ifdef DEBUG
584 __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
585 #endif
586 if (UC && ((nsp->attr & __STANDOUT) ||
587 (nsp->attr & __UNDERSCORE))) {
588 putchar('\b');
589 tputs(UC, 0, __cputchar);
590 }
591 nsp++;
592 #ifdef DEBUG
593 __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
594 #endif
595 }
596 if (lx == wx + win->begx) /* If no change. */
597 break;
598 lx = wx + win->begx;
599 if (lx >= COLS && AM)
600 lx = COLS - 1;
601 else
602 if (wx >= win->maxx) {
603 domvcur(ly, lx, ly, (int) (win->maxx + win->begx - 1));
604 lx = win->maxx + win->begx - 1;
605 }
606 #ifdef DEBUG
607 __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
608 #endif
609 }
610
611 /* Don't leave the screen with attributes set. */
612 unsetattr(0);
613 return (OK);
614 }
615
616 /*
617 * domvcur --
618 * Do a mvcur, leaving attributes if necessary.
619 */
620 static void
621 domvcur(oy, ox, ny, nx)
622 int oy, ox, ny, nx;
623 {
624 unsetattr(1);
625 __mvcur(oy, ox, ny, nx, 1);
626 }
627
628 /*
629 * Quickch() attempts to detect a pattern in the change of the window
630 * in order to optimize the change, e.g., scroll n lines as opposed to
631 * repainting the screen line by line.
632 */
633
634 static void
635 quickch(win)
636 WINDOW *win;
637 {
638 #define THRESH (int) win->maxy / 4
639
640 __LINE *clp, *tmp1, *tmp2;
641 int bsize, curs, curw, starts, startw, i, j;
642 int n, target, cur_period, bot, top, sc_region;
643 __LDATA buf[1024];
644 u_int blank_hash;
645
646 #ifdef __GNUC__
647 curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
648 #endif
649 /*
650 * Find how many lines from the top of the screen are unchanged.
651 */
652 for (top = 0; top < win->maxy; top++)
653 if (win->lines[top]->flags & __FORCEPAINT ||
654 win->lines[top]->hash != curscr->lines[top]->hash
655 || memcmp(win->lines[top]->line,
656 curscr->lines[top]->line,
657 (size_t) win->maxx * __LDATASIZE) != 0)
658 break;
659 else
660 win->lines[top]->flags &= ~__ISDIRTY;
661 /*
662 * Find how many lines from bottom of screen are unchanged.
663 */
664 for (bot = win->maxy - 1; bot >= 0; bot--)
665 if (win->lines[bot]->flags & __FORCEPAINT ||
666 win->lines[bot]->hash != curscr->lines[bot]->hash
667 || memcmp(win->lines[bot]->line,
668 curscr->lines[bot]->line,
669 (size_t) win->maxx * __LDATASIZE) != 0)
670 break;
671 else
672 win->lines[bot]->flags &= ~__ISDIRTY;
673
674 #ifdef NO_JERKINESS
675 /*
676 * If we have a bottom unchanged region return. Scrolling the
677 * bottom region up and then back down causes a screen jitter.
678 * This will increase the number of characters sent to the screen
679 * but it looks better.
680 */
681 if (bot < win->maxy - 1)
682 return;
683 #endif /* NO_JERKINESS */
684
685 /*
686 * Search for the largest block of text not changed.
687 * Invariants of the loop:
688 * - Startw is the index of the beginning of the examined block in win.
689 * - Starts is the index of the beginning of the examined block in
690 * curscr.
691 * - Curs is the index of one past the end of the exmined block in win.
692 * - Curw is the index of one past the end of the exmined block in
693 * curscr.
694 * - bsize is the current size of the examined block.
695 */
696 for (bsize = bot - top; bsize >= THRESH; bsize--) {
697 for (startw = top; startw <= bot - bsize; startw++)
698 for (starts = top; starts <= bot - bsize;
699 starts++) {
700 for (curw = startw, curs = starts;
701 curs < starts + bsize; curw++, curs++)
702 if (win->lines[curw]->flags &
703 __FORCEPAINT ||
704 (win->lines[curw]->hash !=
705 curscr->lines[curs]->hash ||
706 memcmp(win->lines[curw]->line,
707 curscr->lines[curs]->line,
708 (size_t) win->maxx * __LDATASIZE) != 0))
709 break;
710 if (curs == starts + bsize)
711 goto done;
712 }
713 }
714 done:
715 /* Did not find anything */
716 if (bsize < THRESH)
717 return;
718
719 #ifdef DEBUG
720 __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
721 bsize, starts, startw, curw, curs, top, bot);
722 #endif
723
724 /*
725 * Make sure that there is no overlap between the bottom and top
726 * regions and the middle scrolled block.
727 */
728 if (bot < curs)
729 bot = curs - 1;
730 if (top > starts)
731 top = starts;
732
733 n = startw - starts;
734
735 #ifdef DEBUG
736 __CTRACE("#####################################\n");
737 for (i = 0; i < curscr->maxy; i++) {
738 __CTRACE("C: %d:", i);
739 __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
740 for (j = 0; j < curscr->maxx; j++)
741 __CTRACE("%c", curscr->lines[i]->line[j].ch);
742 __CTRACE("\n");
743 __CTRACE(" attr:");
744 for (j = 0; j < curscr->maxx; j++)
745 __CTRACE(" %x", curscr->lines[i]->line[j].attr);
746 __CTRACE("\n");
747 __CTRACE("W: %d:", i);
748 __CTRACE(" 0x%x \n", win->lines[i]->hash);
749 __CTRACE(" 0x%x ", win->lines[i]->flags);
750 for (j = 0; j < win->maxx; j++)
751 __CTRACE("%c", win->lines[i]->line[j].ch);
752 __CTRACE("\n");
753 __CTRACE(" attr:");
754 for (j = 0; j < win->maxx; j++)
755 __CTRACE(" %x", win->lines[i]->line[j].attr);
756 __CTRACE("\n");
757 }
758 #endif
759
760 /* So we don't have to call __hash() each time */
761 for (i = 0; i < win->maxx; i++) {
762 buf[i].ch = ' ';
763 buf[i].attr = 0;
764 }
765 blank_hash = __hash((char *)(void *)buf,
766 (int) (win->maxx * __LDATASIZE));
767
768 /*
769 * Perform the rotation to maintain the consistency of curscr.
770 * This is hairy since we are doing an *in place* rotation.
771 * Invariants of the loop:
772 * - I is the index of the current line.
773 * - Target is the index of the target of line i.
774 * - Tmp1 points to current line (i).
775 * - Tmp2 and points to target line (target);
776 * - Cur_period is the index of the end of the current period.
777 * (see below).
778 *
779 * There are 2 major issues here that make this rotation non-trivial:
780 * 1. Scrolling in a scrolling region bounded by the top
781 * and bottom regions determined (whose size is sc_region).
782 * 2. As a result of the use of the mod function, there may be a
783 * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
784 * 0 to 2, which then causes all odd lines not to be rotated.
785 * To remedy this, an index of the end ( = beginning) of the
786 * current 'period' is kept, cur_period, and when it is reached,
787 * the next period is started from cur_period + 1 which is
788 * guaranteed not to have been reached since that would mean that
789 * all records would have been reached. (think about it...).
790 *
791 * Lines in the rotation can have 3 attributes which are marked on the
792 * line so that curscr is consistent with the visual screen.
793 * 1. Not dirty -- lines inside the scrolled block, top region or
794 * bottom region.
795 * 2. Blank lines -- lines in the differential of the scrolling
796 * region adjacent to top and bot regions
797 * depending on scrolling direction.
798 * 3. Dirty line -- all other lines are marked dirty.
799 */
800 sc_region = bot - top + 1;
801 i = top;
802 tmp1 = curscr->lines[top];
803 cur_period = top;
804 for (j = top; j <= bot; j++) {
805 target = (i - top + n + sc_region) % sc_region + top;
806 tmp2 = curscr->lines[target];
807 curscr->lines[target] = tmp1;
808 /* Mark block as clean and blank out scrolled lines. */
809 clp = curscr->lines[target];
810 #ifdef DEBUG
811 __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
812 n, startw, curw, i, target);
813 #endif
814 if ((target >= startw && target < curw) || target < top
815 || target > bot) {
816 #ifdef DEBUG
817 __CTRACE("-- notdirty");
818 #endif
819 win->lines[target]->flags &= ~__ISDIRTY;
820 } else
821 if ((n > 0 && target >= top && target < top + n) ||
822 (n < 0 && target <= bot && target > bot + n)) {
823 if (clp->hash != blank_hash || memcmp(clp->line,
824 buf, (size_t) win->maxx * __LDATASIZE) !=0) {
825 (void)memcpy(clp->line, buf,
826 (size_t) win->maxx * __LDATASIZE);
827 #ifdef DEBUG
828 __CTRACE("-- blanked out: dirty");
829 #endif
830 clp->hash = blank_hash;
831 __touchline(win, target, 0, (int) win->maxx - 1, 0);
832 } else {
833 __touchline(win, target, 0, (int) win->maxx - 1, 0);
834 #ifdef DEBUG
835 __CTRACE(" -- blank line already: dirty");
836 #endif
837 }
838 } else {
839 #ifdef DEBUG
840 __CTRACE(" -- dirty");
841 #endif
842 __touchline(win, target, 0, (int) win->maxx - 1, 0);
843 }
844 #ifdef DEBUG
845 __CTRACE("\n");
846 #endif
847 if (target == cur_period) {
848 i = target + 1;
849 tmp1 = curscr->lines[i];
850 cur_period = i;
851 } else {
852 tmp1 = tmp2;
853 i = target;
854 }
855 }
856 #ifdef DEBUG
857 __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
858 for (i = 0; i < curscr->maxy; i++) {
859 __CTRACE("C: %d:", i);
860 for (j = 0; j < curscr->maxx; j++)
861 __CTRACE("%c", curscr->lines[i]->line[j].ch);
862 __CTRACE("\n");
863 __CTRACE("W: %d:", i);
864 for (j = 0; j < win->maxx; j++)
865 __CTRACE("%c", win->lines[i]->line[j].ch);
866 __CTRACE("\n");
867 }
868 #endif
869 if (n != 0) {
870 WINDOW *wp;
871 scrolln(win, starts, startw, curs, bot, top);
872 /*
873 * Need to repoint any subwindow lines to the rotated
874 * line structured.
875 */
876 for (wp = win->nextp; wp != win; wp = wp->nextp)
877 __set_subwin(win, wp);
878 }
879 }
880
881 /*
882 * scrolln --
883 * Scroll n lines, where n is starts - startw.
884 */
885 static void /* ARGSUSED */
886 scrolln(win, starts, startw, curs, bot, top)
887 WINDOW *win;
888 int starts, startw, curs, bot, top;
889 {
890 int i, oy, ox, n;
891
892 oy = curscr->cury;
893 ox = curscr->curx;
894 n = starts - startw;
895
896 /*
897 * XXX
898 * The initial tests that set __noqch don't let us reach here unless
899 * we have either CS + HO + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
900 * scrolling can only shift the entire scrolling region, not just a
901 * part of it, which means that the quickch() routine is going to be
902 * sadly disappointed in us if we don't have CS as well.
903 *
904 * If CS, HO and SF/sf are set, can use the scrolling region. Because
905 * the cursor position after CS is undefined, we need HO which gives us
906 * the ability to move to somewhere without knowledge of the current
907 * location of the cursor. Still call __mvcur() anyway, to update its
908 * idea of where the cursor is.
909 *
910 * When the scrolling region has been set, the cursor has to be at the
911 * last line of the region to make the scroll happen.
912 *
913 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
914 * or al/dl, and, some terminals have AL/DL, sf/sr, and CS, but not
915 * SF/SR. So, if we're scrolling almost all of the screen, try and use
916 * AL/DL, otherwise use the scrolling region. The "almost all" is a
917 * shameless hack for vi.
918 */
919 /* XXX: check for background colour and bce here */
920 if (n > 0) {
921 if (CS != NULL && HO != NULL && (SF != NULL ||
922 ((AL == NULL || DL == NULL ||
923 top > 3 || bot + 3 < win->maxy) && sf != NULL))) {
924 tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
925 __mvcur(oy, ox, 0, 0, 1);
926 tputs(HO, 0, __cputchar);
927 __mvcur(0, 0, bot, 0, 1);
928 if (SF != NULL)
929 tputs(__tscroll(SF, n, 0), 0, __cputchar);
930 else
931 for (i = 0; i < n; i++)
932 tputs(sf, 0, __cputchar);
933 tputs(__tscroll(CS, 0, (int) win->maxy), 0, __cputchar);
934 __mvcur(bot, 0, 0, 0, 1);
935 tputs(HO, 0, __cputchar);
936 __mvcur(0, 0, oy, ox, 1);
937 return;
938 }
939
940 /* Scroll up the block. */
941 if (SF != NULL && top == 0) {
942 __mvcur(oy, ox, bot, 0, 1);
943 tputs(__tscroll(SF, n, 0), 0, __cputchar);
944 } else
945 if (DL != NULL) {
946 __mvcur(oy, ox, top, 0, 1);
947 tputs(__tscroll(DL, n, 0), 0, __cputchar);
948 } else
949 if (dl != NULL) {
950 __mvcur(oy, ox, top, 0, 1);
951 for (i = 0; i < n; i++)
952 tputs(dl, 0, __cputchar);
953 } else
954 if (sf != NULL && top == 0) {
955 __mvcur(oy, ox, bot, 0, 1);
956 for (i = 0; i < n; i++)
957 tputs(sf, 0, __cputchar);
958 } else
959 abort();
960
961 /* Push down the bottom region. */
962 __mvcur(top, 0, bot - n + 1, 0, 1);
963 if (AL != NULL)
964 tputs(__tscroll(AL, n, 0), 0, __cputchar);
965 else
966 if (al != NULL)
967 for (i = 0; i < n; i++)
968 tputs(al, 0, __cputchar);
969 else
970 abort();
971 __mvcur(bot - n + 1, 0, oy, ox, 1);
972 } else {
973 /*
974 * !!!
975 * n < 0
976 *
977 * If CS, HO and SR/sr are set, can use the scrolling region.
978 * See the above comments for details.
979 */
980 if (CS != NULL && HO != NULL && (SR != NULL ||
981 ((AL == NULL || DL == NULL ||
982 top > 3 || bot + 3 < win->maxy) && sr != NULL))) {
983 tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
984 __mvcur(oy, ox, 0, 0, 1);
985 tputs(HO, 0, __cputchar);
986 __mvcur(0, 0, top, 0, 1);
987
988 if (SR != NULL)
989 tputs(__tscroll(SR, -n, 0), 0, __cputchar);
990 else
991 for (i = n; i < 0; i++)
992 tputs(sr, 0, __cputchar);
993 tputs(__tscroll(CS, 0, (int) win->maxy), 0, __cputchar);
994 __mvcur(top, 0, 0, 0, 1);
995 tputs(HO, 0, __cputchar);
996 __mvcur(0, 0, oy, ox, 1);
997 return;
998 }
999
1000 /* Preserve the bottom lines. */
1001 __mvcur(oy, ox, bot + n + 1, 0, 1);
1002 if (SR != NULL && bot == win->maxy)
1003 tputs(__tscroll(SR, -n, 0), 0, __cputchar);
1004 else
1005 if (DL != NULL)
1006 tputs(__tscroll(DL, -n, 0), 0, __cputchar);
1007 else
1008 if (dl != NULL)
1009 for (i = n; i < 0; i++)
1010 tputs(dl, 0, __cputchar);
1011 else
1012 if (sr != NULL && bot == win->maxy)
1013 for (i = n; i < 0; i++)
1014 tputs(sr, 0, __cputchar);
1015 else
1016 abort();
1017
1018 /* Scroll the block down. */
1019 __mvcur(bot + n + 1, 0, top, 0, 1);
1020 if (AL != NULL)
1021 tputs(__tscroll(AL, -n, 0), 0, __cputchar);
1022 else
1023 if (al != NULL)
1024 for (i = n; i < 0; i++)
1025 tputs(al, 0, __cputchar);
1026 else
1027 abort();
1028 __mvcur(top, 0, oy, ox, 1);
1029 }
1030 }
1031
1032 /*
1033 * unsetattr --
1034 * Unset attributes on curscr. Leave standout, attribute and colour
1035 * modes if necessary (!MS). Always leave altcharset (xterm at least
1036 * ignores a cursor move if we don't).
1037 */
1038 static void /* ARGSUSED */
1039 unsetattr(int checkms)
1040 {
1041 int isms;
1042
1043 if (checkms)
1044 if (!MS) {
1045 isms = 1;
1046 } else {
1047 isms = 0;
1048 }
1049 else
1050 isms = 1;
1051
1052 /* Don't leave the screen in standout mode (check against MS). */
1053 if (curscr->wattr & __STANDOUT && isms) {
1054 tputs(SE, 0, __cputchar);
1055 curscr->wattr &= ~__STANDOUT;
1056 if (UE != NULL && !strcmp(SE, UE))
1057 curscr->wattr &= ~__UNDERSCORE;
1058 if (ME != NULL && !strcmp(SE, ME))
1059 curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
1060 if (OP != NULL && !strcmp(SE, OP))
1061 curscr->wattr &= ~__COLOR;
1062 }
1063 /* Don't leave the screen in underscore mode (check against MS). */
1064 if (curscr->wattr & __UNDERSCORE && isms) {
1065 tputs(UE, 0, __cputchar);
1066 curscr->wattr &= ~__UNDERSCORE;
1067 if (ME != NULL && !strcmp(UE, ME))
1068 curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
1069 if (OP != NULL && !strcmp(UE, OP))
1070 curscr->wattr &= ~__COLOR;
1071 }
1072 /* Don't leave the screen with attributes set (check against MS). */
1073 if (curscr->wattr & __ATTRIBUTES && isms) {
1074 tputs(ME, 0, __cputchar);
1075 curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
1076 if (OP != NULL && !strcmp(ME, OP))
1077 curscr->wattr &= ~__COLOR;
1078 }
1079 /* Don't leave the screen with altcharset set. */
1080 if (curscr->wattr & __ALTCHARSET) {
1081 tputs(AE, 0, __cputchar);
1082 curscr->wattr &= ~__ALTCHARSET;
1083 }
1084 /* Don't leave the screen with colour set (check against MS). */
1085 if (curscr->wattr & __COLOR && isms) {
1086 if (OC != NULL && CC == NULL)
1087 tputs(OC, 0, __cputchar);
1088 if (OP != NULL)
1089 tputs(OP, 0, __cputchar);
1090 curscr->wattr &= ~__COLOR;
1091 }
1092 }
1093