/[zanavi_public1]/navit/navit/browserplugin.c
ZANavi

Contents of /navit/navit/browserplugin.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (8 years, 9 months ago) by zoff99
File MIME type: text/plain
File size: 14264 byte(s)
import files
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 * Josh Aas <josh@mozilla.com>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 #include "browserplugin.h"
40 #include <navit/navit.h>
41 #include <navit/item.h>
42 #include <navit/config_.h>
43 #include <navit/callback.h>
44
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48
49 #define PLUGIN_NAME "Navit Sample Plug-in"
50 #define PLUGIN_DESCRIPTION PLUGIN_NAME " (Mozilla SDK)"
51 #define PLUGIN_VERSION "1.0.0.0"
52
53 static NPNetscapeFuncs *sBrowserFuncs = NULL;
54 extern struct NPClass navitclass,navitclass2;
55
56 typedef struct InstanceData {
57 NPP npp;
58 NPWindow window;
59 } InstanceData;
60
61
62 static void
63 fillPluginFunctionTable(NPPluginFuncs * pFuncs)
64 {
65 pFuncs->version = 11;
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;
80 }
81
82 NP_EXPORT(NPError)
83 NP_Initialize(NPNetscapeFuncs * bFuncs, NPPluginFuncs * pFuncs)
84 {
85 NPError err = NPERR_NO_ERROR;
86 NPBool supportsXEmbed = false;
87 NPNToolkitType toolkit = 0;
88
89 sBrowserFuncs = bFuncs;
90
91 fillPluginFunctionTable(pFuncs);
92 err =
93 sBrowserFuncs->getvalue(NULL, NPNVSupportsXEmbedBool,
94 (void *) &supportsXEmbed);
95 if (err != NPERR_NO_ERROR || supportsXEmbed != true)
96 return NPERR_INCOMPATIBLE_VERSION_ERROR;
97 err =
98 sBrowserFuncs->getvalue(NULL, NPNVToolkit, (void *) &toolkit);
99
100 if (err != NPERR_NO_ERROR || toolkit != NPNVGtk2)
101 return NPERR_INCOMPATIBLE_VERSION_ERROR;
102
103 return NPERR_NO_ERROR;
104 }
105
106 NP_EXPORT(char *)
107 NP_GetPluginVersion()
108 {
109 return PLUGIN_VERSION;
110 }
111
112 NP_EXPORT(char *)
113 NP_GetMIMEDescription()
114 {
115 return "application/navit-plugin:nsc:Navit plugin";
116 }
117
118 NP_EXPORT(NPError)
119 NP_GetValue(void *future, NPPVariable aVariable, void *aValue)
120 {
121 fprintf(stderr, "NP_GetValue %d\n", aVariable);
122 switch (aVariable) {
123 case NPPVpluginNameString:
124 *((char **) aValue) = PLUGIN_NAME;
125 break;
126 case NPPVpluginDescriptionString:
127 *((char **) aValue) = PLUGIN_DESCRIPTION;
128 break;
129 default:
130 return NPERR_INVALID_PARAM;
131 break;
132 }
133 return NPERR_NO_ERROR;
134 }
135
136 NP_EXPORT(NPError)
137 NP_Shutdown()
138 {
139 return NPERR_NO_ERROR;
140 }
141
142 NPError
143 NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc,
144 char *argn[], char *argv[], NPSavedData * saved)
145 {
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;
154 }
155 #if 0
156 sBrowserFuncs->setvalue(instance, NPPVpluginWindowBool,
157 (void *) true);
158 #endif
159
160 // set up our our instance data
161 InstanceData *instanceData =
162 (InstanceData *) malloc(sizeof(InstanceData));
163 if (!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);
169
170 main_real(1, args);
171 return NPERR_NO_ERROR;
172 }
173
174 NPError
175 NPP_Destroy(NPP instance, NPSavedData ** save)
176 {
177 InstanceData *instanceData = (InstanceData *) (instance->pdata);
178 free(instanceData);
179 return NPERR_NO_ERROR;
180 }
181
182 NPError
183 NPP_SetWindow(NPP instance, NPWindow * window)
184 {
185 struct attr navit,graphics,windowid;
186 InstanceData *instanceData = (InstanceData *) (instance->pdata);
187 if (window->window == instanceData->window.window)
188 return;
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;
194 }
195 if (!navit_get_attr(navit.u.navit, attr_graphics, &graphics, NULL)) {
196 fprintf(stderr,"No Graphics\n");
197 return NPERR_GENERIC_ERROR;
198 }
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;
204 }
205
206 return NPERR_NO_ERROR;
207 }
208
209 NPError
210 NPP_NewStream(NPP instance, NPMIMEType type, NPStream * stream,
211 NPBool seekable, uint16_t * stype)
212 {
213 return NPERR_GENERIC_ERROR;
214 }
215
216 NPError
217 NPP_DestroyStream(NPP instance, NPStream * stream, NPReason reason)
218 {
219 return NPERR_GENERIC_ERROR;
220 }
221
222 int32_t
223 NPP_WriteReady(NPP instance, NPStream * stream)
224 {
225 return 0;
226 }
227
228 int32_t
229 NPP_Write(NPP instance, NPStream * stream, int32_t offset, int32_t len,
230 void *buffer)
231 {
232 return 0;
233 }
234
235 void
236 NPP_StreamAsFile(NPP instance, NPStream * stream, const char *fname)
237 {
238
239 }
240
241 void
242 NPP_Print(NPP instance, NPPrint * platformPrint)
243 {
244
245 }
246
247 int16_t
248 NPP_HandleEvent(NPP instance, void *event)
249 {
250
251
252 return 0;
253
254 #if 0
255 InstanceData *instanceData = (InstanceData *) (instance->pdata);
256 XEvent *nativeEvent = (XEvent *) event;
257
258 if (nativeEvent->type != GraphicsExpose)
259 return 0;
260
261 XGraphicsExposeEvent *expose = &nativeEvent->xgraphicsexpose;
262 instanceData->window.window = (void *) (expose->drawable);
263
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);
269 #endif
270
271 return 1;
272 }
273
274 void
275 NPP_URLNotify(NPP instance, const char *URL, NPReason reason,
276 void *notifyData)
277 {
278
279 }
280
281 struct NavitObject {
282 NPClass *class;
283 uint32_t referenceCount;
284 InstanceData *instanceData;
285 int is_attr;
286 struct attr attr;
287 };
288
289 void
290 printIdentifier(NPIdentifier name)
291 {
292 NPUTF8 *str;
293 str = sBrowserFuncs->utf8fromidentifier(name);
294 fprintf(stderr, "%s\n", str);
295 sBrowserFuncs->memfree(str);
296 }
297
298 NPObject *
299 allocate(NPP npp, NPClass * aClass)
300 {
301 struct NavitObject *ret = calloc(sizeof(struct NavitObject), 1);
302 if (ret) {
303 ret->class = aClass;
304 ret->instanceData = npp->pdata;
305 fprintf(stderr, "instanceData for %p is %p\n", ret,
306 ret->instanceData);
307 }
308 return (NPObject *) ret;
309 }
310
311
312 void
313 invalidate(NPObject * npobj)
314 {
315 fprintf(stderr, "invalidate\n");
316 }
317
318
319 bool
320 hasMethod(NPObject * npobj, NPIdentifier name)
321 {
322 fprintf(stderr, "hasMethod\n");
323 printIdentifier(name);
324 if (name == sBrowserFuncs->getstringidentifier("command"))
325 return true;
326 if (name == sBrowserFuncs->getstringidentifier("get_attr"))
327 return true;
328 if (name == sBrowserFuncs->getstringidentifier("toString"))
329 return true;
330 if (name == sBrowserFuncs->getstringidentifier("nativeMethod"))
331 return true;
332 if (name ==
333 sBrowserFuncs->getstringidentifier("anotherNativeMethod"))
334 return true;
335
336 return false;
337 }
338
339 enum attr_type
340 variant_to_attr_type(const NPVariant *variant)
341 {
342 if (NPVARIANT_IS_STRING(*variant))
343 return attr_from_name(NPVARIANT_TO_STRING(*variant).utf8characters);
344 return attr_none;
345 }
346
347
348
349 bool
350 invoke(NPObject * npobj, NPIdentifier name, const NPVariant * args,
351 uint32_t argCount, NPVariant * result)
352 {
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;
358 struct attr attr;
359 if (!argCount)
360 return false;
361 attr_type=variant_to_attr_type(&args[0]);
362 if (attr_type == attr_none)
363 return false;
364 if (config_get_attr(config, attr_type, &attr, NULL)) {
365 struct NavitObject *obj2 = (struct NavitObject *)sBrowserFuncs->createobject(obj->instanceData->npp, &navitclass);
366 obj2->is_attr=1;
367 obj2->attr=attr;
368 OBJECT_TO_NPVARIANT((NPObject *)obj2, *result);
369 return true;
370 } else {
371 VOID_TO_NPVARIANT(*result);
372 return true;
373 }
374 }
375 if (name == sBrowserFuncs->getstringidentifier("command")) {
376 enum attr_type attr_type;
377 struct attr attr;
378 NPObject *window;
379 NPError err;
380 NPVariant value;
381 if (!argCount || !NPVARIANT_IS_STRING(args[0]))
382 return false;
383 if (navit_get_attr(obj->attr.u.navit, attr_callback_list, &attr, NULL)) {
384 int valid=0;
385 callback_list_call_attr_4(attr.u.callback_list, attr_command, NPVARIANT_TO_STRING(args[0]), NULL, NULL, &valid);
386 }
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);
397
398
399 //VOID_TO_NPVARIANT(*result);
400 return true;
401 }
402 if (name == sBrowserFuncs->getstringidentifier("toString")) {
403 char *s;
404 if (obj->is_attr) {
405 s="[NavitObject attribute]";
406 STRINGZ_TO_NPVARIANT(strdup(s), *result);
407 return true;
408 }
409 s=g_strdup_printf("[NavitObject %s]",attr_to_name(obj->attr.type));
410 STRINGZ_TO_NPVARIANT(strdup(s), *result);
411 g_free(s);
412 return true;
413 }
414 if (name == sBrowserFuncs->getstringidentifier("nativeMethod")) {
415 result->type = NPVariantType_Int32;
416 result->value.intValue = 23;
417 return true;
418 }
419 if (name ==
420 sBrowserFuncs->getstringidentifier("anotherNativeMethod")) {
421 result->type = NPVariantType_Int32;
422 result->value.intValue = 42;
423 return true;
424 }
425 return false;
426 }
427
428
429 bool
430 invokeDefault(NPObject * npobj, const NPVariant * args, uint32_t argCount,
431 NPVariant * result)
432 {
433 fprintf(stderr, "invokeDefault\n");
434 return false;
435 }
436
437
438 bool
439 hasProperty(NPObject * npobj, NPIdentifier name)
440 {
441 struct NavitObject *obj = (struct NavitObject *) npobj;
442 fprintf(stderr, "hasProperty\n");
443 printIdentifier(name);
444 if (obj->is_attr && name == sBrowserFuncs->getstringidentifier("type"))
445 return true;
446 if (obj->is_attr && name == sBrowserFuncs->getstringidentifier("val"))
447 return true;
448 if (name == sBrowserFuncs->getstringidentifier("nativeProperty")) {
449 return true;
450 }
451 return false;
452 }
453
454
455 bool
456 getProperty(NPObject * npobj, NPIdentifier name, NPVariant * result)
457 {
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);
463 return true;
464 }
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);
469 return true;
470 }
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);
475 return true;
476 }
477 return false;
478 }
479
480
481 bool
482 setProperty(NPObject * npobj, NPIdentifier name, const NPVariant * value)
483 {
484 fprintf(stderr, "setProperty\n");
485 return false;
486 }
487
488
489 bool
490 removeProperty(NPObject * npobj, NPIdentifier name)
491 {
492 fprintf(stderr, "removeProperty\n");
493 return false;
494 }
495
496
497
498
499
500 struct NPClass navitclass = {
501 1,
502 allocate,
503 NULL, /* deallocate */
504 invalidate,
505 hasMethod,
506 invoke,
507 invokeDefault,
508 hasProperty,
509 getProperty,
510 setProperty,
511 removeProperty,
512 };
513
514 struct NPClass navitclass2 = {
515 1,
516 allocate, /* allocate */
517 NULL, /* deallocate */
518 invalidate,
519 hasMethod,
520 invoke,
521 invokeDefault,
522 hasProperty,
523 getProperty,
524 setProperty,
525 removeProperty,
526 };
527
528
529 NPError
530 NPP_GetValue(NPP instance, NPPVariable variable, void *value)
531 {
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;
538 }
539
540 if (variable == NPPVpluginScriptableNPObject) {
541 *(NPObject **) value =
542 sBrowserFuncs->createobject(instance, &navitclass);
543 return NPERR_NO_ERROR;
544 }
545 return NPERR_GENERIC_ERROR;
546 }
547
548 NPError
549 NPP_SetValue(NPP instance, NPNVariable variable, void *value)
550 {
551 return NPERR_GENERIC_ERROR;
552 }

   
Visit the ZANavi Wiki