1
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
5
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
11
#include <Resources.h>
12
#include <ToolUtils.h>
17
// A4Stuff.h contains the definition of EnterCodeResource and
18
// EnterCodeResource, used for setting up the code resource's
19
// globals for 68K (analagous to the function SetCurrentA5
20
// defined by the toolbox).
28
// The Mixed Mode procInfos defined in npupp.h assume Think C-
29
// style calling conventions. These conventions are used by
30
// Metrowerks with the exception of pointer return types, which
31
// in Metrowerks 68K are returned in A0, instead of the standard
32
// D0. Thus, since NPN_MemAlloc and NPN_UserAgent return pointers,
33
// Mixed Mode will return the values to a 68K plugin in D0, but
34
// a 68K plugin compiled by Metrowerks will expect the result in
35
// A0. The following pragma forces Metrowerks to use D0 instead.
39
#pragma pointers_in_D0
47
#pragma pointers_in_A0
53
// Define PLUGIN_TRACE to 1 to have the wrapper functions emit
54
// DebugStr messages whenever they are called.
56
#define PLUGIN_TRACE 0
59
#define PLUGINDEBUGSTR(msg) ::DebugStr(msg)
61
#define PLUGINDEBUGSTR
69
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
73
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
75
QDGlobals* gQDPtr; // Pointer to Netscape's QuickDraw globals
76
short gResFile; // Refnum of the plugin's resource file
77
NPNetscapeFuncs gNetscapeFuncs; // Function table for procs in Netscape called by plugin
80
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
82
// Wrapper functions for all calls from the plugin to Netscape.
83
// These functions let the plugin developer just call the APIs
84
// as documented and defined in npapi.h, without needing to know
85
// about the function table and call macros in npupp.h.
87
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
90
void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
92
*plugin_major = NP_VERSION_MAJOR;
93
*plugin_minor = NP_VERSION_MINOR;
94
*netscape_major = gNetscapeFuncs.version >> 8; // Major version is in high byte
95
*netscape_minor = gNetscapeFuncs.version & 0xFF; // Minor version is in low byte
98
NPError NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
100
int navMinorVers = gNetscapeFuncs.version & 0xFF;
103
if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
105
err = CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
109
err = NPERR_INCOMPATIBLE_VERSION_ERROR;
114
NPError NPN_GetURL(NPP instance, const char* url, const char* window)
116
return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
119
NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file, void* notifyData)
121
int navMinorVers = gNetscapeFuncs.version & 0xFF;
124
if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
126
err = CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify, instance, url,
127
window, len, buf, file, notifyData);
131
err = NPERR_INCOMPATIBLE_VERSION_ERROR;
136
NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file)
138
return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance, url, window, len, buf, file);
141
NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
143
return CallNPN_RequestReadProc(gNetscapeFuncs.requestread, stream, rangeList);
146
NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* window, NPStream** stream)
148
int navMinorVers = gNetscapeFuncs.version & 0xFF;
151
if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
153
err = CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance, type, window, stream);
157
err = NPERR_INCOMPATIBLE_VERSION_ERROR;
162
int32 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
164
int navMinorVers = gNetscapeFuncs.version & 0xFF;
167
if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
169
err = CallNPN_WriteProc(gNetscapeFuncs.write, instance, stream, len, buffer);
173
err = NPERR_INCOMPATIBLE_VERSION_ERROR;
178
NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
180
int navMinorVers = gNetscapeFuncs.version & 0xFF;
183
if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
185
err = CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream, instance, stream, reason);
189
err = NPERR_INCOMPATIBLE_VERSION_ERROR;
194
void NPN_Status(NPP instance, const char* message)
196
CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
199
const char* NPN_UserAgent(NPP instance)
201
return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
204
void* NPN_MemAlloc(uint32 size)
206
return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
209
void NPN_MemFree(void* ptr)
211
CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
214
uint32 NPN_MemFlush(uint32 size)
216
return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
219
void NPN_ReloadPlugins(NPBool reloadPages)
221
CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
225
JRIEnv* NPN_GetJavaEnv(void)
227
return CallNPN_GetJavaEnvProc( gNetscapeFuncs.getJavaEnv );
230
jref NPN_GetJavaPeer(NPP instance)
232
return CallNPN_GetJavaPeerProc( gNetscapeFuncs.getJavaPeer, instance );
237
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
239
// Wrapper functions for all calls from Netscape to the plugin.
240
// These functions let the plugin developer just create the APIs
241
// as documented and defined in npapi.h, without needing to
242
// install those functions in the function table or worry about
243
// setting up globals for 68K plugins.
245
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
247
NPError Private_Initialize(void);
248
void Private_Shutdown(void);
249
NPError Private_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved);
250
NPError Private_Destroy(NPP instance, NPSavedData** save);
251
NPError Private_SetWindow(NPP instance, NPWindow* window);
252
NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype);
253
NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
254
int32 Private_WriteReady(NPP instance, NPStream* stream);
255
int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer);
256
void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
257
void Private_Print(NPP instance, NPPrint* platformPrint);
258
int16 Private_HandleEvent(NPP instance, void* event);
259
void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
260
jref Private_GetJavaClass(void);
263
NPError Private_Initialize(void)
267
PLUGINDEBUGSTR("\pInitialize;g;");
268
err = NPP_Initialize();
273
void Private_Shutdown(void)
276
PLUGINDEBUGSTR("\pShutdown;g;");
282
NPError Private_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
285
NPError ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
286
PLUGINDEBUGSTR("\pNew;g;");
291
NPError Private_Destroy(NPP instance, NPSavedData** save)
295
PLUGINDEBUGSTR("\pDestroy;g;");
296
err = NPP_Destroy(instance, save);
301
NPError Private_SetWindow(NPP instance, NPWindow* window)
305
PLUGINDEBUGSTR("\pSetWindow;g;");
306
err = NPP_SetWindow(instance, window);
311
NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
315
PLUGINDEBUGSTR("\pNewStream;g;");
316
err = NPP_NewStream(instance, type, stream, seekable, stype);
321
int32 Private_WriteReady(NPP instance, NPStream* stream)
325
PLUGINDEBUGSTR("\pWriteReady;g;");
326
result = NPP_WriteReady(instance, stream);
331
int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
335
PLUGINDEBUGSTR("\pWrite;g;");
336
result = NPP_Write(instance, stream, offset, len, buffer);
341
void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
344
PLUGINDEBUGSTR("\pStreamAsFile;g;");
345
NPP_StreamAsFile(instance, stream, fname);
350
NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
354
PLUGINDEBUGSTR("\pDestroyStream;g;");
355
err = NPP_DestroyStream(instance, stream, reason);
360
int16 Private_HandleEvent(NPP instance, void* event)
364
PLUGINDEBUGSTR("\pHandleEvent;g;");
365
result = NPP_HandleEvent(instance, event);
370
void Private_Print(NPP instance, NPPrint* platformPrint)
373
PLUGINDEBUGSTR("\pPrint;g;");
374
NPP_Print(instance, platformPrint);
378
void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
381
PLUGINDEBUGSTR("\pURLNotify;g;");
382
NPP_URLNotify(instance, url, reason, notifyData);
387
jref Private_GetJavaClass(void)
390
PLUGINDEBUGSTR("\pGetJavaClass;g;");
392
jref clazz = NPP_GetJavaClass();
396
JRIEnv* env = NPN_GetJavaEnv();
397
return JRI_NewGlobalRef(env, clazz);
406
ProcessSerialNumber PSN;
409
ProcessInfoRec infoRec;
410
OSErr result = noErr;
415
// Memorize the plugin's resource file
416
// refnum for later use.
418
gResFile = CurResFile();
421
// Ask the system if CFM is available.
424
OSErr err = Gestalt(gestaltCFMAttr, &response);
425
Boolean hasCFM = BitTst(&response, 31-gestaltCFMPresent);
430
// GetProcessInformation takes a process serial number and
431
// will give us back the name and FSSpec of the application.
432
// See the Process Manager in IM.
434
infoRec.processInfoLength = sizeof(ProcessInfoRec);
435
infoRec.processName = name;
436
infoRec.processAppSpec = &myFSSpec;
438
PSN.highLongOfPSN = 0;
439
PSN.lowLongOfPSN = kCurrentProcess;
441
result = GetProcessInformation(&PSN, &infoRec);
443
PLUGINDEBUGSTR("\pFailed in GetProcessInformation");
447
// If no CFM installed, assume it must be a 68K app.
454
// Now that we know the app name and FSSpec, we can call GetDiskFragment
455
// to get a connID to use in a subsequent call to FindSymbol (it will also
456
// return the address of `main' in app, which we ignore). If GetDiskFragment
457
// returns an error, we assume the app must be 68K.
460
result = GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
461
kLoadLib, &connID, (Ptr*)&mainAddr, errName);
467
// The app is a PPC code fragment, so call FindSymbol
468
// to get the exported `qd' symbol so we can access its
469
// QuickDraw globals.
472
result = FindSymbol(connID, "\pqd", (Ptr*)&gQDPtr, &symClass);
474
PLUGINDEBUGSTR("\pFailed in FindSymbol qd");
479
// The app is 68K, so use its A5 to compute the address
480
// of its QuickDraw globals.
482
gQDPtr = (QDGlobals*)(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
489
NPError main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
492
RoutineDescriptor mainRD = BUILD_ROUTINE_DESCRIPTOR(uppNPP_MainEntryProcInfo, main);
496
NPError main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp)
499
PLUGINDEBUGSTR("\pmain");
501
NPError err = NPERR_NO_ERROR;
504
// Ensure that everything Netscape passed us is valid!
506
if ((nsTable == NULL) || (pluginFuncs == NULL) || (unloadUpp == NULL))
507
err = NPERR_INVALID_FUNCTABLE_ERROR;
510
// Check the `major' version passed in Netscape's function table.
511
// We won't load if the major version is newer than what we expect.
512
// Also check that the function tables passed in are big enough for
513
// all the functions we need (they could be bigger, if Netscape added
514
// new APIs, but that's OK with us -- we'll just ignore them).
516
if (err == NPERR_NO_ERROR)
518
if ((nsTable->version >> 8) > NP_VERSION_MAJOR) // Major version is in high byte
519
err = NPERR_INCOMPATIBLE_VERSION_ERROR;
520
// if (nsTable->size < sizeof(NPNetscapeFuncs))
521
// err = NPERR_INVALID_FUNCTABLE_ERROR;
522
// if (pluginFuncs->size < sizeof(NPPluginFuncs))
523
// err = NPERR_INVALID_FUNCTABLE_ERROR;
527
if (err == NPERR_NO_ERROR)
530
// Copy all the fields of Netscape's function table into our
531
// copy so we can call back into Netscape later. Note that
532
// we need to copy the fields one by one, rather than assigning
533
// the whole structure, because the Netscape function table
534
// could actually be bigger than what we expect.
537
int navMinorVers = nsTable->version & 0xFF;
539
gNetscapeFuncs.version = nsTable->version;
540
gNetscapeFuncs.size = nsTable->size;
541
gNetscapeFuncs.posturl = nsTable->posturl;
542
gNetscapeFuncs.geturl = nsTable->geturl;
543
gNetscapeFuncs.requestread = nsTable->requestread;
544
gNetscapeFuncs.newstream = nsTable->newstream;
545
gNetscapeFuncs.write = nsTable->write;
546
gNetscapeFuncs.destroystream = nsTable->destroystream;
547
gNetscapeFuncs.status = nsTable->status;
548
gNetscapeFuncs.uagent = nsTable->uagent;
549
gNetscapeFuncs.memalloc = nsTable->memalloc;
550
gNetscapeFuncs.memfree = nsTable->memfree;
551
gNetscapeFuncs.memflush = nsTable->memflush;
552
gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
553
if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
555
gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
556
gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
558
if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
560
gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
561
gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
565
// Set up the plugin function table that Netscape will use to
566
// call us. Netscape needs to know about our version and size
567
// and have a UniversalProcPointer for every function we implement.
569
pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
570
pluginFuncs->size = sizeof(NPPluginFuncs);
571
pluginFuncs->newp = NewNPP_NewProc(Private_New);
572
pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
573
pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
574
pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
575
pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
576
pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
577
pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
578
pluginFuncs->write = NewNPP_WriteProc(Private_Write);
579
pluginFuncs->print = NewNPP_PrintProc(Private_Print);
580
pluginFuncs->event = NewNPP_HandleEventProc(Private_HandleEvent);
581
if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
583
pluginFuncs->urlnotify = NewNPP_URLNotifyProc(Private_URLNotify);
585
if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
587
pluginFuncs->javaClass = (JRIGlobalRef) Private_GetJavaClass();
589
*unloadUpp = NewNPP_ShutdownProc(Private_Shutdown);
591
err = Private_Initialize();