1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 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.272 2012/12/15 21:00:19 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_SP_NAME(putp) (NCURSES_SP_ARGx "\033[6n"); /* only works on ANSI-compatibles */
149 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
152 int ask = sizeof(buf) - 1 - (s - buf);
153 int got = read(0, s, ask);
157 } while (strchr(buf, 'R') == 0);
158 _tracef("probe returned %s", _nc_visbuf(buf));
160 /* try to interpret as a position report */
161 if (sscanf(buf, "\033[%d;%dR", &y, &x) != 2) {
162 _tracef("position probe failed in %s", legend);
168 if (y - 1 != expected_y || x - 1 != expected_x) {
169 NCURSES_SP_NAME(beep) (NCURSES_SP_ARG);
170 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
174 1, NCURSES_SP_NAME(_nc_outch));
175 _tracef("position seen (%d, %d) doesn't match expected one (%d, %d) in %s",
176 y - 1, x - 1, expected_y, expected_x, legend);
178 _tracef("position matches OK in %s", legend);
183 #define position_check(sp, expected_y, expected_x, legend) /* nothing */
184 #endif /* POSITION_DEBUG */
186 /****************************************************************************
188 * Optimized update code
190 ****************************************************************************/
192 static NCURSES_INLINE void
193 GoTo(NCURSES_SP_DCLx int const row, int const col)
195 TR(TRACE_MOVE, ("GoTo(%p, %d, %d) from (%d, %d)",
196 (void *) SP_PARM, row, col, SP_PARM->_cursrow, SP_PARM->_curscol));
198 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "GoTo");
200 TINFO_MVCUR(NCURSES_SP_ARGx
204 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "GoTo2");
207 static NCURSES_INLINE void
208 PutAttrChar(NCURSES_SP_DCLx CARG_CH_T ch)
212 #if USE_WIDEC_SUPPORT
216 NCURSES_CH_T attr = CHDEREF(ch);
218 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
220 SP_PARM->_cursrow, SP_PARM->_curscol));
221 #if USE_WIDEC_SUPPORT
223 * If this is not a valid character, there is nothing more to do.
225 if (isWidecExt(CHDEREF(ch))) {
226 TR(TRACE_CHARPUT, ("...skip"));
230 * Determine the number of character cells which the 'ch' value will use
231 * on the screen. It should be at least one.
233 if ((chlen = wcwidth(CharOf(CHDEREF(ch)))) <= 0) {
234 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
237 * If the character falls into any of these special cases, do
238 * not force the result to a blank:
240 * a) it is printable (this works around a bug in wcwidth()).
241 * b) use_legacy_coding() has been called to modify the treatment
243 * c) the acs_map[] has been initialized to allow codes 0-31
244 * to be rendered. This supports Linux console's "PC"
245 * characters. Codes 128-255 are allowed though this is
248 if (is8bits(CharOf(CHDEREF(ch)))
249 && (isprint(CharOf(CHDEREF(ch)))
250 || (SP_PARM->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
251 || (SP_PARM->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
252 || (AttrOf(attr) & A_ALTCHARSET
253 && ((CharOfD(ch) < ACS_LEN
254 && SP_PARM->_acs_map != 0
255 && SP_PARM->_acs_map[CharOfD(ch)] != 0)
256 || (CharOfD(ch) >= 128))))) {
260 TR(TRACE_CHARPUT, ("forced to blank"));
266 if ((AttrOf(attr) & A_ALTCHARSET)
267 && SP_PARM->_acs_map != 0
268 && CharOfD(ch) < ACS_LEN) {
269 my_ch = CHDEREF(ch); /* work around const param */
270 #if USE_WIDEC_SUPPORT
272 * This is crude & ugly, but works most of the time. It checks if the
273 * acs_chars string specified that we have a mapping for this
274 * character, and uses the wide-character mapping when we expect the
275 * normal one to be broken (by mis-design ;-).
277 if (SP_PARM->_screen_acs_fix
278 && SP_PARM->_screen_acs_map[CharOf(my_ch)]) {
279 RemAttr(attr, A_ALTCHARSET);
280 my_ch = _nc_wacs[CharOf(my_ch)];
284 * If we (still) have alternate character set, it is the normal 8bit
285 * flavor. The _screen_acs_map[] array tells if the character was
286 * really in acs_chars, needed because of the way wide/normal line
287 * drawing flavors are integrated.
289 if (AttrOf(attr) & A_ALTCHARSET) {
291 chtype temp = UChar(SP_PARM->_acs_map[j]);
294 SetChar(my_ch, temp, AttrOf(attr));
297 RemAttr(attr, A_ALTCHARSET);
302 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
303 SetChar(tilde, L('`'), AttrOf(attr));
307 UpdateAttrs(SP_PARM, attr);
309 #if !USE_WIDEC_SUPPORT
312 SP_PARM->_curscol += chlen;
314 TPUTS_TRACE("char_padding");
315 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx char_padding);
320 check_pending(NCURSES_SP_DCL0)
321 /* check for pending input */
323 bool have_pending = FALSE;
326 * Only carry out this check when the flag is zero, otherwise we'll
327 * have the refreshing slow down drastically (or stop) if there's an
328 * unread character available.
330 if (SP_PARM->_fifohold != 0)
333 if (SP_PARM->_checkfd >= 0) {
335 struct pollfd fds[1];
336 fds[0].fd = SP_PARM->_checkfd;
337 fds[0].events = POLLIN;
338 if (poll(fds, (size_t) 1, 0) > 0) {
341 #elif defined(__BEOS__)
343 * BeOS's select() is declared in socket.h, so the configure script does
344 * not see it. That's just as well, since that function works only for
345 * sockets. This (using snooze and ioctl) was distilled from Be's patch
346 * for ncurses which uses a separate thread to simulate select().
348 * FIXME: the return values from the ioctl aren't very clear if we get
352 int howmany = ioctl(0, 'ichr', &n);
353 if (howmany >= 0 && n > 0) {
358 struct timeval ktimeout;
361 ktimeout.tv_usec = 0;
364 FD_SET(SP_PARM->_checkfd, &fdset);
365 if (select(SP_PARM->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
371 SP_PARM->_fifohold = 5;
372 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
377 /* put char at lower right corner */
379 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
381 if (!auto_right_margin) {
382 /* we can put the char directly */
383 PutAttrChar(NCURSES_SP_ARGx ch);
384 } else if (enter_am_mode && exit_am_mode) {
385 /* we can suppress automargin */
386 TPUTS_TRACE("exit_am_mode");
387 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_am_mode);
389 PutAttrChar(NCURSES_SP_ARGx ch);
391 position_check(SP_PARM,
396 TPUTS_TRACE("enter_am_mode");
397 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_am_mode);
398 } else if ((enter_insert_mode && exit_insert_mode)
399 || insert_character || parm_ich) {
401 screen_lines(SP_PARM) - 1,
402 screen_columns(SP_PARM) - 2);
403 PutAttrChar(NCURSES_SP_ARGx ch);
405 screen_lines(SP_PARM) - 1,
406 screen_columns(SP_PARM) - 2);
407 InsStr(NCURSES_SP_ARGx
408 NewScreen(SP_PARM)->_line[screen_lines(SP_PARM) - 1].text +
409 screen_columns(SP_PARM) - 2, 1);
414 * Wrap the cursor position, i.e., advance to the beginning of the next line.
417 wrap_cursor(NCURSES_SP_DCL0)
419 if (eat_newline_glitch) {
421 * xenl can manifest two different ways. The vt100 way is that, when
422 * you'd expect the cursor to wrap, it stays hung at the right margin
423 * (on top of the character just emitted) and doesn't wrap until the
424 * *next* graphic char is emitted. The c100 way is to ignore LF
425 * received just after an am wrap.
427 * An aggressive way to handle this would be to emit CR/LF after the
428 * char and then assume the wrap is done, you're on the first position
429 * of the next line, and the terminal out of its weird state. Here
430 * it's safe to just tell the code that the cursor is in hyperspace and
431 * let the next mvcur() call straighten things out.
433 SP_PARM->_curscol = -1;
434 SP_PARM->_cursrow = -1;
435 } else if (auto_right_margin) {
436 SP_PARM->_curscol = 0;
439 * We've actually moved - but may have to work around problems with
440 * video attributes not working.
442 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP_PARM))) {
443 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
444 (unsigned long) AttrOf(SCREEN_ATTRS(SP_PARM)),
445 _traceattr(AttrOf(SCREEN_ATTRS(SP_PARM)))));
446 (void) VIDATTR(SP_PARM, A_NORMAL, 0);
451 position_check(SP_PARM,
457 static NCURSES_INLINE void
458 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
459 /* insert character, handling automargin stuff */
461 if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1 &&
462 SP_PARM->_curscol == screen_columns(SP_PARM) - 1) {
463 PutCharLR(NCURSES_SP_ARGx ch);
465 PutAttrChar(NCURSES_SP_ARGx ch);
468 if (SP_PARM->_curscol >= screen_columns(SP_PARM))
469 wrap_cursor(NCURSES_SP_ARG);
471 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "PutChar");
475 * Check whether the given character can be output by clearing commands. This
476 * includes test for being a space and not including any 'bad' attributes, such
477 * as A_REVERSE. All attribute flags which don't affect appearance of a space
478 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
480 static NCURSES_INLINE bool
481 can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)
483 if (!back_color_erase && SP_PARM->_coloron) {
484 #if NCURSES_EXT_FUNCS
487 if (!SP_PARM->_default_color)
489 if (SP_PARM->_default_fg != C_MASK || SP_PARM->_default_bg != C_MASK)
491 if ((pair = GetPair(CHDEREF(ch))) != 0) {
493 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
496 if (fg != C_MASK || bg != C_MASK)
500 if (AttrOfD(ch) & A_COLOR)
504 return (ISBLANK(CHDEREF(ch)) &&
505 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
509 * Issue a given span of characters from an array.
510 * Must be functionally equivalent to:
511 * for (i = 0; i < num; i++)
513 * but can leave the cursor positioned at the middle of the interval.
515 * Returns: 0 - cursor is at the end of interval
516 * 1 - cursor is somewhere in the middle
518 * This code is optimized using ech and rep.
521 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext, int num)
525 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
527 if (erase_chars || repeat_char) {
532 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
533 PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
539 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
544 while (runcount < num && CharEq(ntext[runcount], ntext0))
548 * The cost expression in the middle isn't exactly right.
549 * _cup_ch_cost is an upper bound on the cost for moving to the
550 * end of the erased area, but not the cost itself (which we
551 * can't compute without emitting the move). This may result
552 * in erase_chars not getting used in some situations for
553 * which it would be marginally advantageous.
556 && runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
557 && can_clear_with(NCURSES_SP_ARGx CHREF(ntext0))) {
558 UpdateAttrs(SP_PARM, ntext0);
559 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
560 TPARM_1(erase_chars, runcount));
563 * If this is the last part of the given interval,
564 * don't bother moving cursor, since it can be the
565 * last update on the line.
567 if (runcount < num) {
570 SP_PARM->_curscol + runcount);
572 return 1; /* cursor stays in the middle */
574 } else if (repeat_char && runcount > SP_PARM->_rep_cost) {
575 bool wrap_possible = (SP_PARM->_curscol + runcount >=
576 screen_columns(SP_PARM));
577 int rep_count = runcount;
582 UpdateAttrs(SP_PARM, ntext0);
583 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
588 NCURSES_SP_NAME(_nc_outch));
589 SP_PARM->_curscol += rep_count;
592 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
594 for (i = 0; i < runcount; i++)
595 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
603 for (i = 0; i < num; i++)
604 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
609 * Output the line in the given range [first .. last]
611 * If there's a run of identical characters that's long enough to justify
612 * cursor movement, use that also.
614 * Returns: same as EmitRange
617 PutRange(NCURSES_SP_DCLx
618 const NCURSES_CH_T * otext,
619 const NCURSES_CH_T * ntext,
625 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %p, %d, %d, %d)",
627 (const void *) otext,
628 (const void *) ntext,
632 && (last - first + 1) > SP_PARM->_inline_cost) {
633 for (j = first, same = 0; j <= last; j++) {
634 if (!same && isWidecExt(otext[j]))
636 if (CharEq(otext[j], ntext[j])) {
639 if (same > SP_PARM->_inline_cost) {
640 EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
641 GoTo(NCURSES_SP_ARGx row, first = j);
646 i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
648 * Always return 1 for the next GoTo() after a PutRange() if we found
649 * identical characters at end of interval
651 return (same == 0 ? i : 1);
653 return EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
656 /* leave unbracketed here so 'indent' works */
657 #define MARK_NOCHANGE(win,row) \
658 win->_line[row].firstchar = _NOCHANGE; \
659 win->_line[row].lastchar = _NOCHANGE; \
660 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
663 TINFO_DOUPDATE(NCURSES_SP_DCL0)
668 struct tms before, after;
669 #endif /* USE_TRACE_TIMES */
671 T((T_CALLED("_nc_tinfo:doupdate(%p)"), (void *) SP_PARM));
678 * It is "legal" but unlikely that an application could assign a new
679 * value to one of the standard windows. Check for that possibility
680 * and try to recover.
682 * We do not allow applications to assign new values in the reentrant
685 #define SyncScreens(internal,exported) \
686 if (internal == 0) internal = exported; \
687 if (internal != exported) exported = internal
689 SyncScreens(CurScreen(SP_PARM), curscr);
690 SyncScreens(NewScreen(SP_PARM), newscr);
691 SyncScreens(StdScreen(SP_PARM), stdscr);
694 if (CurScreen(SP_PARM) == 0
695 || NewScreen(SP_PARM) == 0
696 || StdScreen(SP_PARM) == 0)
700 if (USE_TRACEF(TRACE_UPDATE)) {
701 if (CurScreen(SP_PARM)->_clear)
702 _tracef("curscr is clear");
704 _tracedump("curscr", CurScreen(SP_PARM));
705 _tracedump("newscr", NewScreen(SP_PARM));
706 _nc_unlock_global(tracef);
710 _nc_signal_handler(FALSE);
712 if (SP_PARM->_fifohold)
713 SP_PARM->_fifohold--;
716 if (SP_PARM->_endwin || _nc_handle_sigwinch(SP_PARM)) {
718 * This is a transparent extension: XSI does not address it,
719 * and applications need not know that ncurses can do it.
721 * Check if the terminal size has changed while curses was off
722 * (this can happen in an xterm, for example), and resize the
723 * ncurses data structures accordingly.
725 _nc_update_screensize(SP_PARM);
729 if (SP_PARM->_endwin) {
731 T(("coming back from shell mode"));
732 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_ARG);
734 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG);
735 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
736 SP_PARM->_mouse_resume(SP_PARM);
738 SP_PARM->_endwin = FALSE;
741 /* zero the metering machinery */
743 (void) times(&before);
744 #endif /* USE_TRACE_TIMES */
747 * This is the support for magic-cookie terminals. The theory: we scan
748 * the virtual screen looking for attribute turnons. Where we find one,
749 * check to make sure it's realizable by seeing if the required number of
750 * un-attributed blanks are present before and after the attributed range;
751 * try to shift the range boundaries over blanks (not changing the screen
752 * display) so this becomes true. If it is, shift the beginning attribute
753 * change appropriately (the end one, if we've gotten this far, is
754 * guaranteed room for its cookie). If not, nuke the added attributes out
758 if (magic_cookie_glitch > 0) {
760 attr_t rattr = A_NORMAL;
762 for (i = 0; i < screen_lines(SP_PARM); i++) {
763 for (j = 0; j < screen_columns(SP_PARM); j++) {
765 NCURSES_CH_T *thisline = NewScreen(SP_PARM)->_line[i].text;
766 attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
767 attr_t turnon = thisattr & ~rattr;
769 /* is an attribute turned on here? */
775 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
776 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
779 * If the attribute change location is a blank with a "safe"
780 * attribute, undo the attribute turnon. This may ensure
781 * there's enough room to set the attribute before the first
782 * non-blank in the run.
784 #define SAFE(scr,a) (!((a) & (scr)->_xmc_triggers))
785 if (ISBLANK(thisline[j]) && SAFE(SP_PARM, turnon)) {
786 RemAttr(thisline[j], turnon);
790 /* check that there's enough room at start of span */
791 for (k = 1; k <= magic_cookie_glitch; k++) {
793 || !ISBLANK(thisline[j - k])
794 || !SAFE(SP_PARM, AttrOf(thisline[j - k]))) {
796 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
798 (ISBLANK(thisline[j - k])
801 (SAFE(SP_PARM, AttrOf(thisline[j - k]))
808 bool end_onscreen = FALSE;
811 /* find end of span, if it's onscreen */
812 for (m = i; m < screen_lines(SP_PARM); m++) {
813 for (; n < screen_columns(SP_PARM); n++) {
815 AttrOf(NewScreen(SP_PARM)->_line[m].text[n]);
816 if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
819 ("Range attributed with %s ends at (%d, %d)",
820 _traceattr(turnon), m, n));
827 ("Range attributed with %s ends offscreen",
828 _traceattr(turnon)));
832 NCURSES_CH_T *lastline =
833 NewScreen(SP_PARM)->_line[m].text;
836 * If there are safely-attributed blanks at the end of
837 * the range, shorten the range. This will help ensure
838 * that there is enough room at end of span.
841 && ISBLANK(lastline[n])
842 && SAFE(SP_PARM, AttrOf(lastline[n]))) {
843 RemAttr(lastline[n--], turnon);
846 /* check that there's enough room at end of span */
847 for (k = 1; k <= magic_cookie_glitch; k++) {
848 if (n + k >= screen_columns(SP_PARM)
849 || !ISBLANK(lastline[n + k])
850 || !SAFE(SP_PARM, AttrOf(lastline[n + k]))) {
853 ("No room at end in %d,%d%s%s",
855 (ISBLANK(lastline[n + k])
858 (SAFE(SP_PARM, AttrOf(lastline[n + k]))
871 ("Clearing %s beginning at (%d, %d)",
872 _traceattr(turnon), i, j));
874 /* turn off new attributes over span */
875 for (p = i; p < screen_lines(SP_PARM); p++) {
876 for (; q < screen_columns(SP_PARM); q++) {
877 attr_t testattr = AttrOf(newscr->_line[p].text[q]);
878 if ((testattr & SP_PARM->_xmc_triggers) == rattr)
880 RemAttr(NewScreen(SP_PARM)->_line[p].text[q], turnon);
887 ("Cookie space for %s found before (%d, %d)",
888 _traceattr(turnon), i, j));
891 * Back up the start of range so there's room for cookies
892 * before the first nonblank character.
894 for (k = 1; k <= magic_cookie_glitch; k++)
895 AddAttr(thisline[j - k], turnon);
903 /* show altered highlights after magic-cookie check */
904 if (USE_TRACEF(TRACE_UPDATE)) {
905 _tracef("After magic-cookie check...");
906 _tracedump("newscr", NewScreen(SP_PARM));
907 _nc_unlock_global(tracef);
911 #endif /* USE_XMC_SUPPORT */
914 if (CurScreen(SP_PARM)->_clear || NewScreen(SP_PARM)->_clear) { /* force refresh ? */
915 ClrUpdate(NCURSES_SP_ARG);
916 CurScreen(SP_PARM)->_clear = FALSE; /* reset flag */
917 NewScreen(SP_PARM)->_clear = FALSE; /* reset flag */
919 int changedlines = CHECK_INTERVAL;
921 if (check_pending(NCURSES_SP_ARG))
924 nonempty = min(screen_lines(SP_PARM), NewScreen(SP_PARM)->_maxy + 1);
926 if (SP_PARM->_scrolling) {
927 NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_ARG);
930 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
932 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
933 for (i = 0; i < nonempty; i++) {
935 * Here is our line-breakout optimization.
937 if (changedlines == CHECK_INTERVAL) {
938 if (check_pending(NCURSES_SP_ARG))
944 * newscr->line[i].firstchar is normally set
945 * by wnoutrefresh. curscr->line[i].firstchar
946 * is normally set by _nc_scroll_window in the
947 * vertical-movement optimization code,
949 if (NewScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE
950 || CurScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE) {
951 TransformLine(NCURSES_SP_ARGx i);
955 /* mark line changed successfully */
956 if (i <= NewScreen(SP_PARM)->_maxy) {
957 MARK_NOCHANGE(NewScreen(SP_PARM), i);
959 if (i <= CurScreen(SP_PARM)->_maxy) {
960 MARK_NOCHANGE(CurScreen(SP_PARM), i);
965 /* put everything back in sync */
966 for (i = nonempty; i <= NewScreen(SP_PARM)->_maxy; i++) {
967 MARK_NOCHANGE(NewScreen(SP_PARM), i);
969 for (i = nonempty; i <= CurScreen(SP_PARM)->_maxy; i++) {
970 MARK_NOCHANGE(CurScreen(SP_PARM), i);
973 if (!NewScreen(SP_PARM)->_leaveok) {
974 CurScreen(SP_PARM)->_curx = NewScreen(SP_PARM)->_curx;
975 CurScreen(SP_PARM)->_cury = NewScreen(SP_PARM)->_cury;
977 GoTo(NCURSES_SP_ARGx CurScreen(SP_PARM)->_cury, CurScreen(SP_PARM)->_curx);
982 * We would like to keep the physical screen in normal mode in case we get
983 * other processes writing to the screen. This goal cannot be met for
984 * magic cookies since it interferes with attributes that may propagate
985 * past the current position.
988 if (magic_cookie_glitch != 0)
990 UpdateAttrs(SP_PARM, normal);
992 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
993 WINDOW_ATTRS(CurScreen(SP_PARM)) = WINDOW_ATTRS(NewScreen(SP_PARM));
996 (void) times(&after);
998 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
1000 (long) (after.tms_stime - before.tms_stime),
1001 (long) (after.tms_utime - before.tms_utime)));
1002 #endif /* USE_TRACE_TIMES */
1004 _nc_signal_handler(TRUE);
1009 #if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)
1013 return TINFO_DOUPDATE(CURRENT_SCREEN);
1020 * Returns the attributed character that corresponds to the "cleared"
1021 * screen. If the terminal has the back-color-erase feature, this will be
1022 * colored according to the wbkgd() call.
1024 * We treat 'curscr' specially because it isn't supposed to be set directly
1025 * in the wbkgd() call. Assume 'stdscr' for this case.
1027 #define BCE_ATTRS (A_NORMAL|A_COLOR)
1028 #define BCE_BKGD(sp,win) (((win) == CurScreen(sp) ? StdScreen(sp) : (win))->_nc_bkgd)
1030 static NCURSES_INLINE NCURSES_CH_T
1031 ClrBlank(NCURSES_SP_DCLx WINDOW *win)
1033 NCURSES_CH_T blank = blankchar;
1034 if (back_color_erase)
1035 AddAttr(blank, (AttrOf(BCE_BKGD(SP_PARM, win)) & BCE_ATTRS));
1042 ** Update by clearing and redrawing the entire screen.
1047 ClrUpdate(NCURSES_SP_DCL0)
1049 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1052 NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1053 int nonempty = min(screen_lines(SP_PARM),
1054 NewScreen(SP_PARM)->_maxy + 1);
1056 ClearScreen(NCURSES_SP_ARGx blank);
1058 TR(TRACE_UPDATE, ("updating screen from scratch"));
1060 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1062 for (i = 0; i < nonempty; i++)
1063 TransformLine(NCURSES_SP_ARGx i);
1065 TR(TRACE_UPDATE, (T_RETURN("")));
1071 ** Clear to end of current line, starting at the cursor position
1075 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, int needclear)
1079 if (CurScreen(SP_PARM) != 0
1080 && SP_PARM->_cursrow >= 0) {
1081 for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1084 &(CurScreen(SP_PARM)->_line[SP_PARM->_cursrow].text[j]);
1086 if (!CharEq(*cp, blank)) {
1095 UpdateAttrs(SP_PARM, blank);
1096 TPUTS_TRACE("clr_eol");
1097 if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1098 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx 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;
1121 UpdateAttrs(SP_PARM, blank);
1122 TPUTS_TRACE("clr_eos");
1123 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1125 screen_lines(SP_PARM) - row,
1126 NCURSES_SP_NAME(_nc_outch));
1128 while (col < screen_columns(SP_PARM))
1129 CurScreen(SP_PARM)->_line[row].text[col++] = blank;
1131 for (row++; row < screen_lines(SP_PARM); row++) {
1132 for (col = 0; col < screen_columns(SP_PARM); col++)
1133 CurScreen(SP_PARM)->_line[row].text[col] = blank;
1140 * Test if clearing the end of the screen would satisfy part of the
1141 * screen-update. Do this by scanning backwards through the lines in the
1142 * screen, checking if each is blank, and one or more are changed.
1145 ClrBottom(NCURSES_SP_DCLx int total)
1150 int last = min(screen_columns(SP_PARM), NewScreen(SP_PARM)->_maxx + 1);
1151 NCURSES_CH_T blank = NewScreen(SP_PARM)->_line[total - 1].text[last - 1];
1154 if (clr_eos && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1156 for (row = total - 1; row >= 0; row--) {
1157 for (col = 0, ok = TRUE; ok && col < last; col++) {
1158 ok = (CharEq(NewScreen(SP_PARM)->_line[row].text[col], blank));
1163 for (col = 0; ok && col < last; col++) {
1164 ok = (CharEq(CurScreen(SP_PARM)->_line[row].text[col], blank));
1170 /* don't use clr_eos for just one line if clr_eol available */
1172 GoTo(NCURSES_SP_ARGx top, 0);
1173 ClrToEOS(NCURSES_SP_ARGx blank);
1174 if (SP_PARM->oldhash && SP_PARM->newhash) {
1175 for (row = top; row < screen_lines(SP_PARM); row++)
1176 SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1184 #if USE_WIDEC_SUPPORT
1185 #define check_xmc_transition(sp, a, b) \
1186 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & (sp)->_xmc_triggers) != 0)
1187 #define xmc_turn_on(sp,a,b) check_xmc_transition(sp,&(a), &(b))
1189 #define xmc_turn_on(sp,a,b) ((((a)^(b)) & ~(a) & (sp)->_xmc_triggers) != 0)
1192 #define xmc_new(sp,r,c) NewScreen(sp)->_line[r].text[c]
1193 #define xmc_turn_off(sp,a,b) xmc_turn_on(sp,b,a)
1194 #endif /* USE_XMC_SUPPORT */
1197 ** TransformLine(lineno)
1199 ** Transform the given line in curscr to the one in newscr, using
1200 ** Insert/Delete Character if idcok && has_ic().
1202 ** firstChar = position of first different character in line
1203 ** oLastChar = position of last different character in old line
1204 ** nLastChar = position of last different character in new line
1206 ** move to firstChar
1207 ** overwrite chars up to min(oLastChar, nLastChar)
1208 ** if oLastChar < nLastChar
1209 ** insert newLine[oLastChar+1..nLastChar]
1211 ** delete oLastChar - nLastChar spaces
1215 TransformLine(NCURSES_SP_DCLx int const lineno)
1217 int firstChar, oLastChar, nLastChar;
1218 NCURSES_CH_T *newLine = NewScreen(SP_PARM)->_line[lineno].text;
1219 NCURSES_CH_T *oldLine = CurScreen(SP_PARM)->_line[lineno].text;
1221 bool attrchanged = FALSE;
1223 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%p, %d)"), (void *) SP_PARM, lineno));
1225 /* copy new hash value to old one */
1226 if (SP_PARM->oldhash && SP_PARM->newhash)
1227 SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1230 * If we have colors, there is the possibility of having two color pairs
1231 * that display as the same colors. For instance, Lynx does this. Check
1232 * for this case, and update the old line with the new line's colors when
1233 * they are equivalent.
1235 if (SP_PARM->_coloron) {
1239 for (n = 0; n < screen_columns(SP_PARM); n++) {
1240 if (!CharEq(newLine[n], oldLine[n])) {
1241 oldPair = GetPair(oldLine[n]);
1242 newPair = GetPair(newLine[n]);
1243 if (oldPair != newPair
1244 && unColor(oldLine[n]) == unColor(newLine[n])) {
1245 if (oldPair < SP_PARM->_pair_limit
1246 && newPair < SP_PARM->_pair_limit
1247 && (SP_PARM->_color_pairs[oldPair] ==
1248 SP_PARM->_color_pairs[newPair])) {
1249 SetPair(oldLine[n], GetPair(newLine[n]));
1256 if (ceol_standout_glitch && clr_eol) {
1258 while (firstChar < screen_columns(SP_PARM)) {
1259 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1269 if (attrchanged) { /* we may have to disregard the whole line */
1270 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1271 ClrToEOL(NCURSES_SP_ARGx
1272 ClrBlank(NCURSES_SP_ARGx
1273 CurScreen(SP_PARM)), FALSE);
1274 PutRange(NCURSES_SP_ARGx
1275 oldLine, newLine, lineno, 0,
1276 screen_columns(SP_PARM) - 1);
1280 * This is a very simple loop to paint characters which may have the
1281 * magic cookie glitch embedded. It doesn't know much about video
1282 * attributes which are continued from one line to the next. It
1283 * assumes that we have filtered out requests for attribute changes
1284 * that do not get mapped to blank positions.
1286 * FIXME: we are not keeping track of where we put the cookies, so this
1287 * will work properly only once, since we may overwrite a cookie in a
1288 * following operation.
1290 } else if (magic_cookie_glitch > 0) {
1291 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1292 for (n = 0; n < screen_columns(SP_PARM); n++) {
1293 int m = n + magic_cookie_glitch;
1295 /* check for turn-on:
1296 * If we are writing an attributed blank, where the
1297 * previous cell is not attributed.
1299 if (ISBLANK(newLine[n])
1301 && xmc_turn_on(SP_PARM, newLine[n - 1], newLine[n]))
1304 && xmc_turn_on(SP_PARM,
1305 xmc_new(SP_PARM, lineno - 1,
1306 screen_columns(SP_PARM) - 1),
1311 PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1313 /* check for turn-off:
1314 * If we are writing an attributed non-blank, where the
1315 * next cell is blank, and not attributed.
1317 if (!ISBLANK(newLine[n])
1318 && ((n + 1 < screen_columns(SP_PARM)
1319 && xmc_turn_off(SP_PARM, newLine[n], newLine[n + 1]))
1320 || (n + 1 >= screen_columns(SP_PARM)
1321 && lineno + 1 < screen_lines(SP_PARM)
1322 && xmc_turn_off(SP_PARM,
1324 xmc_new(SP_PARM, lineno + 1, 0))))) {
1333 /* it may be cheap to clear leading whitespace with clr_bol */
1335 if (clr_bol && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1336 int oFirstChar, nFirstChar;
1338 for (oFirstChar = 0;
1339 oFirstChar < screen_columns(SP_PARM);
1341 if (!CharEq(oldLine[oFirstChar], blank))
1343 for (nFirstChar = 0;
1344 nFirstChar < screen_columns(SP_PARM);
1346 if (!CharEq(newLine[nFirstChar], blank))
1349 if (nFirstChar == oFirstChar) {
1350 firstChar = nFirstChar;
1351 /* find the first differing character */
1352 while (firstChar < screen_columns(SP_PARM)
1353 && CharEq(newLine[firstChar], oldLine[firstChar]))
1355 } else if (oFirstChar > nFirstChar) {
1356 firstChar = nFirstChar;
1357 } else { /* oFirstChar < nFirstChar */
1358 firstChar = oFirstChar;
1359 if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1360 if (nFirstChar >= screen_columns(SP_PARM)
1361 && SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1362 GoTo(NCURSES_SP_ARGx lineno, 0);
1363 UpdateAttrs(SP_PARM, blank);
1364 TPUTS_TRACE("clr_eol");
1365 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1367 GoTo(NCURSES_SP_ARGx lineno, nFirstChar - 1);
1368 UpdateAttrs(SP_PARM, blank);
1369 TPUTS_TRACE("clr_bol");
1370 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_bol);
1373 while (firstChar < nFirstChar)
1374 oldLine[firstChar++] = blank;
1378 /* find the first differing character */
1379 while (firstChar < screen_columns(SP_PARM)
1380 && CharEq(newLine[firstChar], oldLine[firstChar]))
1383 /* if there wasn't one, we're done */
1384 if (firstChar >= screen_columns(SP_PARM)) {
1385 TR(TRACE_UPDATE, (T_RETURN("")));
1389 blank = newLine[screen_columns(SP_PARM) - 1];
1391 if (!can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1392 /* find the last differing character */
1393 nLastChar = screen_columns(SP_PARM) - 1;
1395 while (nLastChar > firstChar
1396 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1399 if (nLastChar >= firstChar) {
1400 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1401 PutRange(NCURSES_SP_ARGx
1407 memcpy(oldLine + firstChar,
1408 newLine + firstChar,
1409 (unsigned) (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1411 TR(TRACE_UPDATE, (T_RETURN("")));
1415 /* find last non-blank character on old line */
1416 oLastChar = screen_columns(SP_PARM) - 1;
1417 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1420 /* find last non-blank character on new line */
1421 nLastChar = screen_columns(SP_PARM) - 1;
1422 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1425 if ((nLastChar == firstChar)
1426 && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1427 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1428 if (!CharEq(newLine[firstChar], blank))
1429 PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1430 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1431 } else if ((nLastChar != oLastChar)
1432 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1433 || !(SP_PARM->_nc_sp_idcok
1434 && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG)))) {
1435 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1436 if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1437 if (PutRange(NCURSES_SP_ARGx
1443 GoTo(NCURSES_SP_ARGx lineno, nLastChar + 1);
1445 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1447 n = max(nLastChar, oLastChar);
1448 PutRange(NCURSES_SP_ARGx
1456 int nLastNonblank = nLastChar;
1457 int oLastNonblank = oLastChar;
1459 /* find the last characters that really differ */
1460 /* can be -1 if no characters differ */
1461 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1462 /* don't split a wide char */
1463 if (isWidecExt(newLine[nLastChar]) &&
1464 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1468 if (nLastChar == -1 || oLastChar == -1)
1472 n = min(oLastChar, nLastChar);
1473 if (n >= firstChar) {
1474 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1475 PutRange(NCURSES_SP_ARGx
1483 if (oLastChar < nLastChar) {
1484 int m = max(nLastNonblank, oLastNonblank);
1485 #if USE_WIDEC_SUPPORT
1486 while (isWidecExt(newLine[n + 1]) && n) {
1491 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1492 if ((nLastChar < nLastNonblank)
1493 || InsCharCost(SP_PARM, nLastChar - oLastChar) > (m - n)) {
1494 PutRange(NCURSES_SP_ARGx
1501 InsStr(NCURSES_SP_ARGx &newLine[n + 1], nLastChar - oLastChar);
1503 } else if (oLastChar > nLastChar) {
1504 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1505 if (DelCharCost(SP_PARM, oLastChar - nLastChar)
1506 > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1507 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1508 n + 1, nLastNonblank))
1509 GoTo(NCURSES_SP_ARGx lineno, nLastNonblank + 1);
1510 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1513 * The delete-char sequence will
1514 * effectively shift in blanks from the
1515 * right margin of the screen. Ensure
1516 * that they are the right color by
1517 * setting the video attributes from
1518 * the last character on the row.
1520 UpdateAttrs(SP_PARM, blank);
1521 DelChar(NCURSES_SP_ARGx oLastChar - nLastChar);
1527 /* update the code's internal representation */
1528 if (screen_columns(SP_PARM) > firstChar)
1529 memcpy(oldLine + firstChar,
1530 newLine + firstChar,
1531 (unsigned) (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1532 TR(TRACE_UPDATE, (T_RETURN("")));
1537 ** ClearScreen(blank)
1539 ** Clear the physical screen and put cursor at home
1544 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1547 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1549 TR(TRACE_UPDATE, ("ClearScreen() called"));
1551 #if NCURSES_EXT_FUNCS
1552 if (SP_PARM->_coloron
1553 && !SP_PARM->_default_color) {
1554 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
1555 (short) GET_SCREEN_PAIR(SP_PARM),
1558 NCURSES_SP_NAME(_nc_outch));
1559 if (!back_color_erase) {
1567 UpdateAttrs(SP_PARM, blank);
1568 TPUTS_TRACE("clear_screen");
1569 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clear_screen);
1570 SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1571 position_check(SP_PARM,
1575 } else if (clr_eos) {
1576 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1577 GoTo(NCURSES_SP_ARGx 0, 0);
1578 UpdateAttrs(SP_PARM, blank);
1579 TPUTS_TRACE("clr_eos");
1580 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1582 screen_lines(SP_PARM),
1583 NCURSES_SP_NAME(_nc_outch));
1584 } else if (clr_eol) {
1585 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1586 UpdateAttrs(SP_PARM, blank);
1587 for (i = 0; i < screen_lines(SP_PARM); i++) {
1588 GoTo(NCURSES_SP_ARGx i, 0);
1589 TPUTS_TRACE("clr_eol");
1590 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1592 GoTo(NCURSES_SP_ARGx 0, 0);
1595 UpdateAttrs(SP_PARM, blank);
1596 for (i = 0; i < screen_lines(SP_PARM); i++) {
1597 GoTo(NCURSES_SP_ARGx i, 0);
1598 for (j = 0; j < screen_columns(SP_PARM); j++)
1599 PutChar(NCURSES_SP_ARGx CHREF(blank));
1601 GoTo(NCURSES_SP_ARGx 0, 0);
1604 for (i = 0; i < screen_lines(SP_PARM); i++) {
1605 for (j = 0; j < screen_columns(SP_PARM); j++)
1606 CurScreen(SP_PARM)->_line[i].text[j] = blank;
1609 TR(TRACE_UPDATE, ("screen cleared"));
1613 ** InsStr(line, count)
1615 ** Insert the count characters pointed to by line.
1620 InsStr(NCURSES_SP_DCLx NCURSES_CH_T * line, int count)
1622 TR(TRACE_UPDATE, ("InsStr(%p, %p,%d) called",
1624 (void *) line, count));
1626 /* Prefer parm_ich as it has the smallest cost - no need to shift
1627 * the whole line on each character. */
1628 /* The order must match that of InsCharCost. */
1630 TPUTS_TRACE("parm_ich");
1631 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1632 TPARM_1(parm_ich, count),
1634 NCURSES_SP_NAME(_nc_outch));
1636 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1640 } else if (enter_insert_mode && exit_insert_mode) {
1641 TPUTS_TRACE("enter_insert_mode");
1642 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_insert_mode);
1644 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1645 if (insert_padding) {
1646 TPUTS_TRACE("insert_padding");
1647 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1652 TPUTS_TRACE("exit_insert_mode");
1653 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
1656 TPUTS_TRACE("insert_character");
1657 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_character);
1658 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1659 if (insert_padding) {
1660 TPUTS_TRACE("insert_padding");
1661 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1667 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "InsStr");
1673 ** Delete count characters at current position
1678 DelChar(NCURSES_SP_DCLx int count)
1682 TR(TRACE_UPDATE, ("DelChar(%p, %d) called, position = (%ld,%ld)",
1683 (void *) SP_PARM, count,
1684 (long) NewScreen(SP_PARM)->_cury,
1685 (long) NewScreen(SP_PARM)->_curx));
1688 TPUTS_TRACE("parm_dch");
1689 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1690 TPARM_1(parm_dch, count),
1692 NCURSES_SP_NAME(_nc_outch));
1694 for (n = 0; n < count; n++) {
1695 TPUTS_TRACE("delete_character");
1696 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_character);
1702 * Physical-scrolling support
1704 * This code was adapted from Keith Bostic's hardware scrolling
1705 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1706 * capabilities, narrowed the call interface slightly, and cleaned
1707 * up some convoluted tests. I also added support for the memory_above
1708 * memory_below, and non_dest_scroll_region capabilities.
1710 * For this code to work, we must have either
1711 * change_scroll_region and scroll forward/reverse commands, or
1712 * insert and delete line capabilities.
1713 * When the scrolling region has been set, the cursor has to
1714 * be at the last line of the region to make the scroll up
1715 * happen, or on the first line of region to scroll down.
1717 * This code makes one aesthetic decision in the opposite way from
1718 * BSD curses. BSD curses preferred pairs of il/dl operations
1719 * over scrolls, allegedly because il/dl looked faster. We, on
1720 * the other hand, prefer scrolls because (a) they're just as fast
1721 * on many terminals and (b) using them avoids bouncing an
1722 * unchanged bottom section of the screen up and down, which is
1725 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1727 * I used assumption that capabilities il/il1/dl/dl1 work inside
1728 * changed scroll region not shifting screen contents outside of it.
1729 * If there are any terminals behaving different way, it would be
1730 * necessary to add some conditions to scroll_csr_forward/backward.
1733 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1735 scroll_csr_forward(NCURSES_SP_DCLx
1745 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1746 GoTo(NCURSES_SP_ARGx bot, 0);
1747 UpdateAttrs(SP_PARM, blank);
1748 TPUTS_TRACE("scroll_forward");
1749 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1750 } else if (n == 1 && delete_line && bot == maxy) {
1751 GoTo(NCURSES_SP_ARGx top, 0);
1752 UpdateAttrs(SP_PARM, blank);
1753 TPUTS_TRACE("delete_line");
1754 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1755 } else if (parm_index && top == miny && bot == maxy) {
1756 GoTo(NCURSES_SP_ARGx bot, 0);
1757 UpdateAttrs(SP_PARM, blank);
1758 TPUTS_TRACE("parm_index");
1759 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1760 TPARM_2(parm_index, n, 0),
1762 NCURSES_SP_NAME(_nc_outch));
1763 } else if (parm_delete_line && bot == maxy) {
1764 GoTo(NCURSES_SP_ARGx top, 0);
1765 UpdateAttrs(SP_PARM, blank);
1766 TPUTS_TRACE("parm_delete_line");
1767 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1768 TPARM_2(parm_delete_line, n, 0),
1770 NCURSES_SP_NAME(_nc_outch));
1771 } else if (scroll_forward && top == miny && bot == maxy) {
1772 GoTo(NCURSES_SP_ARGx bot, 0);
1773 UpdateAttrs(SP_PARM, blank);
1774 for (i = 0; i < n; i++) {
1775 TPUTS_TRACE("scroll_forward");
1776 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1778 } else if (delete_line && bot == maxy) {
1779 GoTo(NCURSES_SP_ARGx top, 0);
1780 UpdateAttrs(SP_PARM, blank);
1781 for (i = 0; i < n; i++) {
1782 TPUTS_TRACE("delete_line");
1783 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1788 #if NCURSES_EXT_FUNCS
1789 if (FILL_BCE(SP_PARM)) {
1791 for (i = 0; i < n; i++) {
1792 GoTo(NCURSES_SP_ARGx bot - i, 0);
1793 for (j = 0; j < screen_columns(SP_PARM); j++)
1794 PutChar(NCURSES_SP_ARGx CHREF(blank));
1801 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1804 scroll_csr_backward(NCURSES_SP_DCLx
1814 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1815 GoTo(NCURSES_SP_ARGx top, 0);
1816 UpdateAttrs(SP_PARM, blank);
1817 TPUTS_TRACE("scroll_reverse");
1818 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1819 } else if (n == 1 && insert_line && bot == maxy) {
1820 GoTo(NCURSES_SP_ARGx top, 0);
1821 UpdateAttrs(SP_PARM, blank);
1822 TPUTS_TRACE("insert_line");
1823 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1824 } else if (parm_rindex && top == miny && bot == maxy) {
1825 GoTo(NCURSES_SP_ARGx top, 0);
1826 UpdateAttrs(SP_PARM, blank);
1827 TPUTS_TRACE("parm_rindex");
1828 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1829 TPARM_2(parm_rindex, n, 0),
1831 NCURSES_SP_NAME(_nc_outch));
1832 } else if (parm_insert_line && bot == maxy) {
1833 GoTo(NCURSES_SP_ARGx top, 0);
1834 UpdateAttrs(SP_PARM, blank);
1835 TPUTS_TRACE("parm_insert_line");
1836 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1837 TPARM_2(parm_insert_line, n, 0),
1839 NCURSES_SP_NAME(_nc_outch));
1840 } else if (scroll_reverse && top == miny && bot == maxy) {
1841 GoTo(NCURSES_SP_ARGx top, 0);
1842 UpdateAttrs(SP_PARM, blank);
1843 for (i = 0; i < n; i++) {
1844 TPUTS_TRACE("scroll_reverse");
1845 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1847 } else if (insert_line && bot == maxy) {
1848 GoTo(NCURSES_SP_ARGx top, 0);
1849 UpdateAttrs(SP_PARM, blank);
1850 for (i = 0; i < n; i++) {
1851 TPUTS_TRACE("insert_line");
1852 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1857 #if NCURSES_EXT_FUNCS
1858 if (FILL_BCE(SP_PARM)) {
1860 for (i = 0; i < n; i++) {
1861 GoTo(NCURSES_SP_ARGx top + i, 0);
1862 for (j = 0; j < screen_columns(SP_PARM); j++)
1863 PutChar(NCURSES_SP_ARGx CHREF(blank));
1870 /* scroll by using delete_line at del and insert_line at ins */
1873 scroll_idl(NCURSES_SP_DCLx int n, int del, int ins, NCURSES_CH_T blank)
1877 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1880 GoTo(NCURSES_SP_ARGx del, 0);
1881 UpdateAttrs(SP_PARM, blank);
1882 if (n == 1 && delete_line) {
1883 TPUTS_TRACE("delete_line");
1884 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1885 } else if (parm_delete_line) {
1886 TPUTS_TRACE("parm_delete_line");
1887 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1888 TPARM_2(parm_delete_line, n, 0),
1890 NCURSES_SP_NAME(_nc_outch));
1891 } else { /* if (delete_line) */
1892 for (i = 0; i < n; i++) {
1893 TPUTS_TRACE("delete_line");
1894 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1898 GoTo(NCURSES_SP_ARGx ins, 0);
1899 UpdateAttrs(SP_PARM, blank);
1900 if (n == 1 && insert_line) {
1901 TPUTS_TRACE("insert_line");
1902 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1903 } else if (parm_insert_line) {
1904 TPUTS_TRACE("parm_insert_line");
1905 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1906 TPARM_2(parm_insert_line, n, 0),
1908 NCURSES_SP_NAME(_nc_outch));
1909 } else { /* if (insert_line) */
1910 for (i = 0; i < n; i++) {
1911 TPUTS_TRACE("insert_line");
1912 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1920 * Note: some terminals require the cursor to be within the scrolling margins
1921 * before setting them. Generally, the cursor must be at the appropriate end
1922 * of the scrolling margins when issuing an indexing operation (it is not
1923 * apparent whether it must also be at the left margin; we do this just to be
1924 * safe). To make the related cursor movement a little faster, we use the
1925 * save/restore cursor capabilities if the terminal has them.
1928 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
1933 /* scroll region from top to bot by n lines */
1937 bool cursor_saved = FALSE;
1940 TR(TRACE_MOVE, ("_nc_scrolln(%p, %d, %d, %d, %d)",
1941 (void *) SP_PARM, n, top, bot, maxy));
1943 if (!IsValidScreen(SP_PARM))
1946 blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1950 * If we scroll, we might remove a cookie.
1952 if (magic_cookie_glitch > 0) {
1957 if (n > 0) { /* scroll up (forward) */
1959 * Explicitly clear if stuff pushed off top of region might
1960 * be saved by the terminal.
1962 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
1964 if (res == ERR && change_scroll_region) {
1965 if ((((n == 1 && scroll_forward) || parm_index)
1966 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
1967 && save_cursor && restore_cursor) {
1968 cursor_saved = TRUE;
1969 TPUTS_TRACE("save_cursor");
1970 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
1972 TPUTS_TRACE("change_scroll_region");
1973 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1974 TPARM_2(change_scroll_region, top, bot));
1976 TPUTS_TRACE("restore_cursor");
1977 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
1979 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1982 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
1984 TPUTS_TRACE("change_scroll_region");
1985 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1986 TPARM_2(change_scroll_region, 0, maxy));
1987 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1990 if (res == ERR && SP_PARM->_nc_sp_idlok)
1991 res = scroll_idl(NCURSES_SP_ARGx n, top, bot - n + 1, blank);
1994 * Clear the newly shifted-in text.
1997 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
1998 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1999 if (bot == maxy && clr_eos) {
2000 GoTo(NCURSES_SP_ARGx bot - n + 1, 0);
2001 ClrToEOS(NCURSES_SP_ARGx blank2);
2003 for (i = 0; i < n; i++) {
2004 GoTo(NCURSES_SP_ARGx bot - i, 0);
2005 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2010 } else { /* (n < 0) - scroll down (backward) */
2011 res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
2013 if (res == ERR && change_scroll_region) {
2015 && (SP_PARM->_cursrow == top ||
2016 SP_PARM->_cursrow == top - 1)
2017 && save_cursor && restore_cursor) {
2018 cursor_saved = TRUE;
2019 TPUTS_TRACE("save_cursor");
2020 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
2022 TPUTS_TRACE("change_scroll_region");
2023 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2024 TPARM_2(change_scroll_region, top, bot));
2026 TPUTS_TRACE("restore_cursor");
2027 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
2029 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2032 res = scroll_csr_backward(NCURSES_SP_ARGx
2033 -n, top, bot, top, bot, blank);
2035 TPUTS_TRACE("change_scroll_region");
2036 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2037 TPARM_2(change_scroll_region, 0, maxy));
2038 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2041 if (res == ERR && SP_PARM->_nc_sp_idlok)
2042 res = scroll_idl(NCURSES_SP_ARGx -n, bot + n + 1, top, blank);
2045 * Clear the newly shifted-in text.
2048 && (non_dest_scroll_region || (memory_above && top == 0))) {
2049 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2050 for (i = 0; i < -n; i++) {
2051 GoTo(NCURSES_SP_ARGx i + top, 0);
2052 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2060 _nc_scroll_window(CurScreen(SP_PARM), n,
2061 (NCURSES_SIZE_T) top,
2062 (NCURSES_SIZE_T) bot,
2065 /* shift hash values too - they can be reused */
2066 NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_ARGx n, top, bot);
2071 #if NCURSES_SP_FUNCS
2073 _nc_scrolln(int n, int top, int bot, int maxy)
2075 return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
2079 NCURSES_EXPORT(void)
2080 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_DCL0)
2084 /* make sure terminal is in a sane known state */
2085 SetAttr(SCREEN_ATTRS(SP_PARM), A_NORMAL);
2086 NewScreen(SP_PARM)->_clear = TRUE;
2088 /* reset color pairs and definitions */
2089 if (SP_PARM->_coloron || SP_PARM->_color_defs)
2090 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2092 /* restore user-defined colors, if any */
2093 if (SP_PARM->_color_defs < 0) {
2095 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
2096 for (n = 0; n < SP_PARM->_color_defs; ++n) {
2097 if (SP_PARM->_color_table[n].init) {
2098 NCURSES_SP_NAME(init_color) (NCURSES_SP_ARGx
2100 SP_PARM->_color_table[n].r,
2101 SP_PARM->_color_table[n].g,
2102 SP_PARM->_color_table[n].b);
2107 if (exit_attribute_mode)
2108 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_attribute_mode);
2110 /* turn off attributes */
2111 if (exit_alt_charset_mode)
2112 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_alt_charset_mode);
2113 if (exit_standout_mode)
2114 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_standout_mode);
2115 if (exit_underline_mode)
2116 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_underline_mode);
2118 if (exit_insert_mode)
2119 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
2120 if (enter_am_mode && exit_am_mode)
2121 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2127 #if NCURSES_SP_FUNCS
2128 NCURSES_EXPORT(void)
2129 _nc_screen_resume(void)
2131 NCURSES_SP_NAME(_nc_screen_resume) (CURRENT_SCREEN);
2135 NCURSES_EXPORT(void)
2136 NCURSES_SP_NAME(_nc_screen_init) (NCURSES_SP_DCL0)
2138 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
2141 #if NCURSES_SP_FUNCS
2142 NCURSES_EXPORT(void)
2143 _nc_screen_init(void)
2145 NCURSES_SP_NAME(_nc_screen_init) (CURRENT_SCREEN);
2149 /* wrap up screen handling */
2150 NCURSES_EXPORT(void)
2151 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2155 UpdateAttrs(SP_PARM, normal);
2156 #if NCURSES_EXT_FUNCS
2157 if (SP_PARM->_coloron
2158 && !SP_PARM->_default_color) {
2159 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2160 SP_PARM->_default_color = TRUE;
2161 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
2165 NCURSES_SP_NAME(_nc_outch));
2166 SP_PARM->_default_color = FALSE;
2168 TINFO_MVCUR(NCURSES_SP_ARGx
2171 screen_lines(SP_PARM) - 1,
2174 ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2177 if (SP_PARM->_color_defs) {
2178 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2183 #if NCURSES_SP_FUNCS
2184 NCURSES_EXPORT(void)
2185 _nc_screen_wrap(void)
2187 NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2192 NCURSES_EXPORT(void)
2193 NCURSES_SP_NAME(_nc_do_xmc_glitch) (NCURSES_SP_DCLx attr_t previous)
2196 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP_PARM)));
2200 SP_PARM->_curscol += magic_cookie_glitch;
2201 if (SP_PARM->_curscol >= SP_PARM->_columns)
2202 wrap_cursor(NCURSES_SP_ARG);
2203 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie",
2204 SP_PARM->_cursrow, SP_PARM->_curscol));
2211 #if NCURSES_SP_FUNCS
2212 NCURSES_EXPORT(void)
2213 _nc_do_xmc_glitch(attr_t previous)
2215 NCURSES_SP_NAME(_nc_do_xmc_glitch) (CURRENT_SCREEN, previous);
2219 #endif /* USE_XMC_SUPPORT */