1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gproxy.c Proxy for remote objects
4 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
5 * Copyright (C) 2005 Nokia
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus-glib-lowlevel.h>
29 #include <dbus/dbus-signature.h>
30 #include "dbus-gutils.h"
31 #include "dbus-gsignature.h"
32 #include "dbus-gvalue.h"
33 #include "dbus-gvalue-utils.h"
34 #include "dbus-gobject.h"
36 #include <gobject/gvaluecollector.h>
39 #define DBUS_G_PROXY_CALL_TO_ID(x) (GPOINTER_TO_UINT(x))
40 #define DBUS_G_PROXY_ID_TO_CALL(x) (GUINT_TO_POINTER(x))
41 #define DBUS_G_PROXY_GET_PRIVATE(o) \
42 (G_TYPE_INSTANCE_GET_PRIVATE ((o), DBUS_TYPE_G_PROXY, DBusGProxyPrivate))
44 static void oom (void) G_GNUC_NORETURN;
48 g_error ("no memory");
51 typedef struct _DBusGProxyManager DBusGProxyManager;
53 typedef struct _DBusGProxyPrivate DBusGProxyPrivate;
55 struct _DBusGProxyPrivate
57 DBusGProxyManager *manager; /**< Proxy manager */
58 char *name; /**< Name messages go to or NULL */
59 char *path; /**< Path messages go to or NULL */
60 char *interface; /**< Interface messages go to or NULL */
62 DBusGProxyCall *name_call; /**< Pending call id to retrieve name owner */
63 guint for_owner : 1; /**< Whether or not this proxy is for a name owner */
64 guint associated : 1; /**< Whether or not this proxy is associated (for name proxies) */
66 /* FIXME: make threadsafe? */
67 guint call_id_counter; /**< Integer counter for pending calls */
69 GData *signal_signatures; /**< D-BUS signatures for each signal */
71 GHashTable *pending_calls; /**< Calls made on this proxy which have not yet returned */
73 int default_timeout; /**< Default timeout to use, see dbus_g_proxy_set_default_timeout */
76 static void dbus_g_proxy_init (DBusGProxy *proxy);
77 static void dbus_g_proxy_class_init (DBusGProxyClass *klass);
78 static GObject *dbus_g_proxy_constructor (GType type,
79 guint n_construct_properties,
80 GObjectConstructParam *construct_properties);
81 static void dbus_g_proxy_set_property (GObject *object,
85 static void dbus_g_proxy_get_property (GObject *object,
90 static void dbus_g_proxy_finalize (GObject *object);
91 static void dbus_g_proxy_dispose (GObject *object);
92 static void dbus_g_proxy_destroy (DBusGProxy *proxy);
93 static void dbus_g_proxy_emit_remote_signal (DBusGProxy *proxy,
94 DBusMessage *message);
96 static DBusGProxyCall *manager_begin_bus_call (DBusGProxyManager *manager,
98 DBusGProxyCallNotify notify,
100 GDestroyNotify destroy,
101 GType first_arg_type,
103 static guint dbus_g_proxy_begin_call_internal (DBusGProxy *proxy,
105 DBusGProxyCallNotify notify,
107 GDestroyNotify destroy,
110 static gboolean dbus_g_proxy_end_call_internal (DBusGProxy *proxy,
113 GType first_arg_type,
117 * A list of proxies with a given name+path+interface, used to
118 * route incoming signals.
122 GSList *proxies; /**< The list of proxies */
124 char name[4]; /**< name (empty string for none), nul byte,
126 * interface, nul byte
132 * DBusGProxyManager's primary task is to route signals to the proxies
133 * those signals are emitted on. In order to do this it also has to
134 * track the owners of the names proxies are bound to.
136 struct _DBusGProxyManager
138 GStaticMutex lock; /**< Thread lock */
139 int refcount; /**< Reference count */
140 DBusConnection *connection; /**< Connection we're associated with. */
142 DBusGProxy *bus_proxy; /**< Special internal proxy used to talk to the bus */
144 GHashTable *proxy_lists; /**< Hash used to route incoming signals
145 * and iterate over proxies
146 * tristring -> DBusGProxyList
148 GHashTable *owner_match_rules; /**< Hash to keep track of match rules of
150 * gchar *name -> guint *refcount
152 GHashTable *owner_names; /**< Hash to keep track of mapping from
153 * char * -> GSList of DBusGProxyNameOwnerInfo
154 * base name -> [name,name,...] for proxies which
157 GSList *unassociated_proxies; /**< List of name proxies for which
158 * there was no result for
163 static DBusGProxyManager *dbus_g_proxy_manager_ref (DBusGProxyManager *manager);
164 static DBusHandlerResult dbus_g_proxy_manager_filter (DBusConnection *connection,
165 DBusMessage *message,
169 /** Lock the DBusGProxyManager */
170 #define LOCK_MANAGER(mgr) (g_static_mutex_lock (&(mgr)->lock))
171 /** Unlock the DBusGProxyManager */
172 #define UNLOCK_MANAGER(mgr) (g_static_mutex_unlock (&(mgr)->lock))
174 static int g_proxy_manager_slot = -1;
176 /* Lock controlling get/set manager as data on each connection */
177 static GStaticMutex connection_g_proxy_lock = G_STATIC_MUTEX_INIT;
179 static DBusGProxyManager*
180 dbus_g_proxy_manager_get (DBusConnection *connection)
182 DBusGProxyManager *manager;
184 dbus_connection_allocate_data_slot (&g_proxy_manager_slot);
185 if (g_proxy_manager_slot < 0)
186 g_error ("out of memory");
188 g_static_mutex_lock (&connection_g_proxy_lock);
190 manager = dbus_connection_get_data (connection, g_proxy_manager_slot);
193 dbus_connection_free_data_slot (&g_proxy_manager_slot);
194 dbus_g_proxy_manager_ref (manager);
195 g_static_mutex_unlock (&connection_g_proxy_lock);
199 manager = g_new0 (DBusGProxyManager, 1);
201 manager->refcount = 1;
202 manager->connection = connection;
204 g_static_mutex_init (&manager->lock);
206 /* Proxy managers keep the connection alive, which means that
207 * DBusGProxy indirectly does. To free a connection you have to free
208 * all the proxies referring to it.
210 dbus_connection_ref (manager->connection);
212 dbus_connection_set_data (connection, g_proxy_manager_slot,
215 dbus_connection_add_filter (connection, dbus_g_proxy_manager_filter,
218 g_static_mutex_unlock (&connection_g_proxy_lock);
223 static DBusGProxyManager *
224 dbus_g_proxy_manager_ref (DBusGProxyManager *manager)
226 g_assert (manager != NULL);
227 g_assert (manager->refcount > 0);
229 LOCK_MANAGER (manager);
231 manager->refcount += 1;
233 UNLOCK_MANAGER (manager);
239 dbus_g_proxy_manager_unref (DBusGProxyManager *manager)
241 g_assert (manager != NULL);
242 g_assert (manager->refcount > 0);
244 LOCK_MANAGER (manager);
245 manager->refcount -= 1;
246 if (manager->refcount == 0)
248 UNLOCK_MANAGER (manager);
250 if (manager->bus_proxy)
251 g_object_unref (manager->bus_proxy);
253 if (manager->proxy_lists)
255 /* can't have any proxies left since they hold
256 * a reference to the proxy manager.
258 g_assert (g_hash_table_size (manager->proxy_lists) == 0);
260 g_hash_table_destroy (manager->proxy_lists);
261 manager->proxy_lists = NULL;
265 if (manager->owner_match_rules)
267 /* Since we destroyed all proxies, none can be tracking
270 g_assert (g_hash_table_size (manager->owner_match_rules) == 0);
271 g_hash_table_destroy (manager->owner_match_rules);
272 manager->owner_match_rules = NULL;
275 if (manager->owner_names)
277 /* Since we destroyed all proxies, none can be tracking
280 g_assert (g_hash_table_size (manager->owner_names) == 0);
282 g_hash_table_destroy (manager->owner_names);
283 manager->owner_names = NULL;
286 g_assert (manager->unassociated_proxies == NULL);
288 g_static_mutex_free (&manager->lock);
290 g_static_mutex_lock (&connection_g_proxy_lock);
292 dbus_connection_remove_filter (manager->connection, dbus_g_proxy_manager_filter,
295 dbus_connection_set_data (manager->connection,
296 g_proxy_manager_slot,
299 g_static_mutex_unlock (&connection_g_proxy_lock);
301 dbus_connection_unref (manager->connection);
304 dbus_connection_free_data_slot (&g_proxy_manager_slot);
308 UNLOCK_MANAGER (manager);
313 tristring_hash (gconstpointer key)
320 for (p += 1; *p != '\0'; p++)
321 h = (h << 5) - h + *p;
324 /* skip nul and do the next substring */
325 for (p += 1; *p != '\0'; p++)
326 h = (h << 5) - h + *p;
328 /* skip nul again and another substring */
329 for (p += 1; *p != '\0'; p++)
330 h = (h << 5) - h + *p;
336 strequal_len (const char *a,
349 if (memcmp (a, b, a_len) != 0)
358 tristring_equal (gconstpointer a,
365 if (!strequal_len (ap, bp, &len))
371 if (!strequal_len (ap, bp, &len))
377 if (strcmp (ap, bp) != 0)
384 tristring_alloc_from_strings (size_t padding_before,
387 const char *interface)
389 size_t name_len, iface_len, path_len, len;
393 name_len = strlen (name);
397 path_len = strlen (path);
399 iface_len = strlen (interface);
401 tri = g_malloc (padding_before + name_len + path_len + iface_len + 3);
403 len = padding_before;
406 memcpy (&tri[len], name, name_len);
412 g_assert (len == (padding_before + name_len + 1));
414 memcpy (&tri[len], path, path_len);
419 g_assert (len == (padding_before + name_len + path_len + 2));
421 memcpy (&tri[len], interface, iface_len);
426 g_assert (len == (padding_before + name_len + path_len + iface_len + 3));
432 tristring_from_proxy (DBusGProxy *proxy)
434 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
436 return tristring_alloc_from_strings (0,
443 tristring_from_message (DBusMessage *message)
446 const char *interface;
448 path = dbus_message_get_path (message);
449 interface = dbus_message_get_interface (message);
452 g_assert (interface);
454 return tristring_alloc_from_strings (0,
455 dbus_message_get_sender (message),
459 static DBusGProxyList*
460 g_proxy_list_new (DBusGProxy *first_proxy)
462 DBusGProxyList *list;
463 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(first_proxy);
465 list = (void*) tristring_alloc_from_strings (G_STRUCT_OFFSET (DBusGProxyList, name),
469 list->proxies = NULL;
475 g_proxy_list_free (DBusGProxyList *list)
477 /* we don't hold a reference to the proxies in the list,
478 * as they ref the GProxyManager
480 g_slist_free (list->proxies);
486 g_proxy_get_signal_match_rule (DBusGProxy *proxy)
488 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
489 /* FIXME Escaping is required here */
492 return g_strdup_printf ("type='signal',sender='%s',path='%s',interface='%s'",
493 priv->name, priv->path, priv->interface);
495 return g_strdup_printf ("type='signal',path='%s',interface='%s'",
496 priv->path, priv->interface);
500 get_owner_match_rule (const gchar *name)
502 return g_strdup_printf ("type='signal',sender='" DBUS_SERVICE_DBUS
503 "',path='" DBUS_PATH_DBUS
504 "',interface='" DBUS_INTERFACE_DBUS
505 "',member='NameOwnerChanged'"
513 } DBusGProxyNameOwnerInfo;
516 find_name_in_info (gconstpointer a, gconstpointer b)
518 const DBusGProxyNameOwnerInfo *info = a;
519 const char *name = b;
521 return strcmp (info->name, name);
528 DBusGProxyNameOwnerInfo *info;
529 } DBusGProxyNameOwnerForeachData;
532 name_owner_foreach (gpointer key, gpointer val, gpointer data)
535 DBusGProxyNameOwnerForeachData *foreach_data;
543 if (foreach_data->owner != NULL)
546 g_assert (foreach_data->info == NULL);
548 link = g_slist_find_custom (names, foreach_data->name, find_name_in_info);
551 foreach_data->owner = owner;
552 foreach_data->info = link->data;
557 dbus_g_proxy_manager_lookup_name_owner (DBusGProxyManager *manager,
559 DBusGProxyNameOwnerInfo **info,
562 DBusGProxyNameOwnerForeachData foreach_data;
564 foreach_data.name = name;
565 foreach_data.owner = NULL;
566 foreach_data.info = NULL;
568 g_hash_table_foreach (manager->owner_names, name_owner_foreach, &foreach_data);
570 *info = foreach_data.info;
571 *owner = foreach_data.owner;
572 return *info != NULL;
576 insert_nameinfo (DBusGProxyManager *manager,
578 DBusGProxyNameOwnerInfo *info)
583 names = g_hash_table_lookup (manager->owner_names, owner);
585 /* Only need to g_hash_table_insert the first time */
586 insert = (names == NULL);
588 names = g_slist_append (names, info);
591 g_hash_table_insert (manager->owner_names, g_strdup (owner), names);
595 dbus_g_proxy_manager_monitor_name_owner (DBusGProxyManager *manager,
601 DBusGProxyNameOwnerInfo *nameinfo;
603 names = g_hash_table_lookup (manager->owner_names, owner);
604 link = g_slist_find_custom (names, name, find_name_in_info);
608 nameinfo = g_new0 (DBusGProxyNameOwnerInfo, 1);
609 nameinfo->name = g_strdup (name);
610 nameinfo->refcount = 1;
612 insert_nameinfo (manager, owner, nameinfo);
616 nameinfo = link->data;
617 nameinfo->refcount++;
622 dbus_g_proxy_manager_unmonitor_name_owner (DBusGProxyManager *manager,
625 DBusGProxyNameOwnerInfo *info;
629 ret = dbus_g_proxy_manager_lookup_name_owner (manager, name, &info, &owner);
631 g_assert (info != NULL);
632 g_assert (owner != NULL);
635 if (info->refcount == 0)
640 names = g_hash_table_lookup (manager->owner_names, owner);
641 link = g_slist_find_custom (names, name, find_name_in_info);
642 names = g_slist_delete_link (names, link);
644 g_hash_table_insert (manager->owner_names, g_strdup (owner), names);
646 g_hash_table_remove (manager->owner_names, owner);
657 } DBusGProxyUnassociateData;
660 unassociate_proxies (gpointer key, gpointer val, gpointer user_data)
662 DBusGProxyList *list;
665 DBusGProxyUnassociateData *data;
671 for (tmp = list->proxies; tmp; tmp = tmp->next)
673 DBusGProxy *proxy = DBUS_G_PROXY (tmp->data);
674 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
675 DBusGProxyManager *manager;
677 manager = priv->manager;
679 if (priv->name != NULL && !strcmp (priv->name, name))
681 if (!priv->for_owner)
683 /* If a service appeared and then vanished very quickly,
684 * it's conceivable we have an inflight request for
685 * GetNameOwner here. Cancel it.
686 * https://bugs.freedesktop.org/show_bug.cgi?id=18573
689 dbus_g_proxy_cancel_call (manager->bus_proxy, priv->name_call);
691 priv->name_call = NULL;
693 priv->associated = FALSE;
694 manager->unassociated_proxies = g_slist_prepend (manager->unassociated_proxies, proxy);
698 data->destroyed = g_slist_prepend (data->destroyed, proxy);
699 /* make contents of list into weak pointers in case the objects
700 * unref each other when disposing */
701 g_object_add_weak_pointer (G_OBJECT (proxy),
702 &(data->destroyed->data));
709 dbus_g_proxy_manager_replace_name_owner (DBusGProxyManager *manager,
711 const char *prev_owner,
712 const char *new_owner)
716 if (prev_owner[0] == '\0')
721 /* We have a new service, look at unassociated proxies */
725 for (tmp = manager->unassociated_proxies; tmp ; tmp = tmp->next)
727 DBusGProxy *proxy = tmp->data;
728 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
730 if (!strcmp (priv->name, name))
732 removed = g_slist_prepend (removed, tmp);
734 dbus_g_proxy_manager_monitor_name_owner (manager, new_owner, name);
735 priv->associated = TRUE;
739 for (tmp = removed; tmp; tmp = tmp->next)
740 manager->unassociated_proxies = g_slist_delete_link (manager->unassociated_proxies, tmp->data);
741 g_slist_free (removed);
745 DBusGProxyNameOwnerInfo *info;
748 /* Name owner changed or deleted */
750 names = g_hash_table_lookup (manager->owner_names, prev_owner);
755 link = g_slist_find_custom (names, name, find_name_in_info);
761 names = g_slist_delete_link (names, link);
765 g_hash_table_remove (manager->owner_names, prev_owner);
769 g_hash_table_insert (manager->owner_names,
770 g_strdup (prev_owner), names);
775 if (new_owner[0] == '\0')
777 DBusGProxyUnassociateData data;
781 data.destroyed = NULL;
783 /* A service went away, we need to unassociate proxies */
784 g_hash_table_foreach (manager->proxy_lists,
785 unassociate_proxies, &data);
787 UNLOCK_MANAGER (manager);
789 /* the destroyed list's data pointers are weak pointers, so that we
790 * don't end up calling destroy on proxies which have already been
791 * freed up as a result of other ones being destroyed */
792 for (tmp = data.destroyed; tmp; tmp = tmp->next)
793 if (tmp->data != NULL)
795 g_object_remove_weak_pointer (G_OBJECT (tmp->data),
797 dbus_g_proxy_destroy (tmp->data);
799 g_slist_free (data.destroyed);
801 LOCK_MANAGER (manager);
811 insert_nameinfo (manager, new_owner, info);
817 got_name_owner_cb (DBusGProxy *bus_proxy,
818 DBusGProxyCall *call,
821 DBusGProxy *proxy = user_data;
822 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
829 LOCK_MANAGER (priv->manager);
831 if (!dbus_g_proxy_end_call (bus_proxy, call, &error,
832 G_TYPE_STRING, &owner,
835 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
837 priv->manager->unassociated_proxies = g_slist_prepend (priv->manager->unassociated_proxies, proxy);
839 else if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
840 g_warning ("Couldn't get name owner (%s): %s",
841 dbus_g_error_get_name (error),
844 g_warning ("Couldn't get name owner (code %d): %s",
845 error->code, error->message);
846 g_clear_error (&error);
851 dbus_g_proxy_manager_monitor_name_owner (priv->manager, owner, priv->name);
852 priv->associated = TRUE;
856 priv->name_call = NULL;
857 UNLOCK_MANAGER (priv->manager);
862 get_name_owner (DBusConnection *connection,
867 DBusMessage *request, *reply;
870 dbus_error_init (&derror);
875 request = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
880 g_error ("Out of memory");
882 if (!dbus_message_append_args (request,
883 DBUS_TYPE_STRING, &name,
885 g_error ("Out of memory");
888 dbus_connection_send_with_reply_and_block (connection,
894 if (dbus_set_error_from_message (&derror, reply))
897 if (!dbus_message_get_args (reply, &derror,
898 DBUS_TYPE_STRING, &base_name,
902 base_name = g_strdup (base_name);
906 g_assert (dbus_error_is_set (&derror));
907 dbus_set_g_error (error, &derror);
908 dbus_error_free (&derror);
912 dbus_message_unref (request);
914 dbus_message_unref (reply);
921 guint_slice_free (gpointer data)
923 g_slice_free (guint, data);
928 dbus_g_proxy_manager_register (DBusGProxyManager *manager,
931 DBusGProxyList *list;
932 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
934 LOCK_MANAGER (manager);
936 if (manager->proxy_lists == NULL)
938 g_assert (manager->owner_names == NULL);
939 g_assert (manager->owner_match_rules == NULL);
942 manager->proxy_lists = g_hash_table_new_full (tristring_hash,
945 (GFreeFunc) g_proxy_list_free);
946 manager->owner_names = g_hash_table_new_full (g_str_hash,
950 manager->owner_match_rules = g_hash_table_new_full (g_str_hash,
959 tri = tristring_from_proxy (proxy);
961 list = g_hash_table_lookup (manager->proxy_lists, tri);
968 list = g_proxy_list_new (proxy);
970 g_hash_table_replace (manager->proxy_lists,
974 if (list->proxies == NULL && priv->name)
976 /* We have to add match rules to the server,
977 * but only if the server is a message bus,
978 * not if it's a peer.
983 rule = g_proxy_get_signal_match_rule (proxy);
984 /* We don't check for errors; it's not like anyone would handle them, and
985 * we don't want a round trip here.
987 dbus_bus_add_match (manager->connection, rule, NULL);
990 refcount = g_hash_table_lookup (manager->owner_match_rules, priv->name);
992 if (refcount != NULL)
994 g_assert (*refcount != 0);
995 g_assert (*refcount < G_MAXUINT);
1001 rule = get_owner_match_rule (priv->name);
1002 dbus_bus_add_match (manager->connection,
1006 refcount = g_slice_new (guint);
1008 g_hash_table_insert (manager->owner_match_rules,
1009 g_strdup (priv->name), refcount);
1013 g_assert (g_slist_find (list->proxies, proxy) == NULL);
1015 list->proxies = g_slist_prepend (list->proxies, proxy);
1017 if (!priv->for_owner)
1020 DBusGProxyNameOwnerInfo *info;
1022 if (!dbus_g_proxy_manager_lookup_name_owner (manager, priv->name, &info, &owner))
1024 priv->name_call = manager_begin_bus_call (manager, "GetNameOwner",
1031 priv->associated = FALSE;
1036 priv->associated = TRUE;
1040 UNLOCK_MANAGER (manager);
1044 dbus_g_proxy_manager_unregister (DBusGProxyManager *manager,
1047 DBusGProxyList *list;
1048 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1051 LOCK_MANAGER (manager);
1053 #ifndef G_DISABLE_CHECKS
1054 if (manager->proxy_lists == NULL)
1056 g_warning ("Trying to unregister a proxy but there aren't any registered");
1061 tri = tristring_from_proxy (proxy);
1063 list = g_hash_table_lookup (manager->proxy_lists, tri);
1065 #ifndef G_DISABLE_CHECKS
1068 g_warning ("Trying to unregister a proxy but it isn't registered");
1073 g_assert (g_slist_find (list->proxies, proxy) != NULL);
1075 list->proxies = g_slist_remove (list->proxies, proxy);
1077 g_assert (g_slist_find (list->proxies, proxy) == NULL);
1079 if (!priv->for_owner)
1081 if (!priv->associated)
1085 if (priv->name_call != 0)
1087 dbus_g_proxy_cancel_call (manager->bus_proxy, priv->name_call);
1088 priv->name_call = 0;
1092 link = g_slist_find (manager->unassociated_proxies, proxy);
1095 manager->unassociated_proxies = g_slist_delete_link (
1096 manager->unassociated_proxies, link);
1102 g_assert (priv->name_call == 0);
1104 dbus_g_proxy_manager_unmonitor_name_owner (manager, priv->name);
1108 if (list->proxies == NULL)
1111 g_hash_table_remove (manager->proxy_lists,
1114 rule = g_proxy_get_signal_match_rule (proxy);
1115 dbus_bus_remove_match (manager->connection,
1122 refcount = g_hash_table_lookup (manager->owner_match_rules, priv->name);
1127 rule = get_owner_match_rule (priv->name);
1128 dbus_bus_remove_match (manager->connection,
1131 g_hash_table_remove (manager->owner_match_rules, priv->name);
1136 if (g_hash_table_size (manager->proxy_lists) == 0)
1138 g_hash_table_destroy (manager->proxy_lists);
1139 manager->proxy_lists = NULL;
1142 if (manager->owner_match_rules != NULL &&
1143 g_hash_table_size (manager->owner_match_rules) == 0)
1145 g_hash_table_destroy (manager->owner_match_rules);
1146 manager->owner_match_rules = NULL;
1151 UNLOCK_MANAGER (manager);
1155 list_proxies_foreach (gpointer key,
1159 DBusGProxyList *list;
1166 tmp = list->proxies;
1169 DBusGProxy *proxy = DBUS_G_PROXY (tmp->data);
1171 g_object_ref (proxy);
1172 *ret = g_slist_prepend (*ret, proxy);
1179 dbus_g_proxy_manager_list_all (DBusGProxyManager *manager)
1185 if (manager->proxy_lists)
1187 g_hash_table_foreach (manager->proxy_lists,
1188 list_proxies_foreach,
1195 static DBusHandlerResult
1196 dbus_g_proxy_manager_filter (DBusConnection *connection,
1197 DBusMessage *message,
1200 DBusGProxyManager *manager;
1202 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
1203 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1205 manager = user_data;
1207 dbus_g_proxy_manager_ref (manager);
1209 LOCK_MANAGER (manager);
1211 if (dbus_message_is_signal (message,
1212 DBUS_INTERFACE_LOCAL,
1215 /* Destroy all the proxies, quite possibly resulting in unreferencing
1216 * the proxy manager and the connection as well.
1221 all = dbus_g_proxy_manager_list_all (manager);
1228 proxy = DBUS_G_PROXY (tmp->data);
1230 UNLOCK_MANAGER (manager);
1231 dbus_g_proxy_destroy (proxy);
1232 g_object_unref (G_OBJECT (proxy));
1233 LOCK_MANAGER (manager);
1240 #ifndef G_DISABLE_CHECKS
1241 if (manager->proxy_lists != NULL)
1242 g_warning ("Disconnection emitted \"destroy\" on all DBusGProxy, but somehow new proxies were created in response to one of those destroy signals. This will cause a memory leak.");
1249 GSList *owned_names;
1253 sender = dbus_message_get_sender (message);
1255 /* First we handle NameOwnerChanged internally */
1256 if (g_strcmp0 (sender, DBUS_SERVICE_DBUS) == 0 &&
1257 dbus_message_is_signal (message,
1258 DBUS_INTERFACE_DBUS,
1259 "NameOwnerChanged"))
1262 const char *prev_owner;
1263 const char *new_owner;
1266 dbus_error_init (&derr);
1267 if (!dbus_message_get_args (message,
1277 /* Ignore this error */
1278 dbus_error_free (&derr);
1280 else if (manager->owner_names != NULL)
1282 dbus_g_proxy_manager_replace_name_owner (manager, name, prev_owner, new_owner);
1286 /* dbus spec requires these, libdbus validates */
1287 g_assert (dbus_message_get_path (message) != NULL);
1288 g_assert (dbus_message_get_interface (message) != NULL);
1289 g_assert (dbus_message_get_member (message) != NULL);
1291 tri = tristring_from_message (message);
1293 if (manager->proxy_lists)
1295 DBusGProxyList *owner_list;
1296 owner_list = g_hash_table_lookup (manager->proxy_lists, tri);
1298 full_list = g_slist_copy (owner_list->proxies);
1307 if (manager->owner_names && sender)
1309 owned_names = g_hash_table_lookup (manager->owner_names, sender);
1310 for (tmp = owned_names; tmp; tmp = tmp->next)
1312 DBusGProxyList *owner_list;
1313 DBusGProxyNameOwnerInfo *nameinfo;
1315 nameinfo = tmp->data;
1316 g_assert (nameinfo->refcount > 0);
1317 tri = tristring_alloc_from_strings (0, nameinfo->name,
1318 dbus_message_get_path (message),
1319 dbus_message_get_interface (message));
1321 owner_list = g_hash_table_lookup (manager->proxy_lists, tri);
1322 if (owner_list != NULL)
1326 /* Ignore duplicates when adding to full_list */
1327 for (elt = owner_list->proxies; elt; elt = g_slist_next (elt))
1329 if (!g_slist_find (full_list, elt->data))
1330 full_list = g_slist_append (full_list, elt->data);
1338 g_print ("proxy got %s,%s,%s = list %p\n",
1340 tri + strlen (tri) + 1,
1341 tri + strlen (tri) + 1 + strlen (tri + strlen (tri) + 1) + 1,
1345 /* Emit the signal */
1347 g_slist_foreach (full_list, (GFunc) g_object_ref, NULL);
1349 for (tmp = full_list; tmp; tmp = tmp->next)
1353 proxy = DBUS_G_PROXY (tmp->data);
1355 UNLOCK_MANAGER (manager);
1356 dbus_g_proxy_emit_remote_signal (proxy, message);
1357 g_object_unref (G_OBJECT (proxy));
1358 LOCK_MANAGER (manager);
1360 g_slist_free (full_list);
1363 UNLOCK_MANAGER (manager);
1364 dbus_g_proxy_manager_unref (manager);
1366 /* "Handling" signals doesn't make sense, they are for everyone
1369 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1374 /* ---------- DBusGProxy -------------- */
1375 #define DBUS_G_PROXY_DESTROYED(proxy) (DBUS_G_PROXY_GET_PRIVATE(proxy)->manager == NULL)
1378 marshal_dbus_message_to_g_marshaller (GClosure *closure,
1379 GValue *return_value,
1380 guint n_param_values,
1381 const GValue *param_values,
1382 gpointer invocation_hint,
1383 gpointer marshal_data);
1400 static void *parent_class;
1401 static guint signals[LAST_SIGNAL] = { 0 };
1404 dbus_g_proxy_init (DBusGProxy *proxy)
1406 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1408 g_datalist_init (&priv->signal_signatures);
1409 priv->pending_calls = g_hash_table_new_full (NULL, NULL, NULL,
1410 (GDestroyNotify) dbus_pending_call_unref);
1411 priv->name_call = 0;
1412 priv->associated = FALSE;
1413 priv->default_timeout = -1;
1417 dbus_g_proxy_constructor (GType type,
1418 guint n_construct_properties,
1419 GObjectConstructParam *construct_properties)
1422 DBusGProxyClass *klass;
1423 GObjectClass *parent_class;
1424 DBusGProxyPrivate *priv;
1426 klass = DBUS_G_PROXY_CLASS (g_type_class_peek (DBUS_TYPE_G_PROXY));
1428 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
1430 proxy = DBUS_G_PROXY (parent_class->constructor (type, n_construct_properties,
1431 construct_properties));
1433 priv = DBUS_G_PROXY_GET_PRIVATE (proxy);
1435 /* if these assertions fail, a deriving class has not set our required
1436 * parameters - our own public constructors do return_if_fail checks
1437 * on these parameters being provided. unfortunately we can't assert
1438 * for manager because it's allowed to be NULL when tha mangager is
1439 * setting up a bus proxy for its own calls */
1440 g_assert (priv->path != NULL);
1441 g_assert (priv->interface != NULL);
1443 if (priv->manager != NULL)
1445 dbus_g_proxy_manager_register (priv->manager, proxy);
1448 return G_OBJECT (proxy);
1452 dbus_g_proxy_class_init (DBusGProxyClass *klass)
1454 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1456 parent_class = g_type_class_peek_parent (klass);
1458 g_type_class_add_private (klass, sizeof (DBusGProxyPrivate));
1460 object_class->set_property = dbus_g_proxy_set_property;
1461 object_class->get_property = dbus_g_proxy_get_property;
1463 g_object_class_install_property (object_class,
1465 g_param_spec_string ("name",
1469 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1471 g_object_class_install_property (object_class,
1473 g_param_spec_string ("path",
1477 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1479 g_object_class_install_property (object_class,
1481 g_param_spec_string ("interface",
1485 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1487 g_object_class_install_property (object_class,
1489 g_param_spec_boxed ("connection",
1492 DBUS_TYPE_G_CONNECTION,
1493 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1495 object_class->finalize = dbus_g_proxy_finalize;
1496 object_class->dispose = dbus_g_proxy_dispose;
1497 object_class->constructor = dbus_g_proxy_constructor;
1500 g_signal_new ("destroy",
1501 G_OBJECT_CLASS_TYPE (object_class),
1502 G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
1505 g_cclosure_marshal_VOID__VOID,
1509 g_signal_new ("received",
1510 G_OBJECT_CLASS_TYPE (object_class),
1511 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
1514 marshal_dbus_message_to_g_marshaller,
1515 G_TYPE_NONE, 2, DBUS_TYPE_MESSAGE, G_TYPE_POINTER);
1519 cancel_pending_call (gpointer key, gpointer val, gpointer data)
1521 DBusPendingCall *pending = val;
1523 dbus_pending_call_cancel (pending);
1529 dbus_g_proxy_dispose (GObject *object)
1531 DBusGProxy *proxy = DBUS_G_PROXY (object);
1532 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1534 if (priv->pending_calls == NULL)
1539 /* Cancel outgoing pending calls */
1540 g_hash_table_foreach_remove (priv->pending_calls, cancel_pending_call, NULL);
1541 g_hash_table_destroy (priv->pending_calls);
1542 priv->pending_calls = NULL;
1544 if (priv->manager && proxy != priv->manager->bus_proxy)
1546 dbus_g_proxy_manager_unregister (priv->manager, proxy);
1547 dbus_g_proxy_manager_unref (priv->manager);
1549 priv->manager = NULL;
1551 g_datalist_clear (&priv->signal_signatures);
1553 g_signal_emit (object, signals[DESTROY], 0);
1555 G_OBJECT_CLASS (parent_class)->dispose (object);
1559 dbus_g_proxy_finalize (GObject *object)
1561 DBusGProxy *proxy = DBUS_G_PROXY (object);
1562 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1564 g_return_if_fail (DBUS_G_PROXY_DESTROYED (proxy));
1566 g_free (priv->name);
1567 g_free (priv->path);
1568 g_free (priv->interface);
1570 G_OBJECT_CLASS (parent_class)->finalize (object);
1574 dbus_g_proxy_destroy (DBusGProxy *proxy)
1576 /* FIXME do we need the GTK_IN_DESTRUCTION style flag
1579 g_object_run_dispose (G_OBJECT (proxy));
1583 dbus_g_proxy_set_property (GObject *object,
1585 const GValue *value,
1588 DBusGProxy *proxy = DBUS_G_PROXY (object);
1589 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1590 DBusGConnection *connection;
1595 priv->name = g_strdup (g_value_get_string (value));
1597 priv->for_owner = (priv->name[0] == ':');
1599 priv->for_owner = TRUE;
1602 priv->path = g_strdup (g_value_get_string (value));
1604 case PROP_INTERFACE:
1605 priv->interface = g_strdup (g_value_get_string (value));
1607 case PROP_CONNECTION:
1608 connection = g_value_get_boxed (value);
1609 if (connection != NULL)
1611 priv->manager = dbus_g_proxy_manager_get (DBUS_CONNECTION_FROM_G_CONNECTION (connection));
1615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1621 dbus_g_proxy_get_property (GObject *object,
1626 DBusGProxy *proxy = DBUS_G_PROXY (object);
1627 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1632 g_value_set_string (value, priv->name);
1635 g_value_set_string (value, priv->path);
1637 case PROP_INTERFACE:
1638 g_value_set_string (value, priv->interface);
1640 case PROP_CONNECTION:
1641 g_value_set_boxed (value, DBUS_G_CONNECTION_FROM_CONNECTION(priv->manager->connection));
1644 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1649 /* this is to avoid people using g_signal_connect() directly,
1650 * to avoid confusion with local signal names, and because
1651 * of the horribly broken current setup (signals are added
1652 * globally to all proxies)
1655 create_signal_name (const char *interface,
1661 str = g_string_new (interface);
1663 g_string_append (str, "-");
1665 g_string_append (str, signal);
1667 /* GLib will silently barf on '.' in signal names */
1676 return g_string_free (str, FALSE);
1680 marshal_dbus_message_to_g_marshaller (GClosure *closure,
1681 GValue *return_value,
1682 guint n_param_values,
1683 const GValue *param_values,
1684 gpointer invocation_hint,
1685 gpointer marshal_data)
1687 /* Incoming here we have three params, the instance (Proxy), the
1688 * DBusMessage, the signature. We want to convert that to an
1689 * expanded GValue array, then call an appropriate normal GLib
1692 #define MAX_SIGNATURE_ARGS 20
1693 GValueArray *value_array;
1694 GSignalCMarshaller c_marshaller;
1696 DBusMessage *message;
1699 DBusGProxyPrivate *priv;
1701 g_assert (n_param_values == 3);
1703 proxy = g_value_get_object (¶m_values[0]);
1704 message = g_value_get_boxed (¶m_values[1]);
1705 gsignature = g_value_get_pointer (¶m_values[2]);
1707 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
1708 g_return_if_fail (message != NULL);
1709 g_return_if_fail (gsignature != NULL);
1711 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1713 c_marshaller = _dbus_gobject_lookup_marshaller (G_TYPE_NONE, gsignature->len,
1714 (GType*) gsignature->data);
1716 g_return_if_fail (c_marshaller != NULL);
1719 DBusGValueMarshalCtx context;
1720 context.recursion_depth = 0;
1721 context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection);
1722 context.proxy = proxy;
1724 types = (const GType*) gsignature->data;
1725 value_array = _dbus_gvalue_demarshal_message (&context, message,
1726 gsignature->len, types, NULL);
1729 if (value_array == NULL)
1732 g_value_array_prepend (value_array, NULL);
1733 g_value_init (g_value_array_get_nth (value_array, 0), G_TYPE_FROM_INSTANCE (proxy));
1734 g_value_set_instance (g_value_array_get_nth (value_array, 0), proxy);
1736 (* c_marshaller) (closure, return_value, value_array->n_values,
1737 value_array->values, invocation_hint, marshal_data);
1739 g_value_array_free (value_array);
1743 dbus_g_proxy_emit_remote_signal (DBusGProxy *proxy,
1744 DBusMessage *message)
1746 const char *interface;
1750 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
1751 GArray *msg_gsignature = NULL;
1753 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
1755 interface = dbus_message_get_interface (message);
1756 signal = dbus_message_get_member (message);
1758 g_assert (interface != NULL);
1759 g_assert (signal != NULL);
1761 name = create_signal_name (interface, signal);
1763 /* If the quark isn't preexisting, there's no way there
1764 * are any handlers connected. We don't want to create
1765 * extra quarks for every possible signal.
1767 q = g_quark_try_string (name);
1774 gsignature = g_datalist_id_get_data (&priv->signal_signatures, q);
1775 if (gsignature == NULL)
1778 msg_gsignature = _dbus_gtypes_from_arg_signature (dbus_message_get_signature (message),
1780 for (i = 0; i < gsignature->len; i++)
1782 if (msg_gsignature->len == i
1783 || g_array_index (gsignature, GType, i) != g_array_index (msg_gsignature, GType, i))
1786 if (msg_gsignature->len != i)
1789 g_signal_emit (proxy,
1799 g_array_free (msg_gsignature, TRUE);
1803 /* Don't spew on remote errors */
1804 g_warning ("Unexpected message signature '%s' for signal '%s'\n",
1805 dbus_message_get_signature (message),
1812 * DBusGProxyCallNotify:
1813 * @proxy: the proxy on which the method was called
1814 * @call_id: the call in progress
1815 * @user_data: data passed to dbus_g_proxy_begin_call() or similar
1817 * Called when a reply to the call represented by @call_id arrives.
1818 * Use dbus_g_proxy_end_call() to see whether @call_id succeeded or
1819 * failed, and get the arguments returned (if any) on success.
1826 DBusGProxyCallNotify func;
1828 GDestroyNotify free_data_func;
1829 } GPendingNotifyClosure;
1832 d_pending_call_notify (DBusPendingCall *dcall,
1835 GPendingNotifyClosure *closure = data;
1837 (* closure->func) (closure->proxy, DBUS_G_PROXY_ID_TO_CALL (closure->call_id), closure->data);
1841 d_pending_call_free (void *data)
1843 GPendingNotifyClosure *closure = data;
1845 if (closure->free_data_func)
1846 (* closure->free_data_func) (closure->data);
1851 #define DBUS_G_VALUE_ARRAY_COLLECT_ALL(VALARRAY, FIRST_ARG_TYPE, ARGS) \
1856 VALARRAY = g_value_array_new (6); \
1857 valtype = FIRST_ARG_TYPE; \
1859 while (valtype != G_TYPE_INVALID) \
1861 gchar *collect_err; \
1864 g_value_array_append (VALARRAY, NULL); \
1865 val = g_value_array_get_nth (VALARRAY, i); \
1866 g_value_init (val, valtype); \
1867 collect_err = NULL; \
1868 G_VALUE_COLLECT (val, ARGS, G_VALUE_NOCOPY_CONTENTS, &collect_err); \
1872 g_critical ("%s: unable to collect argument %u: %s", \
1873 G_STRFUNC, i, collect_err); \
1874 g_free (collect_err); \
1875 g_value_array_free (VALARRAY); \
1880 valtype = va_arg (ARGS, GType); \
1886 manager_begin_bus_call (DBusGProxyManager *manager,
1888 DBusGProxyCallNotify notify,
1890 GDestroyNotify destroy,
1891 GType first_arg_type,
1895 DBusGProxyPrivate *priv;
1897 GValueArray *arg_values;
1899 va_start (args, first_arg_type);
1901 if (!manager->bus_proxy)
1903 manager->bus_proxy = g_object_new (DBUS_TYPE_G_PROXY,
1904 "name", DBUS_SERVICE_DBUS,
1905 "path", DBUS_PATH_DBUS,
1906 "interface", DBUS_INTERFACE_DBUS,
1908 priv = DBUS_G_PROXY_GET_PRIVATE(manager->bus_proxy);
1909 priv->manager = manager;
1912 DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
1914 if (arg_values != NULL)
1916 call_id = dbus_g_proxy_begin_call_internal (manager->bus_proxy, method,
1917 notify, user_data, destroy, arg_values, -1);
1919 g_value_array_free (arg_values);
1924 return DBUS_G_PROXY_ID_TO_CALL (call_id);
1928 * SECTION:dbus-gproxy
1929 * @short_description: DBus Proxy
1930 * @see_also: #DBusGProxy
1931 * @stability: Stable
1933 * A #DBusGProxy is a #GObject representing a remote object in a D-Bus
1940 * A #GObject representing a remote object in a D-Bus service.
1946 * An opaque pointer representing an asynchronous call in progress.
1950 * dbus_g_proxy_get_type:
1951 * Standard GObject get_type() function for DBusGProxy.
1953 * Returns: type ID for DBusGProxy class
1956 dbus_g_proxy_get_type (void)
1958 static GType object_type = 0;
1962 static const GTypeInfo object_info =
1964 sizeof (DBusGProxyClass),
1965 (GBaseInitFunc) NULL,
1966 (GBaseFinalizeFunc) NULL,
1967 (GClassInitFunc) dbus_g_proxy_class_init,
1968 NULL, /* class_finalize */
1969 NULL, /* class_data */
1970 sizeof (DBusGProxy),
1971 0, /* n_preallocs */
1972 (GInstanceInitFunc) dbus_g_proxy_init,
1975 object_type = g_type_register_static (G_TYPE_OBJECT,
1984 dbus_g_proxy_new (DBusGConnection *connection,
1986 const char *path_name,
1987 const char *interface_name)
1991 g_assert (connection != NULL);
1993 proxy = g_object_new (DBUS_TYPE_G_PROXY,
1996 "interface", interface_name,
1997 "connection", connection, NULL);
2003 * dbus_g_proxy_new_for_name:
2004 * @connection: the connection to the remote bus
2005 * @name: any name on the message bus
2006 * @path: name of the object instance to call methods on
2007 * @iface: name of the interface to call methods on
2009 * Creates a new proxy for a remote interface exported by a connection
2010 * on a message bus. Method calls and signal connections over this
2011 * proxy will go to the name owner; the name's owner is expected to
2012 * support the given interface name. THE NAME OWNER MAY CHANGE OVER
2013 * TIME, for example between two different method calls, unless the
2014 * name is a unique name. If you need a fixed owner, you need to
2015 * request the current owner and bind a proxy to its unique name
2016 * rather than to the generic name; see
2017 * dbus_g_proxy_new_for_name_owner().
2019 * A name-associated proxy only makes sense with a message bus, not
2020 * for app-to-app direct dbus connections.
2022 * This proxy will only emit the "destroy" signal if the
2023 * #DBusConnection is disconnected, the proxy has no remaining
2024 * references, or the name is a unique name and its owner
2025 * disappears. If a well-known name changes owner, the proxy will
2028 * Returns: new proxy object
2031 dbus_g_proxy_new_for_name (DBusGConnection *connection,
2036 g_return_val_if_fail (connection != NULL, NULL);
2037 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2038 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
2039 g_return_val_if_fail (g_dbus_is_interface_name (iface), NULL);
2041 return dbus_g_proxy_new (connection, name, path, iface);
2045 * dbus_g_proxy_new_for_name_owner:
2046 * @connection: the connection to the remote bus
2047 * @name: any name on the message bus
2048 * @path: name of the object inside the service to call methods on
2049 * @iface: name of the interface to call methods on
2050 * @error: return location for an error
2052 * Similar to dbus_g_proxy_new_for_name(), but makes a round-trip
2053 * request to the message bus to get the current name owner, then
2054 * binds the proxy to the unique name of the current owner, rather
2055 * than to the well-known name. As a result, the name owner will
2056 * not change over time, and the proxy will emit the "destroy" signal
2057 * when the owner disappears from the message bus.
2059 * An example of the difference between dbus_g_proxy_new_for_name()
2060 * and dbus_g_proxy_new_for_name_owner(): if you provide the well-known name
2061 * "org.freedesktop.Database" dbus_g_proxy_new_for_name() remains bound
2062 * to that name as it changes owner. dbus_g_proxy_new_for_name_owner()
2063 * will fail if the name has no owner. If the name has an owner,
2064 * dbus_g_proxy_new_for_name_owner() will bind to the unique name
2065 * of that owner rather than the generic name.
2067 * Returns: new proxy object, or %NULL on error
2070 dbus_g_proxy_new_for_name_owner (DBusGConnection *connection,
2079 g_return_val_if_fail (connection != NULL, NULL);
2080 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2081 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
2082 g_return_val_if_fail (g_dbus_is_interface_name (iface), NULL);
2084 if (!(unique_name = get_name_owner (DBUS_CONNECTION_FROM_G_CONNECTION (connection), name, error)))
2087 proxy = dbus_g_proxy_new (connection, unique_name, path, iface);
2088 g_free (unique_name);
2093 * dbus_g_proxy_new_from_proxy:
2094 * @proxy: the proxy to use as a template
2095 * @iface: name of the interface to call methods on
2096 * @path: of the object inside the peer to call methods on
2098 * Creates a proxy using an existing proxy as a template, substituting
2099 * the specified interface and path. Either or both may be NULL.
2101 * Returns: new proxy object
2104 dbus_g_proxy_new_from_proxy (DBusGProxy *proxy,
2108 DBusGProxyPrivate *priv;
2110 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
2111 g_return_val_if_fail (path == NULL || g_variant_is_object_path (path), NULL);
2112 g_return_val_if_fail (iface == NULL ||
2113 g_dbus_is_interface_name (iface), NULL);
2115 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2118 iface = priv->interface;
2122 return dbus_g_proxy_new (DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection),
2128 * dbus_g_proxy_new_for_peer:
2129 * @connection: the connection to the peer
2130 * @path: name of the object inside the peer to call methods on
2131 * @iface: name of the interface to call methods on
2133 * Creates a proxy for an object in peer application (one
2134 * we're directly connected to). That is, this function is
2135 * intended for use when there's no message bus involved,
2136 * we're doing a simple 1-to-1 communication between two
2139 * Returns: new proxy object
2142 dbus_g_proxy_new_for_peer (DBusGConnection *connection,
2148 g_return_val_if_fail (connection != NULL, NULL);
2149 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
2150 g_return_val_if_fail (g_dbus_is_interface_name (iface), NULL);
2152 proxy = dbus_g_proxy_new (connection, NULL, path, iface);
2158 * dbus_g_proxy_get_bus_name:
2161 * Gets the bus name a proxy is bound to (may be %NULL in some cases).
2162 * If you created the proxy with dbus_g_proxy_new_for_name(), then
2163 * the name you passed to that will be returned.
2164 * If you created it with dbus_g_proxy_new_for_name_owner(), then the
2165 * unique connection name will be returned. If you created it
2166 * with dbus_g_proxy_new_for_peer() then %NULL will be returned.
2168 * It is an error to call this method on a proxy that has emitted
2169 * the #DBusGProxy::destroy signal.
2171 * Returns: the bus name the proxy sends messages to
2174 dbus_g_proxy_get_bus_name (DBusGProxy *proxy)
2176 DBusGProxyPrivate *priv;
2178 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
2179 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
2181 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2187 * dbus_g_proxy_get_interface:
2190 * Gets the object interface proxy is bound to (may be %NULL in some cases).
2192 * It is an error to call this method on a proxy that has emitted
2193 * the #DBusGProxy::destroy signal.
2195 * Returns: an object interface
2198 dbus_g_proxy_get_interface (DBusGProxy *proxy)
2200 DBusGProxyPrivate *priv;
2202 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
2203 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
2205 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2207 return priv->interface;
2211 * dbus_g_proxy_set_interface:
2213 * @interface_name: an object interface
2215 * Sets the object interface proxy is bound to
2217 * It is an error to call this method on a proxy that has emitted
2218 * the #DBusGProxy::destroy signal.
2221 dbus_g_proxy_set_interface (DBusGProxy *proxy,
2222 const char *interface_name)
2224 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2226 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
2227 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
2228 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2230 /* FIXME - need to unregister when we switch interface for now
2231 * later should support idea of unset interface
2233 dbus_g_proxy_manager_unregister (priv->manager, proxy);
2234 g_free (priv->interface);
2235 priv->interface = g_strdup (interface_name);
2236 dbus_g_proxy_manager_register (priv->manager, proxy);
2240 * dbus_g_proxy_get_path:
2243 * Gets the path this proxy is bound to
2245 * It is an error to call this method on a proxy that has emitted
2246 * the #DBusGProxy::destroy signal.
2248 * Returns: an object path
2251 dbus_g_proxy_get_path (DBusGProxy *proxy)
2253 DBusGProxyPrivate *priv;
2255 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
2256 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
2258 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2263 static DBusMessage *
2264 dbus_g_proxy_marshal_args_to_message (DBusGProxy *proxy,
2268 DBusMessage *message;
2269 DBusMessageIter msgiter;
2271 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2273 message = dbus_message_new_method_call (priv->name,
2277 if (message == NULL)
2280 dbus_message_iter_init_append (message, &msgiter);
2281 for (i = 0; i < args->n_values; i++)
2285 gvalue = g_value_array_get_nth (args, i);
2287 if (!_dbus_gvalue_marshal (&msgiter, gvalue))
2289 /* This is a programming error by the caller, most likely */
2290 gchar *contents = g_strdup_value_contents (gvalue);
2292 g_critical ("Could not marshal argument %u for %s: type %s, value %s",
2293 i, method, G_VALUE_TYPE_NAME (gvalue), contents);
2295 dbus_message_unref (message);
2304 dbus_g_proxy_begin_call_internal (DBusGProxy *proxy,
2306 DBusGProxyCallNotify notify,
2308 GDestroyNotify destroy,
2312 DBusMessage *message;
2313 DBusPendingCall *pending;
2314 GPendingNotifyClosure *closure;
2316 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2320 message = dbus_g_proxy_marshal_args_to_message (proxy, method, args);
2322 /* can only happen on a programming error or OOM; we already critical'd */
2326 if (!dbus_connection_send_with_reply (priv->manager->connection,
2332 dbus_message_unref (message);
2334 /* If we got a NULL pending, that means the connection was disconnected,
2335 * and we need to abort this call.
2336 * https://bugs.freedesktop.org/show_bug.cgi?id=12675
2338 if (pending == NULL)
2341 call_id = ++priv->call_id_counter;
2345 closure = g_new (GPendingNotifyClosure, 1);
2346 closure->proxy = proxy; /* No need to ref as the lifecycle is tied to proxy */
2347 closure->call_id = call_id;
2348 closure->func = notify;
2349 closure->data = user_data;
2350 closure->free_data_func = destroy;
2351 dbus_pending_call_set_notify (pending, d_pending_call_notify,
2353 d_pending_call_free);
2356 g_hash_table_insert (priv->pending_calls, GUINT_TO_POINTER (call_id), pending);
2362 dbus_g_proxy_end_call_internal (DBusGProxy *proxy,
2365 GType first_arg_type,
2369 DBusMessageIter msgiter;
2371 va_list args_unwind;
2373 int n_retvals_processed;
2376 DBusPendingCall *pending;
2377 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2381 /* Being disconnected is the only reason this can happen, except
2382 * for programmer error; if it was programmer error, we already
2383 * emitted a critical warning. */
2384 g_set_error (error, DBUS_GERROR, DBUS_GERROR_DISCONNECTED,
2385 "Disconnected from D-Bus (or argument error during call)");
2391 n_retvals_processed = 0;
2394 /* Keep around a copy of output arguments so we can free on error. */
2395 G_VA_COPY(args_unwind, args);
2397 pending = g_hash_table_lookup (priv->pending_calls, GUINT_TO_POINTER (call_id));
2399 dbus_pending_call_block (pending);
2400 reply = dbus_pending_call_steal_reply (pending);
2402 g_assert (reply != NULL);
2404 dbus_error_init (&derror);
2406 switch (dbus_message_get_type (reply))
2408 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
2409 dbus_message_iter_init (reply, &msgiter);
2410 valtype = first_arg_type;
2411 while (valtype != G_TYPE_INVALID)
2414 gpointer return_storage;
2415 GValue gvalue = { 0, };
2416 DBusGValueMarshalCtx context;
2418 context.recursion_depth = 0;
2419 context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (priv->manager->connection);
2420 context.proxy = proxy;
2422 arg_type = dbus_message_iter_get_arg_type (&msgiter);
2423 if (arg_type == DBUS_TYPE_INVALID)
2425 g_set_error (error, DBUS_GERROR,
2426 DBUS_GERROR_INVALID_ARGS,
2427 "Too few arguments in reply");
2431 return_storage = va_arg (args, gpointer);
2432 if (return_storage == NULL)
2435 /* We handle variants specially; the caller is expected
2436 * to have already allocated storage for them.
2438 if (arg_type == DBUS_TYPE_VARIANT
2439 && g_type_is_a (valtype, G_TYPE_VALUE))
2441 if (!_dbus_gvalue_demarshal_variant (&context, &msgiter, (GValue*) return_storage, NULL))
2445 DBUS_GERROR_INVALID_ARGS,
2446 "Couldn't convert argument, expected \"%s\"",
2447 g_type_name (valtype));
2453 g_value_init (&gvalue, valtype);
2455 if (!_dbus_gvalue_demarshal (&context, &msgiter, &gvalue, error))
2458 /* Anything that can be demarshaled must be storable */
2459 if (!_dbus_gvalue_store (&gvalue, return_storage))
2460 g_assert_not_reached ();
2461 /* Ownership of the value passes to the client, don't unset */
2465 n_retvals_processed++;
2466 dbus_message_iter_next (&msgiter);
2467 valtype = va_arg (args, GType);
2470 while (dbus_message_iter_get_arg_type (&msgiter) != DBUS_TYPE_INVALID)
2473 dbus_message_iter_next (&msgiter);
2478 g_set_error (error, DBUS_GERROR,
2479 DBUS_GERROR_INVALID_ARGS,
2480 "Too many arguments in reply; expected %d, got %d",
2481 n_retvals_processed, over);
2485 case DBUS_MESSAGE_TYPE_ERROR:
2486 dbus_set_error_from_message (&derror, reply);
2487 dbus_set_g_error (error, &derror);
2488 dbus_error_free (&derror);
2492 dbus_set_error (&derror, DBUS_ERROR_FAILED,
2493 "Reply was neither a method return nor an exception");
2494 dbus_set_g_error (error, &derror);
2495 dbus_error_free (&derror);
2506 valtype = first_arg_type;
2507 for (i = 0; i < n_retvals_processed; i++)
2509 GValue value = {0,};
2512 g_value_init (&value, valtype);
2514 retval = va_arg (args_unwind, gpointer);
2521 _dbus_gvalue_take (&value, retval);
2522 g_value_unset (&value);
2524 valtype = va_arg (args_unwind, GType);
2527 va_end (args_unwind);
2530 g_hash_table_remove (priv->pending_calls, GUINT_TO_POINTER (call_id));
2533 dbus_message_unref (reply);
2538 * dbus_g_proxy_begin_call:
2539 * @proxy: a proxy for a remote interface
2540 * @method: the name of the method to invoke
2541 * @notify: callback to be invoked when method returns
2542 * @user_data: user data passed to callback
2543 * @destroy: function called to destroy user_data
2544 * @first_arg_type: type of the first argument, or %G_TYPE_INVALID if there
2546 * @...: first argument, followed by any further type/value pairs, followed
2547 * by %G_TYPE_INVALID
2549 * Asynchronously invokes a method on a remote interface. The method
2550 * call will not be sent over the wire until the application returns
2551 * to the main loop, or blocks in dbus_g_connection_flush() to write out
2552 * pending data. The call will be completed after a timeout, or when
2553 * a reply is received. When the call returns, the callback specified
2554 * will be invoked; you can then collect the results of the call
2555 * (which may be an error, or a reply), use dbus_g_proxy_end_call().
2557 * It is an error to call this method on a proxy that has emitted
2558 * the #DBusGProxy::destroy signal.
2560 * TODO this particular function shouldn't die on out of memory,
2561 * since you should be able to do a call with large arguments.
2563 * Returns: call identifier.
2566 dbus_g_proxy_begin_call (DBusGProxy *proxy,
2568 DBusGProxyCallNotify notify,
2570 GDestroyNotify destroy,
2571 GType first_arg_type,
2576 GValueArray *arg_values;
2577 DBusGProxyPrivate *priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2579 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
2580 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
2581 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
2583 va_start (args, first_arg_type);
2585 DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
2587 if (arg_values != NULL)
2589 call_id = dbus_g_proxy_begin_call_internal (proxy, method, notify,
2590 user_data, destroy, arg_values, priv->default_timeout);
2592 g_value_array_free (arg_values);
2597 return DBUS_G_PROXY_ID_TO_CALL (call_id);
2601 * dbus_g_proxy_begin_call_with_timeout:
2602 * @proxy: a proxy for a remote interface
2603 * @method: the name of the method to invoke
2604 * @notify: callback to be invoked when method returns
2605 * @user_data: user data passed to callback
2606 * @destroy: function called to destroy user_data
2607 * @timeout: the timeout in milliseconds, or -1 to use a default
2608 * @first_arg_type: type of the first argument, or %G_TYPE_INVALID if there
2610 * @...: first argument, followed by any further type/value pairs, followed
2611 * by %G_TYPE_INVALID
2613 * Asynchronously invokes a method on a remote interface. The method
2614 * call will not be sent over the wire until the application returns
2615 * to the main loop, or blocks in dbus_g_connection_flush() to write out
2616 * pending data. The call will be completed after a timeout, or when
2617 * a reply is received. When the call returns, the callback specified
2618 * will be invoked; you can then collect the results of the call
2619 * (which may be an error, or a reply), use dbus_g_proxy_end_call().
2621 * It is an error to call this method on a proxy that has emitted
2622 * the #DBusGProxy::destroy signal.
2624 * TODO this particular function shouldn't die on out of memory,
2625 * since you should be able to do a call with large arguments.
2627 * Returns: call identifier.
2630 dbus_g_proxy_begin_call_with_timeout (DBusGProxy *proxy,
2632 DBusGProxyCallNotify notify,
2634 GDestroyNotify destroy,
2636 GType first_arg_type,
2641 GValueArray *arg_values;
2643 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
2644 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), NULL);
2645 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
2646 g_return_val_if_fail (timeout >= 0 || timeout == -1, NULL);
2648 va_start (args, first_arg_type);
2650 DBUS_G_VALUE_ARRAY_COLLECT_ALL (arg_values, first_arg_type, args);
2652 if (arg_values != NULL)
2654 call_id = dbus_g_proxy_begin_call_internal (proxy, method, notify,
2655 user_data, destroy, arg_values, timeout);
2657 g_value_array_free (arg_values);
2662 return DBUS_G_PROXY_ID_TO_CALL (call_id);
2666 * dbus_g_proxy_end_call:
2667 * @proxy: a proxy for a remote interface
2668 * @call: the pending call ID from dbus_g_proxy_begin_call()
2669 * @error: return location for an error
2670 * @first_arg_type: type of first "out" argument, or %G_TYPE_INVALID if
2671 * there are no "out" arguments
2672 * @...: return location for first "out" argument, followed by any further
2673 * type/location pairs, followed by %G_TYPE_INVALID
2675 * Collects the results of a method call. The method call was normally
2676 * initiated with dbus_g_proxy_end_call(). You may use this function
2677 * outside of the callback given to dbus_g_proxy_begin_call; in that
2678 * case this function will block if the results haven't yet been
2681 * All D-Bus method calls can fail with a remote error. If this occurs,
2682 * the @error will be set and this function will return %FALSE.
2684 * Otherwise, the "out" parameters and return value of the
2685 * method are stored in the provided varargs list.
2686 * The list should be terminated with G_TYPE_INVALID.
2688 * Returns: %TRUE on success
2691 dbus_g_proxy_end_call (DBusGProxy *proxy,
2692 DBusGProxyCall *call,
2694 GType first_arg_type,
2700 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
2702 va_start (args, first_arg_type);
2704 ret = dbus_g_proxy_end_call_internal (proxy, GPOINTER_TO_UINT (call), error, first_arg_type, args);
2712 * dbus_g_proxy_call:
2713 * @proxy: a proxy for a remote interface
2714 * @method: method to invoke
2715 * @error: return location for an error
2716 * @first_arg_type: type of first "in" argument, or %G_TYPE_INVALID if none
2717 * @...: value of first "in" argument, any further type/value pairs,
2718 * %G_TYPE_INVALID, type/location pairs for "out" arguments,
2719 * and %G_TYPE_INVALID again
2721 * Function for synchronously invoking a method and receiving reply
2722 * values. This function is equivalent to dbus_g_proxy_begin_call
2723 * followed by dbus_g_proxy_end_call. All of the input arguments are
2724 * specified first, followed by G_TYPE_INVALID, followed by all of the
2725 * output values, followed by a second G_TYPE_INVALID. Note that
2726 * this means you must always specify G_TYPE_INVALID twice.
2728 * It is an error to call this method on a proxy that has emitted
2729 * the #DBusGProxy::destroy signal.
2731 * Returns: %TRUE if the method succeeds, %FALSE if it fails
2734 dbus_g_proxy_call (DBusGProxy *proxy,
2737 GType first_arg_type,
2743 GValueArray *in_args;
2744 DBusGProxyPrivate *priv;
2746 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
2747 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), FALSE);
2749 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2751 va_start (args, first_arg_type);
2753 DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
2755 if (in_args != NULL)
2757 call_id = dbus_g_proxy_begin_call_internal (proxy, method, NULL, NULL,
2758 NULL, in_args, priv->default_timeout);
2760 g_value_array_free (in_args);
2763 first_arg_type = va_arg (args, GType);
2764 ret = dbus_g_proxy_end_call_internal (proxy, call_id, error, first_arg_type,
2773 * dbus_g_proxy_call_with_timeout:
2774 * @proxy: a proxy for a remote interface
2775 * @method: method to invoke
2776 * @timeout: the timeout in milliseconds, or -1 to use a default
2777 * @error: return location for an error
2778 * @first_arg_type: type of first "in" argument
2779 * @...: as for dbus_g_proxy_call()
2781 * Function for synchronously invoking a method and receiving reply
2782 * values. This function is equivalent to dbus_g_proxy_begin_call
2783 * followed by dbus_g_proxy_end_call. All of the input arguments are
2784 * specified first, followed by G_TYPE_INVALID, followed by all of the
2785 * output values, followed by a second G_TYPE_INVALID. Note that
2786 * this means you must always specify G_TYPE_INVALID twice.
2788 * It is an error to call this method on a proxy that has emitted
2789 * the #DBusGProxy::destroy signal.
2791 * Returns: %TRUE if the method succeeds, %FALSE if it fails
2794 dbus_g_proxy_call_with_timeout (DBusGProxy *proxy,
2798 GType first_arg_type,
2804 GValueArray *in_args;
2806 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
2807 g_return_val_if_fail (!DBUS_G_PROXY_DESTROYED (proxy), FALSE);
2808 g_return_val_if_fail (g_dbus_is_member_name (method), FALSE);
2809 g_return_val_if_fail (timeout >= 0 || timeout == -1, FALSE);
2811 va_start (args, first_arg_type);
2813 DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
2815 if (in_args != NULL)
2817 call_id = dbus_g_proxy_begin_call_internal (proxy, method, NULL, NULL,
2818 NULL, in_args, timeout);
2820 g_value_array_free (in_args);
2823 first_arg_type = va_arg (args, GType);
2824 ret = dbus_g_proxy_end_call_internal (proxy, call_id, error,
2825 first_arg_type, args);
2833 * dbus_g_proxy_call_no_reply:
2834 * @proxy: a proxy for a remote interface
2835 * @method: the name of the method to invoke
2836 * @first_arg_type: type of the first argument, or %G_TYPE_INVALID to call
2837 * the method without arguments
2838 * @...: the first argument and any remaining type/argument pairs, followed by
2839 * %G_TYPE_INVALID to terminate the list
2841 * Sends a method call message as with dbus_g_proxy_begin_call(), but
2842 * does not ask for a reply or allow you to receive one.
2844 * It is an error to call this method on a proxy that has emitted
2845 * the #DBusGProxy::destroy signal.
2847 * TODO: this particular function shouldn't die on out of memory,
2848 * since you should be able to do a call with large arguments.
2851 dbus_g_proxy_call_no_reply (DBusGProxy *proxy,
2853 GType first_arg_type,
2856 DBusMessage *message = NULL;
2858 GValueArray *in_args;
2859 DBusGProxyPrivate *priv;
2861 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
2862 g_return_if_fail (g_dbus_is_member_name (method));
2863 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
2865 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2867 va_start (args, first_arg_type);
2868 DBUS_G_VALUE_ARRAY_COLLECT_ALL (in_args, first_arg_type, args);
2870 if (in_args != NULL)
2872 message = dbus_g_proxy_marshal_args_to_message (proxy, method, in_args);
2874 g_value_array_free (in_args);
2879 /* can only happen on a programming error or OOM; we already critical'd */
2883 dbus_message_set_no_reply (message, TRUE);
2885 if (!dbus_connection_send (priv->manager->connection,
2890 dbus_message_unref (message);
2894 * dbus_g_proxy_cancel_call
2895 * @proxy: a proxy for a remote interface
2896 * @call: the pending call ID from dbus_g_proxy_begin_call()
2898 * Cancels a pending method call. The method call was normally
2899 * initiated with dbus_g_proxy_begin_call(). This function
2900 * may not be used on pending calls that have already been
2901 * ended with dbus_g_proxy_end_call.
2903 * It is an error to call this method on a proxy that has emitted
2904 * the #DBusGProxy::destroy signal.
2907 dbus_g_proxy_cancel_call (DBusGProxy *proxy,
2908 DBusGProxyCall *call)
2911 DBusPendingCall *pending;
2912 DBusGProxyPrivate *priv;
2914 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
2915 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
2917 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2919 call_id = DBUS_G_PROXY_CALL_TO_ID (call);
2923 /* nothing to cancel */
2927 pending = g_hash_table_lookup (priv->pending_calls, GUINT_TO_POINTER (call_id));
2928 g_hash_table_remove (priv->pending_calls, GUINT_TO_POINTER (call_id));
2929 g_return_if_fail (pending != NULL);
2931 dbus_pending_call_cancel (pending);
2935 * dbus_g_proxy_send:
2936 * @proxy: a proxy for a remote interface
2937 * @message: the message to address and send
2938 * @client_serial: return location for message's serial, or %NULL
2940 * Sends a message to the interface we're proxying for. Does not
2941 * block or wait for a reply. The message is only actually written out
2942 * when you return to the main loop or block in
2943 * dbus_g_connection_flush().
2945 * The message is modified to be addressed to the target interface.
2946 * That is, a destination name field or whatever is needed will be
2947 * added to the message. The basic point of this function is to add
2948 * the necessary header fields, otherwise it's equivalent to
2949 * dbus_connection_send().
2951 * This function adds a reference to the message, so the caller
2952 * still owns its original reference.
2954 * It is an error to call this method on a proxy that has emitted
2955 * the #DBusGProxy::destroy signal.
2958 dbus_g_proxy_send (DBusGProxy *proxy,
2959 DBusMessage *message,
2960 dbus_uint32_t *client_serial)
2962 DBusGProxyPrivate *priv;
2964 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
2965 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
2967 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
2971 if (!dbus_message_set_destination (message, priv->name))
2972 g_error ("Out of memory");
2976 if (!dbus_message_set_path (message, priv->path))
2977 g_error ("Out of memory");
2979 if (priv->interface)
2981 if (!dbus_message_set_interface (message, priv->interface))
2982 g_error ("Out of memory");
2985 if (!dbus_connection_send (priv->manager->connection, message, client_serial))
2986 g_error ("Out of memory\n");
2990 array_free_all (gpointer array)
2992 g_array_free (array, TRUE);
2996 * dbus_g_proxy_add_signal:
2997 * @proxy: the proxy for a remote interface
2998 * @signal_name: the name of the signal
2999 * @first_type: the first argument type, or %G_TYPE_INVALID if none
3000 * @...: any subsequent argument types, followed by %G_TYPE_INVALID
3002 * Specifies the argument signature of a D-Bus signal. When the signal is
3003 * emitted by the remote object, if the GTypes corresponding to its arguments'
3004 * types do not match the types given here, the signal will be ignored.
3006 * It is an error to add the same @signal_name to the same @proxy more than
3007 * once, even if the argument types given are the same.
3009 * It is also an error to call this method on a proxy that has emitted
3010 * the #DBusGProxy::destroy signal.
3013 dbus_g_proxy_add_signal (DBusGProxy *proxy,
3014 const char *signal_name,
3023 DBusGProxyPrivate *priv;
3025 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
3026 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
3027 g_return_if_fail (g_dbus_is_member_name (signal_name));
3029 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
3031 name = create_signal_name (priv->interface, signal_name);
3033 q = g_quark_from_string (name);
3035 g_return_if_fail (g_datalist_id_get_data (&priv->signal_signatures, q) == NULL);
3037 gtypesig = g_array_new (FALSE, TRUE, sizeof (GType));
3039 va_start (args, first_type);
3041 while (gtype != G_TYPE_INVALID)
3043 g_array_append_val (gtypesig, gtype);
3044 gtype = va_arg (args, GType);
3048 #ifndef G_DISABLE_CHECKS
3049 if (_dbus_gobject_lookup_marshaller (G_TYPE_NONE, gtypesig->len, (const GType*) gtypesig->data) == NULL)
3050 g_warning ("No marshaller for signature of signal '%s'", signal_name);
3054 g_datalist_id_set_data_full (&priv->signal_signatures,
3062 * dbus_g_proxy_connect_signal:
3063 * @proxy: a proxy for a remote interface
3064 * @signal_name: the DBus signal name to listen for
3065 * @handler: the handler to connect
3066 * @data: data to pass to handler
3067 * @free_data_func: callback function to destroy data
3069 * Connect a signal handler to a proxy for a remote interface. When
3070 * the remote interface emits the specified signal, the proxy will
3071 * emit a corresponding GLib signal.
3073 * It is an error to call this method on a proxy that has emitted
3074 * the #DBusGProxy::destroy signal.
3077 dbus_g_proxy_connect_signal (DBusGProxy *proxy,
3078 const char *signal_name,
3081 GClosureNotify free_data_func)
3086 DBusGProxyPrivate *priv;
3088 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
3089 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
3090 g_return_if_fail (g_dbus_is_member_name (signal_name));
3091 g_return_if_fail (handler != NULL);
3093 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
3094 name = create_signal_name (priv->interface, signal_name);
3096 q = g_quark_try_string (name);
3098 #ifndef G_DISABLE_CHECKS
3099 if (q == 0 || g_datalist_id_get_data (&priv->signal_signatures, q) == NULL)
3101 g_warning ("Must add the signal '%s' with dbus_g_proxy_add_signal() prior to connecting to it\n", name);
3107 closure = g_cclosure_new (G_CALLBACK (handler), data, free_data_func);
3109 g_signal_connect_closure_by_id (G_OBJECT (proxy),
3118 * dbus_g_proxy_disconnect_signal:
3119 * @proxy: a proxy for a remote interface
3120 * @signal_name: the DBus signal name to disconnect
3121 * @handler: the handler to disconnect
3122 * @data: the data that was registered with handler
3124 * Disconnect all signal handlers from a proxy that match the given
3127 * It is an error to call this method on a proxy that has emitted
3128 * the #DBusGProxy::destroy signal.
3131 dbus_g_proxy_disconnect_signal (DBusGProxy *proxy,
3132 const char *signal_name,
3138 DBusGProxyPrivate *priv;
3140 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
3141 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
3142 g_return_if_fail (g_dbus_is_member_name (signal_name));
3143 g_return_if_fail (handler != NULL);
3145 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
3146 name = create_signal_name (priv->interface, signal_name);
3148 q = g_quark_try_string (name);
3152 g_signal_handlers_disconnect_matched (G_OBJECT (proxy),
3153 G_SIGNAL_MATCH_DETAIL |
3154 G_SIGNAL_MATCH_FUNC |
3155 G_SIGNAL_MATCH_DATA,
3159 G_CALLBACK (handler), data);
3163 g_warning ("Attempt to disconnect from signal '%s' which is not registered\n",
3171 * dbus_g_proxy_set_default_timeout:
3172 * @proxy: a proxy for a remote interface
3173 * @timeout: the timeout in milliseconds, or -1 to reset to the libdbus default
3175 * Sets the default timeout to use for a proxy. This timeout will be
3176 * used in calls where the timeout is not specified, or is specified to be -1.
3177 * If this timeout is also set to -1, libdbus will use a reasonable default
3180 * This is useful for long-running operations that takes longer than
3181 * the default timeout (which is a on the order of magnitude of tens
3182 * of seconds). For some applications, consider using a pattern where
3183 * the method returns once the operation is underway
3184 * (e.g. immediately) and emits a signal when the operation terminates
3185 * (though beware of leaking information with/in the signal return value).
3187 * It is an error to call this method on a proxy that has emitted
3188 * the #DBusGProxy::destroy signal.
3193 dbus_g_proxy_set_default_timeout (DBusGProxy *proxy,
3196 DBusGProxyPrivate *priv;
3198 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
3199 g_return_if_fail (!DBUS_G_PROXY_DESTROYED (proxy));
3200 g_return_if_fail (timeout >= 0 || timeout == -1);
3202 priv = DBUS_G_PROXY_GET_PRIVATE(proxy);
3203 priv->default_timeout = timeout;