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>
33 #include <netdnet/dnetdb.h>
34 #include <netdnet/dn.h>
47 int XCBPopcount(CARD32 mask)
50 y = (mask >> 1) & 033333333333;
51 y = mask - y - ((y >> 1) & 033333333333);
52 return ((y + (y >> 3)) & 030707070707) % 077;
55 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
57 int len, display, screen;
58 char *colon, *dot, *end;
60 name = getenv("DISPLAY");
63 colon = strrchr(name, ':');
68 display = strtoul(colon, &dot, 10);
78 screen = strtoul(dot, &end, 10);
79 if(end == dot || *end != '\0')
82 /* At this point, the display string is fully parsed and valid, but
83 * the caller's memory is untouched. */
85 *host = malloc(len + 1);
88 memcpy(*host, name, len);
96 static int _xcb_open_tcp(const char *host, const unsigned short port);
97 static int _xcb_open_unix(const char *file);
99 static int _xcb_open_decnet(const char *host, const unsigned short port);
102 static int _xcb_open(const char *host, const int display)
109 if (strchr(host, ':'))
111 /* DECnet displays have two colons, so the parser will have left
113 char *dnethost = strdup(host);
115 dnethost[strlen(dnethost)-1] = '\0';
116 fd = _xcb_open_decnet(dnethost, display);
122 /* display specifies TCP */
123 unsigned short port = X_TCP_PORT + display;
124 fd = _xcb_open_tcp(host, port);
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);
140 static int _xcb_open_decnet(const char *host, const unsigned short port)
143 struct sockaddr_dn addr;
144 struct accessdata_dn accessdata;
145 struct nodeent *nodeaddr = getnodebyname(host);
149 addr.sdn_family = AF_DECnet;
151 addr.sdn_add.a_len = nodeaddr->n_length;
152 memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
154 sprintf((char *)addr.sdn_objname, "X$X%d", port);
155 addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
158 fd = socket(PF_DECnet, SOCK_STREAM, 0);
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));
167 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
173 static int _xcb_open_tcp(const char *host, const unsigned short port)
176 struct sockaddr_in addr;
177 struct hostent *hostaddr = gethostbyname(host);
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));
184 fd = socket(PF_INET, SOCK_STREAM, 0);
187 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
192 static int _xcb_open_unix(const char *file)
195 struct sockaddr_un addr = { AF_UNIX };
196 strcpy(addr.sun_path, file);
198 fd = socket(AF_UNIX, SOCK_STREAM, 0);
201 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
206 XCBConnection *XCBConnect(const char *displayname, int *screenp)
213 if(!XCBParseDisplay(displayname, &host, &display, screenp))
215 fd = _xcb_open(host, display);
220 _xcb_get_auth_info(fd, &auth);
221 c = XCBConnectToFD(fd, &auth);
227 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
232 if(!XCBParseDisplay(displayname, &host, &display, screenp))
234 fd = _xcb_open(host, display);
239 return XCBConnectToFD(fd, auth);
242 int XCBSync(XCBConnection *c, XCBGenericError **e)
244 XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
252 /* backwards compatible interfaces: remove before 1.0 release */
253 XCBConnection *XCBConnectBasic()
255 XCBConnection *c = XCBConnect(0, 0);
258 perror("XCBConnect");
262 int XCBOpen(const char *host, const int display)
264 return _xcb_open(host, display);
267 int XCBOpenTCP(const char *host, const unsigned short port)
269 return _xcb_open_tcp(host, port);
272 int XCBOpenUnix(const char *file)
274 return _xcb_open_unix(file);
277 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
279 return _xcb_get_auth_info(fd, info);
282 char *XCBConnSetupReqAuthorizationProtocolName(XCBSetupReq *R)
284 return XCBSetupReqAuthorizationProtocolName(R);
287 int XCBConnSetupReqAuthorizationProtocolNameLength(XCBSetupReq *R)
289 return XCBSetupReqAuthorizationProtocolNameLength(R);
292 XCBGenericIter XCBConnSetupReqAuthorizationProtocolNameEnd(XCBSetupReq *R)
294 return XCBSetupReqAuthorizationProtocolNameEnd(R);
297 char *XCBConnSetupReqAuthorizationProtocolData(XCBSetupReq *R)
299 return XCBSetupReqAuthorizationProtocolData(R);
302 int XCBConnSetupReqAuthorizationProtocolDataLength(XCBSetupReq *R)
304 return XCBSetupReqAuthorizationProtocolDataLength(R);
307 XCBGenericIter XCBConnSetupReqAuthorizationProtocolDataEnd(XCBSetupReq *R)
309 return XCBSetupReqAuthorizationProtocolDataEnd(R);
312 void XCBConnSetupReqNext(XCBSetupReqIter *i)
317 XCBGenericIter XCBConnSetupReqEnd(XCBSetupReqIter i)
319 return XCBSetupReqEnd(i);
322 char *XCBConnSetupFailedRepReason(XCBSetupFailed *R)
324 return XCBSetupFailedReason(R);
327 int XCBConnSetupFailedRepReasonLength(XCBSetupFailed *R)
329 return XCBSetupFailedReasonLength(R);
332 XCBGenericIter XCBConnSetupFailedRepReasonEnd(XCBSetupFailed *R)
334 return XCBSetupFailedReasonEnd(R);
337 void XCBConnSetupFailedRepNext(XCBSetupFailedIter *i)
339 XCBSetupFailedNext(i);
342 XCBGenericIter XCBConnSetupFailedRepEnd(XCBSetupFailedIter i)
344 return XCBSetupFailedEnd(i);
347 char *XCBConnSetupAuthenticateRepReason(XCBSetupAuthenticate *R)
349 return XCBSetupAuthenticateReason(R);
352 int XCBConnSetupAuthenticateRepReasonLength(XCBSetupAuthenticate *R)
354 return XCBSetupAuthenticateReasonLength(R);
357 XCBGenericIter XCBConnSetupAuthenticateRepReasonEnd(XCBSetupAuthenticate *R)
359 return XCBSetupAuthenticateReasonEnd(R);
362 void XCBConnSetupAuthenticateRepNext(XCBSetupAuthenticateIter *i)
364 XCBSetupAuthenticateNext(i);
367 XCBGenericIter XCBConnSetupAuthenticateRepEnd(XCBSetupAuthenticateIter i)
369 return XCBSetupAuthenticateEnd(i);
372 char *XCBConnSetupSuccessRepVendor(XCBSetup *R)
374 return XCBSetupVendor(R);
377 int XCBConnSetupSuccessRepVendorLength(XCBSetup *R)
379 return XCBSetupVendorLength(R);
382 XCBGenericIter XCBConnSetupSuccessRepVendorEnd(XCBSetup *R)
384 return XCBSetupVendorEnd(R);
387 XCBFORMAT *XCBConnSetupSuccessRepPixmapFormats(XCBSetup *R)
389 return XCBSetupPixmapFormats(R);
392 int XCBConnSetupSuccessRepPixmapFormatsLength(XCBSetup *R)
394 return XCBSetupPixmapFormatsLength(R);
397 XCBFORMATIter XCBConnSetupSuccessRepPixmapFormatsIter(XCBSetup *R)
399 return XCBSetupPixmapFormatsIter(R);
402 int XCBConnSetupSuccessRepRootsLength(XCBSetup *R)
404 return XCBSetupRootsLength(R);
407 XCBSCREENIter XCBConnSetupSuccessRepRootsIter(XCBSetup *R)
409 return XCBSetupRootsIter(R);
412 void XCBConnSetupSuccessRepNext(XCBSetupIter *i)
417 XCBGenericIter XCBConnSetupSuccessRepEnd(XCBSetupIter i)
419 return XCBSetupEnd(i);