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