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[] = {
90 check_unresolved ((GIBaseInfo*)info);
92 tag = g_type_info_get_tag (info);
93 is_pointer = g_type_info_is_pointer (info);
95 if (tag == GI_TYPE_TAG_VOID)
98 g_fprintf (file, "%s", "any");
100 g_fprintf (file, "%s", "none");
103 g_fprintf (file, "%s", basic[tag]);
108 type = g_type_info_get_param_type (info, 0);
109 write_type_info (namespace, type, file);
110 g_fprintf (file, "[");
112 length = g_type_info_get_array_length (info);
115 g_fprintf (file, "length=%d", length);
117 if (g_type_info_is_zero_terminated (info))
118 g_fprintf (file, "%szero-terminated=1", length >= 0 ? "," : "");
120 g_fprintf (file, "]");
121 g_base_info_unref ((GIBaseInfo *)type);
125 GIBaseInfo *iface = g_type_info_get_interface (info);
126 write_type_name (namespace, iface, file);
127 g_base_info_unref (iface);
131 type = g_type_info_get_param_type (info, 0);
132 g_fprintf (file, "GLib.List");
135 g_fprintf (file, "<");
136 write_type_info (namespace, type, file);
137 g_fprintf (file, ">");
138 g_base_info_unref ((GIBaseInfo *)type);
143 type = g_type_info_get_param_type (info, 0);
144 g_fprintf (file, "GLib.SList");
147 g_fprintf (file, "<");
148 write_type_info (namespace, type, file);
149 g_fprintf (file, ">");
150 g_base_info_unref ((GIBaseInfo *)type);
155 type = g_type_info_get_param_type (info, 0);
156 g_fprintf (file, "GLib.HashTable");
159 g_fprintf (file, "<");
160 write_type_info (namespace, type, file);
161 g_base_info_unref ((GIBaseInfo *)type);
162 type = g_type_info_get_param_type (info, 1);
163 g_fprintf (file, ",");
164 write_type_info (namespace, type, file);
165 g_fprintf (file, ">");
166 g_base_info_unref ((GIBaseInfo *)type);
173 g_fprintf (file, "GLib.Error");
174 n = g_type_info_get_n_error_domains (info);
177 g_fprintf (file, "<");
178 for (i = 0; i < n; i++)
180 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
182 g_fprintf (file, ",");
183 write_type_name (namespace, (GIBaseInfo *)ed, file);
184 g_base_info_unref ((GIBaseInfo *)ed);
186 g_fprintf (file, ">");
192 write_constant_value (const gchar *namespace,
198 write_field_info (const gchar *namespace,
200 GIConstantInfo *branch,
204 GIFieldInfoFlags flags;
210 name = g_base_info_get_name ((GIBaseInfo *)info);
211 flags = g_field_info_get_flags (info);
212 size = g_field_info_get_size (info);
213 offset = g_field_info_get_offset (info);
216 " <field name=\"%s\" readable=\"%s\" writable=\"%s\"",
218 flags & GI_FIELD_IS_READABLE ? "1" : "0",
219 flags & GI_FIELD_IS_WRITABLE ? "1" : "0");
221 g_fprintf (file, " bits=\"%d\"", size);
223 g_fprintf (file, " offset=\"%d\"", offset);
225 type = g_field_info_get_type (info);
229 g_fprintf (file, " branch=\"");
230 type = g_constant_info_get_type (branch);
231 g_constant_info_get_value (branch, &value);
232 write_constant_value (namespace, type, &value, file);
233 g_fprintf (file, "\"");
236 g_fprintf (file,">\n");
238 g_fprintf (file, " <type name=\"");
240 write_type_info (namespace, type, file);
241 g_base_info_unref ((GIBaseInfo *)type);
243 g_fprintf (file, "\"/>\n");
245 g_fprintf (file, " </field>\n");
250 write_callable_info (const gchar *namespace,
251 GICallableInfo *info,
258 type = g_callable_info_get_return_type (info);
260 if (g_type_info_is_pointer (type))
262 switch (g_callable_info_get_caller_owns (info))
264 case GI_TRANSFER_NOTHING:
266 case GI_TRANSFER_CONTAINER:
267 g_fprintf (file, " transfer=\"shallow\"");
269 case GI_TRANSFER_EVERYTHING:
270 g_fprintf (file, " transfer=\"full\"");
273 g_assert_not_reached ();
277 g_fprintf (file, ">\n");
279 g_fprintf (file, "%*s <return-value>\n", indent, "");
281 if (g_callable_info_may_return_null (info))
282 g_fprintf (file, " null-ok=\"1\"");
284 g_fprintf (file, "%*s <type name=\"", indent + 2, "");
286 write_type_info (namespace, type, file);
288 g_fprintf (file, "\"/>\n");
290 g_fprintf (file, "%*s </return-value>\n", indent, "");
292 if (g_callable_info_get_n_args (info) <= 0)
295 g_fprintf (file, "%*s <parameters>\n", indent, "");
296 for (i = 0; i < g_callable_info_get_n_args (info); i++)
298 GIArgInfo *arg = g_callable_info_get_arg (info, i);
300 g_fprintf (file, "%*s <parameter name=\"%s\"",
301 indent, "", g_base_info_get_name ((GIBaseInfo *) arg));
303 switch (g_arg_info_get_ownership_transfer (arg))
305 case GI_TRANSFER_NOTHING:
307 case GI_TRANSFER_CONTAINER:
308 g_fprintf (file, " transfer=\"shallow\"");
310 case GI_TRANSFER_EVERYTHING:
311 g_fprintf (file, " transfer=\"full\"");
314 g_assert_not_reached ();
317 g_fprintf (file, " direction=\"");
318 switch (g_arg_info_get_direction (arg))
320 case GI_DIRECTION_IN:
321 g_fprintf (file, "in");
323 case GI_DIRECTION_OUT:
324 g_fprintf (file, "out");
326 case GI_DIRECTION_INOUT:
327 g_fprintf (file, "inout");
330 g_fprintf (file, "\"");
332 if (g_arg_info_may_be_null (arg))
333 g_fprintf (file, " null-ok=\"1\"");
335 if (g_arg_info_is_dipper (arg))
336 g_fprintf (file, " dipper=\"1\"");
338 if (g_arg_info_is_return_value (arg))
339 g_fprintf (file, " retval=\"1\"");
341 if (g_arg_info_is_optional (arg))
342 g_fprintf (file, " optional=\"1\"");
344 g_fprintf (file, ">\n");
346 g_fprintf (file, "%*s <type name=\"", indent+2, "");
348 type = g_arg_info_get_type (arg);
349 write_type_info (namespace, type, file);
351 g_fprintf (file, "\"/>\n");
353 g_fprintf (file, "%*s </parameter>\n", indent, "");
355 g_base_info_unref ((GIBaseInfo *)arg);
358 g_fprintf (file, "%*s </parameters>\n", indent, "");
359 g_base_info_unref ((GIBaseInfo *)type);
363 write_function_info (const gchar *namespace,
364 GIFunctionInfo *info,
368 GIFunctionInfoFlags flags;
374 flags = g_function_info_get_flags (info);
375 name = g_base_info_get_name ((GIBaseInfo *)info);
376 symbol = g_function_info_get_symbol (info);
377 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
379 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
381 else if (flags & GI_FUNCTION_IS_METHOD)
386 g_fprintf (file, "%*s<%s name=\"%s\" c:identifier=\"%s\"",
387 indent, "", tag, name, symbol);
389 if (flags & GI_FUNCTION_IS_SETTER)
390 g_fprintf (file, " type=\"setter\"");
391 else if (flags & GI_FUNCTION_IS_GETTER)
392 g_fprintf (file, " type=\"getter\"");
395 g_fprintf (file, " deprecated=\"1\"");
397 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
398 g_fprintf (file, "%*s</%s>\n", indent, "", tag);
402 write_callback_info (const gchar *namespace,
403 GICallbackInfo *info,
410 name = g_base_info_get_name ((GIBaseInfo *)info);
411 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
413 g_fprintf (file, "%*s<callback name=\"%s\"", indent, "", name);
416 g_fprintf (file, " deprecated=\"1\"");
418 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
419 g_fprintf (file, "%*s</callback>\n", indent, "");
423 write_struct_info (const gchar *namespace,
428 const gchar *type_name;
429 const gchar *type_init;
434 name = g_base_info_get_name ((GIBaseInfo *)info);
435 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
437 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
438 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
440 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
442 g_fprintf (file, " <glib:boxed glib:name=\"%s\"", name);
445 g_fprintf (file, " <record name=\"%s\"", name);
447 if (type_name != NULL)
448 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
451 g_fprintf (file, " deprecated=\"1\"");
453 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
456 g_fprintf (file, ">\n");
458 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
460 GIFieldInfo *field = g_struct_info_get_field (info, i);
461 write_field_info (namespace, field, NULL, file);
462 g_base_info_unref ((GIBaseInfo *)field);
465 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
467 GIFunctionInfo *function = g_struct_info_get_method (info, i);
468 write_function_info (namespace, function, file, 6);
469 g_base_info_unref ((GIBaseInfo *)function);
472 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
473 g_fprintf (file, " </glib:boxed>\n");
475 g_fprintf (file, " </record>\n");
479 g_fprintf (file, "/>\n");
484 write_value_info (const gchar *namespace,
492 name = g_base_info_get_name ((GIBaseInfo *)info);
493 value = g_value_info_get_value (info);
494 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
496 g_fprintf (file, " <member name=\"%s\" value=\"%ld\"", name, value);
499 g_fprintf (file, " deprecated=\"1\"");
501 g_fprintf (file, " />\n");
505 write_constant_value (const gchar *namespace,
510 switch (g_type_info_get_tag (type))
512 case GI_TYPE_TAG_BOOLEAN:
513 g_fprintf (file, "%d", value->v_boolean);
515 case GI_TYPE_TAG_INT8:
516 g_fprintf (file, "%d", value->v_int8);
518 case GI_TYPE_TAG_UINT8:
519 g_fprintf (file, "%d", value->v_uint8);
521 case GI_TYPE_TAG_INT16:
522 g_fprintf (file, "%" G_GINT16_FORMAT, value->v_int16);
524 case GI_TYPE_TAG_UINT16:
525 g_fprintf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
527 case GI_TYPE_TAG_INT32:
528 g_fprintf (file, "%" G_GINT32_FORMAT, value->v_int32);
530 case GI_TYPE_TAG_UINT32:
531 g_fprintf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
533 case GI_TYPE_TAG_INT64:
534 g_fprintf (file, "%" G_GINT64_FORMAT, value->v_int64);
536 case GI_TYPE_TAG_UINT64:
537 g_fprintf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
539 case GI_TYPE_TAG_INT:
540 g_fprintf (file, "%d", value->v_int);
542 case GI_TYPE_TAG_UINT:
543 g_fprintf (file, "%d", value->v_uint);
545 case GI_TYPE_TAG_LONG:
546 g_fprintf (file, "%ld", value->v_long);
548 case GI_TYPE_TAG_ULONG:
549 g_fprintf (file, "%ld", value->v_ulong);
551 case GI_TYPE_TAG_SSIZE:
552 g_fprintf (file, "%zd", value->v_ssize);
554 case GI_TYPE_TAG_SIZE:
555 g_fprintf (file, "%zd", value->v_size);
557 case GI_TYPE_TAG_FLOAT:
558 g_fprintf (file, "%f", value->v_float);
560 case GI_TYPE_TAG_DOUBLE:
561 g_fprintf (file, "%f", value->v_double);
563 case GI_TYPE_TAG_UTF8:
564 case GI_TYPE_TAG_FILENAME:
565 g_fprintf (file, "%s", value->v_string);
568 g_assert_not_reached ();
573 write_constant_info (const gchar *namespace,
574 GIConstantInfo *info,
583 name = g_base_info_get_name ((GIBaseInfo *)info);
584 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
586 g_fprintf (file, "%*s<constant name=\"%s\"", indent, "", name);
588 type = g_constant_info_get_type (info);
589 g_fprintf (file, " value=\"");
591 g_constant_info_get_value (info, &value);
592 write_constant_value (namespace, type, &value, file);
593 g_fprintf (file, "\">\n");
595 g_fprintf (file, "%*s<type name=\"", indent + 2, "");
597 write_type_info (namespace, type, file);
599 g_fprintf (file, "\"/>\n");
601 g_fprintf (file, "%*s</constant>\n", indent, "");
603 g_base_info_unref ((GIBaseInfo *)type);
608 write_enum_info (const gchar *namespace,
613 const gchar *type_name;
614 const gchar *type_init;
618 name = g_base_info_get_name ((GIBaseInfo *)info);
619 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
621 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
622 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
624 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
625 g_fprintf (file, " <enumeration ");
627 g_fprintf (file, " <bitfield ");
628 g_fprintf (file, "name=\"%s\"", name);
631 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
634 g_fprintf (file, " deprecated=\"1\"");
636 g_fprintf (file, ">\n");
638 for (i = 0; i < g_enum_info_get_n_values (info); i++)
640 GIValueInfo *value = g_enum_info_get_value (info, i);
641 write_value_info (namespace, value, file);
642 g_base_info_unref ((GIBaseInfo *)value);
645 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
646 g_fprintf (file, " </enumeration>\n");
648 g_fprintf (file, " </bitfield>\n");
652 write_signal_info (const gchar *namespace,
660 name = g_base_info_get_name ((GIBaseInfo *)info);
661 flags = g_signal_info_get_flags (info);
662 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
664 g_fprintf (file, " <glib:signal name=\"%s\"", name);
667 g_fprintf (file, " deprecated=\"1\"");
669 if (flags & G_SIGNAL_RUN_FIRST)
670 g_fprintf (file, " when=\"FIRST\"");
671 else if (flags & G_SIGNAL_RUN_LAST)
672 g_fprintf (file, " when=\"LAST\"");
673 else if (flags & G_SIGNAL_RUN_CLEANUP)
674 g_fprintf (file, " when=\"CLEANUP\"");
676 if (flags & G_SIGNAL_NO_RECURSE)
677 g_fprintf (file, " no-recurse=\"1\"");
679 if (flags & G_SIGNAL_DETAILED)
680 g_fprintf (file, " detailed=\"1\"");
682 if (flags & G_SIGNAL_ACTION)
683 g_fprintf (file, " action=\"1\"");
685 if (flags & G_SIGNAL_NO_HOOKS)
686 g_fprintf (file, " no-hooks=\"1\"");
688 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
690 g_fprintf (file, " </glib:signal>\n");
694 write_vfunc_info (const gchar *namespace,
698 GIVFuncInfoFlags flags;
703 name = g_base_info_get_name ((GIBaseInfo *)info);
704 flags = g_vfunc_info_get_flags (info);
705 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
706 offset = g_vfunc_info_get_offset (info);
708 g_fprintf (file, " <vfunc name=\"%s\"", name);
711 g_fprintf (file, " deprecated=\"1\"");
713 if (flags & GI_VFUNC_MUST_CHAIN_UP)
714 g_fprintf (file, " must-chain-up=\"1\"");
716 if (flags & GI_VFUNC_MUST_OVERRIDE)
717 g_fprintf (file, " override=\"always\"");
718 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
719 g_fprintf (file, " override=\"never\"");
721 g_fprintf (file, " offset=\"%d\"", offset);
723 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
725 g_fprintf (file, " </vfunc>\n");
729 write_property_info (const gchar *namespace,
730 GIPropertyInfo *info,
738 name = g_base_info_get_name ((GIBaseInfo *)info);
739 flags = g_property_info_get_flags (info);
740 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
742 g_fprintf (file, " <property name=\"%s\"", name);
745 g_fprintf (file, " deprecated=\"1\"");
747 if (flags & G_PARAM_READABLE)
748 g_fprintf (file, " readable=\"1\"");
750 g_fprintf (file, " readable=\"0\"");
752 if (flags & G_PARAM_WRITABLE)
753 g_fprintf (file, " writable=\"1\"");
755 g_fprintf (file, " writable=\"0\"");
757 if (flags & G_PARAM_CONSTRUCT)
758 g_fprintf (file, " construct=\"1\"");
760 if (flags & G_PARAM_CONSTRUCT_ONLY)
761 g_fprintf (file, " construct-only=\"1\"");
763 type = g_property_info_get_type (info);
765 g_fprintf (file, ">\n");
767 g_fprintf (file, " <type name=\"", name);
769 write_type_info (namespace, type, file);
771 g_fprintf (file, "\"/>\n");
773 g_fprintf (file, " </property>\n");
778 write_object_info (const gchar *namespace,
783 const gchar *type_name;
784 const gchar *type_init;
789 name = g_base_info_get_name ((GIBaseInfo *)info);
790 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
792 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
793 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
794 g_fprintf (file, " <class name=\"%s\"", name);
796 pnode = g_object_info_get_parent (info);
799 g_fprintf (file, " parent=\"");
800 write_type_name (namespace, (GIBaseInfo *)pnode, file);
801 g_fprintf (file, "\"" );
802 g_base_info_unref ((GIBaseInfo *)pnode);
805 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
808 g_fprintf (file, " deprecated=\"1\"");
810 g_fprintf (file, ">\n");
812 if (g_object_info_get_n_interfaces (info) > 0)
814 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
816 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
817 g_fprintf (file, " <implements name=\"");
818 write_type_name (namespace, (GIBaseInfo*)imp, file);
819 g_fprintf (file,"\" />\n");
820 g_base_info_unref ((GIBaseInfo*)imp);
824 for (i = 0; i < g_object_info_get_n_fields (info); i++)
826 GIFieldInfo *field = g_object_info_get_field (info, i);
827 write_field_info (namespace, field, NULL, file);
828 g_base_info_unref ((GIBaseInfo *)field);
831 for (i = 0; i < g_object_info_get_n_methods (info); i++)
833 GIFunctionInfo *function = g_object_info_get_method (info, i);
834 write_function_info (namespace, function, file, 6);
835 g_base_info_unref ((GIBaseInfo *)function);
838 for (i = 0; i < g_object_info_get_n_properties (info); i++)
840 GIPropertyInfo *prop = g_object_info_get_property (info, i);
841 write_property_info (namespace, prop, file);
842 g_base_info_unref ((GIBaseInfo *)prop);
845 for (i = 0; i < g_object_info_get_n_signals (info); i++)
847 GISignalInfo *signal = g_object_info_get_signal (info, i);
848 write_signal_info (namespace, signal, file);
849 g_base_info_unref ((GIBaseInfo *)signal);
852 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
854 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
855 write_vfunc_info (namespace, vfunc, file);
856 g_base_info_unref ((GIBaseInfo *)vfunc);
859 for (i = 0; i < g_object_info_get_n_constants (info); i++)
861 GIConstantInfo *constant = g_object_info_get_constant (info, i);
862 write_constant_info (namespace, constant, file, 6);
863 g_base_info_unref ((GIBaseInfo *)constant);
866 g_fprintf (file, " </class>\n");
870 write_interface_info (const gchar *namespace,
871 GIInterfaceInfo *info,
875 const gchar *type_name;
876 const gchar *type_init;
880 name = g_base_info_get_name ((GIBaseInfo *)info);
881 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
883 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
884 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
885 g_fprintf (file, " <interface name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
886 name, type_name, type_init);
889 g_fprintf (file, " deprecated=\"1\"");
891 g_fprintf (file, ">\n");
893 if (g_interface_info_get_n_prerequisites (info) > 0)
895 g_fprintf (file, " <requires>\n");
896 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
898 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
900 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
901 g_fprintf (file, " <interface name=\"");
903 g_fprintf (file, " <object name=\"");
904 write_type_name (namespace, req, file);
905 g_fprintf (file, "\" />\n");
906 g_base_info_unref (req);
908 g_fprintf (file, " </requires>\n");
911 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
913 GIFunctionInfo *function = g_interface_info_get_method (info, i);
914 write_function_info (namespace, function, file, 6);
915 g_base_info_unref ((GIBaseInfo *)function);
918 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
920 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
921 write_property_info (namespace, prop, file);
922 g_base_info_unref ((GIBaseInfo *)prop);
925 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
927 GISignalInfo *signal = g_interface_info_get_signal (info, i);
928 write_signal_info (namespace, signal, file);
929 g_base_info_unref ((GIBaseInfo *)signal);
932 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
934 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
935 write_vfunc_info (namespace, vfunc, file);
936 g_base_info_unref ((GIBaseInfo *)vfunc);
939 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
941 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
942 write_constant_info (namespace, constant, file, 6);
943 g_base_info_unref ((GIBaseInfo *)constant);
946 g_fprintf (file, " </interface>\n");
950 write_error_domain_info (const gchar *namespace,
951 GIErrorDomainInfo *info,
955 const gchar *name, *quark;
957 name = g_base_info_get_name ((GIBaseInfo *)info);
958 quark = g_error_domain_info_get_quark (info);
959 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
961 " <errordomain name=\"%s\" get-quark=\"%s\" codes=\"",
963 write_type_name (namespace, enum_, file);
964 g_fprintf (file, "\" />\n");
965 g_base_info_unref (enum_);
969 write_union_info (const gchar *namespace,
974 const gchar *type_name;
975 const gchar *type_init;
979 name = g_base_info_get_name ((GIBaseInfo *)info);
980 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
982 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
983 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
985 g_fprintf (file, " <union name=\"%s\"", name);
988 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
991 g_fprintf (file, " deprecated=\"1\"");
993 g_fprintf (file, ">\n");
995 if (g_union_info_is_discriminated (info))
1000 offset = g_union_info_get_discriminator_offset (info);
1001 type = g_union_info_get_discriminator_type (info);
1003 g_fprintf (file, " <discriminator offset=\"%d\" type=\"", offset);
1004 write_type_info (namespace, type, file);
1005 g_fprintf (file, "\" />\n");
1006 g_base_info_unref ((GIBaseInfo *)type);
1009 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1011 GIFieldInfo *field = g_union_info_get_field (info, i);
1012 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1013 write_field_info (namespace, field, constant, file);
1014 g_base_info_unref ((GIBaseInfo *)field);
1016 g_base_info_unref ((GIBaseInfo *)constant);
1019 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1021 GIFunctionInfo *function = g_union_info_get_method (info, i);
1022 write_function_info (namespace, function, file, 6);
1023 g_base_info_unref ((GIBaseInfo *)function);
1026 g_fprintf (file, " </union>\n");
1030 write_repository (const char *namespace,
1031 gboolean needs_prefix)
1036 char **dependencies;
1037 GIRepository *repository;
1039 repository = g_irepository_get_default ();
1048 filename = g_strdup_printf ("%s-%s", namespace, output);
1050 filename = g_strdup (output);
1051 file = g_fopen (filename, "w");
1055 g_fprintf (stderr, "failed to open '%s': %s\n",
1056 filename, g_strerror (errno));
1065 g_fprintf (file, "<?xml version=\"1.0\"?>\n");
1066 g_fprintf (file, "<repository version=\"1.0\"\n"
1067 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1068 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1069 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\">\n");
1071 dependencies = g_irepository_get_dependencies (repository,
1073 if (dependencies != NULL)
1075 for (i = 0; dependencies[i]; i++)
1077 g_fprintf (file, " <include name=\"%s\"/>\n", dependencies[i]);
1083 const gchar *shared_library;
1084 const char *ns = namespace;
1086 shared_library = g_irepository_get_shared_library (repository, ns);
1088 g_fprintf (file, " <namespace name=\"%s\" shared-library=\"%s\">\n",
1089 ns, shared_library);
1091 g_fprintf (file, " <namespace name=\"%s\">\n", ns);
1093 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1095 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1096 switch (g_base_info_get_type (info))
1098 case GI_INFO_TYPE_FUNCTION:
1099 write_function_info (ns, (GIFunctionInfo *)info, file, 4);
1102 case GI_INFO_TYPE_CALLBACK:
1103 write_callback_info (ns, (GICallbackInfo *)info, file, 4);
1106 case GI_INFO_TYPE_STRUCT:
1107 case GI_INFO_TYPE_BOXED:
1108 write_struct_info (ns, (GIStructInfo *)info, file);
1111 case GI_INFO_TYPE_UNION:
1112 write_union_info (ns, (GIUnionInfo *)info, file);
1115 case GI_INFO_TYPE_ENUM:
1116 case GI_INFO_TYPE_FLAGS:
1117 write_enum_info (ns, (GIEnumInfo *)info, file);
1120 case GI_INFO_TYPE_CONSTANT:
1121 write_constant_info (ns, (GIConstantInfo *)info, file, 4);
1124 case GI_INFO_TYPE_OBJECT:
1125 write_object_info (ns, (GIObjectInfo *)info, file);
1128 case GI_INFO_TYPE_INTERFACE:
1129 write_interface_info (ns, (GIInterfaceInfo *)info, file);
1132 case GI_INFO_TYPE_ERROR_DOMAIN:
1133 write_error_domain_info (ns, (GIErrorDomainInfo *)info, file);
1137 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1140 g_base_info_unref (info);
1143 g_fprintf (file, " </namespace>\n");
1146 g_fprintf (file, "</repository>\n");
1152 static const guchar *
1153 load_typelib (const gchar *filename,
1158 gsize *typelib_size;
1161 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1164 g_printerr ("Could not load typelib from '%s': %s\n",
1165 filename, g_module_error ());
1169 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1171 g_printerr ("Could not load typelib from '%s': %s\n",
1172 filename, g_module_error ());
1176 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1178 g_printerr ("Could not load typelib from '%s': %s\n",
1179 filename, g_module_error ());
1183 *len = *typelib_size;
1192 main (int argc, char *argv[])
1194 gboolean shlib = FALSE;
1195 gchar **input = NULL;
1196 GOptionContext *context;
1197 GError *error = NULL;
1198 gboolean needs_prefix;
1201 GOptionEntry options[] =
1203 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1204 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1205 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1206 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1210 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1214 g_typelib_check_sanity ();
1216 context = g_option_context_new ("");
1217 g_option_context_add_main_entries (context, options, NULL);
1218 g_option_context_parse (context, &argc, &argv, &error);
1222 g_fprintf (stderr, "no input files\n");
1227 if (includedirs != NULL)
1228 for (i = 0; includedirs[i]; i++)
1229 g_irepository_prepend_search_path (includedirs[i]);
1231 for (i = 0; input[i]; i++)
1233 GModule *dlhandle = NULL;
1234 const guchar *typelib;
1236 const char *namespace;
1240 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1242 g_fprintf (stderr, "failed to read '%s': %s\n",
1243 input[i], error->message);
1244 g_clear_error (&error);
1250 typelib = load_typelib (input[i], &dlhandle, &len);
1253 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1259 if (input[i + 1] && output)
1260 needs_prefix = TRUE;
1262 needs_prefix = FALSE;
1264 data = g_typelib_new_from_const_memory (typelib, len);
1266 GError *error = NULL;
1267 if (!g_typelib_validate (data, &error)) {
1268 g_printerr ("typelib not valid: %s\n", error->message);
1269 g_clear_error (&error);
1273 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1275 if (namespace == NULL)
1277 g_printerr ("failed to load typelib: %s\n", error->message);
1281 write_repository (namespace, needs_prefix);
1285 g_module_close (dlhandle);
1289 /* when writing to stdout, stop after the first module */
1290 if (input[i + 1] && !output)
1292 g_fprintf (stderr, "warning, %d modules omitted\n",
1293 g_strv_length (input) - 1);