refresh.c revision 1.32 1 /* $NetBSD: refresh.c,v 1.32 2000/05/19 04:15:55 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.32 2000/05/19 04:15:55 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 &= ~__LEAVEOK;
98 __virtscr->flags |= win->flags;
99 if (win->flags & __CLEAROK)
100 win->flags &= ~__CLEAROK;
101
102 for (wy = 0, y_off = win->begy; wy < win->maxy &&
103 y_off < __virtscr->maxy; wy++, y_off++) {
104 #ifdef DEBUG
105 __CTRACE("wnoutrefresh: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
106 *win->lines[wy]->firstchp,
107 *win->lines[wy]->lastchp,
108 win->lines[wy]->flags);
109 #endif
110 y_off = wy + win->begy;
111 if (*win->lines[wy]->firstchp <= win->maxx + win->ch_off &&
112 *win->lines[wy]->lastchp >= win->ch_off) {
113 /* Copy line from "win" to "__virtscr". */
114 for (wx = 0, x_off = win->begx; wx < win->maxx &&
115 x_off < __virtscr->maxx; wx++, x_off++) {
116 __virtscr->lines[y_off]->line[x_off].attr =
117 win->lines[wy]->line[wx].attr;
118 if (!(win->lines[wy]->line[wx].attr & __COLOR)
119 && (win->lines[wy]->line[wx].battr &
120 __COLOR))
121 __virtscr->lines[y_off]->line[x_off].
122 attr |=
123 win->lines[wy]->line[wx].battr &
124 __COLOR;
125 if (win->lines[wy]->line[wx].ch == ' ' &&
126 win->lines[wy]->line[wx].bch != ' ')
127 __virtscr->lines[y_off]->line[x_off].ch
128 = win->lines[wy]->line[wx].bch;
129 else
130 __virtscr->lines[y_off]->line[x_off].ch
131 = win->lines[wy]->line[wx].ch;
132 }
133
134 /* Set flags on "__virtscr" and unset on "win". */
135 if (win->lines[wy]->flags & __FORCEPAINT) {
136 __virtscr->lines[y_off]->flags |= __FORCEPAINT;
137 win->lines[wy]->flags &= ~__FORCEPAINT;
138 }
139 if (win->lines[wy]->flags & __ISPASTEOL)
140 __virtscr->lines[y_off]->flags |= __ISPASTEOL;
141 else
142 __virtscr->lines[y_off]->flags &= ~__ISPASTEOL;
143 if (win->lines[wy]->flags & __ISDIRTY)
144 __virtscr->lines[y_off]->flags |= __ISDIRTY;
145
146 #ifdef DEBUG
147 __CTRACE("win: firstch = %d, lastch = %d\n",
148 *win->lines[wy]->firstchp,
149 *win->lines[wy]->lastchp);
150 #endif
151 /* Set change pointers on "__virtscr". */
152 if (*__virtscr->lines[y_off]->firstchp >
153 *win->lines[wy]->firstchp + win->begx - win->ch_off)
154 *__virtscr->lines[y_off]->firstchp =
155 *win->lines[wy]->firstchp + win->begx -
156 win->ch_off;
157 if (*__virtscr->lines[y_off]->lastchp <
158 *win->lines[wy]->lastchp + win->begx - win->ch_off)
159 *__virtscr->lines[y_off]->lastchp =
160 *win->lines[wy]->lastchp + win->begx -
161 win->ch_off;
162 #ifdef DEBUG
163 __CTRACE("__virtscr: firstch = %d, lastch = %d\n",
164 *__virtscr->lines[y_off]->firstchp,
165 *__virtscr->lines[y_off]->lastchp);
166 #endif
167
168 /* Set change pointers on "win". */
169 if (*win->lines[wy]->firstchp >= win->ch_off)
170 *win->lines[wy]->firstchp = win->maxx +
171 win->ch_off;
172 if (*win->lines[wy]->lastchp < win->maxx + win->ch_off)
173 *win->lines[wy]->lastchp = win->ch_off;
174 if (*win->lines[wy]->lastchp <
175 *win->lines[wy]->firstchp) {
176 #ifdef DEBUG
177 __CTRACE("wnoutrefresh: line %d notdirty\n",
178 wy);
179 #endif
180 win->lines[wy]->flags &= ~__ISDIRTY;
181 }
182 }
183 }
184
185 return (OK);
186 }
187
188 /*
189 * wrefresh --
190 * Make the current screen look like "win" over the area coverd by
191 * win.
192 */
193 int
194 wrefresh(WINDOW *win)
195 {
196 int retval;
197
198 curwin = (win == curscr);
199 if (!curwin)
200 retval = wnoutrefresh(win);
201 else
202 retval = OK;
203 if (retval == OK) {
204 retval = doupdate();
205 if (!win->flags & __LEAVEOK) {
206 win->cury = max(0, curscr->cury - win->begy);
207 win->curx = max(0, curscr->curx - win->begx);
208 }
209 }
210 curwin = 0;
211 return(retval);
212 }
213
214 /*
215 * doupdate --
216 * Make the current screen look like the virtual window "__virtscr".
217 */
218 int
219 doupdate(void)
220 {
221 WINDOW *win;
222 __LINE *wlp;
223 short wy;
224 int dnum;
225
226 /* Check if we need to restart ... */
227 if (__endwin) {
228 __endwin = 0;
229 __restartwin();
230 }
231
232 if (curwin)
233 win = curscr;
234 else
235 win = __virtscr;
236
237 /* Initialize loop parameters. */
238 ly = curscr->cury;
239 lx = curscr->curx;
240 wy = 0;
241
242 if (!curwin)
243 for (wy = 0; wy < win->maxy; wy++) {
244 wlp = win->lines[wy];
245 if (wlp->flags & __ISDIRTY)
246 wlp->hash = __hash((char *)(void *)wlp->line,
247 (int) (win->maxx * __LDATASIZE));
248 }
249
250 if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) || curwin) {
251 if (curscr->wattr & __COLOR)
252 __unsetattr(0);
253 tputs(CL, 0, __cputchar);
254 ly = 0;
255 lx = 0;
256 if (!curwin) {
257 curscr->flags &= ~__CLEAROK;
258 curscr->cury = 0;
259 curscr->curx = 0;
260 werase(curscr);
261 }
262 __touchwin(win);
263 win->flags &= ~__CLEAROK;
264 }
265 if (!CA) {
266 if (win->curx != 0)
267 putchar('\n');
268 if (!curwin)
269 werase(curscr);
270 }
271 #ifdef DEBUG
272 __CTRACE("doupdate: (%0.2o): curwin = %d\n", win, curwin);
273 __CTRACE("doupdate: \tfirstch\tlastch\n");
274 #endif
275
276 if (!curwin) {
277 /*
278 * Invoke quickch() only if more than a quarter of the lines
279 * in the window are dirty.
280 */
281 for (wy = 0, dnum = 0; wy < win->maxy; wy++)
282 if (win->lines[wy]->flags &
283 (__ISDIRTY | __FORCEPAINT))
284 dnum++;
285 if (!__noqch && dnum > (int) win->maxy / 4)
286 quickch();
287 }
288
289 #ifdef DEBUG
290 {
291 int i, j;
292
293 __CTRACE("#####################################\n");
294 for (i = 0; i < curscr->maxy; i++) {
295 __CTRACE("C: %d:", i);
296 __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
297 for (j = 0; j < curscr->maxx; j++)
298 __CTRACE("%c", curscr->lines[i]->line[j].ch);
299 __CTRACE("\n");
300 __CTRACE(" attr:");
301 for (j = 0; j < curscr->maxx; j++)
302 __CTRACE(" %x", curscr->lines[i]->line[j].attr);
303 __CTRACE("\n");
304 __CTRACE("W: %d:", i);
305 __CTRACE(" 0x%x \n", win->lines[i]->hash);
306 __CTRACE(" 0x%x ", win->lines[i]->flags);
307 for (j = 0; j < win->maxx; j++)
308 __CTRACE("%c", win->lines[i]->line[j].ch);
309 __CTRACE("\n");
310 __CTRACE(" attr:");
311 for (j = 0; j < win->maxx; j++)
312 __CTRACE(" %x",
313 win->lines[i]->line[j].attr);
314 __CTRACE("\n");
315 }
316 }
317 #endif /* DEBUG */
318
319 for (wy = 0; wy < win->maxy; wy++) {
320 /* XXX: remove this debug */
321 #ifdef DEBUG
322 __CTRACE("doupdate: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
323 *win->lines[wy]->firstchp,
324 *win->lines[wy]->lastchp,
325 win->lines[wy]->flags);
326 #endif
327 if (!curwin)
328 curscr->lines[wy]->hash = win->lines[wy]->hash;
329 if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT)) {
330 if (makech(wy) == ERR)
331 return (ERR);
332 else {
333 if (*win->lines[wy]->firstchp >= 0)
334 *win->lines[wy]->firstchp =
335 win->maxx;
336 if (*win->lines[wy]->lastchp <
337 win->maxx)
338 *win->lines[wy]->lastchp = 0;
339 if (*win->lines[wy]->lastchp <
340 *win->lines[wy]->firstchp) {
341 #ifdef DEBUG
342 __CTRACE("doupdate: line %d notdirty\n", wy);
343 #endif
344 win->lines[wy]->flags &= ~__ISDIRTY;
345 }
346 }
347
348 }
349 #ifdef DEBUG
350 __CTRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
351 *win->lines[wy]->lastchp);
352 #endif
353 }
354
355 #ifdef DEBUG
356 __CTRACE("doupdate: ly=%d, lx=%d\n", ly, lx);
357 #endif
358
359 if (curwin)
360 domvcur(ly, lx, (int) win->cury, (int) win->curx);
361 else {
362 if (win->flags & __LEAVEOK) {
363 curscr->cury = ly;
364 curscr->curx = lx;
365 } else {
366 domvcur(ly, lx, win->cury, win->curx);
367 curscr->cury = win->cury;
368 curscr->curx = win->curx;
369 }
370 }
371
372 /* Don't leave the screen with attributes set. */
373 __unsetattr(0);
374 (void) fflush(stdout);
375 return (OK);
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(NULL, 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 if (!(win->flags & __SCROLLWIN)) {
649 if (!curwin) {
650 csp->attr = nsp->attr;
651 putchar((int)
652 (csp->ch =
653 nsp->ch));
654 } else
655 putchar((int) nsp->ch);
656 }
657 if (wx < curscr->maxx) {
658 domvcur(ly, wx,
659 (int) (win->maxy - 1),
660 (int) (win->maxx - 1));
661 }
662 ly = win->maxy - 1;
663 lx = win->maxx - 1;
664 return (OK);
665 }
666 if (wx < win->maxx || wy < win->maxy - 1 ||
667 !(win->flags & __SCROLLWIN)) {
668 if (!curwin) {
669 csp->attr = nsp->attr;
670 putchar((int) (csp->ch = nsp->ch));
671 csp++;
672 } else
673 putchar((int) nsp->ch);
674 }
675 #ifdef DEBUG
676 __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
677 #endif
678 if (UC && ((nsp->attr & __STANDOUT) ||
679 (nsp->attr & __UNDERSCORE))) {
680 putchar('\b');
681 tputs(UC, 0, __cputchar);
682 }
683 nsp++;
684 #ifdef DEBUG
685 __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
686 #endif
687 }
688 if (lx == wx) /* If no change. */
689 break;
690 lx = wx;
691 if (lx >= COLS && AM)
692 lx = COLS - 1;
693 else
694 if (wx >= win->maxx) {
695 domvcur(ly, lx, ly,
696 (int) (win->maxx - 1));
697 lx = win->maxx - 1;
698 }
699 #ifdef DEBUG
700 __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
701 #endif
702 }
703
704 return (OK);
705 }
706
707 /*
708 * domvcur --
709 * Do a mvcur, leaving attributes if necessary.
710 */
711 static void
712 domvcur(oy, ox, ny, nx)
713 int oy, ox, ny, nx;
714 {
715 __unsetattr(1);
716 __mvcur(oy, ox, ny, nx, 1);
717 }
718
719 /*
720 * Quickch() attempts to detect a pattern in the change of the window
721 * in order to optimize the change, e.g., scroll n lines as opposed to
722 * repainting the screen line by line.
723 */
724
725 static void
726 quickch(void)
727 {
728 #define THRESH (int) __virtscr->maxy / 4
729
730 __LINE *clp, *tmp1, *tmp2;
731 int bsize, curs, curw, starts, startw, i, j;
732 int n, target, cur_period, bot, top, sc_region;
733 __LDATA buf[1024];
734 u_int blank_hash;
735 attr_t bcolor;
736
737 #ifdef __GNUC__
738 curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
739 #endif
740 /*
741 * Find how many lines from the top of the screen are unchanged.
742 */
743 for (top = 0; top < __virtscr->maxy; top++)
744 if (__virtscr->lines[top]->flags & __FORCEPAINT ||
745 (__virtscr->lines[top]->flags & __ISDIRTY &&
746 (__virtscr->lines[top]->hash != curscr->lines[top]->hash
747 || memcmp(__virtscr->lines[top]->line,
748 curscr->lines[top]->line,
749 (size_t) __virtscr->maxx * __LDATASIZE) != 0)))
750 break;
751 else
752 __virtscr->lines[top]->flags &= ~__ISDIRTY;
753 /*
754 * Find how many lines from bottom of screen are unchanged.
755 */
756 for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
757 if (__virtscr->lines[bot]->flags & __FORCEPAINT ||
758 (__virtscr->lines[bot]->flags & __ISDIRTY &&
759 (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash
760 || memcmp(__virtscr->lines[bot]->line,
761 curscr->lines[bot]->line,
762 (size_t) __virtscr->maxx * __LDATASIZE) != 0)))
763 break;
764 else
765 __virtscr->lines[bot]->flags &= ~__ISDIRTY;
766
767 /*
768 * Work round an xterm bug where inserting lines causes all the
769 * inserted lines to be covered with the background colour we
770 * set on the first line (even if we unset it for subsequent
771 * lines).
772 */
773 bcolor = __virtscr->lines[min(top,
774 __virtscr->maxy - 1)]->line[0].attr & __COLOR;
775 for (i = top + 1, j = 0; i < bot; i++) {
776 if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
777 bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
778 attr & __COLOR;
779 j = i - top;
780 } else
781 break;
782 }
783 top += j;
784
785 #ifdef NO_JERKINESS
786 /*
787 * If we have a bottom unchanged region return. Scrolling the
788 * bottom region up and then back down causes a screen jitter.
789 * This will increase the number of characters sent to the screen
790 * but it looks better.
791 */
792 if (bot < __virtscr->maxy - 1)
793 return;
794 #endif /* NO_JERKINESS */
795
796 /*
797 * Search for the largest block of text not changed.
798 * Invariants of the loop:
799 * - Startw is the index of the beginning of the examined block in
800 * __virtscr.
801 * - Starts is the index of the beginning of the examined block in
802 * curscr.
803 * - Curw is the index of one past the end of the exmined block in
804 * __virtscr.
805 * - Curs is the index of one past the end of the exmined block in
806 * curscr.
807 * - bsize is the current size of the examined block.
808 */
809
810 for (bsize = bot - top; bsize >= THRESH; bsize--) {
811 for (startw = top; startw <= bot - bsize; startw++)
812 for (starts = top; starts <= bot - bsize;
813 starts++) {
814 for (curw = startw, curs = starts;
815 curs < starts + bsize; curw++, curs++)
816 if (__virtscr->lines[curw]->flags &
817 __FORCEPAINT ||
818 __virtscr->lines[curw]->hash !=
819 curscr->lines[curs]->hash)
820 break;
821 if (curs != starts + bsize)
822 continue;
823 for (curw = startw, curs = starts;
824 curs < starts + bsize; curw++, curs++)
825 if (memcmp(__virtscr->lines[curw]->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