2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * Copyright (c) 1992-1998 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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 $
34 #include "opt_syscons.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <machine/console.h>
46 #include <machine/pc/display.h>
50 #if !defined(SC_MAX_HISTORY_SIZE)
51 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * NSC)
54 #if !defined(SC_HISTORY_SIZE)
55 #define SC_HISTORY_SIZE (ROW * 4)
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)
64 static int extra_history_size
65 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
68 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
69 static void history_to_screen(scr_stat *scp);
71 /* allocate a history buffer */
73 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
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.
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 */
88 lines = SC_HISTORY_SIZE; /* use the default value */
90 /* make it at least as large as the screen size */
91 lines = imax(lines, scp->ysize);
93 /* remove the history buffer while we update it */
94 history = prev_history = scp->history;
97 /* calculate the amount of lines to put back to extra_history_size */
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;
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;
116 /* allocate a new buffer */
117 history = (sc_vtb_t *)malloc(sizeof(*history),
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,
126 /* FIXME: XXX no good? */
127 sc_vtb_clear(history, scp->sc->scr_map[0x20],
129 if (prev_history != NULL)
130 copy_history(prev_history, history);
131 scp->history_pos = sc_vtb_tail(history);
133 scp->history_pos = 0;
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);
143 scp->history = history;
149 copy_history(sc_vtb_t *from, sc_vtb_t *to)
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);
166 sc_vtb_seek(to, sc_vtb_pos(to,
169 pos = sc_vtb_pos(from, pos, cols1);
174 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
177 int cur_lines; /* current buffer size */
178 int min_lines; /* guaranteed buffer size */
180 history = scp->history;
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;
190 sc_vtb_destroy(history);
191 free(history, M_DEVBUF);
194 /* copy entire screen into the top of the history buffer */
196 sc_hist_save(scr_stat *scp)
198 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
199 scp->history_pos = sc_vtb_tail(scp->history);
202 /* restore the screen by copying from the history buffer */
204 sc_hist_restore(scr_stat *scp)
208 if (scp->history_pos != sc_vtb_tail(scp->history)) {
209 scp->history_pos = sc_vtb_tail(scp->history);
210 history_to_screen(scp);
215 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
216 sc_vtb_tail(scp->history),
217 -scp->xsize*scp->ysize));
221 /* copy screen-full of saved lines */
223 history_to_screen(scr_stat *scp)
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),
238 /* go to the tail of the history buffer */
240 sc_hist_home(scr_stat *scp)
242 scp->history_pos = sc_vtb_tail(scp->history);
243 history_to_screen(scp);
246 /* go to the top of the history buffer */
248 sc_hist_end(scr_stat *scp)
250 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
251 scp->xsize*scp->ysize);
252 history_to_screen(scp);
255 /* move one line up */
257 sc_hist_up_line(scr_stat *scp)
259 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
260 == sc_vtb_tail(scp->history))
262 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
264 history_to_screen(scp);
268 /* move one line down */
270 sc_hist_down_line(scr_stat *scp)
272 if (scp->history_pos == sc_vtb_tail(scp->history))
274 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
276 history_to_screen(scp);
281 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
289 case CONS_HISTORY: /* set history size */
290 scp = SC_STAT(tp->t_dev);
291 if (*(int *)data <= 0)
293 if (scp->status & BUFFER_SAVED)
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),
300 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
301 sc_vtb_rows(scp->history), extra_history_size));
305 scp = SC_STAT(tp->t_dev);
306 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
314 #endif /* SC_NO_HISTORY */