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