First import
[xorg_rtime.git] / xorg-server-1.4 / hw / dmx / dmxcmap.c
blob949d7d689c43f0f7d30e7754fbac8bb2582c0360
1 /*
2 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
29 * Authors:
30 * Kevin E. Martin <kem@redhat.com>
34 /** \file
35 * Colormap support. */
37 #ifdef HAVE_DMX_CONFIG_H
38 #include <dmx-config.h>
39 #endif
41 #include "dmx.h"
42 #include "dmxlog.h"
43 #include "dmxsync.h"
44 #include "dmxcmap.h"
45 #include "dmxvisual.h"
47 #include "micmap.h"
49 static int dmxInitColormapPrivateFunc(ColormapPtr pColormap, int index)
51 return TRUE;
54 static Bool dmxAllocateColormapPrivates(ColormapPtr pColormap)
56 static unsigned long dmxColormapGeneration;
57 dmxColormapPrivPtr pCmapPriv;
59 if (dmxColormapGeneration != serverGeneration) {
60 if ((dmxColormapPrivateIndex
61 = AllocateColormapPrivateIndex(dmxInitColormapPrivateFunc)) < 0)
62 return FALSE;
64 dmxColormapGeneration = serverGeneration;
67 pCmapPriv = (dmxColormapPrivPtr)xalloc(sizeof(*pCmapPriv));
68 if (!pCmapPriv)
69 return FALSE;
70 pCmapPriv->cmap = (Colormap)0;
72 DMX_SET_COLORMAP_PRIV(pColormap, pCmapPriv);
74 return TRUE;
77 /** Create \a pColormap on the back-end server. */
78 Bool dmxBECreateColormap(ColormapPtr pColormap)
80 ScreenPtr pScreen = pColormap->pScreen;
81 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
82 dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
83 VisualPtr pVisual = pColormap->pVisual;
84 Visual *visual = dmxLookupVisual(pScreen, pVisual);
86 if (visual) {
87 pCmapPriv->cmap = XCreateColormap(dmxScreen->beDisplay,
88 dmxScreen->scrnWin,
89 visual,
90 (pVisual->class & DynamicClass ?
91 AllocAll : AllocNone));
92 return (pCmapPriv->cmap != 0);
94 else {
95 dmxLog(dmxWarning, "dmxBECreateColormap: No visual found\n");
96 return 0;
100 /** Create colormap on back-end server associated with \a pColormap's
101 * screen. */
102 Bool dmxCreateColormap(ColormapPtr pColormap)
104 ScreenPtr pScreen = pColormap->pScreen;
105 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
106 Bool ret = TRUE;
108 if (!dmxAllocateColormapPrivates(pColormap))
109 return FALSE;
111 if (dmxScreen->beDisplay) {
112 if (!dmxBECreateColormap(pColormap))
113 return FALSE;
116 DMX_UNWRAP(CreateColormap, dmxScreen, pScreen);
117 if (pScreen->CreateColormap)
118 ret = pScreen->CreateColormap(pColormap);
119 DMX_WRAP(CreateColormap, dmxCreateColormap, dmxScreen, pScreen);
121 return ret;
124 /** Destroy \a pColormap on the back-end server. */
125 Bool dmxBEFreeColormap(ColormapPtr pColormap)
127 ScreenPtr pScreen = pColormap->pScreen;
128 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
129 dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
131 if (pCmapPriv->cmap) {
132 XFreeColormap(dmxScreen->beDisplay, pCmapPriv->cmap);
133 pCmapPriv->cmap = (Colormap)0;
134 return TRUE;
137 return FALSE;
140 /** Destroy colormap on back-end server associated with \a pColormap's
141 * screen. */
142 void dmxDestroyColormap(ColormapPtr pColormap)
144 ScreenPtr pScreen = pColormap->pScreen;
145 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
146 dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
148 if (dmxScreen->beDisplay)
149 dmxBEFreeColormap(pColormap);
150 xfree(pCmapPriv);
151 DMX_SET_COLORMAP_PRIV(pColormap, NULL);
153 DMX_UNWRAP(DestroyColormap, dmxScreen, pScreen);
154 if (pScreen->DestroyColormap)
155 pScreen->DestroyColormap(pColormap);
156 DMX_WRAP(DestroyColormap, dmxDestroyColormap, dmxScreen, pScreen);
159 /** Install colormap on back-end server associated with \a pColormap's
160 * screen. */
161 void dmxInstallColormap(ColormapPtr pColormap)
163 ScreenPtr pScreen = pColormap->pScreen;
164 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
165 dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
167 DMX_UNWRAP(InstallColormap, dmxScreen, pScreen);
168 if (pScreen->InstallColormap)
169 pScreen->InstallColormap(pColormap);
170 DMX_WRAP(InstallColormap, dmxInstallColormap, dmxScreen, pScreen);
172 if (dmxScreen->beDisplay) {
173 XInstallColormap(dmxScreen->beDisplay, pCmapPriv->cmap);
174 dmxSync(dmxScreen, FALSE);
178 /** Store colors in \a pColormap on back-end server associated with \a
179 * pColormap's screen. */
180 void dmxStoreColors(ColormapPtr pColormap, int ndef, xColorItem *pdef)
182 ScreenPtr pScreen = pColormap->pScreen;
183 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
184 dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
186 if (dmxScreen->beDisplay && (pColormap->pVisual->class & DynamicClass)) {
187 XColor *color = xalloc(sizeof(*color) * ndef);
188 int i;
190 if (color) {
191 for (i = 0; i < ndef; i++) {
192 color[i].pixel = pdef[i].pixel;
193 color[i].red = pdef[i].red;
194 color[i].blue = pdef[i].blue;
195 color[i].green = pdef[i].green;
196 color[i].flags = pdef[i].flags;
197 color[i].pad = pdef[i].pad;
199 XStoreColors(dmxScreen->beDisplay, pCmapPriv->cmap, color, ndef);
200 xfree(color);
201 } else { /* xalloc failed, so fallback */
202 XColor c;
203 for (i = 0; i < ndef; i++) {
204 c.pixel = pdef[i].pixel;
205 c.red = pdef[i].red;
206 c.blue = pdef[i].blue;
207 c.green = pdef[i].green;
208 c.flags = pdef[i].flags;
209 c.pad = pdef[i].pad;
210 XStoreColor(dmxScreen->beDisplay, pCmapPriv->cmap, &c);
213 dmxSync(dmxScreen, FALSE);
216 DMX_UNWRAP(StoreColors, dmxScreen, pScreen);
217 if (pScreen->StoreColors)
218 pScreen->StoreColors(pColormap, ndef, pdef);
219 DMX_WRAP(StoreColors, dmxStoreColors, dmxScreen, pScreen);
222 /** Create the DMX server's default colormap. */
223 Bool dmxCreateDefColormap(ScreenPtr pScreen)
225 return miCreateDefColormap(pScreen);