#include <stdlib.h>
#include <unistd.h>
#include <string.h>
-#include <errno.h>
-
-#ifndef __GNUC__
-# if HAVE_ALLOCA_H
-# include <alloca.h>
-# else
-# ifdef _AIX
- #pragma alloca
-# endif
-# endif
-#endif
#include "xcb.h"
#include "xcbext.h"
#include "xcbint.h"
#include "extensions/bigreq.h"
-static int force_sequence_wrap(XCBConnection *c)
+static int write_block(XCBConnection *c, struct iovec *vector, int count)
{
- int ret = 1;
- if((c->out.request - c->in.request_read) > 65530)
+ while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue))
{
- pthread_mutex_unlock(&c->iolock);
- ret = XCBSync(c, 0);
- pthread_mutex_lock(&c->iolock);
+ memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
+ c->out.queue_len += vector[0].iov_len;
+ vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len;
+ vector[0].iov_len = 0;
+ ++vector, --count;
}
- return ret;
-}
+ if(!count)
+ return 1;
-static int _xcb_writev(const int fd, struct iovec *vec, int count)
-{
- int n = writev(fd, vec, count);
- if(n > 0)
- {
- int rem = n;
- for(; count; --count, ++vec)
- {
- int cur = vec->iov_len;
- if(cur > rem)
- cur = rem;
- vec->iov_len -= cur;
- vec->iov_base = (char *) vec->iov_base + cur;
- rem -= cur;
- if(vec->iov_len)
- break;
- }
- assert(rem == 0);
- }
- return n;
+ --vector, ++count;
+ vector[0].iov_base = c->out.queue;
+ vector[0].iov_len = c->out.queue_len;
+ c->out.queue_len = 0;
+ return _xcb_out_send(c, &vector, &count);
}
/* Public interface */
CARD32 XCBGetMaximumRequestLength(XCBConnection *c)
{
+ if(c->has_error)
+ return 0;
pthread_mutex_lock(&c->out.reqlenlock);
if(!c->out.maximum_request_length)
{
return c->out.maximum_request_length;
}
-int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector, const XCBProtocolRequest *req)
+unsigned int XCBSendRequest(XCBConnection *c, int flags, struct iovec *vector, const XCBProtocolRequest *req)
{
- static const char pad[3];
- int ret;
- int i;
- struct iovec *padded;
- int padlen = 0;
- CARD16 shortlen = 0;
- CARD32 longlen = 0;
+ static const union {
+ struct {
+ CARD8 major;
+ CARD8 pad;
+ CARD16 len;
+ } fields;
+ CARD32 packet;
+ } sync = { { /* GetInputFocus */ 43, 0, 1 } };
+ unsigned int request;
+ CARD32 prefix[3] = { 0 };
+ int veclen = req->count;
enum workarounds workaround = WORKAROUND_NONE;
+ if(c->has_error)
+ return 0;
+
assert(c != 0);
- assert(request != 0);
assert(vector != 0);
assert(req->count > 0);
- /* set the major opcode, and the minor opcode for extensions */
- if(req->ext)
+ if(!(flags & XCB_REQUEST_RAW))
{
- const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
- /* TODO: better error handling here, please! */
- assert(extension && extension->present);
- ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
- ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
-
- /* do we need to work around the X server bug described in glx.xml? */
- if(strcmp(req->ext->name, "GLX") &&
- ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[0] == 0x10004) ||
- req->opcode == 21))
- workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
- }
- else
- ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
+ static const char pad[3];
+ int i;
+ CARD16 shortlen = 0;
+ size_t longlen = 0;
+ assert(vector[0].iov_len >= 4);
+ /* set the major opcode, and the minor opcode for extensions */
+ if(req->ext)
+ {
+ const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext);
+ if(!(extension && extension->present))
+ {
+ _xcb_conn_shutdown(c);
+ return 0;
+ }
+ ((CARD8 *) vector[0].iov_base)[0] = extension->major_opcode;
+ ((CARD8 *) vector[0].iov_base)[1] = req->opcode;
+ }
+ else
+ ((CARD8 *) vector[0].iov_base)[0] = req->opcode;
- /* put together the length field, possibly using BIGREQUESTS */
- for(i = 0; i < req->count; ++i)
- longlen += (vector[i].iov_len + 3) >> 2;
+ /* put together the length field, possibly using BIGREQUESTS */
+ for(i = 0; i < req->count; ++i)
+ {
+ longlen += vector[i].iov_len;
+ if(!vector[i].iov_base)
+ {
+ vector[i].iov_base = (char *) pad;
+ assert(vector[i].iov_len <= sizeof(pad));
+ }
+ }
+ assert((longlen & 3) == 0);
+ longlen >>= 2;
- if(longlen > c->setup->maximum_request_length)
- {
- if(longlen > XCBGetMaximumRequestLength(c))
+ if(longlen <= c->setup->maximum_request_length)
+ {
+ /* we don't need BIGREQUESTS. */
+ shortlen = longlen;
+ longlen = 0;
+ }
+ else if(longlen > XCBGetMaximumRequestLength(c))
+ {
+ _xcb_conn_shutdown(c);
return 0; /* server can't take this; maybe need BIGREQUESTS? */
- }
- else
- {
- /* we don't need BIGREQUESTS. */
- shortlen = longlen;
- longlen = 0;
- }
+ }
- padded =
-#ifdef HAVE_ALLOCA
- alloca
-#else
- malloc
-#endif
- ((req->count * 2 + 3) * sizeof(struct iovec));
- /* set the length field. */
- ((CARD16 *) vector[0].iov_base)[1] = shortlen;
- if(!shortlen)
- {
- padded[0].iov_base = vector[0].iov_base;
- padded[0].iov_len = sizeof(CARD32);
- vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32);
- vector[0].iov_len -= sizeof(CARD32);
- ++longlen;
- padded[1].iov_base = &longlen;
- padded[1].iov_len = sizeof(CARD32);
- padlen = 2;
+ /* set the length field. */
+ ((CARD16 *) vector[0].iov_base)[1] = shortlen;
+ if(!shortlen)
+ prefix[2] = ++longlen;
}
+ flags &= ~XCB_REQUEST_RAW;
- for(i = 0; i < req->count; ++i)
- {
- if(!vector[i].iov_len)
- continue;
- padded[padlen].iov_base = vector[i].iov_base;
- padded[padlen++].iov_len = vector[i].iov_len;
- if(!XCB_PAD(vector[i].iov_len))
- continue;
- padded[padlen].iov_base = (caddr_t) pad;
- padded[padlen++].iov_len = XCB_PAD(vector[i].iov_len);
- }
+ /* do we need to work around the X server bug described in glx.xml? */
+ /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane
+ * configuration, but that should be handled here anyway. */
+ if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") &&
+ ((req->opcode == 17 && ((CARD32 *) vector[0].iov_base)[1] == 0x10004) ||
+ req->opcode == 21))
+ workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG;
/* get a sequence number and arrange for delivery. */
pthread_mutex_lock(&c->iolock);
- if(req->isvoid && !force_sequence_wrap(c))
+ /* wait for other writing threads to get out of my way. */
+ while(c->out.writing)
+ pthread_cond_wait(&c->out.cond, &c->iolock);
+
+ request = ++c->out.request;
+ /* send GetInputFocus (sync) when 64k-2 requests have been sent without
+ * a reply.
+ * Also send sync (could use NoOp) at 32-bit wrap to avoid having
+ * applications see sequence 0 as that is used to indicate
+ * an error in sending the request */
+ while((req->isvoid &&
+ c->out.request == c->in.request_expected + (1 << 16) - 1) ||
+ request == 0)
{
- pthread_mutex_unlock(&c->iolock);
-#ifndef HAVE_ALLOCA
- free(padded);
-#endif
- return -1;
+ prefix[0] = sync.packet;
+ _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY);
+ c->in.request_expected = c->out.request;
+ request = ++c->out.request;
}
- *request = ++c->out.request;
-
+ if(workaround != WORKAROUND_NONE || flags != 0)
+ _xcb_in_expect_reply(c, request, workaround, flags);
if(!req->isvoid)
- _xcb_in_expect_reply(c, *request, workaround);
+ c->in.request_expected = c->out.request;
- ret = _xcb_out_write_block(c, padded, padlen);
- pthread_mutex_unlock(&c->iolock);
-#ifndef HAVE_ALLOCA
- free(padded);
-#endif
+ if(prefix[0] || prefix[2])
+ {
+ --vector, ++veclen;
+ if(prefix[2])
+ {
+ prefix[1] = ((CARD32 *) vector[1].iov_base)[0];
+ vector[1].iov_base = (CARD32 *) vector[1].iov_base + 1;
+ vector[1].iov_len -= sizeof(CARD32);
+ }
+ vector[0].iov_len = sizeof(CARD32) * (prefix[0] ? 1 : 0 | prefix[2] ? 2 : 0);
+ vector[0].iov_base = prefix + !prefix[0];
+ }
- return ret;
+ if(!write_block(c, vector, veclen))
+ {
+ _xcb_conn_shutdown(c);
+ request = 0;
+ }
+ pthread_mutex_unlock(&c->iolock);
+ return request;
}
int XCBFlush(XCBConnection *c)
{
int ret;
+ if(c->has_error)
+ return 0;
pthread_mutex_lock(&c->iolock);
- ret = _xcb_out_flush(c);
+ ret = _xcb_out_flush_to(c, c->out.request);
pthread_mutex_unlock(&c->iolock);
return ret;
}
out->writing = 0;
out->queue_len = 0;
- out->vec = 0;
- out->vec_len = 0;
out->request = 0;
out->request_written = 0;
pthread_mutex_destroy(&out->reqlenlock);
}
-/* precondition: there must be something for us to write. */
-int _xcb_out_write(XCBConnection *c)
+int _xcb_out_send(XCBConnection *c, struct iovec **vector, int *count)
{
- int n;
- assert(!c->out.queue_len);
- n = _xcb_writev(c->fd, c->out.vec, c->out.vec_len);
- while(c->out.vec_len && !c->out.vec[0].iov_len)
- ++c->out.vec, --c->out.vec_len;
- if(!c->out.vec_len)
- c->out.vec = 0;
- return (n > 0) || (n < 0 && errno == EAGAIN);
+ int ret = 1;
+ while(ret && *count)
+ ret = _xcb_conn_wait(c, &c->out.cond, vector, count);
+ c->out.request_written = c->out.request;
+ pthread_cond_broadcast(&c->out.cond);
+ return ret;
}
-int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count)
+int _xcb_out_flush_to(XCBConnection *c, unsigned int request)
{
- while(c->out.writing)
- pthread_cond_wait(&c->out.cond, &c->iolock);
- assert(!c->out.vec && !c->out.vec_len);
- while(count && c->out.queue_len + vector[0].iov_len < sizeof(c->out.queue))
- {
- memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len);
- c->out.queue_len += vector[0].iov_len;
- ++vector, --count;
- }
- if(!count)
+ assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request));
+ if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request))
return 1;
-
- memmove(vector + 1, vector, count++ * sizeof(struct iovec));
- vector[0].iov_base = c->out.queue;
- vector[0].iov_len = c->out.queue_len;
- c->out.queue_len = 0;
-
- c->out.vec_len = count;
- c->out.vec = vector;
- return _xcb_out_flush(c);
-}
-
-int _xcb_out_flush(XCBConnection *c)
-{
- int ret = 1;
- struct iovec vec;
if(c->out.queue_len)
{
- assert(!c->out.vec && !c->out.vec_len);
+ struct iovec vec, *vec_ptr = &vec;
+ int count = 1;
vec.iov_base = c->out.queue;
vec.iov_len = c->out.queue_len;
- c->out.vec = &vec;
- c->out.vec_len = 1;
c->out.queue_len = 0;
+ return _xcb_out_send(c, &vec_ptr, &count);
}
- while(ret && c->out.vec_len)
- ret = _xcb_conn_wait(c, /*should_write*/ 1, &c->out.cond);
- c->out.request_written = c->out.request;
- pthread_cond_broadcast(&c->out.cond);
- return ret;
+ while(c->out.writing)
+ pthread_cond_wait(&c->out.cond, &c->iolock);
+ assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request));
+ return 1;
}