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