xcb_out.c revision 602e473d
1602e473dSmrg/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp. 2602e473dSmrg * 3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a 4602e473dSmrg * copy of this software and associated documentation files (the "Software"), 5602e473dSmrg * to deal in the Software without restriction, including without limitation 6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the 8602e473dSmrg * Software is furnished to do so, subject to the following conditions: 9602e473dSmrg * 10602e473dSmrg * The above copyright notice and this permission notice shall be included in 11602e473dSmrg * all copies or substantial portions of the Software. 12602e473dSmrg * 13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19602e473dSmrg * 20602e473dSmrg * Except as contained in this notice, the names of the authors or their 21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the 22602e473dSmrg * sale, use or other dealings in this Software without prior written 23602e473dSmrg * authorization from the authors. 24602e473dSmrg */ 25602e473dSmrg 26602e473dSmrg/* Stuff that sends stuff to the server. */ 27602e473dSmrg 28602e473dSmrg#include <assert.h> 29602e473dSmrg#include <stdlib.h> 30602e473dSmrg#include <unistd.h> 31602e473dSmrg#include <string.h> 32602e473dSmrg 33602e473dSmrg#include "xcb.h" 34602e473dSmrg#include "xcbext.h" 35602e473dSmrg#include "xcbint.h" 36602e473dSmrg#include "bigreq.h" 37602e473dSmrg 38602e473dSmrgstatic int write_block(xcb_connection_t *c, struct iovec *vector, int count) 39602e473dSmrg{ 40602e473dSmrg while(count && c->out.queue_len + vector[0].iov_len <= sizeof(c->out.queue)) 41602e473dSmrg { 42602e473dSmrg memcpy(c->out.queue + c->out.queue_len, vector[0].iov_base, vector[0].iov_len); 43602e473dSmrg c->out.queue_len += vector[0].iov_len; 44602e473dSmrg vector[0].iov_base = (char *) vector[0].iov_base + vector[0].iov_len; 45602e473dSmrg vector[0].iov_len = 0; 46602e473dSmrg ++vector, --count; 47602e473dSmrg } 48602e473dSmrg if(!count) 49602e473dSmrg return 1; 50602e473dSmrg 51602e473dSmrg --vector, ++count; 52602e473dSmrg vector[0].iov_base = c->out.queue; 53602e473dSmrg vector[0].iov_len = c->out.queue_len; 54602e473dSmrg c->out.queue_len = 0; 55602e473dSmrg return _xcb_out_send(c, &vector, &count); 56602e473dSmrg} 57602e473dSmrg 58602e473dSmrgstatic void get_socket_back(xcb_connection_t *c) 59602e473dSmrg{ 60602e473dSmrg while(c->out.return_socket && c->out.socket_moving) 61602e473dSmrg pthread_cond_wait(&c->out.socket_cond, &c->iolock); 62602e473dSmrg if(!c->out.return_socket) 63602e473dSmrg return; 64602e473dSmrg 65602e473dSmrg c->out.socket_moving = 1; 66602e473dSmrg pthread_mutex_unlock(&c->iolock); 67602e473dSmrg c->out.return_socket(c->out.socket_closure); 68602e473dSmrg pthread_mutex_lock(&c->iolock); 69602e473dSmrg c->out.socket_moving = 0; 70602e473dSmrg 71602e473dSmrg pthread_cond_broadcast(&c->out.socket_cond); 72602e473dSmrg c->out.return_socket = 0; 73602e473dSmrg c->out.socket_closure = 0; 74602e473dSmrg _xcb_in_replies_done(c); 75602e473dSmrg} 76602e473dSmrg 77602e473dSmrg/* Public interface */ 78602e473dSmrg 79602e473dSmrgvoid xcb_prefetch_maximum_request_length(xcb_connection_t *c) 80602e473dSmrg{ 81602e473dSmrg if(c->has_error) 82602e473dSmrg return; 83602e473dSmrg pthread_mutex_lock(&c->out.reqlenlock); 84602e473dSmrg if(c->out.maximum_request_length_tag == LAZY_NONE) 85602e473dSmrg { 86602e473dSmrg const xcb_query_extension_reply_t *ext; 87602e473dSmrg ext = xcb_get_extension_data(c, &xcb_big_requests_id); 88602e473dSmrg if(ext && ext->present) 89602e473dSmrg { 90602e473dSmrg c->out.maximum_request_length_tag = LAZY_COOKIE; 91602e473dSmrg c->out.maximum_request_length.cookie = xcb_big_requests_enable(c); 92602e473dSmrg } 93602e473dSmrg else 94602e473dSmrg { 95602e473dSmrg c->out.maximum_request_length_tag = LAZY_FORCED; 96602e473dSmrg c->out.maximum_request_length.value = c->setup->maximum_request_length; 97602e473dSmrg } 98602e473dSmrg } 99602e473dSmrg pthread_mutex_unlock(&c->out.reqlenlock); 100602e473dSmrg} 101602e473dSmrg 102602e473dSmrguint32_t xcb_get_maximum_request_length(xcb_connection_t *c) 103602e473dSmrg{ 104602e473dSmrg if(c->has_error) 105602e473dSmrg return 0; 106602e473dSmrg xcb_prefetch_maximum_request_length(c); 107602e473dSmrg pthread_mutex_lock(&c->out.reqlenlock); 108602e473dSmrg if(c->out.maximum_request_length_tag == LAZY_COOKIE) 109602e473dSmrg { 110602e473dSmrg xcb_big_requests_enable_reply_t *r = xcb_big_requests_enable_reply(c, c->out.maximum_request_length.cookie, 0); 111602e473dSmrg c->out.maximum_request_length_tag = LAZY_FORCED; 112602e473dSmrg if(r) 113602e473dSmrg { 114602e473dSmrg c->out.maximum_request_length.value = r->maximum_request_length; 115602e473dSmrg free(r); 116602e473dSmrg } 117602e473dSmrg else 118602e473dSmrg c->out.maximum_request_length.value = c->setup->maximum_request_length; 119602e473dSmrg } 120602e473dSmrg pthread_mutex_unlock(&c->out.reqlenlock); 121602e473dSmrg return c->out.maximum_request_length.value; 122602e473dSmrg} 123602e473dSmrg 124602e473dSmrgunsigned int xcb_send_request(xcb_connection_t *c, int flags, struct iovec *vector, const xcb_protocol_request_t *req) 125602e473dSmrg{ 126602e473dSmrg static const union { 127602e473dSmrg struct { 128602e473dSmrg uint8_t major; 129602e473dSmrg uint8_t pad; 130602e473dSmrg uint16_t len; 131602e473dSmrg } fields; 132602e473dSmrg uint32_t packet; 133602e473dSmrg } sync_req = { { /* GetInputFocus */ 43, 0, 1 } }; 134602e473dSmrg uint64_t request; 135602e473dSmrg uint32_t prefix[3] = { 0 }; 136602e473dSmrg int veclen = req->count; 137602e473dSmrg enum workarounds workaround = WORKAROUND_NONE; 138602e473dSmrg 139602e473dSmrg if(c->has_error) 140602e473dSmrg return 0; 141602e473dSmrg 142602e473dSmrg assert(c != 0); 143602e473dSmrg assert(vector != 0); 144602e473dSmrg assert(req->count > 0); 145602e473dSmrg 146602e473dSmrg if(!(flags & XCB_REQUEST_RAW)) 147602e473dSmrg { 148602e473dSmrg static const char pad[3]; 149602e473dSmrg unsigned int i; 150602e473dSmrg uint16_t shortlen = 0; 151602e473dSmrg size_t longlen = 0; 152602e473dSmrg assert(vector[0].iov_len >= 4); 153602e473dSmrg /* set the major opcode, and the minor opcode for extensions */ 154602e473dSmrg if(req->ext) 155602e473dSmrg { 156602e473dSmrg const xcb_query_extension_reply_t *extension = xcb_get_extension_data(c, req->ext); 157602e473dSmrg if(!(extension && extension->present)) 158602e473dSmrg { 159602e473dSmrg _xcb_conn_shutdown(c); 160602e473dSmrg return 0; 161602e473dSmrg } 162602e473dSmrg ((uint8_t *) vector[0].iov_base)[0] = extension->major_opcode; 163602e473dSmrg ((uint8_t *) vector[0].iov_base)[1] = req->opcode; 164602e473dSmrg } 165602e473dSmrg else 166602e473dSmrg ((uint8_t *) vector[0].iov_base)[0] = req->opcode; 167602e473dSmrg 168602e473dSmrg /* put together the length field, possibly using BIGREQUESTS */ 169602e473dSmrg for(i = 0; i < req->count; ++i) 170602e473dSmrg { 171602e473dSmrg longlen += vector[i].iov_len; 172602e473dSmrg if(!vector[i].iov_base) 173602e473dSmrg { 174602e473dSmrg vector[i].iov_base = (char *) pad; 175602e473dSmrg assert(vector[i].iov_len <= sizeof(pad)); 176602e473dSmrg } 177602e473dSmrg } 178602e473dSmrg assert((longlen & 3) == 0); 179602e473dSmrg longlen >>= 2; 180602e473dSmrg 181602e473dSmrg if(longlen <= c->setup->maximum_request_length) 182602e473dSmrg { 183602e473dSmrg /* we don't need BIGREQUESTS. */ 184602e473dSmrg shortlen = longlen; 185602e473dSmrg longlen = 0; 186602e473dSmrg } 187602e473dSmrg else if(longlen > xcb_get_maximum_request_length(c)) 188602e473dSmrg { 189602e473dSmrg _xcb_conn_shutdown(c); 190602e473dSmrg return 0; /* server can't take this; maybe need BIGREQUESTS? */ 191602e473dSmrg } 192602e473dSmrg 193602e473dSmrg /* set the length field. */ 194602e473dSmrg ((uint16_t *) vector[0].iov_base)[1] = shortlen; 195602e473dSmrg if(!shortlen) 196602e473dSmrg prefix[2] = ++longlen; 197602e473dSmrg } 198602e473dSmrg flags &= ~XCB_REQUEST_RAW; 199602e473dSmrg 200602e473dSmrg /* do we need to work around the X server bug described in glx.xml? */ 201602e473dSmrg /* XXX: GetFBConfigs won't use BIG-REQUESTS in any sane 202602e473dSmrg * configuration, but that should be handled here anyway. */ 203602e473dSmrg if(req->ext && !req->isvoid && !strcmp(req->ext->name, "GLX") && 204602e473dSmrg ((req->opcode == 17 && ((uint32_t *) vector[0].iov_base)[1] == 0x10004) || 205602e473dSmrg req->opcode == 21)) 206602e473dSmrg workaround = WORKAROUND_GLX_GET_FB_CONFIGS_BUG; 207602e473dSmrg 208602e473dSmrg /* get a sequence number and arrange for delivery. */ 209602e473dSmrg pthread_mutex_lock(&c->iolock); 210602e473dSmrg /* wait for other writing threads to get out of my way. */ 211602e473dSmrg while(c->out.writing) 212602e473dSmrg pthread_cond_wait(&c->out.cond, &c->iolock); 213602e473dSmrg get_socket_back(c); 214602e473dSmrg 215602e473dSmrg request = ++c->out.request; 216602e473dSmrg /* send GetInputFocus (sync_req) when 64k-2 requests have been sent without 217602e473dSmrg * a reply. 218602e473dSmrg * Also send sync_req (could use NoOp) at 32-bit wrap to avoid having 219602e473dSmrg * applications see sequence 0 as that is used to indicate 220602e473dSmrg * an error in sending the request */ 221602e473dSmrg while((req->isvoid && 222602e473dSmrg c->out.request == c->in.request_expected + (1 << 16) - 1) || 223602e473dSmrg request == 0) 224602e473dSmrg { 225602e473dSmrg prefix[0] = sync_req.packet; 226602e473dSmrg _xcb_in_expect_reply(c, request, WORKAROUND_NONE, XCB_REQUEST_DISCARD_REPLY); 227602e473dSmrg c->in.request_expected = c->out.request; 228602e473dSmrg request = ++c->out.request; 229602e473dSmrg } 230602e473dSmrg 231602e473dSmrg if(workaround != WORKAROUND_NONE || flags != 0) 232602e473dSmrg _xcb_in_expect_reply(c, request, workaround, flags); 233602e473dSmrg if(!req->isvoid) 234602e473dSmrg c->in.request_expected = c->out.request; 235602e473dSmrg 236602e473dSmrg if(prefix[0] || prefix[2]) 237602e473dSmrg { 238602e473dSmrg --vector, ++veclen; 239602e473dSmrg if(prefix[2]) 240602e473dSmrg { 241602e473dSmrg prefix[1] = ((uint32_t *) vector[1].iov_base)[0]; 242602e473dSmrg vector[1].iov_base = (uint32_t *) vector[1].iov_base + 1; 243602e473dSmrg vector[1].iov_len -= sizeof(uint32_t); 244602e473dSmrg } 245602e473dSmrg vector[0].iov_len = sizeof(uint32_t) * ((prefix[0] ? 1 : 0) + (prefix[2] ? 2 : 0)); 246602e473dSmrg vector[0].iov_base = prefix + !prefix[0]; 247602e473dSmrg } 248602e473dSmrg 249602e473dSmrg if(!write_block(c, vector, veclen)) 250602e473dSmrg { 251602e473dSmrg _xcb_conn_shutdown(c); 252602e473dSmrg request = 0; 253602e473dSmrg } 254602e473dSmrg pthread_mutex_unlock(&c->iolock); 255602e473dSmrg return request; 256602e473dSmrg} 257602e473dSmrg 258602e473dSmrgint xcb_take_socket(xcb_connection_t *c, void (*return_socket)(void *closure), void *closure, int flags, uint64_t *sent) 259602e473dSmrg{ 260602e473dSmrg int ret; 261602e473dSmrg if(c->has_error) 262602e473dSmrg return 0; 263602e473dSmrg pthread_mutex_lock(&c->iolock); 264602e473dSmrg get_socket_back(c); 265602e473dSmrg ret = _xcb_out_flush_to(c, c->out.request); 266602e473dSmrg if(ret) 267602e473dSmrg { 268602e473dSmrg c->out.return_socket = return_socket; 269602e473dSmrg c->out.socket_closure = closure; 270602e473dSmrg if(flags) 271602e473dSmrg _xcb_in_expect_reply(c, c->out.request, WORKAROUND_EXTERNAL_SOCKET_OWNER, flags); 272602e473dSmrg assert(c->out.request == c->out.request_written); 273602e473dSmrg *sent = c->out.request; 274602e473dSmrg } 275602e473dSmrg pthread_mutex_unlock(&c->iolock); 276602e473dSmrg return ret; 277602e473dSmrg} 278602e473dSmrg 279602e473dSmrgint xcb_writev(xcb_connection_t *c, struct iovec *vector, int count, uint64_t requests) 280602e473dSmrg{ 281602e473dSmrg int ret; 282602e473dSmrg if(c->has_error) 283602e473dSmrg return 0; 284602e473dSmrg pthread_mutex_lock(&c->iolock); 285602e473dSmrg c->out.request += requests; 286602e473dSmrg ret = _xcb_out_send(c, &vector, &count); 287602e473dSmrg pthread_mutex_unlock(&c->iolock); 288602e473dSmrg return ret; 289602e473dSmrg} 290602e473dSmrg 291602e473dSmrgint xcb_flush(xcb_connection_t *c) 292602e473dSmrg{ 293602e473dSmrg int ret; 294602e473dSmrg if(c->has_error) 295602e473dSmrg return 0; 296602e473dSmrg pthread_mutex_lock(&c->iolock); 297602e473dSmrg ret = _xcb_out_flush_to(c, c->out.request); 298602e473dSmrg pthread_mutex_unlock(&c->iolock); 299602e473dSmrg return ret; 300602e473dSmrg} 301602e473dSmrg 302602e473dSmrg/* Private interface */ 303602e473dSmrg 304602e473dSmrgint _xcb_out_init(_xcb_out *out) 305602e473dSmrg{ 306602e473dSmrg if(pthread_cond_init(&out->socket_cond, 0)) 307602e473dSmrg return 0; 308602e473dSmrg out->return_socket = 0; 309602e473dSmrg out->socket_closure = 0; 310602e473dSmrg out->socket_moving = 0; 311602e473dSmrg 312602e473dSmrg if(pthread_cond_init(&out->cond, 0)) 313602e473dSmrg return 0; 314602e473dSmrg out->writing = 0; 315602e473dSmrg 316602e473dSmrg out->queue_len = 0; 317602e473dSmrg 318602e473dSmrg out->request = 0; 319602e473dSmrg out->request_written = 0; 320602e473dSmrg 321602e473dSmrg if(pthread_mutex_init(&out->reqlenlock, 0)) 322602e473dSmrg return 0; 323602e473dSmrg out->maximum_request_length_tag = LAZY_NONE; 324602e473dSmrg 325602e473dSmrg return 1; 326602e473dSmrg} 327602e473dSmrg 328602e473dSmrgvoid _xcb_out_destroy(_xcb_out *out) 329602e473dSmrg{ 330602e473dSmrg pthread_cond_destroy(&out->cond); 331602e473dSmrg pthread_mutex_destroy(&out->reqlenlock); 332602e473dSmrg} 333602e473dSmrg 334602e473dSmrgint _xcb_out_send(xcb_connection_t *c, struct iovec **vector, int *count) 335602e473dSmrg{ 336602e473dSmrg int ret = 1; 337602e473dSmrg while(ret && *count) 338602e473dSmrg ret = _xcb_conn_wait(c, &c->out.cond, vector, count); 339602e473dSmrg c->out.request_written = c->out.request; 340602e473dSmrg pthread_cond_broadcast(&c->out.cond); 341602e473dSmrg return ret; 342602e473dSmrg} 343602e473dSmrg 344602e473dSmrgint _xcb_out_flush_to(xcb_connection_t *c, uint64_t request) 345602e473dSmrg{ 346602e473dSmrg assert(XCB_SEQUENCE_COMPARE(request, <=, c->out.request)); 347602e473dSmrg if(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request)) 348602e473dSmrg return 1; 349602e473dSmrg if(c->out.queue_len) 350602e473dSmrg { 351602e473dSmrg struct iovec vec, *vec_ptr = &vec; 352602e473dSmrg int count = 1; 353602e473dSmrg vec.iov_base = c->out.queue; 354602e473dSmrg vec.iov_len = c->out.queue_len; 355602e473dSmrg c->out.queue_len = 0; 356602e473dSmrg return _xcb_out_send(c, &vec_ptr, &count); 357602e473dSmrg } 358602e473dSmrg while(c->out.writing) 359602e473dSmrg pthread_cond_wait(&c->out.cond, &c->iolock); 360602e473dSmrg assert(XCB_SEQUENCE_COMPARE(c->out.request_written, >=, request)); 361602e473dSmrg return 1; 362602e473dSmrg} 363