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