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