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