xref: /freebsd-14.2/sys/dev/syscons/schistory.c (revision 08e002f4)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <[email protected]>
3  * Copyright (c) 1992-1998 S�ren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
17  *
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.
28  *
29  * $FreeBSD$
30  */
31 
32 #include "opt_syscons.h"
33 
34 #ifndef SC_NO_HISTORY
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/tty.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 
43 #include <machine/console.h>
44 #include <machine/pc/display.h>
45 
46 #include <dev/syscons/syscons.h>
47 
48 #if !defined(SC_MAX_HISTORY_SIZE)
49 #define SC_MAX_HISTORY_SIZE	(1000 * MAXCONS * NSC)
50 #endif
51 
52 #if !defined(SC_HISTORY_SIZE)
53 #define SC_HISTORY_SIZE		(ROW * 4)
54 #endif
55 
56 #if (SC_HISTORY_SIZE * MAXCONS * NSC) > SC_MAX_HISTORY_SIZE
57 #undef SC_MAX_HISTORY_SIZE
58 #define SC_MAX_HISTORY_SIZE	(SC_HISTORY_SIZE * MAXCONS * NSC)
59 #endif
60 
61 /* local variables */
62 static int		extra_history_size
63 				= SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
64 
65 /* local functions */
66 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
67 static void history_to_screen(scr_stat *scp);
68 
69 /* allocate a history buffer */
70 int
71 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
72 {
73 	/*
74 	 * syscons unconditionally allocates buffers upto
75 	 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever
76 	 * is larger. A value greater than that is allowed,
77 	 * subject to extra_history_size.
78 	 */
79 	sc_vtb_t *history;
80 	sc_vtb_t *prev_history;
81 	int cur_lines;				/* current buffer size */
82 	int min_lines;				/* guaranteed buffer size */
83 	int delta;				/* lines to put back */
84 
85 	if (lines <= 0)
86 		lines = SC_HISTORY_SIZE;	/* use the default value */
87 
88 	/* make it at least as large as the screen size */
89 	lines = imax(lines, scp->ysize);
90 
91 	/* remove the history buffer while we update it */
92 	history = prev_history = scp->history;
93 	scp->history = NULL;
94 
95 	/* calculate the amount of lines to put back to extra_history_size */
96 	delta = 0;
97 	if (prev_history) {
98 		cur_lines = sc_vtb_rows(history);
99 		min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
100 		if (cur_lines > min_lines)
101 			delta = cur_lines - min_lines;
102 	}
103 
104 	/* lines upto min_lines are always allowed. */
105 	min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
106 	if (lines > min_lines) {
107 		if (lines - min_lines > extra_history_size + delta) {
108 			/* too many lines are requested */
109 			scp->history = prev_history;
110 			return EINVAL;
111 		}
112 	}
113 
114 	/* allocate a new buffer */
115 	history = (sc_vtb_t *)malloc(sizeof(*history),
116 				     M_DEVBUF,
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,
123 			    NULL, wait);
124 		/* FIXME: XXX no good? */
125 		sc_vtb_clear(history, scp->sc->scr_map[0x20],
126 			     SC_NORM_ATTR << 8);
127 		if (prev_history != NULL)
128 			copy_history(prev_history, history);
129 		scp->history_pos = sc_vtb_tail(history);
130 	} else {
131 		scp->history_pos = 0;
132 	}
133 
134 	/* destroy the previous buffer */
135 	if (prev_history != NULL) {
136 		extra_history_size += delta;
137 		sc_vtb_destroy(prev_history);
138 		free(prev_history, M_DEVBUF);
139 	}
140 
141 	scp->history = history;
142 
143 	return 0;
144 }
145 
146 static void
147 copy_history(sc_vtb_t *from, sc_vtb_t *to)
148 {
149 	int lines;
150 	int cols;
151 	int cols1;
152 	int cols2;
153 	int pos;
154 	int i;
155 
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);
163 		if (cols < cols2)
164 			sc_vtb_seek(to, sc_vtb_pos(to,
165 						   sc_vtb_tail(to),
166 						   cols2 - cols));
167 		pos = sc_vtb_pos(from, pos, cols1);
168 	}
169 }
170 
171 void
172 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
173 {
174 	sc_vtb_t *history;
175 	int cur_lines;				/* current buffer size */
176 	int min_lines;				/* guaranteed buffer size */
177 
178 	history = scp->history;
179 	scp->history = NULL;
180 	if (history == NULL)
181 		return;
182 
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;
187 
188 	sc_vtb_destroy(history);
189 	free(history, M_DEVBUF);
190 }
191 
192 /* copy entire screen into the top of the history buffer */
193 void
194 sc_hist_save(scr_stat *scp)
195 {
196 	sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
197 	scp->history_pos = sc_vtb_tail(scp->history);
198 }
199 
200 /* restore the screen by copying from the history buffer */
201 int
202 sc_hist_restore(scr_stat *scp)
203 {
204 	int ret;
205 
206 	if (scp->history_pos != sc_vtb_tail(scp->history)) {
207 		scp->history_pos = sc_vtb_tail(scp->history);
208 		history_to_screen(scp);
209 		ret =  0;
210 	} else {
211 		ret = 1;
212 	}
213 	sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
214 					     sc_vtb_tail(scp->history),
215 					     -scp->xsize*scp->ysize));
216 	return ret;
217 }
218 
219 /* copy screen-full of saved lines */
220 static void
221 history_to_screen(scr_stat *scp)
222 {
223 	int pos;
224 	int i;
225 
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),
231 			    scp->xsize);
232 	}
233 	mark_all(scp);
234 }
235 
236 /* go to the tail of the history buffer */
237 void
238 sc_hist_home(scr_stat *scp)
239 {
240 	scp->history_pos = sc_vtb_tail(scp->history);
241 	history_to_screen(scp);
242 }
243 
244 /* go to the top of the history buffer */
245 void
246 sc_hist_end(scr_stat *scp)
247 {
248 	scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
249 				      scp->xsize*scp->ysize);
250 	history_to_screen(scp);
251 }
252 
253 /* move one line up */
254 int
255 sc_hist_up_line(scr_stat *scp)
256 {
257 	if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
258 	    == sc_vtb_tail(scp->history))
259 		return -1;
260 	scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
261 				      -scp->xsize);
262 	history_to_screen(scp);
263 	return 0;
264 }
265 
266 /* move one line down */
267 int
268 sc_hist_down_line(scr_stat *scp)
269 {
270 	if (scp->history_pos == sc_vtb_tail(scp->history))
271 		return -1;
272 	scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
273 				      scp->xsize);
274 	history_to_screen(scp);
275 	return 0;
276 }
277 
278 int
279 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
280 	      struct proc *p)
281 {
282 	scr_stat *scp;
283 	int error;
284 
285 	switch (cmd) {
286 
287 	case CONS_HISTORY:  	/* set history size */
288 		scp = SC_STAT(tp->t_dev);
289 		if (*(int *)data <= 0)
290 			return EINVAL;
291 		if (scp->status & BUFFER_SAVED)
292 			return EBUSY;
293 		DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n",
294 			    *(int *)data, scp->ysize, extra_history_size));
295 		error = sc_alloc_history_buffer(scp,
296 					       imax(*(int *)data, scp->ysize),
297 					       scp->ysize, TRUE);
298 		DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
299 			    sc_vtb_rows(scp->history), extra_history_size));
300 		return error;
301 	}
302 
303 	return ENOIOCTL;
304 }
305 
306 #endif /* SC_NO_HISTORY */
307