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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusintrospection.h"
33 * SECTION:gdbusintrospection
34 * @title: D-Bus Introspection Data
35 * @short_description: Node and interface description data structures
38 * Various data structures and convenience routines to parse and
39 * generate D-Bus introspection XML. Introspection information is
40 * used when registering objects with g_dbus_connection_register_object().
42 * The format of D-Bus introspection XML is specified in the
43 * <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus specification</ulink>.
46 /* ---------------------------------------------------------------------------------------------------- */
48 #define _MY_DEFINE_BOXED_TYPE(TypeName, type_name) \
49 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name##_ref, type_name##_unref)
51 _MY_DEFINE_BOXED_TYPE (GDBusNodeInfo
, g_dbus_node_info
);
52 _MY_DEFINE_BOXED_TYPE (GDBusInterfaceInfo
, g_dbus_interface_info
);
53 _MY_DEFINE_BOXED_TYPE (GDBusMethodInfo
, g_dbus_method_info
);
54 _MY_DEFINE_BOXED_TYPE (GDBusSignalInfo
, g_dbus_signal_info
);
55 _MY_DEFINE_BOXED_TYPE (GDBusPropertyInfo
, g_dbus_property_info
);
56 _MY_DEFINE_BOXED_TYPE (GDBusArgInfo
, g_dbus_arg_info
);
57 _MY_DEFINE_BOXED_TYPE (GDBusAnnotationInfo
, g_dbus_annotation_info
);
59 /* ---------------------------------------------------------------------------------------------------- */
63 /* stuff we are currently collecting */
68 GPtrArray
*properties
;
69 GPtrArray
*interfaces
;
71 GPtrArray
*annotations
;
73 /* A list of GPtrArray's containing annotations */
74 GSList
*annotations_stack
;
76 /* A list of GPtrArray's containing interfaces */
77 GSList
*interfaces_stack
;
79 /* A list of GPtrArray's containing nodes */
82 /* Whether the direction was "in" for last parsed arg */
83 gboolean last_arg_was_in
;
85 /* Number of args currently being collected; used for assigning
86 * names to args without a "name" attribute
92 /* ---------------------------------------------------------------------------------------------------- */
95 * g_dbus_node_info_ref:
96 * @info: A #GDBusNodeInfo
98 * If @info is statically allocated does nothing. Otherwise increases
99 * the reference count.
101 * Returns: The same @info.
106 g_dbus_node_info_ref (GDBusNodeInfo
*info
)
108 if (info
->ref_count
== -1)
110 g_atomic_int_inc (&info
->ref_count
);
115 * g_dbus_interface_info_ref:
116 * @info: A #GDBusInterfaceInfo
118 * If @info is statically allocated does nothing. Otherwise increases
119 * the reference count.
121 * Returns: The same @info.
126 g_dbus_interface_info_ref (GDBusInterfaceInfo
*info
)
128 if (info
->ref_count
== -1)
130 g_atomic_int_inc (&info
->ref_count
);
135 * g_dbus_method_info_ref:
136 * @info: A #GDBusMethodInfo
138 * If @info is statically allocated does nothing. Otherwise increases
139 * the reference count.
141 * Returns: The same @info.
146 g_dbus_method_info_ref (GDBusMethodInfo
*info
)
148 if (info
->ref_count
== -1)
150 g_atomic_int_inc (&info
->ref_count
);
155 * g_dbus_signal_info_ref:
156 * @info: A #GDBusSignalInfo
158 * If @info is statically allocated does nothing. Otherwise increases
159 * the reference count.
161 * Returns: The same @info.
166 g_dbus_signal_info_ref (GDBusSignalInfo
*info
)
168 if (info
->ref_count
== -1)
170 g_atomic_int_inc (&info
->ref_count
);
175 * g_dbus_property_info_ref:
176 * @info: A #GDBusPropertyInfo
178 * If @info is statically allocated does nothing. Otherwise increases
179 * the reference count.
181 * Returns: The same @info.
186 g_dbus_property_info_ref (GDBusPropertyInfo
*info
)
188 if (info
->ref_count
== -1)
190 g_atomic_int_inc (&info
->ref_count
);
195 * g_dbus_arg_info_ref:
196 * @info: A #GDBusArgInfo
198 * If @info is statically allocated does nothing. Otherwise increases
199 * the reference count.
201 * Returns: The same @info.
206 g_dbus_arg_info_ref (GDBusArgInfo
*info
)
208 if (info
->ref_count
== -1)
210 g_atomic_int_inc (&info
->ref_count
);
215 * g_dbus_annotation_info_ref:
216 * @info: A #GDBusNodeInfo
218 * If @info is statically allocated does nothing. Otherwise increases
219 * the reference count.
221 * Returns: The same @info.
225 GDBusAnnotationInfo
*
226 g_dbus_annotation_info_ref (GDBusAnnotationInfo
*info
)
228 if (info
->ref_count
== -1)
230 g_atomic_int_inc (&info
->ref_count
);
234 /* ---------------------------------------------------------------------------------------------------- */
237 free_null_terminated_array (gpointer array
, GDestroyNotify unref_func
)
243 for (n
= 0; p
[n
] != NULL
; n
++)
249 * g_dbus_annotation_info_unref:
250 * @info: A #GDBusAnnotationInfo.
252 * If @info is statically allocated, does nothing. Otherwise decreases
253 * the reference count of @info. When its reference count drops to 0,
254 * the memory used is freed.
259 g_dbus_annotation_info_unref (GDBusAnnotationInfo
*info
)
261 if (info
->ref_count
== -1)
263 if (g_atomic_int_dec_and_test (&info
->ref_count
))
266 g_free (info
->value
);
267 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
273 * g_dbus_arg_info_unref:
274 * @info: A #GDBusArgInfo.
276 * If @info is statically allocated, does nothing. Otherwise decreases
277 * the reference count of @info. When its reference count drops to 0,
278 * the memory used is freed.
283 g_dbus_arg_info_unref (GDBusArgInfo
*info
)
285 if (info
->ref_count
== -1)
287 if (g_atomic_int_dec_and_test (&info
->ref_count
))
290 g_free (info
->signature
);
291 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
297 * g_dbus_method_info_unref:
298 * @info: A #GDBusMethodInfo.
300 * If @info is statically allocated, does nothing. Otherwise decreases
301 * the reference count of @info. When its reference count drops to 0,
302 * the memory used is freed.
307 g_dbus_method_info_unref (GDBusMethodInfo
*info
)
309 if (info
->ref_count
== -1)
311 if (g_atomic_int_dec_and_test (&info
->ref_count
))
314 free_null_terminated_array (info
->in_args
, (GDestroyNotify
) g_dbus_arg_info_unref
);
315 free_null_terminated_array (info
->out_args
, (GDestroyNotify
) g_dbus_arg_info_unref
);
316 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
322 * g_dbus_signal_info_unref:
323 * @info: A #GDBusSignalInfo.
325 * If @info is statically allocated, does nothing. Otherwise decreases
326 * the reference count of @info. When its reference count drops to 0,
327 * the memory used is freed.
332 g_dbus_signal_info_unref (GDBusSignalInfo
*info
)
334 if (info
->ref_count
== -1)
336 if (g_atomic_int_dec_and_test (&info
->ref_count
))
339 free_null_terminated_array (info
->args
, (GDestroyNotify
) g_dbus_arg_info_unref
);
340 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
346 * g_dbus_property_info_unref:
347 * @info: A #GDBusPropertyInfo.
349 * If @info is statically allocated, does nothing. Otherwise decreases
350 * the reference count of @info. When its reference count drops to 0,
351 * the memory used is freed.
356 g_dbus_property_info_unref (GDBusPropertyInfo
*info
)
358 if (info
->ref_count
== -1)
360 if (g_atomic_int_dec_and_test (&info
->ref_count
))
363 g_free (info
->signature
);
364 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
370 * g_dbus_interface_info_unref:
371 * @info: A #GDBusInterfaceInfo.
373 * If @info is statically allocated, does nothing. Otherwise decreases
374 * the reference count of @info. When its reference count drops to 0,
375 * the memory used is freed.
380 g_dbus_interface_info_unref (GDBusInterfaceInfo
*info
)
382 if (info
->ref_count
== -1)
384 if (g_atomic_int_dec_and_test (&info
->ref_count
))
387 free_null_terminated_array (info
->methods
, (GDestroyNotify
) g_dbus_method_info_unref
);
388 free_null_terminated_array (info
->signals
, (GDestroyNotify
) g_dbus_signal_info_unref
);
389 free_null_terminated_array (info
->properties
, (GDestroyNotify
) g_dbus_property_info_unref
);
390 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
396 * g_dbus_node_info_unref:
397 * @info: A #GDBusNodeInfo.
399 * If @info is statically allocated, does nothing. Otherwise decreases
400 * the reference count of @info. When its reference count drops to 0,
401 * the memory used is freed.
406 g_dbus_node_info_unref (GDBusNodeInfo
*info
)
408 if (info
->ref_count
== -1)
410 if (g_atomic_int_dec_and_test (&info
->ref_count
))
413 free_null_terminated_array (info
->interfaces
, (GDestroyNotify
) g_dbus_interface_info_unref
);
414 free_null_terminated_array (info
->nodes
, (GDestroyNotify
) g_dbus_node_info_unref
);
415 free_null_terminated_array (info
->annotations
, (GDestroyNotify
) g_dbus_annotation_info_unref
);
420 /* ---------------------------------------------------------------------------------------------------- */
423 g_dbus_annotation_info_set (ParseData
*data
,
424 GDBusAnnotationInfo
*info
,
427 GDBusAnnotationInfo
**embedded_annotations
)
432 info
->key
= g_strdup (key
);
435 info
->value
= g_strdup (value
);
437 if (embedded_annotations
!= NULL
)
438 info
->annotations
= embedded_annotations
;
442 g_dbus_arg_info_set (ParseData
*data
,
445 const gchar
*signature
,
446 GDBusAnnotationInfo
**annotations
)
450 /* name may be NULL - TODO: compute name? */
452 info
->name
= g_strdup (name
);
454 if (signature
!= NULL
)
455 info
->signature
= g_strdup (signature
);
457 if (annotations
!= NULL
)
458 info
->annotations
= annotations
;
462 g_dbus_method_info_set (ParseData
*data
,
463 GDBusMethodInfo
*info
,
465 GDBusArgInfo
**in_args
,
466 GDBusArgInfo
**out_args
,
467 GDBusAnnotationInfo
**annotations
)
472 info
->name
= g_strdup (name
);
475 info
->in_args
= in_args
;
477 if (out_args
!= NULL
)
478 info
->out_args
= out_args
;
480 if (annotations
!= NULL
)
481 info
->annotations
= annotations
;
485 g_dbus_signal_info_set (ParseData
*data
,
486 GDBusSignalInfo
*info
,
489 GDBusAnnotationInfo
**annotations
)
494 info
->name
= g_strdup (name
);
499 if (annotations
!= NULL
)
500 info
->annotations
= annotations
;
504 g_dbus_property_info_set (ParseData
*data
,
505 GDBusPropertyInfo
*info
,
507 const gchar
*signature
,
508 GDBusPropertyInfoFlags flags
,
509 GDBusAnnotationInfo
**annotations
)
514 info
->name
= g_strdup (name
);
516 if (flags
!= G_DBUS_PROPERTY_INFO_FLAGS_NONE
)
519 if (signature
!= NULL
)
520 info
->signature
= g_strdup (signature
);
522 if (annotations
!= NULL
)
523 info
->annotations
= annotations
;
527 g_dbus_interface_info_set (ParseData
*data
,
528 GDBusInterfaceInfo
*info
,
530 GDBusMethodInfo
**methods
,
531 GDBusSignalInfo
**signals
,
532 GDBusPropertyInfo
**properties
,
533 GDBusAnnotationInfo
**annotations
)
538 info
->name
= g_strdup (name
);
541 info
->methods
= methods
;
544 info
->signals
= signals
;
546 if (properties
!= NULL
)
547 info
->properties
= properties
;
549 if (annotations
!= NULL
)
550 info
->annotations
= annotations
;
554 g_dbus_node_info_set (ParseData
*data
,
557 GDBusInterfaceInfo
**interfaces
,
558 GDBusNodeInfo
**nodes
,
559 GDBusAnnotationInfo
**annotations
)
565 info
->path
= g_strdup (path
);
566 /* TODO: relative / absolute path snafu */
569 if (interfaces
!= NULL
)
570 info
->interfaces
= interfaces
;
575 if (annotations
!= NULL
)
576 info
->annotations
= annotations
;
579 /* ---------------------------------------------------------------------------------------------------- */
582 g_dbus_annotation_info_generate_xml (GDBusAnnotationInfo
*info
,
584 GString
*string_builder
)
588 g_string_append_printf (string_builder
, "%*s<annotation name=\"%s\" value=\"%s\"",
593 if (info
->annotations
== NULL
)
595 g_string_append (string_builder
, "/>\n");
599 g_string_append (string_builder
, ">\n");
601 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
602 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
606 g_string_append_printf (string_builder
, "%*s</annotation>\n",
613 g_dbus_arg_info_generate_xml (GDBusArgInfo
*info
,
615 const gchar
*extra_attributes
,
616 GString
*string_builder
)
620 g_string_append_printf (string_builder
, "%*s<arg type=\"%s\"",
624 if (info
->name
!= NULL
)
625 g_string_append_printf (string_builder
, " name=\"%s\"", info
->name
);
627 if (extra_attributes
!= NULL
)
628 g_string_append_printf (string_builder
, " %s", extra_attributes
);
630 if (info
->annotations
== NULL
)
632 g_string_append (string_builder
, "/>\n");
636 g_string_append (string_builder
, ">\n");
638 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
639 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
643 g_string_append_printf (string_builder
, "%*s</arg>\n", indent
, "");
649 g_dbus_method_info_generate_xml (GDBusMethodInfo
*info
,
651 GString
*string_builder
)
655 g_string_append_printf (string_builder
, "%*s<method name=\"%s\"",
659 if (info
->annotations
== NULL
&& info
->in_args
== NULL
&& info
->out_args
== NULL
)
661 g_string_append (string_builder
, "/>\n");
665 g_string_append (string_builder
, ">\n");
667 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
668 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
672 for (n
= 0; info
->in_args
!= NULL
&& info
->in_args
[n
] != NULL
; n
++)
673 g_dbus_arg_info_generate_xml (info
->in_args
[n
],
678 for (n
= 0; info
->out_args
!= NULL
&& info
->out_args
[n
] != NULL
; n
++)
679 g_dbus_arg_info_generate_xml (info
->out_args
[n
],
684 g_string_append_printf (string_builder
, "%*s</method>\n", indent
, "");
689 g_dbus_signal_info_generate_xml (GDBusSignalInfo
*info
,
691 GString
*string_builder
)
695 g_string_append_printf (string_builder
, "%*s<signal name=\"%s\"",
699 if (info
->annotations
== NULL
&& info
->args
== NULL
)
701 g_string_append (string_builder
, "/>\n");
705 g_string_append (string_builder
, ">\n");
707 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
708 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
712 for (n
= 0; info
->args
!= NULL
&& info
->args
[n
] != NULL
; n
++)
713 g_dbus_arg_info_generate_xml (info
->args
[n
],
718 g_string_append_printf (string_builder
, "%*s</signal>\n", indent
, "");
723 g_dbus_property_info_generate_xml (GDBusPropertyInfo
*info
,
725 GString
*string_builder
)
728 const gchar
*access_string
;
730 if ((info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
) &&
731 (info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
))
733 access_string
= "readwrite";
735 else if (info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
)
737 access_string
= "read";
739 else if (info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
)
741 access_string
= "write";
745 g_assert_not_reached ();
748 g_string_append_printf (string_builder
, "%*s<property type=\"%s\" name=\"%s\" access=\"%s\"",
754 if (info
->annotations
== NULL
)
756 g_string_append (string_builder
, "/>\n");
760 g_string_append (string_builder
, ">\n");
762 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
763 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
767 g_string_append_printf (string_builder
, "%*s</property>\n", indent
, "");
773 * g_dbus_interface_info_generate_xml:
774 * @info: A #GDBusNodeInfo
775 * @indent: Indentation level.
776 * @string_builder: (out): A #GString to to append XML data to.
778 * Appends an XML representation of @info (and its children) to @string_builder.
780 * This function is typically used for generating introspection XML
781 * documents at run-time for handling the
782 * <literal>org.freedesktop.DBus.Introspectable.Introspect</literal>
788 g_dbus_interface_info_generate_xml (GDBusInterfaceInfo
*info
,
790 GString
*string_builder
)
794 g_string_append_printf (string_builder
, "%*s<interface name=\"%s\">\n",
798 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
799 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
803 for (n
= 0; info
->methods
!= NULL
&& info
->methods
[n
] != NULL
; n
++)
804 g_dbus_method_info_generate_xml (info
->methods
[n
],
808 for (n
= 0; info
->signals
!= NULL
&& info
->signals
[n
] != NULL
; n
++)
809 g_dbus_signal_info_generate_xml (info
->signals
[n
],
813 for (n
= 0; info
->properties
!= NULL
&& info
->properties
[n
] != NULL
; n
++)
814 g_dbus_property_info_generate_xml (info
->properties
[n
],
818 g_string_append_printf (string_builder
, "%*s</interface>\n", indent
, "");
822 * g_dbus_node_info_generate_xml:
823 * @info: A #GDBusNodeInfo.
824 * @indent: Indentation level.
825 * @string_builder: (out): A #GString to to append XML data to.
827 * Appends an XML representation of @info (and its children) to @string_builder.
829 * This function is typically used for generating introspection XML documents at run-time for
830 * handling the <literal>org.freedesktop.DBus.Introspectable.Introspect</literal> method.
835 g_dbus_node_info_generate_xml (GDBusNodeInfo
*info
,
837 GString
*string_builder
)
841 g_string_append_printf (string_builder
, "%*s<node", indent
, "");
842 if (info
->path
!= NULL
)
843 g_string_append_printf (string_builder
, " name=\"%s\"", info
->path
);
845 if (info
->interfaces
== NULL
&& info
->nodes
== NULL
&& info
->annotations
== NULL
)
847 g_string_append (string_builder
, "/>\n");
851 g_string_append (string_builder
, ">\n");
853 for (n
= 0; info
->annotations
!= NULL
&& info
->annotations
[n
] != NULL
; n
++)
854 g_dbus_annotation_info_generate_xml (info
->annotations
[n
],
858 for (n
= 0; info
->interfaces
!= NULL
&& info
->interfaces
[n
] != NULL
; n
++)
859 g_dbus_interface_info_generate_xml (info
->interfaces
[n
],
863 for (n
= 0; info
->nodes
!= NULL
&& info
->nodes
[n
] != NULL
; n
++)
864 g_dbus_node_info_generate_xml (info
->nodes
[n
],
868 g_string_append_printf (string_builder
, "%*s</node>\n", indent
, "");
872 /* ---------------------------------------------------------------------------------------------------- */
874 static GDBusAnnotationInfo
**
875 parse_data_steal_annotations (ParseData
*data
,
876 guint
*out_num_elements
)
878 GDBusAnnotationInfo
**ret
;
879 if (out_num_elements
!= NULL
)
880 *out_num_elements
= data
->annotations
->len
;
881 if (data
->annotations
== NULL
)
885 g_ptr_array_add (data
->annotations
, NULL
);
886 ret
= (GDBusAnnotationInfo
**) g_ptr_array_free (data
->annotations
, FALSE
);
888 data
->annotations
= g_ptr_array_new ();
892 static GDBusArgInfo
**
893 parse_data_steal_args (ParseData
*data
,
894 guint
*out_num_elements
)
897 if (out_num_elements
!= NULL
)
898 *out_num_elements
= data
->args
->len
;
899 if (data
->args
== NULL
)
903 g_ptr_array_add (data
->args
, NULL
);
904 ret
= (GDBusArgInfo
**) g_ptr_array_free (data
->args
, FALSE
);
906 data
->args
= g_ptr_array_new ();
910 static GDBusArgInfo
**
911 parse_data_steal_out_args (ParseData
*data
,
912 guint
*out_num_elements
)
915 if (out_num_elements
!= NULL
)
916 *out_num_elements
= data
->out_args
->len
;
917 if (data
->out_args
== NULL
)
921 g_ptr_array_add (data
->out_args
, NULL
);
922 ret
= (GDBusArgInfo
**) g_ptr_array_free (data
->out_args
, FALSE
);
924 data
->out_args
= g_ptr_array_new ();
928 static GDBusMethodInfo
**
929 parse_data_steal_methods (ParseData
*data
,
930 guint
*out_num_elements
)
932 GDBusMethodInfo
**ret
;
933 if (out_num_elements
!= NULL
)
934 *out_num_elements
= data
->methods
->len
;
935 if (data
->methods
== NULL
)
939 g_ptr_array_add (data
->methods
, NULL
);
940 ret
= (GDBusMethodInfo
**) g_ptr_array_free (data
->methods
, FALSE
);
942 data
->methods
= g_ptr_array_new ();
946 static GDBusSignalInfo
**
947 parse_data_steal_signals (ParseData
*data
,
948 guint
*out_num_elements
)
950 GDBusSignalInfo
**ret
;
951 if (out_num_elements
!= NULL
)
952 *out_num_elements
= data
->signals
->len
;
953 if (data
->signals
== NULL
)
957 g_ptr_array_add (data
->signals
, NULL
);
958 ret
= (GDBusSignalInfo
**) g_ptr_array_free (data
->signals
, FALSE
);
960 data
->signals
= g_ptr_array_new ();
964 static GDBusPropertyInfo
**
965 parse_data_steal_properties (ParseData
*data
,
966 guint
*out_num_elements
)
968 GDBusPropertyInfo
**ret
;
969 if (out_num_elements
!= NULL
)
970 *out_num_elements
= data
->properties
->len
;
971 if (data
->properties
== NULL
)
975 g_ptr_array_add (data
->properties
, NULL
);
976 ret
= (GDBusPropertyInfo
**) g_ptr_array_free (data
->properties
, FALSE
);
978 data
->properties
= g_ptr_array_new ();
982 static GDBusInterfaceInfo
**
983 parse_data_steal_interfaces (ParseData
*data
,
984 guint
*out_num_elements
)
986 GDBusInterfaceInfo
**ret
;
987 if (out_num_elements
!= NULL
)
988 *out_num_elements
= data
->interfaces
->len
;
989 if (data
->interfaces
== NULL
)
993 g_ptr_array_add (data
->interfaces
, NULL
);
994 ret
= (GDBusInterfaceInfo
**) g_ptr_array_free (data
->interfaces
, FALSE
);
996 data
->interfaces
= g_ptr_array_new ();
1000 static GDBusNodeInfo
**
1001 parse_data_steal_nodes (ParseData
*data
,
1002 guint
*out_num_elements
)
1004 GDBusNodeInfo
**ret
;
1005 if (out_num_elements
!= NULL
)
1006 *out_num_elements
= data
->nodes
->len
;
1007 if (data
->nodes
== NULL
)
1011 g_ptr_array_add (data
->nodes
, NULL
);
1012 ret
= (GDBusNodeInfo
**) g_ptr_array_free (data
->nodes
, FALSE
);
1014 data
->nodes
= g_ptr_array_new ();
1018 /* ---------------------------------------------------------------------------------------------------- */
1021 parse_data_free_annotations (ParseData
*data
)
1023 if (data
->annotations
== NULL
)
1025 g_ptr_array_foreach (data
->annotations
, (GFunc
) g_dbus_annotation_info_unref
, NULL
);
1026 g_ptr_array_free (data
->annotations
, TRUE
);
1027 data
->annotations
= NULL
;
1031 parse_data_free_args (ParseData
*data
)
1033 if (data
->args
== NULL
)
1035 g_ptr_array_foreach (data
->args
, (GFunc
) g_dbus_arg_info_unref
, NULL
);
1036 g_ptr_array_free (data
->args
, TRUE
);
1041 parse_data_free_out_args (ParseData
*data
)
1043 if (data
->out_args
== NULL
)
1045 g_ptr_array_foreach (data
->out_args
, (GFunc
) g_dbus_arg_info_unref
, NULL
);
1046 g_ptr_array_free (data
->out_args
, TRUE
);
1047 data
->out_args
= NULL
;
1051 parse_data_free_methods (ParseData
*data
)
1053 if (data
->methods
== NULL
)
1055 g_ptr_array_foreach (data
->methods
, (GFunc
) g_dbus_method_info_unref
, NULL
);
1056 g_ptr_array_free (data
->methods
, TRUE
);
1057 data
->methods
= NULL
;
1061 parse_data_free_signals (ParseData
*data
)
1063 if (data
->signals
== NULL
)
1065 g_ptr_array_foreach (data
->signals
, (GFunc
) g_dbus_signal_info_unref
, NULL
);
1066 g_ptr_array_free (data
->signals
, TRUE
);
1067 data
->signals
= NULL
;
1071 parse_data_free_properties (ParseData
*data
)
1073 if (data
->properties
== NULL
)
1075 g_ptr_array_foreach (data
->properties
, (GFunc
) g_dbus_property_info_unref
, NULL
);
1076 g_ptr_array_free (data
->properties
, TRUE
);
1077 data
->properties
= NULL
;
1081 parse_data_free_interfaces (ParseData
*data
)
1083 if (data
->interfaces
== NULL
)
1085 g_ptr_array_foreach (data
->interfaces
, (GFunc
) g_dbus_interface_info_unref
, NULL
);
1086 g_ptr_array_free (data
->interfaces
, TRUE
);
1087 data
->interfaces
= NULL
;
1091 parse_data_free_nodes (ParseData
*data
)
1093 if (data
->nodes
== NULL
)
1095 g_ptr_array_foreach (data
->nodes
, (GFunc
) g_dbus_node_info_unref
, NULL
);
1096 g_ptr_array_free (data
->nodes
, TRUE
);
1100 /* ---------------------------------------------------------------------------------------------------- */
1102 static GDBusAnnotationInfo
*
1103 parse_data_get_annotation (ParseData
*data
,
1104 gboolean create_new
)
1107 g_ptr_array_add (data
->annotations
, g_new0 (GDBusAnnotationInfo
, 1));
1108 return data
->annotations
->pdata
[data
->annotations
->len
- 1];
1111 static GDBusArgInfo
*
1112 parse_data_get_arg (ParseData
*data
,
1113 gboolean create_new
)
1116 g_ptr_array_add (data
->args
, g_new0 (GDBusArgInfo
, 1));
1117 return data
->args
->pdata
[data
->args
->len
- 1];
1120 static GDBusArgInfo
*
1121 parse_data_get_out_arg (ParseData
*data
,
1122 gboolean create_new
)
1125 g_ptr_array_add (data
->out_args
, g_new0 (GDBusArgInfo
, 1));
1126 return data
->out_args
->pdata
[data
->out_args
->len
- 1];
1129 static GDBusMethodInfo
*
1130 parse_data_get_method (ParseData
*data
,
1131 gboolean create_new
)
1134 g_ptr_array_add (data
->methods
, g_new0 (GDBusMethodInfo
, 1));
1135 return data
->methods
->pdata
[data
->methods
->len
- 1];
1138 static GDBusSignalInfo
*
1139 parse_data_get_signal (ParseData
*data
,
1140 gboolean create_new
)
1143 g_ptr_array_add (data
->signals
, g_new0 (GDBusSignalInfo
, 1));
1144 return data
->signals
->pdata
[data
->signals
->len
- 1];
1147 static GDBusPropertyInfo
*
1148 parse_data_get_property (ParseData
*data
,
1149 gboolean create_new
)
1152 g_ptr_array_add (data
->properties
, g_new0 (GDBusPropertyInfo
, 1));
1153 return data
->properties
->pdata
[data
->properties
->len
- 1];
1156 static GDBusInterfaceInfo
*
1157 parse_data_get_interface (ParseData
*data
,
1158 gboolean create_new
)
1161 g_ptr_array_add (data
->interfaces
, g_new0 (GDBusInterfaceInfo
, 1));
1162 return data
->interfaces
->pdata
[data
->interfaces
->len
- 1];
1165 static GDBusNodeInfo
*
1166 parse_data_get_node (ParseData
*data
,
1167 gboolean create_new
)
1170 g_ptr_array_add (data
->nodes
, g_new0 (GDBusNodeInfo
, 1));
1171 return data
->nodes
->pdata
[data
->nodes
->len
- 1];
1174 /* ---------------------------------------------------------------------------------------------------- */
1177 parse_data_new (void)
1181 data
= g_new0 (ParseData
, 1);
1183 /* initialize arrays */
1184 parse_data_steal_annotations (data
, NULL
);
1185 parse_data_steal_args (data
, NULL
);
1186 parse_data_steal_out_args (data
, NULL
);
1187 parse_data_steal_methods (data
, NULL
);
1188 parse_data_steal_signals (data
, NULL
);
1189 parse_data_steal_properties (data
, NULL
);
1190 parse_data_steal_interfaces (data
, NULL
);
1191 parse_data_steal_nodes (data
, NULL
);
1197 parse_data_free (ParseData
*data
)
1201 /* free stack of annotation arrays */
1202 for (l
= data
->annotations_stack
; l
!= NULL
; l
= l
->next
)
1204 GPtrArray
*annotations
= l
->data
;
1205 g_ptr_array_foreach (annotations
, (GFunc
) g_dbus_annotation_info_unref
, NULL
);
1206 g_ptr_array_free (annotations
, TRUE
);
1208 g_slist_free (data
->annotations_stack
);
1210 /* free stack of interface arrays */
1211 for (l
= data
->interfaces_stack
; l
!= NULL
; l
= l
->next
)
1213 GPtrArray
*interfaces
= l
->data
;
1214 g_ptr_array_foreach (interfaces
, (GFunc
) g_dbus_interface_info_unref
, NULL
);
1215 g_ptr_array_free (interfaces
, TRUE
);
1217 g_slist_free (data
->interfaces_stack
);
1219 /* free stack of node arrays */
1220 for (l
= data
->nodes_stack
; l
!= NULL
; l
= l
->next
)
1222 GPtrArray
*nodes
= l
->data
;
1223 g_ptr_array_foreach (nodes
, (GFunc
) g_dbus_node_info_unref
, NULL
);
1224 g_ptr_array_free (nodes
, TRUE
);
1226 g_slist_free (data
->nodes_stack
);
1228 /* free arrays (data->annotations, data->interfaces and data->nodes have been freed above) */
1229 parse_data_free_args (data
);
1230 parse_data_free_out_args (data
);
1231 parse_data_free_methods (data
);
1232 parse_data_free_signals (data
);
1233 parse_data_free_properties (data
);
1234 parse_data_free_interfaces (data
);
1235 parse_data_free_annotations (data
);
1236 parse_data_free_nodes (data
);
1241 /* ---------------------------------------------------------------------------------------------------- */
1244 parser_start_element (GMarkupParseContext
*context
,
1245 const gchar
*element_name
,
1246 const gchar
**attribute_names
,
1247 const gchar
**attribute_values
,
1251 ParseData
*data
= user_data
;
1255 const gchar
*access
;
1256 const gchar
*direction
;
1265 stack
= (GSList
*) g_markup_parse_context_get_element_stack (context
);
1267 /* ---------------------------------------------------------------------------------------------------- */
1268 if (strcmp (element_name
, "node") == 0)
1270 if (!(g_slist_length (stack
) >= 1 || strcmp (stack
->next
->data
, "node") != 0))
1272 g_set_error_literal (error
,
1274 G_MARKUP_ERROR_INVALID_CONTENT
,
1275 "<node> elements can only be top-level or embedded in other <node> elements");
1279 if (!g_markup_collect_known_attributes (element_name
,
1283 G_MARKUP_COLLECT_STRING
| G_MARKUP_COLLECT_OPTIONAL
, "name", &name
,
1284 G_MARKUP_COLLECT_INVALID
))
1287 g_dbus_node_info_set (data
,
1288 parse_data_get_node (data
, TRUE
),
1294 /* push the currently retrieved interfaces and nodes on the stack and prepare new arrays */
1295 data
->interfaces_stack
= g_slist_prepend (data
->interfaces_stack
, data
->interfaces
);
1296 data
->interfaces
= NULL
;
1297 parse_data_steal_interfaces (data
, NULL
);
1299 data
->nodes_stack
= g_slist_prepend (data
->nodes_stack
, data
->nodes
);
1301 parse_data_steal_nodes (data
, NULL
);
1304 /* ---------------------------------------------------------------------------------------------------- */
1305 else if (strcmp (element_name
, "interface") == 0)
1307 if (g_slist_length (stack
) < 2 || strcmp (stack
->next
->data
, "node") != 0)
1309 g_set_error_literal (error
,
1311 G_MARKUP_ERROR_INVALID_CONTENT
,
1312 "<interface> elements can only be embedded in <node> elements");
1316 if (!g_markup_collect_known_attributes (element_name
,
1320 G_MARKUP_COLLECT_STRING
, "name", &name
,
1321 G_MARKUP_COLLECT_INVALID
))
1324 g_dbus_interface_info_set (data
,
1325 parse_data_get_interface (data
, TRUE
),
1333 /* ---------------------------------------------------------------------------------------------------- */
1334 else if (strcmp (element_name
, "method") == 0)
1336 if (g_slist_length (stack
) < 2 || strcmp (stack
->next
->data
, "interface") != 0)
1338 g_set_error_literal (error
,
1340 G_MARKUP_ERROR_INVALID_CONTENT
,
1341 "<method> elements can only be embedded in <interface> elements");
1345 if (!g_markup_collect_known_attributes (element_name
,
1349 G_MARKUP_COLLECT_STRING
, "name", &name
,
1350 G_MARKUP_COLLECT_INVALID
))
1353 g_dbus_method_info_set (data
,
1354 parse_data_get_method (data
, TRUE
),
1363 /* ---------------------------------------------------------------------------------------------------- */
1364 else if (strcmp (element_name
, "signal") == 0)
1366 if (g_slist_length (stack
) < 2 || strcmp (stack
->next
->data
, "interface") != 0)
1368 g_set_error_literal (error
,
1370 G_MARKUP_ERROR_INVALID_CONTENT
,
1371 "<signal> elements can only be embedded in <interface> elements");
1375 if (!g_markup_collect_known_attributes (element_name
,
1379 G_MARKUP_COLLECT_STRING
, "name", &name
,
1380 G_MARKUP_COLLECT_INVALID
))
1383 g_dbus_signal_info_set (data
,
1384 parse_data_get_signal (data
, TRUE
),
1392 /* ---------------------------------------------------------------------------------------------------- */
1393 else if (strcmp (element_name
, "property") == 0)
1395 GDBusPropertyInfoFlags flags
;
1397 if (g_slist_length (stack
) < 2 || strcmp (stack
->next
->data
, "interface") != 0)
1399 g_set_error_literal (error
,
1401 G_MARKUP_ERROR_INVALID_CONTENT
,
1402 "<property> elements can only be embedded in <interface> elements");
1406 if (!g_markup_collect_known_attributes (element_name
,
1410 G_MARKUP_COLLECT_STRING
, "name", &name
,
1411 G_MARKUP_COLLECT_STRING
, "type", &type
,
1412 G_MARKUP_COLLECT_STRING
, "access", &access
,
1413 G_MARKUP_COLLECT_INVALID
))
1416 if (strcmp (access
, "read") == 0)
1417 flags
= G_DBUS_PROPERTY_INFO_FLAGS_READABLE
;
1418 else if (strcmp (access
, "write") == 0)
1419 flags
= G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
;
1420 else if (strcmp (access
, "readwrite") == 0)
1421 flags
= G_DBUS_PROPERTY_INFO_FLAGS_READABLE
| G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
;
1426 G_MARKUP_ERROR_INVALID_CONTENT
,
1427 "Unknown value '%s' of access attribute for element <property>",
1432 g_dbus_property_info_set (data
,
1433 parse_data_get_property (data
, TRUE
),
1440 /* ---------------------------------------------------------------------------------------------------- */
1441 else if (strcmp (element_name
, "arg") == 0)
1446 if (g_slist_length (stack
) < 2 ||
1447 (strcmp (stack
->next
->data
, "method") != 0 &&
1448 strcmp (stack
->next
->data
, "signal") != 0))
1450 g_set_error_literal (error
,
1452 G_MARKUP_ERROR_INVALID_CONTENT
,
1453 "<arg> elements can only be embedded in <method> or <signal> elements");
1457 if (!g_markup_collect_known_attributes (element_name
,
1461 G_MARKUP_COLLECT_STRING
| G_MARKUP_COLLECT_OPTIONAL
, "name", &name
,
1462 G_MARKUP_COLLECT_STRING
| G_MARKUP_COLLECT_OPTIONAL
, "direction", &direction
,
1463 G_MARKUP_COLLECT_STRING
, "type", &type
,
1464 G_MARKUP_COLLECT_INVALID
))
1467 if (strcmp (stack
->next
->data
, "method") == 0)
1471 if (direction
!= NULL
)
1473 if (strcmp (direction
, "in") == 0)
1475 else if (strcmp (direction
, "out") == 0)
1481 G_MARKUP_ERROR_INVALID_CONTENT
,
1482 "Unknown value '%s' of direction attribute",
1488 if (is_in
&& strcmp (stack
->next
->data
, "signal") == 0)
1490 g_set_error_literal (error
,
1492 G_MARKUP_ERROR_INVALID_CONTENT
,
1493 "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
1498 name_to_use
= g_strdup_printf ("arg_%d", data
->num_args
);
1500 name_to_use
= g_strdup (name
);
1505 g_dbus_arg_info_set (data
,
1506 parse_data_get_arg (data
, TRUE
),
1510 data
->last_arg_was_in
= TRUE
;
1514 g_dbus_arg_info_set (data
,
1515 parse_data_get_out_arg (data
, TRUE
),
1519 data
->last_arg_was_in
= FALSE
;
1523 g_free (name_to_use
);
1525 /* ---------------------------------------------------------------------------------------------------- */
1526 else if (strcmp (element_name
, "annotation") == 0)
1528 if (g_slist_length (stack
) < 2 ||
1529 (strcmp (stack
->next
->data
, "node") != 0 &&
1530 strcmp (stack
->next
->data
, "interface") != 0 &&
1531 strcmp (stack
->next
->data
, "signal") != 0 &&
1532 strcmp (stack
->next
->data
, "method") != 0 &&
1533 strcmp (stack
->next
->data
, "property") != 0 &&
1534 strcmp (stack
->next
->data
, "arg") != 0 &&
1535 strcmp (stack
->next
->data
, "annotation") != 0))
1537 g_set_error_literal (error
,
1539 G_MARKUP_ERROR_INVALID_CONTENT
,
1540 "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
1544 if (!g_markup_collect_known_attributes (element_name
,
1548 G_MARKUP_COLLECT_STRING
, "name", &name
,
1549 G_MARKUP_COLLECT_STRING
, "value", &value
,
1550 G_MARKUP_COLLECT_INVALID
))
1553 g_dbus_annotation_info_set (data
,
1554 parse_data_get_annotation (data
, TRUE
),
1559 /* ---------------------------------------------------------------------------------------------------- */
1562 /* don't bail on unknown elements; just ignore them */
1564 /* ---------------------------------------------------------------------------------------------------- */
1566 /* push the currently retrieved annotations on the stack and prepare a new one */
1567 data
->annotations_stack
= g_slist_prepend (data
->annotations_stack
, data
->annotations
);
1568 data
->annotations
= NULL
;
1569 parse_data_steal_annotations (data
, NULL
);
1575 /* ---------------------------------------------------------------------------------------------------- */
1577 static GDBusAnnotationInfo
**
1578 steal_annotations (ParseData
*data
)
1580 return parse_data_steal_annotations (data
, NULL
);
1585 parser_end_element (GMarkupParseContext
*context
,
1586 const gchar
*element_name
,
1590 ParseData
*data
= user_data
;
1591 gboolean have_popped_annotations
;
1593 have_popped_annotations
= FALSE
;
1595 if (strcmp (element_name
, "node") == 0)
1598 guint num_interfaces
;
1599 GDBusNodeInfo
**nodes
;
1600 GDBusInterfaceInfo
**interfaces
;
1602 nodes
= parse_data_steal_nodes (data
, &num_nodes
);
1603 interfaces
= parse_data_steal_interfaces (data
, &num_interfaces
);
1605 /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
1606 * scope we're reentering
1608 parse_data_free_interfaces (data
);
1609 data
->interfaces
= (GPtrArray
*) data
->interfaces_stack
->data
;
1610 data
->interfaces_stack
= g_slist_remove (data
->interfaces_stack
, data
->interfaces_stack
->data
);
1612 parse_data_free_nodes (data
);
1613 data
->nodes
= (GPtrArray
*) data
->nodes_stack
->data
;
1614 data
->nodes_stack
= g_slist_remove (data
->nodes_stack
, data
->nodes_stack
->data
);
1616 g_dbus_node_info_set (data
,
1617 parse_data_get_node (data
, FALSE
),
1621 steal_annotations (data
));
1624 else if (strcmp (element_name
, "interface") == 0)
1628 guint num_properties
;
1629 GDBusMethodInfo
**methods
;
1630 GDBusSignalInfo
**signals
;
1631 GDBusPropertyInfo
**properties
;
1633 methods
= parse_data_steal_methods (data
, &num_methods
);
1634 signals
= parse_data_steal_signals (data
, &num_signals
);
1635 properties
= parse_data_steal_properties (data
, &num_properties
);
1637 g_dbus_interface_info_set (data
,
1638 parse_data_get_interface (data
, FALSE
),
1643 steal_annotations (data
));
1646 else if (strcmp (element_name
, "method") == 0)
1650 GDBusArgInfo
**in_args
;
1651 GDBusArgInfo
**out_args
;
1653 in_args
= parse_data_steal_args (data
, &in_num_args
);
1654 out_args
= parse_data_steal_out_args (data
, &out_num_args
);
1656 g_dbus_method_info_set (data
,
1657 parse_data_get_method (data
, FALSE
),
1661 steal_annotations (data
));
1663 else if (strcmp (element_name
, "signal") == 0)
1666 GDBusArgInfo
**args
;
1668 args
= parse_data_steal_out_args (data
, &num_args
);
1670 g_dbus_signal_info_set (data
,
1671 parse_data_get_signal (data
, FALSE
),
1674 steal_annotations (data
));
1676 else if (strcmp (element_name
, "property") == 0)
1678 g_dbus_property_info_set (data
,
1679 parse_data_get_property (data
, FALSE
),
1682 G_DBUS_PROPERTY_INFO_FLAGS_NONE
,
1683 steal_annotations (data
));
1685 else if (strcmp (element_name
, "arg") == 0)
1687 g_dbus_arg_info_set (data
,
1688 data
->last_arg_was_in
? parse_data_get_arg (data
, FALSE
) : parse_data_get_out_arg (data
, FALSE
),
1691 steal_annotations (data
));
1693 else if (strcmp (element_name
, "annotation") == 0)
1695 GDBusAnnotationInfo
**embedded_annotations
;
1697 embedded_annotations
= steal_annotations (data
);
1699 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1700 parse_data_free_annotations (data
);
1701 data
->annotations
= (GPtrArray
*) data
->annotations_stack
->data
;
1702 data
->annotations_stack
= g_slist_remove (data
->annotations_stack
, data
->annotations_stack
->data
);
1704 have_popped_annotations
= TRUE
;
1706 g_dbus_annotation_info_set (data
,
1707 parse_data_get_annotation (data
, FALSE
),
1710 embedded_annotations
);
1714 /* don't bail on unknown elements; just ignore them */
1717 if (!have_popped_annotations
)
1719 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1720 parse_data_free_annotations (data
);
1721 data
->annotations
= (GPtrArray
*) data
->annotations_stack
->data
;
1722 data
->annotations_stack
= g_slist_remove (data
->annotations_stack
, data
->annotations_stack
->data
);
1726 /* ---------------------------------------------------------------------------------------------------- */
1729 parser_error (GMarkupParseContext
*context
,
1736 g_markup_parse_context_get_position (context
, &line_number
, &char_number
);
1738 g_prefix_error (&error
, "%d:%d: ",
1743 /* ---------------------------------------------------------------------------------------------------- */
1746 * g_dbus_node_info_new_for_xml:
1747 * @xml_data: Valid D-Bus introspection XML.
1748 * @error: Return location for error.
1750 * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
1752 * The introspection XML must contain exactly one top-level
1753 * <tag class="starttag">node</tag> element.
1755 * Note that this routine is using a
1756 * <link linkend="glib-Simple-XML-Subset-Parser.description">GMarkup</link>-based
1757 * parser that only accepts a subset of valid XML documents.
1759 * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
1760 * with g_dbus_node_info_unref().
1765 g_dbus_node_info_new_for_xml (const gchar
*xml_data
,
1769 GMarkupParseContext
*context
;
1770 GMarkupParser
*parser
;
1773 GDBusNodeInfo
**ughret
;
1779 parser
= g_new0 (GMarkupParser
, 1);
1780 parser
->start_element
= parser_start_element
;
1781 parser
->end_element
= parser_end_element
;
1782 parser
->error
= parser_error
;
1784 data
= parse_data_new ();
1785 context
= g_markup_parse_context_new (parser
,
1788 (GDestroyNotify
) parse_data_free
);
1790 if (!g_markup_parse_context_parse (context
,
1796 if (!g_markup_parse_context_end_parse (context
, error
))
1799 ughret
= parse_data_steal_nodes (data
, &num_nodes
);
1807 G_MARKUP_ERROR_INVALID_CONTENT
,
1808 "Expected a single node in introspection XML, found %d",
1812 for (n
= 0; n
< num_nodes
; n
++)
1814 g_dbus_node_info_unref (ughret
[n
]);
1825 if (context
!= NULL
)
1826 g_markup_parse_context_free (context
);
1831 /* ---------------------------------------------------------------------------------------------------- */
1834 * g_dbus_annotation_info_lookup:
1835 * @annotations: (array zero-terminated=1) (allow-none): A %NULL-terminated array of annotations or %NULL.
1836 * @name: The name of the annotation to look up.
1838 * Looks up the value of an annotation.
1840 * This cost of this function is O(n) in number of annotations.
1842 * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1847 g_dbus_annotation_info_lookup (GDBusAnnotationInfo
**annotations
,
1854 for (n
= 0; annotations
!= NULL
&& annotations
[n
] != NULL
; n
++)
1856 if (g_strcmp0 (annotations
[n
]->key
, name
) == 0)
1858 ret
= annotations
[n
]->value
;
1867 /* ---------------------------------------------------------------------------------------------------- */
1869 G_LOCK_DEFINE_STATIC (info_cache_lock
);
1875 /* gchar* -> GDBusMethodInfo* */
1876 GHashTable
*method_name_to_data
;
1878 /* gchar* -> GDBusMethodInfo* */
1879 GHashTable
*signal_name_to_data
;
1881 /* gchar* -> GDBusMethodInfo* */
1882 GHashTable
*property_name_to_data
;
1886 info_cache_free (InfoCacheEntry
*cache
)
1888 g_assert (cache
->use_count
== 0);
1889 g_hash_table_unref (cache
->method_name_to_data
);
1890 g_hash_table_unref (cache
->signal_name_to_data
);
1891 g_hash_table_unref (cache
->property_name_to_data
);
1892 g_slice_free (InfoCacheEntry
, cache
);
1895 /* maps from GDBusInterfaceInfo* to InfoCacheEntry* */
1896 static GHashTable
*info_cache
= NULL
;
1898 /* ---------------------------------------------------------------------------------------------------- */
1901 * g_dbus_interface_info_lookup_method:
1902 * @info: A #GDBusInterfaceInfo.
1903 * @name: A D-Bus method name (typically in CamelCase)
1905 * Looks up information about a method.
1907 * This cost of this function is O(n) in number of methods unless
1908 * g_dbus_interface_info_cache_build() has been used on @info.
1910 * Returns: (transfer none): A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1915 g_dbus_interface_info_lookup_method (GDBusInterfaceInfo
*info
,
1919 GDBusMethodInfo
*result
;
1921 G_LOCK (info_cache_lock
);
1922 if (G_LIKELY (info_cache
!= NULL
))
1924 InfoCacheEntry
*cache
;
1925 cache
= g_hash_table_lookup (info_cache
, info
);
1926 if (G_LIKELY (cache
!= NULL
))
1928 result
= g_hash_table_lookup (cache
->method_name_to_data
, name
);
1929 G_UNLOCK (info_cache_lock
);
1933 G_UNLOCK (info_cache_lock
);
1935 for (n
= 0; info
->methods
!= NULL
&& info
->methods
[n
] != NULL
; n
++)
1937 GDBusMethodInfo
*i
= info
->methods
[n
];
1939 if (g_strcmp0 (i
->name
, name
) == 0)
1952 /* ---------------------------------------------------------------------------------------------------- */
1955 * g_dbus_interface_info_lookup_signal:
1956 * @info: A #GDBusInterfaceInfo.
1957 * @name: A D-Bus signal name (typically in CamelCase)
1959 * Looks up information about a signal.
1961 * This cost of this function is O(n) in number of signals unless
1962 * g_dbus_interface_info_cache_build() has been used on @info.
1964 * Returns: (transfer none): A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1969 g_dbus_interface_info_lookup_signal (GDBusInterfaceInfo
*info
,
1973 GDBusSignalInfo
*result
;
1975 G_LOCK (info_cache_lock
);
1976 if (G_LIKELY (info_cache
!= NULL
))
1978 InfoCacheEntry
*cache
;
1979 cache
= g_hash_table_lookup (info_cache
, info
);
1980 if (G_LIKELY (cache
!= NULL
))
1982 result
= g_hash_table_lookup (cache
->signal_name_to_data
, name
);
1983 G_UNLOCK (info_cache_lock
);
1987 G_UNLOCK (info_cache_lock
);
1989 for (n
= 0; info
->signals
!= NULL
&& info
->signals
[n
] != NULL
; n
++)
1991 GDBusSignalInfo
*i
= info
->signals
[n
];
1993 if (g_strcmp0 (i
->name
, name
) == 0)
2006 /* ---------------------------------------------------------------------------------------------------- */
2009 * g_dbus_interface_info_lookup_property:
2010 * @info: A #GDBusInterfaceInfo.
2011 * @name: A D-Bus property name (typically in CamelCase).
2013 * Looks up information about a property.
2015 * This cost of this function is O(n) in number of properties unless
2016 * g_dbus_interface_info_cache_build() has been used on @info.
2018 * Returns: (transfer none): A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
2023 g_dbus_interface_info_lookup_property (GDBusInterfaceInfo
*info
,
2027 GDBusPropertyInfo
*result
;
2029 G_LOCK (info_cache_lock
);
2030 if (G_LIKELY (info_cache
!= NULL
))
2032 InfoCacheEntry
*cache
;
2033 cache
= g_hash_table_lookup (info_cache
, info
);
2034 if (G_LIKELY (cache
!= NULL
))
2036 result
= g_hash_table_lookup (cache
->property_name_to_data
, name
);
2037 G_UNLOCK (info_cache_lock
);
2041 G_UNLOCK (info_cache_lock
);
2043 for (n
= 0; info
->properties
!= NULL
&& info
->properties
[n
] != NULL
; n
++)
2045 GDBusPropertyInfo
*i
= info
->properties
[n
];
2047 if (g_strcmp0 (i
->name
, name
) == 0)
2060 /* ---------------------------------------------------------------------------------------------------- */
2063 * g_dbus_interface_info_cache_build:
2064 * @info: A #GDBusInterfaceInfo.
2066 * Builds a lookup-cache to speed up
2067 * g_dbus_interface_info_lookup_method(),
2068 * g_dbus_interface_info_lookup_signal() and
2069 * g_dbus_interface_info_lookup_property().
2071 * If this has already been called with @info, the existing cache is
2072 * used and its use count is increased.
2074 * Note that @info cannot be modified until
2075 * g_dbus_interface_info_cache_release() is called.
2080 g_dbus_interface_info_cache_build (GDBusInterfaceInfo
*info
)
2082 InfoCacheEntry
*cache
;
2085 G_LOCK (info_cache_lock
);
2086 if (info_cache
== NULL
)
2087 info_cache
= g_hash_table_new_full (g_direct_hash
, g_direct_equal
, NULL
, (GDestroyNotify
) info_cache_free
);
2088 cache
= g_hash_table_lookup (info_cache
, info
);
2091 cache
->use_count
+= 1;
2094 cache
= g_slice_new0 (InfoCacheEntry
);
2095 cache
->use_count
= 1;
2096 cache
->method_name_to_data
= g_hash_table_new (g_str_hash
, g_str_equal
);
2097 cache
->signal_name_to_data
= g_hash_table_new (g_str_hash
, g_str_equal
);
2098 cache
->property_name_to_data
= g_hash_table_new (g_str_hash
, g_str_equal
);
2099 for (n
= 0; info
->methods
!= NULL
&& info
->methods
[n
] != NULL
; n
++)
2100 g_hash_table_insert (cache
->method_name_to_data
, info
->methods
[n
]->name
, info
->methods
[n
]);
2101 for (n
= 0; info
->signals
!= NULL
&& info
->signals
[n
] != NULL
; n
++)
2102 g_hash_table_insert (cache
->signal_name_to_data
, info
->signals
[n
]->name
, info
->signals
[n
]);
2103 for (n
= 0; info
->properties
!= NULL
&& info
->properties
[n
] != NULL
; n
++)
2104 g_hash_table_insert (cache
->property_name_to_data
, info
->properties
[n
]->name
, info
->properties
[n
]);
2105 g_hash_table_insert (info_cache
, info
, cache
);
2107 G_UNLOCK (info_cache_lock
);
2111 * g_dbus_interface_info_cache_release:
2112 * @info: A GDBusInterfaceInfo
2114 * Decrements the usage count for the cache for @info built by
2115 * g_dbus_interface_info_cache_build() (if any) and frees the
2116 * resources used by the cache if the usage count drops to zero.
2121 g_dbus_interface_info_cache_release (GDBusInterfaceInfo
*info
)
2123 InfoCacheEntry
*cache
;
2125 G_LOCK (info_cache_lock
);
2126 if (G_UNLIKELY (info_cache
== NULL
))
2128 g_warning ("%s called for interface %s but there is no cache", info
->name
, G_STRFUNC
);
2132 cache
= g_hash_table_lookup (info_cache
, info
);
2133 if (G_UNLIKELY (cache
== NULL
))
2135 g_warning ("%s called for interface %s but there is no cache entry", info
->name
, G_STRFUNC
);
2138 cache
->use_count
-= 1;
2139 if (cache
->use_count
== 0)
2141 g_hash_table_remove (info_cache
, info
);
2142 /* could nuke info_cache itself if empty */
2145 G_UNLOCK (info_cache_lock
);
2149 /* ---------------------------------------------------------------------------------------------------- */
2152 * g_dbus_node_info_lookup_interface:
2153 * @info: A #GDBusNodeInfo.
2154 * @name: A D-Bus interface name.
2156 * Looks up information about an interface.
2158 * This cost of this function is O(n) in number of interfaces.
2160 * Returns: (transfer none): A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @info.
2164 GDBusInterfaceInfo
*
2165 g_dbus_node_info_lookup_interface (GDBusNodeInfo
*info
,
2169 GDBusInterfaceInfo
*result
;
2171 for (n
= 0; info
->interfaces
!= NULL
&& info
->interfaces
[n
] != NULL
; n
++)
2173 GDBusInterfaceInfo
*i
= info
->interfaces
[n
];
2175 if (g_strcmp0 (i
->name
, name
) == 0)