Remove deprecated public API functions in preparation for 1.0 release. Xlib-specific...
[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 #ifdef DNETCONN
34 #include <netdnet/dnetdb.h>
35 #include <netdnet/dn.h>
36 #endif
37 #include <netdb.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43
44 #include "xcb.h"
45 #include "xcbext.h"
46 #include "xcbint.h"
47
48 int XCBPopcount(CARD32 mask)
49 {
50     unsigned long y;
51     y = (mask >> 1) & 033333333333;
52     y = mask - y - ((y >> 1) & 033333333333);
53     return ((y + (y >> 3)) & 030707070707) % 077;
54 }
55
56 int XCBParseDisplay(const char *name, char **host, int *displayp, int *screenp)
57 {
58     int len, display, screen;
59     char *colon, *dot, *end;
60     if(!name || !*name)
61         name = getenv("DISPLAY");
62     if(!name)
63         return 0;
64     colon = strrchr(name, ':');
65     if(!colon)
66         return 0;
67     len = colon - name;
68     ++colon;
69     display = strtoul(colon, &dot, 10);
70     if(dot == colon)
71         return 0;
72     if(*dot == '\0')
73         screen = 0;
74     else
75     {
76         if(*dot != '.')
77             return 0;
78         ++dot;
79         screen = strtoul(dot, &end, 10);
80         if(end == dot || *end != '\0')
81             return 0;
82     }
83     /* At this point, the display string is fully parsed and valid, but
84      * the caller's memory is untouched. */
85
86     *host = malloc(len + 1);
87     if(!*host)
88         return 0;
89     memcpy(*host, name, len);
90     (*host)[len] = '\0';
91     *displayp = display;
92     if(screenp)
93         *screenp = screen;
94     return 1;
95 }
96
97 static int _xcb_open_tcp(const char *host, const unsigned short port);
98 static int _xcb_open_unix(const char *file);
99 #ifdef DNETCONN
100 static int _xcb_open_decnet(const char *host, const unsigned short port);
101 #endif
102
103 static int _xcb_open(const char *host, const int display)
104 {
105     int fd;
106
107     if(*host)
108     {
109 #ifdef DNETCONN
110         if (strchr(host,  ':'))
111         {
112             /* DECnet displays have two colons, so the parser will have left
113                one at the end */
114             char *dnethost = strdup(host);
115
116             dnethost[strlen(dnethost)-1] = '\0';
117             fd = _xcb_open_decnet(dnethost, display);
118             free(dnethost);
119         }
120         else
121 #endif
122         {
123             /* display specifies TCP */
124             unsigned short port = X_TCP_PORT + display;
125             fd = _xcb_open_tcp(host, port);
126         }
127     }
128     else
129     {
130         /* display specifies Unix socket */
131         static const char base[] = "/tmp/.X11-unix/X";
132         char file[sizeof(base) + 20];
133         snprintf(file, sizeof(file), "%s%d", base, display);
134         fd = _xcb_open_unix(file);
135     }
136
137     return fd;
138 }
139
140 #ifdef DNETCONN
141 static int _xcb_open_decnet(const char *host, const unsigned short port)
142 {
143     int fd;
144     struct sockaddr_dn addr;
145     struct accessdata_dn accessdata;
146     struct nodeent *nodeaddr = getnodebyname(host);
147
148     if(!nodeaddr)
149         return -1;
150     addr.sdn_family = AF_DECnet;
151
152     addr.sdn_add.a_len = nodeaddr->n_length;
153     memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
154
155     sprintf((char *)addr.sdn_objname, "X$X%d", port);
156     addr.sdn_objnamel = strlen((char *)addr.sdn_objname);
157     addr.sdn_objnum = 0;
158
159     fd = socket(PF_DECnet, SOCK_STREAM, 0);
160     if(fd == -1)
161         return -1;
162
163     memset(&accessdata, 0, sizeof(accessdata));
164     sprintf((char*)accessdata.acc_acc, "%d", getuid());
165     accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
166     setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
167
168     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
169         return -1;
170     return fd;
171 }
172 #endif
173
174 static int _xcb_open_tcp(const char *host, const unsigned short port)
175 {
176     int fd;
177     struct sockaddr_in addr;
178     struct hostent *hostaddr = gethostbyname(host);
179     if(!hostaddr)
180         return -1;
181     addr.sin_family = AF_INET;
182     addr.sin_port = htons(port);
183     memcpy(&addr.sin_addr, hostaddr->h_addr_list[0], sizeof(addr.sin_addr));
184
185     fd = socket(PF_INET, SOCK_STREAM, 0);
186     if(fd == -1)
187         return -1;
188     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
189         return -1;
190     return fd;
191 }
192
193 static int _xcb_open_unix(const char *file)
194 {
195     int fd;
196     struct sockaddr_un addr = { AF_UNIX };
197     strcpy(addr.sun_path, file);
198
199     fd = socket(AF_UNIX, SOCK_STREAM, 0);
200     if(fd == -1)
201         return -1;
202     if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
203         return -1;
204     return fd;
205 }
206
207 XCBConnection *XCBConnect(const char *displayname, int *screenp)
208 {
209     int fd, display = 0;
210     char *host;
211     XCBConnection *c;
212     XCBAuthInfo auth;
213
214     if(!XCBParseDisplay(displayname, &host, &display, screenp))
215         return 0;
216     fd = _xcb_open(host, display);
217     free(host);
218     if(fd == -1)
219         return 0;
220
221     _xcb_get_auth_info(fd, &auth);
222     c = XCBConnectToFD(fd, &auth);
223     free(auth.name);
224     free(auth.data);
225     return c;
226 }
227
228 XCBConnection *XCBConnectToDisplayWithAuthInfo(const char *displayname, XCBAuthInfo *auth, int *screenp)
229 {
230     int fd, display = 0;
231     char *host;
232
233     if(!XCBParseDisplay(displayname, &host, &display, screenp))
234         return 0;
235     fd = _xcb_open(host, display);
236     free(host);
237     if(fd == -1)
238         return 0;
239
240     return XCBConnectToFD(fd, auth);
241 }