2001-10-11 10:14:14 +00:00
|
|
|
#!/usr/bin/perl
|
2001-10-25 22:16:10 +00:00
|
|
|
#
|
|
|
|
# defs-parse.pl : Gtk+ defs format parser and code generator.
|
|
|
|
#
|
|
|
|
# Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
#
|
|
|
|
# <c> 2001 Mike Kestner
|
2001-10-11 10:14:14 +00:00
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
%maptypes = (
|
|
|
|
'none', "void", 'gboolean', "bool", 'gint', "int", 'guint', "uint",
|
|
|
|
'guint32', "uint", 'const-gchar', "String", 'GObject', "GLib.Object",
|
2002-01-04 02:02:28 +00:00
|
|
|
'gchar', "String", 'gfloat', "float", 'gdouble', "double",
|
|
|
|
'GList', "IntPtr", 'GSList', "IntPtr", 'gpointer', "IntPtr",
|
|
|
|
'long', "long", 'gint8', "byte", 'guint8', "byte", 'gint16', "short",
|
|
|
|
'guint16', "ushort", 'char', "String", 'GPtrArray', "IntPtr[]",
|
|
|
|
'const-char', "String", 'gushort', "ushort", 'gshort', "short",
|
|
|
|
'guint1', "bool", 'guchar', "byte", 'GValue', "GLib.Value",
|
|
|
|
'GtkType', "int", 'glong', "long", 'gulong', "ulong", 'GQuark', "int",
|
|
|
|
'va_list', "IntPtr", 'GParamSpec', "IntPtr", 'int', "int",
|
|
|
|
'double', "double", 'gunichar', "String", 'uint1', "bool",
|
|
|
|
'GtkSignalFunc', "IntPtr");
|
2001-10-11 10:14:14 +00:00
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
%marshaltypes = (
|
|
|
|
'none', "void", 'gboolean', "bool", 'gint', "int", 'guint', "uint",
|
|
|
|
'guint32', "uint", 'const-gchar', "IntPtr", 'GObject', "IntPtr",
|
2002-01-04 02:02:28 +00:00
|
|
|
'gchar', "IntPtr", 'gfloat', "float", 'gdouble', "double",
|
|
|
|
'GList', "IntPtr", 'GSList', "IntPtr", 'gpointer', "IntPtr",
|
|
|
|
'long', "long", 'gint8', "byte", 'guint8', "byte", 'gint16', "short",
|
|
|
|
'guint16', "ushort", 'char', "IntPtr", 'GPtrArray', "IntPtr[]",
|
|
|
|
'const-char', "IntPtr", 'gushort', "ushort", 'gshort', "short",
|
|
|
|
'guint1', "bool", 'guchar', "byte", 'GValue', "GLib.Value",
|
|
|
|
'GtkType', "int", 'glong', "long", 'gulong', "ulong", 'GQuark', "int",
|
|
|
|
'va_list', "IntPtr", 'GParamSpec', "IntPtr", 'int', "int",
|
|
|
|
'double', "double", 'gunichar', "Unicode", 'uint1', "bool",
|
|
|
|
'GtkSignalFunc', "IntPtr");
|
2001-10-25 22:16:10 +00:00
|
|
|
|
2001-11-07 23:13:05 +00:00
|
|
|
|
|
|
|
%usings = (
|
2001-11-25 00:25:47 +00:00
|
|
|
'GLib', "System,System.Collections,System.Runtime.InteropServices,GLib",
|
2002-01-04 02:02:28 +00:00
|
|
|
'Pango', "System,System.Collections,System.Runtime.InteropServices,GLib,Pango",
|
|
|
|
'Gdk', "System,System.Collections,System.Runtime.InteropServices,GLib,Pango,Gdk",
|
|
|
|
'Gtk', "System,System.Collections,System.Runtime.InteropServices,GLib,Pango,Gdk,Gtk,GtkSharp",
|
|
|
|
'GtkSharp', "System,System.Collections,System.Runtime.InteropServices,GLib,Pango,Gdk,Gtk");
|
2001-11-07 23:13:05 +00:00
|
|
|
|
2001-11-25 00:25:47 +00:00
|
|
|
`mkdir -p ../glib/generated`;
|
2002-01-04 02:02:28 +00:00
|
|
|
`mkdir -p ../pango/generated`;
|
2001-10-31 01:31:05 +00:00
|
|
|
`mkdir -p ../gdk/generated`;
|
|
|
|
`mkdir -p ../gtk/generated`;
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
while ($def = get_def()) {
|
|
|
|
|
|
|
|
if ($def =~ /^\(define-(enum|flags)/) {
|
|
|
|
gen_enum (split (/\n/, $def));
|
2001-11-03 00:45:15 +00:00
|
|
|
} elsif ($def =~ /^\(define-struct (\w+)/) {
|
|
|
|
$name = $1;
|
2001-11-08 01:35:11 +00:00
|
|
|
$def =~ /c-name "(\w+)"/;
|
|
|
|
$cname=$1;
|
2001-12-31 10:40:48 +00:00
|
|
|
$def =~ s/\r?\n\s*//g;
|
2001-12-04 19:34:26 +00:00
|
|
|
$structs{$cname} = $def;
|
2001-11-08 01:35:11 +00:00
|
|
|
$maptypes{$cname} = $name;
|
|
|
|
$marshaltypes{$cname} = $name;
|
2001-10-25 22:16:10 +00:00
|
|
|
} elsif ($def =~ /^\(define-object (\w+)/) {
|
|
|
|
$name = $1;
|
|
|
|
$def =~ /c-name "(\w+)"/;
|
|
|
|
$cname=$1;
|
2001-12-31 10:40:48 +00:00
|
|
|
$def =~ s/\r?\n\s*//g;
|
2001-10-25 22:16:10 +00:00
|
|
|
$objects{$cname} = $def;
|
|
|
|
$maptypes{$cname} = $name;
|
|
|
|
$marshaltypes{$cname} = "IntPtr";
|
2001-11-14 23:45:44 +00:00
|
|
|
} elsif ($def =~ /^\(define-(prop|signal|method)/) {
|
2001-10-25 22:16:10 +00:00
|
|
|
$def =~ /of-object "(\w+)"/;
|
|
|
|
$cname=$1;
|
2002-01-04 02:02:28 +00:00
|
|
|
if (exists($objects{$cname})) {
|
|
|
|
$def =~ s/\r?\n\s*//g;
|
|
|
|
$objects{$cname} .= "\n$def";
|
|
|
|
}
|
2001-11-03 00:45:15 +00:00
|
|
|
} elsif ($def =~ /^\(define-function/) {
|
2002-01-04 02:02:28 +00:00
|
|
|
if (($def =~ /is-constructor-of (\w+)\)/) &&
|
|
|
|
(exists($objects{$1}))) {
|
2001-11-03 00:45:15 +00:00
|
|
|
$cname=$1;
|
2001-12-31 10:40:48 +00:00
|
|
|
$def =~ s/\r?\n\s*//g;
|
2001-11-03 00:45:15 +00:00
|
|
|
$objects{$cname} .= "\n$def";
|
|
|
|
}
|
2001-10-25 22:16:10 +00:00
|
|
|
} elsif ($def =~ /^\(define-(interface)/) {
|
|
|
|
# Nothing much to do here, I think.
|
2001-11-03 00:45:15 +00:00
|
|
|
} elsif ($def =~ /^\(define-boxed/) {
|
2001-10-25 22:16:10 +00:00
|
|
|
# Probably need to handle these though...
|
|
|
|
} else {
|
|
|
|
die "Unexpected definition $def\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-11-08 01:35:11 +00:00
|
|
|
foreach $key (keys (%structs)) {
|
2002-01-04 02:02:28 +00:00
|
|
|
next if ($key =~ /GtkTree|GtkText/);
|
2001-11-08 01:35:11 +00:00
|
|
|
gen_struct ($key, $structs{$key});
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach $key (keys (%objects)) {
|
2002-01-04 02:02:28 +00:00
|
|
|
next if ($key =~ /GtkTree|GtkText/);
|
2001-10-25 22:16:10 +00:00
|
|
|
gen_object (split (/\n/, $objects{$key}));
|
|
|
|
}
|
|
|
|
|
|
|
|
###############
|
|
|
|
# subroutines
|
|
|
|
###############
|
|
|
|
|
|
|
|
# Gets a single definition from the input stream.
|
|
|
|
sub get_def
|
|
|
|
{
|
|
|
|
while ($line = <STDIN>) {
|
|
|
|
next if ($line !~ /^\(define/);
|
|
|
|
$expr = $line;
|
|
|
|
do {
|
|
|
|
$line = <STDIN>;
|
|
|
|
$expr .= $line;
|
|
|
|
} until ($line =~ /^\)/);
|
|
|
|
return $expr;
|
2001-10-11 10:14:14 +00:00
|
|
|
}
|
2001-10-25 22:16:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Converts a dash or underscore separated name to StudlyCaps.
|
|
|
|
sub StudCaps
|
|
|
|
{
|
|
|
|
my ($symb) = @_;
|
|
|
|
$symb =~ s/^([a-z])/\u\1/;
|
|
|
|
$symb =~ s/[-_]([a-z])/\u\1/g;
|
|
|
|
$symb =~ s/[-_](\d)/\1/g;
|
|
|
|
return $symb;
|
2001-10-11 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
# Code generation for the enum and flags definitions.
|
|
|
|
sub gen_enum
|
2001-10-11 10:14:14 +00:00
|
|
|
{
|
2001-10-25 22:16:10 +00:00
|
|
|
my (@lines) = @_;
|
|
|
|
$line = $lines[$pos=0];
|
2001-10-11 10:14:14 +00:00
|
|
|
$line =~ /^\(define-(enum|flags) (\w+)/;
|
|
|
|
$type = $1;
|
|
|
|
$typename = $2;
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
$line = $lines[++$pos];
|
2001-10-11 10:14:14 +00:00
|
|
|
$line =~ /\(in-module "(\w+)"/;
|
|
|
|
$namespace = $1;
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
$maptypes{"$namespace$typename"} = $typename;
|
|
|
|
$marshaltypes{"$namespace$typename"} = "int";
|
|
|
|
|
|
|
|
do { $line = $lines[++$pos]; } until ($line =~ /\(values/);
|
2001-10-11 10:14:14 +00:00
|
|
|
|
|
|
|
@enums = ();
|
2001-10-25 22:16:10 +00:00
|
|
|
while ($line = $lines[++$pos]) {
|
2001-10-11 10:14:14 +00:00
|
|
|
last if ($line =~ /^\s*\)/);
|
|
|
|
if ($line =~ /\((.+)\)/) {
|
|
|
|
($name, $dontcare, $val) = split (/ /, $1);
|
|
|
|
$name =~ s/\"//g;
|
2001-10-25 22:16:10 +00:00
|
|
|
$name = StudCaps ($name);
|
2001-10-11 10:14:14 +00:00
|
|
|
@enums = (@enums, "$name:$val");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-31 01:31:05 +00:00
|
|
|
$dir = "../" . lc ($namespace) . "/generated";
|
2001-10-11 10:14:14 +00:00
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
open (OUTFILE, ">$dir/$typename.cs") || die "can't open file";
|
2001-10-11 10:14:14 +00:00
|
|
|
|
|
|
|
print OUTFILE "// Generated file: Do not modify\n\n";
|
|
|
|
print OUTFILE "namespace $namespace {\n\n";
|
|
|
|
print OUTFILE "\t/// <summary> $typename Enumeration </summary>\n";
|
2001-10-25 22:16:10 +00:00
|
|
|
print OUTFILE "\t/// <remarks> Valid values:\n";
|
|
|
|
print OUTFILE "\t///\t<list type = \"bullet\">\n";
|
2001-10-11 10:14:14 +00:00
|
|
|
foreach $enum (@enums) {
|
|
|
|
($name) = split (/:/, $enum);
|
2001-10-25 22:16:10 +00:00
|
|
|
print OUTFILE "\t///\t\t<item> $name </item>\n"
|
2001-10-11 10:14:14 +00:00
|
|
|
}
|
2001-10-25 22:16:10 +00:00
|
|
|
print OUTFILE "\t///\t</list>\n\t/// </remarks>\n\n";
|
2001-10-11 10:14:14 +00:00
|
|
|
|
|
|
|
if ($type eq "flags") {
|
|
|
|
print OUTFILE "\tusing System;\n\n\t[Flags]\n";
|
|
|
|
}
|
|
|
|
print OUTFILE "\tpublic enum $typename {\n";
|
|
|
|
|
|
|
|
$flag = 1;
|
|
|
|
foreach $enum (@enums) {
|
|
|
|
($name, $val) = split (/:/, $enum);
|
|
|
|
if ($val) {
|
|
|
|
print OUTFILE "\t\t$name = $val,\n";
|
|
|
|
} elsif ($type eq "enum") {
|
|
|
|
print OUTFILE "\t\t$name,\n";
|
|
|
|
} else {
|
|
|
|
print OUTFILE "\t\t$name = $flag,\n";
|
|
|
|
$flag *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print OUTFILE "\t}\n\n}\n";
|
2001-10-25 22:16:10 +00:00
|
|
|
close (OUTFILE);
|
|
|
|
}
|
|
|
|
|
2001-12-04 19:34:26 +00:00
|
|
|
# Code generation for the structure definitions.
|
2001-11-04 15:42:46 +00:00
|
|
|
sub gen_struct
|
|
|
|
{
|
2001-12-04 19:34:26 +00:00
|
|
|
my ($cname, $def) = @_;
|
2001-11-04 15:42:46 +00:00
|
|
|
|
2001-12-04 19:34:26 +00:00
|
|
|
$def =~ /define-struct (\w+)/;
|
|
|
|
$name = $1;
|
2002-01-04 02:02:28 +00:00
|
|
|
|
2001-11-04 15:42:46 +00:00
|
|
|
$def =~ /in-module "(\w+)"/;
|
|
|
|
$namespace = $1;
|
|
|
|
|
|
|
|
$dir = "../" . lc ($namespace) . "/generated";
|
|
|
|
open (OUTFILE, ">$dir/$name.cs") || die "can't open file";
|
|
|
|
|
|
|
|
print OUTFILE "// Generated file: Do not modify\n\n";
|
|
|
|
print OUTFILE "namespace $namespace {\n\n";
|
2001-11-08 01:35:11 +00:00
|
|
|
foreach $ns (split (/,/, $usings{$namespace})) {
|
|
|
|
print OUTFILE "\tusing $ns;\n";
|
|
|
|
}
|
|
|
|
print OUTFILE "\n\t/// <summary> $name Structure </summary>\n";
|
2001-11-04 15:42:46 +00:00
|
|
|
print OUTFILE "\t/// <remarks>\n\t///\tFIXME: Add docs.\n";
|
|
|
|
print OUTFILE "\t/// </remarks>\n\n";
|
|
|
|
|
2001-12-04 19:34:26 +00:00
|
|
|
print OUTFILE "\t[StructLayout(LayoutKind.Sequential)]\n";
|
|
|
|
print OUTFILE "\tpublic class $name {\n";
|
2001-11-04 15:42:46 +00:00
|
|
|
|
|
|
|
if ($def =~ /fields'\((.*)\)\)\)/) {
|
|
|
|
foreach $parm (split(/\)'\(/, $1)) {
|
|
|
|
$parm =~ s/\*//g;
|
2002-01-04 02:02:28 +00:00
|
|
|
$parm =~ /"(.*)" "(.*)"/;
|
|
|
|
$ptype = $maptypes{$1};
|
2001-11-08 01:35:11 +00:00
|
|
|
$pname = $2;
|
2002-01-04 02:02:28 +00:00
|
|
|
if ($pname =~ /(\w+)\s*\(.*\)/) {
|
|
|
|
$pname = $1;
|
|
|
|
$ptype = "IntPtr"; # FIXME: delegate?
|
|
|
|
}
|
2001-11-08 01:35:11 +00:00
|
|
|
$pname =~ s/object/objekt/;
|
2002-01-04 02:02:28 +00:00
|
|
|
$pname =~ s/string/str1ng/;
|
|
|
|
$pname =~ s/\bin\b/in_/;
|
|
|
|
if ($pname =~ /(\w+)\s*\[\d+\]/) {
|
|
|
|
$ptype .= "[]";
|
|
|
|
$pname = $1;
|
|
|
|
} elsif ($pname =~ /(\w+)\s*\:\s*(\d+)/) {
|
|
|
|
$pname = $1;
|
|
|
|
if ($2 == 1) {
|
|
|
|
$ptype = "bool";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print OUTFILE "\t\tpublic $ptype $pname;\n";
|
2001-11-04 15:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print OUTFILE "\t}\n\n}\n";
|
|
|
|
close (OUTFILE);
|
|
|
|
}
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
# Code generation for objects.
|
|
|
|
sub gen_object
|
|
|
|
{
|
|
|
|
my ($objdef, @defs) = @_;
|
|
|
|
my ($key, $typename, $parent, $dir, $namespace, $abstract, $def);
|
2002-01-04 02:02:28 +00:00
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
$objdef =~ /define-object (\w+)/;
|
|
|
|
$typename = $1;
|
|
|
|
$objdef =~ /parent "(\w+)"/;
|
|
|
|
$parent = $maptypes{$1};
|
|
|
|
$objdef =~ /in-module "(\w+)"/;
|
2002-01-04 02:02:28 +00:00
|
|
|
$namespace = $1;
|
|
|
|
$dir = "../" . lc ($namespace) . "/generated";
|
|
|
|
|
|
|
|
open (OUTFILE, ">$dir/$typename.cs") || die "can't open file $dir/$typename.cs";
|
2001-10-25 22:16:10 +00:00
|
|
|
|
|
|
|
%props = ();
|
2001-11-14 23:45:44 +00:00
|
|
|
%signals = ();
|
2001-10-25 22:16:10 +00:00
|
|
|
%methods = ();
|
2001-11-03 00:45:15 +00:00
|
|
|
@ctors = ();
|
2001-10-25 22:16:10 +00:00
|
|
|
foreach $def (@defs) {
|
|
|
|
if ($def =~ /define-property (\w+)/) {
|
|
|
|
$props{StudCaps($1)} = $def;
|
2001-11-14 23:45:44 +00:00
|
|
|
} elsif ($def =~ /define-signal (\w+)/) {
|
|
|
|
$signals{StudCaps($1)} = $def;
|
2001-11-03 00:45:15 +00:00
|
|
|
} elsif ($def =~ /define-method (\w+)/) {
|
2001-10-25 22:16:10 +00:00
|
|
|
$methods{StudCaps($1)} = $def;
|
2001-11-03 00:45:15 +00:00
|
|
|
} elsif ($def =~ /is-constructor-of/) {
|
|
|
|
@ctors = (@ctors, $def);
|
2001-10-25 22:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print OUTFILE "// Generated file: Do not modify\n\n";
|
|
|
|
print OUTFILE "namespace $namespace {\n\n";
|
2001-11-07 23:13:05 +00:00
|
|
|
foreach $ns (split (/,/, $usings{$namespace})) {
|
|
|
|
print OUTFILE "\tusing $ns;\n";
|
|
|
|
}
|
|
|
|
print OUTFILE "\n\t/// <summary> $typename Class </summary>\n";
|
2001-10-25 22:16:10 +00:00
|
|
|
print OUTFILE "\t/// <remarks>\n\t///\t FIXME: Generate docs\n";
|
|
|
|
print OUTFILE "\t/// </remarks>\n\n";
|
|
|
|
print OUTFILE "\tpublic ";
|
2001-11-10 02:35:15 +00:00
|
|
|
if (@ctors == 0) {
|
2001-10-25 22:16:10 +00:00
|
|
|
print OUTFILE "abstract ";
|
|
|
|
}
|
|
|
|
print OUTFILE "class $typename : $parent {\n\n";
|
2001-10-11 10:14:14 +00:00
|
|
|
|
2001-11-10 02:35:15 +00:00
|
|
|
# Only generate the wrapper ctor if other ctors exist
|
|
|
|
if (@ctors) {
|
|
|
|
print OUTFILE "\t\t/// <summary>\n";
|
|
|
|
print OUTFILE "\t\t///\t$typename Constructor\n";
|
|
|
|
print OUTFILE "\t\t/// </summary>\n";
|
|
|
|
print OUTFILE "\t\t/// <remarks>\n";
|
|
|
|
print OUTFILE "\t\t///\tWraps a raw GObject reference.\n";
|
|
|
|
print OUTFILE "\t\t/// </remarks>\n\n";
|
|
|
|
print OUTFILE "\t\tpublic $typename (IntPtr o)\n\t\t{\n";
|
|
|
|
print OUTFILE "\t\t\tRawObject = o;\n\t\t}\n\n";
|
|
|
|
}
|
2001-11-06 01:08:51 +00:00
|
|
|
|
2001-11-03 00:45:15 +00:00
|
|
|
foreach $ctor (@ctors) {
|
|
|
|
print OUTFILE gen_ctor ($ctor, "gtk-1.3.dll");
|
|
|
|
}
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
foreach $key (sort (keys (%props))) {
|
|
|
|
print OUTFILE gen_prop ($key, $props{$key}, "gtk-1.3.dll");
|
|
|
|
}
|
|
|
|
|
2001-11-14 23:45:44 +00:00
|
|
|
if (%signals) {
|
|
|
|
print OUTFILE "\t\tprivate Hashtable Signals = new Hashtable ();\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach $key (sort (keys (%signals))) {
|
|
|
|
print OUTFILE gen_signal ($key, $signals{$key}, "gtk-1.3.dll");
|
|
|
|
}
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
foreach $key (sort (keys (%methods))) {
|
2001-11-03 00:45:15 +00:00
|
|
|
next if (($key =~ /^(Get|Set)(\w+)/) && exists($props{$2}));
|
2001-11-14 23:45:44 +00:00
|
|
|
my $mod = "";
|
|
|
|
if (exists($signals{$key})) {
|
|
|
|
$mod = "Emit";
|
|
|
|
}
|
|
|
|
print OUTFILE gen_method ("$mod$key", $methods{$key}, "gtk-1.3.dll");
|
2001-10-25 22:16:10 +00:00
|
|
|
}
|
|
|
|
|
2001-11-07 23:13:05 +00:00
|
|
|
$custom = "../" . lc ($namespace) . "/$typename.custom";
|
|
|
|
print OUTFILE `cat $custom` if -e $custom;
|
2001-10-25 22:16:10 +00:00
|
|
|
print OUTFILE "\t}\n}\n";
|
2001-10-11 10:14:14 +00:00
|
|
|
close (OUTFILE);
|
2001-10-25 22:16:10 +00:00
|
|
|
}
|
|
|
|
|
2001-12-04 19:34:26 +00:00
|
|
|
# Code generation for signal definitions.
|
2001-11-14 23:45:44 +00:00
|
|
|
sub gen_signal
|
|
|
|
{
|
|
|
|
my ($name, $def, $dll) = @_;
|
2001-11-25 00:25:47 +00:00
|
|
|
my ($marsh, $cname, @plist, $ret, $sret, $mret, $code);
|
2001-11-14 23:45:44 +00:00
|
|
|
|
|
|
|
$def =~ /define-signal (\w+)/;
|
|
|
|
$cname = "\"$1\"";
|
|
|
|
|
|
|
|
$def =~ /return-type \"(\w+)\"/;
|
|
|
|
$ret = $1;
|
|
|
|
|
|
|
|
$def =~ /parameters\s*'\((.*)\)\)\)/;
|
|
|
|
@plist = split(/\)'\(/, $1);
|
|
|
|
|
2001-11-25 00:25:47 +00:00
|
|
|
$marsh = get_sighandler ($def);
|
|
|
|
|
2001-11-14 23:45:44 +00:00
|
|
|
$code = "\t\t/// <summary> $name Event </summary>\n";
|
|
|
|
$code .= "\t\t/// <remarks>\n\t\t///\tFIXME: Get some docs.\n";
|
|
|
|
$code .= "\t\t/// </remarks>\n\n";
|
|
|
|
$code .= "\t\tpublic event EventHandler $name {\n";
|
|
|
|
$code .= "\t\t\tadd {\n";
|
|
|
|
$code .= "\t\t\t\tif (Events [$cname] == null)\n";
|
|
|
|
$code .= "\t\t\t\t\tSignals [$cname] = new $marsh (this, RawObject, ";
|
|
|
|
$code .= "$cname, value);\n";
|
|
|
|
$code .= "\t\t\t\tEvents.AddHandler ($cname, value);\n\t\t\t}\n";
|
|
|
|
$code .= "\t\t\tremove {\n";
|
|
|
|
$code .= "\t\t\t\tEvents.RemoveHandler ($cname, value);\n";
|
|
|
|
$code .= "\t\t\t\tif (Events [$cname] == null)\n";
|
|
|
|
$code .= "\t\t\t\t\tSignals.Remove ($cname);\n\t\t\t}\n\t\t}\n\n";
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
2001-12-04 19:34:26 +00:00
|
|
|
# Code generation for property definitions.
|
2001-11-14 23:45:44 +00:00
|
|
|
sub gen_prop
|
2001-10-25 22:16:10 +00:00
|
|
|
{
|
|
|
|
my ($name, $def, $dll) = @_;
|
|
|
|
my ($cname, $mode, $sret, $mret, $docs, $code);
|
|
|
|
|
|
|
|
$def =~ /define-property (\w+)/;
|
|
|
|
$cname = $1;
|
|
|
|
|
|
|
|
$def =~ /prop-type "(\w+)/;
|
2001-11-10 02:17:57 +00:00
|
|
|
if (exists ($objects{$1}) || ($1 =~ /GObject/)) {
|
2001-10-25 22:16:10 +00:00
|
|
|
$sret = $maptypes{$1};
|
|
|
|
$mret = "GLib.Object";
|
2001-11-10 16:32:12 +00:00
|
|
|
} elsif ($maptypes{$1} eq "String") {
|
|
|
|
$sret = $mret = "String";
|
2001-10-25 22:16:10 +00:00
|
|
|
} elsif (exists ($maptypes{$1})) {
|
|
|
|
$sret = $maptypes{$1};
|
|
|
|
$mret = $marshaltypes{$1};
|
|
|
|
} else {
|
|
|
|
$sret = $mret = $1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$def =~ /doc-string "(.+)"\)/;
|
|
|
|
$docs = $1;
|
|
|
|
|
|
|
|
$mode = 0;
|
|
|
|
if ($def =~ /\(readable #t\)/) {
|
|
|
|
$mode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($def =~ /\(writeable #t\)/) && ($def !~ /\(construct-only #t\)/)) {
|
|
|
|
$mode += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = "\t\t/// <summary> $name Property </summary>\n";
|
|
|
|
$code .= "\t\t/// <remarks>\n\t\t///\t$docs\n";
|
|
|
|
$code .= "\t\t/// </remarks>\n\n";
|
|
|
|
$code .= "\t\tpublic $sret $name {\n";
|
|
|
|
if ($mode & 1) {
|
|
|
|
$code .= "\t\t\tget {\n\t\t\t\t$mret val;\n";
|
|
|
|
$code .= "\t\t\t\tGetProperty (\"$cname\", out val);\n";
|
|
|
|
$code .= "\t\t\t\treturn ";
|
|
|
|
if ($sret ne $mret) {
|
2001-11-10 16:32:12 +00:00
|
|
|
$code .= "($sret)";
|
2001-10-25 22:16:10 +00:00
|
|
|
}
|
|
|
|
$code .= "val;\n\t\t\t}\n";
|
|
|
|
}
|
|
|
|
if ($mode & 2) {
|
|
|
|
$code .= "\t\t\tset {\n";
|
|
|
|
$code .= "\t\t\t\tSetProperty (\"$cname\", ($mret) value);\n";
|
|
|
|
$code .= "\t\t\t}\n";
|
|
|
|
}
|
|
|
|
$code .= "\t\t}\n\n";
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
2001-11-03 00:45:15 +00:00
|
|
|
# Generate the code for a constructor definition.
|
|
|
|
sub gen_ctor
|
|
|
|
{
|
|
|
|
my ($def, $dll) = @_;
|
|
|
|
my ($cname, $sret, $ret, $mret, $sig, $call, $pinv, $code);
|
|
|
|
|
|
|
|
$def =~ /\(c-name "(\w+)"/;
|
|
|
|
$cname = $1;
|
|
|
|
|
|
|
|
$def =~ /is-constructor-of (\w+)\)/;
|
|
|
|
if (exists ($maptypes{$1})) {
|
|
|
|
$sret = $maptypes{$1};
|
|
|
|
$mret = $marshaltypes{$1};
|
|
|
|
$ret = $1;
|
|
|
|
} else {
|
|
|
|
die "Unexpected return type in constructor: $1\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
($call, $pinv, $sig) = gen_param_strings($def);
|
|
|
|
|
|
|
|
$code = "\t\t/// <summary> $sret Constructor </summary>\n";
|
|
|
|
$code .= "\t\t/// <remarks>\n\t\t///\t FIXME: Generate docs\n";
|
|
|
|
$code .= "\t\t/// </remarks>\n\n";
|
|
|
|
$code .= "\t\t[DllImport(\"$dll\", CharSet=CharSet.Ansi,\n";
|
|
|
|
$code .= "\t\t\t CallingConvention=CallingConvention.Cdecl)]\n";
|
|
|
|
$code .= "\t\tstatic extern $mret $cname ($pinv);\n\n";
|
|
|
|
$code .= "\t\tpublic $sret ($sig)\n";
|
|
|
|
$code .= "\t\t{\n\t\t\t";
|
|
|
|
$code .= "RawObject = $cname ($call);\n\t\t}\n\n";
|
|
|
|
}
|
|
|
|
|
2001-10-25 22:16:10 +00:00
|
|
|
# Generate the code for a method definition.
|
|
|
|
sub gen_method
|
|
|
|
{
|
|
|
|
my ($name, $def, $dll) = @_;
|
|
|
|
my ($cname, $sret, $ret, $mret, $sig, $call, $pinv, $code);
|
|
|
|
|
|
|
|
$def =~ /\(c-name "(\w+)"/;
|
|
|
|
$cname = $1;
|
|
|
|
|
2002-01-04 02:02:28 +00:00
|
|
|
$def =~ /return-type "(const-)*(\w+)/;
|
|
|
|
if (exists ($maptypes{$2})) {
|
|
|
|
$sret = $maptypes{$2};
|
|
|
|
$mret = $marshaltypes{$2};
|
|
|
|
$ret = $2;
|
2001-10-25 22:16:10 +00:00
|
|
|
} else {
|
2002-01-04 02:02:28 +00:00
|
|
|
$sret = $mret = $ret = $2;
|
2001-10-25 22:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
($call, $pinv, $sig) = gen_param_strings($def);
|
|
|
|
|
|
|
|
$code = "\t\t/// <summary> $name Method </summary>\n";
|
|
|
|
$code .= "\t\t/// <remarks>\n\t\t///\t FIXME: Generate docs\n";
|
|
|
|
$code .= "\t\t/// </remarks>\n\n";
|
|
|
|
$code .= "\t\t[DllImport(\"$dll\", CharSet=CharSet.Ansi,\n";
|
|
|
|
$code .= "\t\t\t CallingConvention=CallingConvention.Cdecl)]\n";
|
2001-11-03 00:45:15 +00:00
|
|
|
$code .= "\t\tstatic extern $mret $cname (IntPtr obj";
|
|
|
|
if ($pinv) {
|
|
|
|
$code .= ", $pinv";
|
|
|
|
}
|
|
|
|
$code .= ");\n\n";
|
2001-10-25 22:16:10 +00:00
|
|
|
$code .= "\t\tpublic $sret $name ($sig)\n";
|
|
|
|
$code .= "\t\t{\n\t\t\t";
|
|
|
|
if ($sret ne "void") { $code .= "return "; }
|
2001-11-03 00:45:15 +00:00
|
|
|
if ($call) {
|
|
|
|
$call = "$cname (RawObject, $call)";
|
|
|
|
} else {
|
|
|
|
$call = "$cname (RawObject)";
|
|
|
|
}
|
2001-10-25 22:16:10 +00:00
|
|
|
if ($sret eq $mret) {
|
|
|
|
$code .= "$call";
|
|
|
|
} elsif ($sret eq "String") {
|
|
|
|
$code .= "Marshal.PtrToStringAnsi($call)";
|
|
|
|
} elsif ($mret eq "int") {
|
|
|
|
$code .= "($sret) $call";
|
2001-11-10 02:17:57 +00:00
|
|
|
} elsif (exists ($objects{$ret}) || ($ret =~ /GObject/)) {
|
2001-10-25 22:16:10 +00:00
|
|
|
$code .= "($sret) GLib.Object.GetObject($call)";
|
|
|
|
} else {
|
|
|
|
die "Unexpected return type match $sret:$mret\n";
|
|
|
|
}
|
|
|
|
$code .= ";\n\t\t}\n\n";
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate the DllImport, signature, and call parameter strings.
|
|
|
|
sub gen_param_strings
|
|
|
|
{
|
|
|
|
my ($def) = @_;
|
2001-11-08 01:35:11 +00:00
|
|
|
my ($call, $parm, $pinv, $pname, $ptype, $sig);
|
2001-10-25 22:16:10 +00:00
|
|
|
|
|
|
|
$call = $pinv = $sig = "";
|
|
|
|
if ($def =~ /parameters'\((.*)\)\)\)/) {
|
|
|
|
foreach $parm (split(/\)'\(/, $1)) {
|
|
|
|
$parm =~ s/\*//g;
|
2002-01-04 02:02:28 +00:00
|
|
|
$parm =~ /"(\S*)"\s+"(\S*)"/;
|
2001-11-08 01:35:11 +00:00
|
|
|
$ptype = $1;
|
|
|
|
$pname = $2;
|
2002-01-04 02:02:28 +00:00
|
|
|
$ptype =~ s/const-//;
|
2001-11-08 01:35:11 +00:00
|
|
|
$pname =~ s/object/objekt/;
|
2001-11-25 00:25:47 +00:00
|
|
|
$pname =~ s/event/ev3nt/;
|
2001-11-03 00:45:15 +00:00
|
|
|
if ($sig) {
|
|
|
|
$sig .= ', ';
|
|
|
|
$call .= ', ';
|
|
|
|
$pinv .= ', ';
|
|
|
|
}
|
2002-01-04 02:02:28 +00:00
|
|
|
if ($marshaltypes{$ptype} eq "Unicode") {
|
|
|
|
$pinv .= "IntPtr $pname";
|
|
|
|
} else {
|
|
|
|
$pinv .= "$marshaltypes{$ptype} $pname";
|
|
|
|
}
|
2001-11-08 01:35:11 +00:00
|
|
|
$sig .= "$maptypes{$ptype} $pname";
|
|
|
|
if ($maptypes{$ptype} eq $marshaltypes{$ptype}) {
|
|
|
|
$call .= "$pname";
|
2001-11-10 02:17:57 +00:00
|
|
|
} elsif (exists ($objects{$ptype}) ||
|
|
|
|
($ptype =~ /GObject/)) {
|
2001-11-08 01:35:11 +00:00
|
|
|
$call .= "$pname.Handle";
|
2002-01-04 02:02:28 +00:00
|
|
|
} elsif ($maptypes{$ptype} eq "String") {
|
|
|
|
if ($marshaltypes{$ptype} eq "IntPtr") {
|
|
|
|
$call .= "Marshal.StringToHGlobalAnsi($pname)";
|
|
|
|
} else {
|
|
|
|
$call .= "Marshal.StringToHGlobalUni($pname)";
|
|
|
|
}
|
|
|
|
} elsif ($marshaltypes{$ptype} eq "int") {
|
2001-11-08 01:35:11 +00:00
|
|
|
$call .= "(int) $pname";
|
2001-10-25 22:16:10 +00:00
|
|
|
} else {
|
2001-11-08 01:35:11 +00:00
|
|
|
die "Unexpected type encountered $ptype\n";
|
2001-10-25 22:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ($call, $pinv, $sig);
|
2001-10-11 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
2001-12-04 19:34:26 +00:00
|
|
|
# Code generation for signal handlers.
|
2001-11-25 00:25:47 +00:00
|
|
|
sub get_sighandler
|
|
|
|
{
|
|
|
|
my ($def) = @_;
|
2001-11-25 17:06:27 +00:00
|
|
|
my ($key, $name, $dir, $ns, $nspace, $tok);
|
2001-11-25 00:25:47 +00:00
|
|
|
|
2002-01-04 02:02:28 +00:00
|
|
|
$def =~ /return-type \"(\w+)/;
|
2001-11-25 00:25:47 +00:00
|
|
|
my $ret = $1;
|
|
|
|
|
|
|
|
$def =~ /parameters'\((.*)\)\)\)/;
|
|
|
|
my @parms = split(/\)'\(/, $1);
|
|
|
|
|
2002-01-04 02:02:28 +00:00
|
|
|
$key = "";
|
2001-11-25 00:25:47 +00:00
|
|
|
for ($i = 1; $i < @parms; $i++) {
|
|
|
|
$parms[$i] =~ /^\"(\w+)/;
|
|
|
|
$key .= ":$1";
|
|
|
|
}
|
|
|
|
$key = $ret . $key;
|
|
|
|
|
|
|
|
if (exists($sighandlers{$key})) {
|
|
|
|
return $sighandlers{$key};
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($call, $pinv, $sig) = gen_param_strings($def);
|
|
|
|
|
|
|
|
if ($key =~ /Gtk/) {
|
|
|
|
$dir = "../gtk/generated";
|
|
|
|
$nspace = "Gtk";
|
|
|
|
} elsif ($key =~ /Gdk/) {
|
|
|
|
$dir = "../gdk/generated";
|
|
|
|
$nspace = "Gdk";
|
|
|
|
} else {
|
|
|
|
$dir = "../glib/generated";
|
|
|
|
$nspace = "GLib";
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = "";
|
|
|
|
foreach $tok (split(/:/, $key)) {
|
|
|
|
if (exists($objects{$tok})) {
|
|
|
|
$name .= "Object";
|
|
|
|
} elsif (exists($maptypes{$tok})) {
|
|
|
|
$name .= "$maptypes{$tok}";
|
|
|
|
} else {
|
|
|
|
die "Whassup with $tok?";
|
|
|
|
}
|
|
|
|
}
|
2001-11-25 17:06:27 +00:00
|
|
|
my $sname = $name . "Signal";
|
|
|
|
my $dname = $name . "Delegate";
|
|
|
|
my $cbname = $name . "Callback";
|
2001-11-25 00:25:47 +00:00
|
|
|
|
2002-01-04 02:02:28 +00:00
|
|
|
$sighandlers{$key} = $sname;
|
2001-11-25 00:25:47 +00:00
|
|
|
|
|
|
|
open (SIGFILE, ">$dir/$sname.cs") || die "can't open file";
|
|
|
|
|
|
|
|
print SIGFILE "// Generated file: Do not modify\n\n";
|
|
|
|
print SIGFILE "namespace GtkSharp {\n\n";
|
|
|
|
foreach $ns (split (/,/, $usings{$nspace})) {
|
|
|
|
print SIGFILE "\tusing $ns;\n";
|
|
|
|
}
|
2001-11-25 17:06:27 +00:00
|
|
|
print SIGFILE "\n\tpublic delegate $marshaltypes{$ret} ";
|
2001-11-25 00:25:47 +00:00
|
|
|
print SIGFILE "$dname($pinv, int key);\n\n";
|
2001-11-25 17:06:27 +00:00
|
|
|
print SIGFILE "\tpublic class $sname : SignalCallback {\n\n";
|
|
|
|
print SIGFILE "\t\tprivate static $dname _Delegate;\n\n";
|
|
|
|
print SIGFILE "\t\tprivate static $maptypes{$ret} ";
|
|
|
|
print SIGFILE "$cbname($pinv, int key)\n\t\t{\n";
|
|
|
|
print SIGFILE "\t\t\tif (!_Instances.Contains(key))\n";
|
|
|
|
print SIGFILE "\t\t\t\tthrow new Exception(\"Unexpected signal key\");";
|
|
|
|
print SIGFILE "\n\n\t\t\t$sname inst = ($sname) _Instances[key];\n";
|
|
|
|
print SIGFILE "\t\t\tSignalArgs args = new SignalArgs ();\n";
|
2001-12-04 19:34:26 +00:00
|
|
|
if ($def =~ /parameters'\((.*)\)\)\)/) {
|
|
|
|
my (@parms) = split(/\)'\(/, $1);
|
|
|
|
for ($idx=0; $idx < $#parms; $idx++) {
|
|
|
|
$parms[$idx+1] =~ s/\*//g;
|
2002-01-04 02:02:28 +00:00
|
|
|
$parms[$idx+1] =~ /"(\S*)"\s+"(\S*)"/;
|
2001-12-04 19:34:26 +00:00
|
|
|
$ptype = $1;
|
|
|
|
$pname = $2;
|
|
|
|
$pname =~ s/object/objekt/;
|
|
|
|
$pname =~ s/event/ev3nt/;
|
|
|
|
if (exists($objects{$ptype})) {
|
|
|
|
print SIGFILE "\t\t\targs.Args[$idx] = GLib.Object.GetObject($pname);\n";
|
|
|
|
} elsif (exists($maptypes{$ptype})) {
|
|
|
|
print SIGFILE "\t\t\targs.Args[$idx] = $pname;\n";
|
|
|
|
} else { warn "Whassup wit $ptype?"; }
|
|
|
|
}
|
|
|
|
}
|
2001-11-25 17:06:27 +00:00
|
|
|
print SIGFILE "\t\t\tinst._handler (inst._obj, args);\n";
|
|
|
|
if ($ret ne "none") {
|
|
|
|
print SIGFILE "\t\t\treturn ($maptypes{$ret}) args.RetVal;\n";
|
|
|
|
}
|
|
|
|
print SIGFILE "\t\t}\n\n";
|
|
|
|
print SIGFILE "\t\t[DllImport(\"gobject-1.3.dll\", ";
|
|
|
|
print SIGFILE "CharSet=CharSet.Ansi, ";
|
|
|
|
print SIGFILE "CallingConvention=CallingConvention.Cdecl)]\n";
|
|
|
|
print SIGFILE "\t\tstatic extern void g_signal_connect_data(";
|
|
|
|
print SIGFILE "IntPtr obj, IntPtr name, $dname cb, int key, IntPtr p,";
|
|
|
|
print SIGFILE " int flags);\n\n";
|
|
|
|
print SIGFILE "\t\tpublic $sname(GLib.Object obj, IntPtr raw, ";
|
|
|
|
print SIGFILE "String name, EventHandler eh) : base(obj, eh)";
|
|
|
|
print SIGFILE "\n\t\t{\n\t\t\tif (_Delegate == null) {\n";
|
|
|
|
print SIGFILE "\t\t\t\t_Delegate = new $dname($cbname);\n\t\t\t}\n";
|
|
|
|
print SIGFILE "\t\t\tg_signal_connect_data(raw, ";
|
|
|
|
print SIGFILE "Marshal.StringToHGlobalAnsi(name), _Delegate, _key, ";
|
|
|
|
print SIGFILE "new IntPtr(0), 0);\n\t\t}\n\n";
|
|
|
|
print SIGFILE "\t\t~$sname()\n{\t\t\t_Instances.Remove(_key);\n";
|
|
|
|
print SIGFILE "\t\t\tif(_Instances.Count == 0) {\n";
|
|
|
|
print SIGFILE "\t\t\t\t_Delegate = null;\n\t\t\t}\n\t\t}\n";
|
|
|
|
print SIGFILE "\t}\n}\n";
|
2001-11-25 00:25:47 +00:00
|
|
|
close (SIGFILE);
|
|
|
|
|
|
|
|
return $sighandlers{$key};
|
|
|
|
}
|