1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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.267 2011/10/22 17:30:14 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]);
291 if (!(SP_PARM->_screen_acs_map[j])) {
292 RemAttr(attr, A_ALTCHARSET);
297 SetChar(my_ch, temp, AttrOf(attr));
301 if (tilde_glitch && (CharOfD(ch) == L('~'))) {
302 SetChar(tilde, L('`'), AttrOf(attr));
306 UpdateAttrs(SP_PARM, attr);
307 #if !USE_WIDEC_SUPPORT
308 /* FIXME - we do this special case for signal handling, should see how to
309 * make it work for wide characters.
311 if (SP_PARM->_outch != 0) {
312 SP_PARM->_outch(NCURSES_SP_ARGx UChar(ch));
316 PUTC(CHDEREF(ch), SP_PARM->_ofp); /* macro's fastest... */
319 SP_PARM->_curscol += chlen;
321 TPUTS_TRACE("char_padding");
322 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx char_padding);
327 check_pending(NCURSES_SP_DCL0)
328 /* check for pending input */
330 bool have_pending = FALSE;
333 * Only carry out this check when the flag is zero, otherwise we'll
334 * have the refreshing slow down drastically (or stop) if there's an
335 * unread character available.
337 if (SP_PARM->_fifohold != 0)
340 if (SP_PARM->_checkfd >= 0) {
342 struct pollfd fds[1];
343 fds[0].fd = SP_PARM->_checkfd;
344 fds[0].events = POLLIN;
345 if (poll(fds, (size_t) 1, 0) > 0) {
348 #elif defined(__BEOS__)
350 * BeOS's select() is declared in socket.h, so the configure script does
351 * not see it. That's just as well, since that function works only for
352 * sockets. This (using snooze and ioctl) was distilled from Be's patch
353 * for ncurses which uses a separate thread to simulate select().
355 * FIXME: the return values from the ioctl aren't very clear if we get
359 int howmany = ioctl(0, 'ichr', &n);
360 if (howmany >= 0 && n > 0) {
365 struct timeval ktimeout;
368 ktimeout.tv_usec = 0;
371 FD_SET(SP_PARM->_checkfd, &fdset);
372 if (select(SP_PARM->_checkfd + 1, &fdset, NULL, NULL, &ktimeout) != 0) {
378 SP_PARM->_fifohold = 5;
379 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
384 /* put char at lower right corner */
386 PutCharLR(NCURSES_SP_DCLx const ARG_CH_T ch)
388 if (!auto_right_margin) {
389 /* we can put the char directly */
390 PutAttrChar(NCURSES_SP_ARGx ch);
391 } else if (enter_am_mode && exit_am_mode) {
392 /* we can suppress automargin */
393 TPUTS_TRACE("exit_am_mode");
394 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_am_mode);
396 PutAttrChar(NCURSES_SP_ARGx ch);
398 position_check(SP_PARM,
403 TPUTS_TRACE("enter_am_mode");
404 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_am_mode);
405 } else if ((enter_insert_mode && exit_insert_mode)
406 || insert_character || parm_ich) {
408 screen_lines(SP_PARM) - 1,
409 screen_columns(SP_PARM) - 2);
410 PutAttrChar(NCURSES_SP_ARGx ch);
412 screen_lines(SP_PARM) - 1,
413 screen_columns(SP_PARM) - 2);
414 InsStr(NCURSES_SP_ARGx
415 NewScreen(SP_PARM)->_line[screen_lines(SP_PARM) - 1].text +
416 screen_columns(SP_PARM) - 2, 1);
421 * Wrap the cursor position, i.e., advance to the beginning of the next line.
424 wrap_cursor(NCURSES_SP_DCL0)
426 if (eat_newline_glitch) {
428 * xenl can manifest two different ways. The vt100 way is that, when
429 * you'd expect the cursor to wrap, it stays hung at the right margin
430 * (on top of the character just emitted) and doesn't wrap until the
431 * *next* graphic char is emitted. The c100 way is to ignore LF
432 * received just after an am wrap.
434 * An aggressive way to handle this would be to emit CR/LF after the
435 * char and then assume the wrap is done, you're on the first position
436 * of the next line, and the terminal out of its weird state. Here
437 * it's safe to just tell the code that the cursor is in hyperspace and
438 * let the next mvcur() call straighten things out.
440 SP_PARM->_curscol = -1;
441 SP_PARM->_cursrow = -1;
442 } else if (auto_right_margin) {
443 SP_PARM->_curscol = 0;
446 * We've actually moved - but may have to work around problems with
447 * video attributes not working.
449 if (!move_standout_mode && AttrOf(SCREEN_ATTRS(SP_PARM))) {
450 TR(TRACE_CHARPUT, ("turning off (%#lx) %s before wrapping",
451 (unsigned long) AttrOf(SCREEN_ATTRS(SP_PARM)),
452 _traceattr(AttrOf(SCREEN_ATTRS(SP_PARM)))));
453 (void) VIDATTR(SP_PARM, A_NORMAL, 0);
458 position_check(SP_PARM,
464 static NCURSES_INLINE void
465 PutChar(NCURSES_SP_DCLx const ARG_CH_T ch)
466 /* insert character, handling automargin stuff */
468 if (SP_PARM->_cursrow == screen_lines(SP_PARM) - 1 &&
469 SP_PARM->_curscol == screen_columns(SP_PARM) - 1) {
470 PutCharLR(NCURSES_SP_ARGx ch);
472 PutAttrChar(NCURSES_SP_ARGx ch);
475 if (SP_PARM->_curscol >= screen_columns(SP_PARM))
476 wrap_cursor(NCURSES_SP_ARG);
478 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "PutChar");
482 * Check whether the given character can be output by clearing commands. This
483 * includes test for being a space and not including any 'bad' attributes, such
484 * as A_REVERSE. All attribute flags which don't affect appearance of a space
485 * or can be output by clearing (A_COLOR in case of bce-terminal) are excluded.
487 static NCURSES_INLINE bool
488 can_clear_with(NCURSES_SP_DCLx ARG_CH_T ch)
490 if (!back_color_erase && SP_PARM->_coloron) {
491 #if NCURSES_EXT_FUNCS
494 if (!SP_PARM->_default_color)
496 if (SP_PARM->_default_fg != C_MASK || SP_PARM->_default_bg != C_MASK)
498 if ((pair = GetPair(CHDEREF(ch))) != 0) {
500 NCURSES_SP_NAME(pair_content) (NCURSES_SP_ARGx
503 if (fg != C_MASK || bg != C_MASK)
507 if (AttrOfD(ch) & A_COLOR)
511 return (ISBLANK(CHDEREF(ch)) &&
512 (AttrOfD(ch) & ~(NONBLANK_ATTR | A_COLOR)) == BLANK_ATTR);
516 * Issue a given span of characters from an array.
517 * Must be functionally equivalent to:
518 * for (i = 0; i < num; i++)
520 * but can leave the cursor positioned at the middle of the interval.
522 * Returns: 0 - cursor is at the end of interval
523 * 1 - cursor is somewhere in the middle
525 * This code is optimized using ech and rep.
528 EmitRange(NCURSES_SP_DCLx const NCURSES_CH_T * ntext, int num)
532 TR(TRACE_CHARPUT, ("EmitRange %d:%s", num, _nc_viscbuf(ntext, num)));
534 if (erase_chars || repeat_char) {
539 while (num > 1 && !CharEq(ntext[0], ntext[1])) {
540 PutChar(NCURSES_SP_ARGx CHREF(ntext[0]));
546 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
551 while (runcount < num && CharEq(ntext[runcount], ntext0))
555 * The cost expression in the middle isn't exactly right.
556 * _cup_ch_cost is an upper bound on the cost for moving to the
557 * end of the erased area, but not the cost itself (which we
558 * can't compute without emitting the move). This may result
559 * in erase_chars not getting used in some situations for
560 * which it would be marginally advantageous.
563 && runcount > SP_PARM->_ech_cost + SP_PARM->_cup_ch_cost
564 && can_clear_with(NCURSES_SP_ARGx CHREF(ntext0))) {
565 UpdateAttrs(SP_PARM, ntext0);
566 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
567 TPARM_1(erase_chars, runcount));
570 * If this is the last part of the given interval,
571 * don't bother moving cursor, since it can be the
572 * last update on the line.
574 if (runcount < num) {
577 SP_PARM->_curscol + runcount);
579 return 1; /* cursor stays in the middle */
581 } else if (repeat_char && runcount > SP_PARM->_rep_cost) {
582 bool wrap_possible = (SP_PARM->_curscol + runcount >=
583 screen_columns(SP_PARM));
584 int rep_count = runcount;
589 UpdateAttrs(SP_PARM, ntext0);
590 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
595 NCURSES_SP_NAME(_nc_outch));
596 SP_PARM->_curscol += rep_count;
599 PutChar(NCURSES_SP_ARGx CHREF(ntext0));
601 for (i = 0; i < runcount; i++)
602 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
610 for (i = 0; i < num; i++)
611 PutChar(NCURSES_SP_ARGx CHREF(ntext[i]));
616 * Output the line in the given range [first .. last]
618 * If there's a run of identical characters that's long enough to justify
619 * cursor movement, use that also.
621 * Returns: same as EmitRange
624 PutRange(NCURSES_SP_DCLx
625 const NCURSES_CH_T * otext,
626 const NCURSES_CH_T * ntext,
632 TR(TRACE_CHARPUT, ("PutRange(%p, %p, %p, %d, %d, %d)",
634 (const void *) otext,
635 (const void *) ntext,
639 && (last - first + 1) > SP_PARM->_inline_cost) {
640 for (j = first, same = 0; j <= last; j++) {
641 if (!same && isWidecExt(otext[j]))
643 if (CharEq(otext[j], ntext[j])) {
646 if (same > SP_PARM->_inline_cost) {
647 EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
648 GoTo(NCURSES_SP_ARGx row, first = j);
653 i = EmitRange(NCURSES_SP_ARGx ntext + first, j - same - first);
655 * Always return 1 for the next GoTo() after a PutRange() if we found
656 * identical characters at end of interval
658 return (same == 0 ? i : 1);
660 return EmitRange(NCURSES_SP_ARGx ntext + first, last - first + 1);
663 /* leave unbracketed here so 'indent' works */
664 #define MARK_NOCHANGE(win,row) \
665 win->_line[row].firstchar = _NOCHANGE; \
666 win->_line[row].lastchar = _NOCHANGE; \
667 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row)
670 TINFO_DOUPDATE(NCURSES_SP_DCL0)
675 struct tms before, after;
676 #endif /* USE_TRACE_TIMES */
678 T((T_CALLED("_nc_tinfo:doupdate(%p)"), (void *) SP_PARM));
685 * It is "legal" but unlikely that an application could assign a new
686 * value to one of the standard windows. Check for that possibility
687 * and try to recover.
689 * We do not allow applications to assign new values in the reentrant
692 #define SyncScreens(internal,exported) \
693 if (internal == 0) internal = exported; \
694 if (internal != exported) exported = internal
696 SyncScreens(CurScreen(SP_PARM), curscr);
697 SyncScreens(NewScreen(SP_PARM), newscr);
698 SyncScreens(StdScreen(SP_PARM), stdscr);
701 if (CurScreen(SP_PARM) == 0
702 || NewScreen(SP_PARM) == 0
703 || StdScreen(SP_PARM) == 0)
707 if (USE_TRACEF(TRACE_UPDATE)) {
708 if (CurScreen(SP_PARM)->_clear)
709 _tracef("curscr is clear");
711 _tracedump("curscr", CurScreen(SP_PARM));
712 _tracedump("newscr", NewScreen(SP_PARM));
713 _nc_unlock_global(tracef);
717 _nc_signal_handler(FALSE);
719 if (SP_PARM->_fifohold)
720 SP_PARM->_fifohold--;
723 if (SP_PARM->_endwin || _nc_handle_sigwinch(SP_PARM)) {
725 * This is a transparent extension: XSI does not address it,
726 * and applications need not know that ncurses can do it.
728 * Check if the terminal size has changed while curses was off
729 * (this can happen in an xterm, for example), and resize the
730 * ncurses data structures accordingly.
732 _nc_update_screensize(SP_PARM);
736 if (SP_PARM->_endwin) {
738 T(("coming back from shell mode"));
739 NCURSES_SP_NAME(reset_prog_mode) (NCURSES_SP_ARG);
741 NCURSES_SP_NAME(_nc_mvcur_resume) (NCURSES_SP_ARG);
742 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
743 SP_PARM->_mouse_resume(SP_PARM);
745 SP_PARM->_endwin = FALSE;
748 /* zero the metering machinery */
750 (void) times(&before);
751 #endif /* USE_TRACE_TIMES */
754 * This is the support for magic-cookie terminals. The theory: we scan
755 * the virtual screen looking for attribute turnons. Where we find one,
756 * check to make sure it's realizable by seeing if the required number of
757 * un-attributed blanks are present before and after the attributed range;
758 * try to shift the range boundaries over blanks (not changing the screen
759 * display) so this becomes true. If it is, shift the beginning attribute
760 * change appropriately (the end one, if we've gotten this far, is
761 * guaranteed room for its cookie). If not, nuke the added attributes out
765 if (magic_cookie_glitch > 0) {
767 attr_t rattr = A_NORMAL;
769 for (i = 0; i < screen_lines(SP_PARM); i++) {
770 for (j = 0; j < screen_columns(SP_PARM); j++) {
772 NCURSES_CH_T *thisline = NewScreen(SP_PARM)->_line[i].text;
773 attr_t thisattr = AttrOf(thisline[j]) & SP_PARM->_xmc_triggers;
774 attr_t turnon = thisattr & ~rattr;
776 /* is an attribute turned on here? */
782 TR(TRACE_ATTRS, ("At (%d, %d): from %s...", i, j, _traceattr(rattr)));
783 TR(TRACE_ATTRS, ("...to %s", _traceattr(turnon)));
786 * If the attribute change location is a blank with a "safe"
787 * attribute, undo the attribute turnon. This may ensure
788 * there's enough room to set the attribute before the first
789 * non-blank in the run.
791 #define SAFE(scr,a) (!((a) & (scr)->_xmc_triggers))
792 if (ISBLANK(thisline[j]) && SAFE(SP_PARM, turnon)) {
793 RemAttr(thisline[j], turnon);
797 /* check that there's enough room at start of span */
798 for (k = 1; k <= magic_cookie_glitch; k++) {
800 || !ISBLANK(thisline[j - k])
801 || !SAFE(SP_PARM, AttrOf(thisline[j - k]))) {
803 TR(TRACE_ATTRS, ("No room at start in %d,%d%s%s",
805 (ISBLANK(thisline[j - k])
808 (SAFE(SP_PARM, AttrOf(thisline[j - k]))
815 bool end_onscreen = FALSE;
818 /* find end of span, if it's onscreen */
819 for (m = i; m < screen_lines(SP_PARM); m++) {
820 for (; n < screen_columns(SP_PARM); n++) {
822 AttrOf(NewScreen(SP_PARM)->_line[m].text[n]);
823 if ((testattr & SP_PARM->_xmc_triggers) == rattr) {
826 ("Range attributed with %s ends at (%d, %d)",
827 _traceattr(turnon), m, n));
834 ("Range attributed with %s ends offscreen",
835 _traceattr(turnon)));
839 NCURSES_CH_T *lastline =
840 NewScreen(SP_PARM)->_line[m].text;
843 * If there are safely-attributed blanks at the end of
844 * the range, shorten the range. This will help ensure
845 * that there is enough room at end of span.
848 && ISBLANK(lastline[n])
849 && SAFE(SP_PARM, AttrOf(lastline[n]))) {
850 RemAttr(lastline[n--], turnon);
853 /* check that there's enough room at end of span */
854 for (k = 1; k <= magic_cookie_glitch; k++) {
855 if (n + k >= screen_columns(SP_PARM)
856 || !ISBLANK(lastline[n + k])
857 || !SAFE(SP_PARM, AttrOf(lastline[n + k]))) {
860 ("No room at end in %d,%d%s%s",
862 (ISBLANK(lastline[n + k])
865 (SAFE(SP_PARM, AttrOf(lastline[n + k]))
878 ("Clearing %s beginning at (%d, %d)",
879 _traceattr(turnon), i, j));
881 /* turn off new attributes over span */
882 for (p = i; p < screen_lines(SP_PARM); p++) {
883 for (; q < screen_columns(SP_PARM); q++) {
884 attr_t testattr = AttrOf(newscr->_line[p].text[q]);
885 if ((testattr & SP_PARM->_xmc_triggers) == rattr)
887 RemAttr(NewScreen(SP_PARM)->_line[p].text[q], turnon);
894 ("Cookie space for %s found before (%d, %d)",
895 _traceattr(turnon), i, j));
898 * Back up the start of range so there's room for cookies
899 * before the first nonblank character.
901 for (k = 1; k <= magic_cookie_glitch; k++)
902 AddAttr(thisline[j - k], turnon);
910 /* show altered highlights after magic-cookie check */
911 if (USE_TRACEF(TRACE_UPDATE)) {
912 _tracef("After magic-cookie check...");
913 _tracedump("newscr", NewScreen(SP_PARM));
914 _nc_unlock_global(tracef);
918 #endif /* USE_XMC_SUPPORT */
921 if (CurScreen(SP_PARM)->_clear || NewScreen(SP_PARM)->_clear) { /* force refresh ? */
922 ClrUpdate(NCURSES_SP_ARG);
923 CurScreen(SP_PARM)->_clear = FALSE; /* reset flag */
924 NewScreen(SP_PARM)->_clear = FALSE; /* reset flag */
926 int changedlines = CHECK_INTERVAL;
928 if (check_pending(NCURSES_SP_ARG))
931 nonempty = min(screen_lines(SP_PARM), NewScreen(SP_PARM)->_maxy + 1);
933 if (SP_PARM->_scrolling) {
934 NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_ARG);
937 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
939 TR(TRACE_UPDATE, ("Transforming lines, nonempty %d", nonempty));
940 for (i = 0; i < nonempty; i++) {
942 * Here is our line-breakout optimization.
944 if (changedlines == CHECK_INTERVAL) {
945 if (check_pending(NCURSES_SP_ARG))
951 * newscr->line[i].firstchar is normally set
952 * by wnoutrefresh. curscr->line[i].firstchar
953 * is normally set by _nc_scroll_window in the
954 * vertical-movement optimization code,
956 if (NewScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE
957 || CurScreen(SP_PARM)->_line[i].firstchar != _NOCHANGE) {
958 TransformLine(NCURSES_SP_ARGx i);
962 /* mark line changed successfully */
963 if (i <= NewScreen(SP_PARM)->_maxy) {
964 MARK_NOCHANGE(NewScreen(SP_PARM), i);
966 if (i <= CurScreen(SP_PARM)->_maxy) {
967 MARK_NOCHANGE(CurScreen(SP_PARM), i);
972 /* put everything back in sync */
973 for (i = nonempty; i <= NewScreen(SP_PARM)->_maxy; i++) {
974 MARK_NOCHANGE(NewScreen(SP_PARM), i);
976 for (i = nonempty; i <= CurScreen(SP_PARM)->_maxy; i++) {
977 MARK_NOCHANGE(CurScreen(SP_PARM), i);
980 if (!NewScreen(SP_PARM)->_leaveok) {
981 CurScreen(SP_PARM)->_curx = NewScreen(SP_PARM)->_curx;
982 CurScreen(SP_PARM)->_cury = NewScreen(SP_PARM)->_cury;
984 GoTo(NCURSES_SP_ARGx CurScreen(SP_PARM)->_cury, CurScreen(SP_PARM)->_curx);
989 * We would like to keep the physical screen in normal mode in case we get
990 * other processes writing to the screen. This goal cannot be met for
991 * magic cookies since it interferes with attributes that may propagate
992 * past the current position.
995 if (magic_cookie_glitch != 0)
997 UpdateAttrs(SP_PARM, normal);
999 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1000 WINDOW_ATTRS(CurScreen(SP_PARM)) = WINDOW_ATTRS(NewScreen(SP_PARM));
1003 (void) times(&after);
1005 ("Update cost: %ld chars, %ld clocks system time, %ld clocks user time",
1007 (long) (after.tms_stime - before.tms_stime),
1008 (long) (after.tms_utime - before.tms_utime)));
1009 #endif /* USE_TRACE_TIMES */
1011 _nc_signal_handler(TRUE);
1016 #if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)
1020 return TINFO_DOUPDATE(CURRENT_SCREEN);
1027 * Returns the attributed character that corresponds to the "cleared"
1028 * screen. If the terminal has the back-color-erase feature, this will be
1029 * colored according to the wbkgd() call.
1031 * We treat 'curscr' specially because it isn't supposed to be set directly
1032 * in the wbkgd() call. Assume 'stdscr' for this case.
1034 #define BCE_ATTRS (A_NORMAL|A_COLOR)
1035 #define BCE_BKGD(sp,win) (((win) == CurScreen(sp) ? StdScreen(sp) : (win))->_nc_bkgd)
1037 static NCURSES_INLINE NCURSES_CH_T
1038 ClrBlank(NCURSES_SP_DCLx WINDOW *win)
1040 NCURSES_CH_T blank = blankchar;
1041 if (back_color_erase)
1042 AddAttr(blank, (AttrOf(BCE_BKGD(SP_PARM, win)) & BCE_ATTRS));
1049 ** Update by clearing and redrawing the entire screen.
1054 ClrUpdate(NCURSES_SP_DCL0)
1056 TR(TRACE_UPDATE, (T_CALLED("ClrUpdate")));
1059 NCURSES_CH_T blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1060 int nonempty = min(screen_lines(SP_PARM),
1061 NewScreen(SP_PARM)->_maxy + 1);
1063 ClearScreen(NCURSES_SP_ARGx blank);
1065 TR(TRACE_UPDATE, ("updating screen from scratch"));
1067 nonempty = ClrBottom(NCURSES_SP_ARGx nonempty);
1069 for (i = 0; i < nonempty; i++)
1070 TransformLine(NCURSES_SP_ARGx i);
1072 TR(TRACE_UPDATE, (T_RETURN("")));
1078 ** Clear to end of current line, starting at the cursor position
1082 ClrToEOL(NCURSES_SP_DCLx NCURSES_CH_T blank, int needclear)
1087 if (CurScreen(SP_PARM) != 0
1088 && SP_PARM->_cursrow >= 0) {
1089 for (j = SP_PARM->_curscol; j < screen_columns(SP_PARM); j++) {
1092 &(CurScreen(SP_PARM)->_line[SP_PARM->_cursrow].text[j]);
1094 if (!CharEq(*cp, blank)) {
1103 if (needclear && (SP_PARM != 0)) {
1104 UpdateAttrs(SP_PARM, blank);
1105 TPUTS_TRACE("clr_eol");
1106 if (clr_eol && SP_PARM->_el_cost <= (screen_columns(SP_PARM) - SP_PARM->_curscol)) {
1107 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1109 int count = (screen_columns(SP_PARM) - SP_PARM->_curscol);
1111 PutChar(NCURSES_SP_ARGx CHREF(blank));
1119 ** Clear to end of screen, starting at the cursor position
1123 ClrToEOS(NCURSES_SP_DCLx NCURSES_CH_T blank)
1130 row = SP_PARM->_cursrow;
1131 col = SP_PARM->_curscol;
1133 UpdateAttrs(SP_PARM, blank);
1134 TPUTS_TRACE("clr_eos");
1135 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1137 screen_lines(SP_PARM) - row,
1138 NCURSES_SP_NAME(_nc_outch));
1140 while (col < screen_columns(SP_PARM))
1141 CurScreen(SP_PARM)->_line[row].text[col++] = blank;
1143 for (row++; row < screen_lines(SP_PARM); row++) {
1144 for (col = 0; col < screen_columns(SP_PARM); col++)
1145 CurScreen(SP_PARM)->_line[row].text[col] = blank;
1152 * Test if clearing the end of the screen would satisfy part of the
1153 * screen-update. Do this by scanning backwards through the lines in the
1154 * screen, checking if each is blank, and one or more are changed.
1157 ClrBottom(NCURSES_SP_DCLx int total)
1162 int last = min(screen_columns(SP_PARM), NewScreen(SP_PARM)->_maxx + 1);
1163 NCURSES_CH_T blank = NewScreen(SP_PARM)->_line[total - 1].text[last - 1];
1166 if (clr_eos && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1168 for (row = total - 1; row >= 0; row--) {
1169 for (col = 0, ok = TRUE; ok && col < last; col++) {
1170 ok = (CharEq(NewScreen(SP_PARM)->_line[row].text[col], blank));
1175 for (col = 0; ok && col < last; col++) {
1176 ok = (CharEq(CurScreen(SP_PARM)->_line[row].text[col], blank));
1182 /* don't use clr_eos for just one line if clr_eol available */
1184 GoTo(NCURSES_SP_ARGx top, 0);
1185 ClrToEOS(NCURSES_SP_ARGx blank);
1186 if (SP_PARM->oldhash && SP_PARM->newhash) {
1187 for (row = top; row < screen_lines(SP_PARM); row++)
1188 SP_PARM->oldhash[row] = SP_PARM->newhash[row];
1196 #if USE_WIDEC_SUPPORT
1197 #define check_xmc_transition(sp, a, b) \
1198 ((((a)->attr ^ (b)->attr) & ~((a)->attr) & (sp)->_xmc_triggers) != 0)
1199 #define xmc_turn_on(sp,a,b) check_xmc_transition(sp,&(a), &(b))
1201 #define xmc_turn_on(sp,a,b) ((((a)^(b)) & ~(a) & (sp)->_xmc_triggers) != 0)
1204 #define xmc_new(sp,r,c) NewScreen(sp)->_line[r].text[c]
1205 #define xmc_turn_off(sp,a,b) xmc_turn_on(sp,b,a)
1206 #endif /* USE_XMC_SUPPORT */
1209 ** TransformLine(lineno)
1211 ** Transform the given line in curscr to the one in newscr, using
1212 ** Insert/Delete Character if idcok && has_ic().
1214 ** firstChar = position of first different character in line
1215 ** oLastChar = position of last different character in old line
1216 ** nLastChar = position of last different character in new line
1218 ** move to firstChar
1219 ** overwrite chars up to min(oLastChar, nLastChar)
1220 ** if oLastChar < nLastChar
1221 ** insert newLine[oLastChar+1..nLastChar]
1223 ** delete oLastChar - nLastChar spaces
1227 TransformLine(NCURSES_SP_DCLx int const lineno)
1229 int firstChar, oLastChar, nLastChar;
1230 NCURSES_CH_T *newLine = NewScreen(SP_PARM)->_line[lineno].text;
1231 NCURSES_CH_T *oldLine = CurScreen(SP_PARM)->_line[lineno].text;
1233 bool attrchanged = FALSE;
1235 TR(TRACE_UPDATE, (T_CALLED("TransformLine(%p, %d)"), (void *) SP_PARM, lineno));
1237 /* copy new hash value to old one */
1238 if (SP_PARM->oldhash && SP_PARM->newhash)
1239 SP_PARM->oldhash[lineno] = SP_PARM->newhash[lineno];
1242 * If we have colors, there is the possibility of having two color pairs
1243 * that display as the same colors. For instance, Lynx does this. Check
1244 * for this case, and update the old line with the new line's colors when
1245 * they are equivalent.
1247 if (SP_PARM->_coloron) {
1251 for (n = 0; n < screen_columns(SP_PARM); n++) {
1252 if (!CharEq(newLine[n], oldLine[n])) {
1253 oldPair = GetPair(oldLine[n]);
1254 newPair = GetPair(newLine[n]);
1255 if (oldPair != newPair
1256 && unColor(oldLine[n]) == unColor(newLine[n])) {
1257 if (oldPair < SP_PARM->_pair_limit
1258 && newPair < SP_PARM->_pair_limit
1259 && (SP_PARM->_color_pairs[oldPair] ==
1260 SP_PARM->_color_pairs[newPair])) {
1261 SetPair(oldLine[n], GetPair(newLine[n]));
1268 if (ceol_standout_glitch && clr_eol) {
1270 while (firstChar < screen_columns(SP_PARM)) {
1271 if (!SameAttrOf(newLine[firstChar], oldLine[firstChar])) {
1281 if (attrchanged) { /* we may have to disregard the whole line */
1282 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1283 ClrToEOL(NCURSES_SP_ARGx
1284 ClrBlank(NCURSES_SP_ARGx
1285 CurScreen(SP_PARM)), FALSE);
1286 PutRange(NCURSES_SP_ARGx
1287 oldLine, newLine, lineno, 0,
1288 screen_columns(SP_PARM) - 1);
1292 * This is a very simple loop to paint characters which may have the
1293 * magic cookie glitch embedded. It doesn't know much about video
1294 * attributes which are continued from one line to the next. It
1295 * assumes that we have filtered out requests for attribute changes
1296 * that do not get mapped to blank positions.
1298 * FIXME: we are not keeping track of where we put the cookies, so this
1299 * will work properly only once, since we may overwrite a cookie in a
1300 * following operation.
1302 } else if (magic_cookie_glitch > 0) {
1303 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1304 for (n = 0; n < screen_columns(SP_PARM); n++) {
1305 int m = n + magic_cookie_glitch;
1307 /* check for turn-on:
1308 * If we are writing an attributed blank, where the
1309 * previous cell is not attributed.
1311 if (ISBLANK(newLine[n])
1313 && xmc_turn_on(SP_PARM, newLine[n - 1], newLine[n]))
1316 && xmc_turn_on(SP_PARM,
1317 xmc_new(SP_PARM, lineno - 1,
1318 screen_columns(SP_PARM) - 1),
1323 PutChar(NCURSES_SP_ARGx CHREF(newLine[n]));
1325 /* check for turn-off:
1326 * If we are writing an attributed non-blank, where the
1327 * next cell is blank, and not attributed.
1329 if (!ISBLANK(newLine[n])
1330 && ((n + 1 < screen_columns(SP_PARM)
1331 && xmc_turn_off(SP_PARM, newLine[n], newLine[n + 1]))
1332 || (n + 1 >= screen_columns(SP_PARM)
1333 && lineno + 1 < screen_lines(SP_PARM)
1334 && xmc_turn_off(SP_PARM,
1336 xmc_new(SP_PARM, lineno + 1, 0))))) {
1345 /* it may be cheap to clear leading whitespace with clr_bol */
1347 if (clr_bol && can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1348 int oFirstChar, nFirstChar;
1350 for (oFirstChar = 0;
1351 oFirstChar < screen_columns(SP_PARM);
1353 if (!CharEq(oldLine[oFirstChar], blank))
1355 for (nFirstChar = 0;
1356 nFirstChar < screen_columns(SP_PARM);
1358 if (!CharEq(newLine[nFirstChar], blank))
1361 if (nFirstChar == oFirstChar) {
1362 firstChar = nFirstChar;
1363 /* find the first differing character */
1364 while (firstChar < screen_columns(SP_PARM)
1365 && CharEq(newLine[firstChar], oldLine[firstChar]))
1367 } else if (oFirstChar > nFirstChar) {
1368 firstChar = nFirstChar;
1369 } else { /* oFirstChar < nFirstChar */
1370 firstChar = oFirstChar;
1371 if (SP_PARM->_el1_cost < nFirstChar - oFirstChar) {
1372 if (nFirstChar >= screen_columns(SP_PARM)
1373 && SP_PARM->_el_cost <= SP_PARM->_el1_cost) {
1374 GoTo(NCURSES_SP_ARGx lineno, 0);
1375 UpdateAttrs(SP_PARM, blank);
1376 TPUTS_TRACE("clr_eol");
1377 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1379 GoTo(NCURSES_SP_ARGx lineno, nFirstChar - 1);
1380 UpdateAttrs(SP_PARM, blank);
1381 TPUTS_TRACE("clr_bol");
1382 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_bol);
1385 while (firstChar < nFirstChar)
1386 oldLine[firstChar++] = blank;
1390 /* find the first differing character */
1391 while (firstChar < screen_columns(SP_PARM)
1392 && CharEq(newLine[firstChar], oldLine[firstChar]))
1395 /* if there wasn't one, we're done */
1396 if (firstChar >= screen_columns(SP_PARM)) {
1397 TR(TRACE_UPDATE, (T_RETURN("")));
1401 blank = newLine[screen_columns(SP_PARM) - 1];
1403 if (!can_clear_with(NCURSES_SP_ARGx CHREF(blank))) {
1404 /* find the last differing character */
1405 nLastChar = screen_columns(SP_PARM) - 1;
1407 while (nLastChar > firstChar
1408 && CharEq(newLine[nLastChar], oldLine[nLastChar]))
1411 if (nLastChar >= firstChar) {
1412 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1413 PutRange(NCURSES_SP_ARGx
1419 memcpy(oldLine + firstChar,
1420 newLine + firstChar,
1421 (unsigned) (nLastChar - firstChar + 1) * sizeof(NCURSES_CH_T));
1423 TR(TRACE_UPDATE, (T_RETURN("")));
1427 /* find last non-blank character on old line */
1428 oLastChar = screen_columns(SP_PARM) - 1;
1429 while (oLastChar > firstChar && CharEq(oldLine[oLastChar], blank))
1432 /* find last non-blank character on new line */
1433 nLastChar = screen_columns(SP_PARM) - 1;
1434 while (nLastChar > firstChar && CharEq(newLine[nLastChar], blank))
1437 if ((nLastChar == firstChar)
1438 && (SP_PARM->_el_cost < (oLastChar - nLastChar))) {
1439 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1440 if (!CharEq(newLine[firstChar], blank))
1441 PutChar(NCURSES_SP_ARGx CHREF(newLine[firstChar]));
1442 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1443 } else if ((nLastChar != oLastChar)
1444 && (!CharEq(newLine[nLastChar], oldLine[oLastChar])
1445 || !(SP_PARM->_nc_sp_idcok
1446 && NCURSES_SP_NAME(has_ic) (NCURSES_SP_ARG)))) {
1447 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1448 if ((oLastChar - nLastChar) > SP_PARM->_el_cost) {
1449 if (PutRange(NCURSES_SP_ARGx
1455 GoTo(NCURSES_SP_ARGx lineno, nLastChar + 1);
1457 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1459 n = max(nLastChar, oLastChar);
1460 PutRange(NCURSES_SP_ARGx
1468 int nLastNonblank = nLastChar;
1469 int oLastNonblank = oLastChar;
1471 /* find the last characters that really differ */
1472 /* can be -1 if no characters differ */
1473 while (CharEq(newLine[nLastChar], oldLine[oLastChar])) {
1474 /* don't split a wide char */
1475 if (isWidecExt(newLine[nLastChar]) &&
1476 !CharEq(newLine[nLastChar - 1], oldLine[oLastChar - 1]))
1480 if (nLastChar == -1 || oLastChar == -1)
1484 n = min(oLastChar, nLastChar);
1485 if (n >= firstChar) {
1486 GoTo(NCURSES_SP_ARGx lineno, firstChar);
1487 PutRange(NCURSES_SP_ARGx
1495 if (oLastChar < nLastChar) {
1496 int m = max(nLastNonblank, oLastNonblank);
1497 #if USE_WIDEC_SUPPORT
1498 while (isWidecExt(newLine[n + 1]) && n) {
1503 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1504 if ((nLastChar < nLastNonblank)
1505 || InsCharCost(SP_PARM, nLastChar - oLastChar) > (m - n)) {
1506 PutRange(NCURSES_SP_ARGx
1513 InsStr(NCURSES_SP_ARGx &newLine[n + 1], nLastChar - oLastChar);
1515 } else if (oLastChar > nLastChar) {
1516 GoTo(NCURSES_SP_ARGx lineno, n + 1);
1517 if (DelCharCost(SP_PARM, oLastChar - nLastChar)
1518 > SP_PARM->_el_cost + nLastNonblank - (n + 1)) {
1519 if (PutRange(NCURSES_SP_ARGx oldLine, newLine, lineno,
1520 n + 1, nLastNonblank))
1521 GoTo(NCURSES_SP_ARGx lineno, nLastNonblank + 1);
1522 ClrToEOL(NCURSES_SP_ARGx blank, FALSE);
1525 * The delete-char sequence will
1526 * effectively shift in blanks from the
1527 * right margin of the screen. Ensure
1528 * that they are the right color by
1529 * setting the video attributes from
1530 * the last character on the row.
1532 UpdateAttrs(SP_PARM, blank);
1533 DelChar(NCURSES_SP_ARGx oLastChar - nLastChar);
1539 /* update the code's internal representation */
1540 if (screen_columns(SP_PARM) > firstChar)
1541 memcpy(oldLine + firstChar,
1542 newLine + firstChar,
1543 (unsigned) (screen_columns(SP_PARM) - firstChar) * sizeof(NCURSES_CH_T));
1544 TR(TRACE_UPDATE, (T_RETURN("")));
1549 ** ClearScreen(blank)
1551 ** Clear the physical screen and put cursor at home
1556 ClearScreen(NCURSES_SP_DCLx NCURSES_CH_T blank)
1559 bool fast_clear = (clear_screen || clr_eos || clr_eol);
1561 TR(TRACE_UPDATE, ("ClearScreen() called"));
1563 #if NCURSES_EXT_FUNCS
1564 if (SP_PARM->_coloron
1565 && !SP_PARM->_default_color) {
1566 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
1567 (short) GET_SCREEN_PAIR(SP_PARM),
1570 NCURSES_SP_NAME(_nc_outch));
1571 if (!back_color_erase) {
1579 UpdateAttrs(SP_PARM, blank);
1580 TPUTS_TRACE("clear_screen");
1581 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clear_screen);
1582 SP_PARM->_cursrow = SP_PARM->_curscol = 0;
1583 position_check(SP_PARM,
1587 } else if (clr_eos) {
1588 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1589 GoTo(NCURSES_SP_ARGx 0, 0);
1590 UpdateAttrs(SP_PARM, blank);
1591 TPUTS_TRACE("clr_eos");
1592 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1594 screen_lines(SP_PARM),
1595 NCURSES_SP_NAME(_nc_outch));
1596 } else if (clr_eol) {
1597 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1598 UpdateAttrs(SP_PARM, blank);
1599 for (i = 0; i < screen_lines(SP_PARM); i++) {
1600 GoTo(NCURSES_SP_ARGx i, 0);
1601 TPUTS_TRACE("clr_eol");
1602 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx clr_eol);
1604 GoTo(NCURSES_SP_ARGx 0, 0);
1607 UpdateAttrs(SP_PARM, blank);
1608 for (i = 0; i < screen_lines(SP_PARM); i++) {
1609 GoTo(NCURSES_SP_ARGx i, 0);
1610 for (j = 0; j < screen_columns(SP_PARM); j++)
1611 PutChar(NCURSES_SP_ARGx CHREF(blank));
1613 GoTo(NCURSES_SP_ARGx 0, 0);
1616 for (i = 0; i < screen_lines(SP_PARM); i++) {
1617 for (j = 0; j < screen_columns(SP_PARM); j++)
1618 CurScreen(SP_PARM)->_line[i].text[j] = blank;
1621 TR(TRACE_UPDATE, ("screen cleared"));
1625 ** InsStr(line, count)
1627 ** Insert the count characters pointed to by line.
1632 InsStr(NCURSES_SP_DCLx NCURSES_CH_T * line, int count)
1634 TR(TRACE_UPDATE, ("InsStr(%p, %p,%d) called",
1636 (void *) line, count));
1638 /* Prefer parm_ich as it has the smallest cost - no need to shift
1639 * the whole line on each character. */
1640 /* The order must match that of InsCharCost. */
1642 TPUTS_TRACE("parm_ich");
1643 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1644 TPARM_1(parm_ich, count),
1646 NCURSES_SP_NAME(_nc_outch));
1648 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1652 } else if (enter_insert_mode && exit_insert_mode) {
1653 TPUTS_TRACE("enter_insert_mode");
1654 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx enter_insert_mode);
1656 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1657 if (insert_padding) {
1658 TPUTS_TRACE("insert_padding");
1659 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1664 TPUTS_TRACE("exit_insert_mode");
1665 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
1668 TPUTS_TRACE("insert_character");
1669 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_character);
1670 PutAttrChar(NCURSES_SP_ARGx CHREF(*line));
1671 if (insert_padding) {
1672 TPUTS_TRACE("insert_padding");
1673 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_padding);
1679 position_check(SP_PARM, SP_PARM->_cursrow, SP_PARM->_curscol, "InsStr");
1685 ** Delete count characters at current position
1690 DelChar(NCURSES_SP_DCLx int count)
1694 TR(TRACE_UPDATE, ("DelChar(%p, %d) called, position = (%ld,%ld)",
1695 (void *) SP_PARM, count,
1696 (long) NewScreen(SP_PARM)->_cury,
1697 (long) NewScreen(SP_PARM)->_curx));
1700 TPUTS_TRACE("parm_dch");
1701 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1702 TPARM_1(parm_dch, count),
1704 NCURSES_SP_NAME(_nc_outch));
1706 for (n = 0; n < count; n++) {
1707 TPUTS_TRACE("delete_character");
1708 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_character);
1714 * Physical-scrolling support
1716 * This code was adapted from Keith Bostic's hardware scrolling
1717 * support for 4.4BSD curses. I (esr) translated it to use terminfo
1718 * capabilities, narrowed the call interface slightly, and cleaned
1719 * up some convoluted tests. I also added support for the memory_above
1720 * memory_below, and non_dest_scroll_region capabilities.
1722 * For this code to work, we must have either
1723 * change_scroll_region and scroll forward/reverse commands, or
1724 * insert and delete line capabilities.
1725 * When the scrolling region has been set, the cursor has to
1726 * be at the last line of the region to make the scroll up
1727 * happen, or on the first line of region to scroll down.
1729 * This code makes one aesthetic decision in the opposite way from
1730 * BSD curses. BSD curses preferred pairs of il/dl operations
1731 * over scrolls, allegedly because il/dl looked faster. We, on
1732 * the other hand, prefer scrolls because (a) they're just as fast
1733 * on many terminals and (b) using them avoids bouncing an
1734 * unchanged bottom section of the screen up and down, which is
1737 * (lav): added more cases, used dl/il when bot==maxy and in csr case.
1739 * I used assumption that capabilities il/il1/dl/dl1 work inside
1740 * changed scroll region not shifting screen contents outside of it.
1741 * If there are any terminals behaving different way, it would be
1742 * necessary to add some conditions to scroll_csr_forward/backward.
1745 /* Try to scroll up assuming given csr (miny, maxy). Returns ERR on failure */
1747 scroll_csr_forward(NCURSES_SP_DCLx
1757 if (n == 1 && scroll_forward && top == miny && bot == maxy) {
1758 GoTo(NCURSES_SP_ARGx bot, 0);
1759 UpdateAttrs(SP_PARM, blank);
1760 TPUTS_TRACE("scroll_forward");
1761 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1762 } else if (n == 1 && delete_line && bot == maxy) {
1763 GoTo(NCURSES_SP_ARGx top, 0);
1764 UpdateAttrs(SP_PARM, blank);
1765 TPUTS_TRACE("delete_line");
1766 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1767 } else if (parm_index && top == miny && bot == maxy) {
1768 GoTo(NCURSES_SP_ARGx bot, 0);
1769 UpdateAttrs(SP_PARM, blank);
1770 TPUTS_TRACE("parm_index");
1771 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1772 TPARM_2(parm_index, n, 0),
1774 NCURSES_SP_NAME(_nc_outch));
1775 } else if (parm_delete_line && bot == maxy) {
1776 GoTo(NCURSES_SP_ARGx top, 0);
1777 UpdateAttrs(SP_PARM, blank);
1778 TPUTS_TRACE("parm_delete_line");
1779 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1780 TPARM_2(parm_delete_line, n, 0),
1782 NCURSES_SP_NAME(_nc_outch));
1783 } else if (scroll_forward && top == miny && bot == maxy) {
1784 GoTo(NCURSES_SP_ARGx bot, 0);
1785 UpdateAttrs(SP_PARM, blank);
1786 for (i = 0; i < n; i++) {
1787 TPUTS_TRACE("scroll_forward");
1788 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_forward);
1790 } else if (delete_line && bot == maxy) {
1791 GoTo(NCURSES_SP_ARGx top, 0);
1792 UpdateAttrs(SP_PARM, blank);
1793 for (i = 0; i < n; i++) {
1794 TPUTS_TRACE("delete_line");
1795 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1800 #if NCURSES_EXT_FUNCS
1801 if (FILL_BCE(SP_PARM)) {
1803 for (i = 0; i < n; i++) {
1804 GoTo(NCURSES_SP_ARGx bot - i, 0);
1805 for (j = 0; j < screen_columns(SP_PARM); j++)
1806 PutChar(NCURSES_SP_ARGx CHREF(blank));
1813 /* Try to scroll down assuming given csr (miny, maxy). Returns ERR on failure */
1816 scroll_csr_backward(NCURSES_SP_DCLx
1826 if (n == 1 && scroll_reverse && top == miny && bot == maxy) {
1827 GoTo(NCURSES_SP_ARGx top, 0);
1828 UpdateAttrs(SP_PARM, blank);
1829 TPUTS_TRACE("scroll_reverse");
1830 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1831 } else if (n == 1 && insert_line && bot == maxy) {
1832 GoTo(NCURSES_SP_ARGx top, 0);
1833 UpdateAttrs(SP_PARM, blank);
1834 TPUTS_TRACE("insert_line");
1835 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1836 } else if (parm_rindex && top == miny && bot == maxy) {
1837 GoTo(NCURSES_SP_ARGx top, 0);
1838 UpdateAttrs(SP_PARM, blank);
1839 TPUTS_TRACE("parm_rindex");
1840 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1841 TPARM_2(parm_rindex, n, 0),
1843 NCURSES_SP_NAME(_nc_outch));
1844 } else if (parm_insert_line && bot == maxy) {
1845 GoTo(NCURSES_SP_ARGx top, 0);
1846 UpdateAttrs(SP_PARM, blank);
1847 TPUTS_TRACE("parm_insert_line");
1848 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1849 TPARM_2(parm_insert_line, n, 0),
1851 NCURSES_SP_NAME(_nc_outch));
1852 } else if (scroll_reverse && top == miny && bot == maxy) {
1853 GoTo(NCURSES_SP_ARGx top, 0);
1854 UpdateAttrs(SP_PARM, blank);
1855 for (i = 0; i < n; i++) {
1856 TPUTS_TRACE("scroll_reverse");
1857 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx scroll_reverse);
1859 } else if (insert_line && bot == maxy) {
1860 GoTo(NCURSES_SP_ARGx top, 0);
1861 UpdateAttrs(SP_PARM, blank);
1862 for (i = 0; i < n; i++) {
1863 TPUTS_TRACE("insert_line");
1864 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1869 #if NCURSES_EXT_FUNCS
1870 if (FILL_BCE(SP_PARM)) {
1872 for (i = 0; i < n; i++) {
1873 GoTo(NCURSES_SP_ARGx top + i, 0);
1874 for (j = 0; j < screen_columns(SP_PARM); j++)
1875 PutChar(NCURSES_SP_ARGx CHREF(blank));
1882 /* scroll by using delete_line at del and insert_line at ins */
1885 scroll_idl(NCURSES_SP_DCLx int n, int del, int ins, NCURSES_CH_T blank)
1889 if (!((parm_delete_line || delete_line) && (parm_insert_line || insert_line)))
1892 GoTo(NCURSES_SP_ARGx del, 0);
1893 UpdateAttrs(SP_PARM, blank);
1894 if (n == 1 && delete_line) {
1895 TPUTS_TRACE("delete_line");
1896 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1897 } else if (parm_delete_line) {
1898 TPUTS_TRACE("parm_delete_line");
1899 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1900 TPARM_2(parm_delete_line, n, 0),
1902 NCURSES_SP_NAME(_nc_outch));
1903 } else { /* if (delete_line) */
1904 for (i = 0; i < n; i++) {
1905 TPUTS_TRACE("delete_line");
1906 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx delete_line);
1910 GoTo(NCURSES_SP_ARGx ins, 0);
1911 UpdateAttrs(SP_PARM, blank);
1912 if (n == 1 && insert_line) {
1913 TPUTS_TRACE("insert_line");
1914 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1915 } else if (parm_insert_line) {
1916 TPUTS_TRACE("parm_insert_line");
1917 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1918 TPARM_2(parm_insert_line, n, 0),
1920 NCURSES_SP_NAME(_nc_outch));
1921 } else { /* if (insert_line) */
1922 for (i = 0; i < n; i++) {
1923 TPUTS_TRACE("insert_line");
1924 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx insert_line);
1932 * Note: some terminals require the cursor to be within the scrolling margins
1933 * before setting them. Generally, the cursor must be at the appropriate end
1934 * of the scrolling margins when issuing an indexing operation (it is not
1935 * apparent whether it must also be at the left margin; we do this just to be
1936 * safe). To make the related cursor movement a little faster, we use the
1937 * save/restore cursor capabilities if the terminal has them.
1940 NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_DCLx
1945 /* scroll region from top to bot by n lines */
1949 bool cursor_saved = FALSE;
1952 TR(TRACE_MOVE, ("_nc_scrolln(%p, %d, %d, %d, %d)",
1953 (void *) SP_PARM, n, top, bot, maxy));
1955 if (!IsValidScreen(SP_PARM))
1958 blank = ClrBlank(NCURSES_SP_ARGx StdScreen(SP_PARM));
1962 * If we scroll, we might remove a cookie.
1964 if (magic_cookie_glitch > 0) {
1969 if (n > 0) { /* scroll up (forward) */
1971 * Explicitly clear if stuff pushed off top of region might
1972 * be saved by the terminal.
1974 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, 0, maxy, blank);
1976 if (res == ERR && change_scroll_region) {
1977 if ((((n == 1 && scroll_forward) || parm_index)
1978 && (SP_PARM->_cursrow == bot || SP_PARM->_cursrow == bot - 1))
1979 && save_cursor && restore_cursor) {
1980 cursor_saved = TRUE;
1981 TPUTS_TRACE("save_cursor");
1982 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
1984 TPUTS_TRACE("change_scroll_region");
1985 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1986 TPARM_2(change_scroll_region, top, bot));
1988 TPUTS_TRACE("restore_cursor");
1989 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
1991 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
1994 res = scroll_csr_forward(NCURSES_SP_ARGx n, top, bot, top, bot, blank);
1996 TPUTS_TRACE("change_scroll_region");
1997 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
1998 TPARM_2(change_scroll_region, 0, maxy));
1999 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2002 if (res == ERR && SP_PARM->_nc_sp_idlok)
2003 res = scroll_idl(NCURSES_SP_ARGx n, top, bot - n + 1, blank);
2006 * Clear the newly shifted-in text.
2009 && (non_dest_scroll_region || (memory_below && bot == maxy))) {
2010 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2011 if (bot == maxy && clr_eos) {
2012 GoTo(NCURSES_SP_ARGx bot - n + 1, 0);
2013 ClrToEOS(NCURSES_SP_ARGx blank2);
2015 for (i = 0; i < n; i++) {
2016 GoTo(NCURSES_SP_ARGx bot - i, 0);
2017 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2022 } else { /* (n < 0) - scroll down (backward) */
2023 res = scroll_csr_backward(NCURSES_SP_ARGx -n, top, bot, 0, maxy, blank);
2025 if (res == ERR && change_scroll_region) {
2027 && (SP_PARM->_cursrow == top ||
2028 SP_PARM->_cursrow == top - 1)
2029 && save_cursor && restore_cursor) {
2030 cursor_saved = TRUE;
2031 TPUTS_TRACE("save_cursor");
2032 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx save_cursor);
2034 TPUTS_TRACE("change_scroll_region");
2035 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2036 TPARM_2(change_scroll_region, top, bot));
2038 TPUTS_TRACE("restore_cursor");
2039 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx restore_cursor);
2041 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2044 res = scroll_csr_backward(NCURSES_SP_ARGx
2045 -n, top, bot, top, bot, blank);
2047 TPUTS_TRACE("change_scroll_region");
2048 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2049 TPARM_2(change_scroll_region, 0, maxy));
2050 SP_PARM->_cursrow = SP_PARM->_curscol = -1;
2053 if (res == ERR && SP_PARM->_nc_sp_idlok)
2054 res = scroll_idl(NCURSES_SP_ARGx -n, bot + n + 1, top, blank);
2057 * Clear the newly shifted-in text.
2060 && (non_dest_scroll_region || (memory_above && top == 0))) {
2061 static const NCURSES_CH_T blank2 = NewChar(BLANK_TEXT);
2062 for (i = 0; i < -n; i++) {
2063 GoTo(NCURSES_SP_ARGx i + top, 0);
2064 ClrToEOL(NCURSES_SP_ARGx blank2, FALSE);
2072 _nc_scroll_window(CurScreen(SP_PARM), n,
2073 (NCURSES_SIZE_T) top,
2074 (NCURSES_SIZE_T) bot,
2077 /* shift hash values too - they can be reused */
2078 NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_ARGx n, top, bot);
2083 #if NCURSES_SP_FUNCS
2085 _nc_scrolln(int n, int top, int bot, int maxy)
2087 return NCURSES_SP_NAME(_nc_scrolln) (CURRENT_SCREEN, n, top, bot, maxy);
2091 NCURSES_EXPORT(void)
2092 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_DCL0)
2096 /* make sure terminal is in a sane known state */
2097 SetAttr(SCREEN_ATTRS(SP_PARM), A_NORMAL);
2098 NewScreen(SP_PARM)->_clear = TRUE;
2100 /* reset color pairs and definitions */
2101 if (SP_PARM->_coloron || SP_PARM->_color_defs)
2102 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2104 /* restore user-defined colors, if any */
2105 if (SP_PARM->_color_defs < 0) {
2107 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
2108 for (n = 0; n < SP_PARM->_color_defs; ++n) {
2109 if (SP_PARM->_color_table[n].init) {
2110 NCURSES_SP_NAME(init_color) (NCURSES_SP_ARGx
2112 SP_PARM->_color_table[n].r,
2113 SP_PARM->_color_table[n].g,
2114 SP_PARM->_color_table[n].b);
2119 if (exit_attribute_mode)
2120 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_attribute_mode);
2122 /* turn off attributes */
2123 if (exit_alt_charset_mode)
2124 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_alt_charset_mode);
2125 if (exit_standout_mode)
2126 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_standout_mode);
2127 if (exit_underline_mode)
2128 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_underline_mode);
2130 if (exit_insert_mode)
2131 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx exit_insert_mode);
2132 if (enter_am_mode && exit_am_mode)
2133 NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx
2139 #if NCURSES_SP_FUNCS
2140 NCURSES_EXPORT(void)
2141 _nc_screen_resume(void)
2143 NCURSES_SP_NAME(_nc_screen_resume) (CURRENT_SCREEN);
2147 NCURSES_EXPORT(void)
2148 NCURSES_SP_NAME(_nc_screen_init) (NCURSES_SP_DCL0)
2150 NCURSES_SP_NAME(_nc_screen_resume) (NCURSES_SP_ARG);
2153 #if NCURSES_SP_FUNCS
2154 NCURSES_EXPORT(void)
2155 _nc_screen_init(void)
2157 NCURSES_SP_NAME(_nc_screen_init) (CURRENT_SCREEN);
2161 /* wrap up screen handling */
2162 NCURSES_EXPORT(void)
2163 NCURSES_SP_NAME(_nc_screen_wrap) (NCURSES_SP_DCL0)
2168 UpdateAttrs(SP_PARM, normal);
2169 #if NCURSES_EXT_FUNCS
2170 if (SP_PARM->_coloron
2171 && !SP_PARM->_default_color) {
2172 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
2173 SP_PARM->_default_color = TRUE;
2174 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx
2178 NCURSES_SP_NAME(_nc_outch));
2179 SP_PARM->_default_color = FALSE;
2181 TINFO_MVCUR(NCURSES_SP_ARGx
2184 screen_lines(SP_PARM) - 1,
2187 ClrToEOL(NCURSES_SP_ARGx blank, TRUE);
2190 if (SP_PARM->_color_defs) {
2191 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_ARG);
2195 #if NCURSES_SP_FUNCS
2196 NCURSES_EXPORT(void)
2197 _nc_screen_wrap(void)
2199 NCURSES_SP_NAME(_nc_screen_wrap) (CURRENT_SCREEN);
2204 NCURSES_EXPORT(void)
2205 NCURSES_SP_NAME(_nc_do_xmc_glitch) (NCURSES_SP_DCLx attr_t previous)
2208 attr_t chg = XMC_CHANGES(previous ^ AttrOf(SCREEN_ATTRS(SP_PARM)));
2212 SP_PARM->_curscol += magic_cookie_glitch;
2213 if (SP_PARM->_curscol >= SP_PARM->_columns)
2214 wrap_cursor(NCURSES_SP_ARG);
2215 TR(TRACE_UPDATE, ("bumped to %d,%d after cookie",
2216 SP_PARM->_cursrow, SP_PARM->_curscol));
2223 #if NCURSES_SP_FUNCS
2224 NCURSES_EXPORT(void)
2225 _nc_do_xmc_glitch(attr_t previous)
2227 NCURSES_SP_NAME(_nc_do_xmc_glitch) (CURRENT_SCREEN, previous);
2231 #endif /* USE_XMC_SUPPORT */