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>
44 int XCBPopcount(CARD32 mask)
47 y = (mask >> 1) & 033333333333;
48 y = mask - y - ((y >> 1) & 033333333333);
49 return ((y + (y >> 3)) & 030707070707) % 077;
52 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
54 int len, display, screen;
55 char *colon, *dot, *end;
57 name = getenv("DISPLAY");
60 colon = strrchr(name, ':');
65 display = strtoul(colon, &dot, 10);
75 screen = strtoul(dot, &end, 10);
76 if(end == dot || *end != '\0')
79 /* At this point, the display string is fully parsed and valid, but
80 * the caller's memory is untouched. */
82 *host = malloc(len + 1);
85 memcpy(*host, name, len);
93 static int _xcb_open_tcp(const char *host, const unsigned short port);
94 static int _xcb_open_unix(const char *file);
96 static int _xcb_open(const char *host, const int display)
102 /* display specifies TCP */
103 unsigned short port = X_TCP_PORT + display;
104 fd = _xcb_open_tcp(host, port);
108 /* display specifies Unix socket */
109 static const char base[] = "/tmp/.X11-unix/X";
110 char file[sizeof(base) + 20];
111 snprintf(file, sizeof(file), "%s%d", base, display);
112 fd = _xcb_open_unix(file);
118 static int _xcb_open_tcp(const char *host, const unsigned short port)
121 struct sockaddr_in addr;
122 struct hostent *hostaddr = gethostbyname(host);
125 addr.sin_family = AF_INET;
126 addr.sin_port = htons(port);
127 memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
129 fd = socket(PF_INET, SOCK_STREAM, 0);
132 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
137 static int _xcb_open_unix(const char *file)
140 struct sockaddr_un addr = { AF_UNIX };
141 strcpy(addr.sun_path, file);
143 fd = socket(AF_UNIX, SOCK_STREAM, 0);
146 if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
151 XCBConnection *XCBConnect(const char *displayname, int *screenp)
158 if(!XCBParseDisplay(displayname, &host, &display, screenp))
160 fd = _xcb_open(host, display);
165 _xcb_get_auth_info(fd, &auth);
166 c = XCBConnectToFD(fd, &auth);
172 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
177 if(!XCBParseDisplay(displayname, &host, &display, screenp))
179 fd = _xcb_open(host, display);
184 return XCBConnectToFD(fd, auth);
187 int XCBSync(XCBConnection *c, XCBGenericError **e)
189 XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
197 /* backwards compatible interfaces: remove before 1.0 release */
198 XCBConnection *XCBConnectBasic()
200 XCBConnection *c = XCBConnect(0, 0);
203 perror("XCBConnect");
207 int XCBOpen(const char *host, const int display)
209 return _xcb_open(host, display);
212 int XCBOpenTCP(const char *host, const unsigned short port)
214 return _xcb_open_tcp(host, port);
217 int XCBOpenUnix(const char *file)
219 return _xcb_open_unix(file);
222 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
224 return _xcb_get_auth_info(fd, info);