prototyped edje_part scriptability and outsourced scriptability code to script.c
[mozilla_edje_plugin.git] / src / npshell.c
blob09c7efc4288233036f820819a4ffe4a81266df2b
1 #include "plugin.h"
3 /*
4 * Implementations of plugin API functions
5 */
7 char*
8 NPP_GetMIMEDescription (void)
10 return (MIME_TYPES_HANDLED);
13 /* TODO: set this variables at the right place to enable scriptability
14 * NPPVpluginScriptableInstance
15 * NPPVpluginScriptableIID
18 NPError
19 NPP_GetValue (NPP instance, NPPVariable variable, void *value)
21 PluginInstance *pi;
22 NPError err = NPERR_NO_ERROR;
24 switch (variable) {
25 case NPPVpluginNameString:
26 *((char **) value) = PLUGIN_NAME;
27 break;
28 case NPPVpluginDescriptionString:
29 *((char **) value) = PLUGIN_DESCRIPTION;
30 break;
31 case NPPVpluginScriptableNPObject:
32 pi = (PluginInstance*) instance->pdata;
33 *(NPObject **) value = mep_getscriptableobject (pi);
34 break;
35 default:
36 err = NPERR_GENERIC_ERROR;
38 return err;
41 int16
42 NPP_HandleEvent (NPP instance, void *event)
45 * needed for windowless mode
47 XEvent *ev = (XEvent *) event;
48 printf ("event occured\n");
49 return TRUE;
52 NPError
53 NPP_Initialize (void)
55 ecore_init ();
56 ecore_config_init ("mozilla_edje_plugin");
57 evas_init ();
58 edje_init ();
60 return NPERR_NO_ERROR;
63 #ifdef OJI
64 jref
65 NPP_GetJavaClass ()
67 return NULL;
69 #endif
71 void
72 NPP_Shutdown (void)
74 edje_shutdown ();
75 evas_shutdown ();
76 ecore_config_shutdown ();
77 ecore_shutdown ();
80 NPError
81 NPP_New (NPMIMEType pluginType,
82 NPP instance,
83 uint16 mode,
84 int16 argc,
85 char* argn[],
86 char* argv[],
87 NPSavedData* saved)
89 if (instance == NULL)
90 return NPERR_INVALID_INSTANCE_ERROR;
91 instance->pdata = NPN_MemAlloc (sizeof (PluginInstance));
93 PluginInstance *pi = (PluginInstance*) instance->pdata;
94 if (pi == NULL)
95 return NPERR_OUT_OF_MEMORY_ERROR;
96 memset(pi, 0, sizeof (PluginInstance));
98 /* mode is NP_EMBED, NP_FULL, or NP_BACKGROUND (see npapi.h) */
99 pi->mode = mode;
100 pi->type = strdup ("application/edje");
101 pi->instance = instance;
103 pi->pluginsPageUrl = NULL;
104 pi->exists = FALSE;
106 /* Parse argument list passed to plugin instance */
107 /* We are interested in these arguments
108 * PLUGINSPAGE = <url>
110 while (argc > 0)
112 argc --;
113 if (argv[argc] != NULL)
115 if (!PL_strcasecmp (argn[argc], "PLUGINSPAGE"))
116 pi->pluginsPageUrl = strdup (argv[argc]);
117 else if (!PL_strcasecmp (argn[argc], "PLUGINURL"))
118 pi->pluginsFileUrl = strdup (argv[argc]);
119 else if (!PL_strcasecmp (argn[argc], "CODEBASE"))
120 pi->pluginsPageUrl = strdup (argv[argc]);
121 else if (!PL_strcasecmp (argn[argc], "CLASSID"))
122 pi->pluginsFileUrl = strdup (argv[argc]);
123 else if (!PL_strcasecmp (argn[argc], "HIDDEN"))
124 pi->pluginsHidden = (!PL_strcasecmp (argv[argc], "TRUE"));
125 else if (!PL_strcasecmp (argn[argc], "DATA"))
126 pi->data = strdup (argv[argc]);
127 else if (!PL_strcasecmp (argn[argc], "GROUP"))
128 pi->group = strdup (argv[argc]);
129 else if (!PL_strcasecmp (argn[argc], "THEME"))
130 pi->theme = strdup (argv[argc]);
134 // create evas for this plugin instance
135 pi->evas = evas_new ();
137 // configure plugin instance
138 mep_configure (pi);
140 // set windowless
142 if (NPN_SetValue (instance, NPPVpluginWindowBool, FALSE))
143 printf ("could not set windowless mode\n");
144 if (NPN_SetValue (instance, NPPVpluginTransparentBool, TRUE))
145 printf ("could not set transparent mode\n");
148 return NPERR_NO_ERROR;
151 NPError
152 NPP_Destroy (NPP instance, NPSavedData** save)
154 if (instance == NULL)
155 return NPERR_INVALID_INSTANCE_ERROR;
157 PluginInstance *pi = (PluginInstance*) instance->pdata;
159 pi->done = 1;
161 if (pi != NULL) {
162 if (pi->type)
163 NPN_MemFree(pi->type);
164 if (pi->pluginsPageUrl)
165 NPN_MemFree(pi->pluginsPageUrl);
166 if (pi->pluginsFileUrl)
167 NPN_MemFree(pi->pluginsFileUrl);
168 if (pi->edj)
169 evas_object_del (pi->edj);
170 if (pi->skin)
171 evas_object_del (pi->skin);
172 if (pi->evas)
173 evas_free (pi->evas);
174 if (pi->data)
175 NPN_MemFree (pi->data);
176 if (pi->group)
177 NPN_MemFree (pi->group);
178 if (pi->theme)
179 NPN_MemFree (pi->theme);
180 if (pi->config_theme)
181 NPN_MemFree (pi->config_theme);
182 if (pi->config_engine)
183 NPN_MemFree (pi->config_engine);
184 NPN_MemFree(instance->pdata);
185 instance->pdata = NULL;
188 return NPERR_NO_ERROR;
191 NPError
192 NPP_SetWindow (NPP instance, NPWindow *window)
194 if (instance == NULL)
195 return NPERR_INVALID_INSTANCE_ERROR;
197 PluginInstance *pi = (PluginInstance *) instance->pdata;
198 if (pi == NULL)
199 return NPERR_INVALID_INSTANCE_ERROR;
201 NPSetWindowCallbackStruct *ws_info = (NPSetWindowCallbackStruct *) window->ws_info;
203 if (pi->window == (Window) window->window) {
204 /* The page with the plugin is being resized.
205 Save any UI information because the next time
206 around expect a SetWindow with a new window
210 fprintf(stderr, "plugin received window resize.\n");
211 fprintf(stderr, "Window=(%i)\n", (int)window);
212 fprintf(stderr, "W=(%i) H=(%i)\n",
213 (int)window->width, (int)window->height);
216 return NPERR_NO_ERROR;
217 } else {
218 pi->window = (Window) window->window;
219 pi->x = window->x;
220 pi->y = window->y;
221 pi->width = window->width;
222 pi->height = window->height;
223 pi->display = ws_info->display;
224 pi->visual = ws_info->visual;
225 pi->depth = ws_info->depth;
226 pi->colormap = ws_info->colormap;
228 int output_method = evas_render_method_lookup (pi->config_engine);
229 evas_output_method_set (pi->evas, output_method);
230 evas_output_size_set (pi->evas, pi->width, pi->height);
231 evas_output_viewport_set (pi->evas, 0, 0, pi->width, pi->height);
232 if (output_method == evas_render_method_lookup ("software_x11"))
234 Evas_Engine_Info_Software_X11 *einfo = (Evas_Engine_Info_Software_X11 *) evas_engine_info_get (pi->evas);
235 /* the following is specific to the engine */
236 einfo->info.display = pi->display;
237 einfo->info.visual = pi->visual;
238 einfo->info.colormap = pi->colormap;
239 einfo->info.drawable = (Drawable) pi->window;
240 einfo->info.depth = pi->depth;
241 einfo->info.rotation = 0;
242 einfo->info.debug = 0;
243 evas_engine_info_set(pi->evas, (Evas_Engine_Info *) einfo);
245 else if (output_method == evas_render_method_lookup ("gl_x11"))
247 Evas_Engine_Info_GL_X11 *einfo = (Evas_Engine_Info_GL_X11 *) evas_engine_info_get (pi->evas);
248 /* the following is specific to the engine */
249 einfo->info.display = pi->display;
250 einfo->info.visual = pi->visual;
251 einfo->info.colormap = pi->colormap;
252 einfo->info.drawable = pi->window;
253 einfo->info.depth = pi->depth;
254 evas_engine_info_set(pi->evas, (Evas_Engine_Info *) einfo);
256 else
257 printf ("no supported engine found\n");
259 // add animator to render evas
260 ecore_animator_add (mep_animator, pi);
261 ecore_animator_frametime_set (1.0 / (float) pi->config_fps);
262 edje_frametime_set (1.0 / (float) pi->config_fps);
264 // add theme skin if stated
265 if (!strcmp (pi->theme, "none"))
267 // do nothing
269 else if (!strcmp (pi->theme, "user"))
271 pi->skin = edje_object_add (pi->evas);
272 char PATH [256];
273 sprintf (PATH, "%s/%s", THEMESDIR, pi->config_theme);
274 edje_object_file_set (pi->skin, PATH, "mozilla_edje_plugin/theme");
275 evas_object_resize (pi->skin, pi->width, pi->height);
276 evas_object_show (pi->skin);
277 evas_object_focus_set (pi->skin, 1);
279 else
281 pi->skin = edje_object_add (pi->evas);
282 edje_object_file_set (pi->skin, pi->theme, "mozilla_edje_plugin/theme");
283 evas_object_resize (pi->skin, pi->width, pi->height);
284 evas_object_show (pi->skin);
285 evas_object_focus_set (pi->skin, 1);
288 pi->edj = edje_object_add (pi->evas);
289 edje_object_file_set (pi->edj, pi->data, pi->group);
290 evas_object_data_set (pi->edj, "pi", (void *) pi);
292 evas_object_resize (pi->edj, pi->width, pi->height);
293 evas_object_show (pi->edj);
294 evas_object_focus_set (pi->edj, 1);
295 evas_event_feed_mouse_in (pi->evas, 0, NULL);
297 if (pi->skin) // if not none
299 edje_object_part_swallow (pi->skin, "data", pi->edj);
300 edje_object_signal_callback_add (pi->skin, "ui,play", "*", mep_cb_ui_play, pi);
301 edje_object_signal_callback_add (pi->skin, "ui,pause", "*", mep_cb_ui_pause, pi);
302 edje_object_signal_callback_add (pi->skin, "ui,config", "*", mep_cb_ui_config, pi);
303 edje_object_signal_callback_add (pi->skin, "ui,save", "*", mep_cb_ui_save, pi);
305 edje_object_signal_callback_add (pi->edj, "*", "*", mep_cb_all, pi);
307 // set up message system
308 Widget xtwidget = XtWindowToWidget (pi->display, pi->window);
309 long event_mask = NoEventMask |
310 ExposureMask |
311 ButtonPressMask |
312 ButtonReleaseMask |
313 PointerMotionMask |
314 StructureNotifyMask |
315 SubstructureNotifyMask |
316 EnterWindowMask |
317 LeaveWindowMask;
319 XSelectInput (pi->display, pi->window, event_mask);
320 XtAddEventHandler (xtwidget, event_mask, True, mep_event_handler, pi);
321 pi->atom = XInternAtom (pi->display, "edje_atom", False);
323 pi->msg.type = ClientMessage;
324 pi->msg.display = pi->display;
325 pi->msg.window = pi->window;
326 pi->msg.message_type = pi->atom;
327 pi->msg.format = 32;
328 pi->msg.data.l[0] = 0;
330 pi->done = 0;
332 // initialize asynccallback loop
333 mep_asynccall (pi);
336 return NPERR_NO_ERROR;
339 NPError
340 NPP_NewStream (NPP instance,
341 NPMIMEType type,
342 NPStream *stream,
343 NPBool seekable,
344 uint16 *stype)
346 if (instance == NULL)
347 return NPERR_INVALID_INSTANCE_ERROR;
349 return NPERR_NO_ERROR;
352 int32
353 NPP_WriteReady (NPP instance, NPStream *stream)
355 if (instance == NULL)
356 return NPERR_INVALID_INSTANCE_ERROR;
358 /* We don't want any data, kill the stream */
359 NPN_DestroyStream(instance, stream, NPRES_DONE);
361 /* Number of bytes ready to accept in NPP_Write() */
362 return -1L; /* don't accept any bytes in NPP_Write() */
365 int32
366 NPP_Write (NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
368 if (instance == NULL)
369 return NPERR_INVALID_INSTANCE_ERROR;
371 /* We don't want any data, kill the stream */
372 NPN_DestroyStream(instance, stream, NPRES_DONE);
374 return -1L; /* don't accept any bytes in NPP_Write() */
377 NPError
378 NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
380 if (instance == NULL)
381 return NPERR_INVALID_INSTANCE_ERROR;
383 /***** Insert NPP_DestroyStream code here *****\
384 PluginInstance* pi;
385 pi = (PluginInstance*) instance->pdata;
386 \**********************************************/
388 return NPERR_NO_ERROR;
391 void
392 NPP_StreamAsFile (NPP instance, NPStream *stream, const char* fname)
394 /***** Insert NPP_StreamAsFile code here *****\
395 PluginInstance* pi;
396 if (instance != NULL)
397 pi = (PluginInstance*) instance->pdata;
398 \*********************************************/
401 void
402 NPP_URLNotify (NPP instance, const char* url,
403 NPReason reason, void* notifyData)
405 /***** Insert NPP_URLNotify code here *****\
406 PluginInstance* pi;
407 if (instance != NULL)
408 pi = (PluginInstance*) instance->pdata;
409 \*********************************************/
412 void
413 NPP_Print (NPP instance, NPPrint* printInfo)
415 if(printInfo == NULL)
416 return;
418 if (instance != NULL) {
419 /***** Insert NPP_Print code here *****\
420 PluginInstance* pi = (PluginInstance*) instance->pdata;
421 \**************************************/
423 if (printInfo->mode == NP_FULL) {
425 * PLUGIN DEVELOPERS:
426 * If your plugin would like to take over
427 * printing completely when it is in full-screen mode,
428 * set printInfo->pluginPrinted to TRUE and print your
429 * plugin as you see fit. If your plugin wants Netscape
430 * to handle printing in this case, set
431 * printInfo->pluginPrinted to FALSE (the default) and
432 * do nothing. If you do want to handle printing
433 * yourself, printOne is true if the print button
434 * (as opposed to the print menu) was clicked.
435 * On the Macintosh, platformPrint is a THPrint; on
436 * Windows, platformPrint is a structure
437 * (defined in npapi.h) containing the printer name, port,
438 * etc.
441 /***** Insert NPP_Print code here *****\
442 void* platformPrint =
443 printInfo->print.fullPrint.platformPrint;
444 NPBool printOne =
445 printInfo->print.fullPrint.printOne;
446 \**************************************/
448 /* Do the default*/
449 printInfo->print.fullPrint.pluginPrinted = FALSE;
451 else { /* If not fullscreen, we must be embedded */
453 * PLUGIN DEVELOPERS:
454 * If your plugin is embedded, or is full-screen
455 * but you returned false in pluginPrinted above, NPP_Print
456 * will be called with mode == NP_EMBED. The NPWindow
457 * in the printInfo gives the location and dimensions of
458 * the embedded plugin on the printed page. On the
459 * Macintosh, platformPrint is the printer port; on
460 * Windows, platformPrint is the handle to the printing
461 * device context.
464 /***** Insert NPP_Print code here *****\
465 NPWindow* printWindow =
466 &(printInfo->print.embedPrint.window);
467 void* platformPrint =
468 printInfo->print.embedPrint.platformPrint;
469 \**************************************/