Home | History | Annotate | Line # | Download | only in dev
amidisplaycc.c revision 1.3
      1 /*	$NetBSD: amidisplaycc.c,v 1.3 2002/01/26 13:40:52 aymeric Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 Jukka Andberg.
      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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * wscons interface to amiga custom chips. Contains the necessary functions
     32  * to render text on bitmapped screens. Uses the functions defined in
     33  * grfabs_reg.h for display creation/destruction and low level setup.
     34  *
     35  * For each virtual terminal a new screen ('view') is allocated.
     36  * Also one more is allocated for the mapped screen on demand.
     37  */
     38 
     39 #include "amidisplaycc.h"
     40 #include "grfcc.h"
     41 #include "view.h"
     42 #include "opt_amigaccgrf.h"
     43 
     44 #if NAMIDISPLAYCC>0
     45 
     46 #include <sys/param.h>
     47 #include <sys/types.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <sys/systm.h>
     51 
     52 #include <sys/conf.h>
     53 
     54 #include <amiga/dev/grfabs_reg.h>
     55 #include <amiga/dev/viewioctl.h>
     56 #include <amiga/amiga/device.h>
     57 #include <dev/wscons/wsconsio.h>
     58 #include <dev/rcons/raster.h>
     59 #include <dev/wscons/wscons_raster.h>
     60 #include <dev/wscons/wsdisplayvar.h>
     61 #include <dev/cons.h>
     62 #include <dev/wsfont/wsfont.h>
     63 
     64 #include <machine/stdarg.h>
     65 
     66 #define AMIDISPLAYCC_MAXFONTS 8
     67 
     68 /* These can be lowered if you are sure you dont need that much colors. */
     69 #define MAXDEPTH 8
     70 #define MAXROWS 128
     71 #define MAXCOLUMNS 80
     72 
     73 #define ADJUSTCOLORS
     74 
     75 #define MAXCOLORS (1<<MAXDEPTH)
     76 
     77 struct amidisplaycc_screen;
     78 struct amidisplaycc_softc
     79 {
     80 	struct device dev;
     81 
     82 	/* runtime-loaded fonts */
     83 	struct wsdisplay_font         fonts[AMIDISPLAYCC_MAXFONTS];
     84 
     85 	struct amidisplaycc_screen  * currentscreen;
     86 
     87 	/* display turned on? */
     88 	int       ison;
     89 
     90 	/* stuff relating to the mapped screen */
     91 	view_t  * gfxview;
     92 	int       gfxwidth;
     93 	int       gfxheight;
     94 	int       gfxdepth;
     95 	int       gfxon;
     96 };
     97 
     98 
     99 /*
    100  * Configuration stuff.
    101  */
    102 
    103 static int  amidisplaycc_match(struct device *, struct cfdata *, void *);
    104 static void amidisplaycc_attach(struct device *, struct device *, void *);
    105 
    106 struct cfattach amidisplaycc_ca = {
    107 	sizeof(struct amidisplaycc_softc),
    108 	amidisplaycc_match,
    109 	amidisplaycc_attach
    110 };
    111 
    112 cons_decl(amidisplaycc_);
    113 
    114 /* end of configuration stuff */
    115 
    116 /* private utility functions */
    117 
    118 static int amidisplaycc_setvideo(struct amidisplaycc_softc *, int);
    119 
    120 static int amidisplaycc_setemulcmap(struct amidisplaycc_screen *,
    121 				    struct wsdisplay_cmap *);
    122 
    123 static int amidisplaycc_cmapioctl(view_t *, u_long, struct wsdisplay_cmap *);
    124 static int amidisplaycc_setcmap(view_t *, struct wsdisplay_cmap *);
    125 static int amidisplaycc_getcmap(view_t *, struct wsdisplay_cmap *);
    126 static int amidisplaycc_gfxscreen(struct amidisplaycc_softc *, int);
    127 
    128 static int amidisplaycc_setnamedfont(struct amidisplaycc_screen *, char *);
    129 static void amidisplaycc_setfont(struct amidisplaycc_screen *,
    130 				 struct wsdisplay_font *, int);
    131 static struct wsdisplay_font *amidisplaycc_findfont(struct amidisplaycc_softc *,
    132 						    const char *, int, int);
    133 
    134 static void dprintf(const char *fmt, ...);
    135 
    136 /* end of private utility functions */
    137 
    138 /* emulops for wscons */
    139 void amidisplaycc_cursor(void *, int, int, int);
    140 int amidisplaycc_mapchar(void *, int, unsigned int *);
    141 void amidisplaycc_putchar(void *, int, int, u_int, long);
    142 void amidisplaycc_copycols(void *, int, int, int, int);
    143 void amidisplaycc_erasecols(void *, int, int, int, long);
    144 void amidisplaycc_copyrows(void *, int, int, int);
    145 void amidisplaycc_eraserows(void *, int, int, long);
    146 int  amidisplaycc_alloc_attr(void *, int, int, int, long *);
    147 /* end of emulops for wscons */
    148 
    149 
    150 /* accessops for wscons */
    151 int amidisplaycc_ioctl(void *, u_long, caddr_t, int, struct proc *);
    152 paddr_t amidisplaycc_mmap(void *, off_t, int);
    153 int amidisplaycc_alloc_screen(void *, const struct wsscreen_descr *, void **,
    154 			      int *, int *, long *);
    155 void amidisplaycc_free_screen( void *, void *);
    156 int amidisplaycc_show_screen(void *, void *, int, void (*)(void *, int, int),
    157 			     void *);
    158 int amidisplaycc_load_font(void *, void *, struct wsdisplay_font *);
    159 void amidisplaycc_pollc(void *, int);
    160 /* end of accessops for wscons */
    161 
    162 /*
    163  * These structures are passed to wscons, and they contain the
    164  * display-specific callback functions.
    165  */
    166 
    167 const struct wsdisplay_accessops amidisplaycc_accessops = {
    168 	amidisplaycc_ioctl,
    169 	amidisplaycc_mmap,
    170 	amidisplaycc_alloc_screen,
    171 	amidisplaycc_free_screen,
    172 	amidisplaycc_show_screen,
    173 	amidisplaycc_load_font,
    174 	amidisplaycc_pollc
    175 };
    176 
    177 const struct wsdisplay_emulops amidisplaycc_emulops = {
    178 	amidisplaycc_cursor,
    179 	amidisplaycc_mapchar,
    180 	amidisplaycc_putchar,
    181 	amidisplaycc_copycols,
    182 	amidisplaycc_erasecols,
    183 	amidisplaycc_copyrows,
    184 	amidisplaycc_eraserows,
    185 	amidisplaycc_alloc_attr
    186 };
    187 
    188 /* add some of our own data to the wsscreen_descr */
    189 struct amidisplaycc_screen_descr {
    190 	struct wsscreen_descr  wsdescr;
    191 	int                    depth;
    192 	char                   name[16];
    193 };
    194 
    195 /*
    196  * List of supported screenmodes. Almost anything can be given here.
    197  */
    198 
    199 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
    200     /* CONSTCOND */ \
    201     {{ \
    202     name, \
    203     width / fontwidth, \
    204     height / fontheight, \
    205     &amidisplaycc_emulops, fontwidth, fontheight, \
    206     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
    207     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
    208     depth }
    209 
    210 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
    211 	/* name, width, height, depth, fontwidth==8, fontheight */
    212 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
    213 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
    214 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
    215 	ADCC_SCREEN("80x24", 640, 384, 3, 8, 16),
    216 
    217 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
    218 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
    219 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
    220 
    221 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
    222 	ADCC_SCREEN("640x200x1", 640, 200, 2, 8, 8),
    223 	ADCC_SCREEN("640x200x1", 640, 200, 3, 8, 8),
    224 };
    225 
    226 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
    227 const struct wsscreen_descr *amidisplaycc_screens[] = {
    228 	ADCC_SCREENPTR(0),
    229 	ADCC_SCREENPTR(1),
    230 	ADCC_SCREENPTR(2),
    231 	ADCC_SCREENPTR(3),
    232 	ADCC_SCREENPTR(4),
    233 	ADCC_SCREENPTR(5),
    234 	ADCC_SCREENPTR(6),
    235 	ADCC_SCREENPTR(7),
    236 	ADCC_SCREENPTR(8),
    237 	ADCC_SCREENPTR(9),
    238 };
    239 
    240 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
    241 
    242 /*
    243  * This structure also is passed to wscons. It contains pointers
    244  * to the available display modes.
    245  */
    246 
    247 const struct wsscreen_list amidisplaycc_screenlist = {
    248 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
    249 	amidisplaycc_screens
    250 };
    251 
    252 /*
    253  * Our own screen structure. One will be created for each screen.
    254  */
    255 
    256 struct amidisplaycc_screen
    257 {
    258 	struct amidisplaycc_softc *device;
    259 
    260 	int       isconsole;
    261 	int       isvisible;
    262 	view_t  * view;
    263 
    264 	int       ncols;
    265 	int       nrows;
    266 
    267 	int       cursorrow;
    268 	int       cursorcol;
    269 
    270 	/* Active bitplanes for each character row. */
    271 	int       rowmasks[MAXROWS];
    272 
    273 	/* Mapping of colors to screen colors. */
    274 	int       colormap[MAXCOLORS];
    275 
    276 	/* Copies of display parameters for convenience */
    277 	int       width;
    278 	int       height;
    279 	int       depth;
    280 
    281 	int       widthbytes; /* bytes_per_row           */
    282 	int       linebytes;  /* widthbytes + row_mod    */
    283 	int       rowbytes;   /* linebytes * fontheight  */
    284 
    285 	u_char  * planes[MAXDEPTH];
    286 
    287 	u_char  * savedscreen;
    288 
    289 	/*
    290 	 * The font is either one we loaded ourselves, or
    291 	 * one gotten using the wsfont system.
    292 	 *
    293 	 * wsfontcookie differentiates between them:
    294 	 * For fonts loaded by ourselves it is -1.
    295 	 * For wsfonts it contains a cookie for that system.
    296 	 */
    297 	struct wsdisplay_font  * font;
    298 	int                      wsfontcookie;
    299 	int                      fontwidth;
    300 	int                      fontheight;
    301 };
    302 
    303 typedef struct amidisplaycc_screen adccscr_t;
    304 
    305 /*
    306  * Need one statically allocated screen for early init.
    307  * The rest are mallocated when needed.
    308  */
    309 adccscr_t amidisplaycc_consolescreen;
    310 
    311 
    312 /*
    313  * Bring in the one or two builtin fonts.
    314  */
    315 
    316 extern unsigned char kernel_font_8x8[];
    317 extern unsigned char kernel_font_lo_8x8;
    318 extern unsigned char kernel_font_hi_8x8;
    319 
    320 /*
    321  * Default palettes for 2, 4 and 8 color emulation displays.
    322  */
    323 
    324 /* black, grey */
    325 static u_char pal2red[] = { 0x00, 0xaa };
    326 static u_char pal2grn[] = { 0x00, 0xaa };
    327 static u_char pal2blu[] = { 0x00, 0xaa };
    328 
    329 /* black, red, green, grey */
    330 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
    331 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
    332 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
    333 
    334 /* black, red, green, brown, blue, magenta, cyan, grey */
    335 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
    336 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
    337 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
    338 
    339 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
    340 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
    341 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
    342 
    343 #ifdef GRF_AGA
    344 extern int aga_enable;
    345 #else
    346 static int aga_enable = 0;
    347 #endif
    348 
    349 /*
    350  * This gets called at console init to determine the priority of
    351  * this console device.
    352  *
    353  * Of course pointers to this and other functions must present
    354  * in constab[] in conf.c for this to work.
    355  */
    356 void
    357 amidisplaycc_cnprobe(struct consdev *cd)
    358 {
    359 	cd->cn_pri = CN_INTERNAL;
    360 
    361 	/*
    362 	 * Yeah, real nice. But if we win the console then the wscons system
    363 	 * does the proper initialization.
    364 	 */
    365 	cd->cn_dev = NODEV;
    366 }
    367 
    368 /*
    369  * This gets called if this device is used as the console.
    370  */
    371 void
    372 amidisplaycc_cninit(struct consdev  * cd)
    373 {
    374 	void  * cookie;
    375 	long    attr;
    376 	int     x;
    377 	int     y;
    378 
    379 	/* Yeah, we got the console! */
    380 
    381 	/*
    382 	 * This will do the basic stuff we also need.
    383 	 */
    384 	config_console();
    385 
    386 	/*
    387 	 * Call the init function in grfabs.c if we have
    388 	 * no grfcc to do it.
    389 	 * If grfcc is present it will call grfcc_probe()
    390 	 * during config_console() above.
    391 	 */
    392 #if NGRFCC==0
    393 	grfcc_probe();
    394 #endif
    395 
    396 #if NVIEW>0
    397 	viewprobe();
    398 #endif
    399 
    400 	/*
    401 	 * Set up wscons to handle the details.
    402 	 * It will then call us back when it needs something
    403 	 * display-specific. It will also set up cn_tab properly,
    404 	 * something which we failed to do at amidisplaycc_cnprobe().
    405 	 */
    406 
    407 	/*
    408 	 * The alloc_screen knows to allocate the first screen statically.
    409 	 */
    410 	amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
    411 				  &cookie, &x, &y, &attr);
    412 	wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
    413 			   cookie, x, y, attr);
    414 }
    415 
    416 static int
    417 amidisplaycc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
    418 {
    419 	char *name = auxp;
    420 
    421 	if (matchname("amidisplaycc", name) == 0)
    422 		return (0);
    423 
    424 	/* Allow only one of us now. Not sure about that. */
    425 	if (cfp->cf_unit != 0)
    426 		return (0);
    427 
    428 	return 1;
    429 }
    430 
    431 /* ARGSUSED */
    432 static void
    433 amidisplaycc_attach(struct device *pdp, struct device *dp, void *auxp)
    434 {
    435 	struct wsemuldisplaydev_attach_args    waa;
    436 	struct amidisplaycc_softc            * adp;
    437 
    438 	adp = (struct amidisplaycc_softc*)dp;
    439 
    440 	/*
    441 	 * Attach only at real configuration time. Console init is done at
    442 	 * the amidisplaycc_cninit function above.
    443 	 */
    444 	if (adp) {
    445 		printf(": Amiga custom chip graphics %s",
    446 		       aga_enable ? "(AGA)" : "");
    447 
    448 		if (amidisplaycc_consolescreen.isconsole) {
    449 			adp->currentscreen = &amidisplaycc_consolescreen;
    450 			printf(" (console)");
    451 		} else
    452 			adp->currentscreen = NULL;
    453 
    454 		printf("\n");
    455 
    456 		adp->ison = 1;
    457 
    458 		/*
    459 		 * Mapped screen properties.
    460 		 * Would need a way to configure.
    461 		 */
    462 		adp->gfxview = NULL;
    463 		adp->gfxon = 0;
    464 		adp->gfxwidth = 640;
    465 		adp->gfxheight = 480;
    466 
    467 		if (aga_enable)
    468 			adp->gfxdepth = 8;
    469 		else
    470 			adp->gfxdepth = 4;
    471 
    472 		if (NELEMS(amidisplaycc_screentab) !=
    473 		    NELEMS(amidisplaycc_screens))
    474 			panic("invalid screen definitions");
    475 
    476 		waa.scrdata = &amidisplaycc_screenlist;
    477 		waa.console = amidisplaycc_consolescreen.isconsole;
    478 		waa.accessops = &amidisplaycc_accessops;
    479 		waa.accesscookie = dp;
    480 		config_found(dp, &waa, wsemuldisplaydevprint);
    481 
    482 		bzero(adp->fonts, sizeof(adp->fonts));
    483 
    484 		/* Initialize an alternate system for finding fonts. */
    485 		wsfont_init();
    486 	}
    487 }
    488 
    489 
    490 /*
    491  * Color, bgcolor and style are packed into one long attribute.
    492  * These macros are used to create/split the attribute
    493  */
    494 
    495 #define MAKEATTR(fg, bg, mode) (((fg)<<16) | ((bg)<<8) | (mode))
    496 #define ATTRFG(attr) (((attr)>>16) & 255)
    497 #define ATTRBG(attr) (((attr)>>8) & 255)
    498 #define ATTRMO(attr) ((attr) & 255)
    499 
    500 /*
    501  * Called by wscons to draw/clear the cursor.
    502  * We do this by xorring the block to the screen.
    503  *
    504  * This simple implementation will break if the screen is modified
    505  * under the cursor before clearing it.
    506  */
    507 void
    508 amidisplaycc_cursor(void *screen, int on, int row, int col)
    509 {
    510 	adccscr_t  * scr;
    511 	u_char     * dst;
    512 	int  i;
    513 
    514 	scr = screen;
    515 
    516 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
    517 		return;
    518 
    519 	if (!on && scr->cursorrow==-1 && scr->cursorcol==-1)
    520 		return;
    521 
    522 	if (!on) {
    523 		row = scr->cursorrow;
    524 		col = scr->cursorcol;
    525 	}
    526 
    527 	dst = scr->planes[0];
    528 	dst += row * scr->rowbytes;
    529 	dst += col;
    530 
    531 	if (on) {
    532 		scr->cursorrow = row;
    533 		scr->cursorcol = col;
    534 	} else {
    535 		scr->cursorrow = -1;
    536 		scr->cursorcol = -1;
    537 	}
    538 
    539 	for (i = scr->fontheight ; i > 0 ; i--) {
    540 		*dst ^= 255;
    541 		dst += scr->linebytes;
    542 	}
    543 }
    544 
    545 
    546 /*
    547  * This obviously does something important, don't ask me what.
    548  */
    549 int
    550 amidisplaycc_mapchar(void *screen, int ch, unsigned int *chp)
    551 {
    552 	if (ch > 0 && ch < 256) {
    553 		*chp = ch;
    554 		return (5);
    555 	}
    556 	*chp = ' ';
    557 	return (0);
    558 }
    559 
    560 /*
    561  * Write a character to screen with color / bgcolor / hilite(bold) /
    562  * underline / reverse.
    563  * Surely could be made faster but I'm not sure if its worth the
    564  * effort as scrolling is at least a magnitude slower.
    565  */
    566 void
    567 amidisplaycc_putchar(void *screen, int row, int col, u_int ch, long attr)
    568 {
    569 	adccscr_t  * scr;
    570 	u_char     * dst;
    571 	u_char     * font;
    572 
    573 	int         fontheight;
    574 	u_int8_t  * fontreal;
    575 	int         fontlow;
    576 	int         fonthigh;
    577 
    578 	int     bmapoffset;
    579 	int     linebytes;
    580 	int     underline;
    581 	int     fgcolor;
    582 	int     bgcolor;
    583 	int     plane;
    584 	int     depth;
    585 	int     mode;
    586 	int     bold;
    587 	u_char  f;
    588 	int     j;
    589 
    590 	scr = screen;
    591 
    592 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
    593 		return;
    594 
    595 	/* Extract the colors from the attribute */
    596 	fgcolor = ATTRFG(attr);
    597 	bgcolor = ATTRBG(attr);
    598 	mode    = ATTRMO(attr);
    599 
    600 	/* Translate to screen colors */
    601 	fgcolor = scr->colormap[fgcolor];
    602 	bgcolor = scr->colormap[bgcolor];
    603 
    604 	if (mode & WSATTR_REVERSE) {
    605 		j = fgcolor;
    606 		fgcolor = bgcolor;
    607 		bgcolor = j;
    608 	}
    609 
    610 	bold      = (mode & WSATTR_HILIT) > 0;
    611 	underline = (mode & WSATTR_UNDERLINE) > 0;
    612 
    613 	/* If we have loaded a font use it otherwise the builtin font */
    614 	if (scr->font) {
    615 		fontreal = scr->font->data;
    616 		fontlow  = scr->font->firstchar;
    617 		fonthigh = fontlow + scr->font->numchars - 1;
    618 	} else {
    619 		fontreal = kernel_font_8x8;
    620 		fontlow  = kernel_font_lo_8x8;
    621 		fonthigh = kernel_font_hi_8x8;
    622 	}
    623 
    624 	fontheight = scr->fontheight;
    625 	depth      = scr->depth;
    626 	linebytes  = scr->linebytes;
    627 
    628 	if (ch < fontlow || ch > fonthigh)
    629 		ch = fontlow;
    630 
    631 	/* Find the location where the wanted char is in the font data */
    632 	fontreal += scr->fontheight * (ch - fontlow);
    633 
    634 	bmapoffset = row * scr->rowbytes + col;
    635 
    636 	scr->rowmasks[row] |= fgcolor | bgcolor;
    637 
    638 	for (plane = 0 ; plane < depth ; plane++) {
    639 		dst = scr->planes[plane] + bmapoffset;
    640 
    641 		if (fgcolor & 1) {
    642 			if (bgcolor & 1) {
    643 				/* fg=on bg=on (fill) */
    644 
    645 				for (j = 0 ; j < fontheight ; j++) {
    646 					*dst = 255;
    647 					dst += linebytes;
    648 				}
    649 			} else {
    650 				/* fg=on bg=off (normal) */
    651 
    652 				font = fontreal;
    653 				for (j = 0 ; j < fontheight ; j++) {
    654 					f = *(font++);
    655 					f |= f >> bold;
    656 					*dst = f;
    657 					dst += linebytes;
    658 				}
    659 
    660 				if (underline)
    661 					*(dst - linebytes) = 255;
    662 			}
    663 		} else {
    664 			if (bgcolor & 1) {
    665 				/* fg=off bg=on (inverted) */
    666 
    667 				font = fontreal;
    668 				for (j = 0 ; j < fontheight ; j++) {
    669 					f = *(font++);
    670 					f |= f >> bold;
    671 					*dst = ~f;
    672 					dst += linebytes;
    673 				}
    674 
    675 				if (underline)
    676 					*(dst - linebytes) = 0;
    677 			} else {
    678 				/* fg=off bg=off (clear) */
    679 
    680 				for (j = 0 ; j < fontheight ; j++) {
    681 					*dst = 0;
    682 					dst += linebytes;
    683 				}
    684 			}
    685 		}
    686 		fgcolor >>= 1;
    687 		bgcolor >>= 1;
    688 	}
    689 }
    690 
    691 /*
    692  * Copy characters on a row to another position on the same row.
    693  */
    694 
    695 void
    696 amidisplaycc_copycols(void *screen, int row, int srccol, int dstcol, int ncols)
    697 {
    698 	adccscr_t  * scr;
    699 	u_char     * src;
    700 	u_char     * dst;
    701 
    702 	int  bmapoffset;
    703 	int  linebytes;
    704 	int  depth;
    705 	int  plane;
    706 	int  i;
    707 	int  j;
    708 
    709 	scr = screen;
    710 
    711 	if (srccol < 0 || srccol + ncols > scr->ncols ||
    712 	    dstcol < 0 || dstcol + ncols > scr->ncols ||
    713 	    row < 0 || row >= scr->nrows)
    714 		return;
    715 
    716 	depth = scr->depth;
    717 	linebytes = scr->linebytes;
    718 	bmapoffset = row * scr->rowbytes;
    719 
    720 	for (plane = 0 ; plane < depth ; plane++) {
    721 		src = scr->planes[plane] + bmapoffset;
    722 
    723 		for (j = 0 ; j < scr->fontheight ; j++) {
    724 			dst = src;
    725 
    726 			if (srccol < dstcol) {
    727 
    728 				for (i = ncols - 1 ; i >= 0 ; i--)
    729 					dst[dstcol + i] = src[srccol + i];
    730 
    731 			} else {
    732 
    733 				for (i = 0 ; i < ncols ; i++)
    734 					dst[dstcol + i] = src[srccol + i];
    735 
    736 			}
    737 			src += linebytes;
    738 		}
    739 	}
    740 }
    741 
    742 /*
    743  * Erase part of a row.
    744  */
    745 
    746 void
    747 amidisplaycc_erasecols(void *screen, int row, int startcol, int ncols,
    748 		       long attr)
    749 {
    750 	adccscr_t  * scr;
    751 	u_char     * dst;
    752 
    753 	int  bmapoffset;
    754 	int  linebytes;
    755 	int  bgcolor;
    756 	int  depth;
    757 	int  plane;
    758 	int  fill;
    759 	int  j;
    760 
    761 	scr = screen;
    762 
    763 	if (row < 0 || row >= scr->nrows ||
    764 	    startcol < 0 || startcol + ncols > scr->ncols)
    765 		return;
    766 
    767 	depth = scr->depth;
    768 	linebytes = scr->linebytes;
    769 	bmapoffset = row * scr->rowbytes + startcol;
    770 
    771 	/* Erase will be done using the set background color. */
    772 	bgcolor = ATTRBG(attr);
    773 	bgcolor = scr->colormap[bgcolor];
    774 
    775 	for(plane = 0 ; plane < depth ; plane++) {
    776 
    777 		fill = (bgcolor & 1) ? 255 : 0;
    778 
    779 		dst = scr->planes[plane] + bmapoffset;
    780 
    781 		for (j = 0 ; j < scr->fontheight ; j++) {
    782 			memset(dst, fill, ncols);
    783 			dst += linebytes;
    784 		}
    785 	}
    786 }
    787 
    788 /*
    789  * Copy a number of rows to another location on the screen.
    790  * Combined with eraserows it can be used to perform operation
    791  * also known as 'scrolling'.
    792  */
    793 
    794 void
    795 amidisplaycc_copyrows(void *screen, int srcrow, int dstrow, int nrows)
    796 {
    797 	adccscr_t  * scr;
    798 	u_char     * src;
    799 	u_char     * dst;
    800 
    801 	int  srcbmapoffset;
    802 	int  dstbmapoffset;
    803 	int  widthbytes;
    804 	int  fontheight;
    805 	int  linebytes;
    806 	u_int copysize;
    807 	int  rowdelta;
    808 	int  rowbytes;
    809 	int  srcmask;
    810 	int  dstmask;
    811 	int  bmdelta;
    812 	int  depth;
    813 	int  plane;
    814 	int  i;
    815 	int  j;
    816 
    817 	scr = screen;
    818 
    819 	if (srcrow < 0 || srcrow + nrows > scr->nrows ||
    820 	    dstrow < 0 || dstrow + nrows > scr->nrows)
    821 		return;
    822 
    823 	depth = scr->depth;
    824 
    825 	widthbytes = scr->widthbytes;
    826 	rowbytes   = scr->rowbytes;
    827 	linebytes  = scr->linebytes;
    828 	fontheight = scr->fontheight;
    829 
    830 	srcbmapoffset = rowbytes * srcrow;
    831 	dstbmapoffset = rowbytes * dstrow;
    832 
    833 	if (srcrow < dstrow) {
    834 		/* Move data downwards, need to copy from down to up */
    835 		bmdelta = -rowbytes;
    836 		rowdelta = -1;
    837 
    838 		srcbmapoffset += rowbytes * (nrows - 1);
    839 		srcrow += nrows - 1;
    840 
    841 		dstbmapoffset += rowbytes * (nrows - 1);
    842 		dstrow += nrows - 1;
    843 	} else {
    844 		/* Move data upwards, copy up to down */
    845 		bmdelta = rowbytes;
    846 		rowdelta = 1;
    847 	}
    848 
    849 	if (widthbytes == linebytes)
    850 		copysize = rowbytes;
    851 	else
    852 		copysize = 0;
    853 
    854 	for (j = 0 ; j < nrows ; j++) {
    855 		/* Need to copy only planes that have data on src or dst */
    856 		srcmask = scr->rowmasks[srcrow];
    857 		dstmask = scr->rowmasks[dstrow];
    858 		scr->rowmasks[dstrow] = srcmask;
    859 
    860 		for (plane = 0 ; plane < depth ; plane++) {
    861 
    862 			if (srcmask & 1) {
    863 				/*
    864 				 * Source row has data on this
    865 				 * plane, copy it.
    866 				 */
    867 
    868 				src = scr->planes[plane] + srcbmapoffset;
    869 				dst = scr->planes[plane] + dstbmapoffset;
    870 
    871 				if (copysize > 0) {
    872 
    873 					memcpy(dst, src, copysize);
    874 
    875 				} else {
    876 
    877 					/*
    878 					 * Data not continuous,
    879 					 * must do in pieces
    880 					 */
    881 					for (i=0 ; i < fontheight ; i++) {
    882 						memcpy(dst, src, widthbytes);
    883 
    884 						src += linebytes;
    885 						dst += linebytes;
    886 					}
    887 				}
    888 			} else if (dstmask & 1) {
    889 				/*
    890 				 * Source plane is empty, but dest is not.
    891 				 * so all we need to is clear it.
    892 				 */
    893 
    894 				dst = scr->planes[plane] + dstbmapoffset;
    895 
    896 				if (copysize > 0) {
    897 					/* Do it all */
    898 					bzero(dst, copysize);
    899 				} else {
    900 					for (i = 0 ; i < fontheight ; i++) {
    901 						bzero(dst, widthbytes);
    902 						dst += linebytes;
    903 					}
    904 				}
    905 			}
    906 
    907 			srcmask >>= 1;
    908 			dstmask >>= 1;
    909 		}
    910 		srcbmapoffset += bmdelta;
    911 		dstbmapoffset += bmdelta;
    912 
    913 		srcrow += rowdelta;
    914 		dstrow += rowdelta;
    915 	}
    916 }
    917 
    918 /*
    919  * Erase some rows.
    920  */
    921 
    922 void
    923 amidisplaycc_eraserows(void *screen, int row, int nrows, long attr)
    924 {
    925 	adccscr_t  * scr;
    926 	u_char     * dst;
    927 
    928 	int  bmapoffset;
    929 	int  fillsize;
    930 	int  bgcolor;
    931 	int  depth;
    932 	int  plane;
    933 	int  fill;
    934 	int  j;
    935 
    936 	int  widthbytes;
    937 	int  linebytes;
    938 	int  rowbytes;
    939 
    940 
    941 	scr = screen;
    942 
    943 	if (row < 0 || row + nrows > scr->nrows)
    944 		return;
    945 
    946 	depth      = scr->depth;
    947 	widthbytes = scr->widthbytes;
    948 	linebytes  = scr->linebytes;
    949 	rowbytes   = scr->rowbytes;
    950 
    951 	bmapoffset = row * rowbytes;
    952 
    953 	if (widthbytes == linebytes)
    954 		fillsize = rowbytes * nrows;
    955 	else
    956 		fillsize = 0;
    957 
    958 	bgcolor = ATTRBG(attr);
    959 	bgcolor = scr->colormap[bgcolor];
    960 
    961 	for (j = 0 ; j < nrows ; j++)
    962 		scr->rowmasks[row+j] = bgcolor;
    963 
    964 	for (plane = 0 ; plane < depth ; plane++) {
    965 		dst = scr->planes[plane] + bmapoffset;
    966 		fill = (bgcolor & 1) ? 255 : 0;
    967 
    968 		if (fillsize > 0) {
    969 			/* If the rows are continuous, write them all. */
    970 			memset(dst, fill, fillsize);
    971 		} else {
    972 			for (j = 0 ; j < scr->fontheight * nrows ; j++) {
    973 				memset(dst, fill, widthbytes);
    974 				dst += linebytes;
    975 			}
    976 		}
    977 		bgcolor >>= 1;
    978 	}
    979 }
    980 
    981 
    982 /*
    983  * Compose an attribute value from foreground color,
    984  * background color, and flags.
    985  */
    986 int
    987 amidisplaycc_alloc_attr(void *screen, int fg, int bg, int flags, long *attrp)
    988 {
    989 	adccscr_t  * scr;
    990 	int          maxcolor;
    991 	int          newfg;
    992 	int          newbg;
    993 
    994 	scr = screen;
    995 	maxcolor = (1 << scr->view->bitmap->depth) - 1;
    996 
    997 	/* Ensure the colors are displayable. */
    998 	newfg = fg & maxcolor;
    999 	newbg = bg & maxcolor;
   1000 
   1001 #ifdef ADJUSTCOLORS
   1002 	/*
   1003 	 * Hack for low-color screens, if background color is nonzero
   1004 	 * but would be displayed as one, adjust it.
   1005 	 */
   1006 	if (bg > 0 && newbg == 0)
   1007 		newbg = maxcolor;
   1008 
   1009 	/*
   1010 	 * If foreground and background colors are different but would
   1011 	 * display the same fix them by modifying the foreground.
   1012 	 */
   1013 	if (fg != bg && newfg == newbg) {
   1014 		if (newbg > 0)
   1015 			newfg = 0;
   1016 		else
   1017 			newfg = maxcolor;
   1018 	}
   1019 #endif
   1020 	*attrp = MAKEATTR(newfg, newbg, flags);
   1021 
   1022 	return (0);
   1023 }
   1024 
   1025 int
   1026 amidisplaycc_ioctl(void *dp, u_long cmd, caddr_t data, int flag, struct proc *p)
   1027 {
   1028 	struct amidisplaycc_softc *adp;
   1029 
   1030 	adp = dp;
   1031 
   1032 	if (adp == NULL) {
   1033 		printf("amidisplaycc_ioctl: adp==NULL\n");
   1034 		return (EINVAL);
   1035 	}
   1036 
   1037 #define UINTDATA (*(u_int*)data)
   1038 #define INTDATA (*(int*)data)
   1039 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
   1040 
   1041 	switch (cmd)
   1042 	{
   1043 	case WSDISPLAYIO_GTYPE:
   1044 		UINTDATA = WSDISPLAY_TYPE_AMIGACC;
   1045 		return (0);
   1046 
   1047 	case WSDISPLAYIO_SVIDEO:
   1048 		dprintf("amidisplaycc: WSDISPLAYIO_SVIDEO %s\n",
   1049 			UINTDATA ? "On" : "Off");
   1050 
   1051 		return (amidisplaycc_setvideo(adp, UINTDATA));
   1052 
   1053 	case WSDISPLAYIO_GVIDEO:
   1054 		dprintf("amidisplaycc: WSDISPLAYIO_GVIDEO\n");
   1055 		UINTDATA = adp->ison ?
   1056 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
   1057 
   1058 		return (0);
   1059 
   1060 	case WSDISPLAYIO_SMODE:
   1061 		if (INTDATA == WSDISPLAYIO_MODE_EMUL)
   1062 			return amidisplaycc_gfxscreen(adp, 0);
   1063 		if (INTDATA == WSDISPLAYIO_MODE_MAPPED)
   1064 			return amidisplaycc_gfxscreen(adp, 1);
   1065 		return (-1);
   1066 
   1067 	case WSDISPLAYIO_GINFO:
   1068 		FBINFO.width  = adp->gfxwidth;
   1069 		FBINFO.height = adp->gfxheight;
   1070 		FBINFO.depth  = adp->gfxdepth;
   1071 		FBINFO.cmsize = 1 << FBINFO.depth;
   1072 		return (0);
   1073 
   1074 	case WSDISPLAYIO_PUTCMAP:
   1075 	case WSDISPLAYIO_GETCMAP:
   1076 		return (amidisplaycc_cmapioctl(adp->gfxview,
   1077 					       cmd,
   1078 					       (struct wsdisplay_cmap*)data));
   1079 	}
   1080 
   1081 	dprintf("amidisplaycc: unknown ioctl %lx (grp:'%c' num:%d)\n",
   1082 		(long)cmd,
   1083 		(char)((cmd&0xff00)>>8),
   1084 		(int)(cmd&0xff));
   1085 
   1086 	return (-1);
   1087 
   1088 #undef UINTDATA
   1089 #undef INTDATA
   1090 #undef FBINFO
   1091 }
   1092 
   1093 
   1094 /*
   1095  * Switch to either emulation (text) or mapped (graphics) mode
   1096  * We keep an extra screen for mapped mode so it does not
   1097  * interfere with emulation screens.
   1098  *
   1099  * Once the extra screen is created, it never goes away.
   1100  */
   1101 
   1102 static int
   1103 amidisplaycc_gfxscreen(struct amidisplaycc_softc *adp, int on)
   1104 {
   1105 	dimen_t  dimension;
   1106 
   1107 	dprintf("amidisplaycc: switching to %s mode.\n",
   1108 		on ? "mapped" : "emul");
   1109 
   1110 	/* Current mode same as requested mode? */
   1111 	if ( (on > 0) == (adp->gfxon > 0) )
   1112 		return (0);
   1113 
   1114 	if (!on) {
   1115 		/*
   1116 		 * Switch away from mapped mode. If there is
   1117 		 * a emulation screen, switch to it, otherwise
   1118 		 * just try to hide the mapped screen.
   1119 		 */
   1120 		adp->gfxon = 0;
   1121 		if (adp->currentscreen)
   1122 			grf_display_view(adp->currentscreen->view);
   1123 		else if (adp->gfxview)
   1124 			grf_remove_view(adp->gfxview);
   1125 
   1126 		return (0);
   1127 	}
   1128 
   1129 	/* switch to mapped mode then */
   1130 
   1131 	if (adp->gfxview == NULL) {
   1132 		/* First time here, create the screen */
   1133 
   1134 		dimension.width = adp->gfxwidth;
   1135 		dimension.height = adp->gfxheight;
   1136 
   1137 		dprintf("amidisplaycc: preparing mapped screen %dx%dx%d\n",
   1138 			dimension.width,
   1139 			dimension.height,
   1140 			adp->gfxdepth);
   1141 
   1142 		adp->gfxview = grf_alloc_view(NULL,
   1143 					      &dimension,
   1144 					      adp->gfxdepth);
   1145 	}
   1146 
   1147 	if (adp->gfxview) {
   1148 		adp->gfxon = 1;
   1149 
   1150 		grf_display_view(adp->gfxview);
   1151 	} else {
   1152 		printf("amidisplaycc: failed to make mapped screen\n");
   1153 		return (ENOMEM);
   1154 	}
   1155 	return (0);
   1156 }
   1157 
   1158 /*
   1159  * Map the graphics screen. It must have been created before
   1160  * by switching to mapped mode by using an ioctl.
   1161  */
   1162 paddr_t
   1163 amidisplaycc_mmap(void *dp, off_t off, int prot)
   1164 {
   1165 	struct amidisplaycc_softc  * adp;
   1166 	bmap_t                     * bm;
   1167 	paddr_t                      rv;
   1168 
   1169 	adp = (struct amidisplaycc_softc*)dp;
   1170 
   1171 	/* Check we are in mapped mode */
   1172 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
   1173 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
   1174 		return (paddr_t)(-1);
   1175 	}
   1176 
   1177 	/*
   1178 	 * As we all know by now, we are mapping our special
   1179 	 * screen here so our pretty text consoles are left
   1180 	 * untouched.
   1181 	 */
   1182 
   1183 	bm = adp->gfxview->bitmap;
   1184 
   1185 	/* Check that the offset is valid */
   1186 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
   1187 		dprintf("amidisplaycc_mmap: Offset out of range\n");
   1188 		return (paddr_t)(-1);
   1189 	}
   1190 
   1191 	rv = (paddr_t)bm->hardware_address;
   1192 	rv += off;
   1193 
   1194 	return (rv >> PGSHIFT);
   1195 }
   1196 
   1197 
   1198 /*
   1199  * Create a new screen.
   1200  * NULL dp signifies console and then memory is allocated statically
   1201  * and the screen is automatically displayed.
   1202  *
   1203  * A font with suitable size is searched and if not found
   1204  * the builtin 8x8 font is used.
   1205  *
   1206  * There are separate default palettes for 2, 4 and 8+ color
   1207  * screens.
   1208  */
   1209 
   1210 int
   1211 amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
   1212 			  void  **cookiep, int *curxp, int *curyp,
   1213 			  long *defattrp)
   1214 {
   1215 	const struct amidisplaycc_screen_descr  * adccscreenp;
   1216 	struct amidisplaycc_screen              * scr;
   1217 	struct amidisplaycc_softc               * adp;
   1218 	view_t                                  * view;
   1219 
   1220 	dimen_t  dimension;
   1221 	int      fontheight;
   1222 	int      fontwidth;
   1223 	int      maxcolor;
   1224 	int      depth;
   1225 	int      i;
   1226 	int      j;
   1227 
   1228 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
   1229 	depth = adccscreenp->depth;
   1230 
   1231 	adp = dp;
   1232 
   1233 	maxcolor = (1 << depth) - 1;
   1234 
   1235 	/* Sanity checks because of fixed buffers */
   1236 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
   1237 		return (ENOMEM);
   1238 	if (screenp->nrows > MAXROWS)
   1239 		return (ENOMEM);
   1240 
   1241 	fontwidth = screenp->fontwidth;
   1242 	fontheight = screenp->fontheight;
   1243 
   1244 	if (fontwidth != 8) {
   1245 		dprintf("amidisplaycc_alloc_screen: fontwidth %d invalid.\n",
   1246 		       fontwidth);
   1247 		return (EINVAL);
   1248 	}
   1249 
   1250 	/*
   1251 	 * The screen size is defined in characters.
   1252 	 * Calculate the pixel size using the font size.
   1253 	 */
   1254 
   1255 	dimension.width = screenp->ncols * fontwidth;
   1256 	dimension.height = screenp->nrows * fontheight;
   1257 
   1258 	view = grf_alloc_view(NULL, &dimension, depth);
   1259 	if (view == NULL)
   1260 		return (ENOMEM);
   1261 
   1262 	/*
   1263 	 * First screen gets the statically allocated console screen.
   1264 	 * Others are allocated dynamically.
   1265 	 */
   1266 	if (adp == NULL) {
   1267 		scr = &amidisplaycc_consolescreen;
   1268 		if (scr->isconsole)
   1269 			panic("more than one console?");
   1270 
   1271 		scr->isconsole = 1;
   1272 	} else {
   1273 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK);
   1274 		bzero(scr, sizeof(adccscr_t));
   1275 	}
   1276 
   1277 	scr->view = view;
   1278 
   1279 	scr->ncols = screenp->ncols;
   1280 	scr->nrows = screenp->nrows;
   1281 
   1282 	/* Copies of most used values */
   1283 	scr->width  = dimension.width;
   1284 	scr->height = dimension.height;
   1285 	scr->depth  = depth;
   1286 	scr->widthbytes = view->bitmap->bytes_per_row;
   1287 	scr->linebytes  = scr->widthbytes + view->bitmap->row_mod;
   1288 	scr->rowbytes   = scr->linebytes * fontheight;
   1289 
   1290 	scr->device = adp;
   1291 
   1292 
   1293 	/* --- LOAD FONT --- */
   1294 
   1295 	/* these need to be initialized befory trying to set font */
   1296 	scr->font         = NULL;
   1297 	scr->wsfontcookie = -1;
   1298 	scr->fontwidth    = fontwidth;
   1299 	scr->fontheight   = fontheight;
   1300 
   1301 	/*
   1302 	 * Note that dont try to load font for the console (adp==NULL)
   1303 	 *
   1304 	 * Here we dont care which font we get as long as it is the
   1305 	 * right size so pass NULL.
   1306 	 */
   1307 	if (adp)
   1308 		amidisplaycc_setnamedfont(scr, NULL);
   1309 
   1310 	/*
   1311 	 * If no font found, use the builtin one.
   1312 	 * It will look stupid if the wanted size was different.
   1313 	 */
   1314 	if (scr->font == NULL) {
   1315 		scr->fontwidth = 8;
   1316 		scr->fontheight = min(8, fontheight);
   1317 	}
   1318 
   1319 	/* --- LOAD FONT END --- */
   1320 
   1321 
   1322 	for (i = 0 ; i < depth ; i++) {
   1323 		scr->planes[i] = view->bitmap->plane[i];
   1324 	}
   1325 
   1326 	for (i = 0 ; i < MAXROWS ; i++)
   1327 		scr->rowmasks[i] = 0;
   1328 
   1329 	/* Simple one-to-one mapping for most colors */
   1330 	for (i = 0 ; i < MAXCOLORS ; i++)
   1331 		scr->colormap[i] = i;
   1332 
   1333 	/*
   1334 	 * Arrange the most used pens to quickest colors.
   1335 	 * The default color for given depth is (1<<depth)-1.
   1336 	 * It is assumed it is used most and it is mapped to
   1337 	 * color that can be drawn by writing data to one bitplane
   1338 	 * only.
   1339 	 * So map colors 3->2, 7->4, 15->8 and so on.
   1340 	 */
   1341 	for (i = 2 ; i < MAXCOLORS ; i *= 2) {
   1342 		j = i * 2 - 1;
   1343 
   1344 		if (j < MAXCOLORS) {
   1345 			scr->colormap[i] = j;
   1346 			scr->colormap[j] = i;
   1347 		}
   1348 	}
   1349 
   1350 	/*
   1351 	 * Set the default colormap.
   1352 	 */
   1353 	if (depth == 1)
   1354 		amidisplaycc_setemulcmap(scr, &pal2);
   1355 	else if (depth == 2)
   1356 		amidisplaycc_setemulcmap(scr, &pal4);
   1357 	else
   1358 		amidisplaycc_setemulcmap(scr, &pal8);
   1359 
   1360 	*cookiep = scr;
   1361 
   1362 	*curxp = 0;
   1363 	*curyp = 0;
   1364 	amidisplaycc_cursor(scr, 1, *curxp, *curyp);
   1365 
   1366 	*defattrp = MAKEATTR(maxcolor, 0, 0);
   1367 
   1368 	/* Show the console automatically */
   1369 	if (adp == NULL)
   1370 		grf_display_view(scr->view);
   1371 
   1372 	if (adp) {
   1373 		dprintf("amidisplaycc: allocated screen; %dx%dx%d\n",
   1374 			dimension.width,
   1375 			dimension.height,
   1376 			depth);
   1377 	}
   1378 
   1379 	return (0);
   1380 }
   1381 
   1382 
   1383 /*
   1384  * Destroy a screen.
   1385  */
   1386 
   1387 void
   1388 amidisplaycc_free_screen(void *dp, void *screen)
   1389 {
   1390 	struct amidisplaycc_screen  * scr;
   1391 	struct amidisplaycc_softc   * adp;
   1392 
   1393 	scr = screen;
   1394 	adp = (struct amidisplaycc_softc*)adp;
   1395 
   1396 	if (scr == NULL)
   1397 		return;
   1398 
   1399 	/* Free the used font */
   1400 	amidisplaycc_setfont(scr, NULL, -1);
   1401 
   1402 	if (adp->currentscreen == scr)
   1403 		adp->currentscreen = NULL;
   1404 
   1405 	if (scr->view)
   1406 		grf_free_view(scr->view);
   1407 	scr->view = NULL;
   1408 
   1409 	/* Take care not to free the statically allocated console screen */
   1410 	if (scr != &amidisplaycc_consolescreen) {
   1411 		free(scr, M_DEVBUF);
   1412 	}
   1413 }
   1414 
   1415 /*
   1416  * Switch to another vt. Switch is always made immediately.
   1417  */
   1418 
   1419 /* ARGSUSED2 */
   1420 int
   1421 amidisplaycc_show_screen(void *dp, void *screen, int waitok,
   1422 			 void (*cb) (void *, int, int), void *cbarg)
   1423 {
   1424 	adccscr_t *scr;
   1425 	struct amidisplaycc_softc *adp;
   1426 
   1427 	adp = (struct amidisplaycc_softc*)dp;
   1428 	scr = screen;
   1429 
   1430 	if (adp == NULL) {
   1431 		dprintf("amidisplaycc_show_screen: adp==NULL\n");
   1432 		return (EINVAL);
   1433 	}
   1434 	if (scr == NULL) {
   1435 		dprintf("amidisplaycc_show_screen: scr==NULL\n");
   1436 		return (EINVAL);
   1437 	}
   1438 
   1439 	if (adp->gfxon) {
   1440 		dprintf("amidisplaycc: Screen shift while in gfx mode?");
   1441 		adp->gfxon = 0;
   1442 	}
   1443 
   1444 	adp->currentscreen = scr;
   1445 	adp->ison = 1;
   1446 
   1447 	grf_display_view(scr->view);
   1448 
   1449 	return (0);
   1450 }
   1451 
   1452 /*
   1453  * Internal. Finds the font in our softc that has the desired attributes.
   1454  * Or, if name is NULL, finds a free location for a new font.
   1455  * Returns a pointer to font structure in softc or NULL for failure.
   1456  *
   1457  * Three possible forms:
   1458  * findfont(adp, NULL, 0, 0)  -- find first empty location
   1459  * findfont(adp, NULL, x, y)  -- find last font with given size
   1460  * findfont(adp, name, x, y)  -- find last font with given name and size
   1461  *
   1462  * Note that when finding an empty location first one found is returned,
   1463  * however when finding an existing font, the last one matching is
   1464  * returned. This is because fonts cannot be unloaded and the last
   1465  * font on the list is the one added latest and thus probably preferred.
   1466  *
   1467  * Note also that this is the only function which makes assumptions
   1468  * about the storage location for the fonts.
   1469  */
   1470 static struct wsdisplay_font *
   1471 amidisplaycc_findfont(struct amidisplaycc_softc *adp, const char *name,
   1472 		      int width, int height)
   1473 {
   1474 	struct wsdisplay_font  * font;
   1475 
   1476 	int  findempty;
   1477 	int  f;
   1478 
   1479 	if (adp == NULL) {
   1480 		dprintf("amidisplaycc_findfont: NULL adp\n");
   1481 		return NULL;
   1482 	}
   1483 
   1484 	findempty = (name == NULL) && (width == 0) && (height == 0);
   1485 
   1486 	font = NULL;
   1487 
   1488 	for (f = 0 ; f < AMIDISPLAYCC_MAXFONTS ; f++) {
   1489 
   1490 		if (findempty && adp->fonts[f].name == NULL)
   1491 			return &adp->fonts[f];
   1492 
   1493 		if (!findempty && name == NULL && adp->fonts[f].name &&
   1494 		    adp->fonts[f].fontwidth == width &&
   1495 		    adp->fonts[f].fontheight == height)
   1496 			font = &adp->fonts[f];
   1497 
   1498 		if (name && adp->fonts[f].name &&
   1499 		    strcmp(name, adp->fonts[f].name) == 0 &&
   1500 		    width == adp->fonts[f].fontwidth &&
   1501 		    height == adp->fonts[f].fontheight)
   1502 			font = &adp->fonts[f];
   1503 	}
   1504 
   1505 	return (font);
   1506 }
   1507 
   1508 
   1509 /*
   1510  * Set the font on a screen and free the old one.
   1511  * Can be called with font of NULL to just free the
   1512  * old one.
   1513  * NULL font cannot be accompanied by valid cookie (!= -1)
   1514  */
   1515 static void
   1516 amidisplaycc_setfont(struct amidisplaycc_screen *scr,
   1517 		     struct wsdisplay_font *font, int wsfontcookie)
   1518 {
   1519 	if (scr == NULL)
   1520 		panic("amidisplaycc_setfont: scr==NULL");
   1521 	if (font == NULL && wsfontcookie != -1)
   1522 		panic("amidisplaycc_setfont: no font but eat cookie");
   1523 	if (scr->font == NULL && scr->wsfontcookie != -1)
   1524 		panic("amidisplaycc_setfont: no font but eat old cookie");
   1525 
   1526 	scr->font = font;
   1527 
   1528 	if (scr->wsfontcookie != -1)
   1529 		wsfont_unlock(scr->wsfontcookie);
   1530 
   1531 	scr->wsfontcookie = wsfontcookie;
   1532 }
   1533 
   1534 /*
   1535  * Try to find the named font and set the screen to use it.
   1536  * Check both the fonts we have loaded with load_font and
   1537  * fonts from wsfont system.
   1538  *
   1539  * Returns 0 on success.
   1540  */
   1541 
   1542 static int
   1543 amidisplaycc_setnamedfont(struct amidisplaycc_screen *scr, char *fontname)
   1544 {
   1545 	struct wsdisplay_font  * font;
   1546 	int  wsfontcookie;
   1547 
   1548 	wsfontcookie = -1;
   1549 
   1550 	if (scr == NULL || scr->device == NULL) {
   1551 		dprintf("amidisplaycc_setnamedfont: invalid\n");
   1552 		return (EINVAL);
   1553 	}
   1554 
   1555 	/* Try first our dynamically loaded fonts. */
   1556 	font = amidisplaycc_findfont(scr->device,
   1557 				     fontname,
   1558 				     scr->fontwidth,
   1559 				     scr->fontheight);
   1560 
   1561 	if (font == NULL) {
   1562 		/*
   1563 		 * Ok, no dynamically loaded font found.
   1564 		 * Try the wsfont system then.
   1565 		 */
   1566 		wsfontcookie = wsfont_find(fontname,
   1567 					   scr->fontwidth,
   1568 					   scr->fontheight,
   1569 					   1);
   1570 
   1571 		if (wsfontcookie == -1)
   1572 			return (EINVAL);
   1573 
   1574 		/* So, found a suitable font. Now lock it. */
   1575 		if (wsfont_lock(wsfontcookie,
   1576 				&font,
   1577 				WSDISPLAY_FONTORDER_L2R,
   1578 				WSDISPLAY_FONTORDER_L2R) == -1)
   1579 			return (EINVAL);
   1580 
   1581 		/* Ok here we have the font successfully. */
   1582 	}
   1583 
   1584 	amidisplaycc_setfont(scr, font, wsfontcookie);
   1585 	return (0);
   1586 }
   1587 
   1588 /*
   1589  * Load a font. This is used both to load a font and set it to screen.
   1590  * The function depends on the parameters.
   1591  * If the font has no data we must set a previously loaded
   1592  * font with the same name. If it has data, then just load
   1593  * the font but don't use it.
   1594  */
   1595 int
   1596 amidisplaycc_load_font(void *dp, void *cookie, struct wsdisplay_font *font)
   1597 {
   1598 	struct amidisplaycc_softc   * adp;
   1599 	struct amidisplaycc_screen  * scr;
   1600 	struct wsdisplay_font       * myfont;
   1601 
   1602 	u_int8_t  * c;
   1603 	void      * olddata;
   1604 	char      * name;
   1605 
   1606 	u_int       size;
   1607 	u_int       i;
   1608 
   1609 
   1610 	adp = dp;
   1611 	scr = cookie;
   1612 
   1613 	/*
   1614 	 * If font has no data it means we have to find the
   1615 	 * named font and use it.
   1616 	 */
   1617 	if (scr && font && font->name && !font->data)
   1618 		return amidisplaycc_setnamedfont(scr, font->name);
   1619 
   1620 
   1621 	/* Pre-load the font it is */
   1622 
   1623 	if (font->stride != 1) {
   1624 		dprintf("amidisplaycc_load_font: stride %d != 1\n",
   1625 		       font->stride);
   1626 		return (-1);
   1627 	}
   1628 
   1629 	if (font->fontwidth != 8) {
   1630 		dprintf("amidisplaycc_load_font: width %d not supported\n",
   1631 		       font->fontwidth);
   1632 		return (-1);
   1633 	}
   1634 
   1635 	/* Size of the font in bytes... Assuming stride==1 */
   1636 	size = font->fontheight * font->numchars;
   1637 
   1638 	/* Check if the same font was loaded before */
   1639 	myfont = amidisplaycc_findfont(adp,
   1640 				       font->name,
   1641 				       font->fontwidth,
   1642 				       font->fontheight);
   1643 
   1644 	olddata = NULL;
   1645 	if (myfont) {
   1646 		/* Old font found, we will replace */
   1647 
   1648 		if (myfont->name == NULL || myfont->data == NULL)
   1649 			panic("replacing NULL font/data");
   1650 
   1651 		/*
   1652 		 * Store the old data pointer. We'll free it later
   1653 		 * when the new one is in place. Reallocation is needed
   1654 		 * because the new font may have a different number
   1655 		 * of characters in it than the last time it was loaded.
   1656 		 */
   1657 
   1658 		olddata = myfont->data;
   1659 
   1660 	} else {
   1661 		/* Totally brand new font */
   1662 
   1663 		/* Try to find empty slot for the font */
   1664 		myfont = amidisplaycc_findfont(adp, NULL, 0, 0);
   1665 
   1666 		if (myfont == NULL)
   1667 			return (ENOMEM);
   1668 
   1669 		bzero(myfont, sizeof(struct wsdisplay_font));
   1670 
   1671 		myfont->fontwidth = font->fontwidth;
   1672 		myfont->fontheight = font->fontheight;
   1673 		myfont->stride = font->stride;
   1674 
   1675 		name = malloc(strlen(font->name)+1,
   1676 			      M_DEVBUF,
   1677 			      M_WAITOK);
   1678 		strcpy(name, font->name);
   1679 		myfont->name = name;
   1680 	}
   1681 	myfont->firstchar = font->firstchar;
   1682 	myfont->numchars  = font->numchars;
   1683 
   1684 	myfont->data = malloc(size,
   1685 			      M_DEVBUF,
   1686 			      M_WAITOK);
   1687 
   1688 	if (olddata)
   1689 		free(olddata, M_DEVBUF);
   1690 
   1691 
   1692 	memcpy(myfont->data, font->data, size);
   1693 
   1694 	if (font->bitorder == WSDISPLAY_FONTORDER_R2L) {
   1695 		/* Reverse the characters. */
   1696 		c = myfont->data;
   1697 		for (i = 0 ; i < size ; i++) {
   1698 			*c = ((*c & 0x0f) << 4) | ((*c & 0xf0) >> 4);
   1699 			*c = ((*c & 0x33) << 2) | ((*c & 0xcc) >> 2);
   1700 			*c = ((*c & 0x55) << 1) | ((*c & 0xaa) >> 1);
   1701 
   1702 			c++;
   1703 		}
   1704 	}
   1705 
   1706 	/* Yeah, we made it */
   1707 	return (0);
   1708 }
   1709 
   1710 /*
   1711  * Set display on/off.
   1712  */
   1713 static int
   1714 amidisplaycc_setvideo(struct amidisplaycc_softc *adp, int mode)
   1715 {
   1716         view_t * view;
   1717 
   1718 	if (adp == NULL) {
   1719 		dprintf("amidisplaycc_setvideo: adp==NULL\n");
   1720 		return (EINVAL);
   1721 	}
   1722 	if (adp->currentscreen == NULL) {
   1723 		dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
   1724 		return (EINVAL);
   1725 	}
   1726 
   1727 	/* select graphics or emulation screen */
   1728 	if (adp->gfxon && adp->gfxview)
   1729 		view = adp->gfxview;
   1730 	else
   1731 		view = adp->currentscreen->view;
   1732 
   1733 	if (mode) {
   1734 		/* on */
   1735 
   1736 		grf_display_view(view);
   1737 		dprintf("amidisplaycc: video is now on\n");
   1738 		adp->ison = 1;
   1739 
   1740 	} else {
   1741 		/* off */
   1742 
   1743 		grf_remove_view(view);
   1744 		dprintf("amidisplaycc: video is now off\n");
   1745 		adp->ison = 0;
   1746 	}
   1747 
   1748 	return (0);
   1749 }
   1750 
   1751 /*
   1752  * Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
   1753  * Just handle the copying of data to/from userspace and
   1754  * let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
   1755  * do the real work.
   1756  */
   1757 
   1758 static int
   1759 amidisplaycc_cmapioctl(view_t *view, u_long cmd, struct wsdisplay_cmap *cmap)
   1760 {
   1761 	struct wsdisplay_cmap  tmpcmap;
   1762 	u_char                 cmred[MAXCOLORS];
   1763 	u_char                 cmgrn[MAXCOLORS];
   1764 	u_char                 cmblu[MAXCOLORS];
   1765 
   1766 	int                    err;
   1767 
   1768 	if (cmap->index >= MAXCOLORS ||
   1769 	    cmap->count > MAXCOLORS ||
   1770 	    cmap->index + cmap->count > MAXCOLORS)
   1771 		return (EINVAL);
   1772 
   1773 	if (cmap->count == 0)
   1774 		return (0);
   1775 
   1776 	tmpcmap.index = cmap->index;
   1777 	tmpcmap.count = cmap->count;
   1778 	tmpcmap.red   = cmred;
   1779 	tmpcmap.green = cmgrn;
   1780 	tmpcmap.blue  = cmblu;
   1781 
   1782 	if (cmd == WSDISPLAYIO_PUTCMAP) {
   1783 		/* copy the color data to kernel space */
   1784 
   1785 		err = copyin(cmap->red, cmred, cmap->count);
   1786 		if (err)
   1787 			return (err);
   1788 
   1789 		err = copyin(cmap->green, cmgrn, cmap->count);
   1790 		if (err)
   1791 			return (err);
   1792 
   1793 		err = copyin(cmap->blue, cmblu, cmap->count);
   1794 		if (err)
   1795 			return (err);
   1796 
   1797 		return amidisplaycc_setcmap(view, &tmpcmap);
   1798 
   1799 	} else if (cmd == WSDISPLAYIO_GETCMAP) {
   1800 
   1801 		err = amidisplaycc_getcmap(view, &tmpcmap);
   1802 		if (err)
   1803 			return (err);
   1804 
   1805 		/* copy data to user space */
   1806 
   1807 		err = copyout(cmred, cmap->red, cmap->count);
   1808 		if (err)
   1809 			return (err);
   1810 
   1811 		err = copyout(cmgrn, cmap->green, cmap->count);
   1812 		if (err)
   1813 			return (err);
   1814 
   1815 		err = copyout(cmblu, cmap->blue, cmap->count);
   1816 		if (err)
   1817 			return (err);
   1818 
   1819 		return (0);
   1820 
   1821 	} else
   1822 		return (-1);
   1823 }
   1824 
   1825 /*
   1826  * Set the palette of a emulation screen.
   1827  * Here we do only color remapping and then call
   1828  * amidisplaycc_setcmap to do the work.
   1829  */
   1830 
   1831 static int
   1832 amidisplaycc_setemulcmap(struct amidisplaycc_screen *scr,
   1833 			 struct wsdisplay_cmap *cmap)
   1834 {
   1835 	struct wsdisplay_cmap  tmpcmap;
   1836 
   1837 	u_char                 red [MAXCOLORS];
   1838 	u_char                 grn [MAXCOLORS];
   1839 	u_char                 blu [MAXCOLORS];
   1840 
   1841 	int                    rc;
   1842 	int                    i;
   1843 
   1844 	/*
   1845 	 * Get old palette first.
   1846 	 * Because of the color mapping going on in the emulation
   1847 	 * screen the color range may not be contiguous in the real
   1848 	 * palette.
   1849 	 * So get the whole palette, insert the new colors
   1850 	 * at the appropriate places and then set the whole
   1851 	 * palette back.
   1852 	 */
   1853 
   1854 	tmpcmap.index = 0;
   1855 	tmpcmap.count = 1 << scr->depth;
   1856 	tmpcmap.red   = red;
   1857 	tmpcmap.green = grn;
   1858 	tmpcmap.blue  = blu;
   1859 
   1860 	rc = amidisplaycc_getcmap(scr->view, &tmpcmap);
   1861 	if (rc)
   1862 		return (rc);
   1863 
   1864 	for (i = cmap->index ; i < cmap->index + cmap->count ; i++) {
   1865 
   1866 		tmpcmap.red   [ scr->colormap[ i ] ] = cmap->red   [ i ];
   1867 		tmpcmap.green [ scr->colormap[ i ] ] = cmap->green [ i ];
   1868 		tmpcmap.blue  [ scr->colormap[ i ] ] = cmap->blue  [ i ];
   1869 	}
   1870 
   1871 	rc = amidisplaycc_setcmap(scr->view, &tmpcmap);
   1872 	if (rc)
   1873 		return (rc);
   1874 
   1875 	return (0);
   1876 }
   1877 
   1878 
   1879 /*
   1880  * Set the colormap for the given screen.
   1881  */
   1882 
   1883 static int
   1884 amidisplaycc_setcmap(view_t *view, struct wsdisplay_cmap *cmap)
   1885 {
   1886 	u_long      cmentries [MAXCOLORS];
   1887 
   1888 	int         green_div;
   1889 	int         blue_div;
   1890 	int         grey_div;
   1891 	int         red_div;
   1892 	u_int       colors;
   1893 	int         index;
   1894 	int         count;
   1895 	int         err;
   1896 	colormap_t  cm;
   1897 	int         c;
   1898 
   1899 	if (view == NULL)
   1900 		return (EINVAL);
   1901 
   1902 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue) {
   1903 		dprintf("amidisplaycc_setcmap: other==NULL\n");
   1904 		return (EINVAL);
   1905 	}
   1906 
   1907 	index  = cmap->index;
   1908 	count  = cmap->count;
   1909 	colors = (1 << view->bitmap->depth);
   1910 
   1911 	if (count > colors || index >= colors || index + count > colors)
   1912 		return (EINVAL);
   1913 
   1914 	if (count == 0)
   1915 		return (0);
   1916 
   1917 	cm.entry = cmentries;
   1918 	cm.first = index;
   1919 	cm.size  = count;
   1920 
   1921 	/*
   1922 	 * Get the old colormap. We need to do this at least to know
   1923 	 * how many bits to use with the color values.
   1924 	 */
   1925 
   1926 	err = grf_get_colormap(view, &cm);
   1927 	if (err)
   1928 		return (err);
   1929 
   1930 	/*
   1931 	 * The palette entries from wscons contain 8 bits per gun.
   1932 	 * We need to convert them to the number of bits the view
   1933 	 * expects. That is typically 4 or 8. Here we calculate the
   1934 	 * conversion constants with which we divide the color values.
   1935 	 */
   1936 
   1937 	if (cm.type == CM_COLOR) {
   1938 		red_div = 256 / (cm.red_mask + 1);
   1939 		green_div = 256 / (cm.green_mask + 1);
   1940 		blue_div = 256 / (cm.blue_mask + 1);
   1941 	} else if (cm.type == CM_GREYSCALE)
   1942 		grey_div = 256 / (cm.grey_mask + 1);
   1943 	else
   1944 		return (EINVAL); /* Hmhh */
   1945 
   1946 	/* Copy our new values to the current colormap */
   1947 
   1948 	for (c = 0 ; c < count ; c++) {
   1949 
   1950 		if (cm.type == CM_COLOR) {
   1951 
   1952 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
   1953 				cmap->red[c] / red_div,
   1954 				cmap->green[c] / green_div,
   1955 				cmap->blue[c] / blue_div);
   1956 
   1957 		} else if (cm.type == CM_GREYSCALE) {
   1958 
   1959 			/* Generate grey from average of r-g-b (?) */
   1960 
   1961 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
   1962 				0,
   1963 				0,
   1964 				(cmap->red[c] +
   1965 				 cmap->green[c] +
   1966 				 cmap->blue[c]) / 3 / grey_div);
   1967 		}
   1968 	}
   1969 
   1970 
   1971 	/*
   1972 	 * Now we have a new colormap that contains all the entries. Set
   1973 	 * it to the view.
   1974 	 */
   1975 
   1976 	err = grf_use_colormap(view, &cm);
   1977 	if (err)
   1978 		return err;
   1979 
   1980 	return (0);
   1981 }
   1982 
   1983 /*
   1984  * Return the colormap of the given screen.
   1985  */
   1986 
   1987 static int
   1988 amidisplaycc_getcmap(view_t *view, struct wsdisplay_cmap *cmap)
   1989 {
   1990 	u_long      cmentries [MAXCOLORS];
   1991 
   1992 	int         green_mul;
   1993 	int         blue_mul;
   1994 	int         grey_mul;
   1995 	int         red_mul;
   1996 	u_int       colors;
   1997 	int         index;
   1998 	int         count;
   1999 	int         err;
   2000 	colormap_t  cm;
   2001 	int         c;
   2002 
   2003 	if (view == NULL)
   2004 		return (EINVAL);
   2005 
   2006 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue)
   2007 		return (EINVAL);
   2008 
   2009 	index  = cmap->index;
   2010 	count  = cmap->count;
   2011 	colors = (1 << view->bitmap->depth);
   2012 
   2013 	if (count > colors || index >= colors || index + count > colors)
   2014 		return (EINVAL);
   2015 
   2016 	if (count == 0)
   2017 		return (0);
   2018 
   2019 	cm.entry = cmentries;
   2020 	cm.first = index;
   2021 	cm.size  = count;
   2022 
   2023 
   2024 	err = grf_get_colormap(view, &cm);
   2025 	if (err)
   2026 		return (err);
   2027 
   2028 	if (cm.type == CM_COLOR) {
   2029 		red_mul   = 256 / (cm.red_mask + 1);
   2030 		green_mul = 256 / (cm.green_mask + 1);
   2031 		blue_mul  = 256 / (cm.blue_mask + 1);
   2032 	} else if (cm.type == CM_GREYSCALE) {
   2033 		grey_mul  = 256 / (cm.grey_mask + 1);
   2034 	} else
   2035 		return (EINVAL);
   2036 
   2037 	/*
   2038 	 * Copy color data to wscons-style structure. Translate to
   2039 	 * 8 bits/gun from whatever resolution the color natively is.
   2040 	 */
   2041 
   2042 	for (c = 0 ; c < count ; c++) {
   2043 
   2044 		if (cm.type == CM_COLOR) {
   2045 
   2046 			cmap->red[c]   = CM_GET_RED(cm.entry[index+c]);
   2047 			cmap->green[c] = CM_GET_GREEN(cm.entry[index+c]);
   2048 			cmap->blue[c]  = CM_GET_BLUE(cm.entry[index+c]);
   2049 
   2050 			cmap->red[c]   *= red_mul;
   2051 			cmap->green[c] *= green_mul;
   2052 			cmap->blue[c]  *= blue_mul;
   2053 
   2054 		} else if (cm.type == CM_GREYSCALE) {
   2055 			cmap->red[c]   = CM_GET_GREY(cm.entry[index+c]);
   2056 			cmap->red[c]  *= grey_mul;
   2057 
   2058 			cmap->green[c] = cmap->red[c];
   2059 			cmap->blue[c]  = cmap->red[c];
   2060 		}
   2061 	}
   2062 
   2063 	return (0);
   2064 }
   2065 
   2066 /* ARGSUSED */
   2067 void
   2068 amidisplaycc_pollc(void *cookie, int on)
   2069 {
   2070 }
   2071 
   2072 /*
   2073  * These dummy functions are here just so that we can compete of
   2074  * the console at init.
   2075  * If we win the console then the wscons system will provide the
   2076  * real ones which in turn will call the apropriate wskbd device.
   2077  * These should never be called.
   2078  */
   2079 
   2080 /* ARGSUSED */
   2081 void
   2082 amidisplaycc_cnputc(dev_t cd, int ch)
   2083 {
   2084 }
   2085 
   2086 /* ARGSUSED */
   2087 int
   2088 amidisplaycc_cngetc(dev_t cd)
   2089 {
   2090 	return (0);
   2091 }
   2092 
   2093 /* ARGSUSED */
   2094 void
   2095 amidisplaycc_cnpollc(dev_t cd, int on)
   2096 {
   2097 }
   2098 
   2099 
   2100 /*
   2101  * Prints stuff if DEBUG is turned on.
   2102  */
   2103 
   2104 /* ARGSUSED */
   2105 static void
   2106 dprintf(const char *fmt, ...)
   2107 {
   2108 #ifdef DEBUG
   2109 	va_list ap;
   2110 
   2111 	va_start(ap, fmt);
   2112 	vprintf(fmt, ap);
   2113 	va_end(ap);
   2114 #endif
   2115 }
   2116 
   2117 #endif /* AMIDISPLAYCC */
   2118