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