1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
34 /*-----------------------------------------------------------------
38 * The routine doupdate() and its dependents. Also _nc_outstr(),
39 * so all physical output is concentrated here (except _nc_outch()
42 *-----------------------------------------------------------------*/
48 #include <curses.priv.h>
50 #if defined(TRACE) && HAVE_SYS_TIMES_H && HAVE_TIMES
51 #define USE_TRACE_TIMES 1
53 #define USE_TRACE_TIMES 0
56 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
61 #include <sys/times.h>
67 #include <sys/select.h>
73 MODULE_ID("$Id: tty_update.c,v 1.146 2000/10/07 01:11:44 tom Exp $")
76 * This define controls the line-breakout optimization. Every once in a
77 * while during screen refresh, we want to check for input and abort the
78 * update if there's some waiting. CHECK_INTERVAL controls the number of
79 * changed lines to be emitted between input checks.
81 * Note: Input-check-and-abort is no longer done if the screen is being
82 * updated from scratch. This is a feature, not a bug.
84 #define CHECK_INTERVAL 5
86 #define FILL_BCE() (SP->_coloron && !SP->_default_color && !back_color_erase)
89 * Enable checking to see if doupdate and friends are tracking the true
90 * cursor position correctly. NOTE: this is a debugging hack which will
91 * work ONLY on ANSI-compatible terminals!
93 /* #define POSITION_DEBUG */
95 static inline chtype ClrBlank(WINDOW *win);
96 static int ClrBottom(int total);
97 static void ClearScreen(chtype blank);
98 static void ClrUpdate(void);
99 static void DelChar(int count);
100 static void InsStr(chtype * line, int count);
101 static void TransformLine(int const lineno);
103 #ifdef POSITION_DEBUG
104 /****************************************************************************
106 * Debugging code. Only works on ANSI-standard terminals.
108 ****************************************************************************/
111 position_check(int expected_y, int expected_x, char *legend)
112 /* check to see if the real cursor position matches the virtual */
118 if (!_nc_tracing || (expected_y < 0 && expected_x < 0))
122 memset(buf, '\0', sizeof(buf));
123 putp("\033[6n"); /* only works on ANSI-compatibles */
127 int ask = sizeof(buf) - 1 - (s - buf);
128 int got = read(0, s, ask);
132 } while (strchr(buf, 'R') == 0);
133 _tracef("probe returned %s", _nc_visbuf(buf));
135 /* try to interpret as a position report */
136 if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
137 _tracef("position probe failed in %s", legend);
143 if (y - 1 != expected_y || x - 1 != expected_x) {
145 tputs(tparm("\033[%d;%dH", expected_y + 1, expected_x + 1), 1, _nc_outch);
146 _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
147 y - 1, x - 1, expected_y, expected_x, legend);
149 _tracef("position matches OK in %s", legend);
154 #define position_check(expected_y, expected_x, legend) /* nothing */
155 #endif /* POSITION_DEBUG */
157 /****************************************************************************
159 * Optimized update code
161 ****************************************************************************/
164 GoTo(int const row, int const col)
166 chtype oldattr = SP->_current_attr;
168 TR(TRACE_MOVE, ("GoTo(%d, %d) from (%d, %d)",
169 row, col, SP->_cursrow, SP->_curscol));
171 position_check(SP->_cursrow, SP->_curscol, "GoTo");
174 * Force restore even if msgr is on when we're in an alternate
175 * character set -- these have a strong tendency to screw up the
176 * CR & LF used for local character motions!
178 if ((oldattr & A_ALTCHARSET)
179 || (oldattr && !move_standout_mode)) {
180 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before move",
181 oldattr, _traceattr(oldattr)));
185 mvcur(SP->_cursrow, SP->_curscol, row, col);
188 position_check(SP->_cursrow, SP->_curscol, "GoTo2");
192 PutAttrChar(chtype ch)
196 if (tilde_glitch && (TextOf(ch) == '~'))
197 ch = ('`' | AttrOf(ch));
199 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
201 SP->_cursrow, SP->_curscol));
204 if (SP->_outch != 0) {
207 putc(data, SP->_ofp); /* macro's fastest... */
214 TPUTS_TRACE("char_padding");
221 /* check for pending input */
223 bool have_pending = FALSE;
226 * Only carry out this check when the flag is zero, otherwise we'll
227 * have the refreshing slow down drastically (or stop) if there's an
228 * unread character available.
230 if (SP->_fifohold != 0)
233 if (SP->_checkfd >= 0) {
235 struct pollfd fds[1];
236 fds[0].fd = SP->_checkfd;
237 fds[0].events = POLLIN;
238 if (poll(fds, 1, 0) > 0) {
241 #elif defined(__BEOS__)
243 * BeOS's select() is declared in socket.h, so the configure script does
244 * not see it. That's just as well, since that function works only for
245 * sockets. This (using snooze and ioctl) was distilled from Be's patch
246 * for ncurses which uses a separate thread to simulate select().
248 * FIXME: the return values from the ioctl aren't very clear if we get
252 int howmany = ioctl(0, 'ichr', &n);
253 if (howmany >= 0 && n > 0) {
258 struct timeval ktimeout;
261 ktimeout.tv_usec = 0;
264 FD_SET(SP->_checkfd, &fdset);
265 if (select(SP->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
278 * No one supports recursive inline functions. However, gcc is quieter if we
279 * instantiate the recursive part separately.
281 #if CC_HAS_INLINE_FUNCS
282 static void callPutChar(chtype const);
284 #define callPutChar(ch) PutChar(ch)
287 static inline void PutChar(chtype const ch); /* forward declaration */
289 /* put char at lower right corner */
291 PutCharLR(chtype const ch)
293 if (!auto_right_margin) {
294 /* we can put the char directly */
296 } else if (enter_am_mode && exit_am_mode) {
297 /* we can suppress automargin */
298 TPUTS_TRACE("exit_am_mode");
303 position_check(SP->_cursrow, SP->_curscol, "exit_am_mode");
305 TPUTS_TRACE("enter_am_mode");
307 } else if ((enter_insert_mode && exit_insert_mode)
308 || insert_character || parm_ich) {
309 GoTo(screen_lines - 1, screen_columns - 2);
311 GoTo(screen_lines - 1, screen_columns - 2);
312 InsStr(newscr->_line[screen_lines - 1].text + screen_columns - 2, 1);
319 if (eat_newline_glitch) {
321 * xenl can manifest two different ways. The vt100
322 * way is that, when you'd expect the cursor to wrap,
323 * it stays hung at the right margin (on top of the
324 * character just emitted) and doesn't wrap until the
325 * *next* graphic char is emitted. The c100 way is
326 * to ignore LF received just after an am wrap.
328 * An aggressive way to handle this would be to
329 * emit CR/LF after the char and then assume the wrap
330 * is done, you're on the first position of the next
331 * line, and the terminal out of its weird state.
332 * Here it's safe to just tell the code that the
333 * cursor is in hyperspace and let the next mvcur()
334 * call straighten things out.
338 } else if (auto_right_margin) {
344 position_check(SP->_cursrow, SP->_curscol, "wrap_cursor");
348 PutChar(chtype const ch)
349 /* insert character, handling automargin stuff */
351 if (SP->_cursrow == screen_lines - 1 && SP->_curscol == screen_columns - 1)
356 if (SP->_curscol >= screen_columns)
359 position_check(SP->_cursrow, SP->_curscol, "PutChar");
363 * Check whether the given character can be output by clearing commands. This
364 * includes test for being a space and not including any 'bad' attributes, such
365 * as A_REVERSE. All attribute flags which don't affect appearance of a space
366 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
369 can_clear_with(chtype ch)
371 if (!back_color_erase && SP->_coloron) {
374 #if NCURSES_EXT_FUNCS
375 if (!SP->_default_color)
377 if (SP->_default_fg != C_MASK || SP->_default_bg != C_MASK)
381 return ((ch & ~(NONBLANK_ATTR | A_COLOR)) == BLANK);
385 * Issue a given span of characters from an array.
386 * Must be functionally equivalent to:
387 * for (i = 0; i < num; i++)
389 * but can leave the cursor positioned at the middle of the interval.
391 * Returns: 0 - cursor is at the end of interval
392 * 1 - cursor is somewhere in the middle
394 * This code is optimized using ech and rep.
397 EmitRange(const chtype * ntext, int num)
401 if (erase_chars || repeat_char) {
406 while (num > 1 && ntext[0] != ntext[1]) {
418 while (runcount < num && ntext[runcount] == ntext0)
422 * The cost expression in the middle isn't exactly right.
423 * _cup_ch_cost is an upper bound on the cost for moving to the
424 * end of the erased area, but not the cost itself (which we
425 * can't compute without emitting the move). This may result
426 * in erase_chars not getting used in some situations for
427 * which it would be marginally advantageous.
430 && runcount > SP->_ech_cost + SP->_cup_ch_cost
431 && can_clear_with(ntext0)) {
433 putp(tparm(erase_chars, runcount));
436 * If this is the last part of the given interval,
437 * don't bother moving cursor, since it can be the
438 * last update on the line.
440 if (runcount < num) {
441 GoTo(SP->_cursrow, SP->_curscol + runcount);
443 return 1; /* cursor stays in the middle */
445 } else if (repeat_char && runcount > SP->_rep_cost) {
446 bool wrap_possible = (SP->_curscol + runcount >= screen_columns);
447 int rep_count = runcount;
453 putp(tparm(repeat_char, TextOf(ntext0), rep_count));
454 SP->_curscol += rep_count;
459 for (i = 0; i < runcount; i++)
468 for (i = 0; i < num; i++)
474 * Output the line in the given range [first .. last]
476 * If there's a run of identical characters that's long enough to justify
477 * cursor movement, use that also.
479 * Returns: same as EmitRange
483 const chtype * otext,
484 const chtype * ntext,
490 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %d, %d, %d)",
491 otext, ntext, row, first, last));
494 && (last - first + 1) > SP->_inline_cost) {
495 for (j = first, run = 0; j <= last; j++) {
496 if (otext[j] == ntext[j]) {
499 if (run > SP->_inline_cost) {
500 int before_run = (j - run);
501 EmitRange(ntext + first, before_run - first);
502 GoTo(row, first = j);
508 return EmitRange(ntext + first, last - first + 1);
511 #if CC_HAS_INLINE_FUNCS
513 callPutChar(chtype const ch)
519 /* leave unbracketed here so 'indent' works */
520 #define MARK_NOCHANGE(win,row) \
521 win->_line[row].firstchar = _NOCHANGE; \
522 win->_line[row].lastchar = _NOCHANGE; \
523 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
531 struct tms before, after;
532 #endif /* USE_TRACE_TIMES */
534 T((T_CALLED("doupdate()")));
537 if (_nc_tracing & TRACE_UPDATE) {
539 _tracef("curscr is clear");
541 _tracedump("curscr", curscr);
542 _tracedump("newscr", newscr);
546 _nc_signal_handler(FALSE);
552 if (SP->_endwin || SP->_sig_winch) {
554 * This is a transparent extension: XSI does not address it,
555 * and applications need not know that ncurses can do it.
557 * Check if the terminal size has changed while curses was off
558 * (this can happen in an xterm, for example), and resize the
559 * ncurses data structures accordingly.
561 _nc_update_screensize();
567 T(("coming back from shell mode"));
572 SP->_mouse_resume(SP);
577 /* zero the metering machinery */
579 (void) times(&before);
580 #endif /* USE_TRACE_TIMES */
583 * This is the support for magic-cookie terminals. The
584 * theory: we scan the virtual screen looking for attribute
585 * turnons. Where we find one, check to make sure it's
586 * realizable by seeing if the required number of
587 * un-attributed blanks are present before and after the
588 * attributed range; try to shift the range boundaries over
589 * blanks (not changing the screen display) so this becomes
590 * true. If it is, shift the beginning attribute change
591 * appropriately (the end one, if we've gotten this far, is
592 * guaranteed room for its cookie). If not, nuke the added
593 * attributes out of the span.
596 if (magic_cookie_glitch > 0) {
598 attr_t rattr = A_NORMAL;
600 for (i = 0; i < screen_lines; i++) {
601 for (j = 0; j < screen_columns; j++) {
603 chtype turnon = AttrOf(newscr->_line[i].text[j]) & ~rattr;
605 /* is an attribute turned on here? */
607 rattr = AttrOf(newscr->_line[i].text[j]);
611 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
612 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
615 * If the attribute change location is a blank with a
616 * "safe" attribute, undo the attribute turnon. This may
617 * ensure there's enough room to set the attribute before
618 * the first non-blank in the run.
620 #define SAFE(a) (!((a) & (chtype)~NONBLANK_ATTR))
621 if (TextOf(newscr->_line[i].text[j]) == ' ' && SAFE(turnon)) {
622 newscr->_line[i].text[j] &= ~turnon;
626 /* check that there's enough room at start of span */
627 for (k = 1; k <= magic_cookie_glitch; k++) {
629 || TextOf(newscr->_line[i].text[j - k]) != ' '
630 || !SAFE(AttrOf(newscr->_line[i].text[j - k])))
634 bool end_onscreen = FALSE;
637 /* find end of span, if it's onscreen */
638 for (m = i; m < screen_lines; m++) {
639 for (; n < screen_columns; n++) {
640 if (AttrOf(newscr->_line[m].text[n]) == rattr) {
643 ("Range attributed with %s ends at (%d, %d)",
644 _traceattr(turnon), m, n));
651 ("Range attributed with %s ends offscreen",
652 _traceattr(turnon)));
656 chtype *lastline = newscr->_line[m].text;
659 * If there are safely-attributed blanks at the
660 * end of the range, shorten the range. This will
661 * help ensure that there is enough room at end
665 && TextOf(lastline[n]) == ' '
666 && SAFE(AttrOf(lastline[n])))
667 lastline[n--] &= ~turnon;
669 /* check that there's enough room at end of span */
670 for (k = 1; k <= magic_cookie_glitch; k++)
671 if (n + k >= screen_columns
672 || TextOf(lastline[n + k]) != ' '
673 || !SAFE(AttrOf(lastline[n + k])))
682 ("Clearing %s beginning at (%d, %d)",
683 _traceattr(turnon), i, j));
685 /* turn off new attributes over span */
686 for (p = i; p < screen_lines; p++) {
687 for (; q < screen_columns; q++) {
688 if (AttrOf(newscr->_line[p].text[q]) == rattr)
690 newscr->_line[p].text[q] &= ~turnon;
697 ("Cookie space for %s found before (%d, %d)",
698 _traceattr(turnon), i, j));
701 * back up the start of range so there's room
702 * for cookies before the first nonblank character
704 for (k = 1; k <= magic_cookie_glitch; k++)
705 newscr->_line[i].text[j - k] |= turnon;
708 rattr = AttrOf(newscr->_line[i].text[j]);
713 /* show altered highlights after magic-cookie check */
714 if (_nc_tracing & TRACE_UPDATE) {
715 _tracef("After magic-cookie check...");
716 _tracedump("newscr", newscr);
720 #endif /* USE_XMC_SUPPORT */
723 if (curscr->_clear || newscr->_clear) { /* force refresh ? */
724 TR(TRACE_UPDATE, ("clearing and updating from scratch"));
726 curscr->_clear = FALSE; /* reset flag */
727 newscr->_clear = FALSE; /* reset flag */
729 int changedlines = CHECK_INTERVAL;
734 nonempty = min(screen_lines, newscr->_maxy + 1);
736 if (SP->_scrolling) {
737 _nc_scroll_optimize();
740 nonempty = ClrBottom(nonempty);
742 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
743 for (i = 0; i < nonempty; i++) {
745 * Here is our line-breakout optimization.
747 if (changedlines == CHECK_INTERVAL) {
754 * newscr->line[i].firstchar is normally set
755 * by wnoutrefresh. curscr->line[i].firstchar
756 * is normally set by _nc_scroll_window in the
757 * vertical-movement optimization code,
759 if (newscr->_line[i].firstchar != _NOCHANGE
760 || curscr->_line[i].firstchar != _NOCHANGE) {
765 /* mark line changed successfully */
766 if (i <= newscr->_maxy) {
767 MARK_NOCHANGE(newscr, i)
769 if (i <= curscr->_maxy) {
770 MARK_NOCHANGE(curscr, i)
775 /* put everything back in sync */
776 for (i = nonempty; i <= newscr->_maxy; i++) {
777 MARK_NOCHANGE(newscr, i)
779 for (i = nonempty; i <= curscr->_maxy; i++) {
780 MARK_NOCHANGE(curscr, i)
783 if (!newscr->_leaveok) {
784 curscr->_curx = newscr->_curx;
785 curscr->_cury = newscr->_cury;
787 GoTo(curscr->_cury, curscr->_curx);
792 * Keep the physical screen in normal mode in case we get other
793 * processes writing to the screen.
795 UpdateAttrs(A_NORMAL);
798 curscr->_attrs = newscr->_attrs;
801 (void) times(&after);
803 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
805 after.tms_stime - before.tms_stime,
806 after.tms_utime - before.tms_utime));
807 #endif /* USE_TRACE_TIMES */
809 _nc_signal_handler(TRUE);
817 * Returns the attributed character that corresponds to the "cleared"
818 * screen. If the terminal has the back-color-erase feature, this will be
819 * colored according to the wbkgd() call.
821 * We treat 'curscr' specially because it isn't supposed to be set directly
822 * in the wbkgd() call. Assume 'stdscr' for this case.
824 #define BCE_ATTRS (A_NORMAL|A_COLOR)
825 #define BCE_BKGD(win) (((win) == curscr ? stdscr : (win))->_bkgd)
828 ClrBlank(WINDOW *win)
830 chtype blank = BLANK;
831 if (back_color_erase)
832 blank |= (BCE_BKGD(win) & BCE_ATTRS);
839 ** Update by clearing and redrawing the entire screen.
847 chtype blank = ClrBlank(stdscr);
848 int nonempty = min(screen_lines, newscr->_maxy + 1);
850 TR(TRACE_UPDATE, ("ClrUpdate() called"));
854 TR(TRACE_UPDATE, ("updating screen from scratch"));
856 nonempty = ClrBottom(nonempty);
858 for (i = 0; i < nonempty; i++)
865 ** Clear to end of current line, starting at the cursor position
869 ClrToEOL(chtype blank, bool needclear)
874 && SP->_cursrow >= 0) {
875 for (j = SP->_curscol; j < screen_columns; j++) {
877 chtype *cp = &(curscr->_line[SP->_cursrow].text[j]);
891 TPUTS_TRACE("clr_eol");
892 if (SP->_el_cost > (screen_columns - SP->_curscol)) {
893 int count = (screen_columns - SP->_curscol);
905 ** Clear to end of screen, starting at the cursor position
909 ClrToEOS(chtype blank)
917 TPUTS_TRACE("clr_eos");
918 tputs(clr_eos, screen_lines - row, _nc_outch);
920 while (col < screen_columns)
921 curscr->_line[row].text[col++] = blank;
923 for (row++; row < screen_lines; row++) {
924 for (col = 0; col < screen_columns; col++)
925 curscr->_line[row].text[col] = blank;
932 * Test if clearing the end of the screen would satisfy part of the
933 * screen-update. Do this by scanning backwards through the lines in the
934 * screen, checking if each is blank, and one or more are changed.
942 int last = min(screen_columns, newscr->_maxx + 1);
943 chtype blank = ClrBlank(stdscr);
946 if (clr_eos && can_clear_with(blank)) {
948 for (row = total - 1; row >= 0; row--) {
949 for (col = 0, ok = TRUE; ok && col < last; col++) {
950 ok = (newscr->_line[row].text[col] == blank);
955 for (col = 0; ok && col < last; col++) {
956 ok = (curscr->_line[row].text[col] == blank);
962 /* don't use clr_eos for just one line if clr_eol available */
963 if (top < total - 1 || (top < total && !clr_eol && !clr_bol)) {
967 if (SP->oldhash && SP->newhash) {
968 for (row = top; row < screen_lines; row++)
969 SP->oldhash[row] = SP->newhash[row];
977 ** TransformLine(lineno)
979 ** Transform the given line in curscr to the one in newscr, using
980 ** Insert/Delete Character if _nc_idcok && has_ic().
982 ** firstChar = position of first different character in line
983 ** oLastChar = position of last different character in old line
984 ** nLastChar = position of last different character in new line
987 ** overwrite chars up to min(oLastChar, nLastChar)
988 ** if oLastChar < nLastChar
989 ** insert newLine[oLastChar+1..nLastChar]
991 ** delete oLastChar - nLastChar spaces
995 TransformLine(int const lineno)
997 int firstChar, oLastChar, nLastChar;
998 chtype *newLine = newscr->_line[lineno].text;
999 chtype *oldLine = curscr->_line[lineno].text;
1001 bool attrchanged = FALSE;
1003 TR(TRACE_UPDATE, ("TransformLine(%d) called", lineno));
1005 /* copy new hash value to old one */
1006 if (SP->oldhash && SP->newhash)
1007 SP->oldhash[lineno] = SP->newhash[lineno];
1009 #define ColorOf(n) ((n) & A_COLOR)
1010 #define unColor(n) ((n) & ALL_BUT_COLOR)
1012 * If we have colors, there is the possibility of having two color pairs
1013 * that display as the same colors. For instance, Lynx does this. Check
1014 * for this case, and update the old line with the new line's colors when
1015 * they are equivalent.
1023 for (n = 0; n < screen_columns; n++) {
1024 if (newLine[n] != oldLine[n]) {
1025 oldColor = ColorOf(oldLine[n]);
1026 newColor = ColorOf(newLine[n]);
1027 if (oldColor != newColor
1028 && unColor(oldLine[n]) == unColor(newLine[n])) {
1029 oldPair = PAIR_NUMBER(oldColor);
1030 newPair = PAIR_NUMBER(newColor);
1031 if (oldPair < COLOR_PAIRS
1032 && newPair < COLOR_PAIRS
1033 && SP->_color_pairs[oldPair] == SP->_color_pairs[newPair]) {
1034 oldLine[n] &= ~A_COLOR;
1035 oldLine[n] |= ColorOf(newLine[n]);
1042 if (ceol_standout_glitch && clr_eol) {
1044 while (firstChar < screen_columns) {
1045 if (AttrOf(newLine[firstChar]) != AttrOf(oldLine[firstChar]))
1053 if (attrchanged) { /* we may have to disregard the whole line */
1054 GoTo(lineno, firstChar);
1055 ClrToEOL(ClrBlank(curscr), FALSE);
1056 PutRange(oldLine, newLine, lineno, 0, (screen_columns - 1));
1059 #define NEW(r,c) newscr->_line[r].text[c]
1060 #define xmc_turn_on(a,b) ((((a)^(b)) & ~(a) & SP->_xmc_triggers) != 0)
1061 #define xmc_turn_off(a,b) xmc_turn_on(b,a)
1064 * This is a very simple loop to paint characters which may have the
1065 * magic cookie glitch embedded. It doesn't know much about video
1066 * attributes which are continued from one line to the next. It
1067 * assumes that we have filtered out requests for attribute changes
1068 * that do not get mapped to blank positions.
1070 * FIXME: we are not keeping track of where we put the cookies, so this
1071 * will work properly only once, since we may overwrite a cookie in a
1072 * following operation.
1074 } else if (magic_cookie_glitch > 0) {
1075 GoTo(lineno, firstChar);
1076 for (n = 0; n < screen_columns; n++) {
1077 int m = n + magic_cookie_glitch;
1079 /* check for turn-on:
1080 * If we are writing an attributed blank, where the
1081 * previous cell is not attributed.
1083 if (TextOf(newLine[n]) == ' '
1085 && xmc_turn_on(newLine[n - 1], newLine[n]))
1088 && xmc_turn_on(NEW(lineno - 1, screen_columns - 1),
1093 PutChar(newLine[n]);
1095 /* check for turn-off:
1096 * If we are writing an attributed non-blank, where the
1097 * next cell is blank, and not attributed.
1099 if (TextOf(newLine[n]) != ' '
1100 && ((n + 1 < screen_columns
1101 && xmc_turn_off(newLine[n], newLine[n + 1]))
1102 || (n + 1 >= screen_columns
1103 && lineno + 1 < screen_lines
1104 && xmc_turn_off(newLine[n], NEW(lineno + 1, 0))))) {
1114 /* find the first differing character */
1115 while (firstChar < screen_columns &&
1116 newLine[firstChar] == oldLine[firstChar])
1119 /* if there wasn't one, we're done */
1120 if (firstChar >= screen_columns)
1123 /* it may be cheap to clear leading whitespace with clr_bol */
1124 if (clr_bol && can_clear_with(blank = newLine[0])) {
1125 int oFirstChar, nFirstChar;
1127 for (oFirstChar = 0; oFirstChar < screen_columns; oFirstChar++)
1128 if (oldLine[oFirstChar] != blank)
1130 for (nFirstChar = 0; nFirstChar < screen_columns; nFirstChar++)
1131 if (newLine[nFirstChar] != blank)
1134 if (nFirstChar > oFirstChar + SP->_el1_cost) {
1135 if (nFirstChar >= screen_columns && SP->_el_cost <= SP->_el1_cost) {
1138 TPUTS_TRACE("clr_eol");
1141 GoTo(lineno, nFirstChar - 1);
1143 TPUTS_TRACE("clr_bol");
1147 while (firstChar < nFirstChar)
1148 oldLine[firstChar++] = blank;
1150 if (firstChar >= screen_columns)
1155 blank = newLine[screen_columns - 1];
1157 if (!can_clear_with(blank)) {
1158 /* find the last differing character */
1159 nLastChar = screen_columns - 1;
1161 while (nLastChar > firstChar
1162 && newLine[nLastChar] == oldLine[nLastChar])
1165 if (nLastChar >= firstChar) {
1166 GoTo(lineno, firstChar);
1167 PutRange(oldLine, newLine, lineno, firstChar, nLastChar);
1168 memcpy(oldLine + firstChar,
1169 newLine + firstChar,
1170 (nLastChar - firstChar + 1) * sizeof(chtype));
1175 /* find last non-blank character on old line */
1176 oLastChar = screen_columns - 1;
1177 while (oLastChar > firstChar && oldLine[oLastChar] == blank)
1180 /* find last non-blank character on new line */
1181 nLastChar = screen_columns - 1;
1182 while (nLastChar > firstChar && newLine[nLastChar] == blank)
1185 if ((nLastChar == firstChar)
1186 && (SP->_el_cost < (oLastChar - nLastChar))) {
1187 GoTo(lineno, firstChar);
1188 if (newLine[firstChar] != blank)
1189 PutChar(newLine[firstChar]);
1190 ClrToEOL(blank, FALSE);
1191 } else if ((nLastChar != oLastChar)
1192 && (newLine[nLastChar] != oldLine[oLastChar]
1193 || !(_nc_idcok && has_ic()))) {
1194 GoTo(lineno, firstChar);
1195 if ((oLastChar - nLastChar) > SP->_el_cost) {
1196 if (PutRange(oldLine, newLine, lineno, firstChar, nLastChar))
1197 GoTo(lineno, nLastChar + 1);
1198 ClrToEOL(blank, FALSE);
1200 n = max(nLastChar, oLastChar);
1201 PutRange(oldLine, newLine, lineno, firstChar, n);
1204 int nLastNonblank = nLastChar;
1205 int oLastNonblank = oLastChar;
1207 /* find the last characters that really differ */
1208 while (newLine[nLastChar] == oldLine[oLastChar]) {
1210 && oLastChar != 0) {
1218 n = min(oLastChar, nLastChar);
1219 if (n >= firstChar) {
1220 GoTo(lineno, firstChar);
1221 PutRange(oldLine, newLine, lineno, firstChar, n);
1224 if (oLastChar < nLastChar) {
1225 int m = max(nLastNonblank, oLastNonblank);
1226 GoTo(lineno, n + 1);
1227 if (InsCharCost(nLastChar - oLastChar)
1229 PutRange(oldLine, newLine, lineno, n + 1, m);
1231 InsStr(&newLine[n + 1], nLastChar - oLastChar);
1233 } else if (oLastChar > nLastChar) {
1234 GoTo(lineno, n + 1);
1235 if (DelCharCost(oLastChar - nLastChar)
1236 > SP->_el_cost + nLastNonblank - (n + 1)) {
1237 if (PutRange(oldLine, newLine, lineno,
1238 n + 1, nLastNonblank))
1239 GoTo(lineno, nLastNonblank + 1);
1240 ClrToEOL(blank, FALSE);
1243 * The delete-char sequence will
1244 * effectively shift in blanks from the
1245 * right margin of the screen. Ensure
1246 * that they are the right color by
1247 * setting the video attributes from
1248 * the last character on the row.
1251 DelChar(oLastChar - nLastChar);
1257 /* update the code's internal representation */
1258 if (screen_columns > firstChar)
1259 memcpy(oldLine + firstChar,
1260 newLine + firstChar,
1261 (screen_columns - firstChar) * sizeof(chtype));
1265 ** ClearScreen(blank)
1267 ** Clear the physical screen and put cursor at home
1272 ClearScreen(chtype blank)
1275 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1277 TR(TRACE_UPDATE, ("ClearScreen() called"));
1279 #if NCURSES_EXT_FUNCS
1281 && !SP->_default_color) {
1282 _nc_do_color(COLOR_PAIR(SP->_current_attr), 0, FALSE, _nc_outch);
1283 if (!back_color_erase) {
1292 TPUTS_TRACE("clear_screen");
1294 SP->_cursrow = SP->_curscol = 0;
1295 position_check(SP->_cursrow, SP->_curscol, "ClearScreen");
1296 } else if (clr_eos) {
1297 SP->_cursrow = SP->_curscol = -1;
1301 TPUTS_TRACE("clr_eos");
1303 } else if (clr_eol) {
1304 SP->_cursrow = SP->_curscol = -1;
1307 for (i = 0; i < screen_lines; i++) {
1309 TPUTS_TRACE("clr_eol");
1316 for (i = 0; i < screen_lines; i++) {
1318 for (j = 0; j < screen_columns; j++)
1324 for (i = 0; i < screen_lines; i++) {
1325 for (j = 0; j < screen_columns; j++)
1326 curscr->_line[i].text[j] = blank;
1329 TR(TRACE_UPDATE, ("screen cleared"));
1333 ** InsStr(line, count)
1335 ** Insert the count characters pointed to by line.
1340 InsStr(chtype * line, int count)
1342 TR(TRACE_UPDATE, ("InsStr(%p,%d) called", line, count));
1344 /* Prefer parm_ich as it has the smallest cost - no need to shift
1345 * the whole line on each character. */
1346 /* The order must match that of InsCharCost. */
1348 TPUTS_TRACE("parm_ich");
1349 tputs(tparm(parm_ich, count), count, _nc_outch);
1355 } else if (enter_insert_mode && exit_insert_mode) {
1356 TPUTS_TRACE("enter_insert_mode");
1357 putp(enter_insert_mode);
1360 if (insert_padding) {
1361 TPUTS_TRACE("insert_padding");
1362 putp(insert_padding);
1367 TPUTS_TRACE("exit_insert_mode");
1368 putp(exit_insert_mode);
1371 TPUTS_TRACE("insert_character");
1372 putp(insert_character);
1374 if (insert_padding) {
1375 TPUTS_TRACE("insert_padding");
1376 putp(insert_padding);
1382 position_check(SP->_cursrow, SP->_curscol, "InsStr");
1388 ** Delete count characters at current position
1397 TR(TRACE_UPDATE, ("DelChar(%d) called, position = (%d,%d)", count,
1398 newscr->_cury, newscr->_curx));
1401 TPUTS_TRACE("parm_dch");
1402 tputs(tparm(parm_dch, count), count, _nc_outch);
1404 for (n = 0; n < count; n++) {
1405 TPUTS_TRACE("delete_character");
1406 putp(delete_character);
1412 ** _nc_outstr(char *str)
1414 ** Emit a string without waiting for update.
1418 _nc_outstr(const char *str)
1425 * Physical-scrolling support
1427 * This code was adapted from Keith Bostic's hardware scrolling
1428 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1429 * capabilities, narrowed the call interface slightly, and cleaned
1430 * up some convoluted tests. I also added support for the memory_above
1431 * memory_below, and non_dest_scroll_region capabilities.
1433 * For this code to work, we must have either
1434 * change_scroll_region and scroll forward/reverse commands, or
1435 * insert and delete line capabilities.
1436 * When the scrolling region has been set, the cursor has to
1437 * be at the last line of the region to make the scroll up
1438 * happen, or on the first line of region to scroll down.
1440 * This code makes one aesthetic decision in the opposite way from
1441 * BSD curses. BSD curses preferred pairs of il/dl operations
1442 * over scrolls, allegedly because il/dl looked faster. We, on
1443 * the other hand, prefer scrolls because (a) they're just as fast
1444 * on many terminals and (b) using them avoids bouncing an
1445 * unchanged bottom section of the screen up and down, which is
1448 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1450 * I used assumption that capabilities il/il1/dl/dl1 work inside
1451 * changed scroll region not shifting screen contents outside of it.
1452 * If there are any terminals behaving different way, it would be
1453 * necessary to add some conditions to scroll_csr_forward/backward.
1456 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1458 scroll_csr_forward(int n, int top, int bot, int miny, int maxy, chtype blank)
1462 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1465 TPUTS_TRACE("scroll_forward");
1466 tputs(scroll_forward, 0, _nc_outch);
1467 } else if (n == 1 && delete_line && bot == maxy) {
1470 TPUTS_TRACE("delete_line");
1471 tputs(delete_line, 0, _nc_outch);
1472 } else if (parm_index && top == miny && bot == maxy) {
1475 TPUTS_TRACE("parm_index");
1476 tputs(tparm(parm_index, n, 0), n, _nc_outch);
1477 } else if (parm_delete_line && bot == maxy) {
1480 TPUTS_TRACE("parm_delete_line");
1481 tputs(tparm(parm_delete_line, n, 0), n, _nc_outch);
1482 } else if (scroll_forward && top == miny && bot == maxy) {
1485 for (i = 0; i < n; i++) {
1486 TPUTS_TRACE("scroll_forward");
1487 tputs(scroll_forward, 0, _nc_outch);
1489 } else if (delete_line && bot == maxy) {
1492 for (i = 0; i < n; i++) {
1493 TPUTS_TRACE("delete_line");
1494 tputs(delete_line, 0, _nc_outch);
1499 #if NCURSES_EXT_FUNCS
1501 for (i = 0; i < n; i++) {
1503 for (j = 0; j < screen_columns; j++)
1511 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1514 scroll_csr_backward(int n, int top, int bot, int miny, int maxy, chtype blank)
1518 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1521 TPUTS_TRACE("scroll_reverse");
1522 tputs(scroll_reverse, 0, _nc_outch);
1523 } else if (n == 1 && insert_line && bot == maxy) {
1526 TPUTS_TRACE("insert_line");
1527 tputs(insert_line, 0, _nc_outch);
1528 } else if (parm_rindex && top == miny && bot == maxy) {
1531 TPUTS_TRACE("parm_rindex");
1532 tputs(tparm(parm_rindex, n, 0), n, _nc_outch);
1533 } else if (parm_insert_line && bot == maxy) {
1536 TPUTS_TRACE("parm_insert_line");
1537 tputs(tparm(parm_insert_line, n, 0), n, _nc_outch);
1538 } else if (scroll_reverse && top == miny && bot == maxy) {
1541 for (i = 0; i < n; i++) {
1542 TPUTS_TRACE("scroll_reverse");
1543 tputs(scroll_reverse, 0, _nc_outch);
1545 } else if (insert_line && bot == maxy) {
1548 for (i = 0; i < n; i++) {
1549 TPUTS_TRACE("insert_line");
1550 tputs(insert_line, 0, _nc_outch);
1555 #if NCURSES_EXT_FUNCS
1557 for (i = 0; i < n; i++) {
1559 for (j = 0; j < screen_columns; j++)
1567 /* scroll by using delete_line at del and insert_line at ins */
1570 scroll_idl(int n, int del, int ins, chtype blank)
1574 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1579 if (n == 1 && delete_line) {
1580 TPUTS_TRACE("delete_line");
1581 tputs(delete_line, 0, _nc_outch);
1582 } else if (parm_delete_line) {
1583 TPUTS_TRACE("parm_delete_line");
1584 tputs(tparm(parm_delete_line, n, 0), n, _nc_outch);
1585 } else { /* if (delete_line) */
1586 for (i = 0; i < n; i++) {
1587 TPUTS_TRACE("delete_line");
1588 tputs(delete_line, 0, _nc_outch);
1594 if (n == 1 && insert_line) {
1595 TPUTS_TRACE("insert_line");
1596 tputs(insert_line, 0, _nc_outch);
1597 } else if (parm_insert_line) {
1598 TPUTS_TRACE("parm_insert_line");
1599 tputs(tparm(parm_insert_line, n, 0), n, _nc_outch);
1600 } else { /* if (insert_line) */
1601 for (i = 0; i < n; i++) {
1602 TPUTS_TRACE("insert_line");
1603 tputs(insert_line, 0, _nc_outch);
1611 _nc_scrolln(int n, int top, int bot, int maxy)
1612 /* scroll region from top to bot by n lines */
1614 chtype blank = ClrBlank(stdscr);
1616 bool cursor_saved = FALSE;
1619 TR(TRACE_MOVE, ("mvcur_scrolln(%d, %d, %d, %d)", n, top, bot, maxy));
1623 * If we scroll, we might remove a cookie.
1625 if (magic_cookie_glitch > 0) {
1630 if (n > 0) { /* scroll up (forward) */
1632 * Explicitly clear if stuff pushed off top of region might
1633 * be saved by the terminal.
1635 res = scroll_csr_forward(n, top, bot, 0, maxy, blank);
1637 if (res == ERR && change_scroll_region) {
1638 if ((((n == 1 && scroll_forward) || parm_index)
1639 && (SP->_cursrow == bot || SP->_cursrow == bot - 1))
1640 && save_cursor && restore_cursor) {
1641 cursor_saved = TRUE;
1642 TPUTS_TRACE("save_cursor");
1643 tputs(save_cursor, 0, _nc_outch);
1645 TPUTS_TRACE("change_scroll_region");
1646 tputs(tparm(change_scroll_region, top, bot), 0, _nc_outch);
1648 TPUTS_TRACE("restore_cursor");
1649 tputs(restore_cursor, 0, _nc_outch);
1651 SP->_cursrow = SP->_curscol = -1;
1654 res = scroll_csr_forward(n, top, bot, top, bot, blank);
1656 TPUTS_TRACE("change_scroll_region");
1657 tputs(tparm(change_scroll_region, 0, maxy), 0, _nc_outch);
1658 SP->_cursrow = SP->_curscol = -1;
1661 if (res == ERR && _nc_idlok)
1662 res = scroll_idl(n, top, bot - n + 1, blank);
1665 * Clear the newly shifted-in text.
1668 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
1669 if (bot == maxy && clr_eos) {
1673 for (i = 0; i < n; i++) {
1675 ClrToEOL(BLANK, FALSE);
1680 } else { /* (n < 0) - scroll down (backward) */
1681 res = scroll_csr_backward(-n, top, bot, 0, maxy, blank);
1683 if (res == ERR && change_scroll_region) {
1684 if (top != 0 && (SP->_cursrow == top || SP->_cursrow == top - 1)
1685 && save_cursor && restore_cursor) {
1686 cursor_saved = TRUE;
1687 TPUTS_TRACE("save_cursor");
1688 tputs(save_cursor, 0, _nc_outch);
1690 TPUTS_TRACE("change_scroll_region");
1691 tputs(tparm(change_scroll_region, top, bot), 0, _nc_outch);
1693 TPUTS_TRACE("restore_cursor");
1694 tputs(restore_cursor, 0, _nc_outch);
1696 SP->_cursrow = SP->_curscol = -1;
1699 res = scroll_csr_backward(-n, top, bot, top, bot, blank);
1701 TPUTS_TRACE("change_scroll_region");
1702 tputs(tparm(change_scroll_region, 0, maxy), 0, _nc_outch);
1703 SP->_cursrow = SP->_curscol = -1;
1706 if (res == ERR && _nc_idlok)
1707 res = scroll_idl(-n, bot + n + 1, top, blank);
1710 * Clear the newly shifted-in text.
1713 && (non_dest_scroll_region || (memory_above && top == 0))) {
1714 for (i = 0; i < -n; i++) {
1716 ClrToEOL(BLANK, FALSE);
1724 _nc_scroll_window(curscr, n, top, bot, blank);
1726 /* shift hash values too - they can be reused */
1727 _nc_scroll_oldhash(n, top, bot);
1733 _nc_screen_resume(void)
1735 /* make sure terminal is in a sane known state */
1736 SP->_current_attr = A_NORMAL;
1737 newscr->_clear = TRUE;
1739 if (SP->_coloron == TRUE && orig_pair)
1741 if (exit_attribute_mode)
1742 putp(exit_attribute_mode);
1744 /* turn off attributes */
1745 if (exit_alt_charset_mode)
1746 putp(exit_alt_charset_mode);
1747 if (exit_standout_mode)
1748 putp(exit_standout_mode);
1749 if (exit_underline_mode)
1750 putp(exit_underline_mode);
1752 if (exit_insert_mode)
1753 putp(exit_insert_mode);
1754 if (enter_am_mode && exit_am_mode)
1755 putp(auto_right_margin ? enter_am_mode : exit_am_mode);
1759 _nc_screen_init(void)
1761 _nc_screen_resume();
1764 /* wrap up screen handling */
1766 _nc_screen_wrap(void)
1768 UpdateAttrs(A_NORMAL);
1769 #if NCURSES_EXT_FUNCS
1771 && !SP->_default_color) {
1772 SP->_default_color = TRUE;
1773 _nc_do_color(-1, 0, FALSE, _nc_outch);
1774 SP->_default_color = FALSE;
1776 mvcur(SP->_cursrow, SP->_curscol, screen_lines - 1, 0);
1777 SP->_cursrow = screen_lines - 1;
1780 ClrToEOL(BLANK, TRUE);
1787 _nc_do_xmc_glitch(attr_t previous)
1789 attr_t chg = XMC_CHANGES(previous ^ SP->_current_attr);
1793 SP->_curscol += magic_cookie_glitch;
1794 if (SP->_curscol >= SP->_columns)
1796 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie", SP->_cursrow, SP->_curscol));
1801 #endif /* USE_XMC_SUPPORT */