xref: /dragonfly/usr.bin/window/xxflush.c (revision d4ef6694)
1 /*	@(#)xxflush.c	8.1 (Berkeley) 6/6/93	*/
2 /*	$NetBSD: xxflush.c,v 1.6 2003/08/07 11:17:47 agc Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Wang at The University of California, Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "ww.h"
37 #include "xx.h"
38 #include "tt.h"
39 
40 void
41 xxflush(int intr)
42 {
43 	struct xx *xp, *xq;
44 
45 	for (xp = xx_head; xp != NULL && !(intr && wwinterrupt()); xp = xq) {
46 		switch (xp->cmd) {
47 		case xc_move:
48 			if (xp->link == 0)
49 				(*tt.tt_move)(xp->arg0, xp->arg1);
50 			break;
51 		case xc_scroll:
52 			xxflush_scroll(xp);
53 			break;
54 		case xc_inschar:
55 			(*tt.tt_move)(xp->arg0, xp->arg1);
56 			tt.tt_nmodes = xp->arg3;
57 			(*tt.tt_inschar)(xp->arg2);
58 			break;
59 		case xc_insspace:
60 			(*tt.tt_move)(xp->arg0, xp->arg1);
61 			(*tt.tt_insspace)(xp->arg2);
62 			break;
63 		case xc_delchar:
64 			(*tt.tt_move)(xp->arg0, xp->arg1);
65 			(*tt.tt_delchar)(xp->arg2);
66 			break;
67 		case xc_clear:
68 			(*tt.tt_clear)();
69 			break;
70 		case xc_clreos:
71 			(*tt.tt_move)(xp->arg0, xp->arg1);
72 			(*tt.tt_clreos)();
73 			break;
74 		case xc_clreol:
75 			(*tt.tt_move)(xp->arg0, xp->arg1);
76 			(*tt.tt_clreol)();
77 			break;
78 		case xc_write:
79 			(*tt.tt_move)(xp->arg0, xp->arg1);
80 			tt.tt_nmodes = xp->arg3;
81 			(*tt.tt_write)(xp->buf, xp->arg2);
82 			break;
83 		}
84 		xq = xp->link;
85 		xxfree(xp);
86 	}
87 	if ((xx_head = xp) == NULL) {
88 		xx_tail = 0;
89 		xxbufp = xxbuf;
90 	}
91 	ttflush();
92 }
93 
94 void
95 xxflush_scroll(struct xx *xp)
96 {
97 	struct xx *xq;
98 
99  top:
100 	if (xp->arg0 == 0)
101 		return;
102 	/*
103 	 * We handle retain (da and db) by putting the burden on scrolling up,
104 	 * which is the less common operation.  It must ensure that
105 	 * text is not pushed below the screen, so scrolling down doesn't
106 	 * have to worry about it.
107 	 *
108 	 * Try scrolling region (or scrolling the whole screen) first.
109 	 * Can we assume "sr" doesn't push text below the screen
110 	 * so we don't have to worry about retain below?
111 	 * What about scrolling down with a newline?  It probably does
112 	 * push text above (with da).  Scrolling up would then have
113 	 * to take care of that.
114 	 * It's easy to be fool proof, but that slows things down.
115 	 * The current solution is to disallow tt_scroll_up if da or db is true
116 	 * but cs (scrolling region) is not.  Again, we sacrifice scrolling
117 	 * up in favor of scrolling down.  The idea is having scrolling regions
118 	 * probably means we can scroll (even the whole screen) with impunity.
119 	 * This lets us work efficiently on simple terminals (use newline
120 	 * on the bottom to scroll), on any terminal without retain, and
121 	 * on vt100 style scrolling regions (I think).
122 	 */
123 	if (xp->arg0 > 0) {
124 		if ((xq = xp->link) != NULL && xq->cmd == xc_scroll &&
125 		    xp->arg2 == xq->arg2 && xq->arg0 < 0) {
126 			if (xp->arg1 < xq->arg1) {
127 				if (xp->arg2 - xp->arg0 <= xq->arg1) {
128 					xq->arg0 = xp->arg0;
129 					xq->arg1 = xp->arg1;
130 					xq->arg2 = xp->arg2;
131 					return;
132 				}
133 				xp->arg2 = xq->arg1 + xp->arg0;
134 				xq->arg0 += xp->arg0;
135 				xq->arg1 = xp->arg2;
136 				if (xq->arg0 > 0)
137 					xq->arg1 -= xq->arg0;
138 				goto top;
139 			} else {
140 				if (xp->arg1 - xq->arg0 >= xp->arg2)
141 					return;
142 				xq->arg2 = xp->arg1 - xq->arg0;
143 				xp->arg0 += xq->arg0;
144 				xp->arg1 = xq->arg2;
145 				if (xp->arg0 < 0)
146 					xp->arg1 += xp->arg0;
147 				goto top;
148 			}
149 		}
150 		if (xp->arg0 > xp->arg2 - xp->arg1)
151 			xp->arg0 = xp->arg2 - xp->arg1;
152 		if (tt.tt_scroll_down) {
153 			if (tt.tt_scroll_top != xp->arg1 ||
154 			    tt.tt_scroll_bot != xp->arg2 - 1) {
155 				if (tt.tt_setscroll == 0)
156 					goto down;
157 				(*tt.tt_setscroll)(xp->arg1, xp->arg2 - 1);
158 			}
159 			tt.tt_scroll_down(xp->arg0);
160 		} else {
161 		down:
162 			(*tt.tt_move)(xp->arg1, 0);
163 			(*tt.tt_delline)(xp->arg0);
164 			if (xp->arg2 < tt.tt_nrow) {
165 				(*tt.tt_move)(xp->arg2 - xp->arg0, 0);
166 				(*tt.tt_insline)(xp->arg0);
167 			}
168 		}
169 	} else {
170 		xp->arg0 = - xp->arg0;
171 		if (xp->arg0 > xp->arg2 - xp->arg1)
172 			xp->arg0 = xp->arg2 - xp->arg1;
173 		if (tt.tt_scroll_up) {
174 			if (tt.tt_scroll_top != xp->arg1 ||
175 			    tt.tt_scroll_bot != xp->arg2 - 1) {
176 				if (tt.tt_setscroll == 0)
177 					goto up;
178 				(*tt.tt_setscroll)(xp->arg1, xp->arg2 - 1);
179 			}
180 			tt.tt_scroll_up(xp->arg0);
181 		} else  {
182 		up:
183 			if (tt.tt_retain || xp->arg2 != tt.tt_nrow) {
184 				(*tt.tt_move)(xp->arg2 - xp->arg0, 0);
185 				(*tt.tt_delline)(xp->arg0);
186 			}
187 			(*tt.tt_move)(xp->arg1, 0);
188 			(*tt.tt_insline)(xp->arg0);
189 		}
190 	}
191 }
192