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"
43 DBusConnection
*connection
;
49 rm_change_cb_t change_cb
;
53 #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
55 #define SERVICE_FILTER \
57 "sender='" DBUS_SERVICE_DBUS "'," \
58 "interface='" DBUS_INTERFACE_DBUS "'," \
59 "member='NameOwnerChanged'," \
62 static DBusHandlerResult
filter_handler(
70 dbus_error_init(&error
);
75 if (dbus_message_is_signal(s
, "org.freedesktop.DBus", "NameOwnerChanged")) {
76 const char *name
, *old
, *new;
78 if (!dbus_message_get_args(
81 DBUS_TYPE_STRING
, &name
,
82 DBUS_TYPE_STRING
, &old
,
83 DBUS_TYPE_STRING
, &new,
87 if (strcmp(name
, m
->service_name
) == 0) {
88 m
->busy
= !!(new && *new);
90 /* If we ourselves own the device, then don't consider this 'busy' */
94 if ((un
= dbus_bus_get_unique_name(c
)))
95 if (strcmp(new, un
) == 0)
108 dbus_error_free(&error
);
110 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
115 DBusConnection
*connection
,
116 const char*device_name
,
117 rm_change_cb_t change_cb
,
120 rm_monitor
*m
= NULL
;
127 dbus_error_init(error
);
138 if (!(m
= calloc(sizeof(rm_monitor
), 1)))
143 if (!(m
->device_name
= strdup(device_name
))) {
148 m
->connection
= dbus_connection_ref(connection
);
149 m
->change_cb
= change_cb
;
151 if (!(m
->service_name
= malloc(sizeof(SERVICE_PREFIX
) + strlen(device_name
)))) {
155 sprintf(m
->service_name
, SERVICE_PREFIX
"%s", m
->device_name
);
157 if (!(dbus_connection_add_filter(m
->connection
, filter_handler
, m
, NULL
))) {
164 if (!(m
->match
= malloc(sizeof(SERVICE_FILTER
) - 2 + strlen(m
->service_name
)))) {
169 sprintf(m
->match
, SERVICE_FILTER
, m
->service_name
);
170 dbus_bus_add_match(m
->connection
, m
->match
, error
);
172 if (dbus_error_is_set(error
)) {
179 m
->busy
= dbus_bus_name_has_owner(m
->connection
, m
->service_name
, error
);
181 if (dbus_error_is_set(error
)) {
190 if (&_error
== error
)
191 dbus_error_free(&_error
);
199 void rm_release(rm_monitor
*m
) {
209 dbus_bus_remove_match(
215 dbus_connection_remove_filter(
220 free(m
->device_name
);
221 free(m
->service_name
);
225 dbus_connection_unref(m
->connection
);
230 int rm_busy(rm_monitor
*m
) {
239 void rm_set_userdata(rm_monitor
*m
, void *userdata
) {
245 m
->userdata
= userdata
;
248 void* rm_get_userdata(rm_monitor
*m
) {