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"
42
DBusConnection *connection;
48
rm_change_cb_t change_cb;
52
#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
54
static DBusHandlerResult filter_handler(
63
dbus_error_init(&error);
68
if (dbus_message_is_signal(s, "org.freedesktop.DBus", "NameOwnerChanged")) {
69
const char *name, *old, *new;
71
if (!dbus_message_get_args(
74
DBUS_TYPE_STRING, &name,
75
DBUS_TYPE_STRING, &old,
76
DBUS_TYPE_STRING, &new,
80
if (strcmp(name, m->service_name) == 0) {
81
m->busy = !!(new && *new);
83
/* If we ourselves own the device, then don't consider this 'busy' */
87
if ((un = dbus_bus_get_unique_name(c)))
88
if (strcmp(new, un) == 0)
100
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
103
if (!(reply = dbus_message_new_error(
105
DBUS_ERROR_INVALID_ARGS,
106
"Invalid arguments")))
109
if (!dbus_connection_send(c, reply, NULL))
112
dbus_message_unref(reply);
114
dbus_error_free(&error);
116
return DBUS_HANDLER_RESULT_HANDLED;
120
dbus_message_unref(reply);
122
dbus_error_free(&error);
124
return DBUS_HANDLER_RESULT_NEED_MEMORY;
129
DBusConnection *connection,
130
const char*device_name,
131
rm_change_cb_t change_cb,
134
rm_monitor *m = NULL;
141
dbus_error_init(error);
152
if (!(m = calloc(sizeof(rm_monitor), 1)))
157
if (!(m->device_name = strdup(device_name))) {
162
m->connection = dbus_connection_ref(connection);
163
m->change_cb = change_cb;
165
if (!(m->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
169
sprintf(m->service_name, SERVICE_PREFIX "%s", m->device_name);
171
if (!(dbus_connection_add_filter(m->connection, filter_handler, m, NULL))) {
178
dbus_bus_add_match(m->connection,
180
"sender='" DBUS_SERVICE_DBUS "',"
181
"interface='" DBUS_INTERFACE_DBUS "',"
182
"member='NameOwnerChanged'", error);
184
if (dbus_error_is_set(error)) {
191
m->busy = dbus_bus_name_has_owner(m->connection, m->service_name, error);
193
if (dbus_error_is_set(error)) {
202
if (&_error == error)
203
dbus_error_free(&_error);
211
void rm_release(rm_monitor *m) {
221
dbus_bus_remove_match(
224
"sender='" DBUS_SERVICE_DBUS "',"
225
"interface='" DBUS_INTERFACE_DBUS "',"
226
"member='NameOwnerChanged'", NULL);
229
dbus_connection_remove_filter(
234
free(m->device_name);
235
free(m->service_name);
238
dbus_connection_unref(m->connection);
243
int rm_busy(rm_monitor *m) {
252
void rm_set_userdata(rm_monitor *m, void *userdata) {
258
m->userdata = userdata;
261
void* rm_get_userdata(rm_monitor *m) {