2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Copyright (c) 1992-1998 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_syscons.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/consio.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #if defined(__arm__) || defined(__mips__) || defined(__powerpc__)
48 #include <machine/sc_machdep.h>
50 #include <machine/pc/display.h>
53 #include <dev/syscons/syscons.h>
57 * This calculations should be dynamically scaled by number of separate sc
58 * devices. A base value of 'extra_history_size' should be defined for
59 * each syscons unit, and added and subtracted from the dynamic
60 * 'extra_history_size' as units are added and removed. This way, each time
61 * a new syscons unit goes online, extra_history_size is automatically bumped.
65 #if !defined(SC_MAX_HISTORY_SIZE)
66 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * MAXSC)
69 #if !defined(SC_HISTORY_SIZE)
70 #define SC_HISTORY_SIZE (ROW * 4)
73 #if (SC_HISTORY_SIZE * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE
74 #undef SC_MAX_HISTORY_SIZE
75 #define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * MAXSC)
79 static int extra_history_size
80 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
83 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
84 static void history_to_screen(scr_stat *scp);
86 /* allocate a history buffer */
88 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
91 * syscons unconditionally allocates buffers up to
92 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever
93 * is larger. A value greater than that is allowed,
94 * subject to extra_history_size.
97 sc_vtb_t *prev_history;
98 int cur_lines; /* current buffer size */
99 int min_lines; /* guaranteed buffer size */
100 int delta; /* lines to put back */
103 lines = SC_HISTORY_SIZE; /* use the default value */
105 /* make it at least as large as the screen size */
106 lines = imax(lines, scp->ysize);
108 /* remove the history buffer while we update it */
109 history = prev_history = scp->history;
112 /* calculate the amount of lines to put back to extra_history_size */
115 cur_lines = sc_vtb_rows(history);
116 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
117 if (cur_lines > min_lines)
118 delta = cur_lines - min_lines;
121 /* lines up to min_lines are always allowed. */
122 min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
123 if (lines > min_lines) {
124 if (lines - min_lines > extra_history_size + delta) {
125 /* too many lines are requested */
126 scp->history = prev_history;
131 /* allocate a new buffer */
132 history = (sc_vtb_t *)malloc(sizeof(*history),
134 (wait) ? M_WAITOK : M_NOWAIT);
135 if (history != NULL) {
136 if (lines > min_lines)
137 extra_history_size -= lines - min_lines;
138 /* XXX error check? */
139 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
141 /* FIXME: XXX no good? */
142 sc_vtb_clear(history, scp->sc->scr_map[0x20],
144 if (prev_history != NULL)
145 copy_history(prev_history, history);
146 scp->history_pos = sc_vtb_tail(history);
148 scp->history_pos = 0;
151 /* destroy the previous buffer */
152 if (prev_history != NULL) {
153 extra_history_size += delta;
154 sc_vtb_destroy(prev_history);
155 free(prev_history, M_DEVBUF);
158 scp->history = history;
164 copy_history(sc_vtb_t *from, sc_vtb_t *to)
173 lines = sc_vtb_rows(from);
174 cols1 = sc_vtb_cols(from);
175 cols2 = sc_vtb_cols(to);
176 cols = imin(cols1, cols2);
177 pos = sc_vtb_tail(from);
178 for (i = 0; i < lines; ++i) {
179 sc_vtb_append(from, pos, to, cols);
181 sc_vtb_seek(to, sc_vtb_pos(to,
184 pos = sc_vtb_pos(from, pos, cols1);
189 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
192 int cur_lines; /* current buffer size */
193 int min_lines; /* guaranteed buffer size */
195 history = scp->history;
200 cur_lines = sc_vtb_rows(history);
201 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
202 extra_history_size += (cur_lines > min_lines) ?
203 cur_lines - min_lines : 0;
205 sc_vtb_destroy(history);
206 free(history, M_DEVBUF);
209 /* copy entire screen into the top of the history buffer */
211 sc_hist_save(scr_stat *scp)
213 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
214 scp->history_pos = sc_vtb_tail(scp->history);
217 /* restore the screen by copying from the history buffer */
219 sc_hist_restore(scr_stat *scp)
223 if (scp->history_pos != sc_vtb_tail(scp->history)) {
224 scp->history_pos = sc_vtb_tail(scp->history);
225 history_to_screen(scp);
230 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
231 sc_vtb_tail(scp->history),
232 -scp->xsize*scp->ysize));
236 /* copy screen-full of saved lines */
238 history_to_screen(scr_stat *scp)
243 pos = scp->history_pos;
244 for (i = 1; i <= scp->ysize; ++i) {
245 pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
246 sc_vtb_copy(scp->history, pos,
247 &scp->vtb, scp->xsize*(scp->ysize - i),
253 /* go to the tail of the history buffer */
255 sc_hist_home(scr_stat *scp)
257 scp->history_pos = sc_vtb_tail(scp->history);
258 history_to_screen(scp);
261 /* go to the top of the history buffer */
263 sc_hist_end(scr_stat *scp)
265 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
266 scp->xsize*scp->ysize);
267 history_to_screen(scp);
270 /* move one line up */
272 sc_hist_up_line(scr_stat *scp)
274 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
275 == sc_vtb_tail(scp->history))
277 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
279 history_to_screen(scp);
283 /* move one line down */
285 sc_hist_down_line(scr_stat *scp)
287 if (scp->history_pos == sc_vtb_tail(scp->history))
289 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
291 history_to_screen(scp);
296 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 */