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