home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mfb / mfbcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-19  |  5.1 KB  |  177 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: mfbcmap.c,v 5.3 89/07/19 15:48:00 rws Exp $ */
  25. #include "X.h"
  26. #include "scrnintstr.h"
  27. #include "colormapst.h"
  28. #include "resource.h"
  29.  
  30. extern int    TellLostMap(), TellGainedMap();
  31. /* A monochrome frame buffer is a static gray colormap with two entries.
  32.  * We have a "required list" of length 1.  Because we can only support 1
  33.  * colormap, we never have to change it, but we may have to change the 
  34.  * name we call it.  If someone installs a new colormap, we know it must
  35.  * look just like the old one (because we've checked in dispatch that it was
  36.  * a valid colormap identifier, and all the colormap IDs for this device
  37.  * look the same).  Nevertheless, we still have to uninstall the old colormap
  38.  * and install the new one.  Similarly, if someone uninstalls a colormap,
  39.  * we have to install the default map, even though we know those two looked
  40.  * alike.  
  41.  * The required list concept is pretty much irrelevant when you can only
  42.  * have one map installed at a time.  
  43.  */
  44. static ColormapPtr InstalledMaps[MAXSCREENS];
  45.  
  46. int
  47. mfbListInstalledColormaps(pScreen, pmaps)
  48.     ScreenPtr    pScreen;
  49.     Colormap    *pmaps;
  50. {
  51.     /* By the time we are processing requests, we can guarantee that there
  52.      * is always a colormap installed */
  53.     *pmaps = InstalledMaps[pScreen->myNum]->mid;
  54.     return (1);
  55. }
  56.  
  57.  
  58. void
  59. mfbInstallColormap(pmap)
  60.     ColormapPtr    pmap;
  61. {
  62.     int index = pmap->pScreen->myNum;
  63.     ColormapPtr oldpmap = InstalledMaps[index];
  64.  
  65.     if(pmap != oldpmap)
  66.     {
  67.     /* Uninstall pInstalledMap. No hardware changes required, just
  68.      * notify all interested parties. */
  69.     if(oldpmap != (ColormapPtr)None)
  70.         WalkTree(pmap->pScreen, TellLostMap, (pointer)&oldpmap->mid);
  71.     /* Install pmap */
  72.     InstalledMaps[index] = pmap;
  73.     WalkTree(pmap->pScreen, TellGainedMap, (pointer)&pmap->mid);
  74.  
  75.     }
  76. }
  77.  
  78. void
  79. mfbUninstallColormap(pmap)
  80.     ColormapPtr    pmap;
  81. {
  82.     int index = pmap->pScreen->myNum;
  83.     ColormapPtr curpmap = InstalledMaps[index];
  84.  
  85.     if(pmap == curpmap)
  86.     {
  87.     if (pmap->mid != pmap->pScreen->defColormap)
  88.     {
  89.         curpmap = (ColormapPtr) LookupIDByType(pmap->pScreen->defColormap,
  90.                            RT_COLORMAP);
  91.         (*pmap->pScreen->InstallColormap)(curpmap);
  92.     }
  93.     }
  94. }
  95.  
  96. /*ARGSUSED*/
  97. void
  98. mfbResolveColor (pred, pgreen, pblue, pVisual)
  99.     unsigned short    *pred;
  100.     unsigned short    *pgreen;
  101.     unsigned short    *pblue;
  102.     VisualPtr        pVisual;
  103. {
  104.     /* 
  105.      * Gets intensity from RGB.  If intensity is >= half, pick white, else
  106.      * pick black.  This may well be more trouble than it's worth.
  107.      */
  108.     *pred = *pgreen = *pblue = 
  109.         (((30L * *pred +
  110.            59L * *pgreen +
  111.            11L * *pblue) >> 8) >= (((1<<8)-1)*50)) ? ~0 : 0;
  112. }
  113.  
  114. Bool
  115. mfbCreateColormap(pMap)
  116.     ColormapPtr    pMap;
  117. {
  118.     ScreenPtr    pScreen;
  119.     unsigned short  red0, green0, blue0;
  120.     unsigned short  red1, green1, blue1;
  121.     unsigned long   pix;
  122.     
  123.     pScreen = pMap->pScreen;
  124.     if (pScreen->whitePixel == 0)
  125.     {
  126.     red0 = green0 = blue0 = ~0;
  127.     red1 = green1 = blue1 = 0;
  128.     }
  129.     else
  130.     {
  131.     red0 = green0 = blue0 = 0;
  132.     red1 = green1 = blue1 = ~0;
  133.     }
  134.  
  135.     /* this is a monochrome colormap, it only has two entries, just fill
  136.      * them in by hand.  If it were a more complex static map, it would be
  137.      * worth writing a for loop or three to initialize it */
  138.  
  139.     /* this will be pixel 0 */
  140.     pix = 0;
  141.     if (AllocColor(pMap, &red0, &green0, &blue0, &pix, 0) != Success)
  142.     return FALSE;
  143.  
  144.     /* this will be pixel 1 */
  145.     if (AllocColor(pMap, &red1, &green1, &blue1, &pix, 0) != Success)
  146.     return FALSE;
  147.     return TRUE;
  148. }
  149.  
  150. /*ARGSUSED*/
  151. void
  152. mfbDestroyColormap (pMap)
  153.     ColormapPtr    pMap;
  154. {
  155.     return;
  156. }
  157.  
  158. Bool
  159. mfbCreateDefColormap (pScreen)
  160.     ScreenPtr    pScreen;
  161. {
  162.     VisualPtr    pVisual;
  163.     ColormapPtr    pColormap;
  164.     
  165.     for (pVisual = pScreen->visuals;
  166.      pVisual->vid != pScreen->rootVisual;
  167.      pVisual++)
  168.     ;
  169.     if (CreateColormap (pScreen->defColormap, pScreen, pVisual,
  170.             &pColormap, AllocNone, 0) != Success)
  171.     {
  172.     return FALSE;
  173.     }
  174.     (*pScreen->InstallColormap) (pColormap);
  175.     return TRUE;
  176. }
  177.