* XPCOM implementation in mozilla plugin on MacOSX (Play, pause, stop
[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_GetValue( NPP instance, NPPVariable variable, void *value );
390 NPError         Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype);
391 NPError         Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
392 int32           Private_WriteReady(NPP instance, NPStream* stream);
393 int32           Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer);
394 void            Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
395 void            Private_Print(NPP instance, NPPrint* platformPrint);
396 int16           Private_HandleEvent(NPP instance, void* event);
397 void        Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
398 jobject         Private_GetJavaClass(void);
399
400
401 NPError Private_Initialize(void)
402 {
403         NPError err;
404         EnterCodeResource();
405         PLUGINDEBUGSTR("\pInitialize;g;");
406         err = NPP_Initialize();
407         ExitCodeResource();
408         return err;
409 }
410
411 void Private_Shutdown(void)
412 {
413         EnterCodeResource();
414         PLUGINDEBUGSTR("\pShutdown;g;");
415         NPP_Shutdown();
416
417 #ifdef __MWERKS__
418         __destroy_global_chain();
419 #endif
420
421         ExitCodeResource();
422 }
423
424
425 NPError Private_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
426 {
427         EnterCodeResource();
428         NPError ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
429         PLUGINDEBUGSTR("\pNew;g;");
430         ExitCodeResource();
431         return ret;     
432 }
433
434 NPError Private_Destroy(NPP instance, NPSavedData** save)
435 {
436         NPError err;
437         EnterCodeResource();
438         PLUGINDEBUGSTR("\pDestroy;g;");
439         err = NPP_Destroy(instance, save);
440         ExitCodeResource();
441         return err;
442 }
443
444 NPError Private_SetWindow(NPP instance, NPWindow* window)
445 {
446         NPError err;
447         EnterCodeResource();
448         PLUGINDEBUGSTR("\pSetWindow;g;");
449         err = NPP_SetWindow(instance, window);
450         ExitCodeResource();
451         return err;
452 }
453
454 NPError Private_GetValue( NPP instance, NPPVariable variable, void *value )
455 {
456         NPError err;
457         EnterCodeResource();
458         PLUGINDEBUGSTR("\pGetValue;g;");
459         err = NPP_GetValue( instance, variable, value);
460         ExitCodeResource();
461         return err;
462 }
463
464 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
465 {
466         NPError err;
467         EnterCodeResource();
468         PLUGINDEBUGSTR("\pNewStream;g;");
469         err = NPP_NewStream(instance, type, stream, seekable, stype);
470         ExitCodeResource();
471         return err;
472 }
473
474 int32 Private_WriteReady(NPP instance, NPStream* stream)
475 {
476         int32 result;
477         EnterCodeResource();
478         PLUGINDEBUGSTR("\pWriteReady;g;");
479         result = NPP_WriteReady(instance, stream);
480         ExitCodeResource();
481         return result;
482 }
483
484 int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
485 {
486         int32 result;
487         EnterCodeResource();
488         PLUGINDEBUGSTR("\pWrite;g;");
489         result = NPP_Write(instance, stream, offset, len, buffer);
490         ExitCodeResource();
491         return result;
492 }
493
494 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
495 {
496         EnterCodeResource();
497         PLUGINDEBUGSTR("\pStreamAsFile;g;");
498         NPP_StreamAsFile(instance, stream, fname);
499         ExitCodeResource();
500 }
501
502
503 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
504 {
505         NPError err;
506         EnterCodeResource();
507         PLUGINDEBUGSTR("\pDestroyStream;g;");
508         err = NPP_DestroyStream(instance, stream, reason);
509         ExitCodeResource();
510         return err;
511 }
512
513 int16 Private_HandleEvent(NPP instance, void* event)
514 {
515         int16 result;
516         EnterCodeResource();
517         PLUGINDEBUGSTR("\pHandleEvent;g;");
518         result = NPP_HandleEvent(instance, event);
519         ExitCodeResource();
520         return result;
521 }
522
523 void Private_Print(NPP instance, NPPrint* platformPrint)
524 {
525         EnterCodeResource();
526         PLUGINDEBUGSTR("\pPrint;g;");
527         NPP_Print(instance, platformPrint);
528         ExitCodeResource();
529 }
530
531 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
532 {
533         EnterCodeResource();
534         PLUGINDEBUGSTR("\pURLNotify;g;");
535         NPP_URLNotify(instance, url, reason, notifyData);
536         ExitCodeResource();
537 }
538
539 #ifdef OJI
540 jobject Private_GetJavaClass(void)
541 {
542         EnterCodeResource();
543         PLUGINDEBUGSTR("\pGetJavaClass;g;");
544
545     jobject clazz = NPP_GetJavaClass();
546     ExitCodeResource();
547     if (clazz)
548     {
549                 JRIEnv* env = NPN_GetJavaEnv();
550                 return (jobject)JRI_NewGlobalRef(env, clazz);
551     }
552     return NULL;
553 }
554 #endif
555
556 void SetUpQD(void);
557 void SetUpQD(void)
558 {
559 #if !TARGET_API_MAC_CARBON
560         ProcessSerialNumber PSN;
561         FSSpec                          myFSSpec;
562         Str63                           name;
563         ProcessInfoRec          infoRec;
564         OSErr                           result = noErr;
565         CFragConnectionID       connID;
566         Str255                          errName;
567 #endif  
568
569         //
570         // Memorize the pluginÕs resource file 
571         // refnum for later use.
572         //
573         gResFile = CurResFile();
574         
575 #if !TARGET_API_MAC_CARBON
576         //
577         // Ask the system if CFM is available.
578         //
579         long response;
580         OSErr err = Gestalt(gestaltCFMAttr, &response);
581         Boolean hasCFM = BitTst(&response, 31-gestaltCFMPresent);
582
583         ProcessInfoRec infoRec;
584         if (hasCFM)
585         {
586                 //
587                 // GetProcessInformation takes a process serial number and 
588                 // will give us back the name and FSSpec of the application.
589                 // See the Process Manager in IM.
590                 //
591                 Str63 name;
592                 FSSpec myFSSpec;
593                 infoRec.processInfoLength = sizeof(ProcessInfoRec);
594                 infoRec.processName = name;
595                 infoRec.processAppSpec = &myFSSpec;
596                 
597                 ProcessSerialNumber PSN;
598                 PSN.highLongOfPSN = 0;
599                 PSN.lowLongOfPSN = kCurrentProcess;
600                 
601                 result = GetProcessInformation(&PSN, &infoRec);
602                 if (result != noErr)
603                         PLUGINDEBUGSTR("\pFailed in GetProcessInformation");
604                 }
605         else
606                 //
607                 // If no CFM installed, assume it must be a 68K app.
608                 //
609                 result = -1;            
610                 
611         CFragConnectionID connID;
612         if (result == noErr)
613         {
614                 //
615                 // Now that we know the app name and FSSpec, we can call GetDiskFragment
616                 // to get a connID to use in a subsequent call to FindSymbol (it will also
617                 // return the address of ÒmainÓ in app, which we ignore).  If GetDiskFragment 
618                 // returns an error, we assume the app must be 68K.
619                 //
620                 Ptr mainAddr;   
621                 Str255 errName;
622                 result =  GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
623                                                                   kLoadCFrag, &connID, (Ptr*)&mainAddr, errName);
624         }
625
626         if (result == noErr) 
627         {
628                 //
629                 // The app is a PPC code fragment, so call FindSymbol
630                 // to get the exported ÒqdÓ symbol so we can access its
631                 // QuickDraw globals.
632                 //
633                 CFragSymbolClass symClass;
634                 result = FindSymbol(connID, "\pqd", (Ptr*)&gQDPtr, &symClass);
635                 if (result != noErr) {  // this fails if we are in NS 6
636                         gQDPtr = &qd;           // so we default to the standard QD globals
637                 }
638         }
639         else
640         {
641                 //
642                 // The app is 68K, so use its A5 to compute the address
643                 // of its QuickDraw globals.
644                 //
645                 gQDPtr = (QDGlobals*)(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
646         }
647 #endif
648 }
649
650
651 #ifdef __GNUC__
652 // gcc requires that main have an 'int' return type
653 int main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
654 #else
655 NPError main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
656 #endif
657
658 #if !TARGET_API_MAC_CARBON
659 #pragma export on
660 #if GENERATINGCFM
661 RoutineDescriptor mainRD = BUILD_ROUTINE_DESCRIPTOR(uppNPP_MainEntryProcInfo, main);
662 #endif
663 #pragma export off
664 #endif /* !TARGET_API_MAC_CARBON */
665
666 #ifdef __GNUC__
667 DEFINE_API_C(int) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
668 #else
669 DEFINE_API_C(NPError) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
670 #endif
671 {
672         EnterCodeResource();
673         PLUGINDEBUGSTR("\pmain");
674
675 #ifdef __MWERKS__
676         __InitCode__();
677 #endif
678
679         NPError err = NPERR_NO_ERROR;
680         
681         //
682         // Ensure that everything Netscape passed us is valid!
683         //
684         if ((nsTable == NULL) || (pluginFuncs == NULL) || (unloadUpp == NULL))
685                 err = NPERR_INVALID_FUNCTABLE_ERROR;
686         
687         //
688         // Check the ÒmajorÓ version passed in NetscapeÕs function table.
689         // We wonÕt load if the major version is newer than what we expect.
690         // Also check that the function tables passed in are big enough for
691         // all the functions we need (they could be bigger, if Netscape added
692         // new APIs, but thatÕs OK with us -- weÕll just ignore them).
693         //
694         if (err == NPERR_NO_ERROR)
695         {
696                 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)         // Major version is in high byte
697                         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
698         }
699                 
700         
701         if (err == NPERR_NO_ERROR)
702         {
703                 //
704                 // Copy all the fields of NetscapeÕs function table into our
705                 // copy so we can call back into Netscape later.  Note that
706                 // we need to copy the fields one by one, rather than assigning
707                 // the whole structure, because the Netscape function table
708                 // could actually be bigger than what we expect.
709                 //
710                 
711                 int navMinorVers = nsTable->version & 0xFF;
712
713                 gNetscapeFuncs.version          = nsTable->version;
714                 gNetscapeFuncs.size             = nsTable->size;
715                 gNetscapeFuncs.posturl          = (NPN_PostURLUPP)HOST_TO_PLUGIN_GLUE(posturl, nsTable->posturl);
716                 gNetscapeFuncs.geturl           = (NPN_GetURLUPP)HOST_TO_PLUGIN_GLUE(geturl, nsTable->geturl);
717                 gNetscapeFuncs.requestread      = (NPN_RequestReadUPP)HOST_TO_PLUGIN_GLUE(requestread, nsTable->requestread);
718                 gNetscapeFuncs.newstream        = (NPN_NewStreamUPP)HOST_TO_PLUGIN_GLUE(newstream, nsTable->newstream);
719                 gNetscapeFuncs.write            = (NPN_WriteUPP)HOST_TO_PLUGIN_GLUE(write, nsTable->write);
720                 gNetscapeFuncs.destroystream    = (NPN_DestroyStreamUPP)HOST_TO_PLUGIN_GLUE(destroystream, nsTable->destroystream);
721                 gNetscapeFuncs.status           = (NPN_StatusUPP)HOST_TO_PLUGIN_GLUE(status, nsTable->status);
722                 gNetscapeFuncs.uagent           = (NPN_UserAgentUPP)HOST_TO_PLUGIN_GLUE(uagent, nsTable->uagent);
723                 gNetscapeFuncs.memalloc         = (NPN_MemAllocUPP)HOST_TO_PLUGIN_GLUE(memalloc, nsTable->memalloc);
724                 gNetscapeFuncs.memfree          = (NPN_MemFreeUPP)HOST_TO_PLUGIN_GLUE(memfree, nsTable->memfree);
725                 gNetscapeFuncs.memflush         = (NPN_MemFlushUPP)HOST_TO_PLUGIN_GLUE(memflush, nsTable->memflush);
726                 gNetscapeFuncs.reloadplugins    = (NPN_ReloadPluginsUPP)HOST_TO_PLUGIN_GLUE(reloadplugins, nsTable->reloadplugins);
727                 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
728                 {
729                         gNetscapeFuncs.getJavaEnv   = (NPN_GetJavaEnvUPP)HOST_TO_PLUGIN_GLUE(getJavaEnv, nsTable->getJavaEnv);
730                         gNetscapeFuncs.getJavaPeer  = (NPN_GetJavaPeerUPP)HOST_TO_PLUGIN_GLUE(getJavaPeer, nsTable->getJavaPeer);
731                 }
732                 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
733                 {       
734                         gNetscapeFuncs.geturlnotify     = (NPN_GetURLNotifyUPP)HOST_TO_PLUGIN_GLUE(geturlnotify, nsTable->geturlnotify);
735                         gNetscapeFuncs.posturlnotify    = (NPN_PostURLNotifyUPP)HOST_TO_PLUGIN_GLUE(posturlnotify, nsTable->posturlnotify);
736                 }
737                 gNetscapeFuncs.getvalue         = (NPN_GetValueUPP)HOST_TO_PLUGIN_GLUE(getvalue, nsTable->getvalue);
738                 gNetscapeFuncs.setvalue         = (NPN_SetValueUPP)HOST_TO_PLUGIN_GLUE(setvalue, nsTable->setvalue);
739                 gNetscapeFuncs.invalidaterect   = (NPN_InvalidateRectUPP)HOST_TO_PLUGIN_GLUE(invalidaterect, nsTable->invalidaterect);
740                 gNetscapeFuncs.invalidateregion = (NPN_InvalidateRegionUPP)HOST_TO_PLUGIN_GLUE(invalidateregion, nsTable->invalidateregion);
741                 gNetscapeFuncs.forceredraw      = (NPN_ForceRedrawUPP)HOST_TO_PLUGIN_GLUE(forceredraw, nsTable->forceredraw);
742                 
743                 //
744                 // Set up the plugin function table that Netscape will use to
745                 // call us.  Netscape needs to know about our version and size
746                 // and have a UniversalProcPointer for every function we implement.
747                 //
748                 pluginFuncs->version        = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
749                 pluginFuncs->size           = sizeof(NPPluginFuncs);
750                 pluginFuncs->newp           = NewNPP_NewProc(PLUGIN_TO_HOST_GLUE(newp, Private_New));
751                 pluginFuncs->destroy        = NewNPP_DestroyProc(PLUGIN_TO_HOST_GLUE(destroy, Private_Destroy));
752                 pluginFuncs->setwindow      = NewNPP_SetWindowProc(PLUGIN_TO_HOST_GLUE(setwindow, Private_SetWindow));
753                 pluginFuncs->newstream      = NewNPP_NewStreamProc(PLUGIN_TO_HOST_GLUE(newstream, Private_NewStream));
754                 pluginFuncs->destroystream  = NewNPP_DestroyStreamProc(PLUGIN_TO_HOST_GLUE(destroystream, Private_DestroyStream));
755                 pluginFuncs->asfile         = NewNPP_StreamAsFileProc(PLUGIN_TO_HOST_GLUE(asfile, Private_StreamAsFile));
756                 pluginFuncs->writeready     = NewNPP_WriteReadyProc(PLUGIN_TO_HOST_GLUE(writeready, Private_WriteReady));
757                 pluginFuncs->write          = NewNPP_WriteProc(PLUGIN_TO_HOST_GLUE(write, Private_Write));
758                 pluginFuncs->print          = NewNPP_PrintProc(PLUGIN_TO_HOST_GLUE(print, Private_Print));
759                 pluginFuncs->event          = NewNPP_HandleEventProc(PLUGIN_TO_HOST_GLUE(event, Private_HandleEvent));  
760                 pluginFuncs->getvalue       = NewNPP_GetValueProc(PLUGIN_TO_HOST_GLUE(getvalue, Private_GetValue));     
761                 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
762                 {       
763                         pluginFuncs->urlnotify = NewNPP_URLNotifyProc(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));                       
764                 }
765 #ifdef OJI
766                 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
767                 {
768                         pluginFuncs->javaClass  = (JRIGlobalRef) Private_GetJavaClass();
769                 }
770 #else
771                 pluginFuncs->javaClass = NULL;
772 #endif
773                 *unloadUpp = NewNPP_ShutdownProc(PLUGIN_TO_HOST_GLUE(shutdown, Private_Shutdown));
774
775                 SetUpQD();
776                 err = Private_Initialize();
777         }
778         
779         ExitCodeResource();
780         return err;
781 }