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 * <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus specification</link>.
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,
466 GDBusArgInfo **in_args,
468 GDBusArgInfo **out_args,
469 GDBusAnnotationInfo **annotations)
474 info->name = g_strdup (name);
476 if (in_num_args != 0)
478 //info->in_num_args = in_num_args;
479 info->in_args = in_args;
482 if (out_num_args != 0)
484 //info->out_num_args = out_num_args;
485 info->out_args = out_args;
488 if (annotations != NULL)
489 info->annotations = annotations;
493 g_dbus_signal_info_set (ParseData *data,
494 GDBusSignalInfo *info,
498 GDBusAnnotationInfo **annotations)
503 info->name = g_strdup (name);
507 //info->num_args = num_args;
511 if (annotations != NULL)
513 info->annotations = annotations;
518 g_dbus_property_info_set (ParseData *data,
519 GDBusPropertyInfo *info,
521 const gchar *signature,
522 GDBusPropertyInfoFlags flags,
523 GDBusAnnotationInfo **annotations)
528 info->name = g_strdup (name);
530 if (flags != G_DBUS_PROPERTY_INFO_FLAGS_NONE)
533 if (signature != NULL)
535 info->signature = g_strdup (signature);
538 if (annotations != NULL)
540 info->annotations = annotations;
545 g_dbus_interface_info_set (ParseData *data,
546 GDBusInterfaceInfo *info,
549 GDBusMethodInfo **methods,
551 GDBusSignalInfo **signals,
552 guint num_properties,
553 GDBusPropertyInfo **properties,
554 GDBusAnnotationInfo **annotations)
560 info->name = g_strdup (name);
563 if (num_methods != 0)
565 //info->num_methods = num_methods;
566 info->methods = methods;
569 if (num_signals != 0)
571 //info->num_signals = num_signals;
572 info->signals = signals;
575 if (num_properties != 0)
577 //info->num_properties = num_properties;
578 info->properties = properties;
581 if (annotations != NULL)
583 info->annotations = annotations;
588 g_dbus_node_info_set (ParseData *data,
591 guint num_interfaces,
592 GDBusInterfaceInfo **interfaces,
594 GDBusNodeInfo **nodes,
595 GDBusAnnotationInfo **annotations)
601 info->path = g_strdup (path);
602 /* TODO: relative / absolute path snafu */
605 if (num_interfaces != 0)
607 //info->num_interfaces = num_interfaces;
608 info->interfaces = interfaces;
613 //info->num_nodes = num_nodes;
617 if (annotations != NULL)
619 info->annotations = annotations;
624 /* ---------------------------------------------------------------------------------------------------- */
627 g_dbus_annotation_info_generate_xml (GDBusAnnotationInfo *info,
629 GString *string_builder)
633 g_string_append_printf (string_builder, "%*s<annotation name=\"%s\" value=\"%s\"",
638 if (info->annotations == NULL)
640 g_string_append (string_builder, "/>\n");
644 g_string_append (string_builder, ">\n");
646 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
647 g_dbus_annotation_info_generate_xml (info->annotations[n],
651 g_string_append_printf (string_builder, "%*s</annotation>\n",
658 g_dbus_arg_info_generate_xml (GDBusArgInfo *info,
660 const gchar *extra_attributes,
661 GString *string_builder)
665 g_string_append_printf (string_builder, "%*s<arg type=\"%s\"",
669 if (info->name != NULL)
670 g_string_append_printf (string_builder, " name=\"%s\"", info->name);
672 if (extra_attributes != NULL)
673 g_string_append_printf (string_builder, " %s", extra_attributes);
675 if (info->annotations == NULL)
677 g_string_append (string_builder, "/>\n");
681 g_string_append (string_builder, ">\n");
683 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
684 g_dbus_annotation_info_generate_xml (info->annotations[n],
688 g_string_append_printf (string_builder, "%*s</arg>\n", indent, "");
694 g_dbus_method_info_generate_xml (GDBusMethodInfo *info,
696 GString *string_builder)
700 g_string_append_printf (string_builder, "%*s<method name=\"%s\"",
704 if (info->annotations == NULL && info->in_args == NULL && info->out_args == NULL)
706 g_string_append (string_builder, "/>\n");
710 g_string_append (string_builder, ">\n");
712 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
713 g_dbus_annotation_info_generate_xml (info->annotations[n],
717 for (n = 0; info->in_args != NULL && info->in_args[n] != NULL; n++)
718 g_dbus_arg_info_generate_xml (info->in_args[n],
723 for (n = 0; info->out_args != NULL && info->out_args[n] != NULL; n++)
724 g_dbus_arg_info_generate_xml (info->out_args[n],
729 g_string_append_printf (string_builder, "%*s</method>\n", indent, "");
734 g_dbus_signal_info_generate_xml (GDBusSignalInfo *info,
736 GString *string_builder)
740 g_string_append_printf (string_builder, "%*s<signal name=\"%s\"",
744 if (info->annotations == NULL && info->args == NULL)
746 g_string_append (string_builder, "/>\n");
750 g_string_append (string_builder, ">\n");
752 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
753 g_dbus_annotation_info_generate_xml (info->annotations[n],
757 for (n = 0; info->args != NULL && info->args[n] != NULL; n++)
758 g_dbus_arg_info_generate_xml (info->args[n],
763 g_string_append_printf (string_builder, "%*s</signal>\n", indent, "");
768 g_dbus_property_info_generate_xml (GDBusPropertyInfo *info,
770 GString *string_builder)
773 const gchar *access_string;
775 if ((info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE) &&
776 (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
778 access_string = "readwrite";
780 else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
782 access_string = "read";
784 else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
786 access_string = "write";
790 g_assert_not_reached ();
793 g_string_append_printf (string_builder, "%*s<property type=\"%s\" name=\"%s\" access=\"%s\"",
799 if (info->annotations == NULL)
801 g_string_append (string_builder, "/>\n");
805 g_string_append (string_builder, ">\n");
807 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
808 g_dbus_annotation_info_generate_xml (info->annotations[n],
812 g_string_append_printf (string_builder, "%*s</property>\n", indent, "");
818 * g_dbus_interface_info_generate_xml:
819 * @info: A #GDBusNodeInfo
820 * @indent: Indentation level.
821 * @string_builder: A #GString to to append XML data to.
823 * Appends an XML representation of @info (and its children) to @string_builder.
825 * This function is typically used for generating introspection XML
826 * documents at run-time for handling the
827 * <literal>org.freedesktop.DBus.Introspectable.Introspect</literal>
833 g_dbus_interface_info_generate_xml (GDBusInterfaceInfo *info,
835 GString *string_builder)
839 g_string_append_printf (string_builder, "%*s<interface name=\"%s\">\n",
843 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
844 g_dbus_annotation_info_generate_xml (info->annotations[n],
848 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
849 g_dbus_method_info_generate_xml (info->methods[n],
853 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
854 g_dbus_signal_info_generate_xml (info->signals[n],
858 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
859 g_dbus_property_info_generate_xml (info->properties[n],
863 g_string_append_printf (string_builder, "%*s</interface>\n", indent, "");
867 * g_dbus_node_info_generate_xml:
868 * @info: A #GDBusNodeInfo.
869 * @indent: Indentation level.
870 * @string_builder: A #GString to to append XML data to.
872 * Appends an XML representation of @info (and its children) to @string_builder.
874 * This function is typically used for generating introspection XML documents at run-time for
875 * handling the <literal>org.freedesktop.DBus.Introspectable.Introspect</literal> method.
880 g_dbus_node_info_generate_xml (GDBusNodeInfo *info,
882 GString *string_builder)
886 g_string_append_printf (string_builder, "%*s<node", indent, "");
887 if (info->path != NULL)
888 g_string_append_printf (string_builder, " name=\"%s\"", info->path);
890 if (info->interfaces == NULL && info->nodes == NULL && info->annotations == NULL)
892 g_string_append (string_builder, "/>\n");
896 g_string_append (string_builder, ">\n");
898 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
899 g_dbus_annotation_info_generate_xml (info->annotations[n],
903 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
904 g_dbus_interface_info_generate_xml (info->interfaces[n],
908 for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
909 g_dbus_node_info_generate_xml (info->nodes[n],
913 g_string_append_printf (string_builder, "%*s</node>\n", indent, "");
917 /* ---------------------------------------------------------------------------------------------------- */
919 static GDBusAnnotationInfo **
920 parse_data_steal_annotations (ParseData *data,
921 guint *out_num_elements)
923 GDBusAnnotationInfo **ret;
924 if (out_num_elements != NULL)
925 *out_num_elements = data->annotations->len;
926 if (data->annotations == NULL)
930 g_ptr_array_add (data->annotations, NULL);
931 ret = (GDBusAnnotationInfo **) g_ptr_array_free (data->annotations, FALSE);
933 data->annotations = g_ptr_array_new ();
937 static GDBusArgInfo **
938 parse_data_steal_args (ParseData *data,
939 guint *out_num_elements)
942 if (out_num_elements != NULL)
943 *out_num_elements = data->args->len;
944 if (data->args == NULL)
948 g_ptr_array_add (data->args, NULL);
949 ret = (GDBusArgInfo **) g_ptr_array_free (data->args, FALSE);
951 data->args = g_ptr_array_new ();
955 static GDBusArgInfo **
956 parse_data_steal_out_args (ParseData *data,
957 guint *out_num_elements)
960 if (out_num_elements != NULL)
961 *out_num_elements = data->out_args->len;
962 if (data->out_args == NULL)
966 g_ptr_array_add (data->out_args, NULL);
967 ret = (GDBusArgInfo **) g_ptr_array_free (data->out_args, FALSE);
969 data->out_args = g_ptr_array_new ();
973 static GDBusMethodInfo **
974 parse_data_steal_methods (ParseData *data,
975 guint *out_num_elements)
977 GDBusMethodInfo **ret;
978 if (out_num_elements != NULL)
979 *out_num_elements = data->methods->len;
980 if (data->methods == NULL)
984 g_ptr_array_add (data->methods, NULL);
985 ret = (GDBusMethodInfo **) g_ptr_array_free (data->methods, FALSE);
987 data->methods = g_ptr_array_new ();
991 static GDBusSignalInfo **
992 parse_data_steal_signals (ParseData *data,
993 guint *out_num_elements)
995 GDBusSignalInfo **ret;
996 if (out_num_elements != NULL)
997 *out_num_elements = data->signals->len;
998 if (data->signals == NULL)
1002 g_ptr_array_add (data->signals, NULL);
1003 ret = (GDBusSignalInfo **) g_ptr_array_free (data->signals, FALSE);
1005 data->signals = g_ptr_array_new ();
1009 static GDBusPropertyInfo **
1010 parse_data_steal_properties (ParseData *data,
1011 guint *out_num_elements)
1013 GDBusPropertyInfo **ret;
1014 if (out_num_elements != NULL)
1015 *out_num_elements = data->properties->len;
1016 if (data->properties == NULL)
1020 g_ptr_array_add (data->properties, NULL);
1021 ret = (GDBusPropertyInfo **) g_ptr_array_free (data->properties, FALSE);
1023 data->properties = g_ptr_array_new ();
1027 static GDBusInterfaceInfo **
1028 parse_data_steal_interfaces (ParseData *data,
1029 guint *out_num_elements)
1031 GDBusInterfaceInfo **ret;
1032 if (out_num_elements != NULL)
1033 *out_num_elements = data->interfaces->len;
1034 if (data->interfaces == NULL)
1038 g_ptr_array_add (data->interfaces, NULL);
1039 ret = (GDBusInterfaceInfo **) g_ptr_array_free (data->interfaces, FALSE);
1041 data->interfaces = g_ptr_array_new ();
1045 static GDBusNodeInfo **
1046 parse_data_steal_nodes (ParseData *data,
1047 guint *out_num_elements)
1049 GDBusNodeInfo **ret;
1050 if (out_num_elements != NULL)
1051 *out_num_elements = data->nodes->len;
1052 if (data->nodes == NULL)
1056 g_ptr_array_add (data->nodes, NULL);
1057 ret = (GDBusNodeInfo **) g_ptr_array_free (data->nodes, FALSE);
1059 data->nodes = g_ptr_array_new ();
1063 /* ---------------------------------------------------------------------------------------------------- */
1066 parse_data_free_annotations (ParseData *data)
1068 if (data->annotations == NULL)
1070 g_ptr_array_foreach (data->annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1071 g_ptr_array_free (data->annotations, TRUE);
1072 data->annotations = NULL;
1076 parse_data_free_args (ParseData *data)
1078 if (data->args == NULL)
1080 g_ptr_array_foreach (data->args, (GFunc) g_dbus_arg_info_unref, NULL);
1081 g_ptr_array_free (data->args, TRUE);
1086 parse_data_free_out_args (ParseData *data)
1088 if (data->out_args == NULL)
1090 g_ptr_array_foreach (data->out_args, (GFunc) g_dbus_arg_info_unref, NULL);
1091 g_ptr_array_free (data->out_args, TRUE);
1092 data->out_args = NULL;
1096 parse_data_free_methods (ParseData *data)
1098 if (data->methods == NULL)
1100 g_ptr_array_foreach (data->methods, (GFunc) g_dbus_method_info_unref, NULL);
1101 g_ptr_array_free (data->methods, TRUE);
1102 data->methods = NULL;
1106 parse_data_free_signals (ParseData *data)
1108 if (data->signals == NULL)
1110 g_ptr_array_foreach (data->signals, (GFunc) g_dbus_signal_info_unref, NULL);
1111 g_ptr_array_free (data->signals, TRUE);
1112 data->signals = NULL;
1116 parse_data_free_properties (ParseData *data)
1118 if (data->properties == NULL)
1120 g_ptr_array_foreach (data->properties, (GFunc) g_dbus_property_info_unref, NULL);
1121 g_ptr_array_free (data->properties, TRUE);
1122 data->properties = NULL;
1126 parse_data_free_interfaces (ParseData *data)
1128 if (data->interfaces == NULL)
1130 g_ptr_array_foreach (data->interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1131 g_ptr_array_free (data->interfaces, TRUE);
1132 data->interfaces = NULL;
1136 parse_data_free_nodes (ParseData *data)
1138 if (data->nodes == NULL)
1140 g_ptr_array_foreach (data->nodes, (GFunc) g_dbus_node_info_unref, NULL);
1141 g_ptr_array_free (data->nodes, TRUE);
1145 /* ---------------------------------------------------------------------------------------------------- */
1147 static GDBusAnnotationInfo *
1148 parse_data_get_annotation (ParseData *data,
1149 gboolean create_new)
1152 g_ptr_array_add (data->annotations, g_new0 (GDBusAnnotationInfo, 1));
1153 return data->annotations->pdata[data->annotations->len - 1];
1156 static GDBusArgInfo *
1157 parse_data_get_arg (ParseData *data,
1158 gboolean create_new)
1161 g_ptr_array_add (data->args, g_new0 (GDBusArgInfo, 1));
1162 return data->args->pdata[data->args->len - 1];
1165 static GDBusArgInfo *
1166 parse_data_get_out_arg (ParseData *data,
1167 gboolean create_new)
1170 g_ptr_array_add (data->out_args, g_new0 (GDBusArgInfo, 1));
1171 return data->out_args->pdata[data->out_args->len - 1];
1174 static GDBusMethodInfo *
1175 parse_data_get_method (ParseData *data,
1176 gboolean create_new)
1179 g_ptr_array_add (data->methods, g_new0 (GDBusMethodInfo, 1));
1180 return data->methods->pdata[data->methods->len - 1];
1183 static GDBusSignalInfo *
1184 parse_data_get_signal (ParseData *data,
1185 gboolean create_new)
1188 g_ptr_array_add (data->signals, g_new0 (GDBusSignalInfo, 1));
1189 return data->signals->pdata[data->signals->len - 1];
1192 static GDBusPropertyInfo *
1193 parse_data_get_property (ParseData *data,
1194 gboolean create_new)
1197 g_ptr_array_add (data->properties, g_new0 (GDBusPropertyInfo, 1));
1198 return data->properties->pdata[data->properties->len - 1];
1201 static GDBusInterfaceInfo *
1202 parse_data_get_interface (ParseData *data,
1203 gboolean create_new)
1206 g_ptr_array_add (data->interfaces, g_new0 (GDBusInterfaceInfo, 1));
1207 return data->interfaces->pdata[data->interfaces->len - 1];
1210 static GDBusNodeInfo *
1211 parse_data_get_node (ParseData *data,
1212 gboolean create_new)
1215 g_ptr_array_add (data->nodes, g_new0 (GDBusNodeInfo, 1));
1216 return data->nodes->pdata[data->nodes->len - 1];
1219 /* ---------------------------------------------------------------------------------------------------- */
1222 parse_data_new (void)
1226 data = g_new0 (ParseData, 1);
1228 /* initialize arrays */
1229 parse_data_steal_annotations (data, NULL);
1230 parse_data_steal_args (data, NULL);
1231 parse_data_steal_out_args (data, NULL);
1232 parse_data_steal_methods (data, NULL);
1233 parse_data_steal_signals (data, NULL);
1234 parse_data_steal_properties (data, NULL);
1235 parse_data_steal_interfaces (data, NULL);
1236 parse_data_steal_nodes (data, NULL);
1242 parse_data_free (ParseData *data)
1246 /* free stack of annotation arrays */
1247 for (l = data->annotations_stack; l != NULL; l = l->next)
1249 GPtrArray *annotations = l->data;
1250 g_ptr_array_foreach (annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1251 g_ptr_array_free (annotations, TRUE);
1253 g_slist_free (data->annotations_stack);
1255 /* free stack of interface arrays */
1256 for (l = data->interfaces_stack; l != NULL; l = l->next)
1258 GPtrArray *interfaces = l->data;
1259 g_ptr_array_foreach (interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1260 g_ptr_array_free (interfaces, TRUE);
1262 g_slist_free (data->interfaces_stack);
1264 /* free stack of node arrays */
1265 for (l = data->nodes_stack; l != NULL; l = l->next)
1267 GPtrArray *nodes = l->data;
1268 g_ptr_array_foreach (nodes, (GFunc) g_dbus_node_info_unref, NULL);
1269 g_ptr_array_free (nodes, TRUE);
1271 g_slist_free (data->nodes_stack);
1273 /* free arrays (data->annotations, data->interfaces and data->nodes have been freed above) */
1274 parse_data_free_args (data);
1275 parse_data_free_out_args (data);
1276 parse_data_free_methods (data);
1277 parse_data_free_signals (data);
1278 parse_data_free_properties (data);
1283 /* ---------------------------------------------------------------------------------------------------- */
1286 parser_start_element (GMarkupParseContext *context,
1287 const gchar *element_name,
1288 const gchar **attribute_names,
1289 const gchar **attribute_values,
1293 ParseData *data = user_data;
1297 const gchar *access;
1298 const gchar *direction;
1307 stack = (GSList *) g_markup_parse_context_get_element_stack (context);
1309 /* ---------------------------------------------------------------------------------------------------- */
1310 if (strcmp (element_name, "node") == 0)
1312 if (!(g_slist_length (stack) >= 1 || strcmp (stack->next->data, "node") != 0))
1314 g_set_error_literal (error,
1316 G_MARKUP_ERROR_INVALID_CONTENT,
1317 "<node> elements can only be top-level or embedded in other <node> elements");
1321 if (!g_markup_collect_attributes (element_name,
1325 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1326 /* some hand-written introspection XML documents use this */
1327 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:doc", NULL,
1328 G_MARKUP_COLLECT_INVALID))
1331 g_dbus_node_info_set (data,
1332 parse_data_get_node (data, TRUE),
1338 /* push the currently retrieved interfaces and nodes on the stack and prepare new arrays */
1339 data->interfaces_stack = g_slist_prepend (data->interfaces_stack, data->interfaces);
1340 data->interfaces = NULL;
1341 parse_data_steal_interfaces (data, NULL);
1343 data->nodes_stack = g_slist_prepend (data->nodes_stack, data->nodes);
1345 parse_data_steal_nodes (data, NULL);
1348 /* ---------------------------------------------------------------------------------------------------- */
1349 else if (strcmp (element_name, "interface") == 0)
1351 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "node") != 0)
1353 g_set_error_literal (error,
1355 G_MARKUP_ERROR_INVALID_CONTENT,
1356 "<interface> elements can only be embedded in <node> elements");
1360 if (!g_markup_collect_attributes (element_name,
1364 G_MARKUP_COLLECT_STRING, "name", &name,
1365 G_MARKUP_COLLECT_INVALID))
1368 g_dbus_interface_info_set (data,
1369 parse_data_get_interface (data, TRUE),
1377 /* ---------------------------------------------------------------------------------------------------- */
1378 else if (strcmp (element_name, "method") == 0)
1380 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1382 g_set_error_literal (error,
1384 G_MARKUP_ERROR_INVALID_CONTENT,
1385 "<method> elements can only be embedded in <interface> elements");
1389 if (!g_markup_collect_attributes (element_name,
1393 G_MARKUP_COLLECT_STRING, "name", &name,
1394 G_MARKUP_COLLECT_INVALID))
1397 g_dbus_method_info_set (data,
1398 parse_data_get_method (data, TRUE),
1407 /* ---------------------------------------------------------------------------------------------------- */
1408 else if (strcmp (element_name, "signal") == 0)
1410 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1412 g_set_error_literal (error,
1414 G_MARKUP_ERROR_INVALID_CONTENT,
1415 "<signal> elements can only be embedded in <interface> elements");
1419 if (!g_markup_collect_attributes (element_name,
1423 G_MARKUP_COLLECT_STRING, "name", &name,
1424 G_MARKUP_COLLECT_INVALID))
1427 g_dbus_signal_info_set (data,
1428 parse_data_get_signal (data, TRUE),
1436 /* ---------------------------------------------------------------------------------------------------- */
1437 else if (strcmp (element_name, "property") == 0)
1439 GDBusPropertyInfoFlags flags;
1441 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1443 g_set_error_literal (error,
1445 G_MARKUP_ERROR_INVALID_CONTENT,
1446 "<property> elements can only be embedded in <interface> elements");
1450 if (!g_markup_collect_attributes (element_name,
1454 G_MARKUP_COLLECT_STRING, "name", &name,
1455 G_MARKUP_COLLECT_STRING, "type", &type,
1456 G_MARKUP_COLLECT_STRING, "access", &access,
1457 G_MARKUP_COLLECT_INVALID))
1460 if (strcmp (access, "read") == 0)
1461 flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE;
1462 else if (strcmp (access, "write") == 0)
1463 flags = G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1464 else if (strcmp (access, "readwrite") == 0)
1465 flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1470 G_MARKUP_ERROR_INVALID_CONTENT,
1471 "Unknown value '%s' of access attribute for element <property>",
1476 g_dbus_property_info_set (data,
1477 parse_data_get_property (data, TRUE),
1484 /* ---------------------------------------------------------------------------------------------------- */
1485 else if (strcmp (element_name, "arg") == 0)
1490 if (g_slist_length (stack) < 2 ||
1491 (strcmp (stack->next->data, "method") != 0 &&
1492 strcmp (stack->next->data, "signal") != 0))
1494 g_set_error_literal (error,
1496 G_MARKUP_ERROR_INVALID_CONTENT,
1497 "<arg> elements can only be embedded in <method> or <signal> elements");
1501 if (!g_markup_collect_attributes (element_name,
1505 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1506 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "direction", &direction,
1507 G_MARKUP_COLLECT_STRING, "type", &type,
1508 G_MARKUP_COLLECT_INVALID))
1511 if (strcmp (stack->next->data, "method") == 0)
1515 if (direction != NULL)
1517 if (strcmp (direction, "in") == 0)
1519 else if (strcmp (direction, "out") == 0)
1525 G_MARKUP_ERROR_INVALID_CONTENT,
1526 "Unknown value '%s' of direction attribute",
1532 if (is_in && strcmp (stack->next->data, "signal") == 0)
1534 g_set_error_literal (error,
1536 G_MARKUP_ERROR_INVALID_CONTENT,
1537 "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
1542 name_to_use = g_strdup_printf ("arg_%d", data->num_args);
1544 name_to_use = g_strdup (name);
1549 g_dbus_arg_info_set (data,
1550 parse_data_get_arg (data, TRUE),
1554 data->last_arg_was_in = TRUE;
1558 g_dbus_arg_info_set (data,
1559 parse_data_get_out_arg (data, TRUE),
1563 data->last_arg_was_in = FALSE;
1567 g_free (name_to_use);
1569 /* ---------------------------------------------------------------------------------------------------- */
1570 else if (strcmp (element_name, "annotation") == 0)
1572 if (g_slist_length (stack) < 2 ||
1573 (strcmp (stack->next->data, "node") != 0 &&
1574 strcmp (stack->next->data, "interface") != 0 &&
1575 strcmp (stack->next->data, "signal") != 0 &&
1576 strcmp (stack->next->data, "method") != 0 &&
1577 strcmp (stack->next->data, "property") != 0 &&
1578 strcmp (stack->next->data, "arg") != 0 &&
1579 strcmp (stack->next->data, "annotation") != 0))
1581 g_set_error_literal (error,
1583 G_MARKUP_ERROR_INVALID_CONTENT,
1584 "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
1588 if (!g_markup_collect_attributes (element_name,
1592 G_MARKUP_COLLECT_STRING, "name", &name,
1593 G_MARKUP_COLLECT_STRING, "value", &value,
1594 G_MARKUP_COLLECT_INVALID))
1597 g_dbus_annotation_info_set (data,
1598 parse_data_get_annotation (data, TRUE),
1603 /* ---------------------------------------------------------------------------------------------------- */
1606 /* don't bail on unknown elements; just ignore them */
1608 /* ---------------------------------------------------------------------------------------------------- */
1610 /* push the currently retrieved annotations on the stack and prepare a new one */
1611 data->annotations_stack = g_slist_prepend (data->annotations_stack, data->annotations);
1612 data->annotations = NULL;
1613 parse_data_steal_annotations (data, NULL);
1619 /* ---------------------------------------------------------------------------------------------------- */
1621 static GDBusAnnotationInfo **
1622 steal_annotations (ParseData *data)
1624 return parse_data_steal_annotations (data, NULL);
1629 parser_end_element (GMarkupParseContext *context,
1630 const gchar *element_name,
1634 ParseData *data = user_data;
1635 gboolean have_popped_annotations;
1637 have_popped_annotations = FALSE;
1639 if (strcmp (element_name, "node") == 0)
1642 guint num_interfaces;
1643 GDBusNodeInfo **nodes;
1644 GDBusInterfaceInfo **interfaces;
1646 nodes = parse_data_steal_nodes (data, &num_nodes);
1647 interfaces = parse_data_steal_interfaces (data, &num_interfaces);
1649 /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
1650 * scope we're reentering
1652 parse_data_free_interfaces (data);
1653 data->interfaces = (GPtrArray *) data->interfaces_stack->data;
1654 data->interfaces_stack = g_slist_remove (data->interfaces_stack, data->interfaces_stack->data);
1656 parse_data_free_nodes (data);
1657 data->nodes = (GPtrArray *) data->nodes_stack->data;
1658 data->nodes_stack = g_slist_remove (data->nodes_stack, data->nodes_stack->data);
1660 g_dbus_node_info_set (data,
1661 parse_data_get_node (data, FALSE),
1667 steal_annotations (data));
1670 else if (strcmp (element_name, "interface") == 0)
1674 guint num_properties;
1675 GDBusMethodInfo **methods;
1676 GDBusSignalInfo **signals;
1677 GDBusPropertyInfo **properties;
1679 methods = parse_data_steal_methods (data, &num_methods);
1680 signals = parse_data_steal_signals (data, &num_signals);
1681 properties = parse_data_steal_properties (data, &num_properties);
1683 g_dbus_interface_info_set (data,
1684 parse_data_get_interface (data, FALSE),
1692 steal_annotations (data));
1695 else if (strcmp (element_name, "method") == 0)
1699 GDBusArgInfo **in_args;
1700 GDBusArgInfo **out_args;
1702 in_args = parse_data_steal_args (data, &in_num_args);
1703 out_args = parse_data_steal_out_args (data, &out_num_args);
1705 g_dbus_method_info_set (data,
1706 parse_data_get_method (data, FALSE),
1712 steal_annotations (data));
1714 else if (strcmp (element_name, "signal") == 0)
1717 GDBusArgInfo **args;
1719 args = parse_data_steal_out_args (data, &num_args);
1721 g_dbus_signal_info_set (data,
1722 parse_data_get_signal (data, FALSE),
1726 steal_annotations (data));
1728 else if (strcmp (element_name, "property") == 0)
1730 g_dbus_property_info_set (data,
1731 parse_data_get_property (data, FALSE),
1734 G_DBUS_PROPERTY_INFO_FLAGS_NONE,
1735 steal_annotations (data));
1737 else if (strcmp (element_name, "arg") == 0)
1739 g_dbus_arg_info_set (data,
1740 data->last_arg_was_in ? parse_data_get_arg (data, FALSE) : parse_data_get_out_arg (data, FALSE),
1743 steal_annotations (data));
1745 else if (strcmp (element_name, "annotation") == 0)
1747 GDBusAnnotationInfo **embedded_annotations;
1749 embedded_annotations = steal_annotations (data);
1751 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1752 parse_data_free_annotations (data);
1753 data->annotations = (GPtrArray *) data->annotations_stack->data;
1754 data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1756 have_popped_annotations = TRUE;
1758 g_dbus_annotation_info_set (data,
1759 parse_data_get_annotation (data, FALSE),
1762 embedded_annotations);
1766 /* don't bail on unknown elements; just ignore them */
1769 if (!have_popped_annotations)
1771 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1772 parse_data_free_annotations (data);
1773 data->annotations = (GPtrArray *) data->annotations_stack->data;
1774 data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1778 /* ---------------------------------------------------------------------------------------------------- */
1781 parser_error (GMarkupParseContext *context,
1788 g_markup_parse_context_get_position (context, &line_number, &char_number);
1790 g_prefix_error (&error, "%d:%d: ",
1795 /* ---------------------------------------------------------------------------------------------------- */
1798 * g_dbus_node_info_new_for_xml:
1799 * @xml_data: Valid D-Bus introspection XML.
1800 * @error: Return location for error.
1802 * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
1804 * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
1805 * with g_dbus_node_info_unref().
1810 g_dbus_node_info_new_for_xml (const gchar *xml_data,
1814 GMarkupParseContext *context;
1815 GMarkupParser *parser;
1818 GDBusNodeInfo **ughret;
1824 parser = g_new0 (GMarkupParser, 1);
1825 parser->start_element = parser_start_element;
1826 parser->end_element = parser_end_element;
1827 parser->error = parser_error;
1829 data = parse_data_new ();
1830 context = g_markup_parse_context_new (parser,
1833 (GDestroyNotify) parse_data_free);
1835 if (!g_markup_parse_context_parse (context,
1841 ughret = parse_data_steal_nodes (data, &num_nodes);
1849 G_MARKUP_ERROR_INVALID_CONTENT,
1850 "Expected a single node in introspection XML, found %d",
1854 for (n = 0; n < num_nodes; n++)
1856 for (n = 0; n < num_nodes; n++)
1857 g_dbus_node_info_unref (&(ret[n]));
1869 if (context != NULL)
1870 g_markup_parse_context_free (context);
1875 /* ---------------------------------------------------------------------------------------------------- */
1878 * g_dbus_annotation_info_lookup:
1879 * @annotations: A %NULL-terminated array of annotations or %NULL.
1880 * @name: The name of the annotation to look up.
1882 * Looks up the value of an annotation.
1884 * This cost of this function is O(n) in number of annotations.
1886 * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1891 g_dbus_annotation_info_lookup (GDBusAnnotationInfo **annotations,
1898 for (n = 0; annotations != NULL && annotations[n] != NULL; n++)
1900 if (g_strcmp0 (annotations[n]->key, name) == 0)
1902 ret = annotations[n]->value;
1911 /* ---------------------------------------------------------------------------------------------------- */
1914 * g_dbus_interface_info_lookup_method:
1915 * @info: A #GDBusInterfaceInfo.
1916 * @name: A D-Bus method name (typically in CamelCase)
1918 * Looks up information about a method.
1920 * This cost of this function is O(n) in number of methods.
1922 * Returns: A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1927 g_dbus_interface_info_lookup_method (GDBusInterfaceInfo *info,
1931 GDBusMethodInfo *result;
1933 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
1935 GDBusMethodInfo *i = info->methods[n];
1937 if (g_strcmp0 (i->name, name) == 0)
1950 /* ---------------------------------------------------------------------------------------------------- */
1953 * g_dbus_interface_info_lookup_signal:
1954 * @info: A #GDBusInterfaceInfo.
1955 * @name: A D-Bus signal name (typically in CamelCase)
1957 * Looks up information about a signal.
1959 * This cost of this function is O(n) in number of signals.
1961 * Returns: A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1966 g_dbus_interface_info_lookup_signal (GDBusInterfaceInfo *info,
1970 GDBusSignalInfo *result;
1972 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
1974 GDBusSignalInfo *i = info->signals[n];
1976 if (g_strcmp0 (i->name, name) == 0)
1989 /* ---------------------------------------------------------------------------------------------------- */
1992 * g_dbus_interface_info_lookup_property:
1993 * @info: A #GDBusInterfaceInfo.
1994 * @name: A D-Bus property name (typically in CamelCase).
1996 * Looks up information about a property.
1998 * This cost of this function is O(n) in number of properties.
2000 * Returns: A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
2005 g_dbus_interface_info_lookup_property (GDBusInterfaceInfo *info,
2009 GDBusPropertyInfo *result;
2011 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
2013 GDBusPropertyInfo *i = info->properties[n];
2015 if (g_strcmp0 (i->name, name) == 0)
2028 /* ---------------------------------------------------------------------------------------------------- */
2031 * g_dbus_node_info_lookup_interface:
2032 * @info: A #GDBusNodeInfo.
2033 * @name: A D-Bus interface name.
2035 * Looks up information about an interface.
2037 * This cost of this function is O(n) in number of interfaces.
2039 * Returns: A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @info.
2043 GDBusInterfaceInfo *
2044 g_dbus_node_info_lookup_interface (GDBusNodeInfo *info,
2048 GDBusInterfaceInfo *result;
2050 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
2052 GDBusInterfaceInfo *i = info->interfaces[n];
2054 if (g_strcmp0 (i->name, name) == 0)