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