[Xcb] [PATCH libxcb 1/3] Introduce xcb_peek_for_event.

Rami Ylimäki rami.ylimaki at vincit.fi
Thu Oct 7 08:43:22 PDT 2010


It's sometimes useful to check the type and sequence number of next
event before actually extracting it from XCB buffers with
xcb_wait_for_event or xcb_poll_for_event. Xlib will use this function
to determine if it can continue handling a reply to a request or
whether it needs to handle events first.

Signed-off-by: Rami Ylimäki <rami.ylimaki at vincit.fi>
---
 src/xcb.h    |   15 +++++++++++++++
 src/xcb_in.c |   19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/xcb.h b/src/xcb.h
index 35d8768..b16c7d3 100644
--- a/src/xcb.h
+++ b/src/xcb.h
@@ -268,6 +268,21 @@ xcb_generic_event_t *xcb_wait_for_event(xcb_connection_t *c);
 xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c);
 
 /**
+ * @brief Returns header of next event from XCB buffers.
+ * @param[in] c: The connection to the X server.
+ * @param[out] e: Copy of the next event header.
+ * @return Zero if there are no buffered events, non-zero otherwise.
+ *
+ * XCB event buffer is examined for events and unchecked errors. If
+ * such a response exists, the header of that response is copied in
+ * the output parameter. The response is still left in XCB buffers and
+ * has to be extracted separately. This provides a fast way to check
+ * the type and sequence number of next event so that it is easy to
+ * determine whether the next response should be handled or not.
+ */
+int xcb_peek_for_event(xcb_connection_t *c, xcb_generic_event_t *e);
+
+/**
  * @brief Return the error for a request, or NULL if none can ever arrive.
  * @param c: The connection to the X server.
  * @param cookie: The request cookie.
diff --git a/src/xcb_in.c b/src/xcb_in.c
index 80f5523..8cfc5c9 100644
--- a/src/xcb_in.c
+++ b/src/xcb_in.c
@@ -237,6 +237,12 @@ static int read_packet(xcb_connection_t *c)
     return 1; /* I have something for you... */
 }
 
+static xcb_generic_event_t *peek_event(xcb_connection_t *c)
+{
+    struct event_list *cur = c->in.events;
+    return cur ? cur->event : 0;
+}
+
 static xcb_generic_event_t *get_event(xcb_connection_t *c)
 {
     struct event_list *cur = c->in.events;
@@ -567,6 +573,19 @@ xcb_generic_event_t *xcb_poll_for_event(xcb_connection_t *c)
     return ret;
 }
 
+int xcb_peek_for_event(xcb_connection_t *c, xcb_generic_event_t *e)
+{
+    xcb_generic_event_t *ret = 0;
+    if(!c->has_error)
+    {
+        pthread_mutex_lock(&c->iolock);
+        if ((ret = peek_event(c)))
+            *e = *ret;
+        pthread_mutex_unlock(&c->iolock);
+    }
+    return ret != 0;
+}
+
 xcb_generic_error_t *xcb_request_check(xcb_connection_t *c, xcb_void_cookie_t cookie)
 {
     /* FIXME: this could hold the lock to avoid syncing unnecessarily, but
-- 
1.6.3.3



More information about the Xcb mailing list