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>
45 } xcb_setup_generic_t;
47 static const int error_connection = 1;
49 static int set_fd_flags(const int fd)
51 int 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 _xcb_xlib_init(_xcb_xlib *xlib)
66 xlib->sloppy_lock = (getenv("LIBXCB_ALLOW_SLOPPY_LOCK") != 0);
68 pthread_cond_init(&xlib->cond, 0);
72 static void _xcb_xlib_destroy(_xcb_xlib *xlib)
74 pthread_cond_destroy(&xlib->cond);
77 static int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info)
79 static const char pad[3];
80 xcb_setup_request_t out;
81 struct iovec parts[6];
83 static const uint32_t endian = 0x01020304;
86 memset(&out, 0, sizeof(out));
88 /* B = 0x42 = MSB first, l = 0x6c = LSB first */
89 if(htonl(endian) == endian)
90 out.byte_order = 0x42;
92 out.byte_order = 0x6c;
93 out.protocol_major_version = X_PROTOCOL;
94 out.protocol_minor_version = X_PROTOCOL_REVISION;
95 out.authorization_protocol_name_len = 0;
96 out.authorization_protocol_data_len = 0;
97 parts[count].iov_len = sizeof(xcb_setup_request_t);
98 parts[count++].iov_base = &out;
99 parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t));
100 parts[count++].iov_base = (char *) pad;
104 parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen;
105 parts[count++].iov_base = auth_info->name;
106 parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len);
107 parts[count++].iov_base = (char *) pad;
108 parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen;
109 parts[count++].iov_base = auth_info->data;
110 parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len);
111 parts[count++].iov_base = (char *) pad;
113 assert(count <= (int) (sizeof(parts) / sizeof(*parts)));
117 struct iovec *parts_ptr = parts;
118 ret = _xcb_out_send(c, &parts_ptr, &count);
124 static int read_setup(xcb_connection_t *c)
126 /* Read the server response */
127 c->setup = malloc(sizeof(xcb_setup_generic_t));
131 if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t))
135 void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t));
141 if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0)
144 /* 0 = failed, 2 = authenticate, 1 = success */
145 switch(c->setup->status)
149 xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup;
150 write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup));
154 case 2: /* authenticate */
156 xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup;
157 write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup));
165 /* precondition: there must be something for us to write. */
166 static int write_vec(xcb_connection_t *c, struct iovec **vector, int *count)
169 assert(!c->out.queue_len);
170 n = writev(c->fd, *vector, *count);
171 if(n < 0 && errno == EAGAIN)
175 _xcb_conn_shutdown(c);
179 for(; *count; --*count, ++*vector)
181 int cur = (*vector)->iov_len;
184 (*vector)->iov_len -= cur;
185 (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
187 if((*vector)->iov_len)
196 /* Public interface */
198 const xcb_setup_t *xcb_get_setup(xcb_connection_t *c)
202 /* doesn't need locking because it's never written to. */
206 int xcb_get_file_descriptor(xcb_connection_t *c)
210 /* doesn't need locking because it's never written to. */
214 int xcb_connection_has_error(xcb_connection_t *c)
216 /* doesn't need locking because it's read and written atomically. */
220 xcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info)
224 c = calloc(1, sizeof(xcb_connection_t));
226 return (xcb_connection_t *) &error_connection;
232 pthread_mutex_init(&c->iolock, 0) == 0 &&
233 _xcb_xlib_init(&c->xlib) &&
234 _xcb_in_init(&c->in) &&
235 _xcb_out_init(&c->out) &&
236 write_setup(c, auth_info) &&
243 return (xcb_connection_t *) &error_connection;
249 void xcb_disconnect(xcb_connection_t *c)
257 pthread_mutex_destroy(&c->iolock);
258 _xcb_xlib_destroy(&c->xlib);
259 _xcb_in_destroy(&c->in);
260 _xcb_out_destroy(&c->out);
268 /* Private interface */
270 void _xcb_conn_shutdown(xcb_connection_t *c)
275 void _xcb_lock_io(xcb_connection_t *c)
277 pthread_mutex_lock(&c->iolock);
280 if(pthread_equal(c->xlib.thread, pthread_self()))
282 pthread_cond_wait(&c->xlib.cond, &c->iolock);
286 void _xcb_unlock_io(xcb_connection_t *c)
288 pthread_mutex_unlock(&c->iolock);
291 void _xcb_wait_io(xcb_connection_t *c, pthread_cond_t *cond)
293 int xlib_locked = c->xlib.lock;
297 pthread_cond_broadcast(&c->xlib.cond);
299 pthread_cond_wait(cond, &c->iolock);
303 pthread_cond_wait(&c->xlib.cond, &c->iolock);
305 c->xlib.thread = pthread_self();
309 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count)
311 int ret, xlib_locked;
314 /* If the thing I should be doing is already being done, wait for it. */
315 if(count ? c->out.writing : c->in.reading)
317 _xcb_wait_io(c, cond);
322 FD_SET(c->fd, &rfds);
328 FD_SET(c->fd, &wfds);
332 xlib_locked = c->xlib.lock;
336 pthread_cond_broadcast(&c->xlib.cond);
340 ret = select(c->fd + 1, &rfds, &wfds, 0, 0);
341 } while (ret == -1 && errno == EINTR);
344 _xcb_conn_shutdown(c);
351 c->xlib.thread = pthread_self();
356 if(FD_ISSET(c->fd, &rfds))
357 ret = ret && _xcb_in_read(c);
359 if(FD_ISSET(c->fd, &wfds))
360 ret = ret && write_vec(c, vector, count);