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 /* Public interface */
63 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
65 pthread_mutex_lock(&c->out.reqlenlock);
66 if(!c->out.maximum_request_length)
68 const XCBQueryExtensionRep *ext;
69 c->out.maximum_request_length = c->setup->maximum_request_length;
70 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
71 if(ext && ext->present)
73 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
74 c->out.maximum_request_length = r->maximum_request_length;
78 pthread_mutex_unlock(&c->out.reqlenlock);
79 return c->out.maximum_request_length;
82 int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
84 static const char pad[3];
91 enum workarounds workaround = WORKAROUND_NONE;
96 assert(req->count > 0);
98 /* set the major opcode, and the minor opcode for extensions */
101 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
102 /* TODO: better error handling here, please! */
103 assert(extension && extension->present);
104 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
105 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
107 /* do we need to work around the X server bug described in glx.xml? */
108 if(strcmp(req->ext->name, "GLX") &&
109 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
111 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
114 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
116 /* put together the length field, possibly using BIGREQUESTS */
117 for(i = 0; i < req->count; ++i)
118 longlen += (vector[i].iov_len + 3) >> 2;
120 if(longlen > c->setup->maximum_request_length)
122 if(longlen > XCBGetMaximumRequestLength(c))
123 return 0; /* server can't take this; maybe need BIGREQUESTS? */
127 /* we don't need BIGREQUESTS. */
138 ((req->count * 2 + 3) * sizeof(struct iovec));
139 /* set the length field. */
140 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
143 padded[0].iov_base = vector[0].iov_base;
144 padded[0].iov_len = sizeof(CARD32);
145 vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
146 vector[0].iov_len -= sizeof(CARD32);
148 padded[1].iov_base = &longlen;
149 padded[1].iov_len = sizeof(CARD32);
153 for(i = 0; i < req->count; ++i)
155 if(!vector[i].iov_len)
157 padded[padlen].iov_base = vector[i].iov_base;
158 padded[padlen++].iov_len = vector[i].iov_len;
159 if(!XCB_PAD(vector[i].iov_len))
161 padded[padlen].iov_base = (caddr_t) pad;
162 padded[padlen++].iov_len = XCB_PAD(vector[i].iov_len);
165 /* get a sequence number and arrange for delivery. */
166 pthread_mutex_lock(&c->iolock);
167 if(req->isvoid && !force_sequence_wrap(c))
169 pthread_mutex_unlock(&c->iolock);
176 *request = ++c->out.request;
179 _xcb_in_expect_reply(c, *request, workaround);
181 ret = _xcb_out_write_block(c, padded, padlen);
182 pthread_mutex_unlock(&c->iolock);
190 int XCBFlush(XCBConnection *c)
193 pthread_mutex_lock(&c->iolock);
194 ret = _xcb_out_flush(c);
195 pthread_mutex_unlock(&c->iolock);
199 /* Private interface */
201 int _xcb_out_init(_xcb_out *out)
203 if(pthread_cond_init(&out->cond, 0))
212 out->request_written = 0;
214 if(pthread_mutex_init(&out->reqlenlock, 0))
216 out->maximum_request_length = 0;
221 void _xcb_out_destroy(_xcb_out *out)
223 pthread_cond_destroy(&out->cond);
224 pthread_mutex_destroy(&out->reqlenlock);
227 /* precondition: there must be something for us to write. */
228 int _xcb_out_write(XCBConnection *c)
231 assert(!c->out.queue_len);
232 n = writev(c->fd, c->out.vec, c->out.vec_len);
233 if(n < 0 && errno == EAGAIN)
238 for(; c->out.vec_len; --c->out.vec_len, ++c->out.vec)
240 int cur = c->out.vec->iov_len;
243 c->out.vec->iov_len -= cur;
244 c->out.vec->iov_base = (char *) c->out.vec->iov_base + cur;
246 if(c->out.vec->iov_len)
255 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
257 while(c->out.writing)
258 pthread_cond_wait(&c->out.cond, &c->iolock);
259 assert(!c->out.vec && !c->out.vec_len);
260 while(count && c->out.queue_len + vector[0].iov_len < sizeof(c->out.queue))
262 memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
263 c->out.queue_len += vector[0].iov_len;
269 memmove(vector + 1, vector, count++ * sizeof(struct iovec));
270 vector[0].iov_base = c->out.queue;
271 vector[0].iov_len = c->out.queue_len;
272 c->out.queue_len = 0;
274 c->out.vec_len = count;
276 return _xcb_out_flush(c);
279 int _xcb_out_flush(XCBConnection *c)
285 assert(!c->out.vec && !c->out.vec_len);
286 vec.iov_base = c->out.queue;
287 vec.iov_len = c->out.queue_len;
290 c->out.queue_len = 0;
292 while(ret && c->out.vec_len)
293 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
294 c->out.request_written = c->out.request;
295 pthread_cond_broadcast(&c->out.cond);