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