Skip to content
Snippets Groups Projects
gstdoc-scangobj 45.3 KiB
Newer Older
#!/usr/bin/env perl
# -*- cperl -*-
#
# gtk-doc - GTK DocBook documentation generator.
# Copyright (C) 1998  Damon Chaplin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

#
# This gets information about object hierarchies and signals
# by compiling a small C program. CFLAGS and LDFLAGS must be
# set appropriately before running this script.
#

use Getopt::Long;

my $GTK_DOC_PREFIX=`pkg-config --variable prefix gtk-doc`;
if ($GTK_DOC_PREFIX) {
  chomp $GTK_DOC_PREFIX;
  #print "Adding $GTK_DOC_PREFIX/share/gtk-doc/data to \@INC\n";
  unshift @INC, "$GTK_DOC_PREFIX/share/gtk-doc/data";
} else {
  unshift @INC, '/usr/share/gtk-doc/data';
}
require "gtkdoc-common.pl";

# Options

# name of documentation module
my $MODULE;
my $OUTPUT_DIR;
my $INSPECT_DIR;
my $VERBOSE;
my $PRINT_VERSION;
my $PRINT_HELP;
my $TYPE_INIT_FUNC="g_type_init ()";

# --nogtkinit is deprecated, as it is the default now anyway.
%optctl = (module => \$MODULE,
           source => \$SOURCE,
	   types => \$TYPES_FILE,
	   nogtkinit => \$NO_GTK_INIT,
	   'type-init-func' => \$TYPE_INIT_FUNC,
	   'output-dir' => \$OUTPUT_DIR,
	   'inspect-dir' => \$INSPECT_DIR,
	   'verbose' => \$VERBOSE,
	   'version' => \$PRINT_VERSION,
	   'help' => \$PRINT_HELP);

GetOptions(\%optctl, "module=s", "source=s", "types:s", "output-dir:s", "inspect-dir:s", "nogtkinit", "type-init-func:s", "verbose", "version", "help");

if ($NO_GTK_INIT) {
  # Do nothing. This just avoids a warning.
  # the option is not used anymore
}

if ($PRINT_VERSION) {
    print "1.5\n";
    exit 0;
}

if (!$MODULE) {
    $PRINT_HELP = 1;
}

if ($PRINT_HELP) {
    print <<EOF;
gstdoc-scangobj version 1.5 - introspect gstreamer-plugins

--module=MODULE_NAME          Name of the doc module being parsed
--source=SOURCE_NAME          Name of the source module for plugins
--types=FILE                  The name of the file to store the types in
--type-init-func=FUNC         The init function to call instead of g_type_init()
--output-dir=DIRNAME          The directory where the results are stored
--inspect-dir=DIRNAME         The directory where the plugin inspect data is stored
--verbose                     Print extra output while processing
--version                     Print the version of this program
--help                        Print this help
EOF
    exit 0;
}

$OUTPUT_DIR = $OUTPUT_DIR ? $OUTPUT_DIR : ".";

$TYPES_FILE = $TYPES_FILE ? $TYPES_FILE : "$OUTPUT_DIR/$MODULE.types";

open (TYPES, $TYPES_FILE) || die "Cannot open $TYPES_FILE: $!\n";
open (OUTPUT, ">$MODULE-scan.c") || die "Cannot open $MODULE-scan.c: $!\n";

my $old_signals_filename = "$OUTPUT_DIR/$MODULE.signals";
my $new_signals_filename = "$OUTPUT_DIR/$MODULE.signals.new";
my $old_hierarchy_filename = "$OUTPUT_DIR/$MODULE.hierarchy";
my $new_hierarchy_filename = "$OUTPUT_DIR/$MODULE.hierarchy.new";
my $old_interfaces_filename = "$OUTPUT_DIR/$MODULE.interfaces";
my $new_interfaces_filename = "$OUTPUT_DIR/$MODULE.interfaces.new";
my $old_prerequisites_filename = "$OUTPUT_DIR/$MODULE.prerequisites";
my $new_prerequisites_filename = "$OUTPUT_DIR/$MODULE.prerequisites.new";
my $old_args_filename = "$OUTPUT_DIR/$MODULE.args";
my $new_args_filename = "$OUTPUT_DIR/$MODULE.args.new";

my $debug_log="g_message";
if (!defined($VERBOSE) or $VERBOSE eq "0") {
    $debug_log="//$debug_log";
}

# write a C program to scan the types

$includes = "";
@types = ();
@impl_types = ();

for (<TYPES>) {
    if (/^#include/) {
	$includes .= $_;
    } elsif (/^%/) {
	next;
    } elsif (/^\s*$/) {
	next;
    } elsif (/^type:(.*)$/) {
	$t = $1;
        chomp $t;
	push @impl_types, $t;
    } else {
	chomp;
	push @types, $_;
    }
}

$ntypes = @types + @impl_types + 1;

print OUTPUT <<EOT;

/* file generated by common/gstdoc-scangobj */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <gst/gst.h>
EOT

if ($includes) {
    print OUTPUT $includes;
} else {
    for (@types) {
        print OUTPUT "extern GType $_ (void);\n";
    }
}

print OUTPUT <<EOT;

#ifdef GTK_IS_WIDGET_CLASS
#include <gtk/gtkversion.h>
#endif

static GType *object_types = NULL;

static GString *xmlstr = NULL;

static const gchar*
xmlprint (gint indent, const gchar *tag, const gchar *data)
{
  const gchar indent_str[] = "                                               ";

  /* reset */
  g_string_truncate (xmlstr, 0);
  g_string_append_len (xmlstr, indent_str, MIN (indent, strlen (indent_str)));
  g_string_append_printf (xmlstr, "<%s>", tag);

  if (data) {
    gchar *s;

    s = g_markup_escape_text (data, -1);
    g_string_append (xmlstr, s);
    g_free (s);
  }

  g_string_append_printf (xmlstr, "</%s>\\n", tag);
  return xmlstr->str;
}

static gint
gst_feature_sort_compare (gconstpointer a, gconstpointer b)
{
  const gchar *name_a = gst_plugin_feature_get_name ((GstPluginFeature *) a);
  const gchar *name_b = gst_plugin_feature_get_name ((GstPluginFeature *) b);
  return strcmp (name_a, name_b);
}

static gint
static_pad_template_compare (gconstpointer a, gconstpointer b)
{
  GstStaticPadTemplate *spt_a = (GstStaticPadTemplate *) a;
  GstStaticPadTemplate *spt_b = (GstStaticPadTemplate *) b;

  /* we want SINK before SRC (enum is UNKNOWN, SRC, SINK) */
  if (spt_a->direction != spt_b->direction)
    return spt_b->direction - spt_a->direction;

  /* we want ALWAYS first, SOMETIMES second, REQUEST last
   * (enum is ALWAYS, SOMETIMES, REQUEST) */
  if (spt_a->presence != spt_b->presence)
    return spt_a->presence - spt_b->presence;

  return strcmp (spt_a->name_template, spt_b->name_template);
}

static GType *
get_object_types (void)
{
    gpointer g_object_class;
    GList *plugins = NULL;
    GList *factories = NULL;
    GList *l;
    GstElementFactory *factory = NULL;
    GType type;
    gint i = 0;
    gboolean reinspect;

    /* get a list of features from plugins in our source module */
    plugins = gst_registry_get_plugin_list (gst_registry_get ());

    xmlstr = g_string_new ("");

    reinspect = !g_file_test ("scanobj-build.stamp", G_FILE_TEST_EXISTS);

    while (plugins) {
      GList *features;
      GstPlugin *plugin;
      const gchar *source;
      FILE *inspect = NULL;
      gchar *inspect_name;

      plugin = (GstPlugin *) (plugins->data);
      plugins = g_list_next (plugins);
      source = gst_plugin_get_source (plugin);
      if (!source || strcmp (source, "$SOURCE") != 0) {
        continue;
      }

      /* skip static coreelements plugin with pipeline and bin element factory */
      if (gst_plugin_get_filename (plugin) == NULL)
        continue;

      $debug_log ("plugin: %s source: %s", gst_plugin_get_name (plugin), source);

      if (reinspect) {
        gchar *basename;

        inspect_name = g_strdup_printf ("$INSPECT_DIR" G_DIR_SEPARATOR_S "plugin-%s.xml",
            gst_plugin_get_name (plugin));
        inspect = fopen (inspect_name, "w");
        if (inspect == NULL) {
          g_error ("Could not open %s for writing: %s\\n", inspect_name,
              g_strerror (errno));
        }
        g_free (inspect_name);

		  basename = g_path_get_basename (gst_plugin_get_filename (plugin));

        /* output plugin data */
        fputs ("<plugin>\\n",inspect);
        fputs (xmlprint(2, "name", gst_plugin_get_name (plugin)),inspect);
        fputs (xmlprint(2, "description", gst_plugin_get_description (plugin)),inspect);
        fputs (xmlprint(2, "filename", gst_plugin_get_filename (plugin)),inspect);
        fputs (xmlprint(2, "basename", basename),inspect);
        fputs (xmlprint(2, "version", gst_plugin_get_version (plugin)),inspect);
        fputs (xmlprint(2, "license", gst_plugin_get_license (plugin)),inspect);
        fputs (xmlprint(2, "source", gst_plugin_get_source (plugin)),inspect);
        fputs (xmlprint(2, "package", gst_plugin_get_package (plugin)),inspect);
        fputs (xmlprint(2, "origin", gst_plugin_get_origin (plugin)),inspect);
        fputs ("  <elements>\\n", inspect);

		  g_free (basename);
      }

      features =
          gst_registry_get_feature_list_by_plugin (gst_registry_get (),
          gst_plugin_get_name (plugin));

      /* sort factories by feature->name */
      features = g_list_sort (features, gst_feature_sort_compare);

      while (features) {
        GstPluginFeature *feature;
        feature = GST_PLUGIN_FEATURE (features->data);
        feature = gst_plugin_feature_load (feature);
        if (!feature) {
          g_warning ("Could not load plugin feature %s",
                     gst_plugin_feature_get_name (feature));
        }

        if (GST_IS_ELEMENT_FACTORY (feature)) {
          const gchar *pad_dir[] = { "unknown","source","sink" };
          const gchar *pad_pres[] = { "always","sometimes","request" };
          GList *pads, *pad;

          $debug_log ("  feature: %s", gst_plugin_feature_get_name (feature));

          factory = GST_ELEMENT_FACTORY (feature);
          factories = g_list_prepend (factories, factory);

          if (reinspect) {
            /* output element data */
            fputs ("    <element>\\n", inspect);
            fputs (xmlprint(6, "name", gst_plugin_feature_get_name (feature)),inspect);
            fputs (xmlprint(6, "longname", gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_LONGNAME)),inspect);
            fputs (xmlprint(6, "class", gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS)),inspect);
            fputs (xmlprint(6, "description", gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_DESCRIPTION)),inspect);
            fputs (xmlprint(6, "author", gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_AUTHOR)),inspect);
            fputs ("      <pads>\\n", inspect);

            /* output pad-template data */
            pads = g_list_copy ((GList *) gst_element_factory_get_static_pad_templates (factory));
            pads = g_list_sort (pads, static_pad_template_compare);
            for (pad = pads; pad != NULL; pad = pad->next) {
              GstStaticPadTemplate *pt = pad->data;

              fputs ("        <caps>\\n", inspect);
              fputs (xmlprint(10, "name", pt->name_template),inspect);
              fputs (xmlprint(10, "direction", pad_dir[pt->direction]),inspect);
              fputs (xmlprint(10, "presence", pad_pres[pt->presence]),inspect);
              fputs (xmlprint(10, "details", pt->static_caps.string),inspect);
              fputs ("        </caps>\\n", inspect);
            }
            g_list_free (pads);
            fputs ("      </pads>\\n    </element>\\n", inspect);
          }
        }
        features = g_list_next (features);
      }

      if (reinspect) {
        fputs ("  </elements>\\n</plugin>", inspect);
        fclose (inspect);
      }
    }

    g_string_free (xmlstr, TRUE);

    $debug_log ("number of element factories: %d", g_list_length (factories));

    /* allocate the object_types array to hold them */
    object_types = g_new0 (GType, g_list_length (factories)+$ntypes+1);

    l = factories;
    i = 0;

    /* fill it */
    while (l) {
      factory = GST_ELEMENT_FACTORY (l->data);
      type = gst_element_factory_get_element_type (factory);
      if (type != 0) {
        $debug_log ("adding type for factory %s", gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_LONGNAME));
        object_types[i++] = type;
      } else {
        g_message ("type info for factory %s not found",
            gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_LONGNAME));
      }
      l = g_list_next (l);
    }

EOT

# get_type functions:
for (@types) {
print OUTPUT <<EOT;
    type = $_ ();
    if (type == 0) {
      g_message ("$_ () didn't return a valid type");
    }
    else {
      object_types[i++] = type;
    }
EOT
}

# Implicit types retrieved from GLib:
for (@impl_types) {
print OUTPUT <<EOT;
    type = g_type_from_name ("$_");
    if (type == 0) {
      g_message ("Implicit type $_ not found");
    }
    else {
      object_types[i++] = type;
    }
EOT
}

print OUTPUT <<EOT;

    object_types[i] = 0;

    /* reference the GObjectClass to initialize the param spec pool
     * potentially needed by interfaces. See http://bugs.gnome.org/571820 */
    g_object_class = g_type_class_ref (G_TYPE_OBJECT);

    /* Need to make sure all the types are loaded in and initialize
     * their signals and properties.
     */
    for (i=0; object_types[i]; i++)
      {
        if (G_TYPE_IS_CLASSED (object_types[i]))
          g_type_class_ref (object_types[i]);
        if (G_TYPE_IS_INTERFACE (object_types[i]))
          g_type_default_interface_ref (object_types[i]);
      }

    g_type_class_unref (g_object_class);

    return object_types;
}

/*
 * This uses GObject type functions to output signal prototypes and the object
 * hierarchy.
 */

/* The output files */
const gchar *signals_filename = "$new_signals_filename";
const gchar *hierarchy_filename = "$new_hierarchy_filename";
const gchar *interfaces_filename = "$new_interfaces_filename";
const gchar *prerequisites_filename = "$new_prerequisites_filename";
const gchar *args_filename = "$new_args_filename";


static void output_signals (void);
static void output_object_signals (FILE *fp,
				   GType object_type);
static void output_object_signal (FILE *fp,
				  const gchar *object_class_name,
				  guint signal_id);
static const gchar * get_type_name (GType type,
			            gboolean * is_pointer);
static void output_object_hierarchy (void);
static void output_hierarchy (FILE *fp,
			      GType type,
			      guint level);

static void output_object_interfaces (void);
static void output_interfaces (FILE *fp,
			       GType type);

static void output_interface_prerequisites (void);
static void output_prerequisites (FILE *fp,
			          GType type);

static void output_args (void);
static void output_object_args (FILE *fp, GType object_type);

int
main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
{
  $TYPE_INIT_FUNC;

  get_object_types ();

  output_signals ();
  output_object_hierarchy ();
  output_object_interfaces ();
  output_interface_prerequisites ();
  output_args ();

  return 0;
}


static void
output_signals (void)
{
  FILE *fp;
  gint i;

  fp = fopen (signals_filename, "w");
  if (fp == NULL)
    {
      g_warning ("Couldn't open output file: %s : %s", signals_filename, g_strerror(errno));
      return;
    }

  for (i = 0; object_types[i]; i++)
    output_object_signals (fp, object_types[i]);

  fclose (fp);
}

static gint
compare_signals (const void *a, const void *b)
{
  const guint *signal_a = a;
  const guint *signal_b = b;

  return strcmp (g_signal_name (*signal_a), g_signal_name (*signal_b));
}

/* This outputs all the signals of one object. */
static void
output_object_signals (FILE *fp, GType object_type)
{
  const gchar *object_class_name;
  guint *signals, n_signals;
  guint sig;

  if (G_TYPE_IS_INSTANTIATABLE (object_type) ||
      G_TYPE_IS_INTERFACE (object_type))
    {

      object_class_name = g_type_name (object_type);

      signals = g_signal_list_ids (object_type, &n_signals);
      qsort (signals, n_signals, sizeof (guint), compare_signals);

      for (sig = 0; sig < n_signals; sig++)
        {
           output_object_signal (fp, object_class_name, signals[sig]);
        }
      g_free (signals);
   }
}


/* This outputs one signal. */
static void
output_object_signal (FILE *fp,
		      const gchar *object_name,
		      guint signal_id)
{
  GSignalQuery query_info;
  const gchar *type_name, *ret_type, *object_arg, *arg_name;
  gchar *pos, *object_arg_lower;
  gboolean is_pointer;
  gchar buffer[1024];
  guint i, param;
  gint param_num, widget_num, event_num, callback_num;
  gint *arg_num;
  gchar signal_name[128];
  gchar flags[16];

  $debug_log ("Object: %s Signal: %u", object_name, signal_id);

  param_num = 1;
  widget_num = event_num = callback_num = 0;

  g_signal_query (signal_id, &query_info);

  /* Output the signal object type and the argument name. We assume the
     type is a pointer - I think that is OK. We remove "Gtk" or "Gnome" and
     convert to lower case for the argument name. */
  pos = buffer;
  sprintf (pos, "%s ", object_name);
  pos += strlen (pos);

  /* Try to come up with a sensible variable name for the first arg
   * It chops off 2 know prefixes :/ and makes the name lowercase
   * It should replace lowercase -> uppercase with '_'
   * GFileMonitor -> file_monitor
   * GIOExtensionPoint -> extension_point
   * GtkTreeView -> tree_view
   * if 2nd char is upper case too
   *   search for first lower case and go back one char
   * else
   *   search for next upper case
   */
  if (!strncmp (object_name, "Gtk", 3))
      object_arg = object_name + 3;
  else if (!strncmp (object_name, "Gnome", 5))
      object_arg = object_name + 5;
  else
      object_arg = object_name;

  object_arg_lower = g_ascii_strdown (object_arg, -1);
  sprintf (pos, "*%s\\n", object_arg_lower);
  pos += strlen (pos);
  if (!strncmp (object_arg_lower, "widget", 6))
    widget_num = 2;
  g_free(object_arg_lower);

  /* Convert signal name to use underscores rather than dashes '-'. */
  strncpy (signal_name, query_info.signal_name, 127);
  signal_name[127] = '\\0';
  for (i = 0; signal_name[i]; i++)
    {
      if (signal_name[i] == '-')
	signal_name[i] = '_';
    }

  /* Output the signal parameters. */
  for (param = 0; param < query_info.n_params; param++)
    {
      type_name = get_type_name (query_info.param_types[param] & ~G_SIGNAL_TYPE_STATIC_SCOPE, &is_pointer);

      /* Most arguments to the callback are called "arg1", "arg2", etc.
         GtkWidgets are called "widget", "widget2", ...
         GtkCallbacks are called "callback", "callback2", ... */
      if (!strcmp (type_name, "GtkWidget"))
        {
          arg_name = "widget";
          arg_num = &widget_num;
        }
      else if (!strcmp (type_name, "GtkCallback")
               || !strcmp (type_name, "GtkCCallback"))
        {
          arg_name = "callback";
          arg_num = &callback_num;
        }
      else
        {
          arg_name = "arg";
          arg_num = &param_num;
        }
      sprintf (pos, "%s ", type_name);
      pos += strlen (pos);

      if (!arg_num || *arg_num == 0)
        sprintf (pos, "%s%s\\n", is_pointer ? "*" : " ", arg_name);
      else
        sprintf (pos, "%s%s%i\\n", is_pointer ? "*" : " ", arg_name,
                 *arg_num);
      pos += strlen (pos);

      if (arg_num)
        {
          if (*arg_num == 0)
            *arg_num = 2;
          else
            *arg_num += 1;
        }
    }

  pos = flags;
  /* We use one-character flags for simplicity. */
  if (query_info.signal_flags & G_SIGNAL_RUN_FIRST)
    *pos++ = 'f';
  if (query_info.signal_flags & G_SIGNAL_RUN_LAST)
    *pos++ = 'l';
  if (query_info.signal_flags & G_SIGNAL_RUN_CLEANUP)
    *pos++ = 'c';
  if (query_info.signal_flags & G_SIGNAL_NO_RECURSE)
    *pos++ = 'r';
  if (query_info.signal_flags & G_SIGNAL_DETAILED)
    *pos++ = 'd';
  if (query_info.signal_flags & G_SIGNAL_ACTION)
    *pos++ = 'a';
  if (query_info.signal_flags & G_SIGNAL_NO_HOOKS)
    *pos++ = 'h';
  *pos = 0;

  /* Output the return type and function name. */
  ret_type = get_type_name (query_info.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE, &is_pointer);

  fprintf (fp,
	   "<SIGNAL>\\n<NAME>%s::%s</NAME>\\n<RETURNS>%s%s</RETURNS>\\n<FLAGS>%s</FLAGS>\\n%s</SIGNAL>\\n\\n",
	   object_name, query_info.signal_name, ret_type, is_pointer ? "*" : "", flags, buffer);
}


/* Returns the type name to use for a signal argument or return value, given
   the GtkType from the signal info. It also sets is_pointer to TRUE if the
   argument needs a '*' since it is a pointer. */
static const gchar *
get_type_name (GType type, gboolean * is_pointer)
{
  const gchar *type_name;

  *is_pointer = FALSE;
  type_name = g_type_name (type);

  switch (type) {
  case G_TYPE_NONE:
  case G_TYPE_CHAR:
  case G_TYPE_UCHAR:
  case G_TYPE_BOOLEAN:
  case G_TYPE_INT:
  case G_TYPE_UINT:
  case G_TYPE_LONG:
  case G_TYPE_ULONG:
  case G_TYPE_FLOAT:
  case G_TYPE_DOUBLE:
  case G_TYPE_POINTER:
    /* These all have normal C type names so they are OK. */
    return type_name;

  case G_TYPE_STRING:
    /* A GtkString is really a gchar*. */
    *is_pointer = TRUE;
    return "gchar";

  case G_TYPE_ENUM:
  case G_TYPE_FLAGS:
    /* We use a gint for both of these. Hopefully a subtype with a decent
       name will be registered and used instead, as GTK+ does itself. */
    return "gint";

  case G_TYPE_BOXED:
    /* The boxed type shouldn't be used itself, only subtypes. Though we
       return 'gpointer' just in case. */
    return "gpointer";

  case G_TYPE_PARAM:
    /* A GParam is really a GParamSpec*. */
    *is_pointer = TRUE;
    return "GParamSpec";

#if GLIB_CHECK_VERSION (2, 25, 9)
  case G_TYPE_VARIANT:
    *is_pointer = TRUE;
    return "GVariant";
#endif

default:
    break;
  }

  /* For all GObject subclasses we can use the class name with a "*",
     e.g. 'GtkWidget *'. */
  if (g_type_is_a (type, G_TYPE_OBJECT))
    *is_pointer = TRUE;

  /* Also catch non GObject root types */
  if (G_TYPE_IS_CLASSED (type))
    *is_pointer = TRUE;

  /* All boxed subtypes will be pointers as well. */
  /* Exception: GStrv */
  if (g_type_is_a (type, G_TYPE_BOXED) &&
      !g_type_is_a (type, G_TYPE_STRV))
    *is_pointer = TRUE;

  /* All pointer subtypes will be pointers as well. */
  if (g_type_is_a (type, G_TYPE_POINTER))
    *is_pointer = TRUE;

  /* But enums are not */
  if (g_type_is_a (type, G_TYPE_ENUM) ||
      g_type_is_a (type, G_TYPE_FLAGS))
    *is_pointer = FALSE;

  return type_name;
}


/* This outputs the hierarchy of all objects which have been initialized,
   i.e. by calling their XXX_get_type() initialization function. */
static void
output_object_hierarchy (void)
{
  FILE *fp;
  gint i,j;
  GType root, type;
  GType root_types[$ntypes] = { G_TYPE_INVALID, };

  fp = fopen (hierarchy_filename, "w");
  if (fp == NULL)
    {
      g_warning ("Couldn't open output file: %s : %s", hierarchy_filename, g_strerror(errno));
      return;
    }
  output_hierarchy (fp, G_TYPE_OBJECT, 0);
  output_hierarchy (fp, G_TYPE_INTERFACE, 0);

  for (i=0; object_types[i]; i++) {
    root = object_types[i];
    while ((type = g_type_parent (root))) {
      root = type;
    }
    if ((root != G_TYPE_OBJECT) && (root != G_TYPE_INTERFACE)) {
      for (j=0; root_types[j]; j++) {
        if (root == root_types[j]) {
          root = G_TYPE_INVALID; break;
        }
      }
      if(root) {
        root_types[j] = root;
        output_hierarchy (fp, root, 0);
      }
    }
  }

  fclose (fp);
}

static int
compare_types (const void *a, const void *b)
{
  const char *na = g_type_name (*((GType *)a));
  const char *nb = g_type_name (*((GType *)b));

  return g_strcmp0 (na, nb);
}


/* This is called recursively to output the hierarchy of a object. */
static void
output_hierarchy (FILE  *fp,
		  GType  type,
		  guint   level)
{
  guint i;
  GType *children;
  guint n_children;

  if (!type)
    return;

  for (i = 0; i < level; i++)
    fprintf (fp, "  ");
  fprintf (fp, "%s\\n", g_type_name (type));

  children = g_type_children (type, &n_children);
  qsort (children, n_children, sizeof (GType), compare_types);


  for (i=0; i < n_children; i++)
    output_hierarchy (fp, children[i], level + 1);

  g_free (children);
}

static void output_object_interfaces (void)
{
  guint i;
  FILE *fp;

  fp = fopen (interfaces_filename, "w");
  if (fp == NULL)
    {
      g_warning ("Couldn't open output file: %s : %s", interfaces_filename, g_strerror(errno));
      return;
    }
  output_interfaces (fp, G_TYPE_OBJECT);

  for (i = 0; object_types[i]; i++)
    {
      if (!g_type_parent (object_types[i]) &&
          (object_types[i] != G_TYPE_OBJECT) &&
          G_TYPE_IS_INSTANTIATABLE (object_types[i]))
        {
          output_interfaces (fp, object_types[i]);
        }
    }
  fclose (fp);
}

static void
output_interfaces (FILE  *fp,
		   GType  type)
{
  guint i;
  GType *children, *interfaces;
  guint n_children, n_interfaces;

  if (!type)
    return;

  interfaces = g_type_interfaces (type, &n_interfaces);

  if (n_interfaces > 0)
    {
      fprintf (fp, "%s", g_type_name (type));
      for (i=0; i < n_interfaces; i++)
          fprintf (fp, " %s", g_type_name (interfaces[i]));
      fprintf (fp, "\\n");
     }
  g_free (interfaces);

  children = g_type_children (type, &n_children);

  for (i=0; i < n_children; i++)
    output_interfaces (fp, children[i]);

  g_free (children);
}

static void output_interface_prerequisites (void)
{
  FILE *fp;

  fp = fopen (prerequisites_filename, "w");
  if (fp == NULL)
    {
      g_warning ("Couldn't open output file: %s : %s", prerequisites_filename, g_strerror(errno));
      return;
    }
  output_prerequisites (fp, G_TYPE_INTERFACE);
  fclose (fp);
}

static void
output_prerequisites (FILE  *fp,
		      GType  type)
{
#if GLIB_CHECK_VERSION(2,1,0)
  guint i;
  GType *children, *prerequisites;
  guint n_children, n_prerequisites;

  if (!type)
    return;

  prerequisites = g_type_interface_prerequisites (type, &n_prerequisites);

  if (n_prerequisites > 0)
    {
      fprintf (fp, "%s", g_type_name (type));
      for (i=0; i < n_prerequisites; i++)
          fprintf (fp, " %s", g_type_name (prerequisites[i]));
      fprintf (fp, "\\n");
     }
  g_free (prerequisites);

  children = g_type_children (type, &n_children);

  for (i=0; i < n_children; i++)
    output_prerequisites (fp, children[i]);

  g_free (children);
#endif
}

static void
output_args (void)
{
  FILE *fp;
  gint i;

  fp = fopen (args_filename, "w");
  if (fp == NULL)
    {
      g_warning ("Couldn't open output file: %s : %s", args_filename, g_strerror(errno));
      return;
    }

  for (i = 0; object_types[i]; i++) {
    output_object_args (fp, object_types[i]);
  }

  fclose (fp);
}

static gint
compare_param_specs (const void *a, const void *b)
{
  GParamSpec *spec_a = *(GParamSpec **)a;
  GParamSpec *spec_b = *(GParamSpec **)b;

  return strcmp (g_param_spec_get_name (spec_a), g_param_spec_get_name (spec_b));
}

/* Its common to have unsigned properties restricted
 * to the signed range. Therefore we make this look
 * a bit nicer by spelling out the max constants.
 */

/* Don't use "==" with floats, it might trigger a gcc warning.  */
#define GTKDOC_COMPARE_FLOAT(x, y) (x <= y && x >= y)

static gchar*
describe_double_constant (gdouble value)
{
  gchar *desc;

  if (GTKDOC_COMPARE_FLOAT (value, G_MAXDOUBLE))
    desc = g_strdup ("G_MAXDOUBLE");
  else if (GTKDOC_COMPARE_FLOAT (value, G_MINDOUBLE))
    desc = g_strdup ("G_MINDOUBLE");
  else if (GTKDOC_COMPARE_FLOAT (value, -G_MAXDOUBLE))
    desc = g_strdup ("-G_MAXDOUBLE");
  else if (GTKDOC_COMPARE_FLOAT (value, G_MAXFLOAT))
    desc = g_strdup ("G_MAXFLOAT");
  else if (GTKDOC_COMPARE_FLOAT (value, G_MINFLOAT))
    desc = g_strdup ("G_MINFLOAT");
  else if (GTKDOC_COMPARE_FLOAT (value, -G_MAXFLOAT))
    desc = g_strdup ("-G_MAXFLOAT");
  else{
    /* make sure floats are output with a decimal dot irrespective of
    * current locale. Use formatd since we want human-readable numbers
    * and do not need the exact same bit representation when deserialising */
    desc = g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
    g_ascii_formatd (desc, G_ASCII_DTOSTR_BUF_SIZE, "%g", value);
  }

  return desc;
}

static gchar*