1706f2543Smrg/*
2706f2543Smrg *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
3706f2543Smrg *
4706f2543Smrg *Permission is hereby granted, free of charge, to any person obtaining
5706f2543Smrg * a copy of this software and associated documentation files (the
6706f2543Smrg *"Software"), to deal in the Software without restriction, including
7706f2543Smrg *without limitation the rights to use, copy, modify, merge, publish,
8706f2543Smrg *distribute, sublicense, and/or sell copies of the Software, and to
9706f2543Smrg *permit persons to whom the Software is furnished to do so, subject to
10706f2543Smrg *the following conditions:
11706f2543Smrg *
12706f2543Smrg *The above copyright notice and this permission notice shall be
13706f2543Smrg *included in all copies or substantial portions of the Software.
14706f2543Smrg *
15706f2543Smrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16706f2543Smrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17706f2543Smrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18706f2543Smrg *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
19706f2543Smrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20706f2543Smrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21706f2543Smrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22706f2543Smrg *
23706f2543Smrg *Except as contained in this notice, the name of the XFree86 Project
24706f2543Smrg *shall not be used in advertising or otherwise to promote the sale, use
25706f2543Smrg *or other dealings in this Software without prior written authorization
26706f2543Smrg *from the XFree86 Project.
27706f2543Smrg *
28706f2543Smrg * Authors:	Dakshinamurthy Karra
29706f2543Smrg *		Suhaib M Siddiqi
30706f2543Smrg *		Peter Busch
31706f2543Smrg *		Harold L Hunt II
32706f2543Smrg */
33706f2543Smrg
34706f2543Smrg#ifdef HAVE_XWIN_CONFIG_H
35706f2543Smrg#include <xwin-config.h>
36706f2543Smrg#endif
37706f2543Smrg#include "win.h"
38706f2543Smrg
39706f2543Smrg
40706f2543Smrg/*
41706f2543Smrg * Local prototypes
42706f2543Smrg */
43706f2543Smrg
44706f2543Smrgstatic int
45706f2543SmrgwinListInstalledColormaps (ScreenPtr pScreen, Colormap *pmaps);
46706f2543Smrg
47706f2543Smrgstatic void
48706f2543SmrgwinStoreColors (ColormapPtr pmap, int ndef, xColorItem *pdefs);
49706f2543Smrg
50706f2543Smrgstatic void
51706f2543SmrgwinInstallColormap (ColormapPtr pmap);
52706f2543Smrg
53706f2543Smrgstatic void
54706f2543SmrgwinUninstallColormap (ColormapPtr pmap);
55706f2543Smrg
56706f2543Smrgstatic void
57706f2543SmrgwinResolveColor (unsigned short *pred,
58706f2543Smrg		 unsigned short *pgreen,
59706f2543Smrg		 unsigned short *pblue,
60706f2543Smrg		 VisualPtr	pVisual);
61706f2543Smrg
62706f2543Smrgstatic Bool
63706f2543SmrgwinCreateColormap (ColormapPtr pmap);
64706f2543Smrg
65706f2543Smrgstatic void
66706f2543SmrgwinDestroyColormap (ColormapPtr pmap);
67706f2543Smrg
68706f2543Smrgstatic Bool
69706f2543SmrgwinGetPaletteDIB (ScreenPtr pScreen, ColormapPtr pcmap);
70706f2543Smrg
71706f2543Smrgstatic Bool
72706f2543SmrgwinGetPaletteDD (ScreenPtr pScreen, ColormapPtr pcmap);
73706f2543Smrg
74706f2543Smrg
75706f2543Smrg/*
76706f2543Smrg * Set screen functions for colormaps
77706f2543Smrg */
78706f2543Smrg
79706f2543Smrgvoid
80706f2543SmrgwinSetColormapFunctions (ScreenPtr pScreen)
81706f2543Smrg{
82706f2543Smrg  pScreen->CreateColormap = winCreateColormap;
83706f2543Smrg  pScreen->DestroyColormap = winDestroyColormap;
84706f2543Smrg  pScreen->InstallColormap = winInstallColormap;
85706f2543Smrg  pScreen->UninstallColormap = winUninstallColormap;
86706f2543Smrg  pScreen->ListInstalledColormaps = winListInstalledColormaps;
87706f2543Smrg  pScreen->StoreColors = winStoreColors;
88706f2543Smrg  pScreen->ResolveColor = winResolveColor;
89706f2543Smrg}
90706f2543Smrg
91706f2543Smrg
92706f2543Smrg/* See Porting Layer Definition - p. 30 */
93706f2543Smrg/*
94706f2543Smrg * Walk the list of installed colormaps, filling the pmaps list
95706f2543Smrg * with the resource ids of the installed maps, and return
96706f2543Smrg * a count of the total number of installed maps.
97706f2543Smrg */
98706f2543Smrgstatic int
99706f2543SmrgwinListInstalledColormaps (ScreenPtr pScreen, Colormap *pmaps)
100706f2543Smrg{
101706f2543Smrg  winScreenPriv(pScreen);
102706f2543Smrg
103706f2543Smrg  /*
104706f2543Smrg   * There will only be one installed colormap, so we only need
105706f2543Smrg   * to return one id, and the count of installed maps will always
106706f2543Smrg   * be one.
107706f2543Smrg   */
108706f2543Smrg  *pmaps = pScreenPriv->pcmapInstalled->mid;
109706f2543Smrg  return 1;
110706f2543Smrg}
111706f2543Smrg
112706f2543Smrg
113706f2543Smrg/* See Porting Layer Definition - p. 30 */
114706f2543Smrg/* See Programming Windows - p. 663 */
115706f2543Smrgstatic void
116706f2543SmrgwinInstallColormap (ColormapPtr pColormap)
117706f2543Smrg{
118706f2543Smrg  ScreenPtr		pScreen = pColormap->pScreen;
119706f2543Smrg  winScreenPriv(pScreen);
120706f2543Smrg  ColormapPtr		oldpmap = pScreenPriv->pcmapInstalled;
121706f2543Smrg
122706f2543Smrg#if CYGDEBUG
123706f2543Smrg  winDebug ("winInstallColormap\n");
124706f2543Smrg#endif
125706f2543Smrg
126706f2543Smrg  /* Did the colormap actually change? */
127706f2543Smrg  if (pColormap != oldpmap)
128706f2543Smrg    {
129706f2543Smrg#if CYGDEBUG
130706f2543Smrg      winDebug ("winInstallColormap - Colormap has changed, attempt "
131706f2543Smrg	      "to install.\n");
132706f2543Smrg#endif
133706f2543Smrg
134706f2543Smrg      /* Was there a previous colormap? */
135706f2543Smrg      if (oldpmap != (ColormapPtr) None)
136706f2543Smrg	{
137706f2543Smrg	  /* There was a previous colormap; tell clients it is gone */
138706f2543Smrg	  WalkTree (pColormap->pScreen, TellLostMap, (char *)&oldpmap->mid);
139706f2543Smrg	}
140706f2543Smrg
141706f2543Smrg      /* Install new colormap */
142706f2543Smrg      pScreenPriv->pcmapInstalled = pColormap;
143706f2543Smrg      WalkTree (pColormap->pScreen, TellGainedMap, (char *)&pColormap->mid);
144706f2543Smrg
145706f2543Smrg      /* Call the engine specific colormap install procedure */
146706f2543Smrg      if (!((*pScreenPriv->pwinInstallColormap) (pColormap)))
147706f2543Smrg	{
148706f2543Smrg	  winErrorFVerb (2, "winInstallColormap - Screen specific colormap install "
149706f2543Smrg		  "procedure failed.  Continuing, but colors may be "
150706f2543Smrg		  "messed up from now on.\n");
151706f2543Smrg	}
152706f2543Smrg    }
153706f2543Smrg
154706f2543Smrg  /* Save a pointer to the newly installed colormap */
155706f2543Smrg  pScreenPriv->pcmapInstalled = pColormap;
156706f2543Smrg}
157706f2543Smrg
158706f2543Smrg
159706f2543Smrg/* See Porting Layer Definition - p. 30 */
160706f2543Smrgstatic void
161706f2543SmrgwinUninstallColormap (ColormapPtr pmap)
162706f2543Smrg{
163706f2543Smrg  winScreenPriv(pmap->pScreen);
164706f2543Smrg  ColormapPtr curpmap = pScreenPriv->pcmapInstalled;
165706f2543Smrg
166706f2543Smrg#if CYGDEBUG
167706f2543Smrg  winDebug ("winUninstallColormap\n");
168706f2543Smrg#endif
169706f2543Smrg
170706f2543Smrg  /* Is the colormap currently installed? */
171706f2543Smrg  if (pmap != curpmap)
172706f2543Smrg    {
173706f2543Smrg      /* Colormap not installed, nothing to do */
174706f2543Smrg      return;
175706f2543Smrg    }
176706f2543Smrg
177706f2543Smrg  /* Clear the installed colormap flag */
178706f2543Smrg  pScreenPriv->pcmapInstalled = NULL;
179706f2543Smrg
180706f2543Smrg  /*
181706f2543Smrg   * NOTE: The default colormap does not get "uninstalled" before
182706f2543Smrg   * it is destroyed.
183706f2543Smrg   */
184706f2543Smrg
185706f2543Smrg  /* Install the default cmap in place of the cmap to be uninstalled */
186706f2543Smrg  if (pmap->mid != pmap->pScreen->defColormap)
187706f2543Smrg    {
188706f2543Smrg      dixLookupResourceByType((pointer) &curpmap, pmap->pScreen->defColormap,
189706f2543Smrg				RT_COLORMAP, NullClient, DixUnknownAccess);
190706f2543Smrg      (*pmap->pScreen->InstallColormap) (curpmap);
191706f2543Smrg    }
192706f2543Smrg}
193706f2543Smrg
194706f2543Smrg
195706f2543Smrg/* See Porting Layer Definition - p. 30 */
196706f2543Smrgstatic void
197706f2543SmrgwinStoreColors (ColormapPtr pmap,
198706f2543Smrg		int ndef,
199706f2543Smrg		xColorItem *pdefs)
200706f2543Smrg{
201706f2543Smrg  ScreenPtr		pScreen = pmap->pScreen;
202706f2543Smrg  winScreenPriv(pScreen);
203706f2543Smrg  winCmapPriv(pmap);
204706f2543Smrg  int			i;
205706f2543Smrg  unsigned short	nRed, nGreen, nBlue;
206706f2543Smrg
207706f2543Smrg#if CYGDEBUG
208706f2543Smrg  if (ndef != 1)
209706f2543Smrg    winDebug ("winStoreColors - ndef: %d\n",
210706f2543Smrg	    ndef);
211706f2543Smrg#endif
212706f2543Smrg
213706f2543Smrg  /* Save the new colors in the colormap privates */
214706f2543Smrg  for (i = 0; i < ndef; ++i)
215706f2543Smrg    {
216706f2543Smrg      /* Adjust the colors from the X color spec to the Windows color spec */
217706f2543Smrg      nRed = pdefs[i].red >> 8;
218706f2543Smrg      nGreen = pdefs[i].green >> 8;
219706f2543Smrg      nBlue = pdefs[i].blue >> 8;
220706f2543Smrg
221706f2543Smrg      /* Copy the colors to a palette entry table */
222706f2543Smrg      pCmapPriv->peColors[pdefs[0].pixel + i].peRed = nRed;
223706f2543Smrg      pCmapPriv->peColors[pdefs[0].pixel + i].peGreen = nGreen;
224706f2543Smrg      pCmapPriv->peColors[pdefs[0].pixel + i].peBlue = nBlue;
225706f2543Smrg
226706f2543Smrg      /* Copy the colors to a RGBQUAD table */
227706f2543Smrg      pCmapPriv->rgbColors[pdefs[0].pixel + i].rgbRed = nRed;
228706f2543Smrg      pCmapPriv->rgbColors[pdefs[0].pixel + i].rgbGreen = nGreen;
229706f2543Smrg      pCmapPriv->rgbColors[pdefs[0].pixel + i].rgbBlue = nBlue;
230706f2543Smrg
231706f2543Smrg#if CYGDEBUG
232706f2543Smrg      winDebug ("winStoreColors - nRed %d nGreen %d nBlue %d\n",
233706f2543Smrg	      nRed, nGreen, nBlue);
234706f2543Smrg#endif
235706f2543Smrg    }
236706f2543Smrg
237706f2543Smrg  /* Call the engine specific store colors procedure */
238706f2543Smrg  if (!((pScreenPriv->pwinStoreColors) (pmap, ndef, pdefs)))
239706f2543Smrg    {
240706f2543Smrg      winErrorFVerb (2, "winStoreColors - Engine cpecific color storage procedure "
241706f2543Smrg	      "failed.  Continuing, but colors may be messed up from now "
242706f2543Smrg	      "on.\n");
243706f2543Smrg    }
244706f2543Smrg}
245706f2543Smrg
246706f2543Smrg
247706f2543Smrg/* See Porting Layer Definition - p. 30 */
248706f2543Smrgstatic void
249706f2543SmrgwinResolveColor (unsigned short *pred,
250706f2543Smrg		 unsigned short *pgreen,
251706f2543Smrg		 unsigned short *pblue,
252706f2543Smrg		 VisualPtr	pVisual)
253706f2543Smrg{
254706f2543Smrg#if CYGDEBUG
255706f2543Smrg  winDebug ("winResolveColor ()\n");
256706f2543Smrg#endif
257706f2543Smrg
258706f2543Smrg  miResolveColor (pred, pgreen, pblue, pVisual);
259706f2543Smrg}
260706f2543Smrg
261706f2543Smrg
262706f2543Smrg/* See Porting Layer Definition - p. 29 */
263706f2543Smrgstatic Bool
264706f2543SmrgwinCreateColormap (ColormapPtr pmap)
265706f2543Smrg{
266706f2543Smrg  winPrivCmapPtr	pCmapPriv = NULL;
267706f2543Smrg  ScreenPtr		pScreen = pmap->pScreen;
268706f2543Smrg  winScreenPriv(pScreen);
269706f2543Smrg
270706f2543Smrg#if CYGDEBUG
271706f2543Smrg  winDebug ("winCreateColormap\n");
272706f2543Smrg#endif
273706f2543Smrg
274706f2543Smrg  /* Allocate colormap privates */
275706f2543Smrg  if (!winAllocateCmapPrivates (pmap))
276706f2543Smrg    {
277706f2543Smrg      ErrorF ("winCreateColorma - Couldn't allocate cmap privates\n");
278706f2543Smrg      return FALSE;
279706f2543Smrg    }
280706f2543Smrg
281706f2543Smrg  /* Get a pointer to the newly allocated privates */
282706f2543Smrg  pCmapPriv = winGetCmapPriv (pmap);
283706f2543Smrg
284706f2543Smrg  /*
285706f2543Smrg   * FIXME: This is some evil hackery to help in handling some X clients
286706f2543Smrg   * that expect the top pixel to be white.  This "help" only lasts until
287706f2543Smrg   * some client overwrites the top colormap entry.
288706f2543Smrg   *
289706f2543Smrg   * We don't want to actually allocate the top entry, as that causes
290706f2543Smrg   * problems with X clients that need 7 planes (128 colors) in the default
291706f2543Smrg   * colormap, such as Magic 7.1.
292706f2543Smrg   */
293706f2543Smrg  pCmapPriv->rgbColors[WIN_NUM_PALETTE_ENTRIES - 1].rgbRed = 255;
294706f2543Smrg  pCmapPriv->rgbColors[WIN_NUM_PALETTE_ENTRIES - 1].rgbGreen = 255;
295706f2543Smrg  pCmapPriv->rgbColors[WIN_NUM_PALETTE_ENTRIES - 1].rgbBlue = 255;
296706f2543Smrg  pCmapPriv->peColors[WIN_NUM_PALETTE_ENTRIES - 1].peRed = 255;
297706f2543Smrg  pCmapPriv->peColors[WIN_NUM_PALETTE_ENTRIES - 1].peGreen = 255;
298706f2543Smrg  pCmapPriv->peColors[WIN_NUM_PALETTE_ENTRIES - 1].peBlue = 255;
299706f2543Smrg
300706f2543Smrg  /* Call the engine specific colormap initialization procedure */
301706f2543Smrg  if (!((*pScreenPriv->pwinCreateColormap) (pmap)))
302706f2543Smrg    {
303706f2543Smrg      ErrorF ("winCreateColormap - Engine specific colormap creation "
304706f2543Smrg	      "procedure failed.  Aborting.\n");
305706f2543Smrg      return FALSE;
306706f2543Smrg    }
307706f2543Smrg
308706f2543Smrg  return TRUE;
309706f2543Smrg}
310706f2543Smrg
311706f2543Smrg
312706f2543Smrg/* See Porting Layer Definition - p. 29, 30 */
313706f2543Smrgstatic void
314706f2543SmrgwinDestroyColormap (ColormapPtr pColormap)
315706f2543Smrg{
316706f2543Smrg  winScreenPriv(pColormap->pScreen);
317706f2543Smrg  winCmapPriv(pColormap);
318706f2543Smrg
319706f2543Smrg  /* Call the engine specific colormap destruction procedure */
320706f2543Smrg  if (!((*pScreenPriv->pwinDestroyColormap) (pColormap)))
321706f2543Smrg    {
322706f2543Smrg      winErrorFVerb (2, "winDestroyColormap - Engine specific colormap destruction "
323706f2543Smrg	      "procedure failed.  Continuing, but it is possible that memory "
324706f2543Smrg	      "was leaked, or that colors will be messed up from now on.\n");
325706f2543Smrg    }
326706f2543Smrg
327706f2543Smrg  /* Free the colormap privates */
328706f2543Smrg  free (pCmapPriv);
329706f2543Smrg  winSetCmapPriv (pColormap, NULL);
330706f2543Smrg
331706f2543Smrg#if CYGDEBUG
332706f2543Smrg  winDebug ("winDestroyColormap - Returning\n");
333706f2543Smrg#endif
334706f2543Smrg}
335706f2543Smrg
336706f2543Smrg
337706f2543Smrg/*
338706f2543Smrg * Internal function to load the palette used by the Shadow DIB
339706f2543Smrg */
340706f2543Smrg
341706f2543Smrgstatic Bool
342706f2543SmrgwinGetPaletteDIB (ScreenPtr pScreen, ColormapPtr pcmap)
343706f2543Smrg{
344706f2543Smrg  winScreenPriv(pScreen);
345706f2543Smrg  int			i;
346706f2543Smrg  Pixel			pixel; /* Pixel == CARD32 */
347706f2543Smrg  CARD16		nRed, nGreen, nBlue; /* CARD16 == unsigned short */
348706f2543Smrg  UINT			uiColorsRetrieved = 0;
349706f2543Smrg  RGBQUAD		rgbColors[WIN_NUM_PALETTE_ENTRIES];
350706f2543Smrg
351706f2543Smrg  /* Get the color table for the screen */
352706f2543Smrg  uiColorsRetrieved = GetDIBColorTable (pScreenPriv->hdcScreen,
353706f2543Smrg					0,
354706f2543Smrg					WIN_NUM_PALETTE_ENTRIES,
355706f2543Smrg					rgbColors);
356706f2543Smrg  if (uiColorsRetrieved == 0)
357706f2543Smrg    {
358706f2543Smrg      ErrorF ("winGetPaletteDIB - Could not retrieve screen color table\n");
359706f2543Smrg      return FALSE;
360706f2543Smrg    }
361706f2543Smrg
362706f2543Smrg#if CYGDEBUG
363706f2543Smrg  winDebug ("winGetPaletteDIB - Retrieved %d colors from DIB\n",
364706f2543Smrg	  uiColorsRetrieved);
365706f2543Smrg#endif
366706f2543Smrg
367706f2543Smrg  /* Set the DIB color table to the default screen palette */
368706f2543Smrg  if (SetDIBColorTable (pScreenPriv->hdcShadow,
369706f2543Smrg			0,
370706f2543Smrg			uiColorsRetrieved,
371706f2543Smrg			rgbColors) == 0)
372706f2543Smrg    {
373706f2543Smrg      ErrorF ("winGetPaletteDIB - SetDIBColorTable () failed\n");
374706f2543Smrg      return FALSE;
375706f2543Smrg    }
376706f2543Smrg
377706f2543Smrg  /* Alloc each color in the DIB color table */
378706f2543Smrg  for (i = 0; i < uiColorsRetrieved; ++i)
379706f2543Smrg    {
380706f2543Smrg      pixel = i;
381706f2543Smrg
382706f2543Smrg      /* Extract the color values for current palette entry */
383706f2543Smrg      nRed = rgbColors[i].rgbRed << 8;
384706f2543Smrg      nGreen = rgbColors[i].rgbGreen << 8;
385706f2543Smrg      nBlue = rgbColors[i].rgbBlue << 8;
386706f2543Smrg
387706f2543Smrg#if CYGDEBUG
388706f2543Smrg      winDebug ("winGetPaletteDIB - Allocating a color: %d; "
389706f2543Smrg	      "%d %d %d\n",
390706f2543Smrg	      pixel, nRed, nGreen, nBlue);
391706f2543Smrg#endif
392706f2543Smrg
393706f2543Smrg      /* Allocate a entry in the X colormap */
394706f2543Smrg      if (AllocColor (pcmap,
395706f2543Smrg		      &nRed,
396706f2543Smrg		      &nGreen,
397706f2543Smrg		      &nBlue,
398706f2543Smrg		      &pixel,
399706f2543Smrg		      0) != Success)
400706f2543Smrg	{
401706f2543Smrg	  ErrorF ("winGetPaletteDIB - AllocColor () failed, pixel %d\n",
402706f2543Smrg		  i);
403706f2543Smrg	  return FALSE;
404706f2543Smrg	}
405706f2543Smrg
406706f2543Smrg      if (i != pixel
407706f2543Smrg	  || nRed != rgbColors[i].rgbRed
408706f2543Smrg	  || nGreen != rgbColors[i].rgbGreen
409706f2543Smrg	  || nBlue != rgbColors[i].rgbBlue)
410706f2543Smrg	{
411706f2543Smrg	  winDebug ("winGetPaletteDIB - Got: %d; "
412706f2543Smrg		  "%d %d %d\n",
413706f2543Smrg		  (int) pixel, nRed, nGreen, nBlue);
414706f2543Smrg	}
415706f2543Smrg
416706f2543Smrg      /* FIXME: Not sure that this bit is needed at all */
417706f2543Smrg      pcmap->red[i].co.local.red = nRed;
418706f2543Smrg      pcmap->red[i].co.local.green = nGreen;
419706f2543Smrg      pcmap->red[i].co.local.blue = nBlue;
420706f2543Smrg    }
421706f2543Smrg
422706f2543Smrg  /* System is using a colormap */
423706f2543Smrg  /* Set the black and white pixel indices */
424706f2543Smrg  pScreen->whitePixel = uiColorsRetrieved - 1;
425706f2543Smrg  pScreen->blackPixel = 0;
426706f2543Smrg
427706f2543Smrg  return TRUE;
428706f2543Smrg}
429706f2543Smrg
430706f2543Smrg
431706f2543Smrg/*
432706f2543Smrg * Internal function to load the standard system palette being used by DD
433706f2543Smrg */
434706f2543Smrg
435706f2543Smrgstatic Bool
436706f2543SmrgwinGetPaletteDD (ScreenPtr pScreen, ColormapPtr pcmap)
437706f2543Smrg{
438706f2543Smrg  int			i;
439706f2543Smrg  Pixel			pixel; /* Pixel == CARD32 */
440706f2543Smrg  CARD16		nRed, nGreen, nBlue; /* CARD16 == unsigned short */
441706f2543Smrg  UINT			uiSystemPaletteEntries;
442706f2543Smrg  LPPALETTEENTRY	ppeColors = NULL;
443706f2543Smrg  HDC			hdc = NULL;
444706f2543Smrg
445706f2543Smrg  /* Get a DC to obtain the default palette */
446706f2543Smrg  hdc = GetDC (NULL);
447706f2543Smrg  if (hdc == NULL)
448706f2543Smrg    {
449706f2543Smrg      ErrorF ("winGetPaletteDD - Couldn't get a DC\n");
450706f2543Smrg      return FALSE;
451706f2543Smrg    }
452706f2543Smrg
453706f2543Smrg  /* Get the number of entries in the system palette */
454706f2543Smrg  uiSystemPaletteEntries = GetSystemPaletteEntries (hdc,
455706f2543Smrg						    0, 0, NULL);
456706f2543Smrg  if (uiSystemPaletteEntries == 0)
457706f2543Smrg    {
458706f2543Smrg      ErrorF ("winGetPaletteDD - Unable to determine number of "
459706f2543Smrg	      "system palette entries\n");
460706f2543Smrg      return FALSE;
461706f2543Smrg    }
462706f2543Smrg
463706f2543Smrg#if CYGDEBUG
464706f2543Smrg  winDebug ("winGetPaletteDD - uiSystemPaletteEntries %d\n",
465706f2543Smrg	  uiSystemPaletteEntries);
466706f2543Smrg#endif
467706f2543Smrg
468706f2543Smrg  /* Allocate palette entries structure */
469706f2543Smrg  ppeColors = malloc (uiSystemPaletteEntries * sizeof (PALETTEENTRY));
470706f2543Smrg  if (ppeColors == NULL)
471706f2543Smrg    {
472706f2543Smrg      ErrorF ("winGetPaletteDD - malloc () for colormap failed\n");
473706f2543Smrg      return FALSE;
474706f2543Smrg    }
475706f2543Smrg
476706f2543Smrg  /* Get system palette entries */
477706f2543Smrg  GetSystemPaletteEntries (hdc,
478706f2543Smrg			   0, uiSystemPaletteEntries, ppeColors);
479706f2543Smrg
480706f2543Smrg  /* Allocate an X colormap entry for every system palette entry */
481706f2543Smrg  for (i = 0; i < uiSystemPaletteEntries; ++i)
482706f2543Smrg    {
483706f2543Smrg      pixel = i;
484706f2543Smrg
485706f2543Smrg      /* Extract the color values for current palette entry */
486706f2543Smrg      nRed = ppeColors[i].peRed << 8;
487706f2543Smrg      nGreen = ppeColors[i].peGreen << 8;
488706f2543Smrg      nBlue = ppeColors[i].peBlue << 8;
489706f2543Smrg#if CYGDEBUG
490706f2543Smrg      winDebug ("winGetPaletteDD - Allocating a color: %d; "
491706f2543Smrg	      "%d %d %d\n",
492706f2543Smrg	      pixel, nRed, nGreen, nBlue);
493706f2543Smrg#endif
494706f2543Smrg      if (AllocColor (pcmap,
495706f2543Smrg		      &nRed,
496706f2543Smrg		      &nGreen,
497706f2543Smrg		      &nBlue,
498706f2543Smrg		      &pixel,
499706f2543Smrg		      0) != Success)
500706f2543Smrg	{
501706f2543Smrg	  ErrorF ("winGetPaletteDD - AllocColor () failed, pixel %d\n",
502706f2543Smrg		  i);
503706f2543Smrg	  free (ppeColors);
504706f2543Smrg	  ppeColors = NULL;
505706f2543Smrg	  return FALSE;
506706f2543Smrg	}
507706f2543Smrg
508706f2543Smrg      pcmap->red[i].co.local.red = nRed;
509706f2543Smrg      pcmap->red[i].co.local.green = nGreen;
510706f2543Smrg      pcmap->red[i].co.local.blue = nBlue;
511706f2543Smrg    }
512706f2543Smrg
513706f2543Smrg  /* System is using a colormap */
514706f2543Smrg  /* Set the black and white pixel indices */
515706f2543Smrg  pScreen->whitePixel = uiSystemPaletteEntries - 1;
516706f2543Smrg  pScreen->blackPixel = 0;
517706f2543Smrg
518706f2543Smrg  /* Free colormap */
519706f2543Smrg  free(ppeColors);
520706f2543Smrg  ppeColors = NULL;
521706f2543Smrg
522706f2543Smrg  /* Free the DC */
523706f2543Smrg  if (hdc != NULL)
524706f2543Smrg    {
525706f2543Smrg      ReleaseDC (NULL, hdc);
526706f2543Smrg      hdc = NULL;
527706f2543Smrg    }
528706f2543Smrg
529706f2543Smrg  return TRUE;
530706f2543Smrg}
531706f2543Smrg
532706f2543Smrg
533706f2543Smrg/*
534706f2543Smrg * Install the standard fb colormap, or the GDI colormap,
535706f2543Smrg * depending on the current screen depth.
536706f2543Smrg */
537706f2543Smrg
538706f2543SmrgBool
539706f2543SmrgwinCreateDefColormap (ScreenPtr pScreen)
540706f2543Smrg{
541706f2543Smrg  winScreenPriv(pScreen);
542706f2543Smrg  winScreenInfo		*pScreenInfo = pScreenPriv->pScreenInfo;
543706f2543Smrg  unsigned short	zero = 0, ones = 0xFFFF;
544706f2543Smrg  VisualPtr		pVisual = pScreenPriv->pRootVisual;
545706f2543Smrg  ColormapPtr		pcmap = NULL;
546706f2543Smrg  Pixel			wp, bp;
547706f2543Smrg
548706f2543Smrg#if CYGDEBUG
549706f2543Smrg  winDebug ("winCreateDefColormap\n");
550706f2543Smrg#endif
551706f2543Smrg
552706f2543Smrg  /* Use standard fb colormaps for non palettized color modes */
553706f2543Smrg  if (pScreenInfo->dwBPP > 8)
554706f2543Smrg    {
555706f2543Smrg      winDebug ("winCreateDefColormap - Deferring to " \
556706f2543Smrg	      "fbCreateDefColormap ()\n");
557706f2543Smrg      return fbCreateDefColormap (pScreen);
558706f2543Smrg    }
559706f2543Smrg
560706f2543Smrg  /*
561706f2543Smrg   *  AllocAll for non-Dynamic visual classes,
562706f2543Smrg   *  AllocNone for Dynamic visual classes.
563706f2543Smrg   */
564706f2543Smrg
565706f2543Smrg  /*
566706f2543Smrg   * Dynamic visual classes allow the colors of the color map
567706f2543Smrg   * to be changed by clients.
568706f2543Smrg   */
569706f2543Smrg
570706f2543Smrg#if CYGDEBUG
571706f2543Smrg  winDebug ("winCreateDefColormap - defColormap: %d\n",
572706f2543Smrg	  pScreen->defColormap);
573706f2543Smrg#endif
574706f2543Smrg
575706f2543Smrg  /* Allocate an X colormap, owned by client 0 */
576706f2543Smrg  if (CreateColormap (pScreen->defColormap,
577706f2543Smrg		      pScreen,
578706f2543Smrg		      pVisual,
579706f2543Smrg		      &pcmap,
580706f2543Smrg		      (pVisual->class & DynamicClass) ? AllocNone : AllocAll,
581706f2543Smrg		      0) != Success)
582706f2543Smrg    {
583706f2543Smrg      ErrorF ("winCreateDefColormap - CreateColormap failed\n");
584706f2543Smrg      return FALSE;
585706f2543Smrg    }
586706f2543Smrg  if (pcmap == NULL)
587706f2543Smrg    {
588706f2543Smrg      ErrorF ("winCreateDefColormap - Colormap could not be created\n");
589706f2543Smrg      return FALSE;
590706f2543Smrg    }
591706f2543Smrg
592706f2543Smrg#if CYGDEBUG
593706f2543Smrg  winDebug ("winCreateDefColormap - Created a colormap\n");
594706f2543Smrg#endif
595706f2543Smrg
596706f2543Smrg  /* Branch on the visual class */
597706f2543Smrg  if (!(pVisual->class & DynamicClass))
598706f2543Smrg    {
599706f2543Smrg      /* Branch on engine type */
600706f2543Smrg      if (pScreenInfo->dwEngine == WIN_SERVER_SHADOW_GDI)
601706f2543Smrg	{
602706f2543Smrg	  /* Load the colors being used by the Shadow DIB */
603706f2543Smrg	  if (!winGetPaletteDIB (pScreen, pcmap))
604706f2543Smrg	    {
605706f2543Smrg	      ErrorF ("winCreateDefColormap - Couldn't get DIB colors\n");
606706f2543Smrg	      return FALSE;
607706f2543Smrg	    }
608706f2543Smrg	}
609706f2543Smrg      else
610706f2543Smrg	{
611706f2543Smrg	  /* Load the colors from the default system palette */
612706f2543Smrg	  if (!winGetPaletteDD (pScreen, pcmap))
613706f2543Smrg	    {
614706f2543Smrg	      ErrorF ("winCreateDefColormap - Couldn't get colors "
615706f2543Smrg		      "for DD\n");
616706f2543Smrg	      return FALSE;
617706f2543Smrg	    }
618706f2543Smrg	}
619706f2543Smrg    }
620706f2543Smrg  else
621706f2543Smrg    {
622706f2543Smrg      wp = pScreen->whitePixel;
623706f2543Smrg      bp = pScreen->blackPixel;
624706f2543Smrg
625706f2543Smrg      /* Allocate a black and white pixel */
626706f2543Smrg      if ((AllocColor (pcmap, &ones, &ones, &ones, &wp, 0) !=
627706f2543Smrg	   Success)
628706f2543Smrg	  ||
629706f2543Smrg	  (AllocColor (pcmap, &zero, &zero, &zero, &bp, 0) !=
630706f2543Smrg	   Success))
631706f2543Smrg	{
632706f2543Smrg	  ErrorF ("winCreateDefColormap - Couldn't allocate bp or wp\n");
633706f2543Smrg	  return FALSE;
634706f2543Smrg	}
635706f2543Smrg
636706f2543Smrg      pScreen->whitePixel = wp;
637706f2543Smrg      pScreen->blackPixel = bp;
638706f2543Smrg
639706f2543Smrg#if 0
640706f2543Smrg      /* Have to reserve first 10 and last ten pixels in DirectDraw windowed */
641706f2543Smrg      if (pScreenInfo->dwEngine != WIN_SERVER_SHADOW_GDI)
642706f2543Smrg	{
643706f2543Smrg	  int		k;
644706f2543Smrg	  Pixel		p;
645706f2543Smrg
646706f2543Smrg	  for (k = 1; k < 10; ++k)
647706f2543Smrg	    {
648706f2543Smrg	      p = k;
649706f2543Smrg	      if (AllocColor (pcmap, &ones, &ones, &ones, &p, 0) != Success)
650706f2543Smrg		FatalError ("Foo!\n");
651706f2543Smrg	    }
652706f2543Smrg
653706f2543Smrg	  for (k = 245; k < 255; ++k)
654706f2543Smrg	    {
655706f2543Smrg	      p = k;
656706f2543Smrg	      if (AllocColor (pcmap, &zero, &zero, &zero, &p, 0) != Success)
657706f2543Smrg		FatalError ("Baz!\n");
658706f2543Smrg	    }
659706f2543Smrg	}
660706f2543Smrg#endif
661706f2543Smrg    }
662706f2543Smrg
663706f2543Smrg  /* Install the created colormap */
664706f2543Smrg  (*pScreen->InstallColormap)(pcmap);
665706f2543Smrg
666706f2543Smrg#if CYGDEBUG
667706f2543Smrg  winDebug ("winCreateDefColormap - Returning\n");
668706f2543Smrg#endif
669706f2543Smrg
670706f2543Smrg  return TRUE;
671706f2543Smrg}
672