2002-11-10 10:03:51 +00:00
|
|
|
// GtkSharp.Generation.SignalHandler.cs - The SignalHandler marshaling Class.
|
|
|
|
//
|
2004-03-16 19:43:04 +00:00
|
|
|
// Author: Mike Kestner <mkestner@ximian.com>
|
2002-11-10 10:03:51 +00:00
|
|
|
//
|
2004-06-25 16:35:15 +00:00
|
|
|
// Copyright (c) 2002-2003 Mike Kestner
|
|
|
|
// Copyright (c) 2004 Novell, Inc.
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
public class SignalHandler {
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
XmlElement sig;
|
|
|
|
string ns;
|
2004-11-17 15:57:17 +00:00
|
|
|
ReturnValue retval;
|
2003-09-15 04:20:33 +00:00
|
|
|
Parameters parms = null;
|
|
|
|
|
|
|
|
public SignalHandler (XmlElement sig, string ns)
|
|
|
|
{
|
|
|
|
this.sig = sig;
|
|
|
|
this.ns = ns;
|
2004-11-17 15:57:17 +00:00
|
|
|
retval = new ReturnValue (sig["return-type"]);
|
2003-09-15 04:20:33 +00:00
|
|
|
XmlElement params_elem = sig["parameters"] as XmlElement;
|
|
|
|
if (params_elem != null)
|
|
|
|
parms = new Parameters (params_elem, ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Validate ()
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
2004-11-17 15:57:17 +00:00
|
|
|
if (!retval.Validate ()) {
|
|
|
|
Console.Write(" in signal handler " + Name);
|
2003-09-15 04:20:33 +00:00
|
|
|
return false;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
if (parms == null || !parms.Validate ()) {
|
2002-11-10 10:03:51 +00:00
|
|
|
Console.Write("Missing parameters ");
|
2003-09-15 04:20:33 +00:00
|
|
|
return false;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ISig {
|
|
|
|
get {
|
|
|
|
string result = "";
|
|
|
|
for (int i = 0; i < parms.Count; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
result += ", ";
|
|
|
|
|
|
|
|
result += (parms[i].MarshalType + " arg" + i);
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
2003-09-15 04:20:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string BaseName {
|
|
|
|
get {
|
2004-11-17 15:57:17 +00:00
|
|
|
string result = SymbolTable.Table.GetName (retval.CType);
|
2003-09-15 04:20:33 +00:00
|
|
|
for (int i = 0; i < parms.Count; i++) {
|
|
|
|
if (parms[i].Generatable is ObjectGen || parms[i].Generatable is InterfaceGen) {
|
|
|
|
result += "Object";
|
|
|
|
} else {
|
|
|
|
result += SymbolTable.Table.GetName(parms[i].CType);
|
|
|
|
}
|
|
|
|
}
|
2004-12-06 21:40:44 +00:00
|
|
|
result = result.Replace ("[]", "Array");
|
2003-09-15 04:20:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-06 16:15:05 +00:00
|
|
|
public string CallbackName {
|
|
|
|
get {
|
|
|
|
return BaseName + "Callback";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string DelegateName {
|
|
|
|
get {
|
|
|
|
return BaseName + "Delegate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
public string Name {
|
|
|
|
get {
|
|
|
|
return BaseName + "Signal";
|
|
|
|
}
|
|
|
|
}
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-06 16:15:05 +00:00
|
|
|
public void Generate (string implementor_ns, GenerationInfo gen_info)
|
2003-09-15 04:20:33 +00:00
|
|
|
{
|
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2003-10-06 16:15:05 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (implementor_ns + "Sharp." + Name);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
sw.WriteLine("namespace " + implementor_ns + "Sharp {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine("\tusing System;");
|
|
|
|
sw.WriteLine("\tusing System.Runtime.InteropServices;");
|
|
|
|
sw.WriteLine();
|
2004-11-17 15:57:17 +00:00
|
|
|
sw.Write("\tinternal delegate " + retval.MarshalType + " ");
|
2003-10-06 16:15:05 +00:00
|
|
|
sw.WriteLine(DelegateName + "(" + ISig + ", int key);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
2004-05-05 18:24:04 +00:00
|
|
|
sw.WriteLine("\tinternal class " + Name + " : GLib.SignalCallback {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
2003-10-06 16:15:05 +00:00
|
|
|
sw.WriteLine("\t\tprivate static " + DelegateName + " _Delegate;");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
2004-11-17 15:57:17 +00:00
|
|
|
sw.Write("\t\tprivate static " + retval.MarshalType + " ");
|
2003-10-06 16:15:05 +00:00
|
|
|
sw.WriteLine(CallbackName + "(" + ISig + ", int key)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
sw.WriteLine("\t\t\tif (!_Instances.Contains(key))");
|
|
|
|
sw.WriteLine("\t\t\t\tthrow new Exception(\"Unexpected signal key \" + key);");
|
|
|
|
sw.WriteLine();
|
2003-10-06 16:15:05 +00:00
|
|
|
sw.WriteLine("\t\t\t" + Name + " inst = (" + Name + ") _Instances[key];");
|
2004-11-17 15:57:17 +00:00
|
|
|
if ((retval.CSType == "void") && (parms.Count == 1)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\tEventHandler h = (EventHandler) inst._handler;");
|
|
|
|
sw.WriteLine("\t\t\th (inst._obj, new EventArgs ());");
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
} else {
|
2004-05-05 18:24:04 +00:00
|
|
|
sw.WriteLine("\t\t\tGLib.SignalArgs args = (GLib.SignalArgs) Activator.CreateInstance (inst._argstype);");
|
2002-11-10 10:03:51 +00:00
|
|
|
if (parms.Count > 1) {
|
|
|
|
sw.WriteLine("\t\t\targs.Args = new object[" + (parms.Count-1) + "];");
|
|
|
|
}
|
|
|
|
for (int idx=1; idx < parms.Count; idx++) {
|
2003-09-15 04:20:33 +00:00
|
|
|
string ctype = parms[idx].CType;
|
2003-05-19 02:45:17 +00:00
|
|
|
ClassBase wrapper = table.GetClassGen (ctype);
|
|
|
|
if ((wrapper != null && !(wrapper is StructBase)) || table.IsManuallyWrapped (ctype)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\tif (arg{0} == IntPtr.Zero)", idx);
|
|
|
|
sw.WriteLine("\t\t\t\targs.Args[{0}] = null;", idx - 1);
|
|
|
|
sw.WriteLine("\t\t\telse {");
|
2004-05-24 02:25:16 +00:00
|
|
|
if ((wrapper != null) && wrapper is ObjectGen)
|
|
|
|
sw.WriteLine("\t\t\t\targs.Args[" + (idx-1) + "] = GLib.Object.GetObject(arg" + idx + ");");
|
|
|
|
else
|
|
|
|
sw.WriteLine("\t\t\t\targs.Args[" + (idx-1) + "] = " + table.FromNative (ctype, "arg" + idx) + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\t}");
|
|
|
|
} else {
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine("\t\t\targs.Args[" + (idx-1) + "] = " + table.FromNative (ctype, "arg" + idx) + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sw.WriteLine("\t\t\tobject[] argv = new object[2];");
|
|
|
|
sw.WriteLine("\t\t\targv[0] = inst._obj;");
|
|
|
|
sw.WriteLine("\t\t\targv[1] = args;");
|
|
|
|
sw.WriteLine("\t\t\tinst._handler.DynamicInvoke(argv);");
|
2004-11-17 15:57:17 +00:00
|
|
|
if (retval.CSType != "void") {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t\tif (args.RetVal == null)");
|
2004-11-17 15:57:17 +00:00
|
|
|
if (retval.CSType == "bool")
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t\t\treturn false;");
|
|
|
|
else
|
|
|
|
sw.WriteLine ("\t\t\t\tthrow new Exception(\"args.RetVal unset in callback\");");
|
|
|
|
|
2004-11-17 15:57:17 +00:00
|
|
|
sw.WriteLine("\t\t\treturn (" + retval.MarshalType + ") " + table.ToNativeReturn (retval.CType, "((" + retval.CSType + ")args.RetVal)") + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
}
|
2004-05-05 18:24:04 +00:00
|
|
|
sw.Write("\t\tpublic " + Name + "(GLib.Object obj, ");
|
2004-03-16 19:43:04 +00:00
|
|
|
sw.WriteLine("string name, Delegate eh, Type argstype, int connect_flags) : base(obj, eh, argstype)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
sw.WriteLine("\t\t\tif (_Delegate == null) {");
|
2003-10-06 16:15:05 +00:00
|
|
|
sw.WriteLine("\t\t\t\t_Delegate = new " + DelegateName + "(" + CallbackName + ");");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\t}");
|
2004-05-05 18:24:04 +00:00
|
|
|
sw.WriteLine("\t\t\tConnect (name, _Delegate, connect_flags);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
2003-02-26 02:16:38 +00:00
|
|
|
sw.WriteLine("\t\tprotected override void Dispose (bool disposing)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
sw.WriteLine("\t\t\t_Instances.Remove(_key);");
|
2003-02-28 10:55:59 +00:00
|
|
|
sw.WriteLine("\t\t\tif(_Instances.Count == 0)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\t\t_Delegate = null;");
|
2003-02-28 10:55:59 +00:00
|
|
|
sw.WriteLine();
|
2004-05-05 18:24:04 +00:00
|
|
|
sw.WriteLine("\t\t\tDisconnect ();");
|
2003-02-28 10:55:59 +00:00
|
|
|
sw.WriteLine("\t\t\tbase.Dispose (disposing);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine("\t}");
|
|
|
|
sw.WriteLine("}");
|
|
|
|
sw.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|