1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
4
Copyright 2009 Lennart Poettering
6
Permission is hereby granted, free of charge, to any person
7
obtaining a copy of this software and associated documentation files
8
(the "Software"), to deal in the Software without restriction,
9
including without limitation the rights to use, copy, modify, merge,
10
publish, distribute, sublicense, and/or sell copies of the Software,
11
and to permit persons to whom the Software is furnished to do so,
12
subject to the following conditions:
14
The above copyright notice and this permission notice shall be
15
included in all copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
#include "reserve-monitor.h"
44
DBusConnection *connection;
50
rm_change_cb_t change_cb;
54
#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
56
#define SERVICE_FILTER \
58
"sender='" DBUS_SERVICE_DBUS "'," \
59
"interface='" DBUS_INTERFACE_DBUS "'," \
60
"member='NameOwnerChanged'," \
63
static unsigned get_busy(
65
const char *name_owner) {
69
if (!name_owner || !*name_owner)
72
/* If we ourselves own the device, then don't consider this 'busy' */
73
if ((un = dbus_bus_get_unique_name(c)))
74
if (strcmp(name_owner, un) == 0)
80
static DBusHandlerResult filter_handler(
88
dbus_error_init(&error);
93
if (dbus_message_is_signal(s, "org.freedesktop.DBus", "NameOwnerChanged")) {
94
const char *name, *old, *new;
96
if (!dbus_message_get_args(
99
DBUS_TYPE_STRING, &name,
100
DBUS_TYPE_STRING, &old,
101
DBUS_TYPE_STRING, &new,
105
if (strcmp(name, m->service_name) == 0) {
106
unsigned old_busy = m->busy;
108
m->busy = get_busy(c, new);
110
if (m->busy != old_busy && m->change_cb) {
119
dbus_error_free(&error);
121
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
126
DBusConnection *connection,
127
const char *device_name,
128
rm_change_cb_t change_cb,
131
rm_monitor *m = NULL;
139
dbus_error_init(error);
150
if (!(m = calloc(sizeof(rm_monitor), 1)))
155
if (!(m->device_name = strdup(device_name))) {
160
m->connection = dbus_connection_ref(connection);
161
m->change_cb = change_cb;
163
if (!(m->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
167
sprintf(m->service_name, SERVICE_PREFIX "%s", m->device_name);
169
if (!(dbus_connection_add_filter(m->connection, filter_handler, m, NULL))) {
176
if (!(m->match = malloc(sizeof(SERVICE_FILTER) - 2 + strlen(m->service_name)))) {
181
sprintf(m->match, SERVICE_FILTER, m->service_name);
182
dbus_bus_add_match(m->connection, m->match, error);
184
if (dbus_error_is_set(error)) {
191
if ((r = rd_dbus_get_name_owner(m->connection, m->service_name, &name_owner, error)) < 0)
194
m->busy = get_busy(m->connection, name_owner);
201
if (&_error == error)
202
dbus_error_free(&_error);
210
void rm_release(rm_monitor *m) {
220
dbus_bus_remove_match(
226
dbus_connection_remove_filter(
231
free(m->device_name);
232
free(m->service_name);
236
dbus_connection_unref(m->connection);
241
int rm_busy(rm_monitor *m) {
250
void rm_set_userdata(rm_monitor *m, void *userdata) {
256
m->userdata = userdata;
259
void* rm_get_userdata(rm_monitor *m) {