summaryrefslogtreecommitdiff
path: root/src/PluginLoader.cs
blob: c4682d7d21c9c51684b06583f389c85f912341c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Compiz
{
	public class PluginLoader
	{
		static List<PluginLoader> loaders = new List<PluginLoader> ();

		public PluginLoader ()
		{
			Console.WriteLine ("new managed plugin loader");

			//keep references around, cheap hack for now
			loaders.Add (this);
		}

		/*
		public static bool Load (string name)
		{
			return false;
		}
		*/

		/*
		public unsafe bool Unload (IntPtr plugin)
		{
		}
		*/

		protected Plugin LoadAssembly (string name)
		{
			//temporary hack
			string path = Environment.CurrentDirectory + Path.DirectorySeparatorChar + name + ".dll";

			if (!File.Exists (path))
					return null;

			Assembly asm = Assembly.LoadFile (path);

			//mangle into an acceptable class name
			//improve this later
			string className = "";
			className += Char.ToUpper (name[0]);
			className += name.Substring (1, name.Length - 1);
			className += "Plugin";

			Type t = asm.GetType (className);

			if (t == null)
				return null;

			Plugin p = Activator.CreateInstance (t) as Plugin;

			return p;
		}

		List<Delegate> delegates = new List<Delegate> ();
		List<Plugin> plugins = new List<Plugin> ();

		public unsafe bool Load (IntPtr plugin, string path, string name)
		{
			Console.WriteLine ("managed load " + path + " " + name);

			Plugin plug = LoadAssembly (name);
			if (plug == null)
				return false;

			CompPlugin* p = (CompPlugin*)plugin;

			my_initPlugin = initPlugin;
			my_initDisplay = initDisplay;
			my_initScreen = plug.InitScreen;
			my_initWindow = plug.InitWindow;
			my_finiWindow = plug.FiniWindow;

			//keep references around, cheap hack for now
			delegates.Add (my_initPlugin);
			delegates.Add (my_initDisplay);
			delegates.Add (my_initScreen);
			delegates.Add (my_initWindow);
			delegates.Add (my_finiWindow);

			p->vTable = (CompPluginVTable*)Marshal.AllocCoTaskMem (sizeof (CompPluginVTable));

			p->vTable->Name = name;
			p->vTable->ShortDesc = name;
			p->vTable->LongDesc = name;

			p->vTable->init = Marshal.GetFunctionPointerForDelegate (my_initPlugin);
			p->vTable->fini = IntPtr.Zero;

			p->vTable->initDisplay = Marshal.GetFunctionPointerForDelegate (my_initDisplay);
			p->vTable->finiDisplay = IntPtr.Zero;

			p->vTable->initScreen = Marshal.GetFunctionPointerForDelegate (my_initScreen);
			p->vTable->finiScreen = IntPtr.Zero;

			p->vTable->initWindow = Marshal.GetFunctionPointerForDelegate (my_initWindow);
			p->vTable->finiWindow = Marshal.GetFunctionPointerForDelegate (my_finiWindow);

			p->vTable->getDisplayOptions = IntPtr.Zero;
			p->vTable->setDisplayOption = IntPtr.Zero;

			p->vTable->getScreenOptions = IntPtr.Zero;
			p->vTable->setScreenOption = IntPtr.Zero;

			p->vTable->deps = null;
			p->vTable->nDeps = 0;

			plugins.Add (plug);

			return true;
		}

		InitPluginProc my_initPlugin;
		InitPluginProc<IntPtr> my_initDisplay;
		InitPluginProc<IntPtr> my_initScreen;
		InitPluginProc<IntPtr> my_initWindow;
		FiniPluginProc<IntPtr> my_finiWindow;
		//InitPluginProc<CompWindow> my_initWindow;

		bool initPlugin (IntPtr plugin)
		{
			Console.WriteLine ("init");
			return true;
		}

		bool initDisplay (IntPtr plugin, IntPtr display)
		{
			Console.WriteLine ("initDisplay");
			return true;
		}
	}

	[StructLayout (LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
	public unsafe struct CompPlugin
	{
		public CompPlugin* next;
		public uint uval; //ulong union
		public void* devType;
		public CompPluginVTable* vTable;
	}

	//public unsafe delegate bool InitPluginProc (CompPlugin* plugin);
	//[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
	public delegate bool InitPluginProc (IntPtr plugin);
	//[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
	public delegate void FiniPluginProc (IntPtr plugin);

	public delegate bool InitPluginProc<T> (IntPtr plugin, T obj);
	public delegate void FiniPluginProc<T> (IntPtr plugin, T obj);
	
	public delegate IntPtr GetOptionsProc<T> (T obj, out int count);
	public delegate bool SetOptionProc<T> (T obj, string name, IntPtr value);

	public enum PluginRule
	{
		Before,
		After,
	}

	[StructLayout (LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
	public struct CompPluginDep
	{
		public PluginRule rule;
		//public string plugin;
		public IntPtr plugin;
	}

	[StructLayout (LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
	public unsafe struct CompPluginVTable
	{
		/*
		public string name;
		public string shortDesc;
		public string longDesc;
		*/

		public string Name
		{
			get {
				//return System.Text.Encoding.ASCII.GetString (name);
				return Marshal.PtrToStringAnsi (name);
			} set {
				name = Marshal.StringToCoTaskMemAnsi (value);
			}
		}

		public string ShortDesc
		{
			get {
				return Marshal.PtrToStringAnsi (shortDesc);
			} set {
				shortDesc = Marshal.StringToCoTaskMemAnsi (value);
			}
		}

		public string LongDesc
		{
			get {
				return Marshal.PtrToStringAnsi (longDesc);
			} set {
				longDesc = Marshal.StringToCoTaskMemAnsi (value);
			}
		}

		public IntPtr name;
		public IntPtr shortDesc;
		public IntPtr longDesc;

		//public bool Init (CompPlugin* plugin)
		/*
		public bool Init (IntPtr plugin)
		{
			if (init == null)
				return false;

			return init (plugin);
		}
		*/

		/*
		public event InitPluginProc initt
		{
			add {
				IntPtr f = Marshal.GetFunctionPointerForDelegate (value);
				initWindow = f;
			} remove {
				initWindow = IntPtr.Zero;
			}
		}
		*/

		//public event InitPluginProc init;
		public IntPtr init;
		public IntPtr fini;

		public IntPtr initDisplay;
		public IntPtr finiDisplay;

		public IntPtr initScreen;
		public IntPtr finiScreen;

		public IntPtr initWindow;
		public IntPtr finiWindow;

		public IntPtr getDisplayOptions;
		public IntPtr setDisplayOption;
		public IntPtr getScreenOptions;
		public IntPtr setScreenOption;

		/*
		public InitPluginProc init;
		public FiniPluginProc fini;

		public InitPluginProc<Screen> initScreen;
		public FiniPluginProc<Screen> finiScreen;
		
		public InitPluginProc<Window> initWindow;
		public FiniPluginProc<Window> finiWindow;

		public GetOptionsProc<Display> getDisplayOptions;
		public SetOptionProc<Display> setDisplayOption;
		
		public GetOptionsProc<Screen> getScreenOptions;
		public SetOptionProc<Screen> setScreenOption;
		*/

		public CompPluginDep *deps;
		public int nDeps;
	}

	[StructLayout (LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
	public unsafe struct CompPluginVTableOld
	{
		/*
		public string name;
		public string shortDesc;
		public string longDesc;
		*/

		public IntPtr name;
		public IntPtr shortDesc;
		public IntPtr longDesc;

		public InitPluginProc init;
		public FiniPluginProc fini;

		public InitPluginProc<Screen> initScreen;
		public FiniPluginProc<Screen> finiScreen;
		
		public InitPluginProc<Window> initWindow;
		public FiniPluginProc<Window> finiWindow;

		public GetOptionsProc<Display> getDisplayOptions;
		public SetOptionProc<Display> setDisplayOption;
		
		public GetOptionsProc<Screen> getScreenOptions;
		public SetOptionProc<Screen> setScreenOption;

		public CompPluginDep *deps;
		public int nDeps;
	}

	public delegate CompPluginVTable PluginGetInfoProc ();
}