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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_syscons.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/consio.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #if defined(__sparc64__) || defined(__powerpc__)
46 #include <machine/sc_machdep.h>
48 #include <machine/pc/display.h>
51 #include <dev/syscons/syscons.h>
55 * This calculations should be dynamically scaled by number of seperate sc
56 * devices. A base value of 'extra_history_size' should be defined for
57 * each syscons unit, and added and subtracted from the dynamic
58 * 'extra_history_size' as units are added and removed. This way, each time
59 * a new syscons unit goes online, extra_history_size is automatically bumped.
63 #if !defined(SC_MAX_HISTORY_SIZE)
64 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * MAXSC)
67 #if !defined(SC_HISTORY_SIZE)
68 #define SC_HISTORY_SIZE (ROW * 4)
71 #if (SC_HISTORY_SIZE * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE
72 #undef SC_MAX_HISTORY_SIZE
73 #define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * MAXSC)
77 static int extra_history_size
78 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
81 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
82 static void history_to_screen(scr_stat *scp);
84 /* allocate a history buffer */
86 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
89 * syscons unconditionally allocates buffers upto
90 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever
91 * is larger. A value greater than that is allowed,
92 * subject to extra_history_size.
95 sc_vtb_t *prev_history;
96 int cur_lines; /* current buffer size */
97 int min_lines; /* guaranteed buffer size */
98 int delta; /* lines to put back */
101 lines = SC_HISTORY_SIZE; /* use the default value */
103 /* make it at least as large as the screen size */
104 lines = imax(lines, scp->ysize);
106 /* remove the history buffer while we update it */
107 history = prev_history = scp->history;
110 /* calculate the amount of lines to put back to extra_history_size */
113 cur_lines = sc_vtb_rows(history);
114 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
115 if (cur_lines > min_lines)
116 delta = cur_lines - min_lines;
119 /* lines upto min_lines are always allowed. */
120 min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
121 if (lines > min_lines) {
122 if (lines - min_lines > extra_history_size + delta) {
123 /* too many lines are requested */
124 scp->history = prev_history;
129 /* allocate a new buffer */
130 history = (sc_vtb_t *)malloc(sizeof(*history),
132 (wait) ? M_WAITOK : M_NOWAIT);
133 if (history != NULL) {
134 if (lines > min_lines)
135 extra_history_size -= lines - min_lines;
136 /* XXX error check? */
137 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
139 /* FIXME: XXX no good? */
140 sc_vtb_clear(history, scp->sc->scr_map[0x20],
142 if (prev_history != NULL)
143 copy_history(prev_history, history);
144 scp->history_pos = sc_vtb_tail(history);
146 scp->history_pos = 0;
149 /* destroy the previous buffer */
150 if (prev_history != NULL) {
151 extra_history_size += delta;
152 sc_vtb_destroy(prev_history);
153 free(prev_history, M_DEVBUF);
156 scp->history = history;
162 copy_history(sc_vtb_t *from, sc_vtb_t *to)
171 lines = sc_vtb_rows(from);
172 cols1 = sc_vtb_cols(from);
173 cols2 = sc_vtb_cols(to);
174 cols = imin(cols1, cols2);
175 pos = sc_vtb_tail(from);
176 for (i = 0; i < lines; ++i) {
177 sc_vtb_append(from, pos, to, cols);
179 sc_vtb_seek(to, sc_vtb_pos(to,
182 pos = sc_vtb_pos(from, pos, cols1);
187 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
190 int cur_lines; /* current buffer size */
191 int min_lines; /* guaranteed buffer size */
193 history = scp->history;
198 cur_lines = sc_vtb_rows(history);
199 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
200 extra_history_size += (cur_lines > min_lines) ?
201 cur_lines - min_lines : 0;
203 sc_vtb_destroy(history);
204 free(history, M_DEVBUF);
207 /* copy entire screen into the top of the history buffer */
209 sc_hist_save(scr_stat *scp)
211 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
212 scp->history_pos = sc_vtb_tail(scp->history);
215 /* restore the screen by copying from the history buffer */
217 sc_hist_restore(scr_stat *scp)
221 if (scp->history_pos != sc_vtb_tail(scp->history)) {
222 scp->history_pos = sc_vtb_tail(scp->history);
223 history_to_screen(scp);
228 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
229 sc_vtb_tail(scp->history),
230 -scp->xsize*scp->ysize));
234 /* copy screen-full of saved lines */
236 history_to_screen(scr_stat *scp)
241 pos = scp->history_pos;
242 for (i = 1; i <= scp->ysize; ++i) {
243 pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
244 sc_vtb_copy(scp->history, pos,
245 &scp->vtb, scp->xsize*(scp->ysize - i),
251 /* go to the tail of the history buffer */
253 sc_hist_home(scr_stat *scp)
255 scp->history_pos = sc_vtb_tail(scp->history);
256 history_to_screen(scp);
259 /* go to the top of the history buffer */
261 sc_hist_end(scr_stat *scp)
263 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
264 scp->xsize*scp->ysize);
265 history_to_screen(scp);
268 /* move one line up */
270 sc_hist_up_line(scr_stat *scp)
272 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
273 == sc_vtb_tail(scp->history))
275 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
277 history_to_screen(scp);
281 /* move one line down */
283 sc_hist_down_line(scr_stat *scp)
285 if (scp->history_pos == sc_vtb_tail(scp->history))
287 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
289 history_to_screen(scp);
294 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
302 case CONS_HISTORY: /* set history size */
303 scp = SC_STAT(tp->t_dev);
304 if (*(int *)data <= 0)
306 if (scp->status & BUFFER_SAVED)
308 DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n",
309 *(int *)data, scp->ysize, extra_history_size));
310 error = sc_alloc_history_buffer(scp,
311 imax(*(int *)data, scp->ysize),
313 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
314 sc_vtb_rows(scp->history), extra_history_size));
318 scp = SC_STAT(tp->t_dev);
319 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
327 #endif /* SC_NO_HISTORY */