Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_vcons.c revision 1.9
      1  1.9  christos /*	$NetBSD: wsdisplay_vcons.c,v 1.9 2006/11/16 01:33:31 christos 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  * 3. All advertising materials mentioning features or use of this software
     16  1.1  macallan  *    must display the following acknowledgement:
     17  1.1  macallan  *        This product includes software developed by the NetBSD
     18  1.1  macallan  *        Foundation, Inc. and its contributors.
     19  1.1  macallan  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.1  macallan  *    contributors may be used to endorse or promote products derived
     21  1.1  macallan  *    from this software without specific prior written permission.
     22  1.1  macallan  *
     23  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  macallan  */
     35  1.1  macallan 
     36  1.1  macallan #include <sys/cdefs.h>
     37  1.9  christos __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.9 2006/11/16 01:33:31 christos Exp $");
     38  1.1  macallan 
     39  1.1  macallan #include <sys/param.h>
     40  1.1  macallan #include <sys/systm.h>
     41  1.1  macallan #include <sys/kernel.h>
     42  1.1  macallan #include <sys/buf.h>
     43  1.1  macallan #include <sys/device.h>
     44  1.1  macallan #include <sys/ioctl.h>
     45  1.1  macallan #include <sys/malloc.h>
     46  1.1  macallan #include <sys/mman.h>
     47  1.1  macallan #include <sys/tty.h>
     48  1.1  macallan #include <sys/conf.h>
     49  1.1  macallan #include <sys/proc.h>
     50  1.1  macallan #include <sys/kthread.h>
     51  1.1  macallan #include <sys/tprintf.h>
     52  1.1  macallan 
     53  1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     54  1.1  macallan #include <dev/wscons/wsconsio.h>
     55  1.1  macallan #include <dev/wsfont/wsfont.h>
     56  1.1  macallan #include <dev/rasops/rasops.h>
     57  1.1  macallan 
     58  1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     59  1.1  macallan 
     60  1.1  macallan static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
     61  1.1  macallan 	    long *);
     62  1.1  macallan 
     63  1.6      jmmv static int  vcons_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
     64  1.1  macallan static int  vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
     65  1.1  macallan 	    int *, int *, long *);
     66  1.1  macallan static void vcons_free_screen(void *, void *);
     67  1.1  macallan static int  vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
     68  1.1  macallan 	    void *);
     69  1.1  macallan 
     70  1.1  macallan static void vcons_do_switch(struct vcons_data *);
     71  1.1  macallan 
     72  1.1  macallan /* methods that work only on text buffers */
     73  1.1  macallan static void vcons_copycols_buffer(void *, int, int, int, int);
     74  1.1  macallan static void vcons_erasecols_buffer(void *, int, int, int, long);
     75  1.1  macallan static void vcons_copyrows_buffer(void *, int, int, int);
     76  1.1  macallan static void vcons_eraserows_buffer(void *, int, int, long);
     77  1.1  macallan static void vcons_putchar_buffer(void *, int, int, u_int, long);
     78  1.1  macallan 
     79  1.1  macallan /*
     80  1.1  macallan  * actual wrapper methods which call both the _buffer ones above and the
     81  1.1  macallan  * driver supplied ones to do the drawing
     82  1.1  macallan  */
     83  1.1  macallan static void vcons_copycols(void *, int, int, int, int);
     84  1.1  macallan static void vcons_erasecols(void *, int, int, int, long);
     85  1.1  macallan static void vcons_copyrows(void *, int, int, int);
     86  1.1  macallan static void vcons_eraserows(void *, int, int, long);
     87  1.1  macallan static void vcons_putchar(void *, int, int, u_int, long);
     88  1.1  macallan static void vcons_cursor(void *, int, int, int);
     89  1.1  macallan 
     90  1.1  macallan /* support for readin/writing text buffers. For wsmoused */
     91  1.6      jmmv static int  vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
     92  1.6      jmmv static int  vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
     93  1.1  macallan 
     94  1.1  macallan static void vcons_lock(struct vcons_screen *);
     95  1.1  macallan static void vcons_unlock(struct vcons_screen *);
     96  1.1  macallan 
     97  1.1  macallan 
     98  1.1  macallan int
     99  1.1  macallan vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
    100  1.1  macallan     struct wsdisplay_accessops *ao)
    101  1.1  macallan {
    102  1.2  macallan 
    103  1.2  macallan 	/* zero out everything so we can rely on untouched fields being 0 */
    104  1.3  macallan 	memset(vd, 0, sizeof(struct vcons_data));
    105  1.2  macallan 
    106  1.1  macallan 	vd->cookie = cookie;
    107  1.1  macallan 
    108  1.1  macallan 	vd->init_screen = vcons_dummy_init_screen;
    109  1.1  macallan 	vd->show_screen_cb = NULL;
    110  1.1  macallan 
    111  1.6      jmmv 	/* keep a copy of the accessops that we replace below with our
    112  1.6      jmmv 	 * own wrappers */
    113  1.6      jmmv 	vd->ioctl = ao->ioctl;
    114  1.6      jmmv 
    115  1.6      jmmv 	/* configure the accessops */
    116  1.6      jmmv 	ao->ioctl = vcons_ioctl;
    117  1.1  macallan 	ao->alloc_screen = vcons_alloc_screen;
    118  1.1  macallan 	ao->free_screen = vcons_free_screen;
    119  1.1  macallan 	ao->show_screen = vcons_show_screen;
    120  1.1  macallan 
    121  1.1  macallan 	LIST_INIT(&vd->screens);
    122  1.1  macallan 	vd->active = NULL;
    123  1.1  macallan 	vd->wanted = NULL;
    124  1.1  macallan 	vd->currenttype = def;
    125  1.1  macallan 	callout_init(&vd->switch_callout);
    126  1.1  macallan 
    127  1.1  macallan 	/*
    128  1.1  macallan 	 * a lock to serialize access to the framebuffer.
    129  1.1  macallan 	 * when switching screens we need to make sure there's no rasops
    130  1.1  macallan 	 * operation in progress
    131  1.1  macallan 	 */
    132  1.1  macallan #ifdef DIAGNOSTIC
    133  1.1  macallan 	vd->switch_poll_count = 0;
    134  1.1  macallan #endif
    135  1.1  macallan 	return 0;
    136  1.1  macallan }
    137  1.1  macallan 
    138  1.1  macallan static void
    139  1.1  macallan vcons_lock(struct vcons_screen *scr)
    140  1.1  macallan {
    141  1.1  macallan #ifdef VCONS_PARANOIA
    142  1.1  macallan 	int s;
    143  1.1  macallan 
    144  1.1  macallan 	s = splhigh();
    145  1.1  macallan #endif
    146  1.1  macallan 	SCREEN_BUSY(scr);
    147  1.1  macallan #ifdef VCONS_PARANOIA
    148  1.1  macallan 	splx(s);
    149  1.1  macallan #endif
    150  1.1  macallan }
    151  1.1  macallan 
    152  1.1  macallan static void
    153  1.1  macallan vcons_unlock(struct vcons_screen *scr)
    154  1.1  macallan {
    155  1.1  macallan #ifdef VCONS_PARANOIA
    156  1.1  macallan 	int s;
    157  1.1  macallan 
    158  1.1  macallan 	s = splhigh();
    159  1.1  macallan #endif
    160  1.1  macallan 	SCREEN_IDLE(scr);
    161  1.1  macallan #ifdef VCONS_PARANOIA
    162  1.1  macallan 	splx(s);
    163  1.1  macallan #endif
    164  1.1  macallan }
    165  1.1  macallan 
    166  1.1  macallan static void
    167  1.9  christos vcons_dummy_init_screen(void *cookie,
    168  1.9  christos     struct vcons_screen *scr, int exists,
    169  1.9  christos     long *defattr)
    170  1.1  macallan {
    171  1.1  macallan 
    172  1.1  macallan 	/*
    173  1.1  macallan 	 * default init_screen() method.
    174  1.1  macallan 	 * Needs to be overwritten so we bitch and whine in case anyone ends
    175  1.1  macallan 	 * up in here.
    176  1.1  macallan 	 */
    177  1.1  macallan 	printf("vcons_init_screen: dummy function called. Your driver is "
    178  1.1  macallan 	       "supposed to supply a replacement for proper operation\n");
    179  1.1  macallan }
    180  1.1  macallan 
    181  1.1  macallan int
    182  1.1  macallan vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
    183  1.1  macallan     int existing, long *defattr)
    184  1.1  macallan {
    185  1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    186  1.1  macallan 	int cnt, i;
    187  1.1  macallan 
    188  1.1  macallan 	scr->scr_cookie = vd->cookie;
    189  1.4  jmcneill 	scr->scr_vd = scr->scr_origvd = vd;
    190  1.2  macallan 	SCREEN_IDLE(scr);
    191  1.2  macallan 
    192  1.1  macallan 	/*
    193  1.1  macallan 	 * call the driver-supplied init_screen function which is expected
    194  1.1  macallan 	 * to set up rasops_info, override cursor() and probably others
    195  1.1  macallan 	 */
    196  1.1  macallan 	vd->init_screen(vd->cookie, scr, existing, defattr);
    197  1.1  macallan 
    198  1.1  macallan 	/*
    199  1.1  macallan 	 * save the non virtual console aware rasops and replace them with
    200  1.1  macallan 	 * our wrappers
    201  1.1  macallan 	 */
    202  1.1  macallan 	vd->eraserows = ri->ri_ops.eraserows;
    203  1.1  macallan 	vd->copyrows  = ri->ri_ops.copyrows;
    204  1.1  macallan 	vd->erasecols = ri->ri_ops.erasecols;
    205  1.1  macallan 	vd->copycols  = ri->ri_ops.copycols;
    206  1.1  macallan 	vd->putchar   = ri->ri_ops.putchar;
    207  1.1  macallan 	vd->cursor    = ri->ri_ops.cursor;
    208  1.1  macallan 
    209  1.1  macallan 	ri->ri_ops.eraserows = vcons_eraserows;
    210  1.1  macallan 	ri->ri_ops.copyrows  = vcons_copyrows;
    211  1.1  macallan 	ri->ri_ops.erasecols = vcons_erasecols;
    212  1.1  macallan 	ri->ri_ops.copycols  = vcons_copycols;
    213  1.1  macallan 	ri->ri_ops.putchar   = vcons_putchar;
    214  1.1  macallan 	ri->ri_ops.cursor    = vcons_cursor;
    215  1.1  macallan 	ri->ri_hw = scr;
    216  1.1  macallan 
    217  1.1  macallan 	/*
    218  1.1  macallan 	 * we allocate both chars and attributes in one chunk, attributes first
    219  1.1  macallan 	 * because they have the (potentially) bigger alignment
    220  1.1  macallan 	 */
    221  1.1  macallan 	cnt = ri->ri_rows * ri->ri_cols;
    222  1.1  macallan 	scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
    223  1.1  macallan 	    sizeof(uint16_t)), M_DEVBUF, M_WAITOK);
    224  1.1  macallan 	if (scr->scr_attrs == NULL)
    225  1.1  macallan 		return ENOMEM;
    226  1.1  macallan 
    227  1.1  macallan 	scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt];
    228  1.1  macallan 
    229  1.1  macallan 	ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    230  1.1  macallan 	scr->scr_defattr = *defattr;
    231  1.1  macallan 
    232  1.1  macallan 	/*
    233  1.1  macallan 	 * fill the attribute buffer with *defattr, chars with 0x20
    234  1.1  macallan 	 * since we don't know if the driver tries to mimic firmware output or
    235  1.1  macallan 	 * reset everything we do nothing to VRAM here, any driver that feels
    236  1.1  macallan 	 * the need to clear screen or something will have to do it on its own
    237  1.1  macallan 	 * Additional screens will start out in the background anyway so
    238  1.1  macallan 	 * cleaning or not only really affects the initial console screen
    239  1.1  macallan 	 */
    240  1.1  macallan 	for (i = 0; i < cnt; i++) {
    241  1.1  macallan 		scr->scr_attrs[i] = *defattr;
    242  1.1  macallan 		scr->scr_chars[i] = 0x20;
    243  1.1  macallan 	}
    244  1.1  macallan 
    245  1.2  macallan 	if(vd->active == NULL) {
    246  1.2  macallan 		vd->active = scr;
    247  1.2  macallan 		SCREEN_VISIBLE(scr);
    248  1.2  macallan 	}
    249  1.2  macallan 
    250  1.1  macallan 	if (existing) {
    251  1.2  macallan 		SCREEN_VISIBLE(scr);
    252  1.2  macallan 		vd->active = scr;
    253  1.1  macallan 	} else {
    254  1.2  macallan 		SCREEN_INVISIBLE(scr);
    255  1.1  macallan 	}
    256  1.1  macallan 
    257  1.1  macallan 	LIST_INSERT_HEAD(&vd->screens, scr, next);
    258  1.1  macallan 	return 0;
    259  1.1  macallan }
    260  1.1  macallan 
    261  1.1  macallan static void
    262  1.1  macallan vcons_do_switch(struct vcons_data *vd)
    263  1.1  macallan {
    264  1.1  macallan 	struct vcons_screen *scr, *oldscr;
    265  1.1  macallan 
    266  1.1  macallan 	scr = vd->wanted;
    267  1.1  macallan 	if (!scr) {
    268  1.1  macallan 		printf("vcons_switch_screen: disappeared\n");
    269  1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, EIO, 0);
    270  1.1  macallan 		return;
    271  1.1  macallan 	}
    272  1.1  macallan 	oldscr = vd->active; /* can be NULL! */
    273  1.1  macallan 
    274  1.1  macallan 	/*
    275  1.1  macallan 	 * if there's an old, visible screen we mark it invisible and wait
    276  1.1  macallan 	 * until it's not busy so we can safely switch
    277  1.1  macallan 	 */
    278  1.1  macallan 	if (oldscr != NULL) {
    279  1.1  macallan 		SCREEN_INVISIBLE(oldscr);
    280  1.1  macallan 		if (SCREEN_IS_BUSY(oldscr)) {
    281  1.1  macallan 			callout_reset(&vd->switch_callout, 1,
    282  1.1  macallan 			    (void(*)(void *))vcons_do_switch, vd);
    283  1.1  macallan #ifdef DIAGNOSTIC
    284  1.1  macallan 			/* bitch if we wait too long */
    285  1.1  macallan 			vd->switch_poll_count++;
    286  1.1  macallan 			if (vd->switch_poll_count > 100) {
    287  1.1  macallan 				panic("vcons: screen still busy");
    288  1.1  macallan 			}
    289  1.1  macallan #endif
    290  1.1  macallan 			return;
    291  1.1  macallan 		}
    292  1.1  macallan 		/* invisible screen -> no visible cursor image */
    293  1.1  macallan 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
    294  1.1  macallan #ifdef DIAGNOSTIC
    295  1.1  macallan 		vd->switch_poll_count = 0;
    296  1.1  macallan #endif
    297  1.1  macallan 	}
    298  1.1  macallan 
    299  1.1  macallan 	if (scr == oldscr)
    300  1.1  macallan 		return;
    301  1.1  macallan 
    302  1.1  macallan #ifdef DIAGNOSTIC
    303  1.1  macallan 	if (SCREEN_IS_VISIBLE(scr))
    304  1.1  macallan 		panic("vcons_switch_screen: already active");
    305  1.1  macallan #endif
    306  1.1  macallan 
    307  1.1  macallan #ifdef notyet
    308  1.1  macallan 	if (vd->currenttype != type) {
    309  1.1  macallan 		vcons_set_screentype(vd, type);
    310  1.1  macallan 		vd->currenttype = type;
    311  1.1  macallan 	}
    312  1.1  macallan #endif
    313  1.1  macallan 
    314  1.1  macallan 	SCREEN_VISIBLE(scr);
    315  1.1  macallan 	vd->active = scr;
    316  1.1  macallan 	vd->wanted = NULL;
    317  1.1  macallan 
    318  1.1  macallan 	if (vd->show_screen_cb != NULL)
    319  1.1  macallan 		vd->show_screen_cb(scr);
    320  1.1  macallan 
    321  1.1  macallan 	if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
    322  1.1  macallan 		vcons_redraw_screen(scr);
    323  1.1  macallan 
    324  1.1  macallan 	if (vd->switch_cb)
    325  1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, 0, 0);
    326  1.1  macallan }
    327  1.1  macallan 
    328  1.1  macallan void
    329  1.1  macallan vcons_redraw_screen(struct vcons_screen *scr)
    330  1.1  macallan {
    331  1.1  macallan 	uint16_t *charptr = scr->scr_chars;
    332  1.1  macallan 	long *attrptr = scr->scr_attrs;
    333  1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    334  1.1  macallan 	int i, j, offset;
    335  1.1  macallan 
    336  1.1  macallan 	vcons_lock(scr);
    337  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    338  1.1  macallan 
    339  1.1  macallan 		/*
    340  1.1  macallan 		 * only clear the screen when RI_FULLCLEAR is set since we're
    341  1.1  macallan 		 * going to overwrite every single character cell anyway
    342  1.1  macallan 		 */
    343  1.1  macallan 		if (ri->ri_flg & RI_FULLCLEAR) {
    344  1.1  macallan 			scr->scr_vd->eraserows(ri, 0, ri->ri_rows,
    345  1.1  macallan 			    scr->scr_defattr);
    346  1.1  macallan 		}
    347  1.1  macallan 
    348  1.1  macallan 		/* redraw the screen */
    349  1.1  macallan 		offset = 0;
    350  1.1  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    351  1.1  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    352  1.1  macallan 				/*
    353  1.1  macallan 				 * no need to use the wrapper function - we
    354  1.1  macallan 				 * don't change any characters or attributes
    355  1.1  macallan 				 * and we already made sure the screen we're
    356  1.1  macallan 				 * working on is visible
    357  1.1  macallan 				 */
    358  1.1  macallan 				scr->scr_vd->putchar(ri, i, j,
    359  1.1  macallan 				    charptr[offset], attrptr[offset]);
    360  1.1  macallan 				offset++;
    361  1.1  macallan 			}
    362  1.1  macallan 		}
    363  1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    364  1.1  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    365  1.1  macallan 	}
    366  1.1  macallan 	vcons_unlock(scr);
    367  1.1  macallan }
    368  1.1  macallan 
    369  1.1  macallan static int
    370  1.6      jmmv vcons_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
    371  1.6      jmmv 	struct lwp *l)
    372  1.6      jmmv {
    373  1.6      jmmv 	struct vcons_data *vd = v;
    374  1.6      jmmv 	int error;
    375  1.6      jmmv 
    376  1.6      jmmv 	switch (cmd) {
    377  1.6      jmmv 	case WSDISPLAYIO_GETWSCHAR:
    378  1.6      jmmv 		error = vcons_getwschar((struct vcons_screen *)vs,
    379  1.6      jmmv 			(struct wsdisplay_char *)data);
    380  1.6      jmmv 		break;
    381  1.6      jmmv 
    382  1.6      jmmv 	case WSDISPLAYIO_PUTWSCHAR:
    383  1.6      jmmv 		error = vcons_putwschar((struct vcons_screen *)vs,
    384  1.6      jmmv 			(struct wsdisplay_char *)data);
    385  1.6      jmmv 		break;
    386  1.6      jmmv 
    387  1.6      jmmv 	default:
    388  1.6      jmmv 		if (vd->ioctl != NULL)
    389  1.6      jmmv 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    390  1.6      jmmv 		else
    391  1.6      jmmv 			error = EPASSTHROUGH;
    392  1.6      jmmv 	}
    393  1.6      jmmv 
    394  1.6      jmmv 	return error;
    395  1.6      jmmv }
    396  1.6      jmmv 
    397  1.6      jmmv static int
    398  1.1  macallan vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    399  1.1  macallan     int *curxp, int *curyp, long *defattrp)
    400  1.1  macallan {
    401  1.1  macallan 	struct vcons_data *vd = v;
    402  1.1  macallan 	struct vcons_screen *scr;
    403  1.1  macallan 	int ret;
    404  1.1  macallan 
    405  1.1  macallan 	scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
    406  1.1  macallan 	if (scr == NULL)
    407  1.1  macallan 		return ENOMEM;
    408  1.1  macallan 
    409  1.1  macallan 	scr->scr_flags = 0;
    410  1.2  macallan 	scr->scr_status = 0;
    411  1.2  macallan 	scr->scr_busy = 0;
    412  1.1  macallan 	scr->scr_type = type;
    413  1.1  macallan 
    414  1.1  macallan 	ret = vcons_init_screen(vd, scr, 0, defattrp);
    415  1.1  macallan 	if (ret != 0) {
    416  1.1  macallan 		free(scr, M_DEVBUF);
    417  1.1  macallan 		return ret;
    418  1.1  macallan 	}
    419  1.1  macallan 
    420  1.1  macallan 	if (vd->active == NULL) {
    421  1.1  macallan 		SCREEN_VISIBLE(scr);
    422  1.1  macallan 		vd->active = scr;
    423  1.1  macallan 		vd->currenttype = type;
    424  1.1  macallan 	}
    425  1.1  macallan 
    426  1.1  macallan 	*cookiep = scr;
    427  1.1  macallan 	*curxp = scr->scr_ri.ri_ccol;
    428  1.1  macallan 	*curyp = scr->scr_ri.ri_crow;
    429  1.1  macallan 	return 0;
    430  1.1  macallan }
    431  1.1  macallan 
    432  1.1  macallan static void
    433  1.1  macallan vcons_free_screen(void *v, void *cookie)
    434  1.1  macallan {
    435  1.1  macallan 	struct vcons_data *vd = v;
    436  1.1  macallan 	struct vcons_screen *scr = cookie;
    437  1.1  macallan 
    438  1.1  macallan 	vcons_lock(scr);
    439  1.1  macallan 	/* there should be no rasops activity here */
    440  1.1  macallan 
    441  1.1  macallan 	LIST_REMOVE(scr, next);
    442  1.1  macallan 
    443  1.1  macallan 	if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
    444  1.1  macallan 		free(scr->scr_attrs, M_DEVBUF);
    445  1.1  macallan 		free(scr, M_DEVBUF);
    446  1.1  macallan 	} else {
    447  1.1  macallan 		/*
    448  1.1  macallan 		 * maybe we should just restore the old rasops_info methods
    449  1.1  macallan 		 * and free the character/attribute buffer here?
    450  1.1  macallan 		 */
    451  1.1  macallan #ifdef VCONS_DEBUG
    452  1.1  macallan 		panic("vcons_free_screen: console");
    453  1.1  macallan #else
    454  1.1  macallan 		printf("vcons_free_screen: console\n");
    455  1.1  macallan #endif
    456  1.1  macallan 	}
    457  1.1  macallan 
    458  1.1  macallan 	if (vd->active == scr)
    459  1.1  macallan 		vd->active = NULL;
    460  1.1  macallan }
    461  1.1  macallan 
    462  1.1  macallan static int
    463  1.9  christos vcons_show_screen(void *v, void *cookie, int waitok,
    464  1.1  macallan     void (*cb)(void *, int, int), void *cb_arg)
    465  1.1  macallan {
    466  1.1  macallan 	struct vcons_data *vd = v;
    467  1.1  macallan 	struct vcons_screen *scr;
    468  1.1  macallan 
    469  1.1  macallan 	scr = cookie;
    470  1.1  macallan 	if (scr == vd->active)
    471  1.1  macallan 		return 0;
    472  1.1  macallan 
    473  1.1  macallan 	vd->wanted = scr;
    474  1.1  macallan 	vd->switch_cb = cb;
    475  1.1  macallan 	vd->switch_cb_arg = cb_arg;
    476  1.1  macallan 	if (cb) {
    477  1.1  macallan 		callout_reset(&vd->switch_callout, 0,
    478  1.1  macallan 		    (void(*)(void *))vcons_do_switch, vd);
    479  1.1  macallan 		return EAGAIN;
    480  1.1  macallan 	}
    481  1.1  macallan 
    482  1.1  macallan 	vcons_do_switch(vd);
    483  1.1  macallan 	return 0;
    484  1.1  macallan }
    485  1.1  macallan 
    486  1.1  macallan /* wrappers for rasops_info methods */
    487  1.1  macallan 
    488  1.1  macallan static void
    489  1.1  macallan vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
    490  1.1  macallan {
    491  1.1  macallan 	struct rasops_info *ri = cookie;
    492  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    493  1.1  macallan 	int from = srccol + row * ri->ri_cols;
    494  1.1  macallan 	int to = dstcol + row * ri->ri_cols;
    495  1.1  macallan 
    496  1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    497  1.1  macallan 	    ncols * sizeof(long));
    498  1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    499  1.1  macallan 	    ncols * sizeof(uint16_t));
    500  1.1  macallan }
    501  1.1  macallan 
    502  1.1  macallan static void
    503  1.1  macallan vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    504  1.1  macallan {
    505  1.1  macallan 	struct rasops_info *ri = cookie;
    506  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    507  1.1  macallan 
    508  1.1  macallan 	vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
    509  1.1  macallan 
    510  1.1  macallan 	vcons_lock(scr);
    511  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    512  1.1  macallan 		scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
    513  1.1  macallan 	}
    514  1.1  macallan 	vcons_unlock(scr);
    515  1.1  macallan }
    516  1.1  macallan 
    517  1.1  macallan static void
    518  1.1  macallan vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
    519  1.1  macallan {
    520  1.1  macallan 	struct rasops_info *ri = cookie;
    521  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    522  1.1  macallan 	int start = startcol + row * ri->ri_cols;
    523  1.1  macallan 	int end = start + ncols, i;
    524  1.1  macallan 
    525  1.1  macallan 	for (i = start; i < end; i++) {
    526  1.1  macallan 		scr->scr_attrs[i] = fillattr;
    527  1.1  macallan 		scr->scr_chars[i] = 0x20;
    528  1.1  macallan 	}
    529  1.1  macallan }
    530  1.1  macallan 
    531  1.1  macallan static void
    532  1.1  macallan vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    533  1.1  macallan {
    534  1.1  macallan 	struct rasops_info *ri = cookie;
    535  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    536  1.1  macallan 
    537  1.1  macallan 	vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
    538  1.1  macallan 
    539  1.1  macallan 	vcons_lock(scr);
    540  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    541  1.1  macallan 		scr->scr_vd->erasecols(cookie, row, startcol, ncols,
    542  1.1  macallan 		    fillattr);
    543  1.1  macallan 	}
    544  1.1  macallan 	vcons_unlock(scr);
    545  1.1  macallan }
    546  1.1  macallan 
    547  1.1  macallan static void
    548  1.1  macallan vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
    549  1.1  macallan {
    550  1.1  macallan 	struct rasops_info *ri = cookie;
    551  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    552  1.1  macallan 	int from, to, len;
    553  1.1  macallan 
    554  1.1  macallan 	from = ri->ri_cols * srcrow;
    555  1.1  macallan 	to = ri->ri_cols * dstrow;
    556  1.1  macallan 	len = ri->ri_cols * nrows;
    557  1.1  macallan 
    558  1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    559  1.1  macallan 	    len * sizeof(long));
    560  1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    561  1.1  macallan 	    len * sizeof(uint16_t));
    562  1.1  macallan }
    563  1.1  macallan 
    564  1.1  macallan static void
    565  1.1  macallan vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    566  1.1  macallan {
    567  1.1  macallan 	struct rasops_info *ri = cookie;
    568  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    569  1.1  macallan 
    570  1.1  macallan 	vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
    571  1.1  macallan 
    572  1.1  macallan 	vcons_lock(scr);
    573  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    574  1.1  macallan 		scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
    575  1.1  macallan 	}
    576  1.1  macallan 	vcons_unlock(scr);
    577  1.1  macallan }
    578  1.1  macallan 
    579  1.1  macallan static void
    580  1.1  macallan vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
    581  1.1  macallan {
    582  1.1  macallan 	struct rasops_info *ri = cookie;
    583  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    584  1.1  macallan 	int start, end, i;
    585  1.1  macallan 
    586  1.1  macallan 	start = ri->ri_cols * row;
    587  1.1  macallan 	end = ri->ri_cols * (row + nrows);
    588  1.1  macallan 
    589  1.1  macallan 	for (i = start; i < end; i++) {
    590  1.1  macallan 		scr->scr_attrs[i] = fillattr;
    591  1.1  macallan 		scr->scr_chars[i] = 0x20;
    592  1.1  macallan 	}
    593  1.1  macallan }
    594  1.1  macallan 
    595  1.1  macallan static void
    596  1.1  macallan vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
    597  1.1  macallan {
    598  1.1  macallan 	struct rasops_info *ri = cookie;
    599  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    600  1.1  macallan 
    601  1.1  macallan 	vcons_eraserows_buffer(cookie, row, nrows, fillattr);
    602  1.1  macallan 
    603  1.1  macallan 	vcons_lock(scr);
    604  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    605  1.1  macallan 		scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
    606  1.1  macallan 	}
    607  1.1  macallan 	vcons_unlock(scr);
    608  1.1  macallan }
    609  1.1  macallan 
    610  1.1  macallan static void
    611  1.1  macallan vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
    612  1.1  macallan {
    613  1.1  macallan 	struct rasops_info *ri = cookie;
    614  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    615  1.1  macallan 	int pos;
    616  1.1  macallan 
    617  1.1  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    618  1.1  macallan 	     (col < ri->ri_cols)) {
    619  1.1  macallan 		pos = col + row * ri->ri_cols;
    620  1.1  macallan 		scr->scr_attrs[pos] = attr;
    621  1.1  macallan 		scr->scr_chars[pos] = c;
    622  1.1  macallan 	}
    623  1.1  macallan }
    624  1.1  macallan 
    625  1.1  macallan static void
    626  1.1  macallan vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
    627  1.1  macallan {
    628  1.1  macallan 	struct rasops_info *ri = cookie;
    629  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    630  1.1  macallan 
    631  1.1  macallan 	vcons_putchar_buffer(cookie, row, col, c, attr);
    632  1.1  macallan 
    633  1.1  macallan 	vcons_lock(scr);
    634  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    635  1.1  macallan 		scr->scr_vd->putchar(cookie, row, col, c, attr);
    636  1.1  macallan 	}
    637  1.1  macallan 	vcons_unlock(scr);
    638  1.1  macallan }
    639  1.1  macallan 
    640  1.1  macallan static void
    641  1.1  macallan vcons_cursor(void *cookie, int on, int row, int col)
    642  1.1  macallan {
    643  1.1  macallan 	struct rasops_info *ri = cookie;
    644  1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    645  1.1  macallan 
    646  1.1  macallan 	vcons_lock(scr);
    647  1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    648  1.1  macallan 		scr->scr_vd->cursor(cookie, on, row, col);
    649  1.1  macallan 	} else {
    650  1.1  macallan 		scr->scr_ri.ri_crow = row;
    651  1.1  macallan 		scr->scr_ri.ri_ccol = col;
    652  1.1  macallan 	}
    653  1.1  macallan 	vcons_unlock(scr);
    654  1.1  macallan }
    655  1.1  macallan 
    656  1.1  macallan /* methods to read/write characters via ioctl() */
    657  1.1  macallan 
    658  1.1  macallan static int
    659  1.6      jmmv vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
    660  1.1  macallan {
    661  1.1  macallan 	long attr;
    662  1.6      jmmv 	struct rasops_info *ri;
    663  1.6      jmmv 
    664  1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
    665  1.6      jmmv 
    666  1.6      jmmv 	ri = &scr->scr_ri;
    667  1.1  macallan 
    668  1.1  macallan 	ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
    669  1.1  macallan 	    wsc->flags, &attr);
    670  1.1  macallan 	vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
    671  1.1  macallan 	return 0;
    672  1.1  macallan }
    673  1.1  macallan 
    674  1.1  macallan static int
    675  1.6      jmmv vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
    676  1.1  macallan {
    677  1.6      jmmv 	int offset;
    678  1.1  macallan 	long attr;
    679  1.6      jmmv 	struct rasops_info *ri;
    680  1.6      jmmv 
    681  1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
    682  1.6      jmmv 
    683  1.6      jmmv 	ri = &scr->scr_ri;
    684  1.6      jmmv 	offset = ri->ri_cols * wsc->row + wsc->col;
    685  1.1  macallan 	wsc->letter = scr->scr_chars[offset];
    686  1.1  macallan 	attr = scr->scr_attrs[offset];
    687  1.1  macallan 
    688  1.1  macallan 	/*
    689  1.1  macallan 	 * this is ugly. We need to break up an attribute into colours and
    690  1.1  macallan 	 * flags but there's no rasops method to do that so we must rely on
    691  1.1  macallan 	 * the 'canonical' encoding.
    692  1.1  macallan 	 */
    693  1.1  macallan 	wsc->foreground = (attr & 0xff000000) >> 24;
    694  1.1  macallan 	wsc->background = (attr & 0x00ff0000) >> 16;
    695  1.1  macallan 	wsc->flags      = (attr & 0x0000ff00) >> 8;
    696  1.1  macallan 	return 0;
    697  1.1  macallan }
    698