2009-08-26 Chris Toshok <toshok@ximian.com>
[moon.git] / plugin / plugin-proxy.cpp
blobf28055426f3174a806153f7b60226eff9ff9807a
1 /*
2 * plugin-entry.cpp: MoonLight browser plugin.
4 * Contact:
5 * Moonlight List (moonlight-list@lists.ximian.com)
7 * Copyright 2007 Novell, Inc. (http://www.novell.com)
9 * See the LICENSE file included with the distribution for details.
13 #include <stdio.h>
14 #include <string.h>
15 #include <dlfcn.h>
17 #include <config.h>
18 #include "moonlight.h"
20 #if PLUGIN_SL_2_0
21 #include <mono/metadata/assembly.h>
22 #include <mono/metadata/mono-config.h>
23 #endif
25 typedef NPError (*np_initialize_func) (void *a, void *b);
26 typedef NPError (*np_shutdown_func) ();
27 typedef NPError (*np_getvalue_func) (void *, NPPVariable var, void *avalue);
28 typedef char * (*np_getmime_func) ();
30 static np_initialize_func initialize;
31 static np_getvalue_func getvalue;
32 static np_shutdown_func shutdown;
33 static np_getmime_func getmime;
36 static NPError
37 load (void)
39 char *plugin_path;
41 Dl_info dlinfo;
42 if (dladdr((void *) &load, &dlinfo) == 0) {
43 fprintf (stderr, "Unable to find the location of libmoonloaderxpi %s\n", dlerror ());
44 return FALSE;
47 if (strstr(dlinfo.dli_fname, "libmoonloaderxpi.so")) {
49 fprintf (stdout, "Attempting to load libmoonloaderxpi \n");
50 char *plugin_dir;
51 plugin_dir = g_build_filename (g_path_get_dirname(dlinfo.dli_fname), "moonlight", NULL);
53 plugin_path = g_build_filename (plugin_dir, "libmoonpluginxpi.so", NULL);
55 #if INCLUDE_FFMPEG
56 // load libavutil
57 char *avutil_path = g_build_filename (plugin_dir, "libavutil.so", NULL);
58 void *real_avutil = dlopen (avutil_path, RTLD_LAZY | RTLD_GLOBAL);
59 if (real_avutil == NULL){
60 fprintf (stderr, "Unable to load the libavutil %s\n", dlerror ());
61 return FALSE;
63 g_free (avutil_path);
65 #if INCLUDE_SWSCALE
66 // load libswscale
67 char *swscale_path = g_build_filename (plugin_dir, "libswscale.so", NULL);
68 void *real_swscale = dlopen (swscale_path, RTLD_LAZY | RTLD_GLOBAL);
69 if (real_swscale == NULL){
70 fprintf (stderr, "Unable to load the libswscale %s\n", dlerror ());
71 return FALSE;
73 g_free (swscale_path);
74 #endif
76 // load libavcodec
77 char *avcodec_path = g_build_filename (plugin_dir, "libavcodec.so", NULL);
78 void *real_avcodec = dlopen (avcodec_path, RTLD_LAZY | RTLD_GLOBAL);
79 if (real_avcodec == NULL){
80 fprintf (stderr, "Unable to load the libavcodec %s\n", dlerror ());
81 return FALSE;
83 g_free (avcodec_path);
84 #endif
86 #if PLUGIN_SL_2_0
87 // load libmono
88 char *mono_path = g_build_filename (plugin_dir, "libmono.so", NULL);
89 void *real_mono = dlopen (mono_path, RTLD_LAZY | RTLD_GLOBAL);
90 if (real_mono == NULL){
91 fprintf (stderr, "Unable to load the libmono %s\n", dlerror ());
92 return FALSE;
94 mono_set_dirs (plugin_dir, plugin_dir);
95 g_free (mono_path);
96 #endif
98 // load libmoon
99 char *moon_path = g_build_filename (plugin_dir, "libmoonxpi.so", NULL);
100 void *real_moon = dlopen (moon_path, RTLD_LAZY | RTLD_GLOBAL);
101 if (real_moon == NULL){
102 fprintf (stderr, "Unable to load the libmoonxpi %s\n", dlerror ());
103 return FALSE;
106 char* moon_config = g_strdup_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?><configuration><dllmap dll=\"moon\" target=\"%s\" /></configuration>",moon_path);
107 mono_config_parse_memory(moon_config);
108 g_free (moon_config);
109 g_free (moon_path);
111 g_free (plugin_dir);
113 } else {
115 fprintf (stdout, "Attempting to load the system libmoon \n");
116 const gchar *moon_plugin_dir = g_getenv("MOON_PLUGIN_DIR");
117 if (moon_plugin_dir == NULL) {
118 plugin_path = g_build_filename (PLUGIN_DIR, "plugin", "libmoonplugin.so", NULL);
119 } else {
120 plugin_path = g_build_filename (moon_plugin_dir, "libmoonplugin.so", NULL);
124 void *real_plugin = dlopen (plugin_path, RTLD_LAZY | RTLD_GLOBAL);
126 if (real_plugin == NULL){
127 fprintf (stderr, "Unable to load the real plugin %s\n", dlerror ());
128 fprintf (stderr, "plugin_path is %s\n", plugin_path);
129 return FALSE;
132 // Must dllmap moonplugin, otherwise it doesn't know where to get it
133 char* plugin_config = g_strdup_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?><configuration><dllmap dll=\"moonplugin\" target=\"%s\" /></configuration>",plugin_path);
134 mono_config_parse_memory(plugin_config);
135 g_free (plugin_config);
137 g_free (plugin_path);
139 initialize = (np_initialize_func) dlsym (real_plugin, LOADER_RENAMED_NAME(NP_Initialize));
140 if (initialize == NULL){
141 fprintf (stderr, "NP_Initialize not found %s\n", dlerror ());
142 return FALSE;
146 getvalue = (np_getvalue_func) dlsym (real_plugin, LOADER_RENAMED_NAME(NP_GetValue));
147 if (getvalue == NULL){
148 fprintf (stderr, "NP_GetValue not found %s\n", dlerror ());
149 return FALSE;
152 getmime = (np_getmime_func) dlsym (real_plugin, LOADER_RENAMED_NAME(NP_GetMIMEDescription));
153 if (getmime == NULL){
154 fprintf (stderr, "NP_GetMIMEDescription not found %s\n", dlerror ());
155 return FALSE;
158 shutdown = (np_shutdown_func) dlsym (real_plugin, LOADER_RENAMED_NAME(NP_Shutdown));
159 if (shutdown == NULL){
160 fprintf (stderr, "NP_Shutdown not found %s\n", dlerror ());
161 return FALSE;
164 return TRUE;
167 char*
168 NP_GetMIMEDescription (void)
170 if (getmime == NULL)
171 load ();
173 if (getmime != NULL){
174 return (*getmime)();
176 return (char *) "";
179 NPError
180 NP_GetValue (void *future, NPPVariable variable, void *value)
182 if (getvalue == NULL)
183 load ();
185 if (getvalue != NULL)
186 return (*getvalue) (future, variable, value);
188 return NPERR_GENERIC_ERROR;
191 NPError OSCALL
192 NP_Initialize (NPNetscapeFuncs *mozilla_funcs, NPPluginFuncs *plugin_funcs)
194 if (initialize == NULL)
195 load ();
197 if (initialize == NULL)
198 return NPERR_GENERIC_ERROR;
200 NPError res;
202 #ifdef XP_UNIX
203 res = (*initialize) (mozilla_funcs, plugin_funcs);
204 #else
205 res = (*initialize) (mozilla_funcs);
206 #endif
207 return res;
210 NPError OSCALL
211 NP_Shutdown (void)
213 if (shutdown == NULL)
214 load ();
215 if (shutdown != NULL)
216 return (*shutdown) ();
217 return NPERR_GENERIC_ERROR;