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.268 2012/05/12 21:02:00 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)
214 NCURSES_CH_T attr = CHDEREF(ch);
216 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
218 SP_PARM->_cursrow, SP_PARM->_curscol));
219 #if USE_WIDEC_SUPPORT
221 * If this is not a valid character, there is nothing more to do.
223 if (isWidecExt(CHDEREF(ch))) {
224 TR(TRACE_CHARPUT, ("...skip"));
228 * Determine the number of character cells which the 'ch' value will use
229 * on the screen. It should be at least one.
231 if ((chlen = wcwidth(CharOf(CHDEREF(ch)))) <= 0) {
232 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
235 * If the character falls into any of these special cases, do
236 * not force the result to a blank:
238 * a) it is printable (this works around a bug in wcwidth()).
239 * b) use_legacy_coding() has been called to modify the treatment
241 * c) the acs_map[] has been initialized to allow codes 0-31
242 * to be rendered. This supports Linux console's "PC"
243 * characters. Codes 128-255 are allowed though this is
246 if (is8bits(CharOf(CHDEREF(ch)))
247 && (isprint(CharOf(CHDEREF(ch)))
248 || (SP_PARM->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
249 || (SP_PARM->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
250 || (AttrOf(attr) & A_ALTCHARSET
251 && ((CharOfD(ch) < ACS_LEN
252 && SP_PARM->_acs_map != 0
253 && SP_PARM->_acs_map[CharOfD(ch)] != 0)
254 || (CharOfD(ch) >= 128))))) {
258 TR(TRACE_CHARPUT, ("forced to blank"));
264 if ((AttrOf(attr) & A_ALTCHARSET)
265 && SP_PARM->_acs_map != 0
266 && CharOfD(ch) < ACS_LEN) {
267 my_ch = CHDEREF(ch); /* work around const param */
268 #if USE_WIDEC_SUPPORT
270 * This is crude & ugly, but works most of the time. It checks if the
271 * acs_chars string specified that we have a mapping for this
272 * character, and uses the wide-character mapping when we expect the
273 * normal one to be broken (by mis-design ;-).
275 if (SP_PARM->_screen_acs_fix
276 && SP_PARM->_screen_acs_map[CharOf(my_ch)]) {
277 RemAttr(attr, A_ALTCHARSET);
278 my_ch = _nc_wacs[CharOf(my_ch)];
282 * If we (still) have alternate character set, it is the normal 8bit
283 * flavor. The _screen_acs_map[] array tells if the character was
284 * really in acs_chars, needed because of the way wide/normal line
285 * drawing flavors are integrated.
287 if (AttrOf(attr) & A_ALTCHARSET) {
289 chtype temp = UChar(SP_PARM->_acs_map[j]);
292 SetChar(my_ch, temp, AttrOf(attr));
295 RemAttr(attr, A_ALTCHARSET);
300 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
301 SetChar(tilde, L('`'), AttrOf(attr));
305 UpdateAttrs(SP_PARM, attr);
306 #if !USE_WIDEC_SUPPORT
307 /* FIXME - we do this special case for signal handling, should see how to
308 * make it work for wide characters.
310 if (SP_PARM->_outch != 0) {
311 SP_PARM->_outch(NCURSES_SP_ARGx UChar(ch));
315 PUTC(CHDEREF(ch), SP_PARM->_ofp); /* macro's fastest... */
318 SP_PARM->_curscol += chlen;
320 TPUTS_TRACE("char_padding");
321 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx char_padding);
326 check_pending(NCURSES_SP_DCL0)
327 /* check for pending input */
329 bool have_pending = FALSE;
332 * Only carry out this check when the flag is zero, otherwise we'll
333 * have the refreshing slow down drastically (or stop) if there's an
334 * unread character available.
336 if (SP_PARM->_fifohold != 0)
339 if (SP_PARM->_checkfd >= 0) {
341 struct pollfd fds[1];
342 fds[0].fd = SP_PARM->_checkfd;
343 fds[0].events = POLLIN;
344 if (poll(fds, (size_t) 1, 0) > 0) {
347 #elif defined(__BEOS__)
349 * BeOS's select() is declared in socket.h, so the configure script does
350 * not see it. That's just as well, since that function works only for
351 * sockets. This (using snooze and ioctl) was distilled from Be's patch
352 * for ncurses which uses a separate thread to simulate select().
354 * FIXME: the return values from the ioctl aren't very clear if we get
358 int howmany = ioctl(0, 'ichr', &n);
359 if (howmany >= 0 && n > 0) {
364 struct timeval ktimeout;
367 ktimeout.tv_usec = 0;
370 FD_SET(SP_PARM->_checkfd, &fdset);
371 if (select(SP_PARM->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
377 SP_PARM->_fifohold = 5;
378 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
383 /* put char at lower right corner */
385 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
387 if (!auto_right_margin) {
388 /* we can put the char directly */
389 PutAttrChar(NCURSES_SP_ARGx ch);
390 } else if (enter_am_mode && exit_am_mode) {
391 /* we can suppress automargin */
392 TPUTS_TRACE("exit_am_mode");
393 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_am_mode);
395 PutAttrChar(NCURSES_SP_ARGx ch);
397 position_check(SP_PARM,
402 TPUTS_TRACE("enter_am_mode");
403 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_am_mode);
404 } else if ((enter_insert_mode && exit_insert_mode)
405 || insert_character || parm_ich) {
407 screen_lines(SP_PARM) - 1,
408 screen_columns(SP_PARM) - 2);
409 PutAttrChar(NCURSES_SP_ARGx ch);
411 screen_lines(SP_PARM) - 1,
412 screen_columns(SP_PARM) - 2);
413 InsStr(NCURSES_SP_ARGx
414 NewScreen(SP_PARM)->_line[screen_lines(SP_PARM) - 1].text +
415 screen_columns(SP_PARM) - 2, 1);
420 * Wrap the cursor position, i.e., advance to the beginning of the next line.
423 wrap_cursor(NCURSES_SP_DCL0)
425 if (eat_newline_glitch) {
427 * xenl can manifest two different ways. The vt100 way is that, when
428 * you'd expect the cursor to wrap, it stays hung at the right margin
429 * (on top of the character just emitted) and doesn't wrap until the
430 * *next* graphic char is emitted. The c100 way is to ignore LF
431 * received just after an am wrap.
433 * An aggressive way to handle this would be to emit CR/LF after the
434 * char and then assume the wrap is done, you're on the first position
435 * of the next line, and the terminal out of its weird state. Here
436 * it's safe to just tell the code that the cursor is in hyperspace and
437 * let the next mvcur() call straighten things out.
439 SP_PARM->_curscol = -1;
440 SP_PARM->_cursrow = -1;
441 } else if (auto_right_margin) {
442 SP_PARM->_curscol = 0;
445 * We've actually moved - but may have to work around problems with
446 * video attributes not working.
448 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP_PARM))) {
449 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
450 (unsigned long) AttrOf(SCREEN_ATTRS(SP_PARM)),
451 _traceattr(AttrOf(SCREEN_ATTRS(SP_PARM)))));
452 (void) VIDATTR(SP_PARM, A_NORMAL, 0);
457 position_check(SP_PARM,
463 static NCURSES_INLINE void
464 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
465 /* insert character, handling automargin stuff */
467 if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1 &&
468 SP_PARM->_curscol == screen_columns(SP_PARM) - 1) {
469 PutCharLR(NCURSES_SP_ARGx ch);
471 PutAttrChar(NCURSES_SP_ARGx ch);
474 if (SP_PARM->_curscol >= screen_columns(SP_PARM))
475 wrap_cursor(NCURSES_SP_ARG);
477 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "PutChar");
481 * Check whether the given character can be output by clearing commands. This
482 * includes test for being a space and not including any 'bad' attributes, such
483 * as A_REVERSE. All attribute flags which don't affect appearance of a space
484 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
486 static NCURSES_INLINE bool
487 can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)
489 if (!back_color_erase && SP_PARM->_coloron) {
490 #if NCURSES_EXT_FUNCS
493 if (!SP_PARM->_default_color)
495 if (SP_PARM->_default_fg != C_MASK || SP_PARM->_default_bg != C_MASK)
497 if ((pair = GetPair(CHDEREF(ch))) != 0) {
499 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
502 if (fg != C_MASK || bg != C_MASK)
506 if (AttrOfD(ch) & A_COLOR)
510 return (ISBLANK(CHDEREF(ch)) &&
511 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
515 * Issue a given span of characters from an array.
516 * Must be functionally equivalent to:
517 * for (i = 0; i < num; i++)
519 * but can leave the cursor positioned at the middle of the interval.
521 * Returns: 0 - cursor is at the end of interval
522 * 1 - cursor is somewhere in the middle
524 * This code is optimized using ech and rep.
527 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext, int num)
531 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
533 if (erase_chars || repeat_char) {
538 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
539 PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
545 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
550 while (runcount < num && CharEq(ntext[runcount], ntext0))
554 * The cost expression in the middle isn't exactly right.
555 * _cup_ch_cost is an upper bound on the cost for moving to the
556 * end of the erased area, but not the cost itself (which we
557 * can't compute without emitting the move). This may result
558 * in erase_chars not getting used in some situations for
559 * which it would be marginally advantageous.
562 && runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
563 && can_clear_with(NCURSES_SP_ARGx CHREF(ntext0))) {
564 UpdateAttrs(SP_PARM, ntext0);
565 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
566 TPARM_1(erase_chars, runcount));
569 * If this is the last part of the given interval,
570 * don't bother moving cursor, since it can be the
571 * last update on the line.
573 if (runcount < num) {
576 SP_PARM->_curscol + runcount);
578 return 1; /* cursor stays in the middle */
580 } else if (repeat_char && runcount > SP_PARM->_rep_cost) {
581 bool wrap_possible = (SP_PARM->_curscol + runcount >=
582 screen_columns(SP_PARM));
583 int rep_count = runcount;
588 UpdateAttrs(SP_PARM, ntext0);
589 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
594 NCURSES_SP_NAME(_nc_outch));
595 SP_PARM->_curscol += rep_count;
598 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
600 for (i = 0; i < runcount; i++)
601 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
609 for (i = 0; i < num; i++)
610 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
615 * Output the line in the given range [first .. last]
617 * If there's a run of identical characters that's long enough to justify
618 * cursor movement, use that also.
620 * Returns: same as EmitRange
623 PutRange(NCURSES_SP_DCLx
624 const NCURSES_CH_T * otext,
625 const NCURSES_CH_T * ntext,
631 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %p, %d, %d, %d)",
633 (const void *) otext,
634 (const void *) ntext,
638 && (last - first + 1) > SP_PARM->_inline_cost) {
639 for (j = first, same = 0; j <= last; j++) {
640 if (!same && isWidecExt(otext[j]))
642 if (CharEq(otext[j], ntext[j])) {
645 if (same > SP_PARM->_inline_cost) {
646 EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
647 GoTo(NCURSES_SP_ARGx row, first = j);
652 i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
654 * Always return 1 for the next GoTo() after a PutRange() if we found
655 * identical characters at end of interval
657 return (same == 0 ? i : 1);
659 return EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
662 /* leave unbracketed here so 'indent' works */
663 #define MARK_NOCHANGE(win,row) \
664 win->_line[row].firstchar = _NOCHANGE; \
665 win->_line[row].lastchar = _NOCHANGE; \
666 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
669 TINFO_DOUPDATE(NCURSES_SP_DCL0)
674 struct tms before, after;
675 #endif /* USE_TRACE_TIMES */
677 T((T_CALLED("_nc_tinfo:doupdate(%p)"), (void *) SP_PARM));
684 * It is "legal" but unlikely that an application could assign a new
685 * value to one of the standard windows. Check for that possibility
686 * and try to recover.
688 * We do not allow applications to assign new values in the reentrant
691 #define SyncScreens(internal,exported) \
692 if (internal == 0) internal = exported; \
693 if (internal != exported) exported = internal
695 SyncScreens(CurScreen(SP_PARM), curscr);
696 SyncScreens(NewScreen(SP_PARM), newscr);
697 SyncScreens(StdScreen(SP_PARM), stdscr);
700 if (CurScreen(SP_PARM) == 0
701 || NewScreen(SP_PARM) == 0
702 || StdScreen(SP_PARM) == 0)
706 if (USE_TRACEF(TRACE_UPDATE)) {
707 if (CurScreen(SP_PARM)->_clear)
708 _tracef("curscr is clear");
710 _tracedump("curscr", CurScreen(SP_PARM));
711 _tracedump("newscr", NewScreen(SP_PARM));
712 _nc_unlock_global(tracef);
716 _nc_signal_handler(FALSE);
718 if (SP_PARM->_fifohold)
719 SP_PARM->_fifohold--;
722 if (SP_PARM->_endwin || _nc_handle_sigwinch(SP_PARM)) {
724 * This is a transparent extension: XSI does not address it,
725 * and applications need not know that ncurses can do it.
727 * Check if the terminal size has changed while curses was off
728 * (this can happen in an xterm, for example), and resize the
729 * ncurses data structures accordingly.
731 _nc_update_screensize(SP_PARM);
735 if (SP_PARM->_endwin) {
737 T(("coming back from shell mode"));
738 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_ARG);
740 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG);
741 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
742 SP_PARM->_mouse_resume(SP_PARM);
744 SP_PARM->_endwin = FALSE;
747 /* zero the metering machinery */
749 (void) times(&before);
750 #endif /* USE_TRACE_TIMES */
753 * This is the support for magic-cookie terminals. The theory: we scan
754 * the virtual screen looking for attribute turnons. Where we find one,
755 * check to make sure it's realizable by seeing if the required number of
756 * un-attributed blanks are present before and after the attributed range;
757 * try to shift the range boundaries over blanks (not changing the screen
758 * display) so this becomes true. If it is, shift the beginning attribute
759 * change appropriately (the end one, if we've gotten this far, is
760 * guaranteed room for its cookie). If not, nuke the added attributes out
764 if (magic_cookie_glitch > 0) {
766 attr_t rattr = A_NORMAL;
768 for (i = 0; i < screen_lines(SP_PARM); i++) {
769 for (j = 0; j < screen_columns(SP_PARM); j++) {
771 NCURSES_CH_T *thisline = NewScreen(SP_PARM)->_line[i].text;
772 attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
773 attr_t turnon = thisattr & ~rattr;
775 /* is an attribute turned on here? */
781 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
782 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
785 * If the attribute change location is a blank with a "safe"
786 * attribute, undo the attribute turnon. This may ensure
787 * there's enough room to set the attribute before the first
788 * non-blank in the run.
790 #define SAFE(scr,a) (!((a) & (scr)->_xmc_triggers))
791 if (ISBLANK(thisline[j]) && SAFE(SP_PARM, turnon)) {
792 RemAttr(thisline[j], turnon);
796 /* check that there's enough room at start of span */
797 for (k = 1; k <= magic_cookie_glitch; k++) {
799 || !ISBLANK(thisline[j - k])
800 || !SAFE(SP_PARM, AttrOf(thisline[j - k]))) {
802 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
804 (ISBLANK(thisline[j - k])
807 (SAFE(SP_PARM, AttrOf(thisline[j - k]))
814 bool end_onscreen = FALSE;
817 /* find end of span, if it's onscreen */
818 for (m = i; m < screen_lines(SP_PARM); m++) {
819 for (; n < screen_columns(SP_PARM); n++) {
821 AttrOf(NewScreen(SP_PARM)->_line[m].text[n]);
822 if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
825 ("Range attributed with %s ends at (%d, %d)",
826 _traceattr(turnon), m, n));
833 ("Range attributed with %s ends offscreen",
834 _traceattr(turnon)));
838 NCURSES_CH_T *lastline =
839 NewScreen(SP_PARM)->_line[m].text;
842 * If there are safely-attributed blanks at the end of
843 * the range, shorten the range. This will help ensure
844 * that there is enough room at end of span.
847 && ISBLANK(lastline[n])
848 && SAFE(SP_PARM, AttrOf(lastline[n]))) {
849 RemAttr(lastline[n--], turnon);
852 /* check that there's enough room at end of span */
853 for (k = 1; k <= magic_cookie_glitch; k++) {
854 if (n + k >= screen_columns(SP_PARM)
855 || !ISBLANK(lastline[n + k])
856 || !SAFE(SP_PARM, AttrOf(lastline[n + k]))) {
859 ("No room at end in %d,%d%s%s",
861 (ISBLANK(lastline[n + k])
864 (SAFE(SP_PARM, AttrOf(lastline[n + k]))
877 ("Clearing %s beginning at (%d, %d)",
878 _traceattr(turnon), i, j));
880 /* turn off new attributes over span */
881 for (p = i; p < screen_lines(SP_PARM); p++) {
882 for (; q < screen_columns(SP_PARM); q++) {
883 attr_t testattr = AttrOf(newscr->_line[p].text[q]);
884 if ((testattr & SP_PARM->_xmc_triggers) == rattr)
886 RemAttr(NewScreen(SP_PARM)->_line[p].text[q], turnon);
893 ("Cookie space for %s found before (%d, %d)",
894 _traceattr(turnon), i, j));
897 * Back up the start of range so there's room for cookies
898 * before the first nonblank character.
900 for (k = 1; k <= magic_cookie_glitch; k++)
901 AddAttr(thisline[j - k], turnon);
909 /* show altered highlights after magic-cookie check */
910 if (USE_TRACEF(TRACE_UPDATE)) {
911 _tracef("After magic-cookie check...");
912 _tracedump("newscr", NewScreen(SP_PARM));
913 _nc_unlock_global(tracef);
917 #endif /* USE_XMC_SUPPORT */
920 if (CurScreen(SP_PARM)->_clear || NewScreen(SP_PARM)->_clear) { /* force refresh ? */
921 ClrUpdate(NCURSES_SP_ARG);
922 CurScreen(SP_PARM)->_clear = FALSE; /* reset flag */
923 NewScreen(SP_PARM)->_clear = FALSE; /* reset flag */
925 int changedlines = CHECK_INTERVAL;
927 if (check_pending(NCURSES_SP_ARG))
930 nonempty = min(screen_lines(SP_PARM), NewScreen(SP_PARM)->_maxy + 1);
932 if (SP_PARM->_scrolling) {
933 NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_ARG);
936 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
938 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
939 for (i = 0; i < nonempty; i++) {
941 * Here is our line-breakout optimization.
943 if (changedlines == CHECK_INTERVAL) {
944 if (check_pending(NCURSES_SP_ARG))
950 * newscr->line[i].firstchar is normally set
951 * by wnoutrefresh. curscr->line[i].firstchar
952 * is normally set by _nc_scroll_window in the
953 * vertical-movement optimization code,
955 if (NewScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE
956 || CurScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE) {
957 TransformLine(NCURSES_SP_ARGx i);
961 /* mark line changed successfully */
962 if (i <= NewScreen(SP_PARM)->_maxy) {
963 MARK_NOCHANGE(NewScreen(SP_PARM), i);
965 if (i <= CurScreen(SP_PARM)->_maxy) {
966 MARK_NOCHANGE(CurScreen(SP_PARM), i);
971 /* put everything back in sync */
972 for (i = nonempty; i <= NewScreen(SP_PARM)->_maxy; i++) {
973 MARK_NOCHANGE(NewScreen(SP_PARM), i);
975 for (i = nonempty; i <= CurScreen(SP_PARM)->_maxy; i++) {
976 MARK_NOCHANGE(CurScreen(SP_PARM), i);
979 if (!NewScreen(SP_PARM)->_leaveok) {
980 CurScreen(SP_PARM)->_curx = NewScreen(SP_PARM)->_curx;
981 CurScreen(SP_PARM)->_cury = NewScreen(SP_PARM)->_cury;
983 GoTo(NCURSES_SP_ARGx CurScreen(SP_PARM)->_cury, CurScreen(SP_PARM)->_curx);
988 * We would like to keep the physical screen in normal mode in case we get
989 * other processes writing to the screen. This goal cannot be met for
990 * magic cookies since it interferes with attributes that may propagate
991 * past the current position.
994 if (magic_cookie_glitch != 0)
996 UpdateAttrs(SP_PARM, normal);
998 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
999 WINDOW_ATTRS(CurScreen(SP_PARM)) = WINDOW_ATTRS(NewScreen(SP_PARM));
1002 (void) times(&after);
1004 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
1006 (long) (after.tms_stime - before.tms_stime),
1007 (long) (after.tms_utime - before.tms_utime)));
1008 #endif /* USE_TRACE_TIMES */
1010 _nc_signal_handler(TRUE);
1015 #if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)
1019 return TINFO_DOUPDATE(CURRENT_SCREEN);
1026 * Returns the attributed character that corresponds to the "cleared"
1027 * screen. If the terminal has the back-color-erase feature, this will be
1028 * colored according to the wbkgd() call.
1030 * We treat 'curscr' specially because it isn't supposed to be set directly
1031 * in the wbkgd() call. Assume 'stdscr' for this case.
1033 #define BCE_ATTRS (A_NORMAL|A_COLOR)
1034 #define BCE_BKGD(sp,win) (((win) == CurScreen(sp) ? StdScreen(sp) : (win))->_nc_bkgd)
1036 static NCURSES_INLINE NCURSES_CH_T
1037 ClrBlank(NCURSES_SP_DCLx WINDOW *win)
1039 NCURSES_CH_T blank = blankchar;
1040 if (back_color_erase)
1041 AddAttr(blank, (AttrOf(BCE_BKGD(SP_PARM, win)) & BCE_ATTRS));
1048 ** Update by clearing and redrawing the entire screen.
1053 ClrUpdate(NCURSES_SP_DCL0)
1055 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1058 NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1059 int nonempty = min(screen_lines(SP_PARM),
1060 NewScreen(SP_PARM)->_maxy + 1);
1062 ClearScreen(NCURSES_SP_ARGx blank);
1064 TR(TRACE_UPDATE, ("updating screen from scratch"));
1066 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1068 for (i = 0; i < nonempty; i++)
1069 TransformLine(NCURSES_SP_ARGx i);
1071 TR(TRACE_UPDATE, (T_RETURN("")));
1077 ** Clear to end of current line, starting at the cursor position
1081 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, int needclear)
1086 if (CurScreen(SP_PARM) != 0
1087 && SP_PARM->_cursrow >= 0) {
1088 for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1091 &(CurScreen(SP_PARM)->_line[SP_PARM->_cursrow].text[j]);
1093 if (!CharEq(*cp, blank)) {
1102 if (needclear && (SP_PARM != 0)) {
1103 UpdateAttrs(SP_PARM, blank);
1104 TPUTS_TRACE("clr_eol");
1105 if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1106 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1108 int count = (screen_columns(SP_PARM) - SP_PARM->_curscol);
1110 PutChar(NCURSES_SP_ARGx CHREF(blank));
1118 ** Clear to end of screen, starting at the cursor position
1122 ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)
1129 row = SP_PARM->_cursrow;
1130 col = SP_PARM->_curscol;
1132 UpdateAttrs(SP_PARM, blank);
1133 TPUTS_TRACE("clr_eos");
1134 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1136 screen_lines(SP_PARM) - row,
1137 NCURSES_SP_NAME(_nc_outch));
1139 while (col < screen_columns(SP_PARM))
1140 CurScreen(SP_PARM)->_line[row].text[col++] = blank;
1142 for (row++; row < screen_lines(SP_PARM); row++) {
1143 for (col = 0; col < screen_columns(SP_PARM); col++)
1144 CurScreen(SP_PARM)->_line[row].text[col] = blank;
1151 * Test if clearing the end of the screen would satisfy part of the
1152 * screen-update. Do this by scanning backwards through the lines in the
1153 * screen, checking if each is blank, and one or more are changed.
1156 ClrBottom(NCURSES_SP_DCLx int total)
1161 int last = min(screen_columns(SP_PARM), NewScreen(SP_PARM)->_maxx + 1);
1162 NCURSES_CH_T blank = NewScreen(SP_PARM)->_line[total - 1].text[last - 1];
1165 if (clr_eos && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1167 for (row = total - 1; row >= 0; row--) {
1168 for (col = 0, ok = TRUE; ok && col < last; col++) {
1169 ok = (CharEq(NewScreen(SP_PARM)->_line[row].text[col], blank));
1174 for (col = 0; ok && col < last; col++) {
1175 ok = (CharEq(CurScreen(SP_PARM)->_line[row].text[col], blank));
1181 /* don't use clr_eos for just one line if clr_eol available */
1183 GoTo(NCURSES_SP_ARGx top, 0);
1184 ClrToEOS(NCURSES_SP_ARGx blank);
1185 if (SP_PARM->oldhash && SP_PARM->newhash) {
1186 for (row = top; row < screen_lines(SP_PARM); row++)
1187 SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1195 #if USE_WIDEC_SUPPORT
1196 #define check_xmc_transition(sp, a, b) \
1197 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & (sp)->_xmc_triggers) != 0)
1198 #define xmc_turn_on(sp,a,b) check_xmc_transition(sp,&(a), &(b))
1200 #define xmc_turn_on(sp,a,b) ((((a)^(b)) & ~(a) & (sp)->_xmc_triggers) != 0)
1203 #define xmc_new(sp,r,c) NewScreen(sp)->_line[r].text[c]
1204 #define xmc_turn_off(sp,a,b) xmc_turn_on(sp,b,a)
1205 #endif /* USE_XMC_SUPPORT */
1208 ** TransformLine(lineno)
1210 ** Transform the given line in curscr to the one in newscr, using
1211 ** Insert/Delete Character if idcok && has_ic().
1213 ** firstChar = position of first different character in line
1214 ** oLastChar = position of last different character in old line
1215 ** nLastChar = position of last different character in new line
1217 ** move to firstChar
1218 ** overwrite chars up to min(oLastChar, nLastChar)
1219 ** if oLastChar < nLastChar
1220 ** insert newLine[oLastChar+1..nLastChar]
1222 ** delete oLastChar - nLastChar spaces
1226 TransformLine(NCURSES_SP_DCLx int const lineno)
1228 int firstChar, oLastChar, nLastChar;
1229 NCURSES_CH_T *newLine = NewScreen(SP_PARM)->_line[lineno].text;
1230 NCURSES_CH_T *oldLine = CurScreen(SP_PARM)->_line[lineno].text;
1232 bool attrchanged = FALSE;
1234 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%p, %d)"), (void *) SP_PARM, lineno));
1236 /* copy new hash value to old one */
1237 if (SP_PARM->oldhash && SP_PARM->newhash)
1238 SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1241 * If we have colors, there is the possibility of having two color pairs
1242 * that display as the same colors. For instance, Lynx does this. Check
1243 * for this case, and update the old line with the new line's colors when
1244 * they are equivalent.
1246 if (SP_PARM->_coloron) {
1250 for (n = 0; n < screen_columns(SP_PARM); n++) {
1251 if (!CharEq(newLine[n], oldLine[n])) {
1252 oldPair = GetPair(oldLine[n]);
1253 newPair = GetPair(newLine[n]);
1254 if (oldPair != newPair
1255 && unColor(oldLine[n]) == unColor(newLine[n])) {
1256 if (oldPair < SP_PARM->_pair_limit
1257 && newPair < SP_PARM->_pair_limit
1258 && (SP_PARM->_color_pairs[oldPair] ==
1259 SP_PARM->_color_pairs[newPair])) {
1260 SetPair(oldLine[n], GetPair(newLine[n]));
1267 if (ceol_standout_glitch && clr_eol) {
1269 while (firstChar < screen_columns(SP_PARM)) {
1270 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1280 if (attrchanged) { /* we may have to disregard the whole line */
1281 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1282 ClrToEOL(NCURSES_SP_ARGx
1283 ClrBlank(NCURSES_SP_ARGx
1284 CurScreen(SP_PARM)), FALSE);
1285 PutRange(NCURSES_SP_ARGx
1286 oldLine, newLine, lineno, 0,
1287 screen_columns(SP_PARM) - 1);
1291 * This is a very simple loop to paint characters which may have the
1292 * magic cookie glitch embedded. It doesn't know much about video
1293 * attributes which are continued from one line to the next. It
1294 * assumes that we have filtered out requests for attribute changes
1295 * that do not get mapped to blank positions.
1297 * FIXME: we are not keeping track of where we put the cookies, so this
1298 * will work properly only once, since we may overwrite a cookie in a
1299 * following operation.
1301 } else if (magic_cookie_glitch > 0) {
1302 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1303 for (n = 0; n < screen_columns(SP_PARM); n++) {
1304 int m = n + magic_cookie_glitch;
1306 /* check for turn-on:
1307 * If we are writing an attributed blank, where the
1308 * previous cell is not attributed.
1310 if (ISBLANK(newLine[n])
1312 && xmc_turn_on(SP_PARM, newLine[n - 1], newLine[n]))
1315 && xmc_turn_on(SP_PARM,
1316 xmc_new(SP_PARM, lineno - 1,
1317 screen_columns(SP_PARM) - 1),
1322 PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1324 /* check for turn-off:
1325 * If we are writing an attributed non-blank, where the
1326 * next cell is blank, and not attributed.
1328 if (!ISBLANK(newLine[n])
1329 && ((n + 1 < screen_columns(SP_PARM)
1330 && xmc_turn_off(SP_PARM, newLine[n], newLine[n + 1]))
1331 || (n + 1 >= screen_columns(SP_PARM)
1332 && lineno + 1 < screen_lines(SP_PARM)
1333 && xmc_turn_off(SP_PARM,
1335 xmc_new(SP_PARM, lineno + 1, 0))))) {
1344 /* it may be cheap to clear leading whitespace with clr_bol */
1346 if (clr_bol && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1347 int oFirstChar, nFirstChar;
1349 for (oFirstChar = 0;
1350 oFirstChar < screen_columns(SP_PARM);
1352 if (!CharEq(oldLine[oFirstChar], blank))
1354 for (nFirstChar = 0;
1355 nFirstChar < screen_columns(SP_PARM);
1357 if (!CharEq(newLine[nFirstChar], blank))
1360 if (nFirstChar == oFirstChar) {
1361 firstChar = nFirstChar;
1362 /* find the first differing character */
1363 while (firstChar < screen_columns(SP_PARM)
1364 && CharEq(newLine[firstChar], oldLine[firstChar]))
1366 } else if (oFirstChar > nFirstChar) {
1367 firstChar = nFirstChar;
1368 } else { /* oFirstChar < nFirstChar */
1369 firstChar = oFirstChar;
1370 if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1371 if (nFirstChar >= screen_columns(SP_PARM)
1372 && SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1373 GoTo(NCURSES_SP_ARGx lineno, 0);
1374 UpdateAttrs(SP_PARM, blank);
1375 TPUTS_TRACE("clr_eol");
1376 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1378 GoTo(NCURSES_SP_ARGx lineno, nFirstChar - 1);
1379 UpdateAttrs(SP_PARM, blank);
1380 TPUTS_TRACE("clr_bol");
1381 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_bol);
1384 while (firstChar < nFirstChar)
1385 oldLine[firstChar++] = blank;
1389 /* find the first differing character */
1390 while (firstChar < screen_columns(SP_PARM)
1391 && CharEq(newLine[firstChar], oldLine[firstChar]))
1394 /* if there wasn't one, we're done */
1395 if (firstChar >= screen_columns(SP_PARM)) {
1396 TR(TRACE_UPDATE, (T_RETURN("")));
1400 blank = newLine[screen_columns(SP_PARM) - 1];
1402 if (!can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1403 /* find the last differing character */
1404 nLastChar = screen_columns(SP_PARM) - 1;
1406 while (nLastChar > firstChar
1407 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1410 if (nLastChar >= firstChar) {
1411 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1412 PutRange(NCURSES_SP_ARGx
1418 memcpy(oldLine + firstChar,
1419 newLine + firstChar,
1420 (unsigned) (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1422 TR(TRACE_UPDATE, (T_RETURN("")));
1426 /* find last non-blank character on old line */
1427 oLastChar = screen_columns(SP_PARM) - 1;
1428 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1431 /* find last non-blank character on new line */
1432 nLastChar = screen_columns(SP_PARM) - 1;
1433 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1436 if ((nLastChar == firstChar)
1437 && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1438 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1439 if (!CharEq(newLine[firstChar], blank))
1440 PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1441 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1442 } else if ((nLastChar != oLastChar)
1443 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1444 || !(SP_PARM->_nc_sp_idcok
1445 && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG)))) {
1446 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1447 if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1448 if (PutRange(NCURSES_SP_ARGx
1454 GoTo(NCURSES_SP_ARGx lineno, nLastChar + 1);
1456 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1458 n = max(nLastChar, oLastChar);
1459 PutRange(NCURSES_SP_ARGx
1467 int nLastNonblank = nLastChar;
1468 int oLastNonblank = oLastChar;
1470 /* find the last characters that really differ */
1471 /* can be -1 if no characters differ */
1472 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1473 /* don't split a wide char */
1474 if (isWidecExt(newLine[nLastChar]) &&
1475 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1479 if (nLastChar == -1 || oLastChar == -1)
1483 n = min(oLastChar, nLastChar);
1484 if (n >= firstChar) {
1485 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1486 PutRange(NCURSES_SP_ARGx
1494 if (oLastChar < nLastChar) {
1495 int m = max(nLastNonblank, oLastNonblank);
1496 #if USE_WIDEC_SUPPORT
1497 while (isWidecExt(newLine[n + 1]) && n) {
1502 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1503 if ((nLastChar < nLastNonblank)
1504 || InsCharCost(SP_PARM, nLastChar - oLastChar) > (m - n)) {
1505 PutRange(NCURSES_SP_ARGx
1512 InsStr(NCURSES_SP_ARGx &newLine[n + 1], nLastChar - oLastChar);
1514 } else if (oLastChar > nLastChar) {
1515 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1516 if (DelCharCost(SP_PARM, oLastChar - nLastChar)
1517 > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1518 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1519 n + 1, nLastNonblank))
1520 GoTo(NCURSES_SP_ARGx lineno, nLastNonblank + 1);
1521 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1524 * The delete-char sequence will
1525 * effectively shift in blanks from the
1526 * right margin of the screen. Ensure
1527 * that they are the right color by
1528 * setting the video attributes from
1529 * the last character on the row.
1531 UpdateAttrs(SP_PARM, blank);
1532 DelChar(NCURSES_SP_ARGx oLastChar - nLastChar);
1538 /* update the code's internal representation */
1539 if (screen_columns(SP_PARM) > firstChar)
1540 memcpy(oldLine + firstChar,
1541 newLine + firstChar,
1542 (unsigned) (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1543 TR(TRACE_UPDATE, (T_RETURN("")));
1548 ** ClearScreen(blank)
1550 ** Clear the physical screen and put cursor at home
1555 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1558 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1560 TR(TRACE_UPDATE, ("ClearScreen() called"));
1562 #if NCURSES_EXT_FUNCS
1563 if (SP_PARM->_coloron
1564 && !SP_PARM->_default_color) {
1565 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
1566 (short) GET_SCREEN_PAIR(SP_PARM),
1569 NCURSES_SP_NAME(_nc_outch));
1570 if (!back_color_erase) {
1578 UpdateAttrs(SP_PARM, blank);
1579 TPUTS_TRACE("clear_screen");
1580 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clear_screen);
1581 SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1582 position_check(SP_PARM,
1586 } else if (clr_eos) {
1587 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1588 GoTo(NCURSES_SP_ARGx 0, 0);
1589 UpdateAttrs(SP_PARM, blank);
1590 TPUTS_TRACE("clr_eos");
1591 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1593 screen_lines(SP_PARM),
1594 NCURSES_SP_NAME(_nc_outch));
1595 } else if (clr_eol) {
1596 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1597 UpdateAttrs(SP_PARM, blank);
1598 for (i = 0; i < screen_lines(SP_PARM); i++) {
1599 GoTo(NCURSES_SP_ARGx i, 0);
1600 TPUTS_TRACE("clr_eol");
1601 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1603 GoTo(NCURSES_SP_ARGx 0, 0);
1606 UpdateAttrs(SP_PARM, blank);
1607 for (i = 0; i < screen_lines(SP_PARM); i++) {
1608 GoTo(NCURSES_SP_ARGx i, 0);
1609 for (j = 0; j < screen_columns(SP_PARM); j++)
1610 PutChar(NCURSES_SP_ARGx CHREF(blank));
1612 GoTo(NCURSES_SP_ARGx 0, 0);
1615 for (i = 0; i < screen_lines(SP_PARM); i++) {
1616 for (j = 0; j < screen_columns(SP_PARM); j++)
1617 CurScreen(SP_PARM)->_line[i].text[j] = blank;
1620 TR(TRACE_UPDATE, ("screen cleared"));
1624 ** InsStr(line, count)
1626 ** Insert the count characters pointed to by line.
1631 InsStr(NCURSES_SP_DCLx NCURSES_CH_T * line, int count)
1633 TR(TRACE_UPDATE, ("InsStr(%p, %p,%d) called",
1635 (void *) line, count));
1637 /* Prefer parm_ich as it has the smallest cost - no need to shift
1638 * the whole line on each character. */
1639 /* The order must match that of InsCharCost. */
1641 TPUTS_TRACE("parm_ich");
1642 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1643 TPARM_1(parm_ich, count),
1645 NCURSES_SP_NAME(_nc_outch));
1647 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1651 } else if (enter_insert_mode && exit_insert_mode) {
1652 TPUTS_TRACE("enter_insert_mode");
1653 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_insert_mode);
1655 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1656 if (insert_padding) {
1657 TPUTS_TRACE("insert_padding");
1658 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1663 TPUTS_TRACE("exit_insert_mode");
1664 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
1667 TPUTS_TRACE("insert_character");
1668 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_character);
1669 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1670 if (insert_padding) {
1671 TPUTS_TRACE("insert_padding");
1672 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1678 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "InsStr");
1684 ** Delete count characters at current position
1689 DelChar(NCURSES_SP_DCLx int count)
1693 TR(TRACE_UPDATE, ("DelChar(%p, %d) called, position = (%ld,%ld)",
1694 (void *) SP_PARM, count,
1695 (long) NewScreen(SP_PARM)->_cury,
1696 (long) NewScreen(SP_PARM)->_curx));
1699 TPUTS_TRACE("parm_dch");
1700 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1701 TPARM_1(parm_dch, count),
1703 NCURSES_SP_NAME(_nc_outch));
1705 for (n = 0; n < count; n++) {
1706 TPUTS_TRACE("delete_character");
1707 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_character);
1713 * Physical-scrolling support
1715 * This code was adapted from Keith Bostic's hardware scrolling
1716 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1717 * capabilities, narrowed the call interface slightly, and cleaned
1718 * up some convoluted tests. I also added support for the memory_above
1719 * memory_below, and non_dest_scroll_region capabilities.
1721 * For this code to work, we must have either
1722 * change_scroll_region and scroll forward/reverse commands, or
1723 * insert and delete line capabilities.
1724 * When the scrolling region has been set, the cursor has to
1725 * be at the last line of the region to make the scroll up
1726 * happen, or on the first line of region to scroll down.
1728 * This code makes one aesthetic decision in the opposite way from
1729 * BSD curses. BSD curses preferred pairs of il/dl operations
1730 * over scrolls, allegedly because il/dl looked faster. We, on
1731 * the other hand, prefer scrolls because (a) they're just as fast
1732 * on many terminals and (b) using them avoids bouncing an
1733 * unchanged bottom section of the screen up and down, which is
1736 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1738 * I used assumption that capabilities il/il1/dl/dl1 work inside
1739 * changed scroll region not shifting screen contents outside of it.
1740 * If there are any terminals behaving different way, it would be
1741 * necessary to add some conditions to scroll_csr_forward/backward.
1744 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1746 scroll_csr_forward(NCURSES_SP_DCLx
1756 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1757 GoTo(NCURSES_SP_ARGx bot, 0);
1758 UpdateAttrs(SP_PARM, blank);
1759 TPUTS_TRACE("scroll_forward");
1760 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1761 } else if (n == 1 && delete_line && bot == maxy) {
1762 GoTo(NCURSES_SP_ARGx top, 0);
1763 UpdateAttrs(SP_PARM, blank);
1764 TPUTS_TRACE("delete_line");
1765 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1766 } else if (parm_index && top == miny && bot == maxy) {
1767 GoTo(NCURSES_SP_ARGx bot, 0);
1768 UpdateAttrs(SP_PARM, blank);
1769 TPUTS_TRACE("parm_index");
1770 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1771 TPARM_2(parm_index, n, 0),
1773 NCURSES_SP_NAME(_nc_outch));
1774 } else if (parm_delete_line && bot == maxy) {
1775 GoTo(NCURSES_SP_ARGx top, 0);
1776 UpdateAttrs(SP_PARM, blank);
1777 TPUTS_TRACE("parm_delete_line");
1778 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1779 TPARM_2(parm_delete_line, n, 0),
1781 NCURSES_SP_NAME(_nc_outch));
1782 } else if (scroll_forward && top == miny && bot == maxy) {
1783 GoTo(NCURSES_SP_ARGx bot, 0);
1784 UpdateAttrs(SP_PARM, blank);
1785 for (i = 0; i < n; i++) {
1786 TPUTS_TRACE("scroll_forward");
1787 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1789 } else if (delete_line && bot == maxy) {
1790 GoTo(NCURSES_SP_ARGx top, 0);
1791 UpdateAttrs(SP_PARM, blank);
1792 for (i = 0; i < n; i++) {
1793 TPUTS_TRACE("delete_line");
1794 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1799 #if NCURSES_EXT_FUNCS
1800 if (FILL_BCE(SP_PARM)) {
1802 for (i = 0; i < n; i++) {
1803 GoTo(NCURSES_SP_ARGx bot - i, 0);
1804 for (j = 0; j < screen_columns(SP_PARM); j++)
1805 PutChar(NCURSES_SP_ARGx CHREF(blank));
1812 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1815 scroll_csr_backward(NCURSES_SP_DCLx
1825 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1826 GoTo(NCURSES_SP_ARGx top, 0);
1827 UpdateAttrs(SP_PARM, blank);
1828 TPUTS_TRACE("scroll_reverse");
1829 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1830 } else if (n == 1 && insert_line && bot == maxy) {
1831 GoTo(NCURSES_SP_ARGx top, 0);
1832 UpdateAttrs(SP_PARM, blank);
1833 TPUTS_TRACE("insert_line");
1834 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1835 } else if (parm_rindex && top == miny && bot == maxy) {
1836 GoTo(NCURSES_SP_ARGx top, 0);
1837 UpdateAttrs(SP_PARM, blank);
1838 TPUTS_TRACE("parm_rindex");
1839 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1840 TPARM_2(parm_rindex, n, 0),
1842 NCURSES_SP_NAME(_nc_outch));
1843 } else if (parm_insert_line && bot == maxy) {
1844 GoTo(NCURSES_SP_ARGx top, 0);
1845 UpdateAttrs(SP_PARM, blank);
1846 TPUTS_TRACE("parm_insert_line");
1847 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1848 TPARM_2(parm_insert_line, n, 0),
1850 NCURSES_SP_NAME(_nc_outch));
1851 } else if (scroll_reverse && top == miny && bot == maxy) {
1852 GoTo(NCURSES_SP_ARGx top, 0);
1853 UpdateAttrs(SP_PARM, blank);
1854 for (i = 0; i < n; i++) {
1855 TPUTS_TRACE("scroll_reverse");
1856 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1858 } else if (insert_line && bot == maxy) {
1859 GoTo(NCURSES_SP_ARGx top, 0);
1860 UpdateAttrs(SP_PARM, blank);
1861 for (i = 0; i < n; i++) {
1862 TPUTS_TRACE("insert_line");
1863 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1868 #if NCURSES_EXT_FUNCS
1869 if (FILL_BCE(SP_PARM)) {
1871 for (i = 0; i < n; i++) {
1872 GoTo(NCURSES_SP_ARGx top + i, 0);
1873 for (j = 0; j < screen_columns(SP_PARM); j++)
1874 PutChar(NCURSES_SP_ARGx CHREF(blank));
1881 /* scroll by using delete_line at del and insert_line at ins */
1884 scroll_idl(NCURSES_SP_DCLx int n, int del, int ins, NCURSES_CH_T blank)
1888 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1891 GoTo(NCURSES_SP_ARGx del, 0);
1892 UpdateAttrs(SP_PARM, blank);
1893 if (n == 1 && delete_line) {
1894 TPUTS_TRACE("delete_line");
1895 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1896 } else if (parm_delete_line) {
1897 TPUTS_TRACE("parm_delete_line");
1898 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1899 TPARM_2(parm_delete_line, n, 0),
1901 NCURSES_SP_NAME(_nc_outch));
1902 } else { /* if (delete_line) */
1903 for (i = 0; i < n; i++) {
1904 TPUTS_TRACE("delete_line");
1905 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1909 GoTo(NCURSES_SP_ARGx ins, 0);
1910 UpdateAttrs(SP_PARM, blank);
1911 if (n == 1 && insert_line) {
1912 TPUTS_TRACE("insert_line");
1913 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1914 } else if (parm_insert_line) {
1915 TPUTS_TRACE("parm_insert_line");
1916 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1917 TPARM_2(parm_insert_line, n, 0),
1919 NCURSES_SP_NAME(_nc_outch));
1920 } else { /* if (insert_line) */
1921 for (i = 0; i < n; i++) {
1922 TPUTS_TRACE("insert_line");
1923 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1931 * Note: some terminals require the cursor to be within the scrolling margins
1932 * before setting them. Generally, the cursor must be at the appropriate end
1933 * of the scrolling margins when issuing an indexing operation (it is not
1934 * apparent whether it must also be at the left margin; we do this just to be
1935 * safe). To make the related cursor movement a little faster, we use the
1936 * save/restore cursor capabilities if the terminal has them.
1939 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
1944 /* scroll region from top to bot by n lines */
1948 bool cursor_saved = FALSE;
1951 TR(TRACE_MOVE, ("_nc_scrolln(%p, %d, %d, %d, %d)",
1952 (void *) SP_PARM, n, top, bot, maxy));
1954 if (!IsValidScreen(SP_PARM))
1957 blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1961 * If we scroll, we might remove a cookie.
1963 if (magic_cookie_glitch > 0) {
1968 if (n > 0) { /* scroll up (forward) */
1970 * Explicitly clear if stuff pushed off top of region might
1971 * be saved by the terminal.
1973 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
1975 if (res == ERR && change_scroll_region) {
1976 if ((((n == 1 && scroll_forward) || parm_index)
1977 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
1978 && save_cursor && restore_cursor) {
1979 cursor_saved = TRUE;
1980 TPUTS_TRACE("save_cursor");
1981 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
1983 TPUTS_TRACE("change_scroll_region");
1984 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1985 TPARM_2(change_scroll_region, top, bot));
1987 TPUTS_TRACE("restore_cursor");
1988 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
1990 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1993 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
1995 TPUTS_TRACE("change_scroll_region");
1996 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1997 TPARM_2(change_scroll_region, 0, maxy));
1998 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2001 if (res == ERR && SP_PARM->_nc_sp_idlok)
2002 res = scroll_idl(NCURSES_SP_ARGx n, top, bot - n + 1, blank);
2005 * Clear the newly shifted-in text.
2008 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
2009 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2010 if (bot == maxy && clr_eos) {
2011 GoTo(NCURSES_SP_ARGx bot - n + 1, 0);
2012 ClrToEOS(NCURSES_SP_ARGx blank2);
2014 for (i = 0; i < n; i++) {
2015 GoTo(NCURSES_SP_ARGx bot - i, 0);
2016 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2021 } else { /* (n < 0) - scroll down (backward) */
2022 res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
2024 if (res == ERR && change_scroll_region) {
2026 && (SP_PARM->_cursrow == top ||
2027 SP_PARM->_cursrow == top - 1)
2028 && save_cursor && restore_cursor) {
2029 cursor_saved = TRUE;
2030 TPUTS_TRACE("save_cursor");
2031 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
2033 TPUTS_TRACE("change_scroll_region");
2034 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2035 TPARM_2(change_scroll_region, top, bot));
2037 TPUTS_TRACE("restore_cursor");
2038 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
2040 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2043 res = scroll_csr_backward(NCURSES_SP_ARGx
2044 -n, top, bot, top, bot, blank);
2046 TPUTS_TRACE("change_scroll_region");
2047 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2048 TPARM_2(change_scroll_region, 0, maxy));
2049 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2052 if (res == ERR && SP_PARM->_nc_sp_idlok)
2053 res = scroll_idl(NCURSES_SP_ARGx -n, bot + n + 1, top, blank);
2056 * Clear the newly shifted-in text.
2059 && (non_dest_scroll_region || (memory_above && top == 0))) {
2060 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2061 for (i = 0; i < -n; i++) {
2062 GoTo(NCURSES_SP_ARGx i + top, 0);
2063 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2071 _nc_scroll_window(CurScreen(SP_PARM), n,
2072 (NCURSES_SIZE_T) top,
2073 (NCURSES_SIZE_T) bot,
2076 /* shift hash values too - they can be reused */
2077 NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_ARGx n, top, bot);
2082 #if NCURSES_SP_FUNCS
2084 _nc_scrolln(int n, int top, int bot, int maxy)
2086 return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
2090 NCURSES_EXPORT(void)
2091 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_DCL0)
2095 /* make sure terminal is in a sane known state */
2096 SetAttr(SCREEN_ATTRS(SP_PARM), A_NORMAL);
2097 NewScreen(SP_PARM)->_clear = TRUE;
2099 /* reset color pairs and definitions */
2100 if (SP_PARM->_coloron || SP_PARM->_color_defs)
2101 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2103 /* restore user-defined colors, if any */
2104 if (SP_PARM->_color_defs < 0) {
2106 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
2107 for (n = 0; n < SP_PARM->_color_defs; ++n) {
2108 if (SP_PARM->_color_table[n].init) {
2109 NCURSES_SP_NAME(init_color) (NCURSES_SP_ARGx
2111 SP_PARM->_color_table[n].r,
2112 SP_PARM->_color_table[n].g,
2113 SP_PARM->_color_table[n].b);
2118 if (exit_attribute_mode)
2119 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_attribute_mode);
2121 /* turn off attributes */
2122 if (exit_alt_charset_mode)
2123 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_alt_charset_mode);
2124 if (exit_standout_mode)
2125 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_standout_mode);
2126 if (exit_underline_mode)
2127 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_underline_mode);
2129 if (exit_insert_mode)
2130 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
2131 if (enter_am_mode && exit_am_mode)
2132 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2138 #if NCURSES_SP_FUNCS
2139 NCURSES_EXPORT(void)
2140 _nc_screen_resume(void)
2142 NCURSES_SP_NAME(_nc_screen_resume) (CURRENT_SCREEN);
2146 NCURSES_EXPORT(void)
2147 NCURSES_SP_NAME(_nc_screen_init) (NCURSES_SP_DCL0)
2149 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
2152 #if NCURSES_SP_FUNCS
2153 NCURSES_EXPORT(void)
2154 _nc_screen_init(void)
2156 NCURSES_SP_NAME(_nc_screen_init) (CURRENT_SCREEN);
2160 /* wrap up screen handling */
2161 NCURSES_EXPORT(void)
2162 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2167 UpdateAttrs(SP_PARM, normal);
2168 #if NCURSES_EXT_FUNCS
2169 if (SP_PARM->_coloron
2170 && !SP_PARM->_default_color) {
2171 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2172 SP_PARM->_default_color = TRUE;
2173 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
2177 NCURSES_SP_NAME(_nc_outch));
2178 SP_PARM->_default_color = FALSE;
2180 TINFO_MVCUR(NCURSES_SP_ARGx
2183 screen_lines(SP_PARM) - 1,
2186 ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2189 if (SP_PARM->_color_defs) {
2190 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2194 #if NCURSES_SP_FUNCS
2195 NCURSES_EXPORT(void)
2196 _nc_screen_wrap(void)
2198 NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2203 NCURSES_EXPORT(void)
2204 NCURSES_SP_NAME(_nc_do_xmc_glitch) (NCURSES_SP_DCLx attr_t previous)
2207 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP_PARM)));
2211 SP_PARM->_curscol += magic_cookie_glitch;
2212 if (SP_PARM->_curscol >= SP_PARM->_columns)
2213 wrap_cursor(NCURSES_SP_ARG);
2214 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie",
2215 SP_PARM->_cursrow, SP_PARM->_curscol));
2222 #if NCURSES_SP_FUNCS
2223 NCURSES_EXPORT(void)
2224 _nc_do_xmc_glitch(attr_t previous)
2226 NCURSES_SP_NAME(_nc_do_xmc_glitch) (CURRENT_SCREEN, previous);
2230 #endif /* USE_XMC_SUPPORT */