summaryrefslogtreecommitdiff
path: root/src/FreeEventData.c
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2009-06-26 11:27:43 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2009-07-12 16:09:57 +1000
commit554f755e5545f63d3c8f299297927238da155773 (patch)
tree047773e3676995a5003edbba0b6941844d2967a5 /src/FreeEventData.c
parentd7675cb8fa7155e7aff1459636a117a97aa1bf28 (diff)
downloadxorg-lib-libX11-554f755e5545f63d3c8f299297927238da155773.tar.gz
Add generic event cookie handling to libX11.
Generic events require more bytes than Xlib provides in the standard XEvent. Memory allocated by the extension and stored as pointers inside the event is prone to leak by simple 'while (1) { XNextEvent(...); }' loops. This patch adds cookie handling for generic events. Extensions may register a cookie handler in addition to the normal event vectors. If an extension has registered a cookie handler, _all_ generic events for this extensions must be handled through cookies. Otherwise, the default event handler is used. The cookie handler must return an XGenericEventCookie with a pointer to the data.The rest of the event (type, serialNumber, etc.) are to be filled as normal. When a client retrieves such a cookie event, the data is stored in an internal queue (the 'cookiejar'). This data is freed on the next call to XNextEvent(). New extension interfaces: XESetWireToEventCookie(display, extension_number, cookie_handler) Where cookie_handler must set cookie->data. The data pointer is of arbitray size and type but must be a single memory block. This memory block represents the actual extension's event. New client interfaces: XGetEventData(display, *cookie); XFreeEventData(display, *cookie); If the client needs the actual event data, it must call XGetEventData() with the cookie. This returns the data pointer (and removes it from the cookie jar) and the client is then responsible for freeing the event with XFreeEventData(). It is safe to call either function with a non-cookie event. Events unclaimed or not handled by the XGetEventData() are cleaned up automatically. Example client code: XEvent event; XGenericEventCookie *cookie = &ev; XNextEvent(display, &event); if (XGetEventData(display, cookie)) { XIEvent *xievent = cookie->data; ... } else if (cookie->type == GenericEvent) { /* handle generic event */ } else { /* handle extension/core event */ } XFreeEventData(display, cookie); Cookies are not multi-threading safe. Clients that use XGetEventData() must lock between XNextEvent and XGetEventData to avoid other threads freeing cookies. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'src/FreeEventData.c')
-rw-r--r--src/FreeEventData.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/FreeEventData.c b/src/FreeEventData.c
new file mode 100644
index 00000000..e2d40b0d
--- /dev/null
+++ b/src/FreeEventData.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright © 2009 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include "Xlibint.h"
+
+void
+XFreeEventData(Display *dpy, XGenericEventCookie *event)
+{
+
+ if (_XIsEventCookie(dpy, (XEvent*)event) && event->data)
+ {
+ XFree(event->data);
+ event->data = NULL;
+ event->cookie = 0;
+ }
+ return;
+}
+