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