home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / sun / sunCG2M.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-31  |  9.2 KB  |  312 lines

  1. /*-
  2.  * sunCG2M.c --
  3.  *    Functions to support the sun CG2 board when treated as a monochrome
  4.  *    frame buffer.
  5.  *
  6.  * Copyright (c) 1987 by the Regents of the University of California
  7.  * Copyright (c) 1987 by Adam de Boor, UC Berkeley
  8.  *
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  *
  17.  *
  18.  */
  19. #ifndef lint
  20. static char rcsid[] =
  21.     "$XConsortium: sunCG2M.c,v 4.20 91/05/31 16:06:12 keith Exp $ SPRITE (Berkeley)";
  22. #endif lint
  23.  
  24. #include    "sun.h"
  25. #include    "resource.h"
  26.  
  27. #include    <sys/mman.h>
  28. #include    <pixrect/memreg.h>
  29. #include    <pixrect/cg2reg.h>
  30. #include    <struct.h>
  31.  
  32. #ifndef _MAP_NEW
  33. extern caddr_t valloc();
  34. #else
  35. extern caddr_t mmap();
  36. #endif  _MAP_NEW
  37.  
  38. /*-
  39.  * The cg2 frame buffer is divided into several pieces.
  40.  *    1) a stack of 8 monochrome bitplanes
  41.  *    2) an array of 8-bit pixels
  42.  *    3) a union of these two where modifications are done via RasterOp
  43.  *        chips
  44.  *    4) various control registers
  45.  *    5) a shadow colormap.
  46.  *
  47.  * Each of these things is at a given offset from the base of the 4Mb devoted
  48.  * to each color board. In addition, the mmap() system call insists on the
  49.  * address and the length to be mapped being aligned on 8K boundaries.
  50.  */
  51. struct cg2m_reg {
  52.     char              pad[4096];  /* The status register is at 0x309000 */
  53.                     /* which isn't on an 8K boundary, so we */
  54.                     /* have to pad this thing here to make */
  55.                     /* the mmaping work... */
  56.     union {
  57.     struct cg2statusreg csr;        /* Control/status register */
  58.     char              pad[4096];    /* This is the amount of room */
  59.                     /* dedicated to the status register */
  60.     }                  u_csr;
  61. };
  62.  
  63. struct cg2m_ppmask {
  64.     union {
  65.     unsigned short        ppmask;     /* Per-plane mask */
  66.     char              pad[8192];    /* Padding to keep the length of */
  67.                     /* these registers page-aligned... */
  68.  
  69.     } u_ppmask;
  70. };
  71.  
  72. struct cg2m_cmap {
  73.     union {
  74.     struct {      /* Shouldn't these be u_char's??? */
  75.         u_short            redmap[256];    /* Red-component map */
  76.         u_short            greenmap[256];    /* Green-component map */
  77.         u_short            bluemap[256];    /* Blue-component map */
  78.     }                 cmap;
  79.     char              pad[8192];
  80.     } u_cmap;
  81. };
  82.  
  83. typedef struct cg2m {
  84.     union bitplane    *image;        /* The first bitplane -- treated as a */
  85.                     /* monochrome frame buffer. */
  86.     struct cg2m_reg *u_csr;        /* the status register */
  87.     struct cg2m_ppmask *u_ppmask;    /* the plane mask register */
  88.     struct cg2m_cmap *u_cmap;        /* the colormap */
  89. } CG2M, CG2MRec, *CG2MPtr;
  90.  
  91. #define CG2M_IMAGE(fb)        ((caddr_t)((fb).image))
  92. #define CG2M_IMAGEOFF        ((off_t)0x00000000)
  93. #define CG2M_IMAGELEN        (sizeof(union bitplane))
  94. #define CG2M_REG(fb)        ((caddr_t)((fb).u_csr))
  95. #define CG2M_REGOFF        ((off_t)0x00308000)
  96. #define CG2M_REGLEN        (2*8192)
  97. #define CG2M_MASK(fb)       ((caddr_t)((fb).u_ppmask))
  98. #define CG2M_MASKOFF        ((off_t)0x0030A000)
  99. #define CG2M_MASKLEN        (0x2000)
  100. #define CG2M_CMAP(fb)        ((caddr_t)((fb).u_cmap))
  101. #define CG2M_CMAPOFF        ((off_t)0x00310000)
  102. #define CG2M_CMAPLEN        8192
  103.  
  104. static int  sunCG2MScreenIndex;
  105.  
  106. /*-
  107.  *-----------------------------------------------------------------------
  108.  * sunCG2MSaveScreen --
  109.  *    Preserve the color screen by turning on or off the video
  110.  *
  111.  * Results:
  112.  *    None.
  113.  *
  114.  * Side Effects:
  115.  *    Video state is switched
  116.  *
  117.  *-----------------------------------------------------------------------
  118.  */
  119. static Bool
  120. sunCG2MSaveScreen (pScreen, on)
  121.     ScreenPtr      pScreen;
  122.     int          on;
  123. {
  124.     int        state;
  125.  
  126.     if (on == SCREEN_SAVER_FORCER)
  127.     SetTimeSinceLastInputEvent();
  128.     else
  129.     {
  130.     if (on == SCREEN_SAVER_ON)
  131.         state = 0;
  132.     else
  133.         state = 1;
  134.     ((CG2MPtr)sunFbs[pScreen->myNum].fb)->u_csr->u_csr.csr.video_enab = state;
  135.     }
  136.     return( TRUE );
  137. }
  138.  
  139. /*-
  140.  *-----------------------------------------------------------------------
  141.  * sunCG2MInit --
  142.  *    Attempt to find and initialize a cg2 framebuffer used as mono
  143.  *
  144.  * Results:
  145.  *    TRUE if everything went ok. FALSE if not.
  146.  *
  147.  * Side Effects:
  148.  *    Most of the elements of the ScreenRec are filled in. The
  149.  *    video is enabled for the frame buffer...
  150.  *
  151.  *-----------------------------------------------------------------------
  152.  */
  153. /*ARGSUSED*/
  154. static Bool
  155. sunCG2MInit (index, pScreen, argc, argv)
  156.     int              index;        /* The index of pScreen in the ScreenInfo */
  157.     ScreenPtr      pScreen;      /* The Screen to initialize */
  158.     int              argc;            /* The number of the Server's arguments. */
  159.     char          **argv;       /* The arguments themselves. Don't change! */
  160. {
  161.     if (!mfbScreenInit (pScreen,
  162.             (pointer)(((CG2MPtr)sunFbs[index].fb)->image),
  163.             sunFbs[index].info.fb_width,
  164.             sunFbs[index].info.fb_height,
  165.             monitorResolution, monitorResolution,
  166.             sunFbs[index].info.fb_width))
  167.     return (FALSE);
  168.  
  169.     if (!sunScreenAllocate (pScreen) || !sunScreenInit (pScreen))
  170.     return FALSE;
  171.  
  172.     pScreen->SaveScreen =   sunCG2MSaveScreen;
  173.     pScreen->whitePixel =   0;
  174.     pScreen->blackPixel =   1;
  175.  
  176.     /*
  177.      * Enable video output...
  178.      */
  179.     sunCG2MSaveScreen(pScreen, SCREEN_SAVER_OFF);
  180.  
  181.     return mfbCreateDefColormap(pScreen);
  182. }
  183.  
  184. /*-
  185.  *-----------------------------------------------------------------------
  186.  * sunCG2MProbe --
  187.  *    Attempt to find and initialize a cg2 framebuffer used as mono
  188.  *
  189.  * Results:
  190.  *    TRUE if everything went ok. FALSE if not.
  191.  *
  192.  * Side Effects:
  193.  *    Memory is allocated for the frame buffer and the buffer is mapped.
  194.  *
  195.  *-----------------------------------------------------------------------
  196.  */
  197. /*ARGSUSED*/
  198. Bool
  199. sunCG2MProbe (pScreenInfo, index, fbNum, argc, argv)
  200.     ScreenInfo      *pScreenInfo;    /* The screenInfo struct */
  201.     int              index;        /* The index of pScreen in the ScreenInfo */
  202.     int              fbNum;        /* Index into the sunFbData array */
  203.     int              argc;            /* The number of the Server's arguments. */
  204.     char          **argv;       /* The arguments themselves. Don't change! */
  205. {
  206.     int        i;
  207.     int         fd;
  208.     struct fbtype fbType;
  209.     static CG2MRec      CG2Mfb;
  210.  
  211.     if ((fd = sunOpenFrameBuffer(FBTYPE_SUN2COLOR, &fbType, index, fbNum, 
  212.         argc, argv)) < 0)
  213.     return FALSE;
  214.  
  215. #ifdef    _MAP_NEW
  216.     if ((int)(CG2Mfb.image = (union bitplane *) mmap ((caddr_t) 0,
  217.           CG2M_IMAGELEN, PROT_READ | PROT_WRITE,
  218.           MAP_SHARED | _MAP_NEW, fd, CG2M_IMAGEOFF)) == -1) {
  219.           Error ("Mapping cg2m.image");
  220.           goto bad;
  221.     }
  222.     if ((int)(CG2Mfb.u_csr = (struct cg2m_reg *) mmap ((caddr_t) 0,
  223.           CG2M_REGLEN, PROT_READ | PROT_WRITE,
  224.           MAP_SHARED | _MAP_NEW, fd, CG2M_REGOFF)) == -1) {
  225.           Error ("Mapping cg2m.reg");
  226.           goto bad;
  227.     }
  228.     if ((int)(CG2Mfb.u_ppmask = (struct cg2m_ppmask *) mmap ((caddr_t) 0,
  229.           CG2M_MASKLEN, PROT_READ | PROT_WRITE,
  230.           MAP_SHARED | _MAP_NEW, fd, CG2M_MASKOFF)) == -1) {
  231.           Error ("Mapping cg2m.reg");
  232.           goto bad;
  233.     }
  234.     if ((int)(CG2Mfb.u_cmap = (struct cg2m_cmap *) mmap ((caddr_t) 0,
  235.           CG2M_CMAPLEN, PROT_READ | PROT_WRITE,
  236.           MAP_SHARED | _MAP_NEW, fd, CG2M_CMAPOFF)) != -1) {
  237.           goto ok;
  238.     }
  239.     Error ("Mapping cg2m.cmap");
  240. #else
  241.     CG2Mfb.image = (union bitplane *)valloc (CG2M_IMAGELEN + CG2M_REGLEN +
  242.                 CG2M_MASKLEN + CG2M_CMAPLEN);
  243.     CG2Mfb.u_csr = (struct cg2m_reg *) ((char *)CG2Mfb.image +
  244.                 CG2M_IMAGELEN);
  245.     CG2Mfb.u_ppmask = (struct cg2m_ppmask *) ((char *)CG2Mfb.u_csr +
  246.                 CG2M_REGLEN);
  247.     CG2Mfb.u_cmap = (struct cg2m_cmap *) ((char *)CG2Mfb.u_ppmask +
  248.                 CG2M_MASKLEN);
  249.     if (CG2Mfb.image == (union bitplane *) NULL) {
  250.     ErrorF ("Could not allocate room for frame buffer.\n");
  251.     return FALSE;
  252.     }
  253.  
  254.     if (mmap (CG2M_IMAGE(CG2Mfb), CG2M_IMAGELEN, PROT_READ | PROT_WRITE,
  255.           MAP_SHARED, fd, CG2M_IMAGEOFF) < 0) {
  256.           Error ("Mapping cg2m.image");
  257.           goto bad;
  258.     }
  259.     if (mmap (CG2M_REG(CG2Mfb), CG2M_REGLEN, PROT_READ | PROT_WRITE,
  260.           MAP_SHARED, fd, CG2M_REGOFF) < 0) {
  261.           Error ("Mapping cg2m.reg");
  262.           goto bad;
  263.     }
  264.     if (mmap (CG2M_MASK(CG2Mfb), CG2M_MASKLEN, PROT_READ | PROT_WRITE,
  265.           MAP_SHARED, fd, CG2M_MASKOFF) < 0) {
  266.           Error ("Mapping cg2m.mask");
  267.           goto bad;
  268.     }       
  269.     if (mmap (CG2M_CMAP(CG2Mfb), CG2M_CMAPLEN, PROT_READ | PROT_WRITE,
  270.           MAP_SHARED, fd, CG2M_CMAPOFF) >= 0) {
  271.           goto ok;
  272.     }
  273.     Error ("Mapping cg2m.cmap");
  274. #endif    _MAP_NEW
  275. bad:
  276.     (void) close (fd);
  277.     return FALSE;
  278.  
  279. ok:
  280.     /*
  281.      * Enable only the first plane and make all even pixels be white,
  282.      * while all odd pixels are black.
  283.      */
  284.     CG2Mfb.u_ppmask->u_ppmask.ppmask = 1;
  285.     CG2Mfb.u_csr->u_csr.csr.update_cmap = 0;
  286.     for ( i=0; i<256; i+=2 ) {
  287.     CG2Mfb.u_cmap->u_cmap.cmap.redmap[i] =
  288.         CG2Mfb.u_cmap->u_cmap.cmap.greenmap[i] =
  289.         CG2Mfb.u_cmap->u_cmap.cmap.bluemap[i] = 255;
  290.     CG2Mfb.u_cmap->u_cmap.cmap.redmap[i+1] =
  291.         CG2Mfb.u_cmap->u_cmap.cmap.greenmap[i+1] =
  292.         CG2Mfb.u_cmap->u_cmap.cmap.bluemap[i+1] = 0;
  293.     }
  294.     CG2Mfb.u_csr->u_csr.csr.update_cmap = 1;
  295.  
  296.     sunFbs[index].fd = fd;
  297.     sunFbs[index].info = fbType;
  298.     sunFbs[index].fb = (pointer) &CG2Mfb;
  299.     sunFbs[index].EnterLeave = NoopDDA;
  300.     return TRUE;
  301. }
  302.  
  303. /*ARGSUSED*/
  304. Bool
  305. sunCG2MCreate(pScreenInfo, argc, argv)
  306.     ScreenInfo      *pScreenInfo;
  307.     int              argc;
  308.     char          **argv;
  309. {
  310.     return (AddScreen(sunCG2MInit, argc, argv) >= 0);
  311. }
  312.