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