Home | History | Annotate | Line # | Download | only in dev
view.c revision 1.3
      1 /*
      2  * Copyright (c) 1994 Christian E. Hopps
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *      This product includes software developed by Christian E. Hopps.
     16  * 4. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  *
     30  *	$Id: view.c,v 1.3 1994/01/30 08:28:17 chopps Exp $
     31  */
     32 
     33 /* The view major device is a placeholder device.  It serves
     34  * simply to map the semantics of a graphics dipslay to
     35  * the semantics of a character block device.  In other
     36  * words the graphics system as currently built does not like to be
     37  * refered to by open/close/ioctl.  This device serves as
     38  * a interface to graphics. */
     39 
     40 #include "param.h"
     41 #include "proc.h"
     42 #include "ioctl.h"
     43 #include "file.h"
     44 #include "malloc.h"
     45 
     46 #include "device.h"
     47 
     48 #include "machine/cpu.h"
     49 
     50 #include "vm/vm.h"
     51 #include "vm/vm_kern.h"
     52 #include "vm/vm_page.h"
     53 #include "vm/vm_pager.h"
     54 
     55 #include "miscfs/specfs/specdev.h"
     56 #include "vnode.h"
     57 #include "mman.h"
     58 
     59 #include "grf/grf_types.h"
     60 #include "grf/grf_bitmap.h"
     61 #include "grf/grf_view.h"
     62 #include "grf/grf_mode.h"
     63 #include "grf/grf_monitor.h"
     64 #include "viewioctl.h"
     65 #include "viewvar.h"
     66 #include "view.h"
     67 
     68 int viewprobe ();
     69 
     70 /* not currently used as independant device */
     71 struct driver view_driver = { viewprobe, "view" };
     72 
     73 struct view_softc views[NVIEW];
     74 static int inited;
     75 
     76 int view_default_x;
     77 int view_default_y;
     78 int view_default_width = 640;
     79 int view_default_height = 400;
     80 int view_default_depth = 2;
     81 
     82 /*
     83  *  functions for probeing.
     84  */
     85 
     86 /* this function is called early to set up a display. */
     87 viewconfig ()
     88 {
     89     viewprobe ();
     90 }
     91 
     92 viewprobe ()
     93 {
     94     if (!inited) {
     95     	int i;
     96 
     97     	inited = 1;
     98     	for (i=0; i<NVIEW; i++) {
     99     	    views[i].view = NULL;
    100     	    views[i].flags = 0;
    101     	}
    102     }
    103     return (1);
    104 }
    105 
    106 
    107 /*
    108  *  Internal functions.
    109  */
    110 
    111 static void
    112 view_display (struct view_softc *vu)
    113 {
    114     int s = spltty ();
    115     int i;
    116 
    117     if (vu == NULL) {
    118     	return;
    119     }
    120     /* mark views that share this monitor as not displaying */
    121     for (i=0; i<NVIEW; i++) {
    122         if (views[i].flags&VUF_DISPLAY && views[i].monitor == vu->monitor) {
    123             views[i].flags &= ~VUF_DISPLAY;
    124         }
    125     }
    126 
    127     vu->flags |= VUF_ADDED;
    128     if (vu->view) {
    129 	vu->view->display.x = vu->size.x;
    130 	vu->view->display.y = vu->size.y;
    131 	grf_display_view (vu->view);
    132 	vu->size.x = vu->view->display.x;
    133 	vu->size.y = vu->view->display.y;
    134 	vu->flags |= VUF_DISPLAY;
    135     }
    136 
    137     splx (s);
    138 }
    139 
    140 /* remove a view from our added list if it is marked as displaying */
    141 /* switch to a new display. */
    142 static void
    143 view_remove (struct view_softc *vu)
    144 {
    145     int i;
    146 
    147     if (!(vu->flags & VUF_ADDED)) {
    148     	return;
    149     }
    150 
    151     vu->flags &= ~VUF_ADDED;
    152     if (vu->flags & VUF_DISPLAY) {
    153 	for (i=0; i<NVIEW; i++) {
    154 	    if (views[i].flags & VUF_ADDED && &views[i] != vu &&
    155 	        views[i].monitor == vu->monitor) {
    156 		view_display (&views[i]);
    157 		break;
    158 	    }
    159     	}
    160     }
    161     vu->flags &= ~VUF_DISPLAY;
    162     grf_remove_view (vu->view);
    163 }
    164 
    165 static int
    166 view_setsize (struct view_softc *vu, struct view_size *vs)
    167 {
    168     view_t *new, *old;
    169     dimen_t ns;
    170     int co = 0, cs = 0;
    171 
    172     if (vs->x != vu->size.x ||
    173 	vs->y != vu->size.y) {
    174 	co = 1;
    175     }
    176 
    177     if (vs->width != vu->size.width ||
    178 	vs->height != vu->size.height ||
    179 	vs->depth != vu->size.depth) {
    180 	cs = 1;
    181     }
    182 
    183     if (!cs && !co) {
    184 	/* no change. */
    185 	return (0);
    186     }
    187 
    188     ns.width = vs->width;
    189     ns.height = vs->height;
    190 
    191     new = grf_alloc_view (NULL, &ns, vs->depth);
    192     if (!new) {
    193 	return (ENOMEM);	/* check this error. */
    194     }
    195 
    196     old = vu->view;
    197     vu->view = new;
    198 
    199     vu->size.x = new->display.x;
    200     vu->size.y = new->display.y;
    201     vu->size.width = new->display.width;
    202     vu->size.height = new->display.height;
    203     vu->size.depth = new->bitmap->depth;
    204 
    205     vu->mode = grf_get_display_mode (vu->view);
    206     vu->monitor = grf_get_monitor (vu->mode);
    207 
    208     vu->size.x = vs->x;
    209     vu->size.y = vs->y;
    210 
    211     /* we are all set to go everything is A-OK. */
    212     /* we need a custom remove here to avoid letting another view display */
    213     /* mark as not added or displayed */
    214     if (vu->flags & VUF_DISPLAY) {
    215 	vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
    216 
    217 	/* change view pointers */
    218 
    219 	/* display new view first */
    220 	view_display (vu);
    221     }
    222 
    223     /* remove old view from monitor */
    224     grf_free_view (old);
    225 
    226     return (0);
    227 }
    228 
    229 /*
    230  *  functions made available by conf.c
    231  */
    232 
    233 /*ARGSUSED*/
    234 viewopen (dev, flags)
    235   dev_t dev;
    236 {
    237     dimen_t size;
    238     struct view_softc *vu = &views[minor(dev)];
    239 
    240     if (minor(dev) >= NVIEW)
    241         return EXDEV;
    242 
    243     if (vu->flags & VUF_OPEN) {
    244         return (EBUSY);
    245     }
    246 
    247     vu->size.x = view_default_x;
    248     vu->size.y = view_default_y;
    249     size.width = vu->size.width = view_default_width;
    250     size.height = vu->size.height = view_default_height;
    251     vu->size.depth = view_default_depth;
    252 
    253     vu->view = grf_alloc_view (NULL, &size, vu->size.depth);
    254     if (vu->view) {
    255 	vu->size.x = vu->view->display.x;
    256 	vu->size.y = vu->view->display.y;
    257 	vu->size.width = vu->view->display.width;
    258 	vu->size.height = vu->view->display.height;
    259 	vu->size.depth = vu->view->bitmap->depth;
    260        	vu->flags |= VUF_OPEN;
    261     	vu->mode = grf_get_display_mode (vu->view);
    262     	vu->monitor = grf_get_monitor (vu->mode);
    263        	return (0);
    264     }
    265     return (ENOMEM);
    266 }
    267 
    268 /*ARGSUSED*/
    269 viewclose (dev, flags)
    270   dev_t dev;
    271 {
    272     struct view_softc *vu = &views[minor(dev)];
    273 
    274     if (vu->flags & VUF_OPEN) {
    275 	view_remove (vu);
    276 	grf_free_view (vu->view);
    277 	vu->flags = 0;
    278 	vu->view = NULL;
    279 	vu->mode = NULL;
    280 	vu->monitor = NULL;
    281     }
    282 }
    283 
    284 
    285 /*ARGSUSED*/
    286 viewioctl (dev, cmd, data, flag, p)
    287   dev_t dev;
    288   caddr_t data;
    289   struct proc *p;
    290 {
    291     bmap_t *bm;
    292     colormap_t *cm;
    293     struct view_softc *vu = &views[minor(dev)];
    294     int error = 0;
    295 
    296     switch (cmd) {
    297       case VIEW_DISPLAY:
    298       	view_display (vu);
    299       	break;
    300 
    301       case VIEW_REMOVE:
    302       	view_remove (vu);
    303       	break;
    304 
    305       case VIEW_GETSIZE:
    306         bcopy (&vu->size, data, sizeof (struct view_size));
    307         break;
    308 
    309       case VIEW_SETSIZE:
    310         error = view_setsize (vu, (struct view_size *)data);
    311         break;
    312 
    313       case VIEW_GETBITMAP:
    314         bm = (bmap_t *)data;
    315         bcopy (vu->view->bitmap, bm, sizeof (bmap_t));
    316 	if ((int)p != -1) {
    317 	    bm->plane = 0;
    318 	    bm->blit_temp = 0;
    319 	    bm->hardware_address = 0;
    320 	}
    321         break;
    322       case VIEW_GETCOLORMAP:
    323 	error = view_get_colormap (vu, data);
    324 	break;
    325       case VIEW_USECOLORMAP:
    326 	error = view_use_colormap (vu, data);
    327 	break;
    328       default:
    329 	error = EINVAL;
    330 	break;
    331     }
    332     return (error);
    333 }
    334 
    335 /*ARGSUSED*/
    336 view_get_colormap (vu, ucm)
    337     struct view_softc *vu;
    338     colormap_t *ucm;
    339 {
    340     int error = 0;
    341     u_long *cme = malloc(sizeof (u_long)*(ucm->size + 1),
    342 			    M_IOCTLOPS, M_WAITOK); /* add one incase of zero, ick. */
    343     if (cme) {
    344 	u_long *uep = ucm->entry;
    345 
    346 	ucm->entry = cme;			  /* set entry to out alloc. */
    347 
    348 	if (!vu->view || grf_get_colormap (vu->view, ucm))
    349 	    error = EINVAL;
    350 	else if (copyout (cme, uep, sizeof (u_long)*ucm->size))
    351 	    error = EINVAL;
    352 
    353 	ucm->entry = uep;			  /* set entry back to users. */
    354 	free (cme, M_IOCTLOPS);
    355     } else {
    356 	error = ENOMEM;
    357     }
    358     return (error);
    359 }
    360 
    361 /*ARGSUSED*/
    362 view_use_colormap (vu, ucm)
    363     struct view_softc *vu;
    364     colormap_t *ucm;
    365 {
    366     int error = 0;
    367     colormap_t *cm = malloc(sizeof (u_long)*ucm->size + sizeof (*cm),
    368 			    M_IOCTLOPS, M_WAITOK);
    369     if (cm) {
    370 	bcopy (ucm, cm, sizeof (colormap_t));
    371 	cm->entry = (u_long *) &cm[1];		 /* table directly after. */
    372 	if (copyin (ucm->entry, cm->entry, sizeof (u_long)*ucm->size))
    373 	    error = EINVAL;
    374 	else if (!vu->view || grf_use_colormap (vu->view, cm))
    375 	    error = EINVAL;
    376 	free (cm, M_IOCTLOPS);
    377     } else {
    378 	error = ENOMEM;
    379     }
    380     return (error);
    381 }
    382 
    383 /*ARGSUSED*/
    384 viewmap(dev, off, prot)
    385         dev_t dev;
    386 {
    387     struct view_softc *vu = &views[minor(dev)];
    388     bmap_t *bm = vu->view->bitmap;
    389     u_byte *bmd_start = bm->hardware_address;
    390     u_long  bmd_size = bm->bytes_per_row*bm->rows*bm->depth;
    391 
    392     if (off >= 0 && off < bmd_size) {
    393     	return(((u_int)bmd_start + off) >> PGSHIFT);
    394     }
    395     /* bogus */
    396     return(-1);
    397 }
    398 
    399 /*ARGSUSED*/
    400 viewselect(dev, rw)
    401   dev_t dev;
    402 {
    403     if (rw == FREAD)
    404 	return(0);
    405     return(1);
    406 }
    407 
    408 void
    409 viewattach() {}
    410