Move _xcb_read_block to xcb_in.c and make it static. Change calls in xcb_conn.c to...
[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/select.h>
30 #include <sys/socket.h>
31 #include <sys/fcntl.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <netdb.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40
41 #include "xcb.h"
42 #include "xcbext.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 int XCBOpen(const char *host, const int display)
94 {
95     int fd;
96
97     if(*host)
98     {
99         /* display specifies TCP */
100         unsigned short port = X_TCP_PORT + display;
101         fd = XCBOpenTCP(host, port);
102     }
103     else
104     {
105         /* display specifies Unix socket */
106         static const char base[] = "/tmp/.X11-unix/X";
107         char file[sizeof(base) + 20];
108         snprintf(file, sizeof(file), "%s%d", base, display);
109         fd = XCBOpenUnix(file);
110     }
111
112     return fd;
113 }
114
115 int XCBOpenTCP(const char *host, const unsigned short port)
116 {
117     int fd;
118     struct sockaddr_in addr;
119     struct hostent *hostaddr = gethostbyname(host);
120     if(!hostaddr)
121         return -1;
122     addr.sin_family = AF_INET;
123     addr.sin_port = htons(port);
124     memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
125
126     fd = socket(PF_INET, SOCK_STREAM, 0);
127     if(fd == -1)
128         return -1;
129     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
130         return -1;
131     return fd;
132 }
133
134 int XCBOpenUnix(const char *file)
135 {
136     int fd;
137     struct sockaddr_un addr = { AF_UNIX };
138     strcpy(addr.sun_path, file);
139
140     fd = socket(AF_UNIX, SOCK_STREAM, 0);
141     if(fd == -1)
142         return -1;
143     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
144         return -1;
145     return fd;
146 }
147
148 XCBConnection *XCBConnect(const char *displayname, int *screenp)
149 {
150     int fd, display = 0;
151     char *host;
152     XCBConnection *c;
153     XCBAuthInfo auth;
154
155     if(!XCBParseDisplay(displayname, &host, &display, screenp))
156         return 0;
157     fd = XCBOpen(host, display);
158     free(host);
159     if(fd == -1)
160         return 0;
161
162     XCBGetAuthInfo(fd, &auth);
163     c = XCBConnectToFD(fd, &auth);
164     free(auth.name);
165     free(auth.data);
166     return c;
167 }
168
169 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
170 {
171     int fd, display = 0;
172     char *host;
173
174     if(!XCBParseDisplay(displayname, &host, &display, screenp))
175         return 0;
176     fd = XCBOpen(host, display);
177     free(host);
178     if(fd == -1)
179         return 0;
180
181     return XCBConnectToFD(fd, auth);
182 }
183
184 /* backwards compatible interface: remove before 1.0 release */
185 XCBConnection *XCBConnectBasic()
186 {
187     XCBConnection *c = XCBConnect(0, 0);
188     if(c)
189         return c;
190     perror("XCBConnect");
191     abort();
192 }
193
194 int XCBSync(XCBConnection *c, XCBGenericError **e)
195 {
196     XCBGetInputFocusRep *reply = XCBGetInputFocusReply(c, XCBGetInputFocus(c), e);
197     free(reply);
198     return reply != 0;
199 }
200
201 /* The functions beyond this point still use only public interfaces,
202  * but are not themselves part of the public interface. So their
203  * prototypes are in xcbint.h. */
204
205 #include "xcbint.h"
206
207 int _xcb_set_fd_flags(const int fd)
208 {
209     long flags = fcntl(fd, F_GETFL, 0);
210     if(flags == -1)
211         return 0;
212     flags |= O_NONBLOCK;
213     if(fcntl(fd, F_SETFL, flags) == -1)
214         return 0;
215     if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
216         return 0;
217     return 1;
218 }