Deprecate XCBSync, move to XCBAuxSync.
[free-sw/xcb/libxcb] / src / xcb_util.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * 
20  * Except as contained in this notice, the names of the authors or their
21  * institutions shall not be used in advertising or otherwise to promote the
22  * sale, use or other dealings in this Software without prior written
23  * authorization from the authors.
24  */
25
26 /* Utility functions implementable using only public APIs. */
27
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #ifdef DNETCONN
33 #include <netdnet/dnetdb.h>
34 #include <netdnet/dn.h>
35 #endif
36 #include <netdb.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42
43 #include "xcb.h"
44 #include "xcbext.h"
45 #include "xcbint.h"
46
47 int XCBPopcount(CARD32 mask)
48 {
49     unsigned long y;
50     y = (mask >> 1) & 033333333333;
51     y = mask - y - ((y >> 1) & 033333333333);
52     return ((y + (y >> 3)) & 030707070707) % 077;
53 }
54
55 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
56 {
57     int len, display, screen;
58     char *colon, *dot, *end;
59     if(!name || !*name)
60         name = getenv("DISPLAY");
61     if(!name)
62         return 0;
63     colon = strrchr(name, ':');
64     if(!colon)
65         return 0;
66     len = colon - name;
67     ++colon;
68     display = strtoul(colon, &dot, 10);
69     if(dot == colon)
70         return 0;
71     if(*dot == '\0')
72         screen = 0;
73     else
74     {
75         if(*dot != '.')
76             return 0;
77         ++dot;
78         screen = strtoul(dot, &end, 10);
79         if(end == dot || *end != '\0')
80             return 0;
81     }
82     /* At this point, the display string is fully parsed and valid, but
83      * the caller's memory is untouched. */
84
85     *host = malloc(len + 1);
86     if(!*host)
87         return 0;
88     memcpy(*host, name, len);
89     (*host)[len] = '\0';
90     *displayp = display;
91     if(screenp)
92         *screenp = screen;
93     return 1;
94 }
95
96 static int _xcb_open_tcp(const char *host, const unsigned short port);
97 static int _xcb_open_unix(const char *file);
98 #ifdef DNETCONN
99 static int _xcb_open_decnet(const char *host, const unsigned short port);
100 #endif
101
102 static int _xcb_open(const char *host, const int display)
103 {
104     int fd;
105
106     if(*host)
107     {
108 #ifdef DNETCONN
109         if (strchr(host,  ':'))
110         {
111             /* DECnet displays have two colons, so the parser will have left
112                one at the end */
113             char *dnethost = strdup(host);
114
115             dnethost[strlen(dnethost)-1] = '\0';
116             fd = _xcb_open_decnet(dnethost, display);
117             free(dnethost);
118         }
119         else
120 #endif
121         {
122             /* display specifies TCP */
123             unsigned short port = X_TCP_PORT + display;
124             fd = _xcb_open_tcp(host, port);
125         }
126     }
127     else
128     {
129         /* display specifies Unix socket */
130         static const char base[] = "/tmp/.X11-unix/X";
131         char file[sizeof(base) + 20];
132         snprintf(file, sizeof(file), "%s%d", base, display);
133         fd = _xcb_open_unix(file);
134     }
135
136     return fd;
137 }
138
139 #ifdef DNETCONN
140 static int _xcb_open_decnet(const char *host, const unsigned short port)
141 {
142     int fd;
143     struct sockaddr_dn addr;
144     struct accessdata_dn accessdata;
145     struct nodeent *nodeaddr = getnodebyname(host);
146
147     if(!nodeaddr)
148         return -1;
149     addr.sdn_family = AF_DECnet;
150
151     addr.sdn_add.a_len = nodeaddr->n_length;
152     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
153
154     sprintf((char *)addr.sdn_objname, "X$X%d", port);
155     addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
156     addr.sdn_objnum = 0;
157
158     fd = socket(PF_DECnet, SOCK_STREAM, 0);
159     if(fd == -1)
160         return -1;
161
162     memset(&accessdata, 0, sizeof(accessdata));
163     sprintf((char*)accessdata.acc_acc, "%d", getuid());
164     accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
165     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
166
167     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
168         return -1;
169     return fd;
170 }
171 #endif
172
173 static int _xcb_open_tcp(const char *host, const unsigned short port)
174 {
175     int fd;
176     struct sockaddr_in addr;
177     struct hostent *hostaddr = gethostbyname(host);
178     if(!hostaddr)
179         return -1;
180     addr.sin_family = AF_INET;
181     addr.sin_port = htons(port);
182     memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
183
184     fd = socket(PF_INET, SOCK_STREAM, 0);
185     if(fd == -1)
186         return -1;
187     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
188         return -1;
189     return fd;
190 }
191
192 static int _xcb_open_unix(const char *file)
193 {
194     int fd;
195     struct sockaddr_un addr = { AF_UNIX };
196     strcpy(addr.sun_path, file);
197
198     fd = socket(AF_UNIX, SOCK_STREAM, 0);
199     if(fd == -1)
200         return -1;
201     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
202         return -1;
203     return fd;
204 }
205
206 XCBConnection *XCBConnect(const char *displayname, int *screenp)
207 {
208     int fd, display = 0;
209     char *host;
210     XCBConnection *c;
211     XCBAuthInfo auth;
212
213     if(!XCBParseDisplay(displayname, &host, &display, screenp))
214         return 0;
215     fd = _xcb_open(host, display);
216     free(host);
217     if(fd == -1)
218         return 0;
219
220     _xcb_get_auth_info(fd, &auth);
221     c = XCBConnectToFD(fd, &auth);
222     free(auth.name);
223     free(auth.data);
224     return c;
225 }
226
227 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
228 {
229     int fd, display = 0;
230     char *host;
231
232     if(!XCBParseDisplay(displayname, &host, &display, screenp))
233         return 0;
234     fd = _xcb_open(host, display);
235     free(host);
236     if(fd == -1)
237         return 0;
238
239     return XCBConnectToFD(fd, auth);
240 }
241
242 /* backwards compatible interfaces: remove before 1.0 release */
243 int XCBSync(XCBConnection *c, XCBGenericError **e)
244 {
245     XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
246     free(reply);
247     return reply != 0;
248 }
249
250 XCBConnection *XCBConnectBasic()
251 {
252     XCBConnection *c = XCBConnect(0, 0);
253     if(c)
254         return c;
255     perror("XCBConnect");
256     abort();
257 }
258
259 int XCBOpen(const char *host, const int display)
260 {
261         return _xcb_open(host, display);
262 }
263
264 int XCBOpenTCP(const char *host, const unsigned short port)
265 {
266         return _xcb_open_tcp(host, port);
267 }
268
269 int XCBOpenUnix(const char *file)
270 {
271         return _xcb_open_unix(file);
272 }
273
274 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
275 {
276         return _xcb_get_auth_info(fd, info);
277 }
278
279 char *XCBConnSetupReqAuthorizationProtocolName(XCBSetupReq *R)
280 {
281         return XCBSetupReqAuthorizationProtocolName(R);
282 }
283
284 int XCBConnSetupReqAuthorizationProtocolNameLength(XCBSetupReq *R)
285 {
286         return XCBSetupReqAuthorizationProtocolNameLength(R);
287 }
288
289 XCBGenericIter XCBConnSetupReqAuthorizationProtocolNameEnd(XCBSetupReq *R)
290 {
291         return XCBSetupReqAuthorizationProtocolNameEnd(R);
292 }
293
294 char *XCBConnSetupReqAuthorizationProtocolData(XCBSetupReq *R)
295 {
296         return XCBSetupReqAuthorizationProtocolData(R);
297 }
298
299 int XCBConnSetupReqAuthorizationProtocolDataLength(XCBSetupReq *R)
300 {
301         return XCBSetupReqAuthorizationProtocolDataLength(R);
302 }
303
304 XCBGenericIter XCBConnSetupReqAuthorizationProtocolDataEnd(XCBSetupReq *R)
305 {
306         return XCBSetupReqAuthorizationProtocolDataEnd(R);
307 }
308
309 void XCBConnSetupReqNext(XCBSetupReqIter *i)
310 {
311         XCBSetupReqNext(i);
312 }
313
314 XCBGenericIter XCBConnSetupReqEnd(XCBSetupReqIter i)
315 {
316         return XCBSetupReqEnd(i);
317 }
318
319 char *XCBConnSetupFailedRepReason(XCBSetupFailed *R)
320 {
321         return XCBSetupFailedReason(R);
322 }
323
324 int XCBConnSetupFailedRepReasonLength(XCBSetupFailed *R)
325 {
326         return XCBSetupFailedReasonLength(R);
327 }
328
329 XCBGenericIter XCBConnSetupFailedRepReasonEnd(XCBSetupFailed *R)
330 {
331         return XCBSetupFailedReasonEnd(R);
332 }
333
334 void XCBConnSetupFailedRepNext(XCBSetupFailedIter *i)
335 {
336         XCBSetupFailedNext(i);
337 }
338
339 XCBGenericIter XCBConnSetupFailedRepEnd(XCBSetupFailedIter i)
340 {
341         return XCBSetupFailedEnd(i);
342 }
343
344 char *XCBConnSetupAuthenticateRepReason(XCBSetupAuthenticate *R)
345 {
346         return XCBSetupAuthenticateReason(R);
347 }
348
349 int XCBConnSetupAuthenticateRepReasonLength(XCBSetupAuthenticate *R)
350 {
351         return XCBSetupAuthenticateReasonLength(R);
352 }
353
354 XCBGenericIter XCBConnSetupAuthenticateRepReasonEnd(XCBSetupAuthenticate *R)
355 {
356         return XCBSetupAuthenticateReasonEnd(R);
357 }
358
359 void XCBConnSetupAuthenticateRepNext(XCBSetupAuthenticateIter *i)
360 {
361         XCBSetupAuthenticateNext(i);
362 }
363
364 XCBGenericIter XCBConnSetupAuthenticateRepEnd(XCBSetupAuthenticateIter i)
365 {
366         return XCBSetupAuthenticateEnd(i);
367 }
368
369 char *XCBConnSetupSuccessRepVendor(XCBSetup *R)
370 {
371         return XCBSetupVendor(R);
372 }
373
374 int XCBConnSetupSuccessRepVendorLength(XCBSetup *R)
375 {
376         return XCBSetupVendorLength(R);
377 }
378
379 XCBGenericIter XCBConnSetupSuccessRepVendorEnd(XCBSetup *R)
380 {
381         return XCBSetupVendorEnd(R);
382 }
383
384 XCBFORMAT *XCBConnSetupSuccessRepPixmapFormats(XCBSetup *R)
385 {
386         return XCBSetupPixmapFormats(R);
387 }
388
389 int XCBConnSetupSuccessRepPixmapFormatsLength(XCBSetup *R)
390 {
391         return XCBSetupPixmapFormatsLength(R);
392 }
393
394 XCBFORMATIter XCBConnSetupSuccessRepPixmapFormatsIter(XCBSetup *R)
395 {
396         return XCBSetupPixmapFormatsIter(R);
397 }
398
399 int XCBConnSetupSuccessRepRootsLength(XCBSetup *R)
400 {
401         return XCBSetupRootsLength(R);
402 }
403
404 XCBSCREENIter XCBConnSetupSuccessRepRootsIter(XCBSetup *R)
405 {
406         return XCBSetupRootsIter(R);
407 }
408
409 void XCBConnSetupSuccessRepNext(XCBSetupIter *i)
410 {
411         XCBSetupNext(i);
412 }
413
414 XCBGenericIter XCBConnSetupSuccessRepEnd(XCBSetupIter i)
415 {
416         return XCBSetupEnd(i);
417 }