1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
36 /*-----------------------------------------------------------------
40 * The routine doupdate() and its dependents.
41 * All physical output is concentrated here (except _nc_outch()
44 *-----------------------------------------------------------------*/
46 #include <curses.priv.h>
48 #if defined __HAIKU__ && defined __BEOS__
58 #if defined(TRACE) && HAVE_SYS_TIMES_H && HAVE_TIMES
59 #define USE_TRACE_TIMES 1
61 #define USE_TRACE_TIMES 0
64 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
69 #include <sys/times.h>
75 #include <sys/select.h>
82 MODULE_ID("$Id: tty_update.c,v 1.250 2009/04/18 23:32:17 tom Exp $")
85 * This define controls the line-breakout optimization. Every once in a
86 * while during screen refresh, we want to check for input and abort the
87 * update if there's some waiting. CHECK_INTERVAL controls the number of
88 * changed lines to be emitted between input checks.
90 * Note: Input-check-and-abort is no longer done if the screen is being
91 * updated from scratch. This is a feature, not a bug.
93 #define CHECK_INTERVAL 5
95 #define FILL_BCE() (SP->_coloron && !SP->_default_color && !back_color_erase)
97 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
98 static NCURSES_CH_T normal = NewChar(BLANK_TEXT);
101 * Enable checking to see if doupdate and friends are tracking the true
102 * cursor position correctly. NOTE: this is a debugging hack which will
103 * work ONLY on ANSI-compatible terminals!
105 /* #define POSITION_DEBUG */
107 static NCURSES_INLINE NCURSES_CH_T ClrBlank(WINDOW *win);
108 static void DelChar(int count);
109 static void InsStr(NCURSES_CH_T * line, int count);
112 static void ClearScreen(SCREEN *, NCURSES_CH_T blank);
113 static int ClrBottom(SCREEN *, int total);
114 static void ClrUpdate(SCREEN *);
115 static void TransformLine(SCREEN *, int const lineno);
117 static void ClearScreen(NCURSES_CH_T blank);
118 static int ClrBottom(int total);
119 static void ClrUpdate(void);
120 static void TransformLine(int const lineno);
123 #ifdef POSITION_DEBUG
124 /****************************************************************************
126 * Debugging code. Only works on ANSI-standard terminals.
128 ****************************************************************************/
131 position_check(int expected_y, int expected_x, char *legend)
132 /* check to see if the real cursor position matches the virtual */
138 if (!_nc_tracing || (expected_y < 0 && expected_x < 0))
142 memset(buf, '\0', sizeof(buf));
143 putp("\033[6n"); /* only works on ANSI-compatibles */
147 int ask = sizeof(buf) - 1 - (s - buf);
148 int got = read(0, s, ask);
152 } while (strchr(buf, 'R') == 0);
153 _tracef("probe returned %s", _nc_visbuf(buf));
155 /* try to interpret as a position report */
156 if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
157 _tracef("position probe failed in %s", legend);
163 if (y - 1 != expected_y || x - 1 != expected_x) {
165 tputs(tparm("\033[%d;%dH", expected_y + 1, expected_x + 1), 1, _nc_outch);
166 _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
167 y - 1, x - 1, expected_y, expected_x, legend);
169 _tracef("position matches OK in %s", legend);
174 #define position_check(expected_y, expected_x, legend) /* nothing */
175 #endif /* POSITION_DEBUG */
177 /****************************************************************************
179 * Optimized update code
181 ****************************************************************************/
183 static NCURSES_INLINE void
184 GoTo(int const row, int const col)
186 TR(TRACE_MOVE, ("GoTo(%d, %d) from (%d, %d)",
187 row, col, SP->_cursrow, SP->_curscol));
189 position_check(SP->_cursrow, SP->_curscol, "GoTo");
191 mvcur(SP->_cursrow, SP->_curscol, row, col);
192 position_check(SP->_cursrow, SP->_curscol, "GoTo2");
195 static NCURSES_INLINE void
196 PutAttrChar(CARG_CH_T ch)
202 NCURSES_CH_T attr = CHDEREF(ch);
204 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
206 SP->_cursrow, SP->_curscol));
207 #if USE_WIDEC_SUPPORT
209 * If this is not a valid character, there is nothing more to do.
211 if (isWidecExt(CHDEREF(ch))) {
212 TR(TRACE_CHARPUT, ("...skip"));
216 * Determine the number of character cells which the 'ch' value will use
217 * on the screen. It should be at least one.
219 if ((chlen = wcwidth(CharOf(CHDEREF(ch)))) <= 0) {
220 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
223 * If the character falls into any of these special cases, do
224 * not force the result to a blank:
226 * a) it is printable (this works around a bug in wcwidth()).
227 * b) use_legacy_coding() has been called to modify the treatment
229 * c) the acs_map[] has been initialized to allow codes 0-31
230 * to be rendered. This supports Linux console's "PC"
231 * characters. Codes 128-255 are allowed though this is
234 if (is8bits(CharOf(CHDEREF(ch)))
235 && (isprint(CharOf(CHDEREF(ch)))
236 || (SP->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
237 || (SP->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
238 || (AttrOf(attr) & A_ALTCHARSET
239 && ((CharOfD(ch) < ACS_LEN
241 && SP->_acs_map[CharOfD(ch)] != 0)
242 || (CharOfD(ch) >= 128))))) {
246 TR(TRACE_CHARPUT, ("forced to blank"));
252 if ((AttrOf(attr) & A_ALTCHARSET)
254 && CharOfD(ch) < ACS_LEN) {
255 my_ch = CHDEREF(ch); /* work around const param */
256 #if USE_WIDEC_SUPPORT
258 * This is crude & ugly, but works most of the time. It checks if the
259 * acs_chars string specified that we have a mapping for this
260 * character, and uses the wide-character mapping when we expect the
261 * normal one to be broken (by mis-design ;-).
263 if (SP->_screen_acs_fix
264 && SP->_screen_acs_map[CharOf(my_ch)]) {
265 RemAttr(attr, A_ALTCHARSET);
266 my_ch = _nc_wacs[CharOf(my_ch)];
270 * If we (still) have alternate character set, it is the normal 8bit
271 * flavor. The _screen_acs_map[] array tells if the character was
272 * really in acs_chars, needed because of the way wide/normal line
273 * drawing flavors are integrated.
275 if (AttrOf(attr) & A_ALTCHARSET) {
277 chtype temp = UChar(SP->_acs_map[j]);
279 if (!(SP->_screen_acs_map[j])) {
280 RemAttr(attr, A_ALTCHARSET);
285 SetChar(my_ch, temp, AttrOf(attr));
289 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
290 SetChar(tilde, L('`'), AttrOf(attr));
295 #if !USE_WIDEC_SUPPORT
296 /* FIXME - we do this special case for signal handling, should see how to
297 * make it work for wide characters.
299 if (SP->_outch != 0) {
300 SP->_outch(UChar(ch));
304 PUTC(CHDEREF(ch), SP->_ofp); /* macro's fastest... */
307 SP->_curscol += chlen;
309 TPUTS_TRACE("char_padding");
316 /* check for pending input */
318 bool have_pending = FALSE;
321 * Only carry out this check when the flag is zero, otherwise we'll
322 * have the refreshing slow down drastically (or stop) if there's an
323 * unread character available.
325 if (SP->_fifohold != 0)
328 if (SP->_checkfd >= 0) {
330 struct pollfd fds[1];
331 fds[0].fd = SP->_checkfd;
332 fds[0].events = POLLIN;
333 if (poll(fds, 1, 0) > 0) {
336 #elif defined(__BEOS__)
338 * BeOS's select() is declared in socket.h, so the configure script does
339 * not see it. That's just as well, since that function works only for
340 * sockets. This (using snooze and ioctl) was distilled from Be's patch
341 * for ncurses which uses a separate thread to simulate select().
343 * FIXME: the return values from the ioctl aren't very clear if we get
347 int howmany = ioctl(0, 'ichr', &n);
348 if (howmany >= 0 && n > 0) {
353 struct timeval ktimeout;
356 ktimeout.tv_usec = 0;
359 FD_SET(SP->_checkfd, &fdset);
360 if (select(SP->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
372 /* put char at lower right corner */
374 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
376 if (!auto_right_margin) {
377 /* we can put the char directly */
379 } else if (enter_am_mode && exit_am_mode) {
380 /* we can suppress automargin */
381 TPUTS_TRACE("exit_am_mode");
386 position_check(SP_PARM->_cursrow, SP_PARM->_curscol, "exit_am_mode");
388 TPUTS_TRACE("enter_am_mode");
390 } else if ((enter_insert_mode && exit_insert_mode)
391 || insert_character || parm_ich) {
392 GoTo(screen_lines(SP_PARM) - 1, screen_columns(SP_PARM) - 2);
394 GoTo(screen_lines(SP_PARM) - 1, screen_columns(SP_PARM) - 2);
395 InsStr(newscr->_line[screen_lines(SP_PARM) - 1].text
396 + screen_columns(SP_PARM) - 2,
402 * Wrap the cursor position, i.e., advance to the beginning of the next line.
407 if (eat_newline_glitch) {
409 * xenl can manifest two different ways. The vt100 way is that, when
410 * you'd expect the cursor to wrap, it stays hung at the right margin
411 * (on top of the character just emitted) and doesn't wrap until the
412 * *next* graphic char is emitted. The c100 way is to ignore LF
413 * received just after an am wrap.
415 * An aggressive way to handle this would be to emit CR/LF after the
416 * char and then assume the wrap is done, you're on the first position
417 * of the next line, and the terminal out of its weird state. Here
418 * it's safe to just tell the code that the cursor is in hyperspace and
419 * let the next mvcur() call straighten things out.
423 } else if (auto_right_margin) {
427 * We've actually moved - but may have to work around problems with
428 * video attributes not working.
430 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP))) {
431 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
432 (unsigned long) AttrOf(SCREEN_ATTRS(SP)),
433 _traceattr(AttrOf(SCREEN_ATTRS(SP)))));
434 (void) VIDATTR(A_NORMAL, 0);
439 position_check(SP->_cursrow, SP->_curscol, "wrap_cursor");
442 static NCURSES_INLINE void
443 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
444 /* insert character, handling automargin stuff */
446 if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1
447 && SP_PARM->_curscol == screen_columns(SP_PARM) - 1)
448 PutCharLR(NCURSES_SP_ARGx ch);
452 if (SP_PARM->_curscol >= screen_columns(SP_PARM))
455 position_check(SP_PARM->_cursrow, SP_PARM->_curscol, "PutChar");
459 * Check whether the given character can be output by clearing commands. This
460 * includes test for being a space and not including any 'bad' attributes, such
461 * as A_REVERSE. All attribute flags which don't affect appearance of a space
462 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
464 static NCURSES_INLINE bool
465 can_clear_with(ARG_CH_T ch)
467 if (!back_color_erase && SP->_coloron) {
468 #if NCURSES_EXT_FUNCS
471 if (!SP->_default_color)
473 if (SP->_default_fg != C_MASK || SP->_default_bg != C_MASK)
475 if ((pair = GetPair(CHDEREF(ch))) != 0) {
477 pair_content(pair, &fg, &bg);
478 if (fg != C_MASK || bg != C_MASK)
482 if (AttrOfD(ch) & A_COLOR)
486 return (ISBLANK(CHDEREF(ch)) &&
487 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
491 * Issue a given span of characters from an array.
492 * Must be functionally equivalent to:
493 * for (i = 0; i < num; i++)
495 * but can leave the cursor positioned at the middle of the interval.
497 * Returns: 0 - cursor is at the end of interval
498 * 1 - cursor is somewhere in the middle
500 * This code is optimized using ech and rep.
503 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext, int num)
507 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
509 if (erase_chars || repeat_char) {
514 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
515 PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
521 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
526 while (runcount < num && CharEq(ntext[runcount], ntext0))
530 * The cost expression in the middle isn't exactly right.
531 * _cup_ch_cost is an upper bound on the cost for moving to the
532 * end of the erased area, but not the cost itself (which we
533 * can't compute without emitting the move). This may result
534 * in erase_chars not getting used in some situations for
535 * which it would be marginally advantageous.
538 && runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
539 && can_clear_with(CHREF(ntext0))) {
541 putp(TPARM_1(erase_chars, runcount));
544 * If this is the last part of the given interval,
545 * don't bother moving cursor, since it can be the
546 * last update on the line.
548 if (runcount < num) {
549 GoTo(SP_PARM->_cursrow, SP_PARM->_curscol + runcount);
551 return 1; /* cursor stays in the middle */
553 } else if (repeat_char && runcount > SP_PARM->_rep_cost) {
554 bool wrap_possible = (SP_PARM->_curscol + runcount
555 >= screen_columns(SP_PARM));
556 int rep_count = runcount;
562 tputs(TPARM_2(repeat_char, CharOf(ntext0), rep_count),
563 rep_count, _nc_outch);
564 SP_PARM->_curscol += rep_count;
567 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
569 for (i = 0; i < runcount; i++)
570 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
578 for (i = 0; i < num; i++)
579 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
584 * Output the line in the given range [first .. last]
586 * If there's a run of identical characters that's long enough to justify
587 * cursor movement, use that also.
589 * Returns: same as EmitRange
592 PutRange(NCURSES_SP_DCLx
593 const NCURSES_CH_T * otext,
594 const NCURSES_CH_T * ntext,
600 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %d, %d, %d)",
601 otext, ntext, row, first, last));
604 && (last - first + 1) > SP_PARM->_inline_cost) {
605 for (j = first, same = 0; j <= last; j++) {
606 if (!same && isWidecExt(otext[j]))
608 if (CharEq(otext[j], ntext[j])) {
611 if (same > SP_PARM->_inline_cost) {
612 EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
613 GoTo(row, first = j);
618 i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
620 * Always return 1 for the next GoTo() after a PutRange() if we found
621 * identical characters at end of interval
623 return (same == 0 ? i : 1);
625 return EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
628 /* leave unbracketed here so 'indent' works */
629 #define MARK_NOCHANGE(win,row) \
630 win->_line[row].firstchar = _NOCHANGE; \
631 win->_line[row].lastchar = _NOCHANGE; \
632 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
635 NCURSES_SP_NAME(doupdate) (NCURSES_SP_DCL0)
640 struct tms before, after;
641 #endif /* USE_TRACE_TIMES */
643 T((T_CALLED("doupdate()")));
650 if (USE_TRACEF(TRACE_UPDATE)) {
652 _tracef("curscr is clear");
654 _tracedump("curscr", curscr);
655 _tracedump("newscr", newscr);
656 _nc_unlock_global(tracef);
660 _nc_signal_handler(FALSE);
662 if (SP_PARM->_fifohold)
663 SP_PARM->_fifohold--;
666 if (SP_PARM->_endwin || _nc_handle_sigwinch(SP_PARM)) {
668 * This is a transparent extension: XSI does not address it,
669 * and applications need not know that ncurses can do it.
671 * Check if the terminal size has changed while curses was off
672 * (this can happen in an xterm, for example), and resize the
673 * ncurses data structures accordingly.
675 _nc_update_screensize(SP_PARM);
679 if (SP_PARM->_endwin) {
681 T(("coming back from shell mode"));
686 SP_PARM->_mouse_resume(SP_PARM);
688 SP_PARM->_endwin = FALSE;
691 /* zero the metering machinery */
693 (void) times(&before);
694 #endif /* USE_TRACE_TIMES */
697 * This is the support for magic-cookie terminals. The theory: we scan
698 * the virtual screen looking for attribute turnons. Where we find one,
699 * check to make sure it's realizable by seeing if the required number of
700 * un-attributed blanks are present before and after the attributed range;
701 * try to shift the range boundaries over blanks (not changing the screen
702 * display) so this becomes true. If it is, shift the beginning attribute
703 * change appropriately (the end one, if we've gotten this far, is
704 * guaranteed room for its cookie). If not, nuke the added attributes out
708 if (magic_cookie_glitch > 0) {
710 attr_t rattr = A_NORMAL;
712 for (i = 0; i < screen_lines(SP_PARM); i++) {
713 for (j = 0; j < screen_columns(SP_PARM); j++) {
715 NCURSES_CH_T *thisline = newscr->_line[i].text;
716 attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
717 attr_t turnon = thisattr & ~rattr;
719 /* is an attribute turned on here? */
725 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
726 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
729 * If the attribute change location is a blank with a "safe"
730 * attribute, undo the attribute turnon. This may ensure
731 * there's enough room to set the attribute before the first
732 * non-blank in the run.
734 #define SAFE(a) (!((a) & SP_PARM->_xmc_triggers))
735 if (ISBLANK(thisline[j]) && SAFE(turnon)) {
736 RemAttr(thisline[j], turnon);
740 /* check that there's enough room at start of span */
741 for (k = 1; k <= magic_cookie_glitch; k++) {
743 || !ISBLANK(thisline[j - k])
744 || !SAFE(AttrOf(thisline[j - k]))) {
746 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
748 (ISBLANK(thisline[j - k])
751 (SAFE(AttrOf(thisline[j - k]))
758 bool end_onscreen = FALSE;
761 /* find end of span, if it's onscreen */
762 for (m = i; m < screen_lines(SP_PARM); m++) {
763 for (; n < screen_columns(SP_PARM); n++) {
764 attr_t testattr = AttrOf(newscr->_line[m].text[n]);
765 if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
768 ("Range attributed with %s ends at (%d, %d)",
769 _traceattr(turnon), m, n));
776 ("Range attributed with %s ends offscreen",
777 _traceattr(turnon)));
781 NCURSES_CH_T *lastline = newscr->_line[m].text;
784 * If there are safely-attributed blanks at the end of
785 * the range, shorten the range. This will help ensure
786 * that there is enough room at end of span.
789 && ISBLANK(lastline[n])
790 && SAFE(AttrOf(lastline[n]))) {
791 RemAttr(lastline[n--], turnon);
794 /* check that there's enough room at end of span */
795 for (k = 1; k <= magic_cookie_glitch; k++) {
796 if (n + k >= screen_columns(SP_PARM)
797 || !ISBLANK(lastline[n + k])
798 || !SAFE(AttrOf(lastline[n + k]))) {
801 ("No room at end in %d,%d%s%s",
803 (ISBLANK(lastline[n + k])
806 (SAFE(AttrOf(lastline[n + k]))
819 ("Clearing %s beginning at (%d, %d)",
820 _traceattr(turnon), i, j));
822 /* turn off new attributes over span */
823 for (p = i; p < screen_lines(SP_PARM); p++) {
824 for (; q < screen_columns(SP_PARM); q++) {
825 attr_t testattr = AttrOf(newscr->_line[p].text[q]);
826 if ((testattr & SP_PARM->_xmc_triggers) == rattr)
828 RemAttr(newscr->_line[p].text[q], turnon);
835 ("Cookie space for %s found before (%d, %d)",
836 _traceattr(turnon), i, j));
839 * Back up the start of range so there's room for cookies
840 * before the first nonblank character.
842 for (k = 1; k <= magic_cookie_glitch; k++)
843 AddAttr(thisline[j - k], turnon);
851 /* show altered highlights after magic-cookie check */
852 if (USE_TRACEF(TRACE_UPDATE)) {
853 _tracef("After magic-cookie check...");
854 _tracedump("newscr", newscr);
855 _nc_unlock_global(tracef);
859 #endif /* USE_XMC_SUPPORT */
862 if (curscr->_clear || newscr->_clear) { /* force refresh ? */
863 ClrUpdate(NCURSES_SP_ARG);
864 curscr->_clear = FALSE; /* reset flag */
865 newscr->_clear = FALSE; /* reset flag */
867 int changedlines = CHECK_INTERVAL;
872 nonempty = min(screen_lines(SP_PARM), newscr->_maxy + 1);
874 if (SP_PARM->_scrolling) {
875 _nc_scroll_optimize();
878 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
880 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
881 for (i = 0; i < nonempty; i++) {
883 * Here is our line-breakout optimization.
885 if (changedlines == CHECK_INTERVAL) {
892 * newscr->line[i].firstchar is normally set
893 * by wnoutrefresh. curscr->line[i].firstchar
894 * is normally set by _nc_scroll_window in the
895 * vertical-movement optimization code,
897 if (newscr->_line[i].firstchar != _NOCHANGE
898 || curscr->_line[i].firstchar != _NOCHANGE) {
899 TransformLine(NCURSES_SP_ARGx i);
903 /* mark line changed successfully */
904 if (i <= newscr->_maxy) {
905 MARK_NOCHANGE(newscr, i);
907 if (i <= curscr->_maxy) {
908 MARK_NOCHANGE(curscr, i);
913 /* put everything back in sync */
914 for (i = nonempty; i <= newscr->_maxy; i++) {
915 MARK_NOCHANGE(newscr, i);
917 for (i = nonempty; i <= curscr->_maxy; i++) {
918 MARK_NOCHANGE(curscr, i);
921 if (!newscr->_leaveok) {
922 curscr->_curx = newscr->_curx;
923 curscr->_cury = newscr->_cury;
925 GoTo(curscr->_cury, curscr->_curx);
930 * We would like to keep the physical screen in normal mode in case we get
931 * other processes writing to the screen. This goal cannot be met for
932 * magic cookies since it interferes with attributes that may propagate
933 * past the current position.
936 if (magic_cookie_glitch != 0)
941 WINDOW_ATTRS(curscr) = WINDOW_ATTRS(newscr);
944 (void) times(&after);
946 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
948 (long) (after.tms_stime - before.tms_stime),
949 (long) (after.tms_utime - before.tms_utime)));
950 #endif /* USE_TRACE_TIMES */
952 _nc_signal_handler(TRUE);
961 return NCURSES_SP_NAME(doupdate) (CURRENT_SCREEN);
968 * Returns the attributed character that corresponds to the "cleared"
969 * screen. If the terminal has the back-color-erase feature, this will be
970 * colored according to the wbkgd() call.
972 * We treat 'curscr' specially because it isn't supposed to be set directly
973 * in the wbkgd() call. Assume 'stdscr' for this case.
975 #define BCE_ATTRS (A_NORMAL|A_COLOR)
976 #define BCE_BKGD(win) (((win) == curscr ? stdscr : (win))->_nc_bkgd)
978 static NCURSES_INLINE NCURSES_CH_T
979 ClrBlank(WINDOW *win)
981 NCURSES_CH_T blank = blankchar;
982 if (back_color_erase)
983 AddAttr(blank, (AttrOf(BCE_BKGD(win)) & BCE_ATTRS));
990 ** Update by clearing and redrawing the entire screen.
995 ClrUpdate(NCURSES_SP_DCL0)
998 NCURSES_CH_T blank = ClrBlank(stdscr);
999 int nonempty = min(screen_lines(SP_PARM), newscr->_maxy + 1);
1001 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1003 ClearScreen(NCURSES_SP_ARGx blank);
1005 TR(TRACE_UPDATE, ("updating screen from scratch"));
1007 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1009 for (i = 0; i < nonempty; i++)
1010 TransformLine(NCURSES_SP_ARGx i);
1012 TR(TRACE_UPDATE, (T_RETURN("")));
1018 ** Clear to end of current line, starting at the cursor position
1022 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, bool needclear)
1027 && SP_PARM->_cursrow >= 0) {
1028 for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1030 NCURSES_CH_T *cp = &(curscr->_line[SP_PARM->_cursrow].text[j]);
1032 if (!CharEq(*cp, blank)) {
1044 TPUTS_TRACE("clr_eol");
1045 if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1048 int count = (screen_columns(SP_PARM) - SP_PARM->_curscol);
1050 PutChar(NCURSES_SP_ARGx CHREF(blank));
1058 ** Clear to end of screen, starting at the cursor position
1062 ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)
1066 row = SP_PARM->_cursrow;
1067 col = SP_PARM->_curscol;
1070 TPUTS_TRACE("clr_eos");
1071 tputs(clr_eos, screen_lines(SP_PARM) - row, _nc_outch);
1073 while (col < screen_columns(SP_PARM))
1074 curscr->_line[row].text[col++] = blank;
1076 for (row++; row < screen_lines(SP_PARM); row++) {
1077 for (col = 0; col < screen_columns(SP_PARM); col++)
1078 curscr->_line[row].text[col] = blank;
1085 * Test if clearing the end of the screen would satisfy part of the
1086 * screen-update. Do this by scanning backwards through the lines in the
1087 * screen, checking if each is blank, and one or more are changed.
1090 ClrBottom(NCURSES_SP_DCLx int total)
1095 int last = min(screen_columns(SP_PARM), newscr->_maxx + 1);
1096 NCURSES_CH_T blank = newscr->_line[total - 1].text[last - 1];
1099 if (clr_eos && can_clear_with(CHREF(blank))) {
1101 for (row = total - 1; row >= 0; row--) {
1102 for (col = 0, ok = TRUE; ok && col < last; col++) {
1103 ok = (CharEq(newscr->_line[row].text[col], blank));
1108 for (col = 0; ok && col < last; col++) {
1109 ok = (CharEq(curscr->_line[row].text[col], blank));
1115 /* don't use clr_eos for just one line if clr_eol available */
1118 ClrToEOS(NCURSES_SP_ARGx blank);
1119 if (SP_PARM->oldhash && SP_PARM->newhash) {
1120 for (row = top; row < screen_lines(SP_PARM); row++)
1121 SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1129 #if USE_WIDEC_SUPPORT
1130 #define check_xmc_transition(a, b) \
1131 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & SP->_xmc_triggers) != 0)
1132 #define xmc_turn_on(a,b) check_xmc_transition(&(a), &(b))
1134 #define xmc_turn_on(a,b) ((((a)^(b)) & ~(a) & SP->_xmc_triggers) != 0)
1137 #define xmc_new(r,c) newscr->_line[r].text[c]
1138 #define xmc_turn_off(a,b) xmc_turn_on(b,a)
1139 #endif /* USE_XMC_SUPPORT */
1142 ** TransformLine(lineno)
1144 ** Transform the given line in curscr to the one in newscr, using
1145 ** Insert/Delete Character if _nc_idcok && has_ic().
1147 ** firstChar = position of first different character in line
1148 ** oLastChar = position of last different character in old line
1149 ** nLastChar = position of last different character in new line
1151 ** move to firstChar
1152 ** overwrite chars up to min(oLastChar, nLastChar)
1153 ** if oLastChar < nLastChar
1154 ** insert newLine[oLastChar+1..nLastChar]
1156 ** delete oLastChar - nLastChar spaces
1160 TransformLine(NCURSES_SP_DCLx int const lineno)
1162 int firstChar, oLastChar, nLastChar;
1163 NCURSES_CH_T *newLine = newscr->_line[lineno].text;
1164 NCURSES_CH_T *oldLine = curscr->_line[lineno].text;
1166 bool attrchanged = FALSE;
1168 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%d)"), lineno));
1170 /* copy new hash value to old one */
1171 if (SP_PARM->oldhash && SP_PARM->newhash)
1172 SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1175 * If we have colors, there is the possibility of having two color pairs
1176 * that display as the same colors. For instance, Lynx does this. Check
1177 * for this case, and update the old line with the new line's colors when
1178 * they are equivalent.
1180 if (SP_PARM->_coloron) {
1184 for (n = 0; n < screen_columns(SP_PARM); n++) {
1185 if (!CharEq(newLine[n], oldLine[n])) {
1186 oldPair = GetPair(oldLine[n]);
1187 newPair = GetPair(newLine[n]);
1188 if (oldPair != newPair
1189 && unColor(oldLine[n]) == unColor(newLine[n])) {
1190 if (oldPair < SP_PARM->_pair_limit
1191 && newPair < SP_PARM->_pair_limit
1192 && (SP_PARM->_color_pairs[oldPair] ==
1193 SP_PARM->_color_pairs[newPair])) {
1194 SetPair(oldLine[n], GetPair(newLine[n]));
1201 if (ceol_standout_glitch && clr_eol) {
1203 while (firstChar < screen_columns(SP_PARM)) {
1204 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1214 if (attrchanged) { /* we may have to disregard the whole line */
1215 GoTo(lineno, firstChar);
1216 ClrToEOL(NCURSES_SP_ARGx ClrBlank(curscr), FALSE);
1217 PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1218 0, (screen_columns(SP_PARM) - 1));
1222 * This is a very simple loop to paint characters which may have the
1223 * magic cookie glitch embedded. It doesn't know much about video
1224 * attributes which are continued from one line to the next. It
1225 * assumes that we have filtered out requests for attribute changes
1226 * that do not get mapped to blank positions.
1228 * FIXME: we are not keeping track of where we put the cookies, so this
1229 * will work properly only once, since we may overwrite a cookie in a
1230 * following operation.
1232 } else if (magic_cookie_glitch > 0) {
1233 GoTo(lineno, firstChar);
1234 for (n = 0; n < screen_columns(SP_PARM); n++) {
1235 int m = n + magic_cookie_glitch;
1237 /* check for turn-on:
1238 * If we are writing an attributed blank, where the
1239 * previous cell is not attributed.
1241 if (ISBLANK(newLine[n])
1243 && xmc_turn_on(newLine[n - 1], newLine[n]))
1246 && xmc_turn_on(xmc_new(lineno - 1, screen_columns(SP_PARM) - 1),
1251 PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1253 /* check for turn-off:
1254 * If we are writing an attributed non-blank, where the
1255 * next cell is blank, and not attributed.
1257 if (!ISBLANK(newLine[n])
1258 && ((n + 1 < screen_columns(SP_PARM)
1259 && xmc_turn_off(newLine[n], newLine[n + 1]))
1260 || (n + 1 >= screen_columns(SP_PARM)
1261 && lineno + 1 < screen_lines(SP_PARM)
1262 && xmc_turn_off(newLine[n], xmc_new(lineno + 1, 0))))) {
1271 /* it may be cheap to clear leading whitespace with clr_bol */
1273 if (clr_bol && can_clear_with(CHREF(blank))) {
1274 int oFirstChar, nFirstChar;
1276 for (oFirstChar = 0;
1277 oFirstChar < screen_columns(SP_PARM);
1279 if (!CharEq(oldLine[oFirstChar], blank))
1281 for (nFirstChar = 0;
1282 nFirstChar < screen_columns(SP_PARM);
1284 if (!CharEq(newLine[nFirstChar], blank))
1287 if (nFirstChar == oFirstChar) {
1288 firstChar = nFirstChar;
1289 /* find the first differing character */
1290 while (firstChar < screen_columns(SP_PARM)
1291 && CharEq(newLine[firstChar], oldLine[firstChar]))
1293 } else if (oFirstChar > nFirstChar) {
1294 firstChar = nFirstChar;
1295 } else { /* oFirstChar < nFirstChar */
1296 firstChar = oFirstChar;
1297 if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1298 if (nFirstChar >= screen_columns(SP_PARM)
1299 && SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1302 TPUTS_TRACE("clr_eol");
1305 GoTo(lineno, nFirstChar - 1);
1307 TPUTS_TRACE("clr_bol");
1311 while (firstChar < nFirstChar)
1312 oldLine[firstChar++] = blank;
1316 /* find the first differing character */
1317 while (firstChar < screen_columns(SP_PARM)
1318 && CharEq(newLine[firstChar], oldLine[firstChar]))
1321 /* if there wasn't one, we're done */
1322 if (firstChar >= screen_columns(SP_PARM)) {
1323 TR(TRACE_UPDATE, (T_RETURN("")));
1327 blank = newLine[screen_columns(SP_PARM) - 1];
1329 if (!can_clear_with(CHREF(blank))) {
1330 /* find the last differing character */
1331 nLastChar = screen_columns(SP_PARM) - 1;
1333 while (nLastChar > firstChar
1334 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1337 if (nLastChar >= firstChar) {
1338 GoTo(lineno, firstChar);
1339 PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1340 firstChar, nLastChar);
1341 memcpy(oldLine + firstChar,
1342 newLine + firstChar,
1343 (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1345 TR(TRACE_UPDATE, (T_RETURN("")));
1349 /* find last non-blank character on old line */
1350 oLastChar = screen_columns(SP_PARM) - 1;
1351 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1354 /* find last non-blank character on new line */
1355 nLastChar = screen_columns(SP_PARM) - 1;
1356 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1359 if ((nLastChar == firstChar)
1360 && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1361 GoTo(lineno, firstChar);
1362 if (!CharEq(newLine[firstChar], blank))
1363 PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1364 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1365 } else if ((nLastChar != oLastChar)
1366 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1367 || !(_nc_idcok && has_ic()))) {
1368 GoTo(lineno, firstChar);
1369 if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1370 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1371 firstChar, nLastChar)) {
1372 GoTo(lineno, nLastChar + 1);
1374 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1376 n = max(nLastChar, oLastChar);
1377 PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1381 int nLastNonblank = nLastChar;
1382 int oLastNonblank = oLastChar;
1384 /* find the last characters that really differ */
1385 /* can be -1 if no characters differ */
1386 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1387 /* don't split a wide char */
1388 if (isWidecExt(newLine[nLastChar]) &&
1389 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1393 if (nLastChar == -1 || oLastChar == -1)
1397 n = min(oLastChar, nLastChar);
1398 if (n >= firstChar) {
1399 GoTo(lineno, firstChar);
1400 PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1404 if (oLastChar < nLastChar) {
1405 int m = max(nLastNonblank, oLastNonblank);
1406 #if USE_WIDEC_SUPPORT
1407 while (isWidecExt(newLine[n + 1]) && n) {
1412 GoTo(lineno, n + 1);
1413 if ((nLastChar < nLastNonblank)
1414 || InsCharCost(nLastChar - oLastChar) > (m - n)) {
1415 PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1418 InsStr(&newLine[n + 1], nLastChar - oLastChar);
1420 } else if (oLastChar > nLastChar) {
1421 GoTo(lineno, n + 1);
1422 if (DelCharCost(oLastChar - nLastChar)
1423 > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1424 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1425 n + 1, nLastNonblank))
1426 GoTo(lineno, nLastNonblank + 1);
1427 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1430 * The delete-char sequence will
1431 * effectively shift in blanks from the
1432 * right margin of the screen. Ensure
1433 * that they are the right color by
1434 * setting the video attributes from
1435 * the last character on the row.
1438 DelChar(oLastChar - nLastChar);
1444 /* update the code's internal representation */
1445 if (screen_columns(SP_PARM) > firstChar)
1446 memcpy(oldLine + firstChar,
1447 newLine + firstChar,
1448 (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1449 TR(TRACE_UPDATE, (T_RETURN("")));
1454 ** ClearScreen(blank)
1456 ** Clear the physical screen and put cursor at home
1461 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1464 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1466 TR(TRACE_UPDATE, ("ClearScreen() called"));
1468 #if NCURSES_EXT_FUNCS
1469 if (SP_PARM->_coloron
1470 && !SP_PARM->_default_color) {
1471 _nc_do_color(GET_SCREEN_PAIR(SP_PARM), 0, FALSE, _nc_outch);
1472 if (!back_color_erase) {
1481 TPUTS_TRACE("clear_screen");
1483 SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1484 position_check(SP_PARM->_cursrow, SP_PARM->_curscol,
1486 } else if (clr_eos) {
1487 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1491 TPUTS_TRACE("clr_eos");
1492 tputs(clr_eos, screen_lines(SP_PARM), _nc_outch);
1493 } else if (clr_eol) {
1494 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1497 for (i = 0; i < screen_lines(SP_PARM); i++) {
1499 TPUTS_TRACE("clr_eol");
1506 for (i = 0; i < screen_lines(SP_PARM); i++) {
1508 for (j = 0; j < screen_columns(SP_PARM); j++)
1509 PutChar(NCURSES_SP_ARGx CHREF(blank));
1514 for (i = 0; i < screen_lines(SP_PARM); i++) {
1515 for (j = 0; j < screen_columns(SP_PARM); j++)
1516 curscr->_line[i].text[j] = blank;
1519 TR(TRACE_UPDATE, ("screen cleared"));
1523 ** InsStr(line, count)
1525 ** Insert the count characters pointed to by line.
1530 InsStr(NCURSES_CH_T * line, int count)
1532 TR(TRACE_UPDATE, ("InsStr(%p,%d) called", line, count));
1534 /* Prefer parm_ich as it has the smallest cost - no need to shift
1535 * the whole line on each character. */
1536 /* The order must match that of InsCharCost. */
1538 TPUTS_TRACE("parm_ich");
1539 tputs(TPARM_1(parm_ich, count), count, _nc_outch);
1541 PutAttrChar(CHREF(*line));
1545 } else if (enter_insert_mode && exit_insert_mode) {
1546 TPUTS_TRACE("enter_insert_mode");
1547 putp(enter_insert_mode);
1549 PutAttrChar(CHREF(*line));
1550 if (insert_padding) {
1551 TPUTS_TRACE("insert_padding");
1552 putp(insert_padding);
1557 TPUTS_TRACE("exit_insert_mode");
1558 putp(exit_insert_mode);
1561 TPUTS_TRACE("insert_character");
1562 putp(insert_character);
1563 PutAttrChar(CHREF(*line));
1564 if (insert_padding) {
1565 TPUTS_TRACE("insert_padding");
1566 putp(insert_padding);
1572 position_check(SP_PARM->_cursrow, SP_PARM->_curscol, "InsStr");
1578 ** Delete count characters at current position
1587 TR(TRACE_UPDATE, ("DelChar(%d) called, position = (%ld,%ld)",
1589 (long) newscr->_cury,
1590 (long) newscr->_curx));
1593 TPUTS_TRACE("parm_dch");
1594 tputs(TPARM_1(parm_dch, count), count, _nc_outch);
1596 for (n = 0; n < count; n++) {
1597 TPUTS_TRACE("delete_character");
1598 putp(delete_character);
1604 * Physical-scrolling support
1606 * This code was adapted from Keith Bostic's hardware scrolling
1607 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1608 * capabilities, narrowed the call interface slightly, and cleaned
1609 * up some convoluted tests. I also added support for the memory_above
1610 * memory_below, and non_dest_scroll_region capabilities.
1612 * For this code to work, we must have either
1613 * change_scroll_region and scroll forward/reverse commands, or
1614 * insert and delete line capabilities.
1615 * When the scrolling region has been set, the cursor has to
1616 * be at the last line of the region to make the scroll up
1617 * happen, or on the first line of region to scroll down.
1619 * This code makes one aesthetic decision in the opposite way from
1620 * BSD curses. BSD curses preferred pairs of il/dl operations
1621 * over scrolls, allegedly because il/dl looked faster. We, on
1622 * the other hand, prefer scrolls because (a) they're just as fast
1623 * on many terminals and (b) using them avoids bouncing an
1624 * unchanged bottom section of the screen up and down, which is
1627 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1629 * I used assumption that capabilities il/il1/dl/dl1 work inside
1630 * changed scroll region not shifting screen contents outside of it.
1631 * If there are any terminals behaving different way, it would be
1632 * necessary to add some conditions to scroll_csr_forward/backward.
1635 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1637 scroll_csr_forward(NCURSES_SP_DCLx
1647 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1650 TPUTS_TRACE("scroll_forward");
1651 putp(scroll_forward);
1652 } else if (n == 1 && delete_line && bot == maxy) {
1655 TPUTS_TRACE("delete_line");
1657 } else if (parm_index && top == miny && bot == maxy) {
1660 TPUTS_TRACE("parm_index");
1661 tputs(TPARM_2(parm_index, n, 0), n, _nc_outch);
1662 } else if (parm_delete_line && bot == maxy) {
1665 TPUTS_TRACE("parm_delete_line");
1666 tputs(TPARM_2(parm_delete_line, n, 0), n, _nc_outch);
1667 } else if (scroll_forward && top == miny && bot == maxy) {
1670 for (i = 0; i < n; i++) {
1671 TPUTS_TRACE("scroll_forward");
1672 putp(scroll_forward);
1674 } else if (delete_line && bot == maxy) {
1677 for (i = 0; i < n; i++) {
1678 TPUTS_TRACE("delete_line");
1684 #if NCURSES_EXT_FUNCS
1687 for (i = 0; i < n; i++) {
1689 for (j = 0; j < screen_columns(SP_PARM); j++)
1690 PutChar(NCURSES_SP_ARGx CHREF(blank));
1697 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1700 scroll_csr_backward(NCURSES_SP_DCLx
1710 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1713 TPUTS_TRACE("scroll_reverse");
1714 putp(scroll_reverse);
1715 } else if (n == 1 && insert_line && bot == maxy) {
1718 TPUTS_TRACE("insert_line");
1720 } else if (parm_rindex && top == miny && bot == maxy) {
1723 TPUTS_TRACE("parm_rindex");
1724 tputs(TPARM_2(parm_rindex, n, 0), n, _nc_outch);
1725 } else if (parm_insert_line && bot == maxy) {
1728 TPUTS_TRACE("parm_insert_line");
1729 tputs(TPARM_2(parm_insert_line, n, 0), n, _nc_outch);
1730 } else if (scroll_reverse && top == miny && bot == maxy) {
1733 for (i = 0; i < n; i++) {
1734 TPUTS_TRACE("scroll_reverse");
1735 putp(scroll_reverse);
1737 } else if (insert_line && bot == maxy) {
1740 for (i = 0; i < n; i++) {
1741 TPUTS_TRACE("insert_line");
1747 #if NCURSES_EXT_FUNCS
1750 for (i = 0; i < n; i++) {
1752 for (j = 0; j < screen_columns(SP_PARM); j++)
1753 PutChar(NCURSES_SP_ARGx CHREF(blank));
1760 /* scroll by using delete_line at del and insert_line at ins */
1763 scroll_idl(int n, int del, int ins, NCURSES_CH_T blank)
1767 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1772 if (n == 1 && delete_line) {
1773 TPUTS_TRACE("delete_line");
1775 } else if (parm_delete_line) {
1776 TPUTS_TRACE("parm_delete_line");
1777 tputs(TPARM_2(parm_delete_line, n, 0), n, _nc_outch);
1778 } else { /* if (delete_line) */
1779 for (i = 0; i < n; i++) {
1780 TPUTS_TRACE("delete_line");
1787 if (n == 1 && insert_line) {
1788 TPUTS_TRACE("insert_line");
1790 } else if (parm_insert_line) {
1791 TPUTS_TRACE("parm_insert_line");
1792 tputs(TPARM_2(parm_insert_line, n, 0), n, _nc_outch);
1793 } else { /* if (insert_line) */
1794 for (i = 0; i < n; i++) {
1795 TPUTS_TRACE("insert_line");
1804 * Note: some terminals require the cursor to be within the scrolling margins
1805 * before setting them. Generally, the cursor must be at the appropriate end
1806 * of the scrolling margins when issuing an indexing operation (it is not
1807 * apparent whether it must also be at the left margin; we do this just to be
1808 * safe). To make the related cursor movement a little faster, we use the
1809 * save/restore cursor capabilities if the terminal has them.
1812 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
1817 /* scroll region from top to bot by n lines */
1819 NCURSES_CH_T blank = ClrBlank(stdscr);
1821 bool cursor_saved = FALSE;
1824 TR(TRACE_MOVE, ("mvcur_scrolln(%d, %d, %d, %d)", n, top, bot, maxy));
1828 * If we scroll, we might remove a cookie.
1830 if (magic_cookie_glitch > 0) {
1835 if (n > 0) { /* scroll up (forward) */
1837 * Explicitly clear if stuff pushed off top of region might
1838 * be saved by the terminal.
1840 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
1842 if (res == ERR && change_scroll_region) {
1843 if ((((n == 1 && scroll_forward) || parm_index)
1844 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
1845 && save_cursor && restore_cursor) {
1846 cursor_saved = TRUE;
1847 TPUTS_TRACE("save_cursor");
1850 TPUTS_TRACE("change_scroll_region");
1851 putp(TPARM_2(change_scroll_region, top, bot));
1853 TPUTS_TRACE("restore_cursor");
1854 putp(restore_cursor);
1856 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1859 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
1861 TPUTS_TRACE("change_scroll_region");
1862 putp(TPARM_2(change_scroll_region, 0, maxy));
1863 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1866 if (res == ERR && _nc_idlok)
1867 res = scroll_idl(n, top, bot - n + 1, blank);
1870 * Clear the newly shifted-in text.
1873 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
1874 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1875 if (bot == maxy && clr_eos) {
1876 GoTo(bot - n + 1, 0);
1877 ClrToEOS(NCURSES_SP_ARGx blank2);
1879 for (i = 0; i < n; i++) {
1881 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
1886 } else { /* (n < 0) - scroll down (backward) */
1887 res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
1889 if (res == ERR && change_scroll_region) {
1891 && (SP_PARM->_cursrow == top || SP_PARM->_cursrow == top - 1)
1892 && save_cursor && restore_cursor) {
1893 cursor_saved = TRUE;
1894 TPUTS_TRACE("save_cursor");
1897 TPUTS_TRACE("change_scroll_region");
1898 putp(TPARM_2(change_scroll_region, top, bot));
1900 TPUTS_TRACE("restore_cursor");
1901 putp(restore_cursor);
1903 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1906 res = scroll_csr_backward(NCURSES_SP_ARGx
1907 -n, top, bot, top, bot, blank);
1909 TPUTS_TRACE("change_scroll_region");
1910 putp(TPARM_2(change_scroll_region, 0, maxy));
1911 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1914 if (res == ERR && _nc_idlok)
1915 res = scroll_idl(-n, bot + n + 1, top, blank);
1918 * Clear the newly shifted-in text.
1921 && (non_dest_scroll_region || (memory_above && top == 0))) {
1922 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1923 for (i = 0; i < -n; i++) {
1925 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
1933 _nc_scroll_window(curscr, n, top, bot, blank);
1935 /* shift hash values too - they can be reused */
1936 _nc_scroll_oldhash(n, top, bot);
1941 #if NCURSES_SP_FUNCS
1943 _nc_scrolln(int n, int top, int bot, int maxy)
1945 return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
1949 NCURSES_EXPORT(void)
1950 _nc_screen_resume(void)
1952 /* make sure terminal is in a sane known state */
1953 SetAttr(SCREEN_ATTRS(SP), A_NORMAL);
1954 newscr->_clear = TRUE;
1956 /* reset color pairs and definitions */
1957 if (SP->_coloron || SP->_color_defs)
1960 /* restore user-defined colors, if any */
1961 if (SP->_color_defs < 0) {
1963 SP->_color_defs = -(SP->_color_defs);
1964 for (n = 0; n < SP->_color_defs; ++n) {
1965 if (SP->_color_table[n].init) {
1967 SP->_color_table[n].r,
1968 SP->_color_table[n].g,
1969 SP->_color_table[n].b);
1974 if (exit_attribute_mode)
1975 putp(exit_attribute_mode);
1977 /* turn off attributes */
1978 if (exit_alt_charset_mode)
1979 putp(exit_alt_charset_mode);
1980 if (exit_standout_mode)
1981 putp(exit_standout_mode);
1982 if (exit_underline_mode)
1983 putp(exit_underline_mode);
1985 if (exit_insert_mode)
1986 putp(exit_insert_mode);
1987 if (enter_am_mode && exit_am_mode)
1988 putp(auto_right_margin ? enter_am_mode : exit_am_mode);
1991 NCURSES_EXPORT(void)
1992 _nc_screen_init(void)
1994 _nc_screen_resume();
1997 /* wrap up screen handling */
1998 NCURSES_EXPORT(void)
1999 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2001 UpdateAttrs(normal);
2002 #if NCURSES_EXT_FUNCS
2003 if (SP_PARM->_coloron
2004 && !SP_PARM->_default_color) {
2005 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2006 SP_PARM->_default_color = TRUE;
2007 _nc_do_color(-1, 0, FALSE, _nc_outch);
2008 SP_PARM->_default_color = FALSE;
2010 mvcur(SP_PARM->_cursrow,
2012 screen_lines(SP_PARM) - 1, 0);
2014 ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2017 if (SP_PARM->_color_defs) {
2022 #if NCURSES_SP_FUNCS
2023 NCURSES_EXPORT(void)
2024 _nc_screen_wrap(void)
2026 NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2031 NCURSES_EXPORT(void)
2032 _nc_do_xmc_glitch(attr_t previous)
2034 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP)));
2038 SP->_curscol += magic_cookie_glitch;
2039 if (SP->_curscol >= SP->_columns)
2041 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie", SP->_cursrow, SP->_curscol));
2046 #endif /* USE_XMC_SUPPORT */