1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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.
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.
26 /* Connection management: the core of XCB. */
33 #include <netinet/in.h>
34 #include <sys/select.h>
47 static const int error_connection = 1;
49 static int set_fd_flags(const int fd)
51 long flags = fcntl(fd, F_GETFL, 0);
55 if(fcntl(fd, F_SETFL, flags) == -1)
57 if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
62 static int write_setup(XCBConnection *c, XCBAuthInfo *auth_info)
64 static const char pad[3];
66 struct iovec parts[6];
68 int endian = 0x01020304;
71 memset(&out, 0, sizeof(out));
73 /* B = 0x42 = MSB first, l = 0x6c = LSB first */
74 if(htonl(endian) == endian)
75 out.byte_order = 0x42;
77 out.byte_order = 0x6c;
78 out.protocol_major_version = X_PROTOCOL;
79 out.protocol_minor_version = X_PROTOCOL_REVISION;
80 out.authorization_protocol_name_len = 0;
81 out.authorization_protocol_data_len = 0;
82 parts[count].iov_len = sizeof(XCBSetupReq);
83 parts[count++].iov_base = &out;
84 parts[count].iov_len = XCB_PAD(sizeof(XCBSetupReq));
85 parts[count++].iov_base = (char *) pad;
89 parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
90 parts[count++].iov_base = auth_info->name;
91 parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
92 parts[count++].iov_base = (char *) pad;
93 parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
94 parts[count++].iov_base = auth_info->data;
95 parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
96 parts[count++].iov_base = (char *) pad;
98 assert(count <= sizeof(parts) / sizeof(*parts));
100 pthread_mutex_lock(&c->iolock);
102 struct iovec *parts_ptr = parts;
103 ret = _xcb_out_send(c, &parts_ptr, &count);
105 pthread_mutex_unlock(&c->iolock);
109 static int read_setup(XCBConnection *c)
111 /* Read the server response */
112 c->setup = malloc(sizeof(XCBSetupGeneric));
116 if(_xcb_in_read_block(c, c->setup, sizeof(XCBSetupGeneric)) != sizeof(XCBSetupGeneric))
120 void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(XCBSetupGeneric));
126 if(_xcb_in_read_block(c, (char *) c->setup + sizeof(XCBSetupGeneric), c->setup->length * 4) <= 0)
129 /* 0 = failed, 2 = authenticate, 1 = success */
130 switch(c->setup->status)
134 XCBSetupFailed *setup = (XCBSetupFailed *) c->setup;
135 write(STDERR_FILENO, XCBSetupFailedReason(setup), XCBSetupFailedReasonLength(setup));
139 case 2: /* authenticate */
141 XCBSetupAuthenticate *setup = (XCBSetupAuthenticate *) c->setup;
142 write(STDERR_FILENO, XCBSetupAuthenticateReason(setup), XCBSetupAuthenticateReasonLength(setup));
150 /* precondition: there must be something for us to write. */
151 static int write_vec(XCBConnection *c, struct iovec **vector, int *count)
154 assert(!c->out.queue_len);
155 n = writev(c->fd, *vector, *count);
156 if(n < 0 && errno == EAGAIN)
161 for(; *count; --*count, ++*vector)
163 int cur = (*vector)->iov_len;
166 (*vector)->iov_len -= cur;
167 (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
169 if((*vector)->iov_len)
178 /* Public interface */
180 const XCBSetup *XCBGetSetup(XCBConnection *c)
184 /* doesn't need locking because it's never written to. */
188 int XCBGetFileDescriptor(XCBConnection *c)
192 /* doesn't need locking because it's never written to. */
196 int XCBConnectionHasError(XCBConnection *c)
198 /* doesn't need locking because it's read and written atomically. */
202 XCBConnection *XCBConnectToFD(int fd, XCBAuthInfo *auth_info)
206 c = calloc(1, sizeof(XCBConnection));
208 return (XCBConnection *) &error_connection;
214 pthread_mutex_init(&c->iolock, 0) == 0 &&
215 _xcb_in_init(&c->in) &&
216 _xcb_out_init(&c->out) &&
217 write_setup(c, auth_info) &&
224 return (XCBConnection *) &error_connection;
230 void XCBDisconnect(XCBConnection *c)
238 pthread_mutex_destroy(&c->iolock);
239 _xcb_in_destroy(&c->in);
240 _xcb_out_destroy(&c->out);
248 /* Private interface */
250 void _xcb_conn_shutdown(XCBConnection *c)
255 int _xcb_conn_wait(XCBConnection *c, pthread_cond_t *cond, struct iovec **vector, int *count)
260 /* If the thing I should be doing is already being done, wait for it. */
261 if(count ? c->out.writing : c->in.reading)
263 pthread_cond_wait(cond, &c->iolock);
268 FD_SET(c->fd, &rfds);
274 FD_SET(c->fd, &wfds);
278 pthread_mutex_unlock(&c->iolock);
280 ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
281 } while (ret == -1 && errno == EINTR);
284 pthread_mutex_lock(&c->iolock);
288 if(FD_ISSET(c->fd, &rfds))
289 ret = ret && _xcb_in_read(c);
291 if(FD_ISSET(c->fd, &wfds))
292 ret = ret && write_vec(c, vector, count);