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 /* Public interface */
53 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
55 pthread_mutex_lock(&c->out.reqlenlock);
56 if(!c->out.maximum_request_length)
58 const XCBQueryExtensionRep *ext;
59 c->out.maximum_request_length = c->setup->maximum_request_length;
60 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
61 if(ext && ext->present)
63 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
64 c->out.maximum_request_length = r->maximum_request_length;
68 pthread_mutex_unlock(&c->out.reqlenlock);
69 return c->out.maximum_request_length;
72 int XCBSendRequest(XCBConnection *c, unsigned int *request, int flags, struct iovec *vector, const XCBProtocolRequest *req)
76 int veclen = req->count;
77 enum workarounds workaround = WORKAROUND_NONE;
82 assert(req->count > 0);
84 if(!(flags & XCB_REQUEST_RAW))
86 static const char pad[3];
90 assert(vector[0].iov_len >= 4);
91 /* set the major opcode, and the minor opcode for extensions */
94 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
95 /* TODO: better error handling here, please! */
96 assert(extension && extension->present);
97 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
98 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
101 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
103 /* put together the length field, possibly using BIGREQUESTS */
104 for(i = 0; i < req->count; ++i)
106 longlen += vector[i].iov_len;
107 if(!vector[i].iov_base)
109 vector[i].iov_base = (caddr_t) pad;
110 assert(vector[i].iov_len <= sizeof(pad));
113 assert((longlen & 3) == 0);
116 if(longlen <= c->setup->maximum_request_length)
118 /* we don't need BIGREQUESTS. */
122 else if(longlen > XCBGetMaximumRequestLength(c))
123 return 0; /* server can't take this; maybe need BIGREQUESTS? */
125 /* set the length field. */
126 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
129 memmove(vector + 1, vector, veclen++ * sizeof(*vector));
131 vector[0].iov_base = prefix;
132 vector[0].iov_len = sizeof(prefix);
133 prefix[0] = ((CARD32 *) vector[0].iov_base)[0];
134 prefix[1] = ++longlen;
135 vector[1].iov_base = ((char *) vector[1].iov_base) + sizeof(CARD32);
136 vector[1].iov_len -= sizeof(CARD32);
139 flags &= ~XCB_REQUEST_RAW;
141 /* do we need to work around the X server bug described in glx.xml? */
142 if(req->ext && !req->isvoid && strcmp(req->ext->name, "GLX") &&
143 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
145 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
147 /* get a sequence number and arrange for delivery. */
148 pthread_mutex_lock(&c->iolock);
149 if(req->isvoid && !force_sequence_wrap(c))
151 pthread_mutex_unlock(&c->iolock);
155 /* wait for other writing threads to get out of my way. */
156 while(c->out.writing)
157 pthread_cond_wait(&c->out.cond, &c->iolock);
159 *request = ++c->out.request;
161 _xcb_in_expect_reply(c, *request, workaround, flags);
163 ret = _xcb_out_write_block(c, vector, veclen);
164 pthread_mutex_unlock(&c->iolock);
168 int XCBFlush(XCBConnection *c)
171 pthread_mutex_lock(&c->iolock);
172 ret = _xcb_out_flush(c);
173 pthread_mutex_unlock(&c->iolock);
177 /* Private interface */
179 int _xcb_out_init(_xcb_out *out)
181 if(pthread_cond_init(&out->cond, 0))
190 out->request_written = 0;
192 if(pthread_mutex_init(&out->reqlenlock, 0))
194 out->maximum_request_length = 0;
199 void _xcb_out_destroy(_xcb_out *out)
201 pthread_cond_destroy(&out->cond);
202 pthread_mutex_destroy(&out->reqlenlock);
205 /* precondition: there must be something for us to write. */
206 int _xcb_out_write(XCBConnection *c)
209 assert(!c->out.queue_len);
210 n = writev(c->fd, c->out.vec, c->out.vec_len);
211 if(n < 0 && errno == EAGAIN)
216 for(; c->out.vec_len; --c->out.vec_len, ++c->out.vec)
218 int cur = c->out.vec->iov_len;
221 c->out.vec->iov_len -= cur;
222 c->out.vec->iov_base = (char *) c->out.vec->iov_base + cur;
224 if(c->out.vec->iov_len)
233 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
235 assert(!c->out.vec && !c->out.vec_len);
236 while(count && c->out.queue_len + vector[0].iov_len < sizeof(c->out.queue))
238 memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
239 c->out.queue_len += vector[0].iov_len;
245 memmove(vector + 1, vector, count++ * sizeof(struct iovec));
246 vector[0].iov_base = c->out.queue;
247 vector[0].iov_len = c->out.queue_len;
248 c->out.queue_len = 0;
250 c->out.vec_len = count;
252 return _xcb_out_flush(c);
255 int _xcb_out_flush(XCBConnection *c)
261 assert(!c->out.vec && !c->out.vec_len);
262 vec.iov_base = c->out.queue;
263 vec.iov_len = c->out.queue_len;
266 c->out.queue_len = 0;
268 while(ret && c->out.vec_len)
269 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
270 c->out.request_written = c->out.request;
271 pthread_cond_broadcast(&c->out.cond);