1 # GIMP - The GNU Image Manipulation Program
2 # Copyright (C) 1998-2003 Manish Singh <yosh@gimp.org>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 package Gimp
::CodeGen
::app
;
19 $destdir = "$main::destdir/app/pdb";
20 $builddir = "$main::builddir/app/pdb";
22 *arg_types
= \
%Gimp::CodeGen
::pdb
::arg_types
;
23 *arg_parse
= \
&Gimp
::CodeGen
::pdb
::arg_parse
;
25 *enums
= \
%Gimp::CodeGen
::enums
::enums
;
27 *write_file
= \
&Gimp
::CodeGen
::util
::write_file
;
28 *FILE_EXT
= \
$Gimp::CodeGen
::util
::FILE_EXT
;
30 use Text
::Wrap
qw(wrap);
33 my ($str, $indent, $subsequent_indent) = @_;
34 my $leading = ' ' x
$indent . '"';
35 my $subsequent_leading = ' ' x
$subsequent_indent . '"';
36 $Text::Wrap
::columns
= 1000;
37 $Text::Wrap
::unexpand
= 0;
38 $str = wrap
($leading, $subsequent_leading, $str);
40 $str =~ s/(.)\n(.)/$1\\n"\n$2/g;
45 sub format_code_frag
{
46 my ($code, $indent) = @_;
49 $code =~ s/\t/' ' x 8/eg;
51 if (!$indent && $code =~ /^\s*{\s*\n.*\n\s*}\s*$/s) {
52 $code =~ s/^\s*{\s*\n//s;
53 $code =~ s/\n\s*}\s*$//s;
56 $code =~ s/^/' ' x ($indent ? 4 : 2)/meg;
73 my @args = @
{$proc->{$_}} if (defined $proc->{$_});
76 my ($type, $name) = &arg_parse
($_->{type
});
77 my $arg = $arg_types{$type};
79 if ($arg->{array
} && !exists $_->{array
}) {
80 warn "Array without number of elements param in $proc->{name}";
83 unless (exists $_->{no_declare
} || exists $_->{dead
}) {
85 $result .= " $arg->{type}$_->{name} = $arg->{init_value}";
88 $result .= " $arg->{const_type}$_->{name}";
92 if (exists $arg->{headers
}) {
93 foreach (@
{$arg->{headers
}}) {
94 $out->{headers
}->{$_}++;
105 my ($proc, $argc) = @_;
110 my @inargs = @
{$proc->{inargs
}} if (defined $proc->{inargs
});
113 my($pdbtype, @typeinfo) = &arg_parse
($_->{type
});
114 my $arg = $arg_types{$pdbtype};
115 my $var = $_->{name
};
118 $value = "gimp_value_array_index (args, $argc)";
119 if (!exists $_->{dead
}) {
120 $result .= eval qq/" $arg->{get_value_func};\n"/;
125 if (!exists $_->{no_validate
}) {
130 $result = "\n" . $result . "\n" if $result;
134 sub marshal_outargs
{
138 my @outargs = @
{$proc->{outargs
}} if (defined $proc->{outargs
});
142 return_vals = gimp_procedure_get_return_values (procedure, success,
143 error ? *error : NULL);
147 return_vals = gimp_procedure_get_return_values (procedure, TRUE, NULL);
151 if (scalar @outargs) {
154 foreach (@
{$proc->{outargs
}}) {
155 my ($pdbtype) = &arg_parse
($_->{type
});
156 my $arg = $arg_types{$pdbtype};
157 my $var = $_->{name
};
163 $value = "gimp_value_array_index (return_vals, $argc)";
165 if (exists $_->{array
}) {
166 my $arrayarg = $_->{array
};
168 if (exists $arrayarg->{name
}) {
169 $var_len = $arrayarg->{name
};
172 $var_len = 'num_' . $_->{name
};
176 $outargs .= eval qq/" $arg->{set_value_func};\n"/;
179 $outargs =~ s/^/' ' x 2/meg if $success;
180 $outargs =~ s/^/' ' x 2/meg if $success && $argc > 1;
182 $result .= "\n" if $success || $argc > 1;
183 $result .= ' ' x
2 . "if (success)\n" if $success;
184 $result .= ' ' x
4 . "{\n" if $success && $argc > 1;
186 $result .= ' ' x
4 . "}\n" if $success && $argc > 1;
187 $result .= "\n" . ' ' x
2 . "return return_vals;\n";
191 $result =~ s/return_vals =/return/;
192 $result =~ s/ error/error/;
195 $result =~ s/ return_vals =/\n return/;
196 $result =~ s/ error/error/;
205 my ($pdbtype, @typeinfo) = &arg_parse
($arg->{type
});
206 my $name = $arg->{canonical_name
};
207 my $nick = $arg->{canonical_name
};
208 my $blurb = exists $arg->{desc
} ?
$arg->{desc
} : "";
212 my $flags = 'GIMP_PARAM_READWRITE';
218 if (exists $arg->{no_validate
}) {
219 $flags .= ' | GIMP_PARAM_NO_VALIDATE';
222 if ($pdbtype eq 'image') {
223 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
225 gimp_param_spec_image_id ("$name",
232 elsif ($pdbtype eq 'item') {
233 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
235 gimp_param_spec_item_id ("$name",
242 elsif ($pdbtype eq 'drawable') {
243 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
245 gimp_param_spec_drawable_id ("$name",
252 elsif ($pdbtype eq 'layer') {
253 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
255 gimp_param_spec_layer_id ("$name",
262 elsif ($pdbtype eq 'channel') {
263 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
265 gimp_param_spec_channel_id ("$name",
272 elsif ($pdbtype eq 'layer_mask') {
273 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
275 gimp_param_spec_layer_mask_id ("$name",
282 elsif ($pdbtype eq 'selection') {
283 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
285 gimp_param_spec_selection_id ("$name",
292 elsif ($pdbtype eq 'vectors') {
293 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
295 gimp_param_spec_vectors_id ("$name",
302 elsif ($pdbtype eq 'display') {
303 $none_ok = exists $arg->{none_ok
} ?
'TRUE' : 'FALSE';
305 gimp_param_spec_display_id ("$name",
312 elsif ($pdbtype eq 'tattoo') {
314 g_param_spec_uint ("$name",
321 elsif ($pdbtype eq 'guide') {
323 g_param_spec_uint ("$name",
330 elsif ($pdbtype eq 'sample_point') {
332 g_param_spec_uint ("$name",
339 elsif ($pdbtype eq 'float') {
340 $min = defined $typeinfo[0] ?
$typeinfo[0] : -G_MAXDOUBLE
;
341 $max = defined $typeinfo[2] ?
$typeinfo[2] : G_MAXDOUBLE
;
342 $default = exists $arg->{default} ?
$arg->{default} : defined $typeinfo[0] ?
$typeinfo[0] : 0.0;
344 g_param_spec_double ("$name",
347 $min, $max, $default,
351 elsif ($pdbtype eq 'int32') {
352 if (defined $typeinfo[0]) {
353 $min = ($typeinfo[1] eq '<') ?
($typeinfo[0] + 1) : $typeinfo[0];
358 if (defined $typeinfo[2]) {
359 $max = ($typeinfo[3] eq '<') ?
($typeinfo[2] - 1) : $typeinfo[2];
364 $default = exists $arg->{default} ?
$arg->{default} : defined $typeinfo[0] ?
$typeinfo[0] : 0;
366 gimp_param_spec_int32 ("$name",
369 $min, $max, $default,
373 elsif ($pdbtype eq 'int16') {
374 if (defined $typeinfo[0]) {
375 $min = ($typeinfo[1] eq '<') ?
($typeinfo[0] + 1) : $typeinfo[0];
380 if (defined $typeinfo[2]) {
381 $max = ($typeinfo[3] eq '<') ?
($typeinfo[2] - 1) : $typeinfo[2];
386 $default = exists $arg->{default} ?
$arg->{default} : defined $typeinfo[0] ?
$typeinfo[0] : 0;
388 gimp_param_spec_int16 ("$name",
391 $min, $max, $default,
395 elsif ($pdbtype eq 'int8') {
396 if (defined $typeinfo[0]) {
397 $min = ($typeinfo[1] eq '<') ?
($typeinfo[0] + 1) : $typeinfo[0];
402 if (defined $typeinfo[2]) {
403 $max = ($typeinfo[3] eq '<') ?
($typeinfo[2] - 1) : $typeinfo[2];
408 $default = exists $arg->{default} ?
$arg->{default} : defined $typeinfo[0] ?
$typeinfo[0] : 0;
410 gimp_param_spec_int8 ("$name",
413 $min, $max, $default,
417 elsif ($pdbtype eq 'boolean') {
418 $default = exists $arg->{default} ?
$arg->{default} : FALSE
;
420 g_param_spec_boolean ("$name",
427 elsif ($pdbtype eq 'string') {
428 $allow_non_utf8 = exists $arg->{allow_non_utf8
} ?
'TRUE' : 'FALSE';
429 $null_ok = exists $arg->{null_ok
} ?
'TRUE' : 'FALSE';
430 $non_empty = exists $arg->{non_empty
} ?
'TRUE' : 'FALSE';
431 $default = exists $arg->{default} ?
$arg->{default} : NULL
;
433 gimp_param_spec_string ("$name",
436 $allow_non_utf8, $null_ok, $non_empty,
441 elsif ($pdbtype eq 'enum') {
442 $enum_type = $typeinfo[0];
443 $enum_type =~ s/([a-z])([A-Z])/$1_$2/g;
444 $enum_type =~ s/([A-Z]+)([A-Z])/$1_$2/g;
445 $enum_type =~ tr/[a-z]/[A-Z]/;
446 $enum_type =~ s/^GIMP/GIMP_TYPE/;
447 $enum_type =~ s/^GEGL/GEGL_TYPE/;
448 $default = exists $arg->{default} ?
$arg->{default} : $enums{$typeinfo[0]}->{symbols
}[0];
450 my ($foo, $bar, @remove) = &arg_parse
($arg->{type
});
453 $postproc .= 'gimp_param_spec_enum_exclude_value (GIMP_PARAM_SPEC_ENUM ($pspec),';
454 $postproc .= "\n $_);\n";
457 if ($postproc eq '') {
459 g_param_spec_enum ("$name",
469 gimp_param_spec_enum ("$name",
478 elsif ($pdbtype eq 'unit') {
479 $typeinfo[0] = 'GIMP_UNIT_PIXEL' unless defined $typeinfo[0];
480 $allow_pixels = $typeinfo[0] eq 'GIMP_UNIT_PIXEL' ? TRUE
: FALSE
;
481 $allow_percent = exists $arg->{allow_percent
} ? TRUE
: FALSE
;
482 $default = exists $arg->{default} ?
$arg->{default} : $typeinfo[0];
484 gimp_param_spec_unit ("$name",
493 elsif ($pdbtype eq 'color') {
494 $has_alpha = exists $arg->{has_alpha
} ? TRUE
: FALSE
;
495 $default = exists $arg->{default} ?
$arg->{default} : NULL
;
497 gimp_param_spec_rgb ("$name",
505 elsif ($pdbtype eq 'parasite') {
507 gimp_param_spec_parasite ("$name",
513 elsif ($pdbtype eq 'int32array') {
515 gimp_param_spec_int32_array ("$name",
521 elsif ($pdbtype eq 'int16array') {
523 gimp_param_spec_int16_array ("$name",
529 elsif ($pdbtype eq 'int8array') {
531 gimp_param_spec_int8_array ("$name",
537 elsif ($pdbtype eq 'floatarray') {
539 gimp_param_spec_float_array ("$name",
545 elsif ($pdbtype eq 'stringarray') {
547 gimp_param_spec_string_array ("$name",
553 elsif ($pdbtype eq 'colorarray') {
555 gimp_param_spec_color_array ("$name",
562 warn "Unsupported PDB type: $arg->{name} ($arg->{type})";
568 return ($pspec, $postproc);
572 $_ = shift; s/_/-/g; return $_;
576 my @procs = @
{(shift)};
581 foreach $name (@procs) {
582 my $proc = $main::pdb
{$name};
583 my $out = \
%{$out{$proc->{group
}}};
585 my @inargs = @
{$proc->{inargs
}} if (defined $proc->{inargs
});
586 my @outargs = @
{$proc->{outargs
}} if (defined $proc->{outargs
});
588 my $blurb = $proc->{blurb
};
589 my $help = $proc->{help
};
595 if ($proc->{deprecated
}) {
596 if ($proc->{deprecated
} eq 'NONE') {
598 $blurb = "Deprecated: There is no replacement for this procedure.";
603 $help .= "Deprecated: There is no replacement for this procedure.";
607 $blurb = "Deprecated: Use '$proc->{deprecated}' instead.";
612 $help .= "Deprecated: Use '$proc->{deprecated}' instead.";
616 $help =~ s/gimp(\w+)\(\s*\)/"'gimp".canonicalize($1)."'"/ge;
618 if ($proc->{group
} eq "plug_in_compat") {
619 $procedure_name = "$proc->{canonical_name}";
621 $procedure_name = "gimp-$proc->{canonical_name}";
624 $out->{pcount
}++; $total++;
626 $out->{register
} .= <<CODE;
629 * gimp-$proc->{canonical_name}
631 procedure = gimp_procedure_new (${name}_invoker);
632 gimp_object_set_static_name (GIMP_OBJECT (procedure),
634 gimp_procedure_set_static_strings (procedure,
636 @{[ "ewrap($blurb, 2, 37) ]},
637 @{[ "ewrap($help, 2, 37) ]},
639 "$proc->{copyright}",
641 @{[$proc->{deprecated} ? "\"$proc->{deprecated}\"" : 'NULL']});
646 foreach $arg (@inargs) {
647 my ($pspec, $postproc) = &generate_pspec
($arg);
649 $pspec =~ s/^/' ' x length(" gimp_procedure_add_argument (")/meg;
651 $out->{register
} .= <<CODE;
652 gimp_procedure_add_argument (procedure,
656 if ($postproc ne '') {
657 $pspec = "procedure->args[$argc]";
658 $postproc =~ s/^/' '/meg;
659 $out->{register
} .= eval qq/"$postproc"/;
667 foreach $arg (@outargs) {
668 my ($pspec, $postproc) = &generate_pspec
($arg);
671 $pspec =~ s/^/' ' x length(" gimp_procedure_add_return_value (")/meg;
673 $out->{register
} .= <<CODE;
674 gimp_procedure_add_return_value (procedure,
678 if ($postproc ne '') {
679 $pspec = "procedure->values[$argc]";
680 $postproc =~ s/^/' '/meg;
681 $out->{register
} .= eval qq/"$postproc"/;
687 $out->{register
} .= <<CODE;
688 gimp_pdb_register_procedure (pdb, procedure);
689 g_object_unref (procedure);
692 if (exists $proc->{invoke
}->{headers
}) {
693 foreach $header (@
{$proc->{invoke
}->{headers
}}) {
694 $out->{headers
}->{$header}++;
698 $out->{code
} .= "\nstatic GimpValueArray *\n";
699 $out->{code
} .= "${name}_invoker (GimpProcedure *procedure,\n";
700 $out->{code
} .= ' ' x
length($name) . " Gimp *gimp,\n";
701 $out->{code
} .= ' ' x
length($name) . " GimpContext *context,\n";
702 $out->{code
} .= ' ' x
length($name) . " GimpProgress *progress,\n";
703 $out->{code
} .= ' ' x
length($name) . " const GimpValueArray *args,\n";
704 $out->{code
} .= ' ' x
length($name) . " GError **error)\n{\n";
708 if (exists $proc->{invoke
}->{no_marshalling
}) {
709 $code .= &format_code_frag
($proc->{invoke
}->{code
}, 0) . "}\n";
714 $invoker .= ' ' x
2 . "GimpValueArray *return_vals;\n" if scalar @outargs;
715 $invoker .= &declare_args
($proc, $out, 0, qw(inargs));
716 $invoker .= &declare_args
($proc, $out, 1, qw(outargs));
718 $invoker .= &marshal_inargs
($proc, 0);
719 $invoker .= "\n" if $invoker && $invoker !~ /\n\n/s;
723 if (exists $proc->{invoke
}->{code
}) {
724 $frag = &format_code_frag
($proc->{invoke
}->{code
}, $success);
725 $frag = ' ' x
2 . "if (success)\n" . $frag if $success;
726 $success = ($frag =~ /success =/) unless $success;
729 chomp $invoker if !$frag;
730 $code .= $invoker . $frag;
731 $code .= "\n" if $frag =~ /\n\n/s || $invoker;
732 $code .= &marshal_outargs
($proc) . "}\n";
736 $out->{code
} .= ' ' x
2 . "gboolean success";
737 unless ($proc->{invoke
}->{success
} eq 'NONE') {
738 $out->{code
} .= " = $proc->{invoke}->{success}";
740 $out->{code
} .= ";\n";
743 $out->{code
} .= $code;
747 /* GIMP - The GNU Image Manipulation Program
748 * Copyright (C) 1995-2003 Spencer Kimball and Peter Mattis
750 * This program is free software: you can redistribute it and/or modify
751 * it under the terms of the GNU General Public License as published by
752 * the Free Software Foundation; either version 3 of the License, or
753 * (at your option) any later version.
755 * This program is distributed in the hope that it will be useful,
756 * but WITHOUT ANY WARRANTY; without even the implied warranty of
757 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
758 * GNU General Public License for more details.
760 * You should have received a copy of the GNU General Public License
761 * along with this program. If not, see <https://www.gnu.org/licenses/>.
764 /* NOTE: This file is auto-generated by pdbgen.pl. */
768 my $group_procs = "";
773 foreach $group (@main::groups
) {
774 my $out = $out{$group};
776 foreach (@
{$main::grp
{$group}->{headers
}}) { $out->{headers
}->{$_}++ }
778 $out->{headers
}->{"\"core/gimpparamspecs.h\""}++;
781 my ($x, $y) = ($a, $b);
791 } keys %{$out->{headers
}};
806 if ($sys == 0 && !/^</) {
808 $headers .= "\n" if $seen;
809 $headers .= "#include <gegl.h>\n\n";
810 $headers .= "#include <gdk-pixbuf/gdk-pixbuf.h>\n\n";
819 $headers .= "\n" if $lib;
822 if ($sys == 1 && $base == 0) {
824 $headers .= "#include \"libgimpbase/gimpbase.h\"\n\n";
825 $headers .= "#include \"pdb-types.h\"\n\n";
832 elsif (/gimppdb-utils/) {
835 elsif (/gimppdberror/) {
838 elsif (/gimppdbcontext/) {
842 $headers .= "#include $_\n";
847 $headers .= "#include \"gimppdb.h\"\n";
848 $headers .= "#include \"gimppdberror.h\"\n" if $error;
849 $headers .= "#include \"gimppdb-utils.h\"\n" if $utils;
850 $headers .= "#include \"gimppdbcontext.h\"\n" if $context;
851 $headers .= "#include \"gimpprocedure.h\"\n";
852 $headers .= "#include \"internal-procs.h\"\n";
854 $headers .= "\n#include \"gimp-intl.h\"\n" if $intl;
857 if (exists $main::grp
{$group}->{extra
}->{app
}) {
858 $extra = $main::grp
{$group}->{extra
}->{app
}
861 my $cfile = "$builddir/".canonicalize
(${group
})."-cmds.c$FILE_EXT";
862 open CFILE
, "> $cfile" or die "Can't open $cfile: $!\n";
864 print CFILE
qq/#include "config.h"\n\n/;
865 print CFILE
$headers, "\n";
866 print CFILE
$extra->{decls
}, "\n" if exists $extra->{decls
};
867 print CFILE
"\n", $extra->{code
} if exists $extra->{code
};
868 print CFILE
$out->{code
};
869 print CFILE
"\nvoid\nregister_${group}_procs (GimpPDB *pdb)\n";
870 print CFILE
"{\n GimpProcedure *procedure;\n$out->{register}}\n";
872 &write_file
($cfile, $destdir);
874 my $decl = "register_${group}_procs";
875 push @group_decls, $decl;
876 $longest = length $decl if $longest < length $decl;
878 $group_procs .= ' ' x
2 . "register_${group}_procs (pdb);\n";
879 $pcount += $out->{pcount
};
882 if (! $ENV{PDBGEN_GROUPS
}) {
883 my $internal = "$builddir/internal-procs.h$FILE_EXT";
884 open IFILE
, "> $internal" or die "Can't open $internal: $!\n";
886 my $guard = "__INTERNAL_PROCS_H__";
887 print IFILE
<<HEADER;
893 print IFILE
"void internal_procs_init" . ' ' x
($longest - length "internal_procs_init") . " (GimpPDB *pdb);\n\n";
895 print IFILE
"/* Forward declarations for registering PDB procs */\n\n";
896 foreach (@group_decls) {
897 print IFILE
"void $_" . ' ' x
($longest - length $_) . " (GimpPDB *pdb);\n";
900 print IFILE
<<HEADER;
905 &write_file
($internal, $destdir);
907 $internal = "$builddir/internal-procs.c$FILE_EXT";
908 open IFILE
, "> $internal" or die "Can't open $internal: $!\n";
910 print IFILE
qq@#include "config.h"\n\n@
;
911 print IFILE
qq@#include <glib
-object
.h
>\n\n@
;
912 print IFILE
qq@#include "pdb-types.h"\n\n@
;
913 print IFILE
qq@#include "gimppdb.h"\n\n@
;
914 print IFILE
qq@#include "internal-procs.h"\n\n@
;
916 print IFILE
"\n/* $total procedures registered total */\n\n";
919 internal_procs_init (GimpPDB *pdb)
921 g_return_if_fail (GIMP_IS_PDB (pdb));
927 &write_file
($internal, $destdir);