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];
82 assert(req->count > 0);
84 /* put together the length field, possibly using BIGREQUESTS */
85 for(i = 0; i < req->count; ++i)
86 longlen += XCB_CEIL(vector[i].iov_len) >> 2;
88 if(longlen > c->setup->maximum_request_length)
90 if(longlen > XCBGetMaximumRequestLength(c))
91 return 0; /* server can't take this; maybe need BIGREQUESTS? */
95 /* we don't need BIGREQUESTS. */
100 /* set the length field. */
102 prefix[i].iov_base = vector[0].iov_base;
103 prefix[i].iov_len = sizeof(CARD32);
104 vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
105 vector[0].iov_len -= sizeof(CARD32);
106 ((CARD16 *) prefix[i].iov_base)[1] = shortlen;
111 prefix[i].iov_base = &longlen;
112 prefix[i].iov_len = sizeof(CARD32);
116 /* set the major opcode, and the minor opcode for extensions */
119 const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
120 /* TODO: better error handling here, please! */
121 assert(extension && extension->present);
122 ((CARD8 *) prefix[0].iov_base)[0] = extension->major_opcode;
123 ((CARD8 *) prefix[0].iov_base)[1] = req->opcode;
126 ((CARD8 *) prefix[0].iov_base)[0] = req->opcode;
128 /* get a sequence number and arrange for delivery. */
129 pthread_mutex_lock(&c->iolock);
130 if(req->isvoid && !force_sequence_wrap(c))
132 pthread_mutex_unlock(&c->iolock);
136 *request = ++c->out.request;
139 _xcb_in_expect_reply(c, *request);
141 ret = _xcb_out_write_block(c, prefix, i);
143 ret = _xcb_out_write_block(c, vector, req->count);
144 pthread_mutex_unlock(&c->iolock);
149 int XCBFlush(XCBConnection *c)
152 pthread_mutex_lock(&c->iolock);
153 ret = _xcb_out_flush(c);
154 pthread_mutex_unlock(&c->iolock);
158 /* Private interface */
160 int _xcb_out_init(_xcb_out *out)
162 if(pthread_cond_init(&out->cond, 0))
170 out->last_request = 0;
172 out->request_written = 0;
174 if(pthread_mutex_init(&out->reqlenlock, 0))
176 out->maximum_request_length = 0;
181 void _xcb_out_destroy(_xcb_out *out)
183 pthread_cond_destroy(&out->cond);
184 pthread_mutex_destroy(&out->reqlenlock);
188 int _xcb_out_write(XCBConnection *c)
192 n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
194 n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len);
196 if(n < 0 && errno == EAGAIN)
202 for(i = 0; i < c->out.vec_len; ++i)
203 if(c->out.vec[i].iov_len)
210 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
212 static const char pad[3];
216 for(i = 0; i < count; ++i)
217 len += XCB_CEIL(vector[i].iov_len);
219 /* Is the queue about to overflow? */
220 if(c->out.queue_len + len < sizeof(c->out.queue))
222 /* No, this will fit. */
223 for(i = 0; i < count; ++i)
225 memcpy(c->out.queue + c->out.queue_len, vector[i].iov_base, vector[i].iov_len);
226 if(vector[i].iov_len & 3)
227 memset(c->out.queue + c->out.queue_len + vector[i].iov_len, 0, XCB_PAD(vector[i].iov_len));
228 c->out.queue_len += XCB_CEIL(vector[i].iov_len);
233 assert(!c->out.vec_len);
235 c->out.vec = malloc(sizeof(struct iovec) * (1 + count * 2));
240 c->out.vec[c->out.vec_len].iov_base = c->out.queue;
241 c->out.vec[c->out.vec_len++].iov_len = c->out.queue_len;
242 c->out.queue_len = 0;
244 for(i = 0; i < count; ++i)
246 if(!vector[i].iov_len)
248 c->out.vec[c->out.vec_len].iov_base = vector[i].iov_base;
249 c->out.vec[c->out.vec_len++].iov_len = vector[i].iov_len;
250 if(!XCB_PAD(vector[i].iov_len))
252 c->out.vec[c->out.vec_len].iov_base = (void *) pad;
253 c->out.vec[c->out.vec_len++].iov_len = XCB_PAD(vector[i].iov_len);
255 if(_xcb_out_flush(c) <= 0)
263 int _xcb_out_flush(XCBConnection *c)
266 while(ret > 0 && (c->out.queue_len || c->out.vec_len))
267 ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
268 c->out.last_request = 0;
269 c->out.request_written = c->out.request;
270 pthread_cond_broadcast(&c->out.cond);