refresh.c revision 1.26 1 /* $NetBSD: refresh.c,v 1.26 2000/04/28 17:11:51 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.26 2000/04/28 17:11:51 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 = 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
395 #ifdef __GNUC__
396 nlsp = lspc = 0; /* XXX gcc -Wuninitialized */
397 #endif
398 if (curwin)
399 win = curscr;
400 else
401 win = __virtscr;
402 /* Is the cursor still on the end of the last line? */
403 if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
404 domvcur(ly, lx, ly + 1, 0);
405 ly++;
406 lx = 0;
407 }
408 wx = *win->lines[wy]->firstchp;
409 if (wx < 0)
410 wx = 0;
411 else
412 if (wx >= win->maxx)
413 return (OK);
414 lch = *win->lines[wy]->lastchp;
415 if (lch < 0)
416 return (OK);
417 else
418 if (lch >= (int) win->maxx)
419 lch = win->maxx - 1;
420
421 if (curwin)
422 csp = ␣
423 else
424 csp = &curscr->lines[wy]->line[wx];
425
426 nsp = &win->lines[wy]->line[wx];
427 force = win->lines[wy]->flags & __FORCEPAINT;
428 win->lines[wy]->flags &= ~__FORCEPAINT;
429 if (CE && !curwin) {
430 cp = &win->lines[wy]->line[win->maxx - 1];
431 lspc = cp->attr & __COLOR;
432 while (cp->ch == ' ' && cp->attr == lspc)
433 if (cp-- <= win->lines[wy]->line)
434 break;
435 nlsp = cp - win->lines[wy]->line;
436 }
437 if (!curwin)
438 ce = CE;
439 else
440 ce = NULL;
441
442 if (force) {
443 if (CM) {
444 t_goto(_cursesi_genbuf, CM, lx, ly, cm_buff, 1023);
445 tputs(cm_buff, 0, __cputchar);
446 } else {
447 tputs(HO, 0, __cputchar);
448 __mvcur(0, 0, ly, lx, 1);
449 }
450 }
451
452 while (wx <= lch) {
453 if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
454 if (wx <= lch) {
455 while (wx <= lch &&
456 memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
457 nsp++;
458 if (!curwin)
459 ++csp;
460 ++wx;
461 }
462 continue;
463 }
464 break;
465 }
466 domvcur(ly, lx, wy, wx);
467
468 #ifdef DEBUG
469 __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
470 wx, ly, lx, wy, wx, force);
471 #endif
472 ly = wy;
473 lx = wx;
474 while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
475 && wx <= lch) {
476
477 if (ce != NULL &&
478 wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
479 /* Check for clear to end-of-line. */
480 cep = &curscr->lines[wy]->line[win->maxx - 1];
481 while (cep->ch == ' ' && cep->attr == lspc)
482 if (cep-- <= csp)
483 break;
484 clsp = cep - curscr->lines[wy]->line -
485 win->begx * __LDATASIZE;
486 #ifdef DEBUG
487 __CTRACE("makech: clsp = %d, nlsp = %d\n",
488 clsp, nlsp);
489 #endif
490 if (((clsp - nlsp >= strlen(CE) &&
491 clsp < win->maxx * __LDATASIZE) ||
492 wy == win->maxy - 1) &&
493 (!(lspc & __COLOR) ||
494 ((lspc & __COLOR) && UT))) {
495 __unsetattr(0);
496 if ((lspc & __COLOR) !=
497 (curscr->wattr & __COLOR)) {
498 __set_color(lspc);
499 curscr->wattr &= ~__COLOR;
500 curscr->wattr |= lspc & __COLOR;
501 }
502 tputs(CE, 0, __cputchar);
503 lx = wx + win->begx;
504 while (wx++ <= clsp) {
505 csp->ch = ' ';
506 csp->attr = lspc;
507 csp++;
508 }
509 return (OK);
510 }
511 ce = NULL;
512 }
513
514 /*
515 * Unset colour if appropriate. Check to see
516 * if we also turn off standout, underscore and
517 * attributes.
518 */
519 if (!(nsp->attr & __COLOR) &&
520 (curscr->wattr & __COLOR)) {
521 if (OC != NULL && CC == NULL)
522 tputs(OC, 0, __cputchar);
523 if (OP != NULL) {
524 tputs(OP, 0, __cputchar);
525 if (SE != NULL &&
526 !strcmp(OP, SE))
527 curscr->wattr &=
528 ~__STANDOUT;
529 if (UE != NULL &&
530 !strcmp(OP, UE))
531 curscr->wattr &=
532 ~__UNDERSCORE;
533 if (ME != NULL &&
534 !strcmp(OP, ME))
535 curscr->wattr &=
536 ~__TERMATTR;
537 }
538 curscr->wattr &= ~__COLOR;
539 }
540
541 /*
542 * Unset attributes as appropriate. Unset first
543 * so that the relevant attributes can be reset
544 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
545 * 'mp' and 'mr'). Check to see if we also turn off
546 * standout and attributes.
547 * XXX
548 * Assume that we also turn off colour, as we might
549 * have the entries ":me=\E[m:op=\E[39;49m:" (xterm)
550 * and we can't just do a string comparison on them.
551 */
552 if ((nsp->attr & __TERMATTR) !=
553 (curscr->wattr & __TERMATTR)) {
554 tputs(ME, 0, __cputchar);
555 curscr->wattr &= ~__TERMATTR;
556 if (SE != NULL && !strcmp(ME, SE))
557 curscr->wattr &= ~__STANDOUT;
558 if (UE != NULL && !strcmp(ME, UE))
559 curscr->wattr &= ~__UNDERSCORE;
560 curscr->wattr &= ~__COLOR;
561 }
562
563 /*
564 * Exit underscore mode if appropriate.
565 * Check to see if we also turn off standout and
566 * attributes. Assume that we also turn off colour.
567 */
568 if (!(nsp->attr & __UNDERSCORE) &&
569 (curscr->wattr & __UNDERSCORE)) {
570 tputs(UE, 0, __cputchar);
571 curscr->wattr &= ~__UNDERSCORE;
572 if (SE != NULL && !strcmp(UE, SE))
573 curscr->wattr &= ~__STANDOUT;
574 if (ME != NULL && !strcmp(UE, ME))
575 curscr->wattr &= ~__TERMATTR;
576 curscr->wattr &= ~__COLOR;
577 }
578
579 /*
580 * Enter/exit standout mode as appropriate.
581 * Check to see if we also turn off underscore and
582 * attributes. Assume that we also turn off colour.
583 * XXX
584 * Should use UC if SO/SE not available.
585 */
586 if (nsp->attr & __STANDOUT) {
587 if (!(curscr->wattr & __STANDOUT) &&
588 SO != NULL && SE != NULL) {
589 tputs(SO, 0, __cputchar);
590 curscr->wattr |= __STANDOUT;
591 }
592 } else {
593 if (curscr->wattr & __STANDOUT) {
594 tputs(SE, 0, __cputchar);
595 curscr->wattr &= ~__STANDOUT;
596 if (UE != NULL && !strcmp(SE, UE))
597 curscr->wattr &=
598 ~__UNDERSCORE;
599 if (ME != NULL && !strcmp(SE, ME))
600 curscr->wattr &= ~__TERMATTR;
601 curscr->wattr &= ~__COLOR;
602 }
603 }
604
605 /*
606 * Enter underscore mode if appropriate.
607 * XXX
608 * Should use UC if US/UE not available.
609 */
610 if ((nsp->attr & __UNDERSCORE) &&
611 !(curscr->wattr & __UNDERSCORE) &&
612 US != NULL && UE != NULL) {
613 tputs(US, 0, __cputchar);
614 curscr->wattr |= __UNDERSCORE;
615 }
616
617 /*
618 * Set other attributes as appropriate.
619 */
620 if ((nsp->attr & __BLINK) &&
621 !(curscr->wattr & __BLINK) &&
622 MB != NULL && ME != NULL) {
623 tputs(MB, 0, __cputchar);
624 curscr->wattr |= __BLINK;
625 }
626 if ((nsp->attr & __BOLD) &&
627 !(curscr->wattr & __BOLD) &&
628 MD != NULL && ME != NULL) {
629 tputs(MD, 0, __cputchar);
630 curscr->wattr |= __BOLD;
631 }
632 if ((nsp->attr & __DIM) &&
633 !(curscr->wattr & __DIM) &&
634 MH != NULL && ME != NULL) {
635 tputs(MH, 0, __cputchar);
636 curscr->wattr |= __DIM;
637 }
638 if ((nsp->attr & __BLANK) &&
639 !(curscr->wattr & __BLANK) &&
640 MK != NULL && ME != NULL) {
641 tputs(MK, 0, __cputchar);
642 curscr->wattr |= __BLANK;
643 }
644 if ((nsp->attr & __PROTECT) &&
645 !(curscr->wattr & __PROTECT) &&
646 MP != NULL && ME != NULL) {
647 tputs(MP, 0, __cputchar);
648 curscr->wattr |= __PROTECT;
649 }
650 if ((nsp->attr & __REVERSE) &&
651 !(curscr->wattr & __REVERSE) &&
652 MR != NULL && ME != NULL) {
653 tputs(MR, 0, __cputchar);
654 curscr->wattr |= __REVERSE;
655 }
656
657 /* Set/change colour as appropriate. */
658 if ((nsp->attr & __COLOR) &&
659 cO != NULL && (OC != NULL || OP != NULL)) {
660 if ((nsp->attr & __COLOR) !=
661 (curscr->wattr & __COLOR)) {
662 __set_color(nsp->attr);
663 curscr->wattr &= ~__COLOR;
664 curscr->wattr |= nsp->attr &
665 __COLOR;
666 }
667 }
668
669 /* Enter/exit altcharset mode as appropriate. */
670 if (nsp->attr & __ALTCHARSET) {
671 if (!(curscr->wattr & __ALTCHARSET) &&
672 AS != NULL && AE != NULL) {
673 tputs(AS, 0, __cputchar);
674 curscr->wattr |= __ALTCHARSET;
675 }
676 } else {
677 if (curscr->wattr & __ALTCHARSET) {
678 tputs(AE, 0, __cputchar);
679 curscr->wattr &= ~__ALTCHARSET;
680 }
681 }
682
683 wx++;
684 if (wx >= win->maxx &&
685 wy == win->maxy - 1 && !curwin)
686 if (win->flags & __SCROLLOK) {
687 if (win->flags & __ENDLINE){
688 __unsetattr(1);
689 }
690 if (!(win->flags & __SCROLLWIN)) {
691 if (!curwin) {
692 csp->attr = nsp->attr;
693 putchar((int)
694 (csp->ch =
695 nsp->ch));
696 } else
697 putchar((int) nsp->ch);
698 }
699 if (wx < curscr->maxx) {
700 domvcur(ly, wx,
701 (int) (win->maxy - 1),
702 (int) (win->maxx - 1));
703 }
704 ly = win->maxy - 1;
705 lx = win->maxx - 1;
706 return (OK);
707 }
708 if (wx < win->maxx || wy < win->maxy - 1 ||
709 !(win->flags & __SCROLLWIN)) {
710 if (!curwin) {
711 csp->attr = nsp->attr;
712 putchar((int) (csp->ch = nsp->ch));
713 csp++;
714 } else
715 putchar((int) nsp->ch);
716 }
717 #ifdef DEBUG
718 __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
719 #endif
720 if (UC && ((nsp->attr & __STANDOUT) ||
721 (nsp->attr & __UNDERSCORE))) {
722 putchar('\b');
723 tputs(UC, 0, __cputchar);
724 }
725 nsp++;
726 #ifdef DEBUG
727 __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
728 #endif
729 }
730 if (lx == wx) /* If no change. */
731 break;
732 lx = wx;
733 if (lx >= COLS && AM)
734 lx = COLS - 1;
735 else
736 if (wx >= win->maxx) {
737 domvcur(ly, lx, ly,
738 (int) (win->maxx - 1));
739 lx = win->maxx - 1;
740 }
741 #ifdef DEBUG
742 __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
743 #endif
744 }
745
746 /*
747 * Don't leave the screen with attributes set.
748 * XXX Should this be at the end of doupdate() and not here?
749 */
750 __unsetattr(0);
751 return (OK);
752 }
753
754 /*
755 * domvcur --
756 * Do a mvcur, leaving attributes if necessary.
757 */
758 static void
759 domvcur(oy, ox, ny, nx)
760 int oy, ox, ny, nx;
761 {
762 __unsetattr(1);
763 __mvcur(oy, ox, ny, nx, 1);
764 }
765
766 /*
767 * Quickch() attempts to detect a pattern in the change of the window
768 * in order to optimize the change, e.g., scroll n lines as opposed to
769 * repainting the screen line by line.
770 */
771
772 static void
773 quickch(void)
774 {
775 #define THRESH (int) __virtscr->maxy / 4
776
777 __LINE *clp, *tmp1, *tmp2;
778 int bsize, curs, curw, starts, startw, i, j;
779 int n, target, cur_period, bot, top, sc_region;
780 __LDATA buf[1024];
781 u_int blank_hash;
782 attr_t bcolor;
783
784 #ifdef __GNUC__
785 curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
786 #endif
787 /*
788 * Find how many lines from the top of the screen are unchanged.
789 */
790 for (top = 0; top < __virtscr->maxy; top++)
791 if (__virtscr->lines[top]->flags & __FORCEPAINT ||
792 __virtscr->lines[top]->hash != curscr->lines[top]->hash
793 || memcmp(__virtscr->lines[top]->line,
794 curscr->lines[top]->line,
795 (size_t) __virtscr->maxx * __LDATASIZE) != 0)
796 break;
797 else
798 __virtscr->lines[top]->flags &= ~__ISDIRTY;
799 /*
800 * Find how many lines from bottom of screen are unchanged.
801 */
802 for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
803 if (__virtscr->lines[bot]->flags & __FORCEPAINT ||
804 __virtscr->lines[bot]->hash != curscr->lines[bot]->hash
805 || memcmp(__virtscr->lines[bot]->line,
806 curscr->lines[bot]->line,
807 (size_t) __virtscr->maxx * __LDATASIZE) != 0)
808 break;
809 else
810 __virtscr->lines[bot]->flags &= ~__ISDIRTY;
811
812 /*
813 * Work round an xterm bug where inserting lines causes all the
814 * inserted lines to be covered with the background colour we
815 * set on the first line (even if we unset it for subsequent
816 * lines).
817 */
818 bcolor = __virtscr->lines[min(top,
819 __virtscr->maxy - 1)]->line[0].attr & __COLOR;
820 for (i = top + 1, j = 0; i < bot; i++) {
821 if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
822 bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
823 attr & __COLOR;
824 j = i - top;
825 } else
826 break;
827 }
828 top += j;
829
830 #ifdef NO_JERKINESS
831 /*
832 * If we have a bottom unchanged region return. Scrolling the
833 * bottom region up and then back down causes a screen jitter.
834 * This will increase the number of characters sent to the screen
835 * but it looks better.
836 */
837 if (bot < __virtscr->maxy - 1)
838 return;
839 #endif /* NO_JERKINESS */
840
841 /*
842 * Search for the largest block of text not changed.
843 * Invariants of the loop:
844 * - Startw is the index of the beginning of the examined block in
845 * __virtscr.
846 * - Starts is the index of the beginning of the examined block in
847 * curscr.
848 * - Curw is the index of one past the end of the exmined block in
849 * __virtscr.
850 * - Curs is the index of one past the end of the exmined block in
851 * curscr.
852 * - bsize is the current size of the examined block.
853 */
854
855 for (bsize = bot - top; bsize >= THRESH; bsize--) {
856 for (startw = top; startw <= bot - bsize; startw++)
857 for (starts = top; starts <= bot - bsize;
858 starts++) {
859 for (curw = startw, curs = starts;
860 curs < starts + bsize; curw++, curs++)
861 if (__virtscr->lines[curw]->flags &
862 __FORCEPAINT ||
863 (__virtscr->lines[curw]->hash !=
864 curscr->lines[curs]->hash ||
865 memcmp(__virtscr->lines[curw]
866 ->line,
867 curscr->lines[curs]->line,
868 (size_t) __virtscr->maxx *
869 __LDATASIZE) != 0))
870 break;
871 if (curs == starts + bsize)
872 goto done;
873 }
874 }
875 done:
876
877 /* Did not find anything */
878 if (bsize < THRESH)
879 return;
880
881 #ifdef DEBUG
882 __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
883 bsize, starts, startw, curw, curs, top, bot);
884 #endif
885
886 /*
887 * Make sure that there is no overlap between the bottom and top
888 * regions and the middle scrolled block.
889 */
890 if (bot < curs)
891 bot = curs - 1;
892 if (top > starts)
893 top = starts;
894
895 n = startw - starts;
896
897 #ifdef DEBUG
898 __CTRACE("#####################################\n");
899 for (i = 0; i < curscr->maxy; i++) {
900 __CTRACE("C: %d:", i);
901 __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
902 for (j = 0; j < curscr->maxx; j++)
903 __CTRACE("%c", curscr->lines[i]->line[j].ch);
904 __CTRACE("\n");
905 __CTRACE(" attr:");
906 for (j = 0; j < curscr->maxx; j++)
907 __CTRACE(" %x", curscr->lines[i]->line[j].attr);
908 __CTRACE("\n");
909 __CTRACE("W: %d:", i);
910 __CTRACE(" 0x%x \n", __virtscr->lines[i]->hash);
911 __CTRACE(" 0x%x ", __virtscr->lines[i]->flags);
912 for (j = 0; j < __virtscr->maxx; j++)
913 __CTRACE("%c", __virtscr->lines[i]->line[j].ch);
914 __CTRACE("\n");
915 __CTRACE(" attr:");
916 for (j = 0; j < __virtscr->maxx; j++)
917 __CTRACE(" %x", __virtscr->lines[i]->line[j].attr);
918 __CTRACE("\n");
919 }
920 #endif
921
922 /* So we don't have to call __hash() each time */
923 for (i = 0; i < __virtscr->maxx; i++) {
924 buf[i].ch = ' ';
925 buf[i].bch = ' ';
926 buf[i].attr = 0;
927 buf[i].battr = 0;
928 }
929 blank_hash = __hash((char *)(void *)buf,
930 (int) (__virtscr->maxx * __LDATASIZE));
931
932 /*
933 * Perform the rotation to maintain the consistency of curscr.
934 * This is hairy since we are doing an *in place* rotation.
935 * Invariants of the loop:
936 * - I is the index of the current line.
937 * - Target is the index of the target of line i.
938 * - Tmp1 points to current line (i).
939 * - Tmp2 and points to target line (target);
940 * - Cur_period is the index of the end of the current period.
941 * (see below).
942 *
943 * There are 2 major issues here that make this rotation non-trivial:
944 * 1. Scrolling in a scrolling region bounded by the top
945 * and bottom regions determined (whose size is sc_region).
946 * 2. As a result of the use of the mod function, there may be a
947 * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
948 * 0 to 2, which then causes all odd lines not to be rotated.
949 * To remedy this, an index of the end ( = beginning) of the
950 * current 'period' is kept, cur_period, and when it is reached,
951 * the next period is started from cur_period + 1 which is
952 * guaranteed not to have been reached since that would mean that
953 * all records would have been reached. (think about it...).
954 *
955 * Lines in the rotation can have 3 attributes which are marked on the
956 * line so that curscr is consistent with the visual screen.
957 * 1. Not dirty -- lines inside the scrolled block, top region or
958 * bottom region.
959 * 2. Blank lines -- lines in the differential of the scrolling
960 * region adjacent to top and bot regions
961 * depending on scrolling direction.
962 * 3. Dirty line -- all other lines are marked dirty.
963 */
964 sc_region = bot - top + 1;
965 i = top;
966 tmp1 = curscr->lines[top];
967 cur_period = top;
968 for (j = top; j <= bot; j++) {
969 target = (i - top + n + sc_region) % sc_region + top;
970 tmp2 = curscr->lines[target];
971 curscr->lines[target] = tmp1;
972 /* Mark block as clean and blank out scrolled lines. */
973 clp = curscr->lines[target];
974 #ifdef DEBUG
975 __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
976 n, startw, curw, i, target);
977 #endif
978 if ((target >= startw && target < curw) || target < top
979 || target > bot) {
980 #ifdef DEBUG
981 __CTRACE("-- notdirty\n");
982 #endif
983 __virtscr->lines[target]->flags &= ~__ISDIRTY;
984 } else
985 if ((n > 0 && target >= top && target < top + n) ||
986 (n < 0 && target <= bot && target > bot + n)) {
987 if (clp->hash != blank_hash || memcmp(clp->line,
988 buf, (size_t) __virtscr->maxx * __LDATASIZE) !=0) {
989 (void)memcpy(clp->line, buf,
990 (size_t) __virtscr->maxx * __LDATASIZE);
991 #ifdef DEBUG
992 __CTRACE("-- blanked out: dirty\n");
993 #endif
994 clp->hash = blank_hash;
995 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1, 0);
996 } else {
997 #ifdef DEBUG
998 __CTRACE(" -- blank line already: dirty\n");
999 #endif
1000 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1, 0);
1001 }
1002 } else {
1003 #ifdef DEBUG
1004 __CTRACE(" -- dirty\n");
1005 #endif
1006 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1, 0);
1007 }
1008 if (target == cur_period) {
1009 i = target + 1;
1010 tmp1 = curscr->lines[i];
1011 cur_period = i;
1012 } else {
1013 tmp1 = tmp2;
1014 i = target;
1015 }
1016 }
1017 #ifdef DEBUG
1018 __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1019 for (i = 0; i < curscr->maxy; i++) {
1020 __CTRACE("C: %d:", i);
1021 for (j = 0; j < curscr->maxx; j++)
1022 __CTRACE("%c", curscr->lines[i]->line[j].ch);
1023 __CTRACE("\n");
1024 __CTRACE("W: %d:", i);
1025 for (j = 0; j < __virtscr->maxx; j++)
1026 __CTRACE("%c", __virtscr->lines[i]->line[j].ch);
1027 __CTRACE("\n");
1028 }
1029 #endif
1030 if (n != 0)
1031 scrolln(starts, startw, curs, bot, top);
1032 }
1033
1034 /*
1035 * scrolln --
1036 * Scroll n lines, where n is starts - startw.
1037 */
1038 static void /* ARGSUSED */
1039 scrolln(starts, startw, curs, bot, top)
1040 int starts, startw, curs, bot, top;
1041 {
1042 int i, oy, ox, n;
1043
1044 oy = curscr->cury;
1045 ox = curscr->curx;
1046 n = starts - startw;
1047
1048 /*
1049 * XXX
1050 * The initial tests that set __noqch don't let us reach here unless
1051 * we have either CS + HO + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
1052 * scrolling can only shift the entire scrolling region, not just a
1053 * part of it, which means that the quickch() routine is going to be
1054 * sadly disappointed in us if we don't have CS as well.
1055 *
1056 * If CS, HO and SF/sf are set, can use the scrolling region. Because
1057 * the cursor position after CS is undefined, we need HO which gives us
1058 * the ability to move to somewhere without knowledge of the current
1059 * location of the cursor. Still call __mvcur() anyway, to update its
1060 * idea of where the cursor is.
1061 *
1062 * When the scrolling region has been set, the cursor has to be at the
1063 * last line of the region to make the scroll happen.
1064 *
1065 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1066 * or al/dl, and, some terminals have AL/DL, sf/sr, and CS, but not
1067 * SF/SR. So, if we're scrolling almost all of the screen, try and use
1068 * AL/DL, otherwise use the scrolling region. The "almost all" is a
1069 * shameless hack for vi.
1070 */
1071 if (n > 0) {
1072 if (CS != NULL && HO != NULL && (SF != NULL ||
1073 ((AL == NULL || DL == NULL ||
1074 top > 3 || bot + 3 < __virtscr->maxy) && sf != NULL))) {
1075 tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
1076 __mvcur(oy, ox, 0, 0, 1);
1077 tputs(HO, 0, __cputchar);
1078 __mvcur(0, 0, bot, 0, 1);
1079 if (SF != NULL)
1080 tputs(__tscroll(SF, n, 0), 0, __cputchar);
1081 else
1082 for (i = 0; i < n; i++)
1083 tputs(sf, 0, __cputchar);
1084 tputs(__tscroll(CS, 0, (int) __virtscr->maxy), 0, __cputchar);
1085 __mvcur(bot, 0, 0, 0, 1);
1086 tputs(HO, 0, __cputchar);
1087 __mvcur(0, 0, oy, ox, 1);
1088 return;
1089 }
1090
1091 /* Scroll up the block. */
1092 if (SF != NULL && top == 0) {
1093 __mvcur(oy, ox, bot, 0, 1);
1094 tputs(__tscroll(SF, n, 0), 0, __cputchar);
1095 } else
1096 if (DL != NULL) {
1097 __mvcur(oy, ox, top, 0, 1);
1098 tputs(__tscroll(DL, n, 0), 0, __cputchar);
1099 } else
1100 if (dl != NULL) {
1101 __mvcur(oy, ox, top, 0, 1);
1102 for (i = 0; i < n; i++)
1103 tputs(dl, 0, __cputchar);
1104 } else
1105 if (sf != NULL && top == 0) {
1106 __mvcur(oy, ox, bot, 0, 1);
1107 for (i = 0; i < n; i++)
1108 tputs(sf, 0, __cputchar);
1109 } else
1110 abort();
1111
1112 /* Push down the bottom region. */
1113 __mvcur(top, 0, bot - n + 1, 0, 1);
1114 if (AL != NULL)
1115 tputs(__tscroll(AL, n, 0), 0, __cputchar);
1116 else
1117 if (al != NULL)
1118 for (i = 0; i < n; i++)
1119 tputs(al, 0, __cputchar);
1120 else
1121 abort();
1122 __mvcur(bot - n + 1, 0, oy, ox, 1);
1123 } else {
1124 /*
1125 * !!!
1126 * n < 0
1127 *
1128 * If CS, HO and SR/sr are set, can use the scrolling region.
1129 * See the above comments for details.
1130 */
1131 if (CS != NULL && HO != NULL && (SR != NULL ||
1132 ((AL == NULL || DL == NULL ||
1133 top > 3 || bot + 3 < __virtscr->maxy) && sr != NULL))) {
1134 tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
1135 __mvcur(oy, ox, 0, 0, 1);
1136 tputs(HO, 0, __cputchar);
1137 __mvcur(0, 0, top, 0, 1);
1138
1139 if (SR != NULL)
1140 tputs(__tscroll(SR, -n, 0), 0, __cputchar);
1141 else
1142 for (i = n; i < 0; i++)
1143 tputs(sr, 0, __cputchar);
1144 tputs(__tscroll(CS, 0, (int) __virtscr->maxy), 0, __cputchar);
1145 __mvcur(top, 0, 0, 0, 1);
1146 tputs(HO, 0, __cputchar);
1147 __mvcur(0, 0, oy, ox, 1);
1148 return;
1149 }
1150
1151 /* Preserve the bottom lines. */
1152 __mvcur(oy, ox, bot + n + 1, 0, 1);
1153 if (SR != NULL && bot == __virtscr->maxy)
1154 tputs(__tscroll(SR, -n, 0), 0, __cputchar);
1155 else
1156 if (DL != NULL)
1157 tputs(__tscroll(DL, -n, 0), 0, __cputchar);
1158 else
1159 if (dl != NULL)
1160 for (i = n; i < 0; i++)
1161 tputs(dl, 0, __cputchar);
1162 else
1163 if (sr != NULL && bot == __virtscr->maxy)
1164 for (i = n; i < 0; i++)
1165 tputs(sr, 0, __cputchar);
1166 else
1167 abort();
1168
1169 /* Scroll the block down. */
1170 __mvcur(bot + n + 1, 0, top, 0, 1);
1171 if (AL != NULL)
1172 tputs(__tscroll(AL, -n, 0), 0, __cputchar);
1173 else
1174 if (al != NULL)
1175 for (i = n; i < 0; i++)
1176 tputs(al, 0, __cputchar);
1177 else
1178 abort();
1179 __mvcur(top, 0, oy, ox, 1);
1180 }
1181 }
1182
1183 /*
1184 * __unsetattr --
1185 * Unset attributes on curscr. Leave standout, attribute and colour
1186 * modes if necessary (!MS). Always leave altcharset (xterm at least
1187 * ignores a cursor move if we don't).
1188 */
1189 void /* ARGSUSED */
1190 __unsetattr(int checkms)
1191 {
1192 int isms;
1193
1194 if (checkms)
1195 if (!MS) {
1196 isms = 1;
1197 } else {
1198 isms = 0;
1199 }
1200 else
1201 isms = 1;
1202 #ifdef DEBUG
1203 __CTRACE("__unsetattr: checkms = %d, MS = %s, wattr = %08x\n",
1204 checkms, MS ? "TRUE" : "FALSE", curscr->wattr);
1205 #endif
1206
1207 /*
1208 * Don't leave the screen in standout mode (check against MS).
1209 * Check to see if we also turn off underscore and attributes.
1210 * Assume that we also turn off colour.
1211 */
1212 if (curscr->wattr & __STANDOUT && isms) {
1213 tputs(SE, 0, __cputchar);
1214 curscr->wattr &= ~__STANDOUT;
1215 if (UE != NULL && !strcmp(SE, UE))
1216 curscr->wattr &= ~__UNDERSCORE;
1217 if (ME != NULL && !strcmp(SE, ME))
1218 curscr->wattr &= ~__TERMATTR;
1219 curscr->wattr &= ~__COLOR;
1220 }
1221 /*
1222 * Don't leave the screen in underscore mode (check against MS).
1223 * Check to see if we also turn off attributes. Assume that we
1224 * also turn off colour.
1225 */
1226 if (curscr->wattr & __UNDERSCORE && isms) {
1227 tputs(UE, 0, __cputchar);
1228 curscr->wattr &= ~__UNDERSCORE;
1229 if (ME != NULL && !strcmp(UE, ME))
1230 curscr->wattr &= ~__TERMATTR;
1231 curscr->wattr &= ~__COLOR;
1232 }
1233 /*
1234 * Don't leave the screen with attributes set (check against MS).
1235 * Assume that also turn off colour.
1236 */
1237 if (curscr->wattr & __TERMATTR && isms) {
1238 tputs(ME, 0, __cputchar);
1239 curscr->wattr &= ~__TERMATTR;
1240 curscr->wattr &= ~__COLOR;
1241 }
1242 /* Don't leave the screen with altcharset set (don't check MS). */
1243 if (curscr->wattr & __ALTCHARSET) {
1244 tputs(AE, 0, __cputchar);
1245 curscr->wattr &= ~__ALTCHARSET;
1246 }
1247 /* Don't leave the screen with colour set (check against MS). */
1248 if (curscr->wattr & __COLOR && isms) {
1249 if (OC != NULL && CC == NULL)
1250 tputs(OC, 0, __cputchar);
1251 if (OP != NULL)
1252 tputs(OP, 0, __cputchar);
1253 curscr->wattr &= ~__COLOR;
1254 }
1255 }
1256