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 write_block(XCBConnection *c, struct iovec *vector, int count)
41 while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
43 memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
44 c->out.queue_len += vector[0].iov_len;
45 vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
46 vector[0].iov_len = 0;
53 vector[0].iov_base = c->out.queue;
54 vector[0].iov_len = c->out.queue_len;
56 return _xcb_out_send(c, &vector, &count);
59 /* Public interface */
61 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
63 pthread_mutex_lock(&c->out.reqlenlock);
64 if(!c->out.maximum_request_length)
66 const XCBQueryExtensionRep *ext;
67 c->out.maximum_request_length = c->setup->maximum_request_length;
68 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
69 if(ext && ext->present)
71 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
72 c->out.maximum_request_length = r->maximum_request_length;
76 pthread_mutex_unlock(&c->out.reqlenlock);
77 return c->out.maximum_request_length;
80 unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
89 } sync = { { /* GetInputFocus */ 43, 0, 1 } };
91 CARD32 prefix[3] = { 0 };
92 int veclen = req->count;
93 enum workarounds workaround = WORKAROUND_NONE;
97 assert(req->count > 0);
99 if(!(flags & XCB_REQUEST_RAW))
101 static const char pad[3];
105 assert(vector[0].iov_len >= 4);
106 /* set the major opcode, and the minor opcode for extensions */
109 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
110 if(!(extension && extension->present))
112 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
113 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
116 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
118 /* put together the length field, possibly using BIGREQUESTS */
119 for(i = 0; i < req->count; ++i)
121 longlen += vector[i].iov_len;
122 if(!vector[i].iov_base)
124 vector[i].iov_base = (caddr_t) pad;
125 assert(vector[i].iov_len <= sizeof(pad));
128 assert((longlen & 3) == 0);
131 if(longlen <= c->setup->maximum_request_length)
133 /* we don't need BIGREQUESTS. */
137 else if(longlen > XCBGetMaximumRequestLength(c))
138 return 0; /* server can't take this; maybe need BIGREQUESTS? */
140 /* set the length field. */
141 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
143 prefix[2] = ++longlen;
145 flags &= ~XCB_REQUEST_RAW;
147 /* do we need to work around the X server bug described in glx.xml? */
148 if(req->ext && !req->isvoid && strcmp(req->ext->name, "GLX") &&
149 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
151 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
153 /* get a sequence number and arrange for delivery. */
154 pthread_mutex_lock(&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 if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
161 prefix[0] = sync.packet;
162 request = ++c->out.request;
163 _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
164 c->in.request_expected = c->out.request;
167 request = ++c->out.request;
168 assert(request != 0);
169 _xcb_in_expect_reply(c, request, workaround, flags);
171 c->in.request_expected = c->out.request;
173 if(prefix[0] || prefix[2])
178 prefix[1] = ((CARD32 *) vector[1].iov_base)[0];
179 vector[1].iov_base = (CARD32 *) vector[1].iov_base + 1;
180 vector[1].iov_len -= sizeof(CARD32);
182 vector[0].iov_len = sizeof(CARD32) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
183 vector[0].iov_base = prefix + !prefix[0];
186 if(!write_block(c, vector, veclen))
188 pthread_mutex_unlock(&c->iolock);
192 int XCBFlush(XCBConnection *c)
195 pthread_mutex_lock(&c->iolock);
196 ret = _xcb_out_flush_to(c, c->out.request);
197 pthread_mutex_unlock(&c->iolock);
201 /* Private interface */
203 int _xcb_out_init(_xcb_out *out)
205 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, struct iovec **vector, int *count)
231 assert(!c->out.queue_len);
232 n = writev(c->fd, *vector, *count);
233 if(n < 0 && errno == EAGAIN)
238 for(; *count; --*count, ++*vector)
240 int cur = (*vector)->iov_len;
243 (*vector)->iov_len -= cur;
244 (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
246 if((*vector)->iov_len)
255 int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
259 ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
260 c->out.request_written = c->out.request;
261 pthread_cond_broadcast(&c->out.cond);
265 int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
267 assert(request <= c->out.request);
268 if(c->out.request_written >= request)
272 struct iovec vec, *vec_ptr = &vec;
274 vec.iov_base = c->out.queue;
275 vec.iov_len = c->out.queue_len;
276 c->out.queue_len = 0;
277 return _xcb_out_send(c, &vec_ptr, &count);
279 while(c->out.writing)
280 pthread_cond_wait(&c->out.cond, &c->iolock);
281 assert(c->out.request_written >= request);