Merge branch 'master' of git+ssh://git.freedesktop.org/git/xcb
[free-sw/xcb/libxcb] / src / xcb_conn.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 /* Connection management: the core of XCB. */
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <netinet/in.h>
34 #include <sys/select.h>
35 #include <sys/fcntl.h>
36 #include <errno.h>
37
38 #include "xcb.h"
39 #include "xcbint.h"
40
41 static int set_fd_flags(const int fd)
42 {
43     long flags = fcntl(fd, F_GETFL, 0);
44     if(flags == -1)
45         return 0;
46     flags |= O_NONBLOCK;
47     if(fcntl(fd, F_SETFL, flags) == -1)
48         return 0;
49     if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
50         return 0;
51     return 1;
52 }
53
54 static int write_setup(XCBConnection *c, XCBAuthInfo *auth_info)
55 {
56     static const char pad[3];
57     XCBConnSetupReq out;
58     struct iovec parts[6];
59     int count = 0;
60     int endian = 0x01020304;
61     int ret;
62
63     memset(&out, 0, sizeof(out));
64
65     /* B = 0x42 = MSB first, l = 0x6c = LSB first */
66     if(htonl(endian) == endian)
67         out.byte_order = 0x42;
68     else
69         out.byte_order = 0x6c;
70     out.protocol_major_version = X_PROTOCOL;
71     out.protocol_minor_version = X_PROTOCOL_REVISION;
72     out.authorization_protocol_name_len = 0;
73     out.authorization_protocol_data_len = 0;
74     parts[count].iov_len = sizeof(XCBConnSetupReq);
75     parts[count++].iov_base = &out;
76     parts[count].iov_len = XCB_PAD(sizeof(XCBConnSetupReq));
77     parts[count++].iov_base = (char *) pad;
78
79     if(auth_info)
80     {
81         parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
82         parts[count++].iov_base = auth_info->name;
83         parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
84         parts[count++].iov_base = (char *) pad;
85         parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
86         parts[count++].iov_base = auth_info->data;
87         parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
88         parts[count++].iov_base = (char *) pad;
89     }
90     assert(count <= sizeof(parts) / sizeof(*parts));
91
92     pthread_mutex_lock(&c->iolock);
93     {
94         struct iovec *parts_ptr = parts;
95         ret = _xcb_out_send(c, &parts_ptr, &count);
96     }
97     pthread_mutex_unlock(&c->iolock);
98     return ret;
99 }
100
101 static int read_setup(XCBConnection *c)
102 {
103     /* Read the server response */
104     c->setup = malloc(sizeof(XCBConnSetupGenericRep));
105     if(!c->setup)
106         return 0;
107
108     if(_xcb_in_read_block(c, c->setup, sizeof(XCBConnSetupGenericRep)) != sizeof(XCBConnSetupGenericRep))
109         return 0;
110
111     {
112         void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(XCBConnSetupGenericRep));
113         if(!tmp)
114             return 0;
115         c->setup = tmp;
116     }
117
118     if(_xcb_in_read_block(c, (char *) c->setup + sizeof(XCBConnSetupGenericRep), c->setup->length * 4) <= 0)
119         return 0;
120
121     /* 0 = failed, 2 = authenticate, 1 = success */
122     switch(c->setup->status)
123     {
124     case 0: /* failed */
125         {
126             XCBConnSetupFailedRep *setup = (XCBConnSetupFailedRep *) c->setup;
127             write(STDERR_FILENO, XCBConnSetupFailedRepReason(setup), XCBConnSetupFailedRepReasonLength(setup));
128             return 0;
129         }
130
131     case 2: /* authenticate */
132         {
133             XCBConnSetupAuthenticateRep *setup = (XCBConnSetupAuthenticateRep *) c->setup;
134             write(STDERR_FILENO, XCBConnSetupAuthenticateRepReason(setup), XCBConnSetupAuthenticateRepReasonLength(setup));
135             return 0;
136         }
137     }
138
139     return 1;
140 }
141
142 /* precondition: there must be something for us to write. */
143 static int write_vec(XCBConnection *c, struct iovec **vector, int *count)
144 {
145     int n;
146     assert(!c->out.queue_len);
147     n = writev(c->fd, *vector, *count);
148     if(n < 0 && errno == EAGAIN)
149         return 1;
150     if(n <= 0)
151         return 0;
152
153     for(; *count; --*count, ++*vector)
154     {
155         int cur = (*vector)->iov_len;
156         if(cur > n)
157             cur = n;
158         (*vector)->iov_len -= cur;
159         (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
160         n -= cur;
161         if((*vector)->iov_len)
162             break;
163     }
164     if(!*count)
165         *vector = 0;
166     assert(n == 0);
167     return 1;
168 }
169
170 /* Public interface */
171
172 XCBConnSetupSuccessRep *XCBGetSetup(XCBConnection *c)
173 {
174     /* doesn't need locking because it's never written to. */
175     return c->setup;
176 }
177
178 int XCBGetFileDescriptor(XCBConnection *c)
179 {
180     /* doesn't need locking because it's never written to. */
181     return c->fd;
182 }
183
184 XCBConnection *XCBConnectToFD(int fd, XCBAuthInfo *auth_info)
185 {
186     XCBConnection* c;
187
188     c = calloc(1, sizeof(XCBConnection));
189     if(!c)
190         return 0;
191
192     c->fd = fd;
193
194     if(!(
195         set_fd_flags(fd) &&
196         pthread_mutex_init(&c->iolock, 0) == 0 &&
197         _xcb_in_init(&c->in) &&
198         _xcb_out_init(&c->out) &&
199         write_setup(c, auth_info) &&
200         read_setup(c) &&
201         _xcb_ext_init(c) &&
202         _xcb_xid_init(c)
203         ))
204     {
205         XCBDisconnect(c);
206         return 0;
207     }
208
209     return c;
210 }
211
212 void XCBDisconnect(XCBConnection *c)
213 {
214     if(!c)
215         return;
216
217     free(c->setup);
218     close(c->fd);
219
220     pthread_mutex_destroy(&c->iolock);
221     _xcb_in_destroy(&c->in);
222     _xcb_out_destroy(&c->out);
223
224     _xcb_ext_destroy(c);
225     _xcb_xid_destroy(c);
226
227     free(c);
228 }
229
230 /* Private interface */
231
232 int _xcb_conn_wait(XCBConnection *c, pthread_cond_t *cond, struct iovec **vector, int *count)
233 {
234     int ret;
235     fd_set rfds, wfds;
236
237     /* If the thing I should be doing is already being done, wait for it. */
238     if(count ? c->out.writing : c->in.reading)
239     {
240         pthread_cond_wait(cond, &c->iolock);
241         return 1;
242     }
243
244     FD_ZERO(&rfds);
245     FD_SET(c->fd, &rfds);
246     ++c->in.reading;
247
248     FD_ZERO(&wfds);
249     if(count)
250     {
251         FD_SET(c->fd, &wfds);
252         ++c->out.writing;
253     }
254
255     pthread_mutex_unlock(&c->iolock);
256     ret = select(c->fd + 1, &rfds, &wfds, 0, 0) > 0;
257     pthread_mutex_lock(&c->iolock);
258
259     if(ret)
260     {
261         if(FD_ISSET(c->fd, &rfds))
262             ret = ret && _xcb_in_read(c);
263
264         if(FD_ISSET(c->fd, &wfds))
265             ret = ret && write_vec(c, vector, count);
266     }
267
268     if(count)
269         --c->out.writing;
270     --c->in.reading;
271
272     return ret;
273 }