Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_vcons.c revision 1.16.20.1
      1  1.16.20.1  uebayasi /*	$NetBSD: wsdisplay_vcons.c,v 1.16.20.1 2010/10/22 07:22:21 uebayasi 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.16.20.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.16.20.1 2010/10/22 07:22:21 uebayasi 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.1  macallan 
     46        1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     47        1.1  macallan #include <dev/wscons/wsconsio.h>
     48        1.1  macallan #include <dev/wsfont/wsfont.h>
     49        1.1  macallan #include <dev/rasops/rasops.h>
     50        1.1  macallan 
     51        1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     52        1.1  macallan 
     53       1.14  macallan #include "opt_wsemul.h"
     54       1.14  macallan #include "opt_wsdisplay_compat.h"
     55       1.14  macallan #include "opt_vcons.h"
     56       1.14  macallan 
     57        1.1  macallan static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
     58        1.1  macallan 	    long *);
     59        1.1  macallan 
     60       1.10  christos static int  vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     61        1.1  macallan static int  vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
     62        1.1  macallan 	    int *, int *, long *);
     63        1.1  macallan static void vcons_free_screen(void *, void *);
     64        1.1  macallan static int  vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
     65        1.1  macallan 	    void *);
     66        1.1  macallan 
     67       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
     68       1.14  macallan static void vcons_scroll(void *, void *, int);
     69       1.14  macallan static void vcons_do_scroll(struct vcons_screen *);
     70       1.14  macallan #endif
     71       1.14  macallan 
     72       1.15     joerg static void vcons_do_switch(void *);
     73        1.1  macallan 
     74        1.1  macallan /* methods that work only on text buffers */
     75        1.1  macallan static void vcons_copycols_buffer(void *, int, int, int, int);
     76        1.1  macallan static void vcons_erasecols_buffer(void *, int, int, int, long);
     77        1.1  macallan static void vcons_copyrows_buffer(void *, int, int, int);
     78        1.1  macallan static void vcons_eraserows_buffer(void *, int, int, long);
     79        1.1  macallan static void vcons_putchar_buffer(void *, int, int, u_int, long);
     80        1.1  macallan 
     81        1.1  macallan /*
     82        1.1  macallan  * actual wrapper methods which call both the _buffer ones above and the
     83        1.1  macallan  * driver supplied ones to do the drawing
     84        1.1  macallan  */
     85        1.1  macallan static void vcons_copycols(void *, int, int, int, int);
     86        1.1  macallan static void vcons_erasecols(void *, int, int, int, long);
     87        1.1  macallan static void vcons_copyrows(void *, int, int, int);
     88        1.1  macallan static void vcons_eraserows(void *, int, int, long);
     89        1.1  macallan static void vcons_putchar(void *, int, int, u_int, long);
     90        1.1  macallan static void vcons_cursor(void *, int, int, int);
     91        1.1  macallan 
     92  1.16.20.1  uebayasi /*
     93  1.16.20.1  uebayasi  * methods that avoid framebuffer reads
     94  1.16.20.1  uebayasi  */
     95  1.16.20.1  uebayasi static void vcons_copycols_noread(void *, int, int, int, int);
     96  1.16.20.1  uebayasi static void vcons_copyrows_noread(void *, int, int, int);
     97  1.16.20.1  uebayasi 
     98  1.16.20.1  uebayasi 
     99       1.14  macallan /* support for reading/writing text buffers. For wsmoused */
    100        1.6      jmmv static int  vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
    101        1.6      jmmv static int  vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
    102        1.1  macallan 
    103        1.1  macallan static void vcons_lock(struct vcons_screen *);
    104        1.1  macallan static void vcons_unlock(struct vcons_screen *);
    105        1.1  macallan 
    106       1.14  macallan #ifdef VCONS_SWITCH_ASYNC
    107       1.14  macallan static void vcons_kthread(void *);
    108       1.14  macallan #endif
    109        1.1  macallan 
    110        1.1  macallan int
    111        1.1  macallan vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
    112        1.1  macallan     struct wsdisplay_accessops *ao)
    113        1.1  macallan {
    114        1.2  macallan 
    115        1.2  macallan 	/* zero out everything so we can rely on untouched fields being 0 */
    116        1.3  macallan 	memset(vd, 0, sizeof(struct vcons_data));
    117        1.2  macallan 
    118        1.1  macallan 	vd->cookie = cookie;
    119        1.1  macallan 
    120        1.1  macallan 	vd->init_screen = vcons_dummy_init_screen;
    121        1.1  macallan 	vd->show_screen_cb = NULL;
    122        1.1  macallan 
    123        1.6      jmmv 	/* keep a copy of the accessops that we replace below with our
    124        1.6      jmmv 	 * own wrappers */
    125        1.6      jmmv 	vd->ioctl = ao->ioctl;
    126        1.6      jmmv 
    127        1.6      jmmv 	/* configure the accessops */
    128        1.6      jmmv 	ao->ioctl = vcons_ioctl;
    129        1.1  macallan 	ao->alloc_screen = vcons_alloc_screen;
    130        1.1  macallan 	ao->free_screen = vcons_free_screen;
    131        1.1  macallan 	ao->show_screen = vcons_show_screen;
    132       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    133       1.14  macallan 	ao->scroll = vcons_scroll;
    134       1.14  macallan #endif
    135        1.1  macallan 
    136        1.1  macallan 	LIST_INIT(&vd->screens);
    137        1.1  macallan 	vd->active = NULL;
    138        1.1  macallan 	vd->wanted = NULL;
    139        1.1  macallan 	vd->currenttype = def;
    140       1.11        ad 	callout_init(&vd->switch_callout, 0);
    141       1.15     joerg 	callout_setfunc(&vd->switch_callout, vcons_do_switch, vd);
    142        1.1  macallan 
    143        1.1  macallan 	/*
    144        1.1  macallan 	 * a lock to serialize access to the framebuffer.
    145        1.1  macallan 	 * when switching screens we need to make sure there's no rasops
    146        1.1  macallan 	 * operation in progress
    147        1.1  macallan 	 */
    148        1.1  macallan #ifdef DIAGNOSTIC
    149        1.1  macallan 	vd->switch_poll_count = 0;
    150        1.1  macallan #endif
    151       1.14  macallan #ifdef VCONS_SWITCH_ASYNC
    152       1.14  macallan 	kthread_create(PRI_NONE, 0, NULL, vcons_kthread, vd,
    153       1.14  macallan 	    &vd->redraw_thread, "vcons_draw");
    154       1.14  macallan #endif
    155        1.1  macallan 	return 0;
    156        1.1  macallan }
    157        1.1  macallan 
    158        1.1  macallan static void
    159        1.1  macallan vcons_lock(struct vcons_screen *scr)
    160        1.1  macallan {
    161        1.1  macallan #ifdef VCONS_PARANOIA
    162        1.1  macallan 	int s;
    163        1.1  macallan 
    164        1.1  macallan 	s = splhigh();
    165        1.1  macallan #endif
    166        1.1  macallan 	SCREEN_BUSY(scr);
    167        1.1  macallan #ifdef VCONS_PARANOIA
    168        1.1  macallan 	splx(s);
    169        1.1  macallan #endif
    170        1.1  macallan }
    171        1.1  macallan 
    172        1.1  macallan static void
    173        1.1  macallan vcons_unlock(struct vcons_screen *scr)
    174        1.1  macallan {
    175        1.1  macallan #ifdef VCONS_PARANOIA
    176        1.1  macallan 	int s;
    177        1.1  macallan 
    178        1.1  macallan 	s = splhigh();
    179        1.1  macallan #endif
    180        1.1  macallan 	SCREEN_IDLE(scr);
    181        1.1  macallan #ifdef VCONS_PARANOIA
    182        1.1  macallan 	splx(s);
    183        1.1  macallan #endif
    184       1.14  macallan #ifdef VCONS_SWITCH_ASYNC
    185       1.14  macallan 	wakeup(&scr->scr_vd->done_drawing);
    186       1.14  macallan #endif
    187        1.1  macallan }
    188        1.1  macallan 
    189        1.1  macallan static void
    190        1.9  christos vcons_dummy_init_screen(void *cookie,
    191        1.9  christos     struct vcons_screen *scr, int exists,
    192        1.9  christos     long *defattr)
    193        1.1  macallan {
    194        1.1  macallan 
    195        1.1  macallan 	/*
    196        1.1  macallan 	 * default init_screen() method.
    197        1.1  macallan 	 * Needs to be overwritten so we bitch and whine in case anyone ends
    198        1.1  macallan 	 * up in here.
    199        1.1  macallan 	 */
    200        1.1  macallan 	printf("vcons_init_screen: dummy function called. Your driver is "
    201        1.1  macallan 	       "supposed to supply a replacement for proper operation\n");
    202        1.1  macallan }
    203        1.1  macallan 
    204        1.1  macallan int
    205        1.1  macallan vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
    206        1.1  macallan     int existing, long *defattr)
    207        1.1  macallan {
    208        1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    209        1.1  macallan 	int cnt, i;
    210        1.1  macallan 
    211        1.1  macallan 	scr->scr_cookie = vd->cookie;
    212        1.4  jmcneill 	scr->scr_vd = scr->scr_origvd = vd;
    213       1.14  macallan 	scr->scr_busy = 0;
    214        1.2  macallan 
    215        1.1  macallan 	/*
    216        1.1  macallan 	 * call the driver-supplied init_screen function which is expected
    217        1.1  macallan 	 * to set up rasops_info, override cursor() and probably others
    218        1.1  macallan 	 */
    219        1.1  macallan 	vd->init_screen(vd->cookie, scr, existing, defattr);
    220        1.1  macallan 
    221        1.1  macallan 	/*
    222        1.1  macallan 	 * save the non virtual console aware rasops and replace them with
    223        1.1  macallan 	 * our wrappers
    224        1.1  macallan 	 */
    225        1.1  macallan 	vd->eraserows = ri->ri_ops.eraserows;
    226        1.1  macallan 	vd->copyrows  = ri->ri_ops.copyrows;
    227        1.1  macallan 	vd->erasecols = ri->ri_ops.erasecols;
    228        1.1  macallan 	vd->copycols  = ri->ri_ops.copycols;
    229        1.1  macallan 	vd->putchar   = ri->ri_ops.putchar;
    230        1.1  macallan 	vd->cursor    = ri->ri_ops.cursor;
    231        1.1  macallan 
    232        1.1  macallan 	ri->ri_ops.eraserows = vcons_eraserows;
    233        1.1  macallan 	ri->ri_ops.erasecols = vcons_erasecols;
    234        1.1  macallan 	ri->ri_ops.putchar   = vcons_putchar;
    235        1.1  macallan 	ri->ri_ops.cursor    = vcons_cursor;
    236  1.16.20.1  uebayasi 
    237  1.16.20.1  uebayasi 	if (scr->scr_flags & VCONS_NO_COPYCOLS) {
    238  1.16.20.1  uebayasi 		ri->ri_ops.copycols  = vcons_copycols_noread;
    239  1.16.20.1  uebayasi 	} else {
    240  1.16.20.1  uebayasi 		ri->ri_ops.copycols  = vcons_copycols;
    241  1.16.20.1  uebayasi 	}
    242  1.16.20.1  uebayasi 
    243  1.16.20.1  uebayasi 	if (scr->scr_flags & VCONS_NO_COPYROWS) {
    244  1.16.20.1  uebayasi 		ri->ri_ops.copyrows  = vcons_copyrows_noread;
    245  1.16.20.1  uebayasi 	} else {
    246  1.16.20.1  uebayasi 		ri->ri_ops.copyrows  = vcons_copyrows;
    247  1.16.20.1  uebayasi 	}
    248  1.16.20.1  uebayasi 
    249        1.1  macallan 	ri->ri_hw = scr;
    250        1.1  macallan 
    251        1.1  macallan 	/*
    252        1.1  macallan 	 * we allocate both chars and attributes in one chunk, attributes first
    253        1.1  macallan 	 * because they have the (potentially) bigger alignment
    254        1.1  macallan 	 */
    255       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    256       1.14  macallan 	cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
    257       1.14  macallan 	scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
    258       1.14  macallan 	scr->scr_current_line = 0;
    259       1.14  macallan 	scr->scr_line_wanted = 0;
    260       1.14  macallan 	scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
    261       1.14  macallan 	scr->scr_current_offset = scr->scr_offset_to_zero;
    262       1.14  macallan #else
    263        1.1  macallan 	cnt = ri->ri_rows * ri->ri_cols;
    264       1.14  macallan #endif
    265        1.1  macallan 	scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
    266        1.1  macallan 	    sizeof(uint16_t)), M_DEVBUF, M_WAITOK);
    267        1.1  macallan 	if (scr->scr_attrs == NULL)
    268        1.1  macallan 		return ENOMEM;
    269        1.1  macallan 
    270        1.1  macallan 	scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt];
    271        1.1  macallan 
    272        1.1  macallan 	ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    273        1.1  macallan 	scr->scr_defattr = *defattr;
    274        1.1  macallan 
    275        1.1  macallan 	/*
    276        1.1  macallan 	 * fill the attribute buffer with *defattr, chars with 0x20
    277        1.1  macallan 	 * since we don't know if the driver tries to mimic firmware output or
    278        1.1  macallan 	 * reset everything we do nothing to VRAM here, any driver that feels
    279        1.1  macallan 	 * the need to clear screen or something will have to do it on its own
    280        1.1  macallan 	 * Additional screens will start out in the background anyway so
    281        1.1  macallan 	 * cleaning or not only really affects the initial console screen
    282        1.1  macallan 	 */
    283        1.1  macallan 	for (i = 0; i < cnt; i++) {
    284        1.1  macallan 		scr->scr_attrs[i] = *defattr;
    285        1.1  macallan 		scr->scr_chars[i] = 0x20;
    286        1.1  macallan 	}
    287        1.1  macallan 
    288        1.2  macallan 	if(vd->active == NULL) {
    289        1.2  macallan 		vd->active = scr;
    290        1.2  macallan 		SCREEN_VISIBLE(scr);
    291        1.2  macallan 	}
    292        1.2  macallan 
    293        1.1  macallan 	if (existing) {
    294        1.2  macallan 		SCREEN_VISIBLE(scr);
    295        1.2  macallan 		vd->active = scr;
    296        1.1  macallan 	} else {
    297        1.2  macallan 		SCREEN_INVISIBLE(scr);
    298        1.1  macallan 	}
    299        1.1  macallan 
    300        1.1  macallan 	LIST_INSERT_HEAD(&vd->screens, scr, next);
    301        1.1  macallan 	return 0;
    302        1.1  macallan }
    303        1.1  macallan 
    304        1.1  macallan static void
    305       1.15     joerg vcons_do_switch(void *arg)
    306        1.1  macallan {
    307       1.15     joerg 	struct vcons_data *vd = arg;
    308        1.1  macallan 	struct vcons_screen *scr, *oldscr;
    309        1.1  macallan 
    310        1.1  macallan 	scr = vd->wanted;
    311        1.1  macallan 	if (!scr) {
    312        1.1  macallan 		printf("vcons_switch_screen: disappeared\n");
    313        1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, EIO, 0);
    314        1.1  macallan 		return;
    315        1.1  macallan 	}
    316        1.1  macallan 	oldscr = vd->active; /* can be NULL! */
    317        1.1  macallan 
    318        1.1  macallan 	/*
    319        1.1  macallan 	 * if there's an old, visible screen we mark it invisible and wait
    320        1.1  macallan 	 * until it's not busy so we can safely switch
    321        1.1  macallan 	 */
    322        1.1  macallan 	if (oldscr != NULL) {
    323        1.1  macallan 		SCREEN_INVISIBLE(oldscr);
    324        1.1  macallan 		if (SCREEN_IS_BUSY(oldscr)) {
    325       1.15     joerg 			callout_schedule(&vd->switch_callout, 1);
    326        1.1  macallan #ifdef DIAGNOSTIC
    327        1.1  macallan 			/* bitch if we wait too long */
    328        1.1  macallan 			vd->switch_poll_count++;
    329        1.1  macallan 			if (vd->switch_poll_count > 100) {
    330        1.1  macallan 				panic("vcons: screen still busy");
    331        1.1  macallan 			}
    332        1.1  macallan #endif
    333        1.1  macallan 			return;
    334        1.1  macallan 		}
    335        1.1  macallan 		/* invisible screen -> no visible cursor image */
    336        1.1  macallan 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
    337        1.1  macallan #ifdef DIAGNOSTIC
    338        1.1  macallan 		vd->switch_poll_count = 0;
    339        1.1  macallan #endif
    340        1.1  macallan 	}
    341        1.1  macallan 
    342        1.1  macallan 	if (scr == oldscr)
    343        1.1  macallan 		return;
    344        1.1  macallan 
    345        1.1  macallan #ifdef DIAGNOSTIC
    346        1.1  macallan 	if (SCREEN_IS_VISIBLE(scr))
    347       1.14  macallan 		printf("vcons_switch_screen: already active");
    348        1.1  macallan #endif
    349        1.1  macallan 
    350        1.1  macallan #ifdef notyet
    351        1.1  macallan 	if (vd->currenttype != type) {
    352        1.1  macallan 		vcons_set_screentype(vd, type);
    353        1.1  macallan 		vd->currenttype = type;
    354        1.1  macallan 	}
    355        1.1  macallan #endif
    356        1.1  macallan 
    357        1.1  macallan 	SCREEN_VISIBLE(scr);
    358        1.1  macallan 	vd->active = scr;
    359        1.1  macallan 	vd->wanted = NULL;
    360        1.1  macallan 
    361        1.1  macallan 	if (vd->show_screen_cb != NULL)
    362        1.1  macallan 		vd->show_screen_cb(scr);
    363        1.1  macallan 
    364        1.1  macallan 	if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
    365        1.1  macallan 		vcons_redraw_screen(scr);
    366        1.1  macallan 
    367        1.1  macallan 	if (vd->switch_cb)
    368        1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, 0, 0);
    369        1.1  macallan }
    370        1.1  macallan 
    371        1.1  macallan void
    372        1.1  macallan vcons_redraw_screen(struct vcons_screen *scr)
    373        1.1  macallan {
    374        1.1  macallan 	uint16_t *charptr = scr->scr_chars;
    375        1.1  macallan 	long *attrptr = scr->scr_attrs;
    376        1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    377        1.1  macallan 	int i, j, offset;
    378        1.1  macallan 
    379        1.1  macallan 	vcons_lock(scr);
    380        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    381        1.1  macallan 
    382        1.1  macallan 		/*
    383        1.1  macallan 		 * only clear the screen when RI_FULLCLEAR is set since we're
    384        1.1  macallan 		 * going to overwrite every single character cell anyway
    385        1.1  macallan 		 */
    386        1.1  macallan 		if (ri->ri_flg & RI_FULLCLEAR) {
    387        1.1  macallan 			scr->scr_vd->eraserows(ri, 0, ri->ri_rows,
    388        1.1  macallan 			    scr->scr_defattr);
    389        1.1  macallan 		}
    390        1.1  macallan 
    391        1.1  macallan 		/* redraw the screen */
    392       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    393       1.14  macallan 		offset = scr->scr_current_offset;
    394       1.14  macallan #else
    395        1.1  macallan 		offset = 0;
    396       1.14  macallan #endif
    397        1.1  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    398        1.1  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    399        1.1  macallan 				/*
    400        1.1  macallan 				 * no need to use the wrapper function - we
    401        1.1  macallan 				 * don't change any characters or attributes
    402        1.1  macallan 				 * and we already made sure the screen we're
    403        1.1  macallan 				 * working on is visible
    404        1.1  macallan 				 */
    405        1.1  macallan 				scr->scr_vd->putchar(ri, i, j,
    406        1.1  macallan 				    charptr[offset], attrptr[offset]);
    407        1.1  macallan 				offset++;
    408        1.1  macallan 			}
    409        1.1  macallan 		}
    410        1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    411        1.1  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    412        1.1  macallan 	}
    413        1.1  macallan 	vcons_unlock(scr);
    414        1.1  macallan }
    415        1.1  macallan 
    416        1.1  macallan static int
    417       1.10  christos vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    418        1.6      jmmv 	struct lwp *l)
    419        1.6      jmmv {
    420        1.6      jmmv 	struct vcons_data *vd = v;
    421        1.6      jmmv 	int error;
    422        1.6      jmmv 
    423        1.6      jmmv 	switch (cmd) {
    424        1.6      jmmv 	case WSDISPLAYIO_GETWSCHAR:
    425        1.6      jmmv 		error = vcons_getwschar((struct vcons_screen *)vs,
    426        1.6      jmmv 			(struct wsdisplay_char *)data);
    427        1.6      jmmv 		break;
    428        1.6      jmmv 
    429        1.6      jmmv 	case WSDISPLAYIO_PUTWSCHAR:
    430        1.6      jmmv 		error = vcons_putwschar((struct vcons_screen *)vs,
    431        1.6      jmmv 			(struct wsdisplay_char *)data);
    432        1.6      jmmv 		break;
    433        1.6      jmmv 
    434        1.6      jmmv 	default:
    435        1.6      jmmv 		if (vd->ioctl != NULL)
    436        1.6      jmmv 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    437        1.6      jmmv 		else
    438        1.6      jmmv 			error = EPASSTHROUGH;
    439        1.6      jmmv 	}
    440        1.6      jmmv 
    441        1.6      jmmv 	return error;
    442        1.6      jmmv }
    443        1.6      jmmv 
    444        1.6      jmmv static int
    445        1.1  macallan vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    446        1.1  macallan     int *curxp, int *curyp, long *defattrp)
    447        1.1  macallan {
    448        1.1  macallan 	struct vcons_data *vd = v;
    449        1.1  macallan 	struct vcons_screen *scr;
    450        1.1  macallan 	int ret;
    451        1.1  macallan 
    452        1.1  macallan 	scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
    453        1.1  macallan 	if (scr == NULL)
    454        1.1  macallan 		return ENOMEM;
    455        1.1  macallan 
    456        1.1  macallan 	scr->scr_flags = 0;
    457        1.2  macallan 	scr->scr_status = 0;
    458        1.2  macallan 	scr->scr_busy = 0;
    459        1.1  macallan 	scr->scr_type = type;
    460        1.1  macallan 
    461        1.1  macallan 	ret = vcons_init_screen(vd, scr, 0, defattrp);
    462        1.1  macallan 	if (ret != 0) {
    463        1.1  macallan 		free(scr, M_DEVBUF);
    464        1.1  macallan 		return ret;
    465        1.1  macallan 	}
    466        1.1  macallan 
    467        1.1  macallan 	if (vd->active == NULL) {
    468        1.1  macallan 		SCREEN_VISIBLE(scr);
    469        1.1  macallan 		vd->active = scr;
    470        1.1  macallan 		vd->currenttype = type;
    471        1.1  macallan 	}
    472        1.1  macallan 
    473        1.1  macallan 	*cookiep = scr;
    474        1.1  macallan 	*curxp = scr->scr_ri.ri_ccol;
    475        1.1  macallan 	*curyp = scr->scr_ri.ri_crow;
    476        1.1  macallan 	return 0;
    477        1.1  macallan }
    478        1.1  macallan 
    479        1.1  macallan static void
    480        1.1  macallan vcons_free_screen(void *v, void *cookie)
    481        1.1  macallan {
    482        1.1  macallan 	struct vcons_data *vd = v;
    483        1.1  macallan 	struct vcons_screen *scr = cookie;
    484        1.1  macallan 
    485        1.1  macallan 	vcons_lock(scr);
    486        1.1  macallan 	/* there should be no rasops activity here */
    487        1.1  macallan 
    488        1.1  macallan 	LIST_REMOVE(scr, next);
    489        1.1  macallan 
    490        1.1  macallan 	if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
    491        1.1  macallan 		free(scr->scr_attrs, M_DEVBUF);
    492        1.1  macallan 		free(scr, M_DEVBUF);
    493        1.1  macallan 	} else {
    494        1.1  macallan 		/*
    495        1.1  macallan 		 * maybe we should just restore the old rasops_info methods
    496        1.1  macallan 		 * and free the character/attribute buffer here?
    497        1.1  macallan 		 */
    498        1.1  macallan #ifdef VCONS_DEBUG
    499        1.1  macallan 		panic("vcons_free_screen: console");
    500        1.1  macallan #else
    501        1.1  macallan 		printf("vcons_free_screen: console\n");
    502        1.1  macallan #endif
    503        1.1  macallan 	}
    504        1.1  macallan 
    505        1.1  macallan 	if (vd->active == scr)
    506        1.1  macallan 		vd->active = NULL;
    507        1.1  macallan }
    508        1.1  macallan 
    509        1.1  macallan static int
    510        1.9  christos vcons_show_screen(void *v, void *cookie, int waitok,
    511        1.1  macallan     void (*cb)(void *, int, int), void *cb_arg)
    512        1.1  macallan {
    513        1.1  macallan 	struct vcons_data *vd = v;
    514        1.1  macallan 	struct vcons_screen *scr;
    515        1.1  macallan 
    516        1.1  macallan 	scr = cookie;
    517        1.1  macallan 	if (scr == vd->active)
    518        1.1  macallan 		return 0;
    519        1.1  macallan 
    520        1.1  macallan 	vd->wanted = scr;
    521        1.1  macallan 	vd->switch_cb = cb;
    522        1.1  macallan 	vd->switch_cb_arg = cb_arg;
    523       1.14  macallan #ifdef VCONS_SWITCH_ASYNC
    524       1.14  macallan 	wakeup(&vd->start_drawing);
    525       1.14  macallan 	return EAGAIN;
    526       1.14  macallan #else
    527        1.1  macallan 	if (cb) {
    528       1.15     joerg 		callout_schedule(&vd->switch_callout, 0);
    529        1.1  macallan 		return EAGAIN;
    530        1.1  macallan 	}
    531        1.1  macallan 
    532        1.1  macallan 	vcons_do_switch(vd);
    533        1.1  macallan 	return 0;
    534       1.14  macallan #endif
    535        1.1  macallan }
    536        1.1  macallan 
    537        1.1  macallan /* wrappers for rasops_info methods */
    538        1.1  macallan 
    539        1.1  macallan static void
    540        1.1  macallan vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
    541        1.1  macallan {
    542        1.1  macallan 	struct rasops_info *ri = cookie;
    543        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    544        1.1  macallan 	int from = srccol + row * ri->ri_cols;
    545        1.1  macallan 	int to = dstcol + row * ri->ri_cols;
    546        1.1  macallan 
    547       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    548       1.14  macallan 	int offset;
    549       1.14  macallan 	offset = scr->scr_offset_to_zero;
    550       1.14  macallan 
    551       1.14  macallan 	memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from],
    552       1.14  macallan 	    ncols * sizeof(long));
    553       1.14  macallan 	memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from],
    554       1.14  macallan 	    ncols * sizeof(uint16_t));
    555       1.14  macallan #else
    556        1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    557        1.1  macallan 	    ncols * sizeof(long));
    558        1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    559        1.1  macallan 	    ncols * sizeof(uint16_t));
    560       1.14  macallan #endif
    561        1.1  macallan }
    562        1.1  macallan 
    563        1.1  macallan static void
    564        1.1  macallan vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    565        1.1  macallan {
    566        1.1  macallan 	struct rasops_info *ri = cookie;
    567        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    568        1.1  macallan 
    569        1.1  macallan 	vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
    570        1.1  macallan 
    571        1.1  macallan 	vcons_lock(scr);
    572        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    573        1.1  macallan 		scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
    574        1.1  macallan 	}
    575        1.1  macallan 	vcons_unlock(scr);
    576        1.1  macallan }
    577        1.1  macallan 
    578        1.1  macallan static void
    579  1.16.20.1  uebayasi vcons_copycols_noread(void *cookie, int row, int srccol, int dstcol, int ncols)
    580  1.16.20.1  uebayasi {
    581  1.16.20.1  uebayasi 	struct rasops_info *ri = cookie;
    582  1.16.20.1  uebayasi 	struct vcons_screen *scr = ri->ri_hw;
    583  1.16.20.1  uebayasi 
    584  1.16.20.1  uebayasi 	vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
    585  1.16.20.1  uebayasi 
    586  1.16.20.1  uebayasi 	vcons_lock(scr);
    587  1.16.20.1  uebayasi 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    588  1.16.20.1  uebayasi 		int pos, c, offset;
    589  1.16.20.1  uebayasi 
    590  1.16.20.1  uebayasi #ifdef WSDISPLAY_SCROLLSUPPORT
    591  1.16.20.1  uebayasi 		offset = scr->scr_current_offset;
    592  1.16.20.1  uebayasi #else
    593  1.16.20.1  uebayasi 		offset = 0;
    594  1.16.20.1  uebayasi #endif
    595  1.16.20.1  uebayasi 		pos = ri->ri_cols * row + dstcol + offset;
    596  1.16.20.1  uebayasi 		for (c = dstcol; c < (dstcol + ncols); c++) {
    597  1.16.20.1  uebayasi 			scr->scr_vd->putchar(cookie, row, c,
    598  1.16.20.1  uebayasi 			   scr->scr_chars[pos], scr->scr_attrs[pos]);
    599  1.16.20.1  uebayasi 			pos++;
    600  1.16.20.1  uebayasi 		}
    601  1.16.20.1  uebayasi 	}
    602  1.16.20.1  uebayasi 	vcons_unlock(scr);
    603  1.16.20.1  uebayasi }
    604  1.16.20.1  uebayasi 
    605  1.16.20.1  uebayasi static void
    606        1.1  macallan vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
    607        1.1  macallan {
    608        1.1  macallan 	struct rasops_info *ri = cookie;
    609        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    610        1.1  macallan 	int start = startcol + row * ri->ri_cols;
    611        1.1  macallan 	int end = start + ncols, i;
    612        1.1  macallan 
    613       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    614       1.14  macallan 	int offset;
    615       1.14  macallan 	offset = scr->scr_offset_to_zero;
    616       1.14  macallan 
    617       1.14  macallan 	for (i = start; i < end; i++) {
    618       1.14  macallan 		scr->scr_attrs[offset + i] = fillattr;
    619       1.14  macallan 		scr->scr_chars[offset + i] = 0x20;
    620       1.14  macallan 	}
    621       1.14  macallan #else
    622        1.1  macallan 	for (i = start; i < end; i++) {
    623        1.1  macallan 		scr->scr_attrs[i] = fillattr;
    624        1.1  macallan 		scr->scr_chars[i] = 0x20;
    625        1.1  macallan 	}
    626       1.14  macallan #endif
    627        1.1  macallan }
    628        1.1  macallan 
    629        1.1  macallan static void
    630        1.1  macallan vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    631        1.1  macallan {
    632        1.1  macallan 	struct rasops_info *ri = cookie;
    633        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    634        1.1  macallan 
    635        1.1  macallan 	vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
    636        1.1  macallan 
    637        1.1  macallan 	vcons_lock(scr);
    638        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    639        1.1  macallan 		scr->scr_vd->erasecols(cookie, row, startcol, ncols,
    640        1.1  macallan 		    fillattr);
    641        1.1  macallan 	}
    642        1.1  macallan 	vcons_unlock(scr);
    643        1.1  macallan }
    644        1.1  macallan 
    645        1.1  macallan static void
    646        1.1  macallan vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
    647        1.1  macallan {
    648        1.1  macallan 	struct rasops_info *ri = cookie;
    649        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    650        1.1  macallan 	int from, to, len;
    651        1.1  macallan 
    652       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    653       1.14  macallan 	int offset;
    654       1.14  macallan 	offset = scr->scr_offset_to_zero;
    655       1.14  macallan 
    656       1.14  macallan 	/* do we need to scroll the back buffer? */
    657       1.14  macallan 	if (dstrow == 0) {
    658       1.14  macallan 		from = ri->ri_cols * srcrow;
    659       1.14  macallan 		to = ri->ri_cols * dstrow;
    660       1.14  macallan 
    661       1.14  macallan 		memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    662       1.14  macallan 		    scr->scr_offset_to_zero * sizeof(long));
    663       1.14  macallan 		memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    664       1.14  macallan 		    scr->scr_offset_to_zero * sizeof(uint16_t));
    665       1.14  macallan 	}
    666       1.14  macallan 	from = ri->ri_cols * srcrow + offset;
    667       1.14  macallan 	to = ri->ri_cols * dstrow + offset;
    668       1.14  macallan 	len = ri->ri_cols * nrows;
    669       1.14  macallan 
    670       1.14  macallan #else
    671        1.1  macallan 	from = ri->ri_cols * srcrow;
    672        1.1  macallan 	to = ri->ri_cols * dstrow;
    673        1.1  macallan 	len = ri->ri_cols * nrows;
    674       1.14  macallan #endif
    675        1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    676        1.1  macallan 	    len * sizeof(long));
    677        1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    678        1.1  macallan 	    len * sizeof(uint16_t));
    679        1.1  macallan }
    680        1.1  macallan 
    681        1.1  macallan static void
    682        1.1  macallan vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    683        1.1  macallan {
    684        1.1  macallan 	struct rasops_info *ri = cookie;
    685        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    686        1.1  macallan 
    687        1.1  macallan 	vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
    688        1.1  macallan 
    689        1.1  macallan 	vcons_lock(scr);
    690        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    691        1.1  macallan 		scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
    692        1.1  macallan 	}
    693        1.1  macallan 	vcons_unlock(scr);
    694        1.1  macallan }
    695        1.1  macallan 
    696        1.1  macallan static void
    697  1.16.20.1  uebayasi vcons_copyrows_noread(void *cookie, int srcrow, int dstrow, int nrows)
    698  1.16.20.1  uebayasi {
    699  1.16.20.1  uebayasi 	struct rasops_info *ri = cookie;
    700  1.16.20.1  uebayasi 	struct vcons_screen *scr = ri->ri_hw;
    701  1.16.20.1  uebayasi 
    702  1.16.20.1  uebayasi 	vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
    703  1.16.20.1  uebayasi 
    704  1.16.20.1  uebayasi 	vcons_lock(scr);
    705  1.16.20.1  uebayasi 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    706  1.16.20.1  uebayasi 		int pos, l, c, offset;
    707  1.16.20.1  uebayasi 
    708  1.16.20.1  uebayasi #ifdef WSDISPLAY_SCROLLSUPPORT
    709  1.16.20.1  uebayasi 		offset = scr->scr_current_offset;
    710  1.16.20.1  uebayasi #else
    711  1.16.20.1  uebayasi 		offset = 0;
    712  1.16.20.1  uebayasi #endif
    713  1.16.20.1  uebayasi 		pos = ri->ri_cols * dstrow + offset;
    714  1.16.20.1  uebayasi 		for (l = dstrow; l < (dstrow + nrows); l++) {
    715  1.16.20.1  uebayasi 			for (c = 0; c < ri->ri_cols; c++) {
    716  1.16.20.1  uebayasi 				scr->scr_vd->putchar(cookie, l, c,
    717  1.16.20.1  uebayasi 				   scr->scr_chars[pos], scr->scr_attrs[pos]);
    718  1.16.20.1  uebayasi 				pos++;
    719  1.16.20.1  uebayasi 			}
    720  1.16.20.1  uebayasi 		}
    721  1.16.20.1  uebayasi 	}
    722  1.16.20.1  uebayasi 	vcons_unlock(scr);
    723  1.16.20.1  uebayasi }
    724  1.16.20.1  uebayasi 
    725  1.16.20.1  uebayasi static void
    726        1.1  macallan vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
    727        1.1  macallan {
    728        1.1  macallan 	struct rasops_info *ri = cookie;
    729        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    730        1.1  macallan 	int start, end, i;
    731        1.1  macallan 
    732       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    733       1.14  macallan 	int offset;
    734       1.14  macallan 	offset = scr->scr_offset_to_zero;
    735       1.14  macallan 
    736       1.14  macallan 	start = ri->ri_cols * row + offset;
    737       1.14  macallan 	end = ri->ri_cols * (row + nrows) + offset;
    738       1.14  macallan #else
    739        1.1  macallan 	start = ri->ri_cols * row;
    740        1.1  macallan 	end = ri->ri_cols * (row + nrows);
    741       1.14  macallan #endif
    742        1.1  macallan 
    743        1.1  macallan 	for (i = start; i < end; i++) {
    744        1.1  macallan 		scr->scr_attrs[i] = fillattr;
    745        1.1  macallan 		scr->scr_chars[i] = 0x20;
    746        1.1  macallan 	}
    747        1.1  macallan }
    748        1.1  macallan 
    749        1.1  macallan static void
    750        1.1  macallan vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
    751        1.1  macallan {
    752        1.1  macallan 	struct rasops_info *ri = cookie;
    753        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    754        1.1  macallan 
    755        1.1  macallan 	vcons_eraserows_buffer(cookie, row, nrows, fillattr);
    756        1.1  macallan 
    757        1.1  macallan 	vcons_lock(scr);
    758        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    759        1.1  macallan 		scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
    760        1.1  macallan 	}
    761        1.1  macallan 	vcons_unlock(scr);
    762        1.1  macallan }
    763        1.1  macallan 
    764        1.1  macallan static void
    765        1.1  macallan vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
    766        1.1  macallan {
    767        1.1  macallan 	struct rasops_info *ri = cookie;
    768        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    769        1.1  macallan 	int pos;
    770        1.1  macallan 
    771       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    772       1.14  macallan 	int offset;
    773       1.14  macallan 	offset = scr->scr_offset_to_zero;
    774       1.14  macallan 
    775       1.14  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    776       1.14  macallan 	     (col < ri->ri_cols)) {
    777       1.14  macallan 		pos = col + row * ri->ri_cols;
    778       1.14  macallan 		scr->scr_attrs[pos + offset] = attr;
    779       1.14  macallan 		scr->scr_chars[pos + offset] = c;
    780       1.14  macallan 	}
    781       1.14  macallan #else
    782        1.1  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    783        1.1  macallan 	     (col < ri->ri_cols)) {
    784        1.1  macallan 		pos = col + row * ri->ri_cols;
    785        1.1  macallan 		scr->scr_attrs[pos] = attr;
    786        1.1  macallan 		scr->scr_chars[pos] = c;
    787        1.1  macallan 	}
    788       1.14  macallan #endif
    789        1.1  macallan }
    790        1.1  macallan 
    791        1.1  macallan static void
    792        1.1  macallan vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
    793        1.1  macallan {
    794        1.1  macallan 	struct rasops_info *ri = cookie;
    795        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    796        1.1  macallan 
    797        1.1  macallan 	vcons_putchar_buffer(cookie, row, col, c, attr);
    798        1.1  macallan 
    799        1.1  macallan 	vcons_lock(scr);
    800        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    801        1.1  macallan 		scr->scr_vd->putchar(cookie, row, col, c, attr);
    802        1.1  macallan 	}
    803        1.1  macallan 	vcons_unlock(scr);
    804        1.1  macallan }
    805        1.1  macallan 
    806        1.1  macallan static void
    807        1.1  macallan vcons_cursor(void *cookie, int on, int row, int col)
    808        1.1  macallan {
    809        1.1  macallan 	struct rasops_info *ri = cookie;
    810        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    811        1.1  macallan 
    812        1.1  macallan 	vcons_lock(scr);
    813        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    814        1.1  macallan 		scr->scr_vd->cursor(cookie, on, row, col);
    815        1.1  macallan 	} else {
    816        1.1  macallan 		scr->scr_ri.ri_crow = row;
    817        1.1  macallan 		scr->scr_ri.ri_ccol = col;
    818        1.1  macallan 	}
    819        1.1  macallan 	vcons_unlock(scr);
    820        1.1  macallan }
    821        1.1  macallan 
    822        1.1  macallan /* methods to read/write characters via ioctl() */
    823        1.1  macallan 
    824        1.1  macallan static int
    825        1.6      jmmv vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
    826        1.1  macallan {
    827        1.1  macallan 	long attr;
    828        1.6      jmmv 	struct rasops_info *ri;
    829        1.6      jmmv 
    830        1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
    831        1.6      jmmv 
    832       1.13    rumble 	ri = &scr->scr_ri;
    833       1.13    rumble 
    834       1.12       mjf 	if (__predict_false((unsigned int)wsc->col > ri->ri_cols ||
    835       1.12       mjf 	    (unsigned int)wsc->row > ri->ri_rows))
    836       1.12       mjf 			return (EINVAL);
    837        1.1  macallan 
    838       1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
    839       1.14  macallan 	     (wsc->col < ri->ri_cols)) {
    840       1.14  macallan 
    841       1.14  macallan 		ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
    842       1.14  macallan 		    wsc->flags, &attr);
    843       1.14  macallan 		vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
    844       1.14  macallan #ifdef VCONS_DEBUG
    845       1.14  macallan 		printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col,
    846       1.14  macallan 		    wsc->letter, attr);
    847       1.14  macallan #endif
    848       1.14  macallan 		return 0;
    849       1.14  macallan 	} else
    850       1.14  macallan 		return EINVAL;
    851        1.1  macallan }
    852        1.1  macallan 
    853        1.1  macallan static int
    854        1.6      jmmv vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
    855        1.1  macallan {
    856        1.6      jmmv 	int offset;
    857        1.1  macallan 	long attr;
    858        1.6      jmmv 	struct rasops_info *ri;
    859        1.6      jmmv 
    860        1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
    861        1.6      jmmv 
    862        1.6      jmmv 	ri = &scr->scr_ri;
    863        1.1  macallan 
    864       1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
    865       1.14  macallan 	     (wsc->col < ri->ri_cols)) {
    866       1.14  macallan 
    867       1.14  macallan 		offset = ri->ri_cols * wsc->row + wsc->col;
    868       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    869       1.14  macallan 		offset += scr->scr_offset_to_zero;
    870       1.14  macallan #endif
    871       1.14  macallan 		wsc->letter = scr->scr_chars[offset];
    872       1.14  macallan 		attr = scr->scr_attrs[offset];
    873       1.14  macallan 
    874       1.14  macallan 		/*
    875       1.14  macallan 		 * this is ugly. We need to break up an attribute into colours and
    876       1.14  macallan 		 * flags but there's no rasops method to do that so we must rely on
    877       1.14  macallan 		 * the 'canonical' encoding.
    878       1.14  macallan 		 */
    879       1.14  macallan #ifdef VCONS_DEBUG
    880       1.14  macallan 		printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row,
    881       1.14  macallan 		    wsc->col, wsc->letter, attr);
    882       1.14  macallan #endif
    883       1.14  macallan 		wsc->foreground = (attr >> 24) & 0xff;
    884       1.14  macallan 		wsc->background = (attr >> 16) & 0xff;
    885       1.14  macallan 		wsc->flags      = attr & 0xff;
    886       1.14  macallan 		return 0;
    887       1.14  macallan 	} else
    888       1.14  macallan 		return EINVAL;
    889       1.14  macallan }
    890       1.14  macallan 
    891       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    892       1.14  macallan 
    893       1.14  macallan static void
    894       1.14  macallan vcons_scroll(void *cookie, void *vs, int where)
    895       1.14  macallan {
    896       1.14  macallan 	struct vcons_screen *scr = vs;
    897       1.14  macallan 
    898       1.14  macallan 	if (where == 0) {
    899       1.14  macallan 		scr->scr_line_wanted = 0;
    900       1.14  macallan 	} else {
    901       1.14  macallan 		scr->scr_line_wanted = scr->scr_line_wanted - where;
    902       1.14  macallan 		if (scr->scr_line_wanted < 0)
    903       1.14  macallan 			scr->scr_line_wanted = 0;
    904       1.14  macallan 		if (scr->scr_line_wanted > scr->scr_lines_in_buffer)
    905       1.14  macallan 			scr->scr_line_wanted = scr->scr_lines_in_buffer;
    906       1.14  macallan 	}
    907       1.14  macallan 
    908       1.14  macallan 	if (scr->scr_line_wanted != scr->scr_current_line) {
    909       1.14  macallan 
    910       1.14  macallan 		vcons_do_scroll(scr);
    911       1.14  macallan 	}
    912       1.14  macallan }
    913       1.14  macallan 
    914       1.14  macallan static void
    915       1.14  macallan vcons_do_scroll(struct vcons_screen *scr)
    916       1.14  macallan {
    917       1.14  macallan 	int dist, from, to, num;
    918       1.14  macallan 	int r_offset, r_start;
    919       1.14  macallan 	int i, j;
    920       1.14  macallan 
    921       1.14  macallan 	if (scr->scr_line_wanted == scr->scr_current_line)
    922       1.14  macallan 		return;
    923       1.14  macallan 	dist = scr->scr_line_wanted - scr->scr_current_line;
    924       1.14  macallan 	scr->scr_current_line = scr->scr_line_wanted;
    925       1.14  macallan 	scr->scr_current_offset = scr->scr_ri.ri_cols *
    926       1.14  macallan 	    (scr->scr_lines_in_buffer - scr->scr_current_line);
    927       1.14  macallan 	if (abs(dist) >= scr->scr_ri.ri_rows) {
    928       1.14  macallan 		vcons_redraw_screen(scr);
    929       1.14  macallan 		return;
    930       1.14  macallan 	}
    931       1.14  macallan 	/* scroll and redraw only what we really have to */
    932       1.14  macallan 	if (dist > 0) {
    933       1.14  macallan 		/* we scroll down */
    934       1.14  macallan 		from = 0;
    935       1.14  macallan 		to = dist;
    936       1.14  macallan 		num = scr->scr_ri.ri_rows - dist;
    937       1.14  macallan 		/* now the redraw parameters */
    938       1.14  macallan 		r_offset = scr->scr_current_offset;
    939       1.14  macallan 		r_start = 0;
    940       1.14  macallan 	} else {
    941       1.14  macallan 		/* scrolling up */
    942       1.14  macallan 		to = 0;
    943       1.14  macallan 		from = -dist;
    944       1.14  macallan 		num = scr->scr_ri.ri_rows + dist;
    945       1.14  macallan 		r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
    946       1.14  macallan 		r_start = num;
    947       1.14  macallan 	}
    948       1.14  macallan 	scr->scr_vd->copyrows(scr, from, to, num);
    949       1.14  macallan 	for (i = 0; i < abs(dist); i++) {
    950       1.14  macallan 		for (j = 0; j < scr->scr_ri.ri_cols; j++) {
    951       1.14  macallan 			scr->scr_vd->putchar(scr, i + r_start, j,
    952       1.14  macallan 			    scr->scr_chars[r_offset],
    953       1.14  macallan 			    scr->scr_attrs[r_offset]);
    954       1.14  macallan 			r_offset++;
    955       1.14  macallan 		}
    956       1.14  macallan 	}
    957       1.14  macallan 
    958       1.14  macallan 	if (scr->scr_line_wanted == 0) {
    959       1.14  macallan 		/* this was a reset - need to draw the cursor */
    960       1.14  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    961       1.14  macallan 		scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow,
    962       1.14  macallan 		    scr->scr_ri.ri_ccol);
    963       1.14  macallan 	}
    964       1.14  macallan }
    965       1.14  macallan 
    966       1.14  macallan #endif /* WSDISPLAY_SCROLLSUPPORT */
    967       1.14  macallan 
    968       1.14  macallan /* async drawing using a kernel thread */
    969       1.14  macallan 
    970       1.14  macallan #ifdef VCONS_SWITCH_ASYNC
    971       1.14  macallan static void
    972       1.14  macallan vcons_kthread(void *cookie)
    973       1.14  macallan {
    974       1.14  macallan 	struct vcons_data *vd = cookie;
    975       1.14  macallan 	struct vcons_screen *scr;
    976       1.14  macallan 	int sec = hz;
    977       1.14  macallan 
    978       1.14  macallan 	while (1) {
    979       1.14  macallan 
    980       1.14  macallan 		tsleep(&vd->start_drawing, 0, "vc_idle", sec);
    981       1.14  macallan 		if ((vd->wanted != vd->active) && (vd->wanted != NULL)) {
    982       1.14  macallan 			/*
    983       1.14  macallan 			 * we need to switch screens
    984       1.14  macallan 			 * so first we mark the active screen as invisible
    985       1.14  macallan 			 * and wait until it's idle
    986       1.14  macallan 			 */
    987       1.14  macallan 			scr = vd->wanted;
    988       1.14  macallan 			SCREEN_INVISIBLE(vd->active);
    989       1.14  macallan 			while (SCREEN_IS_BUSY(vd->active)) {
    990       1.14  macallan 
    991       1.14  macallan 				tsleep(&vd->done_drawing, 0, "vc_wait", sec);
    992       1.14  macallan 			}
    993       1.14  macallan 			/*
    994       1.14  macallan 			 * now we mark the wanted screen busy so nobody
    995       1.14  macallan 			 * messes around while we redraw it
    996       1.14  macallan 			 */
    997       1.14  macallan 			vd->active = scr;
    998       1.14  macallan 			vd->wanted = NULL;
    999       1.14  macallan 			SCREEN_VISIBLE(scr);
   1000       1.14  macallan 
   1001       1.14  macallan 			if (vd->show_screen_cb != NULL)
   1002       1.14  macallan 				vd->show_screen_cb(scr);
   1003       1.14  macallan 
   1004       1.14  macallan 			if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
   1005       1.14  macallan 				vcons_redraw_screen(scr);
   1006       1.14  macallan 
   1007       1.14  macallan 			if (vd->switch_cb)
   1008       1.14  macallan 				vd->switch_cb(vd->switch_cb_arg, 0, 0);
   1009       1.14  macallan 		}
   1010       1.14  macallan 	}
   1011        1.1  macallan }
   1012       1.14  macallan #endif /* VCONS_SWITCH_ASYNC */
   1013