summaryrefslogtreecommitdiff
path: root/hw/dmx/dmxcmap.c
blob: 450627b406bf1f54c5ae3b625712a5a0a2e7ba39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Authors:
 *   Kevin E. Martin <kem@redhat.com>
 *
 */

/** \file
 * Colormap support. */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "dmx.h"
#include "dmxlog.h"
#include "dmxsync.h"
#include "dmxcmap.h"
#include "dmxvisual.h"

#include "micmap.h"

static Bool
dmxAllocateColormapPrivates(ColormapPtr pColormap)
{
    dmxColormapPrivPtr pCmapPriv;

    pCmapPriv = (dmxColormapPrivPtr) malloc(sizeof(*pCmapPriv));
    if (!pCmapPriv)
        return FALSE;
    pCmapPriv->cmap = (Colormap) 0;

    DMX_SET_COLORMAP_PRIV(pColormap, pCmapPriv);

    return TRUE;
}

/** Create \a pColormap on the back-end server. */
Bool
dmxBECreateColormap(ColormapPtr pColormap)
{
    ScreenPtr pScreen = pColormap->pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
    VisualPtr pVisual = pColormap->pVisual;
    Visual *visual = dmxLookupVisual(pScreen, pVisual);

    if (visual) {
        pCmapPriv->cmap = XCreateColormap(dmxScreen->beDisplay,
                                          dmxScreen->scrnWin,
                                          visual,
                                          (pVisual->class & DynamicClass ?
                                           AllocAll : AllocNone));
        return pCmapPriv->cmap != 0;
    }
    else {
        dmxLog(dmxWarning, "dmxBECreateColormap: No visual found\n");
        return 0;
    }
}

/** Create colormap on back-end server associated with \a pColormap's
 *  screen. */
Bool
dmxCreateColormap(ColormapPtr pColormap)
{
    ScreenPtr pScreen = pColormap->pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool ret = TRUE;

    if (!dmxAllocateColormapPrivates(pColormap))
        return FALSE;

    if (dmxScreen->beDisplay) {
        if (!dmxBECreateColormap(pColormap))
            return FALSE;
    }

    DMX_UNWRAP(CreateColormap, dmxScreen, pScreen);
    if (pScreen->CreateColormap)
        ret = pScreen->CreateColormap(pColormap);
    DMX_WRAP(CreateColormap, dmxCreateColormap, dmxScreen, pScreen);

    return ret;
}

/** Destroy \a pColormap on the back-end server. */
Bool
dmxBEFreeColormap(ColormapPtr pColormap)
{
    ScreenPtr pScreen = pColormap->pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);

    if (pCmapPriv->cmap) {
        XFreeColormap(dmxScreen->beDisplay, pCmapPriv->cmap);
        pCmapPriv->cmap = (Colormap) 0;
        return TRUE;
    }

    return FALSE;
}

/** Destroy colormap on back-end server associated with \a pColormap's
 *  screen. */
void
dmxDestroyColormap(ColormapPtr pColormap)
{
    ScreenPtr pScreen = pColormap->pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);

    if (dmxScreen->beDisplay)
        dmxBEFreeColormap(pColormap);
    free(pCmapPriv);
    DMX_SET_COLORMAP_PRIV(pColormap, NULL);

    DMX_UNWRAP(DestroyColormap, dmxScreen, pScreen);
    if (pScreen->DestroyColormap)
        pScreen->DestroyColormap(pColormap);
    DMX_WRAP(DestroyColormap, dmxDestroyColormap, dmxScreen, pScreen);
}

/** Install colormap on back-end server associated with \a pColormap's
 *  screen. */
void
dmxInstallColormap(ColormapPtr pColormap)
{
    ScreenPtr pScreen = pColormap->pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);

    DMX_UNWRAP(InstallColormap, dmxScreen, pScreen);
    if (pScreen->InstallColormap)
        pScreen->InstallColormap(pColormap);
    DMX_WRAP(InstallColormap, dmxInstallColormap, dmxScreen, pScreen);

    if (dmxScreen->beDisplay) {
        XInstallColormap(dmxScreen->beDisplay, pCmapPriv->cmap);
        dmxSync(dmxScreen, FALSE);
    }
}

/** Store colors in \a pColormap on back-end server associated with \a
 *  pColormap's screen. */
void
dmxStoreColors(ColormapPtr pColormap, int ndef, xColorItem * pdef)
{
    ScreenPtr pScreen = pColormap->pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);

    if (dmxScreen->beDisplay && (pColormap->pVisual->class & DynamicClass)) {
        XColor *color = malloc(sizeof(*color) * ndef);
        int i;

        if (color) {
            for (i = 0; i < ndef; i++) {
                color[i].pixel = pdef[i].pixel;
                color[i].red = pdef[i].red;
                color[i].blue = pdef[i].blue;
                color[i].green = pdef[i].green;
                color[i].flags = pdef[i].flags;
                color[i].pad = pdef[i].pad;
            }
            XStoreColors(dmxScreen->beDisplay, pCmapPriv->cmap, color, ndef);
            free(color);
        }
        else {                  /* xalloc failed, so fallback */
            XColor c;

            for (i = 0; i < ndef; i++) {
                c.pixel = pdef[i].pixel;
                c.red = pdef[i].red;
                c.blue = pdef[i].blue;
                c.green = pdef[i].green;
                c.flags = pdef[i].flags;
                c.pad = pdef[i].pad;
                XStoreColor(dmxScreen->beDisplay, pCmapPriv->cmap, &c);
            }
        }
        dmxSync(dmxScreen, FALSE);
    }

    DMX_UNWRAP(StoreColors, dmxScreen, pScreen);
    if (pScreen->StoreColors)
        pScreen->StoreColors(pColormap, ndef, pdef);
    DMX_WRAP(StoreColors, dmxStoreColors, dmxScreen, pScreen);
}

/** Create the DMX server's default colormap. */
Bool
dmxCreateDefColormap(ScreenPtr pScreen)
{
    return miCreateDefColormap(pScreen);
}