wincmap.c revision 05b261ec
105b261ecSmrg/* 205b261ecSmrg *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved. 305b261ecSmrg * 405b261ecSmrg *Permission is hereby granted, free of charge, to any person obtaining 505b261ecSmrg * a copy of this software and associated documentation files (the 605b261ecSmrg *"Software"), to deal in the Software without restriction, including 705b261ecSmrg *without limitation the rights to use, copy, modify, merge, publish, 805b261ecSmrg *distribute, sublicense, and/or sell copies of the Software, and to 905b261ecSmrg *permit persons to whom the Software is furnished to do so, subject to 1005b261ecSmrg *the following conditions: 1105b261ecSmrg * 1205b261ecSmrg *The above copyright notice and this permission notice shall be 1305b261ecSmrg *included in all copies or substantial portions of the Software. 1405b261ecSmrg * 1505b261ecSmrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1605b261ecSmrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1705b261ecSmrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1805b261ecSmrg *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR 1905b261ecSmrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 2005b261ecSmrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2105b261ecSmrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2205b261ecSmrg * 2305b261ecSmrg *Except as contained in this notice, the name of the XFree86 Project 2405b261ecSmrg *shall not be used in advertising or otherwise to promote the sale, use 2505b261ecSmrg *or other dealings in this Software without prior written authorization 2605b261ecSmrg *from the XFree86 Project. 2705b261ecSmrg * 2805b261ecSmrg * Authors: Dakshinamurthy Karra 2905b261ecSmrg * Suhaib M Siddiqi 3005b261ecSmrg * Peter Busch 3105b261ecSmrg * Harold L Hunt II 3205b261ecSmrg */ 3305b261ecSmrg 3405b261ecSmrg#ifdef HAVE_XWIN_CONFIG_H 3505b261ecSmrg#include <xwin-config.h> 3605b261ecSmrg#endif 3705b261ecSmrg#include "win.h" 3805b261ecSmrg 3905b261ecSmrg 4005b261ecSmrg/* 4105b261ecSmrg * Local prototypes 4205b261ecSmrg */ 4305b261ecSmrg 4405b261ecSmrgstatic int 4505b261ecSmrgwinListInstalledColormaps (ScreenPtr pScreen, Colormap *pmaps); 4605b261ecSmrg 4705b261ecSmrgstatic void 4805b261ecSmrgwinStoreColors (ColormapPtr pmap, int ndef, xColorItem *pdefs); 4905b261ecSmrg 5005b261ecSmrgstatic void 5105b261ecSmrgwinInstallColormap (ColormapPtr pmap); 5205b261ecSmrg 5305b261ecSmrgstatic void 5405b261ecSmrgwinUninstallColormap (ColormapPtr pmap); 5505b261ecSmrg 5605b261ecSmrgstatic void 5705b261ecSmrgwinResolveColor (unsigned short *pred, 5805b261ecSmrg unsigned short *pgreen, 5905b261ecSmrg unsigned short *pblue, 6005b261ecSmrg VisualPtr pVisual); 6105b261ecSmrg 6205b261ecSmrgstatic Bool 6305b261ecSmrgwinCreateColormap (ColormapPtr pmap); 6405b261ecSmrg 6505b261ecSmrgstatic void 6605b261ecSmrgwinDestroyColormap (ColormapPtr pmap); 6705b261ecSmrg 6805b261ecSmrgstatic Bool 6905b261ecSmrgwinGetPaletteDIB (ScreenPtr pScreen, ColormapPtr pcmap); 7005b261ecSmrg 7105b261ecSmrgstatic Bool 7205b261ecSmrgwinGetPaletteDD (ScreenPtr pScreen, ColormapPtr pcmap); 7305b261ecSmrg 7405b261ecSmrg 7505b261ecSmrg/* 7605b261ecSmrg * Set screen functions for colormaps 7705b261ecSmrg */ 7805b261ecSmrg 7905b261ecSmrgvoid 8005b261ecSmrgwinSetColormapFunctions (ScreenPtr pScreen) 8105b261ecSmrg{ 8205b261ecSmrg pScreen->CreateColormap = winCreateColormap; 8305b261ecSmrg pScreen->DestroyColormap = winDestroyColormap; 8405b261ecSmrg pScreen->InstallColormap = winInstallColormap; 8505b261ecSmrg pScreen->UninstallColormap = winUninstallColormap; 8605b261ecSmrg pScreen->ListInstalledColormaps = winListInstalledColormaps; 8705b261ecSmrg pScreen->StoreColors = winStoreColors; 8805b261ecSmrg pScreen->ResolveColor = winResolveColor; 8905b261ecSmrg} 9005b261ecSmrg 9105b261ecSmrg 9205b261ecSmrg/* See Porting Layer Definition - p. 30 */ 9305b261ecSmrg/* 9405b261ecSmrg * Walk the list of installed colormaps, filling the pmaps list 9505b261ecSmrg * with the resource ids of the installed maps, and return 9605b261ecSmrg * a count of the total number of installed maps. 9705b261ecSmrg */ 9805b261ecSmrgstatic int 9905b261ecSmrgwinListInstalledColormaps (ScreenPtr pScreen, Colormap *pmaps) 10005b261ecSmrg{ 10105b261ecSmrg winScreenPriv(pScreen); 10205b261ecSmrg 10305b261ecSmrg /* 10405b261ecSmrg * There will only be one installed colormap, so we only need 10505b261ecSmrg * to return one id, and the count of installed maps will always 10605b261ecSmrg * be one. 10705b261ecSmrg */ 10805b261ecSmrg *pmaps = pScreenPriv->pcmapInstalled->mid; 10905b261ecSmrg return 1; 11005b261ecSmrg} 11105b261ecSmrg 11205b261ecSmrg 11305b261ecSmrg/* See Porting Layer Definition - p. 30 */ 11405b261ecSmrg/* See Programming Windows - p. 663 */ 11505b261ecSmrgstatic void 11605b261ecSmrgwinInstallColormap (ColormapPtr pColormap) 11705b261ecSmrg{ 11805b261ecSmrg ScreenPtr pScreen = pColormap->pScreen; 11905b261ecSmrg winScreenPriv(pScreen); 12005b261ecSmrg ColormapPtr oldpmap = pScreenPriv->pcmapInstalled; 12105b261ecSmrg 12205b261ecSmrg#if CYGDEBUG 12305b261ecSmrg winDebug ("winInstallColormap\n"); 12405b261ecSmrg#endif 12505b261ecSmrg 12605b261ecSmrg /* Did the colormap actually change? */ 12705b261ecSmrg if (pColormap != oldpmap) 12805b261ecSmrg { 12905b261ecSmrg#if CYGDEBUG 13005b261ecSmrg winDebug ("winInstallColormap - Colormap has changed, attempt " 13105b261ecSmrg "to install.\n"); 13205b261ecSmrg#endif 13305b261ecSmrg 13405b261ecSmrg /* Was there a previous colormap? */ 13505b261ecSmrg if (oldpmap != (ColormapPtr) None) 13605b261ecSmrg { 13705b261ecSmrg /* There was a previous colormap; tell clients it is gone */ 13805b261ecSmrg WalkTree (pColormap->pScreen, TellLostMap, (char *)&oldpmap->mid); 13905b261ecSmrg } 14005b261ecSmrg 14105b261ecSmrg /* Install new colormap */ 14205b261ecSmrg pScreenPriv->pcmapInstalled = pColormap; 14305b261ecSmrg WalkTree (pColormap->pScreen, TellGainedMap, (char *)&pColormap->mid); 14405b261ecSmrg 14505b261ecSmrg /* Call the engine specific colormap install procedure */ 14605b261ecSmrg if (!((*pScreenPriv->pwinInstallColormap) (pColormap))) 14705b261ecSmrg { 14805b261ecSmrg winErrorFVerb (2, "winInstallColormap - Screen specific colormap install " 14905b261ecSmrg "procedure failed. Continuing, but colors may be " 15005b261ecSmrg "messed up from now on.\n"); 15105b261ecSmrg } 15205b261ecSmrg } 15305b261ecSmrg 15405b261ecSmrg /* Save a pointer to the newly installed colormap */ 15505b261ecSmrg pScreenPriv->pcmapInstalled = pColormap; 15605b261ecSmrg} 15705b261ecSmrg 15805b261ecSmrg 15905b261ecSmrg/* See Porting Layer Definition - p. 30 */ 16005b261ecSmrgstatic void 16105b261ecSmrgwinUninstallColormap (ColormapPtr pmap) 16205b261ecSmrg{ 16305b261ecSmrg winScreenPriv(pmap->pScreen); 16405b261ecSmrg ColormapPtr curpmap = pScreenPriv->pcmapInstalled; 16505b261ecSmrg 16605b261ecSmrg#if CYGDEBUG 16705b261ecSmrg winDebug ("winUninstallColormap\n"); 16805b261ecSmrg#endif 16905b261ecSmrg 17005b261ecSmrg /* Is the colormap currently installed? */ 17105b261ecSmrg if (pmap != curpmap) 17205b261ecSmrg { 17305b261ecSmrg /* Colormap not installed, nothing to do */ 17405b261ecSmrg return; 17505b261ecSmrg } 17605b261ecSmrg 17705b261ecSmrg /* Clear the installed colormap flag */ 17805b261ecSmrg pScreenPriv->pcmapInstalled = NULL; 17905b261ecSmrg 18005b261ecSmrg /* 18105b261ecSmrg * NOTE: The default colormap does not get "uninstalled" before 18205b261ecSmrg * it is destroyed. 18305b261ecSmrg */ 18405b261ecSmrg 18505b261ecSmrg /* Install the default cmap in place of the cmap to be uninstalled */ 18605b261ecSmrg if (pmap->mid != pmap->pScreen->defColormap) 18705b261ecSmrg { 18805b261ecSmrg curpmap = (ColormapPtr) LookupIDByType(pmap->pScreen->defColormap, 18905b261ecSmrg RT_COLORMAP); 19005b261ecSmrg (*pmap->pScreen->InstallColormap) (curpmap); 19105b261ecSmrg } 19205b261ecSmrg} 19305b261ecSmrg 19405b261ecSmrg 19505b261ecSmrg/* See Porting Layer Definition - p. 30 */ 19605b261ecSmrgstatic void 19705b261ecSmrgwinStoreColors (ColormapPtr pmap, 19805b261ecSmrg int ndef, 19905b261ecSmrg xColorItem *pdefs) 20005b261ecSmrg{ 20105b261ecSmrg ScreenPtr pScreen = pmap->pScreen; 20205b261ecSmrg winScreenPriv(pScreen); 20305b261ecSmrg winCmapPriv(pmap); 20405b261ecSmrg int i; 20505b261ecSmrg unsigned short nRed, nGreen, nBlue; 20605b261ecSmrg 20705b261ecSmrg#if CYGDEBUG 20805b261ecSmrg if (ndef != 1) 20905b261ecSmrg winDebug ("winStoreColors - ndef: %d\n", 21005b261ecSmrg ndef); 21105b261ecSmrg#endif 21205b261ecSmrg 21305b261ecSmrg /* Save the new colors in the colormap privates */ 21405b261ecSmrg for (i = 0; i < ndef; ++i) 21505b261ecSmrg { 21605b261ecSmrg /* Adjust the colors from the X color spec to the Windows color spec */ 21705b261ecSmrg nRed = pdefs[i].red >> 8; 21805b261ecSmrg nGreen = pdefs[i].green >> 8; 21905b261ecSmrg nBlue = pdefs[i].blue >> 8; 22005b261ecSmrg 22105b261ecSmrg /* Copy the colors to a palette entry table */ 22205b261ecSmrg pCmapPriv->peColors[pdefs[0].pixel + i].peRed = nRed; 22305b261ecSmrg pCmapPriv->peColors[pdefs[0].pixel + i].peGreen = nGreen; 22405b261ecSmrg pCmapPriv->peColors[pdefs[0].pixel + i].peBlue = nBlue; 22505b261ecSmrg 22605b261ecSmrg /* Copy the colors to a RGBQUAD table */ 22705b261ecSmrg pCmapPriv->rgbColors[pdefs[0].pixel + i].rgbRed = nRed; 22805b261ecSmrg pCmapPriv->rgbColors[pdefs[0].pixel + i].rgbGreen = nGreen; 22905b261ecSmrg pCmapPriv->rgbColors[pdefs[0].pixel + i].rgbBlue = nBlue; 23005b261ecSmrg 23105b261ecSmrg#if CYGDEBUG 23205b261ecSmrg winDebug ("winStoreColors - nRed %d nGreen %d nBlue %d\n", 23305b261ecSmrg nRed, nGreen, nBlue); 23405b261ecSmrg#endif 23505b261ecSmrg } 23605b261ecSmrg 23705b261ecSmrg /* Call the engine specific store colors procedure */ 23805b261ecSmrg if (!((pScreenPriv->pwinStoreColors) (pmap, ndef, pdefs))) 23905b261ecSmrg { 24005b261ecSmrg winErrorFVerb (2, "winStoreColors - Engine cpecific color storage procedure " 24105b261ecSmrg "failed. Continuing, but colors may be messed up from now " 24205b261ecSmrg "on.\n"); 24305b261ecSmrg } 24405b261ecSmrg} 24505b261ecSmrg 24605b261ecSmrg 24705b261ecSmrg/* See Porting Layer Definition - p. 30 */ 24805b261ecSmrgstatic void 24905b261ecSmrgwinResolveColor (unsigned short *pred, 25005b261ecSmrg unsigned short *pgreen, 25105b261ecSmrg unsigned short *pblue, 25205b261ecSmrg VisualPtr pVisual) 25305b261ecSmrg{ 25405b261ecSmrg#if CYGDEBUG 25505b261ecSmrg winDebug ("winResolveColor ()\n"); 25605b261ecSmrg#endif 25705b261ecSmrg 25805b261ecSmrg miResolveColor (pred, pgreen, pblue, pVisual); 25905b261ecSmrg} 26005b261ecSmrg 26105b261ecSmrg 26205b261ecSmrg/* See Porting Layer Definition - p. 29 */ 26305b261ecSmrgstatic Bool 26405b261ecSmrgwinCreateColormap (ColormapPtr pmap) 26505b261ecSmrg{ 26605b261ecSmrg winPrivCmapPtr pCmapPriv = NULL; 26705b261ecSmrg ScreenPtr pScreen = pmap->pScreen; 26805b261ecSmrg winScreenPriv(pScreen); 26905b261ecSmrg 27005b261ecSmrg#if CYGDEBUG 27105b261ecSmrg winDebug ("winCreateColormap\n"); 27205b261ecSmrg#endif 27305b261ecSmrg 27405b261ecSmrg /* Allocate colormap privates */ 27505b261ecSmrg if (!winAllocateCmapPrivates (pmap)) 27605b261ecSmrg { 27705b261ecSmrg ErrorF ("winCreateColorma - Couldn't allocate cmap privates\n"); 27805b261ecSmrg return FALSE; 27905b261ecSmrg } 28005b261ecSmrg 28105b261ecSmrg /* Get a pointer to the newly allocated privates */ 28205b261ecSmrg pCmapPriv = winGetCmapPriv (pmap); 28305b261ecSmrg 28405b261ecSmrg /* 28505b261ecSmrg * FIXME: This is some evil hackery to help in handling some X clients 28605b261ecSmrg * that expect the top pixel to be white. This "help" only lasts until 28705b261ecSmrg * some client overwrites the top colormap entry. 28805b261ecSmrg * 28905b261ecSmrg * We don't want to actually allocate the top entry, as that causes 29005b261ecSmrg * problems with X clients that need 7 planes (128 colors) in the default 29105b261ecSmrg * colormap, such as Magic 7.1. 29205b261ecSmrg */ 29305b261ecSmrg pCmapPriv->rgbColors[WIN_NUM_PALETTE_ENTRIES - 1].rgbRed = 255; 29405b261ecSmrg pCmapPriv->rgbColors[WIN_NUM_PALETTE_ENTRIES - 1].rgbGreen = 255; 29505b261ecSmrg pCmapPriv->rgbColors[WIN_NUM_PALETTE_ENTRIES - 1].rgbBlue = 255; 29605b261ecSmrg pCmapPriv->peColors[WIN_NUM_PALETTE_ENTRIES - 1].peRed = 255; 29705b261ecSmrg pCmapPriv->peColors[WIN_NUM_PALETTE_ENTRIES - 1].peGreen = 255; 29805b261ecSmrg pCmapPriv->peColors[WIN_NUM_PALETTE_ENTRIES - 1].peBlue = 255; 29905b261ecSmrg 30005b261ecSmrg /* Call the engine specific colormap initialization procedure */ 30105b261ecSmrg if (!((*pScreenPriv->pwinCreateColormap) (pmap))) 30205b261ecSmrg { 30305b261ecSmrg ErrorF ("winCreateColormap - Engine specific colormap creation " 30405b261ecSmrg "procedure failed. Aborting.\n"); 30505b261ecSmrg return FALSE; 30605b261ecSmrg } 30705b261ecSmrg 30805b261ecSmrg return TRUE; 30905b261ecSmrg} 31005b261ecSmrg 31105b261ecSmrg 31205b261ecSmrg/* See Porting Layer Definition - p. 29, 30 */ 31305b261ecSmrgstatic void 31405b261ecSmrgwinDestroyColormap (ColormapPtr pColormap) 31505b261ecSmrg{ 31605b261ecSmrg winScreenPriv(pColormap->pScreen); 31705b261ecSmrg winCmapPriv(pColormap); 31805b261ecSmrg 31905b261ecSmrg /* Call the engine specific colormap destruction procedure */ 32005b261ecSmrg if (!((*pScreenPriv->pwinDestroyColormap) (pColormap))) 32105b261ecSmrg { 32205b261ecSmrg winErrorFVerb (2, "winDestroyColormap - Engine specific colormap destruction " 32305b261ecSmrg "procedure failed. Continuing, but it is possible that memory " 32405b261ecSmrg "was leaked, or that colors will be messed up from now on.\n"); 32505b261ecSmrg } 32605b261ecSmrg 32705b261ecSmrg /* Free the colormap privates */ 32805b261ecSmrg free (pCmapPriv); 32905b261ecSmrg winSetCmapPriv (pColormap, NULL); 33005b261ecSmrg 33105b261ecSmrg#if CYGDEBUG 33205b261ecSmrg winDebug ("winDestroyColormap - Returning\n"); 33305b261ecSmrg#endif 33405b261ecSmrg} 33505b261ecSmrg 33605b261ecSmrg 33705b261ecSmrg/* 33805b261ecSmrg * Internal function to load the palette used by the Shadow DIB 33905b261ecSmrg */ 34005b261ecSmrg 34105b261ecSmrgstatic Bool 34205b261ecSmrgwinGetPaletteDIB (ScreenPtr pScreen, ColormapPtr pcmap) 34305b261ecSmrg{ 34405b261ecSmrg winScreenPriv(pScreen); 34505b261ecSmrg int i; 34605b261ecSmrg Pixel pixel; /* Pixel == CARD32 */ 34705b261ecSmrg CARD16 nRed, nGreen, nBlue; /* CARD16 == unsigned short */ 34805b261ecSmrg UINT uiColorsRetrieved = 0; 34905b261ecSmrg RGBQUAD rgbColors[WIN_NUM_PALETTE_ENTRIES]; 35005b261ecSmrg 35105b261ecSmrg /* Get the color table for the screen */ 35205b261ecSmrg uiColorsRetrieved = GetDIBColorTable (pScreenPriv->hdcScreen, 35305b261ecSmrg 0, 35405b261ecSmrg WIN_NUM_PALETTE_ENTRIES, 35505b261ecSmrg rgbColors); 35605b261ecSmrg if (uiColorsRetrieved == 0) 35705b261ecSmrg { 35805b261ecSmrg ErrorF ("winGetPaletteDIB - Could not retrieve screen color table\n"); 35905b261ecSmrg return FALSE; 36005b261ecSmrg } 36105b261ecSmrg 36205b261ecSmrg#if CYGDEBUG 36305b261ecSmrg winDebug ("winGetPaletteDIB - Retrieved %d colors from DIB\n", 36405b261ecSmrg uiColorsRetrieved); 36505b261ecSmrg#endif 36605b261ecSmrg 36705b261ecSmrg /* Set the DIB color table to the default screen palette */ 36805b261ecSmrg if (SetDIBColorTable (pScreenPriv->hdcShadow, 36905b261ecSmrg 0, 37005b261ecSmrg uiColorsRetrieved, 37105b261ecSmrg rgbColors) == 0) 37205b261ecSmrg { 37305b261ecSmrg ErrorF ("winGetPaletteDIB - SetDIBColorTable () failed\n"); 37405b261ecSmrg return FALSE; 37505b261ecSmrg } 37605b261ecSmrg 37705b261ecSmrg /* Alloc each color in the DIB color table */ 37805b261ecSmrg for (i = 0; i < uiColorsRetrieved; ++i) 37905b261ecSmrg { 38005b261ecSmrg pixel = i; 38105b261ecSmrg 38205b261ecSmrg /* Extract the color values for current palette entry */ 38305b261ecSmrg nRed = rgbColors[i].rgbRed << 8; 38405b261ecSmrg nGreen = rgbColors[i].rgbGreen << 8; 38505b261ecSmrg nBlue = rgbColors[i].rgbBlue << 8; 38605b261ecSmrg 38705b261ecSmrg#if CYGDEBUG 38805b261ecSmrg winDebug ("winGetPaletteDIB - Allocating a color: %d; " 38905b261ecSmrg "%d %d %d\n", 39005b261ecSmrg pixel, nRed, nGreen, nBlue); 39105b261ecSmrg#endif 39205b261ecSmrg 39305b261ecSmrg /* Allocate a entry in the X colormap */ 39405b261ecSmrg if (AllocColor (pcmap, 39505b261ecSmrg &nRed, 39605b261ecSmrg &nGreen, 39705b261ecSmrg &nBlue, 39805b261ecSmrg &pixel, 39905b261ecSmrg 0) != Success) 40005b261ecSmrg { 40105b261ecSmrg ErrorF ("winGetPaletteDIB - AllocColor () failed, pixel %d\n", 40205b261ecSmrg i); 40305b261ecSmrg return FALSE; 40405b261ecSmrg } 40505b261ecSmrg 40605b261ecSmrg if (i != pixel 40705b261ecSmrg || nRed != rgbColors[i].rgbRed 40805b261ecSmrg || nGreen != rgbColors[i].rgbGreen 40905b261ecSmrg || nBlue != rgbColors[i].rgbBlue) 41005b261ecSmrg { 41105b261ecSmrg winDebug ("winGetPaletteDIB - Got: %d; " 41205b261ecSmrg "%d %d %d\n", 41305b261ecSmrg (int) pixel, nRed, nGreen, nBlue); 41405b261ecSmrg } 41505b261ecSmrg 41605b261ecSmrg /* FIXME: Not sure that this bit is needed at all */ 41705b261ecSmrg pcmap->red[i].co.local.red = nRed; 41805b261ecSmrg pcmap->red[i].co.local.green = nGreen; 41905b261ecSmrg pcmap->red[i].co.local.blue = nBlue; 42005b261ecSmrg } 42105b261ecSmrg 42205b261ecSmrg /* System is using a colormap */ 42305b261ecSmrg /* Set the black and white pixel indices */ 42405b261ecSmrg pScreen->whitePixel = uiColorsRetrieved - 1; 42505b261ecSmrg pScreen->blackPixel = 0; 42605b261ecSmrg 42705b261ecSmrg return TRUE; 42805b261ecSmrg} 42905b261ecSmrg 43005b261ecSmrg 43105b261ecSmrg/* 43205b261ecSmrg * Internal function to load the standard system palette being used by DD 43305b261ecSmrg */ 43405b261ecSmrg 43505b261ecSmrgstatic Bool 43605b261ecSmrgwinGetPaletteDD (ScreenPtr pScreen, ColormapPtr pcmap) 43705b261ecSmrg{ 43805b261ecSmrg int i; 43905b261ecSmrg Pixel pixel; /* Pixel == CARD32 */ 44005b261ecSmrg CARD16 nRed, nGreen, nBlue; /* CARD16 == unsigned short */ 44105b261ecSmrg UINT uiSystemPaletteEntries; 44205b261ecSmrg LPPALETTEENTRY ppeColors = NULL; 44305b261ecSmrg HDC hdc = NULL; 44405b261ecSmrg 44505b261ecSmrg /* Get a DC to obtain the default palette */ 44605b261ecSmrg hdc = GetDC (NULL); 44705b261ecSmrg if (hdc == NULL) 44805b261ecSmrg { 44905b261ecSmrg ErrorF ("winGetPaletteDD - Couldn't get a DC\n"); 45005b261ecSmrg return FALSE; 45105b261ecSmrg } 45205b261ecSmrg 45305b261ecSmrg /* Get the number of entries in the system palette */ 45405b261ecSmrg uiSystemPaletteEntries = GetSystemPaletteEntries (hdc, 45505b261ecSmrg 0, 0, NULL); 45605b261ecSmrg if (uiSystemPaletteEntries == 0) 45705b261ecSmrg { 45805b261ecSmrg ErrorF ("winGetPaletteDD - Unable to determine number of " 45905b261ecSmrg "system palette entries\n"); 46005b261ecSmrg return FALSE; 46105b261ecSmrg } 46205b261ecSmrg 46305b261ecSmrg#if CYGDEBUG 46405b261ecSmrg winDebug ("winGetPaletteDD - uiSystemPaletteEntries %d\n", 46505b261ecSmrg uiSystemPaletteEntries); 46605b261ecSmrg#endif 46705b261ecSmrg 46805b261ecSmrg /* Allocate palette entries structure */ 46905b261ecSmrg ppeColors = malloc (uiSystemPaletteEntries * sizeof (PALETTEENTRY)); 47005b261ecSmrg if (ppeColors == NULL) 47105b261ecSmrg { 47205b261ecSmrg ErrorF ("winGetPaletteDD - malloc () for colormap failed\n"); 47305b261ecSmrg return FALSE; 47405b261ecSmrg } 47505b261ecSmrg 47605b261ecSmrg /* Get system palette entries */ 47705b261ecSmrg GetSystemPaletteEntries (hdc, 47805b261ecSmrg 0, uiSystemPaletteEntries, ppeColors); 47905b261ecSmrg 48005b261ecSmrg /* Allocate an X colormap entry for every system palette entry */ 48105b261ecSmrg for (i = 0; i < uiSystemPaletteEntries; ++i) 48205b261ecSmrg { 48305b261ecSmrg pixel = i; 48405b261ecSmrg 48505b261ecSmrg /* Extract the color values for current palette entry */ 48605b261ecSmrg nRed = ppeColors[i].peRed << 8; 48705b261ecSmrg nGreen = ppeColors[i].peGreen << 8; 48805b261ecSmrg nBlue = ppeColors[i].peBlue << 8; 48905b261ecSmrg#if CYGDEBUG 49005b261ecSmrg winDebug ("winGetPaletteDD - Allocating a color: %d; " 49105b261ecSmrg "%d %d %d\n", 49205b261ecSmrg pixel, nRed, nGreen, nBlue); 49305b261ecSmrg#endif 49405b261ecSmrg if (AllocColor (pcmap, 49505b261ecSmrg &nRed, 49605b261ecSmrg &nGreen, 49705b261ecSmrg &nBlue, 49805b261ecSmrg &pixel, 49905b261ecSmrg 0) != Success) 50005b261ecSmrg { 50105b261ecSmrg ErrorF ("winGetPaletteDD - AllocColor () failed, pixel %d\n", 50205b261ecSmrg i); 50305b261ecSmrg free (ppeColors); 50405b261ecSmrg ppeColors = NULL; 50505b261ecSmrg return FALSE; 50605b261ecSmrg } 50705b261ecSmrg 50805b261ecSmrg pcmap->red[i].co.local.red = nRed; 50905b261ecSmrg pcmap->red[i].co.local.green = nGreen; 51005b261ecSmrg pcmap->red[i].co.local.blue = nBlue; 51105b261ecSmrg } 51205b261ecSmrg 51305b261ecSmrg /* System is using a colormap */ 51405b261ecSmrg /* Set the black and white pixel indices */ 51505b261ecSmrg pScreen->whitePixel = uiSystemPaletteEntries - 1; 51605b261ecSmrg pScreen->blackPixel = 0; 51705b261ecSmrg 51805b261ecSmrg /* Free colormap */ 51905b261ecSmrg if (ppeColors != NULL) 52005b261ecSmrg { 52105b261ecSmrg free (ppeColors); 52205b261ecSmrg ppeColors = NULL; 52305b261ecSmrg } 52405b261ecSmrg 52505b261ecSmrg /* Free the DC */ 52605b261ecSmrg if (hdc != NULL) 52705b261ecSmrg { 52805b261ecSmrg ReleaseDC (NULL, hdc); 52905b261ecSmrg hdc = NULL; 53005b261ecSmrg } 53105b261ecSmrg 53205b261ecSmrg return TRUE; 53305b261ecSmrg} 53405b261ecSmrg 53505b261ecSmrg 53605b261ecSmrg/* 53705b261ecSmrg * Install the standard fb colormap, or the GDI colormap, 53805b261ecSmrg * depending on the current screen depth. 53905b261ecSmrg */ 54005b261ecSmrg 54105b261ecSmrgBool 54205b261ecSmrgwinCreateDefColormap (ScreenPtr pScreen) 54305b261ecSmrg{ 54405b261ecSmrg winScreenPriv(pScreen); 54505b261ecSmrg winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo; 54605b261ecSmrg unsigned short zero = 0, ones = 0xFFFF; 54705b261ecSmrg VisualPtr pVisual = pScreenPriv->pRootVisual; 54805b261ecSmrg ColormapPtr pcmap = NULL; 54905b261ecSmrg Pixel wp, bp; 55005b261ecSmrg 55105b261ecSmrg#if CYGDEBUG 55205b261ecSmrg winDebug ("winCreateDefColormap\n"); 55305b261ecSmrg#endif 55405b261ecSmrg 55505b261ecSmrg /* Use standard fb colormaps for non palettized color modes */ 55605b261ecSmrg if (pScreenInfo->dwBPP > 8) 55705b261ecSmrg { 55805b261ecSmrg winDebug ("winCreateDefColormap - Deferring to " \ 55905b261ecSmrg "fbCreateDefColormap ()\n"); 56005b261ecSmrg return fbCreateDefColormap (pScreen); 56105b261ecSmrg } 56205b261ecSmrg 56305b261ecSmrg /* 56405b261ecSmrg * AllocAll for non-Dynamic visual classes, 56505b261ecSmrg * AllocNone for Dynamic visual classes. 56605b261ecSmrg */ 56705b261ecSmrg 56805b261ecSmrg /* 56905b261ecSmrg * Dynamic visual classes allow the colors of the color map 57005b261ecSmrg * to be changed by clients. 57105b261ecSmrg */ 57205b261ecSmrg 57305b261ecSmrg#if CYGDEBUG 57405b261ecSmrg winDebug ("winCreateDefColormap - defColormap: %d\n", 57505b261ecSmrg pScreen->defColormap); 57605b261ecSmrg#endif 57705b261ecSmrg 57805b261ecSmrg /* Allocate an X colormap, owned by client 0 */ 57905b261ecSmrg if (CreateColormap (pScreen->defColormap, 58005b261ecSmrg pScreen, 58105b261ecSmrg pVisual, 58205b261ecSmrg &pcmap, 58305b261ecSmrg (pVisual->class & DynamicClass) ? AllocNone : AllocAll, 58405b261ecSmrg 0) != Success) 58505b261ecSmrg { 58605b261ecSmrg ErrorF ("winCreateDefColormap - CreateColormap failed\n"); 58705b261ecSmrg return FALSE; 58805b261ecSmrg } 58905b261ecSmrg if (pcmap == NULL) 59005b261ecSmrg { 59105b261ecSmrg ErrorF ("winCreateDefColormap - Colormap could not be created\n"); 59205b261ecSmrg return FALSE; 59305b261ecSmrg } 59405b261ecSmrg 59505b261ecSmrg#if CYGDEBUG 59605b261ecSmrg winDebug ("winCreateDefColormap - Created a colormap\n"); 59705b261ecSmrg#endif 59805b261ecSmrg 59905b261ecSmrg /* Branch on the visual class */ 60005b261ecSmrg if (!(pVisual->class & DynamicClass)) 60105b261ecSmrg { 60205b261ecSmrg /* Branch on engine type */ 60305b261ecSmrg if (pScreenInfo->dwEngine == WIN_SERVER_SHADOW_GDI) 60405b261ecSmrg { 60505b261ecSmrg /* Load the colors being used by the Shadow DIB */ 60605b261ecSmrg if (!winGetPaletteDIB (pScreen, pcmap)) 60705b261ecSmrg { 60805b261ecSmrg ErrorF ("winCreateDefColormap - Couldn't get DIB colors\n"); 60905b261ecSmrg return FALSE; 61005b261ecSmrg } 61105b261ecSmrg } 61205b261ecSmrg else 61305b261ecSmrg { 61405b261ecSmrg /* Load the colors from the default system palette */ 61505b261ecSmrg if (!winGetPaletteDD (pScreen, pcmap)) 61605b261ecSmrg { 61705b261ecSmrg ErrorF ("winCreateDefColormap - Couldn't get colors " 61805b261ecSmrg "for DD\n"); 61905b261ecSmrg return FALSE; 62005b261ecSmrg } 62105b261ecSmrg } 62205b261ecSmrg } 62305b261ecSmrg else 62405b261ecSmrg { 62505b261ecSmrg wp = pScreen->whitePixel; 62605b261ecSmrg bp = pScreen->blackPixel; 62705b261ecSmrg 62805b261ecSmrg /* Allocate a black and white pixel */ 62905b261ecSmrg if ((AllocColor (pcmap, &ones, &ones, &ones, &wp, 0) != 63005b261ecSmrg Success) 63105b261ecSmrg || 63205b261ecSmrg (AllocColor (pcmap, &zero, &zero, &zero, &bp, 0) != 63305b261ecSmrg Success)) 63405b261ecSmrg { 63505b261ecSmrg ErrorF ("winCreateDefColormap - Couldn't allocate bp or wp\n"); 63605b261ecSmrg return FALSE; 63705b261ecSmrg } 63805b261ecSmrg 63905b261ecSmrg pScreen->whitePixel = wp; 64005b261ecSmrg pScreen->blackPixel = bp; 64105b261ecSmrg 64205b261ecSmrg#if 0 64305b261ecSmrg /* Have to reserve first 10 and last ten pixels in DirectDraw windowed */ 64405b261ecSmrg if (pScreenInfo->dwEngine != WIN_SERVER_SHADOW_GDI) 64505b261ecSmrg { 64605b261ecSmrg int k; 64705b261ecSmrg Pixel p; 64805b261ecSmrg 64905b261ecSmrg for (k = 1; k < 10; ++k) 65005b261ecSmrg { 65105b261ecSmrg p = k; 65205b261ecSmrg if (AllocColor (pcmap, &ones, &ones, &ones, &p, 0) != Success) 65305b261ecSmrg FatalError ("Foo!\n"); 65405b261ecSmrg } 65505b261ecSmrg 65605b261ecSmrg for (k = 245; k < 255; ++k) 65705b261ecSmrg { 65805b261ecSmrg p = k; 65905b261ecSmrg if (AllocColor (pcmap, &zero, &zero, &zero, &p, 0) != Success) 66005b261ecSmrg FatalError ("Baz!\n"); 66105b261ecSmrg } 66205b261ecSmrg } 66305b261ecSmrg#endif 66405b261ecSmrg } 66505b261ecSmrg 66605b261ecSmrg /* Install the created colormap */ 66705b261ecSmrg (*pScreen->InstallColormap)(pcmap); 66805b261ecSmrg 66905b261ecSmrg#if CYGDEBUG 67005b261ecSmrg winDebug ("winCreateDefColormap - Returning\n"); 67105b261ecSmrg#endif 67205b261ecSmrg 67305b261ecSmrg return TRUE; 67405b261ecSmrg} 675