1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
35 /*-----------------------------------------------------------------
39 * The routine doupdate() and its dependents.
40 * All physical output is concentrated here (except _nc_outch()
43 *-----------------------------------------------------------------*/
45 #include <curses.priv.h>
53 #if defined(TRACE) && HAVE_SYS_TIMES_H && HAVE_TIMES
54 #define USE_TRACE_TIMES 1
56 #define USE_TRACE_TIMES 0
59 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
64 #include <sys/times.h>
70 #include <sys/select.h>
77 MODULE_ID("$Id: tty_update.c,v 1.240 2007/06/30 21:07:51 tom Exp $")
80 * This define controls the line-breakout optimization. Every once in a
81 * while during screen refresh, we want to check for input and abort the
82 * update if there's some waiting. CHECK_INTERVAL controls the number of
83 * changed lines to be emitted between input checks.
85 * Note: Input-check-and-abort is no longer done if the screen is being
86 * updated from scratch. This is a feature, not a bug.
88 #define CHECK_INTERVAL 5
90 #define FILL_BCE() (SP->_coloron && !SP->_default_color && !back_color_erase)
92 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
93 static NCURSES_CH_T normal = NewChar(BLANK_TEXT);
96 * Enable checking to see if doupdate and friends are tracking the true
97 * cursor position correctly. NOTE: this is a debugging hack which will
98 * work ONLY on ANSI-compatible terminals!
100 /* #define POSITION_DEBUG */
102 static NCURSES_INLINE NCURSES_CH_T ClrBlank(WINDOW *win);
103 static int ClrBottom(int total);
104 static void ClearScreen(NCURSES_CH_T blank);
105 static void ClrUpdate(void);
106 static void DelChar(int count);
107 static void InsStr(NCURSES_CH_T * line, int count);
108 static void TransformLine(int const lineno);
110 #ifdef POSITION_DEBUG
111 /****************************************************************************
113 * Debugging code. Only works on ANSI-standard terminals.
115 ****************************************************************************/
118 position_check(int expected_y, int expected_x, char *legend)
119 /* check to see if the real cursor position matches the virtual */
125 if (!_nc_tracing || (expected_y < 0 && expected_x < 0))
129 memset(buf, '\0', sizeof(buf));
130 putp("\033[6n"); /* only works on ANSI-compatibles */
134 int ask = sizeof(buf) - 1 - (s - buf);
135 int got = read(0, s, ask);
139 } while (strchr(buf, 'R') == 0);
140 _tracef("probe returned %s", _nc_visbuf(buf));
142 /* try to interpret as a position report */
143 if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
144 _tracef("position probe failed in %s", legend);
150 if (y - 1 != expected_y || x - 1 != expected_x) {
152 tputs(tparm("\033[%d;%dH", expected_y + 1, expected_x + 1), 1, _nc_outch);
153 _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
154 y - 1, x - 1, expected_y, expected_x, legend);
156 _tracef("position matches OK in %s", legend);
161 #define position_check(expected_y, expected_x, legend) /* nothing */
162 #endif /* POSITION_DEBUG */
164 /****************************************************************************
166 * Optimized update code
168 ****************************************************************************/
170 static NCURSES_INLINE void
171 GoTo(int const row, int const col)
173 TR(TRACE_MOVE, ("GoTo(%d, %d) from (%d, %d)",
174 row, col, SP->_cursrow, SP->_curscol));
176 position_check(SP->_cursrow, SP->_curscol, "GoTo");
178 mvcur(SP->_cursrow, SP->_curscol, row, col);
179 position_check(SP->_cursrow, SP->_curscol, "GoTo2");
182 static NCURSES_INLINE void
183 PutAttrChar(CARG_CH_T ch)
189 NCURSES_CH_T attr = CHDEREF(ch);
191 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
193 SP->_cursrow, SP->_curscol));
194 #if USE_WIDEC_SUPPORT
196 * If this is not a valid character, there is nothing more to do.
198 if (isWidecExt(CHDEREF(ch))) {
199 TR(TRACE_CHARPUT, ("...skip"));
203 * Determine the number of character cells which the 'ch' value will use
204 * on the screen. It should be at least one.
206 if ((chlen = wcwidth(CharOf(CHDEREF(ch)))) <= 0) {
207 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
210 * If the character falls into any of these special cases, do
211 * not force the result to a blank:
213 * a) it is printable (this works around a bug in wcwidth()).
214 * b) use_legacy_coding() has been called to modify the treatment
216 * c) the acs_map[] has been initialized to allow codes 0-31
217 * to be rendered. This supports Linux console's "PC"
218 * characters. Codes 128-255 are allowed though this is
221 if (is8bits(CharOf(CHDEREF(ch)))
222 && (isprint(CharOf(CHDEREF(ch)))
223 || (SP->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
224 || (SP->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
225 || (AttrOf(attr) & A_ALTCHARSET
226 && ((CharOfD(ch) < ACS_LEN
228 && SP->_acs_map[CharOfD(ch)] != 0)
229 || (CharOfD(ch) >= 128))))) {
233 TR(TRACE_CHARPUT, ("forced to blank"));
239 if ((AttrOf(attr) & A_ALTCHARSET)
241 && CharOfD(ch) < ACS_LEN) {
242 my_ch = CHDEREF(ch); /* work around const param */
243 #if USE_WIDEC_SUPPORT
245 * This is crude & ugly, but works most of the time. It checks if the
246 * acs_chars string specified that we have a mapping for this
247 * character, and uses the wide-character mapping when we expect the
248 * normal one to be broken (by mis-design ;-).
250 if (SP->_screen_acs_fix
251 && SP->_screen_acs_map[CharOf(my_ch)]) {
252 RemAttr(attr, A_ALTCHARSET);
253 my_ch = _nc_wacs[CharOf(my_ch)];
257 * If we (still) have alternate character set, it is the normal 8bit
258 * flavor. The _screen_acs_map[] array tells if the character was
259 * really in acs_chars, needed because of the way wide/normal line
260 * drawing flavors are integrated.
262 if (AttrOf(attr) & A_ALTCHARSET) {
264 chtype temp = UChar(SP->_acs_map[j]);
266 if (!(SP->_screen_acs_map[j])) {
267 RemAttr(attr, A_ALTCHARSET);
272 SetChar(my_ch, temp, AttrOf(attr));
276 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
277 SetChar(tilde, L('`'), AttrOf(attr));
282 #if !USE_WIDEC_SUPPORT
283 /* FIXME - we do this special case for signal handling, should see how to
284 * make it work for wide characters.
286 if (SP->_outch != 0) {
287 SP->_outch(UChar(ch));
291 PUTC(CHDEREF(ch), SP->_ofp); /* macro's fastest... */
294 SP->_curscol += chlen;
296 TPUTS_TRACE("char_padding");
303 /* check for pending input */
305 bool have_pending = FALSE;
308 * Only carry out this check when the flag is zero, otherwise we'll
309 * have the refreshing slow down drastically (or stop) if there's an
310 * unread character available.
312 if (SP->_fifohold != 0)
315 if (SP->_checkfd >= 0) {
317 struct pollfd fds[1];
318 fds[0].fd = SP->_checkfd;
319 fds[0].events = POLLIN;
320 if (poll(fds, 1, 0) > 0) {
323 #elif defined(__BEOS__)
325 * BeOS's select() is declared in socket.h, so the configure script does
326 * not see it. That's just as well, since that function works only for
327 * sockets. This (using snooze and ioctl) was distilled from Be's patch
328 * for ncurses which uses a separate thread to simulate select().
330 * FIXME: the return values from the ioctl aren't very clear if we get
334 int howmany = ioctl(0, 'ichr', &n);
335 if (howmany >= 0 && n > 0) {
340 struct timeval ktimeout;
343 ktimeout.tv_usec = 0;
346 FD_SET(SP->_checkfd, &fdset);
347 if (select(SP->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
359 /* put char at lower right corner */
361 PutCharLR(const ARG_CH_T ch)
363 if (!auto_right_margin) {
364 /* we can put the char directly */
366 } else if (enter_am_mode && exit_am_mode) {
367 /* we can suppress automargin */
368 TPUTS_TRACE("exit_am_mode");
373 position_check(SP->_cursrow, SP->_curscol, "exit_am_mode");
375 TPUTS_TRACE("enter_am_mode");
377 } else if ((enter_insert_mode && exit_insert_mode)
378 || insert_character || parm_ich) {
379 GoTo(screen_lines - 1, screen_columns - 2);
381 GoTo(screen_lines - 1, screen_columns - 2);
382 InsStr(newscr->_line[screen_lines - 1].text + screen_columns - 2, 1);
387 * Wrap the cursor position, i.e., advance to the beginning of the next line.
392 if (eat_newline_glitch) {
394 * xenl can manifest two different ways. The vt100 way is that, when
395 * you'd expect the cursor to wrap, it stays hung at the right margin
396 * (on top of the character just emitted) and doesn't wrap until the
397 * *next* graphic char is emitted. The c100 way is to ignore LF
398 * received just after an am wrap.
400 * An aggressive way to handle this would be to emit CR/LF after the
401 * char and then assume the wrap is done, you're on the first position
402 * of the next line, and the terminal out of its weird state. Here
403 * it's safe to just tell the code that the cursor is in hyperspace and
404 * let the next mvcur() call straighten things out.
408 } else if (auto_right_margin) {
412 * We've actually moved - but may have to work around problems with
413 * video attributes not working.
415 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP))) {
416 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
417 (unsigned long) AttrOf(SCREEN_ATTRS(SP)),
418 _traceattr(AttrOf(SCREEN_ATTRS(SP)))));
419 (void) VIDATTR(A_NORMAL, 0);
424 position_check(SP->_cursrow, SP->_curscol, "wrap_cursor");
427 static NCURSES_INLINE void
428 PutChar(const ARG_CH_T ch)
429 /* insert character, handling automargin stuff */
431 if (SP->_cursrow == screen_lines - 1 && SP->_curscol == screen_columns - 1)
436 if (SP->_curscol >= screen_columns)
439 position_check(SP->_cursrow, SP->_curscol, "PutChar");
443 * Check whether the given character can be output by clearing commands. This
444 * includes test for being a space and not including any 'bad' attributes, such
445 * as A_REVERSE. All attribute flags which don't affect appearance of a space
446 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
448 static NCURSES_INLINE bool
449 can_clear_with(ARG_CH_T ch)
451 if (!back_color_erase && SP->_coloron) {
452 #if NCURSES_EXT_FUNCS
455 if (!SP->_default_color)
457 if (SP->_default_fg != C_MASK || SP->_default_bg != C_MASK)
459 if ((pair = GetPair(CHDEREF(ch))) != 0) {
461 pair_content(pair, &fg, &bg);
462 if (fg != C_MASK || bg != C_MASK)
466 if (AttrOfD(ch) & A_COLOR)
470 return (ISBLANK(CHDEREF(ch)) &&
471 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
475 * Issue a given span of characters from an array.
476 * Must be functionally equivalent to:
477 * for (i = 0; i < num; i++)
479 * but can leave the cursor positioned at the middle of the interval.
481 * Returns: 0 - cursor is at the end of interval
482 * 1 - cursor is somewhere in the middle
484 * This code is optimized using ech and rep.
487 EmitRange(const NCURSES_CH_T * ntext, int num)
491 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
493 if (erase_chars || repeat_char) {
498 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
499 PutChar(CHREF(ntext[0]));
505 PutChar(CHREF(ntext0));
510 while (runcount < num && CharEq(ntext[runcount], ntext0))
514 * The cost expression in the middle isn't exactly right.
515 * _cup_ch_cost is an upper bound on the cost for moving to the
516 * end of the erased area, but not the cost itself (which we
517 * can't compute without emitting the move). This may result
518 * in erase_chars not getting used in some situations for
519 * which it would be marginally advantageous.
522 && runcount > SP->_ech_cost + SP->_cup_ch_cost
523 && can_clear_with(CHREF(ntext0))) {
525 putp(TPARM_1(erase_chars, runcount));
528 * If this is the last part of the given interval,
529 * don't bother moving cursor, since it can be the
530 * last update on the line.
532 if (runcount < num) {
533 GoTo(SP->_cursrow, SP->_curscol + runcount);
535 return 1; /* cursor stays in the middle */
537 } else if (repeat_char && runcount > SP->_rep_cost) {
538 bool wrap_possible = (SP->_curscol + runcount >= screen_columns);
539 int rep_count = runcount;
545 tputs(TPARM_2(repeat_char, CharOf(ntext0), rep_count),
546 rep_count, _nc_outch);
547 SP->_curscol += rep_count;
550 PutChar(CHREF(ntext0));
552 for (i = 0; i < runcount; i++)
553 PutChar(CHREF(ntext[i]));
561 for (i = 0; i < num; i++)
562 PutChar(CHREF(ntext[i]));
567 * Output the line in the given range [first .. last]
569 * If there's a run of identical characters that's long enough to justify
570 * cursor movement, use that also.
572 * Returns: same as EmitRange
575 PutRange(const NCURSES_CH_T * otext,
576 const NCURSES_CH_T * ntext,
582 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %d, %d, %d)",
583 otext, ntext, row, first, last));
586 && (last - first + 1) > SP->_inline_cost) {
587 for (j = first, same = 0; j <= last; j++) {
588 if (!same && isWidecExt(otext[j]))
590 if (CharEq(otext[j], ntext[j])) {
593 if (same > SP->_inline_cost) {
594 EmitRange(ntext + first, j - same - first);
595 GoTo(row, first = j);
600 i = EmitRange(ntext + first, j - same - first);
602 * Always return 1 for the next GoTo() after a PutRange() if we found
603 * identical characters at end of interval
605 return (same == 0 ? i : 1);
607 return EmitRange(ntext + first, last - first + 1);
610 /* leave unbracketed here so 'indent' works */
611 #define MARK_NOCHANGE(win,row) \
612 win->_line[row].firstchar = _NOCHANGE; \
613 win->_line[row].lastchar = _NOCHANGE; \
614 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
622 struct tms before, after;
623 #endif /* USE_TRACE_TIMES */
625 T((T_CALLED("doupdate()")));
628 if (_nc_tracing & TRACE_UPDATE) {
630 _tracef("curscr is clear");
632 _tracedump("curscr", curscr);
633 _tracedump("newscr", newscr);
637 _nc_signal_handler(FALSE);
643 if (SP->_endwin || _nc_handle_sigwinch(FALSE)) {
645 * This is a transparent extension: XSI does not address it,
646 * and applications need not know that ncurses can do it.
648 * Check if the terminal size has changed while curses was off
649 * (this can happen in an xterm, for example), and resize the
650 * ncurses data structures accordingly.
652 _nc_update_screensize();
658 T(("coming back from shell mode"));
663 SP->_mouse_resume(SP);
668 /* zero the metering machinery */
670 (void) times(&before);
671 #endif /* USE_TRACE_TIMES */
674 * This is the support for magic-cookie terminals. The theory: we scan
675 * the virtual screen looking for attribute turnons. Where we find one,
676 * check to make sure it's realizable by seeing if the required number of
677 * un-attributed blanks are present before and after the attributed range;
678 * try to shift the range boundaries over blanks (not changing the screen
679 * display) so this becomes true. If it is, shift the beginning attribute
680 * change appropriately (the end one, if we've gotten this far, is
681 * guaranteed room for its cookie). If not, nuke the added attributes out
685 if (magic_cookie_glitch > 0) {
687 attr_t rattr = A_NORMAL;
689 for (i = 0; i < screen_lines; i++) {
690 for (j = 0; j < screen_columns; j++) {
692 NCURSES_CH_T *thisline = newscr->_line[i].text;
693 attr_t thisattr = AttrOf(thisline[j]) & SP->_xmc_triggers;
694 attr_t turnon = thisattr & ~rattr;
696 /* is an attribute turned on here? */
702 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
703 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
706 * If the attribute change location is a blank with a "safe"
707 * attribute, undo the attribute turnon. This may ensure
708 * there's enough room to set the attribute before the first
709 * non-blank in the run.
711 #define SAFE(a) (!((a) & SP->_xmc_triggers))
712 if (ISBLANK(thisline[j]) && SAFE(turnon)) {
713 RemAttr(thisline[j], turnon);
717 /* check that there's enough room at start of span */
718 for (k = 1; k <= magic_cookie_glitch; k++) {
720 || !ISBLANK(thisline[j - k])
721 || !SAFE(AttrOf(thisline[j - k]))) {
723 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
725 (ISBLANK(thisline[j - k])
728 (SAFE(AttrOf(thisline[j - k]))
735 bool end_onscreen = FALSE;
738 /* find end of span, if it's onscreen */
739 for (m = i; m < screen_lines; m++) {
740 for (; n < screen_columns; n++) {
741 attr_t testattr = AttrOf(newscr->_line[m].text[n]);
742 if ((testattr & SP->_xmc_triggers) == rattr) {
745 ("Range attributed with %s ends at (%d, %d)",
746 _traceattr(turnon), m, n));
753 ("Range attributed with %s ends offscreen",
754 _traceattr(turnon)));
758 NCURSES_CH_T *lastline = newscr->_line[m].text;
761 * If there are safely-attributed blanks at the end of
762 * the range, shorten the range. This will help ensure
763 * that there is enough room at end of span.
766 && ISBLANK(lastline[n])
767 && SAFE(AttrOf(lastline[n]))) {
768 RemAttr(lastline[n--], turnon);
771 /* check that there's enough room at end of span */
772 for (k = 1; k <= magic_cookie_glitch; k++) {
773 if (n + k >= screen_columns
774 || !ISBLANK(lastline[n + k])
775 || !SAFE(AttrOf(lastline[n + k]))) {
778 ("No room at end in %d,%d%s%s",
780 (ISBLANK(lastline[n + k])
783 (SAFE(AttrOf(lastline[n + k]))
796 ("Clearing %s beginning at (%d, %d)",
797 _traceattr(turnon), i, j));
799 /* turn off new attributes over span */
800 for (p = i; p < screen_lines; p++) {
801 for (; q < screen_columns; q++) {
802 attr_t testattr = AttrOf(newscr->_line[p].text[q]);
803 if ((testattr & SP->_xmc_triggers) == rattr)
805 RemAttr(newscr->_line[p].text[q], turnon);
812 ("Cookie space for %s found before (%d, %d)",
813 _traceattr(turnon), i, j));
816 * Back up the start of range so there's room for cookies
817 * before the first nonblank character.
819 for (k = 1; k <= magic_cookie_glitch; k++)
820 AddAttr(thisline[j - k], turnon);
828 /* show altered highlights after magic-cookie check */
829 if (_nc_tracing & TRACE_UPDATE) {
830 _tracef("After magic-cookie check...");
831 _tracedump("newscr", newscr);
835 #endif /* USE_XMC_SUPPORT */
838 if (curscr->_clear || newscr->_clear) { /* force refresh ? */
840 curscr->_clear = FALSE; /* reset flag */
841 newscr->_clear = FALSE; /* reset flag */
843 int changedlines = CHECK_INTERVAL;
848 nonempty = min(screen_lines, newscr->_maxy + 1);
850 if (SP->_scrolling) {
851 _nc_scroll_optimize();
854 nonempty = ClrBottom(nonempty);
856 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
857 for (i = 0; i < nonempty; i++) {
859 * Here is our line-breakout optimization.
861 if (changedlines == CHECK_INTERVAL) {
868 * newscr->line[i].firstchar is normally set
869 * by wnoutrefresh. curscr->line[i].firstchar
870 * is normally set by _nc_scroll_window in the
871 * vertical-movement optimization code,
873 if (newscr->_line[i].firstchar != _NOCHANGE
874 || curscr->_line[i].firstchar != _NOCHANGE) {
879 /* mark line changed successfully */
880 if (i <= newscr->_maxy) {
881 MARK_NOCHANGE(newscr, i);
883 if (i <= curscr->_maxy) {
884 MARK_NOCHANGE(curscr, i);
889 /* put everything back in sync */
890 for (i = nonempty; i <= newscr->_maxy; i++) {
891 MARK_NOCHANGE(newscr, i);
893 for (i = nonempty; i <= curscr->_maxy; i++) {
894 MARK_NOCHANGE(curscr, i);
897 if (!newscr->_leaveok) {
898 curscr->_curx = newscr->_curx;
899 curscr->_cury = newscr->_cury;
901 GoTo(curscr->_cury, curscr->_curx);
906 * We would like to keep the physical screen in normal mode in case we get
907 * other processes writing to the screen. This goal cannot be met for
908 * magic cookies since it interferes with attributes that may propagate
909 * past the current position.
912 if (magic_cookie_glitch != 0)
917 WINDOW_ATTRS(curscr) = WINDOW_ATTRS(newscr);
920 (void) times(&after);
922 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
924 (long) (after.tms_stime - before.tms_stime),
925 (long) (after.tms_utime - before.tms_utime)));
926 #endif /* USE_TRACE_TIMES */
928 _nc_signal_handler(TRUE);
936 * Returns the attributed character that corresponds to the "cleared"
937 * screen. If the terminal has the back-color-erase feature, this will be
938 * colored according to the wbkgd() call.
940 * We treat 'curscr' specially because it isn't supposed to be set directly
941 * in the wbkgd() call. Assume 'stdscr' for this case.
943 #define BCE_ATTRS (A_NORMAL|A_COLOR)
944 #define BCE_BKGD(win) (((win) == curscr ? stdscr : (win))->_nc_bkgd)
946 static NCURSES_INLINE NCURSES_CH_T
947 ClrBlank(WINDOW *win)
949 NCURSES_CH_T blank = blankchar;
950 if (back_color_erase)
951 AddAttr(blank, (AttrOf(BCE_BKGD(win)) & BCE_ATTRS));
958 ** Update by clearing and redrawing the entire screen.
966 NCURSES_CH_T blank = ClrBlank(stdscr);
967 int nonempty = min(screen_lines, newscr->_maxy + 1);
969 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
973 TR(TRACE_UPDATE, ("updating screen from scratch"));
975 nonempty = ClrBottom(nonempty);
977 for (i = 0; i < nonempty; i++)
980 TR(TRACE_UPDATE, (T_RETURN("")));
986 ** Clear to end of current line, starting at the cursor position
990 ClrToEOL(NCURSES_CH_T blank, bool needclear)
995 && SP->_cursrow >= 0) {
996 for (j = SP->_curscol; j < screen_columns; j++) {
998 NCURSES_CH_T *cp = &(curscr->_line[SP->_cursrow].text[j]);
1000 if (!CharEq(*cp, blank)) {
1012 TPUTS_TRACE("clr_eol");
1013 if (clr_eol && SP->_el_cost <= (screen_columns - SP->_curscol)) {
1016 int count = (screen_columns - SP->_curscol);
1018 PutChar(CHREF(blank));
1026 ** Clear to end of screen, starting at the cursor position
1030 ClrToEOS(NCURSES_CH_T blank)
1038 TPUTS_TRACE("clr_eos");
1039 tputs(clr_eos, screen_lines - row, _nc_outch);
1041 while (col < screen_columns)
1042 curscr->_line[row].text[col++] = blank;
1044 for (row++; row < screen_lines; row++) {
1045 for (col = 0; col < screen_columns; col++)
1046 curscr->_line[row].text[col] = blank;
1053 * Test if clearing the end of the screen would satisfy part of the
1054 * screen-update. Do this by scanning backwards through the lines in the
1055 * screen, checking if each is blank, and one or more are changed.
1058 ClrBottom(int total)
1063 int last = min(screen_columns, newscr->_maxx + 1);
1064 NCURSES_CH_T blank = newscr->_line[total - 1].text[last - 1];
1067 if (clr_eos && can_clear_with(CHREF(blank))) {
1069 for (row = total - 1; row >= 0; row--) {
1070 for (col = 0, ok = TRUE; ok && col < last; col++) {
1071 ok = (CharEq(newscr->_line[row].text[col], blank));
1076 for (col = 0; ok && col < last; col++) {
1077 ok = (CharEq(curscr->_line[row].text[col], blank));
1083 /* don't use clr_eos for just one line if clr_eol available */
1087 if (SP->oldhash && SP->newhash) {
1088 for (row = top; row < screen_lines; row++)
1089 SP->oldhash[row] = SP->newhash[row];
1097 #if USE_WIDEC_SUPPORT
1098 #define check_xmc_transition(a, b) \
1099 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & SP->_xmc_triggers) != 0)
1100 #define xmc_turn_on(a,b) check_xmc_transition(&(a), &(b))
1102 #define xmc_turn_on(a,b) ((((a)^(b)) & ~(a) & SP->_xmc_triggers) != 0)
1105 #define xmc_new(r,c) newscr->_line[r].text[c]
1106 #define xmc_turn_off(a,b) xmc_turn_on(b,a)
1107 #endif /* USE_XMC_SUPPORT */
1110 ** TransformLine(lineno)
1112 ** Transform the given line in curscr to the one in newscr, using
1113 ** Insert/Delete Character if _nc_idcok && has_ic().
1115 ** firstChar = position of first different character in line
1116 ** oLastChar = position of last different character in old line
1117 ** nLastChar = position of last different character in new line
1119 ** move to firstChar
1120 ** overwrite chars up to min(oLastChar, nLastChar)
1121 ** if oLastChar < nLastChar
1122 ** insert newLine[oLastChar+1..nLastChar]
1124 ** delete oLastChar - nLastChar spaces
1128 TransformLine(int const lineno)
1130 int firstChar, oLastChar, nLastChar;
1131 NCURSES_CH_T *newLine = newscr->_line[lineno].text;
1132 NCURSES_CH_T *oldLine = curscr->_line[lineno].text;
1134 bool attrchanged = FALSE;
1136 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%d)"), lineno));
1138 /* copy new hash value to old one */
1139 if (SP->oldhash && SP->newhash)
1140 SP->oldhash[lineno] = SP->newhash[lineno];
1143 * If we have colors, there is the possibility of having two color pairs
1144 * that display as the same colors. For instance, Lynx does this. Check
1145 * for this case, and update the old line with the new line's colors when
1146 * they are equivalent.
1152 for (n = 0; n < screen_columns; n++) {
1153 if (!CharEq(newLine[n], oldLine[n])) {
1154 oldPair = GetPair(oldLine[n]);
1155 newPair = GetPair(newLine[n]);
1156 if (oldPair != newPair
1157 && unColor(oldLine[n]) == unColor(newLine[n])) {
1158 if (oldPair < COLOR_PAIRS
1159 && newPair < COLOR_PAIRS
1160 && SP->_color_pairs[oldPair] == SP->_color_pairs[newPair]) {
1161 SetPair(oldLine[n], GetPair(newLine[n]));
1168 if (ceol_standout_glitch && clr_eol) {
1170 while (firstChar < screen_columns) {
1171 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1181 if (attrchanged) { /* we may have to disregard the whole line */
1182 GoTo(lineno, firstChar);
1183 ClrToEOL(ClrBlank(curscr), FALSE);
1184 PutRange(oldLine, newLine, lineno, 0, (screen_columns - 1));
1188 * This is a very simple loop to paint characters which may have the
1189 * magic cookie glitch embedded. It doesn't know much about video
1190 * attributes which are continued from one line to the next. It
1191 * assumes that we have filtered out requests for attribute changes
1192 * that do not get mapped to blank positions.
1194 * FIXME: we are not keeping track of where we put the cookies, so this
1195 * will work properly only once, since we may overwrite a cookie in a
1196 * following operation.
1198 } else if (magic_cookie_glitch > 0) {
1199 GoTo(lineno, firstChar);
1200 for (n = 0; n < screen_columns; n++) {
1201 int m = n + magic_cookie_glitch;
1203 /* check for turn-on:
1204 * If we are writing an attributed blank, where the
1205 * previous cell is not attributed.
1207 if (ISBLANK(newLine[n])
1209 && xmc_turn_on(newLine[n - 1], newLine[n]))
1212 && xmc_turn_on(xmc_new(lineno - 1, screen_columns - 1),
1217 PutChar(CHREF(newLine[n]));
1219 /* check for turn-off:
1220 * If we are writing an attributed non-blank, where the
1221 * next cell is blank, and not attributed.
1223 if (!ISBLANK(newLine[n])
1224 && ((n + 1 < screen_columns
1225 && xmc_turn_off(newLine[n], newLine[n + 1]))
1226 || (n + 1 >= screen_columns
1227 && lineno + 1 < screen_lines
1228 && xmc_turn_off(newLine[n], xmc_new(lineno + 1, 0))))) {
1237 /* it may be cheap to clear leading whitespace with clr_bol */
1239 if (clr_bol && can_clear_with(CHREF(blank))) {
1240 int oFirstChar, nFirstChar;
1242 for (oFirstChar = 0; oFirstChar < screen_columns; oFirstChar++)
1243 if (!CharEq(oldLine[oFirstChar], blank))
1245 for (nFirstChar = 0; nFirstChar < screen_columns; nFirstChar++)
1246 if (!CharEq(newLine[nFirstChar], blank))
1249 if (nFirstChar == oFirstChar) {
1250 firstChar = nFirstChar;
1251 /* find the first differing character */
1252 while (firstChar < screen_columns
1253 && CharEq(newLine[firstChar], oldLine[firstChar]))
1255 } else if (oFirstChar > nFirstChar) {
1256 firstChar = nFirstChar;
1257 } else { /* oFirstChar < nFirstChar */
1258 firstChar = oFirstChar;
1259 if (SP->_el1_cost < nFirstChar - oFirstChar) {
1260 if (nFirstChar >= screen_columns
1261 && SP->_el_cost <= SP->_el1_cost) {
1264 TPUTS_TRACE("clr_eol");
1267 GoTo(lineno, nFirstChar - 1);
1269 TPUTS_TRACE("clr_bol");
1273 while (firstChar < nFirstChar)
1274 oldLine[firstChar++] = blank;
1278 /* find the first differing character */
1279 while (firstChar < screen_columns
1280 && CharEq(newLine[firstChar], oldLine[firstChar]))
1283 /* if there wasn't one, we're done */
1284 if (firstChar >= screen_columns) {
1285 TR(TRACE_UPDATE, (T_RETURN("")));
1289 blank = newLine[screen_columns - 1];
1291 if (!can_clear_with(CHREF(blank))) {
1292 /* find the last differing character */
1293 nLastChar = screen_columns - 1;
1295 while (nLastChar > firstChar
1296 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1299 if (nLastChar >= firstChar) {
1300 GoTo(lineno, firstChar);
1301 PutRange(oldLine, newLine, lineno, firstChar, nLastChar);
1302 memcpy(oldLine + firstChar,
1303 newLine + firstChar,
1304 (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1306 TR(TRACE_UPDATE, (T_RETURN("")));
1310 /* find last non-blank character on old line */
1311 oLastChar = screen_columns - 1;
1312 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1315 /* find last non-blank character on new line */
1316 nLastChar = screen_columns - 1;
1317 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1320 if ((nLastChar == firstChar)
1321 && (SP->_el_cost < (oLastChar - nLastChar))) {
1322 GoTo(lineno, firstChar);
1323 if (!CharEq(newLine[firstChar], blank))
1324 PutChar(CHREF(newLine[firstChar]));
1325 ClrToEOL(blank, FALSE);
1326 } else if ((nLastChar != oLastChar)
1327 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1328 || !(_nc_idcok && has_ic()))) {
1329 GoTo(lineno, firstChar);
1330 if ((oLastChar - nLastChar) > SP->_el_cost) {
1331 if (PutRange(oldLine, newLine, lineno, firstChar, nLastChar))
1332 GoTo(lineno, nLastChar + 1);
1333 ClrToEOL(blank, FALSE);
1335 n = max(nLastChar, oLastChar);
1336 PutRange(oldLine, newLine, lineno, firstChar, n);
1339 int nLastNonblank = nLastChar;
1340 int oLastNonblank = oLastChar;
1342 /* find the last characters that really differ */
1343 /* can be -1 if no characters differ */
1344 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1345 /* don't split a wide char */
1346 if (isWidecExt(newLine[nLastChar]) &&
1347 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1351 if (nLastChar == -1 || oLastChar == -1)
1355 n = min(oLastChar, nLastChar);
1356 if (n >= firstChar) {
1357 GoTo(lineno, firstChar);
1358 PutRange(oldLine, newLine, lineno, firstChar, n);
1361 if (oLastChar < nLastChar) {
1362 int m = max(nLastNonblank, oLastNonblank);
1363 #if USE_WIDEC_SUPPORT
1364 while (isWidecExt(newLine[n + 1]) && n) {
1369 GoTo(lineno, n + 1);
1370 if ((nLastChar < nLastNonblank)
1371 || InsCharCost(nLastChar - oLastChar) > (m - n)) {
1372 PutRange(oldLine, newLine, lineno, n + 1, m);
1374 InsStr(&newLine[n + 1], nLastChar - oLastChar);
1376 } else if (oLastChar > nLastChar) {
1377 GoTo(lineno, n + 1);
1378 if (DelCharCost(oLastChar - nLastChar)
1379 > SP->_el_cost + nLastNonblank - (n + 1)) {
1380 if (PutRange(oldLine, newLine, lineno,
1381 n + 1, nLastNonblank))
1382 GoTo(lineno, nLastNonblank + 1);
1383 ClrToEOL(blank, FALSE);
1386 * The delete-char sequence will
1387 * effectively shift in blanks from the
1388 * right margin of the screen. Ensure
1389 * that they are the right color by
1390 * setting the video attributes from
1391 * the last character on the row.
1394 DelChar(oLastChar - nLastChar);
1400 /* update the code's internal representation */
1401 if (screen_columns > firstChar)
1402 memcpy(oldLine + firstChar,
1403 newLine + firstChar,
1404 (screen_columns - firstChar) * sizeof(NCURSES_CH_T));
1405 TR(TRACE_UPDATE, (T_RETURN("")));
1410 ** ClearScreen(blank)
1412 ** Clear the physical screen and put cursor at home
1417 ClearScreen(NCURSES_CH_T blank)
1420 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1422 TR(TRACE_UPDATE, ("ClearScreen() called"));
1424 #if NCURSES_EXT_FUNCS
1426 && !SP->_default_color) {
1427 _nc_do_color(GET_SCREEN_PAIR(SP), 0, FALSE, _nc_outch);
1428 if (!back_color_erase) {
1437 TPUTS_TRACE("clear_screen");
1439 SP->_cursrow = SP->_curscol = 0;
1440 position_check(SP->_cursrow, SP->_curscol, "ClearScreen");
1441 } else if (clr_eos) {
1442 SP->_cursrow = SP->_curscol = -1;
1446 TPUTS_TRACE("clr_eos");
1447 tputs(clr_eos, screen_lines, _nc_outch);
1448 } else if (clr_eol) {
1449 SP->_cursrow = SP->_curscol = -1;
1452 for (i = 0; i < screen_lines; i++) {
1454 TPUTS_TRACE("clr_eol");
1461 for (i = 0; i < screen_lines; i++) {
1463 for (j = 0; j < screen_columns; j++)
1464 PutChar(CHREF(blank));
1469 for (i = 0; i < screen_lines; i++) {
1470 for (j = 0; j < screen_columns; j++)
1471 curscr->_line[i].text[j] = blank;
1474 TR(TRACE_UPDATE, ("screen cleared"));
1478 ** InsStr(line, count)
1480 ** Insert the count characters pointed to by line.
1485 InsStr(NCURSES_CH_T * line, int count)
1487 TR(TRACE_UPDATE, ("InsStr(%p,%d) called", line, count));
1489 /* Prefer parm_ich as it has the smallest cost - no need to shift
1490 * the whole line on each character. */
1491 /* The order must match that of InsCharCost. */
1493 TPUTS_TRACE("parm_ich");
1494 tputs(TPARM_1(parm_ich, count), count, _nc_outch);
1496 PutAttrChar(CHREF(*line));
1500 } else if (enter_insert_mode && exit_insert_mode) {
1501 TPUTS_TRACE("enter_insert_mode");
1502 putp(enter_insert_mode);
1504 PutAttrChar(CHREF(*line));
1505 if (insert_padding) {
1506 TPUTS_TRACE("insert_padding");
1507 putp(insert_padding);
1512 TPUTS_TRACE("exit_insert_mode");
1513 putp(exit_insert_mode);
1516 TPUTS_TRACE("insert_character");
1517 putp(insert_character);
1518 PutAttrChar(CHREF(*line));
1519 if (insert_padding) {
1520 TPUTS_TRACE("insert_padding");
1521 putp(insert_padding);
1527 position_check(SP->_cursrow, SP->_curscol, "InsStr");
1533 ** Delete count characters at current position
1542 TR(TRACE_UPDATE, ("DelChar(%d) called, position = (%ld,%ld)",
1544 (long) newscr->_cury,
1545 (long) newscr->_curx));
1548 TPUTS_TRACE("parm_dch");
1549 tputs(TPARM_1(parm_dch, count), count, _nc_outch);
1551 for (n = 0; n < count; n++) {
1552 TPUTS_TRACE("delete_character");
1553 putp(delete_character);
1559 * Physical-scrolling support
1561 * This code was adapted from Keith Bostic's hardware scrolling
1562 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1563 * capabilities, narrowed the call interface slightly, and cleaned
1564 * up some convoluted tests. I also added support for the memory_above
1565 * memory_below, and non_dest_scroll_region capabilities.
1567 * For this code to work, we must have either
1568 * change_scroll_region and scroll forward/reverse commands, or
1569 * insert and delete line capabilities.
1570 * When the scrolling region has been set, the cursor has to
1571 * be at the last line of the region to make the scroll up
1572 * happen, or on the first line of region to scroll down.
1574 * This code makes one aesthetic decision in the opposite way from
1575 * BSD curses. BSD curses preferred pairs of il/dl operations
1576 * over scrolls, allegedly because il/dl looked faster. We, on
1577 * the other hand, prefer scrolls because (a) they're just as fast
1578 * on many terminals and (b) using them avoids bouncing an
1579 * unchanged bottom section of the screen up and down, which is
1582 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1584 * I used assumption that capabilities il/il1/dl/dl1 work inside
1585 * changed scroll region not shifting screen contents outside of it.
1586 * If there are any terminals behaving different way, it would be
1587 * necessary to add some conditions to scroll_csr_forward/backward.
1590 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1592 scroll_csr_forward(int n, int top, int bot, int miny, int maxy, NCURSES_CH_T blank)
1596 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1599 TPUTS_TRACE("scroll_forward");
1600 putp(scroll_forward);
1601 } else if (n == 1 && delete_line && bot == maxy) {
1604 TPUTS_TRACE("delete_line");
1606 } else if (parm_index && top == miny && bot == maxy) {
1609 TPUTS_TRACE("parm_index");
1610 tputs(TPARM_2(parm_index, n, 0), n, _nc_outch);
1611 } else if (parm_delete_line && bot == maxy) {
1614 TPUTS_TRACE("parm_delete_line");
1615 tputs(TPARM_2(parm_delete_line, n, 0), n, _nc_outch);
1616 } else if (scroll_forward && top == miny && bot == maxy) {
1619 for (i = 0; i < n; i++) {
1620 TPUTS_TRACE("scroll_forward");
1621 putp(scroll_forward);
1623 } else if (delete_line && bot == maxy) {
1626 for (i = 0; i < n; i++) {
1627 TPUTS_TRACE("delete_line");
1633 #if NCURSES_EXT_FUNCS
1636 for (i = 0; i < n; i++) {
1638 for (j = 0; j < screen_columns; j++)
1639 PutChar(CHREF(blank));
1646 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1649 scroll_csr_backward(int n, int top, int bot, int miny, int maxy,
1654 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1657 TPUTS_TRACE("scroll_reverse");
1658 putp(scroll_reverse);
1659 } else if (n == 1 && insert_line && bot == maxy) {
1662 TPUTS_TRACE("insert_line");
1664 } else if (parm_rindex && top == miny && bot == maxy) {
1667 TPUTS_TRACE("parm_rindex");
1668 tputs(TPARM_2(parm_rindex, n, 0), n, _nc_outch);
1669 } else if (parm_insert_line && bot == maxy) {
1672 TPUTS_TRACE("parm_insert_line");
1673 tputs(TPARM_2(parm_insert_line, n, 0), n, _nc_outch);
1674 } else if (scroll_reverse && top == miny && bot == maxy) {
1677 for (i = 0; i < n; i++) {
1678 TPUTS_TRACE("scroll_reverse");
1679 putp(scroll_reverse);
1681 } else if (insert_line && bot == maxy) {
1684 for (i = 0; i < n; i++) {
1685 TPUTS_TRACE("insert_line");
1691 #if NCURSES_EXT_FUNCS
1694 for (i = 0; i < n; i++) {
1696 for (j = 0; j < screen_columns; j++)
1697 PutChar(CHREF(blank));
1704 /* scroll by using delete_line at del and insert_line at ins */
1707 scroll_idl(int n, int del, int ins, NCURSES_CH_T blank)
1711 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1716 if (n == 1 && delete_line) {
1717 TPUTS_TRACE("delete_line");
1719 } else if (parm_delete_line) {
1720 TPUTS_TRACE("parm_delete_line");
1721 tputs(TPARM_2(parm_delete_line, n, 0), n, _nc_outch);
1722 } else { /* if (delete_line) */
1723 for (i = 0; i < n; i++) {
1724 TPUTS_TRACE("delete_line");
1731 if (n == 1 && insert_line) {
1732 TPUTS_TRACE("insert_line");
1734 } else if (parm_insert_line) {
1735 TPUTS_TRACE("parm_insert_line");
1736 tputs(TPARM_2(parm_insert_line, n, 0), n, _nc_outch);
1737 } else { /* if (insert_line) */
1738 for (i = 0; i < n; i++) {
1739 TPUTS_TRACE("insert_line");
1748 * Note: some terminals require the cursor to be within the scrolling margins
1749 * before setting them. Generally, the cursor must be at the appropriate end
1750 * of the scrolling margins when issuing an indexing operation (it is not
1751 * apparent whether it must also be at the left margin; we do this just to be
1752 * safe). To make the related cursor movement a little faster, we use the
1753 * save/restore cursor capabilities if the terminal has them.
1756 _nc_scrolln(int n, int top, int bot, int maxy)
1757 /* scroll region from top to bot by n lines */
1759 NCURSES_CH_T blank = ClrBlank(stdscr);
1761 bool cursor_saved = FALSE;
1764 TR(TRACE_MOVE, ("mvcur_scrolln(%d, %d, %d, %d)", n, top, bot, maxy));
1768 * If we scroll, we might remove a cookie.
1770 if (magic_cookie_glitch > 0) {
1775 if (n > 0) { /* scroll up (forward) */
1777 * Explicitly clear if stuff pushed off top of region might
1778 * be saved by the terminal.
1780 res = scroll_csr_forward(n, top, bot, 0, maxy, blank);
1782 if (res == ERR && change_scroll_region) {
1783 if ((((n == 1 && scroll_forward) || parm_index)
1784 && (SP->_cursrow == bot || SP->_cursrow == bot - 1))
1785 && save_cursor && restore_cursor) {
1786 cursor_saved = TRUE;
1787 TPUTS_TRACE("save_cursor");
1790 TPUTS_TRACE("change_scroll_region");
1791 putp(TPARM_2(change_scroll_region, top, bot));
1793 TPUTS_TRACE("restore_cursor");
1794 putp(restore_cursor);
1796 SP->_cursrow = SP->_curscol = -1;
1799 res = scroll_csr_forward(n, top, bot, top, bot, blank);
1801 TPUTS_TRACE("change_scroll_region");
1802 putp(TPARM_2(change_scroll_region, 0, maxy));
1803 SP->_cursrow = SP->_curscol = -1;
1806 if (res == ERR && _nc_idlok)
1807 res = scroll_idl(n, top, bot - n + 1, blank);
1810 * Clear the newly shifted-in text.
1813 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
1814 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1815 if (bot == maxy && clr_eos) {
1816 GoTo(bot - n + 1, 0);
1819 for (i = 0; i < n; i++) {
1821 ClrToEOL(blank2, FALSE);
1826 } else { /* (n < 0) - scroll down (backward) */
1827 res = scroll_csr_backward(-n, top, bot, 0, maxy, blank);
1829 if (res == ERR && change_scroll_region) {
1830 if (top != 0 && (SP->_cursrow == top || SP->_cursrow == top - 1)
1831 && save_cursor && restore_cursor) {
1832 cursor_saved = TRUE;
1833 TPUTS_TRACE("save_cursor");
1836 TPUTS_TRACE("change_scroll_region");
1837 putp(TPARM_2(change_scroll_region, top, bot));
1839 TPUTS_TRACE("restore_cursor");
1840 putp(restore_cursor);
1842 SP->_cursrow = SP->_curscol = -1;
1845 res = scroll_csr_backward(-n, top, bot, top, bot, blank);
1847 TPUTS_TRACE("change_scroll_region");
1848 putp(TPARM_2(change_scroll_region, 0, maxy));
1849 SP->_cursrow = SP->_curscol = -1;
1852 if (res == ERR && _nc_idlok)
1853 res = scroll_idl(-n, bot + n + 1, top, blank);
1856 * Clear the newly shifted-in text.
1859 && (non_dest_scroll_region || (memory_above && top == 0))) {
1860 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1861 for (i = 0; i < -n; i++) {
1863 ClrToEOL(blank2, FALSE);
1871 _nc_scroll_window(curscr, n, top, bot, blank);
1873 /* shift hash values too - they can be reused */
1874 _nc_scroll_oldhash(n, top, bot);
1879 NCURSES_EXPORT(void)
1880 _nc_screen_resume(void)
1882 /* make sure terminal is in a sane known state */
1883 SetAttr(SCREEN_ATTRS(SP), A_NORMAL);
1884 newscr->_clear = TRUE;
1886 /* reset color pairs and definitions */
1887 if (SP->_coloron || SP->_color_defs)
1890 /* restore user-defined colors, if any */
1891 if (SP->_color_defs < 0) {
1893 SP->_color_defs = -(SP->_color_defs);
1894 for (n = 0; n < SP->_color_defs; ++n) {
1895 if (SP->_color_table[n].init) {
1897 SP->_color_table[n].r,
1898 SP->_color_table[n].g,
1899 SP->_color_table[n].b);
1904 if (exit_attribute_mode)
1905 putp(exit_attribute_mode);
1907 /* turn off attributes */
1908 if (exit_alt_charset_mode)
1909 putp(exit_alt_charset_mode);
1910 if (exit_standout_mode)
1911 putp(exit_standout_mode);
1912 if (exit_underline_mode)
1913 putp(exit_underline_mode);
1915 if (exit_insert_mode)
1916 putp(exit_insert_mode);
1917 if (enter_am_mode && exit_am_mode)
1918 putp(auto_right_margin ? enter_am_mode : exit_am_mode);
1921 NCURSES_EXPORT(void)
1922 _nc_screen_init(void)
1924 _nc_screen_resume();
1927 /* wrap up screen handling */
1928 NCURSES_EXPORT(void)
1929 _nc_screen_wrap(void)
1931 UpdateAttrs(normal);
1932 #if NCURSES_EXT_FUNCS
1934 && !SP->_default_color) {
1935 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
1936 SP->_default_color = TRUE;
1937 _nc_do_color(-1, 0, FALSE, _nc_outch);
1938 SP->_default_color = FALSE;
1940 mvcur(SP->_cursrow, SP->_curscol, screen_lines - 1, 0);
1942 ClrToEOL(blank, TRUE);
1945 if (SP->_color_defs) {
1951 NCURSES_EXPORT(void)
1952 _nc_do_xmc_glitch(attr_t previous)
1954 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP)));
1958 SP->_curscol += magic_cookie_glitch;
1959 if (SP->_curscol >= SP->_columns)
1961 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie", SP->_cursrow, SP->_curscol));
1966 #endif /* USE_XMC_SUPPORT */