/*-
* Copyright (c) 2005, 2006 Michael Lorenz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef VCONS_DRAW_INTR
int cells;
long *attrs;
uint32_t *chars;
int cursor_offset;
callout_t intr;
int intr_valid;
void *intr_softint;
int use_intr; /* use intr drawing when non-zero */
#endif
};
static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
long *);
static int vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
static int vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
int *, int *, long *);
static void vcons_free_screen(void *, void *);
static int vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
void *);
static int vcons_load_font(void *, void *, struct wsdisplay_font *);
/* support for reading/writing text buffers. For wsmoused */
static int vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
static int vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
/*
* a lock to serialize access to the framebuffer.
* when switching screens we need to make sure there's no rasops
* operation in progress
*/
#ifdef DIAGNOSTIC
vdp->switch_poll_count = 0;
#endif
#ifdef VCONS_DRAW_INTR
if (enable_intr) {
vdp->intr_softint = softint_establish(SOFTINT_SERIAL,
vcons_softintr, vd);
callout_init(&vdp->intr, CALLOUT_MPSAFE);
callout_setfunc(&vdp->intr, vcons_intr, vd);
vdp->intr_valid = 1;
static void
vcons_lock(struct vcons_screen *scr)
{
#ifdef VCONS_PARANOIA
int s;
s = splhigh();
#endif
SCREEN_BUSY(scr);
#ifdef VCONS_PARANOIA
splx(s);
#endif
}
static void
vcons_unlock(struct vcons_screen *scr)
{
#ifdef VCONS_PARANOIA
int s;
s = splhigh();
#endif
SCREEN_IDLE(scr);
#ifdef VCONS_PARANOIA
splx(s);
#endif
}
static void
vcons_dummy_init_screen(void *cookie,
struct vcons_screen *scr, int exists,
long *defattr)
{
/*
* default init_screen() method.
* Needs to be overwritten so we bitch and whine in case anyone ends
* up in here.
*/
printf("vcons_init_screen: dummy function called. Your driver is "
"supposed to supply a replacement for proper operation\n");
}
static int
vcons_alloc_buffers(struct vcons_data *vd, struct vcons_screen *scr)
{
struct rasops_info *ri = &scr->scr_ri;
int cnt, i;
#ifdef VCONS_DRAW_INTR
struct vcons_data_private *vdp = vd->private;
int size;
#endif
/*
* we allocate both chars and attributes in one chunk, attributes first
* because they have the (potentially) bigger alignment
*/
#ifdef WSDISPLAY_SCROLLSUPPORT
cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
scr->scr_current_line = 0;
scr->scr_line_wanted = 0;
scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
scr->scr_current_offset = scr->scr_offset_to_zero;
#else
cnt = ri->ri_rows * ri->ri_cols;
#endif
scr->scr_attrs = malloc(cnt * (sizeof(long) +
sizeof(uint32_t)), M_DEVBUF, M_WAITOK);
if (scr->scr_attrs == NULL)
return ENOMEM;
/*
* fill the attribute buffer with *defattr, chars with 0x20
* since we don't know if the driver tries to mimic firmware output or
* reset everything we do nothing to VRAM here, any driver that feels
* the need to clear screen or something will have to do it on its own
* Additional screens will start out in the background anyway so
* cleaning or not only really affects the initial console screen
*/
for (i = 0; i < cnt; i++) {
scr->scr_attrs[i] = scr->scr_defattr;
scr->scr_chars[i] = 0x20;
}
if (scr->scr_type == NULL)
scr->scr_type = vdp->defaulttype;
/*
* call the driver-supplied init_screen function which is expected
* to set up rasops_info, override cursor() and probably others
*/
vd->init_screen(vd->cookie, scr, existing, defattr);
/*
* save the non virtual console aware rasops and replace them with
* our wrappers
*/
vdp->eraserows = ri->ri_ops.eraserows;
vdp->erasecols = ri->ri_ops.erasecols;
scr->putchar = ri->ri_ops.putchar;
/* see if we're asked to add a font or use it */
if (scr == NULL)
return 0;
ri = &scr->scr_ri;
/* see if the driver knows how to handle multiple fonts */
if ((scr->scr_flags & VCONS_LOADFONT) == 0) {
return EOPNOTSUPP;
}
/* now see what fonts we can use */
if (ri->ri_flg & RI_ENABLE_ALPHA) {
flags |= WSFONT_FIND_ALPHA;
}
strncpy(buf, f->name, 63);
buf[63] = 0;
at = strchr(buf, '@');
if (at != NULL) {
int stat;
at[0] = 0;
at++;
DPRINTF("got '%s'\n", at);
h = strtoi(at, NULL, 10, 1, 99, &stat);
if (stat != 0) h = 0;
DPRINTF("looking for %d\n", h);
}
fcookie = wsfont_find(buf, 0, h, 0,
/* bitorder */
scr->scr_flags & VCONS_FONT_BITS_R2L ?
WSDISPLAY_FONTORDER_R2L : WSDISPLAY_FONTORDER_L2R,
/* byteorder */
scr->scr_flags & VCONS_FONT_BYTES_R2L ?
WSDISPLAY_FONTORDER_R2L : WSDISPLAY_FONTORDER_L2R,
flags);
if (fcookie == -1)
return ENOENT;
wsfont_lock(fcookie, &font);
if (font == NULL)
return EINVAL;
/* ok, we got a font. Now clear the screen with the old parameters */
if (SCREEN_IS_VISIBLE(scr))
vdp->eraserows(ri, 0, ri->ri_rows, scr->scr_defattr);
vcons_lock(vd->active);
#ifdef VCONS_DRAW_INTR
callout_halt(&vdp->intr, NULL);
#endif
/* set the new font and re-initialize things */
ri->ri_font = font;
wsfont_unlock(ri->ri_wsfcookie);
ri->ri_wsfcookie = fcookie;
/* and put our wrappers back */
ri->ri_ops.eraserows = vcons_eraserows;
ri->ri_ops.erasecols = vcons_erasecols;
ri->ri_ops.putchar = vcons_putchar;
ri->ri_ops.cursor = vcons_cursor;
ri->ri_ops.copycols = vcons_copycols;
ri->ri_ops.copyrows = vcons_copyrows;
vcons_unlock(vd->active);
/* notify things that we're about to redraw */
if (vd->show_screen_cb != NULL)
vd->show_screen_cb(scr, vd->show_screen_cookie);
#ifdef VCONS_DRAW_INTR
/*
* XXX
* Something(tm) craps all over VRAM somewhere up there if we're
* using VCONS_DRAW_INTR. Until I figure out what causes it, just
* redraw the screen for now.
*/
vcons_redraw_screen(vd->active);
callout_schedule(&vdp->intr, mstohz(33));
#endif
/* no need to draw anything, wsdisplay should reset the terminal */
scr = vdp->wanted;
if (!scr) {
printf("vcons_switch_screen: disappeared\n");
vdp->switch_cb(vdp->switch_cb_arg, EIO, 0);
return;
}
oldscr = vd->active; /* can be NULL! */
/*
* if there's an old, visible screen we mark it invisible and wait
* until it's not busy so we can safely switch
*/
if (oldscr != NULL) {
SCREEN_INVISIBLE(oldscr);
if (SCREEN_IS_BUSY(oldscr)) {
callout_schedule(&vdp->switch_callout, 1);
#ifdef DIAGNOSTIC
/* bitch if we wait too long */
vdp->switch_poll_count++;
if (vdp->switch_poll_count > 100) {
panic("vcons: screen still busy");
}
#endif
return;
}
/* invisible screen -> no visible cursor image */
oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
#ifdef DIAGNOSTIC
vdp->switch_poll_count = 0;
#endif
}
if (scr == oldscr)
return;
#ifdef DIAGNOSTIC
if (SCREEN_IS_VISIBLE(scr))
printf("vcons_switch_screen: already active");
#endif
mask = 0x00ff00ff; /* background and flags */
cmp = 0xffffffff; /* never match anything */
vcons_lock(scr);
if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
/*
* only clear the screen when RI_FULLCLEAR is set since we're
* going to overwrite every single character cell anyway
*/
if (ri->ri_flg & RI_FULLCLEAR) {
vdp->eraserows(ri, 0, ri->ri_rows,
scr->scr_defattr);
cmp = scr->scr_defattr & mask;
}
/* redraw the screen */
#ifdef WSDISPLAY_SCROLLSUPPORT
offset = scr->scr_current_offset;
#else
offset = 0;
#endif
for (i = 0; i < ri->ri_rows; i++) {
start = -1;
for (j = 0; j < ri->ri_cols; j++) {
/*
* no need to use the wrapper function - we
* don't change any characters or attributes
* and we already made sure the screen we're
* working on is visible
*/
c = charptr[offset];
a = attrptr[offset];
acmp = a & mask;
if (c == ' ') {
/*
* if we already erased the background
* and if this blank uses the same
* colour and flags we don't need to do
* anything here
*/
if (acmp == cmp && start == -1)
goto next;
/*
* see if we can optimize things a
* little bit by drawing stretches of
* blanks using erasecols
*/
if (start == -1) {
start = j;
last_a = acmp;
} else if (acmp != last_a) {
/*
* different attr, need to
* flush & restart
*/
vdp->erasecols(ri, i, start,
j - start, last_a);
start = j;
last_a = acmp;
}
} else {
if (start != -1) {
vdp->erasecols(ri, i, start,
j - start, last_a);
start = -1;
}
scr->putchar(ri, i, j, c, a);
}
next:
#ifdef VCONS_DRAW_INTR
vdp->chars[boffset] = charptr[offset];
vdp->attrs[boffset] = attrptr[offset];
#endif
offset++;
boffset++;
}
/* end of the line - draw all deferred blanks, if any */
if (start != -1) {
vdp->erasecols(ri, i, start, j - start, last_a);
}
}
ri->ri_flg &= ~RI_CURSOR;
scr->scr_vd->private->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
#ifdef VCONS_DRAW_INTR
vdp->cursor_offset = ri->ri_crow * ri->ri_cols + ri->ri_ccol;
#endif
}
vcons_unlock(scr);
}
vcons_lock(scr);
if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
/* redraw the screen */
#ifdef WSDISPLAY_SCROLLSUPPORT
offset = scr->scr_current_offset;
#else
offset = 0;
#endif
/*
* we mark the character cell occupied by the cursor as dirty
* so we don't have to deal with it
* notice that this isn't necessarily the position where rasops
* thinks it is, just where we drew it the last time
*/
if (vdp->cursor_offset >= 0)
vdp->attrs[vdp->cursor_offset] = 0xffffffff;
for (i = 0; i < ri->ri_rows; i++) {
for (j = 0; j < ri->ri_cols; j++) {
/*
* no need to use the wrapper function - we
* don't change any characters or attributes
* and we already made sure the screen we're
* working on is visible
*/
if ((vdp->chars[boffset] != charptr[offset]) ||
(vdp->attrs[boffset] != attrptr[offset])) {
scr->putchar(ri, i, j,
charptr[offset], attrptr[offset]);
vdp->chars[boffset] = charptr[offset];
vdp->attrs[boffset] = attrptr[offset];
}
offset++;
boffset++;
}
}
ri->ri_flg &= ~RI_CURSOR;
scr->scr_vd->private->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
vdp->cursor_offset = ri->ri_crow * ri->ri_cols + ri->ri_ccol;
}
vcons_unlock(scr);
#else /* !VCONS_DRAW_INTR */
vcons_redraw_screen(scr);
#endif
}
static int
vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
struct lwp *l)
{
struct vcons_data *vd = v;
struct vcons_data_private *vdp = vd->private;
int error = 0;
static void
vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
int from = srccol + row * ri->ri_cols;
int to = dstcol + row * ri->ri_cols;
int offset = vcons_offset_to_zero(scr);
static void
vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
int from, to, len;
int offset = vcons_offset_to_zero(scr);
/* do we need to scroll the back buffer? */
if (dstrow == 0 && offset != 0) {
from = ri->ri_cols * srcrow;
to = ri->ri_cols * dstrow;
memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
offset * sizeof(long));
memmove(&scr->scr_chars[to], &scr->scr_chars[from],
offset * sizeof(uint32_t));
}
from = ri->ri_cols * srcrow + offset;
to = ri->ri_cols * dstrow + offset;
len = ri->ri_cols * nrows;
memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
len * sizeof(long));
memmove(&scr->scr_chars[to], &scr->scr_chars[from],
len * sizeof(uint32_t));
vcons_dirty(scr);
}
static void
vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
static int
vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
{
struct rasops_info *ri = cookie;
struct vcons_screen *scr = ri->ri_hw;
int offset = vcons_offset_to_zero(scr);
int pos, ret = 0;
/*
* this is ugly. We need to break up an attribute into colours and
* flags but there's no rasops method to do that so we must rely on
* the 'canonical' encoding.
*/
if (scr->scr_line_wanted != scr->scr_current_line) {
vcons_do_scroll(scr);
}
}
static void
vcons_do_scroll(struct vcons_screen *scr)
{
int dist, from, to, num;
int r_offset, r_start;
int i, j;
if (scr->scr_line_wanted == scr->scr_current_line)
return;
dist = scr->scr_line_wanted - scr->scr_current_line;
scr->scr_current_line = scr->scr_line_wanted;
scr->scr_current_offset = scr->scr_ri.ri_cols *
(scr->scr_lines_in_buffer - scr->scr_current_line);
if (abs(dist) >= scr->scr_ri.ri_rows) {
vcons_redraw_screen(scr);
return;
}
/* scroll and redraw only what we really have to */
if (dist > 0) {
/* we scroll down */
from = 0;
to = dist;
num = scr->scr_ri.ri_rows - dist;
/* now the redraw parameters */
r_offset = scr->scr_current_offset;
r_start = 0;
} else {
/* scrolling up */
to = 0;
from = -dist;
num = scr->scr_ri.ri_rows + dist;
r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
r_start = num;
}
scr->scr_vd->private->copyrows(scr, from, to, num);
for (i = 0; i < abs(dist); i++) {
for (j = 0; j < scr->scr_ri.ri_cols; j++) {
#ifdef VCONS_DRAW_INTR
vcons_putchar_cached(scr, i + r_start, j,
scr->scr_chars[r_offset],
scr->scr_attrs[r_offset]);
#else
scr->putchar(scr, i + r_start, j,
scr->scr_chars[r_offset],
scr->scr_attrs[r_offset]);
#endif
r_offset++;
}
}
if (scr->scr_line_wanted == 0) {
/* this was a reset - need to draw the cursor */
scr->scr_ri.ri_flg &= ~RI_CURSOR;
scr->scr_vd->private->cursor(scr, 1, scr->scr_ri.ri_crow,
scr->scr_ri.ri_ccol);
}
}