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 unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
76 int veclen = req->count;
77 enum workarounds workaround = WORKAROUND_NONE;
81 assert(req->count > 0);
83 if(!(flags & XCB_REQUEST_RAW))
85 static const char pad[3];
89 assert(vector[0].iov_len >= 4);
90 /* set the major opcode, and the minor opcode for extensions */
93 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
94 if(!(extension && extension->present))
96 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
97 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
100 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
102 /* put together the length field, possibly using BIGREQUESTS */
103 for(i = 0; i < req->count; ++i)
105 longlen += vector[i].iov_len;
106 if(!vector[i].iov_base)
108 vector[i].iov_base = (caddr_t) pad;
109 assert(vector[i].iov_len <= sizeof(pad));
112 assert((longlen & 3) == 0);
115 if(longlen <= c->setup->maximum_request_length)
117 /* we don't need BIGREQUESTS. */
121 else if(longlen > XCBGetMaximumRequestLength(c))
122 return 0; /* server can't take this; maybe need BIGREQUESTS? */
124 /* set the length field. */
125 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
129 vector[0].iov_base = prefix;
130 vector[0].iov_len = sizeof(prefix);
131 prefix[0] = ((CARD32 *) vector[0].iov_base)[0];
132 prefix[1] = ++longlen;
133 vector[1].iov_base = ((char *) vector[1].iov_base) + sizeof(CARD32);
134 vector[1].iov_len -= sizeof(CARD32);
137 flags &= ~XCB_REQUEST_RAW;
139 /* do we need to work around the X server bug described in glx.xml? */
140 if(req->ext && !req->isvoid && strcmp(req->ext->name, "GLX") &&
141 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
143 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
145 /* get a sequence number and arrange for delivery. */
146 pthread_mutex_lock(&c->iolock);
147 if(req->isvoid && !force_sequence_wrap(c))
149 pthread_mutex_unlock(&c->iolock);
153 /* wait for other writing threads to get out of my way. */
154 while(c->out.writing)
155 pthread_cond_wait(&c->out.cond, &c->iolock);
157 request = ++c->out.request;
158 assert(request != 0);
160 _xcb_in_expect_reply(c, request, workaround, flags);
162 if(!_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;
240 vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
241 vector[0].iov_len = 0;
248 vector[0].iov_base = c->out.queue;
249 vector[0].iov_len = c->out.queue_len;
250 c->out.queue_len = 0;
252 c->out.vec_len = count;
254 return _xcb_out_flush(c);
257 int _xcb_out_flush(XCBConnection *c)
263 assert(!c->out.vec && !c->out.vec_len);
264 vec.iov_base = c->out.queue;
265 vec.iov_len = c->out.queue_len;
268 c->out.queue_len = 0;
270 while(ret && c->out.vec_len)
271 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
272 c->out.request_written = c->out.request;
273 pthread_cond_broadcast(&c->out.cond);