Make all public functions do nothing on an error connection.
[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(XCBConnection *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 CARD32 XCBGetMaximumRequestLength(XCBConnection *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 XCBQueryExtensionRep *ext;
68         c->out.maximum_request_length = c->setup->maximum_request_length;
69         ext = XCBGetExtensionData(c, &XCBBigRequestsId);
70         if(ext && ext->present)
71         {
72             XCBBigRequestsEnableRep *r = XCBBigRequestsEnableReply(c, XCBBigRequestsEnable(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 XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
82 {
83     static const union {
84         struct {
85             CARD8 major;
86             CARD8 pad;
87             CARD16 len;
88         } fields;
89         CARD32 packet;
90     } sync = { { /* GetInputFocus */ 43, 0, 1 } };
91     unsigned int request;
92     CARD32 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         CARD16 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 XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
114             if(!(extension && extension->present))
115                 return 0;
116             ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
117             ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
118         }
119         else
120             ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
121
122         /* put together the length field, possibly using BIGREQUESTS */
123         for(i = 0; i < req->count; ++i)
124         {
125             longlen += vector[i].iov_len;
126             if(!vector[i].iov_base)
127             {
128                 vector[i].iov_base = (char *) pad;
129                 assert(vector[i].iov_len <= sizeof(pad));
130             }
131         }
132         assert((longlen & 3) == 0);
133         longlen >>= 2;
134
135         if(longlen <= c->setup->maximum_request_length)
136         {
137             /* we don't need BIGREQUESTS. */
138             shortlen = longlen;
139             longlen = 0;
140         }
141         else if(longlen > XCBGetMaximumRequestLength(c))
142             return 0; /* server can't take this; maybe need BIGREQUESTS? */
143
144         /* set the length field. */
145         ((CARD16 *) vector[0].iov_base)[1] = shortlen;
146         if(!shortlen)
147             prefix[2] = ++longlen;
148     }
149     flags &= ~XCB_REQUEST_RAW;
150
151     /* do we need to work around the X server bug described in glx.xml? */
152     /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
153      * configuration, but that should be handled here anyway. */
154     if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
155             ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[1] == 0x10004) ||
156              req->opcode == 21))
157         workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
158
159     /* get a sequence number and arrange for delivery. */
160     pthread_mutex_lock(&c->iolock);
161     /* wait for other writing threads to get out of my way. */
162     while(c->out.writing)
163         pthread_cond_wait(&c->out.cond, &c->iolock);
164
165     request = ++c->out.request;
166     /* send GetInputFocus (sync) when 64k-2 requests have been sent without
167      * a reply.
168      * Also send sync (could use NoOp) at 32-bit wrap to avoid having
169      * applications see sequence 0 as that is used to indicate
170      * an error in sending the request */
171     while((req->isvoid &&
172         c->out.request == c->in.request_expected + (1 << 16) - 1) ||
173        request == 0)
174     {
175         prefix[0] = sync.packet;
176         _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
177         c->in.request_expected = c->out.request;
178         request = ++c->out.request;
179     }
180
181     if(workaround != WORKAROUND_NONE || flags != 0)
182         _xcb_in_expect_reply(c, request, workaround, flags);
183     if(!req->isvoid)
184         c->in.request_expected = c->out.request;
185
186     if(prefix[0] || prefix[2])
187     {
188         --vector, ++veclen;
189         if(prefix[2])
190         {
191             prefix[1] = ((CARD32 *) vector[1].iov_base)[0];
192             vector[1].iov_base = (CARD32 *) vector[1].iov_base + 1;
193             vector[1].iov_len -= sizeof(CARD32);
194         }
195         vector[0].iov_len = sizeof(CARD32) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
196         vector[0].iov_base = prefix + !prefix[0];
197     }
198
199     if(!write_block(c, vector, veclen))
200         request = 0;
201     pthread_mutex_unlock(&c->iolock);
202     return request;
203 }
204
205 int XCBFlush(XCBConnection *c)
206 {
207     int ret;
208     if(c->has_error)
209         return 0;
210     pthread_mutex_lock(&c->iolock);
211     ret = _xcb_out_flush_to(c, c->out.request);
212     pthread_mutex_unlock(&c->iolock);
213     return ret;
214 }
215
216 /* Private interface */
217
218 int _xcb_out_init(_xcb_out *out)
219 {
220     if(pthread_cond_init(&out->cond, 0))
221         return 0;
222     out->writing = 0;
223
224     out->queue_len = 0;
225
226     out->request = 0;
227     out->request_written = 0;
228
229     if(pthread_mutex_init(&out->reqlenlock, 0))
230         return 0;
231     out->maximum_request_length = 0;
232
233     return 1;
234 }
235
236 void _xcb_out_destroy(_xcb_out *out)
237 {
238     pthread_cond_destroy(&out->cond);
239     pthread_mutex_destroy(&out->reqlenlock);
240 }
241
242 int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
243 {
244     int ret = 1;
245     while(ret && *count)
246         ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
247     c->out.request_written = c->out.request;
248     pthread_cond_broadcast(&c->out.cond);
249     return ret;
250 }
251
252 int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
253 {
254     assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
255     if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
256         return 1;
257     if(c->out.queue_len)
258     {
259         struct iovec vec, *vec_ptr = &vec;
260         int count = 1;
261         vec.iov_base = c->out.queue;
262         vec.iov_len = c->out.queue_len;
263         c->out.queue_len = 0;
264         return _xcb_out_send(c, &vec_ptr, &count);
265     }
266     while(c->out.writing)
267         pthread_cond_wait(&c->out.cond, &c->iolock);
268     assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
269     return 1;
270 }