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 /* Stuff that sends stuff to the server. */
47 #include "extensions/bigreq.h"
49 static int force_sequence_wrap(XCBConnection *c)
52 if((c->out.request - c->in.request_read) > 65530)
54 pthread_mutex_unlock(&c->iolock);
56 pthread_mutex_lock(&c->iolock);
61 static int _xcb_write(const int fd, char (*buf)[], int *count)
63 int n = write(fd, *buf, *count);
68 memmove(*buf, *buf + n, *count);
73 static int _xcb_writev(const int fd, struct iovec *vec, int count)
75 int n = writev(fd, vec, count);
79 for(; count; --count, ++vec)
81 int cur = vec->iov_len;
85 vec->iov_base = (char *) vec->iov_base + cur;
95 /* Public interface */
97 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
99 pthread_mutex_lock(&c->out.reqlenlock);
100 if(!c->out.maximum_request_length)
102 const XCBQueryExtensionRep *ext;
103 c->out.maximum_request_length = c->setup->maximum_request_length;
104 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
105 if(ext && ext->present)
107 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
108 c->out.maximum_request_length = r->maximum_request_length;
112 pthread_mutex_unlock(&c->out.reqlenlock);
113 return c->out.maximum_request_length;
116 int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
118 static const char pad[3];
121 struct iovec *padded;
125 enum workarounds workaround = WORKAROUND_NONE;
128 assert(request != 0);
130 assert(req->count > 0);
132 /* set the major opcode, and the minor opcode for extensions */
135 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
136 /* TODO: better error handling here, please! */
137 assert(extension && extension->present);
138 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
139 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
141 /* do we need to work around the X server bug described in glx.xml? */
142 if(strcmp(req->ext->name, "GLX") &&
143 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
145 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
148 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
150 /* put together the length field, possibly using BIGREQUESTS */
151 for(i = 0; i < req->count; ++i)
152 longlen += XCB_CEIL(vector[i].iov_len) >> 2;
154 if(longlen > c->setup->maximum_request_length)
156 if(longlen > XCBGetMaximumRequestLength(c))
157 return 0; /* server can't take this; maybe need BIGREQUESTS? */
161 /* we don't need BIGREQUESTS. */
172 ((req->count * 2 + 3) * sizeof(struct iovec));
173 /* set the length field. */
174 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
177 padded[0].iov_base = vector[0].iov_base;
178 padded[0].iov_len = sizeof(CARD32);
179 vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
180 vector[0].iov_len -= sizeof(CARD32);
182 padded[1].iov_base = &longlen;
183 padded[1].iov_len = sizeof(CARD32);
187 for(i = 0; i < req->count; ++i)
189 if(!vector[i].iov_len)
191 padded[padlen].iov_base = vector[i].iov_base;
192 padded[padlen++].iov_len = vector[i].iov_len;
193 if(!XCB_PAD(vector[i].iov_len))
195 padded[padlen].iov_base = (caddr_t) pad;
196 padded[padlen++].iov_len = XCB_PAD(vector[i].iov_len);
199 /* get a sequence number and arrange for delivery. */
200 pthread_mutex_lock(&c->iolock);
201 if(req->isvoid && !force_sequence_wrap(c))
203 pthread_mutex_unlock(&c->iolock);
210 *request = ++c->out.request;
213 _xcb_in_expect_reply(c, *request, workaround);
215 ret = _xcb_out_write_block(c, padded, padlen);
216 pthread_mutex_unlock(&c->iolock);
224 int XCBFlush(XCBConnection *c)
227 pthread_mutex_lock(&c->iolock);
228 ret = _xcb_out_flush(c);
229 pthread_mutex_unlock(&c->iolock);
233 /* Private interface */
235 int _xcb_out_init(_xcb_out *out)
237 if(pthread_cond_init(&out->cond, 0))
246 out->request_written = 0;
248 if(pthread_mutex_init(&out->reqlenlock, 0))
250 out->maximum_request_length = 0;
255 void _xcb_out_destroy(_xcb_out *out)
257 pthread_cond_destroy(&out->cond);
258 pthread_mutex_destroy(&out->reqlenlock);
262 int _xcb_out_write(XCBConnection *c)
266 n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
268 n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len);
270 /* XXX: should "nothing was written" be considered failure or
271 * success for this function? it's not an I/O error, but... */
272 n = (n > 0) || (n < 0 && errno == EAGAIN);
277 for(i = 0; i < c->out.vec_len; ++i)
278 if(c->out.vec[i].iov_len)
286 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
288 while(count && c->out.queue_len + vector[0].iov_len < sizeof(c->out.queue))
290 memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
291 c->out.queue_len += vector[0].iov_len;
297 memmove(vector + 1, vector, count++ * sizeof(struct iovec));
298 vector[0].iov_base = c->out.queue;
299 vector[0].iov_len = c->out.queue_len;
300 c->out.queue_len = 0;
302 assert(!c->out.vec_len);
304 c->out.vec_len = count;
306 return _xcb_out_flush(c);
309 int _xcb_out_flush(XCBConnection *c)
312 while(ret && (c->out.queue_len || c->out.vec_len))
313 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
314 c->out.request_written = c->out.request;
315 pthread_cond_broadcast(&c->out.cond);