Remove unnecessary include. Noticed by jamey.
[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 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39
40 #include "xcb.h"
41 #include "xcbext.h"
42 #include "xcbint.h"
43
44 int XCBPopcount(CARD32 mask)
45 {
46     unsigned long y;
47     y = (mask >> 1) & 033333333333;
48     y = mask - y - ((y >> 1) & 033333333333);
49     return ((y + (y >> 3)) & 030707070707) % 077;
50 }
51
52 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
53 {
54     int len, display, screen;
55     char *colon, *dot, *end;
56     if(!name || !*name)
57         name = getenv("DISPLAY");
58     if(!name)
59         return 0;
60     colon = strrchr(name, ':');
61     if(!colon)
62         return 0;
63     len = colon - name;
64     ++colon;
65     display = strtoul(colon, &dot, 10);
66     if(dot == colon)
67         return 0;
68     if(*dot == '\0')
69         screen = 0;
70     else
71     {
72         if(*dot != '.')
73             return 0;
74         ++dot;
75         screen = strtoul(dot, &end, 10);
76         if(end == dot || *end != '\0')
77             return 0;
78     }
79     /* At this point, the display string is fully parsed and valid, but
80      * the caller's memory is untouched. */
81
82     *host = malloc(len + 1);
83     if(!*host)
84         return 0;
85     memcpy(*host, name, len);
86     (*host)[len] = '\0';
87     *displayp = display;
88     if(screenp)
89         *screenp = screen;
90     return 1;
91 }
92
93 static int _xcb_open_tcp(const char *host, const unsigned short port);
94 static int _xcb_open_unix(const char *file);
95
96 static int _xcb_open(const char *host, const int display)
97 {
98     int fd;
99
100     if(*host)
101     {
102         /* display specifies TCP */
103         unsigned short port = X_TCP_PORT + display;
104         fd = _xcb_open_tcp(host, port);
105     }
106     else
107     {
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);
113     }
114
115     return fd;
116 }
117
118 static int _xcb_open_tcp(const char *host, const unsigned short port)
119 {
120     int fd;
121     struct sockaddr_in addr;
122     struct hostent *hostaddr = gethostbyname(host);
123     if(!hostaddr)
124         return -1;
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));
128
129     fd = socket(PF_INET, SOCK_STREAM, 0);
130     if(fd == -1)
131         return -1;
132     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
133         return -1;
134     return fd;
135 }
136
137 static int _xcb_open_unix(const char *file)
138 {
139     int fd;
140     struct sockaddr_un addr = { AF_UNIX };
141     strcpy(addr.sun_path, file);
142
143     fd = socket(AF_UNIX, SOCK_STREAM, 0);
144     if(fd == -1)
145         return -1;
146     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
147         return -1;
148     return fd;
149 }
150
151 XCBConnection *XCBConnect(const char *displayname, int *screenp)
152 {
153     int fd, display = 0;
154     char *host;
155     XCBConnection *c;
156     XCBAuthInfo auth;
157
158     if(!XCBParseDisplay(displayname, &host, &display, screenp))
159         return 0;
160     fd = _xcb_open(host, display);
161     free(host);
162     if(fd == -1)
163         return 0;
164
165     _xcb_get_auth_info(fd, &auth);
166     c = XCBConnectToFD(fd, &auth);
167     free(auth.name);
168     free(auth.data);
169     return c;
170 }
171
172 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
173 {
174     int fd, display = 0;
175     char *host;
176
177     if(!XCBParseDisplay(displayname, &host, &display, screenp))
178         return 0;
179     fd = _xcb_open(host, display);
180     free(host);
181     if(fd == -1)
182         return 0;
183
184     return XCBConnectToFD(fd, auth);
185 }
186
187 int XCBSync(XCBConnection *c, XCBGenericError **e)
188 {
189     XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
190     free(reply);
191     return reply != 0;
192 }
193
194
195
196
197 /* backwards compatible interfaces: remove before 1.0 release */
198 XCBConnection *XCBConnectBasic()
199 {
200     XCBConnection *c = XCBConnect(0, 0);
201     if(c)
202         return c;
203     perror("XCBConnect");
204     abort();
205 }
206
207 int XCBOpen(const char *host, const int display)
208 {
209         return _xcb_open(host, display);
210 }
211
212 int XCBOpenTCP(const char *host, const unsigned short port)
213 {
214         return _xcb_open_tcp(host, port);
215 }
216
217 int XCBOpenUnix(const char *file)
218 {
219         return _xcb_open_unix(file);
220 }
221
222 int XCBGetAuthInfo(int fd, XCBAuthInfo *info)
223 {
224         return _xcb_get_auth_info(fd, info);
225 }