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. */
36 #include "extensions/bigreq.h"
38 static int force_sequence_wrap(XCBConnection *c)
41 if((c->out.request - c->in.request_read) > 65530)
43 pthread_mutex_unlock(&c->iolock);
45 pthread_mutex_lock(&c->iolock);
50 /* Public interface */
52 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
54 pthread_mutex_lock(&c->out.reqlenlock);
55 if(!c->out.maximum_request_length)
57 const XCBQueryExtensionRep *ext;
58 c->out.maximum_request_length = c->setup->maximum_request_length;
59 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
60 if(ext && ext->present)
62 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
63 c->out.maximum_request_length = r->maximum_request_length;
67 pthread_mutex_unlock(&c->out.reqlenlock);
68 return c->out.maximum_request_length;
71 int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
75 struct iovec prefix[2];
78 enum workarounds workaround = WORKAROUND_NONE;
83 assert(req->count > 0);
85 /* put together the length field, possibly using BIGREQUESTS */
86 for(i = 0; i < req->count; ++i)
87 longlen += XCB_CEIL(vector[i].iov_len) >> 2;
89 if(longlen > c->setup->maximum_request_length)
91 if(longlen > XCBGetMaximumRequestLength(c))
92 return 0; /* server can't take this; maybe need BIGREQUESTS? */
96 /* we don't need BIGREQUESTS. */
101 /* set the length field. */
103 prefix[i].iov_base = vector[0].iov_base;
104 prefix[i].iov_len = sizeof(CARD32);
105 vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
106 vector[0].iov_len -= sizeof(CARD32);
107 ((CARD16 *) prefix[i].iov_base)[1] = shortlen;
112 prefix[i].iov_base = &longlen;
113 prefix[i].iov_len = sizeof(CARD32);
117 /* set the major opcode, and the minor opcode for extensions */
120 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
121 /* TODO: better error handling here, please! */
122 assert(extension && extension->present);
123 ((CARD8 *) prefix[0].iov_base)[0] = extension->major_opcode;
124 ((CARD8 *) prefix[0].iov_base)[1] = req->opcode;
126 /* do we need to work around the X server bug described in glx.xml? */
127 if(strcmp(req->ext->name, "GLX") &&
128 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
130 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
133 ((CARD8 *) prefix[0].iov_base)[0] = req->opcode;
135 /* get a sequence number and arrange for delivery. */
136 pthread_mutex_lock(&c->iolock);
137 if(req->isvoid && !force_sequence_wrap(c))
139 pthread_mutex_unlock(&c->iolock);
143 *request = ++c->out.request;
146 _xcb_in_expect_reply(c, *request, workaround);
148 ret = _xcb_out_write_block(c, prefix, i);
150 ret = _xcb_out_write_block(c, vector, req->count);
151 pthread_mutex_unlock(&c->iolock);
156 int XCBFlush(XCBConnection *c)
159 pthread_mutex_lock(&c->iolock);
160 ret = _xcb_out_flush(c);
161 pthread_mutex_unlock(&c->iolock);
165 /* Private interface */
167 int _xcb_out_init(_xcb_out *out)
169 if(pthread_cond_init(&out->cond, 0))
177 out->last_request = 0;
179 out->request_written = 0;
181 if(pthread_mutex_init(&out->reqlenlock, 0))
183 out->maximum_request_length = 0;
188 void _xcb_out_destroy(_xcb_out *out)
190 pthread_cond_destroy(&out->cond);
191 pthread_mutex_destroy(&out->reqlenlock);
195 int _xcb_out_write(XCBConnection *c)
199 n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
201 n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len);
203 if(n < 0 && errno == EAGAIN)
209 for(i = 0; i < c->out.vec_len; ++i)
210 if(c->out.vec[i].iov_len)
217 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
219 static const char pad[3];
223 for(i = 0; i < count; ++i)
224 len += XCB_CEIL(vector[i].iov_len);
226 /* Is the queue about to overflow? */
227 if(c->out.queue_len + len < sizeof(c->out.queue))
229 /* No, this will fit. */
230 for(i = 0; i < count; ++i)
232 memcpy(c->out.queue + c->out.queue_len, vector[i].iov_base, vector[i].iov_len);
233 if(vector[i].iov_len & 3)
234 memset(c->out.queue + c->out.queue_len + vector[i].iov_len, 0, XCB_PAD(vector[i].iov_len));
235 c->out.queue_len += XCB_CEIL(vector[i].iov_len);
240 assert(!c->out.vec_len);
242 c->out.vec = malloc(sizeof(struct iovec) * (1 + count * 2));
247 c->out.vec[c->out.vec_len].iov_base = c->out.queue;
248 c->out.vec[c->out.vec_len++].iov_len = c->out.queue_len;
249 c->out.queue_len = 0;
251 for(i = 0; i < count; ++i)
253 if(!vector[i].iov_len)
255 c->out.vec[c->out.vec_len].iov_base = vector[i].iov_base;
256 c->out.vec[c->out.vec_len++].iov_len = vector[i].iov_len;
257 if(!XCB_PAD(vector[i].iov_len))
259 c->out.vec[c->out.vec_len].iov_base = (void *) pad;
260 c->out.vec[c->out.vec_len++].iov_len = XCB_PAD(vector[i].iov_len);
262 if(_xcb_out_flush(c) <= 0)
270 int _xcb_out_flush(XCBConnection *c)
273 while(ret > 0 && (c->out.queue_len || c->out.vec_len))
274 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
275 c->out.last_request = 0;
276 c->out.request_written = c->out.request;
277 pthread_cond_broadcast(&c->out.cond);