common: port more functions and fix one
[gtk-doc.git] / gtkdoc / scangobj.py
blobf4ff9edff34f42813b8b6a382fb96765e90651f1
1 # -*- python -*-
3 # gtk-doc - GTK DocBook documentation generator.
4 # Copyright (C) 1998 Damon Chaplin
5 # 2007-2016 Stefan Sauer
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.
22 """
23 The scangobj tool gets information about object hierarchies and signals by
24 compiling and running a small C program. CFLAGS and LDFLAGS must be set
25 appropriately before running this script.
26 """
28 import logging
29 import os
30 import string
31 import subprocess
33 from . import common, config
36 COMMON_INCLUDES = """
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <glib-object.h>
42 """
44 QUERY_CHILD_PROPS_PROTOTYPE = "extern GParamSpec** %s (gpointer class, guint *n_properties);"
46 QUERY_CHILD_PROPS_CODE = """
47 if (!child_prop) {
48 properties = %s (class, &n_properties);
49 if (properties) {
50 child_prop = TRUE;
51 continue;
54 """
56 MAIN_CODE = """
58 #ifdef GTK_IS_WIDGET_CLASS
59 #include <gtk/gtk.h>
60 #endif
61 static GType object_types[$ntypes];
63 static GType *
64 get_object_types (void)
66 gpointer g_object_class;
67 gint i = 0;
69 ${get_types}
70 object_types[i] = G_TYPE_INVALID;
72 /* reference the GObjectClass to initialize the param spec pool
73 * potentially needed by interfaces. See http://bugs.gnome.org/571820 */
74 g_object_class = g_type_class_ref (G_TYPE_OBJECT);
76 /* Need to make sure all the types are loaded in and initialize
77 * their signals and properties.
79 for (i=0; object_types[i]; i++) {
80 if (G_TYPE_IS_CLASSED (object_types[i]))
81 g_type_class_ref (object_types[i]);
82 if (G_TYPE_IS_INTERFACE (object_types[i]))
83 g_type_default_interface_ref (object_types[i]);
86 g_type_class_unref (g_object_class);
88 return object_types;
92 * This uses GObject type functions to output signal prototypes and the object
93 * hierarchy.
96 /* The output files */
97 const gchar *signals_filename = "$new_signals_filename";
98 const gchar *hierarchy_filename = "$new_hierarchy_filename";
99 const gchar *interfaces_filename = "$new_interfaces_filename";
100 const gchar *prerequisites_filename = "$new_prerequisites_filename";
101 const gchar *args_filename = "$new_args_filename";
103 static void output_signals (void);
104 static void output_object_signals (FILE *fp,
105 GType object_type);
106 static void output_object_signal (FILE *fp,
107 const gchar *object_class_name,
108 guint signal_id);
109 static const gchar * get_type_name (GType type,
110 gboolean * is_pointer);
111 static void output_object_hierarchy (void);
112 static void output_hierarchy (FILE *fp,
113 GType type,
114 guint level);
116 static void output_object_interfaces (void);
117 static void output_interfaces (FILE *fp,
118 GType type);
120 static void output_interface_prerequisites (void);
121 static void output_prerequisites (FILE *fp,
122 GType type);
124 static void output_args (void);
125 static void output_object_args (FILE *fp, GType object_type);
128 main (int argc, char *argv[])
130 ${type_init_func};
132 get_object_types ();
134 output_signals ();
135 output_object_hierarchy ();
136 output_object_interfaces ();
137 output_interface_prerequisites ();
138 output_args ();
140 return 0;
143 static void
144 output_signals (void)
146 FILE *fp;
147 gint i;
149 fp = fopen (signals_filename, "w");
150 if (fp == NULL) {
151 g_warning ("Couldn't open output file: %s : %s", signals_filename, g_strerror(errno));
152 return;
155 for (i = 0; object_types[i]; i++)
156 output_object_signals (fp, object_types[i]);
158 fclose (fp);
161 static gint
162 compare_signals (const void *a, const void *b)
164 const guint *signal_a = a;
165 const guint *signal_b = b;
167 return strcmp (g_signal_name (*signal_a), g_signal_name (*signal_b));
170 /* This outputs all the signals of one object. */
171 static void
172 output_object_signals (FILE *fp, GType object_type)
174 const gchar *object_class_name;
175 guint *signals, n_signals;
176 guint sig;
178 if (G_TYPE_IS_INSTANTIATABLE (object_type) ||
179 G_TYPE_IS_INTERFACE (object_type)) {
181 object_class_name = g_type_name (object_type);
183 signals = g_signal_list_ids (object_type, &n_signals);
184 qsort (signals, n_signals, sizeof (guint), compare_signals);
186 for (sig = 0; sig < n_signals; sig++) {
187 output_object_signal (fp, object_class_name, signals[sig]);
189 g_free (signals);
193 /* This outputs one signal. */
194 static void
195 output_object_signal (FILE *fp,
196 const gchar *object_name,
197 guint signal_id)
199 GSignalQuery query_info;
200 const gchar *type_name, *ret_type, *object_arg, *arg_name;
201 gchar *pos, *object_arg_lower;
202 gboolean is_pointer;
203 gchar buffer[1024];
204 guint i, param;
205 gint param_num, widget_num, event_num, callback_num;
206 gint *arg_num;
207 gchar signal_name[128];
208 gchar flags[16];
210 /* g_print ("Object: %s Signal: %u\\n", object_name, signal_id);*/
212 param_num = 1;
213 widget_num = event_num = callback_num = 0;
215 g_signal_query (signal_id, &query_info);
217 /* Output the signal object type and the argument name. We assume the
218 * type is a pointer - I think that is OK. We remove "Gtk" or "Gnome" and
219 * convert to lower case for the argument name. */
220 pos = buffer;
221 sprintf (pos, "%s ", object_name);
222 pos += strlen (pos);
224 /* Try to come up with a sensible variable name for the first arg
225 * It chops off 2 know prefixes :/ and makes the name lowercase
226 * It should replace lowercase -> uppercase with '_'
227 * GFileMonitor -> file_monitor
228 * GIOExtensionPoint -> extension_point
229 * GtkTreeView -> tree_view
230 * if 2nd char is upper case too
231 * search for first lower case and go back one char
232 * else
233 * search for next upper case
235 if (!strncmp (object_name, "Gtk", 3))
236 object_arg = object_name + 3;
237 else if (!strncmp (object_name, "Gnome", 5))
238 object_arg = object_name + 5;
239 else
240 object_arg = object_name;
242 object_arg_lower = g_ascii_strdown (object_arg, -1);
243 sprintf (pos, "*%s\\n", object_arg_lower);
244 pos += strlen (pos);
245 if (!strncmp (object_arg_lower, "widget", 6))
246 widget_num = 2;
247 g_free(object_arg_lower);
249 /* Convert signal name to use underscores rather than dashes '-'. */
250 strncpy (signal_name, query_info.signal_name, 127);
251 signal_name[127] = '\\0';
252 for (i = 0; signal_name[i]; i++) {
253 if (signal_name[i] == '-')
254 signal_name[i] = '_';
257 /* Output the signal parameters. */
258 for (param = 0; param < query_info.n_params; param++) {
259 type_name = get_type_name (query_info.param_types[param] & ~G_SIGNAL_TYPE_STATIC_SCOPE, &is_pointer);
261 /* Most arguments to the callback are called "arg1", "arg2", etc.
262 GtkWidgets are called "widget", "widget2", ...
263 GtkCallbacks are called "callback", "callback2", ... */
264 if (!strcmp (type_name, "GtkWidget")) {
265 arg_name = "widget";
266 arg_num = &widget_num;
268 else if (!strcmp (type_name, "GtkCallback")
269 || !strcmp (type_name, "GtkCCallback")) {
270 arg_name = "callback";
271 arg_num = &callback_num;
273 else {
274 arg_name = "arg";
275 arg_num = &param_num;
277 sprintf (pos, "%s ", type_name);
278 pos += strlen (pos);
280 if (!arg_num || *arg_num == 0)
281 sprintf (pos, "%s%s\\n", is_pointer ? "*" : " ", arg_name);
282 else
283 sprintf (pos, "%s%s%i\\n", is_pointer ? "*" : " ", arg_name,
284 *arg_num);
285 pos += strlen (pos);
287 if (arg_num) {
288 if (*arg_num == 0)
289 *arg_num = 2;
290 else
291 *arg_num += 1;
295 pos = flags;
296 /* We use one-character flags for simplicity. */
297 if (query_info.signal_flags & G_SIGNAL_RUN_FIRST)
298 *pos++ = 'f';
299 if (query_info.signal_flags & G_SIGNAL_RUN_LAST)
300 *pos++ = 'l';
301 if (query_info.signal_flags & G_SIGNAL_RUN_CLEANUP)
302 *pos++ = 'c';
303 if (query_info.signal_flags & G_SIGNAL_NO_RECURSE)
304 *pos++ = 'r';
305 if (query_info.signal_flags & G_SIGNAL_DETAILED)
306 *pos++ = 'd';
307 if (query_info.signal_flags & G_SIGNAL_ACTION)
308 *pos++ = 'a';
309 if (query_info.signal_flags & G_SIGNAL_NO_HOOKS)
310 *pos++ = 'h';
311 *pos = 0;
313 /* Output the return type and function name. */
314 ret_type = get_type_name (query_info.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE, &is_pointer);
316 fprintf (fp,
317 "<SIGNAL>\\n<NAME>%s::%s</NAME>\\n<RETURNS>%s%s</RETURNS>\\n<FLAGS>%s</FLAGS>\\n%s</SIGNAL>\\n\\n",
318 object_name, query_info.signal_name, ret_type, is_pointer ? "*" : "", flags, buffer);
322 /* Returns the type name to use for a signal argument or return value, given
323 the GtkType from the signal info. It also sets is_pointer to TRUE if the
324 argument needs a '*' since it is a pointer. */
325 static const gchar *
326 get_type_name (GType type, gboolean * is_pointer)
328 const gchar *type_name;
330 *is_pointer = FALSE;
331 type_name = g_type_name (type);
333 switch (type) {
334 case G_TYPE_NONE:
335 case G_TYPE_CHAR:
336 case G_TYPE_UCHAR:
337 case G_TYPE_BOOLEAN:
338 case G_TYPE_INT:
339 case G_TYPE_UINT:
340 case G_TYPE_LONG:
341 case G_TYPE_ULONG:
342 case G_TYPE_FLOAT:
343 case G_TYPE_DOUBLE:
344 case G_TYPE_POINTER:
345 /* These all have normal C type names so they are OK. */
346 return type_name;
348 case G_TYPE_STRING:
349 /* A GtkString is really a gchar*. */
350 *is_pointer = TRUE;
351 return "gchar";
353 case G_TYPE_ENUM:
354 case G_TYPE_FLAGS:
355 /* We use a gint for both of these. Hopefully a subtype with a decent
356 name will be registered and used instead, as GTK+ does itself. */
357 return "gint";
359 case G_TYPE_BOXED:
360 /* The boxed type shouldn't be used itself, only subtypes. Though we
361 return 'gpointer' just in case. */
362 return "gpointer";
364 case G_TYPE_PARAM:
365 /* A GParam is really a GParamSpec*. */
366 *is_pointer = TRUE;
367 return "GParamSpec";
369 #if GLIB_CHECK_VERSION (2, 25, 9)
370 case G_TYPE_VARIANT:
371 *is_pointer = TRUE;
372 return "GVariant";
373 #endif
375 default:
376 break;
379 /* For all GObject subclasses we can use the class name with a "*",
380 e.g. 'GtkWidget *'. */
381 if (g_type_is_a (type, G_TYPE_OBJECT))
382 *is_pointer = TRUE;
384 /* Also catch non GObject root types */
385 if (G_TYPE_IS_CLASSED (type))
386 *is_pointer = TRUE;
388 /* All boxed subtypes will be pointers as well. */
389 /* Exception: GStrv */
390 if (g_type_is_a (type, G_TYPE_BOXED) &&
391 !g_type_is_a (type, G_TYPE_STRV))
392 *is_pointer = TRUE;
394 /* All pointer subtypes will be pointers as well. */
395 if (g_type_is_a (type, G_TYPE_POINTER))
396 *is_pointer = TRUE;
398 /* But enums are not */
399 if (g_type_is_a (type, G_TYPE_ENUM) ||
400 g_type_is_a (type, G_TYPE_FLAGS))
401 *is_pointer = FALSE;
403 return type_name;
407 /* This outputs the hierarchy of all objects which have been initialized,
408 i.e. by calling their XXX_get_type() initialization function. */
409 static void
410 output_object_hierarchy (void)
412 FILE *fp;
413 gint i,j;
414 GType root, type;
415 GType root_types[$ntypes] = { G_TYPE_INVALID, };
417 fp = fopen (hierarchy_filename, "w");
418 if (fp == NULL) {
419 g_warning ("Couldn't open output file: %s : %s", hierarchy_filename, g_strerror(errno));
420 return;
422 output_hierarchy (fp, G_TYPE_OBJECT, 0);
423 output_hierarchy (fp, G_TYPE_INTERFACE, 0);
425 for (i=0; object_types[i]; i++) {
426 root = object_types[i];
427 while ((type = g_type_parent (root))) {
428 root = type;
430 if ((root != G_TYPE_OBJECT) && (root != G_TYPE_INTERFACE)) {
431 for (j=0; root_types[j]; j++) {
432 if (root == root_types[j]) {
433 root = G_TYPE_INVALID; break;
436 if(root) {
437 root_types[j] = root;
438 output_hierarchy (fp, root, 0);
443 fclose (fp);
446 /* This is called recursively to output the hierarchy of a object. */
447 static void
448 output_hierarchy (FILE *fp,
449 GType type,
450 guint level)
452 guint i;
453 GType *children;
454 guint n_children;
456 if (!type)
457 return;
459 for (i = 0; i < level; i++)
460 fprintf (fp, " ");
461 fprintf (fp, "%s\\n", g_type_name (type));
463 children = g_type_children (type, &n_children);
465 for (i=0; i < n_children; i++)
466 output_hierarchy (fp, children[i], level + 1);
468 g_free (children);
471 static void output_object_interfaces (void)
473 guint i;
474 FILE *fp;
476 fp = fopen (interfaces_filename, "w");
477 if (fp == NULL) {
478 g_warning ("Couldn't open output file: %s : %s", interfaces_filename, g_strerror(errno));
479 return;
481 output_interfaces (fp, G_TYPE_OBJECT);
483 for (i = 0; object_types[i]; i++) {
484 if (!g_type_parent (object_types[i]) &&
485 (object_types[i] != G_TYPE_OBJECT) &&
486 G_TYPE_IS_INSTANTIATABLE (object_types[i])) {
487 output_interfaces (fp, object_types[i]);
490 fclose (fp);
493 static void
494 output_interfaces (FILE *fp,
495 GType type)
497 guint i;
498 GType *children, *interfaces;
499 guint n_children, n_interfaces;
501 if (!type)
502 return;
504 interfaces = g_type_interfaces (type, &n_interfaces);
506 if (n_interfaces > 0) {
507 fprintf (fp, "%s", g_type_name (type));
508 for (i=0; i < n_interfaces; i++)
509 fprintf (fp, " %s", g_type_name (interfaces[i]));
510 fprintf (fp, "\\n");
512 g_free (interfaces);
514 children = g_type_children (type, &n_children);
516 for (i=0; i < n_children; i++)
517 output_interfaces (fp, children[i]);
519 g_free (children);
522 static void output_interface_prerequisites (void)
524 FILE *fp;
526 fp = fopen (prerequisites_filename, "w");
527 if (fp == NULL) {
528 g_warning ("Couldn't open output file: %s : %s", prerequisites_filename, g_strerror(errno));
529 return;
531 output_prerequisites (fp, G_TYPE_INTERFACE);
532 fclose (fp);
535 static void
536 output_prerequisites (FILE *fp,
537 GType type)
539 #if GLIB_CHECK_VERSION(2,1,0)
540 guint i;
541 GType *children, *prerequisites;
542 guint n_children, n_prerequisites;
544 if (!type)
545 return;
547 prerequisites = g_type_interface_prerequisites (type, &n_prerequisites);
549 if (n_prerequisites > 0) {
550 fprintf (fp, "%s", g_type_name (type));
551 for (i=0; i < n_prerequisites; i++)
552 fprintf (fp, " %s", g_type_name (prerequisites[i]));
553 fprintf (fp, "\\n");
555 g_free (prerequisites);
557 children = g_type_children (type, &n_children);
559 for (i=0; i < n_children; i++)
560 output_prerequisites (fp, children[i]);
562 g_free (children);
563 #endif
566 static void
567 output_args (void)
569 FILE *fp;
570 gint i;
572 fp = fopen (args_filename, "w");
573 if (fp == NULL) {
574 g_warning ("Couldn't open output file: %s : %s", args_filename, g_strerror(errno));
575 return;
578 for (i = 0; object_types[i]; i++) {
579 output_object_args (fp, object_types[i]);
582 fclose (fp);
585 static gint
586 compare_param_specs (const void *a, const void *b)
588 GParamSpec *spec_a = *(GParamSpec **)a;
589 GParamSpec *spec_b = *(GParamSpec **)b;
591 return strcmp (g_param_spec_get_name (spec_a), g_param_spec_get_name (spec_b));
594 /* Its common to have unsigned properties restricted
595 * to the signed range. Therefore we make this look
596 * a bit nicer by spelling out the max constants.
599 /* Don't use "==" with floats, it might trigger a gcc warning. */
600 #define GTKDOC_COMPARE_FLOAT(x, y) (x <= y && x >= y)
602 static gchar*
603 describe_double_constant (gdouble value)
605 gchar *desc;
607 if (GTKDOC_COMPARE_FLOAT (value, G_MAXDOUBLE))
608 desc = g_strdup ("G_MAXDOUBLE");
609 else if (GTKDOC_COMPARE_FLOAT (value, G_MINDOUBLE))
610 desc = g_strdup ("G_MINDOUBLE");
611 else if (GTKDOC_COMPARE_FLOAT (value, -G_MAXDOUBLE))
612 desc = g_strdup ("-G_MAXDOUBLE");
613 else if (GTKDOC_COMPARE_FLOAT (value, G_MAXFLOAT))
614 desc = g_strdup ("G_MAXFLOAT");
615 else if (GTKDOC_COMPARE_FLOAT (value, G_MINFLOAT))
616 desc = g_strdup ("G_MINFLOAT");
617 else if (GTKDOC_COMPARE_FLOAT (value, -G_MAXFLOAT))
618 desc = g_strdup ("-G_MAXFLOAT");
619 else{
620 /* make sure floats are output with a decimal dot irrespective of
621 * current locale. Use formatd since we want human-readable numbers
622 * and do not need the exact same bit representation when deserialising */
623 desc = g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
624 g_ascii_formatd (desc, G_ASCII_DTOSTR_BUF_SIZE, "%g", value);
627 return desc;
630 static gchar*
631 describe_signed_constant (gsize size, gint64 value)
633 gchar *desc = NULL;
635 switch (size) {
636 case 2:
637 if (sizeof (int) == 2) {
638 if (value == G_MAXINT)
639 desc = g_strdup ("G_MAXINT");
640 else if (value == G_MININT)
641 desc = g_strdup ("G_MININT");
643 break;
644 case 4:
645 if (sizeof (int) == 4) {
646 if (value == G_MAXINT)
647 desc = g_strdup ("G_MAXINT");
648 else if (value == G_MININT)
649 desc = g_strdup ("G_MININT");
651 if (value == G_MAXLONG)
652 desc = g_strdup ("G_MAXLONG");
653 else if (value == G_MINLONG)
654 desc = g_strdup ("G_MINLONG");
655 break;
656 case 8:
657 if (value == G_MAXINT64)
658 desc = g_strdup ("G_MAXINT64");
659 else if (value == G_MININT64)
660 desc = g_strdup ("G_MININT64");
661 break;
662 default:
663 break;
665 if (!desc)
666 desc = g_strdup_printf ("%" G_GINT64_FORMAT, value);
668 return desc;
671 static gchar*
672 describe_unsigned_constant (gsize size, guint64 value)
674 gchar *desc = NULL;
676 switch (size) {
677 case 2:
678 if (sizeof (int) == 2) {
679 if (value == (guint64)G_MAXINT)
680 desc = g_strdup ("G_MAXINT");
681 else if (value == G_MAXUINT)
682 desc = g_strdup ("G_MAXUINT");
684 break;
685 case 4:
686 if (sizeof (int) == 4) {
687 if (value == (guint64)G_MAXINT)
688 desc = g_strdup ("G_MAXINT");
689 else if (value == G_MAXUINT)
690 desc = g_strdup ("G_MAXUINT");
692 if (value == (guint64)G_MAXLONG)
693 desc = g_strdup ("G_MAXLONG");
694 else if (value == G_MAXULONG)
695 desc = g_strdup ("G_MAXULONG");
696 break;
697 case 8:
698 if (value == G_MAXINT64)
699 desc = g_strdup ("G_MAXINT64");
700 else if (value == G_MAXUINT64)
701 desc = g_strdup ("G_MAXUINT64");
702 break;
703 default:
704 break;
706 if (!desc)
707 desc = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
709 return desc;
712 static gchar*
713 describe_type (GParamSpec *spec)
715 gchar *desc;
716 gchar *lower;
717 gchar *upper;
719 if (G_IS_PARAM_SPEC_CHAR (spec)) {
720 GParamSpecChar *pspec = G_PARAM_SPEC_CHAR (spec);
722 lower = describe_signed_constant (sizeof(gchar), pspec->minimum);
723 upper = describe_signed_constant (sizeof(gchar), pspec->maximum);
724 if (pspec->minimum == G_MININT8 && pspec->maximum == G_MAXINT8)
725 desc = g_strdup ("");
726 else if (pspec->minimum == G_MININT8)
727 desc = g_strdup_printf ("<= %s", upper);
728 else if (pspec->maximum == G_MAXINT8)
729 desc = g_strdup_printf (">= %s", lower);
730 else
731 desc = g_strdup_printf ("[%s,%s]", lower, upper);
732 g_free (lower);
733 g_free (upper);
735 else if (G_IS_PARAM_SPEC_UCHAR (spec)) {
736 GParamSpecUChar *pspec = G_PARAM_SPEC_UCHAR (spec);
738 lower = describe_unsigned_constant (sizeof(guchar), pspec->minimum);
739 upper = describe_unsigned_constant (sizeof(guchar), pspec->maximum);
740 if (pspec->minimum == 0 && pspec->maximum == G_MAXUINT8)
741 desc = g_strdup ("");
742 else if (pspec->minimum == 0)
743 desc = g_strdup_printf ("<= %s", upper);
744 else if (pspec->maximum == G_MAXUINT8)
745 desc = g_strdup_printf (">= %s", lower);
746 else
747 desc = g_strdup_printf ("[%s,%s]", lower, upper);
748 g_free (lower);
749 g_free (upper);
751 else if (G_IS_PARAM_SPEC_INT (spec)) {
752 GParamSpecInt *pspec = G_PARAM_SPEC_INT (spec);
754 lower = describe_signed_constant (sizeof(gint), pspec->minimum);
755 upper = describe_signed_constant (sizeof(gint), pspec->maximum);
756 if (pspec->minimum == G_MININT && pspec->maximum == G_MAXINT)
757 desc = g_strdup ("");
758 else if (pspec->minimum == G_MININT)
759 desc = g_strdup_printf ("<= %s", upper);
760 else if (pspec->maximum == G_MAXINT)
761 desc = g_strdup_printf (">= %s", lower);
762 else
763 desc = g_strdup_printf ("[%s,%s]", lower, upper);
764 g_free (lower);
765 g_free (upper);
767 else if (G_IS_PARAM_SPEC_UINT (spec)) {
768 GParamSpecUInt *pspec = G_PARAM_SPEC_UINT (spec);
770 lower = describe_unsigned_constant (sizeof(guint), pspec->minimum);
771 upper = describe_unsigned_constant (sizeof(guint), pspec->maximum);
772 if (pspec->minimum == 0 && pspec->maximum == G_MAXUINT)
773 desc = g_strdup ("");
774 else if (pspec->minimum == 0)
775 desc = g_strdup_printf ("<= %s", upper);
776 else if (pspec->maximum == G_MAXUINT)
777 desc = g_strdup_printf (">= %s", lower);
778 else
779 desc = g_strdup_printf ("[%s,%s]", lower, upper);
780 g_free (lower);
781 g_free (upper);
783 else if (G_IS_PARAM_SPEC_LONG (spec)) {
784 GParamSpecLong *pspec = G_PARAM_SPEC_LONG (spec);
786 lower = describe_signed_constant (sizeof(glong), pspec->minimum);
787 upper = describe_signed_constant (sizeof(glong), pspec->maximum);
788 if (pspec->minimum == G_MINLONG && pspec->maximum == G_MAXLONG)
789 desc = g_strdup ("");
790 else if (pspec->minimum == G_MINLONG)
791 desc = g_strdup_printf ("<= %s", upper);
792 else if (pspec->maximum == G_MAXLONG)
793 desc = g_strdup_printf (">= %s", lower);
794 else
795 desc = g_strdup_printf ("[%s,%s]", lower, upper);
796 g_free (lower);
797 g_free (upper);
799 else if (G_IS_PARAM_SPEC_ULONG (spec)) {
800 GParamSpecULong *pspec = G_PARAM_SPEC_ULONG (spec);
802 lower = describe_unsigned_constant (sizeof(gulong), pspec->minimum);
803 upper = describe_unsigned_constant (sizeof(gulong), pspec->maximum);
804 if (pspec->minimum == 0 && pspec->maximum == G_MAXULONG)
805 desc = g_strdup ("");
806 else if (pspec->minimum == 0)
807 desc = g_strdup_printf ("<= %s", upper);
808 else if (pspec->maximum == G_MAXULONG)
809 desc = g_strdup_printf (">= %s", lower);
810 else
811 desc = g_strdup_printf ("[%s,%s]", lower, upper);
812 g_free (lower);
813 g_free (upper);
815 else if (G_IS_PARAM_SPEC_INT64 (spec)) {
816 GParamSpecInt64 *pspec = G_PARAM_SPEC_INT64 (spec);
818 lower = describe_signed_constant (sizeof(gint64), pspec->minimum);
819 upper = describe_signed_constant (sizeof(gint64), pspec->maximum);
820 if (pspec->minimum == G_MININT64 && pspec->maximum == G_MAXINT64)
821 desc = g_strdup ("");
822 else if (pspec->minimum == G_MININT64)
823 desc = g_strdup_printf ("<= %s", upper);
824 else if (pspec->maximum == G_MAXINT64)
825 desc = g_strdup_printf (">= %s", lower);
826 else
827 desc = g_strdup_printf ("[%s,%s]", lower, upper);
828 g_free (lower);
829 g_free (upper);
831 else if (G_IS_PARAM_SPEC_UINT64 (spec)) {
832 GParamSpecUInt64 *pspec = G_PARAM_SPEC_UINT64 (spec);
834 lower = describe_unsigned_constant (sizeof(guint64), pspec->minimum);
835 upper = describe_unsigned_constant (sizeof(guint64), pspec->maximum);
836 if (pspec->minimum == 0 && pspec->maximum == G_MAXUINT64)
837 desc = g_strdup ("");
838 else if (pspec->minimum == 0)
839 desc = g_strdup_printf ("<= %s", upper);
840 else if (pspec->maximum == G_MAXUINT64)
841 desc = g_strdup_printf (">= %s", lower);
842 else
843 desc = g_strdup_printf ("[%s,%s]", lower, upper);
844 g_free (lower);
845 g_free (upper);
847 else if (G_IS_PARAM_SPEC_FLOAT (spec)) {
848 GParamSpecFloat *pspec = G_PARAM_SPEC_FLOAT (spec);
850 lower = describe_double_constant (pspec->minimum);
851 upper = describe_double_constant (pspec->maximum);
852 if (GTKDOC_COMPARE_FLOAT (pspec->minimum, -G_MAXFLOAT)) {
853 if (GTKDOC_COMPARE_FLOAT (pspec->maximum, G_MAXFLOAT))
854 desc = g_strdup ("");
855 else
856 desc = g_strdup_printf ("<= %s", upper);
858 else if (GTKDOC_COMPARE_FLOAT (pspec->maximum, G_MAXFLOAT))
859 desc = g_strdup_printf (">= %s", lower);
860 else
861 desc = g_strdup_printf ("[%s,%s]", lower, upper);
862 g_free (lower);
863 g_free (upper);
865 else if (G_IS_PARAM_SPEC_DOUBLE (spec)) {
866 GParamSpecDouble *pspec = G_PARAM_SPEC_DOUBLE (spec);
868 lower = describe_double_constant (pspec->minimum);
869 upper = describe_double_constant (pspec->maximum);
870 if (GTKDOC_COMPARE_FLOAT (pspec->minimum, -G_MAXDOUBLE)) {
871 if (GTKDOC_COMPARE_FLOAT (pspec->maximum, G_MAXDOUBLE))
872 desc = g_strdup ("");
873 else
874 desc = g_strdup_printf ("<= %s", upper);
876 else if (GTKDOC_COMPARE_FLOAT (pspec->maximum, G_MAXDOUBLE))
877 desc = g_strdup_printf (">= %s", lower);
878 else
879 desc = g_strdup_printf ("[%s,%s]", lower, upper);
880 g_free (lower);
881 g_free (upper);
883 #if GLIB_CHECK_VERSION (2, 12, 0)
884 else if (G_IS_PARAM_SPEC_GTYPE (spec)) {
885 GParamSpecGType *pspec = G_PARAM_SPEC_GTYPE (spec);
886 gboolean is_pointer;
888 desc = g_strdup (get_type_name (pspec->is_a_type, &is_pointer));
890 #endif
891 #if GLIB_CHECK_VERSION (2, 25, 9)
892 else if (G_IS_PARAM_SPEC_VARIANT (spec)) {
893 GParamSpecVariant *pspec = G_PARAM_SPEC_VARIANT (spec);
894 gchar *variant_type;
896 variant_type = g_variant_type_dup_string (pspec->type);
897 desc = g_strdup_printf ("GVariant<%s>", variant_type);
898 g_free (variant_type);
900 #endif
901 else {
902 desc = g_strdup ("");
905 return desc;
908 static gchar*
909 describe_default (GParamSpec *spec)
911 gchar *desc;
913 if (G_IS_PARAM_SPEC_CHAR (spec)) {
914 GParamSpecChar *pspec = G_PARAM_SPEC_CHAR (spec);
916 desc = g_strdup_printf ("%d", pspec->default_value);
918 else if (G_IS_PARAM_SPEC_UCHAR (spec)) {
919 GParamSpecUChar *pspec = G_PARAM_SPEC_UCHAR (spec);
921 desc = g_strdup_printf ("%u", pspec->default_value);
923 else if (G_IS_PARAM_SPEC_BOOLEAN (spec)) {
924 GParamSpecBoolean *pspec = G_PARAM_SPEC_BOOLEAN (spec);
926 desc = g_strdup_printf ("%s", pspec->default_value ? "TRUE" : "FALSE");
928 else if (G_IS_PARAM_SPEC_INT (spec)) {
929 GParamSpecInt *pspec = G_PARAM_SPEC_INT (spec);
931 desc = g_strdup_printf ("%d", pspec->default_value);
933 else if (G_IS_PARAM_SPEC_UINT (spec)) {
934 GParamSpecUInt *pspec = G_PARAM_SPEC_UINT (spec);
936 desc = g_strdup_printf ("%u", pspec->default_value);
938 else if (G_IS_PARAM_SPEC_LONG (spec)) {
939 GParamSpecLong *pspec = G_PARAM_SPEC_LONG (spec);
941 desc = g_strdup_printf ("%ld", pspec->default_value);
943 else if (G_IS_PARAM_SPEC_LONG (spec)) {
944 GParamSpecULong *pspec = G_PARAM_SPEC_ULONG (spec);
946 desc = g_strdup_printf ("%lu", pspec->default_value);
948 else if (G_IS_PARAM_SPEC_INT64 (spec)) {
949 GParamSpecInt64 *pspec = G_PARAM_SPEC_INT64 (spec);
951 desc = g_strdup_printf ("%" G_GINT64_FORMAT, pspec->default_value);
953 else if (G_IS_PARAM_SPEC_UINT64 (spec))
955 GParamSpecUInt64 *pspec = G_PARAM_SPEC_UINT64 (spec);
957 desc = g_strdup_printf ("%" G_GUINT64_FORMAT, pspec->default_value);
959 else if (G_IS_PARAM_SPEC_UNICHAR (spec)) {
960 GParamSpecUnichar *pspec = G_PARAM_SPEC_UNICHAR (spec);
962 if (g_unichar_isprint (pspec->default_value))
963 desc = g_strdup_printf ("'%c'", pspec->default_value);
964 else
965 desc = g_strdup_printf ("%u", pspec->default_value);
967 else if (G_IS_PARAM_SPEC_ENUM (spec)) {
968 GParamSpecEnum *pspec = G_PARAM_SPEC_ENUM (spec);
970 GEnumValue *value = g_enum_get_value (pspec->enum_class, pspec->default_value);
971 if (value)
972 desc = g_strdup_printf ("%s", value->value_name);
973 else
974 desc = g_strdup_printf ("%d", pspec->default_value);
976 else if (G_IS_PARAM_SPEC_FLAGS (spec)) {
977 GParamSpecFlags *pspec = G_PARAM_SPEC_FLAGS (spec);
978 guint default_value;
979 GString *acc;
981 default_value = pspec->default_value;
982 acc = g_string_new ("");
984 while (default_value) {
985 GFlagsValue *value = g_flags_get_first_value (pspec->flags_class, default_value);
987 if (!value)
988 break;
990 if (acc->len > 0)
991 g_string_append (acc, " | ");
992 g_string_append (acc, value->value_name);
994 default_value &= ~value->value;
997 if (default_value == 0)
998 desc = g_string_free (acc, FALSE);
999 else {
1000 desc = g_strdup_printf ("%d", pspec->default_value);
1001 g_string_free (acc, TRUE);
1004 else if (G_IS_PARAM_SPEC_FLOAT (spec)) {
1005 GParamSpecFloat *pspec = G_PARAM_SPEC_FLOAT (spec);
1007 /* make sure floats are output with a decimal dot irrespective of
1008 * current locale. Use formatd since we want human-readable numbers
1009 * and do not need the exact same bit representation when deserialising */
1010 desc = g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
1011 g_ascii_formatd (desc, G_ASCII_DTOSTR_BUF_SIZE, "%g",
1012 pspec->default_value);
1014 else if (G_IS_PARAM_SPEC_DOUBLE (spec)) {
1015 GParamSpecDouble *pspec = G_PARAM_SPEC_DOUBLE (spec);
1017 /* make sure floats are output with a decimal dot irrespective of
1018 * current locale. Use formatd since we want human-readable numbers
1019 * and do not need the exact same bit representation when deserialising */
1020 desc = g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
1021 g_ascii_formatd (desc, G_ASCII_DTOSTR_BUF_SIZE, "%g",
1022 pspec->default_value);
1024 else if (G_IS_PARAM_SPEC_STRING (spec)) {
1025 GParamSpecString *pspec = G_PARAM_SPEC_STRING (spec);
1027 if (pspec->default_value) {
1028 gchar *esc = g_strescape (pspec->default_value, NULL);
1029 desc = g_strdup_printf ("\\"%s\\"", esc);
1030 g_free (esc);
1032 else
1033 desc = g_strdup_printf ("NULL");
1035 #if GLIB_CHECK_VERSION (2, 25, 9)
1036 else if (G_IS_PARAM_SPEC_VARIANT (spec)) {
1037 GParamSpecVariant *pspec = G_PARAM_SPEC_VARIANT (spec);
1039 if (pspec->default_value)
1040 desc = g_variant_print (pspec->default_value, TRUE);
1041 else
1042 desc = g_strdup ("NULL");
1044 #endif
1045 else {
1046 desc = g_strdup ("");
1049 return desc;
1053 static void
1054 output_object_args (FILE *fp, GType object_type)
1056 gpointer class;
1057 const gchar *object_class_name;
1058 guint arg;
1059 gchar flags[16], *pos;
1060 GParamSpec **properties;
1061 guint n_properties;
1062 gboolean child_prop;
1063 gboolean style_prop;
1064 gboolean is_pointer;
1065 const gchar *type_name;
1066 gchar *type_desc;
1067 gchar *default_value;
1069 if (G_TYPE_IS_OBJECT (object_type)) {
1070 class = g_type_class_peek (object_type);
1071 if (!class)
1072 return;
1074 properties = g_object_class_list_properties (class, &n_properties);
1076 #if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 3)
1077 else if (G_TYPE_IS_INTERFACE (object_type)) {
1078 class = g_type_default_interface_ref (object_type);
1080 if (!class)
1081 return;
1083 properties = g_object_interface_list_properties (class, &n_properties);
1085 #endif
1086 else
1087 return;
1089 object_class_name = g_type_name (object_type);
1091 child_prop = FALSE;
1092 style_prop = FALSE;
1094 while (TRUE) {
1095 qsort (properties, n_properties, sizeof (GParamSpec *), compare_param_specs);
1096 for (arg = 0; arg < n_properties; arg++) {
1097 GParamSpec *spec = properties[arg];
1098 const gchar *nick, *blurb, *dot;
1100 if (spec->owner_type != object_type)
1101 continue;
1103 pos = flags;
1104 /* We use one-character flags for simplicity. */
1105 if (child_prop && !style_prop)
1106 *pos++ = 'c';
1107 if (style_prop)
1108 *pos++ = 's';
1109 if (spec->flags & G_PARAM_READABLE)
1110 *pos++ = 'r';
1111 if (spec->flags & G_PARAM_WRITABLE)
1112 *pos++ = 'w';
1113 if (spec->flags & G_PARAM_CONSTRUCT)
1114 *pos++ = 'x';
1115 if (spec->flags & G_PARAM_CONSTRUCT_ONLY)
1116 *pos++ = 'X';
1117 *pos = 0;
1119 nick = g_param_spec_get_nick (spec);
1120 blurb = g_param_spec_get_blurb (spec);
1122 dot = "";
1123 if (blurb) {
1124 int str_len = strlen (blurb);
1125 if (str_len > 0 && blurb[str_len - 1] != '.')
1126 dot = ".";
1129 type_desc = describe_type (spec);
1130 default_value = describe_default (spec);
1131 type_name = get_type_name (spec->value_type, &is_pointer);
1132 fprintf (fp, "<ARG>\\n"
1133 "<NAME>%s::%s</NAME>\\n"
1134 "<TYPE>%s%s</TYPE>\\n"
1135 "<RANGE>%s</RANGE>\\n"
1136 "<FLAGS>%s</FLAGS>\\n"
1137 "<NICK>%s</NICK>\\n"
1138 "<BLURB>%s%s</BLURB>\\n"
1139 "<DEFAULT>%s</DEFAULT>\\n"
1140 "</ARG>\\n\\n",
1141 object_class_name, g_param_spec_get_name (spec), type_name,
1142 is_pointer ? "*" : "", type_desc, flags, nick ? nick : "(null)",
1143 blurb ? blurb : "(null)", dot, default_value);
1144 g_free (type_desc);
1145 g_free (default_value);
1148 g_free (properties);
1150 #ifdef GTK_IS_CONTAINER_CLASS
1151 if (!child_prop && GTK_IS_CONTAINER_CLASS (class)) {
1152 properties = gtk_container_class_list_child_properties (class, &n_properties);
1153 child_prop = TRUE;
1154 continue;
1156 #endif
1158 #ifdef GTK_IS_CELL_AREA_CLASS
1159 if (!child_prop && GTK_IS_CELL_AREA_CLASS (class)) {
1160 properties = gtk_cell_area_class_list_cell_properties (class, &n_properties);
1161 child_prop = TRUE;
1162 continue;
1164 #endif
1166 #ifdef GTK_IS_WIDGET_CLASS
1167 #if GTK_CHECK_VERSION(2,1,0) && !GTK_CHECK_VERSION(3,89,2)
1168 if (!style_prop && GTK_IS_WIDGET_CLASS (class)) {
1169 properties = gtk_widget_class_list_style_properties (GTK_WIDGET_CLASS (class), &n_properties);
1170 style_prop = TRUE;
1171 continue;
1173 #endif
1174 #endif
1178 MAIN_CODE_END = """
1179 break;
1185 def run(options):
1187 c_file = options.module + '-scan.c'
1188 output = open(c_file, 'w')
1190 base_filename = os.path.join(options.output_dir, options.module)
1191 old_signals_filename = base_filename + '.signals'
1192 new_signals_filename = base_filename + '.signals.new'
1193 old_hierarchy_filename = base_filename + '.hierarchy'
1194 new_hierarchy_filename = base_filename + '.hierarchy.new'
1195 old_interfaces_filename = base_filename + '.interfaces'
1196 new_interfaces_filename = base_filename + '.interfaces.new'
1197 old_prerequisites_filename = base_filename + '.prerequisites'
1198 new_prerequisites_filename = base_filename + '.prerequisites.new'
1199 old_args_filename = base_filename + '.args'
1200 new_args_filename = base_filename + '.args.new'
1202 # generate a C program to scan the types
1204 includes = ""
1205 forward_decls = ""
1206 get_types = ""
1207 ntypes = 1
1209 for line in open(options.types):
1210 if line.startswith('#include'):
1211 includes += line
1212 elif line.startswith('%') or line.strip() == '':
1213 continue
1214 else:
1215 line = line.strip()
1216 get_types += ' object_types[i++] = ' + line + ' ();\n'
1217 forward_decls += 'extern GType ' + line + ' (void);\n'
1218 ntypes += 1
1220 output.write(COMMON_INCLUDES)
1222 if includes:
1223 output.write(includes)
1224 else:
1225 output.write(forward_decls)
1227 if options.query_child_properties:
1228 output.write(QUERY_CHILD_PROPS_PROTOTYPE % options.query_child_properties)
1230 # substitute local vars in the template
1231 type_init_func = options.type_init_func
1232 output.write(string.Template(MAIN_CODE).substitute(locals()))
1234 if options.query_child_properties:
1235 output.write(QUERY_CHILD_PROPS_CODE % options.query_child_properties)
1237 output.write(MAIN_CODE_END)
1239 output.close()
1241 # Compile and run our file
1242 if 'libtool' in options.cc:
1243 o_file = options.module + '-scan.lo'
1244 else:
1245 o_file = options.module + '-scan.o'
1247 x_file = options.module + '-scan' + config.exeext
1249 stdout = ""
1250 if not options.verbose:
1251 stdout = ">/dev/null"
1253 # Compiling scanner
1254 command = '%s %s %s -c -o %s %s' % (options.cc, stdout, options.cflags, o_file, c_file)
1255 res = subprocess.check_call(command, shell=True)
1256 if res > 0:
1257 logging.warning('Compilation of scanner failed: %d', res)
1258 return res
1260 # Linking scanner
1261 command = '%s %s %s %s -o %s' % (options.ld, stdout, o_file, options.ldflags, x_file)
1262 res = subprocess.check_call(command, shell=True)
1263 if res > 0:
1264 logging.warning('Linking of scanner failed: %d', res)
1265 return res
1267 # Running scanner
1268 command = '%s ./%s' % (options.run, x_file)
1269 res = subprocess.check_call(command, shell=True)
1270 if res > 0:
1271 logging.warning('Running scanner failed: %d', res)
1272 return res
1274 if 'GTK_DOC_KEEP_INTERMEDIATE' not in os.environ:
1275 os.unlink(c_file)
1276 os.unlink(o_file)
1277 os.unlink(x_file)
1279 common.UpdateFileIfChanged(old_signals_filename, new_signals_filename, False)
1280 common.UpdateFileIfChanged(old_hierarchy_filename, new_hierarchy_filename, False)
1281 common.UpdateFileIfChanged(old_interfaces_filename, new_interfaces_filename, False)
1282 common.UpdateFileIfChanged(old_prerequisites_filename, new_prerequisites_filename, False)
1283 common.UpdateFileIfChanged(old_args_filename, new_args_filename, False)
1285 return 0