#include <sys/cdefs.h>
#include "opt_syscons.h"
#ifndef SC_NO_HISTORY
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/consio.h>
#include <sys/tty.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#if defined(__arm__) || defined(__powerpc__)
#include <machine/sc_machdep.h>
#else
#include <machine/pc/display.h>
#endif
#include <dev/syscons/syscons.h>
#define MAXSC 1
#if !defined(SC_MAX_HISTORY_SIZE)
#define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * MAXSC)
#endif
#if !defined(SC_HISTORY_SIZE)
#define SC_HISTORY_SIZE (ROW * 4)
#endif
#if (SC_HISTORY_SIZE * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE
#undef SC_MAX_HISTORY_SIZE
#define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * MAXSC)
#endif
static int extra_history_size
= SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
static void history_to_screen(scr_stat *scp);
int
sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
{
sc_vtb_t *history;
sc_vtb_t *prev_history;
int cur_lines;
int min_lines;
int delta;
if (lines <= 0)
lines = SC_HISTORY_SIZE;
lines = imax(lines, scp->ysize);
history = prev_history = scp->history;
scp->history = NULL;
delta = 0;
if (prev_history) {
cur_lines = sc_vtb_rows(history);
min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
if (cur_lines > min_lines)
delta = cur_lines - min_lines;
}
min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
if (lines > min_lines) {
if (lines - min_lines > extra_history_size + delta) {
scp->history = prev_history;
return EINVAL;
}
}
history = (sc_vtb_t *)malloc(sizeof(*history),
M_DEVBUF,
(wait) ? M_WAITOK : M_NOWAIT);
if (history != NULL) {
if (lines > min_lines)
extra_history_size -= lines - min_lines;
sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
NULL, wait);
sc_vtb_clear(history, scp->sc->scr_map[0x20],
SC_NORM_ATTR << 8);
if (prev_history != NULL)
copy_history(prev_history, history);
scp->history_pos = sc_vtb_tail(history);
} else {
scp->history_pos = 0;
}
if (prev_history != NULL) {
extra_history_size += delta;
sc_vtb_destroy(prev_history);
free(prev_history, M_DEVBUF);
}
scp->history = history;
return 0;
}
static void
copy_history(sc_vtb_t *from, sc_vtb_t *to)
{
int lines;
int cols;
int cols1;
int cols2;
int pos;
int i;
lines = sc_vtb_rows(from);
cols1 = sc_vtb_cols(from);
cols2 = sc_vtb_cols(to);
cols = imin(cols1, cols2);
pos = sc_vtb_tail(from);
for (i = 0; i < lines; ++i) {
sc_vtb_append(from, pos, to, cols);
if (cols < cols2)
sc_vtb_seek(to, sc_vtb_pos(to,
sc_vtb_tail(to),
cols2 - cols));
pos = sc_vtb_pos(from, pos, cols1);
}
}
void
sc_free_history_buffer(scr_stat *scp, int prev_ysize)
{
sc_vtb_t *history;
int cur_lines;
int min_lines;
history = scp->history;
scp->history = NULL;
if (history == NULL)
return;
cur_lines = sc_vtb_rows(history);
min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
extra_history_size += (cur_lines > min_lines) ?
cur_lines - min_lines : 0;
sc_vtb_destroy(history);
free(history, M_DEVBUF);
}
void
sc_hist_save(scr_stat *scp)
{
sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
scp->history_pos = sc_vtb_tail(scp->history);
}
int
sc_hist_restore(scr_stat *scp)
{
int ret;
if (scp->history_pos != sc_vtb_tail(scp->history)) {
scp->history_pos = sc_vtb_tail(scp->history);
history_to_screen(scp);
ret = 0;
} else {
ret = 1;
}
sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
sc_vtb_tail(scp->history),
-scp->xsize*scp->ysize));
return ret;
}
static void
history_to_screen(scr_stat *scp)
{
int pos;
int i;
pos = scp->history_pos;
for (i = 1; i <= scp->ysize; ++i) {
pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
sc_vtb_copy(scp->history, pos,
&scp->vtb, scp->xsize*(scp->ysize - i),
scp->xsize);
}
mark_all(scp);
}
void
sc_hist_home(scr_stat *scp)
{
scp->history_pos = sc_vtb_tail(scp->history);
history_to_screen(scp);
}
void
sc_hist_end(scr_stat *scp)
{
scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
scp->xsize*scp->ysize);
history_to_screen(scp);
}
int
sc_hist_up_line(scr_stat *scp)
{
if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
== sc_vtb_tail(scp->history))
return -1;
scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
-scp->xsize);
history_to_screen(scp);
return 0;
}
int
sc_hist_down_line(scr_stat *scp)
{
if (scp->history_pos == sc_vtb_tail(scp->history))
return -1;
scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
scp->xsize);
history_to_screen(scp);
return 0;
}
int
sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
{
scr_stat *scp;
int error;
switch (cmd) {
case CONS_HISTORY:
scp = SC_STAT(tp);
if (*(int *)data <= 0)
return EINVAL;
if (scp->status & BUFFER_SAVED)
return EBUSY;
DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n",
*(int *)data, scp->ysize, extra_history_size));
error = sc_alloc_history_buffer(scp,
imax(*(int *)data, scp->ysize),
scp->ysize, TRUE);
DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
sc_vtb_rows(scp->history), extra_history_size));
return error;
case CONS_CLRHIST:
scp = SC_STAT(tp);
sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
SC_NORM_ATTR << 8);
return 0;
}
return ENOIOCTL;
}
#endif