b3a556a994790e0b76717c976fc8620c6ece9da8
[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 <string.h>
31 #include <errno.h>
32
33 #include "xcb.h"
34 #include "xcbext.h"
35 #include "xcbint.h"
36 #include "extensions/bigreq.h"
37
38 static int force_sequence_wrap(XCBConnection *c)
39 {
40     int ret = 1;
41     if((c->out.request - c->in.request_read) > 65530)
42     {
43         pthread_mutex_unlock(&c->iolock);
44         ret = XCBSync(c, 0);
45         pthread_mutex_lock(&c->iolock);
46     }
47     return ret;
48 }
49
50 /* Public interface */
51
52 CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
53 {
54     pthread_mutex_lock(&c->out.reqlenlock);
55     if(!c->out.maximum_request_length)
56     {
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)
61         {
62             XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(c), 0);
63             c->out.maximum_request_length = r->maximum_request_length;
64             free(r);
65         }
66     }
67     pthread_mutex_unlock(&c->out.reqlenlock);
68     return c->out.maximum_request_length;
69 }
70
71 int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
72 {
73     int ret;
74     int i;
75     struct iovec prefix[2];
76     CARD16 shortlen = 0;
77     CARD32 longlen = 0;
78
79     assert(c != 0);
80     assert(request != 0);
81     assert(vector != 0);
82     assert(req->count > 0);
83
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;
87
88     if(longlen > c->setup->maximum_request_length)
89     {
90         if(longlen > XCBGetMaximumRequestLength(c))
91             return 0; /* server can't take this; maybe need BIGREQUESTS? */
92     }
93     else
94     {
95         /* we don't need BIGREQUESTS. */
96         shortlen = longlen;
97         longlen = 0;
98     }
99
100     /* set the length field. */
101     i = 0;
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;
107     ++i;
108     if(!shortlen)
109     {
110         ++longlen;
111         prefix[i].iov_base = &longlen;
112         prefix[i].iov_len = sizeof(CARD32);
113         ++i;
114     }
115
116     /* set the major opcode, and the minor opcode for extensions */
117     if(req->ext)
118     {
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;
124     }
125     else
126         ((CARD8 *) prefix[0].iov_base)[0] = req->opcode;
127
128     /* get a sequence number and arrange for delivery. */
129     pthread_mutex_lock(&c->iolock);
130     if(req->isvoid && !force_sequence_wrap(c))
131     {
132         pthread_mutex_unlock(&c->iolock);
133         return -1;
134     }
135
136     *request = ++c->out.request;
137
138     if(!req->isvoid)
139         _xcb_in_expect_reply(c, *request);
140
141     ret = _xcb_out_write_block(c, prefix, i);
142     if(ret > 0)
143         ret = _xcb_out_write_block(c, vector, req->count);
144     pthread_mutex_unlock(&c->iolock);
145
146     return ret;
147 }
148
149 int XCBFlush(XCBConnection *c)
150 {
151     int ret;
152     pthread_mutex_lock(&c->iolock);
153     ret = _xcb_out_flush(c);
154     pthread_mutex_unlock(&c->iolock);
155     return ret;
156 }
157
158 /* Private interface */
159
160 int _xcb_out_init(_xcb_out *out)
161 {
162     if(pthread_cond_init(&out->cond, 0))
163         return 0;
164     out->writing = 0;
165
166     out->queue_len = 0;
167     out->vec = 0;
168     out->vec_len = 0;
169
170     out->last_request = 0;
171     out->request = 0;
172     out->request_written = 0;
173
174     if(pthread_mutex_init(&out->reqlenlock, 0))
175         return 0;
176     out->maximum_request_length = 0;
177
178     return 1;
179 }
180
181 void _xcb_out_destroy(_xcb_out *out)
182 {
183     pthread_cond_destroy(&out->cond);
184     pthread_mutex_destroy(&out->reqlenlock);
185     free(out->vec);
186 }
187
188 int _xcb_out_write(XCBConnection *c)
189 {
190     int n;
191     if(c->out.vec_len)
192         n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
193     else
194         n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len);
195
196     if(n < 0 && errno == EAGAIN)
197         n = 1;
198
199     if(c->out.vec_len)
200     {
201         int i;
202         for(i = 0; i < c->out.vec_len; ++i)
203             if(c->out.vec[i].iov_len)
204                 return n;
205         c->out.vec_len = 0;
206     }
207     return n;
208 }
209
210 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
211 {
212     static const char pad[3];
213     int i;
214     int len = 0;
215
216     for(i = 0; i < count; ++i)
217         len += XCB_CEIL(vector[i].iov_len);
218
219     /* Is the queue about to overflow? */
220     if(c->out.queue_len + len < sizeof(c->out.queue))
221     {
222         /* No, this will fit. */
223         for(i = 0; i < count; ++i)
224         {
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);
229         }
230         return len;
231     }
232
233     assert(!c->out.vec_len);
234     assert(!c->out.vec);
235     c->out.vec = malloc(sizeof(struct iovec) * (1 + count * 2));
236     if(!c->out.vec)
237         return -1;
238     if(c->out.queue_len)
239     {
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;
243     }
244     for(i = 0; i < count; ++i)
245     {
246         if(!vector[i].iov_len)
247             continue;
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))
251             continue;
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);
254     }
255     if(_xcb_out_flush(c) <= 0)
256         len = -1;
257     free(c->out.vec);
258     c->out.vec = 0;
259
260     return len;
261 }
262
263 int _xcb_out_flush(XCBConnection *c)
264 {
265     int ret = 1;
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);
271     return ret;
272 }