Code\x11Example\x11C-6	Sample CG3 Driver Written To Support Sun Interface (Sheet 1 of 7)
/* @(#)sunCG3C.c	1.13 13 Jan 1993 */
/*-
 * sunCG3C.c --
 *	Functions to support the sun CG3 board as a memory frame buffer.
 */
/****************************************************************/
/* Modified for loadable modules by Milind Pansare		*/
/****************************************************************/
/************************************************************
Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
                    All Rights Reserved
Permission  to  use,  copy,  modify,  and  distribute   this
software  and  its documentation for any purpose and without
fee is hereby granted, provided that the above copyright no-
tice  appear  in all copies and that both that copyright no-
tice and this permission notice appear in  supporting  docu-
mentation,  and  that the names of Sun or MIT not be used in
advertising or publicity pertaining to distribution  of  the
software  without specific prior written permission. Sun and
M.I.T. make no representations about the suitability of this
software for any purpose. It is provided "as is" without any
express or implied warranty.
SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.
********************************************************/
#include    "sun.h"
#include <servermd.h>
#include    <sys/mman.h>
#ifdef SVR4
#include    <sys/memreg.h>
#else
#include    <pixrect/memreg.h>
#include    <struct.h>
#endif
#include    "colormap.h"
#include    "colormapst.h"
#include    "resource.h"
#if defined(__STDC__)
static const PixmapFormatRec	formats[] = {
#else /* !__STDC__ */
static PixmapFormatRec	formats[] = {
#endif /* __STDC__ */
    1,  1,  BITMAP_SCANLINE_PAD, 
    8,  8,  BITMAP_SCANLINE_PAD,
};
#ifdef NUMFORMATS
#undef NUMFORMATS
#endif /* NUMFORMATS */
#define NUMFORMATS	(sizeof formats)/(sizeof formats[0])
/*-
 *-----------------------------------------------------------------------
 * sunCG3Init --
 *	Attempt to find and initialize a cg3 framebuffer
 *
 * Results:
 *	TRUE if everything went ok. FALSE if not.
 *
 * Side Effects:
 *	Most of the elements of the ScreenRec are filled in. Memory is
 *	allocated for the frame buffer and the buffer is mapped. The
 *	video is enabled for the frame buffer...
 *
 *-----------------------------------------------------------------------
 */
/*ARGSUSED*/
Bool
sunCG3Init (index, pScreen, argc, argv)
    int	    	  index;    	/* The index of pScreen in the ScreenInfo */
    ScreenPtr	  pScreen;  	/* The Screen to initialize */
    int	    	  argc;	    	/* The number of the Server's arguments. */
    char    	  **argv;   	/* The arguments themselves. Don't change! */
{
    sunScreenPtr pPrivate;
    int i;
    int     dpix, dpiy;
    int     defClass, defDepth;
    Bool    grayVis;
    extern Bool sunCG3CloseScreen();
    if (!sunScreenAllocate (pScreen))
	return FALSE;
    pPrivate = GetScreenPrivate(pScreen);
    if (!sunCG3Probe(index, pScreen, argc, argv)) {
	return FALSE;
    }
    /* Advertise the PixmapFormats that this screen can support */
    for (i=0; i< NUMFORMATS; i++) {
	if (!sunSetPixmapFormat(&formats[i])) {
	    return FALSE;
	}
    }
    sunGetMonitorRes(index, &dpix, &dpiy);
    sunGetVisualInfo(index, &defClass, &defDepth, &grayVis);
    /*   
     * Check for bad defDepth.  Since cg6 supports ALL_VISUALS we don't
     * need to check defClass.
     */  
      
    if (defDepth && (defDepth != 8)) {
        ErrorF("cgsix only supports depth 8 visual classes\n");
        defDepth = 8;
    }
 
    cfbSetDefaultVisual(defClass, defDepth);
	
    /* 
     * Advertise the depths & visuals for each depth that cfb should create
     * cfbSetVisualTypes(depth, visualsToExport, bitsPerRGBValue)
    */
    cfbSetVisualTypes(1, NO_VISUALS, 1); /* depth 1 pixmaps are always supported */
    if (grayVis)
        cfbSetVisualTypes(8, GRAY_VISUALS, 8);
    else
        cfbSetVisualTypes(8, ALL_VISUALS, 8);
    if (!cfbScreenInit (pScreen, pPrivate->sunFbs.fb,
			pPrivate->sunFbs.info.fb_width,
			pPrivate->sunFbs.info.fb_height,
			dpix, dpiy,
			pPrivate->sunFbs.info.fb_width))
	return (FALSE);
    sunCGScreenInit (pScreen);
    if (!sunScreenInit (pScreen))
	return FALSE;
    /* Wrap pScreen->CloseScreen */
    pPrivate->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = sunCG3CloseScreen;
    sunSaveScreen (pScreen, SCREEN_SAVER_OFF);
    return cfbCreateDefColormap(pScreen);
}
/*-
 *-----------------------------------------------------------------------
 * sunCG3Probe --
 *	Map the cg3 framebuffer
 *
 * Results:
 *	TRUE if everything went ok. FALSE if not.
 *
 * Side Effects:
 *	Memory is allocated for the frame buffer and the buffer is mapped.
 *
 *-----------------------------------------------------------------------
 */
/*ARGSUSED*/
static Bool
sunCG3Probe(index, pScreen, argc, argv)
    int	    	  index;    	/* The index of pScreen in the ScreenInfo */
    ScreenPtr	  pScreen;	/* Pointer to Screen structure */
    int	    	  argc;	    	/* The number of the Server's arguments. */
    char    	  **argv;   	/* The arguments themselves. Don't change! */
{
    int         fd;
    struct fbtype fbType;
    int		pagemask, mapsize;
    int		imagelen;
    caddr_t	mapaddr;
    caddr_t	addr;
    off_t 	offset;
    static fbFd sunFbs; 
    SetupScreen(pScreen);
    if ((fd = sunOpenFrameBuffer(FBTYPE_SUN3COLOR, &fbType, index, 
	&pPrivate->sunFbs, argc, argv)) < 0)
	return FALSE;
#ifdef SVR4
    pagemask = sysconf(_SC_PAGESIZE) - 1;
#else
    pagemask = getpagesize() - 1;
#endif
    imagelen = ((fbType.fb_width + 1) & ~1) * fbType.fb_height;
    mapsize = (imagelen + pagemask) & ~pagemask;
    offset = (((((((fbType.fb_width + 15) >> 3) & ~1) * fbType.fb_height)
	     + pagemask) & ~pagemask) << 1); /* Take a deep breath */
    addr = 0;
#ifndef	_MAP_NEW
    addr = (caddr_t) valloc(mapsize);
    if (addr == (caddr_t) NULL) {
	ErrorF("Could not allocate room for frame buffer.\n");
	(void) close (fd);
	return FALSE;
    }
#endif	_MAP_NEW
    mapaddr = (caddr_t) mmap((caddr_t) addr,
	     mapsize,
	     PROT_READ | PROT_WRITE,
	     MAP_SHARED, fd, offset);
    if (mapaddr == (caddr_t) -1) {
	Error("Mapping cg3");
	(void) close(fd);
	return FALSE;
    }
    if (mapaddr == 0)
        mapaddr = addr;
    pPrivate->sunFbs.fd = fd;
    strcpy(pPrivate->sunFbs.devName, GetDevname(index));
    pPrivate->sunFbs.info = fbType;
    pPrivate->sunFbs.fb = (pointer)mapaddr;
    pPrivate->sunFbs.maplength = mapsize;
    pPrivate->sunFbs.EnterLeave = NULL;
    return TRUE;
}
/*-
 *-----------------------------------------------------------------------
 * sunCG3CloseScreen
 *	Close the cg3 device. Closes the file descriptor, and munmaps itself.
 *
 * Results:
 *	TRUE if everything went ok. FALSE if not.
 *
 * Side Effects:
 *	Closes the file descriptor, closes the memory mapping.
 *
 *-----------------------------------------------------------------------
 */
static Bool
sunCG3CloseScreen(i, pScreen)
    int		i;
    ScreenPtr	pScreen;
{
    SetupScreen(pScreen);
    Bool    ret;
    sunDisableCursor (pScreen);
    (void) (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
    pScreen->CloseScreen = pPrivate->CloseScreen;
    ret = (*pScreen->CloseScreen) (i, pScreen);
    if (munmap((caddr_t)(pPrivate->sunFbs.fb), 
	      (size_t)(pPrivate->sunFbs.maplength)) == -1) {
	Error("Unmapping cg3");
    }
    (void) close(pPrivate->sunFbs.fd);
    xfree ((pointer) pPrivate);
    return ret;
}

