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,
98 GLIB_AVAILABLE_IN_2_40
99 gchar **g_dbus_get_list_names (GDBusConnection *connection,
102 GLIB_AVAILABLE_IN_2_40
103 gchar **g_dbus_get_list_activatable_names (GDBusConnection *connection,
106 GLIB_AVAILABLE_IN_2_40
107 gchar **g_dbus_get_list_queued_owners (GDBusConnection *connection,
111 GLIB_AVAILABLE_IN_2_40
112 gchar *g_dbus_get_name_owner (GDBusConnection *connection,
116 GLIB_AVAILABLE_IN_2_40
117 gboolean g_dbus_name_has_owner (GDBusConnection *connection,
121 GLIB_AVAILABLE_IN_2_40
122 guint32 g_dbus_get_connection_pid (GDBusConnection *connection,
126 GLIB_AVAILABLE_IN_2_40
127 guint32 g_dbus_get_connection_uid (GDBusConnection *connection,
132 /* ---------------------------------------------------------------------------------------------------- */
134 GLIB_AVAILABLE_IN_ALL
135 void g_dbus_connection_start_message_processing (GDBusConnection *connection);
136 GLIB_AVAILABLE_IN_ALL
137 gboolean g_dbus_connection_is_closed (GDBusConnection *connection);
138 GLIB_AVAILABLE_IN_ALL
139 GIOStream *g_dbus_connection_get_stream (GDBusConnection *connection);
140 GLIB_AVAILABLE_IN_ALL
141 const gchar *g_dbus_connection_get_guid (GDBusConnection *connection);
142 GLIB_AVAILABLE_IN_ALL
143 const gchar *g_dbus_connection_get_unique_name (GDBusConnection *connection);
144 GLIB_AVAILABLE_IN_ALL
145 GCredentials *g_dbus_connection_get_peer_credentials (GDBusConnection *connection);
147 GLIB_AVAILABLE_IN_2_34
148 guint32 g_dbus_connection_get_last_serial (GDBusConnection *connection);
150 GLIB_AVAILABLE_IN_ALL
151 gboolean g_dbus_connection_get_exit_on_close (GDBusConnection *connection);
152 GLIB_AVAILABLE_IN_ALL
153 void g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
154 gboolean exit_on_close);
155 GLIB_AVAILABLE_IN_ALL
156 GDBusCapabilityFlags g_dbus_connection_get_capabilities (GDBusConnection *connection);
158 /* ---------------------------------------------------------------------------------------------------- */
160 GLIB_AVAILABLE_IN_ALL
161 void g_dbus_connection_close (GDBusConnection *connection,
162 GCancellable *cancellable,
163 GAsyncReadyCallback callback,
165 GLIB_AVAILABLE_IN_ALL
166 gboolean g_dbus_connection_close_finish (GDBusConnection *connection,
169 GLIB_AVAILABLE_IN_ALL
170 gboolean g_dbus_connection_close_sync (GDBusConnection *connection,
171 GCancellable *cancellable,
174 /* ---------------------------------------------------------------------------------------------------- */
176 GLIB_AVAILABLE_IN_ALL
177 void g_dbus_connection_flush (GDBusConnection *connection,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 GLIB_AVAILABLE_IN_ALL
182 gboolean g_dbus_connection_flush_finish (GDBusConnection *connection,
185 GLIB_AVAILABLE_IN_ALL
186 gboolean g_dbus_connection_flush_sync (GDBusConnection *connection,
187 GCancellable *cancellable,
190 /* ---------------------------------------------------------------------------------------------------- */
192 GLIB_AVAILABLE_IN_ALL
193 gboolean g_dbus_connection_send_message (GDBusConnection *connection,
194 GDBusMessage *message,
195 GDBusSendMessageFlags flags,
196 volatile guint32 *out_serial,
198 GLIB_AVAILABLE_IN_ALL
199 void g_dbus_connection_send_message_with_reply (GDBusConnection *connection,
200 GDBusMessage *message,
201 GDBusSendMessageFlags flags,
203 volatile guint32 *out_serial,
204 GCancellable *cancellable,
205 GAsyncReadyCallback callback,
207 GLIB_AVAILABLE_IN_ALL
208 GDBusMessage *g_dbus_connection_send_message_with_reply_finish (GDBusConnection *connection,
211 GLIB_AVAILABLE_IN_ALL
212 GDBusMessage *g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection,
213 GDBusMessage *message,
214 GDBusSendMessageFlags flags,
216 volatile guint32 *out_serial,
217 GCancellable *cancellable,
220 /* ---------------------------------------------------------------------------------------------------- */
222 GLIB_AVAILABLE_IN_ALL
223 gboolean g_dbus_connection_emit_signal (GDBusConnection *connection,
224 const gchar *destination_bus_name,
225 const gchar *object_path,
226 const gchar *interface_name,
227 const gchar *signal_name,
228 GVariant *parameters,
230 GLIB_AVAILABLE_IN_ALL
231 void g_dbus_connection_call (GDBusConnection *connection,
232 const gchar *bus_name,
233 const gchar *object_path,
234 const gchar *interface_name,
235 const gchar *method_name,
236 GVariant *parameters,
237 const GVariantType *reply_type,
238 GDBusCallFlags flags,
240 GCancellable *cancellable,
241 GAsyncReadyCallback callback,
243 GLIB_AVAILABLE_IN_ALL
244 GVariant *g_dbus_connection_call_finish (GDBusConnection *connection,
247 GLIB_AVAILABLE_IN_ALL
248 GVariant *g_dbus_connection_call_sync (GDBusConnection *connection,
249 const gchar *bus_name,
250 const gchar *object_path,
251 const gchar *interface_name,
252 const gchar *method_name,
253 GVariant *parameters,
254 const GVariantType *reply_type,
255 GDBusCallFlags flags,
257 GCancellable *cancellable,
259 GLIB_AVAILABLE_IN_2_30
260 void g_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
261 const gchar *bus_name,
262 const gchar *object_path,
263 const gchar *interface_name,
264 const gchar *method_name,
265 GVariant *parameters,
266 const GVariantType *reply_type,
267 GDBusCallFlags flags,
269 GUnixFDList *fd_list,
270 GCancellable *cancellable,
271 GAsyncReadyCallback callback,
273 GLIB_AVAILABLE_IN_2_30
274 GVariant *g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection *connection,
275 GUnixFDList **out_fd_list,
278 GLIB_AVAILABLE_IN_2_30
279 GVariant *g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection *connection,
280 const gchar *bus_name,
281 const gchar *object_path,
282 const gchar *interface_name,
283 const gchar *method_name,
284 GVariant *parameters,
285 const GVariantType *reply_type,
286 GDBusCallFlags flags,
288 GUnixFDList *fd_list,
289 GUnixFDList **out_fd_list,
290 GCancellable *cancellable,
293 /* ---------------------------------------------------------------------------------------------------- */
297 * GDBusInterfaceMethodCallFunc:
298 * @connection: A #GDBusConnection.
299 * @sender: The unique bus name of the remote caller.
300 * @object_path: The object path that the method was invoked on.
301 * @interface_name: The D-Bus interface name the method was invoked on.
302 * @method_name: The name of the method that was invoked.
303 * @parameters: A #GVariant tuple with parameters.
304 * @invocation: (transfer full): A #GDBusMethodInvocation object that must be used to return a value or error.
305 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
307 * The type of the @method_call function in #GDBusInterfaceVTable.
311 typedef void (*GDBusInterfaceMethodCallFunc) (GDBusConnection *connection,
313 const gchar *object_path,
314 const gchar *interface_name,
315 const gchar *method_name,
316 GVariant *parameters,
317 GDBusMethodInvocation *invocation,
321 * GDBusInterfaceGetPropertyFunc:
322 * @connection: A #GDBusConnection.
323 * @sender: The unique bus name of the remote caller.
324 * @object_path: The object path that the method was invoked on.
325 * @interface_name: The D-Bus interface name for the property.
326 * @property_name: The name of the property to get the value of.
327 * @error: Return location for error.
328 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
330 * The type of the @get_property function in #GDBusInterfaceVTable.
332 * Returns: A #GVariant with the value for @property_name or %NULL if
333 * @error is set. If the returned #GVariant is floating, it is
334 * consumed - otherwise its reference count is decreased by one.
338 typedef GVariant *(*GDBusInterfaceGetPropertyFunc) (GDBusConnection *connection,
340 const gchar *object_path,
341 const gchar *interface_name,
342 const gchar *property_name,
347 * GDBusInterfaceSetPropertyFunc:
348 * @connection: A #GDBusConnection.
349 * @sender: The unique bus name of the remote caller.
350 * @object_path: The object path that the method was invoked on.
351 * @interface_name: The D-Bus interface name for the property.
352 * @property_name: The name of the property to get the value of.
353 * @value: The value to set the property to.
354 * @error: Return location for error.
355 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
357 * The type of the @set_property function in #GDBusInterfaceVTable.
359 * Returns: %TRUE if the property was set to @value, %FALSE if @error is set.
363 typedef gboolean (*GDBusInterfaceSetPropertyFunc) (GDBusConnection *connection,
365 const gchar *object_path,
366 const gchar *interface_name,
367 const gchar *property_name,
373 * GDBusInterfaceVTable:
374 * @method_call: Function for handling incoming method calls.
375 * @get_property: Function for getting a property.
376 * @set_property: Function for setting a property.
378 * Virtual table for handling properties and method calls for a D-Bus
381 * Since 2.38, if you want to handle getting/setting D-Bus properties
382 * asynchronously, give %NULL as your get_property() or set_property()
383 * function. The D-Bus call will be directed to your @method_call function,
384 * with the provided @interface_name set to "org.freedesktop.DBus.Properties".
386 * Ownership of the #GDBusMethodInvocation object passed to the
387 * method_call() function is transferred to your handler; you must
388 * call one of the methods of #GDBusMethodInvocation to return a reply
389 * (possibly empty), or an error. These functions also take ownership
390 * of the passed-in invocation object, so unless the invocation
391 * object has otherwise been referenced, it will be then be freed.
392 * Calling one of these functions may be done within your
393 * method_call() implementation but it also can be done at a later
394 * point to handle the method asynchronously.
396 * The usual checks on the validity of the calls is performed. For
397 * `Get` calls, an error is automatically returned if the property does
398 * not exist or the permissions do not allow access. The same checks are
399 * performed for `Set` calls, and the provided value is also checked for
400 * being the correct type.
402 * For both `Get` and `Set` calls, the #GDBusMethodInvocation
403 * passed to the @method_call handler can be queried with
404 * g_dbus_method_invocation_get_property_info() to get a pointer
405 * to the #GDBusPropertyInfo of the property.
407 * If you have readable properties specified in your interface info,
408 * you must ensure that you either provide a non-%NULL @get_property()
409 * function or provide implementations of both the `Get` and `GetAll`
410 * methods on org.freedesktop.DBus.Properties interface in your @method_call
411 * function. Note that the required return type of the `Get` call is
412 * `(v)`, not the type of the property. `GetAll` expects a return value
415 * If you have writable properties specified in your interface info,
416 * you must ensure that you either provide a non-%NULL @set_property()
417 * function or provide an implementation of the `Set` call. If implementing
418 * the call, you must return the value of type %G_VARIANT_TYPE_UNIT.
422 struct _GDBusInterfaceVTable
424 GDBusInterfaceMethodCallFunc method_call;
425 GDBusInterfaceGetPropertyFunc get_property;
426 GDBusInterfaceSetPropertyFunc set_property;
429 /* Padding for future expansion - also remember to update
430 * gdbusconnection.c:_g_dbus_interface_vtable_copy() when
436 GLIB_AVAILABLE_IN_ALL
437 guint g_dbus_connection_register_object (GDBusConnection *connection,
438 const gchar *object_path,
439 GDBusInterfaceInfo *interface_info,
440 const GDBusInterfaceVTable *vtable,
442 GDestroyNotify user_data_free_func,
444 GLIB_AVAILABLE_IN_ALL
445 gboolean g_dbus_connection_unregister_object (GDBusConnection *connection,
446 guint registration_id);
448 /* ---------------------------------------------------------------------------------------------------- */
451 * GDBusSubtreeEnumerateFunc:
452 * @connection: A #GDBusConnection.
453 * @sender: The unique bus name of the remote caller.
454 * @object_path: The object path that was registered with g_dbus_connection_register_subtree().
455 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_subtree().
457 * The type of the @enumerate function in #GDBusSubtreeVTable.
459 * This function is called when generating introspection data and also
460 * when preparing to dispatch incoming messages in the event that the
461 * %G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is not
462 * specified (ie: to verify that the object path is valid).
464 * Hierarchies are not supported; the items that you return should not
465 * contain the '/' character.
467 * The return value will be freed with g_strfreev().
469 * Returns: A newly allocated array of strings for node names that are children of @object_path.
473 typedef gchar** (*GDBusSubtreeEnumerateFunc) (GDBusConnection *connection,
475 const gchar *object_path,
479 * GDBusSubtreeIntrospectFunc:
480 * @connection: A #GDBusConnection.
481 * @sender: The unique bus name of the remote caller.
482 * @object_path: The object path that was registered with g_dbus_connection_register_subtree().
483 * @node: A node that is a child of @object_path (relative to @object_path) or %NULL for the root of the subtree.
484 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_subtree().
486 * The type of the @introspect function in #GDBusSubtreeVTable.
488 * Subtrees are flat. @node, if non-%NULL, is always exactly one
489 * segment of the object path (ie: it never contains a slash).
491 * This function should return %NULL to indicate that there is no object
494 * If this function returns non-%NULL, the return value is expected to
495 * be a %NULL-terminated array of pointers to #GDBusInterfaceInfo
496 * structures describing the interfaces implemented by @node. This
497 * array will have g_dbus_interface_info_unref() called on each item
498 * before being freed with g_free().
500 * The difference between returning %NULL and an array containing zero
501 * items is that the standard DBus interfaces will returned to the
502 * remote introspector in the empty array case, but not in the %NULL
505 * Returns: A %NULL-terminated array of pointers to #GDBusInterfaceInfo, or %NULL.
509 typedef GDBusInterfaceInfo ** (*GDBusSubtreeIntrospectFunc) (GDBusConnection *connection,
511 const gchar *object_path,
516 * GDBusSubtreeDispatchFunc:
517 * @connection: A #GDBusConnection.
518 * @sender: The unique bus name of the remote caller.
519 * @object_path: The object path that was registered with g_dbus_connection_register_subtree().
520 * @interface_name: The D-Bus interface name that the method call or property access is for.
521 * @node: A node that is a child of @object_path (relative to @object_path) or %NULL for the root of the subtree.
522 * @out_user_data: Return location for user data to pass to functions in the returned #GDBusInterfaceVTable (never %NULL).
523 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_subtree().
525 * The type of the @dispatch function in #GDBusSubtreeVTable.
527 * Subtrees are flat. @node, if non-%NULL, is always exactly one
528 * segment of the object path (ie: it never contains a slash).
530 * Returns: A #GDBusInterfaceVTable or %NULL if you don't want to handle the methods.
534 typedef const GDBusInterfaceVTable * (*GDBusSubtreeDispatchFunc) (GDBusConnection *connection,
536 const gchar *object_path,
537 const gchar *interface_name,
539 gpointer *out_user_data,
543 * GDBusSubtreeVTable:
544 * @enumerate: Function for enumerating child nodes.
545 * @introspect: Function for introspecting a child node.
546 * @dispatch: Function for dispatching a remote call on a child node.
548 * Virtual table for handling subtrees registered with g_dbus_connection_register_subtree().
552 struct _GDBusSubtreeVTable
554 GDBusSubtreeEnumerateFunc enumerate;
555 GDBusSubtreeIntrospectFunc introspect;
556 GDBusSubtreeDispatchFunc dispatch;
559 /* Padding for future expansion - also remember to update
560 * gdbusconnection.c:_g_dbus_subtree_vtable_copy() when
566 GLIB_AVAILABLE_IN_ALL
567 guint g_dbus_connection_register_subtree (GDBusConnection *connection,
568 const gchar *object_path,
569 const GDBusSubtreeVTable *vtable,
570 GDBusSubtreeFlags flags,
572 GDestroyNotify user_data_free_func,
574 GLIB_AVAILABLE_IN_ALL
575 gboolean g_dbus_connection_unregister_subtree (GDBusConnection *connection,
576 guint registration_id);
578 /* ---------------------------------------------------------------------------------------------------- */
581 * GDBusSignalCallback:
582 * @connection: A #GDBusConnection.
583 * @sender_name: The unique bus name of the sender of the signal.
584 * @object_path: The object path that the signal was emitted on.
585 * @interface_name: The name of the interface.
586 * @signal_name: The name of the signal.
587 * @parameters: A #GVariant tuple with parameters for the signal.
588 * @user_data: User data passed when subscribing to the signal.
590 * Signature for callback function used in g_dbus_connection_signal_subscribe().
594 typedef void (*GDBusSignalCallback) (GDBusConnection *connection,
595 const gchar *sender_name,
596 const gchar *object_path,
597 const gchar *interface_name,
598 const gchar *signal_name,
599 GVariant *parameters,
602 GLIB_AVAILABLE_IN_ALL
603 guint g_dbus_connection_signal_subscribe (GDBusConnection *connection,
605 const gchar *interface_name,
607 const gchar *object_path,
609 GDBusSignalFlags flags,
610 GDBusSignalCallback callback,
612 GDestroyNotify user_data_free_func);
613 GLIB_AVAILABLE_IN_ALL
614 void g_dbus_connection_signal_unsubscribe (GDBusConnection *connection,
615 guint subscription_id);
617 /* ---------------------------------------------------------------------------------------------------- */
620 * GDBusMessageFilterFunction:
621 * @connection: (transfer none): A #GDBusConnection.
622 * @message: (transfer full): A locked #GDBusMessage that the filter function takes ownership of.
623 * @incoming: %TRUE if it is a message received from the other peer, %FALSE if it is
624 * a message to be sent to the other peer.
625 * @user_data: User data passed when adding the filter.
627 * Signature for function used in g_dbus_connection_add_filter().
629 * A filter function is passed a #GDBusMessage and expected to return
630 * a #GDBusMessage too. Passive filter functions that don't modify the
631 * message can simply return the @message object:
633 * static GDBusMessage *
634 * passive_filter (GDBusConnection *connection
635 * GDBusMessage *message,
637 * gpointer user_data)
639 * /<!-- -->* inspect @message *<!-- -->/
643 * Filter functions that wants to drop a message can simply return %NULL:
645 * static GDBusMessage *
646 * drop_filter (GDBusConnection *connection
647 * GDBusMessage *message,
649 * gpointer user_data)
651 * if (should_drop_message)
653 * g_object_unref (message);
659 * Finally, a filter function may modify a message by copying it:
661 * static GDBusMessage *
662 * modifying_filter (GDBusConnection *connection
663 * GDBusMessage *message,
665 * gpointer user_data)
667 * GDBusMessage *copy;
671 * copy = g_dbus_message_copy (message, &error);
672 * /<!-- -->* handle @error being is set *<!-- -->/
673 * g_object_unref (message);
675 * /<!-- -->* modify @copy *<!-- -->/
680 * If the returned #GDBusMessage is different from @message and cannot
681 * be sent on @connection (it could use features, such as file
682 * descriptors, not compatible with @connection), then a warning is
683 * logged to <emphasis>standard error</emphasis>. Applications can
684 * check this ahead of time using g_dbus_message_to_blob() passing a
685 * #GDBusCapabilityFlags value obtained from @connection.
687 * Returns: (transfer full) (allow-none): A #GDBusMessage that will be freed with
688 * g_object_unref() or %NULL to drop the message. Passive filter
689 * functions can simply return the passed @message object.
693 typedef GDBusMessage *(*GDBusMessageFilterFunction) (GDBusConnection *connection,
694 GDBusMessage *message,
698 GLIB_AVAILABLE_IN_ALL
699 guint g_dbus_connection_add_filter (GDBusConnection *connection,
700 GDBusMessageFilterFunction filter_function,
702 GDestroyNotify user_data_free_func);
704 GLIB_AVAILABLE_IN_ALL
705 void g_dbus_connection_remove_filter (GDBusConnection *connection,
708 /* ---------------------------------------------------------------------------------------------------- */
713 #endif /* __G_DBUS_CONNECTION_H__ */