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