Debian Codegraph @

src/Xcup.c

raw
1 /*
2
3 Copyright 1987, 1988, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #ifdef WIN32
31 #include <X11/Xwindows.h>
32 #endif
33
34 #include <X11/Xlibint.h>
35 #include <X11/extensions/Xcup.h>
36 #include <X11/extensions/cupproto.h>
37 #include <X11/extensions/Xext.h>
38 #include <X11/extensions/extutil.h>
39 #include <limits.h>
40
41 static XExtensionInfo _xcup_info_data;
42 static XExtensionInfo *xcup_info = &_xcup_info_data;
43 static const char *xcup_extension_name = XCUPNAME;
44
45 /*****************************************************************************
46 * *
47 * private utility routines *
48 * *
49 *****************************************************************************/
50
51 static int close_display(Display *dpy, XExtCodes *codes);
52 static /* const */ XExtensionHooks xcup_extension_hooks = {
53 NULL, /* create_gc */
54 NULL, /* copy_gc */
55 NULL, /* flush_gc */
56 NULL, /* free_gc */
57 NULL, /* create_font */
58 NULL, /* free_font */
59 close_display, /* close_display */
60 NULL, /* wire_to_event */
61 NULL, /* event_to_wire */
62 NULL, /* error */
63 NULL, /* error_string */
64 };
65
66 static XEXT_GENERATE_FIND_DISPLAY (find_display, xcup_info,
67 xcup_extension_name,
68 &xcup_extension_hooks,
69 0, NULL)
70
71 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xcup_info)
72
73
74 /*****************************************************************************
75 * *
76 * public Xcup Extension routines *
77 * *
78 *****************************************************************************/
79
80 Status
81 XcupQueryVersion(
82 Display* dpy,
83 int* major_version_return,
84 int* minor_version_return)
85 {
86 XExtDisplayInfo *info = find_display (dpy);
87 xXcupQueryVersionReply rep;
88 xXcupQueryVersionReq *req;
89
90 XextCheckExtension (dpy, info, xcup_extension_name, False);
91
92 LockDisplay(dpy);
93 GetReq(XcupQueryVersion, req);
94 req->reqType = info->codes->major_opcode;
95 req->xcupReqType = X_XcupQueryVersion;
96 req->client_major_version = XCUP_MAJOR_VERSION;
97 req->client_minor_version = XCUP_MINOR_VERSION;
98 if (!_XReply(dpy, (xReply *)&rep, 0, xTrue)) {
99 UnlockDisplay(dpy);
100 SyncHandle();
101 return False;
102 }
103 *major_version_return = rep.server_major_version;
104 *minor_version_return = rep.server_minor_version;
105 UnlockDisplay(dpy);
106 SyncHandle();
107 return True;
108 }
109
110 /* Win32 reserves 20 colormap entries for its desktop */
111 #ifndef TYP_RESERVED_ENTRIES
112 #define TYP_RESERVED_ENTRIES 20
113 #endif
114
115 Status
116 XcupGetReservedColormapEntries(
117 Display* dpy,
118 int screen,
119 XColor** colors_out,
120 int* ncolors)
121 {
122 XExtDisplayInfo *info = find_display (dpy);
123 xXcupGetReservedColormapEntriesReply rep;
124 xXcupGetReservedColormapEntriesReq *req;
125 xColorItem rbuf[TYP_RESERVED_ENTRIES];
126
127 *ncolors = 0;
128
129 XextCheckExtension (dpy, info, xcup_extension_name, False);
130
131 LockDisplay(dpy);
132 GetReq(XcupGetReservedColormapEntries, req);
133 req->reqType = info->codes->major_opcode;
134 req->xcupReqType = X_XcupGetReservedColormapEntries;
135 req->screen = screen;
136 if (_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
137 unsigned long nbytes;
138 xColorItem* rbufp;
139 unsigned int nentries = rep.length / 3;
140
141 if (nentries < (INT_MAX / SIZEOF (xColorItem))) {
142 nbytes = nentries * SIZEOF (xColorItem);
143
144 if (nentries > TYP_RESERVED_ENTRIES)
145 rbufp = Xmalloc (nbytes);
146 else
147 rbufp = rbuf;
148 } else
149 rbufp = NULL;
150
151 if (rbufp == NULL) {
152 _XEatDataWords(dpy, rep.length);
153 UnlockDisplay (dpy);
154 SyncHandle ();
155 return False;
156 }
157 _XRead (dpy, (char*) rbufp, nbytes);
158
159 *colors_out = (XColor*) Xmalloc (nentries * sizeof (XColor));
160 if (*colors_out) {
161 xColorItem* cs = (xColorItem *) rbufp;
162 XColor* cd = *colors_out;
163 int i;
164
165 *ncolors = nentries;
166 for (i = 0; i < *ncolors; i++, cd++) {
167 cd->pixel = cs->pixel;
168 cd->red = cs->red;
169 cd->green = cs->green;
170 cd->blue = cs->blue;
171 cs = (xColorItem*) (((char*) cs) + SIZEOF(xColorItem));
172 }
173 if (rbufp != rbuf) XFree ((char*) rbufp);
174 UnlockDisplay(dpy);
175 SyncHandle();
176 return True;
177 }
178 if (rbufp != rbuf) XFree ((char*) rbufp);
179 }
180 UnlockDisplay(dpy);
181 SyncHandle();
182 return False;
183 }
184
185 Status
186 XcupStoreColors(
187 Display* dpy,
188 Colormap colormap,
189 XColor* colors_in_out,
190 int ncolors)
191 {
192 XExtDisplayInfo *info = find_display (dpy);
193 xXcupStoreColorsReply rep;
194 xXcupStoreColorsReq *req;
195 xColorItem rbuf[256];
196 xColorItem citem;
197 int i;
198 XColor* xcp;
199
200 XextCheckExtension (dpy, info, xcup_extension_name, False);
201
202 LockDisplay(dpy);
203 GetReq(XcupStoreColors, req);
204 req->reqType = info->codes->major_opcode;
205 req->xcupReqType = X_XcupStoreColors;
206 req->cmap = colormap;
207 req->length += (ncolors * SIZEOF(xColorItem)) >> 2;
208
209 for (i = 0, xcp = colors_in_out; i < ncolors; i++, xcp++) {
210 citem.pixel = xcp->pixel;
211 citem.red = xcp->red;
212 citem.green = xcp->green;
213 citem.blue = xcp->blue;
214
215 /* note that xColorItem doesn't contain all 16-bit quantities, so
216 we can't use Data16 */
217 Data(dpy, (char *)&citem, (long) SIZEOF(xColorItem));
218 }
219
220 if (_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
221 unsigned long nbytes;
222 xColorItem* rbufp;
223 xColorItem* cs;
224 unsigned int nentries = rep.length / 3;
225
226 if ((nentries == ncolors) &&
227 (nentries < (INT_MAX / SIZEOF (xColorItem)))) {
228 nbytes = nentries * SIZEOF (xColorItem);
229
230 if (ncolors > 256)
231 rbufp = Xmalloc (nbytes);
232 else
233 rbufp = rbuf;
234 } else
235 rbufp = NULL;
236
237 if (rbufp == NULL) {
238 _XEatDataWords(dpy, rep.length);
239 UnlockDisplay (dpy);
240 SyncHandle ();
241 return False;
242
243 }
244
245 _XRead (dpy, (char*) rbufp, nbytes);
246
247 for (i = 0, xcp = colors_in_out, cs = rbufp; i < ncolors; i++, xcp++, cs++) {
248 xcp->pixel = cs->pixel;
249 xcp->red = cs->red;
250 xcp->green = cs->green;
251 xcp->blue = cs->blue;
252 xcp->flags = cs->flags;
253 }
254 if (rbufp != rbuf) XFree ((char*)rbufp);
255
256 UnlockDisplay(dpy);
257 SyncHandle();
258 return True;
259 }
260 UnlockDisplay(dpy);
261 SyncHandle();
262 return False;
263 }
264