2002-11-10 10:03:51 +00:00
|
|
|
// GtkSharp.Generation.CallbackGen.cs - The Callback Generatable.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
2004-06-25 16:35:15 +00:00
|
|
|
// Copyright (c) 2002-2003 Mike Kestner
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the GNU General Public
|
|
|
|
// License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public class CallbackGen : GenBase {
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
private Parameters parms;
|
2003-12-03 23:08:14 +00:00
|
|
|
private Signature sig = null;
|
|
|
|
private ImportSignature isig = null;
|
2004-12-27 21:32:08 +00:00
|
|
|
private ReturnValue retval;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
public CallbackGen (XmlElement ns, XmlElement elem) : base (ns, elem)
|
|
|
|
{
|
2004-12-27 21:32:08 +00:00
|
|
|
retval = new ReturnValue (elem ["return-type"]);
|
2005-01-26 19:17:07 +00:00
|
|
|
parms = new Parameters (elem ["parameters"]);
|
|
|
|
parms.HideData = true;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override string MarshalType {
|
|
|
|
get {
|
2002-11-10 10:03:51 +00:00
|
|
|
return NS + "Sharp." + Name + "Native";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override string CallByName (string var_name)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
return var_name + ".NativeDelegate";
|
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override string FromNative(string var)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
public string GenWrapper (string ns, GenerationInfo gen_info)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
string wrapper = Name + "Native";
|
2003-10-05 00:20:17 +00:00
|
|
|
string qualname = ns + "Sharp." + wrapper;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
isig = new ImportSignature (parms, NS);
|
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (qualname);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
sw.WriteLine ("namespace " + ns + "Sharp {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ();
|
2003-01-05 23:51:37 +00:00
|
|
|
sw.WriteLine ("#region Autogenerated code");
|
2005-03-25 18:13:00 +00:00
|
|
|
sw.WriteLine ("\t[GLib.CDeclCallback]");
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\tinternal delegate " + retval.MarshalType + " " + wrapper + "(" + isig + ");");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\tinternal class " + Name + "Wrapper {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\tpublic " + retval.MarshalType + " NativeCallback (" + isig + ")");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
2003-04-06 09:21:15 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
bool need_sep = false;
|
|
|
|
string call_str = "";
|
2003-10-13 21:53:40 +00:00
|
|
|
string cleanup_str = "";
|
2005-01-26 19:17:07 +00:00
|
|
|
for (int i = 0, idx = 0; i < parms.Count; i++)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
2004-12-27 21:32:08 +00:00
|
|
|
Parameter p = parms [i];
|
2003-02-24 03:13:08 +00:00
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
if (i > 0 && p.IsLength && parms[i-1].IsString)
|
2003-02-24 03:13:08 +00:00
|
|
|
continue;
|
|
|
|
|
2005-01-26 19:17:07 +00:00
|
|
|
if ((i == parms.Count - 1) && p.IsUserData)
|
2002-11-10 10:03:51 +00:00
|
|
|
continue;
|
2003-02-24 03:13:08 +00:00
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
if (p.CType == "GError**") {
|
|
|
|
sw.WriteLine ("\t\t\t" + p.Name + " = IntPtr.Zero;");
|
2004-05-11 20:56:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
IGeneratable gen = p.Generatable;
|
|
|
|
|
|
|
|
sw.Write("\t\t\t" + p.CSType + " _arg" + idx);
|
|
|
|
if (p.PassAs == "out") {
|
2003-10-13 21:53:40 +00:00
|
|
|
sw.WriteLine(";");
|
2004-12-27 21:32:08 +00:00
|
|
|
cleanup_str += "\t\t\t" + p.Name + " = " + gen.CallByName ("_arg" + idx) + ";\n";
|
2003-10-13 21:53:40 +00:00
|
|
|
} else
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine(" = " + gen.FromNative (p.Name) + ";");
|
2003-10-13 21:53:40 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
if (need_sep)
|
|
|
|
call_str += ", ";
|
|
|
|
else
|
|
|
|
need_sep = true;
|
2004-12-27 21:32:08 +00:00
|
|
|
call_str += String.Format ("{0} _arg{1}", p.PassAs, idx);
|
2002-11-10 10:03:51 +00:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.Write ("\t\t\t");
|
2005-03-12 18:54:53 +00:00
|
|
|
string invoke = "managed (" + call_str + ")";
|
2004-12-27 21:32:08 +00:00
|
|
|
if (retval.MarshalType != "void") {
|
2003-11-03 01:36:55 +00:00
|
|
|
if (cleanup_str == "")
|
|
|
|
sw.Write ("return ");
|
|
|
|
else {
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.Write (retval.MarshalType + " ret = ");
|
2003-11-03 01:36:55 +00:00
|
|
|
cleanup_str += "\t\t\treturn ret;\n";
|
|
|
|
}
|
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
ClassBase ret_wrapper = table.GetClassGen (retval.CType);
|
2003-11-03 01:36:55 +00:00
|
|
|
if (ret_wrapper != null && (ret_wrapper is ObjectGen || ret_wrapper is OpaqueGen))
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine ("(({0}) {1}).Handle;", retval.CSType, invoke);
|
|
|
|
else if (table.IsStruct (retval.CType) || table.IsBoxed (retval.CType)) {
|
2003-11-03 01:36:55 +00:00
|
|
|
// Shoot. I have no idea what to do here.
|
2004-12-27 21:32:08 +00:00
|
|
|
Console.WriteLine ("Struct return type {0} in callback {1}", retval.CType, CName);
|
2003-11-03 01:36:55 +00:00
|
|
|
sw.WriteLine ("IntPtr.Zero;");
|
2004-12-27 21:32:08 +00:00
|
|
|
} else if (table.IsEnum (retval.CType))
|
2003-11-03 01:36:55 +00:00
|
|
|
sw.WriteLine ("(int) {0};", invoke);
|
|
|
|
else
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine ("({0}) {1};", retval.MarshalType, table.ToNativeReturn (retval.CType, invoke));
|
2003-10-13 21:53:40 +00:00
|
|
|
} else
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine (invoke + ";");
|
2003-10-13 21:53:40 +00:00
|
|
|
|
|
|
|
if (cleanup_str != "")
|
|
|
|
sw.Write (cleanup_str);
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\tinternal " + wrapper + " NativeDelegate;");
|
|
|
|
sw.WriteLine ("\t\t" + NS + "." + Name + " managed;");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\tpublic " + Name + "Wrapper (" + NS + "." + Name + " managed)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\t\tthis.managed = managed;");
|
2005-03-24 22:47:50 +00:00
|
|
|
sw.WriteLine ("\t\t\tif (managed != null)");
|
|
|
|
sw.WriteLine ("\t\t\t\tNativeDelegate = new " + wrapper + " (NativeCallback);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ("\t}");
|
2003-01-05 23:51:37 +00:00
|
|
|
sw.WriteLine ("#endregion");
|
2003-10-05 00:37:24 +00:00
|
|
|
sw.WriteLine ("}");
|
|
|
|
sw.Close ();
|
2003-06-14 17:30:32 +00:00
|
|
|
return ns + "Sharp." + Name + "Wrapper";
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override void Generate (GenerationInfo gen_info)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
2005-04-04 16:27:08 +00:00
|
|
|
gen_info.CurrentType = Name;
|
2004-12-27 21:32:08 +00:00
|
|
|
if (!retval.Validate ()) {
|
|
|
|
Console.WriteLine("rettype: " + retval.CType + " in callback " + CName);
|
2002-11-10 10:03:51 +00:00
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-26 19:17:07 +00:00
|
|
|
if (!parms.Validate ()) {
|
2002-11-10 10:03:51 +00:00
|
|
|
Console.WriteLine(" in callback " + CName + " **** Stubbing it out ****");
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
}
|
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
sig = new Signature (parms);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (Name);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.WriteLine ("namespace " + NS + " {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ();
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine ("\tpublic delegate " + retval.CSType + " " + Name + "(" + sig.ToString() + ");");
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("}");
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.Close ();
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
GenWrapper (NS, gen_info);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
Statistics.CBCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|