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(__arm__) || defined(__mips__) || \
46 defined(__powerpc__) || defined(__sparc64__)
47 #include <machine/sc_machdep.h>
49 #include <machine/pc/display.h>
52 #include <dev/syscons/syscons.h>
56 * This calculations should be dynamically scaled by number of separate sc
57 * devices. A base value of 'extra_history_size' should be defined for
58 * each syscons unit, and added and subtracted from the dynamic
59 * 'extra_history_size' as units are added and removed. This way, each time
60 * a new syscons unit goes online, extra_history_size is automatically bumped.
64 #if !defined(SC_MAX_HISTORY_SIZE)
65 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * MAXSC)
68 #if !defined(SC_HISTORY_SIZE)
69 #define SC_HISTORY_SIZE (ROW * 4)
72 #if (SC_HISTORY_SIZE * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE
73 #undef SC_MAX_HISTORY_SIZE
74 #define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * MAXSC)
78 static int extra_history_size
79 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
82 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
83 static void history_to_screen(scr_stat *scp);
85 /* allocate a history buffer */
87 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
90 * syscons unconditionally allocates buffers up to
91 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever
92 * is larger. A value greater than that is allowed,
93 * subject to extra_history_size.
96 sc_vtb_t *prev_history;
97 int cur_lines; /* current buffer size */
98 int min_lines; /* guaranteed buffer size */
99 int delta; /* lines to put back */
102 lines = SC_HISTORY_SIZE; /* use the default value */
104 /* make it at least as large as the screen size */
105 lines = imax(lines, scp->ysize);
107 /* remove the history buffer while we update it */
108 history = prev_history = scp->history;
111 /* calculate the amount of lines to put back to extra_history_size */
114 cur_lines = sc_vtb_rows(history);
115 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
116 if (cur_lines > min_lines)
117 delta = cur_lines - min_lines;
120 /* lines up to min_lines are always allowed. */
121 min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
122 if (lines > min_lines) {
123 if (lines - min_lines > extra_history_size + delta) {
124 /* too many lines are requested */
125 scp->history = prev_history;
130 /* allocate a new buffer */
131 history = (sc_vtb_t *)malloc(sizeof(*history),
133 (wait) ? M_WAITOK : M_NOWAIT);
134 if (history != NULL) {
135 if (lines > min_lines)
136 extra_history_size -= lines - min_lines;
137 /* XXX error check? */
138 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
140 /* FIXME: XXX no good? */
141 sc_vtb_clear(history, scp->sc->scr_map[0x20],
143 if (prev_history != NULL)
144 copy_history(prev_history, history);
145 scp->history_pos = sc_vtb_tail(history);
147 scp->history_pos = 0;
150 /* destroy the previous buffer */
151 if (prev_history != NULL) {
152 extra_history_size += delta;
153 sc_vtb_destroy(prev_history);
154 free(prev_history, M_DEVBUF);
157 scp->history = history;
163 copy_history(sc_vtb_t *from, sc_vtb_t *to)
172 lines = sc_vtb_rows(from);
173 cols1 = sc_vtb_cols(from);
174 cols2 = sc_vtb_cols(to);
175 cols = imin(cols1, cols2);
176 pos = sc_vtb_tail(from);
177 for (i = 0; i < lines; ++i) {
178 sc_vtb_append(from, pos, to, cols);
180 sc_vtb_seek(to, sc_vtb_pos(to,
183 pos = sc_vtb_pos(from, pos, cols1);
188 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
191 int cur_lines; /* current buffer size */
192 int min_lines; /* guaranteed buffer size */
194 history = scp->history;
199 cur_lines = sc_vtb_rows(history);
200 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
201 extra_history_size += (cur_lines > min_lines) ?
202 cur_lines - min_lines : 0;
204 sc_vtb_destroy(history);
205 free(history, M_DEVBUF);
208 /* copy entire screen into the top of the history buffer */
210 sc_hist_save(scr_stat *scp)
212 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
213 scp->history_pos = sc_vtb_tail(scp->history);
216 /* restore the screen by copying from the history buffer */
218 sc_hist_restore(scr_stat *scp)
222 if (scp->history_pos != sc_vtb_tail(scp->history)) {
223 scp->history_pos = sc_vtb_tail(scp->history);
224 history_to_screen(scp);
229 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
230 sc_vtb_tail(scp->history),
231 -scp->xsize*scp->ysize));
235 /* copy screen-full of saved lines */
237 history_to_screen(scr_stat *scp)
242 pos = scp->history_pos;
243 for (i = 1; i <= scp->ysize; ++i) {
244 pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
245 sc_vtb_copy(scp->history, pos,
246 &scp->vtb, scp->xsize*(scp->ysize - i),
252 /* go to the tail of the history buffer */
254 sc_hist_home(scr_stat *scp)
256 scp->history_pos = sc_vtb_tail(scp->history);
257 history_to_screen(scp);
260 /* go to the top of the history buffer */
262 sc_hist_end(scr_stat *scp)
264 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
265 scp->xsize*scp->ysize);
266 history_to_screen(scp);
269 /* move one line up */
271 sc_hist_up_line(scr_stat *scp)
273 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
274 == sc_vtb_tail(scp->history))
276 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
278 history_to_screen(scp);
282 /* move one line down */
284 sc_hist_down_line(scr_stat *scp)
286 if (scp->history_pos == sc_vtb_tail(scp->history))
288 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
290 history_to_screen(scp);
295 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
302 case CONS_HISTORY: /* set history size */
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));
319 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
327 #endif /* SC_NO_HISTORY */