1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-navit-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Josh Aas <josh@mozilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "browserplugin.h"
40 #include <navit/navit.h>
41 #include <navit/item.h>
42 #include <navit/config_.h>
43 #include <navit/callback.h>
49 #define PLUGIN_NAME "Navit Sample Plug-in"
50 #define PLUGIN_DESCRIPTION PLUGIN_NAME " (Mozilla SDK)"
51 #define PLUGIN_VERSION "1.0.0.0"
53 static NPNetscapeFuncs *sBrowserFuncs = NULL;
54 extern struct NPClass navitclass,navitclass2;
56 typedef struct InstanceData {
63 fillPluginFunctionTable(NPPluginFuncs * pFuncs)
66 pFuncs->size = sizeof(*pFuncs);
67 pFuncs->newp = NPP_New;
68 pFuncs->destroy = NPP_Destroy;
69 pFuncs->setwindow = NPP_SetWindow;
70 pFuncs->newstream = NPP_NewStream;
71 pFuncs->destroystream = NPP_DestroyStream;
72 pFuncs->asfile = NPP_StreamAsFile;
73 pFuncs->writeready = NPP_WriteReady;
74 pFuncs->write = NPP_Write;
75 pFuncs->print = NPP_Print;
76 pFuncs->event = NPP_HandleEvent;
77 pFuncs->urlnotify = NPP_URLNotify;
78 pFuncs->getvalue = NPP_GetValue;
79 pFuncs->setvalue = NPP_SetValue;
83 NP_Initialize(NPNetscapeFuncs * bFuncs, NPPluginFuncs * pFuncs)
85 NPError err = NPERR_NO_ERROR;
86 NPBool supportsXEmbed = false;
87 NPNToolkitType toolkit = 0;
89 sBrowserFuncs = bFuncs;
91 fillPluginFunctionTable(pFuncs);
93 sBrowserFuncs->getvalue(NULL, NPNVSupportsXEmbedBool,
94 (void *) &supportsXEmbed);
95 if (err != NPERR_NO_ERROR || supportsXEmbed != true)
96 return NPERR_INCOMPATIBLE_VERSION_ERROR;
98 sBrowserFuncs->getvalue(NULL, NPNVToolkit, (void *) &toolkit);
100 if (err != NPERR_NO_ERROR || toolkit != NPNVGtk2)
101 return NPERR_INCOMPATIBLE_VERSION_ERROR;
103 return NPERR_NO_ERROR;
107 NP_GetPluginVersion()
109 return PLUGIN_VERSION;
113 NP_GetMIMEDescription()
115 return "application/navit-plugin:nsc:Navit plugin";
119 NP_GetValue(void *future, NPPVariable aVariable, void *aValue)
121 fprintf(stderr, "NP_GetValue %d\n", aVariable);
123 case NPPVpluginNameString:
124 *((char **) aValue) = PLUGIN_NAME;
126 case NPPVpluginDescriptionString:
127 *((char **) aValue) = PLUGIN_DESCRIPTION;
130 return NPERR_INVALID_PARAM;
133 return NPERR_NO_ERROR;
139 return NPERR_NO_ERROR;
143 NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc,
144 char *argn[], char *argv[], NPSavedData * saved)
146 char *args[]={"/usr/bin/navit",NULL};
147 // Make sure we can render this plugin
148 NPBool browserSupportsWindowless = false;
149 sBrowserFuncs->getvalue(instance, NPNVSupportsWindowless,
150 &browserSupportsWindowless);
151 if (!browserSupportsWindowless) {
152 printf("Windowless mode not supported by the browser\n");
153 return NPERR_GENERIC_ERROR;
156 sBrowserFuncs->setvalue(instance, NPPVpluginWindowBool,
160 // set up our our instance data
161 InstanceData *instanceData =
162 (InstanceData *) malloc(sizeof(InstanceData));
164 return NPERR_OUT_OF_MEMORY_ERROR;
165 memset(instanceData, 0, sizeof(InstanceData));
166 instanceData->npp = instance;
167 instance->pdata = instanceData;
168 fprintf(stderr, "npp=%p\n", instance);
171 return NPERR_NO_ERROR;
175 NPP_Destroy(NPP instance, NPSavedData ** save)
177 InstanceData *instanceData = (InstanceData *) (instance->pdata);
179 return NPERR_NO_ERROR;
183 NPP_SetWindow(NPP instance, NPWindow * window)
185 struct attr navit,graphics,windowid;
186 InstanceData *instanceData = (InstanceData *) (instance->pdata);
187 if (window->window == instanceData->window.window)
189 instanceData->window = *window;
190 fprintf(stderr, "Window 0x%x\n", window->window);
191 if (!config_get_attr(config, attr_navit, &navit, NULL)) {
192 fprintf(stderr,"No navit\n");
193 return NPERR_GENERIC_ERROR;
195 if (!navit_get_attr(navit.u.navit, attr_graphics, &graphics, NULL)) {
196 fprintf(stderr,"No Graphics\n");
197 return NPERR_GENERIC_ERROR;
199 windowid.type=attr_windowid;
200 windowid.u.num=window->window;
201 if (!graphics_set_attr(graphics.u.graphics, &windowid)) {
202 fprintf(stderr,"Failed to set window\n");
203 return NPERR_GENERIC_ERROR;
206 return NPERR_NO_ERROR;
210 NPP_NewStream(NPP instance, NPMIMEType type, NPStream * stream,
211 NPBool seekable, uint16_t * stype)
213 return NPERR_GENERIC_ERROR;
217 NPP_DestroyStream(NPP instance, NPStream * stream, NPReason reason)
219 return NPERR_GENERIC_ERROR;
223 NPP_WriteReady(NPP instance, NPStream * stream)
229 NPP_Write(NPP instance, NPStream * stream, int32_t offset, int32_t len,
236 NPP_StreamAsFile(NPP instance, NPStream * stream, const char *fname)
242 NPP_Print(NPP instance, NPPrint * platformPrint)
248 NPP_HandleEvent(NPP instance, void *event)
255 InstanceData *instanceData = (InstanceData *) (instance->pdata);
256 XEvent *nativeEvent = (XEvent *) event;
258 if (nativeEvent->type != GraphicsExpose)
261 XGraphicsExposeEvent *expose = &nativeEvent->xgraphicsexpose;
262 instanceData->window.window = (void *) (expose->drawable);
264 GdkNativeWindow nativeWinId = (XID) (instanceData->window.window);
265 GdkDrawable *gdkWindow =
266 GDK_DRAWABLE(gdk_window_foreign_new(nativeWinId));
267 drawWindow(instanceData, gdkWindow);
268 g_object_unref(gdkWindow);
275 NPP_URLNotify(NPP instance, const char *URL, NPReason reason,
283 uint32_t referenceCount;
284 InstanceData *instanceData;
290 printIdentifier(NPIdentifier name)
293 str = sBrowserFuncs->utf8fromidentifier(name);
294 fprintf(stderr, "%s\n", str);
295 sBrowserFuncs->memfree(str);
299 allocate(NPP npp, NPClass * aClass)
301 struct NavitObject *ret = calloc(sizeof(struct NavitObject), 1);
304 ret->instanceData = npp->pdata;
305 fprintf(stderr, "instanceData for %p is %p\n", ret,
308 return (NPObject *) ret;
313 invalidate(NPObject * npobj)
315 fprintf(stderr, "invalidate\n");
320 hasMethod(NPObject * npobj, NPIdentifier name)
322 fprintf(stderr, "hasMethod\n");
323 printIdentifier(name);
324 if (name == sBrowserFuncs->getstringidentifier("command"))
326 if (name == sBrowserFuncs->getstringidentifier("get_attr"))
328 if (name == sBrowserFuncs->getstringidentifier("toString"))
330 if (name == sBrowserFuncs->getstringidentifier("nativeMethod"))
333 sBrowserFuncs->getstringidentifier("anotherNativeMethod"))
340 variant_to_attr_type(const NPVariant *variant)
342 if (NPVARIANT_IS_STRING(*variant))
343 return attr_from_name(NPVARIANT_TO_STRING(*variant).utf8characters);
350 invoke(NPObject * npobj, NPIdentifier name, const NPVariant * args,
351 uint32_t argCount, NPVariant * result)
353 struct NavitObject *obj = (struct NavitObject *) npobj;
354 fprintf(stderr, "invoke\n");
355 printIdentifier(name);
356 if (name == sBrowserFuncs->getstringidentifier("get_attr")) {
357 enum attr_type attr_type;
361 attr_type=variant_to_attr_type(&args[0]);
362 if (attr_type == attr_none)
364 if (config_get_attr(config, attr_type, &attr, NULL)) {
365 struct NavitObject *obj2 = (struct NavitObject *)sBrowserFuncs->createobject(obj->instanceData->npp, &navitclass);
368 OBJECT_TO_NPVARIANT((NPObject *)obj2, *result);
371 VOID_TO_NPVARIANT(*result);
375 if (name == sBrowserFuncs->getstringidentifier("command")) {
376 enum attr_type attr_type;
381 if (!argCount || !NPVARIANT_IS_STRING(args[0]))
383 if (navit_get_attr(obj->attr.u.navit, attr_callback_list, &attr, NULL)) {
385 callback_list_call_attr_4(attr.u.callback_list, attr_command, NPVARIANT_TO_STRING(args[0]), NULL, NULL, &valid);
387 err=sBrowserFuncs->getvalue(obj->instanceData->npp, NPNVWindowNPObject, (void *) &window);
388 fprintf(stderr,"error1:%d\n",err);
389 //OBJECT_TO_NPVARIANT(window, *result);
390 err=sBrowserFuncs->invoke(obj->instanceData->npp, window, sBrowserFuncs->getstringidentifier("Array"), window, 0, result);
391 fprintf(stderr,"error2:%d\n",err);
392 INT32_TO_NPVARIANT(23, value);
393 err=sBrowserFuncs->setproperty(obj->instanceData->npp, NPVARIANT_TO_OBJECT(*result), sBrowserFuncs->getintidentifier(0), &value);
394 INT32_TO_NPVARIANT(42, value);
395 err=sBrowserFuncs->setproperty(obj->instanceData->npp, NPVARIANT_TO_OBJECT(*result), sBrowserFuncs->getintidentifier(1), &value);
396 fprintf(stderr,"error3:%d\n",err);
399 //VOID_TO_NPVARIANT(*result);
402 if (name == sBrowserFuncs->getstringidentifier("toString")) {
405 s="[NavitObject attribute]";
406 STRINGZ_TO_NPVARIANT(strdup(s), *result);
409 s=g_strdup_printf("[NavitObject %s]",attr_to_name(obj->attr.type));
410 STRINGZ_TO_NPVARIANT(strdup(s), *result);
414 if (name == sBrowserFuncs->getstringidentifier("nativeMethod")) {
415 result->type = NPVariantType_Int32;
416 result->value.intValue = 23;
420 sBrowserFuncs->getstringidentifier("anotherNativeMethod")) {
421 result->type = NPVariantType_Int32;
422 result->value.intValue = 42;
430 invokeDefault(NPObject * npobj, const NPVariant * args, uint32_t argCount,
433 fprintf(stderr, "invokeDefault\n");
439 hasProperty(NPObject * npobj, NPIdentifier name)
441 struct NavitObject *obj = (struct NavitObject *) npobj;
442 fprintf(stderr, "hasProperty\n");
443 printIdentifier(name);
444 if (obj->is_attr && name == sBrowserFuncs->getstringidentifier("type"))
446 if (obj->is_attr && name == sBrowserFuncs->getstringidentifier("val"))
448 if (name == sBrowserFuncs->getstringidentifier("nativeProperty")) {
456 getProperty(NPObject * npobj, NPIdentifier name, NPVariant * result)
458 struct NavitObject *obj = (struct NavitObject *) npobj;
459 fprintf(stderr, "getProperty %p\n", obj);
460 fprintf(stderr, "instanceData %p\n", obj->instanceData);
461 if (obj->is_attr && name == sBrowserFuncs->getstringidentifier("type")) {
462 STRINGZ_TO_NPVARIANT(strdup(attr_to_name(obj->attr.type)), *result);
465 if (obj->is_attr && name == sBrowserFuncs->getstringidentifier("val")) {
466 struct NavitObject *obj2 = (struct NavitObject *)sBrowserFuncs->createobject(obj->instanceData->npp, &navitclass);
467 obj2->attr=obj->attr;
468 OBJECT_TO_NPVARIANT((NPObject *)obj2, *result);
471 if (name == sBrowserFuncs->getstringidentifier("nativeProperty")) {
472 result->type = NPVariantType_Object;
473 fprintf(stderr, "npp=%p\n", obj->instanceData->npp);
474 result->value.objectValue = sBrowserFuncs->createobject(obj->instanceData->npp, &navitclass2);
482 setProperty(NPObject * npobj, NPIdentifier name, const NPVariant * value)
484 fprintf(stderr, "setProperty\n");
490 removeProperty(NPObject * npobj, NPIdentifier name)
492 fprintf(stderr, "removeProperty\n");
500 struct NPClass navitclass = {
503 NULL, /* deallocate */
514 struct NPClass navitclass2 = {
516 allocate, /* allocate */
517 NULL, /* deallocate */
530 NPP_GetValue(NPP instance, NPPVariable variable, void *value)
532 fprintf(stderr, "NPP_GetValue %d %d\n", variable,
533 NPPVpluginScriptableNPObject);
534 if (variable == NPPVpluginNeedsXEmbed) {
535 *((NPBool *) value) = true;
536 fprintf(stderr, "Xembedd\n");
537 return NPERR_NO_ERROR;
540 if (variable == NPPVpluginScriptableNPObject) {
541 *(NPObject **) value =
542 sBrowserFuncs->createobject(instance, &navitclass);
543 return NPERR_NO_ERROR;
545 return NPERR_GENERIC_ERROR;
549 NPP_SetValue(NPP instance, NPNVariable variable, void *value)
551 return NPERR_GENERIC_ERROR;