The Great XCB Renaming
[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
33 #include "xcb.h"
34 #include "xcbext.h"
35 #include "xcbint.h"
36 #include "extensions/bigreq.h"
37
38 static int write_block(xcb_connection_t *c, struct iovec *vector, int count)
39 {
40     while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
41     {
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;
46         ++vector, --count;
47     }
48     if(!count)
49         return 1;
50
51     --vector, ++count;
52     vector[0].iov_base = c->out.queue;
53     vector[0].iov_len = c->out.queue_len;
54     c->out.queue_len = 0;
55     return _xcb_out_send(c, &vector, &count);
56 }
57
58 /* Public interface */
59
60 uint32_t xcb_get_maximum_request_length(xcb_connection_t *c)
61 {
62     if(c->has_error)
63         return 0;
64     pthread_mutex_lock(&c->out.reqlenlock);
65     if(!c->out.maximum_request_length)
66     {
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)
71         {
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;
74             free(r);
75         }
76     }
77     pthread_mutex_unlock(&c->out.reqlenlock);
78     return c->out.maximum_request_length;
79 }
80
81 unsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req)
82 {
83     static const union {
84         struct {
85             uint8_t major;
86             uint8_t pad;
87             uint16_t len;
88         } fields;
89         uint32_t packet;
90     } sync = { { /* GetInputFocus */ 43, 0, 1 } };
91     unsigned int request;
92     uint32_t prefix[3] = { 0 };
93     int veclen = req->count;
94     enum workarounds workaround = WORKAROUND_NONE;
95
96     if(c->has_error)
97         return 0;
98
99     assert(c != 0);
100     assert(vector != 0);
101     assert(req->count > 0);
102
103     if(!(flags & XCB_REQUEST_RAW))
104     {
105         static const char pad[3];
106         int i;
107         uint16_t shortlen = 0;
108         size_t longlen = 0;
109         assert(vector[0].iov_len >= 4);
110         /* set the major opcode, and the minor opcode for extensions */
111         if(req->ext)
112         {
113             const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext);
114             if(!(extension && extension->present))
115             {
116                 _xcb_conn_shutdown(c);
117                 return 0;
118             }
119             ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode;
120             ((uint8_t *) vector[0].iov_base)[1] = req->opcode;
121         }
122         else
123             ((uint8_t *) vector[0].iov_base)[0] = req->opcode;
124
125         /* put together the length field, possibly using BIGREQUESTS */
126         for(i = 0; i < req->count; ++i)
127         {
128             longlen += vector[i].iov_len;
129             if(!vector[i].iov_base)
130             {
131                 vector[i].iov_base = (char *) pad;
132                 assert(vector[i].iov_len <= sizeof(pad));
133             }
134         }
135         assert((longlen & 3) == 0);
136         longlen >>= 2;
137
138         if(longlen <= c->setup->maximum_request_length)
139         {
140             /* we don't need BIGREQUESTS. */
141             shortlen = longlen;
142             longlen = 0;
143         }
144         else if(longlen > xcb_get_maximum_request_length(c))
145         {
146             _xcb_conn_shutdown(c);
147             return 0; /* server can't take this; maybe need BIGREQUESTS? */
148         }
149
150         /* set the length field. */
151         ((uint16_t *) vector[0].iov_base)[1] = shortlen;
152         if(!shortlen)
153             prefix[2] = ++longlen;
154     }
155     flags &= ~XCB_REQUEST_RAW;
156
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) ||
162              req->opcode == 21))
163         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
164
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);
170
171     request = ++c->out.request;
172     /* send GetInputFocus (sync) when 64k-2 requests have been sent without
173      * a reply.
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) ||
179        request == 0)
180     {
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;
185     }
186
187     if(workaround != WORKAROUND_NONE || flags != 0)
188         _xcb_in_expect_reply(c, request, workaround, flags);
189     if(!req->isvoid)
190         c->in.request_expected = c->out.request;
191
192     if(prefix[0] || prefix[2])
193     {
194         --vector, ++veclen;
195         if(prefix[2])
196         {
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);
200         }
201         vector[0].iov_len = sizeof(uint32_t) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
202         vector[0].iov_base = prefix + !prefix[0];
203     }
204
205     if(!write_block(c, vector, veclen))
206     {
207         _xcb_conn_shutdown(c);
208         request = 0;
209     }
210     pthread_mutex_unlock(&c->iolock);
211     return request;
212 }
213
214 int xcb_flush(xcb_connection_t *c)
215 {
216     int ret;
217     if(c->has_error)
218         return 0;
219     pthread_mutex_lock(&c->iolock);
220     ret = _xcb_out_flush_to(c, c->out.request);
221     pthread_mutex_unlock(&c->iolock);
222     return ret;
223 }
224
225 /* Private interface */
226
227 int _xcb_out_init(_xcb_out *out)
228 {
229     if(pthread_cond_init(&out->cond, 0))
230         return 0;
231     out->writing = 0;
232
233     out->queue_len = 0;
234
235     out->request = 0;
236     out->request_written = 0;
237
238     if(pthread_mutex_init(&out->reqlenlock, 0))
239         return 0;
240     out->maximum_request_length = 0;
241
242     return 1;
243 }
244
245 void _xcb_out_destroy(_xcb_out *out)
246 {
247     pthread_cond_destroy(&out->cond);
248     pthread_mutex_destroy(&out->reqlenlock);
249 }
250
251 int _xcb_out_send(xcb_connection_t *c, struct iovec **vector, int *count)
252 {
253     int ret = 1;
254     while(ret && *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);
258     return ret;
259 }
260
261 int _xcb_out_flush_to(xcb_connection_t *c, unsigned int request)
262 {
263     assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
264     if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
265         return 1;
266     if(c->out.queue_len)
267     {
268         struct iovec vec, *vec_ptr = &vec;
269         int count = 1;
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);
274     }
275     while(c->out.writing)
276         pthread_cond_wait(&c->out.cond, &c->iolock);
277     assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
278     return 1;
279 }