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 write_block(XCBConnection *c, struct iovec *vector, int count)
40 while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
42 memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
43 c->out.queue_len += vector[0].iov_len;
44 vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
45 vector[0].iov_len = 0;
52 vector[0].iov_base = c->out.queue;
53 vector[0].iov_len = c->out.queue_len;
55 return _xcb_out_send(c, &vector, &count);
58 /* Public interface */
60 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
62 pthread_mutex_lock(&c->out.reqlenlock);
63 if(!c->out.maximum_request_length)
65 const XCBQueryExtensionRep *ext;
66 c->out.maximum_request_length = c->setup->maximum_request_length;
67 ext = XCBGetExtensionData(c, &XCBBigRequestsId);
68 if(ext && ext->present)
70 XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
71 c->out.maximum_request_length = r->maximum_request_length;
75 pthread_mutex_unlock(&c->out.reqlenlock);
76 return c->out.maximum_request_length;
79 unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
88 } sync = { { /* GetInputFocus */ 43, 0, 1 } };
90 CARD32 prefix[3] = { 0 };
91 int veclen = req->count;
92 enum workarounds workaround = WORKAROUND_NONE;
96 assert(req->count > 0);
98 if(!(flags & XCB_REQUEST_RAW))
100 static const char pad[3];
104 assert(vector[0].iov_len >= 4);
105 /* set the major opcode, and the minor opcode for extensions */
108 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
109 if(!(extension && extension->present))
111 ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
112 ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
115 ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
117 /* put together the length field, possibly using BIGREQUESTS */
118 for(i = 0; i < req->count; ++i)
120 longlen += vector[i].iov_len;
121 if(!vector[i].iov_base)
123 vector[i].iov_base = (char *) pad;
124 assert(vector[i].iov_len <= sizeof(pad));
127 assert((longlen & 3) == 0);
130 if(longlen <= c->setup->maximum_request_length)
132 /* we don't need BIGREQUESTS. */
136 else if(longlen > XCBGetMaximumRequestLength(c))
137 return 0; /* server can't take this; maybe need BIGREQUESTS? */
139 /* set the length field. */
140 ((CARD16 *) vector[0].iov_base)[1] = shortlen;
142 prefix[2] = ++longlen;
144 flags &= ~XCB_REQUEST_RAW;
146 /* do we need to work around the X server bug described in glx.xml? */
147 /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
148 * configuration, but that should be handled here anyway. */
149 if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
150 ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[1] == 0x10004) ||
152 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
154 /* get a sequence number and arrange for delivery. */
155 pthread_mutex_lock(&c->iolock);
156 /* wait for other writing threads to get out of my way. */
157 while(c->out.writing)
158 pthread_cond_wait(&c->out.cond, &c->iolock);
160 request = ++c->out.request;
161 /* send GetInputFocus (sync) when 64k-2 requests have been sent without
163 * Also send sync (could use NoOp) at 32-bit wrap to avoid having
164 * applications see sequence 0 as that is used to indicate
165 * an error in sending the request */
167 c->out.request == c->in.request_expected + (1 << 16) - 2) ||
170 prefix[0] = sync.packet;
171 _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
172 c->in.request_expected = c->out.request;
173 request = ++c->out.request;
176 if(workaround != WORKAROUND_NONE || flags != 0)
177 _xcb_in_expect_reply(c, request, workaround, flags);
179 c->in.request_expected = c->out.request;
181 if(prefix[0] || prefix[2])
186 prefix[1] = ((CARD32 *) vector[1].iov_base)[0];
187 vector[1].iov_base = (CARD32 *) vector[1].iov_base + 1;
188 vector[1].iov_len -= sizeof(CARD32);
190 vector[0].iov_len = sizeof(CARD32) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
191 vector[0].iov_base = prefix + !prefix[0];
194 if(!write_block(c, vector, veclen))
196 pthread_mutex_unlock(&c->iolock);
200 int XCBFlush(XCBConnection *c)
203 pthread_mutex_lock(&c->iolock);
204 ret = _xcb_out_flush_to(c, c->out.request);
205 pthread_mutex_unlock(&c->iolock);
209 /* Private interface */
211 int _xcb_out_init(_xcb_out *out)
213 if(pthread_cond_init(&out->cond, 0))
220 out->request_written = 0;
222 if(pthread_mutex_init(&out->reqlenlock, 0))
224 out->maximum_request_length = 0;
229 void _xcb_out_destroy(_xcb_out *out)
231 pthread_cond_destroy(&out->cond);
232 pthread_mutex_destroy(&out->reqlenlock);
235 int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
239 ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
240 c->out.request_written = c->out.request;
241 pthread_cond_broadcast(&c->out.cond);
245 int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
247 assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
248 if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
252 struct iovec vec, *vec_ptr = &vec;
254 vec.iov_base = c->out.queue;
255 vec.iov_len = c->out.queue_len;
256 c->out.queue_len = 0;
257 return _xcb_out_send(c, &vec_ptr, &count);
259 while(c->out.writing)
260 pthread_cond_wait(&c->out.cond, &c->iolock);
261 assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));