summaryrefslogtreecommitdiff
path: root/glue/compiz-mono.c
blob: 70e6cb0004547d4006ab7a67914470b92209bc84 (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
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details

// Plugin loader plugin for compiz-sharp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>

#include <compiz.h>

#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/object.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/appdomain.h>

typedef struct _CompMonoData {
	MonoDomain *domain;
	MonoAssembly *assembly;
  MonoClass *loader_class;
  MonoObject *loader_instance;
} CompMonoData;

static CompMonoData *data;

static LoadPluginProc baseloaderLoadPlugin;
static UnloadPluginProc baseloaderUnloadPlugin;

static Bool
monoloaderLoadPlugin (CompPlugin *p, char *path, char *name)
{
	Bool status;

	fprintf (stderr, "monoloaderLoadPlugin %s %s\n", path, name);

	MonoMethodDesc *method_desc = mono_method_desc_new (":Load(intptr,string,string)", FALSE);
	MonoMethod *method = mono_method_desc_search_in_class (method_desc, data->loader_class);

	gpointer params[3];
	params[0] = &p;
	params[1] = (gpointer*) mono_string_new (data->domain, path);
	params[2] = (gpointer*) mono_string_new (data->domain, name);

	MonoObject *mret = mono_runtime_invoke (method, data->loader_instance, params, NULL);
	status = *(Bool*)mono_object_unbox(mret);

	fprintf (stderr, "ret %d\n", status);

	if (status)
		return status;

	status = (*baseloaderLoadPlugin) (p, path, name);

	return status;
}

static void
monoloaderUnloadPlugin (CompPlugin *p)
{
	(*baseloaderUnloadPlugin) (p);
}

static MonoObject *
plugin_loader_new (CompMonoData *data)
{
	MonoMethodDesc *method_desc = mono_method_desc_new (":.ctor()", FALSE);
	MonoMethod *method = mono_method_desc_search_in_class (method_desc, data->loader_class);

	MonoObject *object = mono_object_new (data->domain, data->loader_class);
	mono_runtime_invoke (method, object, NULL, NULL);

	return object;
}

static Bool
monoInit (CompPlugin *p)
{
	data = malloc (sizeof (CompMonoData));

	//initialize Mono
	mono_set_defaults (0, mono_parse_default_optimizations (NULL));

	//set the domain name to the assembly name
	//this seems to let the runtime pick up 2.0 deps
	data->domain = mono_jit_init ("compiz-sharp.dll");

	//enable debugging
	mono_debug_init (MONO_DEBUG_FORMAT_MONO);
	mono_debug_init_1 (data->domain);

	data->assembly = mono_domain_assembly_open (data->domain, "compiz-sharp.dll");
	mono_dllmap_insert (mono_assembly_get_image (data->assembly), "compiz", NULL, "__Internal", NULL);
	mono_dllmap_insert (mono_assembly_get_image (data->assembly), "compmono", NULL, "__Internal", NULL);
	data->loader_class = mono_class_from_name (mono_assembly_get_image (data->assembly), "Compiz", "PluginLoader");
	mono_debug_init_2 (data->assembly);

	mono_class_init (data->loader_class);

	//create a new loader instance
	data->loader_instance = plugin_loader_new (data);

	//save existing loader load/unload callbacks
	baseloaderLoadPlugin = loaderLoadPlugin;
	baseloaderUnloadPlugin = loaderUnloadPlugin;

	//install our new loader load/unload callbacks
	loaderLoadPlugin = monoloaderLoadPlugin;
	loaderUnloadPlugin = monoloaderUnloadPlugin;

	return TRUE;
}

static void
monoFini (CompPlugin *p)
{
	//restore saved loader load/unload callbacks
	loaderLoadPlugin = baseloaderLoadPlugin;
	loaderUnloadPlugin = baseloaderUnloadPlugin;

	mono_jit_cleanup (data->domain);
	free (data);
}

static Bool
monoInitDisplay (CompPlugin *p, CompDisplay *d)
{
	fprintf (stderr, "InitDisplay\n");

	return TRUE;
}

static void
monoFiniDisplay (CompPlugin *p, CompDisplay *d)
{
	fprintf (stderr, "FiniDisplay\n");
}

CompPluginVTable monoVTable = {
	"compmono",
	"Mono Loader",
	"Experimental plugin loader",
	monoInit,
	monoFini,
	monoInitDisplay,
	monoFiniDisplay,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	0,
};

CompPluginVTable *
getCompPluginInfo (void)
{
	fprintf (stderr, "getCompPluginInfo\n");

	return &monoVTable;
}