1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #ifndef __G_DBUS_CONNECTION_H__
22 #define __G_DBUS_CONNECTION_H__
24 #if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
25 #error "Only <gio/gio.h> can be included directly."
28 #include <gio/giotypes.h>
32 #define G_TYPE_DBUS_CONNECTION (g_dbus_connection_get_type ())
33 #define G_DBUS_CONNECTION(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_DBUS_CONNECTION, GDBusConnection))
34 #define G_IS_DBUS_CONNECTION(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_DBUS_CONNECTION))
37 GType g_dbus_connection_get_type (void) G_GNUC_CONST;
39 /* ---------------------------------------------------------------------------------------------------- */
42 void g_bus_get (GBusType bus_type,
43 GCancellable *cancellable,
44 GAsyncReadyCallback callback,
47 GDBusConnection *g_bus_get_finish (GAsyncResult *res,
50 GDBusConnection *g_bus_get_sync (GBusType bus_type,
51 GCancellable *cancellable,
54 /* ---------------------------------------------------------------------------------------------------- */
57 void g_dbus_connection_new (GIOStream *stream,
59 GDBusConnectionFlags flags,
60 GDBusAuthObserver *observer,
61 GCancellable *cancellable,
62 GAsyncReadyCallback callback,
65 GDBusConnection *g_dbus_connection_new_finish (GAsyncResult *res,
68 GDBusConnection *g_dbus_connection_new_sync (GIOStream *stream,
70 GDBusConnectionFlags flags,
71 GDBusAuthObserver *observer,
72 GCancellable *cancellable,
76 void g_dbus_connection_new_for_address (const gchar *address,
77 GDBusConnectionFlags flags,
78 GDBusAuthObserver *observer,
79 GCancellable *cancellable,
80 GAsyncReadyCallback callback,
83 GDBusConnection *g_dbus_connection_new_for_address_finish (GAsyncResult *res,
86 GDBusConnection *g_dbus_connection_new_for_address_sync (const gchar *address,
87 GDBusConnectionFlags flags,
88 GDBusAuthObserver *observer,
89 GCancellable *cancellable,
92 /* ---------------------------------------------------------------------------------------------------- */
94 GLIB_AVAILABLE_IN_2_40
95 gchar *g_dbus_get_bus_id (GDBusConnection *connection,
97 GLIB_AVAILABLE_IN_2_40
98 gchar **g_dbus_get_list_names (GDBusConnection *connection,
100 GLIB_AVAILABLE_IN_2_40
101 gchar **g_dbus_get_list_activatable_names (GDBusConnection *connection,
103 GLIB_AVAILABLE_IN_2_40
104 gchar **g_dbus_get_list_queued_owners (GDBusConnection *connection,
107 GLIB_AVAILABLE_IN_2_40
108 gchar *g_dbus_get_name_owner (GDBusConnection *connection,
111 GLIB_AVAILABLE_IN_2_40
112 gboolean g_dbus_name_has_owner (GDBusConnection *connection,
115 GLIB_AVAILABLE_IN_2_40
116 guint32 g_dbus_get_connection_pid (GDBusConnection *connection,
119 GLIB_AVAILABLE_IN_2_40
120 guint32 g_dbus_get_connection_uid (GDBusConnection *connection,
124 /* ---------------------------------------------------------------------------------------------------- */
126 GLIB_AVAILABLE_IN_ALL
127 void g_dbus_connection_start_message_processing (GDBusConnection *connection);
128 GLIB_AVAILABLE_IN_ALL
129 gboolean g_dbus_connection_is_closed (GDBusConnection *connection);
130 GLIB_AVAILABLE_IN_ALL
131 GIOStream *g_dbus_connection_get_stream (GDBusConnection *connection);
132 GLIB_AVAILABLE_IN_ALL
133 const gchar *g_dbus_connection_get_guid (GDBusConnection *connection);
134 GLIB_AVAILABLE_IN_ALL
135 const gchar *g_dbus_connection_get_unique_name (GDBusConnection *connection);
136 GLIB_AVAILABLE_IN_ALL
137 GCredentials *g_dbus_connection_get_peer_credentials (GDBusConnection *connection);
139 GLIB_AVAILABLE_IN_2_34
140 guint32 g_dbus_connection_get_last_serial (GDBusConnection *connection);
142 GLIB_AVAILABLE_IN_ALL
143 gboolean g_dbus_connection_get_exit_on_close (GDBusConnection *connection);
144 GLIB_AVAILABLE_IN_ALL
145 void g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
146 gboolean exit_on_close);
147 GLIB_AVAILABLE_IN_ALL
148 GDBusCapabilityFlags g_dbus_connection_get_capabilities (GDBusConnection *connection);
150 /* ---------------------------------------------------------------------------------------------------- */
152 GLIB_AVAILABLE_IN_ALL
153 void g_dbus_connection_close (GDBusConnection *connection,
154 GCancellable *cancellable,
155 GAsyncReadyCallback callback,
157 GLIB_AVAILABLE_IN_ALL
158 gboolean g_dbus_connection_close_finish (GDBusConnection *connection,
161 GLIB_AVAILABLE_IN_ALL
162 gboolean g_dbus_connection_close_sync (GDBusConnection *connection,
163 GCancellable *cancellable,
166 /* ---------------------------------------------------------------------------------------------------- */
168 GLIB_AVAILABLE_IN_ALL
169 void g_dbus_connection_flush (GDBusConnection *connection,
170 GCancellable *cancellable,
171 GAsyncReadyCallback callback,
173 GLIB_AVAILABLE_IN_ALL
174 gboolean g_dbus_connection_flush_finish (GDBusConnection *connection,
177 GLIB_AVAILABLE_IN_ALL
178 gboolean g_dbus_connection_flush_sync (GDBusConnection *connection,
179 GCancellable *cancellable,
182 /* ---------------------------------------------------------------------------------------------------- */
184 GLIB_AVAILABLE_IN_ALL
185 gboolean g_dbus_connection_send_message (GDBusConnection *connection,
186 GDBusMessage *message,
187 GDBusSendMessageFlags flags,
188 volatile guint32 *out_serial,
190 GLIB_AVAILABLE_IN_ALL
191 void g_dbus_connection_send_message_with_reply (GDBusConnection *connection,
192 GDBusMessage *message,
193 GDBusSendMessageFlags flags,
195 volatile guint32 *out_serial,
196 GCancellable *cancellable,
197 GAsyncReadyCallback callback,
199 GLIB_AVAILABLE_IN_ALL
200 GDBusMessage *g_dbus_connection_send_message_with_reply_finish (GDBusConnection *connection,
203 GLIB_AVAILABLE_IN_ALL
204 GDBusMessage *g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection,
205 GDBusMessage *message,
206 GDBusSendMessageFlags flags,
208 volatile guint32 *out_serial,
209 GCancellable *cancellable,
212 /* ---------------------------------------------------------------------------------------------------- */
214 GLIB_AVAILABLE_IN_ALL
215 gboolean g_dbus_connection_emit_signal (GDBusConnection *connection,
216 const gchar *destination_bus_name,
217 const gchar *object_path,
218 const gchar *interface_name,
219 const gchar *signal_name,
220 GVariant *parameters,
222 GLIB_AVAILABLE_IN_ALL
223 void g_dbus_connection_call (GDBusConnection *connection,
224 const gchar *bus_name,
225 const gchar *object_path,
226 const gchar *interface_name,
227 const gchar *method_name,
228 GVariant *parameters,
229 const GVariantType *reply_type,
230 GDBusCallFlags flags,
232 GCancellable *cancellable,
233 GAsyncReadyCallback callback,
235 GLIB_AVAILABLE_IN_ALL
236 GVariant *g_dbus_connection_call_finish (GDBusConnection *connection,
239 GLIB_AVAILABLE_IN_ALL
240 GVariant *g_dbus_connection_call_sync (GDBusConnection *connection,
241 const gchar *bus_name,
242 const gchar *object_path,
243 const gchar *interface_name,
244 const gchar *method_name,
245 GVariant *parameters,
246 const GVariantType *reply_type,
247 GDBusCallFlags flags,
249 GCancellable *cancellable,
251 GLIB_AVAILABLE_IN_2_30
252 void g_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
253 const gchar *bus_name,
254 const gchar *object_path,
255 const gchar *interface_name,
256 const gchar *method_name,
257 GVariant *parameters,
258 const GVariantType *reply_type,
259 GDBusCallFlags flags,
261 GUnixFDList *fd_list,
262 GCancellable *cancellable,
263 GAsyncReadyCallback callback,
265 GLIB_AVAILABLE_IN_2_30
266 GVariant *g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection *connection,
267 GUnixFDList **out_fd_list,
270 GLIB_AVAILABLE_IN_2_30
271 GVariant *g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection *connection,
272 const gchar *bus_name,
273 const gchar *object_path,
274 const gchar *interface_name,
275 const gchar *method_name,
276 GVariant *parameters,
277 const GVariantType *reply_type,
278 GDBusCallFlags flags,
280 GUnixFDList *fd_list,
281 GUnixFDList **out_fd_list,
282 GCancellable *cancellable,
285 /* ---------------------------------------------------------------------------------------------------- */
289 * GDBusInterfaceMethodCallFunc:
290 * @connection: A #GDBusConnection.
291 * @sender: The unique bus name of the remote caller.
292 * @object_path: The object path that the method was invoked on.
293 * @interface_name: The D-Bus interface name the method was invoked on.
294 * @method_name: The name of the method that was invoked.
295 * @parameters: A #GVariant tuple with parameters.
296 * @invocation: (transfer full): A #GDBusMethodInvocation object that must be used to return a value or error.
297 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
299 * The type of the @method_call function in #GDBusInterfaceVTable.
303 typedef void (*GDBusInterfaceMethodCallFunc) (GDBusConnection *connection,
305 const gchar *object_path,
306 const gchar *interface_name,
307 const gchar *method_name,
308 GVariant *parameters,
309 GDBusMethodInvocation *invocation,
313 * GDBusInterfaceGetPropertyFunc:
314 * @connection: A #GDBusConnection.
315 * @sender: The unique bus name of the remote caller.
316 * @object_path: The object path that the method was invoked on.
317 * @interface_name: The D-Bus interface name for the property.
318 * @property_name: The name of the property to get the value of.
319 * @error: Return location for error.
320 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
322 * The type of the @get_property function in #GDBusInterfaceVTable.
324 * Returns: A #GVariant with the value for @property_name or %NULL if
325 * @error is set. If the returned #GVariant is floating, it is
326 * consumed - otherwise its reference count is decreased by one.
330 typedef GVariant *(*GDBusInterfaceGetPropertyFunc) (GDBusConnection *connection,
332 const gchar *object_path,
333 const gchar *interface_name,
334 const gchar *property_name,
339 * GDBusInterfaceSetPropertyFunc:
340 * @connection: A #GDBusConnection.
341 * @sender: The unique bus name of the remote caller.
342 * @object_path: The object path that the method was invoked on.
343 * @interface_name: The D-Bus interface name for the property.
344 * @property_name: The name of the property to get the value of.
345 * @value: The value to set the property to.
346 * @error: Return location for error.
347 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
349 * The type of the @set_property function in #GDBusInterfaceVTable.
351 * Returns: %TRUE if the property was set to @value, %FALSE if @error is set.
355 typedef gboolean (*GDBusInterfaceSetPropertyFunc) (GDBusConnection *connection,
357 const gchar *object_path,
358 const gchar *interface_name,
359 const gchar *property_name,
365 * GDBusInterfaceVTable:
366 * @method_call: Function for handling incoming method calls.
367 * @get_property: Function for getting a property.
368 * @set_property: Function for setting a property.
370 * Virtual table for handling properties and method calls for a D-Bus
373 * Since 2.38, if you want to handle getting/setting D-Bus properties
374 * asynchronously, give %NULL as your get_property() or set_property()
375 * function. The D-Bus call will be directed to your @method_call function,
376 * with the provided @interface_name set to "org.freedesktop.DBus.Properties".
378 * Ownership of the #GDBusMethodInvocation object passed to the
379 * method_call() function is transferred to your handler; you must
380 * call one of the methods of #GDBusMethodInvocation to return a reply
381 * (possibly empty), or an error. These functions also take ownership
382 * of the passed-in invocation object, so unless the invocation
383 * object has otherwise been referenced, it will be then be freed.
384 * Calling one of these functions may be done within your
385 * method_call() implementation but it also can be done at a later
386 * point to handle the method asynchronously.
388 * The usual checks on the validity of the calls is performed. For
389 * `Get` calls, an error is automatically returned if the property does
390 * not exist or the permissions do not allow access. The same checks are
391 * performed for `Set` calls, and the provided value is also checked for
392 * being the correct type.
394 * For both `Get` and `Set` calls, the #GDBusMethodInvocation
395 * passed to the @method_call handler can be queried with
396 * g_dbus_method_invocation_get_property_info() to get a pointer
397 * to the #GDBusPropertyInfo of the property.
399 * If you have readable properties specified in your interface info,
400 * you must ensure that you either provide a non-%NULL @get_property()
401 * function or provide implementations of both the `Get` and `GetAll`
402 * methods on org.freedesktop.DBus.Properties interface in your @method_call
403 * function. Note that the required return type of the `Get` call is
404 * `(v)`, not the type of the property. `GetAll` expects a return value
407 * If you have writable properties specified in your interface info,
408 * you must ensure that you either provide a non-%NULL @set_property()
409 * function or provide an implementation of the `Set` call. If implementing
410 * the call, you must return the value of type %G_VARIANT_TYPE_UNIT.
414 struct _GDBusInterfaceVTable
416 GDBusInterfaceMethodCallFunc method_call;
417 GDBusInterfaceGetPropertyFunc get_property;
418 GDBusInterfaceSetPropertyFunc set_property;
421 /* Padding for future expansion - also remember to update
422 * gdbusconnection.c:_g_dbus_interface_vtable_copy() when
428 GLIB_AVAILABLE_IN_ALL
429 guint g_dbus_connection_register_object (GDBusConnection *connection,
430 const gchar *object_path,
431 GDBusInterfaceInfo *interface_info,
432 const GDBusInterfaceVTable *vtable,
434 GDestroyNotify user_data_free_func,
436 GLIB_AVAILABLE_IN_ALL
437 gboolean g_dbus_connection_unregister_object (GDBusConnection *connection,
438 guint registration_id);
440 /* ---------------------------------------------------------------------------------------------------- */
443 * GDBusSubtreeEnumerateFunc:
444 * @connection: A #GDBusConnection.
445 * @sender: The unique bus name of the remote caller.
446 * @object_path: The object path that was registered with g_dbus_connection_register_subtree().
447 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_subtree().
449 * The type of the @enumerate function in #GDBusSubtreeVTable.
451 * This function is called when generating introspection data and also
452 * when preparing to dispatch incoming messages in the event that the
453 * %G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is not
454 * specified (ie: to verify that the object path is valid).
456 * Hierarchies are not supported; the items that you return should not
457 * contain the '/' character.
459 * The return value will be freed with g_strfreev().
461 * Returns: A newly allocated array of strings for node names that are children of @object_path.
465 typedef gchar** (*GDBusSubtreeEnumerateFunc) (GDBusConnection *connection,
467 const gchar *object_path,
471 * GDBusSubtreeIntrospectFunc:
472 * @connection: A #GDBusConnection.
473 * @sender: The unique bus name of the remote caller.
474 * @object_path: The object path that was registered with g_dbus_connection_register_subtree().
475 * @node: A node that is a child of @object_path (relative to @object_path) or %NULL for the root of the subtree.
476 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_subtree().
478 * The type of the @introspect function in #GDBusSubtreeVTable.
480 * Subtrees are flat. @node, if non-%NULL, is always exactly one
481 * segment of the object path (ie: it never contains a slash).
483 * This function should return %NULL to indicate that there is no object
486 * If this function returns non-%NULL, the return value is expected to
487 * be a %NULL-terminated array of pointers to #GDBusInterfaceInfo
488 * structures describing the interfaces implemented by @node. This
489 * array will have g_dbus_interface_info_unref() called on each item
490 * before being freed with g_free().
492 * The difference between returning %NULL and an array containing zero
493 * items is that the standard DBus interfaces will returned to the
494 * remote introspector in the empty array case, but not in the %NULL
497 * Returns: A %NULL-terminated array of pointers to #GDBusInterfaceInfo, or %NULL.
501 typedef GDBusInterfaceInfo ** (*GDBusSubtreeIntrospectFunc) (GDBusConnection *connection,
503 const gchar *object_path,
508 * GDBusSubtreeDispatchFunc:
509 * @connection: A #GDBusConnection.
510 * @sender: The unique bus name of the remote caller.
511 * @object_path: The object path that was registered with g_dbus_connection_register_subtree().
512 * @interface_name: The D-Bus interface name that the method call or property access is for.
513 * @node: A node that is a child of @object_path (relative to @object_path) or %NULL for the root of the subtree.
514 * @out_user_data: Return location for user data to pass to functions in the returned #GDBusInterfaceVTable (never %NULL).
515 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_subtree().
517 * The type of the @dispatch function in #GDBusSubtreeVTable.
519 * Subtrees are flat. @node, if non-%NULL, is always exactly one
520 * segment of the object path (ie: it never contains a slash).
522 * Returns: A #GDBusInterfaceVTable or %NULL if you don't want to handle the methods.
526 typedef const GDBusInterfaceVTable * (*GDBusSubtreeDispatchFunc) (GDBusConnection *connection,
528 const gchar *object_path,
529 const gchar *interface_name,
531 gpointer *out_user_data,
535 * GDBusSubtreeVTable:
536 * @enumerate: Function for enumerating child nodes.
537 * @introspect: Function for introspecting a child node.
538 * @dispatch: Function for dispatching a remote call on a child node.
540 * Virtual table for handling subtrees registered with g_dbus_connection_register_subtree().
544 struct _GDBusSubtreeVTable
546 GDBusSubtreeEnumerateFunc enumerate;
547 GDBusSubtreeIntrospectFunc introspect;
548 GDBusSubtreeDispatchFunc dispatch;
551 /* Padding for future expansion - also remember to update
552 * gdbusconnection.c:_g_dbus_subtree_vtable_copy() when
558 GLIB_AVAILABLE_IN_ALL
559 guint g_dbus_connection_register_subtree (GDBusConnection *connection,
560 const gchar *object_path,
561 const GDBusSubtreeVTable *vtable,
562 GDBusSubtreeFlags flags,
564 GDestroyNotify user_data_free_func,
566 GLIB_AVAILABLE_IN_ALL
567 gboolean g_dbus_connection_unregister_subtree (GDBusConnection *connection,
568 guint registration_id);
570 /* ---------------------------------------------------------------------------------------------------- */
573 * GDBusSignalCallback:
574 * @connection: A #GDBusConnection.
575 * @sender_name: The unique bus name of the sender of the signal.
576 * @object_path: The object path that the signal was emitted on.
577 * @interface_name: The name of the interface.
578 * @signal_name: The name of the signal.
579 * @parameters: A #GVariant tuple with parameters for the signal.
580 * @user_data: User data passed when subscribing to the signal.
582 * Signature for callback function used in g_dbus_connection_signal_subscribe().
586 typedef void (*GDBusSignalCallback) (GDBusConnection *connection,
587 const gchar *sender_name,
588 const gchar *object_path,
589 const gchar *interface_name,
590 const gchar *signal_name,
591 GVariant *parameters,
594 GLIB_AVAILABLE_IN_ALL
595 guint g_dbus_connection_signal_subscribe (GDBusConnection *connection,
597 const gchar *interface_name,
599 const gchar *object_path,
601 GDBusSignalFlags flags,
602 GDBusSignalCallback callback,
604 GDestroyNotify user_data_free_func);
605 GLIB_AVAILABLE_IN_ALL
606 void g_dbus_connection_signal_unsubscribe (GDBusConnection *connection,
607 guint subscription_id);
609 /* ---------------------------------------------------------------------------------------------------- */
612 * GDBusMessageFilterFunction:
613 * @connection: (transfer none): A #GDBusConnection.
614 * @message: (transfer full): A locked #GDBusMessage that the filter function takes ownership of.
615 * @incoming: %TRUE if it is a message received from the other peer, %FALSE if it is
616 * a message to be sent to the other peer.
617 * @user_data: User data passed when adding the filter.
619 * Signature for function used in g_dbus_connection_add_filter().
621 * A filter function is passed a #GDBusMessage and expected to return
622 * a #GDBusMessage too. Passive filter functions that don't modify the
623 * message can simply return the @message object:
625 * static GDBusMessage *
626 * passive_filter (GDBusConnection *connection
627 * GDBusMessage *message,
629 * gpointer user_data)
631 * /<!-- -->* inspect @message *<!-- -->/
635 * Filter functions that wants to drop a message can simply return %NULL:
637 * static GDBusMessage *
638 * drop_filter (GDBusConnection *connection
639 * GDBusMessage *message,
641 * gpointer user_data)
643 * if (should_drop_message)
645 * g_object_unref (message);
651 * Finally, a filter function may modify a message by copying it:
653 * static GDBusMessage *
654 * modifying_filter (GDBusConnection *connection
655 * GDBusMessage *message,
657 * gpointer user_data)
659 * GDBusMessage *copy;
663 * copy = g_dbus_message_copy (message, &error);
664 * /<!-- -->* handle @error being is set *<!-- -->/
665 * g_object_unref (message);
667 * /<!-- -->* modify @copy *<!-- -->/
672 * If the returned #GDBusMessage is different from @message and cannot
673 * be sent on @connection (it could use features, such as file
674 * descriptors, not compatible with @connection), then a warning is
675 * logged to <emphasis>standard error</emphasis>. Applications can
676 * check this ahead of time using g_dbus_message_to_blob() passing a
677 * #GDBusCapabilityFlags value obtained from @connection.
679 * Returns: (transfer full) (allow-none): A #GDBusMessage that will be freed with
680 * g_object_unref() or %NULL to drop the message. Passive filter
681 * functions can simply return the passed @message object.
685 typedef GDBusMessage *(*GDBusMessageFilterFunction) (GDBusConnection *connection,
686 GDBusMessage *message,
690 GLIB_AVAILABLE_IN_ALL
691 guint g_dbus_connection_add_filter (GDBusConnection *connection,
692 GDBusMessageFilterFunction filter_function,
694 GDestroyNotify user_data_free_func);
696 GLIB_AVAILABLE_IN_ALL
697 void g_dbus_connection_remove_filter (GDBusConnection *connection,
700 /* ---------------------------------------------------------------------------------------------------- */
705 #endif /* __G_DBUS_CONNECTION_H__ */