4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998 Damon Chaplin
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # This gets information about object heirarchies and signals
24 # by compiling a small C program. CFLAGS and LDFLAGS must be
25 # set appropriately before running this script.
30 push @INC, '@PACKAGE_DATA_DIR@';
31 require "gtkdoc-common.pl";
35 # name of documentation module
40 %optctl = (module => \$MODULE,
41 types => \$TYPES_FILE,
42 nogtkinit => \$NO_GTK_INIT,
43 'output-dir' => \$OUTPUT_DIR,
44 'version' => \$PRINT_VERSION,
45 'help' => \$PRINT_HELP);
47 GetOptions(\%optctl, "module=s", "types:s", "output-dir:s", "nogtkinit", "version", "help");
60 gtkdoc-scanobj version @VERSION@ - introspect gtk-objects
62 --module=MODULE_NAME Name of the doc module being parsed
63 --types=FILE The name of the file to store the types in
64 --output-dir=DIRNAME The directory where the results are stored
65 --version Print the version of this program
66 --help Print this help
71 $OUTPUT_DIR = $OUTPUT_DIR ? $OUTPUT_DIR : ".";
73 $TYPES_FILE = $TYPES_FILE ? $TYPES_FILE : "$OUTPUT_DIR/$MODULE.types";
75 open (TYPES, $TYPES_FILE) || die "Cannot open $TYPES_FILE: $!\n";
76 open (OUTPUT, ">$MODULE-scan.c") || die "Cannot open $MODULE-scan.c: $!\n";
78 my $old_signals_filename = "$OUTPUT_DIR/$MODULE.signals";
79 my $new_signals_filename = "$OUTPUT_DIR/$MODULE.signals.new";
80 my $old_hierarchy_filename = "$OUTPUT_DIR/$MODULE.hierarchy";
81 my $new_hierarchy_filename = "$OUTPUT_DIR/$MODULE.hierarchy.new";
82 my $old_args_filename = "$OUTPUT_DIR/$MODULE.args";
83 my $new_args_filename = "$OUTPUT_DIR/$MODULE.args.new";
85 # write a C program to scan the types
103 $ntypes = @types + 1;
112 GtkType object_types[$ntypes];
115 get_object_types (void)
121 print OUTPUT " object_types[i++] = $_ ();\n";
131 * This uses GTK type functions to output signal prototypes and the widget
135 /* The output files */
136 gchar *signals_filename = "$new_signals_filename";
137 gchar *hierarchy_filename = "$new_hierarchy_filename";
138 gchar *args_filename = "$new_args_filename";
141 static void output_signals (void);
142 static void output_widget_signals (FILE *fp,
143 GtkType object_type);
144 static void output_widget_signal (FILE *fp,
146 gchar *object_class_name,
148 static gchar * get_type_name (GtkType type,
149 gboolean * is_pointer);
150 static void output_widget_hierarchy (void);
151 static void output_hierarchy (FILE *fp,
155 static void output_args (void);
156 static void output_widget_args (FILE *fp, GtkType object_type);
159 main (int argc, char *argv[])
169 gtk_init (&argc, &argv);
177 output_widget_hierarchy ();
185 output_signals (void)
190 fp = fopen (signals_filename, "w");
193 g_warning ("Couldn't open output file: %s : %s", signals_filename, g_strerror(errno));
197 for (i = 0; object_types[i]; i++)
198 output_widget_signals (fp, object_types[i]);
204 /* This outputs all the signals of one widget. */
206 output_widget_signals (FILE *fp, GtkType object_type)
208 GtkObjectClass *class;
209 gchar *object_class_name;
212 class = gtk_type_class (object_type);
213 if (!class || class->nsignals == 0)
216 object_class_name = gtk_type_name (object_type);
218 for (sig = 0; sig < class->nsignals; sig++)
220 if (!class->signals[sig])
222 /*g_print ("Signal slot [%u] is empty\\n", sig);*/
226 output_widget_signal (fp, object_type, object_class_name,
227 class->signals[sig]);
232 /* This outputs one signal. */
234 output_widget_signal (FILE *fp,
239 GtkSignalQuery *query_info;
240 gchar *ret_type, *pos, *type_name, *arg_name, *object_arg, *object_arg_start;
244 gint param_num, widget_num, event_num, callback_num;
246 gchar signal_name[128];
248 /* g_print ("Object: %s Type: %i Signal: %u\\n", object_name, object_type,
252 widget_num = event_num = callback_num = 0;
254 query_info = gtk_signal_query (signal_id);
255 if (query_info == NULL)
257 g_warning ("Couldn't query signal");
261 /* Output the signal object type and the argument name. We assume the
262 type is a pointer - I think that is OK. We remove "Gtk" or "Gnome" and
263 convert to lower case for the argument name. */
265 sprintf (pos, "%s ", object_name);
268 /* Try to come up with a sensible variable name for the first arg
269 * I chops off 2 know prefixes :/ and makes the name lowercase
270 * It should replace lowercase -> uppercase with '_'
271 * see gtkdoc-scangobject.in for better algorithm
273 if (!strncmp (object_name, "Gtk", 3))
274 object_arg = object_name + 3;
275 else if (!strncmp (object_name, "Gnome", 5))
276 object_arg = object_name + 5;
278 object_arg = object_name;
280 object_arg_start = pos;
281 sprintf (pos, "*%s\\n", object_arg);
283 g_strdown (object_arg_start);
284 if (!strcmp (object_arg_start, "widget"))
287 /* Convert signal name to use underscores rather than dashes '-'. */
288 strncpy (signal_name, query_info->signal_name, 127);
289 signal_name[127] = '\\0';
290 for (i = 0; signal_name[i]; i++)
292 if (signal_name[i] == '-')
293 signal_name[i] = '_';
296 /* Output the signal parameters. */
297 for (param = 0; param < query_info->nparams; param++)
299 type_name = get_type_name (query_info->params[param], &is_pointer);
301 /* Most arguments to the callback are called "arg1", "arg2", etc.
302 GtkWidgets are called "widget", "widget2", ...
303 GtkCallbacks are called "callback", "callback2", ... */
304 if (!strcmp (type_name, "GtkWidget"))
307 arg_num = &widget_num;
309 else if (!strcmp (type_name, "GtkCallback")
310 || !strcmp (type_name, "GtkCCallback"))
312 arg_name = "callback";
313 arg_num = &callback_num;
318 arg_num = ¶m_num;
320 sprintf (pos, "%s ", type_name);
323 if (!arg_num || *arg_num == 0)
324 sprintf (pos, "%s%s\\n", is_pointer ? "*" : " ", arg_name);
326 sprintf (pos, "%s%s%i\\n", is_pointer ? "*" : " ", arg_name,
334 /* Output the return type and function name. */
335 ret_type = get_type_name (query_info->return_val, &is_pointer);
338 "<SIGNAL>\\n<NAME>%s::%s</NAME>\\n<RETURNS>%s%s</RETURNS>\\n%s</SIGNAL>\\n\\n",
339 object_name, query_info->signal_name, ret_type, is_pointer ? "*" : "", buffer);
344 /* Returns the type name to use for a signal argument or return value, given
345 the GtkType from the signal info. It also sets is_pointer to TRUE if the
346 argument needs a '*' since it is a pointer. */
348 get_type_name (GtkType type, gboolean * is_pointer)
353 type_name = gtk_type_name (type);
365 case GTK_TYPE_DOUBLE:
366 case GTK_TYPE_POINTER:
367 /* These all have normal C type names so they are OK. */
370 case GTK_TYPE_STRING:
371 /* A GtkString is really a gchar*. */
377 /* We use a gint for both of these. Hopefully a subtype with a decent
378 name will be registered and used instead, as GTK+ does itself. */
382 /* A boxed value is just an opaque pointer, I think. */
385 case GTK_TYPE_SIGNAL:
387 case GTK_TYPE_FOREIGN:
388 case GTK_TYPE_CALLBACK:
389 case GTK_TYPE_C_CALLBACK:
390 /* FIXME: These are wrong. I think they expand into more than 1 argument.
391 See the GtkArg struct in gtktypeutils.h and gtkargcollector.c.
392 Fortunately I doubt anything uses these as signal args. */
399 /* For all GtkObject subclasses we can use the class name with a "*",
400 e.g. 'GtkWidget *'. */
401 if (gtk_type_is_a (type, GTK_TYPE_OBJECT))
408 /* This outputs the hierarchy of all widgets which have been initialized,
409 i.e. by calling their XXX_get_type() initialization function. */
411 output_widget_hierarchy (void)
415 fp = fopen (hierarchy_filename, "w");
418 g_warning ("Couldn't open output file: %s : %s", hierarchy_filename, g_strerror(errno));
421 output_hierarchy (fp, GTK_TYPE_OBJECT, 0);
426 /* This is called recursively to output the hierarchy of a widget. */
428 output_hierarchy (FILE *fp,
438 for (i = 0; i < level; i++)
440 fprintf (fp, "%s\\n", gtk_type_name (type));
442 list = gtk_type_children_types (type);
446 GtkType child = (GtkType) list->data;
447 output_hierarchy (fp, child, level + 1);
459 fp = fopen (args_filename, "w");
462 g_warning ("Couldn't open output file: %s : %s", args_filename, g_strerror(errno));
466 for (i = 0; object_types[i]; i++)
467 output_widget_args (fp, object_types[i]);
474 output_widget_args (FILE *fp, GtkType object_type)
476 GtkObjectClass *class;
477 gchar *object_class_name;
482 gchar flags[16], *pos;
484 class = gtk_type_class (object_type);
488 object_class_name = gtk_type_name (object_type);
490 args = gtk_object_query_args (class->type, &arg_flags, &n_args);
492 for (arg = 0; arg < n_args; arg++)
495 /* We use one-character flags for simplicity. */
496 if (arg_flags[arg] & GTK_ARG_READABLE)
498 if (arg_flags[arg] & GTK_ARG_WRITABLE)
500 if (arg_flags[arg] & GTK_ARG_CONSTRUCT)
502 if (arg_flags[arg] & GTK_ARG_CONSTRUCT_ONLY)
504 if (arg_flags[arg] & GTK_ARG_CHILD_ARG)
508 fprintf (fp, "<ARG>\\n<NAME>%s</NAME>\\n<TYPE>%s</TYPE>\\n<FLAGS>%s</FLAGS>\\n</ARG>\\n\\n",
509 args[arg].name, gtk_type_name (args[arg].type), flags);
519 # Compile and run our file
521 $CC = $ENV{CC} ? $ENV{CC} : "gcc";
522 $LD = $ENV{LD} ? $ENV{LD} : $CC;
523 $CFLAGS = $ENV{CFLAGS} ? $ENV{CFLAGS} : "";
524 $LDFLAGS = $ENV{LDFLAGS} ? $ENV{LDFLAGS} : "";
525 $RUN = $ENV{RUN} ? $ENV{RUN} : "";
528 if ($CC =~ /libtool/) {
529 $o_file = "$MODULE-scan.lo"
531 $o_file = "$MODULE-scan.o"
535 $command = "$CC $CFLAGS -c -o $o_file $MODULE-scan.c";
536 system("($command)") == 0 or die "Compilation of scanner failed: $!\n";
539 # FIXME: Can we turn off as-needed for the docs (or better fix it?)
540 #$command = "$LD -Wl,--no-as-needed $o_file $LDFLAGS -o $MODULE-scan";
541 $command = "$LD $o_file $LDFLAGS -o $MODULE-scan";
542 system("($command)") == 0 or die "Linking of scanner failed: $!\n";
544 # Running scanner $MODULE-scan ";
545 system("($RUN ./$MODULE-scan)") == 0 or die "Scan failed: $!\n";
547 if (!defined($ENV{"GTK_DOC_KEEP_INTERMEDIATE"})) {
548 unlink "./$MODULE-scan.c", "./$MODULE-scan.o", "./$MODULE-scan.lo", "./$MODULE-scan";
551 &UpdateFileIfChanged ($old_signals_filename, $new_signals_filename, 0);
552 &UpdateFileIfChanged ($old_hierarchy_filename, $new_hierarchy_filename, 0);
553 &UpdateFileIfChanged ($old_args_filename, $new_args_filename, 0);