1 /****************************************************************************
2 * Copyright (c) 1998-2012,2013 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>
49 #define CUR SP_TERMTYPE
52 #if defined __HAIKU__ && defined __BEOS__
62 #if defined(TRACE) && HAVE_SYS_TIMES_H && HAVE_TIMES
63 #define USE_TRACE_TIMES 1
65 #define USE_TRACE_TIMES 0
68 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
73 #include <sys/times.h>
79 #include <sys/select.h>
85 MODULE_ID("$Id: tty_update.c,v 1.276 2013/02/16 21:12:02 tom Exp $")
88 * This define controls the line-breakout optimization. Every once in a
89 * while during screen refresh, we want to check for input and abort the
90 * update if there's some waiting. CHECK_INTERVAL controls the number of
91 * changed lines to be emitted between input checks.
93 * Note: Input-check-and-abort is no longer done if the screen is being
94 * updated from scratch. This is a feature, not a bug.
96 #define CHECK_INTERVAL 5
98 #define FILL_BCE(sp) (sp->_coloron && !sp->_default_color && !back_color_erase)
100 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
101 static NCURSES_CH_T normal = NewChar(BLANK_TEXT);
104 * Enable checking to see if doupdate and friends are tracking the true
105 * cursor position correctly. NOTE: this is a debugging hack which will
106 * work ONLY on ANSI-compatible terminals!
108 /* #define POSITION_DEBUG */
110 static NCURSES_INLINE NCURSES_CH_T ClrBlank(NCURSES_SP_DCLx WINDOW *win);
113 static int ClrBottom(SCREEN *, int total);
114 static void ClearScreen(SCREEN *, NCURSES_CH_T blank);
115 static void ClrUpdate(SCREEN *);
116 static void DelChar(SCREEN *, int count);
117 static void InsStr(SCREEN *, NCURSES_CH_T * line, int count);
118 static void TransformLine(SCREEN *, int const lineno);
120 static int ClrBottom(int total);
121 static void ClearScreen(NCURSES_CH_T blank);
122 static void ClrUpdate(void);
123 static void DelChar(int count);
124 static void InsStr(NCURSES_CH_T * line, int count);
125 static void TransformLine(int const lineno);
128 #ifdef POSITION_DEBUG
129 /****************************************************************************
131 * Debugging code. Only works on ANSI-standard terminals.
133 ****************************************************************************/
136 position_check(NCURSES_SP_DCLx int expected_y, int expected_x, char *legend)
137 /* check to see if the real cursor position matches the virtual */
143 if (!_nc_tracing || (expected_y < 0 && expected_x < 0))
146 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
147 memset(buf, '\0', sizeof(buf));
148 NCURSES_PUTP2_FLUSH("cpr", "\033[6n"); /* only works on ANSI-compatibles */
151 int ask = sizeof(buf) - 1 - (s - buf);
152 int got = read(0, s, ask);
156 } while (strchr(buf, 'R') == 0);
157 _tracef("probe returned %s", _nc_visbuf(buf));
159 /* try to interpret as a position report */
160 if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
161 _tracef("position probe failed in %s", legend);
167 if (y - 1 != expected_y || x - 1 != expected_x) {
168 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG);
169 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
173 1, NCURSES_SP_NAME(_nc_outch));
174 _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
175 y - 1, x - 1, expected_y, expected_x, legend);
177 _tracef("position matches OK in %s", legend);
182 #define position_check(sp, expected_y, expected_x, legend) /* nothing */
183 #endif /* POSITION_DEBUG */
185 /****************************************************************************
187 * Optimized update code
189 ****************************************************************************/
191 static NCURSES_INLINE void
192 GoTo(NCURSES_SP_DCLx int const row, int const col)
194 TR(TRACE_MOVE, ("GoTo(%p, %d, %d) from (%d, %d)",
195 (void *) SP_PARM, row, col, SP_PARM->_cursrow, SP_PARM->_curscol));
197 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "GoTo");
199 TINFO_MVCUR(NCURSES_SP_ARGx
203 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "GoTo2");
206 static NCURSES_INLINE void
207 PutAttrChar(NCURSES_SP_DCLx CARG_CH_T ch)
211 #if USE_WIDEC_SUPPORT
215 NCURSES_CH_T attr = CHDEREF(ch);
217 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
219 SP_PARM->_cursrow, SP_PARM->_curscol));
220 #if USE_WIDEC_SUPPORT
222 * If this is not a valid character, there is nothing more to do.
224 if (isWidecExt(CHDEREF(ch))) {
225 TR(TRACE_CHARPUT, ("...skip"));
229 * Determine the number of character cells which the 'ch' value will use
230 * on the screen. It should be at least one.
232 if ((chlen = wcwidth(CharOf(CHDEREF(ch)))) <= 0) {
233 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
236 * If the character falls into any of these special cases, do
237 * not force the result to a blank:
239 * a) it is printable (this works around a bug in wcwidth()).
240 * b) use_legacy_coding() has been called to modify the treatment
242 * c) the acs_map[] has been initialized to allow codes 0-31
243 * to be rendered. This supports Linux console's "PC"
244 * characters. Codes 128-255 are allowed though this is
247 if (is8bits(CharOf(CHDEREF(ch)))
248 && (isprint(CharOf(CHDEREF(ch)))
249 || (SP_PARM->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
250 || (SP_PARM->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
251 || (AttrOf(attr) & A_ALTCHARSET
252 && ((CharOfD(ch) < ACS_LEN
253 && SP_PARM->_acs_map != 0
254 && SP_PARM->_acs_map[CharOfD(ch)] != 0)
255 || (CharOfD(ch) >= 128))))) {
259 TR(TRACE_CHARPUT, ("forced to blank"));
265 if ((AttrOf(attr) & A_ALTCHARSET)
266 && SP_PARM->_acs_map != 0
267 && CharOfD(ch) < ACS_LEN) {
268 my_ch = CHDEREF(ch); /* work around const param */
269 #if USE_WIDEC_SUPPORT
271 * This is crude & ugly, but works most of the time. It checks if the
272 * acs_chars string specified that we have a mapping for this
273 * character, and uses the wide-character mapping when we expect the
274 * normal one to be broken (by mis-design ;-).
276 if (SP_PARM->_screen_acs_fix
277 && SP_PARM->_screen_acs_map[CharOf(my_ch)]) {
278 RemAttr(attr, A_ALTCHARSET);
279 my_ch = _nc_wacs[CharOf(my_ch)];
280 } else if (SP_PARM->_screen_unicode
281 && !SP_PARM->_screen_acs_map[CharOf(my_ch)]
282 && _nc_wacs[CharOf(my_ch)].chars[0]) {
283 RemAttr(attr, A_ALTCHARSET);
284 my_ch = _nc_wacs[CharOf(my_ch)];
288 * If we (still) have alternate character set, it is the normal 8bit
289 * flavor. The _screen_acs_map[] array tells if the character was
290 * really in acs_chars, needed because of the way wide/normal line
291 * drawing flavors are integrated.
293 if (AttrOf(attr) & A_ALTCHARSET) {
295 chtype temp = UChar(SP_PARM->_acs_map[j]);
298 SetChar(my_ch, temp, AttrOf(attr));
301 RemAttr(attr, A_ALTCHARSET);
306 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
307 SetChar(tilde, L('`'), AttrOf(attr));
311 UpdateAttrs(SP_PARM, attr);
313 #if !USE_WIDEC_SUPPORT
316 SP_PARM->_curscol += chlen;
318 NCURSES_PUTP2("char_padding", char_padding);
323 check_pending(NCURSES_SP_DCL0)
324 /* check for pending input */
326 bool have_pending = FALSE;
329 * Only carry out this check when the flag is zero, otherwise we'll
330 * have the refreshing slow down drastically (or stop) if there's an
331 * unread character available.
333 if (SP_PARM->_fifohold != 0)
336 if (SP_PARM->_checkfd >= 0) {
338 struct pollfd fds[1];
339 fds[0].fd = SP_PARM->_checkfd;
340 fds[0].events = POLLIN;
341 if (poll(fds, (size_t) 1, 0) > 0) {
344 #elif defined(__BEOS__)
346 * BeOS's select() is declared in socket.h, so the configure script does
347 * not see it. That's just as well, since that function works only for
348 * sockets. This (using snooze and ioctl) was distilled from Be's patch
349 * for ncurses which uses a separate thread to simulate select().
351 * FIXME: the return values from the ioctl aren't very clear if we get
355 int howmany = ioctl(0, 'ichr', &n);
356 if (howmany >= 0 && n > 0) {
361 struct timeval ktimeout;
364 ktimeout.tv_usec = 0;
367 FD_SET(SP_PARM->_checkfd, &fdset);
368 if (select(SP_PARM->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
374 SP_PARM->_fifohold = 5;
375 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
380 /* put char at lower right corner */
382 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
384 if (!auto_right_margin) {
385 /* we can put the char directly */
386 PutAttrChar(NCURSES_SP_ARGx ch);
387 } else if (enter_am_mode && exit_am_mode) {
388 /* we can suppress automargin */
389 NCURSES_PUTP2("exit_am_mode", exit_am_mode);
391 PutAttrChar(NCURSES_SP_ARGx ch);
393 position_check(SP_PARM,
398 NCURSES_PUTP2("enter_am_mode", enter_am_mode);
399 } else if ((enter_insert_mode && exit_insert_mode)
400 || insert_character || parm_ich) {
402 screen_lines(SP_PARM) - 1,
403 screen_columns(SP_PARM) - 2);
404 PutAttrChar(NCURSES_SP_ARGx ch);
406 screen_lines(SP_PARM) - 1,
407 screen_columns(SP_PARM) - 2);
408 InsStr(NCURSES_SP_ARGx
409 NewScreen(SP_PARM)->_line[screen_lines(SP_PARM) - 1].text +
410 screen_columns(SP_PARM) - 2, 1);
415 * Wrap the cursor position, i.e., advance to the beginning of the next line.
418 wrap_cursor(NCURSES_SP_DCL0)
420 if (eat_newline_glitch) {
422 * xenl can manifest two different ways. The vt100 way is that, when
423 * you'd expect the cursor to wrap, it stays hung at the right margin
424 * (on top of the character just emitted) and doesn't wrap until the
425 * *next* graphic char is emitted. The c100 way is to ignore LF
426 * received just after an am wrap.
428 * An aggressive way to handle this would be to emit CR/LF after the
429 * char and then assume the wrap is done, you're on the first position
430 * of the next line, and the terminal out of its weird state. Here
431 * it's safe to just tell the code that the cursor is in hyperspace and
432 * let the next mvcur() call straighten things out.
434 SP_PARM->_curscol = -1;
435 SP_PARM->_cursrow = -1;
436 } else if (auto_right_margin) {
437 SP_PARM->_curscol = 0;
440 * We've actually moved - but may have to work around problems with
441 * video attributes not working.
443 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP_PARM))) {
444 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
445 (unsigned long) AttrOf(SCREEN_ATTRS(SP_PARM)),
446 _traceattr(AttrOf(SCREEN_ATTRS(SP_PARM)))));
447 (void) VIDATTR(SP_PARM, A_NORMAL, 0);
452 position_check(SP_PARM,
458 static NCURSES_INLINE void
459 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
460 /* insert character, handling automargin stuff */
462 if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1 &&
463 SP_PARM->_curscol == screen_columns(SP_PARM) - 1) {
464 PutCharLR(NCURSES_SP_ARGx ch);
466 PutAttrChar(NCURSES_SP_ARGx ch);
469 if (SP_PARM->_curscol >= screen_columns(SP_PARM))
470 wrap_cursor(NCURSES_SP_ARG);
472 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "PutChar");
476 * Check whether the given character can be output by clearing commands. This
477 * includes test for being a space and not including any 'bad' attributes, such
478 * as A_REVERSE. All attribute flags which don't affect appearance of a space
479 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
481 static NCURSES_INLINE bool
482 can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)
484 if (!back_color_erase && SP_PARM->_coloron) {
485 #if NCURSES_EXT_FUNCS
488 if (!SP_PARM->_default_color)
490 if (SP_PARM->_default_fg != C_MASK || SP_PARM->_default_bg != C_MASK)
492 if ((pair = GetPair(CHDEREF(ch))) != 0) {
494 if (NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
497 || (fg != C_MASK || bg != C_MASK)) {
502 if (AttrOfD(ch) & A_COLOR)
506 return (ISBLANK(CHDEREF(ch)) &&
507 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
511 * Issue a given span of characters from an array.
512 * Must be functionally equivalent to:
513 * for (i = 0; i < num; i++)
515 * but can leave the cursor positioned at the middle of the interval.
517 * Returns: 0 - cursor is at the end of interval
518 * 1 - cursor is somewhere in the middle
520 * This code is optimized using ech and rep.
523 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext, int num)
527 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
529 if (erase_chars || repeat_char) {
534 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
535 PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
541 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
546 while (runcount < num && CharEq(ntext[runcount], ntext0))
550 * The cost expression in the middle isn't exactly right.
551 * _cup_ch_cost is an upper bound on the cost for moving to the
552 * end of the erased area, but not the cost itself (which we
553 * can't compute without emitting the move). This may result
554 * in erase_chars not getting used in some situations for
555 * which it would be marginally advantageous.
558 && runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
559 && can_clear_with(NCURSES_SP_ARGx CHREF(ntext0))) {
560 UpdateAttrs(SP_PARM, ntext0);
561 NCURSES_PUTP2("erase_chars", TPARM_1(erase_chars, runcount));
564 * If this is the last part of the given interval,
565 * don't bother moving cursor, since it can be the
566 * last update on the line.
568 if (runcount < num) {
571 SP_PARM->_curscol + runcount);
573 return 1; /* cursor stays in the middle */
575 } else if (repeat_char && runcount > SP_PARM->_rep_cost) {
576 bool wrap_possible = (SP_PARM->_curscol + runcount >=
577 screen_columns(SP_PARM));
578 int rep_count = runcount;
583 UpdateAttrs(SP_PARM, ntext0);
584 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
589 NCURSES_SP_NAME(_nc_outch));
590 SP_PARM->_curscol += rep_count;
593 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
595 for (i = 0; i < runcount; i++)
596 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
604 for (i = 0; i < num; i++)
605 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
610 * Output the line in the given range [first .. last]
612 * If there's a run of identical characters that's long enough to justify
613 * cursor movement, use that also.
615 * Returns: same as EmitRange
618 PutRange(NCURSES_SP_DCLx
619 const NCURSES_CH_T * otext,
620 const NCURSES_CH_T * ntext,
626 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %p, %d, %d, %d)",
628 (const void *) otext,
629 (const void *) ntext,
633 && (last - first + 1) > SP_PARM->_inline_cost) {
634 for (j = first, same = 0; j <= last; j++) {
635 if (!same && isWidecExt(otext[j]))
637 if (CharEq(otext[j], ntext[j])) {
640 if (same > SP_PARM->_inline_cost) {
641 EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
642 GoTo(NCURSES_SP_ARGx row, first = j);
647 i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
649 * Always return 1 for the next GoTo() after a PutRange() if we found
650 * identical characters at end of interval
652 return (same == 0 ? i : 1);
654 return EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
657 /* leave unbracketed here so 'indent' works */
658 #define MARK_NOCHANGE(win,row) \
659 win->_line[row].firstchar = _NOCHANGE; \
660 win->_line[row].lastchar = _NOCHANGE; \
661 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
664 TINFO_DOUPDATE(NCURSES_SP_DCL0)
669 struct tms before, after;
670 #endif /* USE_TRACE_TIMES */
672 T((T_CALLED("_nc_tinfo:doupdate(%p)"), (void *) SP_PARM));
679 * It is "legal" but unlikely that an application could assign a new
680 * value to one of the standard windows. Check for that possibility
681 * and try to recover.
683 * We do not allow applications to assign new values in the reentrant
686 #define SyncScreens(internal,exported) \
687 if (internal == 0) internal = exported; \
688 if (internal != exported) exported = internal
690 SyncScreens(CurScreen(SP_PARM), curscr);
691 SyncScreens(NewScreen(SP_PARM), newscr);
692 SyncScreens(StdScreen(SP_PARM), stdscr);
695 if (CurScreen(SP_PARM) == 0
696 || NewScreen(SP_PARM) == 0
697 || StdScreen(SP_PARM) == 0)
701 if (USE_TRACEF(TRACE_UPDATE)) {
702 if (CurScreen(SP_PARM)->_clear)
703 _tracef("curscr is clear");
705 _tracedump("curscr", CurScreen(SP_PARM));
706 _tracedump("newscr", NewScreen(SP_PARM));
707 _nc_unlock_global(tracef);
711 _nc_signal_handler(FALSE);
713 if (SP_PARM->_fifohold)
714 SP_PARM->_fifohold--;
717 if (SP_PARM->_endwin || _nc_handle_sigwinch(SP_PARM)) {
719 * This is a transparent extension: XSI does not address it,
720 * and applications need not know that ncurses can do it.
722 * Check if the terminal size has changed while curses was off
723 * (this can happen in an xterm, for example), and resize the
724 * ncurses data structures accordingly.
726 _nc_update_screensize(SP_PARM);
730 if (SP_PARM->_endwin) {
732 T(("coming back from shell mode"));
733 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_ARG);
735 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG);
736 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
737 SP_PARM->_mouse_resume(SP_PARM);
739 SP_PARM->_endwin = FALSE;
742 /* zero the metering machinery */
744 (void) times(&before);
745 #endif /* USE_TRACE_TIMES */
748 * This is the support for magic-cookie terminals. The theory: we scan
749 * the virtual screen looking for attribute turnons. Where we find one,
750 * check to make sure it's realizable by seeing if the required number of
751 * un-attributed blanks are present before and after the attributed range;
752 * try to shift the range boundaries over blanks (not changing the screen
753 * display) so this becomes true. If it is, shift the beginning attribute
754 * change appropriately (the end one, if we've gotten this far, is
755 * guaranteed room for its cookie). If not, nuke the added attributes out
759 if (magic_cookie_glitch > 0) {
761 attr_t rattr = A_NORMAL;
763 for (i = 0; i < screen_lines(SP_PARM); i++) {
764 for (j = 0; j < screen_columns(SP_PARM); j++) {
766 NCURSES_CH_T *thisline = NewScreen(SP_PARM)->_line[i].text;
767 attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
768 attr_t turnon = thisattr & ~rattr;
770 /* is an attribute turned on here? */
776 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
777 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
780 * If the attribute change location is a blank with a "safe"
781 * attribute, undo the attribute turnon. This may ensure
782 * there's enough room to set the attribute before the first
783 * non-blank in the run.
785 #define SAFE(scr,a) (!((a) & (scr)->_xmc_triggers))
786 if (ISBLANK(thisline[j]) && SAFE(SP_PARM, turnon)) {
787 RemAttr(thisline[j], turnon);
791 /* check that there's enough room at start of span */
792 for (k = 1; k <= magic_cookie_glitch; k++) {
794 || !ISBLANK(thisline[j - k])
795 || !SAFE(SP_PARM, AttrOf(thisline[j - k]))) {
797 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
799 (ISBLANK(thisline[j - k])
802 (SAFE(SP_PARM, AttrOf(thisline[j - k]))
809 bool end_onscreen = FALSE;
812 /* find end of span, if it's onscreen */
813 for (m = i; m < screen_lines(SP_PARM); m++) {
814 for (; n < screen_columns(SP_PARM); n++) {
816 AttrOf(NewScreen(SP_PARM)->_line[m].text[n]);
817 if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
820 ("Range attributed with %s ends at (%d, %d)",
821 _traceattr(turnon), m, n));
828 ("Range attributed with %s ends offscreen",
829 _traceattr(turnon)));
833 NCURSES_CH_T *lastline =
834 NewScreen(SP_PARM)->_line[m].text;
837 * If there are safely-attributed blanks at the end of
838 * the range, shorten the range. This will help ensure
839 * that there is enough room at end of span.
842 && ISBLANK(lastline[n])
843 && SAFE(SP_PARM, AttrOf(lastline[n]))) {
844 RemAttr(lastline[n--], turnon);
847 /* check that there's enough room at end of span */
848 for (k = 1; k <= magic_cookie_glitch; k++) {
849 if (n + k >= screen_columns(SP_PARM)
850 || !ISBLANK(lastline[n + k])
851 || !SAFE(SP_PARM, AttrOf(lastline[n + k]))) {
854 ("No room at end in %d,%d%s%s",
856 (ISBLANK(lastline[n + k])
859 (SAFE(SP_PARM, AttrOf(lastline[n + k]))
872 ("Clearing %s beginning at (%d, %d)",
873 _traceattr(turnon), i, j));
875 /* turn off new attributes over span */
876 for (p = i; p < screen_lines(SP_PARM); p++) {
877 for (; q < screen_columns(SP_PARM); q++) {
878 attr_t testattr = AttrOf(newscr->_line[p].text[q]);
879 if ((testattr & SP_PARM->_xmc_triggers) == rattr)
881 RemAttr(NewScreen(SP_PARM)->_line[p].text[q], turnon);
888 ("Cookie space for %s found before (%d, %d)",
889 _traceattr(turnon), i, j));
892 * Back up the start of range so there's room for cookies
893 * before the first nonblank character.
895 for (k = 1; k <= magic_cookie_glitch; k++)
896 AddAttr(thisline[j - k], turnon);
904 /* show altered highlights after magic-cookie check */
905 if (USE_TRACEF(TRACE_UPDATE)) {
906 _tracef("After magic-cookie check...");
907 _tracedump("newscr", NewScreen(SP_PARM));
908 _nc_unlock_global(tracef);
912 #endif /* USE_XMC_SUPPORT */
915 if (CurScreen(SP_PARM)->_clear || NewScreen(SP_PARM)->_clear) { /* force refresh ? */
916 ClrUpdate(NCURSES_SP_ARG);
917 CurScreen(SP_PARM)->_clear = FALSE; /* reset flag */
918 NewScreen(SP_PARM)->_clear = FALSE; /* reset flag */
920 int changedlines = CHECK_INTERVAL;
922 if (check_pending(NCURSES_SP_ARG))
925 nonempty = min(screen_lines(SP_PARM), NewScreen(SP_PARM)->_maxy + 1);
927 if (SP_PARM->_scrolling) {
928 NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_ARG);
931 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
933 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
934 for (i = 0; i < nonempty; i++) {
936 * Here is our line-breakout optimization.
938 if (changedlines == CHECK_INTERVAL) {
939 if (check_pending(NCURSES_SP_ARG))
945 * newscr->line[i].firstchar is normally set
946 * by wnoutrefresh. curscr->line[i].firstchar
947 * is normally set by _nc_scroll_window in the
948 * vertical-movement optimization code,
950 if (NewScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE
951 || CurScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE) {
952 TransformLine(NCURSES_SP_ARGx i);
956 /* mark line changed successfully */
957 if (i <= NewScreen(SP_PARM)->_maxy) {
958 MARK_NOCHANGE(NewScreen(SP_PARM), i);
960 if (i <= CurScreen(SP_PARM)->_maxy) {
961 MARK_NOCHANGE(CurScreen(SP_PARM), i);
966 /* put everything back in sync */
967 for (i = nonempty; i <= NewScreen(SP_PARM)->_maxy; i++) {
968 MARK_NOCHANGE(NewScreen(SP_PARM), i);
970 for (i = nonempty; i <= CurScreen(SP_PARM)->_maxy; i++) {
971 MARK_NOCHANGE(CurScreen(SP_PARM), i);
974 if (!NewScreen(SP_PARM)->_leaveok) {
975 CurScreen(SP_PARM)->_curx = NewScreen(SP_PARM)->_curx;
976 CurScreen(SP_PARM)->_cury = NewScreen(SP_PARM)->_cury;
978 GoTo(NCURSES_SP_ARGx CurScreen(SP_PARM)->_cury, CurScreen(SP_PARM)->_curx);
983 * We would like to keep the physical screen in normal mode in case we get
984 * other processes writing to the screen. This goal cannot be met for
985 * magic cookies since it interferes with attributes that may propagate
986 * past the current position.
989 if (magic_cookie_glitch != 0)
991 UpdateAttrs(SP_PARM, normal);
993 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
994 WINDOW_ATTRS(CurScreen(SP_PARM)) = WINDOW_ATTRS(NewScreen(SP_PARM));
997 (void) times(&after);
999 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
1001 (long) (after.tms_stime - before.tms_stime),
1002 (long) (after.tms_utime - before.tms_utime)));
1003 #endif /* USE_TRACE_TIMES */
1005 _nc_signal_handler(TRUE);
1010 #if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)
1014 return TINFO_DOUPDATE(CURRENT_SCREEN);
1021 * Returns the attributed character that corresponds to the "cleared"
1022 * screen. If the terminal has the back-color-erase feature, this will be
1023 * colored according to the wbkgd() call.
1025 * We treat 'curscr' specially because it isn't supposed to be set directly
1026 * in the wbkgd() call. Assume 'stdscr' for this case.
1028 #define BCE_ATTRS (A_NORMAL|A_COLOR)
1029 #define BCE_BKGD(sp,win) (((win) == CurScreen(sp) ? StdScreen(sp) : (win))->_nc_bkgd)
1031 static NCURSES_INLINE NCURSES_CH_T
1032 ClrBlank(NCURSES_SP_DCLx WINDOW *win)
1034 NCURSES_CH_T blank = blankchar;
1035 if (back_color_erase)
1036 AddAttr(blank, (AttrOf(BCE_BKGD(SP_PARM, win)) & BCE_ATTRS));
1043 ** Update by clearing and redrawing the entire screen.
1048 ClrUpdate(NCURSES_SP_DCL0)
1050 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1053 NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1054 int nonempty = min(screen_lines(SP_PARM),
1055 NewScreen(SP_PARM)->_maxy + 1);
1057 ClearScreen(NCURSES_SP_ARGx blank);
1059 TR(TRACE_UPDATE, ("updating screen from scratch"));
1061 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1063 for (i = 0; i < nonempty; i++)
1064 TransformLine(NCURSES_SP_ARGx i);
1066 TR(TRACE_UPDATE, (T_RETURN("")));
1072 ** Clear to end of current line, starting at the cursor position
1076 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, int needclear)
1080 if (CurScreen(SP_PARM) != 0
1081 && SP_PARM->_cursrow >= 0) {
1082 for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1085 &(CurScreen(SP_PARM)->_line[SP_PARM->_cursrow].text[j]);
1087 if (!CharEq(*cp, blank)) {
1096 UpdateAttrs(SP_PARM, blank);
1097 if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1098 NCURSES_PUTP2("clr_eol", clr_eol);
1100 int count = (screen_columns(SP_PARM) - SP_PARM->_curscol);
1102 PutChar(NCURSES_SP_ARGx CHREF(blank));
1110 ** Clear to end of screen, starting at the cursor position
1114 ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)
1118 row = SP_PARM->_cursrow;
1119 col = SP_PARM->_curscol;
1126 UpdateAttrs(SP_PARM, blank);
1127 TPUTS_TRACE("clr_eos");
1128 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1130 screen_lines(SP_PARM) - row,
1131 NCURSES_SP_NAME(_nc_outch));
1133 while (col < screen_columns(SP_PARM))
1134 CurScreen(SP_PARM)->_line[row].text[col++] = blank;
1136 for (row++; row < screen_lines(SP_PARM); row++) {
1137 for (col = 0; col < screen_columns(SP_PARM); col++)
1138 CurScreen(SP_PARM)->_line[row].text[col] = blank;
1145 * Test if clearing the end of the screen would satisfy part of the
1146 * screen-update. Do this by scanning backwards through the lines in the
1147 * screen, checking if each is blank, and one or more are changed.
1150 ClrBottom(NCURSES_SP_DCLx int total)
1155 int last = min(screen_columns(SP_PARM), NewScreen(SP_PARM)->_maxx + 1);
1156 NCURSES_CH_T blank = NewScreen(SP_PARM)->_line[total - 1].text[last - 1];
1159 if (clr_eos && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1161 for (row = total - 1; row >= 0; row--) {
1162 for (col = 0, ok = TRUE; ok && col < last; col++) {
1163 ok = (CharEq(NewScreen(SP_PARM)->_line[row].text[col], blank));
1168 for (col = 0; ok && col < last; col++) {
1169 ok = (CharEq(CurScreen(SP_PARM)->_line[row].text[col], blank));
1175 /* don't use clr_eos for just one line if clr_eol available */
1177 GoTo(NCURSES_SP_ARGx top, 0);
1178 ClrToEOS(NCURSES_SP_ARGx blank);
1179 if (SP_PARM->oldhash && SP_PARM->newhash) {
1180 for (row = top; row < screen_lines(SP_PARM); row++)
1181 SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1189 #if USE_WIDEC_SUPPORT
1190 #define check_xmc_transition(sp, a, b) \
1191 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & (sp)->_xmc_triggers) != 0)
1192 #define xmc_turn_on(sp,a,b) check_xmc_transition(sp,&(a), &(b))
1194 #define xmc_turn_on(sp,a,b) ((((a)^(b)) & ~(a) & (sp)->_xmc_triggers) != 0)
1197 #define xmc_new(sp,r,c) NewScreen(sp)->_line[r].text[c]
1198 #define xmc_turn_off(sp,a,b) xmc_turn_on(sp,b,a)
1199 #endif /* USE_XMC_SUPPORT */
1202 ** TransformLine(lineno)
1204 ** Transform the given line in curscr to the one in newscr, using
1205 ** Insert/Delete Character if idcok && has_ic().
1207 ** firstChar = position of first different character in line
1208 ** oLastChar = position of last different character in old line
1209 ** nLastChar = position of last different character in new line
1211 ** move to firstChar
1212 ** overwrite chars up to min(oLastChar, nLastChar)
1213 ** if oLastChar < nLastChar
1214 ** insert newLine[oLastChar+1..nLastChar]
1216 ** delete oLastChar - nLastChar spaces
1220 TransformLine(NCURSES_SP_DCLx int const lineno)
1222 int firstChar, oLastChar, nLastChar;
1223 NCURSES_CH_T *newLine = NewScreen(SP_PARM)->_line[lineno].text;
1224 NCURSES_CH_T *oldLine = CurScreen(SP_PARM)->_line[lineno].text;
1226 bool attrchanged = FALSE;
1228 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%p, %d)"), (void *) SP_PARM, lineno));
1230 /* copy new hash value to old one */
1231 if (SP_PARM->oldhash && SP_PARM->newhash)
1232 SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1235 * If we have colors, there is the possibility of having two color pairs
1236 * that display as the same colors. For instance, Lynx does this. Check
1237 * for this case, and update the old line with the new line's colors when
1238 * they are equivalent.
1240 if (SP_PARM->_coloron) {
1244 for (n = 0; n < screen_columns(SP_PARM); n++) {
1245 if (!CharEq(newLine[n], oldLine[n])) {
1246 oldPair = GetPair(oldLine[n]);
1247 newPair = GetPair(newLine[n]);
1248 if (oldPair != newPair
1249 && unColor(oldLine[n]) == unColor(newLine[n])) {
1250 if (oldPair < SP_PARM->_pair_limit
1251 && newPair < SP_PARM->_pair_limit
1252 && (SP_PARM->_color_pairs[oldPair] ==
1253 SP_PARM->_color_pairs[newPair])) {
1254 SetPair(oldLine[n], GetPair(newLine[n]));
1261 if (ceol_standout_glitch && clr_eol) {
1263 while (firstChar < screen_columns(SP_PARM)) {
1264 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1274 if (attrchanged) { /* we may have to disregard the whole line */
1275 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1276 ClrToEOL(NCURSES_SP_ARGx
1277 ClrBlank(NCURSES_SP_ARGx
1278 CurScreen(SP_PARM)), FALSE);
1279 PutRange(NCURSES_SP_ARGx
1280 oldLine, newLine, lineno, 0,
1281 screen_columns(SP_PARM) - 1);
1285 * This is a very simple loop to paint characters which may have the
1286 * magic cookie glitch embedded. It doesn't know much about video
1287 * attributes which are continued from one line to the next. It
1288 * assumes that we have filtered out requests for attribute changes
1289 * that do not get mapped to blank positions.
1291 * FIXME: we are not keeping track of where we put the cookies, so this
1292 * will work properly only once, since we may overwrite a cookie in a
1293 * following operation.
1295 } else if (magic_cookie_glitch > 0) {
1296 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1297 for (n = 0; n < screen_columns(SP_PARM); n++) {
1298 int m = n + magic_cookie_glitch;
1300 /* check for turn-on:
1301 * If we are writing an attributed blank, where the
1302 * previous cell is not attributed.
1304 if (ISBLANK(newLine[n])
1306 && xmc_turn_on(SP_PARM, newLine[n - 1], newLine[n]))
1309 && xmc_turn_on(SP_PARM,
1310 xmc_new(SP_PARM, lineno - 1,
1311 screen_columns(SP_PARM) - 1),
1316 PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1318 /* check for turn-off:
1319 * If we are writing an attributed non-blank, where the
1320 * next cell is blank, and not attributed.
1322 if (!ISBLANK(newLine[n])
1323 && ((n + 1 < screen_columns(SP_PARM)
1324 && xmc_turn_off(SP_PARM, newLine[n], newLine[n + 1]))
1325 || (n + 1 >= screen_columns(SP_PARM)
1326 && lineno + 1 < screen_lines(SP_PARM)
1327 && xmc_turn_off(SP_PARM,
1329 xmc_new(SP_PARM, lineno + 1, 0))))) {
1338 /* it may be cheap to clear leading whitespace with clr_bol */
1340 if (clr_bol && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1341 int oFirstChar, nFirstChar;
1343 for (oFirstChar = 0;
1344 oFirstChar < screen_columns(SP_PARM);
1346 if (!CharEq(oldLine[oFirstChar], blank))
1348 for (nFirstChar = 0;
1349 nFirstChar < screen_columns(SP_PARM);
1351 if (!CharEq(newLine[nFirstChar], blank))
1354 if (nFirstChar == oFirstChar) {
1355 firstChar = nFirstChar;
1356 /* find the first differing character */
1357 while (firstChar < screen_columns(SP_PARM)
1358 && CharEq(newLine[firstChar], oldLine[firstChar]))
1360 } else if (oFirstChar > nFirstChar) {
1361 firstChar = nFirstChar;
1362 } else { /* oFirstChar < nFirstChar */
1363 firstChar = oFirstChar;
1364 if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1365 if (nFirstChar >= screen_columns(SP_PARM)
1366 && SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1367 GoTo(NCURSES_SP_ARGx lineno, 0);
1368 UpdateAttrs(SP_PARM, blank);
1369 NCURSES_PUTP2("clr_eol", clr_eol);
1371 GoTo(NCURSES_SP_ARGx lineno, nFirstChar - 1);
1372 UpdateAttrs(SP_PARM, blank);
1373 NCURSES_PUTP2("clr_bol", clr_bol);
1376 while (firstChar < nFirstChar)
1377 oldLine[firstChar++] = blank;
1381 /* find the first differing character */
1382 while (firstChar < screen_columns(SP_PARM)
1383 && CharEq(newLine[firstChar], oldLine[firstChar]))
1386 /* if there wasn't one, we're done */
1387 if (firstChar >= screen_columns(SP_PARM)) {
1388 TR(TRACE_UPDATE, (T_RETURN("")));
1392 blank = newLine[screen_columns(SP_PARM) - 1];
1394 if (!can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1395 /* find the last differing character */
1396 nLastChar = screen_columns(SP_PARM) - 1;
1398 while (nLastChar > firstChar
1399 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1402 if (nLastChar >= firstChar) {
1403 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1404 PutRange(NCURSES_SP_ARGx
1410 memcpy(oldLine + firstChar,
1411 newLine + firstChar,
1412 (unsigned) (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1414 TR(TRACE_UPDATE, (T_RETURN("")));
1418 /* find last non-blank character on old line */
1419 oLastChar = screen_columns(SP_PARM) - 1;
1420 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1423 /* find last non-blank character on new line */
1424 nLastChar = screen_columns(SP_PARM) - 1;
1425 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1428 if ((nLastChar == firstChar)
1429 && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1430 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1431 if (!CharEq(newLine[firstChar], blank))
1432 PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1433 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1434 } else if ((nLastChar != oLastChar)
1435 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1436 || !(SP_PARM->_nc_sp_idcok
1437 && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG)))) {
1438 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1439 if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1440 if (PutRange(NCURSES_SP_ARGx
1446 GoTo(NCURSES_SP_ARGx lineno, nLastChar + 1);
1448 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1450 n = max(nLastChar, oLastChar);
1451 PutRange(NCURSES_SP_ARGx
1459 int nLastNonblank = nLastChar;
1460 int oLastNonblank = oLastChar;
1462 /* find the last characters that really differ */
1463 /* can be -1 if no characters differ */
1464 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1465 /* don't split a wide char */
1466 if (isWidecExt(newLine[nLastChar]) &&
1467 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1471 if (nLastChar == -1 || oLastChar == -1)
1475 n = min(oLastChar, nLastChar);
1476 if (n >= firstChar) {
1477 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1478 PutRange(NCURSES_SP_ARGx
1486 if (oLastChar < nLastChar) {
1487 int m = max(nLastNonblank, oLastNonblank);
1488 #if USE_WIDEC_SUPPORT
1489 while (isWidecExt(newLine[n + 1]) && n) {
1494 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1495 if ((nLastChar < nLastNonblank)
1496 || InsCharCost(SP_PARM, nLastChar - oLastChar) > (m - n)) {
1497 PutRange(NCURSES_SP_ARGx
1504 InsStr(NCURSES_SP_ARGx &newLine[n + 1], nLastChar - oLastChar);
1506 } else if (oLastChar > nLastChar) {
1507 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1508 if (DelCharCost(SP_PARM, oLastChar - nLastChar)
1509 > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1510 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1511 n + 1, nLastNonblank))
1512 GoTo(NCURSES_SP_ARGx lineno, nLastNonblank + 1);
1513 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1516 * The delete-char sequence will
1517 * effectively shift in blanks from the
1518 * right margin of the screen. Ensure
1519 * that they are the right color by
1520 * setting the video attributes from
1521 * the last character on the row.
1523 UpdateAttrs(SP_PARM, blank);
1524 DelChar(NCURSES_SP_ARGx oLastChar - nLastChar);
1530 /* update the code's internal representation */
1531 if (screen_columns(SP_PARM) > firstChar)
1532 memcpy(oldLine + firstChar,
1533 newLine + firstChar,
1534 (unsigned) (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1535 TR(TRACE_UPDATE, (T_RETURN("")));
1540 ** ClearScreen(blank)
1542 ** Clear the physical screen and put cursor at home
1547 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1550 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1552 TR(TRACE_UPDATE, ("ClearScreen() called"));
1554 #if NCURSES_EXT_FUNCS
1555 if (SP_PARM->_coloron
1556 && !SP_PARM->_default_color) {
1557 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
1558 (short) GET_SCREEN_PAIR(SP_PARM),
1561 NCURSES_SP_NAME(_nc_outch));
1562 if (!back_color_erase) {
1570 UpdateAttrs(SP_PARM, blank);
1571 NCURSES_PUTP2("clear_screen", clear_screen);
1572 SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1573 position_check(SP_PARM,
1577 } else if (clr_eos) {
1578 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1579 GoTo(NCURSES_SP_ARGx 0, 0);
1580 UpdateAttrs(SP_PARM, blank);
1581 TPUTS_TRACE("clr_eos");
1582 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1584 screen_lines(SP_PARM),
1585 NCURSES_SP_NAME(_nc_outch));
1586 } else if (clr_eol) {
1587 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1588 UpdateAttrs(SP_PARM, blank);
1589 for (i = 0; i < screen_lines(SP_PARM); i++) {
1590 GoTo(NCURSES_SP_ARGx i, 0);
1591 NCURSES_PUTP2("clr_eol", clr_eol);
1593 GoTo(NCURSES_SP_ARGx 0, 0);
1596 UpdateAttrs(SP_PARM, blank);
1597 for (i = 0; i < screen_lines(SP_PARM); i++) {
1598 GoTo(NCURSES_SP_ARGx i, 0);
1599 for (j = 0; j < screen_columns(SP_PARM); j++)
1600 PutChar(NCURSES_SP_ARGx CHREF(blank));
1602 GoTo(NCURSES_SP_ARGx 0, 0);
1605 for (i = 0; i < screen_lines(SP_PARM); i++) {
1606 for (j = 0; j < screen_columns(SP_PARM); j++)
1607 CurScreen(SP_PARM)->_line[i].text[j] = blank;
1610 TR(TRACE_UPDATE, ("screen cleared"));
1614 ** InsStr(line, count)
1616 ** Insert the count characters pointed to by line.
1621 InsStr(NCURSES_SP_DCLx NCURSES_CH_T * line, int count)
1623 TR(TRACE_UPDATE, ("InsStr(%p, %p,%d) called",
1625 (void *) line, count));
1627 /* Prefer parm_ich as it has the smallest cost - no need to shift
1628 * the whole line on each character. */
1629 /* The order must match that of InsCharCost. */
1631 TPUTS_TRACE("parm_ich");
1632 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1633 TPARM_1(parm_ich, count),
1635 NCURSES_SP_NAME(_nc_outch));
1637 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1641 } else if (enter_insert_mode && exit_insert_mode) {
1642 NCURSES_PUTP2("enter_insert_mode", enter_insert_mode);
1644 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1645 if (insert_padding) {
1646 NCURSES_PUTP2("insert_padding", insert_padding);
1651 NCURSES_PUTP2("exit_insert_mode", exit_insert_mode);
1654 NCURSES_PUTP2("insert_character", insert_character);
1655 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1656 if (insert_padding) {
1657 NCURSES_PUTP2("insert_padding", insert_padding);
1663 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "InsStr");
1669 ** Delete count characters at current position
1674 DelChar(NCURSES_SP_DCLx int count)
1678 TR(TRACE_UPDATE, ("DelChar(%p, %d) called, position = (%ld,%ld)",
1679 (void *) SP_PARM, count,
1680 (long) NewScreen(SP_PARM)->_cury,
1681 (long) NewScreen(SP_PARM)->_curx));
1684 TPUTS_TRACE("parm_dch");
1685 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1686 TPARM_1(parm_dch, count),
1688 NCURSES_SP_NAME(_nc_outch));
1690 for (n = 0; n < count; n++) {
1691 NCURSES_PUTP2("delete_character", delete_character);
1697 * Physical-scrolling support
1699 * This code was adapted from Keith Bostic's hardware scrolling
1700 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1701 * capabilities, narrowed the call interface slightly, and cleaned
1702 * up some convoluted tests. I also added support for the memory_above
1703 * memory_below, and non_dest_scroll_region capabilities.
1705 * For this code to work, we must have either
1706 * change_scroll_region and scroll forward/reverse commands, or
1707 * insert and delete line capabilities.
1708 * When the scrolling region has been set, the cursor has to
1709 * be at the last line of the region to make the scroll up
1710 * happen, or on the first line of region to scroll down.
1712 * This code makes one aesthetic decision in the opposite way from
1713 * BSD curses. BSD curses preferred pairs of il/dl operations
1714 * over scrolls, allegedly because il/dl looked faster. We, on
1715 * the other hand, prefer scrolls because (a) they're just as fast
1716 * on many terminals and (b) using them avoids bouncing an
1717 * unchanged bottom section of the screen up and down, which is
1720 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1722 * I used assumption that capabilities il/il1/dl/dl1 work inside
1723 * changed scroll region not shifting screen contents outside of it.
1724 * If there are any terminals behaving different way, it would be
1725 * necessary to add some conditions to scroll_csr_forward/backward.
1728 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1730 scroll_csr_forward(NCURSES_SP_DCLx
1740 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1741 GoTo(NCURSES_SP_ARGx bot, 0);
1742 UpdateAttrs(SP_PARM, blank);
1743 NCURSES_PUTP2("scroll_forward", scroll_forward);
1744 } else if (n == 1 && delete_line && bot == maxy) {
1745 GoTo(NCURSES_SP_ARGx top, 0);
1746 UpdateAttrs(SP_PARM, blank);
1747 NCURSES_PUTP2("delete_line", delete_line);
1748 } else if (parm_index && top == miny && bot == maxy) {
1749 GoTo(NCURSES_SP_ARGx bot, 0);
1750 UpdateAttrs(SP_PARM, blank);
1751 TPUTS_TRACE("parm_index");
1752 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1753 TPARM_2(parm_index, n, 0),
1755 NCURSES_SP_NAME(_nc_outch));
1756 } else if (parm_delete_line && bot == maxy) {
1757 GoTo(NCURSES_SP_ARGx top, 0);
1758 UpdateAttrs(SP_PARM, blank);
1759 TPUTS_TRACE("parm_delete_line");
1760 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1761 TPARM_2(parm_delete_line, n, 0),
1763 NCURSES_SP_NAME(_nc_outch));
1764 } else if (scroll_forward && top == miny && bot == maxy) {
1765 GoTo(NCURSES_SP_ARGx bot, 0);
1766 UpdateAttrs(SP_PARM, blank);
1767 for (i = 0; i < n; i++) {
1768 NCURSES_PUTP2("scroll_forward", scroll_forward);
1770 } else if (delete_line && bot == maxy) {
1771 GoTo(NCURSES_SP_ARGx top, 0);
1772 UpdateAttrs(SP_PARM, blank);
1773 for (i = 0; i < n; i++) {
1774 NCURSES_PUTP2("delete_line", delete_line);
1779 #if NCURSES_EXT_FUNCS
1780 if (FILL_BCE(SP_PARM)) {
1782 for (i = 0; i < n; i++) {
1783 GoTo(NCURSES_SP_ARGx bot - i, 0);
1784 for (j = 0; j < screen_columns(SP_PARM); j++)
1785 PutChar(NCURSES_SP_ARGx CHREF(blank));
1792 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1795 scroll_csr_backward(NCURSES_SP_DCLx
1805 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1806 GoTo(NCURSES_SP_ARGx top, 0);
1807 UpdateAttrs(SP_PARM, blank);
1808 NCURSES_PUTP2("scroll_reverse", scroll_reverse);
1809 } else if (n == 1 && insert_line && bot == maxy) {
1810 GoTo(NCURSES_SP_ARGx top, 0);
1811 UpdateAttrs(SP_PARM, blank);
1812 NCURSES_PUTP2("insert_line", insert_line);
1813 } else if (parm_rindex && top == miny && bot == maxy) {
1814 GoTo(NCURSES_SP_ARGx top, 0);
1815 UpdateAttrs(SP_PARM, blank);
1816 TPUTS_TRACE("parm_rindex");
1817 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1818 TPARM_2(parm_rindex, n, 0),
1820 NCURSES_SP_NAME(_nc_outch));
1821 } else if (parm_insert_line && bot == maxy) {
1822 GoTo(NCURSES_SP_ARGx top, 0);
1823 UpdateAttrs(SP_PARM, blank);
1824 TPUTS_TRACE("parm_insert_line");
1825 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1826 TPARM_2(parm_insert_line, n, 0),
1828 NCURSES_SP_NAME(_nc_outch));
1829 } else if (scroll_reverse && top == miny && bot == maxy) {
1830 GoTo(NCURSES_SP_ARGx top, 0);
1831 UpdateAttrs(SP_PARM, blank);
1832 for (i = 0; i < n; i++) {
1833 NCURSES_PUTP2("scroll_reverse", scroll_reverse);
1835 } else if (insert_line && bot == maxy) {
1836 GoTo(NCURSES_SP_ARGx top, 0);
1837 UpdateAttrs(SP_PARM, blank);
1838 for (i = 0; i < n; i++) {
1839 NCURSES_PUTP2("insert_line", insert_line);
1844 #if NCURSES_EXT_FUNCS
1845 if (FILL_BCE(SP_PARM)) {
1847 for (i = 0; i < n; i++) {
1848 GoTo(NCURSES_SP_ARGx top + i, 0);
1849 for (j = 0; j < screen_columns(SP_PARM); j++)
1850 PutChar(NCURSES_SP_ARGx CHREF(blank));
1857 /* scroll by using delete_line at del and insert_line at ins */
1860 scroll_idl(NCURSES_SP_DCLx int n, int del, int ins, NCURSES_CH_T blank)
1864 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1867 GoTo(NCURSES_SP_ARGx del, 0);
1868 UpdateAttrs(SP_PARM, blank);
1869 if (n == 1 && delete_line) {
1870 NCURSES_PUTP2("delete_line", delete_line);
1871 } else if (parm_delete_line) {
1872 TPUTS_TRACE("parm_delete_line");
1873 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1874 TPARM_2(parm_delete_line, n, 0),
1876 NCURSES_SP_NAME(_nc_outch));
1877 } else { /* if (delete_line) */
1878 for (i = 0; i < n; i++) {
1879 NCURSES_PUTP2("delete_line", delete_line);
1883 GoTo(NCURSES_SP_ARGx ins, 0);
1884 UpdateAttrs(SP_PARM, blank);
1885 if (n == 1 && insert_line) {
1886 NCURSES_PUTP2("insert_line", insert_line);
1887 } else if (parm_insert_line) {
1888 TPUTS_TRACE("parm_insert_line");
1889 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1890 TPARM_2(parm_insert_line, n, 0),
1892 NCURSES_SP_NAME(_nc_outch));
1893 } else { /* if (insert_line) */
1894 for (i = 0; i < n; i++) {
1895 NCURSES_PUTP2("insert_line", insert_line);
1903 * Note: some terminals require the cursor to be within the scrolling margins
1904 * before setting them. Generally, the cursor must be at the appropriate end
1905 * of the scrolling margins when issuing an indexing operation (it is not
1906 * apparent whether it must also be at the left margin; we do this just to be
1907 * safe). To make the related cursor movement a little faster, we use the
1908 * save/restore cursor capabilities if the terminal has them.
1911 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
1916 /* scroll region from top to bot by n lines */
1920 bool cursor_saved = FALSE;
1923 TR(TRACE_MOVE, ("_nc_scrolln(%p, %d, %d, %d, %d)",
1924 (void *) SP_PARM, n, top, bot, maxy));
1926 if (!IsValidScreen(SP_PARM))
1929 blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1933 * If we scroll, we might remove a cookie.
1935 if (magic_cookie_glitch > 0) {
1940 if (n > 0) { /* scroll up (forward) */
1942 * Explicitly clear if stuff pushed off top of region might
1943 * be saved by the terminal.
1945 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
1947 if (res == ERR && change_scroll_region) {
1948 if ((((n == 1 && scroll_forward) || parm_index)
1949 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
1950 && save_cursor && restore_cursor) {
1951 cursor_saved = TRUE;
1952 NCURSES_PUTP2("save_cursor", save_cursor);
1954 NCURSES_PUTP2("change_scroll_region",
1955 TPARM_2(change_scroll_region, top, bot));
1957 NCURSES_PUTP2("restore_cursor", restore_cursor);
1959 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1962 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
1964 NCURSES_PUTP2("change_scroll_region",
1965 TPARM_2(change_scroll_region, 0, maxy));
1966 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1969 if (res == ERR && SP_PARM->_nc_sp_idlok)
1970 res = scroll_idl(NCURSES_SP_ARGx n, top, bot - n + 1, blank);
1973 * Clear the newly shifted-in text.
1976 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
1977 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1978 if (bot == maxy && clr_eos) {
1979 GoTo(NCURSES_SP_ARGx bot - n + 1, 0);
1980 ClrToEOS(NCURSES_SP_ARGx blank2);
1982 for (i = 0; i < n; i++) {
1983 GoTo(NCURSES_SP_ARGx bot - i, 0);
1984 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
1989 } else { /* (n < 0) - scroll down (backward) */
1990 res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
1992 if (res == ERR && change_scroll_region) {
1994 && (SP_PARM->_cursrow == top ||
1995 SP_PARM->_cursrow == top - 1)
1996 && save_cursor && restore_cursor) {
1997 cursor_saved = TRUE;
1998 NCURSES_PUTP2("save_cursor", save_cursor);
2000 NCURSES_PUTP2("change_scroll_region",
2001 TPARM_2(change_scroll_region, top, bot));
2003 NCURSES_PUTP2("restore_cursor", restore_cursor);
2005 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2008 res = scroll_csr_backward(NCURSES_SP_ARGx
2009 -n, top, bot, top, bot, blank);
2011 NCURSES_PUTP2("change_scroll_region",
2012 TPARM_2(change_scroll_region, 0, maxy));
2013 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2016 if (res == ERR && SP_PARM->_nc_sp_idlok)
2017 res = scroll_idl(NCURSES_SP_ARGx -n, bot + n + 1, top, blank);
2020 * Clear the newly shifted-in text.
2023 && (non_dest_scroll_region || (memory_above && top == 0))) {
2024 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2025 for (i = 0; i < -n; i++) {
2026 GoTo(NCURSES_SP_ARGx i + top, 0);
2027 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2035 _nc_scroll_window(CurScreen(SP_PARM), n,
2036 (NCURSES_SIZE_T) top,
2037 (NCURSES_SIZE_T) bot,
2040 /* shift hash values too - they can be reused */
2041 NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_ARGx n, top, bot);
2046 #if NCURSES_SP_FUNCS
2048 _nc_scrolln(int n, int top, int bot, int maxy)
2050 return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
2054 NCURSES_EXPORT(void)
2055 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_DCL0)
2059 /* make sure terminal is in a sane known state */
2060 SetAttr(SCREEN_ATTRS(SP_PARM), A_NORMAL);
2061 NewScreen(SP_PARM)->_clear = TRUE;
2063 /* reset color pairs and definitions */
2064 if (SP_PARM->_coloron || SP_PARM->_color_defs)
2065 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2067 /* restore user-defined colors, if any */
2068 if (SP_PARM->_color_defs < 0) {
2070 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
2071 for (n = 0; n < SP_PARM->_color_defs; ++n) {
2072 if (SP_PARM->_color_table[n].init) {
2073 NCURSES_SP_NAME(init_color) (NCURSES_SP_ARGx
2075 SP_PARM->_color_table[n].r,
2076 SP_PARM->_color_table[n].g,
2077 SP_PARM->_color_table[n].b);
2082 if (exit_attribute_mode)
2083 NCURSES_PUTP2("exit_attribute_mode", exit_attribute_mode);
2085 /* turn off attributes */
2086 if (exit_alt_charset_mode)
2087 NCURSES_PUTP2("exit_alt_charset_mode", exit_alt_charset_mode);
2088 if (exit_standout_mode)
2089 NCURSES_PUTP2("exit_standout_mode", exit_standout_mode);
2090 if (exit_underline_mode)
2091 NCURSES_PUTP2("exit_underline_mode", exit_underline_mode);
2093 if (exit_insert_mode)
2094 NCURSES_PUTP2("exit_insert_mode", exit_insert_mode);
2095 if (enter_am_mode && exit_am_mode) {
2096 if (auto_right_margin) {
2097 NCURSES_PUTP2("enter_am_mode", enter_am_mode);
2099 NCURSES_PUTP2("exit_am_mode", exit_am_mode);
2104 #if NCURSES_SP_FUNCS
2105 NCURSES_EXPORT(void)
2106 _nc_screen_resume(void)
2108 NCURSES_SP_NAME(_nc_screen_resume) (CURRENT_SCREEN);
2112 NCURSES_EXPORT(void)
2113 NCURSES_SP_NAME(_nc_screen_init) (NCURSES_SP_DCL0)
2115 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
2118 #if NCURSES_SP_FUNCS
2119 NCURSES_EXPORT(void)
2120 _nc_screen_init(void)
2122 NCURSES_SP_NAME(_nc_screen_init) (CURRENT_SCREEN);
2126 /* wrap up screen handling */
2127 NCURSES_EXPORT(void)
2128 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2132 UpdateAttrs(SP_PARM, normal);
2133 #if NCURSES_EXT_FUNCS
2134 if (SP_PARM->_coloron
2135 && !SP_PARM->_default_color) {
2136 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2137 SP_PARM->_default_color = TRUE;
2138 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
2142 NCURSES_SP_NAME(_nc_outch));
2143 SP_PARM->_default_color = FALSE;
2145 TINFO_MVCUR(NCURSES_SP_ARGx
2148 screen_lines(SP_PARM) - 1,
2151 ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2154 if (SP_PARM->_color_defs) {
2155 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2160 #if NCURSES_SP_FUNCS
2161 NCURSES_EXPORT(void)
2162 _nc_screen_wrap(void)
2164 NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2169 NCURSES_EXPORT(void)
2170 NCURSES_SP_NAME(_nc_do_xmc_glitch) (NCURSES_SP_DCLx attr_t previous)
2173 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP_PARM)));
2177 SP_PARM->_curscol += magic_cookie_glitch;
2178 if (SP_PARM->_curscol >= SP_PARM->_columns)
2179 wrap_cursor(NCURSES_SP_ARG);
2180 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie",
2181 SP_PARM->_cursrow, SP_PARM->_curscol));
2188 #if NCURSES_SP_FUNCS
2189 NCURSES_EXPORT(void)
2190 _nc_do_xmc_glitch(attr_t previous)
2192 NCURSES_SP_NAME(_nc_do_xmc_glitch) (CURRENT_SCREEN, previous);
2196 #endif /* USE_XMC_SUPPORT */