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