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 /* Public interface */
41 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
43 pthread_mutex_lock(&c->out.reqlenlock);
44 if(!c->out.maximum_request_length)
46 const XCBQueryExtensionRep *ext;
47 c->out.maximum_request_length = c->setup->maximum_request_length;
48 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
49 if(ext && ext->present)
51 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
52 c->out.maximum_request_length = r->maximum_request_length;
56 pthread_mutex_unlock(&c->out.reqlenlock);
57 return c->out.maximum_request_length;
60 unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
69 } sync = { { /* GetInputFocus */ 43, 0, 1 } };
71 CARD32 prefix[3] = { 0 };
72 int veclen = req->count;
73 enum workarounds workaround = WORKAROUND_NONE;
77 assert(req->count > 0);
79 if(!(flags & XCB_REQUEST_RAW))
81 static const char pad[3];
85 assert(vector[0].iov_len >= 4);
86 /* set the major opcode, and the minor opcode for extensions */
89 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
90 if(!(extension && extension->present))
92 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
93 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
96 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
98 /* put together the length field, possibly using BIGREQUESTS */
99 for(i = 0; i < req->count; ++i)
101 longlen += vector[i].iov_len;
102 if(!vector[i].iov_base)
104 vector[i].iov_base = (caddr_t) pad;
105 assert(vector[i].iov_len <= sizeof(pad));
108 assert((longlen & 3) == 0);
111 if(longlen <= c->setup->maximum_request_length)
113 /* we don't need BIGREQUESTS. */
117 else if(longlen > XCBGetMaximumRequestLength(c))
118 return 0; /* server can't take this; maybe need BIGREQUESTS? */
120 /* set the length field. */
121 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
123 prefix[2] = ++longlen;
125 flags &= ~XCB_REQUEST_RAW;
127 /* do we need to work around the X server bug described in glx.xml? */
128 if(req->ext && !req->isvoid && strcmp(req->ext->name, "GLX") &&
129 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
131 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
133 /* get a sequence number and arrange for delivery. */
134 pthread_mutex_lock(&c->iolock);
135 /* wait for other writing threads to get out of my way. */
136 while(c->out.writing)
137 pthread_cond_wait(&c->out.cond, &c->iolock);
139 if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
141 prefix[0] = sync.packet;
142 request = ++c->out.request;
143 _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
144 c->in.request_expected = c->out.request;
147 request = ++c->out.request;
148 assert(request != 0);
149 _xcb_in_expect_reply(c, request, workaround, flags);
151 c->in.request_expected = c->out.request;
153 if(prefix[0] || prefix[2])
158 prefix[1] = ((CARD32 *) vector[1].iov_base)[0];
159 vector[1].iov_base = (CARD32 *) vector[1].iov_base + 1;
160 vector[1].iov_len -= sizeof(CARD32);
162 vector[0].iov_len = sizeof(CARD32) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
163 vector[0].iov_base = prefix + !prefix[0];
166 if(!_xcb_out_write_block(c, vector, veclen))
168 pthread_mutex_unlock(&c->iolock);
172 int XCBFlush(XCBConnection *c)
175 pthread_mutex_lock(&c->iolock);
176 ret = _xcb_out_flush(c);
177 pthread_mutex_unlock(&c->iolock);
181 /* Private interface */
183 int _xcb_out_init(_xcb_out *out)
185 if(pthread_cond_init(&out->cond, 0))
194 out->request_written = 0;
196 if(pthread_mutex_init(&out->reqlenlock, 0))
198 out->maximum_request_length = 0;
203 void _xcb_out_destroy(_xcb_out *out)
205 pthread_cond_destroy(&out->cond);
206 pthread_mutex_destroy(&out->reqlenlock);
209 /* precondition: there must be something for us to write. */
210 int _xcb_out_write(XCBConnection *c, struct iovec **vector, int *count)
213 assert(!c->out.queue_len);
214 n = writev(c->fd, *vector, *count);
215 if(n < 0 && errno == EAGAIN)
220 for(; *count; --*count, ++*vector)
222 int cur = (*vector)->iov_len;
225 (*vector)->iov_len -= cur;
226 (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
228 if((*vector)->iov_len)
237 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
239 assert(!c->out.vec && !c->out.vec_len);
240 while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
242 memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
243 c->out.queue_len += vector[0].iov_len;
244 vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
245 vector[0].iov_len = 0;
252 vector[0].iov_base = c->out.queue;
253 vector[0].iov_len = c->out.queue_len;
254 c->out.queue_len = 0;
256 c->out.vec_len = count;
258 return _xcb_out_flush(c);
261 int _xcb_out_flush(XCBConnection *c)
267 assert(!c->out.vec && !c->out.vec_len);
268 vec.iov_base = c->out.queue;
269 vec.iov_len = c->out.queue_len;
272 c->out.queue_len = 0;
274 while(ret && c->out.vec_len)
275 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
276 c->out.request_written = c->out.request;
277 pthread_cond_broadcast(&c->out.cond);