view.c revision 1.4 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.4 1994/02/13 21:11:07 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 <sys/param.h>
41 #include <sys/proc.h>
42 #include <sys/ioctl.h>
43 #include <sys/file.h>
44 #include <sys/malloc.h>
45
46 #include <amiga/dev/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 <sys/vnode.h>
57 #include <sys/mman.h>
58
59 #include <amiga/amiga/dlists.h>
60 #include <amiga/dev/grfabs_reg.h>
61 #include <amiga/dev/viewioctl.h>
62 #include <amiga/dev/viewvar.h>
63 #include "view.h"
64
65 int viewprobe ();
66
67 /* not currently used as independant device */
68 struct driver view_driver = { viewprobe, "view" };
69
70 struct view_softc views[NVIEW];
71 static int inited;
72
73 int view_default_x;
74 int view_default_y;
75 int view_default_width = 640;
76 int view_default_height = 400;
77 int view_default_depth = 2;
78
79 /*
80 * functions for probeing.
81 */
82
83 /* this function is called early to set up a display. */
84 viewconfig ()
85 {
86 viewprobe ();
87 }
88
89 viewprobe ()
90 {
91 if (!inited) {
92 int i;
93
94 inited = 1;
95 for (i=0; i<NVIEW; i++) {
96 views[i].view = NULL;
97 views[i].flags = 0;
98 }
99 }
100 return (1);
101 }
102
103
104 /*
105 * Internal functions.
106 */
107
108 static void
109 view_display (struct view_softc *vu)
110 {
111 int s = spltty ();
112 int i;
113
114 if (vu == NULL) {
115 return;
116 }
117 /* mark views that share this monitor as not displaying */
118 for (i=0; i<NVIEW; i++) {
119 if (views[i].flags&VUF_DISPLAY && views[i].monitor == vu->monitor) {
120 views[i].flags &= ~VUF_DISPLAY;
121 }
122 }
123
124 vu->flags |= VUF_ADDED;
125 if (vu->view) {
126 vu->view->display.x = vu->size.x;
127 vu->view->display.y = vu->size.y;
128 grf_display_view (vu->view);
129 vu->size.x = vu->view->display.x;
130 vu->size.y = vu->view->display.y;
131 vu->flags |= VUF_DISPLAY;
132 }
133
134 splx (s);
135 }
136
137 /* remove a view from our added list if it is marked as displaying */
138 /* switch to a new display. */
139 static void
140 view_remove (struct view_softc *vu)
141 {
142 int i;
143
144 if (!(vu->flags & VUF_ADDED)) {
145 return;
146 }
147
148 vu->flags &= ~VUF_ADDED;
149 if (vu->flags & VUF_DISPLAY) {
150 for (i=0; i<NVIEW; i++) {
151 if (views[i].flags & VUF_ADDED && &views[i] != vu &&
152 views[i].monitor == vu->monitor) {
153 view_display (&views[i]);
154 break;
155 }
156 }
157 }
158 vu->flags &= ~VUF_DISPLAY;
159 grf_remove_view (vu->view);
160 }
161
162 static int
163 view_setsize (struct view_softc *vu, struct view_size *vs)
164 {
165 view_t *new, *old;
166 dimen_t ns;
167 int co = 0, cs = 0;
168
169 if (vs->x != vu->size.x ||
170 vs->y != vu->size.y) {
171 co = 1;
172 }
173
174 if (vs->width != vu->size.width ||
175 vs->height != vu->size.height ||
176 vs->depth != vu->size.depth) {
177 cs = 1;
178 }
179
180 if (!cs && !co) {
181 /* no change. */
182 return (0);
183 }
184
185 ns.width = vs->width;
186 ns.height = vs->height;
187
188 new = grf_alloc_view (NULL, &ns, vs->depth);
189 if (!new) {
190 return (ENOMEM); /* check this error. */
191 }
192
193 old = vu->view;
194 vu->view = new;
195
196 vu->size.x = new->display.x;
197 vu->size.y = new->display.y;
198 vu->size.width = new->display.width;
199 vu->size.height = new->display.height;
200 vu->size.depth = new->bitmap->depth;
201
202 vu->mode = grf_get_display_mode (vu->view);
203 vu->monitor = grf_get_monitor (vu->mode);
204
205 vu->size.x = vs->x;
206 vu->size.y = vs->y;
207
208 /* we are all set to go everything is A-OK. */
209 /* we need a custom remove here to avoid letting another view display */
210 /* mark as not added or displayed */
211 if (vu->flags & VUF_DISPLAY) {
212 vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
213
214 /* change view pointers */
215
216 /* display new view first */
217 view_display (vu);
218 }
219
220 /* remove old view from monitor */
221 grf_free_view (old);
222
223 return (0);
224 }
225
226 /*
227 * functions made available by conf.c
228 */
229
230 /*ARGSUSED*/
231 viewopen (dev, flags)
232 dev_t dev;
233 {
234 dimen_t size;
235 struct view_softc *vu = &views[minor(dev)];
236
237 if (minor(dev) >= NVIEW)
238 return EXDEV;
239
240 if (vu->flags & VUF_OPEN) {
241 return (EBUSY);
242 }
243
244 vu->size.x = view_default_x;
245 vu->size.y = view_default_y;
246 size.width = vu->size.width = view_default_width;
247 size.height = vu->size.height = view_default_height;
248 vu->size.depth = view_default_depth;
249
250 vu->view = grf_alloc_view (NULL, &size, vu->size.depth);
251 if (vu->view) {
252 vu->size.x = vu->view->display.x;
253 vu->size.y = vu->view->display.y;
254 vu->size.width = vu->view->display.width;
255 vu->size.height = vu->view->display.height;
256 vu->size.depth = vu->view->bitmap->depth;
257 vu->flags |= VUF_OPEN;
258 vu->mode = grf_get_display_mode (vu->view);
259 vu->monitor = grf_get_monitor (vu->mode);
260 return (0);
261 }
262 return (ENOMEM);
263 }
264
265 /*ARGSUSED*/
266 viewclose (dev, flags)
267 dev_t dev;
268 {
269 struct view_softc *vu = &views[minor(dev)];
270
271 if (vu->flags & VUF_OPEN) {
272 view_remove (vu);
273 grf_free_view (vu->view);
274 vu->flags = 0;
275 vu->view = NULL;
276 vu->mode = NULL;
277 vu->monitor = NULL;
278 }
279 }
280
281
282 /*ARGSUSED*/
283 viewioctl (dev, cmd, data, flag, p)
284 dev_t dev;
285 caddr_t data;
286 struct proc *p;
287 {
288 bmap_t *bm;
289 colormap_t *cm;
290 struct view_softc *vu = &views[minor(dev)];
291 int error = 0;
292
293 switch (cmd) {
294 case VIEW_DISPLAY:
295 view_display (vu);
296 break;
297
298 case VIEW_REMOVE:
299 view_remove (vu);
300 break;
301
302 case VIEW_GETSIZE:
303 bcopy (&vu->size, data, sizeof (struct view_size));
304 break;
305
306 case VIEW_SETSIZE:
307 error = view_setsize (vu, (struct view_size *)data);
308 break;
309
310 case VIEW_GETBITMAP:
311 bm = (bmap_t *)data;
312 bcopy (vu->view->bitmap, bm, sizeof (bmap_t));
313 if ((int)p != -1) {
314 bm->plane = 0;
315 bm->blit_temp = 0;
316 bm->hardware_address = 0;
317 }
318 break;
319 case VIEW_GETCOLORMAP:
320 error = view_get_colormap (vu, data);
321 break;
322 case VIEW_USECOLORMAP:
323 error = view_use_colormap (vu, data);
324 break;
325 default:
326 error = EINVAL;
327 break;
328 }
329 return (error);
330 }
331
332 /*ARGSUSED*/
333 view_get_colormap (vu, ucm)
334 struct view_softc *vu;
335 colormap_t *ucm;
336 {
337 int error = 0;
338 u_long *cme = malloc(sizeof (u_long)*(ucm->size + 1),
339 M_IOCTLOPS, M_WAITOK); /* add one incase of zero, ick. */
340 if (cme) {
341 u_long *uep = ucm->entry;
342
343 ucm->entry = cme; /* set entry to out alloc. */
344
345 if (!vu->view || grf_get_colormap (vu->view, ucm))
346 error = EINVAL;
347 else if (copyout (cme, uep, sizeof (u_long)*ucm->size))
348 error = EINVAL;
349
350 ucm->entry = uep; /* set entry back to users. */
351 free (cme, M_IOCTLOPS);
352 } else {
353 error = ENOMEM;
354 }
355 return (error);
356 }
357
358 /*ARGSUSED*/
359 view_use_colormap (vu, ucm)
360 struct view_softc *vu;
361 colormap_t *ucm;
362 {
363 int error = 0;
364 colormap_t *cm = malloc(sizeof (u_long)*ucm->size + sizeof (*cm),
365 M_IOCTLOPS, M_WAITOK);
366 if (cm) {
367 bcopy (ucm, cm, sizeof (colormap_t));
368 cm->entry = (u_long *) &cm[1]; /* table directly after. */
369 if (copyin (ucm->entry, cm->entry, sizeof (u_long)*ucm->size))
370 error = EINVAL;
371 else if (!vu->view || grf_use_colormap (vu->view, cm))
372 error = EINVAL;
373 free (cm, M_IOCTLOPS);
374 } else {
375 error = ENOMEM;
376 }
377 return (error);
378 }
379
380 /*ARGSUSED*/
381 viewmap(dev, off, prot)
382 dev_t dev;
383 {
384 struct view_softc *vu = &views[minor(dev)];
385 bmap_t *bm = vu->view->bitmap;
386 u_char *bmd_start = bm->hardware_address;
387 u_long bmd_size = bm->bytes_per_row*bm->rows*bm->depth;
388
389 if (off >= 0 && off < bmd_size) {
390 return(((u_int)bmd_start + off) >> PGSHIFT);
391 }
392 /* bogus */
393 return(-1);
394 }
395
396 /*ARGSUSED*/
397 viewselect(dev, rw)
398 dev_t dev;
399 {
400 if (rw == FREAD)
401 return(0);
402 return(1);
403 }
404
405 void
406 viewattach() {}
407