[PATCH v2] Add a macro to get the opcode number of a request given the interface

Neil Roberts neil at linux.intel.com
Tue May 20 04:09:22 PDT 2014


Here is a version two of the patch which moves the macro to
wayland-server.h and fixes a typo in the docs.

- Neil

------- >8 --------------- (use git am --scissors to automatically chop here)

This adds a macro called WL_REQUEST_OPCODE which takes the name of the
struct for the interface and the name of one of its members. It then
calculates the opcode number by dividing the offsetof the member by
the size of a function pointer. This assumes the interface struct only
contains function pointers and that they are in order of the opcodes.
---
 src/wayland-server.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/wayland-server.h b/src/wayland-server.h
index 7fc5b47..24cd53f 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -39,6 +39,18 @@ enum {
 	WL_EVENT_ERROR    = 0x08
 };
 
+/**
+ * Gets the opcode of a request from its interface struct
+ *
+ * Given the name of an interface struct and a request within it this
+ * will return the opcode number. For example it can be used like
+ * this:
+ *
+ * int opcode = WL_REQUEST_OPCODE(wl_region_interface, subtract);
+ */
+#define WL_REQUEST_OPCODE(interface, member) \
+	(offsetof(struct interface, member) / sizeof (void (*) (void)))
+
 struct wl_event_loop;
 struct wl_event_source;
 typedef int (*wl_event_loop_fd_func_t)(int fd, uint32_t mask, void *data);
-- 
1.9.0



More information about the wayland-devel mailing list