grfabs.c revision 1.18 1 1.18 matt /* $NetBSD: grfabs.c,v 1.18 2012/02/12 16:34:07 matt Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo *
16 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 leo */
27 1.1 leo
28 1.1 leo /*
29 1.1 leo * atari abstract graphics driver.
30 1.1 leo */
31 1.12 lukem
32 1.12 lukem #include <sys/cdefs.h>
33 1.18 matt __KERNEL_RCSID(0, "$NetBSD: grfabs.c,v 1.18 2012/02/12 16:34:07 matt Exp $");
34 1.12 lukem
35 1.1 leo #include <sys/param.h>
36 1.7 leo #include <sys/systm.h>
37 1.1 leo #include <sys/queue.h>
38 1.1 leo #include <sys/malloc.h>
39 1.1 leo
40 1.5 leo #include <machine/cpu.h>
41 1.1 leo #include <machine/iomap.h>
42 1.1 leo #include <machine/video.h>
43 1.1 leo #include <machine/mfp.h>
44 1.1 leo #include <atari/dev/grfabs_reg.h>
45 1.1 leo
46 1.1 leo /*
47 1.1 leo * Function decls
48 1.1 leo */
49 1.14 dsl static dmode_t *get_best_display_mode(dimen_t *, int, dmode_t *);
50 1.1 leo
51 1.1 leo /*
52 1.5 leo * List of available graphic modes
53 1.1 leo */
54 1.5 leo static MODES modes;
55 1.1 leo
56 1.1 leo /*
57 1.5 leo * Ugh.. Stuff needed to allocate console structures before the VM-system
58 1.5 leo * is running. There is no malloc() available at that time.
59 1.5 leo * Decision to use these: atari_realconfig == 0
60 1.1 leo */
61 1.5 leo view_t gra_con_view;
62 1.5 leo colormap_t gra_con_cmap;
63 1.5 leo long gra_con_colors[MAX_CENTRIES];
64 1.1 leo
65 1.1 leo /*
66 1.1 leo * Default colors.....
67 1.5 leo * Currently the TT-low (256 colors) just uses 16 times the 16-color default.
68 1.1 leo * If you have a sensible 256 scale, feel free to add.....
69 1.1 leo * The first 2 colors in all maps are {black,white}, so ite (text) displays
70 1.1 leo * are initially readable. Also, this enables me to supply only 1 map. The
71 1.1 leo * 4 color mode uses the first four entries of the 16 color mode thus giving
72 1.1 leo * a gray scale display. (Maybe we can add an intensity bit to the ite...)
73 1.1 leo */
74 1.5 leo u_long gra_def_color16[16] = {
75 1.5 leo 0x00000000, /* black */
76 1.5 leo 0x00ffffff, /* white */
77 1.6 leo 0x000c0c0c, /* light gray */
78 1.6 leo 0x00808008, /* gray */
79 1.6 leo 0x0000000c, /* blue */
80 1.6 leo 0x00000c00, /* green */
81 1.6 leo 0x00000c0c, /* cyan */
82 1.5 leo 0x00c00000, /* red */
83 1.6 leo 0x00c0000c, /* magenta */
84 1.6 leo 0x00c00c00, /* brown */
85 1.5 leo 0x000000ff, /* light blue */
86 1.5 leo 0x0000ff00, /* light green */
87 1.5 leo 0x0000ffff, /* light cyan */
88 1.5 leo 0x00ff0000, /* light red */
89 1.5 leo 0x00ff00ff, /* light magenta */
90 1.5 leo 0x00ffff00 /* light brown */
91 1.1 leo };
92 1.1 leo
93 1.1 leo /*
94 1.1 leo * XXX: called from ite console init routine.
95 1.11 wiz * Initialize list of possible video modes.
96 1.1 leo */
97 1.1 leo int
98 1.15 dsl grfabs_probe(grf_probe_t probe_fun)
99 1.1 leo {
100 1.5 leo static int inited = 0;
101 1.1 leo
102 1.9 leo if (!inited) {
103 1.9 leo LIST_INIT(&modes);
104 1.9 leo inited = 1;
105 1.9 leo }
106 1.9 leo (*probe_fun)(&modes);
107 1.9 leo
108 1.5 leo return ((modes.lh_first == NULL) ? 0 : 1);
109 1.1 leo }
110 1.1 leo
111 1.1 leo view_t *
112 1.15 dsl grf_alloc_view(dmode_t *d, dimen_t *dim, u_char depth)
113 1.1 leo {
114 1.5 leo if (!d)
115 1.3 leo d = get_best_display_mode(dim, depth, NULL);
116 1.5 leo if (d)
117 1.5 leo return ((d->grfabs_funcs->alloc_view)(d, dim, depth));
118 1.1 leo return(NULL);
119 1.1 leo }
120 1.1 leo
121 1.4 leo dmode_t *
122 1.15 dsl grf_get_best_mode(dimen_t *dim, u_char depth)
123 1.4 leo {
124 1.5 leo return (get_best_display_mode(dim, depth, NULL));
125 1.4 leo }
126 1.4 leo
127 1.18 matt void
128 1.18 matt grf_display_view(view_t *v)
129 1.1 leo {
130 1.5 leo (v->mode->grfabs_funcs->display_view)(v);
131 1.1 leo }
132 1.1 leo
133 1.18 matt void
134 1.18 matt grf_remove_view(view_t *v)
135 1.1 leo {
136 1.5 leo (v->mode->grfabs_funcs->remove_view)(v);
137 1.1 leo }
138 1.1 leo
139 1.18 matt void
140 1.18 matt grf_save_view(view_t *v)
141 1.10 leo {
142 1.10 leo (v->mode->grfabs_funcs->save_view)(v);
143 1.10 leo }
144 1.10 leo
145 1.18 matt void
146 1.18 matt grf_free_view(view_t *v)
147 1.1 leo {
148 1.5 leo (v->mode->grfabs_funcs->free_view)(v);
149 1.1 leo }
150 1.1 leo
151 1.1 leo int
152 1.15 dsl grf_get_colormap(view_t *v, colormap_t *cm)
153 1.1 leo {
154 1.1 leo colormap_t *gcm;
155 1.1 leo int i, n;
156 1.4 leo u_long *sv_entry;
157 1.1 leo
158 1.1 leo gcm = v->colormap;
159 1.2 leo n = cm->size < gcm->size ? cm->size : gcm->size;
160 1.4 leo
161 1.4 leo /*
162 1.4 leo * Copy struct from view but be carefull to keep 'entry'
163 1.4 leo */
164 1.4 leo sv_entry = cm->entry;
165 1.4 leo *cm = *gcm;
166 1.4 leo cm->entry = sv_entry;
167 1.4 leo
168 1.4 leo /*
169 1.4 leo * Copy the colors
170 1.4 leo */
171 1.16 cegger memset(cm->entry, 0, cm->size * sizeof(long));
172 1.5 leo for (i = 0; i < n; i++)
173 1.2 leo cm->entry[i] = gcm->entry[i];
174 1.5 leo return (0);
175 1.1 leo }
176 1.1 leo
177 1.1 leo int
178 1.15 dsl grf_use_colormap(view_t *v, colormap_t *cm)
179 1.1 leo {
180 1.5 leo return (v->mode->grfabs_funcs->use_colormap)(v, cm);
181 1.1 leo }
182 1.1 leo
183 1.1 leo static dmode_t *
184 1.15 dsl get_best_display_mode(dimen_t *dim, int depth, dmode_t *curr_mode)
185 1.1 leo {
186 1.1 leo dmode_t *save;
187 1.1 leo dmode_t *dm;
188 1.8 leo long dx, dy, dd, ct;
189 1.8 leo long size_diff, depth_diff;
190 1.1 leo
191 1.8 leo save = NULL;
192 1.8 leo size_diff = 0;
193 1.8 leo depth_diff = 0;
194 1.8 leo dm = modes.lh_first;
195 1.5 leo while (dm != NULL) {
196 1.3 leo dx = abs(dm->size.width - dim->width);
197 1.3 leo dy = abs(dm->size.height - dim->height);
198 1.8 leo dd = abs(dm->depth - depth);
199 1.1 leo ct = dx + dy;
200 1.1 leo
201 1.8 leo if ((save != NULL) && (size_diff == 0)) {
202 1.8 leo if (dd > depth_diff) {
203 1.8 leo dm = dm->link.le_next;
204 1.8 leo continue;
205 1.8 leo }
206 1.8 leo }
207 1.8 leo if ((save == NULL) || (ct <= size_diff)) {
208 1.8 leo save = dm;
209 1.8 leo size_diff = ct;
210 1.8 leo depth_diff = dd;
211 1.1 leo }
212 1.1 leo dm = dm->link.le_next;
213 1.3 leo }
214 1.3 leo /*
215 1.3 leo * Did we do better than the current mode?
216 1.3 leo */
217 1.5 leo if ((save != NULL) && (curr_mode != NULL)) {
218 1.3 leo dx = abs(curr_mode->size.width - dim->width);
219 1.3 leo dy = abs(curr_mode->size.height - dim->height);
220 1.8 leo dd = abs(curr_mode->depth - depth);
221 1.3 leo ct = dx + dy;
222 1.8 leo if ((ct <= size_diff) && (dd <= depth_diff))
223 1.5 leo return (NULL);
224 1.1 leo }
225 1.1 leo return (save);
226 1.1 leo }
227