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/select.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
43 int XCBPopcount(CARD32 mask)
46 y = (mask >> 1) & 033333333333;
47 y = mask - y - ((y >> 1) & 033333333333);
48 return ((y + (y >> 3)) & 030707070707) % 077;
51 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
53 int len, display, screen;
54 char *colon, *dot, *end;
56 name = getenv("DISPLAY");
59 colon = strrchr(name, ':');
64 display = strtoul(colon, &dot, 10);
74 screen = strtoul(dot, &end, 10);
75 if(end == dot || *end != '\0')
78 /* At this point, the display string is fully parsed and valid, but
79 * the caller's memory is untouched. */
81 *host = malloc(len + 1);
84 memcpy(*host, name, len);
92 static int _xcb_open_tcp(const char *host, const unsigned short port);
93 static int _xcb_open_unix(const char *file);
95 static int _xcb_open(const char *host, const int display)
101 /* display specifies TCP */
102 unsigned short port = X_TCP_PORT + display;
103 fd = _xcb_open_tcp(host, port);
107 /* display specifies Unix socket */
108 static const char base[] = "/tmp/.X11-unix/X";
109 char file[sizeof(base) + 20];
110 snprintf(file, sizeof(file), "%s%d", base, display);
111 fd = _xcb_open_unix(file);
117 static int _xcb_open_tcp(const char *host, const unsigned short port)
120 struct sockaddr_in addr;
121 struct hostent *hostaddr = gethostbyname(host);
124 addr.sin_family = AF_INET;
125 addr.sin_port = htons(port);
126 memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
128 fd = socket(PF_INET, SOCK_STREAM, 0);
131 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
136 static int _xcb_open_unix(const char *file)
139 struct sockaddr_un addr = { AF_UNIX };
140 strcpy(addr.sun_path, file);
142 fd = socket(AF_UNIX, SOCK_STREAM, 0);
145 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
150 XCBConnection *XCBConnect(const char *displayname, int *screenp)
157 if(!XCBParseDisplay(displayname, &host, &display, screenp))
159 fd = _xcb_open(host, display);
164 XCBGetAuthInfo(fd, &auth);
165 c = XCBConnectToFD(fd, &auth);
171 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
176 if(!XCBParseDisplay(displayname, &host, &display, screenp))
178 fd = _xcb_open(host, display);
183 return XCBConnectToFD(fd, auth);
186 int XCBSync(XCBConnection *c, XCBGenericError **e)
188 XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
196 /* backwards compatible interfaces: remove before 1.0 release */
197 XCBConnection *XCBConnectBasic()
199 XCBConnection *c = XCBConnect(0, 0);
202 perror("XCBConnect");
206 int XCBOpen(const char *host, const int display)
208 return _xcb_open(host, display);
211 int XCBOpenTCP(const char *host, const unsigned short port)
213 return _xcb_open_tcp(host, port);
216 int XCBOpenUnix(const char *file)
218 return _xcb_open_unix(file);