kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / misc / syscons / schistory.c
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * Copyright (c) 1992-1998 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/syscons/schistory.c,v 1.8.2.1 2001/07/19 06:38:53 dd Exp $
30  * $DragonFly: src/sys/dev/misc/syscons/schistory.c,v 1.4 2003/08/07 21:16:59 dillon Exp $
31  */
32
33 #include "use_sc.h"
34 #include "opt_syscons.h"
35
36 #ifndef SC_NO_HISTORY
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/conf.h>
41 #include <sys/tty.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44
45 #include <machine/console.h>
46 #include <machine/pc/display.h>
47
48 #include "syscons.h"
49
50 #if !defined(SC_MAX_HISTORY_SIZE)
51 #define SC_MAX_HISTORY_SIZE     (1000 * MAXCONS * NSC)
52 #endif
53
54 #if !defined(SC_HISTORY_SIZE)
55 #define SC_HISTORY_SIZE         (ROW * 4)
56 #endif
57
58 #if (SC_HISTORY_SIZE * MAXCONS * NSC) > SC_MAX_HISTORY_SIZE
59 #undef SC_MAX_HISTORY_SIZE
60 #define SC_MAX_HISTORY_SIZE     (SC_HISTORY_SIZE * MAXCONS * NSC)
61 #endif
62
63 /* local variables */
64 static int              extra_history_size
65                                 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
66
67 /* local functions */
68 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
69 static void history_to_screen(scr_stat *scp);
70
71 /* allocate a history buffer */
72 int
73 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
74 {
75         /*
76          * syscons unconditionally allocates buffers upto 
77          * SC_HISTORY_SIZE lines or scp->ysize lines, whichever 
78          * is larger. A value greater than that is allowed, 
79          * subject to extra_history_size.
80          */
81         sc_vtb_t *history;
82         sc_vtb_t *prev_history;
83         int cur_lines;                          /* current buffer size */
84         int min_lines;                          /* guaranteed buffer size */
85         int delta;                              /* lines to put back */
86
87         if (lines <= 0)
88                 lines = SC_HISTORY_SIZE;        /* use the default value */
89
90         /* make it at least as large as the screen size */
91         lines = imax(lines, scp->ysize);
92
93         /* remove the history buffer while we update it */
94         history = prev_history = scp->history;
95         scp->history = NULL;
96
97         /* calculate the amount of lines to put back to extra_history_size */
98         delta = 0;
99         if (prev_history) {
100                 cur_lines = sc_vtb_rows(history);
101                 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
102                 if (cur_lines > min_lines)
103                         delta = cur_lines - min_lines;
104         }
105
106         /* lines upto min_lines are always allowed. */
107         min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
108         if (lines > min_lines) {
109                 if (lines - min_lines > extra_history_size + delta) {
110                         /* too many lines are requested */
111                         scp->history = prev_history;
112                         return EINVAL;
113                 }
114         }
115
116         /* allocate a new buffer */
117         history = (sc_vtb_t *)malloc(sizeof(*history),
118                                      M_DEVBUF,
119                                      (wait) ? M_WAITOK : M_NOWAIT);
120         if (history != NULL) {
121                 if (lines > min_lines)
122                         extra_history_size -= lines - min_lines;
123                 /* XXX error check? */
124                 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
125                             NULL, wait);
126                 /* FIXME: XXX no good? */
127                 sc_vtb_clear(history, scp->sc->scr_map[0x20],
128                              SC_NORM_ATTR << 8);
129                 if (prev_history != NULL)
130                         copy_history(prev_history, history);
131                 scp->history_pos = sc_vtb_tail(history);
132         } else {
133                 scp->history_pos = 0;
134         }
135
136         /* destroy the previous buffer */
137         if (prev_history != NULL) {
138                 extra_history_size += delta;
139                 sc_vtb_destroy(prev_history);
140                 free(prev_history, M_DEVBUF);
141         }
142
143         scp->history = history;
144
145         return 0;
146 }
147
148 static void
149 copy_history(sc_vtb_t *from, sc_vtb_t *to)
150 {
151         int lines;
152         int cols;
153         int cols1;
154         int cols2;
155         int pos;
156         int i;
157
158         lines = sc_vtb_rows(from);
159         cols1 = sc_vtb_cols(from);
160         cols2 = sc_vtb_cols(to);
161         cols = imin(cols1, cols2);
162         pos = sc_vtb_tail(from);
163         for (i = 0; i < lines; ++i) {
164                 sc_vtb_append(from, pos, to, cols);
165                 if (cols < cols2)
166                         sc_vtb_seek(to, sc_vtb_pos(to, 
167                                                    sc_vtb_tail(to), 
168                                                    cols2 - cols));
169                 pos = sc_vtb_pos(from, pos, cols1);
170         }
171 }
172
173 void
174 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
175 {
176         sc_vtb_t *history;
177         int cur_lines;                          /* current buffer size */
178         int min_lines;                          /* guaranteed buffer size */
179
180         history = scp->history;
181         scp->history = NULL;
182         if (history == NULL)
183                 return;
184
185         cur_lines = sc_vtb_rows(history);
186         min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
187         extra_history_size += (cur_lines > min_lines) ? 
188                                   cur_lines - min_lines : 0;
189
190         sc_vtb_destroy(history);
191         free(history, M_DEVBUF);
192 }
193
194 /* copy entire screen into the top of the history buffer */
195 void
196 sc_hist_save(scr_stat *scp)
197 {
198         sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
199         scp->history_pos = sc_vtb_tail(scp->history);
200 }
201
202 /* restore the screen by copying from the history buffer */
203 int
204 sc_hist_restore(scr_stat *scp)
205 {
206         int ret;
207
208         if (scp->history_pos != sc_vtb_tail(scp->history)) {
209                 scp->history_pos = sc_vtb_tail(scp->history);
210                 history_to_screen(scp);
211                 ret =  0;
212         } else {
213                 ret = 1;
214         }
215         sc_vtb_seek(scp->history, sc_vtb_pos(scp->history, 
216                                              sc_vtb_tail(scp->history),
217                                              -scp->xsize*scp->ysize));
218         return ret;
219 }
220
221 /* copy screen-full of saved lines */
222 static void
223 history_to_screen(scr_stat *scp)
224 {
225         int pos;
226         int i;
227
228         pos = scp->history_pos;
229         for (i = 1; i <= scp->ysize; ++i) {
230                 pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
231                 sc_vtb_copy(scp->history, pos,
232                             &scp->vtb, scp->xsize*(scp->ysize - i),
233                             scp->xsize);
234         }
235         mark_all(scp);
236 }
237
238 /* go to the tail of the history buffer */
239 void
240 sc_hist_home(scr_stat *scp)
241 {
242         scp->history_pos = sc_vtb_tail(scp->history);
243         history_to_screen(scp);
244 }
245
246 /* go to the top of the history buffer */
247 void
248 sc_hist_end(scr_stat *scp)
249 {
250         scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
251                                       scp->xsize*scp->ysize);
252         history_to_screen(scp);
253 }
254
255 /* move one line up */
256 int
257 sc_hist_up_line(scr_stat *scp)
258 {
259         if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
260             == sc_vtb_tail(scp->history))
261                 return -1;
262         scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
263                                       -scp->xsize);
264         history_to_screen(scp);
265         return 0;
266 }
267
268 /* move one line down */
269 int
270 sc_hist_down_line(scr_stat *scp)
271 {
272         if (scp->history_pos == sc_vtb_tail(scp->history))
273                 return -1;
274         scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
275                                       scp->xsize);
276         history_to_screen(scp);
277         return 0;
278 }
279
280 int
281 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
282               struct thread *td)
283 {
284         scr_stat *scp;
285         int error;
286
287         switch (cmd) {
288
289         case CONS_HISTORY:      /* set history size */
290                 scp = SC_STAT(tp->t_dev);
291                 if (*(int *)data <= 0)
292                         return EINVAL;
293                 if (scp->status & BUFFER_SAVED)
294                         return EBUSY;
295                 DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n",
296                             *(int *)data, scp->ysize, extra_history_size));
297                 error = sc_alloc_history_buffer(scp, 
298                                                imax(*(int *)data, scp->ysize),
299                                                scp->ysize, TRUE);
300                 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
301                             sc_vtb_rows(scp->history), extra_history_size));
302                 return error;
303
304         case CONS_CLRHIST:
305                 scp = SC_STAT(tp->t_dev);
306                 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
307                     SC_NORM_ATTR << 8);
308                 return 0;
309         }
310
311         return ENOIOCTL;
312 }
313
314 #endif /* SC_NO_HISTORY */