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 check_unresolved ((GIBaseInfo*)info);
69 tag = g_type_info_get_tag (info);
70 is_pointer = g_type_info_is_pointer (info);
72 if (tag == GI_TYPE_TAG_VOID)
75 g_fprintf (file, "%s", "any");
77 g_fprintf (file, "%s", "none");
79 else if (G_TYPE_TAG_IS_BASIC (tag))
80 g_fprintf (file, "%s", g_type_tag_to_string (tag));
81 else if (tag == GI_TYPE_TAG_ARRAY)
85 type = g_type_info_get_param_type (info, 0);
86 write_type_info (namespace, type, file);
87 g_fprintf (file, "[");
89 length = g_type_info_get_array_length (info);
92 g_fprintf (file, "length=%d", length);
94 if (g_type_info_is_zero_terminated (info))
95 g_fprintf (file, "%szero-terminated=1", length >= 0 ? "," : "");
97 g_fprintf (file, "]");
98 g_base_info_unref ((GIBaseInfo *)type);
100 else if (tag == GI_TYPE_TAG_INTERFACE)
102 GIBaseInfo *iface = g_type_info_get_interface (info);
103 write_type_name (namespace, iface, file);
104 g_base_info_unref (iface);
106 else if (tag == GI_TYPE_TAG_GLIST)
108 type = g_type_info_get_param_type (info, 0);
109 g_fprintf (file, "GLib.List");
112 g_fprintf (file, "<");
113 write_type_info (namespace, type, file);
114 g_fprintf (file, ">");
115 g_base_info_unref ((GIBaseInfo *)type);
118 else if (tag == GI_TYPE_TAG_GSLIST)
120 type = g_type_info_get_param_type (info, 0);
121 g_fprintf (file, "GLib.SList");
124 g_fprintf (file, "<");
125 write_type_info (namespace, type, file);
126 g_fprintf (file, ">");
127 g_base_info_unref ((GIBaseInfo *)type);
130 else if (tag == GI_TYPE_TAG_GHASH)
132 type = g_type_info_get_param_type (info, 0);
133 g_fprintf (file, "GLib.HashTable");
136 g_fprintf (file, "<");
137 write_type_info (namespace, type, file);
138 g_base_info_unref ((GIBaseInfo *)type);
139 type = g_type_info_get_param_type (info, 1);
140 g_fprintf (file, ",");
141 write_type_info (namespace, type, file);
142 g_fprintf (file, ">");
143 g_base_info_unref ((GIBaseInfo *)type);
146 else if (tag == GI_TYPE_TAG_ERROR)
150 g_fprintf (file, "GLib.Error");
151 n = g_type_info_get_n_error_domains (info);
154 g_fprintf (file, "<");
155 for (i = 0; i < n; i++)
157 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
159 g_fprintf (file, ",");
160 write_type_name (namespace, (GIBaseInfo *)ed, file);
161 g_base_info_unref ((GIBaseInfo *)ed);
163 g_fprintf (file, ">");
168 g_printerr ("Unhandled type tag %d\n", tag);
169 g_assert_not_reached ();
174 write_constant_value (const gchar *namespace,
180 write_field_info (const gchar *namespace,
182 GIConstantInfo *branch,
186 GIFieldInfoFlags flags;
192 name = g_base_info_get_name ((GIBaseInfo *)info);
193 flags = g_field_info_get_flags (info);
194 size = g_field_info_get_size (info);
195 offset = g_field_info_get_offset (info);
198 " <field name=\"%s\" readable=\"%s\" writable=\"%s\"",
200 flags & GI_FIELD_IS_READABLE ? "1" : "0",
201 flags & GI_FIELD_IS_WRITABLE ? "1" : "0");
203 g_fprintf (file, " bits=\"%d\"", size);
205 g_fprintf (file, " offset=\"%d\"", offset);
207 type = g_field_info_get_type (info);
211 g_fprintf (file, " branch=\"");
212 type = g_constant_info_get_type (branch);
213 g_constant_info_get_value (branch, &value);
214 write_constant_value (namespace, type, &value, file);
215 g_fprintf (file, "\"");
218 g_fprintf (file,">\n");
220 g_fprintf (file, " <type name=\"");
222 write_type_info (namespace, type, file);
223 g_base_info_unref ((GIBaseInfo *)type);
225 g_fprintf (file, "\"/>\n");
227 g_fprintf (file, " </field>\n");
232 write_callable_info (const gchar *namespace,
233 GICallableInfo *info,
240 type = g_callable_info_get_return_type (info);
242 if (g_type_info_is_pointer (type))
244 switch (g_callable_info_get_caller_owns (info))
246 case GI_TRANSFER_NOTHING:
248 case GI_TRANSFER_CONTAINER:
249 g_fprintf (file, " transfer=\"shallow\"");
251 case GI_TRANSFER_EVERYTHING:
252 g_fprintf (file, " transfer=\"full\"");
255 g_assert_not_reached ();
259 g_fprintf (file, ">\n");
261 g_fprintf (file, "%*s <return-value>\n", indent, "");
263 if (g_callable_info_may_return_null (info))
264 g_fprintf (file, " null-ok=\"1\"");
266 g_fprintf (file, "%*s <type name=\"", indent + 2, "");
268 write_type_info (namespace, type, file);
270 g_fprintf (file, "\"/>\n");
272 g_fprintf (file, "%*s </return-value>\n", indent, "");
274 if (g_callable_info_get_n_args (info) <= 0)
277 g_fprintf (file, "%*s <parameters>\n", indent, "");
278 for (i = 0; i < g_callable_info_get_n_args (info); i++)
280 GIArgInfo *arg = g_callable_info_get_arg (info, i);
282 g_fprintf (file, "%*s <parameter name=\"%s\"",
283 indent, "", g_base_info_get_name ((GIBaseInfo *) arg));
285 switch (g_arg_info_get_ownership_transfer (arg))
287 case GI_TRANSFER_NOTHING:
289 case GI_TRANSFER_CONTAINER:
290 g_fprintf (file, " transfer=\"shallow\"");
292 case GI_TRANSFER_EVERYTHING:
293 g_fprintf (file, " transfer=\"full\"");
296 g_assert_not_reached ();
299 g_fprintf (file, " direction=\"");
300 switch (g_arg_info_get_direction (arg))
302 case GI_DIRECTION_IN:
303 g_fprintf (file, "in");
305 case GI_DIRECTION_OUT:
306 g_fprintf (file, "out");
308 case GI_DIRECTION_INOUT:
309 g_fprintf (file, "inout");
312 g_fprintf (file, "\"");
314 if (g_arg_info_may_be_null (arg))
315 g_fprintf (file, " null-ok=\"1\"");
317 if (g_arg_info_is_dipper (arg))
318 g_fprintf (file, " dipper=\"1\"");
320 if (g_arg_info_is_return_value (arg))
321 g_fprintf (file, " retval=\"1\"");
323 if (g_arg_info_is_optional (arg))
324 g_fprintf (file, " optional=\"1\"");
326 g_fprintf (file, ">\n");
328 g_fprintf (file, "%*s <type name=\"", indent+2, "");
330 type = g_arg_info_get_type (arg);
331 write_type_info (namespace, type, file);
333 g_fprintf (file, "\"/>\n");
335 g_fprintf (file, "%*s </parameter>\n", indent, "");
337 g_base_info_unref ((GIBaseInfo *)arg);
340 g_fprintf (file, "%*s </parameters>\n", indent, "");
341 g_base_info_unref ((GIBaseInfo *)type);
345 write_function_info (const gchar *namespace,
346 GIFunctionInfo *info,
350 GIFunctionInfoFlags flags;
356 flags = g_function_info_get_flags (info);
357 name = g_base_info_get_name ((GIBaseInfo *)info);
358 symbol = g_function_info_get_symbol (info);
359 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
361 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
363 else if (flags & GI_FUNCTION_IS_METHOD)
368 g_fprintf (file, "%*s<%s name=\"%s\" c:identifier=\"%s\"",
369 indent, "", tag, name, symbol);
371 if (flags & GI_FUNCTION_IS_SETTER)
372 g_fprintf (file, " type=\"setter\"");
373 else if (flags & GI_FUNCTION_IS_GETTER)
374 g_fprintf (file, " type=\"getter\"");
377 g_fprintf (file, " deprecated=\"1\"");
379 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
380 g_fprintf (file, "%*s</%s>\n", indent, "", tag);
384 write_callback_info (const gchar *namespace,
385 GICallbackInfo *info,
392 name = g_base_info_get_name ((GIBaseInfo *)info);
393 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
395 g_fprintf (file, "%*s<callback name=\"%s\"", indent, "", name);
398 g_fprintf (file, " deprecated=\"1\"");
400 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
401 g_fprintf (file, "%*s</callback>\n", indent, "");
405 write_struct_info (const gchar *namespace,
410 const gchar *type_name;
411 const gchar *type_init;
416 name = g_base_info_get_name ((GIBaseInfo *)info);
417 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
419 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
420 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
422 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
424 g_fprintf (file, " <glib:boxed glib:name=\"%s\"", name);
427 g_fprintf (file, " <record name=\"%s\"", name);
429 if (type_name != NULL)
430 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
433 g_fprintf (file, " deprecated=\"1\"");
435 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
438 g_fprintf (file, ">\n");
440 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
442 GIFieldInfo *field = g_struct_info_get_field (info, i);
443 write_field_info (namespace, field, NULL, file);
444 g_base_info_unref ((GIBaseInfo *)field);
447 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
449 GIFunctionInfo *function = g_struct_info_get_method (info, i);
450 write_function_info (namespace, function, file, 6);
451 g_base_info_unref ((GIBaseInfo *)function);
454 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
455 g_fprintf (file, " </glib:boxed>\n");
457 g_fprintf (file, " </record>\n");
461 g_fprintf (file, "/>\n");
466 write_value_info (const gchar *namespace,
474 name = g_base_info_get_name ((GIBaseInfo *)info);
475 value = g_value_info_get_value (info);
476 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
478 g_fprintf (file, " <member name=\"%s\" value=\"%ld\"", name, value);
481 g_fprintf (file, " deprecated=\"1\"");
483 g_fprintf (file, " />\n");
487 write_constant_value (const gchar *namespace,
492 switch (g_type_info_get_tag (type))
494 case GI_TYPE_TAG_BOOLEAN:
495 g_fprintf (file, "%d", value->v_boolean);
497 case GI_TYPE_TAG_INT8:
498 g_fprintf (file, "%d", value->v_int8);
500 case GI_TYPE_TAG_UINT8:
501 g_fprintf (file, "%d", value->v_uint8);
503 case GI_TYPE_TAG_INT16:
504 g_fprintf (file, "%" G_GINT16_FORMAT, value->v_int16);
506 case GI_TYPE_TAG_UINT16:
507 g_fprintf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
509 case GI_TYPE_TAG_INT32:
510 g_fprintf (file, "%" G_GINT32_FORMAT, value->v_int32);
512 case GI_TYPE_TAG_UINT32:
513 g_fprintf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
515 case GI_TYPE_TAG_INT64:
516 g_fprintf (file, "%" G_GINT64_FORMAT, value->v_int64);
518 case GI_TYPE_TAG_UINT64:
519 g_fprintf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
521 case GI_TYPE_TAG_INT:
522 g_fprintf (file, "%d", value->v_int);
524 case GI_TYPE_TAG_UINT:
525 g_fprintf (file, "%d", value->v_uint);
527 case GI_TYPE_TAG_LONG:
528 g_fprintf (file, "%ld", value->v_long);
530 case GI_TYPE_TAG_ULONG:
531 g_fprintf (file, "%ld", value->v_ulong);
533 case GI_TYPE_TAG_SSIZE:
534 g_fprintf (file, "%zd", value->v_ssize);
536 case GI_TYPE_TAG_SIZE:
537 g_fprintf (file, "%zd", value->v_size);
539 case GI_TYPE_TAG_FLOAT:
540 g_fprintf (file, "%f", value->v_float);
542 case GI_TYPE_TAG_DOUBLE:
543 g_fprintf (file, "%f", value->v_double);
545 case GI_TYPE_TAG_UTF8:
546 case GI_TYPE_TAG_FILENAME:
547 g_fprintf (file, "%s", value->v_string);
550 g_assert_not_reached ();
555 write_constant_info (const gchar *namespace,
556 GIConstantInfo *info,
565 name = g_base_info_get_name ((GIBaseInfo *)info);
566 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
568 g_fprintf (file, "%*s<constant name=\"%s\"", indent, "", name);
570 type = g_constant_info_get_type (info);
571 g_fprintf (file, " value=\"");
573 g_constant_info_get_value (info, &value);
574 write_constant_value (namespace, type, &value, file);
575 g_fprintf (file, "\">\n");
577 g_fprintf (file, "%*s<type name=\"", indent + 2, "");
579 write_type_info (namespace, type, file);
581 g_fprintf (file, "\"/>\n");
583 g_fprintf (file, "%*s</constant>\n", indent, "");
585 g_base_info_unref ((GIBaseInfo *)type);
590 write_enum_info (const gchar *namespace,
595 const gchar *type_name;
596 const gchar *type_init;
600 name = g_base_info_get_name ((GIBaseInfo *)info);
601 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
603 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
604 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
606 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
607 g_fprintf (file, " <enumeration ");
609 g_fprintf (file, " <bitfield ");
610 g_fprintf (file, "name=\"%s\"", name);
613 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
616 g_fprintf (file, " deprecated=\"1\"");
618 g_fprintf (file, ">\n");
620 for (i = 0; i < g_enum_info_get_n_values (info); i++)
622 GIValueInfo *value = g_enum_info_get_value (info, i);
623 write_value_info (namespace, value, file);
624 g_base_info_unref ((GIBaseInfo *)value);
627 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
628 g_fprintf (file, " </enumeration>\n");
630 g_fprintf (file, " </bitfield>\n");
634 write_signal_info (const gchar *namespace,
642 name = g_base_info_get_name ((GIBaseInfo *)info);
643 flags = g_signal_info_get_flags (info);
644 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
646 g_fprintf (file, " <glib:signal name=\"%s\"", name);
649 g_fprintf (file, " deprecated=\"1\"");
651 if (flags & G_SIGNAL_RUN_FIRST)
652 g_fprintf (file, " when=\"FIRST\"");
653 else if (flags & G_SIGNAL_RUN_LAST)
654 g_fprintf (file, " when=\"LAST\"");
655 else if (flags & G_SIGNAL_RUN_CLEANUP)
656 g_fprintf (file, " when=\"CLEANUP\"");
658 if (flags & G_SIGNAL_NO_RECURSE)
659 g_fprintf (file, " no-recurse=\"1\"");
661 if (flags & G_SIGNAL_DETAILED)
662 g_fprintf (file, " detailed=\"1\"");
664 if (flags & G_SIGNAL_ACTION)
665 g_fprintf (file, " action=\"1\"");
667 if (flags & G_SIGNAL_NO_HOOKS)
668 g_fprintf (file, " no-hooks=\"1\"");
670 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
672 g_fprintf (file, " </glib:signal>\n");
676 write_vfunc_info (const gchar *namespace,
680 GIVFuncInfoFlags flags;
685 name = g_base_info_get_name ((GIBaseInfo *)info);
686 flags = g_vfunc_info_get_flags (info);
687 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
688 offset = g_vfunc_info_get_offset (info);
690 g_fprintf (file, " <vfunc name=\"%s\"", name);
693 g_fprintf (file, " deprecated=\"1\"");
695 if (flags & GI_VFUNC_MUST_CHAIN_UP)
696 g_fprintf (file, " must-chain-up=\"1\"");
698 if (flags & GI_VFUNC_MUST_OVERRIDE)
699 g_fprintf (file, " override=\"always\"");
700 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
701 g_fprintf (file, " override=\"never\"");
703 g_fprintf (file, " offset=\"%d\"", offset);
705 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
707 g_fprintf (file, " </vfunc>\n");
711 write_property_info (const gchar *namespace,
712 GIPropertyInfo *info,
720 name = g_base_info_get_name ((GIBaseInfo *)info);
721 flags = g_property_info_get_flags (info);
722 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
724 g_fprintf (file, " <property name=\"%s\"", name);
727 g_fprintf (file, " deprecated=\"1\"");
729 if (flags & G_PARAM_READABLE)
730 g_fprintf (file, " readable=\"1\"");
732 g_fprintf (file, " readable=\"0\"");
734 if (flags & G_PARAM_WRITABLE)
735 g_fprintf (file, " writable=\"1\"");
737 g_fprintf (file, " writable=\"0\"");
739 if (flags & G_PARAM_CONSTRUCT)
740 g_fprintf (file, " construct=\"1\"");
742 if (flags & G_PARAM_CONSTRUCT_ONLY)
743 g_fprintf (file, " construct-only=\"1\"");
745 type = g_property_info_get_type (info);
747 g_fprintf (file, ">\n");
749 g_fprintf (file, " <type name=\"", name);
751 write_type_info (namespace, type, file);
753 g_fprintf (file, "\"/>\n");
755 g_fprintf (file, " </property>\n");
760 write_object_info (const gchar *namespace,
765 const gchar *type_name;
766 const gchar *type_init;
771 name = g_base_info_get_name ((GIBaseInfo *)info);
772 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
774 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
775 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
776 g_fprintf (file, " <class name=\"%s\"", name);
778 pnode = g_object_info_get_parent (info);
781 g_fprintf (file, " parent=\"");
782 write_type_name (namespace, (GIBaseInfo *)pnode, file);
783 g_fprintf (file, "\"" );
784 g_base_info_unref ((GIBaseInfo *)pnode);
787 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
790 g_fprintf (file, " deprecated=\"1\"");
792 g_fprintf (file, ">\n");
794 if (g_object_info_get_n_interfaces (info) > 0)
796 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
798 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
799 g_fprintf (file, " <implements name=\"");
800 write_type_name (namespace, (GIBaseInfo*)imp, file);
801 g_fprintf (file,"\" />\n");
802 g_base_info_unref ((GIBaseInfo*)imp);
806 for (i = 0; i < g_object_info_get_n_fields (info); i++)
808 GIFieldInfo *field = g_object_info_get_field (info, i);
809 write_field_info (namespace, field, NULL, file);
810 g_base_info_unref ((GIBaseInfo *)field);
813 for (i = 0; i < g_object_info_get_n_methods (info); i++)
815 GIFunctionInfo *function = g_object_info_get_method (info, i);
816 write_function_info (namespace, function, file, 6);
817 g_base_info_unref ((GIBaseInfo *)function);
820 for (i = 0; i < g_object_info_get_n_properties (info); i++)
822 GIPropertyInfo *prop = g_object_info_get_property (info, i);
823 write_property_info (namespace, prop, file);
824 g_base_info_unref ((GIBaseInfo *)prop);
827 for (i = 0; i < g_object_info_get_n_signals (info); i++)
829 GISignalInfo *signal = g_object_info_get_signal (info, i);
830 write_signal_info (namespace, signal, file);
831 g_base_info_unref ((GIBaseInfo *)signal);
834 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
836 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
837 write_vfunc_info (namespace, vfunc, file);
838 g_base_info_unref ((GIBaseInfo *)vfunc);
841 for (i = 0; i < g_object_info_get_n_constants (info); i++)
843 GIConstantInfo *constant = g_object_info_get_constant (info, i);
844 write_constant_info (namespace, constant, file, 6);
845 g_base_info_unref ((GIBaseInfo *)constant);
848 g_fprintf (file, " </class>\n");
852 write_interface_info (const gchar *namespace,
853 GIInterfaceInfo *info,
857 const gchar *type_name;
858 const gchar *type_init;
862 name = g_base_info_get_name ((GIBaseInfo *)info);
863 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
865 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
866 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
867 g_fprintf (file, " <interface name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
868 name, type_name, type_init);
871 g_fprintf (file, " deprecated=\"1\"");
873 g_fprintf (file, ">\n");
875 if (g_interface_info_get_n_prerequisites (info) > 0)
877 g_fprintf (file, " <requires>\n");
878 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
880 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
882 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
883 g_fprintf (file, " <interface name=\"");
885 g_fprintf (file, " <object name=\"");
886 write_type_name (namespace, req, file);
887 g_fprintf (file, "\" />\n");
888 g_base_info_unref (req);
890 g_fprintf (file, " </requires>\n");
893 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
895 GIFunctionInfo *function = g_interface_info_get_method (info, i);
896 write_function_info (namespace, function, file, 6);
897 g_base_info_unref ((GIBaseInfo *)function);
900 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
902 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
903 write_property_info (namespace, prop, file);
904 g_base_info_unref ((GIBaseInfo *)prop);
907 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
909 GISignalInfo *signal = g_interface_info_get_signal (info, i);
910 write_signal_info (namespace, signal, file);
911 g_base_info_unref ((GIBaseInfo *)signal);
914 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
916 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
917 write_vfunc_info (namespace, vfunc, file);
918 g_base_info_unref ((GIBaseInfo *)vfunc);
921 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
923 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
924 write_constant_info (namespace, constant, file, 6);
925 g_base_info_unref ((GIBaseInfo *)constant);
928 g_fprintf (file, " </interface>\n");
932 write_error_domain_info (const gchar *namespace,
933 GIErrorDomainInfo *info,
937 const gchar *name, *quark;
939 name = g_base_info_get_name ((GIBaseInfo *)info);
940 quark = g_error_domain_info_get_quark (info);
941 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
943 " <errordomain name=\"%s\" get-quark=\"%s\" codes=\"",
945 write_type_name (namespace, enum_, file);
946 g_fprintf (file, "\" />\n");
947 g_base_info_unref (enum_);
951 write_union_info (const gchar *namespace,
956 const gchar *type_name;
957 const gchar *type_init;
961 name = g_base_info_get_name ((GIBaseInfo *)info);
962 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
964 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
965 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
967 g_fprintf (file, " <union name=\"%s\"", name);
970 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
973 g_fprintf (file, " deprecated=\"1\"");
975 g_fprintf (file, ">\n");
977 if (g_union_info_is_discriminated (info))
982 offset = g_union_info_get_discriminator_offset (info);
983 type = g_union_info_get_discriminator_type (info);
985 g_fprintf (file, " <discriminator offset=\"%d\" type=\"", offset);
986 write_type_info (namespace, type, file);
987 g_fprintf (file, "\" />\n");
988 g_base_info_unref ((GIBaseInfo *)type);
991 for (i = 0; i < g_union_info_get_n_fields (info); i++)
993 GIFieldInfo *field = g_union_info_get_field (info, i);
994 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
995 write_field_info (namespace, field, constant, file);
996 g_base_info_unref ((GIBaseInfo *)field);
998 g_base_info_unref ((GIBaseInfo *)constant);
1001 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1003 GIFunctionInfo *function = g_union_info_get_method (info, i);
1004 write_function_info (namespace, function, file, 6);
1005 g_base_info_unref ((GIBaseInfo *)function);
1008 g_fprintf (file, " </union>\n");
1012 write_repository (const char *namespace,
1013 gboolean needs_prefix)
1018 char **dependencies;
1019 GIRepository *repository;
1021 repository = g_irepository_get_default ();
1030 filename = g_strdup_printf ("%s-%s", namespace, output);
1032 filename = g_strdup (output);
1033 file = g_fopen (filename, "w");
1037 g_fprintf (stderr, "failed to open '%s': %s\n",
1038 filename, g_strerror (errno));
1047 g_fprintf (file, "<?xml version=\"1.0\"?>\n");
1048 g_fprintf (file, "<repository version=\"1.0\"\n"
1049 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1050 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1051 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\">\n");
1053 dependencies = g_irepository_get_dependencies (repository,
1055 if (dependencies != NULL)
1057 for (i = 0; dependencies[i]; i++)
1059 g_fprintf (file, " <include name=\"%s\"/>\n", dependencies[i]);
1065 const gchar *shared_library;
1066 const char *ns = namespace;
1067 const char *version;
1069 version = g_irepository_get_version (repository, ns);
1071 shared_library = g_irepository_get_shared_library (repository, ns);
1073 g_fprintf (file, " <namespace name=\"%s\" version=\"%s\" shared-library=\"%s\">\n",
1074 ns, version, shared_library);
1076 g_fprintf (file, " <namespace name=\"%s\" version=\"%s\">\n", ns, version);
1078 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1080 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1081 switch (g_base_info_get_type (info))
1083 case GI_INFO_TYPE_FUNCTION:
1084 write_function_info (ns, (GIFunctionInfo *)info, file, 4);
1087 case GI_INFO_TYPE_CALLBACK:
1088 write_callback_info (ns, (GICallbackInfo *)info, file, 4);
1091 case GI_INFO_TYPE_STRUCT:
1092 case GI_INFO_TYPE_BOXED:
1093 write_struct_info (ns, (GIStructInfo *)info, file);
1096 case GI_INFO_TYPE_UNION:
1097 write_union_info (ns, (GIUnionInfo *)info, file);
1100 case GI_INFO_TYPE_ENUM:
1101 case GI_INFO_TYPE_FLAGS:
1102 write_enum_info (ns, (GIEnumInfo *)info, file);
1105 case GI_INFO_TYPE_CONSTANT:
1106 write_constant_info (ns, (GIConstantInfo *)info, file, 4);
1109 case GI_INFO_TYPE_OBJECT:
1110 write_object_info (ns, (GIObjectInfo *)info, file);
1113 case GI_INFO_TYPE_INTERFACE:
1114 write_interface_info (ns, (GIInterfaceInfo *)info, file);
1117 case GI_INFO_TYPE_ERROR_DOMAIN:
1118 write_error_domain_info (ns, (GIErrorDomainInfo *)info, file);
1122 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1125 g_base_info_unref (info);
1128 g_fprintf (file, " </namespace>\n");
1131 g_fprintf (file, "</repository>\n");
1137 static const guchar *
1138 load_typelib (const gchar *filename,
1143 gsize *typelib_size;
1146 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1149 g_printerr ("Could not load typelib from '%s': %s\n",
1150 filename, g_module_error ());
1154 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1156 g_printerr ("Could not load typelib from '%s': %s\n",
1157 filename, g_module_error ());
1161 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1163 g_printerr ("Could not load typelib from '%s': %s\n",
1164 filename, g_module_error ());
1168 *len = *typelib_size;
1177 main (int argc, char *argv[])
1179 gboolean shlib = FALSE;
1180 gchar **input = NULL;
1181 GOptionContext *context;
1182 GError *error = NULL;
1183 gboolean needs_prefix;
1186 GOptionEntry options[] =
1188 { "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
1189 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1190 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
1191 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1195 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
1199 g_typelib_check_sanity ();
1201 context = g_option_context_new ("");
1202 g_option_context_add_main_entries (context, options, NULL);
1203 g_option_context_parse (context, &argc, &argv, &error);
1207 g_fprintf (stderr, "no input files\n");
1212 if (includedirs != NULL)
1213 for (i = 0; includedirs[i]; i++)
1214 g_irepository_prepend_search_path (includedirs[i]);
1216 for (i = 0; input[i]; i++)
1218 GModule *dlhandle = NULL;
1219 const guchar *typelib;
1221 const char *namespace;
1225 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1227 g_fprintf (stderr, "failed to read '%s': %s\n",
1228 input[i], error->message);
1229 g_clear_error (&error);
1235 typelib = load_typelib (input[i], &dlhandle, &len);
1238 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1244 if (input[i + 1] && output)
1245 needs_prefix = TRUE;
1247 needs_prefix = FALSE;
1249 data = g_typelib_new_from_const_memory (typelib, len);
1251 GError *error = NULL;
1252 if (!g_typelib_validate (data, &error)) {
1253 g_printerr ("typelib not valid: %s\n", error->message);
1254 g_clear_error (&error);
1258 namespace = g_irepository_load_typelib (g_irepository_get_default (), data, 0,
1260 if (namespace == NULL)
1262 g_printerr ("failed to load typelib: %s\n", error->message);
1266 write_repository (namespace, needs_prefix);
1270 g_module_close (dlhandle);
1274 /* when writing to stdout, stop after the first module */
1275 if (input[i + 1] && !output)
1277 g_fprintf (stderr, "warning, %d modules omitted\n",
1278 g_strv_length (input) - 1);