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(xcb_connection_t *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 uint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
64 pthread_mutex_lock(&c->out.reqlenlock);
65 if(!c->out.maximum_request_length)
67 const xcb_query_extension_reply_t *ext;
68 c->out.maximum_request_length = c->setup->maximum_request_length;
69 ext = xcb_get_extension_data(c, &xcb_big_requests_id);
70 if(ext && ext->present)
72 xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, xcb_big_requests_enable(c), 0);
73 c->out.maximum_request_length = r->maximum_request_length;
77 pthread_mutex_unlock(&c->out.reqlenlock);
78 return c->out.maximum_request_length;
81 unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
90 } sync = { { /* GetInputFocus */ 43, 0, 1 } };
92 uint32_t prefix[3] = { 0 };
93 int veclen = req->count;
94 enum workarounds workaround = WORKAROUND_NONE;
101 assert(req->count > 0);
103 if(!(flags & XCB_REQUEST_RAW))
105 static const char pad[3];
107 uint16_t shortlen = 0;
109 assert(vector[0].iov_len >= 4);
110 /* set the major opcode, and the minor opcode for extensions */
113 const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
114 if(!(extension && extension->present))
116 _xcb_conn_shutdown(c);
119 ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
120 ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
123 ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
125 /* put together the length field, possibly using BIGREQUESTS */
126 for(i = 0; i < req->count; ++i)
128 longlen += vector[i].iov_len;
129 if(!vector[i].iov_base)
131 vector[i].iov_base = (char *) pad;
132 assert(vector[i].iov_len <= sizeof(pad));
135 assert((longlen & 3) == 0);
138 if(longlen <= c->setup->maximum_request_length)
140 /* we don't need BIGREQUESTS. */
144 else if(longlen > xcb_get_maximum_request_length(c))
146 _xcb_conn_shutdown(c);
147 return 0; /* server can't take this; maybe need BIGREQUESTS? */
150 /* set the length field. */
151 ((uint16_t *) vector[0].iov_base)[1] = shortlen;
153 prefix[2] = ++longlen;
155 flags &= ~XCB_REQUEST_RAW;
157 /* do we need to work around the X server bug described in glx.xml? */
158 /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
159 * configuration, but that should be handled here anyway. */
160 if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
161 ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) ||
163 workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
165 /* get a sequence number and arrange for delivery. */
166 pthread_mutex_lock(&c->iolock);
167 /* wait for other writing threads to get out of my way. */
168 while(c->out.writing)
169 pthread_cond_wait(&c->out.cond, &c->iolock);
171 request = ++c->out.request;
172 /* send GetInputFocus (sync) when 64k-2 requests have been sent without
174 * Also send sync (could use NoOp) at 32-bit wrap to avoid having
175 * applications see sequence 0 as that is used to indicate
176 * an error in sending the request */
177 while((req->isvoid &&
178 c->out.request == c->in.request_expected + (1 << 16) - 1) ||
181 prefix[0] = sync.packet;
182 _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
183 c->in.request_expected = c->out.request;
184 request = ++c->out.request;
187 if(workaround != WORKAROUND_NONE || flags != 0)
188 _xcb_in_expect_reply(c, request, workaround, flags);
190 c->in.request_expected = c->out.request;
192 if(prefix[0] || prefix[2])
197 prefix[1] = ((uint32_t *) vector[1].iov_base)[0];
198 vector[1].iov_base = (uint32_t *) vector[1].iov_base + 1;
199 vector[1].iov_len -= sizeof(uint32_t);
201 vector[0].iov_len = sizeof(uint32_t) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
202 vector[0].iov_base = prefix + !prefix[0];
205 if(!write_block(c, vector, veclen))
207 _xcb_conn_shutdown(c);
210 pthread_mutex_unlock(&c->iolock);
214 int xcb_flush(xcb_connection_t *c)
219 pthread_mutex_lock(&c->iolock);
220 ret = _xcb_out_flush_to(c, c->out.request);
221 pthread_mutex_unlock(&c->iolock);
225 /* Private interface */
227 int _xcb_out_init(_xcb_out *out)
229 if(pthread_cond_init(&out->cond, 0))
236 out->request_written = 0;
238 if(pthread_mutex_init(&out->reqlenlock, 0))
240 out->maximum_request_length = 0;
245 void _xcb_out_destroy(_xcb_out *out)
247 pthread_cond_destroy(&out->cond);
248 pthread_mutex_destroy(&out->reqlenlock);
251 int _xcb_out_send(xcb_connection_t *c, struct iovec **vector, int *count)
255 ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
256 c->out.request_written = c->out.request;
257 pthread_cond_broadcast(&c->out.cond);
261 int _xcb_out_flush_to(xcb_connection_t *c, unsigned int request)
263 assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
264 if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
268 struct iovec vec, *vec_ptr = &vec;
270 vec.iov_base = c->out.queue;
271 vec.iov_len = c->out.queue_len;
272 c->out.queue_len = 0;
273 return _xcb_out_send(c, &vec_ptr, &count);
275 while(c->out.writing)
276 pthread_cond_wait(&c->out.cond, &c->iolock);
277 assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));