Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_vcons.c revision 1.31
      1  1.31   mlelstv /*	$NetBSD: wsdisplay_vcons.c,v 1.31 2014/01/21 00:36:11 mlelstv Exp $ */
      2   1.1  macallan 
      3   1.1  macallan /*-
      4   1.1  macallan  * Copyright (c) 2005, 2006 Michael Lorenz
      5   1.1  macallan  * All rights reserved.
      6   1.1  macallan  *
      7   1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8   1.1  macallan  * modification, are permitted provided that the following conditions
      9   1.1  macallan  * are met:
     10   1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11   1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12   1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15   1.1  macallan  *
     16   1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  macallan  */
     28   1.1  macallan 
     29   1.1  macallan #include <sys/cdefs.h>
     30  1.31   mlelstv __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.31 2014/01/21 00:36:11 mlelstv Exp $");
     31   1.1  macallan 
     32   1.1  macallan #include <sys/param.h>
     33   1.1  macallan #include <sys/systm.h>
     34   1.1  macallan #include <sys/kernel.h>
     35   1.1  macallan #include <sys/buf.h>
     36   1.1  macallan #include <sys/device.h>
     37   1.1  macallan #include <sys/ioctl.h>
     38   1.1  macallan #include <sys/malloc.h>
     39   1.1  macallan #include <sys/mman.h>
     40   1.1  macallan #include <sys/tty.h>
     41   1.1  macallan #include <sys/conf.h>
     42   1.1  macallan #include <sys/proc.h>
     43   1.1  macallan #include <sys/kthread.h>
     44   1.1  macallan #include <sys/tprintf.h>
     45  1.19  macallan #include <sys/atomic.h>
     46   1.1  macallan 
     47   1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     48   1.1  macallan #include <dev/wscons/wsconsio.h>
     49   1.1  macallan #include <dev/wsfont/wsfont.h>
     50   1.1  macallan #include <dev/rasops/rasops.h>
     51   1.1  macallan 
     52   1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     53   1.1  macallan 
     54  1.14  macallan #include "opt_wsemul.h"
     55  1.14  macallan #include "opt_wsdisplay_compat.h"
     56  1.14  macallan #include "opt_vcons.h"
     57  1.14  macallan 
     58   1.1  macallan static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
     59   1.1  macallan 	    long *);
     60   1.1  macallan 
     61  1.10  christos static int  vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     62   1.1  macallan static int  vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
     63   1.1  macallan 	    int *, int *, long *);
     64   1.1  macallan static void vcons_free_screen(void *, void *);
     65   1.1  macallan static int  vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
     66   1.1  macallan 	    void *);
     67   1.1  macallan 
     68  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
     69  1.14  macallan static void vcons_scroll(void *, void *, int);
     70  1.14  macallan static void vcons_do_scroll(struct vcons_screen *);
     71  1.14  macallan #endif
     72  1.14  macallan 
     73  1.15     joerg static void vcons_do_switch(void *);
     74   1.1  macallan 
     75   1.1  macallan /* methods that work only on text buffers */
     76   1.1  macallan static void vcons_copycols_buffer(void *, int, int, int, int);
     77   1.1  macallan static void vcons_erasecols_buffer(void *, int, int, int, long);
     78   1.1  macallan static void vcons_copyrows_buffer(void *, int, int, int);
     79   1.1  macallan static void vcons_eraserows_buffer(void *, int, int, long);
     80   1.1  macallan static void vcons_putchar_buffer(void *, int, int, u_int, long);
     81   1.1  macallan 
     82   1.1  macallan /*
     83   1.1  macallan  * actual wrapper methods which call both the _buffer ones above and the
     84   1.1  macallan  * driver supplied ones to do the drawing
     85   1.1  macallan  */
     86   1.1  macallan static void vcons_copycols(void *, int, int, int, int);
     87   1.1  macallan static void vcons_erasecols(void *, int, int, int, long);
     88   1.1  macallan static void vcons_copyrows(void *, int, int, int);
     89   1.1  macallan static void vcons_eraserows(void *, int, int, long);
     90   1.1  macallan static void vcons_putchar(void *, int, int, u_int, long);
     91  1.25  macallan #ifdef VCONS_DRAW_INTR
     92  1.30   mlelstv static void vcons_erasecols_cached(void *, int, int, int, long);
     93  1.30   mlelstv static void vcons_eraserows_cached(void *, int, int, long);
     94  1.25  macallan static void vcons_putchar_cached(void *, int, int, u_int, long);
     95  1.25  macallan #endif
     96   1.1  macallan static void vcons_cursor(void *, int, int, int);
     97   1.1  macallan 
     98  1.17  macallan /*
     99  1.17  macallan  * methods that avoid framebuffer reads
    100  1.17  macallan  */
    101  1.17  macallan static void vcons_copycols_noread(void *, int, int, int, int);
    102  1.17  macallan static void vcons_copyrows_noread(void *, int, int, int);
    103  1.17  macallan 
    104  1.17  macallan 
    105  1.14  macallan /* support for reading/writing text buffers. For wsmoused */
    106   1.6      jmmv static int  vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
    107   1.6      jmmv static int  vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
    108   1.1  macallan 
    109   1.1  macallan static void vcons_lock(struct vcons_screen *);
    110   1.1  macallan static void vcons_unlock(struct vcons_screen *);
    111   1.1  macallan 
    112  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    113  1.20  jmcneill static void vcons_intr(void *);
    114  1.22  jmcneill static void vcons_softintr(void *);
    115  1.21  jmcneill static void vcons_intr_enable(device_t);
    116  1.20  jmcneill #endif
    117   1.1  macallan 
    118   1.1  macallan int
    119   1.1  macallan vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
    120   1.1  macallan     struct wsdisplay_accessops *ao)
    121   1.1  macallan {
    122   1.2  macallan 
    123   1.2  macallan 	/* zero out everything so we can rely on untouched fields being 0 */
    124   1.3  macallan 	memset(vd, 0, sizeof(struct vcons_data));
    125   1.2  macallan 
    126   1.1  macallan 	vd->cookie = cookie;
    127   1.1  macallan 
    128   1.1  macallan 	vd->init_screen = vcons_dummy_init_screen;
    129   1.1  macallan 	vd->show_screen_cb = NULL;
    130   1.1  macallan 
    131   1.6      jmmv 	/* keep a copy of the accessops that we replace below with our
    132   1.6      jmmv 	 * own wrappers */
    133   1.6      jmmv 	vd->ioctl = ao->ioctl;
    134   1.6      jmmv 
    135   1.6      jmmv 	/* configure the accessops */
    136   1.6      jmmv 	ao->ioctl = vcons_ioctl;
    137   1.1  macallan 	ao->alloc_screen = vcons_alloc_screen;
    138   1.1  macallan 	ao->free_screen = vcons_free_screen;
    139   1.1  macallan 	ao->show_screen = vcons_show_screen;
    140  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    141  1.14  macallan 	ao->scroll = vcons_scroll;
    142  1.14  macallan #endif
    143   1.1  macallan 
    144   1.1  macallan 	LIST_INIT(&vd->screens);
    145   1.1  macallan 	vd->active = NULL;
    146   1.1  macallan 	vd->wanted = NULL;
    147   1.1  macallan 	vd->currenttype = def;
    148  1.11        ad 	callout_init(&vd->switch_callout, 0);
    149  1.15     joerg 	callout_setfunc(&vd->switch_callout, vcons_do_switch, vd);
    150  1.25  macallan #ifdef VCONS_DRAW_INTR
    151  1.25  macallan 	vd->cells = 0;
    152  1.25  macallan 	vd->attrs = NULL;
    153  1.25  macallan 	vd->chars = NULL;
    154  1.25  macallan 	vd->cursor_offset = -1;
    155  1.25  macallan #endif
    156   1.1  macallan 
    157   1.1  macallan 	/*
    158   1.1  macallan 	 * a lock to serialize access to the framebuffer.
    159   1.1  macallan 	 * when switching screens we need to make sure there's no rasops
    160   1.1  macallan 	 * operation in progress
    161   1.1  macallan 	 */
    162   1.1  macallan #ifdef DIAGNOSTIC
    163   1.1  macallan 	vd->switch_poll_count = 0;
    164   1.1  macallan #endif
    165  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    166  1.22  jmcneill 	vd->intr_softint = softint_establish(SOFTINT_SERIAL,
    167  1.22  jmcneill 	    vcons_softintr, vd);
    168  1.20  jmcneill 	callout_init(&vd->intr, 0);
    169  1.20  jmcneill 	callout_setfunc(&vd->intr, vcons_intr, vd);
    170  1.23  jmcneill 	vd->intr_valid = 1;
    171  1.21  jmcneill 
    172  1.21  jmcneill 	/* XXX assume that the 'dev' arg is never dereferenced */
    173  1.21  jmcneill 	config_interrupts((device_t)vd, vcons_intr_enable);
    174  1.20  jmcneill #endif
    175   1.1  macallan 	return 0;
    176   1.1  macallan }
    177   1.1  macallan 
    178   1.1  macallan static void
    179   1.1  macallan vcons_lock(struct vcons_screen *scr)
    180   1.1  macallan {
    181   1.1  macallan #ifdef VCONS_PARANOIA
    182   1.1  macallan 	int s;
    183   1.1  macallan 
    184   1.1  macallan 	s = splhigh();
    185   1.1  macallan #endif
    186   1.1  macallan 	SCREEN_BUSY(scr);
    187   1.1  macallan #ifdef VCONS_PARANOIA
    188   1.1  macallan 	splx(s);
    189   1.1  macallan #endif
    190   1.1  macallan }
    191   1.1  macallan 
    192   1.1  macallan static void
    193   1.1  macallan vcons_unlock(struct vcons_screen *scr)
    194   1.1  macallan {
    195   1.1  macallan #ifdef VCONS_PARANOIA
    196   1.1  macallan 	int s;
    197   1.1  macallan 
    198   1.1  macallan 	s = splhigh();
    199   1.1  macallan #endif
    200   1.1  macallan 	SCREEN_IDLE(scr);
    201   1.1  macallan #ifdef VCONS_PARANOIA
    202   1.1  macallan 	splx(s);
    203   1.1  macallan #endif
    204   1.1  macallan }
    205   1.1  macallan 
    206   1.1  macallan static void
    207   1.9  christos vcons_dummy_init_screen(void *cookie,
    208   1.9  christos     struct vcons_screen *scr, int exists,
    209   1.9  christos     long *defattr)
    210   1.1  macallan {
    211   1.1  macallan 
    212   1.1  macallan 	/*
    213   1.1  macallan 	 * default init_screen() method.
    214   1.1  macallan 	 * Needs to be overwritten so we bitch and whine in case anyone ends
    215   1.1  macallan 	 * up in here.
    216   1.1  macallan 	 */
    217   1.1  macallan 	printf("vcons_init_screen: dummy function called. Your driver is "
    218   1.1  macallan 	       "supposed to supply a replacement for proper operation\n");
    219   1.1  macallan }
    220   1.1  macallan 
    221   1.1  macallan int
    222   1.1  macallan vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
    223   1.1  macallan     int existing, long *defattr)
    224   1.1  macallan {
    225   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    226   1.1  macallan 	int cnt, i;
    227  1.25  macallan #ifdef VCONS_DRAW_INTR
    228  1.25  macallan 	int size;
    229  1.25  macallan #endif
    230   1.1  macallan 
    231   1.1  macallan 	scr->scr_cookie = vd->cookie;
    232   1.4  jmcneill 	scr->scr_vd = scr->scr_origvd = vd;
    233  1.14  macallan 	scr->scr_busy = 0;
    234   1.2  macallan 
    235   1.1  macallan 	/*
    236   1.1  macallan 	 * call the driver-supplied init_screen function which is expected
    237   1.1  macallan 	 * to set up rasops_info, override cursor() and probably others
    238   1.1  macallan 	 */
    239   1.1  macallan 	vd->init_screen(vd->cookie, scr, existing, defattr);
    240   1.1  macallan 
    241   1.1  macallan 	/*
    242   1.1  macallan 	 * save the non virtual console aware rasops and replace them with
    243   1.1  macallan 	 * our wrappers
    244   1.1  macallan 	 */
    245   1.1  macallan 	vd->eraserows = ri->ri_ops.eraserows;
    246   1.1  macallan 	vd->erasecols = ri->ri_ops.erasecols;
    247   1.1  macallan 	vd->putchar   = ri->ri_ops.putchar;
    248   1.1  macallan 	vd->cursor    = ri->ri_ops.cursor;
    249   1.1  macallan 
    250  1.18  macallan 	if (scr->scr_flags & VCONS_NO_COPYCOLS) {
    251  1.19  macallan 		vd->copycols  = vcons_copycols_noread;
    252  1.18  macallan 	} else {
    253  1.19  macallan 		vd->copycols = ri->ri_ops.copycols;
    254  1.18  macallan 	}
    255  1.18  macallan 
    256  1.18  macallan 	if (scr->scr_flags & VCONS_NO_COPYROWS) {
    257  1.19  macallan 		vd->copyrows  = vcons_copyrows_noread;
    258  1.17  macallan 	} else {
    259  1.19  macallan 		vd->copyrows = ri->ri_ops.copyrows;
    260  1.17  macallan 	}
    261  1.17  macallan 
    262  1.19  macallan 	ri->ri_ops.eraserows = vcons_eraserows;
    263  1.19  macallan 	ri->ri_ops.erasecols = vcons_erasecols;
    264  1.19  macallan 	ri->ri_ops.putchar   = vcons_putchar;
    265  1.19  macallan 	ri->ri_ops.cursor    = vcons_cursor;
    266  1.19  macallan 	ri->ri_ops.copycols  = vcons_copycols;
    267  1.19  macallan 	ri->ri_ops.copyrows  = vcons_copyrows;
    268  1.19  macallan 
    269  1.19  macallan 
    270   1.1  macallan 	ri->ri_hw = scr;
    271   1.1  macallan 
    272   1.1  macallan 	/*
    273   1.1  macallan 	 * we allocate both chars and attributes in one chunk, attributes first
    274   1.1  macallan 	 * because they have the (potentially) bigger alignment
    275   1.1  macallan 	 */
    276  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    277  1.14  macallan 	cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
    278  1.14  macallan 	scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
    279  1.14  macallan 	scr->scr_current_line = 0;
    280  1.14  macallan 	scr->scr_line_wanted = 0;
    281  1.14  macallan 	scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
    282  1.14  macallan 	scr->scr_current_offset = scr->scr_offset_to_zero;
    283  1.14  macallan #else
    284   1.1  macallan 	cnt = ri->ri_rows * ri->ri_cols;
    285  1.14  macallan #endif
    286   1.1  macallan 	scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
    287  1.27  macallan 	    sizeof(uint32_t)), M_DEVBUF, M_WAITOK);
    288   1.1  macallan 	if (scr->scr_attrs == NULL)
    289   1.1  macallan 		return ENOMEM;
    290   1.1  macallan 
    291  1.27  macallan 	scr->scr_chars = (uint32_t *)&scr->scr_attrs[cnt];
    292   1.1  macallan 
    293   1.1  macallan 	ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    294   1.1  macallan 	scr->scr_defattr = *defattr;
    295   1.1  macallan 
    296   1.1  macallan 	/*
    297   1.1  macallan 	 * fill the attribute buffer with *defattr, chars with 0x20
    298   1.1  macallan 	 * since we don't know if the driver tries to mimic firmware output or
    299   1.1  macallan 	 * reset everything we do nothing to VRAM here, any driver that feels
    300   1.1  macallan 	 * the need to clear screen or something will have to do it on its own
    301   1.1  macallan 	 * Additional screens will start out in the background anyway so
    302   1.1  macallan 	 * cleaning or not only really affects the initial console screen
    303   1.1  macallan 	 */
    304   1.1  macallan 	for (i = 0; i < cnt; i++) {
    305   1.1  macallan 		scr->scr_attrs[i] = *defattr;
    306   1.1  macallan 		scr->scr_chars[i] = 0x20;
    307   1.1  macallan 	}
    308   1.1  macallan 
    309  1.25  macallan #ifdef VCONS_DRAW_INTR
    310  1.25  macallan 	size = ri->ri_cols * ri->ri_rows;
    311  1.25  macallan 	if (size > vd->cells) {
    312  1.25  macallan 		if (vd->chars != NULL) free(vd->chars, M_DEVBUF);
    313  1.25  macallan 		if (vd->attrs != NULL) free(vd->attrs, M_DEVBUF);
    314  1.25  macallan 		vd->cells = size;
    315  1.27  macallan 		vd->chars = malloc(size * sizeof(uint32_t), M_DEVBUF, M_WAITOK);
    316  1.25  macallan 		vd->attrs = malloc(size * sizeof(long), M_DEVBUF, M_WAITOK);
    317  1.25  macallan 		vcons_invalidate_cache(vd);
    318  1.25  macallan 	}
    319  1.25  macallan #endif
    320  1.25  macallan 
    321   1.2  macallan 	if(vd->active == NULL) {
    322   1.2  macallan 		vd->active = scr;
    323   1.2  macallan 		SCREEN_VISIBLE(scr);
    324   1.2  macallan 	}
    325   1.2  macallan 
    326   1.1  macallan 	if (existing) {
    327   1.2  macallan 		SCREEN_VISIBLE(scr);
    328   1.2  macallan 		vd->active = scr;
    329   1.1  macallan 	} else {
    330   1.2  macallan 		SCREEN_INVISIBLE(scr);
    331   1.1  macallan 	}
    332   1.1  macallan 
    333   1.1  macallan 	LIST_INSERT_HEAD(&vd->screens, scr, next);
    334   1.1  macallan 	return 0;
    335   1.1  macallan }
    336   1.1  macallan 
    337   1.1  macallan static void
    338  1.15     joerg vcons_do_switch(void *arg)
    339   1.1  macallan {
    340  1.15     joerg 	struct vcons_data *vd = arg;
    341   1.1  macallan 	struct vcons_screen *scr, *oldscr;
    342   1.1  macallan 
    343   1.1  macallan 	scr = vd->wanted;
    344   1.1  macallan 	if (!scr) {
    345   1.1  macallan 		printf("vcons_switch_screen: disappeared\n");
    346   1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, EIO, 0);
    347   1.1  macallan 		return;
    348   1.1  macallan 	}
    349   1.1  macallan 	oldscr = vd->active; /* can be NULL! */
    350   1.1  macallan 
    351   1.1  macallan 	/*
    352   1.1  macallan 	 * if there's an old, visible screen we mark it invisible and wait
    353   1.1  macallan 	 * until it's not busy so we can safely switch
    354   1.1  macallan 	 */
    355   1.1  macallan 	if (oldscr != NULL) {
    356   1.1  macallan 		SCREEN_INVISIBLE(oldscr);
    357   1.1  macallan 		if (SCREEN_IS_BUSY(oldscr)) {
    358  1.15     joerg 			callout_schedule(&vd->switch_callout, 1);
    359   1.1  macallan #ifdef DIAGNOSTIC
    360   1.1  macallan 			/* bitch if we wait too long */
    361   1.1  macallan 			vd->switch_poll_count++;
    362   1.1  macallan 			if (vd->switch_poll_count > 100) {
    363   1.1  macallan 				panic("vcons: screen still busy");
    364   1.1  macallan 			}
    365   1.1  macallan #endif
    366   1.1  macallan 			return;
    367   1.1  macallan 		}
    368   1.1  macallan 		/* invisible screen -> no visible cursor image */
    369   1.1  macallan 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
    370   1.1  macallan #ifdef DIAGNOSTIC
    371   1.1  macallan 		vd->switch_poll_count = 0;
    372   1.1  macallan #endif
    373   1.1  macallan 	}
    374   1.1  macallan 
    375   1.1  macallan 	if (scr == oldscr)
    376   1.1  macallan 		return;
    377   1.1  macallan 
    378   1.1  macallan #ifdef DIAGNOSTIC
    379   1.1  macallan 	if (SCREEN_IS_VISIBLE(scr))
    380  1.14  macallan 		printf("vcons_switch_screen: already active");
    381   1.1  macallan #endif
    382   1.1  macallan 
    383   1.1  macallan #ifdef notyet
    384   1.1  macallan 	if (vd->currenttype != type) {
    385   1.1  macallan 		vcons_set_screentype(vd, type);
    386   1.1  macallan 		vd->currenttype = type;
    387   1.1  macallan 	}
    388   1.1  macallan #endif
    389   1.1  macallan 
    390   1.1  macallan 	SCREEN_VISIBLE(scr);
    391   1.1  macallan 	vd->active = scr;
    392   1.1  macallan 	vd->wanted = NULL;
    393   1.1  macallan 
    394   1.1  macallan 	if (vd->show_screen_cb != NULL)
    395   1.1  macallan 		vd->show_screen_cb(scr);
    396   1.1  macallan 
    397   1.1  macallan 	if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
    398   1.1  macallan 		vcons_redraw_screen(scr);
    399   1.1  macallan 
    400   1.1  macallan 	if (vd->switch_cb)
    401   1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, 0, 0);
    402   1.1  macallan }
    403   1.1  macallan 
    404   1.1  macallan void
    405   1.1  macallan vcons_redraw_screen(struct vcons_screen *scr)
    406   1.1  macallan {
    407  1.28  macallan 	uint32_t *charptr = scr->scr_chars, c;
    408  1.28  macallan 	long *attrptr = scr->scr_attrs, a, last_a = 0, mask, cmp, acmp;
    409   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    410  1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    411  1.28  macallan 	int i, j, offset, boffset = 0, start = -1;
    412   1.1  macallan 
    413  1.28  macallan 	mask = 0x00ff00ff;	/* background and flags */
    414  1.28  macallan 	cmp = -1;		/* never match anything */
    415   1.1  macallan 	vcons_lock(scr);
    416   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    417   1.1  macallan 
    418   1.1  macallan 		/*
    419   1.1  macallan 		 * only clear the screen when RI_FULLCLEAR is set since we're
    420   1.1  macallan 		 * going to overwrite every single character cell anyway
    421   1.1  macallan 		 */
    422   1.1  macallan 		if (ri->ri_flg & RI_FULLCLEAR) {
    423  1.25  macallan 			vd->eraserows(ri, 0, ri->ri_rows,
    424   1.1  macallan 			    scr->scr_defattr);
    425  1.28  macallan 			cmp = scr->scr_defattr & mask;
    426   1.1  macallan 		}
    427   1.1  macallan 
    428   1.1  macallan 		/* redraw the screen */
    429  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    430  1.14  macallan 		offset = scr->scr_current_offset;
    431  1.14  macallan #else
    432   1.1  macallan 		offset = 0;
    433  1.14  macallan #endif
    434   1.1  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    435  1.28  macallan 			start = -1;
    436   1.1  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    437   1.1  macallan 				/*
    438   1.1  macallan 				 * no need to use the wrapper function - we
    439   1.1  macallan 				 * don't change any characters or attributes
    440   1.1  macallan 				 * and we already made sure the screen we're
    441   1.1  macallan 				 * working on is visible
    442   1.1  macallan 				 */
    443  1.28  macallan 				c = charptr[offset];
    444  1.28  macallan 				a = attrptr[offset];
    445  1.28  macallan 				acmp = a & mask;
    446  1.28  macallan 				if (c == ' ') {
    447  1.28  macallan 					/*
    448  1.28  macallan 					 * if we already erased the background
    449  1.28  macallan 					 * and this blank uses the same colour
    450  1.28  macallan 					 * and flags we don't need to do
    451  1.28  macallan 					 * anything here
    452  1.28  macallan 					 */
    453  1.28  macallan 					if (acmp == cmp)
    454  1.28  macallan 						goto next;
    455  1.28  macallan 					/*
    456  1.28  macallan 					 * see if we can optimize things a
    457  1.28  macallan 					 * little bit by drawing stretches of
    458  1.28  macallan 					 * blanks using erasecols
    459  1.28  macallan 					 */
    460  1.28  macallan 
    461  1.28  macallan 					if (start == -1) {
    462  1.28  macallan 						start = j;
    463  1.28  macallan 						last_a = acmp;
    464  1.28  macallan 					} else if (acmp != last_a) {
    465  1.28  macallan 						/*
    466  1.28  macallan 						 * different attr, need to
    467  1.28  macallan 						 * flush
    468  1.28  macallan 						 */
    469  1.28  macallan 						vd->erasecols(ri, i, start,
    470  1.28  macallan 						    j - start, last_a);
    471  1.28  macallan 						start = -1;
    472  1.28  macallan 					}
    473  1.28  macallan 				} else {
    474  1.28  macallan 					if (start != -1) {
    475  1.28  macallan 						vd->erasecols(ri, i, start,
    476  1.28  macallan 						    j - start, last_a);
    477  1.28  macallan 						start = -1;
    478  1.28  macallan 					}
    479  1.28  macallan 
    480  1.28  macallan 					vd->putchar(ri, i, j, c, a);
    481  1.28  macallan 				}
    482  1.28  macallan next:
    483  1.25  macallan #ifdef VCONS_DRAW_INTR
    484  1.25  macallan 				vd->chars[boffset] = charptr[offset];
    485  1.25  macallan 				vd->attrs[boffset] = attrptr[offset];
    486  1.25  macallan #endif
    487  1.25  macallan 				offset++;
    488  1.25  macallan 				boffset++;
    489  1.25  macallan 			}
    490  1.28  macallan 			/* end of the line - draw all defered blanks, if any */
    491  1.28  macallan 			if (start != -1) {
    492  1.28  macallan 				vd->erasecols(ri, i, start, j - start, last_a);
    493  1.28  macallan 			}
    494  1.25  macallan 		}
    495  1.25  macallan 		ri->ri_flg &= ~RI_CURSOR;
    496  1.25  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    497  1.25  macallan #ifdef VCONS_DRAW_INTR
    498  1.25  macallan 		vd->cursor_offset = ri->ri_crow * ri->ri_cols + ri->ri_ccol;
    499  1.25  macallan #endif
    500  1.25  macallan 	}
    501  1.25  macallan 	vcons_unlock(scr);
    502  1.25  macallan }
    503  1.25  macallan 
    504  1.25  macallan #ifdef VCONS_DRAW_INTR
    505  1.25  macallan void
    506  1.25  macallan vcons_update_screen(struct vcons_screen *scr)
    507  1.25  macallan {
    508  1.27  macallan 	uint32_t *charptr = scr->scr_chars;
    509  1.25  macallan 	long *attrptr = scr->scr_attrs;
    510  1.25  macallan 	struct rasops_info *ri = &scr->scr_ri;
    511  1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    512  1.25  macallan 	int i, j, offset, boffset = 0;
    513  1.25  macallan 
    514  1.25  macallan 	vcons_lock(scr);
    515  1.25  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    516  1.25  macallan 
    517  1.25  macallan 		/* redraw the screen */
    518  1.25  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    519  1.25  macallan 		offset = scr->scr_current_offset;
    520  1.25  macallan #else
    521  1.25  macallan 		offset = 0;
    522  1.25  macallan #endif
    523  1.25  macallan 		/*
    524  1.25  macallan 		 * we mark the character cell occupied by the cursor as dirty
    525  1.25  macallan 		 * so we don't have to deal with it
    526  1.25  macallan 		 * notice that this isn't necessarily the position where rasops
    527  1.25  macallan 		 * thinks it is, just where we drew it the last time
    528  1.25  macallan 		 */
    529  1.25  macallan 		if (vd->cursor_offset >= 0)
    530  1.25  macallan 			vd->attrs[vd->cursor_offset] = 0xffffffff;
    531  1.25  macallan 
    532  1.25  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    533  1.25  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    534  1.25  macallan 				/*
    535  1.25  macallan 				 * no need to use the wrapper function - we
    536  1.25  macallan 				 * don't change any characters or attributes
    537  1.25  macallan 				 * and we already made sure the screen we're
    538  1.25  macallan 				 * working on is visible
    539  1.25  macallan 				 */
    540  1.25  macallan 				if ((vd->chars[boffset] != charptr[offset]) ||
    541  1.25  macallan 				    (vd->attrs[boffset] != attrptr[offset])) {
    542  1.25  macallan 					vd->putchar(ri, i, j,
    543  1.25  macallan 				 	   charptr[offset], attrptr[offset]);
    544  1.25  macallan 					vd->chars[boffset] = charptr[offset];
    545  1.25  macallan 					vd->attrs[boffset] = attrptr[offset];
    546  1.25  macallan 				}
    547   1.1  macallan 				offset++;
    548  1.25  macallan 				boffset++;
    549   1.1  macallan 			}
    550   1.1  macallan 		}
    551   1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    552   1.1  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    553  1.25  macallan 		vd->cursor_offset = ri->ri_crow * ri->ri_cols + ri->ri_ccol;
    554   1.1  macallan 	}
    555   1.1  macallan 	vcons_unlock(scr);
    556   1.1  macallan }
    557  1.25  macallan #endif
    558   1.1  macallan 
    559   1.1  macallan static int
    560  1.10  christos vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    561   1.6      jmmv 	struct lwp *l)
    562   1.6      jmmv {
    563   1.6      jmmv 	struct vcons_data *vd = v;
    564  1.27  macallan 	int error = 0;
    565  1.27  macallan 
    566   1.6      jmmv 
    567   1.6      jmmv 	switch (cmd) {
    568   1.6      jmmv 	case WSDISPLAYIO_GETWSCHAR:
    569   1.6      jmmv 		error = vcons_getwschar((struct vcons_screen *)vs,
    570   1.6      jmmv 			(struct wsdisplay_char *)data);
    571   1.6      jmmv 		break;
    572   1.6      jmmv 
    573   1.6      jmmv 	case WSDISPLAYIO_PUTWSCHAR:
    574   1.6      jmmv 		error = vcons_putwschar((struct vcons_screen *)vs,
    575   1.6      jmmv 			(struct wsdisplay_char *)data);
    576   1.6      jmmv 		break;
    577   1.6      jmmv 
    578  1.27  macallan 	case WSDISPLAYIO_SET_POLLING: {
    579  1.27  macallan 		int poll = *(int *)data;
    580  1.27  macallan 
    581  1.27  macallan 		/* first call the driver's ioctl handler */
    582  1.27  macallan 		if (vd->ioctl != NULL)
    583  1.27  macallan 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    584  1.27  macallan 		if (poll) {
    585  1.27  macallan 			vcons_enable_polling(vd);
    586  1.27  macallan 			vcons_hard_switch(LIST_FIRST(&vd->screens));
    587  1.27  macallan 		} else
    588  1.27  macallan 			vcons_disable_polling(vd);
    589  1.27  macallan 		}
    590  1.27  macallan 		break;
    591  1.27  macallan 
    592   1.6      jmmv 	default:
    593   1.6      jmmv 		if (vd->ioctl != NULL)
    594   1.6      jmmv 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    595   1.6      jmmv 		else
    596   1.6      jmmv 			error = EPASSTHROUGH;
    597   1.6      jmmv 	}
    598   1.6      jmmv 
    599   1.6      jmmv 	return error;
    600   1.6      jmmv }
    601   1.6      jmmv 
    602   1.6      jmmv static int
    603   1.1  macallan vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    604   1.1  macallan     int *curxp, int *curyp, long *defattrp)
    605   1.1  macallan {
    606   1.1  macallan 	struct vcons_data *vd = v;
    607   1.1  macallan 	struct vcons_screen *scr;
    608   1.1  macallan 	int ret;
    609   1.1  macallan 
    610   1.1  macallan 	scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
    611   1.1  macallan 	if (scr == NULL)
    612   1.1  macallan 		return ENOMEM;
    613   1.1  macallan 
    614   1.1  macallan 	scr->scr_flags = 0;
    615   1.2  macallan 	scr->scr_status = 0;
    616   1.2  macallan 	scr->scr_busy = 0;
    617   1.1  macallan 	scr->scr_type = type;
    618   1.1  macallan 
    619   1.1  macallan 	ret = vcons_init_screen(vd, scr, 0, defattrp);
    620   1.1  macallan 	if (ret != 0) {
    621   1.1  macallan 		free(scr, M_DEVBUF);
    622   1.1  macallan 		return ret;
    623   1.1  macallan 	}
    624   1.1  macallan 
    625   1.1  macallan 	if (vd->active == NULL) {
    626   1.1  macallan 		SCREEN_VISIBLE(scr);
    627   1.1  macallan 		vd->active = scr;
    628   1.1  macallan 		vd->currenttype = type;
    629   1.1  macallan 	}
    630   1.1  macallan 
    631   1.1  macallan 	*cookiep = scr;
    632   1.1  macallan 	*curxp = scr->scr_ri.ri_ccol;
    633   1.1  macallan 	*curyp = scr->scr_ri.ri_crow;
    634   1.1  macallan 	return 0;
    635   1.1  macallan }
    636   1.1  macallan 
    637   1.1  macallan static void
    638   1.1  macallan vcons_free_screen(void *v, void *cookie)
    639   1.1  macallan {
    640   1.1  macallan 	struct vcons_data *vd = v;
    641   1.1  macallan 	struct vcons_screen *scr = cookie;
    642   1.1  macallan 
    643   1.1  macallan 	vcons_lock(scr);
    644   1.1  macallan 	/* there should be no rasops activity here */
    645   1.1  macallan 
    646   1.1  macallan 	LIST_REMOVE(scr, next);
    647   1.1  macallan 
    648   1.1  macallan 	if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
    649   1.1  macallan 		free(scr->scr_attrs, M_DEVBUF);
    650   1.1  macallan 		free(scr, M_DEVBUF);
    651   1.1  macallan 	} else {
    652   1.1  macallan 		/*
    653   1.1  macallan 		 * maybe we should just restore the old rasops_info methods
    654   1.1  macallan 		 * and free the character/attribute buffer here?
    655   1.1  macallan 		 */
    656   1.1  macallan #ifdef VCONS_DEBUG
    657   1.1  macallan 		panic("vcons_free_screen: console");
    658   1.1  macallan #else
    659   1.1  macallan 		printf("vcons_free_screen: console\n");
    660   1.1  macallan #endif
    661   1.1  macallan 	}
    662   1.1  macallan 
    663   1.1  macallan 	if (vd->active == scr)
    664   1.1  macallan 		vd->active = NULL;
    665   1.1  macallan }
    666   1.1  macallan 
    667   1.1  macallan static int
    668   1.9  christos vcons_show_screen(void *v, void *cookie, int waitok,
    669   1.1  macallan     void (*cb)(void *, int, int), void *cb_arg)
    670   1.1  macallan {
    671   1.1  macallan 	struct vcons_data *vd = v;
    672   1.1  macallan 	struct vcons_screen *scr;
    673   1.1  macallan 
    674   1.1  macallan 	scr = cookie;
    675   1.1  macallan 	if (scr == vd->active)
    676   1.1  macallan 		return 0;
    677   1.1  macallan 
    678   1.1  macallan 	vd->wanted = scr;
    679   1.1  macallan 	vd->switch_cb = cb;
    680   1.1  macallan 	vd->switch_cb_arg = cb_arg;
    681   1.1  macallan 	if (cb) {
    682  1.15     joerg 		callout_schedule(&vd->switch_callout, 0);
    683   1.1  macallan 		return EAGAIN;
    684   1.1  macallan 	}
    685   1.1  macallan 
    686   1.1  macallan 	vcons_do_switch(vd);
    687   1.1  macallan 	return 0;
    688   1.1  macallan }
    689   1.1  macallan 
    690   1.1  macallan /* wrappers for rasops_info methods */
    691   1.1  macallan 
    692   1.1  macallan static void
    693   1.1  macallan vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
    694   1.1  macallan {
    695   1.1  macallan 	struct rasops_info *ri = cookie;
    696   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    697   1.1  macallan 	int from = srccol + row * ri->ri_cols;
    698   1.1  macallan 	int to = dstcol + row * ri->ri_cols;
    699   1.1  macallan 
    700  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    701  1.14  macallan 	int offset;
    702  1.14  macallan 	offset = scr->scr_offset_to_zero;
    703  1.14  macallan 
    704  1.14  macallan 	memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from],
    705  1.14  macallan 	    ncols * sizeof(long));
    706  1.14  macallan 	memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from],
    707  1.27  macallan 	    ncols * sizeof(uint32_t));
    708  1.14  macallan #else
    709   1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    710   1.1  macallan 	    ncols * sizeof(long));
    711   1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    712  1.27  macallan 	    ncols * sizeof(uint32_t));
    713  1.14  macallan #endif
    714  1.20  jmcneill 
    715  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    716  1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
    717  1.20  jmcneill #endif
    718   1.1  macallan }
    719   1.1  macallan 
    720   1.1  macallan static void
    721   1.1  macallan vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    722   1.1  macallan {
    723   1.1  macallan 	struct rasops_info *ri = cookie;
    724   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    725   1.1  macallan 
    726   1.1  macallan 	vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
    727   1.1  macallan 
    728  1.21  jmcneill #if defined(VCONS_DRAW_INTR)
    729  1.21  jmcneill 	if (scr->scr_vd->use_intr)
    730  1.21  jmcneill 		return;
    731  1.21  jmcneill #endif
    732  1.21  jmcneill 
    733   1.1  macallan 	vcons_lock(scr);
    734   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    735  1.26  macallan 		scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
    736  1.30   mlelstv #if defined(VCONS_DRAW_INTR)
    737  1.30   mlelstv 		vcons_invalidate_cache(scr->scr_vd);
    738  1.30   mlelstv #endif
    739   1.1  macallan 	}
    740   1.1  macallan 	vcons_unlock(scr);
    741   1.1  macallan }
    742   1.1  macallan 
    743   1.1  macallan static void
    744  1.17  macallan vcons_copycols_noread(void *cookie, int row, int srccol, int dstcol, int ncols)
    745  1.17  macallan {
    746  1.17  macallan 	struct rasops_info *ri = cookie;
    747  1.17  macallan 	struct vcons_screen *scr = ri->ri_hw;
    748  1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    749  1.17  macallan 
    750  1.30   mlelstv 	vcons_lock(scr);
    751  1.17  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    752  1.25  macallan 		int pos, c, offset, ppos;
    753  1.17  macallan 
    754  1.17  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    755  1.17  macallan 		offset = scr->scr_current_offset;
    756  1.17  macallan #else
    757  1.17  macallan 		offset = 0;
    758  1.17  macallan #endif
    759  1.25  macallan 		ppos = ri->ri_cols * row + dstcol;
    760  1.25  macallan 		pos = ppos + offset;
    761  1.17  macallan 		for (c = dstcol; c < (dstcol + ncols); c++) {
    762  1.25  macallan #ifdef VCONS_DRAW_INTR
    763  1.25  macallan 			if ((scr->scr_chars[pos] != vd->chars[ppos]) ||
    764  1.25  macallan 			    (scr->scr_attrs[pos] != vd->attrs[ppos])) {
    765  1.25  macallan 				vd->putchar(cookie, row, c,
    766  1.25  macallan 				   scr->scr_chars[pos], scr->scr_attrs[pos]);
    767  1.25  macallan 				vd->chars[ppos] = scr->scr_chars[pos];
    768  1.25  macallan 				vd->attrs[ppos] = scr->scr_attrs[pos];
    769  1.25  macallan 			}
    770  1.25  macallan #else
    771  1.25  macallan 			vd->putchar(cookie, row, c, scr->scr_chars[pos],
    772  1.25  macallan 			    scr->scr_attrs[pos]);
    773  1.25  macallan #endif
    774  1.17  macallan 			pos++;
    775  1.25  macallan 			ppos++;
    776  1.17  macallan 		}
    777  1.17  macallan 	}
    778  1.30   mlelstv 	vcons_unlock(scr);
    779  1.17  macallan }
    780  1.17  macallan 
    781  1.17  macallan static void
    782   1.1  macallan vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
    783   1.1  macallan {
    784   1.1  macallan 	struct rasops_info *ri = cookie;
    785   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    786   1.1  macallan 	int start = startcol + row * ri->ri_cols;
    787   1.1  macallan 	int end = start + ncols, i;
    788   1.1  macallan 
    789  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    790  1.14  macallan 	int offset;
    791  1.14  macallan 	offset = scr->scr_offset_to_zero;
    792  1.14  macallan 
    793  1.14  macallan 	for (i = start; i < end; i++) {
    794  1.14  macallan 		scr->scr_attrs[offset + i] = fillattr;
    795  1.14  macallan 		scr->scr_chars[offset + i] = 0x20;
    796  1.14  macallan 	}
    797  1.14  macallan #else
    798   1.1  macallan 	for (i = start; i < end; i++) {
    799   1.1  macallan 		scr->scr_attrs[i] = fillattr;
    800   1.1  macallan 		scr->scr_chars[i] = 0x20;
    801   1.1  macallan 	}
    802  1.14  macallan #endif
    803  1.20  jmcneill 
    804  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    805  1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
    806  1.20  jmcneill #endif
    807   1.1  macallan }
    808   1.1  macallan 
    809  1.25  macallan #ifdef VCONS_DRAW_INTR
    810  1.25  macallan static void
    811  1.25  macallan vcons_erasecols_cached(void *cookie, int row, int startcol, int ncols, long fillattr)
    812  1.25  macallan {
    813  1.25  macallan 	struct rasops_info *ri = cookie;
    814  1.25  macallan 	struct vcons_screen *scr = ri->ri_hw;
    815  1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    816  1.25  macallan 	int i, pos = row * ri->ri_cols + startcol;
    817  1.25  macallan 
    818  1.25  macallan 	for (i = pos; i < ncols; i++) {
    819  1.25  macallan 		vd->chars[i] = 0x20;
    820  1.25  macallan 		vd->attrs[i] = fillattr;
    821  1.25  macallan 	}
    822  1.25  macallan 	vd->erasecols(cookie, row, startcol, ncols, fillattr);
    823  1.25  macallan }
    824  1.25  macallan #endif
    825  1.25  macallan 
    826   1.1  macallan static void
    827   1.1  macallan vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    828   1.1  macallan {
    829   1.1  macallan 	struct rasops_info *ri = cookie;
    830   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    831   1.1  macallan 
    832   1.1  macallan 	vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
    833   1.1  macallan 
    834  1.21  jmcneill #if defined(VCONS_DRAW_INTR)
    835  1.21  jmcneill 	if (scr->scr_vd->use_intr)
    836  1.21  jmcneill 		return;
    837  1.21  jmcneill #endif
    838  1.21  jmcneill 
    839   1.1  macallan 	vcons_lock(scr);
    840   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    841  1.25  macallan #ifdef VCONS_DRAW_INTR
    842  1.30   mlelstv 		vcons_erasecols_cached(cookie, row, startcol, ncols,
    843  1.30   mlelstv 		    fillattr);
    844  1.25  macallan #else
    845  1.25  macallan 		scr->scr_vd->erasecols(cookie, row, startcol, ncols, fillattr);
    846  1.25  macallan #endif
    847   1.1  macallan 	}
    848   1.1  macallan 	vcons_unlock(scr);
    849   1.1  macallan }
    850   1.1  macallan 
    851   1.1  macallan static void
    852   1.1  macallan vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
    853   1.1  macallan {
    854   1.1  macallan 	struct rasops_info *ri = cookie;
    855   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    856   1.1  macallan 	int from, to, len;
    857   1.1  macallan 
    858  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    859  1.14  macallan 	int offset;
    860  1.14  macallan 	offset = scr->scr_offset_to_zero;
    861  1.14  macallan 
    862  1.14  macallan 	/* do we need to scroll the back buffer? */
    863  1.14  macallan 	if (dstrow == 0) {
    864  1.14  macallan 		from = ri->ri_cols * srcrow;
    865  1.14  macallan 		to = ri->ri_cols * dstrow;
    866  1.14  macallan 
    867  1.14  macallan 		memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    868  1.14  macallan 		    scr->scr_offset_to_zero * sizeof(long));
    869  1.14  macallan 		memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    870  1.27  macallan 		    scr->scr_offset_to_zero * sizeof(uint32_t));
    871  1.14  macallan 	}
    872  1.14  macallan 	from = ri->ri_cols * srcrow + offset;
    873  1.14  macallan 	to = ri->ri_cols * dstrow + offset;
    874  1.14  macallan 	len = ri->ri_cols * nrows;
    875  1.14  macallan 
    876  1.14  macallan #else
    877   1.1  macallan 	from = ri->ri_cols * srcrow;
    878   1.1  macallan 	to = ri->ri_cols * dstrow;
    879   1.1  macallan 	len = ri->ri_cols * nrows;
    880  1.14  macallan #endif
    881   1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    882   1.1  macallan 	    len * sizeof(long));
    883   1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    884  1.27  macallan 	    len * sizeof(uint32_t));
    885  1.20  jmcneill 
    886  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    887  1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
    888  1.20  jmcneill #endif
    889   1.1  macallan }
    890   1.1  macallan 
    891   1.1  macallan static void
    892   1.1  macallan vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    893   1.1  macallan {
    894   1.1  macallan 	struct rasops_info *ri = cookie;
    895   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    896   1.1  macallan 
    897   1.1  macallan 	vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
    898   1.1  macallan 
    899  1.21  jmcneill #if defined(VCONS_DRAW_INTR)
    900  1.21  jmcneill 	if (scr->scr_vd->use_intr)
    901  1.21  jmcneill 		return;
    902  1.21  jmcneill #endif
    903  1.21  jmcneill 
    904   1.1  macallan 	vcons_lock(scr);
    905   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    906  1.26  macallan 		scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
    907  1.30   mlelstv #if defined(VCONS_DRAW_INTR)
    908  1.30   mlelstv 		vcons_invalidate_cache(scr->scr_vd);
    909  1.30   mlelstv #endif
    910   1.1  macallan 	}
    911   1.1  macallan 	vcons_unlock(scr);
    912   1.1  macallan }
    913   1.1  macallan 
    914   1.1  macallan static void
    915  1.17  macallan vcons_copyrows_noread(void *cookie, int srcrow, int dstrow, int nrows)
    916  1.17  macallan {
    917  1.17  macallan 	struct rasops_info *ri = cookie;
    918  1.17  macallan 	struct vcons_screen *scr = ri->ri_hw;
    919  1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    920  1.17  macallan 
    921  1.30   mlelstv 	vcons_lock(scr);
    922  1.17  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    923  1.25  macallan 		int pos, l, c, offset, ppos;
    924  1.17  macallan 
    925  1.17  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    926  1.17  macallan 		offset = scr->scr_current_offset;
    927  1.17  macallan #else
    928  1.17  macallan 		offset = 0;
    929  1.17  macallan #endif
    930  1.25  macallan 		ppos = ri->ri_cols * dstrow;
    931  1.25  macallan 		pos = ppos + offset;
    932  1.17  macallan 		for (l = dstrow; l < (dstrow + nrows); l++) {
    933  1.17  macallan 			for (c = 0; c < ri->ri_cols; c++) {
    934  1.25  macallan #ifdef VCONS_DRAW_INTR
    935  1.25  macallan 				if ((scr->scr_chars[pos] != vd->chars[ppos]) ||
    936  1.25  macallan 				    (scr->scr_attrs[pos] != vd->attrs[ppos])) {
    937  1.25  macallan 					vd->putchar(cookie, l, c,
    938  1.25  macallan 					   scr->scr_chars[pos], scr->scr_attrs[pos]);
    939  1.25  macallan 					vd->chars[ppos] = scr->scr_chars[pos];
    940  1.25  macallan 					vd->attrs[ppos] = scr->scr_attrs[pos];
    941  1.25  macallan 				}
    942  1.25  macallan #else
    943  1.31   mlelstv 				vd->putchar(cookie, l, c, scr->scr_chars[pos],
    944  1.25  macallan 				    scr->scr_attrs[pos]);
    945  1.25  macallan #endif
    946  1.17  macallan 				pos++;
    947  1.25  macallan 				ppos++;
    948  1.17  macallan 			}
    949  1.17  macallan 		}
    950  1.17  macallan 	}
    951  1.30   mlelstv 	vcons_unlock(scr);
    952  1.17  macallan }
    953  1.17  macallan 
    954  1.17  macallan static void
    955   1.1  macallan vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
    956   1.1  macallan {
    957   1.1  macallan 	struct rasops_info *ri = cookie;
    958   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    959   1.1  macallan 	int start, end, i;
    960   1.1  macallan 
    961  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    962  1.14  macallan 	int offset;
    963  1.14  macallan 	offset = scr->scr_offset_to_zero;
    964  1.14  macallan 
    965  1.14  macallan 	start = ri->ri_cols * row + offset;
    966  1.14  macallan 	end = ri->ri_cols * (row + nrows) + offset;
    967  1.14  macallan #else
    968   1.1  macallan 	start = ri->ri_cols * row;
    969   1.1  macallan 	end = ri->ri_cols * (row + nrows);
    970  1.14  macallan #endif
    971   1.1  macallan 
    972   1.1  macallan 	for (i = start; i < end; i++) {
    973   1.1  macallan 		scr->scr_attrs[i] = fillattr;
    974   1.1  macallan 		scr->scr_chars[i] = 0x20;
    975   1.1  macallan 	}
    976  1.20  jmcneill 
    977  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    978  1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
    979  1.20  jmcneill #endif
    980   1.1  macallan }
    981   1.1  macallan 
    982  1.30   mlelstv #ifdef VCONS_DRAW_INTR
    983  1.30   mlelstv static void
    984  1.30   mlelstv vcons_eraserows_cached(void *cookie, int row, int nrows, long fillattr)
    985  1.30   mlelstv {
    986  1.30   mlelstv 	struct rasops_info *ri = cookie;
    987  1.30   mlelstv 	struct vcons_screen *scr = ri->ri_hw;
    988  1.30   mlelstv 	struct vcons_data *vd = scr->scr_vd;
    989  1.30   mlelstv 	int i, pos = row * ri->ri_cols, end = (row+nrows) * ri->ri_cols;
    990  1.30   mlelstv 
    991  1.30   mlelstv 	for (i = pos; i < end; i++) {
    992  1.30   mlelstv 		vd->chars[i] = 0x20;
    993  1.30   mlelstv 		vd->attrs[i] = fillattr;
    994  1.30   mlelstv 	}
    995  1.30   mlelstv 	vd->eraserows(cookie, row, nrows, fillattr);
    996  1.30   mlelstv }
    997  1.30   mlelstv #endif
    998  1.30   mlelstv 
    999   1.1  macallan static void
   1000   1.1  macallan vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
   1001   1.1  macallan {
   1002   1.1  macallan 	struct rasops_info *ri = cookie;
   1003   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1004   1.1  macallan 
   1005   1.1  macallan 	vcons_eraserows_buffer(cookie, row, nrows, fillattr);
   1006   1.1  macallan 
   1007  1.21  jmcneill #if defined(VCONS_DRAW_INTR)
   1008  1.21  jmcneill 	if (scr->scr_vd->use_intr)
   1009  1.21  jmcneill 		return;
   1010  1.21  jmcneill #endif
   1011  1.21  jmcneill 
   1012   1.1  macallan 	vcons_lock(scr);
   1013   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1014  1.30   mlelstv #ifdef VCONS_DRAW_INTR
   1015  1.30   mlelstv 		vcons_eraserows_cached(cookie, row, nrows, fillattr);
   1016  1.30   mlelstv #else
   1017  1.25  macallan 		scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
   1018  1.30   mlelstv #endif
   1019   1.1  macallan 	}
   1020   1.1  macallan 	vcons_unlock(scr);
   1021   1.1  macallan }
   1022   1.1  macallan 
   1023   1.1  macallan static void
   1024   1.1  macallan vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
   1025   1.1  macallan {
   1026   1.1  macallan 	struct rasops_info *ri = cookie;
   1027   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1028   1.1  macallan 	int pos;
   1029   1.1  macallan 
   1030  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1031  1.14  macallan 	int offset;
   1032  1.14  macallan 	offset = scr->scr_offset_to_zero;
   1033  1.14  macallan 
   1034  1.14  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
   1035  1.14  macallan 	     (col < ri->ri_cols)) {
   1036  1.14  macallan 		pos = col + row * ri->ri_cols;
   1037  1.14  macallan 		scr->scr_attrs[pos + offset] = attr;
   1038  1.14  macallan 		scr->scr_chars[pos + offset] = c;
   1039  1.14  macallan 	}
   1040  1.14  macallan #else
   1041   1.1  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
   1042   1.1  macallan 	     (col < ri->ri_cols)) {
   1043   1.1  macallan 		pos = col + row * ri->ri_cols;
   1044   1.1  macallan 		scr->scr_attrs[pos] = attr;
   1045   1.1  macallan 		scr->scr_chars[pos] = c;
   1046   1.1  macallan 	}
   1047  1.14  macallan #endif
   1048  1.20  jmcneill 
   1049  1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1050  1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
   1051  1.20  jmcneill #endif
   1052   1.1  macallan }
   1053   1.1  macallan 
   1054  1.25  macallan #ifdef VCONS_DRAW_INTR
   1055  1.25  macallan static void
   1056  1.25  macallan vcons_putchar_cached(void *cookie, int row, int col, u_int c, long attr)
   1057  1.25  macallan {
   1058  1.25  macallan 	struct rasops_info *ri = cookie;
   1059  1.25  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1060  1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
   1061  1.25  macallan 	int pos = row * ri->ri_cols + col;
   1062  1.25  macallan 
   1063  1.25  macallan 	if ((vd->chars == NULL) || (vd->attrs == NULL)) {
   1064  1.25  macallan 		vd->putchar(cookie, row, col, c, attr);
   1065  1.25  macallan 		return;
   1066  1.25  macallan 	}
   1067  1.25  macallan 	if ((vd->chars[pos] != c) || (vd->attrs[pos] != attr)) {
   1068  1.25  macallan 		vd->attrs[pos] = attr;
   1069  1.25  macallan 		vd->chars[pos] = c;
   1070  1.25  macallan 		vd->putchar(cookie, row, col, c, attr);
   1071  1.25  macallan 	}
   1072  1.25  macallan }
   1073  1.25  macallan #endif
   1074  1.25  macallan 
   1075   1.1  macallan static void
   1076   1.1  macallan vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
   1077   1.1  macallan {
   1078   1.1  macallan 	struct rasops_info *ri = cookie;
   1079   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1080   1.1  macallan 
   1081   1.1  macallan 	vcons_putchar_buffer(cookie, row, col, c, attr);
   1082  1.21  jmcneill 
   1083  1.21  jmcneill #if defined(VCONS_DRAW_INTR)
   1084  1.21  jmcneill 	if (scr->scr_vd->use_intr)
   1085  1.21  jmcneill 		return;
   1086  1.21  jmcneill #endif
   1087  1.21  jmcneill 
   1088   1.1  macallan 	vcons_lock(scr);
   1089   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1090  1.25  macallan #ifdef VCONS_DRAW_INTR
   1091  1.25  macallan 		vcons_putchar_cached(cookie, row, col, c, attr);
   1092  1.25  macallan #else
   1093  1.25  macallan 		scr->scr_vd->putchar(cookie, row, col, c, attr);
   1094  1.25  macallan #endif
   1095   1.1  macallan 	}
   1096   1.1  macallan 	vcons_unlock(scr);
   1097   1.1  macallan }
   1098   1.1  macallan 
   1099   1.1  macallan static void
   1100   1.1  macallan vcons_cursor(void *cookie, int on, int row, int col)
   1101   1.1  macallan {
   1102   1.1  macallan 	struct rasops_info *ri = cookie;
   1103   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1104   1.1  macallan 
   1105  1.20  jmcneill 
   1106  1.20  jmcneill #if defined(VCONS_DRAW_INTR)
   1107  1.21  jmcneill 	if (scr->scr_vd->use_intr) {
   1108  1.21  jmcneill 		vcons_lock(scr);
   1109  1.21  jmcneill 		if (scr->scr_ri.ri_crow != row || scr->scr_ri.ri_ccol != col) {
   1110  1.21  jmcneill 			scr->scr_ri.ri_crow = row;
   1111  1.21  jmcneill 			scr->scr_ri.ri_ccol = col;
   1112  1.22  jmcneill 			atomic_inc_uint(&scr->scr_dirty);
   1113  1.21  jmcneill 		}
   1114  1.21  jmcneill 		vcons_unlock(scr);
   1115  1.21  jmcneill 		return;
   1116  1.20  jmcneill 	}
   1117  1.21  jmcneill #endif
   1118  1.21  jmcneill 
   1119  1.21  jmcneill 	vcons_lock(scr);
   1120  1.21  jmcneill 
   1121   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1122  1.26  macallan 		scr->scr_vd->cursor(cookie, on, row, col);
   1123   1.1  macallan 	} else {
   1124   1.1  macallan 		scr->scr_ri.ri_crow = row;
   1125   1.1  macallan 		scr->scr_ri.ri_ccol = col;
   1126   1.1  macallan 	}
   1127   1.1  macallan 	vcons_unlock(scr);
   1128   1.1  macallan }
   1129   1.1  macallan 
   1130   1.1  macallan /* methods to read/write characters via ioctl() */
   1131   1.1  macallan 
   1132   1.1  macallan static int
   1133   1.6      jmmv vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
   1134   1.1  macallan {
   1135   1.1  macallan 	long attr;
   1136   1.6      jmmv 	struct rasops_info *ri;
   1137   1.6      jmmv 
   1138   1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
   1139   1.6      jmmv 
   1140  1.13    rumble 	ri = &scr->scr_ri;
   1141  1.13    rumble 
   1142  1.12       mjf 	if (__predict_false((unsigned int)wsc->col > ri->ri_cols ||
   1143  1.12       mjf 	    (unsigned int)wsc->row > ri->ri_rows))
   1144  1.12       mjf 			return (EINVAL);
   1145   1.1  macallan 
   1146  1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
   1147  1.14  macallan 	     (wsc->col < ri->ri_cols)) {
   1148  1.14  macallan 
   1149  1.14  macallan 		ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
   1150  1.14  macallan 		    wsc->flags, &attr);
   1151  1.14  macallan 		vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
   1152  1.14  macallan #ifdef VCONS_DEBUG
   1153  1.14  macallan 		printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col,
   1154  1.14  macallan 		    wsc->letter, attr);
   1155  1.14  macallan #endif
   1156  1.14  macallan 		return 0;
   1157  1.14  macallan 	} else
   1158  1.14  macallan 		return EINVAL;
   1159   1.1  macallan }
   1160   1.1  macallan 
   1161   1.1  macallan static int
   1162   1.6      jmmv vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
   1163   1.1  macallan {
   1164   1.6      jmmv 	int offset;
   1165   1.1  macallan 	long attr;
   1166   1.6      jmmv 	struct rasops_info *ri;
   1167   1.6      jmmv 
   1168   1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
   1169   1.6      jmmv 
   1170   1.6      jmmv 	ri = &scr->scr_ri;
   1171   1.1  macallan 
   1172  1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
   1173  1.14  macallan 	     (wsc->col < ri->ri_cols)) {
   1174  1.14  macallan 
   1175  1.14  macallan 		offset = ri->ri_cols * wsc->row + wsc->col;
   1176  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1177  1.14  macallan 		offset += scr->scr_offset_to_zero;
   1178  1.14  macallan #endif
   1179  1.14  macallan 		wsc->letter = scr->scr_chars[offset];
   1180  1.14  macallan 		attr = scr->scr_attrs[offset];
   1181  1.14  macallan 
   1182  1.14  macallan 		/*
   1183  1.14  macallan 		 * this is ugly. We need to break up an attribute into colours and
   1184  1.14  macallan 		 * flags but there's no rasops method to do that so we must rely on
   1185  1.14  macallan 		 * the 'canonical' encoding.
   1186  1.14  macallan 		 */
   1187  1.14  macallan #ifdef VCONS_DEBUG
   1188  1.14  macallan 		printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row,
   1189  1.14  macallan 		    wsc->col, wsc->letter, attr);
   1190  1.14  macallan #endif
   1191  1.14  macallan 		wsc->foreground = (attr >> 24) & 0xff;
   1192  1.14  macallan 		wsc->background = (attr >> 16) & 0xff;
   1193  1.14  macallan 		wsc->flags      = attr & 0xff;
   1194  1.14  macallan 		return 0;
   1195  1.14  macallan 	} else
   1196  1.14  macallan 		return EINVAL;
   1197  1.14  macallan }
   1198  1.14  macallan 
   1199  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1200  1.14  macallan 
   1201  1.14  macallan static void
   1202  1.14  macallan vcons_scroll(void *cookie, void *vs, int where)
   1203  1.14  macallan {
   1204  1.14  macallan 	struct vcons_screen *scr = vs;
   1205  1.14  macallan 
   1206  1.14  macallan 	if (where == 0) {
   1207  1.14  macallan 		scr->scr_line_wanted = 0;
   1208  1.14  macallan 	} else {
   1209  1.14  macallan 		scr->scr_line_wanted = scr->scr_line_wanted - where;
   1210  1.14  macallan 		if (scr->scr_line_wanted < 0)
   1211  1.14  macallan 			scr->scr_line_wanted = 0;
   1212  1.14  macallan 		if (scr->scr_line_wanted > scr->scr_lines_in_buffer)
   1213  1.14  macallan 			scr->scr_line_wanted = scr->scr_lines_in_buffer;
   1214  1.14  macallan 	}
   1215  1.14  macallan 
   1216  1.14  macallan 	if (scr->scr_line_wanted != scr->scr_current_line) {
   1217  1.14  macallan 
   1218  1.14  macallan 		vcons_do_scroll(scr);
   1219  1.14  macallan 	}
   1220  1.14  macallan }
   1221  1.14  macallan 
   1222  1.14  macallan static void
   1223  1.14  macallan vcons_do_scroll(struct vcons_screen *scr)
   1224  1.14  macallan {
   1225  1.14  macallan 	int dist, from, to, num;
   1226  1.14  macallan 	int r_offset, r_start;
   1227  1.14  macallan 	int i, j;
   1228  1.14  macallan 
   1229  1.14  macallan 	if (scr->scr_line_wanted == scr->scr_current_line)
   1230  1.14  macallan 		return;
   1231  1.14  macallan 	dist = scr->scr_line_wanted - scr->scr_current_line;
   1232  1.14  macallan 	scr->scr_current_line = scr->scr_line_wanted;
   1233  1.14  macallan 	scr->scr_current_offset = scr->scr_ri.ri_cols *
   1234  1.14  macallan 	    (scr->scr_lines_in_buffer - scr->scr_current_line);
   1235  1.14  macallan 	if (abs(dist) >= scr->scr_ri.ri_rows) {
   1236  1.14  macallan 		vcons_redraw_screen(scr);
   1237  1.14  macallan 		return;
   1238  1.14  macallan 	}
   1239  1.14  macallan 	/* scroll and redraw only what we really have to */
   1240  1.14  macallan 	if (dist > 0) {
   1241  1.14  macallan 		/* we scroll down */
   1242  1.14  macallan 		from = 0;
   1243  1.14  macallan 		to = dist;
   1244  1.14  macallan 		num = scr->scr_ri.ri_rows - dist;
   1245  1.14  macallan 		/* now the redraw parameters */
   1246  1.14  macallan 		r_offset = scr->scr_current_offset;
   1247  1.14  macallan 		r_start = 0;
   1248  1.14  macallan 	} else {
   1249  1.14  macallan 		/* scrolling up */
   1250  1.14  macallan 		to = 0;
   1251  1.14  macallan 		from = -dist;
   1252  1.14  macallan 		num = scr->scr_ri.ri_rows + dist;
   1253  1.14  macallan 		r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
   1254  1.14  macallan 		r_start = num;
   1255  1.14  macallan 	}
   1256  1.14  macallan 	scr->scr_vd->copyrows(scr, from, to, num);
   1257  1.14  macallan 	for (i = 0; i < abs(dist); i++) {
   1258  1.14  macallan 		for (j = 0; j < scr->scr_ri.ri_cols; j++) {
   1259  1.25  macallan #ifdef VCONS_DRAW_INTR
   1260  1.25  macallan 			vcons_putchar_cached(scr, i + r_start, j,
   1261  1.25  macallan 			    scr->scr_chars[r_offset],
   1262  1.25  macallan 			    scr->scr_attrs[r_offset]);
   1263  1.25  macallan #else
   1264  1.14  macallan 			scr->scr_vd->putchar(scr, i + r_start, j,
   1265  1.14  macallan 			    scr->scr_chars[r_offset],
   1266  1.14  macallan 			    scr->scr_attrs[r_offset]);
   1267  1.25  macallan #endif
   1268  1.14  macallan 			r_offset++;
   1269  1.14  macallan 		}
   1270  1.14  macallan 	}
   1271  1.14  macallan 
   1272  1.14  macallan 	if (scr->scr_line_wanted == 0) {
   1273  1.14  macallan 		/* this was a reset - need to draw the cursor */
   1274  1.14  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1275  1.14  macallan 		scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow,
   1276  1.14  macallan 		    scr->scr_ri.ri_ccol);
   1277  1.14  macallan 	}
   1278  1.14  macallan }
   1279  1.14  macallan 
   1280  1.14  macallan #endif /* WSDISPLAY_SCROLLSUPPORT */
   1281  1.14  macallan 
   1282  1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1283  1.20  jmcneill static void
   1284  1.20  jmcneill vcons_intr(void *cookie)
   1285  1.20  jmcneill {
   1286  1.20  jmcneill 	struct vcons_data *vd = cookie;
   1287  1.20  jmcneill 
   1288  1.22  jmcneill 	softint_schedule(vd->intr_softint);
   1289  1.20  jmcneill }
   1290  1.20  jmcneill 
   1291  1.20  jmcneill static void
   1292  1.22  jmcneill vcons_softintr(void *cookie)
   1293  1.20  jmcneill {
   1294  1.20  jmcneill 	struct vcons_data *vd = cookie;
   1295  1.20  jmcneill 	struct vcons_screen *scr = vd->active;
   1296  1.22  jmcneill 	unsigned int dirty;
   1297  1.20  jmcneill 
   1298  1.30   mlelstv 	if (scr && vd->use_intr) {
   1299  1.22  jmcneill 		if (!SCREEN_IS_BUSY(scr)) {
   1300  1.22  jmcneill 			dirty = atomic_swap_uint(&scr->scr_dirty, 0);
   1301  1.30   mlelstv 			if (vd->use_intr == 2) {
   1302  1.30   mlelstv 				if ((scr->scr_flags & VCONS_NO_REDRAW) == 0) {
   1303  1.30   mlelstv 					vd->use_intr = 1;
   1304  1.30   mlelstv 					vcons_redraw_screen(scr);
   1305  1.30   mlelstv 				}
   1306  1.30   mlelstv 			} else if (dirty > 0) {
   1307  1.22  jmcneill 				if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
   1308  1.25  macallan 					vcons_update_screen(scr);
   1309  1.22  jmcneill 			}
   1310  1.20  jmcneill 		}
   1311  1.20  jmcneill 	}
   1312  1.20  jmcneill 
   1313  1.20  jmcneill 	callout_schedule(&vd->intr, mstohz(33));
   1314  1.20  jmcneill }
   1315  1.21  jmcneill 
   1316  1.21  jmcneill static void
   1317  1.21  jmcneill vcons_intr_enable(device_t dev)
   1318  1.21  jmcneill {
   1319  1.21  jmcneill 	/* the 'dev' arg we pass to config_interrupts isn't a device_t */
   1320  1.21  jmcneill 	struct vcons_data *vd = (struct vcons_data *)dev;
   1321  1.30   mlelstv 	vd->use_intr = 2;
   1322  1.21  jmcneill 	callout_schedule(&vd->intr, mstohz(33));
   1323  1.21  jmcneill }
   1324  1.20  jmcneill #endif /* VCONS_DRAW_INTR */
   1325  1.23  jmcneill 
   1326  1.23  jmcneill void
   1327  1.23  jmcneill vcons_enable_polling(struct vcons_data *vd)
   1328  1.23  jmcneill {
   1329  1.23  jmcneill 	struct vcons_screen *scr = vd->active;
   1330  1.23  jmcneill 
   1331  1.23  jmcneill #ifdef VCONS_DRAW_INTR
   1332  1.23  jmcneill 	vd->use_intr = 0;
   1333  1.23  jmcneill #endif
   1334  1.23  jmcneill 
   1335  1.23  jmcneill 	if (scr && !SCREEN_IS_BUSY(scr)) {
   1336  1.23  jmcneill 		if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
   1337  1.23  jmcneill 			vcons_redraw_screen(scr);
   1338  1.23  jmcneill 	}
   1339  1.23  jmcneill }
   1340  1.23  jmcneill 
   1341  1.23  jmcneill void
   1342  1.23  jmcneill vcons_disable_polling(struct vcons_data *vd)
   1343  1.23  jmcneill {
   1344  1.23  jmcneill #ifdef VCONS_DRAW_INTR
   1345  1.23  jmcneill 	struct vcons_screen *scr = vd->active;
   1346  1.23  jmcneill 
   1347  1.23  jmcneill 	if (!vd->intr_valid)
   1348  1.23  jmcneill 		return;
   1349  1.23  jmcneill 
   1350  1.30   mlelstv 	vd->use_intr = 2;
   1351  1.23  jmcneill 	if (scr)
   1352  1.23  jmcneill 		atomic_inc_uint(&scr->scr_dirty);
   1353  1.23  jmcneill #endif
   1354  1.23  jmcneill }
   1355  1.24  jmcneill 
   1356  1.24  jmcneill void
   1357  1.24  jmcneill vcons_hard_switch(struct vcons_screen *scr)
   1358  1.24  jmcneill {
   1359  1.24  jmcneill 	struct vcons_data *vd = scr->scr_vd;
   1360  1.24  jmcneill 	struct vcons_screen *oldscr = vd->active;
   1361  1.24  jmcneill 
   1362  1.24  jmcneill 	if (oldscr) {
   1363  1.24  jmcneill 		SCREEN_INVISIBLE(oldscr);
   1364  1.24  jmcneill 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
   1365  1.24  jmcneill 	}
   1366  1.24  jmcneill 	SCREEN_VISIBLE(scr);
   1367  1.24  jmcneill 	vd->active = scr;
   1368  1.24  jmcneill 	vd->wanted = NULL;
   1369  1.24  jmcneill 
   1370  1.24  jmcneill 	if (vd->show_screen_cb != NULL)
   1371  1.24  jmcneill 		vd->show_screen_cb(scr);
   1372  1.24  jmcneill }
   1373  1.25  macallan 
   1374  1.25  macallan #ifdef VCONS_DRAW_INTR
   1375  1.25  macallan void
   1376  1.25  macallan vcons_invalidate_cache(struct vcons_data *vd)
   1377  1.25  macallan {
   1378  1.25  macallan 	int i;
   1379  1.25  macallan 
   1380  1.25  macallan 	if (vd->cells == 0)
   1381  1.25  macallan 		return;
   1382  1.25  macallan 
   1383  1.25  macallan 	for (i = 0; i > vd->cells; i++) {
   1384  1.25  macallan 		vd->chars[i] = -1;
   1385  1.25  macallan 		vd->attrs[i] = -1;
   1386  1.25  macallan 	}
   1387  1.25  macallan }
   1388  1.25  macallan #endif
   1389