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.
27 #include <glib-object.h>
28 #include <glib/gstdio.h>
30 #include "girepository.h"
33 /* FIXME: Avoid global */
34 static gchar *output = NULL;
37 write_type_name (const gchar *namespace,
41 if (strcmp (namespace, g_base_info_get_namespace (info)) != 0)
42 g_fprintf (file, "%s.", g_base_info_get_namespace (info));
44 g_fprintf (file, "%s", g_base_info_get_name (info));
48 write_type_info (const gchar *namespace,
56 const gchar* basic[] = {
79 tag = g_type_info_get_tag (info);
82 g_fprintf (file, "%s%s", basic[tag], g_type_info_is_pointer (info) ? "*" : "");
84 g_fprintf (file, "%s", basic[tag]);
89 type = g_type_info_get_param_type (info, 0);
90 write_type_info (namespace, type, file);
91 g_fprintf (file, "[");
93 length = g_type_info_get_array_length (info);
96 g_fprintf (file, "length=%d", length);
98 if (g_type_info_is_zero_terminated (info))
99 g_fprintf (file, "%szero-terminated=1", length >= 0 ? "," : "");
101 g_fprintf (file, "]");
102 g_base_info_unref ((GIBaseInfo *)type);
106 GIBaseInfo *iface = g_type_info_get_interface (info);
107 write_type_name (namespace, iface, file);
108 if (g_type_info_is_pointer (info))
109 g_fprintf (file, "*");
110 g_base_info_unref (iface);
114 type = g_type_info_get_param_type (info, 0);
115 g_fprintf (file, "GList");
118 g_fprintf (file, "<");
119 write_type_info (namespace, type, file);
120 g_fprintf (file, ">");
121 g_base_info_unref ((GIBaseInfo *)type);
123 g_fprintf (file, "*");
127 type = g_type_info_get_param_type (info, 0);
128 g_fprintf (file, "GSList");
131 g_fprintf (file, "<");
132 write_type_info (namespace, type, file);
133 g_fprintf (file, ">");
134 g_base_info_unref ((GIBaseInfo *)type);
136 g_fprintf (file, "*");
140 type = g_type_info_get_param_type (info, 0);
141 g_fprintf (file, "GHashTable");
144 g_fprintf (file, "<");
145 write_type_info (namespace, type, file);
146 g_base_info_unref ((GIBaseInfo *)type);
147 type = g_type_info_get_param_type (info, 1);
148 g_fprintf (file, ",");
149 write_type_info (namespace, type, file);
150 g_fprintf (file, ">");
151 g_base_info_unref ((GIBaseInfo *)type);
153 g_fprintf (file, "*");
159 g_fprintf (file, "GError");
160 n = g_type_info_get_n_error_domains (info);
163 g_fprintf (file, "<");
164 for (i = 0; i < n; i++)
166 GIErrorDomainInfo *ed = g_type_info_get_error_domain (info, i);
168 g_fprintf (file, ",");
169 write_type_name (namespace, (GIBaseInfo *)ed, file);
170 g_base_info_unref ((GIBaseInfo *)ed);
172 g_fprintf (file, ">");
174 g_fprintf (file, "*");
179 write_constant_value (const gchar *namespace,
185 write_field_info (const gchar *namespace,
187 GIConstantInfo *branch,
191 GIFieldInfoFlags flags;
197 name = g_base_info_get_name ((GIBaseInfo *)info);
198 flags = g_field_info_get_flags (info);
199 size = g_field_info_get_size (info);
200 offset = g_field_info_get_offset (info);
203 " <field name=\"%s\" readable=\"%s\" writable=\"%s\"",
205 flags & GI_FIELD_IS_READABLE ? "1" : "0",
206 flags & GI_FIELD_IS_WRITABLE ? "1" : "0");
208 g_fprintf (file, " bits=\"%d\"", size);
210 g_fprintf (file, " offset=\"%d\"", offset);
212 type = g_field_info_get_type (info);
216 g_fprintf (file, " branch=\"");
217 type = g_constant_info_get_type (branch);
218 g_constant_info_get_value (branch, &value);
219 write_constant_value (namespace, type, &value, file);
220 g_fprintf (file, "\"");
223 g_fprintf (file,">\n");
225 g_fprintf (file, " <type name=\"");
227 write_type_info (namespace, type, file);
228 g_base_info_unref ((GIBaseInfo *)type);
230 g_fprintf (file, "\"/>\n");
232 g_fprintf (file, " </field>\n");
237 write_callable_info (const gchar *namespace,
238 GICallableInfo *info,
245 type = g_callable_info_get_return_type (info);
247 if (g_type_info_is_pointer (type))
249 switch (g_callable_info_get_caller_owns (info))
251 case GI_TRANSFER_NOTHING:
252 g_fprintf (file, " transfer=\"none\"");
254 case GI_TRANSFER_CONTAINER:
255 g_fprintf (file, " transfer=\"shallow\"");
257 case GI_TRANSFER_EVERYTHING:
258 g_fprintf (file, " transfer=\"full\"");
261 g_assert_not_reached ();
265 g_fprintf (file, ">\n");
267 g_fprintf (file, "%*s <return-value>\n", indent, "");
269 if (g_callable_info_may_return_null (info))
270 g_fprintf (file, " null-ok=\"1\"");
272 g_fprintf (file, "%*s <type name=\"", indent + 2, "");
274 write_type_info (namespace, type, file);
276 g_fprintf (file, "\"/>\n");
278 g_fprintf (file, "%*s </return-value>\n", indent, "");
280 if (g_callable_info_get_n_args (info) <= 0)
283 g_fprintf (file, "%*s <parameters>\n", indent, "");
284 for (i = 0; i < g_callable_info_get_n_args (info); i++)
286 GIArgInfo *arg = g_callable_info_get_arg (info, i);
288 g_fprintf (file, "%*s <parameter name=\"%s\"",
289 indent, "", g_base_info_get_name ((GIBaseInfo *) arg));
291 if (g_type_info_is_pointer (type))
293 switch (g_arg_info_get_ownership_transfer (arg))
295 case GI_TRANSFER_NOTHING:
296 g_fprintf (file, " transfer=\"none\"");
298 case GI_TRANSFER_CONTAINER:
299 g_fprintf (file, " transfer=\"shallow\"");
301 case GI_TRANSFER_EVERYTHING:
302 g_fprintf (file, " transfer=\"full\"");
305 g_assert_not_reached ();
309 g_fprintf (file, " direction=\"");
310 switch (g_arg_info_get_direction (arg))
312 case GI_DIRECTION_IN:
313 g_fprintf (file, "in");
315 case GI_DIRECTION_OUT:
316 g_fprintf (file, "out");
318 case GI_DIRECTION_INOUT:
319 g_fprintf (file, "inout");
322 g_fprintf (file, "\"");
324 if (g_arg_info_may_be_null (arg))
325 g_fprintf (file, " null-ok=\"1\"");
327 if (g_arg_info_is_dipper (arg))
328 g_fprintf (file, " dipper=\"1\"");
330 if (g_arg_info_is_return_value (arg))
331 g_fprintf (file, " retval=\"1\"");
333 if (g_arg_info_is_optional (arg))
334 g_fprintf (file, " optional=\"1\"");
336 g_fprintf (file, ">\n");
338 g_fprintf (file, "%*s <type name=\"", indent+2, "");
340 type = g_arg_info_get_type (arg);
341 write_type_info (namespace, type, file);
343 g_fprintf (file, "\"/>\n");
345 g_fprintf (file, "%*s </parameter>\n", indent, "");
347 g_base_info_unref ((GIBaseInfo *)arg);
350 g_fprintf (file, "%*s </parameters>\n", indent, "");
351 g_base_info_unref ((GIBaseInfo *)type);
355 write_function_info (const gchar *namespace,
356 GIFunctionInfo *info,
360 GIFunctionInfoFlags flags;
366 flags = g_function_info_get_flags (info);
367 name = g_base_info_get_name ((GIBaseInfo *)info);
368 symbol = g_function_info_get_symbol (info);
369 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
371 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
373 else if (flags & GI_FUNCTION_IS_METHOD)
378 g_fprintf (file, "%*s<%s name=\"%s\" c:identifier=\"%s\"",
379 indent, "", tag, name, symbol);
381 if (flags & GI_FUNCTION_IS_SETTER)
382 g_fprintf (file, " type=\"setter\"");
383 else if (flags & GI_FUNCTION_IS_GETTER)
384 g_fprintf (file, " type=\"getter\"");
387 g_fprintf (file, " deprecated=\"1\"");
389 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
390 g_fprintf (file, "%*s</%s>\n", indent, "", tag);
394 write_callback_info (const gchar *namespace,
395 GICallbackInfo *info,
402 name = g_base_info_get_name ((GIBaseInfo *)info);
403 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
405 g_fprintf (file, "%*s<callback name=\"%s\"", indent, "", name);
408 g_fprintf (file, " deprecated=\"1\"");
410 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
411 g_fprintf (file, "%*s</callback>\n", indent, "");
415 write_struct_info (const gchar *namespace,
420 const gchar *type_name;
421 const gchar *type_init;
426 name = g_base_info_get_name ((GIBaseInfo *)info);
427 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
429 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
431 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
432 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
434 g_fprintf (file, " <glib:boxed glib:name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"", name, type_name, type_init);
437 g_fprintf (file, " <record name=\"%s\"", name);
440 g_fprintf (file, " deprecated=\"1\"");
442 n_elts = g_struct_info_get_n_fields (info) + g_struct_info_get_n_methods (info);
445 g_fprintf (file, ">\n");
447 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
449 GIFieldInfo *field = g_struct_info_get_field (info, i);
450 write_field_info (namespace, field, NULL, file);
451 g_base_info_unref ((GIBaseInfo *)field);
454 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
456 GIFunctionInfo *function = g_struct_info_get_method (info, i);
457 write_function_info (namespace, function, file, 6);
458 g_base_info_unref ((GIBaseInfo *)function);
461 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
462 g_fprintf (file, " </glib:boxed>\n");
464 g_fprintf (file, " </record>\n");
468 g_fprintf (file, "/>\n");
473 write_value_info (const gchar *namespace,
481 name = g_base_info_get_name ((GIBaseInfo *)info);
482 value = g_value_info_get_value (info);
483 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
485 g_fprintf (file, " <member name=\"%s\" value=\"%ld\"", name, value);
488 g_fprintf (file, " deprecated=\"1\"");
490 g_fprintf (file, " />\n");
494 write_constant_value (const gchar *namespace,
499 switch (g_type_info_get_tag (type))
501 case GI_TYPE_TAG_BOOLEAN:
502 g_fprintf (file, "%d", value->v_boolean);
504 case GI_TYPE_TAG_INT8:
505 g_fprintf (file, "%d", value->v_int8);
507 case GI_TYPE_TAG_UINT8:
508 g_fprintf (file, "%d", value->v_uint8);
510 case GI_TYPE_TAG_INT16:
511 g_fprintf (file, "%" G_GINT16_FORMAT, value->v_int16);
513 case GI_TYPE_TAG_UINT16:
514 g_fprintf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
516 case GI_TYPE_TAG_INT32:
517 g_fprintf (file, "%" G_GINT32_FORMAT, value->v_int32);
519 case GI_TYPE_TAG_UINT32:
520 g_fprintf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
522 case GI_TYPE_TAG_INT64:
523 g_fprintf (file, "%" G_GINT64_FORMAT, value->v_int64);
525 case GI_TYPE_TAG_UINT64:
526 g_fprintf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
528 case GI_TYPE_TAG_INT:
529 g_fprintf (file, "%d", value->v_int);
531 case GI_TYPE_TAG_UINT:
532 g_fprintf (file, "%d", value->v_uint);
534 case GI_TYPE_TAG_LONG:
535 g_fprintf (file, "%ld", value->v_long);
537 case GI_TYPE_TAG_ULONG:
538 g_fprintf (file, "%ld", value->v_ulong);
540 case GI_TYPE_TAG_SSIZE:
541 g_fprintf (file, "%zd", value->v_ssize);
543 case GI_TYPE_TAG_SIZE:
544 g_fprintf (file, "%zd", value->v_size);
546 case GI_TYPE_TAG_FLOAT:
547 g_fprintf (file, "%f", value->v_float);
549 case GI_TYPE_TAG_DOUBLE:
550 g_fprintf (file, "%f", value->v_double);
552 case GI_TYPE_TAG_UTF8:
553 case GI_TYPE_TAG_FILENAME:
554 g_fprintf (file, "%s", value->v_string);
557 g_assert_not_reached ();
562 write_constant_info (const gchar *namespace,
563 GIConstantInfo *info,
572 name = g_base_info_get_name ((GIBaseInfo *)info);
573 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
575 g_fprintf (file, "%*s<constant name=\"%s\"", indent, "", name);
577 type = g_constant_info_get_type (info);
578 g_fprintf (file, " value=\"");
580 g_constant_info_get_value (info, &value);
581 write_constant_value (namespace, type, &value, file);
582 g_fprintf (file, "\">\n");
584 g_fprintf (file, "%*s<type name=\"", indent + 2, "");
586 write_type_info (namespace, type, file);
588 g_fprintf (file, "\"/>\n");
590 g_fprintf (file, "%*s</constant>\n", indent, "");
592 g_base_info_unref ((GIBaseInfo *)type);
597 write_enum_info (const gchar *namespace,
602 const gchar *type_name;
603 const gchar *type_init;
607 name = g_base_info_get_name ((GIBaseInfo *)info);
608 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
610 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
611 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
613 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
614 g_fprintf (file, " <enumeration ");
616 g_fprintf (file, " <bitfield ");
617 g_fprintf (file, "name=\"%s\"", name);
620 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
623 g_fprintf (file, " deprecated=\"1\"");
625 g_fprintf (file, ">\n");
627 for (i = 0; i < g_enum_info_get_n_values (info); i++)
629 GIValueInfo *value = g_enum_info_get_value (info, i);
630 write_value_info (namespace, value, file);
631 g_base_info_unref ((GIBaseInfo *)value);
634 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
635 g_fprintf (file, " </enumeration>\n");
637 g_fprintf (file, " </bitfield>\n");
641 write_signal_info (const gchar *namespace,
649 name = g_base_info_get_name ((GIBaseInfo *)info);
650 flags = g_signal_info_get_flags (info);
651 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
653 g_fprintf (file, " <glib:signal name=\"%s\"", name);
656 g_fprintf (file, " deprecated=\"1\"");
658 if (flags & G_SIGNAL_RUN_FIRST)
659 g_fprintf (file, " when=\"FIRST\"");
660 else if (flags & G_SIGNAL_RUN_LAST)
661 g_fprintf (file, " when=\"LAST\"");
662 else if (flags & G_SIGNAL_RUN_CLEANUP)
663 g_fprintf (file, " when=\"CLEANUP\"");
665 if (flags & G_SIGNAL_NO_RECURSE)
666 g_fprintf (file, " no-recurse=\"1\"");
668 if (flags & G_SIGNAL_DETAILED)
669 g_fprintf (file, " detailed=\"1\"");
671 if (flags & G_SIGNAL_ACTION)
672 g_fprintf (file, " action=\"1\"");
674 if (flags & G_SIGNAL_NO_HOOKS)
675 g_fprintf (file, " no-hooks=\"1\"");
677 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
679 g_fprintf (file, " </glib:signal>\n");
683 write_vfunc_info (const gchar *namespace,
687 GIVFuncInfoFlags flags;
692 name = g_base_info_get_name ((GIBaseInfo *)info);
693 flags = g_vfunc_info_get_flags (info);
694 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
695 offset = g_vfunc_info_get_offset (info);
697 g_fprintf (file, " <vfunc name=\"%s\"", name);
700 g_fprintf (file, " deprecated=\"1\"");
702 if (flags & GI_VFUNC_MUST_CHAIN_UP)
703 g_fprintf (file, " must-chain-up=\"1\"");
705 if (flags & GI_VFUNC_MUST_OVERRIDE)
706 g_fprintf (file, " override=\"always\"");
707 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
708 g_fprintf (file, " override=\"never\"");
710 g_fprintf (file, " offset=\"%d\"", offset);
712 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
714 g_fprintf (file, " </vfunc>\n");
718 write_property_info (const gchar *namespace,
719 GIPropertyInfo *info,
727 name = g_base_info_get_name ((GIBaseInfo *)info);
728 flags = g_property_info_get_flags (info);
729 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
731 g_fprintf (file, " <property name=\"%s\"", name);
734 g_fprintf (file, " deprecated=\"1\"");
736 if (flags & G_PARAM_READABLE)
737 g_fprintf (file, " readable=\"1\"");
739 g_fprintf (file, " readable=\"0\"");
741 if (flags & G_PARAM_WRITABLE)
742 g_fprintf (file, " writable=\"1\"");
744 g_fprintf (file, " writable=\"0\"");
746 if (flags & G_PARAM_CONSTRUCT)
747 g_fprintf (file, " construct=\"1\"");
749 if (flags & G_PARAM_CONSTRUCT_ONLY)
750 g_fprintf (file, " construct-only=\"1\"");
752 type = g_property_info_get_type (info);
754 g_fprintf (file, ">\n");
756 g_fprintf (file, " <type name=\"", name);
758 write_type_info (namespace, type, file);
760 g_fprintf (file, "\"/>\n");
762 g_fprintf (file, " </property>\n");
767 write_object_info (const gchar *namespace,
772 const gchar *type_name;
773 const gchar *type_init;
778 name = g_base_info_get_name ((GIBaseInfo *)info);
779 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
781 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
782 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
783 g_fprintf (file, " <class name=\"%s\"", name);
785 pnode = g_object_info_get_parent (info);
788 g_fprintf (file, " parent=\"");
789 write_type_name (namespace, (GIBaseInfo *)pnode, file);
790 g_fprintf (file, "\"" );
791 g_base_info_unref ((GIBaseInfo *)pnode);
794 g_fprintf (file, " glib:type-name=\"%s\" glib:get-type=\"%s\"", type_name, type_init);
797 g_fprintf (file, " deprecated=\"1\"");
799 g_fprintf (file, ">\n");
801 if (g_object_info_get_n_interfaces (info) > 0)
803 g_fprintf (file, " <implements>\n");
804 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
806 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
807 g_fprintf (file, " <interface name=\"");
808 write_type_name (namespace, (GIBaseInfo*)imp, file);
809 g_fprintf (file,"\" />\n");
810 g_base_info_unref ((GIBaseInfo*)imp);
812 g_fprintf (file, " </implements>\n");
815 for (i = 0; i < g_object_info_get_n_fields (info); i++)
817 GIFieldInfo *field = g_object_info_get_field (info, i);
818 write_field_info (namespace, field, NULL, file);
819 g_base_info_unref ((GIBaseInfo *)field);
822 for (i = 0; i < g_object_info_get_n_methods (info); i++)
824 GIFunctionInfo *function = g_object_info_get_method (info, i);
825 write_function_info (namespace, function, file, 6);
826 g_base_info_unref ((GIBaseInfo *)function);
829 for (i = 0; i < g_object_info_get_n_properties (info); i++)
831 GIPropertyInfo *prop = g_object_info_get_property (info, i);
832 write_property_info (namespace, prop, file);
833 g_base_info_unref ((GIBaseInfo *)prop);
836 for (i = 0; i < g_object_info_get_n_signals (info); i++)
838 GISignalInfo *signal = g_object_info_get_signal (info, i);
839 write_signal_info (namespace, signal, file);
840 g_base_info_unref ((GIBaseInfo *)signal);
843 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
845 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
846 write_vfunc_info (namespace, vfunc, file);
847 g_base_info_unref ((GIBaseInfo *)vfunc);
850 for (i = 0; i < g_object_info_get_n_constants (info); i++)
852 GIConstantInfo *constant = g_object_info_get_constant (info, i);
853 write_constant_info (namespace, constant, file, 6);
854 g_base_info_unref ((GIBaseInfo *)constant);
857 g_fprintf (file, " </class>\n");
861 write_interface_info (const gchar *namespace,
862 GIInterfaceInfo *info,
866 const gchar *type_name;
867 const gchar *type_init;
871 name = g_base_info_get_name ((GIBaseInfo *)info);
872 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
874 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
875 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
876 g_fprintf (file, " <interface name=\"%s\" glib:type-name=\"%s\" glib:get-type=\"%s\"",
877 name, type_name, type_init);
880 g_fprintf (file, " deprecated=\"1\"");
882 g_fprintf (file, ">\n");
884 if (g_interface_info_get_n_prerequisites (info) > 0)
886 g_fprintf (file, " <requires>\n");
887 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
889 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
891 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
892 g_fprintf (file, " <interface name=\"");
894 g_fprintf (file, " <object name=\"");
895 write_type_name (namespace, req, file);
896 g_fprintf (file, "\" />\n");
897 g_base_info_unref (req);
899 g_fprintf (file, " </requires>\n");
902 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
904 GIFunctionInfo *function = g_interface_info_get_method (info, i);
905 write_function_info (namespace, function, file, 6);
906 g_base_info_unref ((GIBaseInfo *)function);
909 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
911 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
912 write_property_info (namespace, prop, file);
913 g_base_info_unref ((GIBaseInfo *)prop);
916 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
918 GISignalInfo *signal = g_interface_info_get_signal (info, i);
919 write_signal_info (namespace, signal, file);
920 g_base_info_unref ((GIBaseInfo *)signal);
923 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
925 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
926 write_vfunc_info (namespace, vfunc, file);
927 g_base_info_unref ((GIBaseInfo *)vfunc);
930 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
932 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
933 write_constant_info (namespace, constant, file, 6);
934 g_base_info_unref ((GIBaseInfo *)constant);
937 g_fprintf (file, " </interface>\n");
941 write_error_domain_info (const gchar *namespace,
942 GIErrorDomainInfo *info,
946 const gchar *name, *quark;
948 name = g_base_info_get_name ((GIBaseInfo *)info);
949 quark = g_error_domain_info_get_quark (info);
950 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
952 " <errordomain name=\"%s\" get-quark=\"%s\" codes=\"",
954 write_type_name (namespace, enum_, file);
955 g_fprintf (file, "\" />\n");
956 g_base_info_unref (enum_);
960 write_union_info (const gchar *namespace,
965 const gchar *type_name;
966 const gchar *type_init;
970 name = g_base_info_get_name ((GIBaseInfo *)info);
971 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
973 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
974 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
976 g_fprintf (file, " <union name=\"%s\"", name);
979 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
982 g_fprintf (file, " deprecated=\"1\"");
984 g_fprintf (file, ">\n");
986 if (g_union_info_is_discriminated (info))
991 offset = g_union_info_get_discriminator_offset (info);
992 type = g_union_info_get_discriminator_type (info);
994 g_fprintf (file, " <discriminator offset=\"%d\" type=\"", offset);
995 write_type_info (namespace, type, file);
996 g_fprintf (file, "\" />\n");
997 g_base_info_unref ((GIBaseInfo *)type);
1000 for (i = 0; i < g_union_info_get_n_fields (info); i++)
1002 GIFieldInfo *field = g_union_info_get_field (info, i);
1003 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
1004 write_field_info (namespace, field, constant, file);
1005 g_base_info_unref ((GIBaseInfo *)field);
1007 g_base_info_unref ((GIBaseInfo *)constant);
1010 for (i = 0; i < g_union_info_get_n_methods (info); i++)
1012 GIFunctionInfo *function = g_union_info_get_method (info, i);
1013 write_function_info (namespace, function, file, 6);
1014 g_base_info_unref ((GIBaseInfo *)function);
1017 g_fprintf (file, " </union>\n");
1021 write_repository (GIRepository *repository,
1022 gboolean needs_prefix)
1029 namespaces = g_irepository_get_namespaces (repository);
1038 filename = g_strdup_printf ("%s-%s", namespaces[0], output);
1040 filename = g_strdup (output);
1041 file = g_fopen (filename, "w");
1045 g_fprintf (stderr, "failed to open '%s': %s\n",
1046 filename, g_strerror (errno));
1055 g_fprintf (file, "<?xml version=\"1.0\"?>\n");
1056 g_fprintf (file, "<repository version=\"1.0\"\n"
1057 " xmlns=\"http://www.gtk.org/introspection/core/1.0\"\n"
1058 " xmlns:c=\"http://www.gtk.org/introspection/c/1.0\"\n"
1059 " xmlns:glib=\"http://www.gtk.org/introspection/glib/1.0\">\n");
1061 for (i = 0; namespaces[i]; i++)
1063 const gchar *shared_library;
1065 shared_library = g_irepository_get_shared_library (repository, ns);
1067 g_fprintf (file, " <namespace name=\"%s\" shared-library=\"%s\">\n",
1068 ns, shared_library);
1070 g_fprintf (file, " <namespace name=\"%s\">\n", ns);
1072 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1074 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1075 switch (g_base_info_get_type (info))
1077 case GI_INFO_TYPE_FUNCTION:
1078 write_function_info (ns, (GIFunctionInfo *)info, file, 4);
1081 case GI_INFO_TYPE_CALLBACK:
1082 write_callback_info (ns, (GICallbackInfo *)info, file, 4);
1085 case GI_INFO_TYPE_STRUCT:
1086 case GI_INFO_TYPE_BOXED:
1087 write_struct_info (ns, (GIStructInfo *)info, file);
1090 case GI_INFO_TYPE_UNION:
1091 write_union_info (ns, (GIUnionInfo *)info, file);
1094 case GI_INFO_TYPE_ENUM:
1095 case GI_INFO_TYPE_FLAGS:
1096 write_enum_info (ns, (GIEnumInfo *)info, file);
1099 case GI_INFO_TYPE_CONSTANT:
1100 write_constant_info (ns, (GIConstantInfo *)info, file, 4);
1103 case GI_INFO_TYPE_OBJECT:
1104 write_object_info (ns, (GIObjectInfo *)info, file);
1107 case GI_INFO_TYPE_INTERFACE:
1108 write_interface_info (ns, (GIInterfaceInfo *)info, file);
1111 case GI_INFO_TYPE_ERROR_DOMAIN:
1112 write_error_domain_info (ns, (GIErrorDomainInfo *)info, file);
1116 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1119 g_base_info_unref (info);
1122 g_fprintf (file, " </namespace>\n");
1125 g_fprintf (file, "</repository>\n");
1130 g_strfreev (namespaces);
1133 static const guchar *
1134 load_typelib (const gchar *filename,
1139 gsize *typelib_size;
1142 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1143 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &typelib))
1145 g_printerr ("Could not load typelib from '%s': %s\n",
1146 filename, g_module_error ());
1150 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &typelib_size))
1152 g_printerr ("Could not load typelib from '%s': %s\n",
1153 filename, g_module_error ());
1157 *len = *typelib_size;
1166 main (int argc, char *argv[])
1168 gboolean raw = FALSE;
1169 gchar **input = NULL;
1170 GOptionContext *context;
1171 GError *error = NULL;
1172 gboolean needs_prefix;
1175 GOptionEntry options[] =
1177 { "raw", 0, 0, G_OPTION_ARG_NONE, &raw, "handle raw typelib", NULL },
1178 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1179 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1185 g_typelib_check_sanity ();
1187 context = g_option_context_new ("");
1188 g_option_context_add_main_entries (context, options, NULL);
1189 g_option_context_parse (context, &argc, &argv, &error);
1193 g_fprintf (stderr, "no input files\n");
1198 for (i = 0; input[i]; i++)
1200 GModule *dlhandle = NULL;
1201 const guchar *typelib;
1206 if (!g_file_get_contents (input[i], (gchar **)&typelib, &len, &error))
1208 g_fprintf (stderr, "failed to read '%s': %s\n",
1209 input[i], error->message);
1210 g_clear_error (&error);
1216 typelib = load_typelib (input[i], &dlhandle, &len);
1219 g_fprintf (stderr, "failed to load typelib from '%s'\n",
1225 if (input[i + 1] && output)
1226 needs_prefix = TRUE;
1228 needs_prefix = FALSE;
1230 data = g_typelib_new_from_const_memory (typelib, len);
1232 GError *error = NULL;
1233 if (!g_typelib_validate (data, &error)) {
1234 g_printerr ("typelib not valid: %s\n", error->message);
1235 g_clear_error (&error);
1238 g_irepository_register (g_irepository_get_default (), data);
1239 write_repository (g_irepository_get_default (), needs_prefix);
1240 g_irepository_unregister (g_irepository_get_default (),
1241 g_typelib_get_namespace (data));
1245 g_module_close (dlhandle);
1249 /* when writing to stdout, stop after the first module */
1250 if (input[i + 1] && !output)
1252 g_fprintf (stderr, "warning, %d modules omitted\n",
1253 g_strv_length (input) - 1);