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