]> git.sesse.net Git - vlc/blob - projects/mozilla/support/npmac.cpp
72747b6dc3f101a184637d7b34b92734fffada7f
[vlc] / projects / mozilla / support / npmac.cpp
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * Safari/Mozilla/Firefox plugin for VLC
4  * Copyright (C) 2009, Jean-Paul Saman <jpsaman@videolan.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
22 //
23 // npmac.cpp
24 //
25 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include <Processes.h>
32 #include <Gestalt.h>
33 #include <CodeFragments.h>
34 #include <Timer.h>
35 #include <Resources.h>
36 #include <ToolUtils.h>
37
38 #define XP_MACOSX 1
39 #undef TARGET_RT_MAC_CFM
40
41 //
42 // A4Stuff.h contains the definition of EnterCodeResource and
43 // EnterCodeResource, used for setting up the code resource’s
44 // globals for 68K (analagous to the function SetCurrentA5
45 // defined by the toolbox).
46 //
47 // A4Stuff does not exist as of CW 7. Define them to nothing.
48 //
49
50 #if (defined(__MWERKS__) && (__MWERKS__ >= 0x2400)) || defined(__GNUC__)
51     #define EnterCodeResource()
52     #define ExitCodeResource()
53 #else
54     #include <A4Stuff.h>
55 #endif
56
57 #include "npapi.h"
58
59 //
60 // The Mixed Mode procInfos defined in npupp.h assume Think C-
61 // style calling conventions.  These conventions are used by
62 // Metrowerks with the exception of pointer return types, which
63 // in Metrowerks 68K are returned in A0, instead of the standard
64 // D0. Thus, since NPN_MemAlloc and NPN_UserAgent return pointers,
65 // Mixed Mode will return the values to a 68K plugin in D0, but
66 // a 68K plugin compiled by Metrowerks will expect the result in
67 // A0.  The following pragma forces Metrowerks to use D0 instead.
68 //
69 #ifdef __MWERKS__
70 #ifndef powerc
71 #pragma pointers_in_D0
72 #endif
73 #endif
74
75 #ifdef XP_UNIX
76 #undef XP_UNIX
77 #endif
78
79 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
80 #include "npupp.h" 
81 #else
82 #include "npfunctions.h"
83 #endif
84
85 #include "../vlcshell.h"
86
87 #ifdef __MWERKS__
88 #ifndef powerc
89 #pragma pointers_in_A0
90 #endif
91 #endif
92
93 // The following fix for static initializers (which fixes a previous
94 // incompatibility with some parts of PowerPlant, was submitted by
95 // Jan Ulbrich.
96 #ifdef __MWERKS__
97     #ifdef __cplusplus
98     extern "C" {
99     #endif
100         #ifndef powerc
101             extern void __InitCode__(void);
102         #else
103             extern void __sinit(void);
104             #define __InitCode__ __sinit
105         #endif
106         extern void __destroy_global_chain(void);
107     #ifdef __cplusplus
108     }
109     #endif // __cplusplus
110 #endif // __MWERKS__
111
112 //
113 // Define PLUGIN_TRACE to 1 to have the wrapper functions emit
114 // DebugStr messages whenever they are called.
115 //
116 #define PLUGIN_TRACE 0
117
118 #if PLUGIN_TRACE
119 #define PLUGINDEBUGSTR(msg)     ::DebugStr(msg)
120 #else
121 #define PLUGINDEBUGSTR(msg) {}
122 #endif
123
124 #if defined(XP_MACOSX) && defined(__POWERPC__) && (!defined(TARGET_RT_MAC_CFM))
125
126 // glue for mapping outgoing Macho function pointers to TVectors
127 struct TFPtoTVGlue {
128     void* glue[2];
129 };
130
131 static struct {
132     TFPtoTVGlue     newp;
133     TFPtoTVGlue     destroy;
134     TFPtoTVGlue     setwindow;
135     TFPtoTVGlue     newstream;
136     TFPtoTVGlue     destroystream;
137     TFPtoTVGlue     asfile;
138     TFPtoTVGlue     writeready;
139     TFPtoTVGlue     write;
140     TFPtoTVGlue     print;
141     TFPtoTVGlue     event;
142     TFPtoTVGlue     urlnotify;
143     TFPtoTVGlue     getvalue;
144     TFPtoTVGlue     setvalue;
145
146     TFPtoTVGlue     shutdown;
147 } gPluginFuncsGlueTable;
148
149 static inline void* SetupFPtoTVGlue(TFPtoTVGlue* functionGlue, void* fp)
150 {
151     functionGlue->glue[0] = fp;
152     functionGlue->glue[1] = 0;
153     return functionGlue;
154 }
155
156 #define PLUGIN_TO_HOST_GLUE(name, fp) (SetupFPtoTVGlue(&gPluginFuncsGlueTable.name, (void*)fp))
157
158 // glue for mapping netscape TVectors to Macho function pointers
159 struct TTVtoFPGlue {
160     uint32_t glue[6];
161 };
162
163 static struct {
164     TTVtoFPGlue             geturl;
165     TTVtoFPGlue             posturl;
166     TTVtoFPGlue             requestread;
167     TTVtoFPGlue             newstream;
168     TTVtoFPGlue             write;
169     TTVtoFPGlue             destroystream;
170     TTVtoFPGlue             status;
171     TTVtoFPGlue             uagent;
172     TTVtoFPGlue             memalloc;
173     TTVtoFPGlue             memfree;
174     TTVtoFPGlue             memflush;
175     TTVtoFPGlue             reloadplugins;
176     TTVtoFPGlue             getJavaEnv;
177     TTVtoFPGlue             getJavaPeer;
178     TTVtoFPGlue             geturlnotify;
179     TTVtoFPGlue             posturlnotify;
180     TTVtoFPGlue             getvalue;
181     TTVtoFPGlue             setvalue;
182     TTVtoFPGlue             invalidaterect;
183     TTVtoFPGlue             invalidateregion;
184     TTVtoFPGlue             forceredraw;
185     // NPRuntime support
186     TTVtoFPGlue             getstringidentifier;
187     TTVtoFPGlue             getstringidentifiers;
188     TTVtoFPGlue             getintidentifier;
189     TTVtoFPGlue             identifierisstring;
190     TTVtoFPGlue             utf8fromidentifier;
191     TTVtoFPGlue             intfromidentifier;
192     TTVtoFPGlue             createobject;
193     TTVtoFPGlue             retainobject;
194     TTVtoFPGlue             releaseobject;
195     TTVtoFPGlue             invoke;
196     TTVtoFPGlue             invokeDefault;
197     TTVtoFPGlue             evaluate;
198     TTVtoFPGlue             getproperty;
199     TTVtoFPGlue             setproperty;
200     TTVtoFPGlue             removeproperty;
201     TTVtoFPGlue             hasproperty;
202     TTVtoFPGlue             hasmethod;
203     TTVtoFPGlue             releasevariantvalue;
204     TTVtoFPGlue             setexception;
205 } gNetscapeFuncsGlueTable;
206
207 static void* SetupTVtoFPGlue(TTVtoFPGlue* functionGlue, void* tvp)
208 {
209     static const TTVtoFPGlue glueTemplate = { 0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420 };
210
211     memcpy(functionGlue, &glueTemplate, sizeof(TTVtoFPGlue));
212     functionGlue->glue[0] |= ((UInt32)tvp >> 16);
213     functionGlue->glue[1] |= ((UInt32)tvp & 0xFFFF);
214     ::MakeDataExecutable(functionGlue, sizeof(TTVtoFPGlue));
215     return functionGlue;
216 }
217
218 #define HOST_TO_PLUGIN_GLUE(name, fp) (SetupTVtoFPGlue(&gNetscapeFuncsGlueTable.name, (void*)fp))
219
220 #else
221
222 #define PLUGIN_TO_HOST_GLUE(name, fp) (fp)
223 #define HOST_TO_PLUGIN_GLUE(name, fp) (fp)
224
225 #endif /* XP_MACOSX */
226
227 #pragma mark -
228
229 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
230 //
231 // Globals
232 //
233 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
234
235 #if !TARGET_API_MAC_CARBON
236 QDGlobals*      gQDPtr;             // Pointer to Netscape’s QuickDraw globals
237 #endif
238 short           gResFile;           // Refnum of the plugin’s resource file
239 NPNetscapeFuncs    gNetscapeFuncs;      // Function table for procs in Netscape called by plugin
240
241 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
242 //
243 // Wrapper functions for all calls from the plugin to Netscape.
244 // These functions let the plugin developer just call the APIs
245 // as documented and defined in npapi.h, without needing to know
246 // about the function table and call macros in npupp.h.
247 //
248 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
249
250 void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
251 {
252     *plugin_major = NP_VERSION_MAJOR;
253     *plugin_minor = NP_VERSION_MINOR;
254     *netscape_major = gNetscapeFuncs.version >> 8;      // Major version is in high byte
255     *netscape_minor = gNetscapeFuncs.version & 0xFF;    // Minor version is in low byte
256 }
257
258 NPError NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
259 {
260     int navMinorVers = gNetscapeFuncs.version & 0xFF;
261     NPError err;
262
263     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
264     {
265         err = CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
266     }
267     else
268     {
269         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
270     }
271     return err;
272 }
273
274 NPError NPN_GetURL(NPP instance, const char* url, const char* window)
275 {
276     return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
277 }
278
279 NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32_t len, const char* buf, NPBool file, void* notifyData)
280 {
281     int navMinorVers = gNetscapeFuncs.version & 0xFF;
282     NPError err;
283
284     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
285     {
286         err = CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify, instance, url,
287                                                         window, len, buf, file, notifyData);
288     }
289     else
290     {
291         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
292     }
293     return err;
294 }
295
296 NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32_t len, const char* buf, NPBool file)
297 {
298     return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance, url, window, len, buf, file);
299 }
300
301 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
302 {
303     return CallNPN_RequestReadProc(gNetscapeFuncs.requestread, stream, rangeList);
304 }
305
306 NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* window, NPStream** stream)
307 {
308     int navMinorVers = gNetscapeFuncs.version & 0xFF;
309     NPError err;
310
311     if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
312     {
313         err = CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance, type, window, stream);
314     }
315     else
316     {
317         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
318     }
319     return err;
320 }
321
322 int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
323 {
324     int navMinorVers = gNetscapeFuncs.version & 0xFF;
325     NPError err;
326
327     if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
328     {
329         err = CallNPN_WriteProc(gNetscapeFuncs.write, instance, stream, len, buffer);
330     }
331     else
332     {
333         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
334     }
335     return err;
336 }
337
338 NPError    NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
339 {
340     int navMinorVers = gNetscapeFuncs.version & 0xFF;
341     NPError err;
342
343     if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
344     {
345         err = CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream, instance, stream, reason);
346     }
347     else
348     {
349         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
350     }
351     return err;
352 }
353
354 void NPN_Status(NPP instance, const char* message)
355 {
356     CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
357 }
358
359 const char* NPN_UserAgent(NPP instance)
360 {
361     return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
362 }
363
364 void* NPN_MemAlloc(uint32_t size)
365 {
366     return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
367 }
368
369 void NPN_MemFree(void* ptr)
370 {
371     CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
372 }
373
374 uint32_t NPN_MemFlush(uint32_t size)
375 {
376     return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
377 }
378
379 void NPN_ReloadPlugins(NPBool reloadPages)
380 {
381     CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
382 }
383
384 #ifdef OJI
385 JRIEnv* NPN_GetJavaEnv(void)
386 {
387     return CallNPN_GetJavaEnvProc( gNetscapeFuncs.getJavaEnv );
388 }
389
390 jobject  NPN_GetJavaPeer(NPP instance)
391 {
392     return CallNPN_GetJavaPeerProc( gNetscapeFuncs.getJavaPeer, instance );
393 }
394 #endif
395
396 NPError NPN_GetValue(NPP instance, NPNVariable variable, void *value)
397 {
398     return CallNPN_GetValueProc( gNetscapeFuncs.getvalue, instance, variable, value);
399 }
400
401 NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
402 {
403     return CallNPN_SetValueProc( gNetscapeFuncs.setvalue, instance, variable, value);
404 }
405
406 void NPN_InvalidateRect(NPP instance, NPRect *rect)
407 {
408     CallNPN_InvalidateRectProc( gNetscapeFuncs.invalidaterect, instance, rect);
409 }
410
411 void NPN_InvalidateRegion(NPP instance, NPRegion region)
412 {
413     CallNPN_InvalidateRegionProc( gNetscapeFuncs.invalidateregion, instance, region);
414 }
415
416 void NPN_ForceRedraw(NPP instance)
417 {
418     CallNPN_ForceRedrawProc( gNetscapeFuncs.forceredraw, instance);
419 }
420
421 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
422 {
423     int navMinorVers = gNetscapeFuncs.version & 0xFF;
424     if( navMinorVers >= 14 )
425     {
426         return CallNPN_GetStringIdentifierProc( gNetscapeFuncs.getstringidentifier, name);
427     }
428     return NULL;
429 }
430
431 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
432 {
433     int navMinorVers = gNetscapeFuncs.version & 0xFF;
434     if( navMinorVers >= 14 )
435     {
436         CallNPN_GetStringIdentifiersProc( gNetscapeFuncs.getstringidentifiers, names, nameCount, identifiers);
437     }
438 }
439
440 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
441 {
442     int navMinorVers = gNetscapeFuncs.version & 0xFF;
443     if( navMinorVers >= 14 )
444     {
445         return CallNPN_GetIntIdentifierProc( gNetscapeFuncs.getintidentifier, intid);
446     }
447     return NULL;
448 }
449
450 bool NPN_IdentifierIsString(NPIdentifier identifier)
451 {
452     int navMinorVers = gNetscapeFuncs.version & 0xFF;
453     if( navMinorVers >= 14 )
454     {
455         return CallNPN_IdentifierIsStringProc( gNetscapeFuncs.identifierisstring, identifier);
456     }
457     return false;
458 }
459
460 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
461 {
462     int navMinorVers = gNetscapeFuncs.version & 0xFF;
463     if( navMinorVers >= 14 )
464     {
465         return CallNPN_UTF8FromIdentifierProc( gNetscapeFuncs.utf8fromidentifier, identifier);
466     }
467     return NULL;
468 }
469
470 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
471 {
472     int navMinorVers = gNetscapeFuncs.version & 0xFF;
473     if( navMinorVers >= 14 )
474     {
475         return CallNPN_IntFromIdentifierProc( gNetscapeFuncs.intfromidentifier, identifier);
476     }
477     return 0;
478 }
479
480 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
481 {
482     int navMinorVers = gNetscapeFuncs.version & 0xFF;
483     if( navMinorVers >= 14 )
484     {
485         return CallNPN_CreateObjectProc( gNetscapeFuncs.createobject, instance, aClass);
486     }
487     return NULL;
488 }
489
490 NPObject *NPN_RetainObject(NPObject *npobj)
491 {
492     int navMinorVers = gNetscapeFuncs.version & 0xFF;
493     if( navMinorVers >= 14 )
494     {
495         return CallNPN_RetainObjectProc( gNetscapeFuncs.retainobject, npobj);
496     }
497     return NULL;
498 }
499
500 void NPN_ReleaseObject(NPObject *npobj)
501 {
502     int navMinorVers = gNetscapeFuncs.version & 0xFF;
503     if( navMinorVers >= 14 )
504     {
505         CallNPN_ReleaseObjectProc( gNetscapeFuncs.releaseobject, npobj);
506     }
507 }
508
509 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
510 {
511     int navMinorVers = gNetscapeFuncs.version & 0xFF;
512     if( navMinorVers >= 14 )
513     {
514         return CallNPN_InvokeProc( gNetscapeFuncs.invoke, instance, npobj, methodName, args, argCount, result);
515     }
516     return false;
517 }
518
519 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
520 {
521     int navMinorVers = gNetscapeFuncs.version & 0xFF;
522     if( navMinorVers >= 14 )
523     {
524         return CallNPN_InvokeDefaultProc( gNetscapeFuncs.invokeDefault, instance, npobj, args, argCount, result);
525     }
526     return false;
527 }
528
529 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
530 {
531     int navMinorVers = gNetscapeFuncs.version & 0xFF;
532     if( navMinorVers >= 14 )
533     {
534         return CallNPN_EvaluateProc( gNetscapeFuncs.evaluate, instance, npobj, script, result);
535     }
536     return false;
537 }
538
539 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
540 {
541     int navMinorVers = gNetscapeFuncs.version & 0xFF;
542     if( navMinorVers >= 14 )
543     {
544         return CallNPN_GetPropertyProc( gNetscapeFuncs.getproperty, instance, npobj, propertyName, result);
545     }
546     return false;
547 }
548
549 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
550 {
551     int navMinorVers = gNetscapeFuncs.version & 0xFF;
552     if( navMinorVers >= 14 )
553     {
554         return CallNPN_SetPropertyProc( gNetscapeFuncs.setproperty, instance, npobj, propertyName, value);
555     }
556     return false;
557 }
558
559 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
560 {
561     int navMinorVers = gNetscapeFuncs.version & 0xFF;
562     if( navMinorVers >= 14 )
563     {
564         return CallNPN_RemovePropertyProc( gNetscapeFuncs.removeproperty, instance, npobj, propertyName);
565     }
566     return false;
567 }
568
569 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
570 {
571     int navMinorVers = gNetscapeFuncs.version & 0xFF;
572     if( navMinorVers >= 14 )
573     {
574         return CallNPN_HasPropertyProc( gNetscapeFuncs.hasproperty, instance, npobj, propertyName);
575     }
576     return false;
577 }
578
579 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
580 {
581     int navMinorVers = gNetscapeFuncs.version & 0xFF;
582     if( navMinorVers >= 14 )
583     {
584         return CallNPN_HasMethodProc( gNetscapeFuncs.hasmethod, instance, npobj, methodName);
585     }
586     return false;
587 }
588
589 void NPN_ReleaseVariantValue(NPVariant *variant)
590 {
591     int navMinorVers = gNetscapeFuncs.version & 0xFF;
592     if( navMinorVers >= 14 )
593     {
594         CallNPN_ReleaseVariantValueProc( gNetscapeFuncs.releasevariantvalue, variant);
595     }
596 }
597
598 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
599 {
600     int navMinorVers = gNetscapeFuncs.version & 0xFF;
601     if( navMinorVers >= 14 )
602     {
603         CallNPN_SetExceptionProc( gNetscapeFuncs.setexception, npobj, message);
604     }
605 }
606
607 #pragma mark -
608
609 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
610 //
611 // Wrapper functions for all calls from Netscape to the plugin.
612 // These functions let the plugin developer just create the APIs
613 // as documented and defined in npapi.h, without needing to
614 // install those functions in the function table or worry about
615 // setting up globals for 68K plugins.
616 //
617 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
618
619 NPError     Private_Initialize(void);
620 void        Private_Shutdown(void);
621 NPError     Private_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved);
622 NPError     Private_Destroy(NPP instance, NPSavedData** save);
623 NPError     Private_SetWindow(NPP instance, NPWindow* window);
624 NPError     Private_GetValue( NPP instance, NPPVariable variable, void *value );
625 NPError     Private_SetValue( NPP instance, NPPVariable variable, void *value );
626 NPError     Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype);
627 NPError     Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
628 int32_t     Private_WriteReady(NPP instance, NPStream* stream);
629 int32_t     Private_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer);
630 void        Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
631 void        Private_Print(NPP instance, NPPrint* platformPrint);
632 int16_t     Private_HandleEvent(NPP instance, void* event);
633 void        Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
634 jobject     Private_GetJavaClass(void);
635
636
637 NPError Private_Initialize(void)
638 {
639     NPError err;
640     EnterCodeResource();
641     PLUGINDEBUGSTR("\pInitialize;g;");
642     err = NPP_Initialize();
643     ExitCodeResource();
644     return err;
645 }
646
647 void Private_Shutdown(void)
648 {
649     EnterCodeResource();
650     PLUGINDEBUGSTR("\pShutdown;g;");
651     NPP_Shutdown();
652
653 #ifdef __MWERKS__
654     __destroy_global_chain();
655 #endif
656
657     ExitCodeResource();
658 }
659
660 NPError    Private_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
661 {
662     EnterCodeResource();
663     NPError ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
664     PLUGINDEBUGSTR("\pNew;g;");
665     ExitCodeResource();
666     return ret;
667 }
668
669 NPError Private_Destroy(NPP instance, NPSavedData** save)
670 {
671     NPError err;
672     EnterCodeResource();
673     PLUGINDEBUGSTR("\pDestroy;g;");
674     err = NPP_Destroy(instance, save);
675     ExitCodeResource();
676     return err;
677 }
678
679 NPError Private_SetWindow(NPP instance, NPWindow* window)
680 {
681     NPError err;
682     EnterCodeResource();
683     PLUGINDEBUGSTR("\pSetWindow;g;");
684     err = NPP_SetWindow(instance, window);
685     ExitCodeResource();
686     return err;
687 }
688
689 NPError Private_GetValue( NPP instance, NPPVariable variable, void *value )
690 {
691     NPError err;
692     EnterCodeResource();
693     PLUGINDEBUGSTR("\pGetValue;g;");
694     err = NPP_GetValue(instance, variable, value);
695     ExitCodeResource();
696     return err;
697 }
698
699 NPError Private_SetValue( NPP instance, NPNVariable variable, void *value )
700 {
701     NPError err;
702     EnterCodeResource();
703     PLUGINDEBUGSTR("\pSetValue;g;");
704     err = NPP_SetValue(instance, variable, value);
705     ExitCodeResource();
706     return err;
707 }
708
709 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
710 {
711     NPError err;
712     EnterCodeResource();
713     PLUGINDEBUGSTR("\pNewStream;g;");
714     err = NPP_NewStream(instance, type, stream, seekable, stype);
715     ExitCodeResource();
716     return err;
717 }
718
719 int32_t Private_WriteReady(NPP instance, NPStream* stream)
720 {
721     int32_t result;
722     EnterCodeResource();
723     PLUGINDEBUGSTR("\pWriteReady;g;");
724     result = NPP_WriteReady(instance, stream);
725     ExitCodeResource();
726     return result;
727 }
728
729 int32_t Private_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer)
730 {
731     int32_t result;
732     EnterCodeResource();
733     PLUGINDEBUGSTR("\pWrite;g;");
734     result = NPP_Write(instance, stream, offset, len, buffer);
735     ExitCodeResource();
736     return result;
737 }
738
739 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
740 {
741     EnterCodeResource();
742     PLUGINDEBUGSTR("\pStreamAsFile;g;");
743     NPP_StreamAsFile(instance, stream, fname);
744     ExitCodeResource();
745 }
746
747 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
748 {
749     NPError err;
750     EnterCodeResource();
751     PLUGINDEBUGSTR("\pDestroyStream;g;");
752     err = NPP_DestroyStream(instance, stream, reason);
753     ExitCodeResource();
754     return err;
755 }
756
757 int16_t Private_HandleEvent(NPP instance, void* event)
758 {
759     int16_t result;
760     EnterCodeResource();
761     PLUGINDEBUGSTR("\pHandleEvent;g;");
762     result = NPP_HandleEvent(instance, event);
763     ExitCodeResource();
764     return result;
765 }
766
767 void Private_Print(NPP instance, NPPrint* platformPrint)
768 {
769     EnterCodeResource();
770     PLUGINDEBUGSTR("\pPrint;g;");
771     NPP_Print(instance, platformPrint);
772     ExitCodeResource();
773 }
774
775 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
776 {
777     EnterCodeResource();
778     PLUGINDEBUGSTR("\pURLNotify;g;");
779     NPP_URLNotify(instance, url, reason, notifyData);
780     ExitCodeResource();
781 }
782
783 #ifdef OJI
784 jobject Private_GetJavaClass(void)
785 {
786     EnterCodeResource();
787     PLUGINDEBUGSTR("\pGetJavaClass;g;");
788
789     jobject clazz = NPP_GetJavaClass();
790     ExitCodeResource();
791     if (clazz)
792     {
793         JRIEnv* env = NPN_GetJavaEnv();
794         return (jobject)JRI_NewGlobalRef(env, clazz);
795     }
796     return NULL;
797 }
798 #endif
799
800 void SetUpQD(void);
801 void SetUpQD(void)
802 {
803 #if !TARGET_API_MAC_CARBON
804     ProcessSerialNumber PSN;
805     FSSpec              myFSSpec;
806     Str63               name;
807     ProcessInfoRec      infoRec;
808     OSErr               result = noErr;
809     CFragConnectionID   connID;
810     Str255              errName;
811 #endif
812
813     //
814     // Memorize the plugin’s resource file
815     // refnum for later use.
816     //
817     gResFile = CurResFile();
818
819 #if !TARGET_API_MAC_CARBON
820     //
821     // Ask the system if CFM is available.
822     //
823     long response;
824     OSErr err = Gestalt(gestaltCFMAttr, &response);
825     Boolean hasCFM = BitTst(&response, 31-gestaltCFMPresent);
826
827     ProcessInfoRec infoRec;
828     if (hasCFM)
829     {
830         //
831         // GetProcessInformation takes a process serial number and
832         // will give us back the name and FSSpec of the application.
833         // See the Process Manager in IM.
834         //
835         Str63 name;
836         FSSpec myFSSpec;
837         infoRec.processInfoLength = sizeof(ProcessInfoRec);
838         infoRec.processName = name;
839         infoRec.processAppSpec = &myFSSpec;
840
841         ProcessSerialNumber PSN;
842         PSN.highLongOfPSN = 0;
843         PSN.lowLongOfPSN = kCurrentProcess;
844
845         result = GetProcessInformation(&PSN, &infoRec);
846         if (result != noErr)
847             PLUGINDEBUGSTR("\pFailed in GetProcessInformation");
848     }
849     else
850         //
851         // If no CFM installed, assume it must be a 68K app.
852         //
853         result = -1;
854
855     CFragConnectionID connID;
856     if (result == noErr)
857     {
858         //
859         // Now that we know the app name and FSSpec, we can call GetDiskFragment
860         // to get a connID to use in a subsequent call to FindSymbol (it will also
861         // return the address of “main” in app, which we ignore).  If GetDiskFragment
862         // returns an error, we assume the app must be 68K.
863         //
864         Ptr mainAddr;
865         Str255 errName;
866         result =  GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
867                                   kLoadCFrag, &connID, (Ptr*)&mainAddr, errName);
868     }
869
870     if (result == noErr)
871     {
872         //
873         // The app is a PPC code fragment, so call FindSymbol
874         // to get the exported “qd” symbol so we can access its
875         // QuickDraw globals.
876         //
877         CFragSymbolClass symClass;
878         result = FindSymbol(connID, "\pqd", (Ptr*)&gQDPtr, &symClass);
879         if (result != noErr) {  // this fails if we are in NS 6
880             gQDPtr = &qd;       // so we default to the standard QD globals
881         }
882     }
883     else
884     {
885         //
886         // The app is 68K, so use its A5 to compute the address
887         // of its QuickDraw globals.
888         //
889         gQDPtr = (QDGlobals*)(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
890     }
891 #endif
892 }
893
894
895 #ifdef __GNUC__
896 // gcc requires that main have an 'int' return type
897 int main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
898 #else
899 NPError main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
900 #endif
901
902 #if !TARGET_API_MAC_CARBON
903 #pragma export on
904
905 #if TARGET_RT_MAC_CFM
906
907 RoutineDescriptor mainRD = BUILD_ROUTINE_DESCRIPTOR(uppNPP_MainEntryProcInfo, main);
908
909 #endif
910
911 #pragma export off
912 #endif /* !TARGET_API_MAC_CARBON */
913
914 #ifdef __GNUC__
915 DEFINE_API_C(int) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
916 #else
917 DEFINE_API_C(NPError) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
918 #endif
919 {
920     EnterCodeResource();
921     PLUGINDEBUGSTR("\pmain");
922
923 #ifdef __MWERKS__
924     __InitCode__();
925 #endif
926
927     NPError err = NPERR_NO_ERROR;
928
929     //
930     // Ensure that everything Netscape passed us is valid!
931     //
932     if ((nsTable == NULL) || (pluginFuncs == NULL) || (unloadUpp == NULL))
933         err = NPERR_INVALID_FUNCTABLE_ERROR;
934
935     //
936     // Check the “major” version passed in Netscape’s function table.
937     // We won’t load if the major version is newer than what we expect.
938     // Also check that the function tables passed in are big enough for
939     // all the functions we need (they could be bigger, if Netscape added
940     // new APIs, but that’s OK with us -- we’ll just ignore them).
941     //
942     if (err == NPERR_NO_ERROR)
943     {
944         if ((nsTable->version >> 8) > NP_VERSION_MAJOR)     // Major version is in high byte
945             err = NPERR_INCOMPATIBLE_VERSION_ERROR;
946     }
947
948     if (err == NPERR_NO_ERROR)
949     {
950         //
951         // Copy all the fields of Netscape’s function table into our
952         // copy so we can call back into Netscape later.  Note that
953         // we need to copy the fields one by one, rather than assigning
954         // the whole structure, because the Netscape function table
955         // could actually be bigger than what we expect.
956         //
957
958         int navMinorVers = nsTable->version & 0xFF;
959
960         gNetscapeFuncs.version          = nsTable->version;
961         gNetscapeFuncs.size             = nsTable->size;
962         gNetscapeFuncs.posturl          = (NPN_PostURLUPP)HOST_TO_PLUGIN_GLUE(posturl, nsTable->posturl);
963         gNetscapeFuncs.geturl           = (NPN_GetURLUPP)HOST_TO_PLUGIN_GLUE(geturl, nsTable->geturl);
964         gNetscapeFuncs.requestread      = (NPN_RequestReadUPP)HOST_TO_PLUGIN_GLUE(requestread, nsTable->requestread);
965         gNetscapeFuncs.newstream        = (NPN_NewStreamUPP)HOST_TO_PLUGIN_GLUE(newstream, nsTable->newstream);
966         gNetscapeFuncs.write            = (NPN_WriteUPP)HOST_TO_PLUGIN_GLUE(write, nsTable->write);
967         gNetscapeFuncs.destroystream    = (NPN_DestroyStreamUPP)HOST_TO_PLUGIN_GLUE(destroystream, nsTable->destroystream);
968         gNetscapeFuncs.status           = (NPN_StatusUPP)HOST_TO_PLUGIN_GLUE(status, nsTable->status);
969         gNetscapeFuncs.uagent           = (NPN_UserAgentUPP)HOST_TO_PLUGIN_GLUE(uagent, nsTable->uagent);
970         gNetscapeFuncs.memalloc         = (NPN_MemAllocUPP)HOST_TO_PLUGIN_GLUE(memalloc, nsTable->memalloc);
971         gNetscapeFuncs.memfree          = (NPN_MemFreeUPP)HOST_TO_PLUGIN_GLUE(memfree, nsTable->memfree);
972         gNetscapeFuncs.memflush         = (NPN_MemFlushUPP)HOST_TO_PLUGIN_GLUE(memflush, nsTable->memflush);
973         gNetscapeFuncs.reloadplugins    = (NPN_ReloadPluginsUPP)HOST_TO_PLUGIN_GLUE(reloadplugins, nsTable->reloadplugins);
974         if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
975         {
976             gNetscapeFuncs.getJavaEnv   = (NPN_GetJavaEnvUPP)HOST_TO_PLUGIN_GLUE(getJavaEnv, nsTable->getJavaEnv);
977             gNetscapeFuncs.getJavaPeer  = (NPN_GetJavaPeerUPP)HOST_TO_PLUGIN_GLUE(getJavaPeer, nsTable->getJavaPeer);
978         }
979         if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
980         {
981             gNetscapeFuncs.geturlnotify = (NPN_GetURLNotifyUPP)HOST_TO_PLUGIN_GLUE(geturlnotify, nsTable->geturlnotify);
982             gNetscapeFuncs.posturlnotify    = (NPN_PostURLNotifyUPP)HOST_TO_PLUGIN_GLUE(posturlnotify, nsTable->posturlnotify);
983         }
984         gNetscapeFuncs.getvalue         = (NPN_GetValueUPP)HOST_TO_PLUGIN_GLUE(getvalue, nsTable->getvalue);
985         gNetscapeFuncs.setvalue         = (NPN_SetValueUPP)HOST_TO_PLUGIN_GLUE(setvalue, nsTable->setvalue);
986         gNetscapeFuncs.invalidaterect   = (NPN_InvalidateRectUPP)HOST_TO_PLUGIN_GLUE(invalidaterect, nsTable->invalidaterect);
987         gNetscapeFuncs.invalidateregion = (NPN_InvalidateRegionUPP)HOST_TO_PLUGIN_GLUE(invalidateregion, nsTable->invalidateregion);
988         gNetscapeFuncs.forceredraw      = (NPN_ForceRedrawUPP)HOST_TO_PLUGIN_GLUE(forceredraw, nsTable->forceredraw);
989         if( navMinorVers >= 14 )
990         {
991             // NPRuntime support
992             gNetscapeFuncs.getstringidentifier  = (NPN_GetStringIdentifierUPP)HOST_TO_PLUGIN_GLUE(getstringidentifier, nsTable->getstringidentifier);
993             gNetscapeFuncs.getstringidentifiers = (NPN_GetStringIdentifiersUPP)HOST_TO_PLUGIN_GLUE(getstringidentifiers, nsTable->getstringidentifiers);
994             gNetscapeFuncs.getintidentifier     = (NPN_GetIntIdentifierUPP)HOST_TO_PLUGIN_GLUE(getintidentifier, nsTable->getintidentifier);
995             gNetscapeFuncs.identifierisstring   = (NPN_IdentifierIsStringUPP)HOST_TO_PLUGIN_GLUE(identifierisstring, nsTable->identifierisstring);
996             gNetscapeFuncs.utf8fromidentifier   = (NPN_UTF8FromIdentifierUPP)HOST_TO_PLUGIN_GLUE(utf8fromidentifier, nsTable->utf8fromidentifier);
997             gNetscapeFuncs.intfromidentifier    = (NPN_IntFromIdentifierUPP)HOST_TO_PLUGIN_GLUE(intfromidentifier, nsTable->intfromidentifier);
998             gNetscapeFuncs.createobject         = (NPN_CreateObjectUPP)HOST_TO_PLUGIN_GLUE(createobject, nsTable->createobject);
999             gNetscapeFuncs.retainobject         = (NPN_RetainObjectUPP)HOST_TO_PLUGIN_GLUE(retainobject, nsTable->retainobject);
1000             gNetscapeFuncs.releaseobject        = (NPN_ReleaseObjectUPP)HOST_TO_PLUGIN_GLUE(releaseobject, nsTable->releaseobject);
1001             gNetscapeFuncs.invoke               = (NPN_InvokeUPP)HOST_TO_PLUGIN_GLUE(invoke, nsTable->invoke);
1002             gNetscapeFuncs.invokeDefault        = (NPN_InvokeDefaultUPP)HOST_TO_PLUGIN_GLUE(invokeDefault, nsTable->invokeDefault);
1003             gNetscapeFuncs.evaluate             = (NPN_EvaluateUPP)HOST_TO_PLUGIN_GLUE(evaluate, nsTable->evaluate);
1004             gNetscapeFuncs.getproperty          = (NPN_GetPropertyUPP)HOST_TO_PLUGIN_GLUE(getproperty, nsTable->getproperty);
1005             gNetscapeFuncs.setproperty          = (NPN_SetPropertyUPP)HOST_TO_PLUGIN_GLUE(setproperty, nsTable->setproperty);
1006             gNetscapeFuncs.removeproperty       = (NPN_RemovePropertyUPP)HOST_TO_PLUGIN_GLUE(removeproperty, nsTable->removeproperty);
1007             gNetscapeFuncs.hasproperty          = (NPN_HasPropertyUPP)HOST_TO_PLUGIN_GLUE(hasproperty, nsTable->hasproperty);
1008             gNetscapeFuncs.hasmethod            = (NPN_HasMethodUPP)HOST_TO_PLUGIN_GLUE(hasmethod, nsTable->hasmethod);
1009             gNetscapeFuncs.releasevariantvalue  = (NPN_ReleaseVariantValueUPP)HOST_TO_PLUGIN_GLUE(releasevariantvalue, nsTable->releasevariantvalue);
1010             gNetscapeFuncs.setexception         = (NPN_SetExceptionUPP)HOST_TO_PLUGIN_GLUE(setexception, nsTable->setexception);
1011         }
1012
1013         //
1014         // Set up the plugin function table that Netscape will use to
1015         // call us.  Netscape needs to know about our version and size
1016         // and have a UniversalProcPointer for every function we implement.
1017         //
1018         pluginFuncs->version        = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
1019         pluginFuncs->size           = sizeof(NPPluginFuncs);
1020 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1021         pluginFuncs->newp           = NewNPP_NewProc(PLUGIN_TO_HOST_GLUE(newp, Private_New));
1022         pluginFuncs->destroy        = NewNPP_DestroyProc(PLUGIN_TO_HOST_GLUE(destroy, Private_Destroy));
1023         pluginFuncs->setwindow      = NewNPP_SetWindowProc(PLUGIN_TO_HOST_GLUE(setwindow, Private_SetWindow));
1024         pluginFuncs->newstream      = NewNPP_NewStreamProc(PLUGIN_TO_HOST_GLUE(newstream, Private_NewStream));
1025         pluginFuncs->destroystream  = NewNPP_DestroyStreamProc(PLUGIN_TO_HOST_GLUE(destroystream, Private_DestroyStream));
1026         pluginFuncs->asfile         = NewNPP_StreamAsFileProc(PLUGIN_TO_HOST_GLUE(asfile, Private_StreamAsFile));
1027         pluginFuncs->writeready     = NewNPP_WriteReadyProc(PLUGIN_TO_HOST_GLUE(writeready, Private_WriteReady));
1028         pluginFuncs->write          = NewNPP_WriteProc(PLUGIN_TO_HOST_GLUE(write, Private_Write));
1029         pluginFuncs->print          = NewNPP_PrintProc(PLUGIN_TO_HOST_GLUE(print, Private_Print));
1030         pluginFuncs->event          = NewNPP_HandleEventProc(PLUGIN_TO_HOST_GLUE(event, Private_HandleEvent));
1031         pluginFuncs->getvalue       = NewNPP_GetValueProc(PLUGIN_TO_HOST_GLUE(getvalue, Private_GetValue));
1032 #else
1033         pluginFuncs->newp           = (NPP_NewProcPtr)(PLUGIN_TO_HOST_GLUE(newp, Private_New));
1034         pluginFuncs->destroy        = (NPP_DestroyProcPtr)(PLUGIN_TO_HOST_GLUE(destroy, Private_Destroy));
1035         pluginFuncs->setwindow      = (NPP_SetWindowProcPtr)(PLUGIN_TO_HOST_GLUE(setwindow, Private_SetWindow));
1036         pluginFuncs->newstream      = (NPP_NewStreamProcPtr)(PLUGIN_TO_HOST_GLUE(newstream, Private_NewStream));
1037         pluginFuncs->destroystream  = (NPP_DestroyStreamProcPtr)(PLUGIN_TO_HOST_GLUE(destroystream, Private_DestroyStream));
1038         pluginFuncs->asfile         = (NPP_StreamAsFileProcPtr)(PLUGIN_TO_HOST_GLUE(asfile, Private_StreamAsFile));
1039         pluginFuncs->writeready     = (NPP_WriteReadyProcPtr)(PLUGIN_TO_HOST_GLUE(writeready, Private_WriteReady));
1040         pluginFuncs->write          = (NPP_WriteProcPtr)(PLUGIN_TO_HOST_GLUE(write, Private_Write));
1041         pluginFuncs->print          = (NPP_PrintProcPtr)(PLUGIN_TO_HOST_GLUE(print, Private_Print));
1042         pluginFuncs->event          = (NPP_HandleEventProcPtr)(PLUGIN_TO_HOST_GLUE(event, Private_HandleEvent));
1043         pluginFuncs->getvalue       = (NPP_GetValueProcPtr)(PLUGIN_TO_HOST_GLUE(getvalue, Private_GetValue));
1044 #endif
1045         if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
1046         {
1047 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1048             pluginFuncs->urlnotify = NewNPP_URLNotifyProc(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));
1049 #else
1050             pluginFuncs->urlnotify = (NPP_URLNotifyProcPtr)(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));
1051 #endif
1052         }
1053 #ifdef OJI
1054         if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
1055         {
1056             pluginFuncs->javaClass  = (JRIGlobalRef) Private_GetJavaClass();
1057         }
1058 #else
1059         pluginFuncs->javaClass = NULL;
1060 #endif
1061 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1062         *unloadUpp = NewNPP_ShutdownProc(PLUGIN_TO_HOST_GLUE(shutdown, Private_Shutdown));
1063 #else
1064         *unloadUpp = (NPP_ShutdownProcPtr)(PLUGIN_TO_HOST_GLUE(shutdown, Private_Shutdown));
1065 #endif
1066         SetUpQD();
1067         err = Private_Initialize();
1068     }
1069
1070     ExitCodeResource();
1071     return err;
1072 }
1073
1074 #ifdef __MACH__
1075
1076 /*
1077 ** netscape plugins functions when building Mach-O binary
1078 */
1079
1080 extern "C" {
1081     NPError NP_Initialize(NPNetscapeFuncs* nsTable);
1082     NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs);
1083     NPError NP_Shutdown(void);
1084 }
1085
1086 /*
1087 ** netscape plugins functions when using Mach-O binary
1088 */
1089
1090 NPError NP_Initialize(NPNetscapeFuncs* nsTable)
1091 {
1092     PLUGINDEBUGSTR("\pNP_Initialize");
1093
1094     /* validate input parameters */
1095
1096     if( NULL == nsTable  )
1097         return NPERR_INVALID_FUNCTABLE_ERROR;
1098
1099     /*
1100      * Check the major version passed in Netscape's function table.
1101      * We won't load if the major version is newer than what we expect.
1102      * Also check that the function tables passed in are big enough for
1103      * all the functions we need (they could be bigger, if Netscape added
1104      * new APIs, but that's OK with us -- we'll just ignore them).
1105      *
1106      */
1107
1108     if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
1109         return NPERR_INCOMPATIBLE_VERSION_ERROR;
1110
1111     if (nsTable->size < sizeof(NPNetscapeFuncs))
1112         return NPERR_INVALID_FUNCTABLE_ERROR;
1113
1114     int navMinorVers = nsTable->version & 0xFF;
1115
1116     /*
1117      * Copy all the fields of Netscape function table into our
1118      * copy so we can call back into Netscape later.  Note that
1119      * we need to copy the fields one by one, rather than assigning
1120      * the whole structure, because the Netscape function table
1121      * could actually be bigger than what we expect.
1122      */
1123     gNetscapeFuncs.version       = nsTable->version;
1124     gNetscapeFuncs.size          = nsTable->size;
1125     gNetscapeFuncs.posturl       = nsTable->posturl;
1126     gNetscapeFuncs.geturl        = nsTable->geturl;
1127     gNetscapeFuncs.requestread   = nsTable->requestread;
1128     gNetscapeFuncs.newstream     = nsTable->newstream;
1129     gNetscapeFuncs.write         = nsTable->write;
1130     gNetscapeFuncs.destroystream = nsTable->destroystream;
1131     gNetscapeFuncs.status        = nsTable->status;
1132     gNetscapeFuncs.uagent        = nsTable->uagent;
1133     gNetscapeFuncs.memalloc      = nsTable->memalloc;
1134     gNetscapeFuncs.memfree       = nsTable->memfree;
1135     gNetscapeFuncs.memflush      = nsTable->memflush;
1136     gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
1137     if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
1138     {
1139         gNetscapeFuncs.getJavaEnv   = nsTable->getJavaEnv;
1140         gNetscapeFuncs.getJavaPeer  = nsTable->getJavaPeer;
1141     }
1142     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
1143     {
1144         gNetscapeFuncs.geturlnotify     = nsTable->geturlnotify;
1145         gNetscapeFuncs.posturlnotify    = nsTable->posturlnotify;
1146     }
1147
1148     gNetscapeFuncs.getvalue         = nsTable->getvalue;
1149     gNetscapeFuncs.setvalue         = nsTable->setvalue;
1150     gNetscapeFuncs.invalidaterect   = nsTable->invalidaterect;
1151     gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
1152     gNetscapeFuncs.forceredraw      = nsTable->forceredraw;
1153     if( navMinorVers >= 14 )
1154     {
1155         // NPRuntime support
1156         gNetscapeFuncs.getstringidentifier  = nsTable->getstringidentifier;
1157         gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
1158         gNetscapeFuncs.getintidentifier     = nsTable->getintidentifier;
1159         gNetscapeFuncs.identifierisstring   = nsTable->identifierisstring;
1160         gNetscapeFuncs.utf8fromidentifier   = nsTable->utf8fromidentifier;
1161         gNetscapeFuncs.intfromidentifier    = nsTable->intfromidentifier;
1162         gNetscapeFuncs.createobject         = nsTable->createobject;
1163         gNetscapeFuncs.retainobject         = nsTable->retainobject;
1164         gNetscapeFuncs.releaseobject        = nsTable->releaseobject;
1165         gNetscapeFuncs.invoke               = nsTable->invoke;
1166         gNetscapeFuncs.invokeDefault        = nsTable->invokeDefault;
1167         gNetscapeFuncs.evaluate             = nsTable->evaluate;
1168         gNetscapeFuncs.getproperty          = nsTable->getproperty;
1169         gNetscapeFuncs.setproperty          = nsTable->setproperty;
1170         gNetscapeFuncs.removeproperty       = nsTable->removeproperty;
1171         gNetscapeFuncs.hasproperty          = nsTable->hasproperty;
1172         gNetscapeFuncs.hasmethod            = nsTable->hasmethod;
1173         gNetscapeFuncs.releasevariantvalue  = nsTable->releasevariantvalue;
1174         gNetscapeFuncs.setexception         = nsTable->setexception;
1175     }
1176     return NPP_Initialize();
1177 }
1178
1179 NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
1180 {
1181     int navMinorVers = gNetscapeFuncs.version & 0xFF;
1182
1183     PLUGINDEBUGSTR("\pNP_GetEntryPoints");
1184
1185     if( pluginFuncs == NULL )
1186         return NPERR_INVALID_FUNCTABLE_ERROR;
1187
1188     /*if (pluginFuncs->size < sizeof(NPPluginFuncs))
1189     return NPERR_INVALID_FUNCTABLE_ERROR;*/
1190
1191     /*
1192      * Set up the plugin function table that Netscape will use to
1193      * call us.  Netscape needs to know about our version and size
1194      * and have a UniversalProcPointer for every function we
1195      * implement.
1196      */
1197
1198     pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
1199     pluginFuncs->size       = sizeof(NPPluginFuncs);
1200 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1201     pluginFuncs->newp       = NewNPP_NewProc(Private_New);
1202     pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
1203     pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
1204     pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
1205     pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
1206     pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
1207     pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
1208     pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
1209     pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
1210     pluginFuncs->event      = NewNPP_HandleEventProc(Private_HandleEvent);
1211     pluginFuncs->getvalue   = NewNPP_GetValueProc(Private_GetValue);
1212     pluginFuncs->setvalue   = NewNPP_SetValueProc(Private_SetValue);
1213 #else
1214     pluginFuncs->newp       = (NPP_NewProcPtr)(Private_New);
1215     pluginFuncs->destroy    = (NPP_DestroyProcPtr)(Private_Destroy);
1216     pluginFuncs->setwindow  = (NPP_SetWindowProcPtr)(Private_SetWindow);
1217     pluginFuncs->newstream  = (NPP_NewStreamProcPtr)(Private_NewStream);
1218     pluginFuncs->destroystream = (NPP_DestroyStreamProcPtr)(Private_DestroyStream);
1219     pluginFuncs->asfile     = (NPP_StreamAsFileProcPtr)(Private_StreamAsFile);
1220     pluginFuncs->writeready = (NPP_WriteReadyProcPtr)(Private_WriteReady);
1221     pluginFuncs->write      = (NPP_WriteProcPtr)(Private_Write);
1222     pluginFuncs->print      = (NPP_PrintProcPtr)(Private_Print);
1223     pluginFuncs->event      = (NPP_HandleEventProcPtr)(Private_HandleEvent);
1224     pluginFuncs->getvalue   = (NPP_GetValueProcPtr)(Private_GetValue);
1225     pluginFuncs->setvalue   = (NPP_SetValueProcPtr)(Private_SetValue);
1226 #endif
1227     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
1228     {
1229         pluginFuncs->urlnotify = Private_URLNotify;
1230     }
1231 #ifdef OJI
1232     if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
1233     {
1234         pluginFuncs->javaClass  = (JRIGlobalRef) Private_GetJavaClass();
1235     }
1236 #else
1237     pluginFuncs->javaClass = NULL;
1238 #endif
1239
1240     return NPERR_NO_ERROR;
1241 }
1242
1243 NPError NP_Shutdown(void)
1244 {
1245     PLUGINDEBUGSTR("\pNP_Shutdown");
1246     NPP_Shutdown();
1247     return NPERR_NO_ERROR;
1248 }
1249
1250 #endif