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. */
37 #include "extensions/bigreq.h"
39 static int force_sequence_wrap(XCBConnection *c)
42 if((c->out.request - c->in.request_read) > 65530)
44 pthread_mutex_unlock(&c->iolock);
46 pthread_mutex_lock(&c->iolock);
51 static int _xcb_write(const int fd, char (*buf)[], int *count)
53 int n = write(fd, *buf, *count);
58 memmove(*buf, *buf + n, *count);
63 static int _xcb_writev(const int fd, struct iovec *vec, int count)
65 int n = writev(fd, vec, count);
69 for(; count; --count, ++vec)
71 int cur = vec->iov_len;
75 vec->iov_base = (char *) vec->iov_base + cur;
85 /* Public interface */
87 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
89 pthread_mutex_lock(&c->out.reqlenlock);
90 if(!c->out.maximum_request_length)
92 const XCBQueryExtensionRep *ext;
93 c->out.maximum_request_length = c->setup->maximum_request_length;
94 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
95 if(ext && ext->present)
97 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
98 c->out.maximum_request_length = r->maximum_request_length;
102 pthread_mutex_unlock(&c->out.reqlenlock);
103 return c->out.maximum_request_length;
106 int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
110 struct iovec prefix[2];
113 enum workarounds workaround = WORKAROUND_NONE;
116 assert(request != 0);
118 assert(req->count > 0);
120 /* put together the length field, possibly using BIGREQUESTS */
121 for(i = 0; i < req->count; ++i)
122 longlen += XCB_CEIL(vector[i].iov_len) >> 2;
124 if(longlen > c->setup->maximum_request_length)
126 if(longlen > XCBGetMaximumRequestLength(c))
127 return 0; /* server can't take this; maybe need BIGREQUESTS? */
131 /* we don't need BIGREQUESTS. */
136 /* set the length field. */
138 prefix[i].iov_base = vector[0].iov_base;
139 prefix[i].iov_len = sizeof(CARD32);
140 vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
141 vector[0].iov_len -= sizeof(CARD32);
142 ((CARD16 *) prefix[i].iov_base)[1] = shortlen;
147 prefix[i].iov_base = &longlen;
148 prefix[i].iov_len = sizeof(CARD32);
152 /* set the major opcode, and the minor opcode for extensions */
155 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
156 /* TODO: better error handling here, please! */
157 assert(extension && extension->present);
158 ((CARD8 *) prefix[0].iov_base)[0] = extension->major_opcode;
159 ((CARD8 *) prefix[0].iov_base)[1] = req->opcode;
161 /* do we need to work around the X server bug described in glx.xml? */
162 if(strcmp(req->ext->name, "GLX") &&
163 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
165 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
168 ((CARD8 *) prefix[0].iov_base)[0] = req->opcode;
170 /* get a sequence number and arrange for delivery. */
171 pthread_mutex_lock(&c->iolock);
172 if(req->isvoid && !force_sequence_wrap(c))
174 pthread_mutex_unlock(&c->iolock);
178 *request = ++c->out.request;
181 _xcb_in_expect_reply(c, *request, workaround);
183 ret = _xcb_out_write_block(c, prefix, i);
185 ret = _xcb_out_write_block(c, vector, req->count);
186 pthread_mutex_unlock(&c->iolock);
191 int XCBFlush(XCBConnection *c)
194 pthread_mutex_lock(&c->iolock);
195 ret = _xcb_out_flush(c);
196 pthread_mutex_unlock(&c->iolock);
200 /* Private interface */
202 int _xcb_out_init(_xcb_out *out)
204 if(pthread_cond_init(&out->cond, 0))
213 out->request_written = 0;
215 if(pthread_mutex_init(&out->reqlenlock, 0))
217 out->maximum_request_length = 0;
222 void _xcb_out_destroy(_xcb_out *out)
224 pthread_cond_destroy(&out->cond);
225 pthread_mutex_destroy(&out->reqlenlock);
229 int _xcb_out_write(XCBConnection *c)
233 n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
235 n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len);
237 /* XXX: should "nothing was written" be considered failure or
238 * success for this function? it's not an I/O error, but... */
239 n = (n > 0) || (n < 0 && errno == EAGAIN);
244 for(i = 0; i < c->out.vec_len; ++i)
245 if(c->out.vec[i].iov_len)
252 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
254 static const char pad[3];
258 for(i = 0; i < count; ++i)
259 len += XCB_CEIL(vector[i].iov_len);
261 /* Is the queue about to overflow? */
262 if(c->out.queue_len + len < sizeof(c->out.queue))
264 /* No, this will fit. */
265 for(i = 0; i < count; ++i)
267 memcpy(c->out.queue + c->out.queue_len, vector[i].iov_base, vector[i].iov_len);
268 if(vector[i].iov_len & 3)
269 memset(c->out.queue + c->out.queue_len + vector[i].iov_len, 0, XCB_PAD(vector[i].iov_len));
270 c->out.queue_len += XCB_CEIL(vector[i].iov_len);
275 assert(!c->out.vec_len);
277 c->out.vec = malloc(sizeof(struct iovec) * (1 + count * 2));
282 c->out.vec[c->out.vec_len].iov_base = c->out.queue;
283 c->out.vec[c->out.vec_len++].iov_len = c->out.queue_len;
284 c->out.queue_len = 0;
286 for(i = 0; i < count; ++i)
288 if(!vector[i].iov_len)
290 c->out.vec[c->out.vec_len].iov_base = vector[i].iov_base;
291 c->out.vec[c->out.vec_len++].iov_len = vector[i].iov_len;
292 if(!XCB_PAD(vector[i].iov_len))
294 c->out.vec[c->out.vec_len].iov_base = (void *) pad;
295 c->out.vec[c->out.vec_len++].iov_len = XCB_PAD(vector[i].iov_len);
297 if(!_xcb_out_flush(c))
305 int _xcb_out_flush(XCBConnection *c)
308 c->out.request_written = c->out.request;
309 while(ret && (c->out.queue_len || c->out.vec_len))
310 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
311 pthread_cond_broadcast(&c->out.cond);