a3ea6f229306cf071913e890a49f7378c97579fb
[vlc.git] / mozilla / support / npmac.cpp
1 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
2 //
3 // npmac.cpp
4 //
5 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
6
7 #include <string.h>
8
9 #include <Processes.h>
10 #include <Gestalt.h>
11 #include <CodeFragments.h>
12 #include <Timer.h>
13 #include <Resources.h>
14 #include <ToolUtils.h>
15
16 #define XP_MAC 1
17
18 //
19 // A4Stuff.h contains the definition of EnterCodeResource and 
20 // EnterCodeResource, used for setting up the code resourceÕs
21 // globals for 68K (analagous to the function SetCurrentA5
22 // defined by the toolbox).
23 //
24 // A4Stuff does not exist as of CW 7. Define them to nothing.
25 //
26
27 #if (defined(__MWERKS__) && (__MWERKS__ >= 0x2400)) || defined(__GNUC__)
28         #define EnterCodeResource()
29         #define ExitCodeResource()
30 #else
31     #include <A4Stuff.h>
32 #endif
33
34 #include "npapi.h"
35
36 //
37 // The Mixed Mode procInfos defined in npupp.h assume Think C-
38 // style calling conventions.  These conventions are used by
39 // Metrowerks with the exception of pointer return types, which
40 // in Metrowerks 68K are returned in A0, instead of the standard
41 // D0. Thus, since NPN_MemAlloc and NPN_UserAgent return pointers,
42 // Mixed Mode will return the values to a 68K plugin in D0, but 
43 // a 68K plugin compiled by Metrowerks will expect the result in
44 // A0.  The following pragma forces Metrowerks to use D0 instead.
45 //
46 #ifdef __MWERKS__
47 #ifndef powerc
48 #pragma pointers_in_D0
49 #endif
50 #endif
51
52 #include "npupp.h"
53
54 #ifdef __MWERKS__
55 #ifndef powerc
56 #pragma pointers_in_A0
57 #endif
58 #endif
59
60 // The following fix for static initializers (which fixes a previous
61 // incompatibility with some parts of PowerPlant, was submitted by 
62 // Jan Ulbrich.
63 #ifdef __MWERKS__
64         #ifdef __cplusplus
65         extern "C" {
66         #endif
67                 #ifndef powerc
68                         extern void     __InitCode__(void);
69                 #else
70                         extern void __sinit(void);
71                         #define __InitCode__ __sinit
72                 #endif
73                 extern void     __destroy_global_chain(void);
74         #ifdef __cplusplus
75         }
76         #endif // __cplusplus
77 #endif // __MWERKS__
78
79 //
80 // Define PLUGIN_TRACE to 1 to have the wrapper functions emit
81 // DebugStr messages whenever they are called.
82 //
83 //#define PLUGIN_TRACE 1
84
85 #if PLUGIN_TRACE
86 #define PLUGINDEBUGSTR(msg)             ::DebugStr(msg)
87 #else
88 #define PLUGINDEBUGSTR
89 #endif
90
91
92 #ifdef XP_MACOSX
93
94 // glue for mapping outgoing Macho function pointers to TVectors
95 struct TFPtoTVGlue{
96     void* glue[2];
97 };
98
99 struct {
100     TFPtoTVGlue     newp;
101     TFPtoTVGlue     destroy;
102     TFPtoTVGlue     setwindow;
103     TFPtoTVGlue     newstream;
104     TFPtoTVGlue     destroystream;
105     TFPtoTVGlue     asfile;
106     TFPtoTVGlue     writeready;
107     TFPtoTVGlue     write;
108     TFPtoTVGlue     print;
109     TFPtoTVGlue     event;
110     TFPtoTVGlue     urlnotify;
111     TFPtoTVGlue     getvalue;
112     TFPtoTVGlue     setvalue;
113
114     TFPtoTVGlue     shutdown;
115 } gPluginFuncsGlueTable;
116
117 static inline void* SetupFPtoTVGlue(TFPtoTVGlue* functionGlue, void* fp)
118 {
119     functionGlue->glue[0] = fp;
120     functionGlue->glue[1] = 0;
121     return functionGlue;
122 }
123
124 #define PLUGIN_TO_HOST_GLUE(name, fp) (SetupFPtoTVGlue(&gPluginFuncsGlueTable.name, (void*)fp))
125
126 // glue for mapping netscape TVectors to Macho function pointers
127 struct TTVtoFPGlue {
128     uint32 glue[6];
129 };
130
131 struct {
132     TTVtoFPGlue             geturl;
133     TTVtoFPGlue             posturl;
134     TTVtoFPGlue             requestread;
135     TTVtoFPGlue             newstream;
136     TTVtoFPGlue             write;
137     TTVtoFPGlue             destroystream;
138     TTVtoFPGlue             status;
139     TTVtoFPGlue             uagent;
140     TTVtoFPGlue             memalloc;
141     TTVtoFPGlue             memfree;
142     TTVtoFPGlue             memflush;
143     TTVtoFPGlue             reloadplugins;
144     TTVtoFPGlue             getJavaEnv;
145     TTVtoFPGlue             getJavaPeer;
146     TTVtoFPGlue             geturlnotify;
147     TTVtoFPGlue             posturlnotify;
148     TTVtoFPGlue             getvalue;
149     TTVtoFPGlue             setvalue;
150     TTVtoFPGlue             invalidaterect;
151     TTVtoFPGlue             invalidateregion;
152     TTVtoFPGlue             forceredraw;
153 } gNetscapeFuncsGlueTable;
154
155 static void* SetupTVtoFPGlue(TTVtoFPGlue* functionGlue, void* tvp)
156 {
157     static const TTVtoFPGlue glueTemplate = { 0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420 };
158
159     memcpy(functionGlue, &glueTemplate, sizeof(TTVtoFPGlue));
160     functionGlue->glue[0] |= ((UInt32)tvp >> 16);
161     functionGlue->glue[1] |= ((UInt32)tvp & 0xFFFF);
162     ::MakeDataExecutable(functionGlue, sizeof(TTVtoFPGlue));
163     return functionGlue;
164 }
165
166 #define HOST_TO_PLUGIN_GLUE(name, fp) (SetupTVtoFPGlue(&gNetscapeFuncsGlueTable.name, (void*)fp))
167
168 #else
169
170 #define PLUGIN_TO_HOST_GLUE(name, fp) (fp)
171 #define HOST_TO_PLUGIN_GLUE(name, fp) (fp)
172
173 #endif /* XP_MACOSX */
174
175
176 #pragma mark -
177
178
179 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
180 //
181 // Globals
182 //
183 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
184
185 #if !TARGET_API_MAC_CARBON
186 QDGlobals*              gQDPtr;                         // Pointer to NetscapeÕs QuickDraw globals
187 #endif
188 short                   gResFile;                       // Refnum of the pluginÕs resource file
189 NPNetscapeFuncs gNetscapeFuncs;         // Function table for procs in Netscape called by plugin
190
191 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
192 //
193 // Wrapper functions for all calls from the plugin to Netscape.
194 // These functions let the plugin developer just call the APIs
195 // as documented and defined in npapi.h, without needing to know
196 // about the function table and call macros in npupp.h.
197 //
198 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
199
200
201 void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
202 {
203         *plugin_major = NP_VERSION_MAJOR;
204         *plugin_minor = NP_VERSION_MINOR;
205         *netscape_major = gNetscapeFuncs.version >> 8;          // Major version is in high byte
206         *netscape_minor = gNetscapeFuncs.version & 0xFF;        // Minor version is in low byte
207 }
208
209 NPError NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
210 {
211         int navMinorVers = gNetscapeFuncs.version & 0xFF;
212         NPError err;
213         
214         if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
215         {
216                 err = CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
217         }
218         else
219         {
220                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
221         }
222         return err;
223 }
224
225 NPError NPN_GetURL(NPP instance, const char* url, const char* window)
226 {
227         return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
228 }
229
230 NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file, void* notifyData)
231 {
232         int navMinorVers = gNetscapeFuncs.version & 0xFF;
233         NPError err;
234         
235         if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
236         {
237                 err = CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify, instance, url, 
238                                                                                                                 window, len, buf, file, notifyData);
239         }
240         else
241         {
242                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
243         }
244         return err;
245 }
246
247 NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file)
248 {
249         return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance, url, window, len, buf, file);
250 }
251
252 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
253 {
254         return CallNPN_RequestReadProc(gNetscapeFuncs.requestread, stream, rangeList);
255 }
256
257 NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* window, NPStream** stream)
258 {
259         int navMinorVers = gNetscapeFuncs.version & 0xFF;
260         NPError err;
261         
262         if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
263         {
264                 err = CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance, type, window, stream);
265         }
266         else
267         {
268                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
269         }
270         return err;
271 }
272
273 int32 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
274 {
275         int navMinorVers = gNetscapeFuncs.version & 0xFF;
276         NPError err;
277         
278         if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
279         {
280                 err = CallNPN_WriteProc(gNetscapeFuncs.write, instance, stream, len, buffer);
281         }
282         else
283         {
284                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
285         }
286         return err;
287 }
288
289 NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
290 {
291         int navMinorVers = gNetscapeFuncs.version & 0xFF;
292         NPError err;
293         
294         if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
295         {
296                 err = CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream, instance, stream, reason);
297         }
298         else
299         {
300                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
301         }
302         return err;
303 }
304
305 void NPN_Status(NPP instance, const char* message)
306 {
307         CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
308 }
309
310 const char* NPN_UserAgent(NPP instance)
311 {
312         return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
313 }
314
315 void* NPN_MemAlloc(uint32 size)
316 {
317         return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
318 }
319
320 void NPN_MemFree(void* ptr)
321 {
322         CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
323 }
324
325 uint32 NPN_MemFlush(uint32 size)
326 {
327         return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
328 }
329
330 void NPN_ReloadPlugins(NPBool reloadPages)
331 {
332         CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
333 }
334
335 #ifdef OJI
336 JRIEnv* NPN_GetJavaEnv(void)
337 {
338         return CallNPN_GetJavaEnvProc( gNetscapeFuncs.getJavaEnv );
339 }
340
341 jobject  NPN_GetJavaPeer(NPP instance)
342 {
343         return CallNPN_GetJavaPeerProc( gNetscapeFuncs.getJavaPeer, instance );
344 }
345 #endif
346
347 NPError NPN_GetValue(NPP instance, NPNVariable variable, void *value)
348 {
349         return CallNPN_GetValueProc( gNetscapeFuncs.getvalue, instance, variable, value);
350 }
351
352 NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
353 {
354         return CallNPN_SetValueProc( gNetscapeFuncs.setvalue, instance, variable, value);
355 }
356
357 void NPN_InvalidateRect(NPP instance, NPRect *rect)
358 {
359         CallNPN_InvalidateRectProc( gNetscapeFuncs.invalidaterect, instance, rect);
360 }
361
362 void NPN_InvalidateRegion(NPP instance, NPRegion region)
363 {
364         CallNPN_InvalidateRegionProc( gNetscapeFuncs.invalidateregion, instance, region);
365 }
366
367 void NPN_ForceRedraw(NPP instance)
368 {
369         CallNPN_ForceRedrawProc( gNetscapeFuncs.forceredraw, instance);
370 }
371
372 #pragma mark -
373
374 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
375 //
376 // Wrapper functions for all calls from Netscape to the plugin.
377 // These functions let the plugin developer just create the APIs
378 // as documented and defined in npapi.h, without needing to 
379 // install those functions in the function table or worry about
380 // setting up globals for 68K plugins.
381 //
382 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
383
384 NPError         Private_Initialize(void);
385 void            Private_Shutdown(void);
386 NPError         Private_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved);
387 NPError         Private_Destroy(NPP instance, NPSavedData** save);
388 NPError         Private_SetWindow(NPP instance, NPWindow* window);
389 NPError         Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype);
390 NPError         Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
391 int32           Private_WriteReady(NPP instance, NPStream* stream);
392 int32           Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer);
393 void            Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
394 void            Private_Print(NPP instance, NPPrint* platformPrint);
395 int16           Private_HandleEvent(NPP instance, void* event);
396 void        Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
397 jobject         Private_GetJavaClass(void);
398
399
400 NPError Private_Initialize(void)
401 {
402         NPError err;
403         EnterCodeResource();
404         PLUGINDEBUGSTR("\pInitialize;g;");
405         err = NPP_Initialize();
406         ExitCodeResource();
407         return err;
408 }
409
410 void Private_Shutdown(void)
411 {
412         EnterCodeResource();
413         PLUGINDEBUGSTR("\pShutdown;g;");
414         NPP_Shutdown();
415
416 #ifdef __MWERKS__
417         __destroy_global_chain();
418 #endif
419
420         ExitCodeResource();
421 }
422
423
424 NPError Private_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
425 {
426         EnterCodeResource();
427         NPError ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
428         PLUGINDEBUGSTR("\pNew;g;");
429         ExitCodeResource();
430         return ret;     
431 }
432
433 NPError Private_Destroy(NPP instance, NPSavedData** save)
434 {
435         NPError err;
436         EnterCodeResource();
437         PLUGINDEBUGSTR("\pDestroy;g;");
438         err = NPP_Destroy(instance, save);
439         ExitCodeResource();
440         return err;
441 }
442
443 NPError Private_SetWindow(NPP instance, NPWindow* window)
444 {
445         NPError err;
446         EnterCodeResource();
447         PLUGINDEBUGSTR("\pSetWindow;g;");
448         err = NPP_SetWindow(instance, window);
449         ExitCodeResource();
450         return err;
451 }
452
453 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
454 {
455         NPError err;
456         EnterCodeResource();
457         PLUGINDEBUGSTR("\pNewStream;g;");
458         err = NPP_NewStream(instance, type, stream, seekable, stype);
459         ExitCodeResource();
460         return err;
461 }
462
463 int32 Private_WriteReady(NPP instance, NPStream* stream)
464 {
465         int32 result;
466         EnterCodeResource();
467         PLUGINDEBUGSTR("\pWriteReady;g;");
468         result = NPP_WriteReady(instance, stream);
469         ExitCodeResource();
470         return result;
471 }
472
473 int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
474 {
475         int32 result;
476         EnterCodeResource();
477         PLUGINDEBUGSTR("\pWrite;g;");
478         result = NPP_Write(instance, stream, offset, len, buffer);
479         ExitCodeResource();
480         return result;
481 }
482
483 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
484 {
485         EnterCodeResource();
486         PLUGINDEBUGSTR("\pStreamAsFile;g;");
487         NPP_StreamAsFile(instance, stream, fname);
488         ExitCodeResource();
489 }
490
491
492 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
493 {
494         NPError err;
495         EnterCodeResource();
496         PLUGINDEBUGSTR("\pDestroyStream;g;");
497         err = NPP_DestroyStream(instance, stream, reason);
498         ExitCodeResource();
499         return err;
500 }
501
502 int16 Private_HandleEvent(NPP instance, void* event)
503 {
504         int16 result;
505         EnterCodeResource();
506         PLUGINDEBUGSTR("\pHandleEvent;g;");
507         result = NPP_HandleEvent(instance, event);
508         ExitCodeResource();
509         return result;
510 }
511
512 void Private_Print(NPP instance, NPPrint* platformPrint)
513 {
514         EnterCodeResource();
515         PLUGINDEBUGSTR("\pPrint;g;");
516         NPP_Print(instance, platformPrint);
517         ExitCodeResource();
518 }
519
520 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
521 {
522         EnterCodeResource();
523         PLUGINDEBUGSTR("\pURLNotify;g;");
524         NPP_URLNotify(instance, url, reason, notifyData);
525         ExitCodeResource();
526 }
527
528 #ifdef OJI
529 jobject Private_GetJavaClass(void)
530 {
531         EnterCodeResource();
532         PLUGINDEBUGSTR("\pGetJavaClass;g;");
533
534     jobject clazz = NPP_GetJavaClass();
535     ExitCodeResource();
536     if (clazz)
537     {
538                 JRIEnv* env = NPN_GetJavaEnv();
539                 return (jobject)JRI_NewGlobalRef(env, clazz);
540     }
541     return NULL;
542 }
543 #endif
544
545 void SetUpQD(void);
546 void SetUpQD(void)
547 {
548 #if !TARGET_API_MAC_CARBON
549         ProcessSerialNumber PSN;
550         FSSpec                          myFSSpec;
551         Str63                           name;
552         ProcessInfoRec          infoRec;
553         OSErr                           result = noErr;
554         CFragConnectionID       connID;
555         Str255                          errName;
556 #endif  
557
558         //
559         // Memorize the pluginÕs resource file 
560         // refnum for later use.
561         //
562         gResFile = CurResFile();
563         
564 #if !TARGET_API_MAC_CARBON
565         //
566         // Ask the system if CFM is available.
567         //
568         long response;
569         OSErr err = Gestalt(gestaltCFMAttr, &response);
570         Boolean hasCFM = BitTst(&response, 31-gestaltCFMPresent);
571
572         ProcessInfoRec infoRec;
573         if (hasCFM)
574         {
575                 //
576                 // GetProcessInformation takes a process serial number and 
577                 // will give us back the name and FSSpec of the application.
578                 // See the Process Manager in IM.
579                 //
580                 Str63 name;
581                 FSSpec myFSSpec;
582                 infoRec.processInfoLength = sizeof(ProcessInfoRec);
583                 infoRec.processName = name;
584                 infoRec.processAppSpec = &myFSSpec;
585                 
586                 ProcessSerialNumber PSN;
587                 PSN.highLongOfPSN = 0;
588                 PSN.lowLongOfPSN = kCurrentProcess;
589                 
590                 result = GetProcessInformation(&PSN, &infoRec);
591                 if (result != noErr)
592                         PLUGINDEBUGSTR("\pFailed in GetProcessInformation");
593                 }
594         else
595                 //
596                 // If no CFM installed, assume it must be a 68K app.
597                 //
598                 result = -1;            
599                 
600         CFragConnectionID connID;
601         if (result == noErr)
602         {
603                 //
604                 // Now that we know the app name and FSSpec, we can call GetDiskFragment
605                 // to get a connID to use in a subsequent call to FindSymbol (it will also
606                 // return the address of ÒmainÓ in app, which we ignore).  If GetDiskFragment 
607                 // returns an error, we assume the app must be 68K.
608                 //
609                 Ptr mainAddr;   
610                 Str255 errName;
611                 result =  GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
612                                                                   kLoadCFrag, &connID, (Ptr*)&mainAddr, errName);
613         }
614
615         if (result == noErr) 
616         {
617                 //
618                 // The app is a PPC code fragment, so call FindSymbol
619                 // to get the exported ÒqdÓ symbol so we can access its
620                 // QuickDraw globals.
621                 //
622                 CFragSymbolClass symClass;
623                 result = FindSymbol(connID, "\pqd", (Ptr*)&gQDPtr, &symClass);
624                 if (result != noErr) {  // this fails if we are in NS 6
625                         gQDPtr = &qd;           // so we default to the standard QD globals
626                 }
627         }
628         else
629         {
630                 //
631                 // The app is 68K, so use its A5 to compute the address
632                 // of its QuickDraw globals.
633                 //
634                 gQDPtr = (QDGlobals*)(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
635         }
636 #endif
637 }
638
639
640 #ifdef __GNUC__
641 // gcc requires that main have an 'int' return type
642 int main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
643 #else
644 NPError main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
645 #endif
646
647 #if !TARGET_API_MAC_CARBON
648 #pragma export on
649 #if GENERATINGCFM
650 RoutineDescriptor mainRD = BUILD_ROUTINE_DESCRIPTOR(uppNPP_MainEntryProcInfo, main);
651 #endif
652 #pragma export off
653 #endif /* !TARGET_API_MAC_CARBON */
654
655 #ifdef __GNUC__
656 DEFINE_API_C(int) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
657 #else
658 DEFINE_API_C(NPError) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
659 #endif
660 {
661         EnterCodeResource();
662         PLUGINDEBUGSTR("\pmain");
663
664 #ifdef __MWERKS__
665         __InitCode__();
666 #endif
667
668         NPError err = NPERR_NO_ERROR;
669         
670         //
671         // Ensure that everything Netscape passed us is valid!
672         //
673         if ((nsTable == NULL) || (pluginFuncs == NULL) || (unloadUpp == NULL))
674                 err = NPERR_INVALID_FUNCTABLE_ERROR;
675         
676         //
677         // Check the ÒmajorÓ version passed in NetscapeÕs function table.
678         // We wonÕt load if the major version is newer than what we expect.
679         // Also check that the function tables passed in are big enough for
680         // all the functions we need (they could be bigger, if Netscape added
681         // new APIs, but thatÕs OK with us -- weÕll just ignore them).
682         //
683         if (err == NPERR_NO_ERROR)
684         {
685                 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)         // Major version is in high byte
686                         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
687         }
688                 
689         
690         if (err == NPERR_NO_ERROR)
691         {
692                 //
693                 // Copy all the fields of NetscapeÕs function table into our
694                 // copy so we can call back into Netscape later.  Note that
695                 // we need to copy the fields one by one, rather than assigning
696                 // the whole structure, because the Netscape function table
697                 // could actually be bigger than what we expect.
698                 //
699                 
700                 int navMinorVers = nsTable->version & 0xFF;
701
702                 gNetscapeFuncs.version          = nsTable->version;
703                 gNetscapeFuncs.size             = nsTable->size;
704                 gNetscapeFuncs.posturl          = (NPN_PostURLUPP)HOST_TO_PLUGIN_GLUE(posturl, nsTable->posturl);
705                 gNetscapeFuncs.geturl           = (NPN_GetURLUPP)HOST_TO_PLUGIN_GLUE(geturl, nsTable->geturl);
706                 gNetscapeFuncs.requestread      = (NPN_RequestReadUPP)HOST_TO_PLUGIN_GLUE(requestread, nsTable->requestread);
707                 gNetscapeFuncs.newstream        = (NPN_NewStreamUPP)HOST_TO_PLUGIN_GLUE(newstream, nsTable->newstream);
708                 gNetscapeFuncs.write            = (NPN_WriteUPP)HOST_TO_PLUGIN_GLUE(write, nsTable->write);
709                 gNetscapeFuncs.destroystream    = (NPN_DestroyStreamUPP)HOST_TO_PLUGIN_GLUE(destroystream, nsTable->destroystream);
710                 gNetscapeFuncs.status           = (NPN_StatusUPP)HOST_TO_PLUGIN_GLUE(status, nsTable->status);
711                 gNetscapeFuncs.uagent           = (NPN_UserAgentUPP)HOST_TO_PLUGIN_GLUE(uagent, nsTable->uagent);
712                 gNetscapeFuncs.memalloc         = (NPN_MemAllocUPP)HOST_TO_PLUGIN_GLUE(memalloc, nsTable->memalloc);
713                 gNetscapeFuncs.memfree          = (NPN_MemFreeUPP)HOST_TO_PLUGIN_GLUE(memfree, nsTable->memfree);
714                 gNetscapeFuncs.memflush         = (NPN_MemFlushUPP)HOST_TO_PLUGIN_GLUE(memflush, nsTable->memflush);
715                 gNetscapeFuncs.reloadplugins    = (NPN_ReloadPluginsUPP)HOST_TO_PLUGIN_GLUE(reloadplugins, nsTable->reloadplugins);
716                 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
717                 {
718                         gNetscapeFuncs.getJavaEnv   = (NPN_GetJavaEnvUPP)HOST_TO_PLUGIN_GLUE(getJavaEnv, nsTable->getJavaEnv);
719                         gNetscapeFuncs.getJavaPeer  = (NPN_GetJavaPeerUPP)HOST_TO_PLUGIN_GLUE(getJavaPeer, nsTable->getJavaPeer);
720                 }
721                 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
722                 {       
723                         gNetscapeFuncs.geturlnotify     = (NPN_GetURLNotifyUPP)HOST_TO_PLUGIN_GLUE(geturlnotify, nsTable->geturlnotify);
724                         gNetscapeFuncs.posturlnotify    = (NPN_PostURLNotifyUPP)HOST_TO_PLUGIN_GLUE(posturlnotify, nsTable->posturlnotify);
725                 }
726                 gNetscapeFuncs.getvalue         = (NPN_GetValueUPP)HOST_TO_PLUGIN_GLUE(getvalue, nsTable->getvalue);
727                 gNetscapeFuncs.setvalue         = (NPN_SetValueUPP)HOST_TO_PLUGIN_GLUE(setvalue, nsTable->setvalue);
728                 gNetscapeFuncs.invalidaterect   = (NPN_InvalidateRectUPP)HOST_TO_PLUGIN_GLUE(invalidaterect, nsTable->invalidaterect);
729                 gNetscapeFuncs.invalidateregion = (NPN_InvalidateRegionUPP)HOST_TO_PLUGIN_GLUE(invalidateregion, nsTable->invalidateregion);
730                 gNetscapeFuncs.forceredraw      = (NPN_ForceRedrawUPP)HOST_TO_PLUGIN_GLUE(forceredraw, nsTable->forceredraw);
731                 
732                 //
733                 // Set up the plugin function table that Netscape will use to
734                 // call us.  Netscape needs to know about our version and size
735                 // and have a UniversalProcPointer for every function we implement.
736                 //
737                 pluginFuncs->version        = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
738                 pluginFuncs->size           = sizeof(NPPluginFuncs);
739                 pluginFuncs->newp           = NewNPP_NewProc(PLUGIN_TO_HOST_GLUE(newp, Private_New));
740                 pluginFuncs->destroy        = NewNPP_DestroyProc(PLUGIN_TO_HOST_GLUE(destroy, Private_Destroy));
741                 pluginFuncs->setwindow      = NewNPP_SetWindowProc(PLUGIN_TO_HOST_GLUE(setwindow, Private_SetWindow));
742                 pluginFuncs->newstream      = NewNPP_NewStreamProc(PLUGIN_TO_HOST_GLUE(newstream, Private_NewStream));
743                 pluginFuncs->destroystream  = NewNPP_DestroyStreamProc(PLUGIN_TO_HOST_GLUE(destroystream, Private_DestroyStream));
744                 pluginFuncs->asfile         = NewNPP_StreamAsFileProc(PLUGIN_TO_HOST_GLUE(asfile, Private_StreamAsFile));
745                 pluginFuncs->writeready     = NewNPP_WriteReadyProc(PLUGIN_TO_HOST_GLUE(writeready, Private_WriteReady));
746                 pluginFuncs->write          = NewNPP_WriteProc(PLUGIN_TO_HOST_GLUE(write, Private_Write));
747                 pluginFuncs->print          = NewNPP_PrintProc(PLUGIN_TO_HOST_GLUE(print, Private_Print));
748                 pluginFuncs->event          = NewNPP_HandleEventProc(PLUGIN_TO_HOST_GLUE(event, Private_HandleEvent));  
749                 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
750                 {       
751                         pluginFuncs->urlnotify = NewNPP_URLNotifyProc(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));                       
752                 }
753 #ifdef OJI
754                 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
755                 {
756                         pluginFuncs->javaClass  = (JRIGlobalRef) Private_GetJavaClass();
757                 }
758 #else
759                 pluginFuncs->javaClass = NULL;
760 #endif
761                 *unloadUpp = NewNPP_ShutdownProc(PLUGIN_TO_HOST_GLUE(shutdown, Private_Shutdown));
762
763                 SetUpQD();
764                 err = Private_Initialize();
765         }
766         
767         ExitCodeResource();
768         return err;
769 }