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