[PATCH v2 2/4] dbus: add dbus-match helpers

David Herrmann dh.herrmann at gmail.com
Tue Oct 22 00:28:07 CEST 2013


These helpers simplify adding dbus-matches by allowing var-arg arguments
to assemble the matching rules.
---
 src/dbus.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/dbus.h | 40 ++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)

diff --git a/src/dbus.c b/src/dbus.c
index 4de8c9c..a1abbd5 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -334,3 +334,71 @@ void weston_dbus_close(DBusConnection *c, struct wl_event_source *ctx)
 	dbus_connection_close(c);
 	dbus_connection_unref(c);
 }
+
+int weston_dbus_add_match(DBusConnection *c, const char *format, ...)
+{
+	DBusError err;
+	int r;
+	va_list list;
+	char *str;
+
+	va_start(list, format);
+	r = vasprintf(&str, format, list);
+	va_end(list);
+
+	if (r < 0)
+		return -ENOMEM;
+
+	dbus_error_init(&err);
+	dbus_bus_add_match(c, str, &err);
+	free(str);
+	if (dbus_error_is_set(&err)) {
+		dbus_error_free(&err);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int weston_dbus_add_match_signal(DBusConnection *c, const char *sender,
+				 const char *iface, const char *member,
+				 const char *path)
+{
+	return weston_dbus_add_match(c,
+				     "type='signal',"
+				     "sender='%s',"
+				     "interface='%s',"
+				     "member='%s',"
+				     "path='%s'",
+				     sender, iface, member, path);
+}
+
+void weston_dbus_remove_match(DBusConnection *c, const char *format, ...)
+{
+	int r;
+	va_list list;
+	char *str;
+
+	va_start(list, format);
+	r = vasprintf(&str, format, list);
+	va_end(list);
+
+	if (r < 0)
+		return;
+
+	dbus_bus_remove_match(c, str, NULL);
+	free(str);
+}
+
+void weston_dbus_remove_match_signal(DBusConnection *c, const char *sender,
+				     const char *iface, const char *member,
+				     const char *path)
+{
+	return weston_dbus_remove_match(c,
+					"type='signal',"
+					"sender='%s',"
+					"interface='%s',"
+					"member='%s',"
+					"path='%s'",
+					sender, iface, member, path);
+}
diff --git a/src/dbus.h b/src/dbus.h
index 90ab8f6..06fe762 100644
--- a/src/dbus.h
+++ b/src/dbus.h
@@ -62,6 +62,46 @@ int weston_dbus_open(struct wl_event_loop *loop, DBusBusType bus,
  */
 void weston_dbus_close(DBusConnection *c, struct wl_event_source *ctx);
 
+/*
+ * weston_dbus_add_match() - Add dbus match
+ *
+ * Configure a dbus-match on the given dbus-connection. This match is saved
+ * on the dbus-server as long as the connection is open. See dbus-manual
+ * for information. Compared to the dbus_bus_add_match() this allows a
+ * var-arg formatted match-string.
+ */
+int weston_dbus_add_match(DBusConnection *c, const char *format, ...);
+
+/*
+ * weston_dbus_add_match_signal() - Add dbus signal match
+ *
+ * Same as weston_dbus_add_match() but does the dbus-match formatting for
+ * signals internally.
+ */
+int weston_dbus_add_match_signal(DBusConnection *c, const char *sender,
+				 const char *iface, const char *member,
+				 const char *path);
+
+/*
+ * weston_dbus_remove_match() - Remove dbus match
+ *
+ * Remove a previously configured dbus-match from the dbus server. There is
+ * no need to remove dbus-matches if you close the connection, anyway.
+ * Compared to dbus_bus_remove_match() this allows a var-arg formatted
+ * match string.
+ */
+void weston_dbus_remove_match(DBusConnection *c, const char *format, ...);
+
+/*
+ * weston_dbus_remove_match_signal() - Remove dbus signal match
+ *
+ * Same as weston_dbus_remove_match() but does the dbus-match formatting for
+ * signals internally.
+ */
+void weston_dbus_remove_match_signal(DBusConnection *c, const char *sender,
+				     const char *iface, const char *member,
+				     const char *path);
+
 #endif /* HAVE_DBUS */
 
 #endif // _WESTON_DBUS_H_
-- 
1.8.4.1



More information about the wayland-devel mailing list