x68kGraph.c revision 706f2543
1/* $NetBSD: x68kGraph.c,v 1.1.1.1 2016/06/09 09:07:59 mrg Exp $ */
2/*-------------------------------------------------------------------------
3 * Copyright (c) 1996 Yasushi Yamasaki
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed by Yasushi Yamasaki
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *-----------------------------------------------------------------------*/
31
32/* $XConsortium: sunCfb.c,v 1.15.1.2 95/01/12 18:54:42 kaleb Exp $ */
33
34/*
35Copyright (c) 1990  X Consortium
36
37Permission is hereby granted, free of charge, to any person obtaining a copy
38of this software and associated documentation files (the "Software"), to deal
39in the Software without restriction, including without limitation the rights
40to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
41copies of the Software, and to permit persons to whom the Software is
42furnished to do so, subject to the following conditions:
43
44The above copyright notice and this permission notice shall be included in
45all copies or substantial portions of the Software.
46
47THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
50X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
51AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53
54Except as contained in this notice, the name of the X Consortium shall not be
55used in advertising or otherwise to promote the sale, use or other dealings
56in this Software without prior written authorization from the X Consortium.
57 */
58
59/************************************************************
60Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
61
62                    All Rights Reserved
63
64Permission  to  use,  copy,  modify,  and  distribute   this
65software  and  its documentation for any purpose and without
66fee is hereby granted, provided that the above copyright no-
67tice  appear  in all copies and that both that copyright no-
68tice and this permission notice appear in  supporting  docu-
69mentation,  and  that the names of Sun or X Consortium
70not be used in advertising or publicity pertaining to
71distribution  of  the software  without specific prior
72written permission. Sun and X Consortium make no
73representations about the suitability of this software for
74any purpose. It is provided "as is" without any express or
75implied warranty.
76
77SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
78INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
79NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
80ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
81ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
82PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
83OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
84THE USE OR PERFORMANCE OF THIS SOFTWARE.
85
86********************************************************/
87
88/*
89 * Copyright (c) 1987 by the Regents of the University of California
90 * Copyright (c) 1987 by Adam de Boor, UC Berkeley
91 *
92 * Permission to use, copy, modify, and distribute this
93 * software and its documentation for any purpose and without
94 * fee is hereby granted, provided that the above copyright
95 * notice appear in all copies.  The University of California
96 * makes no representations about the suitability of this
97 * software for any purpose.  It is provided "as is" without
98 * express or implied warranty.
99 */
100
101/****************************************************************/
102/* Modified from  sunCG4C.c for X11R3 by Tom Jarmolowski	*/
103/****************************************************************/
104
105/*
106 * Copyright 1991, 1992, 1993 Kaleb S. Keithley
107 *
108 * Permission to use, copy, modify, and distribute this
109 * software and its documentation for any purpose and without
110 * fee is hereby granted, provided that the above copyright
111 * notice appear in all copies.  Kaleb S. Keithley makes no
112 * representations about the suitability of this software for
113 * any purpose.  It is provided "as is" without express or
114 * implied warranty.
115 */
116
117#include "x68k.h"
118#include "mi.h"
119#include "micmap.h"
120
121#include "fb.h"
122
123/* local functions */
124static Bool x68kCfbFinishScreenInit(ScreenPtr pScreen, pointer pbits,
125                                    int xsize, int ysize,
126                                    int dpix, int dpiy, int width);
127static void x68kInstallColormap(ColormapPtr cmap);
128static void x68kUninstallColormap(ColormapPtr cmap);
129static int x68kListInstalledColormaps(ScreenPtr pScreen, Colormap *pCmapList);
130static void x68kStoreColors(ColormapPtr pmap, int ndef, xColorItem *pdefs);
131
132static void x68kUpdateColormap(ScreenPtr pScreen, int dex, int count,
133                               u_char *rmap, u_char *gmap, u_char *bmap);
134
135/*-------------------------------------------------------------------------
136 * function "x68kGraphOpen"                          [ X68kFBProc function ]
137 *
138 *  purpose:  call common frame buffer opening procedure
139 *            then set hardware colormap for several static color modes.
140 *  argument: (X68kScreenRec *)pPriv : X68k private screen record
141 *  returns:  (Bool): TRUE  if successed
142 *                    FALSE otherwise
143 *-----------------------------------------------------------------------*/
144Bool
145x68kGraphOpen(X68kScreenRec *pPriv)
146{
147    if( !x68kFbCommonOpen(pPriv, "/dev/grf1") )
148        return FALSE;
149
150    /* initialize hardware colormap
151       in cases of static visual class */
152    if (pPriv->depth == 15 && pPriv->class == TrueColor) {
153        /* for 32768 TrueColor mode */
154	int i;
155	u_short x = 0x0001;
156	for ( i = 0; i < 256; ) {
157	    pPriv->reg->gpal[i++] = x;
158	    pPriv->reg->gpal[i++] = x;
159 	    x += 0x0202;
160        }
161    }
162    if (pPriv->depth == 4 && pPriv->class == StaticGray ) {
163        /* for 16 StaticGray mode */
164        int i;
165        for( i = 0; i < 16; i++ )
166            pPriv->reg->gpal[i] = (i*2 << 11) | (i*2 << 6) | (i*2 << 1);
167    }
168    return TRUE;
169}
170
171/*-------------------------------------------------------------------------
172 * function "x68kGraphClose"                        [ X68kFBProc function ]
173 *
174 *  purpose:  close graphic frame buffer
175 *  argument: (X68kScreenRec *)pPriv : X68k private screen record
176 *  returns:  nothing
177 *-----------------------------------------------------------------------*/
178void
179x68kGraphClose(X68kScreenRec *pPriv)
180{
181    x68kFbCommonClose(pPriv);
182}
183
184/*-------------------------------------------------------------------------
185 * function "x68kGraphInit"                     [ called by DIX AddScreen ]
186 *
187 *  purpose:  initialize graphic frame buffer
188 *  argument: (int)screen              : screen index
189 *            (ScreenPtr)pScreen       : DIX screen record
190 *            (int)argc, (char **)argv : standard C arguments
191 *  returns:  (Bool) : TRUE  if succeeded
192 *                     FALSE otherwise
193 *-----------------------------------------------------------------------*/
194Bool
195x68kGraphInit(int screen, ScreenPtr pScreen, int argc, char *argv[])
196{
197    X68kScreenRec *pPriv;
198
199    /* get private screen record set by X68KConfig */
200    pPriv = x68kGetScreenRecByType(X68K_FB_GRAPHIC);
201
202    /* store private record into screen */
203    if (!dixRegisterPrivateKey(&x68kScreenPrivateKeyRec, PRIVATE_SCREEN, 0)) {
204        Error("dixRegisterPrivateKey failed");
205        return FALSE;
206    }
207    x68kSetScreenPrivate(pScreen, pPriv);
208
209    /* register normal cfb screen functions */
210    if (!fbSetupScreen(pScreen, pPriv->fb,
211                       pPriv->scr_width, pPriv->scr_height,
212                       pPriv->dpi, pPriv->dpi, pPriv->fb_width,
213		       pPriv->depth))
214	return FALSE;
215
216    /* register colormap functions */
217    pScreen->InstallColormap = x68kInstallColormap;
218    pScreen->UninstallColormap = x68kUninstallColormap;
219    pScreen->ListInstalledColormaps = x68kListInstalledColormaps;
220    pScreen->StoreColors = x68kStoreColors;
221
222    /* visual initialization and etc.. */
223    if (!x68kCfbFinishScreenInit(pScreen, pPriv->fb,
224                                 pPriv->scr_width, pPriv->scr_height,
225                                 pPriv->dpi, pPriv->dpi, pPriv->fb_width))
226        return FALSE;
227
228    if ( !miDCInitialize(pScreen, &x68kPointerScreenFuncs) )
229        return FALSE;
230
231    pScreen->whitePixel = 1;
232    pScreen->blackPixel = 0;
233    if ( !miCreateDefColormap(pScreen) )
234        return FALSE;
235
236    return TRUE;
237}
238
239/*-------------------------------------------------------------------------
240 * function "x68kCfbFinishScreenInit"
241 *
242 *  purpose:  initialize visuals and perform miscellaneous settings
243 *  argument: (ScreenPtr)pScreen     : DIX screen record
244 *            (pointer)pbits         : frame buffer
245 *            (int)xsize, (int)ysize : screen size
246 *            (int)dpix, (int)dpiy   : screen resolution in dots per inch
247 *            (int)width             : pixel width of frame buffer
248 *  returns:  (Bool) : TRUE  if succeeded
249 *                     FALSE otherwise
250 *-----------------------------------------------------------------------*/
251static Bool
252x68kCfbFinishScreenInit(
253    ScreenPtr pScreen,
254    pointer pbits,
255    int xsize, int ysize,
256    int dpix, int dpiy,
257    int width)
258{
259    X68kScreenRec *pPriv = x68kGetScreenPrivate(pScreen);
260    VisualPtr	visuals;
261    int		nvisuals;
262    DepthPtr	depths;
263    int		ndepths;
264    VisualID	defaultVisual;
265    int		rootdepth = 0;
266
267    /* for 15/16bit TrueColor visual mode */
268    if (pPriv->depth == 15 && pPriv->class == TrueColor) {
269        VisualID *vid = NULL;
270
271        ndepths = 1;
272        nvisuals = 1;
273        depths = (DepthPtr)malloc( sizeof(DepthRec) );
274        visuals = (VisualPtr)malloc( sizeof(VisualRec) );
275        vid = (VisualID *)malloc( sizeof(VisualID) );
276        if( !depths || !visuals || !vid ) {
277            free( depths );
278            free( visuals );
279            free( vid );
280            return FALSE;
281        }
282        depths[0].depth = 15;
283        depths[0].numVids = 1;
284        depths[0].vids = vid;
285        visuals[0].class = TrueColor;
286        visuals[0].bitsPerRGBValue = 5;
287        visuals[0].ColormapEntries = 1 << 5;
288        visuals[0].nplanes = 15;
289        visuals[0].vid = *vid = FakeClientID(0);
290        visuals[0].greenMask = 0xf800;
291        visuals[0].redMask   = 0x07c0;
292        visuals[0].blueMask  = 0x003e;
293        visuals[0].offsetGreen = 11;
294        visuals[0].offsetRed   = 6;
295        visuals[0].offsetBlue  = 1;
296        rootdepth = 15;
297        defaultVisual = *vid;
298    }
299    /* for 4/16bit StaticGray visual mode */
300    else if (pPriv->depth == 4 && pPriv->class == StaticGray ) {
301        VisualID *vid = NULL;
302
303        ndepths = 1;
304        nvisuals = 1;
305        depths = (DepthPtr)malloc( sizeof(DepthRec) );
306        visuals = (VisualPtr)malloc( sizeof(VisualRec) );
307        vid = (VisualID *)malloc( sizeof(VisualID) );
308        if( !depths || !visuals || !vid ) {
309            free( depths );
310            free( visuals );
311            free( vid );
312            return FALSE;
313        }
314        depths[0].depth = 4;
315        depths[0].numVids = 1;
316        depths[0].vids = vid;
317        visuals[0].class = StaticGray;
318        visuals[0].bitsPerRGBValue = 4;
319        visuals[0].ColormapEntries = 1 << 4;
320        visuals[0].nplanes = 4;
321        visuals[0].vid = *vid = FakeClientID(0);
322        visuals[0].greenMask = 0;
323        visuals[0].redMask   = 0;
324        visuals[0].blueMask  = 0;
325        visuals[0].offsetGreen = 0;
326        visuals[0].offsetRed   = 0;
327        visuals[0].offsetBlue  = 0;
328        rootdepth = 4;
329        defaultVisual = *vid;
330    }
331    /* for other modes ( supports all visuals ) */
332    else if (!miInitVisuals(&visuals, &depths, &nvisuals, &ndepths,
333                            &rootdepth, &defaultVisual, 1 << (16 - 1),
334                            5, PseudoColor))
335	return FALSE;
336
337    if (!miScreenInit(pScreen, pbits, xsize, ysize, dpix, dpiy, width,
338                      rootdepth, ndepths, depths,
339                      defaultVisual, nvisuals, visuals))
340	return FALSE;
341
342    pScreen->CloseScreen = fbCloseScreen;
343    pScreen->SaveScreen = x68kSaveScreen;
344    miInitializeBackingStore (pScreen);
345
346    return TRUE;
347}
348
349/*-------------------------------------------------------------------------
350 * function "x68kInstallColormap"                      [ screen function ]
351 *
352 *  purpose:  install colormap, then update hardware colormap.
353 *  argument: (ColormapPtr)cmap : colormap to install
354 *  returns:  nothing
355 *-----------------------------------------------------------------------*/
356static void
357x68kInstallColormap(ColormapPtr cmap)
358{
359    X68kScreenRec *pPriv = x68kGetScreenPrivate(cmap->pScreen);
360    register int i;
361    register Entry *pent;
362    register VisualPtr pVisual = cmap->pVisual;
363    u_char   rmap[256], gmap[256], bmap[256];
364    unsigned long rMask, gMask, bMask;
365    int	oRed, oGreen, oBlue;
366
367    if (cmap == pPriv->installedMap)
368	return;
369    if (pPriv->installedMap)
370	WalkTree(pPriv->installedMap->pScreen, TellLostMap,
371		 (pointer) &(pPriv->installedMap->mid));
372
373    if (pPriv->class & DynamicClass) {
374        if ((cmap->pVisual->class | DynamicClass) == DirectColor) {
375            rMask = pVisual->redMask;
376            gMask = pVisual->greenMask;
377            bMask = pVisual->blueMask;
378            oRed = pVisual->offsetRed;
379            oGreen = pVisual->offsetGreen;
380            oBlue = pVisual->offsetBlue;
381            for (i = 0; i < 1<<(pPriv->depth); i++) {
382                rmap[i] = cmap->red[(i & rMask) >> oRed].co.local.red >> 11;
383                gmap[i] = cmap->green[(i & gMask) >> oGreen].co.local.green >> 11;
384                bmap[i] = cmap->blue[(i & bMask) >> oBlue].co.local.blue >> 11;
385            }
386        } else {
387            for (i = 0, pent = cmap->red;
388                 i < pVisual->ColormapEntries;
389                 i++, pent++) {
390                if (pent->fShared) {
391                    rmap[i] = pent->co.shco.red->color >> 11;
392                    gmap[i] = pent->co.shco.green->color >> 11;
393                    bmap[i] = pent->co.shco.blue->color >> 11;
394                }
395                else {
396                    rmap[i] = pent->co.local.red >> 11;
397                    gmap[i] = pent->co.local.green >> 11;
398                    bmap[i] = pent->co.local.blue >> 11;
399                }
400            }
401        }
402        x68kUpdateColormap(cmap->pScreen, 0, 1<<(pPriv->depth), rmap, gmap, bmap);
403    }
404    pPriv->installedMap = cmap;
405    WalkTree(cmap->pScreen, TellGainedMap, (pointer) &(cmap->mid));
406}
407
408/*-------------------------------------------------------------------------
409 * function "x68kUninstallColormap"                    [ screen function ]
410 *
411 *  purpose:  uninstall colormap
412 *  argument: (ColormapPtr)cmap : colormap to uninstall
413 *  returns:  nothing
414 *-----------------------------------------------------------------------*/
415static void
416x68kUninstallColormap(ColormapPtr cmap)
417{
418    X68kScreenRec *pPriv = x68kGetScreenPrivate(cmap->pScreen);
419
420    if (cmap == pPriv->installedMap) {
421	Colormap defMapID = cmap->pScreen->defColormap;
422
423	if (cmap->mid != defMapID) {
424	    ColormapPtr defMap = (ColormapPtr) LookupIDByType(defMapID,
425							      RT_COLORMAP);
426	    (*cmap->pScreen->InstallColormap)(defMap);
427	}
428    }
429}
430
431/*-------------------------------------------------------------------------
432 * function "x68kListInstalledColormaps"               [ screen function ]
433 *
434 *  purpose:  Get the list of currently installed colormaps.
435 *            In X68k, installed colormap in always only one.
436 *  argument: (ScreenPtr)pScreen    : screen
437 *            (Colormap *)pCmapList : colormap list got
438 *  returns:  (int)
439 *-----------------------------------------------------------------------*/
440static int
441x68kListInstalledColormaps(ScreenPtr pScreen, Colormap *pCmapList)
442{
443    X68kScreenRec *pPriv = x68kGetScreenPrivate(pScreen);
444    *pCmapList = pPriv->installedMap->mid;
445    return 1;
446}
447
448/*-------------------------------------------------------------------------
449 * function "x68kStoreColors"                          [ screen function ]
450 *
451 *  purpose:  store specified color items
452 *  argument: (ColormapPtr)pmap   : colormap
453 *            (int)ndef           : the number of items
454 *            (xColorItem *)pdefs : items
455 *  returns:  nothing
456 *-----------------------------------------------------------------------*/
457static void
458x68kStoreColors(ColormapPtr pmap, int ndef, xColorItem *pdefs)
459{
460    X68kScreenRec *pPriv = x68kGetScreenPrivate(pmap->pScreen);
461    u_char     rmap[256], gmap[256], bmap[256];
462    xColorItem expanddefs[256];
463    register int i;
464
465    if (pPriv->installedMap != NULL && pPriv->installedMap != pmap)
466	return;
467    if ((pmap->pVisual->class | DynamicClass) == DirectColor) {
468	ndef = fbExpandDirectColors(pmap, ndef, pdefs, expanddefs);
469	pdefs = expanddefs;
470    }
471    while (ndef--) {
472	i = pdefs->pixel;
473	rmap[i] = pdefs->red >> 11;
474	gmap[i] = pdefs->green >> 11;
475	bmap[i] = pdefs->blue >> 11;
476	x68kUpdateColormap(pmap->pScreen, i, 1, rmap, gmap, bmap);
477	pdefs++;
478    }
479}
480
481/*-------------------------------------------------------------------------
482 * function "x68kUpdateColormap"
483 *
484 *  purpose:  update hardware colormap
485 *  argument: (ScreenPtr)pScreen: screen
486 *            (int)dex          : colormap index
487 *            (int)count        : count for updating
488 *            (u_char *)[rgb]map: each map
489 *  returns:  nothing
490 *-----------------------------------------------------------------------*/
491static void
492x68kUpdateColormap(ScreenPtr pScreen, int dex, int count,
493                               u_char *rmap, u_char *gmap, u_char *bmap)
494{
495    X68kScreenRec *pPriv = x68kGetScreenPrivate(pScreen);
496    volatile u_short *pal = pPriv->reg->gpal;
497
498    for( ; count > 0; count--,dex++ ) {
499        pal[dex] = (u_short)gmap[dex] << 11 |
500                   (u_short)rmap[dex] << 6 |
501                   (u_short)bmap[dex] << 1;
502    }
503}
504
505/* EOF x68kGraph.c */
506