2002-11-10 10:03:51 +00:00
|
|
|
// GtkSharp.Generation.CallbackGen.cs - The Callback Generatable.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
|
|
|
// (c) 2002 Mike Kestner
|
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
public class CallbackGen : GenBase, IGeneratable {
|
|
|
|
|
|
|
|
private Parameters parms;
|
|
|
|
|
|
|
|
public CallbackGen (XmlElement ns, XmlElement elem) : base (ns, elem)
|
|
|
|
{
|
|
|
|
if (elem ["parameters"] != null)
|
|
|
|
parms = new Parameters (elem ["parameters"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String MarshalType {
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return NS + "Sharp." + Name + "Native";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String MarshalReturnType {
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return MarshalType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String CallByName (String var_name)
|
|
|
|
{
|
|
|
|
return var_name + ".NativeDelegate";
|
|
|
|
}
|
|
|
|
|
|
|
|
public String FromNative(String var)
|
|
|
|
{
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String FromNativeReturn(String var)
|
|
|
|
{
|
|
|
|
return FromNative (var);
|
|
|
|
}
|
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
public void GenWrapper (string ns)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
char sep = Path.DirectorySeparatorChar;
|
2003-05-14 00:27:00 +00:00
|
|
|
string dir = ".." + sep + ns.ToLower() + sep + "generated";
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
if (!Directory.Exists (dir))
|
|
|
|
Directory.CreateDirectory (dir);
|
|
|
|
|
|
|
|
string wrapper = Name + "Native";
|
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
string filename = dir + sep + ns + "Sharp." + wrapper + ".cs";
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
|
|
|
|
StreamWriter sw = new StreamWriter (stream);
|
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
sw.WriteLine ("// This file was generated by the Gtk# code generator.");
|
|
|
|
sw.WriteLine ("// Any changes made will be lost on regeneration.");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
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");
|
2003-05-14 00:27:00 +00:00
|
|
|
string import_sig = "";
|
|
|
|
string sig = "";
|
2002-11-10 10:03:51 +00:00
|
|
|
if (parms != null)
|
|
|
|
{
|
2003-05-14 00:27:00 +00:00
|
|
|
parms.HideData = true;
|
2002-11-10 10:03:51 +00:00
|
|
|
parms.CreateSignature (false);
|
|
|
|
import_sig = parms.ImportSig;
|
2003-05-14 00:27:00 +00:00
|
|
|
sig = parms.Signature;
|
|
|
|
}
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-05-19 02:45:17 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
XmlElement ret_elem = Elem["return-type"];
|
|
|
|
string rettype = ret_elem.GetAttribute("type");
|
2003-05-19 02:45:17 +00:00
|
|
|
string m_ret = table.GetMarshalReturnType (rettype);
|
|
|
|
string s_ret = table.GetCSType (rettype);
|
|
|
|
ClassBase ret_wrapper = table.GetClassGen (rettype);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
sw.WriteLine ("\tinternal delegate " + m_ret + " " + wrapper + "(" + import_sig + ");");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\tpublic class " + Name + "Wrapper : GLib.DelegateWrapper {");
|
2003-04-06 09:21:15 +00:00
|
|
|
if (m_ret != "void") {
|
2003-05-19 02:45:17 +00:00
|
|
|
if (table.IsEnum (rettype)) {
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.WriteLine ("\t\tstatic int _dummy;");
|
|
|
|
} else if (ret_wrapper != null && (ret_wrapper is ObjectGen || ret_wrapper is OpaqueGen)) {
|
|
|
|
// Do nothing
|
2003-05-19 02:45:17 +00:00
|
|
|
} else if (!table.IsStruct (rettype) && !table.IsBoxed (rettype)) {
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.WriteLine ("\t\tstatic {0} _dummy;", s_ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\tpublic " + m_ret + " NativeCallback (" + import_sig + ")");
|
|
|
|
sw.WriteLine ("\t\t{");
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.Write ("\t\t\tif (RemoveIfNotAlive ()) return ");
|
2003-05-19 02:45:17 +00:00
|
|
|
if (table.IsStruct (rettype) || table.IsBoxed (rettype))
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.WriteLine ("IntPtr.Zero;");
|
|
|
|
else if (ret_wrapper != null && (ret_wrapper is ObjectGen || ret_wrapper is OpaqueGen))
|
|
|
|
sw.WriteLine ("IntPtr.Zero;");
|
|
|
|
else
|
|
|
|
sw.WriteLine (m_ret != "void" ? "_dummy;" : ";");
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
int count = (parms != null) ? parms.Count : 0;
|
|
|
|
if (count > 0)
|
|
|
|
sw.WriteLine ("\t\t\tobject[] _args = new object[{0}];", count);
|
|
|
|
int idx = 0;
|
|
|
|
bool need_sep = false;
|
|
|
|
string call_str = "";
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
string parm_name = parms[i].Name;
|
|
|
|
string ctype = parms[i].CType;
|
2003-02-24 03:13:08 +00:00
|
|
|
|
|
|
|
if (i > 0 && parms[i].IsLength && parms[i-1].IsString)
|
|
|
|
continue;
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
if ((i == count - 1) && ctype == "gpointer" && (parm_name.EndsWith ("data") || parm_name.EndsWith ("data_or_owner")))
|
|
|
|
continue;
|
2003-02-24 03:13:08 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
string cstype = parms[i].CSType;
|
|
|
|
// FIXME: Too much code copy/pasted here. Refactor?
|
2003-05-19 02:45:17 +00:00
|
|
|
ClassBase parm_wrapper = table.GetClassGen (ctype);
|
|
|
|
sw.WriteLine("\t\t\t_args[" + idx + "] = " + table.FromNative (ctype, parm_name) + ";");
|
|
|
|
if ((parm_wrapper != null && ((parm_wrapper is OpaqueGen))) || table.IsManuallyWrapped (ctype)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\tif (_args[" + idx + "] == null)");
|
|
|
|
sw.WriteLine("\t\t\t\t_args[{0}] = new {1}({2});", idx, cstype, parm_name);
|
|
|
|
}
|
|
|
|
if (need_sep)
|
|
|
|
call_str += ", ";
|
|
|
|
else
|
|
|
|
need_sep = true;
|
|
|
|
call_str += String.Format ("({0}) _args[{1}]", cstype, idx);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.Write ("\t\t\t");
|
|
|
|
string invoke = "_managed (" + call_str + ")";
|
|
|
|
if (m_ret != "void") {
|
2003-04-06 09:21:15 +00:00
|
|
|
if (ret_wrapper != null && (ret_wrapper is ObjectGen || ret_wrapper is OpaqueGen))
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("return (({0}) {1}).Handle;", s_ret, invoke);
|
2003-05-19 02:45:17 +00:00
|
|
|
else if (table.IsStruct (rettype) || table.IsBoxed (rettype)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
// Shoot. I have no idea what to do here.
|
|
|
|
sw.WriteLine ("return IntPtr.Zero;");
|
|
|
|
}
|
2003-05-19 02:45:17 +00:00
|
|
|
else if (table.IsEnum (rettype))
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("return (int) {0};", invoke);
|
|
|
|
else
|
|
|
|
sw.WriteLine ("return ({0}) {1};", s_ret, invoke);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sw.WriteLine (invoke + ";");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\tpublic {0} NativeDelegate;", wrapper);
|
|
|
|
sw.WriteLine ("\t\tprotected {0} _managed;", NS + "." + Name);
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.WriteLine ("\t\tpublic {0} ({1} managed, object o) : base (o)", Name + "Wrapper", NS + "." + Name);
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\tNativeDelegate = new {0} (NativeCallback);", wrapper);
|
|
|
|
sw.WriteLine ("\t\t\t_managed = managed;");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
|
|
|
|
sw.WriteLine ("\t}");
|
2003-01-05 23:51:37 +00:00
|
|
|
|
|
|
|
sw.WriteLine ("#endregion");
|
2002-11-10 10:03:51 +00:00
|
|
|
CloseWriter (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Generate ()
|
|
|
|
{
|
|
|
|
if (!DoGenerate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
XmlElement ret_elem = Elem["return-type"];
|
|
|
|
if (ret_elem == null) {
|
|
|
|
Console.WriteLine("No return type in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-19 02:45:17 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
string rettype = ret_elem.GetAttribute("type");
|
2003-05-19 02:45:17 +00:00
|
|
|
string s_ret = table.GetCSType (rettype);
|
2002-11-10 10:03:51 +00:00
|
|
|
if (s_ret == "") {
|
|
|
|
Console.WriteLine("rettype: " + rettype + " in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((parms != null) && !parms.Validate ()) {
|
|
|
|
Console.WriteLine(" in callback " + CName + " **** Stubbing it out ****");
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
parms = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamWriter sw = CreateWriter ();
|
|
|
|
|
|
|
|
string sig = "";
|
|
|
|
if (parms != null) {
|
|
|
|
parms.HideData = true;
|
|
|
|
parms.CreateSignature (false);
|
|
|
|
sig = parms.Signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + sig + ");");
|
|
|
|
|
|
|
|
CloseWriter (sw);
|
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
GenWrapper (NS);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
Statistics.CBCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|