1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
36 /*-----------------------------------------------------------------
40 * The routine doupdate() and its dependents.
41 * All physical output is concentrated here (except _nc_outch()
44 *-----------------------------------------------------------------*/
46 #include <curses.priv.h>
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.256 2009/05/17 00:13:49 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(%d, %d) from (%d, %d)",
196 row, col, SP_PARM->_cursrow, SP_PARM->_curscol));
198 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "GoTo");
200 mvcur(SP_PARM->_cursrow, SP_PARM->_curscol, row, col);
201 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "GoTo2");
204 static NCURSES_INLINE void
205 PutAttrChar(NCURSES_SP_DCLx CARG_CH_T ch)
211 NCURSES_CH_T attr = CHDEREF(ch);
213 TR(TRACE_CHARPUT, ("PutAttrChar(%s) at (%d, %d)",
215 SP_PARM->_cursrow, SP_PARM->_curscol));
216 #if USE_WIDEC_SUPPORT
218 * If this is not a valid character, there is nothing more to do.
220 if (isWidecExt(CHDEREF(ch))) {
221 TR(TRACE_CHARPUT, ("...skip"));
225 * Determine the number of character cells which the 'ch' value will use
226 * on the screen. It should be at least one.
228 if ((chlen = wcwidth(CharOf(CHDEREF(ch)))) <= 0) {
229 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
232 * If the character falls into any of these special cases, do
233 * not force the result to a blank:
235 * a) it is printable (this works around a bug in wcwidth()).
236 * b) use_legacy_coding() has been called to modify the treatment
238 * c) the acs_map[] has been initialized to allow codes 0-31
239 * to be rendered. This supports Linux console's "PC"
240 * characters. Codes 128-255 are allowed though this is
243 if (is8bits(CharOf(CHDEREF(ch)))
244 && (isprint(CharOf(CHDEREF(ch)))
245 || (SP_PARM->_legacy_coding > 0 && CharOf(CHDEREF(ch)) >= 160)
246 || (SP_PARM->_legacy_coding > 1 && CharOf(CHDEREF(ch)) >= 128)
247 || (AttrOf(attr) & A_ALTCHARSET
248 && ((CharOfD(ch) < ACS_LEN
249 && SP_PARM->_acs_map != 0
250 && SP_PARM->_acs_map[CharOfD(ch)] != 0)
251 || (CharOfD(ch) >= 128))))) {
255 TR(TRACE_CHARPUT, ("forced to blank"));
261 if ((AttrOf(attr) & A_ALTCHARSET)
262 && SP_PARM->_acs_map != 0
263 && CharOfD(ch) < ACS_LEN) {
264 my_ch = CHDEREF(ch); /* work around const param */
265 #if USE_WIDEC_SUPPORT
267 * This is crude & ugly, but works most of the time. It checks if the
268 * acs_chars string specified that we have a mapping for this
269 * character, and uses the wide-character mapping when we expect the
270 * normal one to be broken (by mis-design ;-).
272 if (SP_PARM->_screen_acs_fix
273 && SP_PARM->_screen_acs_map[CharOf(my_ch)]) {
274 RemAttr(attr, A_ALTCHARSET);
275 my_ch = _nc_wacs[CharOf(my_ch)];
279 * If we (still) have alternate character set, it is the normal 8bit
280 * flavor. The _screen_acs_map[] array tells if the character was
281 * really in acs_chars, needed because of the way wide/normal line
282 * drawing flavors are integrated.
284 if (AttrOf(attr) & A_ALTCHARSET) {
286 chtype temp = UChar(SP_PARM->_acs_map[j]);
288 if (!(SP_PARM->_screen_acs_map[j])) {
289 RemAttr(attr, A_ALTCHARSET);
294 SetChar(my_ch, temp, AttrOf(attr));
298 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
299 SetChar(tilde, L('`'), AttrOf(attr));
303 UpdateAttrs(SP_PARM, attr);
304 #if !USE_WIDEC_SUPPORT
305 /* FIXME - we do this special case for signal handling, should see how to
306 * make it work for wide characters.
308 if (SP_PARM->_outch != 0) {
309 SP_PARM->_outch(NCURSES_SP_ARGx UChar(ch));
313 PUTC(CHDEREF(ch), SP_PARM->_ofp); /* macro's fastest... */
316 SP_PARM->_curscol += chlen;
318 TPUTS_TRACE("char_padding");
319 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx char_padding);
324 check_pending(NCURSES_SP_DCL0)
325 /* check for pending input */
327 bool have_pending = FALSE;
330 * Only carry out this check when the flag is zero, otherwise we'll
331 * have the refreshing slow down drastically (or stop) if there's an
332 * unread character available.
334 if (SP_PARM->_fifohold != 0)
337 if (SP_PARM->_checkfd >= 0) {
339 struct pollfd fds[1];
340 fds[0].fd = SP_PARM->_checkfd;
341 fds[0].events = POLLIN;
342 if (poll(fds, 1, 0) > 0) {
345 #elif defined(__BEOS__)
347 * BeOS's select() is declared in socket.h, so the configure script does
348 * not see it. That's just as well, since that function works only for
349 * sockets. This (using snooze and ioctl) was distilled from Be's patch
350 * for ncurses which uses a separate thread to simulate select().
352 * FIXME: the return values from the ioctl aren't very clear if we get
356 int howmany = ioctl(0, 'ichr', &n);
357 if (howmany >= 0 && n > 0) {
362 struct timeval ktimeout;
365 ktimeout.tv_usec = 0;
368 FD_SET(SP_PARM->_checkfd, &fdset);
369 if (select(SP_PARM->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
375 SP_PARM->_fifohold = 5;
376 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
381 /* put char at lower right corner */
383 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
385 if (!auto_right_margin) {
386 /* we can put the char directly */
387 PutAttrChar(NCURSES_SP_ARGx ch);
388 } else if (enter_am_mode && exit_am_mode) {
389 /* we can suppress automargin */
390 TPUTS_TRACE("exit_am_mode");
391 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_am_mode);
393 PutAttrChar(NCURSES_SP_ARGx ch);
395 position_check(SP_PARM,
400 TPUTS_TRACE("enter_am_mode");
401 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_am_mode);
402 } else if ((enter_insert_mode && exit_insert_mode)
403 || insert_character || parm_ich) {
405 screen_lines(SP_PARM) - 1,
406 screen_columns(SP_PARM) - 2);
407 PutAttrChar(NCURSES_SP_ARGx ch);
409 screen_lines(SP_PARM) - 1,
410 screen_columns(SP_PARM) - 2);
411 InsStr(NCURSES_SP_ARGx
412 NewScreen(SP_PARM)->_line[screen_lines(SP_PARM) - 1].text +
413 screen_columns(SP_PARM) - 2, 1);
418 * Wrap the cursor position, i.e., advance to the beginning of the next line.
421 wrap_cursor(NCURSES_SP_DCL0)
423 if (eat_newline_glitch) {
425 * xenl can manifest two different ways. The vt100 way is that, when
426 * you'd expect the cursor to wrap, it stays hung at the right margin
427 * (on top of the character just emitted) and doesn't wrap until the
428 * *next* graphic char is emitted. The c100 way is to ignore LF
429 * received just after an am wrap.
431 * An aggressive way to handle this would be to emit CR/LF after the
432 * char and then assume the wrap is done, you're on the first position
433 * of the next line, and the terminal out of its weird state. Here
434 * it's safe to just tell the code that the cursor is in hyperspace and
435 * let the next mvcur() call straighten things out.
437 SP_PARM->_curscol = -1;
438 SP_PARM->_cursrow = -1;
439 } else if (auto_right_margin) {
440 SP_PARM->_curscol = 0;
443 * We've actually moved - but may have to work around problems with
444 * video attributes not working.
446 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP_PARM))) {
447 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
448 (unsigned long) AttrOf(SCREEN_ATTRS(SP_PARM)),
449 _traceattr(AttrOf(SCREEN_ATTRS(SP_PARM)))));
450 (void) VIDATTR(SP_PARM, A_NORMAL, 0);
455 position_check(SP_PARM,
461 static NCURSES_INLINE void
462 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
463 /* insert character, handling automargin stuff */
465 if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1 &&
466 SP_PARM->_curscol == screen_columns(SP_PARM) - 1) {
467 PutCharLR(NCURSES_SP_ARGx ch);
469 PutAttrChar(NCURSES_SP_ARGx ch);
472 if (SP_PARM->_curscol >= screen_columns(SP_PARM))
473 wrap_cursor(NCURSES_SP_ARG);
475 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "PutChar");
479 * Check whether the given character can be output by clearing commands. This
480 * includes test for being a space and not including any 'bad' attributes, such
481 * as A_REVERSE. All attribute flags which don't affect appearance of a space
482 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
484 static NCURSES_INLINE bool
485 can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)
487 if (!back_color_erase && SP_PARM->_coloron) {
488 #if NCURSES_EXT_FUNCS
491 if (!SP_PARM->_default_color)
493 if (SP_PARM->_default_fg != C_MASK || SP_PARM->_default_bg != C_MASK)
495 if ((pair = GetPair(CHDEREF(ch))) != 0) {
497 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx pair, &fg, &bg);
498 if (fg != C_MASK || bg != C_MASK)
502 if (AttrOfD(ch) & A_COLOR)
506 return (ISBLANK(CHDEREF(ch)) &&
507 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
511 * Issue a given span of characters from an array.
512 * Must be functionally equivalent to:
513 * for (i = 0; i < num; i++)
515 * but can leave the cursor positioned at the middle of the interval.
517 * Returns: 0 - cursor is at the end of interval
518 * 1 - cursor is somewhere in the middle
520 * This code is optimized using ech and rep.
523 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext, int num)
527 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
529 if (erase_chars || repeat_char) {
534 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
535 PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
541 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
546 while (runcount < num && CharEq(ntext[runcount], ntext0))
550 * The cost expression in the middle isn't exactly right.
551 * _cup_ch_cost is an upper bound on the cost for moving to the
552 * end of the erased area, but not the cost itself (which we
553 * can't compute without emitting the move). This may result
554 * in erase_chars not getting used in some situations for
555 * which it would be marginally advantageous.
558 && runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
559 && can_clear_with(NCURSES_SP_ARGx CHREF(ntext0))) {
560 UpdateAttrs(SP_PARM, ntext0);
561 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx TPARM_1(erase_chars, runcount));
564 * If this is the last part of the given interval,
565 * don't bother moving cursor, since it can be the
566 * last update on the line.
568 if (runcount < num) {
571 SP_PARM->_curscol + runcount);
573 return 1; /* cursor stays in the middle */
575 } else if (repeat_char && runcount > SP_PARM->_rep_cost) {
576 bool wrap_possible = (SP_PARM->_curscol + runcount >=
577 screen_columns(SP_PARM));
578 int rep_count = runcount;
583 UpdateAttrs(SP_PARM, ntext0);
584 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
589 NCURSES_SP_NAME(_nc_outch));
590 SP_PARM->_curscol += rep_count;
593 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
595 for (i = 0; i < runcount; i++)
596 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
604 for (i = 0; i < num; i++)
605 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
610 * Output the line in the given range [first .. last]
612 * If there's a run of identical characters that's long enough to justify
613 * cursor movement, use that also.
615 * Returns: same as EmitRange
618 PutRange(NCURSES_SP_DCLx
619 const NCURSES_CH_T * otext,
620 const NCURSES_CH_T * ntext,
626 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %p, %d, %d, %d)",
627 SP_PARM, otext, ntext, row, first, last));
630 && (last - first + 1) > SP_PARM->_inline_cost) {
631 for (j = first, same = 0; j <= last; j++) {
632 if (!same && isWidecExt(otext[j]))
634 if (CharEq(otext[j], ntext[j])) {
637 if (same > SP_PARM->_inline_cost) {
638 EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
639 GoTo(NCURSES_SP_ARGx row, first = j);
644 i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
646 * Always return 1 for the next GoTo() after a PutRange() if we found
647 * identical characters at end of interval
649 return (same == 0 ? i : 1);
651 return EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
654 /* leave unbracketed here so 'indent' works */
655 #define MARK_NOCHANGE(win,row) \
656 win->_line[row].firstchar = _NOCHANGE; \
657 win->_line[row].lastchar = _NOCHANGE; \
658 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
661 NCURSES_SP_NAME(doupdate) (NCURSES_SP_DCL0)
666 struct tms before, after;
667 #endif /* USE_TRACE_TIMES */
669 T((T_CALLED("doupdate()")));
673 * It is "legal" but unlikely that an application could assign a new
674 * value to one of the standard windows. Check for that possibility
675 * and try to recover.
677 * We do not allow applications to assign new values in the reentrant
680 #define SyncScreens(internal,exported) \
681 if (internal == 0) internal = exported; \
682 if (internal != exported) exported = internal
684 SyncScreens(CurScreen(SP_PARM), curscr);
685 SyncScreens(NewScreen(SP_PARM), newscr);
686 SyncScreens(StdScreen(SP_PARM), stdscr);
689 if (CurScreen(SP_PARM) == 0
690 || NewScreen(SP_PARM) == 0
691 || StdScreen(SP_PARM) == 0)
695 if (USE_TRACEF(TRACE_UPDATE)) {
696 if (CurScreen(SP_PARM)->_clear)
697 _tracef("curscr is clear");
699 _tracedump("curscr", CurScreen(SP_PARM));
700 _tracedump("newscr", NewScreen(SP_PARM));
701 _nc_unlock_global(tracef);
705 _nc_signal_handler(FALSE);
707 if (SP_PARM->_fifohold)
708 SP_PARM->_fifohold--;
711 if (SP_PARM->_endwin || _nc_handle_sigwinch(SP_PARM)) {
713 * This is a transparent extension: XSI does not address it,
714 * and applications need not know that ncurses can do it.
716 * Check if the terminal size has changed while curses was off
717 * (this can happen in an xterm, for example), and resize the
718 * ncurses data structures accordingly.
720 _nc_update_screensize(SP_PARM);
724 if (SP_PARM->_endwin) {
726 T(("coming back from shell mode"));
727 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_ARG);
729 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG);
730 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
731 SP_PARM->_mouse_resume(SP_PARM);
733 SP_PARM->_endwin = FALSE;
736 /* zero the metering machinery */
738 (void) times(&before);
739 #endif /* USE_TRACE_TIMES */
742 * This is the support for magic-cookie terminals. The theory: we scan
743 * the virtual screen looking for attribute turnons. Where we find one,
744 * check to make sure it's realizable by seeing if the required number of
745 * un-attributed blanks are present before and after the attributed range;
746 * try to shift the range boundaries over blanks (not changing the screen
747 * display) so this becomes true. If it is, shift the beginning attribute
748 * change appropriately (the end one, if we've gotten this far, is
749 * guaranteed room for its cookie). If not, nuke the added attributes out
753 if (magic_cookie_glitch > 0) {
755 attr_t rattr = A_NORMAL;
757 for (i = 0; i < screen_lines(SP_PARM); i++) {
758 for (j = 0; j < screen_columns(SP_PARM); j++) {
760 NCURSES_CH_T *thisline = NewScreen(SP_PARM)->_line[i].text;
761 attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
762 attr_t turnon = thisattr & ~rattr;
764 /* is an attribute turned on here? */
770 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
771 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
774 * If the attribute change location is a blank with a "safe"
775 * attribute, undo the attribute turnon. This may ensure
776 * there's enough room to set the attribute before the first
777 * non-blank in the run.
779 #define SAFE(scr,a) (!((a) & (scr)->_xmc_triggers))
780 if (ISBLANK(thisline[j]) && SAFE(SP_PARM, turnon)) {
781 RemAttr(thisline[j], turnon);
785 /* check that there's enough room at start of span */
786 for (k = 1; k <= magic_cookie_glitch; k++) {
788 || !ISBLANK(thisline[j - k])
789 || !SAFE(SP_PARM, AttrOf(thisline[j - k]))) {
791 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
793 (ISBLANK(thisline[j - k])
796 (SAFE(SP_PARM, AttrOf(thisline[j - k]))
803 bool end_onscreen = FALSE;
806 /* find end of span, if it's onscreen */
807 for (m = i; m < screen_lines(SP_PARM); m++) {
808 for (; n < screen_columns(SP_PARM); n++) {
810 = AttrOf(NewScreen(SP_PARM)->_line[m].text[n]);
812 if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
815 ("Range attributed with %s ends at (%d, %d)",
816 _traceattr(turnon), m, n));
823 ("Range attributed with %s ends offscreen",
824 _traceattr(turnon)));
828 NCURSES_CH_T *lastline =
829 NewScreen(SP_PARM)->_line[m].text;
832 * If there are safely-attributed blanks at the end of
833 * the range, shorten the range. This will help ensure
834 * that there is enough room at end of span.
837 && ISBLANK(lastline[n])
838 && SAFE(SP_PARM, AttrOf(lastline[n]))) {
839 RemAttr(lastline[n--], turnon);
842 /* check that there's enough room at end of span */
843 for (k = 1; k <= magic_cookie_glitch; k++) {
844 if (n + k >= screen_columns(SP_PARM)
845 || !ISBLANK(lastline[n + k])
846 || !SAFE(SP_PARM, AttrOf(lastline[n + k]))) {
849 ("No room at end in %d,%d%s%s",
851 (ISBLANK(lastline[n + k])
854 (SAFE(SP_PARM, AttrOf(lastline[n + k]))
867 ("Clearing %s beginning at (%d, %d)",
868 _traceattr(turnon), i, j));
870 /* turn off new attributes over span */
871 for (p = i; p < screen_lines(SP_PARM); p++) {
872 for (; q < screen_columns(SP_PARM); q++) {
874 AttrOf(NewScreen(SP_PARM)->_line[p].text[q]);
875 if ((testattr & SP_PARM->_xmc_triggers) == rattr)
877 RemAttr(NewScreen(SP_PARM)->_line[p].text[q], turnon);
884 ("Cookie space for %s found before (%d, %d)",
885 _traceattr(turnon), i, j));
888 * Back up the start of range so there's room for cookies
889 * before the first nonblank character.
891 for (k = 1; k <= magic_cookie_glitch; k++)
892 AddAttr(thisline[j - k], turnon);
900 /* show altered highlights after magic-cookie check */
901 if (USE_TRACEF(TRACE_UPDATE)) {
902 _tracef("After magic-cookie check...");
903 _tracedump("newscr", NewScreen(SP_PARM));
904 _nc_unlock_global(tracef);
908 #endif /* USE_XMC_SUPPORT */
911 if (CurScreen(SP_PARM)->_clear || NewScreen(SP_PARM)->_clear) { /* force refresh ? */
912 ClrUpdate(NCURSES_SP_ARG);
913 CurScreen(SP_PARM)->_clear = FALSE; /* reset flag */
914 NewScreen(SP_PARM)->_clear = FALSE; /* reset flag */
916 int changedlines = CHECK_INTERVAL;
918 if (check_pending(NCURSES_SP_ARG))
921 nonempty = min(screen_lines(SP_PARM), NewScreen(SP_PARM)->_maxy + 1);
923 if (SP_PARM->_scrolling) {
924 _nc_scroll_optimize();
927 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
929 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
930 for (i = 0; i < nonempty; i++) {
932 * Here is our line-breakout optimization.
934 if (changedlines == CHECK_INTERVAL) {
935 if (check_pending(NCURSES_SP_ARG))
941 * newscr.line[i].firstchar is normally set
942 * by wnoutrefresh. curscr.line[i].firstchar
943 * is normally set by _nc_scroll_window in the
944 * vertical-movement optimization code,
946 if (NewScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE
947 || CurScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE) {
948 TransformLine(NCURSES_SP_ARGx i);
952 /* mark line changed successfully */
953 if (i <= NewScreen(SP_PARM)->_maxy) {
954 MARK_NOCHANGE(NewScreen(SP_PARM), i);
956 if (i <= CurScreen(SP_PARM)->_maxy) {
957 MARK_NOCHANGE(CurScreen(SP_PARM), i);
962 /* put everything back in sync */
963 for (i = nonempty; i <= NewScreen(SP_PARM)->_maxy; i++) {
964 MARK_NOCHANGE(NewScreen(SP_PARM), i);
966 for (i = nonempty; i <= CurScreen(SP_PARM)->_maxy; i++) {
967 MARK_NOCHANGE(CurScreen(SP_PARM), i);
970 if (!NewScreen(SP_PARM)->_leaveok) {
971 CurScreen(SP_PARM)->_curx = NewScreen(SP_PARM)->_curx;
972 CurScreen(SP_PARM)->_cury = NewScreen(SP_PARM)->_cury;
974 GoTo(NCURSES_SP_ARGx CurScreen(SP_PARM)->_cury, CurScreen(SP_PARM)->_curx);
979 * We would like to keep the physical screen in normal mode in case we get
980 * other processes writing to the screen. This goal cannot be met for
981 * magic cookies since it interferes with attributes that may propagate
982 * past the current position.
985 if (magic_cookie_glitch != 0)
987 UpdateAttrs(SP_PARM, normal);
989 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
990 WINDOW_ATTRS(CurScreen(SP_PARM)) = WINDOW_ATTRS(NewScreen(SP_PARM));
993 (void) times(&after);
995 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
997 (long) (after.tms_stime - before.tms_stime),
998 (long) (after.tms_utime - before.tms_utime)));
999 #endif /* USE_TRACE_TIMES */
1001 _nc_signal_handler(TRUE);
1006 #if NCURSES_SP_FUNCS
1010 return NCURSES_SP_NAME(doupdate) (CURRENT_SCREEN);
1017 * Returns the attributed character that corresponds to the "cleared"
1018 * screen. If the terminal has the back-color-erase feature, this will be
1019 * colored according to the wbkgd() call.
1021 * We treat 'curscr' specially because it isn't supposed to be set directly
1022 * in the wbkgd() call. Assume 'stdscr' for this case.
1024 #define BCE_ATTRS (A_NORMAL|A_COLOR)
1025 #define BCE_BKGD(win) (((win) == CurScreen(SP_PARM) ? StdScreen(SP_PARM) : (win))->_nc_bkgd)
1027 static NCURSES_INLINE NCURSES_CH_T
1028 ClrBlank(NCURSES_SP_DCLx WINDOW *win)
1030 NCURSES_CH_T blank = blankchar;
1031 if (back_color_erase)
1032 AddAttr(blank, (AttrOf(BCE_BKGD(win)) & BCE_ATTRS));
1039 ** Update by clearing and redrawing the entire screen.
1044 ClrUpdate(NCURSES_SP_DCL0)
1046 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1049 NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1050 int nonempty = min(screen_lines(SP_PARM),
1051 NewScreen(SP_PARM)->_maxy + 1);
1053 ClearScreen(NCURSES_SP_ARGx blank);
1055 TR(TRACE_UPDATE, ("updating screen from scratch"));
1057 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1059 for (i = 0; i < nonempty; i++)
1060 TransformLine(NCURSES_SP_ARGx i);
1062 TR(TRACE_UPDATE, (T_RETURN("")));
1068 ** Clear to end of current line, starting at the cursor position
1072 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, bool needclear)
1076 if (CurScreen(SP_PARM) != 0
1077 && SP_PARM->_cursrow >= 0) {
1078 for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1081 &(CurScreen(SP_PARM)->_line[SP_PARM->_cursrow].text[j]);
1083 if (!CharEq(*cp, blank)) {
1094 UpdateAttrs(SP_PARM, blank);
1095 TPUTS_TRACE("clr_eol");
1096 if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1097 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1099 int count = (screen_columns(SP_PARM) - SP_PARM->_curscol);
1101 PutChar(NCURSES_SP_ARGx CHREF(blank));
1109 ** Clear to end of screen, starting at the cursor position
1113 ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)
1120 row = SP_PARM->_cursrow;
1121 col = SP_PARM->_curscol;
1123 UpdateAttrs(SP_PARM, blank);
1124 TPUTS_TRACE("clr_eos");
1125 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1127 screen_lines(SP_PARM) - row,
1128 NCURSES_SP_NAME(_nc_outch));
1130 while (col < screen_columns(SP_PARM))
1131 CurScreen(SP_PARM)->_line[row].text[col++] = blank;
1133 for (row++; row < screen_lines(SP_PARM); row++) {
1134 for (col = 0; col < screen_columns(SP_PARM); col++)
1135 CurScreen(SP_PARM)->_line[row].text[col] = blank;
1142 * Test if clearing the end of the screen would satisfy part of the
1143 * screen-update. Do this by scanning backwards through the lines in the
1144 * screen, checking if each is blank, and one or more are changed.
1147 ClrBottom(NCURSES_SP_DCLx int total)
1152 int last = min(screen_columns(SP_PARM), NewScreen(SP_PARM)->_maxx + 1);
1153 NCURSES_CH_T blank = NewScreen(SP_PARM)->_line[total - 1].text[last - 1];
1156 if (clr_eos && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1158 for (row = total - 1; row >= 0; row--) {
1159 for (col = 0, ok = TRUE; ok && col < last; col++) {
1160 ok = (CharEq(NewScreen(SP_PARM)->_line[row].text[col], blank));
1165 for (col = 0; ok && col < last; col++) {
1166 ok = (CharEq(CurScreen(SP_PARM)->_line[row].text[col], blank));
1172 /* don't use clr_eos for just one line if clr_eol available */
1174 GoTo(NCURSES_SP_ARGx top, 0);
1175 ClrToEOS(NCURSES_SP_ARGx blank);
1176 if (SP_PARM->oldhash && SP_PARM->newhash) {
1177 for (row = top; row < screen_lines(SP_PARM); row++)
1178 SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1186 #if USE_WIDEC_SUPPORT
1187 #define check_xmc_transition(sp, a, b) \
1188 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & (sp)->_xmc_triggers) != 0)
1189 #define xmc_turn_on(sp,a,b) check_xmc_transition(sp,&(a), &(b))
1191 #define xmc_turn_on(sp,a,b) ((((a)^(b)) & ~(a) & (sp)->_xmc_triggers) != 0)
1194 #define xmc_new(sp,r,c) NewScreen(SP_PARM)->_line[r].text[c]
1195 #define xmc_turn_off(sp,a,b) xmc_turn_on(sp,b,a)
1196 #endif /* USE_XMC_SUPPORT */
1199 ** TransformLine(lineno)
1201 ** Transform the given line in curscr to the one in newscr, using
1202 ** Insert/Delete Character if _nc_idcok && has_ic().
1204 ** firstChar = position of first different character in line
1205 ** oLastChar = position of last different character in old line
1206 ** nLastChar = position of last different character in new line
1208 ** move to firstChar
1209 ** overwrite chars up to min(oLastChar, nLastChar)
1210 ** if oLastChar < nLastChar
1211 ** insert newLine[oLastChar+1..nLastChar]
1213 ** delete oLastChar - nLastChar spaces
1217 TransformLine(NCURSES_SP_DCLx int const lineno)
1219 int firstChar, oLastChar, nLastChar;
1220 NCURSES_CH_T *newLine = NewScreen(SP_PARM)->_line[lineno].text;
1221 NCURSES_CH_T *oldLine = CurScreen(SP_PARM)->_line[lineno].text;
1223 bool attrchanged = FALSE;
1225 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%p, %d)"), SP_PARM, lineno));
1227 /* copy new hash value to old one */
1228 if (SP_PARM->oldhash && SP_PARM->newhash)
1229 SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1232 * If we have colors, there is the possibility of having two color pairs
1233 * that display as the same colors. For instance, Lynx does this. Check
1234 * for this case, and update the old line with the new line's colors when
1235 * they are equivalent.
1237 if (SP_PARM->_coloron) {
1241 for (n = 0; n < screen_columns(SP_PARM); n++) {
1242 if (!CharEq(newLine[n], oldLine[n])) {
1243 oldPair = GetPair(oldLine[n]);
1244 newPair = GetPair(newLine[n]);
1245 if (oldPair != newPair
1246 && unColor(oldLine[n]) == unColor(newLine[n])) {
1247 if (oldPair < SP_PARM->_pair_limit
1248 && newPair < SP_PARM->_pair_limit
1249 && (SP_PARM->_color_pairs[oldPair] ==
1250 SP_PARM->_color_pairs[newPair])) {
1251 SetPair(oldLine[n], GetPair(newLine[n]));
1258 if (ceol_standout_glitch && clr_eol) {
1260 while (firstChar < screen_columns(SP_PARM)) {
1261 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1271 if (attrchanged) { /* we may have to disregard the whole line */
1272 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1273 ClrToEOL(NCURSES_SP_ARGx
1274 ClrBlank(NCURSES_SP_ARGx CurScreen(SP_PARM)),
1276 PutRange(NCURSES_SP_ARGx
1277 oldLine, newLine, lineno, 0,
1278 screen_columns(SP_PARM) - 1);
1282 * This is a very simple loop to paint characters which may have the
1283 * magic cookie glitch embedded. It doesn't know much about video
1284 * attributes which are continued from one line to the next. It
1285 * assumes that we have filtered out requests for attribute changes
1286 * that do not get mapped to blank positions.
1288 * FIXME: we are not keeping track of where we put the cookies, so this
1289 * will work properly only once, since we may overwrite a cookie in a
1290 * following operation.
1292 } else if (magic_cookie_glitch > 0) {
1293 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1294 for (n = 0; n < screen_columns(SP_PARM); n++) {
1295 int m = n + magic_cookie_glitch;
1297 /* check for turn-on:
1298 * If we are writing an attributed blank, where the
1299 * previous cell is not attributed.
1301 if (ISBLANK(newLine[n])
1303 && xmc_turn_on(SP_PARM, newLine[n - 1], newLine[n]))
1306 && xmc_turn_on(SP_PARM,
1307 xmc_new(SP_PARM, lineno - 1,
1308 screen_columns(SP_PARM) - 1),
1313 PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1315 /* check for turn-off:
1316 * If we are writing an attributed non-blank, where the
1317 * next cell is blank, and not attributed.
1319 if (!ISBLANK(newLine[n])
1320 && ((n + 1 < screen_columns(SP_PARM)
1321 && xmc_turn_off(SP_PARM, newLine[n], newLine[n + 1]))
1322 || (n + 1 >= screen_columns(SP_PARM)
1323 && lineno + 1 < screen_lines(SP_PARM)
1324 && xmc_turn_off(SP_PARM,
1326 xmc_new(SP_PARM, lineno + 1, 0))))) {
1335 /* it may be cheap to clear leading whitespace with clr_bol */
1337 if (clr_bol && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1338 int oFirstChar, nFirstChar;
1340 for (oFirstChar = 0;
1341 oFirstChar < screen_columns(SP_PARM);
1343 if (!CharEq(oldLine[oFirstChar], blank))
1345 for (nFirstChar = 0;
1346 nFirstChar < screen_columns(SP_PARM);
1348 if (!CharEq(newLine[nFirstChar], blank))
1351 if (nFirstChar == oFirstChar) {
1352 firstChar = nFirstChar;
1353 /* find the first differing character */
1354 while (firstChar < screen_columns(SP_PARM)
1355 && CharEq(newLine[firstChar], oldLine[firstChar]))
1357 } else if (oFirstChar > nFirstChar) {
1358 firstChar = nFirstChar;
1359 } else { /* oFirstChar < nFirstChar */
1360 firstChar = oFirstChar;
1361 if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1362 if (nFirstChar >= screen_columns(SP_PARM)
1363 && SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1364 GoTo(NCURSES_SP_ARGx lineno, 0);
1365 UpdateAttrs(SP_PARM, blank);
1366 TPUTS_TRACE("clr_eol");
1367 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1369 GoTo(NCURSES_SP_ARGx lineno, nFirstChar - 1);
1370 UpdateAttrs(SP_PARM, blank);
1371 TPUTS_TRACE("clr_bol");
1372 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_bol);
1375 while (firstChar < nFirstChar)
1376 oldLine[firstChar++] = blank;
1380 /* find the first differing character */
1381 while (firstChar < screen_columns(SP_PARM)
1382 && CharEq(newLine[firstChar], oldLine[firstChar]))
1385 /* if there wasn't one, we're done */
1386 if (firstChar >= screen_columns(SP_PARM)) {
1387 TR(TRACE_UPDATE, (T_RETURN("")));
1391 blank = newLine[screen_columns(SP_PARM) - 1];
1393 if (!can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1394 /* find the last differing character */
1395 nLastChar = screen_columns(SP_PARM) - 1;
1397 while (nLastChar > firstChar
1398 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1401 if (nLastChar >= firstChar) {
1402 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1403 PutRange(NCURSES_SP_ARGx
1409 memcpy(oldLine + firstChar,
1410 newLine + firstChar,
1411 (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1413 TR(TRACE_UPDATE, (T_RETURN("")));
1417 /* find last non-blank character on old line */
1418 oLastChar = screen_columns(SP_PARM) - 1;
1419 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1422 /* find last non-blank character on new line */
1423 nLastChar = screen_columns(SP_PARM) - 1;
1424 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1427 if ((nLastChar == firstChar)
1428 && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1429 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1430 if (!CharEq(newLine[firstChar], blank))
1431 PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1432 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1433 } else if ((nLastChar != oLastChar)
1434 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1435 || !(_nc_idcok && has_ic()))) {
1436 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1437 if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1438 if (PutRange(NCURSES_SP_ARGx
1444 GoTo(NCURSES_SP_ARGx lineno, nLastChar + 1);
1446 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1448 n = max(nLastChar, oLastChar);
1449 PutRange(NCURSES_SP_ARGx
1457 int nLastNonblank = nLastChar;
1458 int oLastNonblank = oLastChar;
1460 /* find the last characters that really differ */
1461 /* can be -1 if no characters differ */
1462 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1463 /* don't split a wide char */
1464 if (isWidecExt(newLine[nLastChar]) &&
1465 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1469 if (nLastChar == -1 || oLastChar == -1)
1473 n = min(oLastChar, nLastChar);
1474 if (n >= firstChar) {
1475 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1476 PutRange(NCURSES_SP_ARGx
1484 if (oLastChar < nLastChar) {
1485 int m = max(nLastNonblank, oLastNonblank);
1486 #if USE_WIDEC_SUPPORT
1487 while (isWidecExt(newLine[n + 1]) && n) {
1492 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1493 if ((nLastChar < nLastNonblank)
1494 || InsCharCost(SP_PARM, nLastChar - oLastChar) > (m - n)) {
1495 PutRange(NCURSES_SP_ARGx
1502 InsStr(NCURSES_SP_ARGx &newLine[n + 1], nLastChar - oLastChar);
1504 } else if (oLastChar > nLastChar) {
1505 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1506 if (DelCharCost(SP_PARM, oLastChar - nLastChar)
1507 > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1508 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1509 n + 1, nLastNonblank))
1510 GoTo(NCURSES_SP_ARGx lineno, nLastNonblank + 1);
1511 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1514 * The delete-char sequence will
1515 * effectively shift in blanks from the
1516 * right margin of the screen. Ensure
1517 * that they are the right color by
1518 * setting the video attributes from
1519 * the last character on the row.
1521 UpdateAttrs(SP_PARM, blank);
1522 DelChar(NCURSES_SP_ARGx oLastChar - nLastChar);
1528 /* update the code's internal representation */
1529 if (screen_columns(SP_PARM) > firstChar)
1530 memcpy(oldLine + firstChar,
1531 newLine + firstChar,
1532 (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1533 TR(TRACE_UPDATE, (T_RETURN("")));
1538 ** ClearScreen(blank)
1540 ** Clear the physical screen and put cursor at home
1545 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1548 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1550 TR(TRACE_UPDATE, ("ClearScreen() called"));
1552 #if NCURSES_EXT_FUNCS
1553 if (SP_PARM->_coloron
1554 && !SP_PARM->_default_color) {
1555 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
1556 GET_SCREEN_PAIR(SP_PARM),
1559 NCURSES_SP_NAME(_nc_outch));
1560 if (!back_color_erase) {
1568 UpdateAttrs(SP_PARM, blank);
1569 TPUTS_TRACE("clear_screen");
1570 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clear_screen);
1571 SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1572 position_check(SP_PARM,
1576 } else if (clr_eos) {
1577 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1578 GoTo(NCURSES_SP_ARGx 0, 0);
1580 UpdateAttrs(SP_PARM, blank);
1581 TPUTS_TRACE("clr_eos");
1582 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1584 screen_lines(SP_PARM),
1585 NCURSES_SP_NAME(_nc_outch));
1586 } else if (clr_eol) {
1587 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1589 UpdateAttrs(SP_PARM, blank);
1590 for (i = 0; i < screen_lines(SP_PARM); i++) {
1591 GoTo(NCURSES_SP_ARGx i, 0);
1592 TPUTS_TRACE("clr_eol");
1593 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1595 GoTo(NCURSES_SP_ARGx 0, 0);
1598 UpdateAttrs(SP_PARM, blank);
1599 for (i = 0; i < screen_lines(SP_PARM); i++) {
1600 GoTo(NCURSES_SP_ARGx i, 0);
1601 for (j = 0; j < screen_columns(SP_PARM); j++)
1602 PutChar(NCURSES_SP_ARGx CHREF(blank));
1604 GoTo(NCURSES_SP_ARGx 0, 0);
1607 for (i = 0; i < screen_lines(SP_PARM); i++) {
1608 for (j = 0; j < screen_columns(SP_PARM); j++)
1609 CurScreen(SP_PARM)->_line[i].text[j] = blank;
1612 TR(TRACE_UPDATE, ("screen cleared"));
1616 ** InsStr(line, count)
1618 ** Insert the count characters pointed to by line.
1623 InsStr(NCURSES_SP_DCLx NCURSES_CH_T * line, int count)
1625 TR(TRACE_UPDATE, ("InsStr(%p, %p,%d) called", SP_PARM, line, count));
1627 /* Prefer parm_ich as it has the smallest cost - no need to shift
1628 * the whole line on each character. */
1629 /* The order must match that of InsCharCost. */
1631 TPUTS_TRACE("parm_ich");
1632 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1633 TPARM_1(parm_ich, count),
1635 NCURSES_SP_NAME(_nc_outch));
1637 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1641 } else if (enter_insert_mode && exit_insert_mode) {
1642 TPUTS_TRACE("enter_insert_mode");
1643 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_insert_mode);
1645 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1646 if (insert_padding) {
1647 TPUTS_TRACE("insert_padding");
1648 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1653 TPUTS_TRACE("exit_insert_mode");
1654 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
1657 TPUTS_TRACE("insert_character");
1658 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_character);
1659 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1660 if (insert_padding) {
1661 TPUTS_TRACE("insert_padding");
1662 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1668 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "InsStr");
1674 ** Delete count characters at current position
1679 DelChar(NCURSES_SP_DCLx int count)
1683 TR(TRACE_UPDATE, ("DelChar(%p, %d) called, position = (%ld,%ld)",
1685 (long) NewScreen(SP_PARM)->_cury,
1686 (long) NewScreen(SP_PARM)->_curx));
1689 TPUTS_TRACE("parm_dch");
1690 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1691 TPARM_1(parm_dch, count),
1693 NCURSES_SP_NAME(_nc_outch));
1695 for (n = 0; n < count; n++) {
1696 TPUTS_TRACE("delete_character");
1697 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_character);
1703 * Physical-scrolling support
1705 * This code was adapted from Keith Bostic's hardware scrolling
1706 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1707 * capabilities, narrowed the call interface slightly, and cleaned
1708 * up some convoluted tests. I also added support for the memory_above
1709 * memory_below, and non_dest_scroll_region capabilities.
1711 * For this code to work, we must have either
1712 * change_scroll_region and scroll forward/reverse commands, or
1713 * insert and delete line capabilities.
1714 * When the scrolling region has been set, the cursor has to
1715 * be at the last line of the region to make the scroll up
1716 * happen, or on the first line of region to scroll down.
1718 * This code makes one aesthetic decision in the opposite way from
1719 * BSD curses. BSD curses preferred pairs of il/dl operations
1720 * over scrolls, allegedly because il/dl looked faster. We, on
1721 * the other hand, prefer scrolls because (a) they're just as fast
1722 * on many terminals and (b) using them avoids bouncing an
1723 * unchanged bottom section of the screen up and down, which is
1726 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1728 * I used assumption that capabilities il/il1/dl/dl1 work inside
1729 * changed scroll region not shifting screen contents outside of it.
1730 * If there are any terminals behaving different way, it would be
1731 * necessary to add some conditions to scroll_csr_forward/backward.
1734 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1736 scroll_csr_forward(NCURSES_SP_DCLx
1746 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1747 GoTo(NCURSES_SP_ARGx bot, 0);
1748 UpdateAttrs(SP_PARM, blank);
1749 TPUTS_TRACE("scroll_forward");
1750 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1751 } else if (n == 1 && delete_line && bot == maxy) {
1752 GoTo(NCURSES_SP_ARGx top, 0);
1753 UpdateAttrs(SP_PARM, blank);
1754 TPUTS_TRACE("delete_line");
1755 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1756 } else if (parm_index && top == miny && bot == maxy) {
1757 GoTo(NCURSES_SP_ARGx bot, 0);
1758 UpdateAttrs(SP_PARM, blank);
1759 TPUTS_TRACE("parm_index");
1760 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1761 TPARM_2(parm_index, n, 0),
1763 NCURSES_SP_NAME(_nc_outch));
1764 } else if (parm_delete_line && bot == maxy) {
1765 GoTo(NCURSES_SP_ARGx top, 0);
1766 UpdateAttrs(SP_PARM, blank);
1767 TPUTS_TRACE("parm_delete_line");
1768 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1769 TPARM_2(parm_delete_line, n, 0),
1771 NCURSES_SP_NAME(_nc_outch));
1772 } else if (scroll_forward && top == miny && bot == maxy) {
1773 GoTo(NCURSES_SP_ARGx bot, 0);
1774 UpdateAttrs(SP_PARM, blank);
1775 for (i = 0; i < n; i++) {
1776 TPUTS_TRACE("scroll_forward");
1777 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1779 } else if (delete_line && bot == maxy) {
1780 GoTo(NCURSES_SP_ARGx top, 0);
1781 UpdateAttrs(SP_PARM, blank);
1782 for (i = 0; i < n; i++) {
1783 TPUTS_TRACE("delete_line");
1784 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1789 #if NCURSES_EXT_FUNCS
1790 if (FILL_BCE(SP_PARM)) {
1792 for (i = 0; i < n; i++) {
1793 GoTo(NCURSES_SP_ARGx bot - i, 0);
1794 for (j = 0; j < screen_columns(SP_PARM); j++)
1795 PutChar(NCURSES_SP_ARGx CHREF(blank));
1802 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1805 scroll_csr_backward(NCURSES_SP_DCLx
1815 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1816 GoTo(NCURSES_SP_ARGx top, 0);
1817 UpdateAttrs(SP_PARM, blank);
1818 TPUTS_TRACE("scroll_reverse");
1819 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1820 } else if (n == 1 && insert_line && bot == maxy) {
1821 GoTo(NCURSES_SP_ARGx top, 0);
1822 UpdateAttrs(SP_PARM, blank);
1823 TPUTS_TRACE("insert_line");
1824 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1825 } else if (parm_rindex && top == miny && bot == maxy) {
1826 GoTo(NCURSES_SP_ARGx top, 0);
1827 UpdateAttrs(SP_PARM, blank);
1828 TPUTS_TRACE("parm_rindex");
1829 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1830 TPARM_2(parm_rindex, n, 0),
1832 NCURSES_SP_NAME(_nc_outch));
1833 } else if (parm_insert_line && bot == maxy) {
1834 GoTo(NCURSES_SP_ARGx top, 0);
1835 UpdateAttrs(SP_PARM, blank);
1836 TPUTS_TRACE("parm_insert_line");
1837 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1838 TPARM_2(parm_insert_line, n, 0),
1840 NCURSES_SP_NAME(_nc_outch));
1841 } else if (scroll_reverse && top == miny && bot == maxy) {
1842 GoTo(NCURSES_SP_ARGx top, 0);
1843 UpdateAttrs(SP_PARM, blank);
1844 for (i = 0; i < n; i++) {
1845 TPUTS_TRACE("scroll_reverse");
1846 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1848 } else if (insert_line && bot == maxy) {
1849 GoTo(NCURSES_SP_ARGx top, 0);
1850 UpdateAttrs(SP_PARM, blank);
1851 for (i = 0; i < n; i++) {
1852 TPUTS_TRACE("insert_line");
1853 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1858 #if NCURSES_EXT_FUNCS
1859 if (FILL_BCE(SP_PARM)) {
1861 for (i = 0; i < n; i++) {
1862 GoTo(NCURSES_SP_ARGx top + i, 0);
1863 for (j = 0; j < screen_columns(SP_PARM); j++)
1864 PutChar(NCURSES_SP_ARGx CHREF(blank));
1871 /* scroll by using delete_line at del and insert_line at ins */
1874 scroll_idl(NCURSES_SP_DCLx int n, int del, int ins, NCURSES_CH_T blank)
1878 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1881 GoTo(NCURSES_SP_ARGx del, 0);
1882 UpdateAttrs(SP_PARM, blank);
1883 if (n == 1 && delete_line) {
1884 TPUTS_TRACE("delete_line");
1885 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1886 } else if (parm_delete_line) {
1887 TPUTS_TRACE("parm_delete_line");
1888 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1889 TPARM_2(parm_delete_line, n, 0),
1891 NCURSES_SP_NAME(_nc_outch));
1892 } else { /* if (delete_line) */
1893 for (i = 0; i < n; i++) {
1894 TPUTS_TRACE("delete_line");
1895 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1899 GoTo(NCURSES_SP_ARGx ins, 0);
1900 UpdateAttrs(SP_PARM, blank);
1901 if (n == 1 && insert_line) {
1902 TPUTS_TRACE("insert_line");
1903 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1904 } else if (parm_insert_line) {
1905 TPUTS_TRACE("parm_insert_line");
1906 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1907 TPARM_2(parm_insert_line, n, 0),
1909 NCURSES_SP_NAME(_nc_outch));
1910 } else { /* if (insert_line) */
1911 for (i = 0; i < n; i++) {
1912 TPUTS_TRACE("insert_line");
1913 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1921 * Note: some terminals require the cursor to be within the scrolling margins
1922 * before setting them. Generally, the cursor must be at the appropriate end
1923 * of the scrolling margins when issuing an indexing operation (it is not
1924 * apparent whether it must also be at the left margin; we do this just to be
1925 * safe). To make the related cursor movement a little faster, we use the
1926 * save/restore cursor capabilities if the terminal has them.
1929 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
1934 /* scroll region from top to bot by n lines */
1936 NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1938 bool cursor_saved = FALSE;
1941 TR(TRACE_MOVE, ("mvcur_scrolln(%d, %d, %d, %d)", n, top, bot, maxy));
1945 * If we scroll, we might remove a cookie.
1947 if (magic_cookie_glitch > 0) {
1952 if (n > 0) { /* scroll up (forward) */
1954 * Explicitly clear if stuff pushed off top of region might
1955 * be saved by the terminal.
1957 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
1959 if (res == ERR && change_scroll_region) {
1960 if ((((n == 1 && scroll_forward) || parm_index)
1961 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
1962 && save_cursor && restore_cursor) {
1963 cursor_saved = TRUE;
1964 TPUTS_TRACE("save_cursor");
1965 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
1967 TPUTS_TRACE("change_scroll_region");
1968 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1969 TPARM_2(change_scroll_region, top, bot));
1971 TPUTS_TRACE("restore_cursor");
1972 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
1974 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1977 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
1979 TPUTS_TRACE("change_scroll_region");
1980 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1981 TPARM_2(change_scroll_region, 0, maxy));
1982 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1985 if (res == ERR && _nc_idlok)
1986 res = scroll_idl(NCURSES_SP_ARGx n, top, bot - n + 1, blank);
1989 * Clear the newly shifted-in text.
1992 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
1993 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
1994 if (bot == maxy && clr_eos) {
1995 GoTo(NCURSES_SP_ARGx bot - n + 1, 0);
1996 ClrToEOS(NCURSES_SP_ARGx blank2);
1998 for (i = 0; i < n; i++) {
1999 GoTo(NCURSES_SP_ARGx bot - i, 0);
2000 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2005 } else { /* (n < 0) - scroll down (backward) */
2006 res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
2008 if (res == ERR && change_scroll_region) {
2010 && (SP_PARM->_cursrow == top ||
2011 SP_PARM->_cursrow == top - 1)
2012 && save_cursor && restore_cursor) {
2013 cursor_saved = TRUE;
2014 TPUTS_TRACE("save_cursor");
2015 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
2017 TPUTS_TRACE("change_scroll_region");
2018 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2019 TPARM_2(change_scroll_region, top, bot));
2021 TPUTS_TRACE("restore_cursor");
2022 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
2024 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2027 res = scroll_csr_backward(NCURSES_SP_ARGx
2028 -n, top, bot, top, bot, blank);
2030 TPUTS_TRACE("change_scroll_region");
2031 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2032 TPARM_2(change_scroll_region, 0, maxy));
2033 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2036 if (res == ERR && _nc_idlok)
2037 res = scroll_idl(NCURSES_SP_ARGx -n, bot + n + 1, top, blank);
2040 * Clear the newly shifted-in text.
2043 && (non_dest_scroll_region || (memory_above && top == 0))) {
2044 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2045 for (i = 0; i < -n; i++) {
2046 GoTo(NCURSES_SP_ARGx i + top, 0);
2047 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2055 _nc_scroll_window(CurScreen(SP_PARM), n, top, bot, blank);
2057 /* shift hash values too - they can be reused */
2058 NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_ARGx n, top, bot);
2063 #if NCURSES_SP_FUNCS
2065 _nc_scrolln(int n, int top, int bot, int maxy)
2067 return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
2071 NCURSES_EXPORT(void)
2072 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_DCL0)
2076 /* make sure terminal is in a sane known state */
2077 SetAttr(SCREEN_ATTRS(SP_PARM), A_NORMAL);
2078 NewScreen(SP_PARM)->_clear = TRUE;
2080 /* reset color pairs and definitions */
2081 if (SP_PARM->_coloron || SP_PARM->_color_defs)
2084 /* restore user-defined colors, if any */
2085 if (SP_PARM->_color_defs < 0) {
2087 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
2088 for (n = 0; n < SP_PARM->_color_defs; ++n) {
2089 if (SP_PARM->_color_table[n].init) {
2090 NCURSES_SP_NAME(init_color) (NCURSES_SP_ARGx n,
2091 SP_PARM->_color_table[n].r,
2092 SP_PARM->_color_table[n].g,
2093 SP_PARM->_color_table[n].b);
2098 if (exit_attribute_mode)
2099 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_attribute_mode);
2101 /* turn off attributes */
2102 if (exit_alt_charset_mode)
2103 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_alt_charset_mode);
2104 if (exit_standout_mode)
2105 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_standout_mode);
2106 if (exit_underline_mode)
2107 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_underline_mode);
2109 if (exit_insert_mode)
2110 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
2111 if (enter_am_mode && exit_am_mode)
2112 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2118 #if NCURSES_SP_FUNCS
2119 NCURSES_EXPORT(void)
2120 _nc_screen_resume(void)
2122 NCURSES_SP_NAME(_nc_screen_resume) (CURRENT_SCREEN);
2126 NCURSES_EXPORT(void)
2127 NCURSES_SP_NAME(_nc_screen_init) (NCURSES_SP_DCL0)
2129 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
2132 #if NCURSES_SP_FUNCS
2133 NCURSES_EXPORT(void)
2134 _nc_screen_init(void)
2136 NCURSES_SP_NAME(_nc_screen_init) (CURRENT_SCREEN);
2140 /* wrap up screen handling */
2141 NCURSES_EXPORT(void)
2142 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2147 UpdateAttrs(SP_PARM, normal);
2148 #if NCURSES_EXT_FUNCS
2149 if (SP_PARM->_coloron
2150 && !SP_PARM->_default_color) {
2151 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2152 SP_PARM->_default_color = TRUE;
2153 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
2156 NCURSES_SP_NAME(_nc_outch));
2157 SP_PARM->_default_color = FALSE;
2159 mvcur(SP_PARM->_cursrow,
2161 screen_lines(SP_PARM) - 1, 0);
2163 ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2166 if (SP_PARM->_color_defs) {
2171 #if NCURSES_SP_FUNCS
2172 NCURSES_EXPORT(void)
2173 _nc_screen_wrap(void)
2175 NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2180 NCURSES_EXPORT(void)
2181 NCURSES_SP_NAME(_nc_do_xmc_glitch) (NCURSES_SP_DCLx attr_t previous)
2184 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP_PARM)));
2188 SP_PARM->_curscol += magic_cookie_glitch;
2189 if (SP_PARM->_curscol >= SP_PARM->_columns)
2190 wrap_cursor(NCURSES_SP_ARG);
2191 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie",
2192 SP_PARM->_cursrow, SP_PARM->_curscol));
2199 #if NCURSES_SP_FUNCS
2200 NCURSES_EXPORT(void)
2201 _nc_do_xmc_glitch(attr_t previous)
2203 NCURSES_SP_NAME(_nc_do_xmc_glitch) (CURRENT_SCREEN, previous);
2207 #endif /* USE_XMC_SUPPORT */