1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * Except as contained in this notice, the names of the authors or their
21 * institutions shall not be used in advertising or otherwise to promote the
22 * sale, use or other dealings in this Software without prior written
23 * authorization from the authors.
26 /* Stuff that reads stuff from the server. */
42 #include <sys/select.h>
43 #include <sys/socket.h>
47 #include "xcb_windefs.h"
52 #define XCB_XGE_EVENT 35
54 /* required for compiling for Win32 using MinGW */
60 xcb_generic_event_t *event;
61 struct event_list *next;
66 struct reply_list *next;
69 typedef struct pending_reply {
70 uint64_t first_request;
71 uint64_t last_request;
72 enum workarounds workaround;
74 struct pending_reply *next;
77 typedef struct reader_list {
80 struct reader_list *next;
83 static int read_packet(xcb_connection_t *c)
85 xcb_generic_reply_t genrep;
87 int eventlength = 0; /* length after first 32 bytes for GenericEvents */
89 pending_reply *pend = 0;
90 struct event_list *event;
92 /* Wait for there to be enough data for us to read a whole packet */
93 if(c->in.queue_len < length)
96 /* Get the response type, length, and sequence number. */
97 memcpy(&genrep, c->in.queue, sizeof(genrep));
99 /* Compute 32-bit sequence number of this packet. */
100 if((genrep.response_type & 0x7f) != XCB_KEYMAP_NOTIFY)
102 uint64_t lastread = c->in.request_read;
103 c->in.request_read = (lastread & UINT64_C(0xffffffffffff0000)) | genrep.sequence;
104 if(XCB_SEQUENCE_COMPARE(c->in.request_read, <, lastread))
105 c->in.request_read += 0x10000;
106 if(XCB_SEQUENCE_COMPARE(c->in.request_read, >, c->in.request_expected))
107 c->in.request_expected = c->in.request_read;
109 if(c->in.request_read != lastread)
111 if(c->in.current_reply)
113 _xcb_map_put(c->in.replies, lastread, c->in.current_reply);
114 c->in.current_reply = 0;
115 c->in.current_reply_tail = &c->in.current_reply;
117 c->in.request_completed = c->in.request_read - 1;
120 while(c->in.pending_replies &&
121 c->in.pending_replies->workaround != WORKAROUND_EXTERNAL_SOCKET_OWNER &&
122 XCB_SEQUENCE_COMPARE (c->in.pending_replies->last_request, <=, c->in.request_completed))
124 pending_reply *oldpend = c->in.pending_replies;
125 c->in.pending_replies = oldpend->next;
127 c->in.pending_replies_tail = &c->in.pending_replies;
131 if(genrep.response_type == XCB_ERROR)
132 c->in.request_completed = c->in.request_read;
135 if(genrep.response_type == XCB_ERROR || genrep.response_type == XCB_REPLY)
137 pend = c->in.pending_replies;
139 !(XCB_SEQUENCE_COMPARE(pend->first_request, <=, c->in.request_read) &&
140 (pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER ||
141 XCB_SEQUENCE_COMPARE(c->in.request_read, <=, pend->last_request))))
145 /* For reply packets, check that the entire packet is available. */
146 if(genrep.response_type == XCB_REPLY)
148 if(pend && pend->workaround == WORKAROUND_GLX_GET_FB_CONFIGS_BUG)
150 uint32_t *p = (uint32_t *) c->in.queue;
151 genrep.length = p[2] * p[3] * 2;
153 length += genrep.length * 4;
156 /* XGE events may have sizes > 32 */
157 if (genrep.response_type == XCB_XGE_EVENT)
158 eventlength = genrep.length * 4;
160 buf = malloc(length + eventlength +
161 (genrep.response_type == XCB_REPLY ? 0 : sizeof(uint32_t)));
164 _xcb_conn_shutdown(c);
168 if(_xcb_in_read_block(c, buf, length) <= 0)
174 /* pull in XGE event data if available, append after event struct */
177 if(_xcb_in_read_block(c, &((xcb_generic_event_t*)buf)[1], eventlength) <= 0)
184 if(pend && (pend->flags & XCB_REQUEST_DISCARD_REPLY))
190 if(genrep.response_type != XCB_REPLY)
191 ((xcb_generic_event_t *) buf)->full_sequence = c->in.request_read;
193 /* reply, or checked error */
194 if( genrep.response_type == XCB_REPLY ||
195 (genrep.response_type == XCB_ERROR && pend && (pend->flags & XCB_REQUEST_CHECKED)))
198 struct reply_list *cur = malloc(sizeof(struct reply_list));
201 _xcb_conn_shutdown(c);
207 *c->in.current_reply_tail = cur;
208 c->in.current_reply_tail = &cur->next;
209 for(reader = c->in.readers;
211 XCB_SEQUENCE_COMPARE(reader->request, <=, c->in.request_read);
212 reader = reader->next)
214 if(reader->request == c->in.request_read)
216 pthread_cond_signal(reader->data);
223 /* event, or unchecked error */
224 event = malloc(sizeof(struct event_list));
227 _xcb_conn_shutdown(c);
233 *c->in.events_tail = event;
234 c->in.events_tail = &event->next;
235 pthread_cond_signal(&c->in.event_cond);
236 return 1; /* I have something for you... */
239 static xcb_generic_event_t *get_event(xcb_connection_t *c)
241 struct event_list *cur = c->in.events;
242 xcb_generic_event_t *ret;
246 c->in.events = cur->next;
248 c->in.events_tail = &c->in.events;
253 static void free_reply_list(struct reply_list *head)
257 struct reply_list *cur = head;
264 static int read_block(const int fd, void *buf, const ssize_t len)
269 int ret = recv(fd, ((char *) buf) + done, len - done,MSG_WAITALL);
273 if(ret < 0 && errno == EAGAIN)
275 if(ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
284 ret = poll(&pfd, 1, -1);
285 } while (ret == -1 && errno == EINTR);
291 /* Initializing errno here makes sure that for Win32 this loop will execute only once */
294 ret = select(fd + 1, &fds, 0, 0, 0);
295 } while (ret == -1 && errno == EINTR);
296 #endif /* USE_POLL */
304 static int poll_for_reply(xcb_connection_t *c, uint64_t request, void **reply, xcb_generic_error_t **error)
306 struct reply_list *head;
308 /* If an error occurred when issuing the request, fail immediately. */
311 /* We've read requests past the one we want, so if it has replies we have
312 * them all and they're in the replies map. */
313 else if(XCB_SEQUENCE_COMPARE(request, <, c->in.request_read))
315 head = _xcb_map_remove(c->in.replies, request);
316 if(head && head->next)
317 _xcb_map_put(c->in.replies, request, head->next);
319 /* We're currently processing the responses to the request we want, and we
320 * have a reply ready to return. So just return it without blocking. */
321 else if(request == c->in.request_read && c->in.current_reply)
323 head = c->in.current_reply;
324 c->in.current_reply = head->next;
326 c->in.current_reply_tail = &c->in.current_reply;
328 /* We know this request can't have any more replies, and we've already
329 * established it doesn't have a reply now. Don't bother blocking. */
330 else if(request == c->in.request_completed)
332 /* We may have more replies on the way for this request: block until we're
343 if(((xcb_generic_reply_t *) head->reply)->response_type == XCB_ERROR)
346 *error = head->reply;
351 *reply = head->reply;
359 static void *wait_for_reply(xcb_connection_t *c, uint64_t request, xcb_generic_error_t **e)
363 /* If this request has not been written yet, write it. */
364 if(c->out.return_socket || _xcb_out_flush_to(c, request))
366 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
368 reader_list **prev_reader;
370 for(prev_reader = &c->in.readers;
372 XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request);
373 prev_reader = &(*prev_reader)->next)
377 reader.request = request;
379 reader.next = *prev_reader;
380 *prev_reader = &reader;
382 while(!poll_for_reply(c, request, &ret, e))
383 if(!_xcb_conn_wait(c, &cond, 0, 0))
386 for(prev_reader = &c->in.readers;
388 XCB_SEQUENCE_COMPARE((*prev_reader)->request, <=, request);
389 prev_reader = &(*prev_reader)->next)
391 if(*prev_reader == &reader)
393 *prev_reader = (*prev_reader)->next;
397 pthread_cond_destroy(&cond);
400 _xcb_in_wake_up_next_reader(c);
404 static uint64_t widen(xcb_connection_t *c, unsigned int request)
406 uint64_t widened_request = (c->out.request & UINT64_C(0xffffffff00000000)) | request;
407 if(widened_request > c->out.request)
408 widened_request -= UINT64_C(1) << 32;
409 return widened_request;
412 /* Public interface */
414 void *xcb_wait_for_reply(xcb_connection_t *c, unsigned int request, xcb_generic_error_t **e)
422 pthread_mutex_lock(&c->iolock);
423 ret = wait_for_reply(c, widen(c, request), e);
424 pthread_mutex_unlock(&c->iolock);
428 static void insert_pending_discard(xcb_connection_t *c, pending_reply **prev_next, uint64_t seq)
431 pend = malloc(sizeof(*pend));
434 _xcb_conn_shutdown(c);
438 pend->first_request = seq;
439 pend->last_request = seq;
440 pend->workaround = 0;
441 pend->flags = XCB_REQUEST_DISCARD_REPLY;
442 pend->next = *prev_next;
446 c->in.pending_replies_tail = &pend->next;
449 static void discard_reply(xcb_connection_t *c, uint64_t request)
452 pending_reply **prev_pend;
454 /* Free any replies or errors that we've already read. Stop if
455 * xcb_wait_for_reply would block or we've run out of replies. */
456 while(poll_for_reply(c, request, &reply, 0) && reply)
459 /* If we've proven there are no more responses coming, we're done. */
460 if(XCB_SEQUENCE_COMPARE(request, <=, c->in.request_completed))
463 /* Walk the list of pending requests. Mark the first match for deletion. */
464 for(prev_pend = &c->in.pending_replies; *prev_pend; prev_pend = &(*prev_pend)->next)
466 if(XCB_SEQUENCE_COMPARE((*prev_pend)->first_request, >, request))
469 if((*prev_pend)->first_request == request)
471 /* Pending reply found. Mark for discard: */
472 (*prev_pend)->flags |= XCB_REQUEST_DISCARD_REPLY;
477 /* Pending reply not found (likely due to _unchecked request). Create one: */
478 insert_pending_discard(c, prev_pend, request);
481 void xcb_discard_reply(xcb_connection_t *c, unsigned int sequence)
486 /* If an error occurred when issuing the request, fail immediately. */
490 pthread_mutex_lock(&c->iolock);
491 discard_reply(c, widen(c, sequence));
492 pthread_mutex_unlock(&c->iolock);
495 int xcb_poll_for_reply(xcb_connection_t *c, unsigned int request, void **reply, xcb_generic_error_t **error)
503 return 1; /* would not block */
506 pthread_mutex_lock(&c->iolock);
507 ret = poll_for_reply(c, widen(c, request), reply, error);
508 pthread_mutex_unlock(&c->iolock);
512 xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c)
514 xcb_generic_event_t *ret;
517 pthread_mutex_lock(&c->iolock);
518 /* get_event returns 0 on empty list. */
519 while(!(ret = get_event(c)))
520 if(!_xcb_conn_wait(c, &c->in.event_cond, 0, 0))
523 _xcb_in_wake_up_next_reader(c);
524 pthread_mutex_unlock(&c->iolock);
528 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
530 xcb_generic_event_t *ret = 0;
533 pthread_mutex_lock(&c->iolock);
534 /* FIXME: follow X meets Z architecture changes. */
536 if(!ret && _xcb_in_read(c)) /* _xcb_in_read shuts down the connection on error */
538 pthread_mutex_unlock(&c->iolock);
543 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
546 xcb_generic_error_t *ret = 0;
550 pthread_mutex_lock(&c->iolock);
551 request = widen(c, cookie.sequence);
552 if(XCB_SEQUENCE_COMPARE(request, >=, c->in.request_expected)
553 && XCB_SEQUENCE_COMPARE(request, >, c->in.request_completed))
555 _xcb_out_send_sync(c);
556 _xcb_out_flush_to(c, c->out.request);
558 reply = wait_for_reply(c, request, &ret);
560 pthread_mutex_unlock(&c->iolock);
564 /* Private interface */
566 int _xcb_in_init(_xcb_in *in)
568 if(pthread_cond_init(&in->event_cond, 0))
574 in->request_read = 0;
575 in->request_completed = 0;
577 in->replies = _xcb_map_new();
581 in->current_reply_tail = &in->current_reply;
582 in->events_tail = &in->events;
583 in->pending_replies_tail = &in->pending_replies;
588 void _xcb_in_destroy(_xcb_in *in)
590 pthread_cond_destroy(&in->event_cond);
591 free_reply_list(in->current_reply);
592 _xcb_map_delete(in->replies, (void (*)(void *)) free_reply_list);
595 struct event_list *e = in->events;
596 in->events = e->next;
600 while(in->pending_replies)
602 pending_reply *pend = in->pending_replies;
603 in->pending_replies = pend->next;
608 void _xcb_in_wake_up_next_reader(xcb_connection_t *c)
612 pthreadret = pthread_cond_signal(c->in.readers->data);
614 pthreadret = pthread_cond_signal(&c->in.event_cond);
615 assert(pthreadret == 0);
618 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags)
620 pending_reply *pend = malloc(sizeof(pending_reply));
621 assert(workaround != WORKAROUND_NONE || flags != 0);
624 _xcb_conn_shutdown(c);
627 pend->first_request = pend->last_request = request;
628 pend->workaround = workaround;
631 *c->in.pending_replies_tail = pend;
632 c->in.pending_replies_tail = &pend->next;
636 void _xcb_in_replies_done(xcb_connection_t *c)
638 struct pending_reply *pend;
639 if (c->in.pending_replies_tail != &c->in.pending_replies)
641 pend = container_of(c->in.pending_replies_tail, struct pending_reply, next);
642 if(pend->workaround == WORKAROUND_EXTERNAL_SOCKET_OWNER)
644 pend->last_request = c->out.request;
645 pend->workaround = WORKAROUND_NONE;
650 int _xcb_in_read(xcb_connection_t *c)
652 int n = recv(c->fd, c->in.queue + c->in.queue_len, sizeof(c->in.queue) - c->in.queue_len,MSG_WAITALL);
654 c->in.queue_len += n;
655 while(read_packet(c))
658 if((n > 0) || (n < 0 && errno == EAGAIN))
660 if((n > 0) || (n < 0 && WSAGetLastError() == WSAEWOULDBLOCK))
663 _xcb_conn_shutdown(c);
667 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int len)
669 int done = c->in.queue_len;
673 memcpy(buf, c->in.queue, done);
674 c->in.queue_len -= done;
675 memmove(c->in.queue, c->in.queue + done, c->in.queue_len);
679 int ret = read_block(c->fd, (char *) buf + done, len - done);
682 _xcb_conn_shutdown(c);