#include <assert.h>
#include "reserve-monitor.h"
+#include "reserve.h"
struct rm_monitor {
int ref;
"member='NameOwnerChanged'," \
"arg0='%s'"
+static unsigned get_busy(
+ DBusConnection *c,
+ const char *name_owner) {
+
+ const char *un;
+
+ if (!name_owner || !*name_owner)
+ return FALSE;
+
+ /* If we ourselves own the device, then don't consider this 'busy' */
+ if ((un = dbus_bus_get_unique_name(c)))
+ if (strcmp(name_owner, un) == 0)
+ return FALSE;
+
+ return TRUE;
+}
+
static DBusHandlerResult filter_handler(
DBusConnection *c,
DBusMessage *s,
goto invalid;
if (strcmp(name, m->service_name) == 0) {
- m->busy = !!(new && *new);
+ unsigned old_busy = m->busy;
- /* If we ourselves own the device, then don't consider this 'busy' */
- if (m->busy) {
- const char *un;
+ m->busy = get_busy(c, new);
- if ((un = dbus_bus_get_unique_name(c)))
- if (strcmp(new, un) == 0)
- m->busy = FALSE;
- }
-
- if (m->change_cb) {
+ if (m->busy != old_busy && m->change_cb) {
m->ref++;
m->change_cb(m);
rm_release(m);
int rm_watch(
rm_monitor **_m,
DBusConnection *connection,
- const char*device_name,
+ const char *device_name,
rm_change_cb_t change_cb,
DBusError *error) {
rm_monitor *m = NULL;
+ char *name_owner;
int r;
DBusError _error;
m->matching = 1;
- m->busy = dbus_bus_name_has_owner(m->connection, m->service_name, error);
-
- if (dbus_error_is_set(error)) {
- r = -EIO;
+ if ((r = rd_dbus_get_name_owner(m->connection, m->service_name, &name_owner, error)) < 0)
goto fail;
- }
+
+ m->busy = get_busy(m->connection, name_owner);
+ free(name_owner);
*_m = m;
return 0;