bff11107ed5a2c48713a6d09af92ba2c80b511e2
[dragonfly.git] / usr.bin / window / wwscroll.c
1 /*      $NetBSD: wwscroll.c,v 1.7 2003/08/07 11:17:44 agc Exp $ */
2
3 /*
4  * Copyright (c) 1983, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Edward Wang at The University of California, Berkeley.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)wwscroll.c  8.1 (Berkeley) 6/6/93";
39 #else
40 __RCSID("$NetBSD: wwscroll.c,v 1.7 2003/08/07 11:17:44 agc Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <stdlib.h>
45
46 #include "ww.h"
47 #include "tt.h"
48 #include "xx.h"
49
50 void
51 wwscroll(struct ww *w, int n)
52 {
53         int dir;
54         int top;
55
56         if (n == 0)
57                 return;
58         dir = n < 0 ? -1 : 1;
59         top = w->ww_b.t - n;
60         if (top > w->ww_w.t)
61                 top = w->ww_w.t;
62         else if (top + w->ww_b.nr < w->ww_w.b)
63                 top = w->ww_w.b - w->ww_b.nr;
64         n = abs(top - w->ww_b.t);
65         if (n < w->ww_i.nr) {
66                 while (--n >= 0) {
67                         (void) wwscroll1(w, w->ww_i.t, w->ww_i.b, dir, 0);
68                         w->ww_buf += dir;
69                         w->ww_b.t -= dir;
70                         w->ww_b.b -= dir;
71                 }
72         } else {
73                 w->ww_buf -= top - w->ww_b.t;
74                 w->ww_b.t = top;
75                 w->ww_b.b = top + w->ww_b.nr;
76                 wwredrawwin(w);
77         }
78 }
79
80 /*
81  * Scroll one line, between 'row1' and 'row2', in direction 'dir'.
82  * Don't adjust ww_scroll.
83  * And don't redraw 'leaveit' lines.
84  */
85 int
86 wwscroll1(struct ww *w, int row1, int row2, int dir, int leaveit)
87 {
88         int i;
89         int row1x, row2x;
90         int nvis;
91         int nvismax;
92         int scrolled = 0;
93
94         /*
95          * See how many lines on the screen are affected.
96          * And calculate row1x, row2x, and left at the same time.
97          */
98         for (i = row1; i < row2 && w->ww_nvis[i] == 0; i++)
99                 ;
100         if (i >= row2)                  /* can't do any fancy stuff */
101                 goto out;
102         row1x = i;
103         for (i = row2 - 1; i >= row1 && w->ww_nvis[i] == 0; i--)
104                 ;
105         if (i <= row1x)
106                 goto out;               /* just one line is easy */
107         row2x = i + 1;
108
109         /*
110          * See how much of this window is visible.
111          */
112         nvismax = wwncol * (row2x - row1x);
113         nvis = 0;
114         for (i = row1x; i < row2x; i++)
115                 nvis += w->ww_nvis[i];
116
117         /*
118          * If it's a good idea to scroll and the terminal can, then do it.
119          */
120         if (nvis < nvismax / 2)
121                 goto no_scroll;         /* not worth it */
122         if ((dir > 0 ? tt.tt_scroll_down == 0 : tt.tt_scroll_up == 0) ||
123             ((tt.tt_scroll_top != row1x || tt.tt_scroll_bot != row2x - 1) &&
124             tt.tt_setscroll == 0))
125                 if (tt.tt_delline == 0 || tt.tt_insline == 0)
126                         goto no_scroll;
127         xxscroll(dir, row1x, row2x);
128         scrolled = 1;
129         /*
130          * Fix up the old screen.
131          */
132         {
133                 union ww_char *tmp;
134                 union ww_char **cpp, **cqq;
135
136                 if (dir > 0) {
137                         cpp = &wwos[row1x];
138                         cqq = cpp + 1;
139                         tmp = *cpp;
140                         for (i = row2x - row1x; --i > 0;)
141                                 *cpp++ = *cqq++;
142                         *cpp = tmp;
143                 } else {
144                         cpp = &wwos[row2x];
145                         cqq = cpp - 1;
146                         tmp = *cqq;
147                         for (i = row2x - row1x; --i > 0;)
148                                 *--cpp = *--cqq;
149                         *cqq = tmp;
150                 }
151                 for (i = wwncol; --i >= 0;)
152                         tmp++->c_w = ' ';
153         }
154
155 no_scroll:
156         /*
157          * Fix the new screen.
158          */
159         if (nvis == nvismax) {
160                 /*
161                  * Can shift whole lines.
162                  */
163                 if (dir > 0) {
164                         {
165                                 union ww_char *tmp;
166                                 union ww_char **cpp, **cqq;
167
168                                 cpp = &wwns[row1x];
169                                 cqq = cpp + 1;
170                                 tmp = *cpp;
171                                 for (i = row2x - row1x; --i > 0;)
172                                         *cpp++ = *cqq++;
173                                 *cpp = tmp;
174                         }
175                         if (scrolled) {
176                                 char *p, *q;
177
178                                 p = &wwtouched[row1x];
179                                 q = p + 1;
180                                 for (i = row2x - row1x; --i > 0;)
181                                         *p++ = *q++;
182                                 *p |= WWU_TOUCHED;
183                         } else {
184                                 char *p;
185
186                                 p = &wwtouched[row1x];
187                                 for (i = row2x - row1x; --i >= 0;)
188                                         *p++ |= WWU_TOUCHED;
189                         }
190                         wwredrawwin1(w, row1, row1x, dir);
191                         wwredrawwin1(w, row2x - 1, row2 - leaveit, dir);
192                 } else {
193                         {
194                                 union ww_char *tmp;
195                                 union ww_char **cpp, **cqq;
196
197                                 cpp = &wwns[row2x];
198                                 cqq = cpp - 1;
199                                 tmp = *cqq;
200                                 for (i = row2x - row1x; --i > 0;)
201                                         *--cpp = *--cqq;
202                                 *cqq = tmp;
203                         }
204                         if (scrolled) {
205                                 char *p, *q;
206
207                                 p = &wwtouched[row2x];
208                                 q = p - 1;
209                                 for (i = row2x - row1x; --i > 0;)
210                                         *--p = *--q;
211                                 *q |= WWU_TOUCHED;
212                         } else {
213                                 char *p;
214
215                                 p = &wwtouched[row1x];
216                                 for (i = row2x - row1x; --i >= 0;)
217                                         *p++ |= WWU_TOUCHED;
218                         }
219                         wwredrawwin1(w, row1 + leaveit, row1x + 1, dir);
220                         wwredrawwin1(w, row2x, row2, dir);
221                 }
222         } else {
223                 if (scrolled) {
224                         char *p;
225
226                         p = &wwtouched[row1x];
227                         for (i = row2x - row1x; --i >= 0;)
228                                 *p++ |= WWU_TOUCHED;
229                 }
230 out:
231                 if (dir > 0)
232                         wwredrawwin1(w, row1, row2 - leaveit, dir);
233                 else
234                         wwredrawwin1(w, row1 + leaveit, row2, dir);
235         }
236         return scrolled;
237 }