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.8 2006/09/05 00:55:38 dillon Exp $
34 #include "opt_syscons.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <machine/console.h>
45 #include <machine/pc/display.h>
49 #if !defined(SC_MAX_HISTORY_SIZE)
50 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * NSC)
53 #if !defined(SC_HISTORY_SIZE)
54 #define SC_HISTORY_SIZE (ROW * 4)
57 #if (SC_HISTORY_SIZE * MAXCONS * NSC) > SC_MAX_HISTORY_SIZE
58 #undef SC_MAX_HISTORY_SIZE
59 #define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * NSC)
63 static int extra_history_size
64 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
67 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
68 static void history_to_screen(scr_stat *scp);
70 /* allocate a history buffer */
72 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
75 * syscons unconditionally allocates buffers upto
76 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever
77 * is larger. A value greater than that is allowed,
78 * subject to extra_history_size.
81 sc_vtb_t *prev_history;
82 int cur_lines; /* current buffer size */
83 int min_lines; /* guaranteed buffer size */
84 int delta; /* lines to put back */
87 lines = SC_HISTORY_SIZE; /* use the default value */
89 /* make it at least as large as the screen size */
90 lines = imax(lines, scp->ysize);
92 /* remove the history buffer while we update it */
93 history = prev_history = scp->history;
96 /* calculate the amount of lines to put back to extra_history_size */
99 cur_lines = sc_vtb_rows(history);
100 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
101 if (cur_lines > min_lines)
102 delta = cur_lines - min_lines;
105 /* lines upto min_lines are always allowed. */
106 min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
107 if (lines > min_lines) {
108 if (lines - min_lines > extra_history_size + delta) {
109 /* too many lines are requested */
110 scp->history = prev_history;
115 /* allocate a new buffer */
116 history = kmalloc(sizeof(*history), M_SYSCONS,
117 (wait) ? M_WAITOK : M_NOWAIT);
118 if (history != NULL) {
119 if (lines > min_lines)
120 extra_history_size -= lines - min_lines;
121 /* XXX error check? */
122 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
124 /* FIXME: XXX no good? */
125 sc_vtb_clear(history, scp->sc->scr_map[0x20],
127 if (prev_history != NULL)
128 copy_history(prev_history, history);
129 scp->history_pos = sc_vtb_tail(history);
131 scp->history_pos = 0;
134 /* destroy the previous buffer */
135 if (prev_history != NULL) {
136 extra_history_size += delta;
137 sc_vtb_destroy(prev_history);
138 kfree(prev_history, M_SYSCONS);
141 scp->history = history;
147 copy_history(sc_vtb_t *from, sc_vtb_t *to)
156 lines = sc_vtb_rows(from);
157 cols1 = sc_vtb_cols(from);
158 cols2 = sc_vtb_cols(to);
159 cols = imin(cols1, cols2);
160 pos = sc_vtb_tail(from);
161 for (i = 0; i < lines; ++i) {
162 sc_vtb_append(from, pos, to, cols);
164 sc_vtb_seek(to, sc_vtb_pos(to,
167 pos = sc_vtb_pos(from, pos, cols1);
172 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
175 int cur_lines; /* current buffer size */
176 int min_lines; /* guaranteed buffer size */
178 history = scp->history;
183 cur_lines = sc_vtb_rows(history);
184 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
185 extra_history_size += (cur_lines > min_lines) ?
186 cur_lines - min_lines : 0;
188 sc_vtb_destroy(history);
189 kfree(history, M_SYSCONS);
192 /* copy entire screen into the top of the history buffer */
194 sc_hist_save(scr_stat *scp)
196 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
197 scp->history_pos = sc_vtb_tail(scp->history);
200 /* restore the screen by copying from the history buffer */
202 sc_hist_restore(scr_stat *scp)
206 if (scp->history_pos != sc_vtb_tail(scp->history)) {
207 scp->history_pos = sc_vtb_tail(scp->history);
208 history_to_screen(scp);
213 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
214 sc_vtb_tail(scp->history),
215 -scp->xsize*scp->ysize));
219 /* copy screen-full of saved lines */
221 history_to_screen(scr_stat *scp)
226 pos = scp->history_pos;
227 for (i = 1; i <= scp->ysize; ++i) {
228 pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
229 sc_vtb_copy(scp->history, pos,
230 &scp->vtb, scp->xsize*(scp->ysize - i),
236 /* go to the tail of the history buffer */
238 sc_hist_home(scr_stat *scp)
240 scp->history_pos = sc_vtb_tail(scp->history);
241 history_to_screen(scp);
244 /* go to the top of the history buffer */
246 sc_hist_end(scr_stat *scp)
248 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
249 scp->xsize*scp->ysize);
250 history_to_screen(scp);
253 /* move one line up */
255 sc_hist_up_line(scr_stat *scp)
257 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
258 == sc_vtb_tail(scp->history))
260 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
262 history_to_screen(scp);
266 /* move one line down */
268 sc_hist_down_line(scr_stat *scp)
270 if (scp->history_pos == sc_vtb_tail(scp->history))
272 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
274 history_to_screen(scp);
279 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag)
286 case CONS_HISTORY: /* set history size */
287 scp = SC_STAT(tp->t_dev);
288 if (*(int *)data <= 0)
290 if (scp->status & BUFFER_SAVED)
292 DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n",
293 *(int *)data, scp->ysize, extra_history_size));
294 error = sc_alloc_history_buffer(scp,
295 imax(*(int *)data, scp->ysize),
297 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
298 sc_vtb_rows(scp->history), extra_history_size));
302 scp = SC_STAT(tp->t_dev);
303 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
311 #endif /* SC_NO_HISTORY */