Ryujinx-GtkSharp/generator/SignalHandler.cs

198 lines
6.8 KiB
C#
Raw Normal View History

// GtkSharp.Generation.SignalHandler.cs - The SignalHandler marshaling Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2002 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Xml;
public class SignalHandler {
2002-08-19 Rachel Hestilow <hestilow@ximian.com> * art/Makefile.in (clean): Change to avoid bugging out on generated/CVS. * glib/ObjectManager.cs: Added. Used to be auto-generated, but now it can infer names, and relies on per-namespace ObjectManager classes to inform it of oddly-named classes. * generator/IGeneratable.cs, GenBase.cs: New "DoGenerate" property. * generator/*Gen.cs: Honor DoGenerate. * generator/CodeGenerator.cs: Support including dependency files which will not be generated. * generator/ObjectGen.cs: Generate mapping file per-namespace, as one that calls back to the one in glib. Only generate if the name does not follow the normal conventions, otherwise, GtkSharp.ObjectManager can infer the name. * generator/Parser.cs: Accept 'generate' flag to pass on to the IGeneratables. Parse a new toplevel element, "symbol", which adds a type to the SymbolTable (instead of hard-coding it). * generator/SignalHandler.cs: Do not optimize signal handler creation, instead creating them in their own namespaces. Do not generate if the calling Signal told us not to. * generator/Signal.cs: Do not generate handlers if container's DoGenerate is false. Adjust to the marshaller name being in a sub-namespace. * generator/SymbolTable.cs (AddSimpleType, AddManualType): Used to add simple and manually wrapped types at runtime instead of compile-time. (FromNative): Remove hard-coded cases for manually wrapped types, use a generic case instead. * api: Added. Move api files and generation targets here. * source: Added. Move source parsing here. * generator/makefile: Move actual generation to api/. * glib/Makefile.in: Remove generated/* target. * glue/Makefile.am: Fix to include canvas-marshal. Move canvas stuff to GNOME target. * gnome/CanvasProxy.cs: Update to work with SignalHandlers being namespace-specific. * parser/Metadata.pm: Moved to GAPI/Metadata.pm, renamed, etc. * parser/gapi2xml.pl: Use GAPI::Metadata. * parser/makefile: Install scripts, remove source parse build target. Rename formatXML to gapi_format_xml. svn path=/trunk/gtk-sharp/; revision=6818
2002-08-20 21:56:18 +02:00
public static String GetName(XmlElement sig, string ns, bool generate)
{
XmlElement ret_elem = sig["return-type"];
if (ret_elem == null) {
Console.Write("Missing return-type ");
return "";
}
string retval = ret_elem.GetAttribute("type");
if (retval == "") {
Console.Write("Invalid return-type ");
return "";
}
string s_ret = SymbolTable.GetCSType(retval);
2002-07-25 Rachel Hestilow <hestilow@ximian.com> [about 60% of the marshalling patch that I lost. The rest to come tomorrow.] * generator/BoxedGen.cs, StructGen.cs: Move most of this to StructBase, delete large chunks duplicated from ClassBase. * generator/IGeneratable.cs: Add MarshalReturnType, FromNativeReturn. * generator/ClassBase.cs: Move ctor stuff here. Add a CallByName overload with no parameters for the "self" reference. * generator/EnumGen.cs, CallbackGen.cs: Implement new MarshalReturnType, FromNativeReturn. * generator/Method.cs: Use container_type.MarshalType, CallByName, and SymbolTable.FromNativeReturn when generating call and import sigs. * generator/OpaqueGen.cs: Added. * generator/Property.cs: Handle boxed and opaques differently. * generator/SymbolTable.cs: Update for the opaque stuff and the new Return methods. Also change GetClassGen to simply call the as operator. * glib/Boxed.cs: Update for struct usage -- this is now a wrapper for the purposes of using with Value. * glib/Opaque.cs: Added. New base class for opaque structs. * glue/textiter.c, gtk/TextIter.custom: Remove. * gnome/Program.cs: Update for new struct marshalling. * parser/Metadata.pm: Use our own getChildrenByTagName. * parser/README: Update for new requirements (was out of sync with build.pl) * parser/gapi2xml.pl: Hide struct like const in field elements. * parser/gapi_pp.pl: Handle embedded union fields (poorly). * sample/test/TestColorSelection.cs: Comment out null color tests for now. svn path=/trunk/gtk-sharp/; revision=6186
2002-07-26 08:08:52 +02:00
string p_ret = SymbolTable.GetMarshalReturnType(retval);
if ((s_ret == "") || (p_ret == "")) {
Console.Write("Funky type: " + retval);
return "";
}
string key = retval;
string pinv = "";
string name = SymbolTable.GetName(retval);
int pcnt = 0;
ArrayList parms = new ArrayList();
XmlElement params_elem = sig["parameters"];
if (params_elem == null) {
Console.Write("Missing parameters ");
return "";
}
foreach (XmlNode parm in params_elem.ChildNodes) {
if (!(parm is XmlElement) || parm.Name != "parameter") continue;
XmlElement elem = (XmlElement) parm;
string type = elem.GetAttribute("type");
string ptype = SymbolTable.GetMarshalType(type);
if (ptype == "") {
Console.Write("Funky type: " + type);
return "";
}
if (pcnt > 0) {
pinv += ", ";
}
pinv += (ptype + " arg" + pcnt);
parms.Add(type);
2002-08-19 Rachel Hestilow <hestilow@ximian.com> * art/Makefile.in (clean): Change to avoid bugging out on generated/CVS. * glib/ObjectManager.cs: Added. Used to be auto-generated, but now it can infer names, and relies on per-namespace ObjectManager classes to inform it of oddly-named classes. * generator/IGeneratable.cs, GenBase.cs: New "DoGenerate" property. * generator/*Gen.cs: Honor DoGenerate. * generator/CodeGenerator.cs: Support including dependency files which will not be generated. * generator/ObjectGen.cs: Generate mapping file per-namespace, as one that calls back to the one in glib. Only generate if the name does not follow the normal conventions, otherwise, GtkSharp.ObjectManager can infer the name. * generator/Parser.cs: Accept 'generate' flag to pass on to the IGeneratables. Parse a new toplevel element, "symbol", which adds a type to the SymbolTable (instead of hard-coding it). * generator/SignalHandler.cs: Do not optimize signal handler creation, instead creating them in their own namespaces. Do not generate if the calling Signal told us not to. * generator/Signal.cs: Do not generate handlers if container's DoGenerate is false. Adjust to the marshaller name being in a sub-namespace. * generator/SymbolTable.cs (AddSimpleType, AddManualType): Used to add simple and manually wrapped types at runtime instead of compile-time. (FromNative): Remove hard-coded cases for manually wrapped types, use a generic case instead. * api: Added. Move api files and generation targets here. * source: Added. Move source parsing here. * generator/makefile: Move actual generation to api/. * glib/Makefile.in: Remove generated/* target. * glue/Makefile.am: Fix to include canvas-marshal. Move canvas stuff to GNOME target. * gnome/CanvasProxy.cs: Update to work with SignalHandlers being namespace-specific. * parser/Metadata.pm: Moved to GAPI/Metadata.pm, renamed, etc. * parser/gapi2xml.pl: Use GAPI::Metadata. * parser/makefile: Install scripts, remove source parse build target. Rename formatXML to gapi_format_xml. svn path=/trunk/gtk-sharp/; revision=6818
2002-08-20 21:56:18 +02:00
if (SymbolTable.IsObject(type) || SymbolTable.IsInterface(type)) {
name += "Object";
key += "Object";
} else {
name += SymbolTable.GetName(type);
key += type;
}
pcnt++;
}
String sname = name + "Signal";
String dname = name + "Delegate";
String cbname = name + "Callback";
2002-08-19 Rachel Hestilow <hestilow@ximian.com> * art/Makefile.in (clean): Change to avoid bugging out on generated/CVS. * glib/ObjectManager.cs: Added. Used to be auto-generated, but now it can infer names, and relies on per-namespace ObjectManager classes to inform it of oddly-named classes. * generator/IGeneratable.cs, GenBase.cs: New "DoGenerate" property. * generator/*Gen.cs: Honor DoGenerate. * generator/CodeGenerator.cs: Support including dependency files which will not be generated. * generator/ObjectGen.cs: Generate mapping file per-namespace, as one that calls back to the one in glib. Only generate if the name does not follow the normal conventions, otherwise, GtkSharp.ObjectManager can infer the name. * generator/Parser.cs: Accept 'generate' flag to pass on to the IGeneratables. Parse a new toplevel element, "symbol", which adds a type to the SymbolTable (instead of hard-coding it). * generator/SignalHandler.cs: Do not optimize signal handler creation, instead creating them in their own namespaces. Do not generate if the calling Signal told us not to. * generator/Signal.cs: Do not generate handlers if container's DoGenerate is false. Adjust to the marshaller name being in a sub-namespace. * generator/SymbolTable.cs (AddSimpleType, AddManualType): Used to add simple and manually wrapped types at runtime instead of compile-time. (FromNative): Remove hard-coded cases for manually wrapped types, use a generic case instead. * api: Added. Move api files and generation targets here. * source: Added. Move source parsing here. * generator/makefile: Move actual generation to api/. * glib/Makefile.in: Remove generated/* target. * glue/Makefile.am: Fix to include canvas-marshal. Move canvas stuff to GNOME target. * gnome/CanvasProxy.cs: Update to work with SignalHandlers being namespace-specific. * parser/Metadata.pm: Moved to GAPI/Metadata.pm, renamed, etc. * parser/gapi2xml.pl: Use GAPI::Metadata. * parser/makefile: Install scripts, remove source parse build target. Rename formatXML to gapi_format_xml. svn path=/trunk/gtk-sharp/; revision=6818
2002-08-20 21:56:18 +02:00
if (!generate)
return ns + "." + sname;
char sep = Path.DirectorySeparatorChar;
String dir = ".." + sep + ns.ToLower() + sep + "generated";
if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}
String filename = dir + sep + ns + "Sharp." + sname + ".cs";
FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter (stream);
sw.WriteLine ("// Generated File. Do not modify.");
sw.WriteLine ("// <c> 2001-2002 Mike Kestner");
sw.WriteLine ();
sw.WriteLine("namespace " + ns + "Sharp {");
2002-08-19 Rachel Hestilow <hestilow@ximian.com> * art/Makefile.in (clean): Change to avoid bugging out on generated/CVS. * glib/ObjectManager.cs: Added. Used to be auto-generated, but now it can infer names, and relies on per-namespace ObjectManager classes to inform it of oddly-named classes. * generator/IGeneratable.cs, GenBase.cs: New "DoGenerate" property. * generator/*Gen.cs: Honor DoGenerate. * generator/CodeGenerator.cs: Support including dependency files which will not be generated. * generator/ObjectGen.cs: Generate mapping file per-namespace, as one that calls back to the one in glib. Only generate if the name does not follow the normal conventions, otherwise, GtkSharp.ObjectManager can infer the name. * generator/Parser.cs: Accept 'generate' flag to pass on to the IGeneratables. Parse a new toplevel element, "symbol", which adds a type to the SymbolTable (instead of hard-coding it). * generator/SignalHandler.cs: Do not optimize signal handler creation, instead creating them in their own namespaces. Do not generate if the calling Signal told us not to. * generator/Signal.cs: Do not generate handlers if container's DoGenerate is false. Adjust to the marshaller name being in a sub-namespace. * generator/SymbolTable.cs (AddSimpleType, AddManualType): Used to add simple and manually wrapped types at runtime instead of compile-time. (FromNative): Remove hard-coded cases for manually wrapped types, use a generic case instead. * api: Added. Move api files and generation targets here. * source: Added. Move source parsing here. * generator/makefile: Move actual generation to api/. * glib/Makefile.in: Remove generated/* target. * glue/Makefile.am: Fix to include canvas-marshal. Move canvas stuff to GNOME target. * gnome/CanvasProxy.cs: Update to work with SignalHandlers being namespace-specific. * parser/Metadata.pm: Moved to GAPI/Metadata.pm, renamed, etc. * parser/gapi2xml.pl: Use GAPI::Metadata. * parser/makefile: Install scripts, remove source parse build target. Rename formatXML to gapi_format_xml. svn path=/trunk/gtk-sharp/; revision=6818
2002-08-20 21:56:18 +02:00
sw.WriteLine();
sw.WriteLine("\tusing System;");
sw.WriteLine("\tusing System.Runtime.InteropServices;");
sw.WriteLine("\tusing GtkSharp;");
sw.WriteLine();
sw.Write("\tpublic delegate " + p_ret + " ");
sw.WriteLine(dname + "(" + pinv + ", int key);");
sw.WriteLine();
sw.WriteLine("\tpublic class " + sname + " : SignalCallback {");
sw.WriteLine();
sw.WriteLine("\t\tprivate static " + dname + " _Delegate;");
sw.WriteLine();
sw.Write("\t\tprivate static " + s_ret + " ");
sw.WriteLine(cbname + "(" + pinv + ", int key)");
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();
sw.WriteLine("\t\t\t" + sname + " inst = (" + sname + ") _Instances[key];");
if ((s_ret == "void") && (parms.Count == 1)) {
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 {
sw.WriteLine("\t\t\tSignalArgs args = (SignalArgs) Activator.CreateInstance (inst._argstype);");
if (parms.Count > 1) {
sw.WriteLine("\t\t\targs.Args = new object[" + (parms.Count-1) + "];");
}
for (int idx=1; idx < parms.Count; idx++) {
string ctype = (string) parms[idx];
ClassBase wrapper = SymbolTable.GetClassGen (ctype);
if ((wrapper != null && !(wrapper is StructBase)) || SymbolTable.IsManuallyWrapped (ctype)) {
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 {");
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) + "] = " + SymbolTable.FromNative (ctype, "arg" + idx) + ";");
if ((wrapper != null && (wrapper is OpaqueGen)) || SymbolTable.IsManuallyWrapped (ctype)) {
sw.WriteLine("\t\t\t\tif (args.Args[" + (idx-1) + "] == null)");
sw.WriteLine("\t\t\t\t\targs.Args[{0}] = new {1}(arg{2});", idx-1, SymbolTable.GetCSType (ctype), idx);
}
sw.WriteLine("\t\t\t}");
} else {
if (wrapper != null && (wrapper is StructBase))
2002-07-30 Rachel Hestilow <hestilow@ximian.com> * generator/ClassBase.cs: Change hasDefaultConstructor to protected, adjust now that it is an attr and not a subnode. Also add virtual property AssignToName (for ctors). * generator/Ctor.cs: Add property ForceStatic. (Generate): Optimize return code a bit for the static case. * generator/Method.cs: Assign to a "raw_ret" pointer before calling FromNativeReturn. * generator/Parameters.cs: Change "out ref" to "out", not "ref". * generator/Property.cs: Fix to work correctly with all object and struct types (mostly just some if-cases added). * generator/SignalHandler.cs: Remove args_type and argfields (unused). (Generate): Initialize struct if necessary. * generator/StructBase.cs: Massive reworking to support methods, ctors, etc. * generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types. * glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both. * glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event fields. Fix docs. * glib/Value.cs: Work correctly with boxed properties. * gnome/Modules.cs: Use new struct ctors. * gnome/Program.custom: Remove Get, this is being generated now. * parser/Gdk.metadata: Fix the drawable classes to inherit correctly. * parser/Metadata.pm: Change per-class attributes to actually be attributes. * parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor. * parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap typedefs. * sample/test/TestColorSelection.cs: Display color string in hex format, update to use IsNull instead of == null, and size dialog to look pretty. * sample/Size.cs: Added. svn path=/trunk/gtk-sharp/; revision=6264
2002-07-31 01:02:12 +02:00
sw.WriteLine("\t\t\targ{0}._Initialize ();", idx);
sw.WriteLine("\t\t\targs.Args[" + (idx-1) + "] = " + SymbolTable.FromNative (ctype, "arg" + idx) + ";");
}
}
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);");
if (retval != "void") {
sw.WriteLine ("\t\t\tif (args.RetVal == null)");
if (s_ret == "bool")
sw.WriteLine ("\t\t\t\treturn false;");
else
sw.WriteLine ("\t\t\t\tthrow new Exception(\"args.RetVal unset in callback\");");
sw.WriteLine("\t\t\treturn (" + s_ret + ") args.RetVal;");
}
sw.WriteLine("\t\t}");
sw.WriteLine();
}
sw.Write("\t\t[DllImport(\"gobject-2.0\")]");
sw.Write("\t\tstatic extern void g_signal_connect_data(");
sw.Write("IntPtr obj, String name, " + dname + " cb, int key, IntPtr p,");
sw.WriteLine(" int flags);");
sw.WriteLine();
sw.Write("\t\tpublic " + sname + "(GLib.Object obj, IntPtr raw, ");
sw.WriteLine("String name, MulticastDelegate eh, Type argstype) : base(obj, eh, argstype)");
sw.WriteLine("\t\t{");
sw.WriteLine("\t\t\tif (_Delegate == null) {");
sw.WriteLine("\t\t\t\t_Delegate = new " + dname + "(" + cbname + ");");
sw.WriteLine("\t\t\t}");
sw.Write("\t\t\tg_signal_connect_data(raw, name, ");
sw.WriteLine("_Delegate, _key, new IntPtr(0), 0);");
sw.WriteLine("\t\t}");
sw.WriteLine();
sw.WriteLine("\t\t~" + sname + "()");
sw.WriteLine("\t\t{");
sw.WriteLine("\t\t\t_Instances.Remove(_key);");
sw.WriteLine("\t\t\tif(_Instances.Count == 0) {");
sw.WriteLine("\t\t\t\t_Delegate = null;");
sw.WriteLine("\t\t\t}");
sw.WriteLine("\t\t}");
sw.WriteLine("\t}");
sw.WriteLine("}");
sw.Close();
return ns + "Sharp." + sname;
}
}
}