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