X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_out.c;h=442fcf598f473b7587bc4547ac075b15765b87e7;hb=6e29e5f2ee2e6158f1a9480a83e4f906ab9c04d1;hp=80d71086a8409bf092fa3495aad26debbad12b2b;hpb=a7d749ec3fd3303a4e7ace9d4f0f1672f9310ef2;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_out.c b/src/xcb_out.c index 80d7108..442fcf5 100644 --- a/src/xcb_out.c +++ b/src/xcb_out.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -47,6 +48,40 @@ static int force_sequence_wrap(XCBConnection *c) return ret; } +static int _xcb_write(const int fd, char (*buf)[], int *count) +{ + int n = write(fd, *buf, *count); + if(n > 0) + { + *count -= n; + if(*count) + memmove(*buf, *buf + n, *count); + } + return n; +} + +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; +} + /* Public interface */ CARD32 XCBGetMaximumRequestLength(XCBConnection *c) @@ -82,6 +117,24 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector assert(vector != 0); assert(req->count > 0); + /* set the major opcode, and the minor opcode for extensions */ + if(req->ext) + { + 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; + /* put together the length field, possibly using BIGREQUESTS */ for(i = 0; i < req->count; ++i) longlen += XCB_CEIL(vector[i].iov_len) >> 2; @@ -99,39 +152,18 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector } /* set the length field. */ - i = 0; - prefix[i].iov_base = vector[0].iov_base; - prefix[i].iov_len = sizeof(CARD32); - vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32); - vector[0].iov_len -= sizeof(CARD32); - ((CARD16 *) prefix[i].iov_base)[1] = shortlen; - ++i; + ((CARD16 *) vector[0].iov_base)[1] = shortlen; if(!shortlen) { + prefix[0].iov_base = vector[0].iov_base; + prefix[0].iov_len = sizeof(CARD32); + vector[0].iov_base = ((char *) vector[0].iov_base) + sizeof(CARD32); + vector[0].iov_len -= sizeof(CARD32); ++longlen; - prefix[i].iov_base = &longlen; - prefix[i].iov_len = sizeof(CARD32); - ++i; + prefix[1].iov_base = &longlen; + prefix[1].iov_len = sizeof(CARD32); } - /* set the major opcode, and the minor opcode for extensions */ - if(req->ext) - { - const XCBQueryExtensionRep *extension = XCBGetExtensionData(c, req->ext); - /* TODO: better error handling here, please! */ - assert(extension && extension->present); - ((CARD8 *) prefix[0].iov_base)[0] = extension->major_opcode; - ((CARD8 *) prefix[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 *) prefix[0].iov_base)[0] = req->opcode; - /* get a sequence number and arrange for delivery. */ pthread_mutex_lock(&c->iolock); if(req->isvoid && !force_sequence_wrap(c)) @@ -145,7 +177,10 @@ int XCBSendRequest(XCBConnection *c, unsigned int *request, struct iovec *vector if(!req->isvoid) _xcb_in_expect_reply(c, *request, workaround); - ret = _xcb_out_write_block(c, prefix, i); + if(!shortlen) + ret = _xcb_out_write_block(c, prefix, 2); + else + ret = 1; if(ret > 0) ret = _xcb_out_write_block(c, vector, req->count); pthread_mutex_unlock(&c->iolock); @@ -199,8 +234,9 @@ int _xcb_out_write(XCBConnection *c) else n = _xcb_write(c->fd, &c->out.queue, &c->out.queue_len); - if(n < 0 && errno == EAGAIN) - n = 1; + /* XXX: should "nothing was written" be considered failure or + * success for this function? it's not an I/O error, but... */ + n = (n > 0) || (n < 0 && errno == EAGAIN); if(c->out.vec_len) { @@ -258,7 +294,7 @@ int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count) c->out.vec[c->out.vec_len].iov_base = (void *) pad; c->out.vec[c->out.vec_len++].iov_len = XCB_PAD(vector[i].iov_len); } - if(_xcb_out_flush(c) <= 0) + if(!_xcb_out_flush(c)) len = -1; free(c->out.vec); c->out.vec = 0; @@ -269,7 +305,7 @@ int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count) int _xcb_out_flush(XCBConnection *c) { int ret = 1; - while(ret > 0 && (c->out.queue_len || c->out.vec_len)) + while(ret && (c->out.queue_len || 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);