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);
209 g_fprintf (file, "offset=\"%d\" ", offset);
211 g_fprintf (file, "type=\"");
213 type = g_field_info_get_type (info);
214 write_type_info (namespace, type, file);
215 g_base_info_unref ((GIBaseInfo *)type);
217 g_fprintf (file, "\"");
221 g_fprintf (file, " branch=\"");
222 type = g_constant_info_get_type (branch);
223 g_constant_info_get_value (branch, &value);
224 write_constant_value (namespace, type, &value, file);
225 g_fprintf (file, "\"");
228 g_fprintf (file," />\n");
232 write_callable_info (const gchar *namespace,
233 GICallableInfo *info,
240 g_fprintf (file, "%*s <return-type type=\"", indent, "");
242 type = g_callable_info_get_return_type (info);
243 write_type_info (namespace, type, file);
245 g_fprintf (file, "\"");
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 ();
264 g_base_info_unref ((GIBaseInfo *)type);
265 if (g_callable_info_may_return_null (info))
266 g_fprintf (file, " null-ok=\"1\"");
268 g_fprintf (file, " />\n");
270 if (g_callable_info_get_n_args (info) > 0)
272 g_fprintf (file, "%*s <parameters>\n", indent, "");
273 for (i = 0; i < g_callable_info_get_n_args (info); i++)
275 GIArgInfo *arg = g_callable_info_get_arg (info, i);
277 g_fprintf (file, "%*s <parameter name=\"%s\" type=\"",
278 indent, "", g_base_info_get_name ((GIBaseInfo *) arg));
280 type = g_arg_info_get_type (arg);
281 write_type_info (namespace, type, file);
282 g_fprintf (file, "\"");
284 if (g_type_info_is_pointer (type))
286 switch (g_arg_info_get_ownership_transfer (arg))
288 case GI_TRANSFER_NOTHING:
289 g_fprintf (file, " transfer=\"none\"");
291 case GI_TRANSFER_CONTAINER:
292 g_fprintf (file, " transfer=\"shallow\"");
294 case GI_TRANSFER_EVERYTHING:
295 g_fprintf (file, " transfer=\"full\"");
298 g_assert_not_reached ();
301 g_base_info_unref ((GIBaseInfo *)type);
303 g_fprintf (file, " direction=\"");
304 switch (g_arg_info_get_direction (arg))
306 case GI_DIRECTION_IN:
307 g_fprintf (file, "in");
309 case GI_DIRECTION_OUT:
310 g_fprintf (file, "out");
312 case GI_DIRECTION_INOUT:
313 g_fprintf (file, "inout");
316 g_fprintf (file, "\"");
318 if (g_arg_info_may_be_null (arg))
319 g_fprintf (file, " null-ok=\"1\"");
321 if (g_arg_info_is_dipper (arg))
322 g_fprintf (file, " dipper=\"1\"");
324 if (g_arg_info_is_return_value (arg))
325 g_fprintf (file, " retval=\"1\"");
327 if (g_arg_info_is_optional (arg))
328 g_fprintf (file, " optional=\"1\"");
330 g_fprintf (file, " />\n");
332 g_base_info_unref ((GIBaseInfo *)arg);
335 g_fprintf (file, "%*s </parameters>\n", indent, "");
340 write_function_info (const gchar *namespace,
341 GIFunctionInfo *info,
345 GIFunctionInfoFlags flags;
351 flags = g_function_info_get_flags (info);
352 name = g_base_info_get_name ((GIBaseInfo *)info);
353 symbol = g_function_info_get_symbol (info);
354 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
356 if (flags & GI_FUNCTION_IS_CONSTRUCTOR)
358 else if (flags & GI_FUNCTION_IS_METHOD)
363 g_fprintf (file, "%*s<%s name=\"%s\" symbol=\"%s\"",
364 indent, "", tag, name, symbol);
366 if (flags & GI_FUNCTION_IS_SETTER)
367 g_fprintf (file, " type=\"setter\"");
368 else if (flags & GI_FUNCTION_IS_GETTER)
369 g_fprintf (file, " type=\"getter\"");
372 g_fprintf (file, " deprecated=\"1\"");
374 g_fprintf (file, ">\n");
375 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
376 g_fprintf (file, "%*s</%s>\n", indent, "", tag);
380 write_callback_info (const gchar *namespace,
381 GICallbackInfo *info,
388 name = g_base_info_get_name ((GIBaseInfo *)info);
389 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
391 g_fprintf (file, "%*s<callback name=\"%s\"", indent, "", name);
394 g_fprintf (file, " deprecated=\"1\"");
396 g_fprintf (file, ">\n");
397 write_callable_info (namespace, (GICallableInfo*)info, file, indent);
398 g_fprintf (file, "%*s</callback>\n", indent, "");
402 write_struct_info (const gchar *namespace,
407 const gchar *type_name;
408 const gchar *type_init;
412 name = g_base_info_get_name ((GIBaseInfo *)info);
413 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
415 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
417 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
418 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
420 g_fprintf (file, " <boxed name=\"%s\" type-name=\"%s\" get-type=\"%s\"", name, type_name, type_init);
423 g_fprintf (file, " <struct name=\"%s\"", name);
426 g_fprintf (file, " deprecated=\"1\"");
428 g_fprintf (file, ">\n");
430 for (i = 0; i < g_struct_info_get_n_fields (info); i++)
432 GIFieldInfo *field = g_struct_info_get_field (info, i);
433 write_field_info (namespace, field, NULL, file);
434 g_base_info_unref ((GIBaseInfo *)field);
437 for (i = 0; i < g_struct_info_get_n_methods (info); i++)
439 GIFunctionInfo *function = g_struct_info_get_method (info, i);
440 write_function_info (namespace, function, file, 6);
441 g_base_info_unref ((GIBaseInfo *)function);
444 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_BOXED)
445 g_fprintf (file, " </boxed>\n");
447 g_fprintf (file, " </struct>\n");
451 write_value_info (const gchar *namespace,
459 name = g_base_info_get_name ((GIBaseInfo *)info);
460 value = g_value_info_get_value (info);
461 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
463 g_fprintf (file, " <member name=\"%s\" value=\"%ld\"", name, value);
466 g_fprintf (file, " deprecated=\"1\"");
468 g_fprintf (file, " />\n");
472 write_constant_value (const gchar *namespace,
477 switch (g_type_info_get_tag (type))
479 case GI_TYPE_TAG_BOOLEAN:
480 g_fprintf (file, "%d", value->v_boolean);
482 case GI_TYPE_TAG_INT8:
483 g_fprintf (file, "%d", value->v_int8);
485 case GI_TYPE_TAG_UINT8:
486 g_fprintf (file, "%d", value->v_uint8);
488 case GI_TYPE_TAG_INT16:
489 g_fprintf (file, "%" G_GINT16_FORMAT, value->v_int16);
491 case GI_TYPE_TAG_UINT16:
492 g_fprintf (file, "%" G_GUINT16_FORMAT, value->v_uint16);
494 case GI_TYPE_TAG_INT32:
495 g_fprintf (file, "%" G_GINT32_FORMAT, value->v_int32);
497 case GI_TYPE_TAG_UINT32:
498 g_fprintf (file, "%" G_GUINT32_FORMAT, value->v_uint32);
500 case GI_TYPE_TAG_INT64:
501 g_fprintf (file, "%" G_GINT64_FORMAT, value->v_int64);
503 case GI_TYPE_TAG_UINT64:
504 g_fprintf (file, "%" G_GUINT64_FORMAT, value->v_uint64);
506 case GI_TYPE_TAG_INT:
507 g_fprintf (file, "%d", value->v_int);
509 case GI_TYPE_TAG_UINT:
510 g_fprintf (file, "%d", value->v_uint);
512 case GI_TYPE_TAG_LONG:
513 g_fprintf (file, "%ld", value->v_long);
515 case GI_TYPE_TAG_ULONG:
516 g_fprintf (file, "%ld", value->v_ulong);
518 case GI_TYPE_TAG_SSIZE:
519 g_fprintf (file, "%zd", value->v_ssize);
521 case GI_TYPE_TAG_SIZE:
522 g_fprintf (file, "%zd", value->v_size);
524 case GI_TYPE_TAG_FLOAT:
525 g_fprintf (file, "%f", value->v_float);
527 case GI_TYPE_TAG_DOUBLE:
528 g_fprintf (file, "%f", value->v_double);
530 case GI_TYPE_TAG_UTF8:
531 case GI_TYPE_TAG_FILENAME:
532 g_fprintf (file, "%s", value->v_string);
535 g_assert_not_reached ();
540 write_constant_info (const gchar *namespace,
541 GIConstantInfo *info,
550 name = g_base_info_get_name ((GIBaseInfo *)info);
551 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
553 g_fprintf (file, "%*s<constant name=\"%s\" type=\"", indent, "", name);
555 type = g_constant_info_get_type (info);
556 write_type_info (namespace, type, file);
557 g_fprintf (file, "\" value=\"");
559 g_constant_info_get_value (info, &value);
560 write_constant_value (namespace, type, &value, file);
561 g_fprintf (file, "\" />\n");
563 g_base_info_unref ((GIBaseInfo *)type);
568 write_enum_info (const gchar *namespace,
573 const gchar *type_name;
574 const gchar *type_init;
578 name = g_base_info_get_name ((GIBaseInfo *)info);
579 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
581 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
582 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
584 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
585 g_fprintf (file, " <enum ");
587 g_fprintf (file, " <flags ");
588 g_fprintf (file, "name=\"%s\"", name);
591 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
594 g_fprintf (file, " deprecated=\"1\"");
596 g_fprintf (file, ">\n");
598 for (i = 0; i < g_enum_info_get_n_values (info); i++)
600 GIValueInfo *value = g_enum_info_get_value (info, i);
601 write_value_info (namespace, value, file);
602 g_base_info_unref ((GIBaseInfo *)value);
605 if (g_base_info_get_type ((GIBaseInfo *)info) == GI_INFO_TYPE_ENUM)
606 g_fprintf (file, " </enum>\n");
608 g_fprintf (file, " </flags>\n");
612 write_signal_info (const gchar *namespace,
620 name = g_base_info_get_name ((GIBaseInfo *)info);
621 flags = g_signal_info_get_flags (info);
622 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
624 g_fprintf (file, " <signal name=\"%s\"", name);
627 g_fprintf (file, " deprecated=\"1\"");
629 if (flags & G_SIGNAL_RUN_FIRST)
630 g_fprintf (file, " when=\"FIRST\"");
631 else if (flags & G_SIGNAL_RUN_LAST)
632 g_fprintf (file, " when=\"LAST\"");
633 else if (flags & G_SIGNAL_RUN_CLEANUP)
634 g_fprintf (file, " when=\"CLEANUP\"");
636 if (flags & G_SIGNAL_NO_RECURSE)
637 g_fprintf (file, " no-recurse=\"1\"");
639 if (flags & G_SIGNAL_DETAILED)
640 g_fprintf (file, " detailed=\"1\"");
642 if (flags & G_SIGNAL_ACTION)
643 g_fprintf (file, " action=\"1\"");
645 if (flags & G_SIGNAL_NO_HOOKS)
646 g_fprintf (file, " no-hooks=\"1\"");
648 g_fprintf (file, ">\n");
650 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
651 g_fprintf (file, " </signal>\n");
655 write_vfunc_info (const gchar *namespace,
659 GIVFuncInfoFlags flags;
664 name = g_base_info_get_name ((GIBaseInfo *)info);
665 flags = g_vfunc_info_get_flags (info);
666 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
667 offset = g_vfunc_info_get_offset (info);
669 g_fprintf (file, " <vfunc name=\"%s\"", name);
672 g_fprintf (file, " deprecated=\"1\"");
674 if (flags & GI_VFUNC_MUST_CHAIN_UP)
675 g_fprintf (file, " must-chain-up=\"1\"");
677 if (flags & GI_VFUNC_MUST_OVERRIDE)
678 g_fprintf (file, " override=\"always\"");
679 else if (flags & GI_VFUNC_MUST_NOT_OVERRIDE)
680 g_fprintf (file, " override=\"never\"");
682 g_fprintf (file, " offset=\"%d\"", offset);
683 g_fprintf (file, ">\n");
685 write_callable_info (namespace, (GICallableInfo*)info, file, 6);
686 g_fprintf (file, " </vfunc>\n");
690 write_property_info (const gchar *namespace,
691 GIPropertyInfo *info,
699 name = g_base_info_get_name ((GIBaseInfo *)info);
700 flags = g_property_info_get_flags (info);
701 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
703 g_fprintf (file, " <property name=\"%s\"", name);
706 g_fprintf (file, " deprecated=\"1\"");
708 if (flags & G_PARAM_READABLE)
709 g_fprintf (file, " readable=\"1\"");
711 g_fprintf (file, " readable=\"0\"");
713 if (flags & G_PARAM_WRITABLE)
714 g_fprintf (file, " writable=\"1\"");
716 g_fprintf (file, " writable=\"0\"");
718 if (flags & G_PARAM_CONSTRUCT)
719 g_fprintf (file, " construct=\"1\"");
721 if (flags & G_PARAM_CONSTRUCT_ONLY)
722 g_fprintf (file, " construct-only=\"1\"");
724 type = g_property_info_get_type (info);
725 g_fprintf (file, " type=\"");
726 write_type_info (namespace, type, file);
727 g_fprintf (file, "\"");
729 g_fprintf (file, " />\n");
733 write_object_info (const gchar *namespace,
738 const gchar *type_name;
739 const gchar *type_init;
744 name = g_base_info_get_name ((GIBaseInfo *)info);
745 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
747 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
748 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
749 g_fprintf (file, " <object name=\"%s\"", name);
751 pnode = g_object_info_get_parent (info);
754 g_fprintf (file, " parent=\"");
755 write_type_name (namespace, (GIBaseInfo *)pnode, file);
756 g_fprintf (file, "\"" );
757 g_base_info_unref ((GIBaseInfo *)pnode);
760 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
763 g_fprintf (file, " deprecated=\"1\"");
765 g_fprintf (file, ">\n");
767 if (g_object_info_get_n_interfaces (info) > 0)
769 g_fprintf (file, " <implements>\n");
770 for (i = 0; i < g_object_info_get_n_interfaces (info); i++)
772 GIInterfaceInfo *imp = g_object_info_get_interface (info, i);
773 g_fprintf (file, " <interface name=\"");
774 write_type_name (namespace, (GIBaseInfo*)imp, file);
775 g_fprintf (file,"\" />\n");
776 g_base_info_unref ((GIBaseInfo*)imp);
778 g_fprintf (file, " </implements>\n");
781 for (i = 0; i < g_object_info_get_n_fields (info); i++)
783 GIFieldInfo *field = g_object_info_get_field (info, i);
784 write_field_info (namespace, field, NULL, file);
785 g_base_info_unref ((GIBaseInfo *)field);
788 for (i = 0; i < g_object_info_get_n_methods (info); i++)
790 GIFunctionInfo *function = g_object_info_get_method (info, i);
791 write_function_info (namespace, function, file, 6);
792 g_base_info_unref ((GIBaseInfo *)function);
795 for (i = 0; i < g_object_info_get_n_properties (info); i++)
797 GIPropertyInfo *prop = g_object_info_get_property (info, i);
798 write_property_info (namespace, prop, file);
799 g_base_info_unref ((GIBaseInfo *)prop);
802 for (i = 0; i < g_object_info_get_n_signals (info); i++)
804 GISignalInfo *signal = g_object_info_get_signal (info, i);
805 write_signal_info (namespace, signal, file);
806 g_base_info_unref ((GIBaseInfo *)signal);
809 for (i = 0; i < g_object_info_get_n_vfuncs (info); i++)
811 GIVFuncInfo *vfunc = g_object_info_get_vfunc (info, i);
812 write_vfunc_info (namespace, vfunc, file);
813 g_base_info_unref ((GIBaseInfo *)vfunc);
816 for (i = 0; i < g_object_info_get_n_constants (info); i++)
818 GIConstantInfo *constant = g_object_info_get_constant (info, i);
819 write_constant_info (namespace, constant, file, 6);
820 g_base_info_unref ((GIBaseInfo *)constant);
823 g_fprintf (file, " </object>\n");
827 write_interface_info (const gchar *namespace,
828 GIInterfaceInfo *info,
832 const gchar *type_name;
833 const gchar *type_init;
837 name = g_base_info_get_name ((GIBaseInfo *)info);
838 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
840 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
841 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
842 g_fprintf (file, " <interface name=\"%s\" type-name=\"%s\" get-type=\"%s\"",
843 name, type_name, type_init);
846 g_fprintf (file, " deprecated=\"1\"");
848 g_fprintf (file, ">\n");
850 if (g_interface_info_get_n_prerequisites (info) > 0)
852 g_fprintf (file, " <requires>\n");
853 for (i = 0; i < g_interface_info_get_n_prerequisites (info); i++)
855 GIBaseInfo *req = g_interface_info_get_prerequisite (info, i);
857 if (g_base_info_get_type (req) == GI_INFO_TYPE_INTERFACE)
858 g_fprintf (file, " <interface name=\"");
860 g_fprintf (file, " <object name=\"");
861 write_type_name (namespace, req, file);
862 g_fprintf (file, "\" />\n");
863 g_base_info_unref (req);
865 g_fprintf (file, " </requires>\n");
868 for (i = 0; i < g_interface_info_get_n_methods (info); i++)
870 GIFunctionInfo *function = g_interface_info_get_method (info, i);
871 write_function_info (namespace, function, file, 6);
872 g_base_info_unref ((GIBaseInfo *)function);
875 for (i = 0; i < g_interface_info_get_n_properties (info); i++)
877 GIPropertyInfo *prop = g_interface_info_get_property (info, i);
878 write_property_info (namespace, prop, file);
879 g_base_info_unref ((GIBaseInfo *)prop);
882 for (i = 0; i < g_interface_info_get_n_signals (info); i++)
884 GISignalInfo *signal = g_interface_info_get_signal (info, i);
885 write_signal_info (namespace, signal, file);
886 g_base_info_unref ((GIBaseInfo *)signal);
889 for (i = 0; i < g_interface_info_get_n_vfuncs (info); i++)
891 GIVFuncInfo *vfunc = g_interface_info_get_vfunc (info, i);
892 write_vfunc_info (namespace, vfunc, file);
893 g_base_info_unref ((GIBaseInfo *)vfunc);
896 for (i = 0; i < g_interface_info_get_n_constants (info); i++)
898 GIConstantInfo *constant = g_interface_info_get_constant (info, i);
899 write_constant_info (namespace, constant, file, 6);
900 g_base_info_unref ((GIBaseInfo *)constant);
903 g_fprintf (file, " </interface>\n");
907 write_error_domain_info (const gchar *namespace,
908 GIErrorDomainInfo *info,
912 const gchar *name, *quark;
914 name = g_base_info_get_name ((GIBaseInfo *)info);
915 quark = g_error_domain_info_get_quark (info);
916 enum_ = (GIBaseInfo *)g_error_domain_info_get_codes (info);
918 " <errordomain name=\"%s\" get-quark=\"%s\" codes=\"",
920 write_type_name (namespace, enum_, file);
921 g_fprintf (file, "\" />\n");
922 g_base_info_unref (enum_);
926 write_union_info (const gchar *namespace,
931 const gchar *type_name;
932 const gchar *type_init;
936 name = g_base_info_get_name ((GIBaseInfo *)info);
937 deprecated = g_base_info_is_deprecated ((GIBaseInfo *)info);
939 type_name = g_registered_type_info_get_type_name ((GIRegisteredTypeInfo*)info);
940 type_init = g_registered_type_info_get_type_init ((GIRegisteredTypeInfo*)info);
942 g_fprintf (file, " <union name=\"%s\"", name);
945 g_fprintf (file, " type-name=\"%s\" get-type=\"%s\"", type_name, type_init);
948 g_fprintf (file, " deprecated=\"1\"");
950 g_fprintf (file, ">\n");
952 if (g_union_info_is_discriminated (info))
957 offset = g_union_info_get_discriminator_offset (info);
958 type = g_union_info_get_discriminator_type (info);
960 g_fprintf (file, " <discriminator offset=\"%d\" type=\"", offset);
961 write_type_info (namespace, type, file);
962 g_fprintf (file, "\" />\n");
963 g_base_info_unref ((GIBaseInfo *)type);
966 for (i = 0; i < g_union_info_get_n_fields (info); i++)
968 GIFieldInfo *field = g_union_info_get_field (info, i);
969 GIConstantInfo *constant = g_union_info_get_discriminator (info, i);
970 write_field_info (namespace, field, constant, file);
971 g_base_info_unref ((GIBaseInfo *)field);
973 g_base_info_unref ((GIBaseInfo *)constant);
976 for (i = 0; i < g_union_info_get_n_methods (info); i++)
978 GIFunctionInfo *function = g_union_info_get_method (info, i);
979 write_function_info (namespace, function, file, 6);
980 g_base_info_unref ((GIBaseInfo *)function);
983 g_fprintf (file, " </union>\n");
987 write_repository (GIRepository *repository,
988 gboolean needs_prefix)
995 namespaces = g_irepository_get_namespaces (repository);
1004 filename = g_strdup_printf ("%s-%s", namespaces[0], output);
1006 filename = g_strdup (output);
1007 file = g_fopen (filename, "w");
1011 g_fprintf (stderr, "failed to open '%s': %s\n",
1012 filename, g_strerror (errno));
1021 g_fprintf (file, "<?xml version=\"1.0\"?>\n");
1022 g_fprintf (file, "<api version=\"1.0\">\n");
1024 for (i = 0; namespaces[i]; i++)
1026 const gchar *shared_library;
1028 shared_library = g_irepository_get_shared_library (repository, ns);
1030 g_fprintf (file, " <namespace name=\"%s\" shared-library=\"%s\">\n",
1031 ns, shared_library);
1033 g_fprintf (file, " <namespace name=\"%s\">\n", ns);
1035 for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
1037 GIBaseInfo *info = g_irepository_get_info (repository, ns, j);
1038 switch (g_base_info_get_type (info))
1040 case GI_INFO_TYPE_FUNCTION:
1041 write_function_info (ns, (GIFunctionInfo *)info, file, 4);
1044 case GI_INFO_TYPE_CALLBACK:
1045 write_callback_info (ns, (GICallbackInfo *)info, file, 4);
1048 case GI_INFO_TYPE_STRUCT:
1049 case GI_INFO_TYPE_BOXED:
1050 write_struct_info (ns, (GIStructInfo *)info, file);
1053 case GI_INFO_TYPE_UNION:
1054 write_union_info (ns, (GIUnionInfo *)info, file);
1057 case GI_INFO_TYPE_ENUM:
1058 case GI_INFO_TYPE_FLAGS:
1059 write_enum_info (ns, (GIEnumInfo *)info, file);
1062 case GI_INFO_TYPE_CONSTANT:
1063 write_constant_info (ns, (GIConstantInfo *)info, file, 4);
1066 case GI_INFO_TYPE_OBJECT:
1067 write_object_info (ns, (GIObjectInfo *)info, file);
1070 case GI_INFO_TYPE_INTERFACE:
1071 write_interface_info (ns, (GIInterfaceInfo *)info, file);
1074 case GI_INFO_TYPE_ERROR_DOMAIN:
1075 write_error_domain_info (ns, (GIErrorDomainInfo *)info, file);
1079 g_error ("unknown info type %d\n", g_base_info_get_type (info));
1082 g_base_info_unref (info);
1085 g_fprintf (file, " </namespace>\n");
1088 g_fprintf (file, "</api>\n");
1093 g_strfreev (namespaces);
1096 static const guchar *
1097 load_metadata (const gchar *filename,
1102 gsize *metadata_size;
1105 handle = g_module_open (filename, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
1106 if (!g_module_symbol (handle, "_G_TYPELIB", (gpointer *) &metadata))
1108 g_printerr ("Could not load metadata from '%s': %s\n",
1109 filename, g_module_error ());
1113 if (!g_module_symbol (handle, "_G_TYPELIB_SIZE", (gpointer *) &metadata_size))
1115 g_printerr ("Could not load metadata from '%s': %s\n",
1116 filename, g_module_error ());
1120 *len = *metadata_size;
1129 main (int argc, char *argv[])
1131 gboolean raw = FALSE;
1132 gchar **input = NULL;
1133 GOptionContext *context;
1134 GError *error = NULL;
1135 gboolean needs_prefix;
1138 GOptionEntry options[] =
1140 { "raw", 0, 0, G_OPTION_ARG_NONE, &raw, "handle raw metadata", NULL },
1141 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
1142 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
1148 g_typelib_check_sanity ();
1150 context = g_option_context_new ("");
1151 g_option_context_add_main_entries (context, options, NULL);
1152 g_option_context_parse (context, &argc, &argv, &error);
1156 g_fprintf (stderr, "no input files\n");
1161 for (i = 0; input[i]; i++)
1163 GModule *dlhandle = NULL;
1164 const guchar *metadata;
1169 if (!g_file_get_contents (input[i], (gchar **)&metadata, &len, &error))
1171 g_fprintf (stderr, "failed to read '%s': %s\n",
1172 input[i], error->message);
1173 g_clear_error (&error);
1179 metadata = load_metadata (input[i], &dlhandle, &len);
1182 g_fprintf (stderr, "failed to load metadata from '%s'\n",
1188 if (input[i + 1] && output)
1189 needs_prefix = TRUE;
1191 needs_prefix = FALSE;
1193 data = g_typelib_new_from_const_memory (metadata, len);
1195 GError *error = NULL;
1196 if (!g_typelib_validate (data, &error)) {
1197 g_printerr ("metadata not valid: %s\n", error->message);
1198 g_clear_error (&error);
1201 g_irepository_register (g_irepository_get_default (), data);
1202 write_repository (g_irepository_get_default (), needs_prefix);
1203 g_irepository_unregister (g_irepository_get_default (),
1204 g_typelib_get_namespace (data));
1208 g_module_close (dlhandle);
1212 /* when writing to stdout, stop after the first module */
1213 if (input[i + 1] && !output)
1215 g_fprintf (stderr, "warning, %d modules omitted\n",
1216 g_strv_length (input) - 1);