refresh.c revision 1.65 1 /* $NetBSD: refresh.c,v 1.65 2007/05/28 15:01:57 blymn 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. 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.
18 *
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.
30 */
31
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.65 2007/05/28 15:01:57 blymn Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "curses.h"
45 #include "curses_private.h"
46
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);
51
52 static int _cursesi_wnoutrefresh(SCREEN *, WINDOW *,
53 int, int, int, int, int, int);
54
55 #ifdef HAVE_WCHAR
56 int cellcmp( __LDATA *, __LDATA * );
57 int linecmp( __LDATA *, __LDATA *, size_t );
58 #endif /* HAVE_WCHAR */
59
60 #ifndef _CURSES_USE_MACROS
61
62 /*
63 * refresh --
64 * Make the current screen look like "stdscr" over the area covered by
65 * stdscr.
66 */
67 int
68 refresh(void)
69 {
70 return wrefresh(stdscr);
71 }
72
73 #endif
74
75 /*
76 * wnoutrefresh --
77 * Add the contents of "win" to the virtual window.
78 */
79 int
80 wnoutrefresh(WINDOW *win)
81 {
82 #ifdef DEBUG
83 __CTRACE(__CTRACE_REFRESH, "wnoutrefresh: win %p\n", win);
84 #endif
85
86 return _cursesi_wnoutrefresh(_cursesi_screen, win, 0, 0, win->begy,
87 win->begx, win->maxy, win->maxx);
88 }
89
90 /*
91 * pnoutrefresh --
92 * Add the contents of "pad" to the virtual window.
93 */
94 int
95 pnoutrefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
96 int smaxy, int smaxx)
97 {
98 int pmaxy, pmaxx;
99
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
107
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;
117
118 /* Calculate rectangle on pad - used by _cursesi_wnoutrefresh */
119 pmaxy = pbegy + smaxy - sbegy + 1;
120 pmaxx = pbegx + smaxx - sbegx + 1;
121
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;
127
128 if (smaxy - sbegy < 0 || smaxx - sbegx < 0 )
129 return ERR;
130
131 return _cursesi_wnoutrefresh(_cursesi_screen, pad,
132 pad->begy + pbegy, pad->begx + pbegx, pad->begy + sbegy,
133 pad->begx + sbegx, pmaxy, pmaxx);
134 }
135
136 /*
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).
141 */
142 int
143 _cursesi_wnoutrefresh(SCREEN *screen, WINDOW *win, int begy, int begx,
144 int wbegy, int wbegx, int maxy, int maxx)
145 {
146
147 short sy, wy, wx, y_off, x_off, mx;
148 __LINE *wlp, *vlp;
149 WINDOW *sub_win, *orig;
150
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
158
159 if (screen->curwin)
160 return OK;
161
162 /*
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.
166 */
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->lines[sy]->flags == __ISDIRTY) {
178 orig->lines[sy + sub_win->begy - orig->begy]->flags
179 |= __ISDIRTY;
180 sub_win->lines[sy]->flags
181 &= ~__ISDIRTY;
182 }
183 }
184 }
185 }
186
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;
194
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;
200 }
201 screen->__virtscr->flags &= ~__LEAVEOK;
202 screen->__virtscr->flags |= win->flags;
203
204 for (wy = begy, y_off = wbegy; wy < maxy &&
205 y_off < screen->__virtscr->maxy; wy++, y_off++) {
206 wlp = win->lines[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->lines[y_off];
215
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;
224 }
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;
257 }
258 #endif /* HAVE_WCHAR */
259 wx++;
260 x_off++;
261 }
262
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;
270
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
290 /*
291 * Unset change pointers only if a window, as a pad
292 * can be displayed again without any of the contents
293 * changing.
294 */
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;
310 }
311 }
312 }
313 }
314 return OK;
315 }
316
317 /*
318 * wrefresh --
319 * Make the current screen look like "win" over the area covered by
320 * win.
321 */
322 int
323 wrefresh(WINDOW *win)
324 {
325 int retval;
326
327 #ifdef DEBUG
328 __CTRACE(__CTRACE_REFRESH, "wrefresh: win %p\n", win);
329 #endif
330
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);
342 }
343 }
344 _cursesi_screen->curwin = 0;
345 return(retval);
346 }
347
348 /*
349 * prefresh --
350 * Make the current screen look like "pad" over the area coverd by
351 * the specified area of pad.
352 */
353 int
354 prefresh(WINDOW *pad, int pbegy, int pbegx, int sbegy, int sbegx,
355 int smaxy, int smaxx)
356 {
357 int retval;
358
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;
370
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);
378 }
379 }
380 return(retval);
381 }
382
383 /*
384 * doupdate --
385 * Make the current screen look like the virtual window "__virtscr".
386 */
387 int
388 doupdate(void)
389 {
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 */
399
400 /* Check if we need to restart ... */
401 if (_cursesi_screen->endwin)
402 __restartwin();
403
404 if (_cursesi_screen->curwin)
405 win = curscr;
406 else
407 win = _cursesi_screen->__virtscr;
408
409 /* Initialize loop parameters. */
410 _cursesi_screen->ly = curscr->cury;
411 _cursesi_screen->lx = curscr->curx;
412 wy = 0;
413
414 if (!_cursesi_screen->curwin) {
415 for (wy = 0; wy < win->maxy; wy++) {
416 wlp = win->lines[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;
438 }
439 }
440 }
441 #endif /* HAVE_WCHAR */
442 }
443 }
444 }
445
446 if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) ||
447 _cursesi_screen->curwin) {
448 if (curscr->wattr & __COLOR)
449 __unsetattr(0);
450 tputs(__tc_cl, 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);
458 }
459 __touchwin(win);
460 win->flags &= ~__CLEAROK;
461 }
462 if (!__CA) {
463 if (win->curx != 0)
464 __cputchar('\n');
465 if (!_cursesi_screen->curwin)
466 werase(curscr);
467 }
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
473
474 if (!_cursesi_screen->curwin) {
475 /*
476 * Invoke quickch() only if more than a quarter of the lines
477 * in the window are dirty.
478 */
479 for (wy = 0, dnum = 0; wy < win->maxy; wy++)
480 if (win->lines[wy]->flags & __ISDIRTY)
481 dnum++;
482 if (!__noqch && dnum > (int) win->maxy / 4)
483 quickch();
484 }
485
486 #ifdef DEBUG
487 {
488 int i, j;
489
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->lines[i]->hash);
499 for (j = 0; j < curscr->maxx; j++)
500 __CTRACE(__CTRACE_REFRESH, "%c",
501 curscr->lines[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->lines[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->lines[i]->hash);
511 __CTRACE(__CTRACE_REFRESH, " 0x%x ",
512 win->lines[i]->flags);
513 for (j = 0; j < win->maxx; j++)
514 __CTRACE(__CTRACE_REFRESH, "%c",
515 win->lines[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->lines[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->lines[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 */
534 }
535 }
536 #endif /* DEBUG */
537
538 for (wy = 0; wy < win->maxy; wy++) {
539 wlp = win->lines[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->lines[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;
567 }
568 }
569
570 }
571 #ifdef DEBUG
572 __CTRACE(__CTRACE_REFRESH, "\t%d\t%d\n",
573 *wlp->firstchp, *wlp->lastchp);
574 #endif /* DEBUG */
575 }
576
577 #ifdef DEBUG
578 __CTRACE(__CTRACE_REFRESH, "doupdate: ly=%d, lx=%d\n",
579 _cursesi_screen->ly, _cursesi_screen->lx);
580 #endif /* DEBUG */
581
582 if (_cursesi_screen->curwin)
583 domvcur(_cursesi_screen->ly, _cursesi_screen->lx,
584 (int) win->cury, (int) 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;
594 }
595 }
596
597 /* Don't leave the screen with attributes set. */
598 __unsetattr(0);
599 #ifdef DEBUG
600 #ifdef HAVE_WCHAR
601 {
602 int i, j;
603
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->lines[i]->line[j].ch,
615 curscr->lines[i]->line[j].attr,
616 curscr->lines[i]->line[j].nsp,
617 _cursesi_screen->__virtscr->lines[i]->line[j].ch,
618 _cursesi_screen->__virtscr->lines[i]->line[j].attr,
619 _cursesi_screen->__virtscr->lines[i]->line[j].nsp);
620 }
621 }
622 #endif /* HAVE_WCHAR */
623 #endif /* DEBUG */
624 return fflush(_cursesi_screen->outfd) == EOF ? ERR : OK;
625 }
626
627 /*
628 * makech --
629 * Make a change on the screen.
630 */
631 static int
632 makech(int wy)
633 {
634 WINDOW *win;
635 static __LDATA blank;
636 __LDATA *nsp, *csp, *cp, *cep;
637 int clsp, nlsp; /* Last space in lines. */
638 int lch, wx;
639 char *ce;
640 attr_t lspc; /* Last space colour */
641 attr_t off, on;
642
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
659 {
660 int x;
661 __LDATA *lp, *vlp;
662
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->lines[wy]->line[x];
668 vlp = &__virtscr->lines[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);
676 }
677 }
678 #endif /* HAVE_WCHAR */
679 #endif /* DEBUG */
680 /* Is the cursor still on the end of the last line? */
681 if (wy > 0 && curscr->lines[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;
686 }
687 wx = *win->lines[wy]->firstchp;
688 if (wx < 0)
689 wx = 0;
690 else
691 if (wx >= win->maxx)
692 return (OK);
693 lch = *win->lines[wy]->lastchp;
694 if (lch < 0)
695 return (OK);
696 else
697 if (lch >= (int) win->maxx)
698 lch = win->maxx - 1;
699
700 if (_cursesi_screen->curwin) {
701 csp = ␣
702 #ifdef DEBUG
703 __CTRACE(__CTRACE_REFRESH, "makech: csp is blank\n");
704 #endif /* DEBUG */
705 } else {
706 csp = &curscr->lines[wy]->line[wx];
707 #ifdef DEBUG
708 __CTRACE(__CTRACE_REFRESH,
709 "makech: csp is on curscr:(%d,%d)\n", wy, wx);
710 #endif /* DEBUG */
711 }
712
713 nsp = &win->lines[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 (__tc_ce && !_cursesi_screen->curwin) {
723 cp = &win->lines[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->lines[wy]->line)
728 break;
729 #else
730 while (cp->ch == ( wchar_t )btowc(( int )' ' )
731 && ( cp->attr & WA_ATTRIBUTES ) == lspc)
732 if (cp-- <= win->lines[wy]->line)
733 break;
734 #endif /* HAVE_WCHAR */
735 nlsp = cp - win->lines[wy]->line;
736 if (nlsp < 0)
737 nlsp = 0;
738 }
739 if (!_cursesi_screen->curwin)
740 ce = __tc_ce;
741 else
742 ce = NULL;
743
744 while (wx <= lch) {
745 #ifdef DEBUG
746 __CTRACE(__CTRACE_REFRESH, "makech: wx=%d,lch=%d\n", wx, lch);
747 #endif /* DEBUG */
748 #ifndef HAVE_WCHAR
749 if (memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
750 if (wx <= lch) {
751 while (wx <= lch &&
752 memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
753 nsp++;
754 if (!_cursesi_screen->curwin)
755 ++csp;
756 ++wx;
757 }
758 continue;
759 }
760 break;
761 }
762 #else
763 #ifdef DEBUG
764 __CTRACE(__CTRACE_REFRESH, "makech: nsp=(%x,%x,%x,%x,%p)\n",
765 nsp->ch, nsp->attr, win->bch, win->battr, nsp->nsp);
766 __CTRACE(__CTRACE_REFRESH, "makech: csp=(%x,%x,%x,%x,%p)\n",
767 csp->ch, csp->attr, win->bch, win->battr, csp->nsp);
768 #endif /* DEBUG */
769 if (((nsp->attr & __WCWIDTH) != __WCWIDTH) &&
770 cellcmp(nsp, csp)) {
771 if (wx <= lch) {
772 while (wx <= lch && cellcmp( csp, nsp )) {
773 nsp++;
774 if (!_cursesi_screen->curwin)
775 ++csp;
776 ++wx;
777 }
778 continue;
779 }
780 break;
781 }
782 #endif /* HAVE_WCHAR */
783 domvcur(_cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
784
785 #ifdef DEBUG
786 __CTRACE(__CTRACE_REFRESH, "makech: 1: wx = %d, ly= %d, "
787 "lx = %d, newy = %d, newx = %d\n",
788 wx, _cursesi_screen->ly, _cursesi_screen->lx, wy, wx);
789 #endif
790 _cursesi_screen->ly = wy;
791 _cursesi_screen->lx = wx;
792 #ifndef HAVE_WCHAR
793 while (wx <= lch && memcmp(nsp, csp, sizeof(__LDATA)) != 0) {
794 if (ce != NULL &&
795 wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
796 /* Are we continuing a multircell character? */
797 if ((nsp->attr & __WCWIDTH) == __WCWIDTH) {
798 #ifdef DEBUG
799 __CTRACE(__CTRACE_REFRESH,
800 "Skipping continuation cell\n");
801 wx++;
802 csp->ch = nsp->ch;
803 csp->attr = nsp->attr;
804 nsp++;
805 csp++;
806 continue;
807 }
808 #endif
809 #else
810 while (!cellcmp(nsp, csp) && wx <= lch) {
811 if (ce != NULL && wx >= nlsp
812 && nsp->ch == (wchar_t)btowc((int)' ') /* XXX */
813 && (nsp->attr & WA_ATTRIBUTES) == lspc) {
814
815 #endif
816 /* Check for clear to end-of-line. */
817 cep = &curscr->lines[wy]->line[win->maxx - 1];
818 #ifndef HAVE_WCHAR
819 while (cep->ch == ' ' && cep->attr == lspc) /* XXX */
820 #else
821 while (cep->ch == (wchar_t)btowc((int)' ')
822 && (cep->attr & WA_ATTRIBUTES) == lspc)
823 #endif /* HAVE_WCHAR */
824 if (cep-- <= csp)
825 break;
826 clsp = cep - curscr->lines[wy]->line -
827 win->begx * __LDATASIZE;
828 #ifdef DEBUG
829 __CTRACE(__CTRACE_REFRESH,
830 "makech: clsp = %d, nlsp = %d\n",
831 clsp, nlsp);
832 #endif
833 if (((clsp - nlsp >= strlen(__tc_ce) &&
834 clsp < win->maxx * __LDATASIZE) ||
835 wy == win->maxy - 1) &&
836 (!(lspc & __COLOR) ||
837 ((lspc & __COLOR) && __tc_ut))) {
838 __unsetattr(0);
839 if (__using_color &&
840 ((lspc & __COLOR) !=
841 (curscr->wattr & __COLOR)))
842 __set_color(curscr, lspc &
843 __COLOR);
844 tputs(__tc_ce, 0, __cputchar);
845 _cursesi_screen->lx = wx + win->begx;
846 while (wx++ <= clsp) {
847 csp->attr = lspc;
848 #ifndef HAVE_WCHAR
849 csp->ch = ' '; /* XXX */
850 #else
851 csp->ch = (wchar_t)btowc((int)' ');
852 SET_WCOL( *csp, 1 );
853 #endif /* HAVE_WCHAR */
854 csp++;
855 }
856 return (OK);
857 }
858 ce = NULL;
859 }
860
861 #ifdef DEBUG
862 __CTRACE(__CTRACE_REFRESH,
863 "makech: have attr %08x, need attr %08x\n",
864 curscr->wattr & WA_ATTRIBUTES,
865 nsp->attr & WA_ATTRIBUTES);
866 #endif
867
868 off = (~nsp->attr & curscr->wattr) & WA_ATTRIBUTES;
869
870 /*
871 * Unset attributes as appropriate. Unset first
872 * so that the relevant attributes can be reset
873 * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
874 * 'mp' and 'mr'). Check to see if we also turn off
875 * standout, attributes and colour.
876 */
877 if (off & __TERMATTR && __tc_me != NULL) {
878 tputs(__tc_me, 0, __cputchar);
879 curscr->wattr &= __mask_me;
880 off &= __mask_me;
881 }
882
883 /*
884 * Exit underscore mode if appropriate.
885 * Check to see if we also turn off standout,
886 * attributes and colour.
887 */
888 if (off & __UNDERSCORE && __tc_ue != NULL) {
889 tputs(__tc_ue, 0, __cputchar);
890 curscr->wattr &= __mask_ue;
891 off &= __mask_ue;
892 }
893
894 /*
895 * Exit standout mode as appropriate.
896 * Check to see if we also turn off underscore,
897 * attributes and colour.
898 * XXX
899 * Should use uc if so/se not available.
900 */
901 if (off & __STANDOUT && __tc_se != NULL) {
902 tputs(__tc_se, 0, __cputchar);
903 curscr->wattr &= __mask_se;
904 off &= __mask_se;
905 }
906
907 #ifndef HAVE_WCHAR
908 if (off & __ALTCHARSET && __tc_ae != NULL) {
909 tputs(__tc_ae, 0, __cputchar);
910 curscr->wattr &= ~__ALTCHARSET;
911 }
912 #else
913 if (off & WA_ALTCHARSET && __tc_ae != NULL) {
914 tputs(__tc_ae, 0, __cputchar);
915 curscr->wattr &= ~WA_ALTCHARSET;
916 }
917 #endif /* HAVE_WCHAR */
918
919 /* Set/change colour as appropriate. */
920 if (__using_color)
921 __set_color(curscr, nsp->attr & __COLOR);
922
923 on = (nsp->attr & ~curscr->wattr) & WA_ATTRIBUTES;
924
925 /*
926 * Enter standout mode if appropriate.
927 */
928 if (on & __STANDOUT && __tc_so != NULL && __tc_se
929 != NULL) {
930 tputs(__tc_so, 0, __cputchar);
931 curscr->wattr |= __STANDOUT;
932 }
933
934 /*
935 * Enter underscore mode if appropriate.
936 * XXX
937 * Should use uc if us/ue not available.
938 */
939 if (on & __UNDERSCORE && __tc_us != NULL &&
940 __tc_ue != NULL) {
941 tputs(__tc_us, 0, __cputchar);
942 curscr->wattr |= __UNDERSCORE;
943 }
944
945 /*
946 * Set other attributes as appropriate.
947 */
948 if (__tc_me != NULL) {
949 if (on & __BLINK && __tc_mb != NULL) {
950 tputs(__tc_mb, 0, __cputchar);
951 curscr->wattr |= __BLINK;
952 }
953 if (on & __BOLD && __tc_md != NULL) {
954 tputs(__tc_md, 0, __cputchar);
955 curscr->wattr |= __BOLD;
956 }
957 if (on & __DIM && __tc_mh != NULL) {
958 tputs(__tc_mh, 0, __cputchar);
959 curscr->wattr |= __DIM;
960 }
961 if (on & __BLANK && __tc_mk != NULL) {
962 tputs(__tc_mk, 0, __cputchar);
963 curscr->wattr |= __BLANK;
964 }
965 if (on & __PROTECT && __tc_mp != NULL) {
966 tputs(__tc_mp, 0, __cputchar);
967 curscr->wattr |= __PROTECT;
968 }
969 if (on & __REVERSE && __tc_mr != NULL) {
970 tputs(__tc_mr, 0, __cputchar);
971 curscr->wattr |= __REVERSE;
972 }
973 #ifdef HAVE_WCHAR
974 if (on & WA_TOP && __tc_Xt != NULL) {
975 tputs(__tc_Xt, 0, __cputchar);
976 curscr->wattr |= WA_TOP;
977 }
978 if (on & WA_LOW && __tc_Xo != NULL) {
979 tputs(__tc_Xo, 0, __cputchar);
980 curscr->wattr |= WA_LOW;
981 }
982 if (on & WA_LEFT && __tc_Xl != NULL) {
983 tputs(__tc_Xl, 0, __cputchar);
984 curscr->wattr |= WA_LEFT;
985 }
986 if (on & WA_RIGHT && __tc_Xl != NULL) {
987 tputs(__tc_Xl, 0, __cputchar);
988 curscr->wattr |= WA_RIGHT;
989 }
990 if (on & WA_HORIZONTAL && __tc_Xh != NULL) {
991 tputs(__tc_Xh, 0, __cputchar);
992 curscr->wattr |= WA_HORIZONTAL;
993 }
994 if (on & WA_VERTICAL && __tc_Xv != NULL) {
995 tputs(__tc_Xv, 0, __cputchar);
996 curscr->wattr |= WA_VERTICAL;
997 }
998 #endif /* HAVE_WCHAR */
999 }
1000
1001 /* Enter/exit altcharset mode as appropriate. */
1002 #ifndef HAVE_WCHAR
1003 if (on & __ALTCHARSET && __tc_as != NULL &&
1004 __tc_ae != NULL) {
1005 tputs(__tc_as, 0, __cputchar);
1006 curscr->wattr |= __ALTCHARSET;
1007 }
1008 #else
1009 if (on & WA_ALTCHARSET && __tc_as != NULL &&
1010 __tc_ae != NULL) {
1011 tputs(__tc_as, 0, __cputchar);
1012 curscr->wattr |= WA_ALTCHARSET;
1013 }
1014 #endif /* HAVE_WCHAR */
1015
1016 wx++;
1017 if (wx >= win->maxx &&
1018 wy == win->maxy - 1 && !_cursesi_screen->curwin) {
1019 if (win->flags & __SCROLLOK) {
1020 if (win->flags & __ENDLINE)
1021 __unsetattr(1);
1022 if (!(win->flags & __SCROLLWIN)) {
1023 if (!_cursesi_screen->curwin) {
1024 csp->attr = nsp->attr;
1025 csp->ch = nsp->ch;
1026 #ifdef HAVE_WCHAR
1027 if (_cursesi_copy_nsp(nsp->nsp, csp) == ERR)
1028 return ERR;
1029 #endif /* HAVE_WCHAR */
1030 }
1031 #ifndef HAVE_WCHAR
1032 __cputchar((int) nsp->ch);
1033 #else
1034 if ( WCOL( *nsp ) > 0 ) {
1035 __cputwchar((int)nsp->ch);
1036 #ifdef DEBUG
1037 __CTRACE(__CTRACE_REFRESH,
1038 "makech: (%d,%d)putwchar(0x%x)\n",
1039 wy, wx - 1,
1040 nsp->ch );
1041 #endif /* DEBUG */
1042 /*
1043 * Output non-spacing
1044 * characters for the
1045 * cell.
1046 */
1047 __cursesi_putnsp(nsp->nsp,
1048 wy, wx);
1049
1050 }
1051 #endif /* HAVE_WCHAR */
1052 }
1053 if (wx < curscr->maxx) {
1054 domvcur(_cursesi_screen->ly, wx,
1055 (int) (win->maxy - 1),
1056 (int) (win->maxx - 1));
1057 }
1058 _cursesi_screen->ly = win->maxy - 1;
1059 _cursesi_screen->lx = win->maxx - 1;
1060 return (OK);
1061 }
1062 }
1063 if (wx < win->maxx || wy < win->maxy - 1 ||
1064 !(win->flags & __SCROLLWIN)) {
1065 if (!_cursesi_screen->curwin) {
1066 csp->attr = nsp->attr;
1067 csp->ch = nsp->ch;
1068 #ifdef HAVE_WCHAR
1069 if (_cursesi_copy_nsp(nsp->nsp,
1070 csp) == ERR)
1071 return ERR;
1072 #endif /* HAVE_WCHAR */
1073 csp++;
1074 }
1075 #ifndef HAVE_WCHAR
1076 __cputchar((int) nsp->ch);
1077 #ifdef DEBUG
1078 __CTRACE(__CTRACE_REFRESH,
1079 "makech: putchar(%c)\n", nsp->ch & 0177);
1080 #endif
1081 #else
1082 if (WCOL(*nsp) > 0) {
1083 __cputwchar((int) nsp->ch);
1084 #ifdef DEBUG
1085 __CTRACE(__CTRACE_REFRESH,
1086 "makech:(%d,%d) putwchar(%x)\n",
1087 wy, wx - 1, nsp->ch);
1088 __cursesi_putnsp(nsp->nsp, wy, wx);
1089 #endif /* DEBUG */
1090 }
1091 #endif /* HAVE_WCHAR */
1092 }
1093 if (__tc_uc && ((nsp->attr & __STANDOUT) ||
1094 (nsp->attr & __UNDERSCORE))) {
1095 __cputchar('\b');
1096 tputs(__tc_uc, 0, __cputchar);
1097 }
1098 nsp++;
1099 #ifdef DEBUG
1100 __CTRACE(__CTRACE_REFRESH,
1101 "makech: 2: wx = %d, lx = %d\n",
1102 wx, _cursesi_screen->lx);
1103 #endif
1104 }
1105 if (_cursesi_screen->lx == wx) /* If no change. */
1106 break;
1107 _cursesi_screen->lx = wx;
1108 if (_cursesi_screen->lx >= COLS && __tc_am)
1109 _cursesi_screen->lx = COLS - 1;
1110 else
1111 if (wx >= win->maxx) {
1112 domvcur(_cursesi_screen->ly,
1113 _cursesi_screen->lx,
1114 _cursesi_screen->ly,
1115 (int) (win->maxx - 1));
1116 _cursesi_screen->lx = win->maxx - 1;
1117 }
1118 #ifdef DEBUG
1119 __CTRACE(__CTRACE_REFRESH, "makech: 3: wx = %d, lx = %d\n",
1120 wx, _cursesi_screen->lx);
1121 #endif
1122 }
1123 #ifdef DEBUG
1124 #if HAVE_WCHAR
1125 {
1126 int x;
1127 __LDATA *lp, *vlp;
1128
1129 __CTRACE(__CTRACE_REFRESH,
1130 "makech-after: curscr(%p)-__virtscr(%p)\n",
1131 curscr, __virtscr );
1132 for (x = 0; x < curscr->maxx; x++) {
1133 lp = &curscr->lines[wy]->line[x];
1134 vlp = &__virtscr->lines[wy]->line[x];
1135 __CTRACE(__CTRACE_REFRESH,
1136 "[%d,%d](%x,%x,%x,%x,%p)-"
1137 "(%x,%x,%x,%x,%p)\n",
1138 wy, x, lp->ch, lp->attr,
1139 win->bch, win->battr, lp->nsp,
1140 vlp->ch, vlp->attr,
1141 win->bch, win->battr, vlp->nsp);
1142 }
1143 }
1144 #endif /* HAVE_WCHAR */
1145 #endif /* DEBUG */
1146
1147 return (OK);
1148 }
1149
1150 /*
1151 * domvcur --
1152 * Do a mvcur, leaving attributes if necessary.
1153 */
1154 static void
1155 domvcur(oy, ox, ny, nx)
1156 int oy, ox, ny, nx;
1157 {
1158 #ifdef DEBUG
1159 __CTRACE(__CTRACE_REFRESH, "domvcur: (%x,%d)=>(%d,%d)\n",
1160 oy, ox, ny, nx );
1161 #endif /* DEBUG */
1162 __unsetattr(1);
1163 if ( oy == ny && ox == nx )
1164 return;
1165 __mvcur(oy, ox, ny, nx, 1);
1166 }
1167
1168 /*
1169 * Quickch() attempts to detect a pattern in the change of the window
1170 * in order to optimize the change, e.g., scroll n lines as opposed to
1171 * repainting the screen line by line.
1172 */
1173
1174 static __LDATA buf[128];
1175 static u_int last_hash;
1176 static size_t last_hash_len;
1177 #define BLANKSIZE (sizeof(buf) / sizeof(buf[0]))
1178
1179 static void
1180 quickch(void)
1181 {
1182 #define THRESH (int) __virtscr->maxy / 4
1183
1184 __LINE *clp, *tmp1, *tmp2;
1185 int bsize, curs, curw, starts, startw, i, j;
1186 int n, target, cur_period, bot, top, sc_region;
1187 u_int blank_hash;
1188 attr_t bcolor;
1189
1190 #ifdef __GNUC__
1191 curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
1192 #endif
1193 /*
1194 * Find how many lines from the top of the screen are unchanged.
1195 */
1196 for (top = 0; top < __virtscr->maxy; top++)
1197 #ifndef HAVE_WCHAR
1198 if (__virtscr->lines[top]->flags & __ISDIRTY &&
1199 (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
1200 memcmp(__virtscr->lines[top]->line,
1201 curscr->lines[top]->line,
1202 (size_t) __virtscr->maxx * __LDATASIZE)
1203 != 0))
1204 break;
1205 #else
1206 if (__virtscr->lines[top]->flags & __ISDIRTY &&
1207 (__virtscr->lines[top]->hash != curscr->lines[top]->hash ||
1208 !linecmp(__virtscr->lines[top]->line,
1209 curscr->lines[top]->line,
1210 (size_t) __virtscr->maxx )))
1211 break;
1212 #endif /* HAVE_WCHAR */
1213 else
1214 __virtscr->lines[top]->flags &= ~__ISDIRTY;
1215 /*
1216 * Find how many lines from bottom of screen are unchanged.
1217 */
1218 for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
1219 #ifndef HAVE_WCHAR
1220 if (__virtscr->lines[bot]->flags & __ISDIRTY &&
1221 (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
1222 memcmp(__virtscr->lines[bot]->line,
1223 curscr->lines[bot]->line,
1224 (size_t) __virtscr->maxx * __LDATASIZE)
1225 != 0))
1226 break;
1227 #else
1228 if (__virtscr->lines[bot]->flags & __ISDIRTY &&
1229 (__virtscr->lines[bot]->hash != curscr->lines[bot]->hash ||
1230 !linecmp(__virtscr->lines[bot]->line,
1231 curscr->lines[bot]->line,
1232 (size_t) __virtscr->maxx )))
1233 break;
1234 #endif /* HAVE_WCHAR */
1235 else
1236 __virtscr->lines[bot]->flags &= ~__ISDIRTY;
1237
1238 /*
1239 * Work round an xterm bug where inserting lines causes all the
1240 * inserted lines to be covered with the background colour we
1241 * set on the first line (even if we unset it for subsequent
1242 * lines).
1243 */
1244 bcolor = __virtscr->lines[min(top,
1245 __virtscr->maxy - 1)]->line[0].attr & __COLOR;
1246 for (i = top + 1, j = 0; i < bot; i++) {
1247 if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
1248 bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
1249 attr & __COLOR;
1250 j = i - top;
1251 } else
1252 break;
1253 }
1254 top += j;
1255
1256 #ifdef NO_JERKINESS
1257 /*
1258 * If we have a bottom unchanged region return. Scrolling the
1259 * bottom region up and then back down causes a screen jitter.
1260 * This will increase the number of characters sent to the screen
1261 * but it looks better.
1262 */
1263 if (bot < __virtscr->maxy - 1)
1264 return;
1265 #endif /* NO_JERKINESS */
1266
1267 /*
1268 * Search for the largest block of text not changed.
1269 * Invariants of the loop:
1270 * - Startw is the index of the beginning of the examined block in
1271 * __virtscr.
1272 * - Starts is the index of the beginning of the examined block in
1273 * curscr.
1274 * - Curw is the index of one past the end of the exmined block in
1275 * __virtscr.
1276 * - Curs is the index of one past the end of the exmined block in
1277 * curscr.
1278 * - bsize is the current size of the examined block.
1279 */
1280
1281 for (bsize = bot - top; bsize >= THRESH; bsize--) {
1282 for (startw = top; startw <= bot - bsize; startw++)
1283 for (starts = top; starts <= bot - bsize;
1284 starts++) {
1285 for (curw = startw, curs = starts;
1286 curs < starts + bsize; curw++, curs++)
1287 if (__virtscr->lines[curw]->hash !=
1288 curscr->lines[curs]->hash)
1289 break;
1290 if (curs != starts + bsize)
1291 continue;
1292 for (curw = startw, curs = starts;
1293 curs < starts + bsize; curw++, curs++)
1294 #ifndef HAVE_WCHAR
1295 if (memcmp(__virtscr->lines[curw]->line,
1296 curscr->lines[curs]->line,
1297 (size_t) __virtscr->maxx *
1298 __LDATASIZE) != 0)
1299 break;
1300 #else
1301 if (!linecmp(__virtscr->lines[curw]->line,
1302 curscr->lines[curs]->line,
1303 (size_t) __virtscr->maxx))
1304 break;
1305 #endif /* HAVE_WCHAR */
1306 if (curs == starts + bsize)
1307 goto done;
1308 }
1309 }
1310 done:
1311
1312 /* Did not find anything */
1313 if (bsize < THRESH)
1314 return;
1315
1316 #ifdef DEBUG
1317 __CTRACE(__CTRACE_REFRESH, "quickch:bsize=%d, starts=%d, startw=%d, "
1318 "curw=%d, curs=%d, top=%d, bot=%d\n",
1319 bsize, starts, startw, curw, curs, top, bot);
1320 #endif
1321
1322 /*
1323 * Make sure that there is no overlap between the bottom and top
1324 * regions and the middle scrolled block.
1325 */
1326 if (bot < curs)
1327 bot = curs - 1;
1328 if (top > starts)
1329 top = starts;
1330
1331 n = startw - starts;
1332
1333 #ifdef DEBUG
1334 __CTRACE(__CTRACE_REFRESH, "#####################################\n");
1335 for (i = 0; i < curscr->maxy; i++) {
1336 __CTRACE(__CTRACE_REFRESH, "C: %d:", i);
1337 __CTRACE(__CTRACE_REFRESH, " 0x%x \n", curscr->lines[i]->hash);
1338 for (j = 0; j < curscr->maxx; j++)
1339 __CTRACE(__CTRACE_REFRESH, "%c",
1340 curscr->lines[i]->line[j].ch);
1341 __CTRACE(__CTRACE_REFRESH, "\n");
1342 __CTRACE(__CTRACE_REFRESH, " attr:");
1343 for (j = 0; j < curscr->maxx; j++)
1344 __CTRACE(__CTRACE_REFRESH, " %x",
1345 curscr->lines[i]->line[j].attr);
1346 __CTRACE(__CTRACE_REFRESH, "\n");
1347 __CTRACE(__CTRACE_REFRESH, "W: %d:", i);
1348 __CTRACE(__CTRACE_REFRESH, " 0x%x \n",
1349 __virtscr->lines[i]->hash);
1350 __CTRACE(__CTRACE_REFRESH, " 0x%x ",
1351 __virtscr->lines[i]->flags);
1352 for (j = 0; j < __virtscr->maxx; j++)
1353 __CTRACE(__CTRACE_REFRESH, "%c",
1354 __virtscr->lines[i]->line[j].ch);
1355 __CTRACE(__CTRACE_REFRESH, "\n");
1356 __CTRACE(__CTRACE_REFRESH, " attr:");
1357 for (j = 0; j < __virtscr->maxx; j++)
1358 __CTRACE(__CTRACE_REFRESH, " %x",
1359 __virtscr->lines[i]->line[j].attr);
1360 __CTRACE(__CTRACE_REFRESH, "\n");
1361 }
1362 #endif
1363
1364 #ifndef HAVE_WCHAR
1365 if (buf[0].ch != ' ') {
1366 for (i = 0; i < BLANKSIZE; i++) {
1367 buf[i].ch = ' ';
1368 buf[i].attr = 0;
1369 }
1370 }
1371 #else
1372 if (buf[0].ch != ( wchar_t )btowc(( int ) curscr->bch )) {
1373 for (i = 0; i < BLANKSIZE; i++) {
1374 buf[i].ch = ( wchar_t )btowc(( int ) curscr->bch );
1375 if (_cursesi_copy_nsp(curscr->bnsp, &buf[i]) == ERR)
1376 return;
1377 buf[i].attr = 0;
1378 SET_WCOL( buf[ i ], 1 );
1379 }
1380 }
1381 #endif /* HAVE_WCHAR */
1382
1383 if (__virtscr->maxx != last_hash_len) {
1384 blank_hash = 0;
1385 for (i = __virtscr->maxx; i > BLANKSIZE; i -= BLANKSIZE) {
1386 blank_hash = __hash_more(buf, sizeof(buf), blank_hash);
1387 }
1388 blank_hash = __hash_more((char *)(void *)buf,
1389 i * sizeof(buf[0]), blank_hash);
1390 /* cache result in static data - screen width doesn't change often */
1391 last_hash_len = __virtscr->maxx;
1392 last_hash = blank_hash;
1393 } else
1394 blank_hash = last_hash;
1395
1396 /*
1397 * Perform the rotation to maintain the consistency of curscr.
1398 * This is hairy since we are doing an *in place* rotation.
1399 * Invariants of the loop:
1400 * - I is the index of the current line.
1401 * - Target is the index of the target of line i.
1402 * - Tmp1 points to current line (i).
1403 * - Tmp2 and points to target line (target);
1404 * - Cur_period is the index of the end of the current period.
1405 * (see below).
1406 *
1407 * There are 2 major issues here that make this rotation non-trivial:
1408 * 1. Scrolling in a scrolling region bounded by the top
1409 * and bottom regions determined (whose size is sc_region).
1410 * 2. As a result of the use of the mod function, there may be a
1411 * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
1412 * 0 to 2, which then causes all odd lines not to be rotated.
1413 * To remedy this, an index of the end ( = beginning) of the
1414 * current 'period' is kept, cur_period, and when it is reached,
1415 * the next period is started from cur_period + 1 which is
1416 * guaranteed not to have been reached since that would mean that
1417 * all records would have been reached. (think about it...).
1418 *
1419 * Lines in the rotation can have 3 attributes which are marked on the
1420 * line so that curscr is consistent with the visual screen.
1421 * 1. Not dirty -- lines inside the scrolled block, top region or
1422 * bottom region.
1423 * 2. Blank lines -- lines in the differential of the scrolling
1424 * region adjacent to top and bot regions
1425 * depending on scrolling direction.
1426 * 3. Dirty line -- all other lines are marked dirty.
1427 */
1428 sc_region = bot - top + 1;
1429 i = top;
1430 tmp1 = curscr->lines[top];
1431 cur_period = top;
1432 for (j = top; j <= bot; j++) {
1433 target = (i - top + n + sc_region) % sc_region + top;
1434 tmp2 = curscr->lines[target];
1435 curscr->lines[target] = tmp1;
1436 /* Mark block as clean and blank out scrolled lines. */
1437 clp = curscr->lines[target];
1438 #ifdef DEBUG
1439 __CTRACE(__CTRACE_REFRESH,
1440 "quickch: n=%d startw=%d curw=%d i = %d target=%d ",
1441 n, startw, curw, i, target);
1442 #endif
1443 if ((target >= startw && target < curw) || target < top
1444 || target > bot) {
1445 #ifdef DEBUG
1446 __CTRACE(__CTRACE_REFRESH, " notdirty\n");
1447 #endif
1448 __virtscr->lines[target]->flags &= ~__ISDIRTY;
1449 } else
1450 if ((n > 0 && target >= top && target < top + n) ||
1451 (n < 0 && target <= bot && target > bot + n)) {
1452 #ifndef HAVE_WCHAR
1453 if (clp->hash != blank_hash ||
1454 memcmp(clp->line, clp->line + 1,
1455 (__virtscr->maxx - 1)
1456 * __LDATASIZE) ||
1457 memcmp(clp->line, buf, __LDATASIZE)) {
1458 #else
1459 if (clp->hash != blank_hash
1460 || linecmp(clp->line, clp->line + 1,
1461 (unsigned int) (__virtscr->maxx - 1))
1462 || cellcmp(clp->line, buf)) {
1463 #endif /* HAVE_WCHAR */
1464 for (i = __virtscr->maxx;
1465 i > BLANKSIZE;
1466 i -= BLANKSIZE) {
1467 (void) memcpy(clp->line + i -
1468 BLANKSIZE, buf, sizeof(buf));
1469 }
1470 (void) memcpy(clp->line , buf, i *
1471 sizeof(buf[0]));
1472 #ifdef DEBUG
1473 __CTRACE(__CTRACE_REFRESH,
1474 " blanked out: dirty\n");
1475 #endif
1476 clp->hash = blank_hash;
1477 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1478 } else {
1479 #ifdef DEBUG
1480 __CTRACE(__CTRACE_REFRESH,
1481 " -- blank line already: dirty\n");
1482 #endif
1483 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1484 }
1485 } else {
1486 #ifdef DEBUG
1487 __CTRACE(__CTRACE_REFRESH, " -- dirty\n");
1488 #endif
1489 __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1);
1490 }
1491 if (target == cur_period) {
1492 i = target + 1;
1493 tmp1 = curscr->lines[i];
1494 cur_period = i;
1495 } else {
1496 tmp1 = tmp2;
1497 i = target;
1498 }
1499 }
1500 #ifdef DEBUG
1501 __CTRACE(__CTRACE_REFRESH, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1502 for (i = 0; i < curscr->maxy; i++) {
1503 __CTRACE(__CTRACE_REFRESH, "C: %d:", i);
1504 for (j = 0; j < curscr->maxx; j++)
1505 __CTRACE(__CTRACE_REFRESH, "%c",
1506 curscr->lines[i]->line[j].ch);
1507 __CTRACE(__CTRACE_REFRESH, "\n");
1508 __CTRACE(__CTRACE_REFRESH, "W: %d:", i);
1509 for (j = 0; j < __virtscr->maxx; j++)
1510 __CTRACE(__CTRACE_REFRESH, "%c",
1511 __virtscr->lines[i]->line[j].ch);
1512 __CTRACE(__CTRACE_REFRESH, "\n");
1513 }
1514 #endif
1515 if (n != 0)
1516 scrolln(starts, startw, curs, bot, top);
1517 }
1518
1519 /*
1520 * scrolln --
1521 * Scroll n lines, where n is starts - startw.
1522 */
1523 static void /* ARGSUSED */
1524 scrolln(starts, startw, curs, bot, top)
1525 int starts, startw, curs, bot, top;
1526 {
1527 int i, oy, ox, n;
1528
1529 oy = curscr->cury;
1530 ox = curscr->curx;
1531 n = starts - startw;
1532
1533 /*
1534 * XXX
1535 * The initial tests that set __noqch don't let us reach here unless
1536 * we have either cs + ho + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
1537 * scrolling can only shift the entire scrolling region, not just a
1538 * part of it, which means that the quickch() routine is going to be
1539 * sadly disappointed in us if we don't have cs as well.
1540 *
1541 * If cs, ho and SF/sf are set, can use the scrolling region. Because
1542 * the cursor position after cs is undefined, we need ho which gives us
1543 * the ability to move to somewhere without knowledge of the current
1544 * location of the cursor. Still call __mvcur() anyway, to update its
1545 * idea of where the cursor is.
1546 *
1547 * When the scrolling region has been set, the cursor has to be at the
1548 * last line of the region to make the scroll happen.
1549 *
1550 * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1551 * or AL/DL, and, some terminals have AL/DL, sf/sr, and cs, but not
1552 * SF/SR. So, if we're scrolling almost all of the screen, try and use
1553 * AL/DL, otherwise use the scrolling region. The "almost all" is a
1554 * shameless hack for vi.
1555 */
1556 if (n > 0) {
1557 if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SF != NULL ||
1558 ((__tc_AL == NULL || __tc_DL == NULL ||
1559 top > 3 || bot + 3 < __virtscr->maxy) &&
1560 __tc_sf != NULL))) {
1561 tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
1562 __mvcur(oy, ox, 0, 0, 1);
1563 tputs(__tc_ho, 0, __cputchar);
1564 __mvcur(0, 0, bot, 0, 1);
1565 if (__tc_SF != NULL)
1566 tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
1567 else
1568 for (i = 0; i < n; i++)
1569 tputs(__tc_sf, 0, __cputchar);
1570 tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
1571 __cputchar);
1572 __mvcur(bot, 0, 0, 0, 1);
1573 tputs(__tc_ho, 0, __cputchar);
1574 __mvcur(0, 0, oy, ox, 1);
1575 return;
1576 }
1577
1578 /* Scroll up the block. */
1579 if (__tc_SF != NULL && top == 0) {
1580 __mvcur(oy, ox, bot, 0, 1);
1581 tputs(__tscroll(__tc_SF, n, 0), 0, __cputchar);
1582 } else
1583 if (__tc_DL != NULL) {
1584 __mvcur(oy, ox, top, 0, 1);
1585 tputs(__tscroll(__tc_DL, n, 0), 0, __cputchar);
1586 } else
1587 if (__tc_dl != NULL) {
1588 __mvcur(oy, ox, top, 0, 1);
1589 for (i = 0; i < n; i++)
1590 tputs(__tc_dl, 0, __cputchar);
1591 } else
1592 if (__tc_sf != NULL && top == 0) {
1593 __mvcur(oy, ox, bot, 0, 1);
1594 for (i = 0; i < n; i++)
1595 tputs(__tc_sf, 0,
1596 __cputchar);
1597 } else
1598 abort();
1599
1600 /* Push down the bottom region. */
1601 __mvcur(top, 0, bot - n + 1, 0, 1);
1602 if (__tc_AL != NULL)
1603 tputs(__tscroll(__tc_AL, n, 0), 0, __cputchar);
1604 else
1605 if (__tc_al != NULL)
1606 for (i = 0; i < n; i++)
1607 tputs(__tc_al, 0, __cputchar);
1608 else
1609 abort();
1610 __mvcur(bot - n + 1, 0, oy, ox, 1);
1611 } else {
1612 /*
1613 * !!!
1614 * n < 0
1615 *
1616 * If cs, ho and SR/sr are set, can use the scrolling region.
1617 * See the above comments for details.
1618 */
1619 if (__tc_cs != NULL && __tc_ho != NULL && (__tc_SR != NULL ||
1620 ((__tc_AL == NULL || __tc_DL == NULL || top > 3 ||
1621 bot + 3 < __virtscr->maxy) && __tc_sr != NULL))) {
1622 tputs(__tscroll(__tc_cs, top, bot + 1), 0, __cputchar);
1623 __mvcur(oy, ox, 0, 0, 1);
1624 tputs(__tc_ho, 0, __cputchar);
1625 __mvcur(0, 0, top, 0, 1);
1626
1627 if (__tc_SR != NULL)
1628 tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
1629 else
1630 for (i = n; i < 0; i++)
1631 tputs(__tc_sr, 0, __cputchar);
1632 tputs(__tscroll(__tc_cs, 0, (int) __virtscr->maxy), 0,
1633 __cputchar);
1634 __mvcur(top, 0, 0, 0, 1);
1635 tputs(__tc_ho, 0, __cputchar);
1636 __mvcur(0, 0, oy, ox, 1);
1637 return;
1638 }
1639
1640 /* Preserve the bottom lines. */
1641 __mvcur(oy, ox, bot + n + 1, 0, 1);
1642 if (__tc_SR != NULL && bot == __virtscr->maxy)
1643 tputs(__tscroll(__tc_SR, -n, 0), 0, __cputchar);
1644 else
1645 if (__tc_DL != NULL)
1646 tputs(__tscroll(__tc_DL, -n, 0), 0, __cputchar);
1647 else
1648 if (__tc_dl != NULL)
1649 for (i = n; i < 0; i++)
1650 tputs(__tc_dl, 0, __cputchar);
1651 else
1652 if (__tc_sr != NULL &&
1653 bot == __virtscr->maxy)
1654 for (i = n; i < 0; i++)
1655 tputs(__tc_sr, 0,
1656 __cputchar);
1657 else
1658 abort();
1659
1660 /* Scroll the block down. */
1661 __mvcur(bot + n + 1, 0, top, 0, 1);
1662 if (__tc_AL != NULL)
1663 tputs(__tscroll(__tc_AL, -n, 0), 0, __cputchar);
1664 else
1665 if (__tc_al != NULL)
1666 for (i = n; i < 0; i++)
1667 tputs(__tc_al, 0, __cputchar);
1668 else
1669 abort();
1670 __mvcur(top, 0, oy, ox, 1);
1671 }
1672 }
1673
1674 /*
1675 * __unsetattr --
1676 * Unset attributes on curscr. Leave standout, attribute and colour
1677 * modes if necessary (!ms). Always leave altcharset (xterm at least
1678 * ignores a cursor move if we don't).
1679 */
1680 void /* ARGSUSED */
1681 __unsetattr(int checkms)
1682 {
1683 int isms;
1684
1685 if (checkms)
1686 if (!__tc_ms) {
1687 isms = 1;
1688 } else {
1689 isms = 0;
1690 }
1691 else
1692 isms = 1;
1693 #ifdef DEBUG
1694 __CTRACE(__CTRACE_REFRESH,
1695 "__unsetattr: checkms = %d, ms = %s, wattr = %08x\n",
1696 checkms, __tc_ms ? "TRUE" : "FALSE", curscr->wattr);
1697 #endif
1698
1699 /*
1700 * Don't leave the screen in standout mode (check against ms). Check
1701 * to see if we also turn off underscore, attributes and colour.
1702 */
1703 if (curscr->wattr & __STANDOUT && isms) {
1704 tputs(__tc_se, 0, __cputchar);
1705 curscr->wattr &= __mask_se;
1706 }
1707 /*
1708 * Don't leave the screen in underscore mode (check against ms).
1709 * Check to see if we also turn off attributes. Assume that we
1710 * also turn off colour.
1711 */
1712 if (curscr->wattr & __UNDERSCORE && isms) {
1713 tputs(__tc_ue, 0, __cputchar);
1714 curscr->wattr &= __mask_ue;
1715 }
1716 /*
1717 * Don't leave the screen with attributes set (check against ms).
1718 * Assume that also turn off colour.
1719 */
1720 if (curscr->wattr & __TERMATTR && isms) {
1721 tputs(__tc_me, 0, __cputchar);
1722 curscr->wattr &= __mask_me;
1723 }
1724 /* Don't leave the screen with altcharset set (don't check ms). */
1725 #ifndef HAVE_WCHAR
1726 if (curscr->wattr & __ALTCHARSET) {
1727 tputs(__tc_ae, 0, __cputchar);
1728 curscr->wattr &= ~__ALTCHARSET;
1729 }
1730 #else
1731 if (curscr->wattr & WA_ALTCHARSET) {
1732 tputs(__tc_ae, 0, __cputchar);
1733 curscr->wattr &= ~WA_ALTCHARSET;
1734 }
1735 #endif /* HAVE_WCHAR */
1736 /* Don't leave the screen with colour set (check against ms). */
1737 if (__using_color && isms)
1738 __unset_color(curscr);
1739 }
1740
1741 #ifdef HAVE_WCHAR
1742 /* compare two cells on screen, must have the same forground/background,
1743 * and the same sequence of non-spacing characters */
1744 int
1745 cellcmp( __LDATA *x, __LDATA *y )
1746 {
1747 nschar_t *xnp = x->nsp, *ynp = y->nsp;
1748 int ret = ( x->ch == y->ch ) & ( x->attr == y->attr );
1749
1750 if ( !ret )
1751 return 0;
1752 if ( !xnp && !ynp )
1753 return 1;
1754 if (( xnp && !ynp ) || ( !xnp && ynp ))
1755 return 0;
1756
1757 while ( xnp && ynp ) {
1758 if ( xnp->ch != ynp->ch )
1759 return 0;
1760 xnp = xnp->next;
1761 ynp = ynp->next;
1762 }
1763 return ( !xnp && !ynp );
1764 }
1765
1766 /* compare two line segments */
1767 int
1768 linecmp( __LDATA *xl, __LDATA *yl, size_t len )
1769 {
1770 int i = 0;
1771 __LDATA *xp = xl, *yp = yl;
1772
1773 for ( i = 0; i < len; i++, xp++, yp++ ) {
1774 if ( !cellcmp( xp, yp ))
1775 return 0;
1776 }
1777 return 1;
1778 }
1779
1780 /*
1781 * Output the non-spacing characters associated with the given character
1782 * cell to the screen.
1783 */
1784
1785 void
1786 __cursesi_putnsp(nschar_t *nsp, const int wy, const int wx)
1787 {
1788 nschar_t *p;
1789
1790 p = nsp;
1791 while (p != NULL) {
1792 __cputwchar((int) p->ch);
1793 #ifdef BEBUG
1794 __CTRACE(__CTRACE_REFRESH,
1795 "_cursesi_putnsp: (%d,%d) non-spacing putwchar(0x%x)\n",
1796 wy, wx - 1, p->ch);
1797 #endif
1798 p = p->next;
1799 }
1800 }
1801
1802 #endif /* HAVE_WCHAR */
1803