xcb_io.c revision eb411b4b
11ab64890Smrg/* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett 21ab64890Smrg * This file is licensed under the MIT license. See the file COPYING. */ 31ab64890Smrg 4b4ee4795Smrg#ifdef HAVE_CONFIG_H 5b4ee4795Smrg#include <config.h> 6b4ee4795Smrg#endif 7b4ee4795Smrg 81ab64890Smrg#include "Xlibint.h" 91ab64890Smrg#include "locking.h" 1061b2299dSmrg#include "Xprivate.h" 111ab64890Smrg#include "Xxcbint.h" 121ab64890Smrg#include <xcb/xcbext.h> 131ab64890Smrg 141ab64890Smrg#include <assert.h> 1588de56ccSmrg#ifdef HAVE_INTTYPES_H 1661b2299dSmrg#include <inttypes.h> 1788de56ccSmrg#endif 186cc2b21fSmrg#include <stdio.h> 1961b2299dSmrg#include <stdint.h> 201ab64890Smrg#include <stdlib.h> 211ab64890Smrg#include <string.h> 22eb411b4bSmrg#include <limits.h> 23b4ee4795Smrg#ifdef HAVE_SYS_SELECT_H 24b4ee4795Smrg#include <sys/select.h> 25b4ee4795Smrg#endif 261ab64890Smrg 276cc2b21fSmrg#define xcb_fail_assert(_message, _var) { \ 286cc2b21fSmrg unsigned int _var = 1; \ 296cc2b21fSmrg fprintf(stderr, "[xcb] Aborting, sorry about that.\n"); \ 306cc2b21fSmrg assert(!_var); \ 316cc2b21fSmrg} 326cc2b21fSmrg 336cc2b21fSmrg#define throw_thread_fail_assert(_message, _var) { \ 346cc2b21fSmrg fprintf(stderr, "[xcb] " _message "\n"); \ 356cc2b21fSmrg fprintf(stderr, "[xcb] Most likely this is a multi-threaded client " \ 366cc2b21fSmrg "and XInitThreads has not been called\n"); \ 376cc2b21fSmrg xcb_fail_assert(_message, _var); \ 386cc2b21fSmrg} 396cc2b21fSmrg 406cc2b21fSmrg/* XXX: It would probably be most useful if we stored the last-processed 416cc2b21fSmrg * request, so we could find the offender from the message. */ 426cc2b21fSmrg#define throw_extlib_fail_assert(_message, _var) { \ 436cc2b21fSmrg fprintf(stderr, "[xcb] " _message "\n"); \ 446cc2b21fSmrg fprintf(stderr, "[xcb] This is most likely caused by a broken X " \ 456cc2b21fSmrg "extension library\n"); \ 466cc2b21fSmrg xcb_fail_assert(_message, _var); \ 476cc2b21fSmrg} 486cc2b21fSmrg 4961b2299dSmrgstatic void return_socket(void *closure) 5061b2299dSmrg{ 5161b2299dSmrg Display *dpy = closure; 5288de56ccSmrg InternalLockDisplay(dpy, /* don't skip user locks */ 0); 5361b2299dSmrg _XSend(dpy, NULL, 0); 5461b2299dSmrg dpy->bufmax = dpy->buffer; 5561b2299dSmrg UnlockDisplay(dpy); 5661b2299dSmrg} 5761b2299dSmrg 5861b2299dSmrgstatic void require_socket(Display *dpy) 5961b2299dSmrg{ 6061b2299dSmrg if(dpy->bufmax == dpy->buffer) 6161b2299dSmrg { 6261b2299dSmrg uint64_t sent; 6361b2299dSmrg int flags = 0; 6461b2299dSmrg /* if we don't own the event queue, we have to ask XCB 6561b2299dSmrg * to set our errors aside for us. */ 6661b2299dSmrg if(dpy->xcb->event_owner != XlibOwnsEventQueue) 6761b2299dSmrg flags = XCB_REQUEST_CHECKED; 6861b2299dSmrg if(!xcb_take_socket(dpy->xcb->connection, return_socket, dpy, 6961b2299dSmrg flags, &sent)) 7061b2299dSmrg _XIOError(dpy); 7161b2299dSmrg /* Xlib uses unsigned long for sequence numbers. XCB 7261b2299dSmrg * uses 64-bit internally, but currently exposes an 7361b2299dSmrg * unsigned int API. If these differ, Xlib cannot track 7461b2299dSmrg * the full 64-bit sequence number if 32-bit wrap 7561b2299dSmrg * happens while Xlib does not own the socket. A 7661b2299dSmrg * complete fix would be to make XCB's public API use 7761b2299dSmrg * 64-bit sequence numbers. */ 786cc2b21fSmrg if (sizeof(unsigned long) > sizeof(unsigned int) && 796cc2b21fSmrg dpy->xcb->event_owner == XlibOwnsEventQueue && 806cc2b21fSmrg (sent - dpy->last_request_read >= (UINT64_C(1) << 32))) { 816cc2b21fSmrg throw_thread_fail_assert("Sequence number wrapped " 826cc2b21fSmrg "beyond 32 bits while Xlib " 836cc2b21fSmrg "did not own the socket", 846cc2b21fSmrg xcb_xlib_seq_number_wrapped); 856cc2b21fSmrg } 8661b2299dSmrg dpy->xcb->last_flushed = dpy->request = sent; 8761b2299dSmrg dpy->bufmax = dpy->xcb->real_bufmax; 8861b2299dSmrg } 8961b2299dSmrg} 9061b2299dSmrg 911ab64890Smrg/* Call internal connection callbacks for any fds that are currently 921ab64890Smrg * ready to read. This function will not block unless one of the 931ab64890Smrg * callbacks blocks. 941ab64890Smrg * 951ab64890Smrg * This code borrowed from _XWaitForReadable. Inverse call tree: 961ab64890Smrg * _XRead 971ab64890Smrg * _XWaitForWritable 981ab64890Smrg * _XFlush 991ab64890Smrg * _XSend 1001ab64890Smrg * _XEventsQueued 1011ab64890Smrg * _XReadEvents 1021ab64890Smrg * _XRead[0-9]+ 1031ab64890Smrg * _XAllocIDs 1041ab64890Smrg * _XReply 1051ab64890Smrg * _XEatData 1061ab64890Smrg * _XReadPad 1071ab64890Smrg */ 1081ab64890Smrgstatic void check_internal_connections(Display *dpy) 1091ab64890Smrg{ 11061b2299dSmrg struct _XConnectionInfo *ilist; 1111ab64890Smrg fd_set r_mask; 1121ab64890Smrg struct timeval tv; 1131ab64890Smrg int result; 1141ab64890Smrg int highest_fd = -1; 1151ab64890Smrg 1161ab64890Smrg if(dpy->flags & XlibDisplayProcConni || !dpy->im_fd_info) 1171ab64890Smrg return; 1181ab64890Smrg 1191ab64890Smrg FD_ZERO(&r_mask); 1201ab64890Smrg for(ilist = dpy->im_fd_info; ilist; ilist = ilist->next) 1211ab64890Smrg { 1221ab64890Smrg assert(ilist->fd >= 0); 1231ab64890Smrg FD_SET(ilist->fd, &r_mask); 1241ab64890Smrg if(ilist->fd > highest_fd) 1251ab64890Smrg highest_fd = ilist->fd; 1261ab64890Smrg } 1271ab64890Smrg assert(highest_fd >= 0); 1281ab64890Smrg 1291ab64890Smrg tv.tv_sec = 0; 1301ab64890Smrg tv.tv_usec = 0; 1311ab64890Smrg result = select(highest_fd + 1, &r_mask, NULL, NULL, &tv); 1321ab64890Smrg 1331ab64890Smrg if(result == -1) 1341ab64890Smrg { 1351ab64890Smrg if(errno == EINTR) 1361ab64890Smrg return; 1371ab64890Smrg _XIOError(dpy); 1381ab64890Smrg } 1391ab64890Smrg 1401ab64890Smrg for(ilist = dpy->im_fd_info; result && ilist; ilist = ilist->next) 1411ab64890Smrg if(FD_ISSET(ilist->fd, &r_mask)) 1421ab64890Smrg { 1431ab64890Smrg _XProcessInternalConnection(dpy, ilist); 1441ab64890Smrg --result; 1451ab64890Smrg } 1461ab64890Smrg} 1471ab64890Smrg 14888de56ccSmrgstatic PendingRequest *append_pending_request(Display *dpy, unsigned long sequence) 1491ab64890Smrg{ 15088de56ccSmrg PendingRequest *node = malloc(sizeof(PendingRequest)); 15188de56ccSmrg assert(node); 15288de56ccSmrg node->next = NULL; 15388de56ccSmrg node->sequence = sequence; 15488de56ccSmrg node->reply_waiter = 0; 15588de56ccSmrg if(dpy->xcb->pending_requests_tail) 1561ab64890Smrg { 1576cc2b21fSmrg if (XLIB_SEQUENCE_COMPARE(dpy->xcb->pending_requests_tail->sequence, 1586cc2b21fSmrg >=, node->sequence)) 1596cc2b21fSmrg throw_thread_fail_assert("Unknown sequence number " 1606cc2b21fSmrg "while appending request", 1616cc2b21fSmrg xcb_xlib_unknown_seq_number); 1626cc2b21fSmrg if (dpy->xcb->pending_requests_tail->next != NULL) 1636cc2b21fSmrg throw_thread_fail_assert("Unknown request in queue " 1646cc2b21fSmrg "while appending request", 1656cc2b21fSmrg xcb_xlib_unknown_req_pending); 16688de56ccSmrg dpy->xcb->pending_requests_tail->next = node; 1671ab64890Smrg } 16888de56ccSmrg else 16988de56ccSmrg dpy->xcb->pending_requests = node; 17088de56ccSmrg dpy->xcb->pending_requests_tail = node; 17188de56ccSmrg return node; 1721ab64890Smrg} 1731ab64890Smrg 17488de56ccSmrgstatic void dequeue_pending_request(Display *dpy, PendingRequest *req) 1751ab64890Smrg{ 1766cc2b21fSmrg if (req != dpy->xcb->pending_requests) 1776cc2b21fSmrg throw_thread_fail_assert("Unknown request in queue while " 1786cc2b21fSmrg "dequeuing", 1796cc2b21fSmrg xcb_xlib_unknown_req_in_deq); 1806cc2b21fSmrg 18188de56ccSmrg dpy->xcb->pending_requests = req->next; 18288de56ccSmrg if(!dpy->xcb->pending_requests) 1831ab64890Smrg { 1846cc2b21fSmrg if (req != dpy->xcb->pending_requests_tail) 1856cc2b21fSmrg throw_thread_fail_assert("Unknown request in queue " 1866cc2b21fSmrg "while dequeuing", 1876cc2b21fSmrg xcb_xlib_unknown_req_in_deq); 18888de56ccSmrg dpy->xcb->pending_requests_tail = NULL; 1891ab64890Smrg } 1906cc2b21fSmrg else if (XLIB_SEQUENCE_COMPARE(req->sequence, >=, 1916cc2b21fSmrg dpy->xcb->pending_requests->sequence)) 1926cc2b21fSmrg throw_thread_fail_assert("Unknown sequence number while " 1936cc2b21fSmrg "dequeuing request", 1946cc2b21fSmrg xcb_xlib_threads_sequence_lost); 1956cc2b21fSmrg 19688de56ccSmrg free(req); 19788de56ccSmrg} 19888de56ccSmrg 19988de56ccSmrgstatic int handle_error(Display *dpy, xError *err, Bool in_XReply) 20088de56ccSmrg{ 20188de56ccSmrg _XExtension *ext; 20288de56ccSmrg int ret_code; 20388de56ccSmrg /* Oddly, Xlib only allows extensions to suppress errors when 20488de56ccSmrg * those errors were seen by _XReply. */ 20588de56ccSmrg if(in_XReply) 20688de56ccSmrg /* 20788de56ccSmrg * we better see if there is an extension who may 20888de56ccSmrg * want to suppress the error. 20988de56ccSmrg */ 21088de56ccSmrg for(ext = dpy->ext_procs; ext; ext = ext->next) 21188de56ccSmrg if(ext->error && (*ext->error)(dpy, err, &ext->codes, &ret_code)) 21288de56ccSmrg return ret_code; 21388de56ccSmrg _XError(dpy, err); 21488de56ccSmrg return 0; 2151ab64890Smrg} 2161ab64890Smrg 21761b2299dSmrg/* Widen a 32-bit sequence number into a native-word-size (unsigned long) 21861b2299dSmrg * sequence number. Treating the comparison as a 1 and shifting it avoids a 21961b2299dSmrg * conditional branch, and shifting by 16 twice avoids a compiler warning when 22061b2299dSmrg * sizeof(unsigned long) == 4. */ 22161b2299dSmrgstatic void widen(unsigned long *wide, unsigned int narrow) 22261b2299dSmrg{ 22361b2299dSmrg unsigned long new = (*wide & ~0xFFFFFFFFUL) | narrow; 22461b2299dSmrg *wide = new + ((unsigned long) (new < *wide) << 16 << 16); 22561b2299dSmrg} 22661b2299dSmrg 22788de56ccSmrg/* Thread-safety rules: 22888de56ccSmrg * 22988de56ccSmrg * At most one thread can be reading from XCB's event queue at a time. 23088de56ccSmrg * If you are not the current event-reading thread and you need to find 23188de56ccSmrg * out if an event is available, you must wait. 23288de56ccSmrg * 23388de56ccSmrg * The same rule applies for reading replies. 23488de56ccSmrg * 23588de56ccSmrg * A single thread cannot be both the the event-reading and the 23688de56ccSmrg * reply-reading thread at the same time. 23788de56ccSmrg * 23888de56ccSmrg * We always look at both the current event and the first pending reply 23988de56ccSmrg * to decide which to process next. 24088de56ccSmrg * 24188de56ccSmrg * We always process all responses in sequence-number order, which may 24288de56ccSmrg * mean waiting for another thread (either the event_waiter or the 24388de56ccSmrg * reply_waiter) to handle an earlier response before we can process or 24488de56ccSmrg * return a later one. If so, we wait on the corresponding condition 24588de56ccSmrg * variable for that thread to process the response and wake us up. 24688de56ccSmrg */ 2471ab64890Smrg 24888de56ccSmrgstatic xcb_generic_reply_t *poll_for_event(Display *dpy) 24988de56ccSmrg{ 25088de56ccSmrg /* Make sure the Display's sequence numbers are valid */ 25161b2299dSmrg require_socket(dpy); 25261b2299dSmrg 25388de56ccSmrg /* Precondition: This thread can safely get events from XCB. */ 25488de56ccSmrg assert(dpy->xcb->event_owner == XlibOwnsEventQueue && !dpy->xcb->event_waiter); 25588de56ccSmrg 25688de56ccSmrg if(!dpy->xcb->next_event) 25788de56ccSmrg dpy->xcb->next_event = xcb_poll_for_event(dpy->xcb->connection); 25888de56ccSmrg 25988de56ccSmrg if(dpy->xcb->next_event) 2601ab64890Smrg { 2611ab64890Smrg PendingRequest *req = dpy->xcb->pending_requests; 26288de56ccSmrg xcb_generic_event_t *event = dpy->xcb->next_event; 26361b2299dSmrg unsigned long event_sequence = dpy->last_request_read; 26488de56ccSmrg widen(&event_sequence, event->full_sequence); 26588de56ccSmrg if(!req || XLIB_SEQUENCE_COMPARE(event_sequence, <, req->sequence) 26688de56ccSmrg || (event->response_type != X_Error && event_sequence == req->sequence)) 2671ab64890Smrg { 2686cc2b21fSmrg if (XLIB_SEQUENCE_COMPARE(event_sequence, >, 2696cc2b21fSmrg dpy->request)) 2706cc2b21fSmrg { 2716cc2b21fSmrg throw_thread_fail_assert("Unknown sequence " 2726cc2b21fSmrg "number while " 2736cc2b21fSmrg "processing queue", 2746cc2b21fSmrg xcb_xlib_threads_sequence_lost); 2756cc2b21fSmrg } 27661b2299dSmrg dpy->last_request_read = event_sequence; 27788de56ccSmrg dpy->xcb->next_event = NULL; 27888de56ccSmrg return (xcb_generic_reply_t *) event; 2791ab64890Smrg } 28088de56ccSmrg } 28188de56ccSmrg return NULL; 28288de56ccSmrg} 28388de56ccSmrg 28488de56ccSmrgstatic xcb_generic_reply_t *poll_for_response(Display *dpy) 28588de56ccSmrg{ 28688de56ccSmrg void *response; 28788de56ccSmrg xcb_generic_error_t *error; 28888de56ccSmrg PendingRequest *req; 28988de56ccSmrg while(!(response = poll_for_event(dpy)) && 29088de56ccSmrg (req = dpy->xcb->pending_requests) && 29188de56ccSmrg !req->reply_waiter && 29288de56ccSmrg xcb_poll_for_reply(dpy->xcb->connection, req->sequence, &response, &error)) 29388de56ccSmrg { 2946cc2b21fSmrg if(XLIB_SEQUENCE_COMPARE(req->sequence, >, dpy->request)) 2956cc2b21fSmrg { 2966cc2b21fSmrg throw_thread_fail_assert("Unknown sequence number " 2976cc2b21fSmrg "while awaiting reply", 2986cc2b21fSmrg xcb_xlib_threads_sequence_lost); 2996cc2b21fSmrg } 30088de56ccSmrg dpy->last_request_read = req->sequence; 301e9fcaa8aSmrg if(response) 302e9fcaa8aSmrg break; 303e9fcaa8aSmrg dequeue_pending_request(dpy, req); 30488de56ccSmrg if(error) 30588de56ccSmrg return (xcb_generic_reply_t *) error; 30688de56ccSmrg } 30788de56ccSmrg return response; 30888de56ccSmrg} 30988de56ccSmrg 31088de56ccSmrgstatic void handle_response(Display *dpy, xcb_generic_reply_t *response, Bool in_XReply) 31188de56ccSmrg{ 31288de56ccSmrg _XAsyncHandler *async, *next; 31388de56ccSmrg switch(response->response_type) 31488de56ccSmrg { 31588de56ccSmrg case X_Reply: 31688de56ccSmrg for(async = dpy->async_handlers; async; async = next) 3171ab64890Smrg { 31888de56ccSmrg next = async->next; 31988de56ccSmrg if(async->handler(dpy, (xReply *) response, (char *) response, sizeof(xReply) + (response->length << 2), async->data)) 32088de56ccSmrg break; 3211ab64890Smrg } 32288de56ccSmrg break; 32388de56ccSmrg 32488de56ccSmrg case X_Error: 32588de56ccSmrg handle_error(dpy, (xError *) response, in_XReply); 32688de56ccSmrg break; 32788de56ccSmrg 32888de56ccSmrg default: /* event */ 32988de56ccSmrg /* GenericEvents may be > 32 bytes. In this case, the 33088de56ccSmrg * event struct is trailed by the additional bytes. the 33188de56ccSmrg * xcb_generic_event_t struct uses 4 bytes for internal 33288de56ccSmrg * numbering, so we need to shift the trailing data to 33388de56ccSmrg * be after the first 32 bytes. */ 33488de56ccSmrg if(response->response_type == GenericEvent && ((xcb_ge_event_t *) response)->length) 3351ab64890Smrg { 33688de56ccSmrg xcb_ge_event_t *event = (xcb_ge_event_t *) response; 33788de56ccSmrg memmove(&event->full_sequence, &event[1], event->length * 4); 3381ab64890Smrg } 33988de56ccSmrg _XEnq(dpy, (xEvent *) response); 34088de56ccSmrg break; 3411ab64890Smrg } 34288de56ccSmrg free(response); 3431ab64890Smrg} 3441ab64890Smrg 3451ab64890Smrgint _XEventsQueued(Display *dpy, int mode) 3461ab64890Smrg{ 34788de56ccSmrg xcb_generic_reply_t *response; 3481ab64890Smrg if(dpy->flags & XlibDisplayIOError) 3491ab64890Smrg return 0; 3501ab64890Smrg if(dpy->xcb->event_owner != XlibOwnsEventQueue) 3511ab64890Smrg return 0; 3521ab64890Smrg 3531ab64890Smrg if(mode == QueuedAfterFlush) 35461b2299dSmrg _XSend(dpy, NULL, 0); 3551ab64890Smrg else 3561ab64890Smrg check_internal_connections(dpy); 35788de56ccSmrg 35888de56ccSmrg /* If another thread is blocked waiting for events, then we must 35988de56ccSmrg * let that thread pick up the next event. Since it blocked, we 36088de56ccSmrg * can reasonably claim there are no new events right now. */ 36188de56ccSmrg if(!dpy->xcb->event_waiter) 36288de56ccSmrg { 36388de56ccSmrg while((response = poll_for_response(dpy))) 36488de56ccSmrg handle_response(dpy, response, False); 36588de56ccSmrg if(xcb_connection_has_error(dpy->xcb->connection)) 36688de56ccSmrg _XIOError(dpy); 36788de56ccSmrg } 3681ab64890Smrg return dpy->qlen; 3691ab64890Smrg} 3701ab64890Smrg 3711ab64890Smrg/* _XReadEvents - Flush the output queue, 3721ab64890Smrg * then read as many events as possible (but at least 1) and enqueue them 3731ab64890Smrg */ 3741ab64890Smrgvoid _XReadEvents(Display *dpy) 3751ab64890Smrg{ 37688de56ccSmrg xcb_generic_reply_t *response; 37788de56ccSmrg unsigned long serial; 37888de56ccSmrg 3791ab64890Smrg if(dpy->flags & XlibDisplayIOError) 3801ab64890Smrg return; 38161b2299dSmrg _XSend(dpy, NULL, 0); 3821ab64890Smrg if(dpy->xcb->event_owner != XlibOwnsEventQueue) 3831ab64890Smrg return; 3841ab64890Smrg check_internal_connections(dpy); 38588de56ccSmrg 38688de56ccSmrg serial = dpy->next_event_serial_num; 38788de56ccSmrg while(serial == dpy->next_event_serial_num || dpy->qlen == 0) 38888de56ccSmrg { 38988de56ccSmrg if(dpy->xcb->event_waiter) 39088de56ccSmrg { 39188de56ccSmrg ConditionWait(dpy, dpy->xcb->event_notify); 39288de56ccSmrg /* Maybe the other thread got us an event. */ 39388de56ccSmrg continue; 39488de56ccSmrg } 39588de56ccSmrg 39688de56ccSmrg if(!dpy->xcb->next_event) 39788de56ccSmrg { 39888de56ccSmrg xcb_generic_event_t *event; 39988de56ccSmrg dpy->xcb->event_waiter = 1; 40088de56ccSmrg UnlockDisplay(dpy); 40188de56ccSmrg event = xcb_wait_for_event(dpy->xcb->connection); 40257f47464Smrg /* It appears that classic Xlib respected user 40357f47464Smrg * locks when waking up after waiting for 40457f47464Smrg * events. However, if this thread did not have 40557f47464Smrg * any user locks, and another thread takes a 40657f47464Smrg * user lock and tries to read events, then we'd 40757f47464Smrg * deadlock. So we'll choose to let the thread 40857f47464Smrg * that got in first consume events, despite the 40957f47464Smrg * later thread's user locks. */ 41057f47464Smrg InternalLockDisplay(dpy, /* ignore user locks */ 1); 41188de56ccSmrg dpy->xcb->event_waiter = 0; 41288de56ccSmrg ConditionBroadcast(dpy, dpy->xcb->event_notify); 41388de56ccSmrg if(!event) 41488de56ccSmrg _XIOError(dpy); 41588de56ccSmrg dpy->xcb->next_event = event; 41688de56ccSmrg } 41788de56ccSmrg 41888de56ccSmrg /* We've established most of the conditions for 41988de56ccSmrg * poll_for_response to return non-NULL. The exceptions 42088de56ccSmrg * are connection shutdown, and finding that another 42188de56ccSmrg * thread is waiting for the next reply we'd like to 42288de56ccSmrg * process. */ 42388de56ccSmrg 42488de56ccSmrg response = poll_for_response(dpy); 42588de56ccSmrg if(response) 42688de56ccSmrg handle_response(dpy, response, False); 42788de56ccSmrg else if(dpy->xcb->pending_requests->reply_waiter) 42888de56ccSmrg { /* need braces around ConditionWait */ 42988de56ccSmrg ConditionWait(dpy, dpy->xcb->reply_notify); 43088de56ccSmrg } 43188de56ccSmrg else 43288de56ccSmrg _XIOError(dpy); 43388de56ccSmrg } 43488de56ccSmrg 43588de56ccSmrg /* The preceding loop established that there is no 43688de56ccSmrg * event_waiter--unless we just called ConditionWait because of 43788de56ccSmrg * a reply_waiter, in which case another thread may have become 43888de56ccSmrg * the event_waiter while we slept unlocked. */ 43988de56ccSmrg if(!dpy->xcb->event_waiter) 44088de56ccSmrg while((response = poll_for_response(dpy))) 44188de56ccSmrg handle_response(dpy, response, False); 44288de56ccSmrg if(xcb_connection_has_error(dpy->xcb->connection)) 44388de56ccSmrg _XIOError(dpy); 4441ab64890Smrg} 4451ab64890Smrg 4461ab64890Smrg/* 4471ab64890Smrg * _XSend - Flush the buffer and send the client data. 32 bit word aligned 4481ab64890Smrg * transmission is used, if size is not 0 mod 4, extra bytes are transmitted. 4491ab64890Smrg * 4501ab64890Smrg * Note that the connection must not be read from once the data currently 4511ab64890Smrg * in the buffer has been written. 4521ab64890Smrg */ 4531ab64890Smrgvoid _XSend(Display *dpy, const char *data, long size) 4541ab64890Smrg{ 45561b2299dSmrg static const xReq dummy_request; 45661b2299dSmrg static char const pad[3]; 45761b2299dSmrg struct iovec vec[3]; 45861b2299dSmrg uint64_t requests; 45961b2299dSmrg _XExtension *ext; 4601ab64890Smrg xcb_connection_t *c = dpy->xcb->connection; 4611ab64890Smrg if(dpy->flags & XlibDisplayIOError) 4621ab64890Smrg return; 4631ab64890Smrg 46461b2299dSmrg if(dpy->bufptr == dpy->buffer && !size) 46561b2299dSmrg return; 4661ab64890Smrg 46761b2299dSmrg /* iff we asked XCB to set aside errors, we must pick those up 46861b2299dSmrg * eventually. iff there are async handlers, we may have just 46961b2299dSmrg * issued requests that will generate replies. in either case, 47061b2299dSmrg * we need to remember to check later. */ 47161b2299dSmrg if(dpy->xcb->event_owner != XlibOwnsEventQueue || dpy->async_handlers) 47261b2299dSmrg { 4732e9c7c8cSmrg uint64_t sequence; 47488de56ccSmrg for(sequence = dpy->xcb->last_flushed + 1; sequence <= dpy->request; ++sequence) 47588de56ccSmrg append_pending_request(dpy, sequence); 47661b2299dSmrg } 47761b2299dSmrg requests = dpy->request - dpy->xcb->last_flushed; 47861b2299dSmrg dpy->xcb->last_flushed = dpy->request; 4791ab64890Smrg 48061b2299dSmrg vec[0].iov_base = dpy->buffer; 48161b2299dSmrg vec[0].iov_len = dpy->bufptr - dpy->buffer; 482eb411b4bSmrg vec[1].iov_base = (char *)data; 48361b2299dSmrg vec[1].iov_len = size; 484eb411b4bSmrg vec[2].iov_base = (char *)pad; 48561b2299dSmrg vec[2].iov_len = -size & 3; 48661b2299dSmrg 48761b2299dSmrg for(ext = dpy->flushes; ext; ext = ext->next_flush) 48861b2299dSmrg { 48961b2299dSmrg int i; 49061b2299dSmrg for(i = 0; i < 3; ++i) 49161b2299dSmrg if(vec[i].iov_len) 49261b2299dSmrg ext->before_flush(dpy, &ext->codes, vec[i].iov_base, vec[i].iov_len); 49361b2299dSmrg } 4941ab64890Smrg 49561b2299dSmrg if(xcb_writev(c, vec, 3, requests) < 0) 49661b2299dSmrg _XIOError(dpy); 49761b2299dSmrg dpy->bufptr = dpy->buffer; 49861b2299dSmrg dpy->last_req = (char *) &dummy_request; 4991ab64890Smrg 5001ab64890Smrg check_internal_connections(dpy); 5011ab64890Smrg 50261b2299dSmrg _XSetSeqSyncFunction(dpy); 5031ab64890Smrg} 5041ab64890Smrg 5051ab64890Smrg/* 5061ab64890Smrg * _XFlush - Flush the X request buffer. If the buffer is empty, no 5071ab64890Smrg * action is taken. 5081ab64890Smrg */ 5091ab64890Smrgvoid _XFlush(Display *dpy) 5101ab64890Smrg{ 51161b2299dSmrg require_socket(dpy); 51261b2299dSmrg _XSend(dpy, NULL, 0); 5131ab64890Smrg 5141ab64890Smrg _XEventsQueued(dpy, QueuedAfterReading); 5151ab64890Smrg} 5161ab64890Smrg 51761b2299dSmrgstatic const XID inval_id = ~0UL; 51861b2299dSmrg 51988de56ccSmrgvoid _XIDHandler(Display *dpy) 5201ab64890Smrg{ 52188de56ccSmrg if (dpy->xcb->next_xid == inval_id) 52288de56ccSmrg _XAllocIDs(dpy, &dpy->xcb->next_xid, 1); 5231ab64890Smrg} 5241ab64890Smrg 5251ab64890Smrg/* _XAllocID - resource ID allocation routine. */ 5261ab64890SmrgXID _XAllocID(Display *dpy) 5271ab64890Smrg{ 5281ab64890Smrg XID ret = dpy->xcb->next_xid; 52961b2299dSmrg assert (ret != inval_id); 53061b2299dSmrg dpy->xcb->next_xid = inval_id; 53161b2299dSmrg _XSetPrivSyncFunction(dpy); 5321ab64890Smrg return ret; 5331ab64890Smrg} 5341ab64890Smrg 5351ab64890Smrg/* _XAllocIDs - multiple resource ID allocation routine. */ 5361ab64890Smrgvoid _XAllocIDs(Display *dpy, XID *ids, int count) 5371ab64890Smrg{ 5381ab64890Smrg int i; 53961b2299dSmrg#ifdef XTHREADS 54061b2299dSmrg if (dpy->lock) 54161b2299dSmrg (*dpy->lock->user_lock_display)(dpy); 54261b2299dSmrg UnlockDisplay(dpy); 54361b2299dSmrg#endif 5441ab64890Smrg for (i = 0; i < count; i++) 5451ab64890Smrg ids[i] = xcb_generate_id(dpy->xcb->connection); 54661b2299dSmrg#ifdef XTHREADS 54788de56ccSmrg InternalLockDisplay(dpy, /* don't skip user locks */ 0); 54861b2299dSmrg if (dpy->lock) 54961b2299dSmrg (*dpy->lock->user_unlock_display)(dpy); 55061b2299dSmrg#endif 5511ab64890Smrg} 5521ab64890Smrg 5531ab64890Smrgstatic void _XFreeReplyData(Display *dpy, Bool force) 5541ab64890Smrg{ 5551ab64890Smrg if(!force && dpy->xcb->reply_consumed < dpy->xcb->reply_length) 5561ab64890Smrg return; 5571ab64890Smrg free(dpy->xcb->reply_data); 55861b2299dSmrg dpy->xcb->reply_data = NULL; 5591ab64890Smrg} 5601ab64890Smrg 5611ab64890Smrg/* 5621ab64890Smrg * _XReply - Wait for a reply packet and copy its contents into the 5631ab64890Smrg * specified rep. 5641ab64890Smrg * extra: number of 32-bit words expected after the reply 5651ab64890Smrg * discard: should I discard data following "extra" words? 5661ab64890Smrg */ 5671ab64890SmrgStatus _XReply(Display *dpy, xReply *rep, int extra, Bool discard) 5681ab64890Smrg{ 5691ab64890Smrg xcb_generic_error_t *error; 5701ab64890Smrg xcb_connection_t *c = dpy->xcb->connection; 5711ab64890Smrg char *reply; 5721ab64890Smrg PendingRequest *current; 5731ab64890Smrg 5746cc2b21fSmrg if (dpy->xcb->reply_data) 5756cc2b21fSmrg throw_extlib_fail_assert("Extra reply data still left in queue", 5766cc2b21fSmrg xcb_xlib_extra_reply_data_left); 5771ab64890Smrg 5781ab64890Smrg if(dpy->flags & XlibDisplayIOError) 5791ab64890Smrg return 0; 5801ab64890Smrg 58161b2299dSmrg _XSend(dpy, NULL, 0); 58288de56ccSmrg if(dpy->xcb->pending_requests_tail && dpy->xcb->pending_requests_tail->sequence == dpy->request) 58388de56ccSmrg current = dpy->xcb->pending_requests_tail; 58488de56ccSmrg else 58588de56ccSmrg current = append_pending_request(dpy, dpy->request); 58688de56ccSmrg /* Don't let any other thread get this reply. */ 58788de56ccSmrg current->reply_waiter = 1; 58888de56ccSmrg 58988de56ccSmrg while(1) 59088de56ccSmrg { 59188de56ccSmrg PendingRequest *req = dpy->xcb->pending_requests; 59288de56ccSmrg xcb_generic_reply_t *response; 5931ab64890Smrg 59488de56ccSmrg if(req != current && req->reply_waiter) 59588de56ccSmrg { 59688de56ccSmrg ConditionWait(dpy, dpy->xcb->reply_notify); 59788de56ccSmrg /* Another thread got this reply. */ 59888de56ccSmrg continue; 59988de56ccSmrg } 60088de56ccSmrg req->reply_waiter = 1; 60188de56ccSmrg UnlockDisplay(dpy); 60288de56ccSmrg response = xcb_wait_for_reply(c, req->sequence, &error); 60357f47464Smrg /* Any user locks on another thread must have been taken 60457f47464Smrg * while we slept in xcb_wait_for_reply. Classic Xlib 60557f47464Smrg * ignored those user locks in this case, so we do too. */ 60657f47464Smrg InternalLockDisplay(dpy, /* ignore user locks */ 1); 60788de56ccSmrg 60888de56ccSmrg /* We have the response we're looking for. Now, before 60988de56ccSmrg * letting anyone else process this sequence number, we 61088de56ccSmrg * need to process any events that should have come 61188de56ccSmrg * earlier. */ 61288de56ccSmrg 61388de56ccSmrg if(dpy->xcb->event_owner == XlibOwnsEventQueue) 61488de56ccSmrg { 61588de56ccSmrg xcb_generic_reply_t *event; 61688de56ccSmrg /* If some thread is already waiting for events, 61788de56ccSmrg * it will get the first one. That thread must 61888de56ccSmrg * process that event before we can continue. */ 61988de56ccSmrg /* FIXME: That event might be after this reply, 62088de56ccSmrg * and might never even come--or there might be 62188de56ccSmrg * multiple threads trying to get events. */ 62288de56ccSmrg while(dpy->xcb->event_waiter) 62388de56ccSmrg { /* need braces around ConditionWait */ 62488de56ccSmrg ConditionWait(dpy, dpy->xcb->event_notify); 62588de56ccSmrg } 62688de56ccSmrg while((event = poll_for_event(dpy))) 62788de56ccSmrg handle_response(dpy, event, True); 62888de56ccSmrg } 62988de56ccSmrg 63088de56ccSmrg req->reply_waiter = 0; 63188de56ccSmrg ConditionBroadcast(dpy, dpy->xcb->reply_notify); 6326cc2b21fSmrg if(XLIB_SEQUENCE_COMPARE(req->sequence, >, dpy->request)) { 6336cc2b21fSmrg throw_thread_fail_assert("Unknown sequence number " 6346cc2b21fSmrg "while processing reply", 6356cc2b21fSmrg xcb_xlib_threads_sequence_lost); 6366cc2b21fSmrg } 63788de56ccSmrg dpy->last_request_read = req->sequence; 63888de56ccSmrg if(!response) 63988de56ccSmrg dequeue_pending_request(dpy, req); 64088de56ccSmrg 64188de56ccSmrg if(req == current) 64288de56ccSmrg { 64388de56ccSmrg reply = (char *) response; 64488de56ccSmrg break; 64588de56ccSmrg } 64688de56ccSmrg 64788de56ccSmrg if(error) 64888de56ccSmrg handle_response(dpy, (xcb_generic_reply_t *) error, True); 64988de56ccSmrg else if(response) 65088de56ccSmrg handle_response(dpy, response, True); 65188de56ccSmrg } 6521ab64890Smrg check_internal_connections(dpy); 65388de56ccSmrg 65488de56ccSmrg if(dpy->xcb->next_event && dpy->xcb->next_event->response_type == X_Error) 65588de56ccSmrg { 65688de56ccSmrg xcb_generic_event_t *event = dpy->xcb->next_event; 65788de56ccSmrg unsigned long event_sequence = dpy->last_request_read; 65888de56ccSmrg widen(&event_sequence, event->full_sequence); 65961234c2cSmrg if(event_sequence == dpy->last_request_read) 66088de56ccSmrg { 66188de56ccSmrg error = (xcb_generic_error_t *) event; 66288de56ccSmrg dpy->xcb->next_event = NULL; 66388de56ccSmrg } 66488de56ccSmrg } 6651ab64890Smrg 6661ab64890Smrg if(error) 6671ab64890Smrg { 6681ab64890Smrg int ret_code; 6691ab64890Smrg 6701ab64890Smrg /* Xlib is evil and assumes that even errors will be 6711ab64890Smrg * copied into rep. */ 6721ab64890Smrg memcpy(rep, error, 32); 6731ab64890Smrg 6741ab64890Smrg /* do not die on "no such font", "can't allocate", 6751ab64890Smrg "can't grab" failures */ 67688de56ccSmrg switch(error->error_code) 6771ab64890Smrg { 6781ab64890Smrg case BadName: 67988de56ccSmrg switch(error->major_code) 6801ab64890Smrg { 6811ab64890Smrg case X_LookupColor: 6821ab64890Smrg case X_AllocNamedColor: 68361b2299dSmrg free(error); 6841ab64890Smrg return 0; 6851ab64890Smrg } 6861ab64890Smrg break; 6871ab64890Smrg case BadFont: 68888de56ccSmrg if(error->major_code == X_QueryFont) { 68961b2299dSmrg free(error); 6901ab64890Smrg return 0; 69161b2299dSmrg } 6921ab64890Smrg break; 6931ab64890Smrg case BadAlloc: 6941ab64890Smrg case BadAccess: 69561b2299dSmrg free(error); 6961ab64890Smrg return 0; 6971ab64890Smrg } 6981ab64890Smrg 69988de56ccSmrg ret_code = handle_error(dpy, (xError *) error, True); 70061b2299dSmrg free(error); 70188de56ccSmrg return ret_code; 7021ab64890Smrg } 7031ab64890Smrg 7041ab64890Smrg /* it's not an error, but we don't have a reply, so it's an I/O 7051ab64890Smrg * error. */ 7061ab64890Smrg if(!reply) 7071ab64890Smrg { 7081ab64890Smrg _XIOError(dpy); 7091ab64890Smrg return 0; 7101ab64890Smrg } 7111ab64890Smrg 7121ab64890Smrg /* there's no error and we have a reply. */ 7131ab64890Smrg dpy->xcb->reply_data = reply; 7141ab64890Smrg dpy->xcb->reply_consumed = sizeof(xReply) + (extra * 4); 7151ab64890Smrg dpy->xcb->reply_length = sizeof(xReply); 7161ab64890Smrg if(dpy->xcb->reply_data[0] == 1) 7171ab64890Smrg dpy->xcb->reply_length += (((xcb_generic_reply_t *) dpy->xcb->reply_data)->length * 4); 7181ab64890Smrg 7191ab64890Smrg /* error: Xlib asks too much. give them what we can anyway. */ 7201ab64890Smrg if(dpy->xcb->reply_length < dpy->xcb->reply_consumed) 7211ab64890Smrg dpy->xcb->reply_consumed = dpy->xcb->reply_length; 7221ab64890Smrg 7231ab64890Smrg memcpy(rep, dpy->xcb->reply_data, dpy->xcb->reply_consumed); 7241ab64890Smrg _XFreeReplyData(dpy, discard); 7251ab64890Smrg return 1; 7261ab64890Smrg} 7271ab64890Smrg 7281ab64890Smrgint _XRead(Display *dpy, char *data, long size) 7291ab64890Smrg{ 7301ab64890Smrg assert(size >= 0); 7311ab64890Smrg if(size == 0) 7321ab64890Smrg return 0; 7336cc2b21fSmrg if(dpy->xcb->reply_data == NULL || 7346cc2b21fSmrg dpy->xcb->reply_consumed + size > dpy->xcb->reply_length) 7356cc2b21fSmrg throw_extlib_fail_assert("Too much data requested from _XRead", 7366cc2b21fSmrg xcb_xlib_too_much_data_requested); 7371ab64890Smrg memcpy(data, dpy->xcb->reply_data + dpy->xcb->reply_consumed, size); 7381ab64890Smrg dpy->xcb->reply_consumed += size; 7391ab64890Smrg _XFreeReplyData(dpy, False); 7401ab64890Smrg return 0; 7411ab64890Smrg} 7421ab64890Smrg 7431ab64890Smrg/* 7441ab64890Smrg * _XReadPad - Read bytes from the socket taking into account incomplete 7451ab64890Smrg * reads. If the number of bytes is not 0 mod 4, read additional pad 7461ab64890Smrg * bytes. 7471ab64890Smrg */ 7481ab64890Smrgvoid _XReadPad(Display *dpy, char *data, long size) 7491ab64890Smrg{ 7501ab64890Smrg _XRead(dpy, data, size); 7511ab64890Smrg dpy->xcb->reply_consumed += -size & 3; 7521ab64890Smrg _XFreeReplyData(dpy, False); 7531ab64890Smrg} 7541ab64890Smrg 7551ab64890Smrg/* Read and discard "n" 8-bit bytes of data */ 7561ab64890Smrgvoid _XEatData(Display *dpy, unsigned long n) 7571ab64890Smrg{ 7581ab64890Smrg dpy->xcb->reply_consumed += n; 7591ab64890Smrg _XFreeReplyData(dpy, False); 7601ab64890Smrg} 761eb411b4bSmrg 762eb411b4bSmrg/* 763eb411b4bSmrg * Read and discard "n" 32-bit words of data 764eb411b4bSmrg * Matches the units of the length field in X protocol replies, and provides 765eb411b4bSmrg * a single implementation of overflow checking to avoid having to replicate 766eb411b4bSmrg * those checks in every caller. 767eb411b4bSmrg */ 768eb411b4bSmrgvoid _XEatDataWords(Display *dpy, unsigned long n) 769eb411b4bSmrg{ 770eb411b4bSmrg if (n < ((INT_MAX - dpy->xcb->reply_consumed) >> 2)) 771eb411b4bSmrg dpy->xcb->reply_consumed += (n << 2); 772eb411b4bSmrg else 773eb411b4bSmrg /* Overflow would happen, so just eat the rest of the reply */ 774eb411b4bSmrg dpy->xcb->reply_consumed = dpy->xcb->reply_length; 775eb411b4bSmrg _XFreeReplyData(dpy, False); 776eb411b4bSmrg} 777