5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <dbus/dbus.h>
40 static DBusHandlerResult message_filter(DBusConnection *connection,
41 DBusMessage *message, void *user_data);
43 static guint listener_id = 0;
44 static GSList *listeners = NULL;
46 struct filter_callback {
47 GDBusWatchFunction conn_func;
48 GDBusWatchFunction disc_func;
49 GDBusSignalFunction signal_func;
50 GDBusDestroyFunction destroy_func;
56 DBusConnection *connection;
57 DBusHandleMessageFunction handle_func;
69 static struct filter_data *filter_data_find(DBusConnection *connection,
72 const char *interface,
78 for (current = listeners;
79 current != NULL; current = current->next) {
80 struct filter_data *data = current->data;
82 if (connection != data->connection)
85 if (sender && data->sender &&
86 g_str_equal(sender, data->sender) == FALSE)
89 if (path && data->path &&
90 g_str_equal(path, data->path) == FALSE)
93 if (interface && data->interface &&
94 g_str_equal(interface, data->interface) == FALSE)
97 if (member && data->member &&
98 g_str_equal(member, data->member) == FALSE)
101 if (argument && data->argument &&
102 g_str_equal(argument, data->argument) == FALSE)
111 static void format_rule(struct filter_data *data, char *rule, size_t size)
115 offset = snprintf(rule, size, "type='signal'");
118 offset += snprintf(rule + offset, size - offset,
119 ",sender='%s'", data->sender);
121 offset += snprintf(rule + offset, size - offset,
122 ",path='%s'", data->path);
124 offset += snprintf(rule + offset, size - offset,
125 ",interface='%s'", data->interface);
127 offset += snprintf(rule + offset, size - offset,
128 ",member='%s'", data->member);
130 snprintf(rule + offset, size - offset,
131 ",arg0='%s'", data->argument);
134 static gboolean add_match(struct filter_data *data,
135 DBusHandleMessageFunction filter)
138 char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH];
140 format_rule(data, rule, sizeof(rule));
141 dbus_error_init(&err);
143 dbus_bus_add_match(data->connection, rule, &err);
144 if (dbus_error_is_set(&err)) {
145 error("Adding match rule \"%s\" failed: %s", rule,
147 dbus_error_free(&err);
151 data->handle_func = filter;
152 data->registered = TRUE;
157 static gboolean remove_match(struct filter_data *data)
160 char rule[DBUS_MAXIMUM_MATCH_RULE_LENGTH];
162 format_rule(data, rule, sizeof(rule));
164 dbus_error_init(&err);
166 dbus_bus_remove_match(data->connection, rule, &err);
167 if (dbus_error_is_set(&err)) {
168 error("Removing owner match rule for %s failed: %s",
170 dbus_error_free(&err);
177 static struct filter_data *filter_data_get(DBusConnection *connection,
178 DBusHandleMessageFunction filter,
181 const char *interface,
183 const char *argument)
185 struct filter_data *data;
187 if (!filter_data_find(connection, NULL, NULL, NULL, NULL, NULL)) {
188 if (!dbus_connection_add_filter(connection,
189 message_filter, NULL, NULL)) {
190 error("dbus_connection_add_filter() failed");
195 data = filter_data_find(connection, sender, path, interface, member,
200 data = g_new0(struct filter_data, 1);
202 data->connection = dbus_connection_ref(connection);
203 data->sender = g_strdup(sender);
204 data->path = g_strdup(path);
205 data->interface = g_strdup(interface);
206 data->member = g_strdup(member);
207 data->argument = g_strdup(argument);
209 if (!add_match(data, filter)) {
214 listeners = g_slist_append(listeners, data);
219 static struct filter_callback *filter_data_find_callback(
220 struct filter_data *data,
225 for (l = data->callbacks; l; l = l->next) {
226 struct filter_callback *cb = l->data;
230 for (l = data->processed; l; l = l->next) {
231 struct filter_callback *cb = l->data;
239 static void filter_data_free(struct filter_data *data)
243 for (l = data->callbacks; l != NULL; l = l->next)
246 g_slist_free(data->callbacks);
247 g_free(data->sender);
249 g_free(data->interface);
250 g_free(data->member);
251 g_free(data->argument);
252 dbus_connection_unref(data->connection);
256 static void filter_data_call_and_free(struct filter_data *data)
260 for (l = data->callbacks; l != NULL; l = l->next) {
261 struct filter_callback *cb = l->data;
263 cb->disc_func(data->connection, cb->user_data);
264 if (cb->destroy_func)
265 cb->destroy_func(cb->user_data);
269 filter_data_free(data);
272 static struct filter_callback *filter_data_add_callback(
273 struct filter_data *data,
274 GDBusWatchFunction connect,
275 GDBusWatchFunction disconnect,
276 GDBusSignalFunction signal,
277 GDBusDestroyFunction destroy,
280 struct filter_callback *cb = NULL;
282 cb = g_new(struct filter_callback, 1);
284 cb->conn_func = connect;
285 cb->disc_func = disconnect;
286 cb->signal_func = signal;
287 cb->destroy_func = destroy;
288 cb->user_data = user_data;
289 cb->id = ++listener_id;
292 data->processed = g_slist_append(data->processed, cb);
294 data->callbacks = g_slist_append(data->callbacks, cb);
299 static gboolean filter_data_remove_callback(struct filter_data *data,
300 struct filter_callback *cb)
302 DBusConnection *connection;
304 data->callbacks = g_slist_remove(data->callbacks, cb);
305 data->processed = g_slist_remove(data->processed, cb);
307 if (cb->destroy_func)
308 cb->destroy_func(cb->user_data);
312 /* Don't remove the filter if other callbacks exist or data is lock
313 * processing callbacks */
314 if (data->callbacks || data->lock)
317 if (data->registered && !remove_match(data))
320 connection = dbus_connection_ref(data->connection);
321 listeners = g_slist_remove(listeners, data);
322 filter_data_free(data);
324 /* Remove filter if there are no listeners left for the connection */
325 data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL);
327 dbus_connection_remove_filter(connection, message_filter,
330 dbus_connection_unref(connection);
335 static DBusHandlerResult signal_filter(DBusConnection *connection,
336 DBusMessage *message, void *user_data)
338 struct filter_data *data = user_data;
339 struct filter_callback *cb;
341 while (data->callbacks) {
342 cb = data->callbacks->data;
344 if (cb->signal_func && !cb->signal_func(connection, message,
346 filter_data_remove_callback(data, cb);
350 /* Check if the watch was removed/freed by the callback
352 if (!g_slist_find(data->callbacks, cb))
355 data->callbacks = g_slist_remove(data->callbacks, cb);
356 data->processed = g_slist_append(data->processed, cb);
359 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
362 static DBusHandlerResult service_filter(DBusConnection *connection,
363 DBusMessage *message, void *user_data)
365 struct filter_data *data = user_data;
366 struct filter_callback *cb;
367 char *name, *old, *new;
369 if (!dbus_message_get_args(message, NULL,
370 DBUS_TYPE_STRING, &name,
371 DBUS_TYPE_STRING, &old,
372 DBUS_TYPE_STRING, &new,
373 DBUS_TYPE_INVALID)) {
374 error("Invalid arguments for NameOwnerChanged signal");
375 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
378 while (data->callbacks) {
379 cb = data->callbacks->data;
383 cb->disc_func(connection, cb->user_data);
386 cb->conn_func(connection, cb->user_data);
389 /* Check if the watch was removed/freed by the callback
391 if (!g_slist_find(data->callbacks, cb))
394 data->callbacks = g_slist_remove(data->callbacks, cb);
396 if (!cb->conn_func || !cb->disc_func) {
401 data->processed = g_slist_append(data->processed, cb);
404 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
408 static DBusHandlerResult message_filter(DBusConnection *connection,
409 DBusMessage *message, void *user_data)
411 struct filter_data *data;
412 const char *sender, *path, *iface, *member, *arg = NULL;
414 /* Only filter signals */
415 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
416 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
418 sender = dbus_message_get_sender(message);
419 path = dbus_message_get_path(message);
420 iface = dbus_message_get_interface(message);
421 member = dbus_message_get_member(message);
422 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID);
424 data = filter_data_find(connection, sender, path, iface, member, arg);
426 error("Got %s.%s signal which has no listeners", iface, member);
427 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
430 if (data->handle_func) {
433 data->handle_func(connection, message, data);
435 data->callbacks = data->processed;
436 data->processed = NULL;
441 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
445 listeners = g_slist_remove(listeners, data);
446 filter_data_free(data);
448 /* Remove filter if there no listener left for the connection */
449 data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL);
451 dbus_connection_remove_filter(connection, message_filter,
454 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
457 struct service_data {
458 DBusConnection *conn;
459 GDBusWatchFunction conn_func;
463 static void service_reply(DBusPendingCall *call, void *user_data)
465 struct service_data *data = user_data;
468 dbus_bool_t has_owner;
470 reply = dbus_pending_call_steal_reply(call);
474 dbus_error_init(&error);
476 if (dbus_message_get_args(reply, &error,
477 DBUS_TYPE_BOOLEAN, &has_owner,
478 DBUS_TYPE_INVALID) == FALSE) {
479 if (dbus_error_is_set(&error) == TRUE) {
480 error("%s", error.message);
481 dbus_error_free(&error);
483 error("Wrong arguments for NameHasOwner reply");
488 if (has_owner && data->conn_func)
489 data->conn_func(data->conn, data->user_data);
492 dbus_message_unref(reply);
495 static void check_service(DBusConnection *connection, const char *name,
496 GDBusWatchFunction connect, void *user_data)
498 DBusMessage *message;
499 DBusPendingCall *call;
500 struct service_data *data;
502 data = g_try_malloc0(sizeof(*data));
504 error("Can't allocate data structure");
508 data->conn = connection;
509 data->conn_func = connect;
510 data->user_data = user_data;
512 message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
513 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameHasOwner");
514 if (message == NULL) {
515 error("Can't allocate new message");
520 dbus_message_append_args(message, DBUS_TYPE_STRING, &name,
523 if (dbus_connection_send_with_reply(connection, message,
524 &call, -1) == FALSE) {
525 error("Failed to execute method call");
531 error("D-Bus connection not available");
536 dbus_pending_call_set_notify(call, service_reply, data, g_free);
538 dbus_pending_call_unref(call);
541 dbus_message_unref(message);
544 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
545 GDBusWatchFunction connect,
546 GDBusWatchFunction disconnect,
547 void *user_data, GDBusDestroyFunction destroy)
549 struct filter_data *data;
550 struct filter_callback *cb;
555 data = filter_data_get(connection, service_filter, NULL, NULL,
556 DBUS_INTERFACE_DBUS, "NameOwnerChanged",
561 cb = filter_data_add_callback(data, connect, disconnect, NULL, NULL,
567 check_service(connection, name, connect, user_data);
572 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
573 GDBusWatchFunction func,
574 void *user_data, GDBusDestroyFunction destroy)
576 return g_dbus_add_service_watch(connection, name, NULL, func,
580 guint g_dbus_add_signal_watch(DBusConnection *connection,
581 const char *sender, const char *path,
582 const char *interface, const char *member,
583 GDBusSignalFunction function, void *user_data,
584 GDBusDestroyFunction destroy)
586 struct filter_data *data;
587 struct filter_callback *cb;
589 data = filter_data_get(connection, signal_filter, sender, path,
590 interface, member, NULL);
594 cb = filter_data_add_callback(data, NULL, NULL, function, destroy,
602 gboolean g_dbus_remove_watch(DBusConnection *connection, guint id)
604 struct filter_data *data;
605 struct filter_callback *cb;
611 for (ldata = listeners; ldata; ldata = ldata->next) {
614 cb = filter_data_find_callback(data, id);
616 filter_data_remove_callback(data, cb);
624 void g_dbus_remove_all_watches(DBusConnection *connection)
626 struct filter_data *data;
628 while ((data = filter_data_find(connection, NULL, NULL, NULL, NULL, NULL))) {
629 listeners = g_slist_remove(listeners, data);
630 filter_data_call_and_free(data);
633 dbus_connection_remove_filter(connection, message_filter, NULL);