Remove c->out.vec. Pass iovecs directly down the call tree. Add _xcb_out_flush_to...
[free-sw/xcb/libxcb] / src / xcb_out.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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.
19  * 
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.
24  */
25
26 /* Stuff that sends stuff to the server. */
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include "xcb.h"
35 #include "xcbext.h"
36 #include "xcbint.h"
37 #include "extensions/bigreq.h"
38
39 static int write_block(XCBConnection *c, struct iovec *vector, int count)
40 {
41     while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
42     {
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;
47         ++vector, --count;
48     }
49     if(!count)
50         return 1;
51
52     --vector, ++count;
53     vector[0].iov_base = c->out.queue;
54     vector[0].iov_len = c->out.queue_len;
55     c->out.queue_len = 0;
56     return _xcb_out_send(c, &vector, &count);
57 }
58
59 /* Public interface */
60
61 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
62 {
63     pthread_mutex_lock(&c->out.reqlenlock);
64     if(!c->out.maximum_request_length)
65     {
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)
70         {
71             XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
72             c->out.maximum_request_length = r->maximum_request_length;
73             free(r);
74         }
75     }
76     pthread_mutex_unlock(&c->out.reqlenlock);
77     return c->out.maximum_request_length;
78 }
79
80 unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
81 {
82     static const union {
83         struct {
84             CARD8 major;
85             CARD8 pad;
86             CARD16 len;
87         } fields;
88         CARD32 packet;
89     } sync = { { /* GetInputFocus */ 43, 0, 1 } };
90     unsigned int request;
91     CARD32 prefix[3] = { 0 };
92     int veclen = req->count;
93     enum workarounds workaround = WORKAROUND_NONE;
94
95     assert(c != 0);
96     assert(vector != 0);
97     assert(req->count > 0);
98
99     if(!(flags & XCB_REQUEST_RAW))
100     {
101         static const char pad[3];
102         int i;
103         CARD16 shortlen = 0;
104         size_t longlen = 0;
105         assert(vector[0].iov_len >= 4);
106         /* set the major opcode, and the minor opcode for extensions */
107         if(req->ext)
108         {
109             const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
110             if(!(extension && extension->present))
111                 return 0;
112             ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
113             ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
114         }
115         else
116             ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
117
118         /* put together the length field, possibly using BIGREQUESTS */
119         for(i = 0; i < req->count; ++i)
120         {
121             longlen += vector[i].iov_len;
122             if(!vector[i].iov_base)
123             {
124                 vector[i].iov_base = (caddr_t) pad;
125                 assert(vector[i].iov_len <= sizeof(pad));
126             }
127         }
128         assert((longlen & 3) == 0);
129         longlen >>= 2;
130
131         if(longlen <= c->setup->maximum_request_length)
132         {
133             /* we don't need BIGREQUESTS. */
134             shortlen = longlen;
135             longlen = 0;
136         }
137         else if(longlen > XCBGetMaximumRequestLength(c))
138             return 0; /* server can't take this; maybe need BIGREQUESTS? */
139
140         /* set the length field. */
141         ((CARD16 *) vector[0].iov_base)[1] = shortlen;
142         if(!shortlen)
143             prefix[2] = ++longlen;
144     }
145     flags &= ~XCB_REQUEST_RAW;
146
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) ||
150              req->opcode == 21))
151         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
152
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);
158
159     if(req->isvoid && c->out.request == c->in.request_expected + (1 << 16) - 2)
160     {
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;
165     }
166
167     request = ++c->out.request;
168     assert(request != 0);
169     _xcb_in_expect_reply(c, request, workaround, flags);
170     if(!req->isvoid)
171         c->in.request_expected = c->out.request;
172
173     if(prefix[0] || prefix[2])
174     {
175         --vector, ++veclen;
176         if(prefix[2])
177         {
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);
181         }
182         vector[0].iov_len = sizeof(CARD32) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
183         vector[0].iov_base = prefix + !prefix[0];
184     }
185
186     if(!write_block(c, vector, veclen))
187         request = 0;
188     pthread_mutex_unlock(&c->iolock);
189     return request;
190 }
191
192 int XCBFlush(XCBConnection *c)
193 {
194     int ret;
195     pthread_mutex_lock(&c->iolock);
196     ret = _xcb_out_flush_to(c, c->out.request);
197     pthread_mutex_unlock(&c->iolock);
198     return ret;
199 }
200
201 /* Private interface */
202
203 int _xcb_out_init(_xcb_out *out)
204 {
205     if(pthread_cond_init(&out->cond, 0))
206         return 0;
207     out->writing = 0;
208
209     out->queue_len = 0;
210
211     out->request = 0;
212     out->request_written = 0;
213
214     if(pthread_mutex_init(&out->reqlenlock, 0))
215         return 0;
216     out->maximum_request_length = 0;
217
218     return 1;
219 }
220
221 void _xcb_out_destroy(_xcb_out *out)
222 {
223     pthread_cond_destroy(&out->cond);
224     pthread_mutex_destroy(&out->reqlenlock);
225 }
226
227 /* precondition: there must be something for us to write. */
228 int _xcb_out_write(XCBConnection *c, struct iovec **vector, int *count)
229 {
230     int n;
231     assert(!c->out.queue_len);
232     n = writev(c->fd, *vector, *count);
233     if(n < 0 && errno == EAGAIN)
234         return 1;
235     if(n <= 0)
236         return 0;
237
238     for(; *count; --*count, ++*vector)
239     {
240         int cur = (*vector)->iov_len;
241         if(cur > n)
242             cur = n;
243         (*vector)->iov_len -= cur;
244         (*vector)->iov_base = (char *) (*vector)->iov_base + cur;
245         n -= cur;
246         if((*vector)->iov_len)
247             break;
248     }
249     if(!*count)
250         *vector = 0;
251     assert(n == 0);
252     return 1;
253 }
254
255 int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
256 {
257     int ret = 1;
258     while(ret && *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);
262     return ret;
263 }
264
265 int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
266 {
267     assert(request <= c->out.request);
268     if(c->out.request_written >= request)
269         return 1;
270     if(c->out.queue_len)
271     {
272         struct iovec vec, *vec_ptr = &vec;
273         int count = 1;
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);
278     }
279     while(c->out.writing)
280         pthread_cond_wait(&c->out.cond, &c->iolock);
281     assert(c->out.request_written >= request);
282     return 1;
283 }