1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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.
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.
26 /* Utility functions implementable using only public APIs. */
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <netdnet/dnetdb.h>
35 #include <netdnet/dn.h>
48 int XCBPopcount(CARD32 mask)
51 y = (mask >> 1) & 033333333333;
52 y = mask - y - ((y >> 1) & 033333333333);
53 return ((y + (y >> 3)) & 030707070707) % 077;
56 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
58 int len, display, screen;
59 char *colon, *dot, *end;
61 name = getenv("DISPLAY");
64 colon = strrchr(name, ':');
69 display = strtoul(colon, &dot, 10);
79 screen = strtoul(dot, &end, 10);
80 if(end == dot || *end != '\0')
83 /* At this point, the display string is fully parsed and valid, but
84 * the caller's memory is untouched. */
86 *host = malloc(len + 1);
89 memcpy(*host, name, len);
97 static int _xcb_open_tcp(const char *host, const unsigned short port);
98 static int _xcb_open_unix(const char *file);
100 static int _xcb_open_decnet(const char *host, const unsigned short port);
103 static int _xcb_open(const char *host, const int display)
110 if (strchr(host, ':'))
112 /* DECnet displays have two colons, so the parser will have left
114 char *dnethost = strdup(host);
116 dnethost[strlen(dnethost)-1] = '\0';
117 fd = _xcb_open_decnet(dnethost, display);
123 /* display specifies TCP */
124 unsigned short port = X_TCP_PORT + display;
125 fd = _xcb_open_tcp(host, port);
130 /* display specifies Unix socket */
131 static const char base[] = "/tmp/.X11-unix/X";
132 char file[sizeof(base) + 20];
133 snprintf(file, sizeof(file), "%s%d", base, display);
134 fd = _xcb_open_unix(file);
141 static int _xcb_open_decnet(const char *host, const unsigned short port)
144 struct sockaddr_dn addr;
145 struct accessdata_dn accessdata;
146 struct nodeent *nodeaddr = getnodebyname(host);
150 addr.sdn_family = AF_DECnet;
152 addr.sdn_add.a_len = nodeaddr->n_length;
153 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
155 sprintf((char *)addr.sdn_objname, "X$X%d", port);
156 addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
159 fd = socket(PF_DECnet, SOCK_STREAM, 0);
163 memset(&accessdata, 0, sizeof(accessdata));
164 sprintf((char*)accessdata.acc_acc, "%d", getuid());
165 accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
166 setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
168 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
174 static int _xcb_open_tcp(const char *host, const unsigned short port)
177 struct sockaddr_in addr;
178 struct hostent *hostaddr = gethostbyname(host);
181 addr.sin_family = AF_INET;
182 addr.sin_port = htons(port);
183 memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
185 fd = socket(PF_INET, SOCK_STREAM, 0);
188 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
193 static int _xcb_open_unix(const char *file)
196 struct sockaddr_un addr = { AF_UNIX };
197 strcpy(addr.sun_path, file);
199 fd = socket(AF_UNIX, SOCK_STREAM, 0);
202 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
207 XCBConnection *XCBConnect(const char *displayname, int *screenp)
214 if(!XCBParseDisplay(displayname, &host, &display, screenp))
216 fd = _xcb_open(host, display);
221 _xcb_get_auth_info(fd, &auth);
222 c = XCBConnectToFD(fd, &auth);
228 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
233 if(!XCBParseDisplay(displayname, &host, &display, screenp))
235 fd = _xcb_open(host, display);
240 return XCBConnectToFD(fd, auth);
243 /* backwards compatible interfaces: remove before 1.0 release */
244 int XCBSync(XCBConnection *c, XCBGenericError **e)
246 XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
251 XCBConnection *XCBConnectBasic()
253 XCBConnection *c = XCBConnect(0, 0);
256 perror("XCBConnect");
260 int XCBOpen(const char *host, const int display)
262 return _xcb_open(host, display);
265 int XCBOpenTCP(const char *host, const unsigned short port)
267 return _xcb_open_tcp(host, port);
270 int XCBOpenUnix(const char *file)
272 return _xcb_open_unix(file);
275 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
277 return _xcb_get_auth_info(fd, info);
280 char *XCBConnSetupReqAuthorizationProtocolName(XCBSetupReq *R)
282 return XCBSetupReqAuthorizationProtocolName(R);
285 int XCBConnSetupReqAuthorizationProtocolNameLength(XCBSetupReq *R)
287 return XCBSetupReqAuthorizationProtocolNameLength(R);
290 XCBGenericIter XCBConnSetupReqAuthorizationProtocolNameEnd(XCBSetupReq *R)
292 return XCBSetupReqAuthorizationProtocolNameEnd(R);
295 char *XCBConnSetupReqAuthorizationProtocolData(XCBSetupReq *R)
297 return XCBSetupReqAuthorizationProtocolData(R);
300 int XCBConnSetupReqAuthorizationProtocolDataLength(XCBSetupReq *R)
302 return XCBSetupReqAuthorizationProtocolDataLength(R);
305 XCBGenericIter XCBConnSetupReqAuthorizationProtocolDataEnd(XCBSetupReq *R)
307 return XCBSetupReqAuthorizationProtocolDataEnd(R);
310 void XCBConnSetupReqNext(XCBSetupReqIter *i)
315 XCBGenericIter XCBConnSetupReqEnd(XCBSetupReqIter i)
317 return XCBSetupReqEnd(i);
320 char *XCBConnSetupFailedRepReason(XCBSetupFailed *R)
322 return XCBSetupFailedReason(R);
325 int XCBConnSetupFailedRepReasonLength(XCBSetupFailed *R)
327 return XCBSetupFailedReasonLength(R);
330 XCBGenericIter XCBConnSetupFailedRepReasonEnd(XCBSetupFailed *R)
332 return XCBSetupFailedReasonEnd(R);
335 void XCBConnSetupFailedRepNext(XCBSetupFailedIter *i)
337 XCBSetupFailedNext(i);
340 XCBGenericIter XCBConnSetupFailedRepEnd(XCBSetupFailedIter i)
342 return XCBSetupFailedEnd(i);
345 char *XCBConnSetupAuthenticateRepReason(XCBSetupAuthenticate *R)
347 return XCBSetupAuthenticateReason(R);
350 int XCBConnSetupAuthenticateRepReasonLength(XCBSetupAuthenticate *R)
352 return XCBSetupAuthenticateReasonLength(R);
355 XCBGenericIter XCBConnSetupAuthenticateRepReasonEnd(XCBSetupAuthenticate *R)
357 return XCBSetupAuthenticateReasonEnd(R);
360 void XCBConnSetupAuthenticateRepNext(XCBSetupAuthenticateIter *i)
362 XCBSetupAuthenticateNext(i);
365 XCBGenericIter XCBConnSetupAuthenticateRepEnd(XCBSetupAuthenticateIter i)
367 return XCBSetupAuthenticateEnd(i);
370 char *XCBConnSetupSuccessRepVendor(XCBSetup *R)
372 return XCBSetupVendor(R);
375 int XCBConnSetupSuccessRepVendorLength(XCBSetup *R)
377 return XCBSetupVendorLength(R);
380 XCBGenericIter XCBConnSetupSuccessRepVendorEnd(XCBSetup *R)
382 return XCBSetupVendorEnd(R);
385 XCBFORMAT *XCBConnSetupSuccessRepPixmapFormats(XCBSetup *R)
387 return XCBSetupPixmapFormats(R);
390 int XCBConnSetupSuccessRepPixmapFormatsLength(XCBSetup *R)
392 return XCBSetupPixmapFormatsLength(R);
395 XCBFORMATIter XCBConnSetupSuccessRepPixmapFormatsIter(XCBSetup *R)
397 return XCBSetupPixmapFormatsIter(R);
400 int XCBConnSetupSuccessRepRootsLength(XCBSetup *R)
402 return XCBSetupRootsLength(R);
405 XCBSCREENIter XCBConnSetupSuccessRepRootsIter(XCBSetup *R)
407 return XCBSetupRootsIter(R);
410 void XCBConnSetupSuccessRepNext(XCBSetupIter *i)
415 XCBGenericIter XCBConnSetupSuccessRepEnd(XCBSetupIter i)
417 return XCBSetupEnd(i);