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