1 /* $NetBSD: grfabs.c,v 1.11 2002/03/04 15:35:57 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman. 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 Leo Weppelman. 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 /* 34 * atari abstract graphics driver. 35 */ 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/queue.h> 39 #include <sys/malloc.h> 40 41 #include <machine/cpu.h> 42 #include <machine/iomap.h> 43 #include <machine/video.h> 44 #include <machine/mfp.h> 45 #include <atari/dev/grfabs_reg.h> 46 47 /* 48 * Function decls 49 */ 50 static dmode_t *get_best_display_mode __P((dimen_t *, int, dmode_t *)); 51 52 /* 53 * List of available graphic modes 54 */ 55 static MODES modes; 56 57 /* 58 * Ugh.. Stuff needed to allocate console structures before the VM-system 59 * is running. There is no malloc() available at that time. 60 * Decision to use these: atari_realconfig == 0 61 */ 62 view_t gra_con_view; 63 colormap_t gra_con_cmap; 64 long gra_con_colors[MAX_CENTRIES]; 65 66 /* 67 * Default colors..... 68 * Currently the TT-low (256 colors) just uses 16 times the 16-color default. 69 * If you have a sensible 256 scale, feel free to add..... 70 * The first 2 colors in all maps are {black,white}, so ite (text) displays 71 * are initially readable. Also, this enables me to supply only 1 map. The 72 * 4 color mode uses the first four entries of the 16 color mode thus giving 73 * a gray scale display. (Maybe we can add an intensity bit to the ite...) 74 */ 75 u_long gra_def_color16[16] = { 76 0x00000000, /* black */ 77 0x00ffffff, /* white */ 78 0x000c0c0c, /* light gray */ 79 0x00808008, /* gray */ 80 0x0000000c, /* blue */ 81 0x00000c00, /* green */ 82 0x00000c0c, /* cyan */ 83 0x00c00000, /* red */ 84 0x00c0000c, /* magenta */ 85 0x00c00c00, /* brown */ 86 0x000000ff, /* light blue */ 87 0x0000ff00, /* light green */ 88 0x0000ffff, /* light cyan */ 89 0x00ff0000, /* light red */ 90 0x00ff00ff, /* light magenta */ 91 0x00ffff00 /* light brown */ 92 }; 93 94 /* 95 * XXX: called from ite console init routine. 96 * Initialize list of possible video modes. 97 */ 98 int 99 grfabs_probe(probe_fun) 100 grf_probe_t probe_fun; 101 { 102 static int inited = 0; 103 104 if (!inited) { 105 LIST_INIT(&modes); 106 inited = 1; 107 } 108 (*probe_fun)(&modes); 109 110 return ((modes.lh_first == NULL) ? 0 : 1); 111 } 112 113 view_t * 114 grf_alloc_view(d, dim, depth) 115 dmode_t *d; 116 dimen_t *dim; 117 u_char depth; 118 { 119 if (!d) 120 d = get_best_display_mode(dim, depth, NULL); 121 if (d) 122 return ((d->grfabs_funcs->alloc_view)(d, dim, depth)); 123 return(NULL); 124 } 125 126 dmode_t * 127 grf_get_best_mode(dim, depth) 128 dimen_t *dim; 129 u_char depth; 130 { 131 return (get_best_display_mode(dim, depth, NULL)); 132 } 133 134 void grf_display_view(v) 135 view_t *v; 136 { 137 (v->mode->grfabs_funcs->display_view)(v); 138 } 139 140 void grf_remove_view(v) 141 view_t *v; 142 { 143 (v->mode->grfabs_funcs->remove_view)(v); 144 } 145 146 void grf_save_view(v) 147 view_t *v; 148 { 149 (v->mode->grfabs_funcs->save_view)(v); 150 } 151 152 void grf_free_view(v) 153 view_t *v; 154 { 155 (v->mode->grfabs_funcs->free_view)(v); 156 } 157 158 int 159 grf_get_colormap(v, cm) 160 view_t *v; 161 colormap_t *cm; 162 { 163 colormap_t *gcm; 164 int i, n; 165 u_long *sv_entry; 166 167 gcm = v->colormap; 168 n = cm->size < gcm->size ? cm->size : gcm->size; 169 170 /* 171 * Copy struct from view but be carefull to keep 'entry' 172 */ 173 sv_entry = cm->entry; 174 *cm = *gcm; 175 cm->entry = sv_entry; 176 177 /* 178 * Copy the colors 179 */ 180 bzero(cm->entry, cm->size * sizeof(long)); 181 for (i = 0; i < n; i++) 182 cm->entry[i] = gcm->entry[i]; 183 return (0); 184 } 185 186 int 187 grf_use_colormap(v, cm) 188 view_t *v; 189 colormap_t *cm; 190 { 191 return (v->mode->grfabs_funcs->use_colormap)(v, cm); 192 } 193 194 static dmode_t * 195 get_best_display_mode(dim, depth, curr_mode) 196 int depth; 197 dimen_t *dim; 198 dmode_t *curr_mode; 199 { 200 dmode_t *save; 201 dmode_t *dm; 202 long dx, dy, dd, ct; 203 long size_diff, depth_diff; 204 205 save = NULL; 206 size_diff = 0; 207 depth_diff = 0; 208 dm = modes.lh_first; 209 while (dm != NULL) { 210 dx = abs(dm->size.width - dim->width); 211 dy = abs(dm->size.height - dim->height); 212 dd = abs(dm->depth - depth); 213 ct = dx + dy; 214 215 if ((save != NULL) && (size_diff == 0)) { 216 if (dd > depth_diff) { 217 dm = dm->link.le_next; 218 continue; 219 } 220 } 221 if ((save == NULL) || (ct <= size_diff)) { 222 save = dm; 223 size_diff = ct; 224 depth_diff = dd; 225 } 226 dm = dm->link.le_next; 227 } 228 /* 229 * Did we do better than the current mode? 230 */ 231 if ((save != NULL) && (curr_mode != NULL)) { 232 dx = abs(curr_mode->size.width - dim->width); 233 dy = abs(curr_mode->size.height - dim->height); 234 dd = abs(curr_mode->depth - depth); 235 ct = dx + dy; 236 if ((ct <= size_diff) && (dd <= depth_diff)) 237 return (NULL); 238 } 239 return (save); 240 } 241