1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
2 /* GObject introspection: IDL generator
4 * Copyright (C) 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include <glib-object.h>
27 #include <glib/gstdio.h>
29 #include "girepository.h"
32 /* FIXME: Avoid global */
33 static gchar *output = NULL;
34 gchar **includedirs = NULL;
37 check_unresolved (GIBaseInfo *info)
39 if (g_base_info_get_type (info) != GI_INFO_TYPE_UNRESOLVED)
42 g_critical ("Found unresolved type '%s' '%s'\n",
43 g_base_info_get_name (info), g_base_info_get_namespace (info));
47 write_type_name (const gchar *namespace,
51 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
52 g_fprintf (file, "%s.", g_base_info_get_namespace (info));
54 g_fprintf (file, "%s", g_base_info_get_name (info));
58 write_type_info (const gchar *namespace,
67 const gchar* basic[] = {
91 check_unresolved ((GIBaseInfo*)info);
93 tag = g_type_info_get_tag (info);
94 is_pointer = g_type_info_is_pointer (info);
96 if (tag == GI_TYPE_TAG_VOID)
99 g_fprintf (file, "%s", "any");
101 g_fprintf (file, "%s", "none");
104 g_fprintf (file, "%s", basic[tag]);
109 type = g_type_info_get_param_type (info, 0);
110 write_type_info (namespace, type, file);
111 g_fprintf (file, "[");
113 length = g_type_info_get_array_length (info);
116 g_fprintf (file, "length=%d", length);
118 if (g_type_info_is_zero_terminated (info))
119 g_fprintf (file, "%szero-terminated=1", length >= 0 ? "," : "");
121 g_fprintf (file, "]");
122 g_base_info_unref ((GIBaseInfo *)type);
126 GIBaseInfo *iface = g_type_info_get_interface (info);
127 write_type_name (namespace, iface, file);
128 g_base_info_unref (iface);
132 type = g_type_info_get_param_type (info, 0);
133 g_fprintf (file, "GLib.List");
136 g_fprintf (file, "<");
137 write_type_info (namespace, type, file);
138 g_fprintf (file, ">");
139 g_base_info_unref ((GIBaseInfo *)type);
144 type = g_type_info_get_param_type (info, 0);
145 g_fprintf (file, "GLib.SList");
148 g_fprintf (file, "<");
149 write_type_info (namespace, type, file);
150 g_fprintf (file, ">");
151 g_base_info_unref ((GIBaseInfo *)type);
156 type = g_type_info_get_param_type (info, 0);
157 g_fprintf (file, "GLib.HashTable");
160 g_fprintf (file, "<");
161 write_type_info (namespace, type, file);
162 g_base_info_unref ((GIBaseInfo *)type);
163 type = g_type_info_get_param_type (info, 1);
164 g_fprintf (file, ",");
165 write_type_info (namespace, type, file);
166 g_fprintf (file, ">");
167 g_base_info_unref ((GIBaseInfo *)type);
174 g_fprintf (file, "GLib.Error");
175 n = g_type_info_get_n_error_domains (info);
178 g_fprintf (file, "<");
179 for (i = 0; i < n; i++)
181 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
183 g_fprintf (file, ",");
184 write_type_name (namespace, (GIBaseInfo *)ed, file);
185 g_base_info_unref ((GIBaseInfo *)ed);
187 g_fprintf (file, ">");
191 g_assert_not_reached ();
195 write_constant_value (const gchar *namespace,
201 write_field_info (const gchar *namespace,
203 GIConstantInfo *branch,
207 GIFieldInfoFlags flags;
213 name = g_base_info_get_name ((GIBaseInfo *)info);
214 flags = g_field_info_get_flags (info);
215 size = g_field_info_get_size (info);
216 offset = g_field_info_get_offset (info);
219 " <field name=\"%s\" readable=\"%s\" writable=\"%s\"",
221 flags & GI_FIELD_IS_READABLE ? "1" : "0",
222 flags & GI_FIELD_IS_WRITABLE ? "1" : "0");
224 g_fprintf (file, " bits=\"%d\"", size);
226 g_fprintf (file, " offset=\"%d\"", offset);
228 type = g_field_info_get_type (info);
232 g_fprintf (file, " branch=\"");
233 type = g_constant_info_get_type (branch);
234 g_constant_info_get_value (branch, &value);
235 write_constant_value (namespace, type, &value, file);
236 g_fprintf (file, "\"");
239 g_fprintf (file,">\n");
241 g_fprintf (file, " <type name=\"");
243 write_type_info (namespace, type, file);
244 g_base_info_unref ((GIBaseInfo *)type);
246 g_fprintf (file, "\"/>\n");
248 g_fprintf (file, " </field>\n");
253 write_callable_info (const gchar *namespace,
254 GICallableInfo *info,
261 type = g_callable_info_get_return_type (info);
263 if (g_type_info_is_pointer (type))
265 switch (g_callable_info_get_caller_owns (info))
267 case GI_TRANSFER_NOTHING:
269 case GI_TRANSFER_CONTAINER:
270 g_fprintf (file, " transfer=\"shallow\"");
272 case GI_TRANSFER_EVERYTHING:
273 g_fprintf (file, " transfer=\"full\"");
276 g_assert_not_reached ();
280 g_fprintf (file, ">\n");
282 g_fprintf (file, "%*s <return-value>\n", indent, "");
284 if (g_callable_info_may_return_null (info))
285 g_fprintf (file, " null-ok=\"1\"");
287 g_fprintf (file, "%*s <type name=\"", indent + 2, "");
289 write_type_info (namespace, type, file);
291 g_fprintf (file, "\"/>\n");
293 g_fprintf (file, "%*s </return-value>\n", indent, "");
295 if (g_callable_info_get_n_args (info) <= 0)
298 g_fprintf (file, "%*s <parameters>\n", indent, "");
299 for (i = 0; i < g_callable_info_get_n_args (info); i++)
301 GIArgInfo *arg = g_callable_info_get_arg (info, i);
303 g_fprintf (file, "%*s <parameter name=\"%s\"",
304 indent, "", g_base_info_get_name ((GIBaseInfo *) arg));
306 switch (g_arg_info_get_ownership_transfer (arg))
308 case GI_TRANSFER_NOTHING:
310 case GI_TRANSFER_CONTAINER:
311 g_fprintf (file, " transfer=\"shallow\"");
313 case GI_TRANSFER_EVERYTHING:
314 g_fprintf (file, " transfer=\"full\"");
317 g_assert_not_reached ();
320 g_fprintf (file, " direction=\"");
321 switch (g_arg_info_get_direction (arg))
323 case GI_DIRECTION_IN:
324 g_fprintf (file, "in");
326 case GI_DIRECTION_OUT:
327 g_fprintf (file, "out");
329 case GI_DIRECTION_INOUT:
330 g_fprintf (file, "inout");
333 g_fprintf (file, "\"");
335 if (g_arg_info_may_be_null (arg))
336 g_fprintf (file, " null-ok=\"1\"");
338 if (g_arg_info_is_dipper (arg))
339 g_fprintf (file, " dipper=\"1\"");
341 if (g_arg_info_is_return_value (arg))
342 g_fprintf (file, " retval=\"1\"");
344 if (g_arg_info_is_optional (arg))
345 g_fprintf (file, " optional=\"1\"");
347 g_fprintf (file, ">\n");
349 g_fprintf (file, "%*s <type name=\"", indent+2, "");
351 type = g_arg_info_get_type (arg);
352 write_type_info (namespace, type, file);
354 g_fprintf (file, "\"/>\n");
356 g_fprintf (file, "%*s </parameter>\n", indent, "");
358 g_base_info_unref ((GIBaseInfo *)arg);
361 g_fprintf (file, "%*s </parameters>\n", indent, "");
362 g_base_info_unref ((GIBaseInfo *)type);
366 write_function_info (const gchar *namespace,
367 GIFunctionInfo *info,
371 GIFunctionInfoFlags flags;
377 flags = g_function_info_get_flags (info);
378 name = g_base_info_get_name ((GIBaseInfo *)info);
379 symbol = g_function_info_get_symbol (info);
380 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
382 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
384 else if (flags & GI_FUNCTION_IS_METHOD)
389 g_fprintf (file, "%*s<%s name=\"%s\" c:identifier=\"%s\"",
390 indent, "", tag, name, symbol);
392 if (flags & GI_FUNCTION_IS_SETTER)
393 g_fprintf (file, " type=\"setter\"");
394 else if (flags & GI_FUNCTION_IS_GETTER)
395 g_fprintf (file, " type=\"getter\"");
398 g_fprintf (file, " deprecated=\"1\"");
400 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
401 g_fprintf (file, "%*s</%s>\n", indent, "", tag);
405 write_callback_info (const gchar *namespace,
406 GICallbackInfo *info,
413 name = g_base_info_get_name ((GIBaseInfo *)info);
414 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
416 g_fprintf (file, "%*s<callback name=\"%s\"", indent, "", name);
419 g_fprintf (file, " deprecated=\"1\"");
421 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
422 g_fprintf (file, "%*s</callback>\n", indent, "");
426 write_struct_info (const gchar *namespace,
431 const gchar *type_name;
432 const gchar *type_init;
437 name = g_base_info_get_name ((GIBaseInfo *)info);
438 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
440 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
441 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
443 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
445 g_fprintf (file, " <glib:boxed glib:name=\"%s\"", name);
448 g_fprintf (file, " <record name=\"%s\"", name);
450 if (type_name != NULL)
451 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
454 g_fprintf (file, " deprecated=\"1\"");
456 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
459 g_fprintf (file, ">\n");
461 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
463 GIFieldInfo *field = g_struct_info_get_field (info, i);
464 write_field_info (namespace, field, NULL, file);
465 g_base_info_unref ((GIBaseInfo *)field);
468 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
470 GIFunctionInfo *function = g_struct_info_get_method (info, i);
471 write_function_info (namespace, function, file, 6);
472 g_base_info_unref ((GIBaseInfo *)function);
475 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
476 g_fprintf (file, " </glib:boxed>\n");
478 g_fprintf (file, " </record>\n");
482 g_fprintf (file, "/>\n");
487 write_value_info (const gchar *namespace,
495 name = g_base_info_get_name ((GIBaseInfo *)info);
496 value = g_value_info_get_value (info);
497 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
499 g_fprintf (file, " <member name=\"%s\" value=\"%ld\"", name, value);
502 g_fprintf (file, " deprecated=\"1\"");
504 g_fprintf (file, " />\n");
508 write_constant_value (const gchar *namespace,
513 switch (g_type_info_get_tag (type))
515 case GI_TYPE_TAG_BOOLEAN:
516 g_fprintf (file, "%d", value->v_boolean);
518 case GI_TYPE_TAG_INT8:
519 g_fprintf (file, "%d", value->v_int8);
521 case GI_TYPE_TAG_UINT8:
522 g_fprintf (file, "%d", value->v_uint8);
524 case GI_TYPE_TAG_INT16:
525 g_fprintf (file, "%" G_GINT16_FORMAT, value->v_int16);
527 case GI_TYPE_TAG_UINT16:
528 g_fprintf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
530 case GI_TYPE_TAG_INT32:
531 g_fprintf (file, "%" G_GINT32_FORMAT, value->v_int32);
533 case GI_TYPE_TAG_UINT32:
534 g_fprintf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
536 case GI_TYPE_TAG_INT64:
537 g_fprintf (file, "%" G_GINT64_FORMAT, value->v_int64);
539 case GI_TYPE_TAG_UINT64:
540 g_fprintf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
542 case GI_TYPE_TAG_INT:
543 g_fprintf (file, "%d", value->v_int);
545 case GI_TYPE_TAG_UINT:
546 g_fprintf (file, "%d", value->v_uint);
548 case GI_TYPE_TAG_LONG:
549 g_fprintf (file, "%ld", value->v_long);
551 case GI_TYPE_TAG_ULONG:
552 g_fprintf (file, "%ld", value->v_ulong);
554 case GI_TYPE_TAG_SSIZE:
555 g_fprintf (file, "%zd", value->v_ssize);
557 case GI_TYPE_TAG_SIZE:
558 g_fprintf (file, "%zd", value->v_size);
560 case GI_TYPE_TAG_FLOAT:
561 g_fprintf (file, "%f", value->v_float);
563 case GI_TYPE_TAG_DOUBLE:
564 g_fprintf (file, "%f", value->v_double);
566 case GI_TYPE_TAG_UTF8:
567 case GI_TYPE_TAG_FILENAME:
568 g_fprintf (file, "%s", value->v_string);
571 g_assert_not_reached ();
576 write_constant_info (const gchar *namespace,
577 GIConstantInfo *info,
586 name = g_base_info_get_name ((GIBaseInfo *)info);
587 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
589 g_fprintf (file, "%*s<constant name=\"%s\"", indent, "", name);
591 type = g_constant_info_get_type (info);
592 g_fprintf (file, " value=\"");
594 g_constant_info_get_value (info, &value);
595 write_constant_value (namespace, type, &value, file);
596 g_fprintf (file, "\">\n");
598 g_fprintf (file, "%*s<type name=\"", indent + 2, "");
600 write_type_info (namespace, type, file);
602 g_fprintf (file, "\"/>\n");
604 g_fprintf (file, "%*s</constant>\n", indent, "");
606 g_base_info_unref ((GIBaseInfo *)type);
611 write_enum_info (const gchar *namespace,
616 const gchar *type_name;
617 const gchar *type_init;
621 name = g_base_info_get_name ((GIBaseInfo *)info);
622 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
624 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
625 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
627 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
628 g_fprintf (file, " <enumeration ");
630 g_fprintf (file, " <bitfield ");
631 g_fprintf (file, "name=\"%s\"", name);
634 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
637 g_fprintf (file, " deprecated=\"1\"");
639 g_fprintf (file, ">\n");
641 for (i = 0; i < g_enum_info_get_n_values (info); i++)
643 GIValueInfo *value = g_enum_info_get_value (info, i);
644 write_value_info (namespace, value, file);
645 g_base_info_unref ((GIBaseInfo *)value);
648 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
649 g_fprintf (file, " </enumeration>\n");
651 g_fprintf (file, " </bitfield>\n");
655 write_signal_info (const gchar *namespace,
663 name = g_base_info_get_name ((GIBaseInfo *)info);
664 flags = g_signal_info_get_flags (info);
665 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
667 g_fprintf (file, " <glib:signal name=\"%s\"", name);
670 g_fprintf (file, " deprecated=\"1\"");
672 if (flags & G_SIGNAL_RUN_FIRST)
673 g_fprintf (file, " when=\"FIRST\"");
674 else if (flags & G_SIGNAL_RUN_LAST)
675 g_fprintf (file, " when=\"LAST\"");
676 else if (flags & G_SIGNAL_RUN_CLEANUP)
677 g_fprintf (file, " when=\"CLEANUP\"");
679 if (flags & G_SIGNAL_NO_RECURSE)
680 g_fprintf (file, " no-recurse=\"1\"");
682 if (flags & G_SIGNAL_DETAILED)
683 g_fprintf (file, " detailed=\"1\"");
685 if (flags & G_SIGNAL_ACTION)
686 g_fprintf (file, " action=\"1\"");
688 if (flags & G_SIGNAL_NO_HOOKS)
689 g_fprintf (file, " no-hooks=\"1\"");
691 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
693 g_fprintf (file, " </glib:signal>\n");
697 write_vfunc_info (const gchar *namespace,
701 GIVFuncInfoFlags flags;
706 name = g_base_info_get_name ((GIBaseInfo *)info);
707 flags = g_vfunc_info_get_flags (info);
708 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
709 offset = g_vfunc_info_get_offset (info);
711 g_fprintf (file, " <vfunc name=\"%s\"", name);
714 g_fprintf (file, " deprecated=\"1\"");
716 if (flags & GI_VFUNC_MUST_CHAIN_UP)
717 g_fprintf (file, " must-chain-up=\"1\"");
719 if (flags & GI_VFUNC_MUST_OVERRIDE)
720 g_fprintf (file, " override=\"always\"");
721 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
722 g_fprintf (file, " override=\"never\"");
724 g_fprintf (file, " offset=\"%d\"", offset);
726 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
728 g_fprintf (file, " </vfunc>\n");
732 write_property_info (const gchar *namespace,
733 GIPropertyInfo *info,
741 name = g_base_info_get_name ((GIBaseInfo *)info);
742 flags = g_property_info_get_flags (info);
743 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
745 g_fprintf (file, " <property name=\"%s\"", name);
748 g_fprintf (file, " deprecated=\"1\"");
750 if (flags & G_PARAM_READABLE)
751 g_fprintf (file, " readable=\"1\"");
753 g_fprintf (file, " readable=\"0\"");
755 if (flags & G_PARAM_WRITABLE)
756 g_fprintf (file, " writable=\"1\"");
758 g_fprintf (file, " writable=\"0\"");
760 if (flags & G_PARAM_CONSTRUCT)
761 g_fprintf (file, " construct=\"1\"");
763 if (flags & G_PARAM_CONSTRUCT_ONLY)
764 g_fprintf (file, " construct-only=\"1\"");
766 type = g_property_info_get_type (info);
768 g_fprintf (file, ">\n");
770 g_fprintf (file, " <type name=\"", name);
772 write_type_info (namespace, type, file);
774 g_fprintf (file, "\"/>\n");
776 g_fprintf (file, " </property>\n");
781 write_object_info (const gchar *namespace,
786 const gchar *type_name;
787 const gchar *type_init;
792 name = g_base_info_get_name ((GIBaseInfo *)info);
793 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
795 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
796 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
797 g_fprintf (file, " <class name=\"%s\"", name);
799 pnode = g_object_info_get_parent (info);
802 g_fprintf (file, " parent=\"");
803 write_type_name (namespace, (GIBaseInfo *)pnode, file);
804 g_fprintf (file, "\"" );
805 g_base_info_unref ((GIBaseInfo *)pnode);
808 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
811 g_fprintf (file, " deprecated=\"1\"");
813 g_fprintf (file, ">\n");
815 if (g_object_info_get_n_interfaces (info) > 0)
817 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
819 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
820 g_fprintf (file, " <implements name=\"");
821 write_type_name (namespace, (GIBaseInfo*)imp, file);
822 g_fprintf (file,"\" />\n");
823 g_base_info_unref ((GIBaseInfo*)imp);
827 for (i = 0; i < g_object_info_get_n_fields (info); i++)
829 GIFieldInfo *field = g_object_info_get_field (info, i);
830 write_field_info (namespace, field, NULL, file);
831 g_base_info_unref ((GIBaseInfo *)field);
834 for (i = 0; i < g_object_info_get_n_methods (info); i++)
836 GIFunctionInfo *function = g_object_info_get_method (info, i);
837 write_function_info (namespace, function, file, 6);
838 g_base_info_unref ((GIBaseInfo *)function);
841 for (i = 0; i < g_object_info_get_n_properties (info); i++)
843 GIPropertyInfo *prop = g_object_info_get_property (info, i);
844 write_property_info (namespace, prop, file);
845 g_base_info_unref ((GIBaseInfo *)prop);
848 for (i = 0; i < g_object_info_get_n_signals (info); i++)
850 GISignalInfo *signal = g_object_info_get_signal (info, i);
851 write_signal_info (namespace, signal, file);
852 g_base_info_unref ((GIBaseInfo *)signal);
855 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
857 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
858 write_vfunc_info (namespace, vfunc, file);
859 g_base_info_unref ((GIBaseInfo *)vfunc);
862 for (i = 0; i < g_object_info_get_n_constants (info); i++)
864 GIConstantInfo *constant = g_object_info_get_constant (info, i);
865 write_constant_info (namespace, constant, file, 6);
866 g_base_info_unref ((GIBaseInfo *)constant);
869 g_fprintf (file, " </class>\n");
873 write_interface_info (const gchar *namespace,
874 GIInterfaceInfo *info,
878 const gchar *type_name;
879 const gchar *type_init;
883 name = g_base_info_get_name ((GIBaseInfo *)info);
884 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
886 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
887 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
888 g_fprintf (file, " <interface name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
889 name, type_name, type_init);
892 g_fprintf (file, " deprecated=\"1\"");
894 g_fprintf (file, ">\n");
896 if (g_interface_info_get_n_prerequisites (info) > 0)
898 g_fprintf (file, " <requires>\n");
899 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
901 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
903 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
904 g_fprintf (file, " <interface name=\"");
906 g_fprintf (file, " <object name=\"");
907 write_type_name (namespace, req, file);
908 g_fprintf (file, "\" />\n");
909 g_base_info_unref (req);
911 g_fprintf (file, " </requires>\n");
914 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
916 GIFunctionInfo *function = g_interface_info_get_method (info, i);
917 write_function_info (namespace, function, file, 6);
918 g_base_info_unref ((GIBaseInfo *)function);
921 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
923 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
924 write_property_info (namespace, prop, file);
925 g_base_info_unref ((GIBaseInfo *)prop);
928 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
930 GISignalInfo *signal = g_interface_info_get_signal (info, i);
931 write_signal_info (namespace, signal, file);
932 g_base_info_unref ((GIBaseInfo *)signal);
935 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
937 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
938 write_vfunc_info (namespace, vfunc, file);
939 g_base_info_unref ((GIBaseInfo *)vfunc);
942 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
944 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
945 write_constant_info (namespace, constant, file, 6);
946 g_base_info_unref ((GIBaseInfo *)constant);
949 g_fprintf (file, " </interface>\n");
953 write_error_domain_info (const gchar *namespace,
954 GIErrorDomainInfo *info,
958 const gchar *name, *quark;
960 name = g_base_info_get_name ((GIBaseInfo *)info);
961 quark = g_error_domain_info_get_quark (info);
962 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
964 " <errordomain name=\"%s\" get-quark=\"%s\" codes=\"",
966 write_type_name (namespace, enum_, file);
967 g_fprintf (file, "\" />\n");
968 g_base_info_unref (enum_);
972 write_union_info (const gchar *namespace,
977 const gchar *type_name;
978 const gchar *type_init;
982 name = g_base_info_get_name ((GIBaseInfo *)info);
983 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
985 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
986 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
988 g_fprintf (file, " <union name=\"%s\"", name);
991 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
994 g_fprintf (file, " deprecated=\"1\"");
996 g_fprintf (file, ">\n");
998 if (g_union_info_is_discriminated (info))
1003 offset = g_union_info_get_discriminator_offset (info);
1004 type = g_union_info_get_discriminator_type (info);
1006 g_fprintf (file, " <discriminator offset=\"%d\" type=\"", offset);
1007 write_type_info (namespace, type, file);
1008 g_fprintf (file, "\" />\n");
1009 g_base_info_unref ((GIBaseInfo *)type);
1012 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1014 GIFieldInfo *field = g_union_info_get_field (info, i);
1015 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1016 write_field_info (namespace, field, constant, file);
1017 g_base_info_unref ((GIBaseInfo *)field);
1019 g_base_info_unref ((GIBaseInfo *)constant);
1022 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1024 GIFunctionInfo *function = g_union_info_get_method (info, i);
1025 write_function_info (namespace, function, file, 6);
1026 g_base_info_unref ((GIBaseInfo *)function);
1029 g_fprintf (file, " </union>\n");
1033 write_repository (const char *namespace,
1034 gboolean needs_prefix)
1039 char **dependencies;
1040 GIRepository *repository;
1042 repository = g_irepository_get_default ();
1051 filename = g_strdup_printf ("%s-%s", namespace, output);
1053 filename = g_strdup (output);
1054 file = g_fopen (filename, "w");
1058 g_fprintf (stderr, "failed to open '%s': %s\n",
1059 filename, g_strerror (errno));
1068 g_fprintf (file, "<?xml version=\"1.0\"?>\n");
1069 g_fprintf (file, "<repository version=\"1.0\"\n"
1070 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1071 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1072 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\">\n");
1074 dependencies = g_irepository_get_dependencies (repository,
1076 if (dependencies != NULL)
1078 for (i = 0; dependencies[i]; i++)
1080 g_fprintf (file, " <include name=\"%s\"/>\n", dependencies[i]);
1086 const gchar *shared_library;
1087 const char *ns = namespace;
1089 shared_library = g_irepository_get_shared_library (repository, ns);
1091 g_fprintf (file, " <namespace name=\"%s\" shared-library=\"%s\">\n",
1092 ns, shared_library);
1094 g_fprintf (file, " <namespace name=\"%s\">\n", ns);
1096 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1098 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1099 switch (g_base_info_get_type (info))
1101 case GI_INFO_TYPE_FUNCTION:
1102 write_function_info (ns, (GIFunctionInfo *)info, file, 4);
1105 case GI_INFO_TYPE_CALLBACK:
1106 write_callback_info (ns, (GICallbackInfo *)info, file, 4);
1109 case GI_INFO_TYPE_STRUCT:
1110 case GI_INFO_TYPE_BOXED:
1111 write_struct_info (ns, (GIStructInfo *)info, file);
1114 case GI_INFO_TYPE_UNION:
1115 write_union_info (ns, (GIUnionInfo *)info, file);
1118 case GI_INFO_TYPE_ENUM:
1119 case GI_INFO_TYPE_FLAGS:
1120 write_enum_info (ns, (GIEnumInfo *)info, file);
1123 case GI_INFO_TYPE_CONSTANT:
1124 write_constant_info (ns, (GIConstantInfo *)info, file, 4);
1127 case GI_INFO_TYPE_OBJECT:
1128 write_object_info (ns, (GIObjectInfo *)info, file);
1131 case GI_INFO_TYPE_INTERFACE:
1132 write_interface_info (ns, (GIInterfaceInfo *)info, file);
1135 case GI_INFO_TYPE_ERROR_DOMAIN:
1136 write_error_domain_info (ns, (GIErrorDomainInfo *)info, file);
1140 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1143 g_base_info_unref (info);
1146 g_fprintf (file, " </namespace>\n");
1149 g_fprintf (file, "</repository>\n");
1155 static const guchar *
1156 load_typelib (const gchar *filename,
1161 gsize *typelib_size;
1164 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1167 g_printerr ("Could not load typelib from '%s': %s\n",
1168 filename, g_module_error ());
1172 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1174 g_printerr ("Could not load typelib from '%s': %s\n",
1175 filename, g_module_error ());
1179 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1181 g_printerr ("Could not load typelib from '%s': %s\n",
1182 filename, g_module_error ());
1186 *len = *typelib_size;
1195 main (int argc, char *argv[])
1197 gboolean shlib = FALSE;
1198 gchar **input = NULL;
1199 GOptionContext *context;
1200 GError *error = NULL;
1201 gboolean needs_prefix;
1204 GOptionEntry options[] =
1206 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1207 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1208 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1209 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1213 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1217 g_typelib_check_sanity ();
1219 context = g_option_context_new ("");
1220 g_option_context_add_main_entries (context, options, NULL);
1221 g_option_context_parse (context, &argc, &argv, &error);
1225 g_fprintf (stderr, "no input files\n");
1230 if (includedirs != NULL)
1231 for (i = 0; includedirs[i]; i++)
1232 g_irepository_prepend_search_path (includedirs[i]);
1234 for (i = 0; input[i]; i++)
1236 GModule *dlhandle = NULL;
1237 const guchar *typelib;
1239 const char *namespace;
1243 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1245 g_fprintf (stderr, "failed to read '%s': %s\n",
1246 input[i], error->message);
1247 g_clear_error (&error);
1253 typelib = load_typelib (input[i], &dlhandle, &len);
1256 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1262 if (input[i + 1] && output)
1263 needs_prefix = TRUE;
1265 needs_prefix = FALSE;
1267 data = g_typelib_new_from_const_memory (typelib, len);
1269 GError *error = NULL;
1270 if (!g_typelib_validate (data, &error)) {
1271 g_printerr ("typelib not valid: %s\n", error->message);
1272 g_clear_error (&error);
1276 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1278 if (namespace == NULL)
1280 g_printerr ("failed to load typelib: %s\n", error->message);
1284 write_repository (namespace, needs_prefix);
1288 g_module_close (dlhandle);
1292 /* when writing to stdout, stop after the first module */
1293 if (input[i + 1] && !output)
1295 g_fprintf (stderr, "warning, %d modules omitted\n",
1296 g_strv_length (input) - 1);