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