From 814d49f0b4998e4f9d2aa89741ad8acd699d706b Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Tue, 22 Oct 2013 00:28:07 +0200 Subject: [PATCH] dbus: add dbus-match helpers 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 4de8c9c4..a1abbd54 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 90ab8f61..06fe7621 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_