summaryrefslogtreecommitdiff
path: root/src/Main.cs
blob: f17c2347bbbceaa0e53218ee729c62a96ec84437 (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
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;

namespace Compiz
{
	//compiz.exe uses a custom libcompiz.so to control the main loop
	//this is useful for debugging and experimenting with plugin loading
	public class Session
	{
		[DllImport("compiz")]
			protected extern static void initSession (string smPrevClientId);

		[DllImport("compiz")]
			protected unsafe extern static void* addDisplay (string name, string[] plugins, int nPlugin);

		[DllImport("compiz")]
			protected extern static void eventLoop ();

		[DllImport("compiz")]
			protected extern static bool pushPlugin (IntPtr plugin);
			//protected unsafe extern static bool pushPlugin (CompPlugin* plugin);

		[DllImport("compiz")]
			protected unsafe extern static void* getPlugins ();

		[DllImport("compiz"), SuppressUnmanagedCodeSecurity]
			protected unsafe extern static void runIteration (void* display, IntPtr tmpRegion);

		[DllImport("compiz")]
			protected unsafe extern static IntPtr compAddWatchFd (int fd, short events, IntPtr callBack, IntPtr closure);

		[DllImport("X11")]
			protected extern static int XConnectionNumber (IntPtr display);

		[DllImport("X11")]
			protected unsafe extern static IntPtr XCreateRegion ();

		const string defaultPlugins = "decoration wobbly fade minimize cube rotate zoom scale move resize place switcher";

		public unsafe static int Main (string[] args)
		{
			string[] pluginList = (args.Length != 0) ? args : defaultPlugins.Split (' ');

			CompDisplay* display = (CompDisplay*)addDisplay (null, pluginList, pluginList.Length);

			if (display == null)
				return 1;

			IntPtr tmpRegion = XCreateRegion ();

			compAddWatchFd (XConnectionNumber (display->Display), 0x0001, IntPtr.Zero, IntPtr.Zero);

			//eventLoop ();
			
			bool done = false;

			while (true) {
					runIteration (display, tmpRegion);

					/*
					if (!done) {
						done = true;
						LoadPlugin ("sharp");
					}
					*/

					//Console.WriteLine (tmpRegion.size);
					//Console.WriteLine (tmpRegion.numRects);

					//CompPlugin p = getPlugins ();
					//Console.WriteLine (p.dlhand);

					CompPlugin* plug = (CompPlugin*)getPlugins ();

					while (plug != null) {
						Console.WriteLine (plug->vTable->Name);
						//plug->vTable->Init (plug);
						//plug->vTable->Init (plugPtr);
						//plug->vTable->initt += delegate {Console.WriteLine ("foo"); return false;};
						//plug->vTable->initWindow = Marshal.GetFunctionPointerForDelegate (fiz);
						plug = plug->next;
					}

					Console.WriteLine ();
					//CompPluginVTable* vt = (CompPluginVTable*)(plug->vTable);

			}

			return 0;
		}

		public unsafe static void LoadPlugin (string name)
		{
			/*
			CompPluginVTable vt = new CompPluginVTable ();
			vt.Name = "sharp";

			CompPlugin p = new CompPlugin ();
			p.vTable = vt;

			*/
			IntPtr plugPtr = Marshal.AllocCoTaskMem (sizeof (CompPlugin));
			//IntPtr plugPtr = Marshal.AllocCoTaskMem (Marshal.SizeOf ());
			//IntPtr plugPtr = Marshal.AllocHGlobal (sizeof (CompPlugin));
			CompPlugin* p = (CompPlugin*)plugPtr;
			//p->next = (CompPlugin*)getPlugins ();

			IntPtr vtPtr = Marshal.AllocCoTaskMem (sizeof (CompPluginVTable));
			//IntPtr vtPtr = Marshal.AllocHGlobal (sizeof (CompPluginVTable));
			CompPluginVTable* vt = (CompPluginVTable*)vtPtr;
			p->vTable = vt;

			vt->Name = name;

			//d1 = init;
			vt->init = Marshal.GetFunctionPointerForDelegate (d1);
			/*

			InitPluginProc<Display> d2 = initD;
			vt->initDisplay = Marshal.GetFunctionPointerForDelegate (d2);
			*/

			//pushPlugin (plugPtr);
		}

		static InitPluginProc d1 = init;
		public static bool init (IntPtr plugin)
		{
			return true;
		}

		public static bool initD (IntPtr plugin, Display display)
		{
			return true;
		}
	}

	[StructLayout (LayoutKind.Sequential)]
	public unsafe struct Box
	{
		public short x1, x2, y1, y2;
	}

	[StructLayout (LayoutKind.Sequential)]
	public unsafe class Region
	{
		public int size;
		public int numRects;
		public Box* rects;
		public Box extents;
	}
}