1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gcall.c convenience routines for calling methods, etc.
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.0
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <dbus/dbus-glib.h>
24 #include <dbus/dbus-glib-lowlevel.h>
25 #include "dbus-gutils.h"
29 * @addtogroup DBusGLibInternals
35 * DBusGProxyManager typedef
38 typedef struct DBusGProxyManager DBusGProxyManager;
41 * Internals of DBusGProxy
45 GObject parent; /**< Parent instance */
47 DBusGProxyManager *manager; /**< Proxy manager */
48 char *service; /**< Service messages go to or NULL */
49 char *path; /**< Path messages go to or NULL */
50 char *interface; /**< Interface messages go to or NULL */
54 * Class struct for DBusGProxy
56 struct DBusGProxyClass
58 GObjectClass parent_class; /**< Parent class */
61 static void dbus_g_proxy_init (DBusGProxy *proxy);
62 static void dbus_g_proxy_class_init (DBusGProxyClass *klass);
63 static void dbus_g_proxy_finalize (GObject *object);
64 static void dbus_g_proxy_dispose (GObject *object);
65 static void dbus_g_proxy_destroy (DBusGProxy *proxy);
66 static void dbus_g_proxy_emit_received (DBusGProxy *proxy,
67 DBusMessage *message);
71 * A list of proxies with a given service+path+interface, used to
72 * route incoming signals.
76 GSList *proxies; /**< The list of proxies */
78 char name[4]; /**< service (empty string for none), nul byte,
86 * DBusGProxyManager's primary task is to route signals to the proxies
87 * those signals are emitted on. In order to do this it also has to
88 * track the owners of the services proxies are bound to.
90 struct DBusGProxyManager
92 GStaticMutex lock; /**< Thread lock */
93 int refcount; /**< Reference count */
94 DBusConnection *connection; /**< Connection we're associated with. */
96 GHashTable *proxy_lists; /**< Hash used to route incoming signals
97 * and iterate over proxies
102 static DBusGProxyManager *dbus_g_proxy_manager_ref (DBusGProxyManager *manager);
103 static DBusHandlerResult dbus_g_proxy_manager_filter (DBusConnection *connection,
104 DBusMessage *message,
107 /** Lock the DBusGProxyManager */
108 #define LOCK_MANAGER(mgr) (g_static_mutex_lock (&(mgr)->lock))
109 /** Unlock the DBusGProxyManager */
110 #define UNLOCK_MANAGER(mgr) (g_static_mutex_unlock (&(mgr)->lock))
112 static int g_proxy_manager_slot = -1;
114 /* Lock controlling get/set manager as data on each connection */
115 static GStaticMutex connection_g_proxy_lock = G_STATIC_MUTEX_INIT;
117 static DBusGProxyManager*
118 dbus_g_proxy_manager_get (DBusConnection *connection)
120 DBusGProxyManager *manager;
122 dbus_connection_allocate_data_slot (&g_proxy_manager_slot);
123 if (g_proxy_manager_slot < 0)
124 g_error ("out of memory");
126 g_static_mutex_lock (&connection_g_proxy_lock);
128 manager = dbus_connection_get_data (connection, g_proxy_manager_slot);
131 dbus_connection_free_data_slot (&g_proxy_manager_slot);
132 dbus_g_proxy_manager_ref (manager);
133 g_static_mutex_unlock (&connection_g_proxy_lock);
137 manager = g_new0 (DBusGProxyManager, 1);
139 manager->refcount = 1;
140 manager->connection = connection;
142 g_static_mutex_init (&manager->lock);
144 /* Proxy managers keep the connection alive, which means that
145 * DBusGProxy indirectly does. To free a connection you have to free
146 * all the proxies referring to it.
148 dbus_connection_ref (manager->connection);
150 dbus_connection_set_data (connection, g_proxy_manager_slot,
153 dbus_connection_add_filter (connection, dbus_g_proxy_manager_filter,
156 g_static_mutex_unlock (&connection_g_proxy_lock);
161 static DBusGProxyManager *
162 dbus_g_proxy_manager_ref (DBusGProxyManager *manager)
164 g_assert (manager != NULL);
165 g_assert (manager->refcount > 0);
167 LOCK_MANAGER (manager);
169 manager->refcount += 1;
171 UNLOCK_MANAGER (manager);
177 dbus_g_proxy_manager_unref (DBusGProxyManager *manager)
179 g_assert (manager != NULL);
180 g_assert (manager->refcount > 0);
182 LOCK_MANAGER (manager);
183 manager->refcount -= 1;
184 if (manager->refcount == 0)
186 UNLOCK_MANAGER (manager);
188 if (manager->proxy_lists)
190 /* can't have any proxies left since they hold
191 * a reference to the proxy manager.
193 g_assert (g_hash_table_size (manager->proxy_lists) == 0);
195 g_hash_table_destroy (manager->proxy_lists);
196 manager->proxy_lists = NULL;
199 g_static_mutex_free (&manager->lock);
201 g_static_mutex_lock (&connection_g_proxy_lock);
203 dbus_connection_remove_filter (manager->connection, dbus_g_proxy_manager_filter,
206 dbus_connection_set_data (manager->connection,
207 g_proxy_manager_slot,
210 g_static_mutex_unlock (&connection_g_proxy_lock);
212 dbus_connection_unref (manager->connection);
215 dbus_connection_free_data_slot (&g_proxy_manager_slot);
219 UNLOCK_MANAGER (manager);
224 tristring_hash (gconstpointer key)
231 for (p += 1; *p != '\0'; p++)
232 h = (h << 5) - h + *p;
235 /* skip nul and do the next substring */
236 for (p += 1; *p != '\0'; p++)
237 h = (h << 5) - h + *p;
239 /* skip nul again and another substring */
240 for (p += 1; *p != '\0'; p++)
241 h = (h << 5) - h + *p;
247 strequal_len (const char *a,
260 if (memcmp (a, b, a_len) != 0)
269 tristring_equal (gconstpointer a,
276 if (!strequal_len (ap, bp, &len))
282 if (!strequal_len (ap, bp, &len))
288 if (strcmp (ap, bp) != 0)
295 tristring_alloc_from_strings (size_t padding_before,
298 const char *interface)
300 size_t service_len, iface_len, path_len, len;
304 service_len = strlen (service);
308 path_len = strlen (path);
310 iface_len = strlen (interface);
312 tri = g_malloc (padding_before + service_len + path_len + iface_len + 3);
314 len = padding_before;
317 memcpy (&tri[len], service, service_len);
323 g_assert (len == (padding_before + service_len + 1));
325 memcpy (&tri[len], path, path_len);
330 g_assert (len == (padding_before + service_len + path_len + 2));
332 memcpy (&tri[len], interface, iface_len);
337 g_assert (len == (padding_before + service_len + path_len + iface_len + 3));
343 tristring_from_proxy (DBusGProxy *proxy)
345 return tristring_alloc_from_strings (0,
352 tristring_from_message (DBusMessage *message)
354 return tristring_alloc_from_strings (0,
355 dbus_message_get_sender (message),
356 dbus_message_get_path (message),
357 dbus_message_get_interface (message));
360 static DBusGProxyList*
361 g_proxy_list_new (DBusGProxy *first_proxy)
363 DBusGProxyList *list;
365 list = (void*) tristring_alloc_from_strings (G_STRUCT_OFFSET (DBusGProxyList, name),
366 first_proxy->service,
368 first_proxy->interface);
369 list->proxies = NULL;
375 g_proxy_list_free (DBusGProxyList *list)
377 /* we don't hold a reference to the proxies in the list,
378 * as they ref the GProxyManager
380 g_slist_free (list->proxies);
386 g_proxy_get_match_rule (DBusGProxy *proxy)
388 /* FIXME Escaping is required here */
391 return g_strdup_printf ("type='signal',sender='%s',path='%s',interface='%s'",
392 proxy->service, proxy->path, proxy->interface);
394 return g_strdup_printf ("type='signal',path='%s',interface='%s'",
395 proxy->path, proxy->interface);
399 dbus_g_proxy_manager_register (DBusGProxyManager *manager,
402 DBusGProxyList *list;
404 LOCK_MANAGER (manager);
406 if (manager->proxy_lists == NULL)
409 manager->proxy_lists = g_hash_table_new_full (tristring_hash,
412 (GFreeFunc) g_proxy_list_free);
418 tri = tristring_from_proxy (proxy);
420 list = g_hash_table_lookup (manager->proxy_lists, tri);
427 list = g_proxy_list_new (proxy);
429 g_hash_table_replace (manager->proxy_lists,
433 if (list->proxies == NULL)
435 /* We have to add the match rule to the server,
436 * but FIXME only if the server is a message bus,
437 * not if it's a peer.
441 rule = g_proxy_get_match_rule (proxy);
443 /* We don't check for errors; it's not like anyone would handle them,
444 * and we don't want a round trip here.
446 dbus_bus_add_match (manager->connection,
452 g_assert (g_slist_find (list->proxies, proxy) == NULL);
454 list->proxies = g_slist_prepend (list->proxies, proxy);
456 UNLOCK_MANAGER (manager);
460 dbus_g_proxy_manager_unregister (DBusGProxyManager *manager,
463 DBusGProxyList *list;
466 LOCK_MANAGER (manager);
468 #ifndef G_DISABLE_CHECKS
469 if (manager->proxy_lists == NULL)
471 g_warning ("Trying to unregister a proxy but there aren't any registered");
476 tri = tristring_from_proxy (proxy);
478 list = g_hash_table_lookup (manager->proxy_lists, tri);
480 #ifndef G_DISABLE_CHECKS
483 g_warning ("Trying to unregister a proxy but it isn't registered");
488 g_assert (g_slist_find (list->proxies, proxy) != NULL);
490 list->proxies = g_slist_remove (list->proxies, proxy);
492 g_assert (g_slist_find (list->proxies, proxy) == NULL);
494 if (list->proxies == NULL)
496 g_hash_table_remove (manager->proxy_lists,
501 if (g_hash_table_size (manager->proxy_lists) == 0)
503 g_hash_table_destroy (manager->proxy_lists);
504 manager->proxy_lists = NULL;
509 UNLOCK_MANAGER (manager);
513 list_proxies_foreach (gpointer key,
517 DBusGProxyList *list;
527 DBusGProxy *proxy = DBUS_G_PROXY (tmp->data);
529 g_object_ref (proxy);
530 *ret = g_slist_prepend (*ret, proxy);
537 dbus_g_proxy_manager_list_all (DBusGProxyManager *manager)
543 if (manager->proxy_lists)
545 g_hash_table_foreach (manager->proxy_lists,
546 list_proxies_foreach,
553 static DBusHandlerResult
554 dbus_g_proxy_manager_filter (DBusConnection *connection,
555 DBusMessage *message,
558 DBusGProxyManager *manager;
560 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
561 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
565 dbus_g_proxy_manager_ref (manager);
567 LOCK_MANAGER (manager);
569 if (dbus_message_is_signal (message,
570 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
573 /* Destroy all the proxies, quite possibly resulting in unreferencing
574 * the proxy manager and the connection as well.
579 all = dbus_g_proxy_manager_list_all (manager);
586 proxy = DBUS_G_PROXY (tmp->data);
588 UNLOCK_MANAGER (manager);
589 dbus_g_proxy_destroy (proxy);
590 g_object_unref (G_OBJECT (proxy));
591 LOCK_MANAGER (manager);
598 #ifndef G_DISABLE_CHECKS
599 if (manager->proxy_lists != NULL)
600 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.");
606 DBusGProxyList *list;
608 tri = tristring_from_message (message);
610 if (manager->proxy_lists)
611 list = g_hash_table_lookup (manager->proxy_lists, tri);
616 g_print ("proxy got %s,%s,%s = list %p\n",
618 tri + strlen (tri) + 1,
619 tri + strlen (tri) + 1 + strlen (tri + strlen (tri) + 1) + 1,
625 /* Emit the signal */
632 copy = g_slist_copy (list->proxies);
633 g_slist_foreach (copy, (GFunc) g_object_ref, NULL);
640 proxy = DBUS_G_PROXY (tmp->data);
642 UNLOCK_MANAGER (manager);
643 dbus_g_proxy_emit_received (proxy, message);
644 g_object_unref (G_OBJECT (proxy));
645 LOCK_MANAGER (manager);
654 UNLOCK_MANAGER (manager);
655 dbus_g_proxy_manager_unref (manager);
657 /* "Handling" signals doesn't make sense, they are for everyone
660 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
665 /* ---------- DBusGProxy -------------- */
676 static void *parent_class;
677 static guint signals[LAST_SIGNAL] = { 0 };
680 dbus_g_proxy_init (DBusGProxy *proxy)
686 dbus_g_proxy_class_init (DBusGProxyClass *klass)
688 GObjectClass *object_class = G_OBJECT_CLASS (klass);
690 parent_class = g_type_class_peek_parent (klass);
692 object_class->finalize = dbus_g_proxy_finalize;
693 object_class->dispose = dbus_g_proxy_dispose;
696 g_signal_new ("destroy",
697 G_OBJECT_CLASS_TYPE (object_class),
698 G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
701 g_cclosure_marshal_VOID__VOID,
705 g_signal_new ("received",
706 G_OBJECT_CLASS_TYPE (object_class),
707 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
710 g_cclosure_marshal_VOID__BOXED,
717 dbus_g_proxy_dispose (GObject *object)
721 proxy = DBUS_G_PROXY (object);
723 g_signal_emit (object, signals[DESTROY], 0);
725 G_OBJECT_CLASS (parent_class)->dispose (object);
729 dbus_g_proxy_finalize (GObject *object)
733 proxy = DBUS_G_PROXY (object);
737 dbus_g_proxy_manager_unregister (proxy->manager, proxy);
738 dbus_g_proxy_manager_unref (proxy->manager);
741 g_free (proxy->service);
742 g_free (proxy->path);
743 g_free (proxy->interface);
745 G_OBJECT_CLASS (parent_class)->finalize (object);
749 dbus_g_proxy_destroy (DBusGProxy *proxy)
751 /* FIXME do we need the GTK_IN_DESTRUCTION style flag
754 g_object_run_dispose (G_OBJECT (proxy));
758 create_signal_detail (const char *interface,
763 str = g_string_new (interface);
765 g_string_append (str, ".");
767 g_string_append (str, signal);
769 return g_string_free (str, FALSE);
773 dbus_g_proxy_emit_received (DBusGProxy *proxy,
774 DBusMessage *message)
776 const char *interface;
781 interface = dbus_message_get_interface (message);
782 signal = dbus_message_get_member (message);
784 g_assert (interface != NULL);
785 g_assert (signal != NULL);
787 detail = create_signal_detail (interface, signal);
789 /* If the quark isn't preexisting, there's no way there
790 * are any handlers connected. We don't want to create
791 * extra quarks for every possible signal.
793 q = g_quark_try_string (detail);
796 g_signal_emit (G_OBJECT (proxy),
804 /** @} End of DBusGLibInternals */
806 /** @addtogroup DBusGLib
811 * Standard GObject get_type() function for DBusGProxy.
813 * @returns type ID for DBusGProxy class
816 dbus_g_proxy_get_type (void)
818 static GType object_type = 0;
822 static const GTypeInfo object_info =
824 sizeof (DBusGProxyClass),
825 (GBaseInitFunc) NULL,
826 (GBaseFinalizeFunc) NULL,
827 (GClassInitFunc) dbus_g_proxy_class_init,
828 NULL, /* class_finalize */
829 NULL, /* class_data */
832 (GInstanceInitFunc) dbus_g_proxy_init,
835 object_type = g_type_register_static (G_TYPE_OBJECT,
844 dbus_g_proxy_new (DBusGConnection *connection,
845 const char *service_name,
846 const char *path_name,
847 const char *interface_name)
851 g_assert (connection != NULL);
853 proxy = g_object_new (DBUS_TYPE_G_PROXY, NULL);
855 /* These should all be construct-only mandatory properties,
856 * for now we just don't let people use g_object_new().
859 proxy->manager = dbus_g_proxy_manager_get (DBUS_CONNECTION_FROM_G_CONNECTION (connection));
861 proxy->service = g_strdup (service_name);
862 proxy->path = g_strdup (path_name);
863 proxy->interface = g_strdup (interface_name);
865 dbus_g_proxy_manager_register (proxy->manager, proxy);
871 * Creates a new proxy for a remote interface exported by a service on
872 * a message bus. Method calls and signal connections over this proxy
873 * will go to the service owner; the service owner is expected to
874 * support the given interface name. THE SERVICE OWNER MAY CHANGE OVER
875 * TIME, for example between two different method calls. If you need a
876 * fixed owner, you need to request the current owner and bind a proxy
877 * to that rather than to the generic service name; see
878 * dbus_g_proxy_new_for_service_owner().
880 * A service-associated proxy only makes sense with a message bus,
881 * not for app-to-app direct dbus connections.
883 * This proxy will only emit the "destroy" signal if the #DBusConnection
884 * is disconnected or the proxy is has no remaining references.
886 * @param connection the connection to the remote bus
887 * @param service_name name of the service on the message bus
888 * @param path_name name of the object inside the service to call methods on
889 * @param interface_name name of the interface to call methods on
890 * @returns new proxy object
893 dbus_g_proxy_new_for_service (DBusGConnection *connection,
894 const char *service_name,
895 const char *path_name,
896 const char *interface_name)
900 g_return_val_if_fail (connection != NULL, NULL);
901 g_return_val_if_fail (service_name != NULL, NULL);
902 g_return_val_if_fail (path_name != NULL, NULL);
903 g_return_val_if_fail (interface_name != NULL, NULL);
905 proxy = dbus_g_proxy_new (connection, service_name,
906 path_name, interface_name);
912 * Similar to dbus_g_proxy_new_for_service(), but makes a round-trip
913 * request to the message bus to get the current service owner, then
914 * binds the proxy specifically to the current owner. As a result, the
915 * service owner will not change over time, and the proxy will emit
916 * the "destroy" signal when the owner disappears from the message
919 * An example of the difference between dbus_g_proxy_new_for_service()
920 * and dbus_g_proxy_new_for_service_owner(): if you pass the service name
921 * "org.freedesktop.Database" dbus_g_proxy_new_for_service() remains bound
922 * to that name as it changes owner. dbus_g_proxy_new_for_service_owner()
923 * will fail if the service has no owner. If the service has an owner,
924 * dbus_g_proxy_new_for_service_owner() will bind to the unique name
925 * of that owner rather than the generic service name.
927 * @param connection the connection to the remote bus
928 * @param service_name name of the service on the message bus
929 * @param path_name name of the object inside the service to call methods on
930 * @param interface_name name of the interface to call methods on
931 * @param error return location for an error
932 * @returns new proxy object, or #NULL on error
935 dbus_g_proxy_new_for_service_owner (DBusGConnection *connection,
936 const char *service_name,
937 const char *path_name,
938 const char *interface_name,
942 DBusMessage *request, *reply;
944 char *base_service_name;
946 g_return_val_if_fail (connection != NULL, NULL);
947 g_return_val_if_fail (service_name != NULL, NULL);
948 g_return_val_if_fail (path_name != NULL, NULL);
949 g_return_val_if_fail (interface_name != NULL, NULL);
951 dbus_error_init (&derror);
954 base_service_name = NULL;
957 request = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
958 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
959 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
962 g_error ("Out of memory");
964 if (! dbus_message_append_args (request,
965 DBUS_TYPE_STRING, service_name,
967 g_error ("Out of memory");
970 dbus_connection_send_with_reply_and_block (DBUS_CONNECTION_FROM_G_CONNECTION (connection),
976 if (dbus_set_error_from_message (&derror, reply))
979 if (! dbus_message_get_args (reply, &derror,
980 DBUS_TYPE_STRING, &base_service_name,
985 proxy = dbus_g_proxy_new (connection, base_service_name,
986 path_name, interface_name);
991 g_assert (dbus_error_is_set (&derror));
992 dbus_set_g_error (error, &derror);
993 dbus_error_free (&derror);
997 dbus_message_unref (request);
999 dbus_message_unref (reply);
1000 dbus_free (base_service_name);
1006 * Creates a proxy for an object in peer application (one
1007 * we're directly connected to). That is, this function is
1008 * intended for use when there's no message bus involved,
1009 * we're doing a simple 1-to-1 communication between two
1013 * @param connection the connection to the peer
1014 * @param path_name name of the object inside the peer to call methods on
1015 * @param interface_name name of the interface to call methods on
1016 * @returns new proxy object
1020 dbus_g_proxy_new_for_peer (DBusGConnection *connection,
1021 const char *path_name,
1022 const char *interface_name)
1026 g_return_val_if_fail (connection != NULL, NULL);
1027 g_return_val_if_fail (path_name != NULL, NULL);
1028 g_return_val_if_fail (interface_name != NULL, NULL);
1030 proxy = dbus_g_proxy_new (connection, NULL,
1031 path_name, interface_name);
1037 * Invokes a method on a remote interface. This function does not
1038 * block; instead it returns an opaque #DBusPendingCall object that
1039 * tracks the pending call. The method call will not be sent over the
1040 * wire until the application returns to the main loop, or blocks in
1041 * dbus_connection_flush() to write out pending data. The call will
1042 * be completed after a timeout, or when a reply is received.
1043 * To collect the results of the call (which may be an error,
1044 * or a reply), use dbus_g_proxy_end_call().
1046 * @todo this particular function shouldn't die on out of memory,
1047 * since you should be able to do a call with large arguments.
1049 * @param proxy a proxy for a remote interface
1050 * @param method the name of the method to invoke
1051 * @param first_arg_type type of the first argument
1053 * @returns opaque pending call object
1056 dbus_g_proxy_begin_call (DBusGProxy *proxy,
1061 DBusPendingCall *pending;
1062 DBusMessage *message;
1065 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), NULL);
1067 message = dbus_message_new_method_call (proxy->service,
1071 if (message == NULL)
1074 va_start (args, first_arg_type);
1075 if (!dbus_message_append_args_valist (message, first_arg_type,
1080 if (!dbus_connection_send_with_reply (proxy->manager->connection,
1086 return DBUS_G_PENDING_CALL_FROM_PENDING_CALL (pending);
1089 /* FIXME we should create a pending call that's
1090 * immediately completed with an error status without
1091 * ever going on the wire.
1094 g_error ("Out of memory");
1099 * Collects the results of a method call. The method call was normally
1100 * initiated with dbus_g_proxy_end_call(). This function will block if
1101 * the results haven't yet been received; use
1102 * dbus_pending_call_set_notify() to be notified asynchronously that a
1103 * pending call has been completed. Use
1104 * dbus_pending_call_get_completed() to check whether a call has been
1105 * completed. If it's completed, it will not block.
1107 * If the call results in an error, the error is set as normal for
1108 * GError and the function returns #FALSE.
1110 * Otherwise, the "out" parameters and return value of the
1111 * method are stored in the provided varargs list.
1112 * The list should be terminated with DBUS_TYPE_INVALID.
1114 * This function doesn't affect the reference count of the
1115 * #DBusPendingCall, the caller of dbus_g_proxy_begin_call() still owns
1118 * @param proxy a proxy for a remote interface
1119 * @param pending the pending call from dbus_g_proxy_begin_call()
1120 * @param error return location for an error
1121 * @param first_arg_type type of first "out" argument
1122 * @returns #FALSE if an error is set */
1124 dbus_g_proxy_end_call (DBusGProxy *proxy,
1125 DBusGPendingCall *pending,
1130 DBusMessage *message;
1134 g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), FALSE);
1135 g_return_val_if_fail (pending != NULL, FALSE);
1137 dbus_pending_call_block (DBUS_PENDING_CALL_FROM_G_PENDING_CALL (pending));
1138 message = dbus_pending_call_get_reply (DBUS_PENDING_CALL_FROM_G_PENDING_CALL (pending));
1140 g_assert (message != NULL);
1142 dbus_error_init (&derror);
1144 switch (dbus_message_get_type (message))
1146 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1147 va_start (args, first_arg_type);
1148 if (!dbus_message_get_args_valist (message, &derror, first_arg_type, args))
1157 case DBUS_MESSAGE_TYPE_ERROR:
1158 dbus_set_error_from_message (&derror, message);
1162 dbus_set_error (&derror, DBUS_ERROR_FAILED,
1163 "Reply was neither a method return nor an exception");
1168 dbus_set_g_error (error, &derror);
1169 dbus_error_free (&derror);
1174 * Sends a method call message as with dbus_g_proxy_begin_call(), but
1175 * does not ask for a reply or allow you to receive one.
1177 * @todo this particular function shouldn't die on out of memory,
1178 * since you should be able to do a call with large arguments.
1180 * @param proxy a proxy for a remote interface
1181 * @param method the name of the method to invoke
1182 * @param first_arg_type type of the first argument
1185 dbus_g_proxy_call_no_reply (DBusGProxy *proxy,
1190 DBusMessage *message;
1193 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
1195 message = dbus_message_new_method_call (proxy->service,
1199 if (message == NULL)
1202 dbus_message_set_no_reply (message, TRUE);
1204 va_start (args, first_arg_type);
1205 if (!dbus_message_append_args_valist (message, first_arg_type,
1210 if (!dbus_connection_send (proxy->manager->connection,
1218 g_error ("Out of memory");
1222 * Sends a message to the interface we're proxying for. Does not
1223 * block or wait for a reply. The message is only actually written out
1224 * when you return to the main loop or block in
1225 * dbus_connection_flush().
1227 * The message is modified to be addressed to the target interface.
1228 * That is, a destination service field or whatever is needed will be
1229 * added to the message. The basic point of this function is to add
1230 * the necessary header fields, otherwise it's equivalent to
1231 * dbus_connection_send().
1233 * This function adds a reference to the message, so the caller
1234 * still owns its original reference.
1236 * @param proxy a proxy for a remote interface
1237 * @param message the message to address and send
1238 * @param client_serial return location for message's serial, or #NULL */
1240 dbus_g_proxy_send (DBusGProxy *proxy,
1241 DBusMessage *message,
1242 dbus_uint32_t *client_serial)
1244 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
1248 if (!dbus_message_set_destination (message, proxy->service))
1249 g_error ("Out of memory");
1253 if (!dbus_message_set_path (message, proxy->path))
1254 g_error ("Out of memory");
1256 if (proxy->interface)
1258 if (!dbus_message_set_interface (message, proxy->interface))
1259 g_error ("Out of memory");
1262 if (!dbus_connection_send (proxy->manager->connection, message, client_serial))
1263 g_error ("Out of memory\n");
1267 * Connect a signal handler to a proxy for a remote interface. When
1268 * the remote interface emits the specified signal, the proxy will
1269 * emit a corresponding GLib signal.
1271 * @todo Right now there's no way to specify the signature to use
1272 * for invoking the GCallback. Need to either rely on introspection,
1273 * or require signature here.
1275 * @param proxy a proxy for a remote interface
1276 * @param signal_name the DBus signal name to listen for
1277 * @param handler the handler to connect
1278 * @param data data to pass to handler
1279 * @param free_data_func callback function to destroy data
1282 dbus_g_proxy_connect_signal (DBusGProxy *proxy,
1283 const char *signal_name,
1286 GClosureNotify free_data_func)
1291 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
1292 g_return_if_fail (signal_name != NULL);
1293 g_return_if_fail (handler != NULL);
1295 detail = create_signal_detail (proxy->interface, signal_name);
1297 closure = g_cclosure_new (G_CALLBACK (handler), data, free_data_func);
1298 g_signal_connect_closure_by_id (G_OBJECT (proxy),
1300 g_quark_from_string (detail),
1307 * Disconnect all signal handlers from a proxy that match the given
1310 * @param proxy a proxy for a remote interface
1311 * @param signal_name the DBus signal name to disconnect
1312 * @param handler the handler to disconnect
1313 * @param data the data that was registered with handler
1316 dbus_g_proxy_disconnect_signal (DBusGProxy *proxy,
1317 const char *signal_name,
1324 g_return_if_fail (DBUS_IS_G_PROXY (proxy));
1325 g_return_if_fail (signal_name != NULL);
1326 g_return_if_fail (handler != NULL);
1328 detail = create_signal_detail (proxy->interface, signal_name);
1329 q = g_quark_try_string (detail);
1332 #ifndef G_DISABLE_CHECKS
1335 g_warning ("%s: No signal handlers for %s found on this DBusGProxy",
1336 G_GNUC_FUNCTION, signal_name);
1341 g_signal_handlers_disconnect_matched (G_OBJECT (proxy),
1342 G_SIGNAL_MATCH_DETAIL |
1343 G_SIGNAL_MATCH_FUNC |
1344 G_SIGNAL_MATCH_DATA,
1348 G_CALLBACK (handler), data);
1351 /** @} End of DBusGLib public */
1353 #ifdef DBUS_BUILD_TESTS
1356 * @ingroup DBusGLibInternals
1357 * Unit test for GLib proxy functions
1358 * @returns #TRUE on success.
1361 _dbus_g_proxy_test (void)
1368 #endif /* DBUS_BUILD_TESTS */