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

  1. /*-
  2.  * sunCG2C.c --
  3.  *    Functions to support the sun CG2 board as a memory frame buffer.
  4.  */
  5.  
  6. /************************************************************
  7. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  8.  
  9.                     All Rights Reserved
  10.  
  11. Permission  to  use,  copy,  modify,  and  distribute   this
  12. software  and  its documentation for any purpose and without
  13. fee is hereby granted, provided that the above copyright no-
  14. tice  appear  in all copies and that both that copyright no-
  15. tice and this permission notice appear in  supporting  docu-
  16. mentation,  and  that the names of Sun or MIT not be used in
  17. advertising or publicity pertaining to distribution  of  the
  18. software  without specific prior written permission. Sun and
  19. M.I.T. make no representations about the suitability of this
  20. software for any purpose. It is provided "as is" without any
  21. express or implied warranty.
  22.  
  23. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  24. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  25. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  26. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  27. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  28. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  29. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  30. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  
  32. ********************************************************/
  33.  
  34. #ifndef    lint
  35. static char sccsid[] = "@(#)sunCG2C.c 2.8 87/06/05 Copyright 1987 Sun Micro";
  36. #endif
  37.  
  38. #include    "sun.h"
  39.  
  40. #include    <sys/mman.h>
  41. #include    <struct.h>
  42. #include    <pixrect/memreg.h>
  43. #include    <pixrect/cg2reg.h>
  44. #include    "colormap.h"
  45. #include    "colormapst.h"
  46. #include    "resource.h"
  47.  
  48. extern Bool sunCG2MProbe();
  49. #ifndef _MAP_NEW
  50. extern caddr_t valloc();
  51. #else
  52. extern caddr_t mmap();
  53. #endif    _MAP_NEW
  54.  
  55. /*-
  56.  * The cg2 frame buffer is divided into several pieces.
  57.  *    1) a stack of 8 monochrome bitplanes
  58.  *    2) an array of 8-bit pixels
  59.  *    3) a union of these two where modifications are done via RasterOp
  60.  *        chips
  61.  *    4) various control registers
  62.  *    5) a shadow colormap.
  63.  *
  64.  * Each of these things is at a given offset from the base of the 4Mb devoted
  65.  * to each color board. In addition, the mmap() system call insists on the
  66.  * address and the length to be mapped being aligned on 8K boundaries.
  67.  * 
  68.  * XXX This could be made a lot cleaner with proper use of structs and 
  69.  * sizeof()'s.
  70.  */
  71.  
  72. struct cg2c_reg {
  73.     /*
  74.      * The status register is at 0x309000.  This isn't on an 8K
  75.      * boundary, so we have to put a 4K (0x1000) pad in front of it and
  76.      * map it here at 0x308000.
  77.      */
  78.     char csr_base[4096];
  79.     union {
  80.     struct cg2statusreg csr;
  81.     char csr_pad[4096];
  82.     } u_csr;
  83. };
  84.  
  85. struct cg2c_ppmask {
  86.     /* per-plane mask, offset = 0x30A000, size = 8K */
  87.     union {
  88.     unsigned short        ppmask;
  89.     char              ppm_pad[8192];
  90.     } u_ppmask;
  91. };
  92.  
  93. struct cg2c_cmap {
  94.     /* colormap, offset = 0x310000, size = 8K */
  95.     union {
  96.     struct {      /* Shouldn't these be u_char's??? */
  97.         u_short            redmap[256];    /* Red-component map */
  98.         u_short            greenmap[256];    /* Green-component map */
  99.         u_short            bluemap[256];    /* Blue-component map */
  100.     }                 cmap;
  101.     char              cmap_pad[8192];
  102.     } u_cmap;
  103. };
  104.  
  105. typedef struct cg2c {
  106.     union byteplane *image;        /* the 8-bit memory */
  107.     struct cg2c_reg *u_csr;        /* the status register */
  108.     struct cg2c_ppmask *u_ppmask;    /* The plane mask register */
  109.     struct cg2c_cmap *u_cmap;        /* the colormap */
  110. } CG2C, CG2CRec, *CG2CPtr;
  111.  
  112. #define CG2C_IMAGE(fb)        ((caddr_t)((fb).image))
  113. #define CG2C_IMAGEOFF        ((off_t)0x00100000)
  114. #define CG2C_IMAGELEN        (sizeof(union byteplane))
  115. #define CG2C_REG(fb)        ((caddr_t)((fb).u_csr))
  116. #define CG2C_REGOFF        ((off_t)0x00308000)
  117. #define CG2C_REGLEN        (0x2000)
  118. #define CG2C_MASK(fb)        ((caddr_t)((fb).u_ppmask))
  119. #define CG2C_MASKOFF        ((off_t)0x0030A000)
  120. #define CG2C_MASKLEN        (0x2000)
  121. #define CG2C_CMAP(fb)        ((caddr_t)((fb).u_cmap))
  122. #define CG2C_CMAPOFF        ((off_t)0x00310000)
  123. #define CG2C_CMAPLEN        (0x2000)
  124.  
  125. extern int TellLostMap(), TellGainedMap();
  126.  
  127. static void
  128. sunCG2CUpdateColormap(pScreen, index, count, rmap, gmap,bmap)
  129.     ScreenPtr    pScreen;
  130.     int          index, count;
  131.     u_char      *rmap, *gmap, *bmap;
  132. {
  133.     CG2CPtr       fb;
  134.  
  135.     fb = (CG2CPtr) sunFbs[pScreen->myNum].fb;
  136. #ifdef SUN_WINDOWS
  137.     if (sunUseSunWindows()) {
  138.     static Pixwin *pw = 0;
  139.  
  140.     if (! pw) {
  141.         if ( ! (pw = pw_open(windowFd)) )
  142.         FatalError( "sunCG2CUpdateColormap: pw_open failed\n" );
  143.         pw_setcmsname(pw, "X.V11");
  144.     }
  145.     pw_putcolormap(
  146.         pw, index, count, &rmap[index], &gmap[index], &bmap[index]
  147.     );
  148.     }
  149. #endif SUN_WINDOWS
  150.  
  151.     fb->u_csr->u_csr.csr.update_cmap = 0;
  152.     while (count--) {
  153.     fb->u_cmap->u_cmap.cmap.redmap[index] = rmap[index];
  154.     fb->u_cmap->u_cmap.cmap.greenmap[index] = gmap[index];
  155.     fb->u_cmap->u_cmap.cmap.bluemap[index] = bmap[index];
  156.     index++;
  157.     }
  158.     fb->u_csr->u_csr.csr.update_cmap = 1;
  159. }
  160.  
  161. /*-
  162.  *-----------------------------------------------------------------------
  163.  * sunCG2CSaveScreen --
  164.  *    Preserve the color screen by turning on or off the video
  165.  *
  166.  * Results:
  167.  *    None.
  168.  *
  169.  * Side Effects:
  170.  *    Video state is switched
  171.  *
  172.  *-----------------------------------------------------------------------
  173.  */
  174. static Bool
  175. sunCG2CSaveScreen (pScreen, on)
  176.     ScreenPtr      pScreen;
  177.     int          on;
  178. {
  179.     int        state;
  180.  
  181.     if (on == SCREEN_SAVER_FORCER)
  182.     SetTimeSinceLastInputEvent();
  183.     else
  184.     {
  185.     if (on == SCREEN_SAVER_ON)
  186.         state = 0;
  187.     else
  188.         state = 1;
  189.     ((CG2CPtr)sunFbs[pScreen->myNum].fb)->u_csr->u_csr.csr.video_enab = state;
  190.     }
  191.     return( TRUE );
  192. }
  193.  
  194. /*-
  195.  *-----------------------------------------------------------------------
  196.  * sunCG2CInit --
  197.  *    Attempt to find and initialize a cg2 framebuffer used as mono
  198.  *
  199.  * Results:
  200.  *    TRUE if everything went ok. FALSE if not.
  201.  *
  202.  * Side Effects:
  203.  *    Most of the elements of the ScreenRec are filled in. Memory is
  204.  *    allocated for the frame buffer and the buffer is mapped. The
  205.  *    video is enabled for the frame buffer...
  206.  *
  207.  *-----------------------------------------------------------------------
  208.  */
  209. /*ARGSUSED*/
  210. static Bool
  211. sunCG2CInit (index, pScreen, argc, argv)
  212.     int              index;        /* The index of pScreen in the ScreenInfo */
  213.     ScreenPtr      pScreen;      /* The Screen to initialize */
  214.     int              argc;            /* The number of the Server's arguments. */
  215.     char          **argv;       /* The arguments themselves. Don't change! */
  216. {
  217.     sunScreenPtr    pPrivate;
  218.     if (!cfbScreenInit (pScreen,
  219.             (pointer)((CG2CPtr)sunFbs[index].fb)->image,
  220.             sunFbs[index].info.fb_width,
  221.             sunFbs[index].info.fb_height,
  222.             monitorResolution, monitorResolution,
  223.             sunFbs[index].info.fb_width))
  224.     return (FALSE);
  225.  
  226.     if (!sunScreenAllocate (pScreen))
  227.     return FALSE;
  228.  
  229.     sunCGScreenInit (pScreen);
  230.  
  231.     if (!sunScreenInit (pScreen))
  232.     return FALSE;
  233.  
  234.     pPrivate = GetScreenPrivate (pScreen);
  235.  
  236.     pScreen->SaveScreen = sunCG2CSaveScreen;
  237.     pPrivate->UpdateColormap = sunCG2CUpdateColormap;
  238.  
  239.     return cfbCreateDefColormap(pScreen);
  240. }
  241.  
  242.  
  243. /*-
  244.  *-----------------------------------------------------------------------
  245.  * sunCG2CProbe --
  246.  *    Attempt to find and initialize a cg2 framebuffer used as mono
  247.  *
  248.  * Results:
  249.  *    TRUE if everything went ok. FALSE if not.
  250.  *
  251.  * Side Effects:
  252.  *    Memory is allocated for the frame buffer and the buffer is mapped.
  253.  *
  254.  *-----------------------------------------------------------------------
  255.  */
  256. Bool
  257. sunCG2CProbe (pScreenInfo, index, fbNum, argc, argv)
  258.     ScreenInfo      *pScreenInfo;    /* The screenInfo struct */
  259.     int              index;        /* The index of pScreen in the ScreenInfo */
  260.     int              fbNum;        /* Index into the sunFbData array */
  261.     int              argc;            /* The number of the Server's arguments. */
  262.     char          **argv;       /* The arguments themselves. Don't change! */
  263. {
  264.     int        i;
  265.     int         fd;
  266.     struct fbtype    fbType;
  267.     static CG2CRec      CG2Cfb;
  268.  
  269.     /*
  270.      * See if the user wants this board to be treated as a monochrome
  271.      * display.
  272.      */
  273.     for (i = 0; i < argc; i++) {
  274.     if (strcmp (argv[i], "-mono") == 0) {
  275.         return sunCG2MProbe (pScreenInfo, index, fbNum, argc, argv);
  276.     }
  277.     }
  278.  
  279.     if ((fd = sunOpenFrameBuffer(FBTYPE_SUN2COLOR, &fbType,
  280.                  index, fbNum, argc, argv)) < 0)
  281.     return FALSE;
  282.  
  283. #ifdef    _MAP_NEW
  284.     if ((int)(CG2Cfb.image = (union byteplane *) mmap ((caddr_t) 0, CG2C_IMAGELEN, PROT_READ | PROT_WRITE,
  285.           MAP_SHARED | _MAP_NEW, fd, CG2C_IMAGEOFF)) == -1) {
  286.           Error ("Mapping cg2c.image");
  287.           goto bad;
  288.     }
  289.     if ((int)(CG2Cfb.u_csr = (struct cg2c_reg *) mmap ((caddr_t) 0, CG2C_REGLEN, PROT_READ | PROT_WRITE,
  290.           MAP_SHARED | _MAP_NEW, fd, CG2C_REGOFF)) == -1) {
  291.           Error ("Mapping cg2c.reg");
  292.           goto bad;
  293.     }
  294.     if ((int)(CG2Cfb.u_ppmask = (struct cg2c_ppmask *) mmap ((caddr_t) 0, CG2C_MASKLEN, PROT_READ | PROT_WRITE,
  295.           MAP_SHARED | _MAP_NEW, fd, CG2C_MASKOFF)) == -1) {
  296.           Error ("Mapping cg2c.reg");
  297.           goto bad;
  298.     }
  299.     if ((int)(CG2Cfb.u_cmap = (struct cg2c_cmap *) mmap ((caddr_t) 0, CG2C_CMAPLEN, PROT_READ | PROT_WRITE,
  300.           MAP_SHARED | _MAP_NEW, fd, CG2C_CMAPOFF)) != -1) {
  301.           goto ok;
  302.     }
  303.     Error ("Mapping cg2c.cmap");
  304. #else
  305.     CG2Cfb.image = (union byteplane *)valloc (CG2C_IMAGELEN + CG2C_REGLEN + CG2C_MASKLEN + CG2C_CMAPLEN);
  306.     CG2Cfb.u_csr = (struct cg2c_reg *) ((char *)CG2Cfb.image + CG2C_IMAGELEN);
  307.     CG2Cfb.u_ppmask = (struct cg2c_ppmask *) ((char *)CG2Cfb.u_csr + CG2C_REGLEN);
  308.     CG2Cfb.u_cmap = (struct cg2c_cmap *) ((char *)CG2Cfb.u_ppmask + CG2C_MASKLEN);
  309.     if (CG2Cfb.image == (union byteplane *) NULL) {
  310.     ErrorF ("Could not allocate room for frame buffer.\n");
  311.     return FALSE;
  312.     }
  313.  
  314.     if (mmap (CG2C_IMAGE(CG2Cfb), CG2C_IMAGELEN, PROT_READ | PROT_WRITE,
  315.           MAP_SHARED, fd, CG2C_IMAGEOFF) < 0) {
  316.           Error ("Mapping cg2c.image");
  317.           goto bad;
  318.     }
  319.     if (mmap (CG2C_REG(CG2Cfb), CG2C_REGLEN, PROT_READ | PROT_WRITE,
  320.           MAP_SHARED, fd, CG2C_REGOFF) < 0) {
  321.           Error ("Mapping cg2c.reg");
  322.           goto bad;
  323.     }
  324.     if (mmap (CG2C_MASK(CG2Cfb), CG2C_MASKLEN, PROT_READ | PROT_WRITE,
  325.           MAP_SHARED, fd, CG2C_MASKOFF) < 0) {
  326.           Error ("Mapping cg2c.reg");
  327.           goto bad;
  328.     }
  329.     if (mmap (CG2C_CMAP(CG2Cfb), CG2C_CMAPLEN, PROT_READ | PROT_WRITE,
  330.           MAP_SHARED, fd, CG2C_CMAPOFF) >= 0) {
  331.           goto ok;
  332.     }
  333.     Error ("Mapping cg2c.cmap");
  334. #endif    _MAP_NEW
  335. bad:
  336.     (void) close (fd);
  337.     return FALSE;
  338.  
  339. ok:
  340.     /*
  341.      * Enable all planes
  342.      */
  343.     CG2Cfb.u_ppmask->u_ppmask.ppmask = 0xFF;
  344.  
  345.     sunFbs[index].fd = fd;
  346.     sunFbs[index].info = fbType;
  347.     sunFbs[index].fb = (pointer) &CG2Cfb;
  348.     sunFbs[index].EnterLeave = NoopDDA;
  349.     sunSupportsDepth8 = TRUE;
  350.     return TRUE;
  351. }
  352.  
  353. /*ARGSUSED*/
  354. Bool
  355. sunCG2CCreate(pScreenInfo, argc, argv)
  356.     ScreenInfo      *pScreenInfo;
  357.     int              argc;
  358.     char          **argv;
  359. {
  360.     return (AddScreen(sunCG2CInit, argc, argv) >= 0);
  361. }
  362.