pkgin_all: script to auto-install all packages
[minix.git] / lib / libcurses / refresh.c
blobfd7a9c42dcb6a0d61da33294ba086ba13520bba0
1 /* $NetBSD: refresh.c,v 1.76 2012/04/21 11:33:16 blymn Exp $ */
3 /*
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)refresh.c 8.7 (Berkeley) 8/13/94";
36 #else
37 __RCSID("$NetBSD: refresh.c,v 1.76 2012/04/21 11:33:16 blymn Exp $");
38 #endif
39 #endif /* not lint */
41 #include <stdlib.h>
42 #include <string.h>
44 #include "curses.h"
45 #include "curses_private.h"
47 static void domvcur(int, int, int, int);
48 static int makech(int);
49 static void quickch(void);
50 static void scrolln(int, int, int, int, int);
52 static int _cursesi_wnoutrefresh(SCREEN *, WINDOW *,
53 int, int, int, int, int, int);
55 #ifdef HAVE_WCHAR
56 int cellcmp( __LDATA *, __LDATA * );
57 int linecmp( __LDATA *, __LDATA *, size_t );
58 #endif /* HAVE_WCHAR */
60 #ifndef _CURSES_USE_MACROS
63 * refresh --
64 * Make the current screen look like "stdscr" over the area covered by
65 * stdscr.
67 int
68 refresh(void)
70 return wrefresh(stdscr);
73 #endif
76 * wnoutrefresh --
77 * Add the contents of "win" to the virtual window.
79 int
80 wnoutrefresh(WINDOW *win)
82 #ifdef DEBUG
83 __CTRACE(__CTRACE_REFRESH, "wnoutrefresh: win %p\n", win);
84 #endif
86 return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
87 win->begx, win->maxy, win->maxx);
91 * pnoutrefresh --
92 * Add the contents of "pad" to the virtual window.
94 int
95 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
96 int smaxy, int smaxx)
98 int pmaxy, pmaxx;
100 #ifdef DEBUG
101 __CTRACE(__CTRACE_REFRESH, "pnoutrefresh: pad %p, flags 0x%08x\n",
102 pad, pad->flags);
103 __CTRACE(__CTRACE_REFRESH,
104 "pnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
105 pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
106 #endif
108 /* SUS says if these are negative, they should be treated as zero */
109 if (pbegy < 0)
110 pbegy = 0;
111 if (pbegx < 0)
112 pbegx = 0;
113 if (sbegy < 0)
114 sbegy = 0;
115 if (sbegx < 0)
116 sbegx = 0;
118 /* Calculate rectangle on pad - used by _cursesi_wnoutrefresh */
119 pmaxy = pbegy + smaxy - sbegy + 1;
120 pmaxx = pbegx + smaxx - sbegx + 1;
122 /* Check rectangle fits in pad */
123 if (pmaxy > pad->maxy - pad->begy)
124 pmaxy = pad->maxy - pad->begy;
125 if (pmaxx > pad->maxx - pad->begx)
126 pmaxx = pad->maxx - pad->begx;
128 if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
129 return ERR;
131 return _cursesi_wnoutrefresh(_cursesi_screen, pad,
132 pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
133 pad->begx + sbegx, pmaxy, pmaxx);
137 * _cursesi_wnoutrefresh --
138 * Does the grunt work for wnoutrefresh to the given screen.
139 * Copies the part of the window given by the rectangle
140 * (begy, begx) to (maxy, maxx) at screen position (wbegy, wbegx).
143 _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
144 int wbegy, int wbegx, int maxy, int maxx)
147 short sy, wy, wx, y_off, x_off, mx;
148 __LINE *wlp, *vlp;
149 WINDOW *sub_win, *orig;
151 #ifdef DEBUG
152 __CTRACE(__CTRACE_REFRESH, "_wnoutrefresh: win %p, flags 0x%08x\n",
153 win, win->flags);
154 __CTRACE(__CTRACE_REFRESH,
155 "_wnoutrefresh: (%d, %d), (%d, %d), (%d, %d)\n",
156 begy, begx, wbegy, wbegx, maxy, maxx);
157 #endif
159 if (screen->curwin)
160 return OK;
163 * Recurse through any sub-windows, mark as dirty lines on the parent
164 * window that are dirty on the sub-window and clear the dirty flag on
165 * the sub-window.
167 if (win->orig == 0) {
168 orig = win;
169 for (sub_win = win->nextp; sub_win != win;
170 sub_win = sub_win->nextp) {
171 #ifdef DEBUG
172 __CTRACE(__CTRACE_REFRESH,
173 "wnout_refresh: win %p, sub_win %p\n",
174 orig, sub_win);
175 #endif
176 for (sy = 0; sy < sub_win->maxy; sy++) {
177 if (sub_win->alines[sy]->flags & __ISDIRTY) {
178 orig->alines[sy + sub_win->begy - orig->begy]->flags
179 |= __ISDIRTY;
180 sub_win->alines[sy]->flags
181 &= ~__ISDIRTY;
187 /* Check that cursor position on "win" is valid for "__virtscr" */
188 if (win->cury + wbegy - begy < screen->__virtscr->maxy &&
189 win->cury + wbegy - begy >= 0 && win->cury < maxy - begy)
190 screen->__virtscr->cury = win->cury + wbegy - begy;
191 if (win->curx + wbegx - begx < screen->__virtscr->maxx &&
192 win->curx + wbegx - begx >= 0 && win->curx < maxx - begx)
193 screen->__virtscr->curx = win->curx + wbegx - begx;
195 /* Copy the window flags from "win" to "__virtscr" */
196 if (win->flags & __CLEAROK) {
197 if (win->flags & __FULLWIN)
198 screen->__virtscr->flags |= __CLEAROK;
199 win->flags &= ~__CLEAROK;
201 screen->__virtscr->flags &= ~__LEAVEOK;
202 screen->__virtscr->flags |= win->flags;
204 for (wy = begy, y_off = wbegy; wy < maxy &&
205 y_off < screen->__virtscr->maxy; wy++, y_off++) {
206 wlp = win->alines[wy];
207 #ifdef DEBUG
208 __CTRACE(__CTRACE_REFRESH,
209 "_wnoutrefresh: wy %d\tf %d\tl %d\tflags %x\n",
210 wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
211 #endif
212 if ((wlp->flags & __ISDIRTY) == 0)
213 continue;
214 vlp = screen->__virtscr->alines[y_off];
216 if (*wlp->firstchp < maxx + win->ch_off &&
217 *wlp->lastchp >= win->ch_off) {
218 /* Set start column */
219 wx = begx;
220 x_off = wbegx;
221 if (*wlp->firstchp - win->ch_off > 0) {
222 wx += *wlp->firstchp - win->ch_off;
223 x_off += *wlp->firstchp - win->ch_off;
225 /* Set finish column */
226 mx = maxx;
227 if (mx > *wlp->lastchp - win->ch_off + 1)
228 mx = *wlp->lastchp - win->ch_off + 1;
229 if (x_off + (mx - wx) > __virtscr->maxx)
230 mx -= (x_off + maxx) - __virtscr->maxx;
231 /* Copy line from "win" to "__virtscr". */
232 while (wx < mx) {
233 #ifdef DEBUG
234 __CTRACE(__CTRACE_REFRESH,
235 "_wnoutrefresh: copy from %d, "
236 "%d to %d, %d\n",
237 wy, wx, y_off, x_off);
238 #endif
239 /* Copy character */
240 vlp->line[x_off].ch = wlp->line[wx].ch;
241 /* Copy attributes */
242 vlp->line[x_off].attr = wlp->line[wx].attr;
243 /* Check for nca conflict with colour */
244 if ((vlp->line[x_off].attr & __COLOR) &&
245 (vlp->line[x_off].attr &
246 _cursesi_screen->nca))
247 vlp->line[x_off].attr &= ~__COLOR;
248 #ifdef HAVE_WCHAR
249 if (wlp->line[wx].ch
250 == (wchar_t)btowc((int) win->bch)) {
251 vlp->line[x_off].ch = win->bch;
252 SET_WCOL( vlp->line[x_off], 1 );
253 if (_cursesi_copy_nsp(win->bnsp,
254 &vlp->line[x_off])
255 == ERR)
256 return ERR;
258 #endif /* HAVE_WCHAR */
259 wx++;
260 x_off++;
263 /* Set flags on "__virtscr" and unset on "win". */
264 if (wlp->flags & __ISPASTEOL)
265 vlp->flags |= __ISPASTEOL;
266 else
267 vlp->flags &= ~__ISPASTEOL;
268 if (wlp->flags & __ISDIRTY)
269 vlp->flags |= __ISDIRTY;
271 #ifdef DEBUG
272 __CTRACE(__CTRACE_REFRESH,
273 "win: firstch = %d, lastch = %d\n",
274 *wlp->firstchp, *wlp->lastchp);
275 #endif
276 /* Set change pointers on "__virtscr". */
277 if (*vlp->firstchp >
278 *wlp->firstchp + wbegx - win->ch_off)
279 *vlp->firstchp =
280 *wlp->firstchp + wbegx - win->ch_off;
281 if (*vlp->lastchp <
282 *wlp->lastchp + wbegx - win->ch_off)
283 *vlp->lastchp =
284 *wlp->lastchp + wbegx - win->ch_off;
285 #ifdef DEBUG
286 __CTRACE(__CTRACE_REFRESH,
287 "__virtscr: firstch = %d, lastch = %d\n",
288 *vlp->firstchp, *vlp->lastchp);
289 #endif
291 * Unset change pointers only if a window, as a pad
292 * can be displayed again without any of the contents
293 * changing.
295 if (!(win->flags & __ISPAD)) {
296 /* Set change pointers on "win". */
297 if (*wlp->firstchp >= win->ch_off)
298 *wlp->firstchp = maxx + win->ch_off;
299 if (*wlp->lastchp < maxx + win->ch_off)
300 *wlp->lastchp = win->ch_off;
301 if ((*wlp->lastchp < *wlp->firstchp) ||
302 (*wlp->firstchp >= maxx + win->ch_off) ||
303 (*wlp->lastchp <= win->ch_off)) {
304 #ifdef DEBUG
305 __CTRACE(__CTRACE_REFRESH,
306 "_wnoutrefresh: "
307 "line %d notdirty\n", wy);
308 #endif
309 wlp->flags &= ~__ISDIRTY;
314 return OK;
318 * wrefresh --
319 * Make the current screen look like "win" over the area covered by
320 * win.
323 wrefresh(WINDOW *win)
325 int retval;
327 #ifdef DEBUG
328 __CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
329 #endif
331 _cursesi_screen->curwin = (win == _cursesi_screen->curscr);
332 if (!_cursesi_screen->curwin)
333 retval = _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0,
334 win->begy, win->begx, win->maxy, win->maxx);
335 else
336 retval = OK;
337 if (retval == OK) {
338 retval = doupdate();
339 if (!(win->flags & __LEAVEOK)) {
340 win->cury = max(0, curscr->cury - win->begy);
341 win->curx = max(0, curscr->curx - win->begx);
344 _cursesi_screen->curwin = 0;
345 return(retval);
349 * prefresh --
350 * Make the current screen look like "pad" over the area coverd by
351 * the specified area of pad.
354 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
355 int smaxy, int smaxx)
357 int retval;
359 #ifdef DEBUG
360 __CTRACE(__CTRACE_REFRESH, "prefresh: pad %p, flags 0x%08x\n",
361 pad, pad->flags);
362 #endif
363 /* Retain values in case pechochar() is called. */
364 pad->pbegy = pbegy;
365 pad->pbegx = pbegx;
366 pad->sbegy = sbegy;
367 pad->sbegx = sbegx;
368 pad->smaxy = smaxy;
369 pad->smaxx = smaxx;
371 /* Use pnoutrefresh() to avoid duplicating code here */
372 retval = pnoutrefresh(pad, pbegy, pbegx, sbegy, sbegx, smaxy, smaxx);
373 if (retval == OK) {
374 retval = doupdate();
375 if (!(pad->flags & __LEAVEOK)) {
376 pad->cury = max(0, curscr->cury - pad->begy);
377 pad->curx = max(0, curscr->curx - pad->begx);
380 return(retval);
384 * doupdate --
385 * Make the current screen look like the virtual window "__virtscr".
388 doupdate(void)
390 WINDOW *win;
391 __LINE *wlp;
392 short wy;
393 int dnum;
394 #ifdef HAVE_WCHAR
395 __LDATA *lp;
396 nschar_t *np;
397 int x;
398 #endif /* HAVE_WCHAR */
400 /* Check if we need to restart ... */
401 if (_cursesi_screen->endwin)
402 __restartwin();
404 if (_cursesi_screen->curwin)
405 win = curscr;
406 else
407 win = _cursesi_screen->__virtscr;
409 /* Initialize loop parameters. */
410 _cursesi_screen->ly = curscr->cury;
411 _cursesi_screen->lx = curscr->curx;
412 wy = 0;
414 if (!_cursesi_screen->curwin) {
415 for (wy = 0; wy < win->maxy; wy++) {
416 wlp = win->alines[wy];
417 if (wlp->flags & __ISDIRTY) {
418 #ifndef HAVE_WCHAR
419 wlp->hash = __hash(wlp->line,
420 (size_t)(win->maxx * __LDATASIZE));
421 #else
422 wlp->hash = 0;
423 for ( x = 0; x < win->maxx; x++ ) {
424 lp = &wlp->line[ x ];
425 wlp->hash = __hash_more( &lp->ch,
426 sizeof( wchar_t ), wlp->hash );
427 wlp->hash = __hash_more( &lp->attr,
428 sizeof( attr_t ), wlp->hash );
429 np = lp->nsp;
430 if (np) {
431 while ( np ) {
432 wlp->hash
433 = __hash_more(
434 &np->ch,
435 sizeof(wchar_t),
436 wlp->hash );
437 np = np->next;
441 #endif /* HAVE_WCHAR */
446 if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
447 _cursesi_screen->curwin) {
448 if (curscr->wattr & __COLOR)
449 __unsetattr(0);
450 tputs(clear_screen, 0, __cputchar);
451 _cursesi_screen->ly = 0;
452 _cursesi_screen->lx = 0;
453 if (!_cursesi_screen->curwin) {
454 curscr->flags &= ~__CLEAROK;
455 curscr->cury = 0;
456 curscr->curx = 0;
457 werase(curscr);
459 __touchwin(win);
460 win->flags &= ~__CLEAROK;
462 if (!cursor_address) {
463 if (win->curx != 0)
464 __cputchar('\n');
465 if (!_cursesi_screen->curwin)
466 werase(curscr);
468 #ifdef DEBUG
469 __CTRACE(__CTRACE_REFRESH, "doupdate: (%p): curwin = %d\n", win,
470 _cursesi_screen->curwin);
471 __CTRACE(__CTRACE_REFRESH, "doupdate: \tfirstch\tlastch\n");
472 #endif
474 if (!_cursesi_screen->curwin) {
476 * Invoke quickch() only if more than a quarter of the lines
477 * in the window are dirty.
479 for (wy = 0, dnum = 0; wy < win->maxy; wy++)
480 if (win->alines[wy]->flags & __ISDIRTY)
481 dnum++;
482 if (!__noqch && dnum > (int) win->maxy / 4)
483 quickch();
486 #ifdef DEBUG
488 int i, j;
490 __CTRACE(__CTRACE_REFRESH,
491 "#####################################\n");
492 __CTRACE(__CTRACE_REFRESH,
493 "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
494 stdscr, curscr, _cursesi_screen->__virtscr);
495 for (i = 0; i < curscr->maxy; i++) {
496 __CTRACE(__CTRACE_REFRESH, "C: %d:", i);
497 __CTRACE(__CTRACE_REFRESH, " 0x%x \n",
498 curscr->alines[i]->hash);
499 for (j = 0; j < curscr->maxx; j++)
500 __CTRACE(__CTRACE_REFRESH, "%c",
501 curscr->alines[i]->line[j].ch);
502 __CTRACE(__CTRACE_REFRESH, "\n");
503 __CTRACE(__CTRACE_REFRESH, " attr:");
504 for (j = 0; j < curscr->maxx; j++)
505 __CTRACE(__CTRACE_REFRESH, " %x",
506 curscr->alines[i]->line[j].attr);
507 __CTRACE(__CTRACE_REFRESH, "\n");
508 __CTRACE(__CTRACE_REFRESH, "W: %d:", i);
509 __CTRACE(__CTRACE_REFRESH, " 0x%x \n",
510 win->alines[i]->hash);
511 __CTRACE(__CTRACE_REFRESH, " 0x%x ",
512 win->alines[i]->flags);
513 for (j = 0; j < win->maxx; j++)
514 __CTRACE(__CTRACE_REFRESH, "%c",
515 win->alines[i]->line[j].ch);
516 __CTRACE(__CTRACE_REFRESH, "\n");
517 __CTRACE(__CTRACE_REFRESH, " attr:");
518 for (j = 0; j < win->maxx; j++)
519 __CTRACE(__CTRACE_REFRESH, " %x",
520 win->alines[i]->line[j].attr);
521 __CTRACE(__CTRACE_REFRESH, "\n");
522 #ifdef HAVE_WCHAR
523 __CTRACE(__CTRACE_REFRESH, " nsp:");
524 for (j = 0; j < curscr->maxx; j++)
525 __CTRACE(__CTRACE_REFRESH, " %p",
526 win->alines[i]->line[j].nsp);
527 __CTRACE(__CTRACE_REFRESH, "\n");
528 __CTRACE(__CTRACE_REFRESH, " bnsp:");
529 for (j = 0; j < curscr->maxx; j++)
530 __CTRACE(__CTRACE_REFRESH, " %p",
531 win->bnsp);
532 __CTRACE(__CTRACE_REFRESH, "\n");
533 #endif /* HAVE_WCHAR */
536 #endif /* DEBUG */
538 for (wy = 0; wy < win->maxy; wy++) {
539 wlp = win->alines[wy];
540 /* XXX: remove this debug */
541 #ifdef DEBUG
542 __CTRACE(__CTRACE_REFRESH,
543 "doupdate: wy %d\tf: %d\tl:%d\tflags %x\n",
544 wy, *wlp->firstchp, *wlp->lastchp, wlp->flags);
545 #endif /* DEBUG */
546 if (!_cursesi_screen->curwin)
547 curscr->alines[wy]->hash = wlp->hash;
548 if (wlp->flags & __ISDIRTY) {
549 #ifdef DEBUG
550 __CTRACE(__CTRACE_REFRESH,
551 "doupdate: [ISDIRTY]wy:%d\tf:%d\tl:%d\n", wy,
552 *wlp->firstchp, *wlp->lastchp);
553 #endif /* DEBUG */
554 if (makech(wy) == ERR)
555 return (ERR);
556 else {
557 if (*wlp->firstchp >= 0)
558 *wlp->firstchp = win->maxx;
559 if (*wlp->lastchp < win->maxx)
560 *wlp->lastchp = 0;
561 if (*wlp->lastchp < *wlp->firstchp) {
562 #ifdef DEBUG
563 __CTRACE(__CTRACE_REFRESH,
564 "doupdate: line %d notdirty\n", wy);
565 #endif /* DEBUG */
566 wlp->flags &= ~__ISDIRTY;
571 #ifdef DEBUG
572 __CTRACE(__CTRACE_REFRESH, "\t%d\t%d\n",
573 *wlp->firstchp, *wlp->lastchp);
574 #endif /* DEBUG */
577 #ifdef DEBUG
578 __CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
579 _cursesi_screen->ly, _cursesi_screen->lx);
580 #endif /* DEBUG */
582 if (_cursesi_screen->curwin)
583 domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
584 win->cury, win->curx);
585 else {
586 if (win->flags & __LEAVEOK) {
587 curscr->cury = _cursesi_screen->ly;
588 curscr->curx = _cursesi_screen->lx;
589 } else {
590 domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
591 win->cury, win->curx);
592 curscr->cury = win->cury;
593 curscr->curx = win->curx;
597 /* Don't leave the screen with attributes set. */
598 __unsetattr(0);
599 #ifdef DEBUG
600 #ifdef HAVE_WCHAR
602 int i, j;
604 __CTRACE(__CTRACE_REFRESH,
605 "***********after*****************\n");
606 __CTRACE(__CTRACE_REFRESH,
607 "stdscr(%p)-curscr(%p)-__virtscr(%p)\n",
608 stdscr, curscr, _cursesi_screen->__virtscr);
609 for (i = 0; i < curscr->maxy; i++) {
610 for (j = 0; j < curscr->maxx; j++)
611 __CTRACE(__CTRACE_REFRESH,
612 "[%d,%d](%x,%x,%p)-(%x,%x,%p)\n",
613 i, j,
614 curscr->alines[i]->line[j].ch,
615 curscr->alines[i]->line[j].attr,
616 curscr->alines[i]->line[j].nsp,
617 _cursesi_screen->__virtscr->alines[i]->line[j].ch,
618 _cursesi_screen->__virtscr->alines[i]->line[j].attr,
619 _cursesi_screen->__virtscr->alines[i]->line[j].nsp);
622 #endif /* HAVE_WCHAR */
623 #endif /* DEBUG */
624 return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
628 * makech --
629 * Make a change on the screen.
631 static int
632 makech(int wy)
634 WINDOW *win;
635 static __LDATA blank;
636 __LDATA *nsp, *csp, *cp, *cep;
637 size_t clsp, nlsp; /* Last space in lines. */
638 int lch, wx;
639 const char *ce;
640 attr_t lspc; /* Last space colour */
641 attr_t off, on;
643 #ifdef __GNUC__
644 nlsp = lspc = 0; /* XXX gcc -Wuninitialized */
645 #endif
646 if (_cursesi_screen->curwin)
647 win = curscr;
648 else
649 win = __virtscr;
650 #ifdef HAVE_WCHAR
651 blank.ch = ( wchar_t )btowc(( int ) win->bch );
652 blank.attr = 0;
653 if (_cursesi_copy_nsp(win->bnsp, &blank) == ERR)
654 return ERR;
655 SET_WCOL( blank, 1 );
656 #endif /* HAVE_WCHAR */
657 #ifdef DEBUG
658 #if HAVE_WCHAR
660 int x;
661 __LDATA *lp, *vlp;
663 __CTRACE(__CTRACE_REFRESH,
664 "[makech-before]wy=%d,curscr(%p)-__virtscr(%p)\n",
665 wy, curscr, __virtscr);
666 for (x = 0; x < curscr->maxx; x++) {
667 lp = &curscr->alines[wy]->line[x];
668 vlp = &__virtscr->alines[wy]->line[x];
669 __CTRACE(__CTRACE_REFRESH,
670 "[%d,%d](%x,%x,%x,%x,%p)-"
671 "(%x,%x,%x,%x,%p)\n",
672 wy, x, lp->ch, lp->attr,
673 win->bch, win->battr, lp->nsp,
674 vlp->ch, vlp->attr,
675 win->bch, win->battr, vlp->nsp);
678 #endif /* HAVE_WCHAR */
679 #endif /* DEBUG */
680 /* Is the cursor still on the end of the last line? */
681 if (wy > 0 && curscr->alines[wy - 1]->flags & __ISPASTEOL) {
682 domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
683 _cursesi_screen->ly + 1, 0);
684 _cursesi_screen->ly++;
685 _cursesi_screen->lx = 0;
687 wx = *win->alines[wy]->firstchp;
688 if (wx < 0)
689 wx = 0;
690 else
691 if (wx >= win->maxx)
692 return (OK);
693 lch = *win->alines[wy]->lastchp;
694 if (lch < 0)
695 return (OK);
696 else
697 if (lch >= (int) win->maxx)
698 lch = win->maxx - 1;
700 if (_cursesi_screen->curwin) {
701 csp = &blank;
702 #ifdef DEBUG
703 __CTRACE(__CTRACE_REFRESH, "makech: csp is blank\n");
704 #endif /* DEBUG */
705 } else {
706 csp = &curscr->alines[wy]->line[wx];
707 #ifdef DEBUG
708 __CTRACE(__CTRACE_REFRESH,
709 "makech: csp is on curscr:(%d,%d)\n", wy, wx);
710 #endif /* DEBUG */
713 nsp = &win->alines[wy]->line[wx];
714 #ifdef DEBUG
715 if ( _cursesi_screen->curwin )
716 __CTRACE(__CTRACE_REFRESH,
717 "makech: nsp is at curscr:(%d,%d)\n", wy, wx);
718 else
719 __CTRACE(__CTRACE_REFRESH,
720 "makech: nsp is at __virtscr:(%d,%d)\n", wy, wx);
721 #endif /* DEBUG */
722 if (clr_eol && !_cursesi_screen->curwin) {
723 cp = &win->alines[wy]->line[win->maxx - 1];
724 lspc = cp->attr & __COLOR;
725 #ifndef HAVE_WCHAR
726 while (cp->ch == ' ' && cp->attr == lspc) /* XXX */
727 if (cp-- <= win->alines[wy]->line)
728 break;
729 #else
730 while (cp->ch == ( wchar_t )btowc(( int )' ' )
731 && ( cp->attr & WA_ATTRIBUTES ) == lspc)
732 if (cp-- <= win->alines[wy]->line)
733 break;
734 #endif /* HAVE_WCHAR */
735 if (win->alines[wy]->line > cp)
736 nlsp = 0;
737 else
738 nlsp = cp - win->alines[wy]->line;
740 if (!_cursesi_screen->curwin)
741 ce = clr_eol;
742 else
743 ce = NULL;
745 while (wx <= lch) {
746 #ifdef DEBUG
747 __CTRACE(__CTRACE_REFRESH, "makech: wx=%d,lch=%d\n", wx, lch);
748 #endif /* DEBUG */
749 #ifndef HAVE_WCHAR
750 if (memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
751 if (wx <= lch) {
752 while (wx <= lch &&
753 memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
754 nsp++;
755 if (!_cursesi_screen->curwin)
756 ++csp;
757 ++wx;
759 continue;
761 break;
763 #else
764 #ifdef DEBUG
765 __CTRACE(__CTRACE_REFRESH, "makech: nsp=(%x,%x,%x,%x,%p)\n",
766 nsp->ch, nsp->attr, win->bch, win->battr, nsp->nsp);
767 __CTRACE(__CTRACE_REFRESH, "makech: csp=(%x,%x,%x,%x,%p)\n",
768 csp->ch, csp->attr, win->bch, win->battr, csp->nsp);
769 #endif /* DEBUG */
770 if (((nsp->attr & __WCWIDTH) != __WCWIDTH) &&
771 cellcmp(nsp, csp)) {
772 if (wx <= lch) {
773 while (wx <= lch && cellcmp( csp, nsp )) {
774 nsp++;
775 if (!_cursesi_screen->curwin)
776 ++csp;
777 ++wx;
779 continue;
781 break;
783 #endif /* HAVE_WCHAR */
784 domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
786 #ifdef DEBUG
787 __CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
788 "lx = %d, newy = %d, newx = %d\n",
789 wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
790 #endif
791 _cursesi_screen->ly = wy;
792 _cursesi_screen->lx = wx;
793 #ifndef HAVE_WCHAR
794 while (wx <= lch && memcmp(nsp, csp, sizeof(__LDATA)) != 0) {
795 if (ce != NULL &&
796 wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
797 #else
798 while (!cellcmp(nsp, csp) && wx <= lch) {
799 if (ce != NULL && wx >= nlsp
800 && nsp->ch == (wchar_t)btowc((int)' ') /* XXX */
801 && (nsp->attr & WA_ATTRIBUTES) == lspc) {
803 #endif
804 /* Check for clear to end-of-line. */
805 cep = &curscr->alines[wy]->line[win->maxx - 1];
806 #ifndef HAVE_WCHAR
807 while (cep->ch == ' ' && cep->attr == lspc) /* XXX */
808 #else
809 while (cep->ch == (wchar_t)btowc((int)' ')
810 && (cep->attr & WA_ATTRIBUTES) == lspc)
811 #endif /* HAVE_WCHAR */
812 if (cep-- <= csp)
813 break;
814 clsp = cep - curscr->alines[wy]->line -
815 win->begx * __LDATASIZE;
816 #ifdef DEBUG
817 __CTRACE(__CTRACE_REFRESH,
818 "makech: clsp = %zu, nlsp = %zu\n",
819 clsp, nlsp);
820 #endif
821 if (((clsp - nlsp >= strlen(clr_eol) &&
822 clsp < win->maxx * __LDATASIZE) ||
823 wy == win->maxy - 1) &&
824 (!(lspc & __COLOR) ||
825 ((lspc & __COLOR) && back_color_erase))) {
826 __unsetattr(0);
827 if (__using_color &&
828 ((lspc & __COLOR) !=
829 (curscr->wattr & __COLOR)))
830 __set_color(curscr, lspc &
831 __COLOR);
832 tputs(clr_eol, 0, __cputchar);
833 _cursesi_screen->lx = wx + win->begx;
834 while (wx++ <= clsp) {
835 csp->attr = lspc;
836 #ifndef HAVE_WCHAR
837 csp->ch = ' '; /* XXX */
838 #else
839 csp->ch = (wchar_t)btowc((int)' ');
840 SET_WCOL( *csp, 1 );
841 #endif /* HAVE_WCHAR */
842 csp++;
844 return (OK);
846 ce = NULL;
849 #ifdef DEBUG
850 __CTRACE(__CTRACE_REFRESH,
851 "makech: have attr %08x, need attr %08x\n",
852 curscr->wattr
853 #ifndef HAVE_WCHAR
854 & __ATTRIBUTES
855 #else
856 & WA_ATTRIBUTES
857 #endif
858 , nsp->attr
859 #ifndef HAVE_WCHAR
860 & __ATTRIBUTES
861 #else
862 & WA_ATTRIBUTES
863 #endif
865 #endif
867 off = (~nsp->attr & curscr->wattr)
868 #ifndef HAVE_WCHAR
869 & __ATTRIBUTES
870 #else
871 & WA_ATTRIBUTES
872 #endif
876 * Unset attributes as appropriate. Unset first
877 * so that the relevant attributes can be reset
878 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
879 * 'mp' and 'mr'). Check to see if we also turn off
880 * standout, attributes and colour.
882 if (off & __TERMATTR && exit_attribute_mode != NULL) {
883 tputs(exit_attribute_mode, 0, __cputchar);
884 curscr->wattr &= __mask_me;
885 off &= __mask_me;
889 * Exit underscore mode if appropriate.
890 * Check to see if we also turn off standout,
891 * attributes and colour.
893 if (off & __UNDERSCORE && exit_underline_mode != NULL) {
894 tputs(exit_underline_mode, 0, __cputchar);
895 curscr->wattr &= __mask_ue;
896 off &= __mask_ue;
900 * Exit standout mode as appropriate.
901 * Check to see if we also turn off underscore,
902 * attributes and colour.
903 * XXX
904 * Should use uc if so/se not available.
906 if (off & __STANDOUT && exit_standout_mode != NULL) {
907 tputs(exit_standout_mode, 0, __cputchar);
908 curscr->wattr &= __mask_se;
909 off &= __mask_se;
912 if (off & __ALTCHARSET && exit_alt_charset_mode != NULL) {
913 tputs(exit_alt_charset_mode, 0, __cputchar);
914 curscr->wattr &= ~__ALTCHARSET;
917 /* Set/change colour as appropriate. */
918 if (__using_color)
919 __set_color(curscr, nsp->attr & __COLOR);
921 on = (nsp->attr & ~curscr->wattr)
922 #ifndef HAVE_WCHAR
923 & __ATTRIBUTES
924 #else
925 & WA_ATTRIBUTES
926 #endif
930 * Enter standout mode if appropriate.
932 if (on & __STANDOUT &&
933 enter_standout_mode != NULL &&
934 exit_standout_mode != NULL)
936 tputs(enter_standout_mode, 0, __cputchar);
937 curscr->wattr |= __STANDOUT;
941 * Enter underscore mode if appropriate.
942 * XXX
943 * Should use uc if us/ue not available.
945 if (on & __UNDERSCORE &&
946 enter_underline_mode != NULL &&
947 exit_underline_mode != NULL)
949 tputs(enter_underline_mode, 0, __cputchar);
950 curscr->wattr |= __UNDERSCORE;
954 * Set other attributes as appropriate.
956 if (exit_attribute_mode != NULL) {
957 if (on & __BLINK &&
958 enter_blink_mode != NULL)
960 tputs(enter_blink_mode, 0, __cputchar);
961 curscr->wattr |= __BLINK;
963 if (on & __BOLD &&
964 enter_bold_mode != NULL)
966 tputs(enter_bold_mode, 0, __cputchar);
967 curscr->wattr |= __BOLD;
969 if (on & __DIM &&
970 enter_dim_mode != NULL)
972 tputs(enter_dim_mode, 0, __cputchar);
973 curscr->wattr |= __DIM;
975 if (on & __BLANK &&
976 enter_secure_mode != NULL)
978 tputs(enter_secure_mode, 0, __cputchar);
979 curscr->wattr |= __BLANK;
981 if (on & __PROTECT &&
982 enter_protected_mode != NULL)
984 tputs(enter_protected_mode, 0, __cputchar);
985 curscr->wattr |= __PROTECT;
987 if (on & __REVERSE &&
988 enter_reverse_mode != NULL)
990 tputs(enter_reverse_mode, 0, __cputchar);
991 curscr->wattr |= __REVERSE;
993 #ifdef HAVE_WCHAR
994 if (on & WA_TOP &&
995 enter_top_hl_mode != NULL)
997 tputs(enter_top_hl_mode, 0, __cputchar);
998 curscr->wattr |= WA_TOP;
1000 if (on & WA_LOW &&
1001 enter_low_hl_mode != NULL)
1003 tputs(enter_low_hl_mode, 0, __cputchar);
1004 curscr->wattr |= WA_LOW;
1006 if (on & WA_LEFT &&
1007 enter_left_hl_mode != NULL)
1009 tputs(enter_left_hl_mode, 0, __cputchar);
1010 curscr->wattr |= WA_LEFT;
1012 if (on & WA_RIGHT &&
1013 enter_right_hl_mode != NULL)
1015 tputs(enter_right_hl_mode, 0, __cputchar);
1016 curscr->wattr |= WA_RIGHT;
1018 if (on & WA_HORIZONTAL &&
1019 enter_horizontal_hl_mode != NULL)
1021 tputs(enter_horizontal_hl_mode, 0, __cputchar);
1022 curscr->wattr |= WA_HORIZONTAL;
1024 if (on & WA_VERTICAL &&
1025 enter_vertical_hl_mode != NULL)
1027 tputs(enter_vertical_hl_mode, 0, __cputchar);
1028 curscr->wattr |= WA_VERTICAL;
1030 #endif /* HAVE_WCHAR */
1033 /* Enter/exit altcharset mode as appropriate. */
1034 if (on & __ALTCHARSET && enter_alt_charset_mode != NULL &&
1035 exit_alt_charset_mode != NULL) {
1036 tputs(enter_alt_charset_mode, 0, __cputchar);
1037 curscr->wattr |= __ALTCHARSET;
1040 wx++;
1041 if (wx >= win->maxx &&
1042 wy == win->maxy - 1 && !_cursesi_screen->curwin) {
1043 if (win->flags & __SCROLLOK) {
1044 if (win->flags & __ENDLINE)
1045 __unsetattr(1);
1046 if (!(win->flags & __SCROLLWIN)) {
1047 if (!_cursesi_screen->curwin) {
1048 csp->attr = nsp->attr;
1049 csp->ch = nsp->ch;
1050 #ifdef HAVE_WCHAR
1051 if (_cursesi_copy_nsp(nsp->nsp, csp) == ERR)
1052 return ERR;
1053 #endif /* HAVE_WCHAR */
1055 #ifndef HAVE_WCHAR
1056 __cputchar((int) nsp->ch);
1057 #else
1058 if ( WCOL( *nsp ) > 0 ) {
1059 __cputwchar((int)nsp->ch);
1060 #ifdef DEBUG
1061 __CTRACE(__CTRACE_REFRESH,
1062 "makech: (%d,%d)putwchar(0x%x)\n",
1063 wy, wx - 1,
1064 nsp->ch );
1065 #endif /* DEBUG */
1067 * Output non-spacing
1068 * characters for the
1069 * cell.
1071 __cursesi_putnsp(nsp->nsp,
1072 wy, wx);
1075 #endif /* HAVE_WCHAR */
1077 if (wx < curscr->maxx) {
1078 domvcur(_cursesi_screen->ly, wx,
1079 (int) (win->maxy - 1),
1080 (int) (win->maxx - 1));
1082 _cursesi_screen->ly = win->maxy - 1;
1083 _cursesi_screen->lx = win->maxx - 1;
1084 return (OK);
1087 if (wx < win->maxx || wy < win->maxy - 1 ||
1088 !(win->flags & __SCROLLWIN)) {
1089 if (!_cursesi_screen->curwin) {
1090 csp->attr = nsp->attr;
1091 csp->ch = nsp->ch;
1092 #ifdef HAVE_WCHAR
1093 if (_cursesi_copy_nsp(nsp->nsp,
1094 csp) == ERR)
1095 return ERR;
1096 #endif /* HAVE_WCHAR */
1097 csp++;
1099 #ifndef HAVE_WCHAR
1100 __cputchar((int) nsp->ch);
1101 #ifdef DEBUG
1102 __CTRACE(__CTRACE_REFRESH,
1103 "makech: putchar(%c)\n", nsp->ch & 0177);
1104 #endif
1105 #else
1106 if (WCOL(*nsp) > 0) {
1107 __cputwchar((int) nsp->ch);
1108 #ifdef DEBUG
1109 __CTRACE(__CTRACE_REFRESH,
1110 "makech:(%d,%d) putwchar(%x)\n",
1111 wy, wx - 1, nsp->ch);
1112 __cursesi_putnsp(nsp->nsp, wy, wx);
1113 #endif /* DEBUG */
1115 #endif /* HAVE_WCHAR */
1117 if (underline_char && ((nsp->attr & __STANDOUT) ||
1118 (nsp->attr & __UNDERSCORE))) {
1119 __cputchar('\b');
1120 tputs(underline_char, 0, __cputchar);
1122 nsp++;
1123 #ifdef DEBUG
1124 __CTRACE(__CTRACE_REFRESH,
1125 "makech: 2: wx = %d, lx = %d\n",
1126 wx, _cursesi_screen->lx);
1127 #endif
1129 if (_cursesi_screen->lx == wx) /* If no change. */
1130 break;
1131 _cursesi_screen->lx = wx;
1132 if (_cursesi_screen->lx >= COLS && auto_right_margin)
1133 _cursesi_screen->lx = COLS - 1;
1134 else
1135 if (wx >= win->maxx) {
1136 domvcur(_cursesi_screen->ly,
1137 _cursesi_screen->lx,
1138 _cursesi_screen->ly,
1139 (int) (win->maxx - 1));
1140 _cursesi_screen->lx = win->maxx - 1;
1142 #ifdef DEBUG
1143 __CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
1144 wx, _cursesi_screen->lx);
1145 #endif
1147 #ifdef DEBUG
1148 #if HAVE_WCHAR
1150 int x;
1151 __LDATA *lp, *vlp;
1153 __CTRACE(__CTRACE_REFRESH,
1154 "makech-after: curscr(%p)-__virtscr(%p)\n",
1155 curscr, __virtscr );
1156 for (x = 0; x < curscr->maxx; x++) {
1157 lp = &curscr->alines[wy]->line[x];
1158 vlp = &__virtscr->alines[wy]->line[x];
1159 __CTRACE(__CTRACE_REFRESH,
1160 "[%d,%d](%x,%x,%x,%x,%p)-"
1161 "(%x,%x,%x,%x,%p)\n",
1162 wy, x, lp->ch, lp->attr,
1163 win->bch, win->battr, lp->nsp,
1164 vlp->ch, vlp->attr,
1165 win->bch, win->battr, vlp->nsp);
1168 #endif /* HAVE_WCHAR */
1169 #endif /* DEBUG */
1171 return (OK);
1175 * domvcur --
1176 * Do a mvcur, leaving attributes if necessary.
1178 static void
1179 domvcur(oy, ox, ny, nx)
1180 int oy, ox, ny, nx;
1182 #ifdef DEBUG
1183 __CTRACE(__CTRACE_REFRESH, "domvcur: (%x,%d)=>(%d,%d)\n",
1184 oy, ox, ny, nx );
1185 #endif /* DEBUG */
1186 __unsetattr(1);
1187 if ( oy == ny && ox == nx )
1188 return;
1189 __mvcur(oy, ox, ny, nx, 1);
1193 * Quickch() attempts to detect a pattern in the change of the window
1194 * in order to optimize the change, e.g., scroll n lines as opposed to
1195 * repainting the screen line by line.
1198 static __LDATA buf[128];
1199 static u_int last_hash;
1200 static size_t last_hash_len;
1201 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
1203 static void
1204 quickch(void)
1206 #define THRESH (int) __virtscr->maxy / 4
1208 __LINE *clp, *tmp1, *tmp2;
1209 int bsize, curs, curw, starts, startw, i, j;
1210 int n, target, cur_period, bot, top, sc_region;
1211 u_int blank_hash;
1212 attr_t bcolor;
1214 #ifdef __GNUC__
1215 curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
1216 #endif
1218 * Find how many lines from the top of the screen are unchanged.
1220 for (top = 0; top < __virtscr->maxy; top++)
1221 #ifndef HAVE_WCHAR
1222 if (__virtscr->alines[top]->flags & __ISDIRTY &&
1223 (__virtscr->alines[top]->hash != curscr->alines[top]->hash ||
1224 memcmp(__virtscr->alines[top]->line,
1225 curscr->alines[top]->line,
1226 (size_t) __virtscr->maxx * __LDATASIZE)
1227 != 0))
1228 break;
1229 #else
1230 if (__virtscr->alines[top]->flags & __ISDIRTY &&
1231 (__virtscr->alines[top]->hash != curscr->alines[top]->hash ||
1232 !linecmp(__virtscr->alines[top]->line,
1233 curscr->alines[top]->line,
1234 (size_t) __virtscr->maxx )))
1235 break;
1236 #endif /* HAVE_WCHAR */
1237 else
1238 __virtscr->alines[top]->flags &= ~__ISDIRTY;
1240 * Find how many lines from bottom of screen are unchanged.
1242 for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
1243 #ifndef HAVE_WCHAR
1244 if (__virtscr->alines[bot]->flags & __ISDIRTY &&
1245 (__virtscr->alines[bot]->hash != curscr->alines[bot]->hash ||
1246 memcmp(__virtscr->alines[bot]->line,
1247 curscr->alines[bot]->line,
1248 (size_t) __virtscr->maxx * __LDATASIZE)
1249 != 0))
1250 break;
1251 #else
1252 if (__virtscr->alines[bot]->flags & __ISDIRTY &&
1253 (__virtscr->alines[bot]->hash != curscr->alines[bot]->hash ||
1254 !linecmp(__virtscr->alines[bot]->line,
1255 curscr->alines[bot]->line,
1256 (size_t) __virtscr->maxx )))
1257 break;
1258 #endif /* HAVE_WCHAR */
1259 else
1260 __virtscr->alines[bot]->flags &= ~__ISDIRTY;
1263 * Work round an xterm bug where inserting lines causes all the
1264 * inserted lines to be covered with the background colour we
1265 * set on the first line (even if we unset it for subsequent
1266 * lines).
1268 bcolor = __virtscr->alines[min(top,
1269 __virtscr->maxy - 1)]->line[0].attr & __COLOR;
1270 for (i = top + 1, j = 0; i < bot; i++) {
1271 if ((__virtscr->alines[i]->line[0].attr & __COLOR) != bcolor) {
1272 bcolor = __virtscr->alines[i]->line[__virtscr->maxx].
1273 attr & __COLOR;
1274 j = i - top;
1275 } else
1276 break;
1278 top += j;
1280 #ifdef NO_JERKINESS
1282 * If we have a bottom unchanged region return. Scrolling the
1283 * bottom region up and then back down causes a screen jitter.
1284 * This will increase the number of characters sent to the screen
1285 * but it looks better.
1287 if (bot < __virtscr->maxy - 1)
1288 return;
1289 #endif /* NO_JERKINESS */
1292 * Search for the largest block of text not changed.
1293 * Invariants of the loop:
1294 * - Startw is the index of the beginning of the examined block in
1295 * __virtscr.
1296 * - Starts is the index of the beginning of the examined block in
1297 * curscr.
1298 * - Curw is the index of one past the end of the exmined block in
1299 * __virtscr.
1300 * - Curs is the index of one past the end of the exmined block in
1301 * curscr.
1302 * - bsize is the current size of the examined block.
1305 for (bsize = bot - top; bsize >= THRESH; bsize--) {
1306 for (startw = top; startw <= bot - bsize; startw++)
1307 for (starts = top; starts <= bot - bsize;
1308 starts++) {
1309 for (curw = startw, curs = starts;
1310 curs < starts + bsize; curw++, curs++)
1311 if (__virtscr->alines[curw]->hash !=
1312 curscr->alines[curs]->hash)
1313 break;
1314 if (curs != starts + bsize)
1315 continue;
1316 for (curw = startw, curs = starts;
1317 curs < starts + bsize; curw++, curs++)
1318 #ifndef HAVE_WCHAR
1319 if (memcmp(__virtscr->alines[curw]->line,
1320 curscr->alines[curs]->line,
1321 (size_t) __virtscr->maxx *
1322 __LDATASIZE) != 0)
1323 break;
1324 #else
1325 if (!linecmp(__virtscr->alines[curw]->line,
1326 curscr->alines[curs]->line,
1327 (size_t) __virtscr->maxx))
1328 break;
1329 #endif /* HAVE_WCHAR */
1330 if (curs == starts + bsize)
1331 goto done;
1334 done:
1336 /* Did not find anything */
1337 if (bsize < THRESH)
1338 return;
1340 #ifdef DEBUG
1341 __CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, starts=%d, startw=%d, "
1342 "curw=%d, curs=%d, top=%d, bot=%d\n",
1343 bsize, starts, startw, curw, curs, top, bot);
1344 #endif
1347 * Make sure that there is no overlap between the bottom and top
1348 * regions and the middle scrolled block.
1350 if (bot < curs)
1351 bot = curs - 1;
1352 if (top > starts)
1353 top = starts;
1355 n = startw - starts;
1357 #ifdef DEBUG
1358 __CTRACE(__CTRACE_REFRESH, "#####################################\n");
1359 for (i = 0; i < curscr->maxy; i++) {
1360 __CTRACE(__CTRACE_REFRESH, "C: %d:", i);
1361 __CTRACE(__CTRACE_REFRESH, " 0x%x \n", curscr->alines[i]->hash);
1362 for (j = 0; j < curscr->maxx; j++)
1363 __CTRACE(__CTRACE_REFRESH, "%c",
1364 curscr->alines[i]->line[j].ch);
1365 __CTRACE(__CTRACE_REFRESH, "\n");
1366 __CTRACE(__CTRACE_REFRESH, " attr:");
1367 for (j = 0; j < curscr->maxx; j++)
1368 __CTRACE(__CTRACE_REFRESH, " %x",
1369 curscr->alines[i]->line[j].attr);
1370 __CTRACE(__CTRACE_REFRESH, "\n");
1371 __CTRACE(__CTRACE_REFRESH, "W: %d:", i);
1372 __CTRACE(__CTRACE_REFRESH, " 0x%x \n",
1373 __virtscr->alines[i]->hash);
1374 __CTRACE(__CTRACE_REFRESH, " 0x%x ",
1375 __virtscr->alines[i]->flags);
1376 for (j = 0; j < __virtscr->maxx; j++)
1377 __CTRACE(__CTRACE_REFRESH, "%c",
1378 __virtscr->alines[i]->line[j].ch);
1379 __CTRACE(__CTRACE_REFRESH, "\n");
1380 __CTRACE(__CTRACE_REFRESH, " attr:");
1381 for (j = 0; j < __virtscr->maxx; j++)
1382 __CTRACE(__CTRACE_REFRESH, " %x",
1383 __virtscr->alines[i]->line[j].attr);
1384 __CTRACE(__CTRACE_REFRESH, "\n");
1386 #endif
1388 #ifndef HAVE_WCHAR
1389 if (buf[0].ch != ' ') {
1390 for (i = 0; i < BLANKSIZE; i++) {
1391 buf[i].ch = ' ';
1392 buf[i].attr = 0;
1395 #else
1396 if (buf[0].ch != ( wchar_t )btowc(( int ) curscr->bch )) {
1397 for (i = 0; i < BLANKSIZE; i++) {
1398 buf[i].ch = ( wchar_t )btowc(( int ) curscr->bch );
1399 if (_cursesi_copy_nsp(curscr->bnsp, &buf[i]) == ERR)
1400 return;
1401 buf[i].attr = 0;
1402 SET_WCOL( buf[ i ], 1 );
1405 #endif /* HAVE_WCHAR */
1407 if (__virtscr->maxx != last_hash_len) {
1408 blank_hash = 0;
1409 for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
1410 blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
1412 blank_hash = __hash_more((char *)(void *)buf,
1413 i * sizeof(buf[0]), blank_hash);
1414 /* cache result in static data - screen width doesn't change often */
1415 last_hash_len = __virtscr->maxx;
1416 last_hash = blank_hash;
1417 } else
1418 blank_hash = last_hash;
1421 * Perform the rotation to maintain the consistency of curscr.
1422 * This is hairy since we are doing an *in place* rotation.
1423 * Invariants of the loop:
1424 * - I is the index of the current line.
1425 * - Target is the index of the target of line i.
1426 * - Tmp1 points to current line (i).
1427 * - Tmp2 and points to target line (target);
1428 * - Cur_period is the index of the end of the current period.
1429 * (see below).
1431 * There are 2 major issues here that make this rotation non-trivial:
1432 * 1. Scrolling in a scrolling region bounded by the top
1433 * and bottom regions determined (whose size is sc_region).
1434 * 2. As a result of the use of the mod function, there may be a
1435 * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
1436 * 0 to 2, which then causes all odd lines not to be rotated.
1437 * To remedy this, an index of the end ( = beginning) of the
1438 * current 'period' is kept, cur_period, and when it is reached,
1439 * the next period is started from cur_period + 1 which is
1440 * guaranteed not to have been reached since that would mean that
1441 * all records would have been reached. (think about it...).
1443 * Lines in the rotation can have 3 attributes which are marked on the
1444 * line so that curscr is consistent with the visual screen.
1445 * 1. Not dirty -- lines inside the scrolled block, top region or
1446 * bottom region.
1447 * 2. Blank lines -- lines in the differential of the scrolling
1448 * region adjacent to top and bot regions
1449 * depending on scrolling direction.
1450 * 3. Dirty line -- all other lines are marked dirty.
1452 sc_region = bot - top + 1;
1453 i = top;
1454 tmp1 = curscr->alines[top];
1455 cur_period = top;
1456 for (j = top; j <= bot; j++) {
1457 target = (i - top + n + sc_region) % sc_region + top;
1458 tmp2 = curscr->alines[target];
1459 curscr->alines[target] = tmp1;
1460 /* Mark block as clean and blank out scrolled lines. */
1461 clp = curscr->alines[target];
1462 #ifdef DEBUG
1463 __CTRACE(__CTRACE_REFRESH,
1464 "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
1465 n, startw, curw, i, target);
1466 #endif
1467 if ((target >= startw && target < curw) || target < top
1468 || target > bot) {
1469 #ifdef DEBUG
1470 __CTRACE(__CTRACE_REFRESH, " notdirty\n");
1471 #endif
1472 __virtscr->alines[target]->flags &= ~__ISDIRTY;
1473 } else
1474 if ((n > 0 && target >= top && target < top + n) ||
1475 (n < 0 && target <= bot && target > bot + n)) {
1476 #ifndef HAVE_WCHAR
1477 if (clp->hash != blank_hash ||
1478 memcmp(clp->line, clp->line + 1,
1479 (__virtscr->maxx - 1)
1480 * __LDATASIZE) ||
1481 memcmp(clp->line, buf, __LDATASIZE)) {
1482 #else
1483 if (clp->hash != blank_hash
1484 || linecmp(clp->line, clp->line + 1,
1485 (unsigned int) (__virtscr->maxx - 1))
1486 || cellcmp(clp->line, buf)) {
1487 #endif /* HAVE_WCHAR */
1488 for (i = __virtscr->maxx;
1489 i > BLANKSIZE;
1490 i -= BLANKSIZE) {
1491 (void) memcpy(clp->line + i -
1492 BLANKSIZE, buf, sizeof(buf));
1494 (void) memcpy(clp->line , buf, i *
1495 sizeof(buf[0]));
1496 #ifdef DEBUG
1497 __CTRACE(__CTRACE_REFRESH,
1498 " blanked out: dirty\n");
1499 #endif
1500 clp->hash = blank_hash;
1501 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1502 } else {
1503 #ifdef DEBUG
1504 __CTRACE(__CTRACE_REFRESH,
1505 " -- blank line already: dirty\n");
1506 #endif
1507 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1509 } else {
1510 #ifdef DEBUG
1511 __CTRACE(__CTRACE_REFRESH, " -- dirty\n");
1512 #endif
1513 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1515 if (target == cur_period) {
1516 i = target + 1;
1517 tmp1 = curscr->alines[i];
1518 cur_period = i;
1519 } else {
1520 tmp1 = tmp2;
1521 i = target;
1524 #ifdef DEBUG
1525 __CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1526 for (i = 0; i < curscr->maxy; i++) {
1527 __CTRACE(__CTRACE_REFRESH, "C: %d:", i);
1528 for (j = 0; j < curscr->maxx; j++)
1529 __CTRACE(__CTRACE_REFRESH, "%c",
1530 curscr->alines[i]->line[j].ch);
1531 __CTRACE(__CTRACE_REFRESH, "\n");
1532 __CTRACE(__CTRACE_REFRESH, "W: %d:", i);
1533 for (j = 0; j < __virtscr->maxx; j++)
1534 __CTRACE(__CTRACE_REFRESH, "%c",
1535 __virtscr->alines[i]->line[j].ch);
1536 __CTRACE(__CTRACE_REFRESH, "\n");
1538 #endif
1539 if (n != 0)
1540 scrolln(starts, startw, curs, bot, top);
1544 * scrolln --
1545 * Scroll n lines, where n is starts - startw.
1547 static void /* ARGSUSED */
1548 scrolln(starts, startw, curs, bot, top)
1549 int starts, startw, curs, bot, top;
1551 int i, oy, ox, n;
1553 oy = curscr->cury;
1554 ox = curscr->curx;
1555 n = starts - startw;
1558 * XXX
1559 * The initial tests that set __noqch don't let us reach here unless
1560 * we have either cs + ho + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
1561 * scrolling can only shift the entire scrolling region, not just a
1562 * part of it, which means that the quickch() routine is going to be
1563 * sadly disappointed in us if we don't have cs as well.
1565 * If cs, ho and SF/sf are set, can use the scrolling region. Because
1566 * the cursor position after cs is undefined, we need ho which gives us
1567 * the ability to move to somewhere without knowledge of the current
1568 * location of the cursor. Still call __mvcur() anyway, to update its
1569 * idea of where the cursor is.
1571 * When the scrolling region has been set, the cursor has to be at the
1572 * last line of the region to make the scroll happen.
1574 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1575 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
1576 * SF/SR. So, if we're scrolling almost all of the screen, try and use
1577 * AL/DL, otherwise use the scrolling region. The "almost all" is a
1578 * shameless hack for vi.
1580 if (n > 0) {
1581 if (change_scroll_region != NULL && cursor_home != NULL &&
1582 (parm_index != NULL ||
1583 ((parm_insert_line == NULL || parm_delete_line == NULL ||
1584 top > 3 || bot + 3 < __virtscr->maxy) &&
1585 scroll_forward != NULL)))
1587 tputs(tiparm(change_scroll_region, top, bot),
1588 0, __cputchar);
1589 __mvcur(oy, ox, 0, 0, 1);
1590 tputs(cursor_home, 0, __cputchar);
1591 __mvcur(0, 0, bot, 0, 1);
1592 if (parm_index != NULL)
1593 tputs(tiparm(parm_index, n),
1594 0, __cputchar);
1595 else
1596 for (i = 0; i < n; i++)
1597 tputs(scroll_forward, 0, __cputchar);
1598 tputs(tiparm(change_scroll_region,
1599 0, (int) __virtscr->maxy - 1), 0, __cputchar);
1600 __mvcur(bot, 0, 0, 0, 1);
1601 tputs(cursor_home, 0, __cputchar);
1602 __mvcur(0, 0, oy, ox, 1);
1603 return;
1606 /* Scroll up the block. */
1607 if (parm_index != NULL && top == 0) {
1608 __mvcur(oy, ox, bot, 0, 1);
1609 tputs(tiparm(parm_index, n), 0, __cputchar);
1610 } else
1611 if (parm_delete_line != NULL) {
1612 __mvcur(oy, ox, top, 0, 1);
1613 tputs(tiparm(parm_delete_line, n),
1614 0, __cputchar);
1615 } else
1616 if (delete_line != NULL) {
1617 __mvcur(oy, ox, top, 0, 1);
1618 for (i = 0; i < n; i++)
1619 tputs(delete_line,
1620 0, __cputchar);
1621 } else
1622 if (scroll_forward != NULL && top == 0) {
1623 __mvcur(oy, ox, bot, 0, 1);
1624 for (i = 0; i < n; i++)
1625 tputs(scroll_forward, 0,
1626 __cputchar);
1627 } else
1628 abort();
1630 /* Push down the bottom region. */
1631 __mvcur(top, 0, bot - n + 1, 0, 1);
1632 if (parm_insert_line != NULL)
1633 tputs(tiparm(parm_insert_line, n), 0, __cputchar);
1634 else
1635 if (insert_line != NULL)
1636 for (i = 0; i < n; i++)
1637 tputs(insert_line, 0, __cputchar);
1638 else
1639 abort();
1640 __mvcur(bot - n + 1, 0, oy, ox, 1);
1641 } else {
1643 * !!!
1644 * n < 0
1646 * If cs, ho and SR/sr are set, can use the scrolling region.
1647 * See the above comments for details.
1649 if (change_scroll_region != NULL && cursor_home != NULL &&
1650 (parm_rindex != NULL ||
1651 ((parm_insert_line == NULL || parm_delete_line == NULL ||
1652 top > 3 ||
1653 bot + 3 < __virtscr->maxy) && scroll_reverse != NULL)))
1655 tputs(tiparm(change_scroll_region, top, bot),
1656 0, __cputchar);
1657 __mvcur(oy, ox, 0, 0, 1);
1658 tputs(cursor_home, 0, __cputchar);
1659 __mvcur(0, 0, top, 0, 1);
1661 if (parm_rindex != NULL)
1662 tputs(tiparm(parm_rindex, -n),
1663 0, __cputchar);
1664 else
1665 for (i = n; i < 0; i++)
1666 tputs(scroll_reverse, 0, __cputchar);
1667 tputs(tiparm(change_scroll_region,
1668 0, (int) __virtscr->maxy - 1), 0, __cputchar);
1669 __mvcur(top, 0, 0, 0, 1);
1670 tputs(cursor_home, 0, __cputchar);
1671 __mvcur(0, 0, oy, ox, 1);
1672 return;
1675 /* Preserve the bottom lines. */
1676 __mvcur(oy, ox, bot + n + 1, 0, 1);
1677 if (parm_rindex != NULL && bot == __virtscr->maxy)
1678 tputs(tiparm(parm_rindex, -n), 0, __cputchar);
1679 else
1680 if (parm_delete_line != NULL)
1681 tputs(tiparm(parm_delete_line, -n),
1682 0, __cputchar);
1683 else
1684 if (delete_line != NULL)
1685 for (i = n; i < 0; i++)
1686 tputs(delete_line,
1687 0, __cputchar);
1688 else
1689 if (scroll_reverse != NULL &&
1690 bot == __virtscr->maxy)
1691 for (i = n; i < 0; i++)
1692 tputs(scroll_reverse, 0,
1693 __cputchar);
1694 else
1695 abort();
1697 /* Scroll the block down. */
1698 __mvcur(bot + n + 1, 0, top, 0, 1);
1699 if (parm_insert_line != NULL)
1700 tputs(tiparm(parm_insert_line, -n), 0, __cputchar);
1701 else
1702 if (insert_line != NULL)
1703 for (i = n; i < 0; i++)
1704 tputs(insert_line, 0, __cputchar);
1705 else
1706 abort();
1707 __mvcur(top, 0, oy, ox, 1);
1712 * __unsetattr --
1713 * Unset attributes on curscr. Leave standout, attribute and colour
1714 * modes if necessary (!ms). Always leave altcharset (xterm at least
1715 * ignores a cursor move if we don't).
1717 void /* ARGSUSED */
1718 __unsetattr(int checkms)
1720 int isms;
1722 if (checkms)
1723 if (!move_standout_mode) {
1724 isms = 1;
1725 } else {
1726 isms = 0;
1728 else
1729 isms = 1;
1730 #ifdef DEBUG
1731 __CTRACE(__CTRACE_REFRESH,
1732 "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
1733 checkms, move_standout_mode ? "TRUE" : "FALSE", curscr->wattr);
1734 #endif
1737 * Don't leave the screen in standout mode (check against ms). Check
1738 * to see if we also turn off underscore, attributes and colour.
1740 if (curscr->wattr & __STANDOUT && isms) {
1741 tputs(exit_standout_mode, 0, __cputchar);
1742 curscr->wattr &= __mask_se;
1745 * Don't leave the screen in underscore mode (check against ms).
1746 * Check to see if we also turn off attributes. Assume that we
1747 * also turn off colour.
1749 if (curscr->wattr & __UNDERSCORE && isms) {
1750 tputs(exit_underline_mode, 0, __cputchar);
1751 curscr->wattr &= __mask_ue;
1754 * Don't leave the screen with attributes set (check against ms).
1755 * Assume that also turn off colour.
1757 if (curscr->wattr & __TERMATTR && isms) {
1758 tputs(exit_attribute_mode, 0, __cputchar);
1759 curscr->wattr &= __mask_me;
1761 /* Don't leave the screen with altcharset set (don't check ms). */
1762 if (curscr->wattr & __ALTCHARSET) {
1763 tputs(exit_alt_charset_mode, 0, __cputchar);
1764 curscr->wattr &= ~__ALTCHARSET;
1766 /* Don't leave the screen with colour set (check against ms). */
1767 if (__using_color && isms)
1768 __unset_color(curscr);
1771 #ifdef HAVE_WCHAR
1772 /* compare two cells on screen, must have the same forground/background,
1773 * and the same sequence of non-spacing characters */
1775 cellcmp( __LDATA *x, __LDATA *y )
1777 nschar_t *xnp = x->nsp, *ynp = y->nsp;
1778 int ret = ( x->ch == y->ch ) & ( x->attr == y->attr );
1780 if ( !ret )
1781 return 0;
1782 if ( !xnp && !ynp )
1783 return 1;
1784 if (( xnp && !ynp ) || ( !xnp && ynp ))
1785 return 0;
1787 while ( xnp && ynp ) {
1788 if ( xnp->ch != ynp->ch )
1789 return 0;
1790 xnp = xnp->next;
1791 ynp = ynp->next;
1793 return ( !xnp && !ynp );
1796 /* compare two line segments */
1798 linecmp( __LDATA *xl, __LDATA *yl, size_t len )
1800 int i = 0;
1801 __LDATA *xp = xl, *yp = yl;
1803 for ( i = 0; i < len; i++, xp++, yp++ ) {
1804 if ( !cellcmp( xp, yp ))
1805 return 0;
1807 return 1;
1811 * Output the non-spacing characters associated with the given character
1812 * cell to the screen.
1815 void
1816 __cursesi_putnsp(nschar_t *nsp, const int wy, const int wx)
1818 nschar_t *p;
1820 /* this shuts up gcc warnings about wx and wy not being used */
1821 if (wx > wy) {
1824 p = nsp;
1825 while (p != NULL) {
1826 __cputwchar((int) p->ch);
1827 #ifdef DEBUG
1828 __CTRACE(__CTRACE_REFRESH,
1829 "_cursesi_putnsp: (%d,%d) non-spacing putwchar(0x%x)\n",
1830 wy, wx - 1, p->ch);
1831 #endif
1832 p = p->next;
1836 #endif /* HAVE_WCHAR */