X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_in.c;h=b8107834c2fb965b813670927d11bee2cea58a8e;hb=b52790e8ed4bb077eabdeca803935d2910558acc;hp=fb551116541d259d92f23bbb2ac7b494cbf273f7;hpb=131e867fca5cda94e634af69214ad54e066ac871;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_in.c b/src/xcb_in.c index fb55111..b810783 100644 --- a/src/xcb_in.c +++ b/src/xcb_in.c @@ -25,6 +25,10 @@ /* Stuff that reads stuff from the server. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include @@ -51,11 +55,6 @@ #define XCB_REPLY 1 #define XCB_XGE_EVENT 35 -/* required for compiling for Win32 using MinGW */ -#ifndef MSG_WAITALL -#define MSG_WAITALL 0 -#endif - struct event_list { xcb_generic_event_t *event; struct event_list *next; @@ -80,6 +79,17 @@ typedef struct reader_list { struct reader_list *next; } reader_list; +static void remove_finished_readers(reader_list **prev_reader, uint64_t completed) +{ + while(*prev_reader && XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, completed)) + { + /* If you don't have what you're looking for now, you never + * will. Wake up and leave me alone. */ + pthread_cond_signal((*prev_reader)->data); + *prev_reader = (*prev_reader)->next; + } +} + static int read_packet(xcb_connection_t *c) { xcb_generic_reply_t genrep; @@ -130,6 +140,8 @@ static int read_packet(xcb_connection_t *c) if(genrep.response_type == XCB_ERROR) c->in.request_completed = c->in.request_read; + + remove_finished_readers(&c->in.readers, c->in.request_completed); } if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY) @@ -154,14 +166,14 @@ static int read_packet(xcb_connection_t *c) } /* XGE events may have sizes > 32 */ - if (genrep.response_type == XCB_XGE_EVENT) + if ((genrep.response_type & 0x7f) == XCB_XGE_EVENT) eventlength = genrep.length * 4; buf = malloc(length + eventlength + (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t))); if(!buf) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT); return 0; } @@ -194,11 +206,10 @@ static int read_packet(xcb_connection_t *c) if( genrep.response_type == XCB_REPLY || (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED))) { - reader_list *reader; struct reply_list *cur = malloc(sizeof(struct reply_list)); if(!cur) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT); free(buf); return 0; } @@ -206,17 +217,8 @@ static int read_packet(xcb_connection_t *c) cur->next = 0; *c->in.current_reply_tail = cur; c->in.current_reply_tail = &cur->next; - for(reader = c->in.readers; - reader && - XCB_SEQUENCE_COMPARE(reader->request, <=, c->in.request_read); - reader = reader->next) - { - pthread_cond_signal(reader->data); - if(reader->request == c->in.request_read) - { - break; - } - } + if(c->in.readers && c->in.readers->request == c->in.request_read) + pthread_cond_signal(c->in.readers->data); return 1; } @@ -224,7 +226,7 @@ static int read_packet(xcb_connection_t *c) event = malloc(sizeof(struct event_list)); if(!event) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT); free(buf); return 0; } @@ -266,7 +268,7 @@ static int read_block(const int fd, void *buf, const ssize_t len) int done = 0; while(done < len) { - int ret = recv(fd, ((char *) buf) + done, len - done,MSG_WAITALL); + int ret = recv(fd, ((char *) buf) + done, len - done, 0); if(ret > 0) done += ret; #ifndef _WIN32 @@ -430,7 +432,7 @@ static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_nex pend = malloc(sizeof(*pend)); if(!pend) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT); return; } @@ -524,7 +526,7 @@ xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c) return ret; } -xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c) +static xcb_generic_event_t *poll_for_next_event(xcb_connection_t *c, int queued) { xcb_generic_event_t *ret = 0; if(!c->has_error) @@ -532,13 +534,23 @@ xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c) pthread_mutex_lock(&c->iolock); /* FIXME: follow X meets Z architecture changes. */ ret = get_event(c); - if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */ + if(!ret && !queued && c->in.reading == 0 && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */ ret = get_event(c); pthread_mutex_unlock(&c->iolock); } return ret; } +xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c) +{ + return poll_for_next_event(c, 0); +} + +xcb_generic_event_t *xcb_poll_for_queued_event(xcb_connection_t *c) +{ + return poll_for_next_event(c, 1); +} + xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie) { uint64_t request; @@ -620,7 +632,7 @@ int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds assert(workaround != WORKAROUND_NONE || flags != 0); if(!pend) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_CLOSED_MEM_INSUFFICIENT); return 0; } pend->first_request = pend->last_request = request; @@ -648,7 +660,7 @@ void _xcb_in_replies_done(xcb_connection_t *c) int _xcb_in_read(xcb_connection_t *c) { - int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,MSG_WAITALL); + int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len, 0); if(n > 0) c->in.queue_len += n; while(read_packet(c)) @@ -659,7 +671,7 @@ int _xcb_in_read(xcb_connection_t *c) if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK)) #endif /* !_WIN32 */ return 1; - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_ERROR); return 0; } @@ -678,7 +690,7 @@ int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len) int ret = read_block(c->fd, (char *) buf + done, len - done); if(ret <= 0) { - _xcb_conn_shutdown(c); + _xcb_conn_shutdown(c, XCB_CONN_ERROR); return ret; } }