Blender V2.61 - r43446

npunix.c

Go to the documentation of this file.
00001 /*
00002  * npunix.c
00003  *
00004  * Netscape Client Plugin API
00005  * - Wrapper function to interface with the Netscape Navigator
00006  *
00007  * dp Suresh <dp@netscape.com>
00008  *
00009  *----------------------------------------------------------------------
00010  * PLUGIN DEVELOPERS:
00011  *  YOU WILL NOT NEED TO EDIT THIS FILE.
00012  *----------------------------------------------------------------------
00013  */
00014 
00020 #include <stdio.h>
00021 #include "npapi.h"
00022 #include "npupp.h"
00023 
00024 /*
00025  * Define PLUGIN_TRACE to have the wrapper functions print
00026  * messages to stderr whenever they are called.
00027  */
00028 
00029 #ifdef PLUGIN_TRACE
00030 #include <stdio.h>
00031 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
00032 #else
00033 #define PLUGINDEBUGSTR(msg)
00034 #endif
00035 
00036 
00037 /***********************************************************************
00038  *
00039  * Globals
00040  *
00041  ***********************************************************************/
00042 
00043 static NPNetscapeFuncs   gNetscapeFuncs;    /* Netscape Function table */
00044 
00045 
00046 /***********************************************************************
00047  *
00048  * Wrapper functions : plugin calling Netscape Navigator
00049  *
00050  * These functions let the plugin developer just call the APIs
00051  * as documented and defined in npapi.h, without needing to know
00052  * about the function table and call macros in npupp.h.
00053  *
00054  ***********************************************************************/
00055 
00056 void
00057 NPN_Version(int* plugin_major, int* plugin_minor,
00058             int* netscape_major, int* netscape_minor)
00059 {
00060     *plugin_major = NP_VERSION_MAJOR;
00061     *plugin_minor = NP_VERSION_MINOR;
00062 
00063     /* Major version is in high byte */
00064     *netscape_major = gNetscapeFuncs.version >> 8;
00065     /* Minor version is in low byte */
00066     *netscape_minor = gNetscapeFuncs.version & 0xFF;
00067 }
00068 
00069 NPError
00070 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
00071 {
00072     return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
00073                     instance, variable, r_value);
00074 }
00075 
00076 NPError
00077 NPN_GetURL(NPP instance, const char* url, const char* window)
00078 {
00079     return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
00080 }
00081 
00082 /* nzc added this: */
00083 /* NPError */
00084 /* NPN_GetURLNotify(NPP instance, const char* url, const char* window, void * data) */
00085 /* { */
00086 //  return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturl, instance, url, data);
00087 /*  return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, data); */
00088 /* } */
00089 
00090 
00091 NPError
00092 NPN_PostURL(NPP instance, const char* url, const char* window,
00093          uint32 len, const char* buf, NPBool file)
00094 {
00095     return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
00096                     url, window, len, buf, file);
00097 }
00098 
00099 NPError
00100 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
00101 {
00102     return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
00103                     stream, rangeList);
00104 }
00105 
00106 NPError
00107 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
00108           NPStream** stream_ptr)
00109 {
00110     return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
00111                     type, window, stream_ptr);
00112 }
00113 
00114 int32
00115 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
00116 {
00117     return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
00118                     stream, len, buffer);
00119 }
00120 
00121 NPError
00122 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
00123 {
00124     return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
00125                         instance, stream, reason);
00126 }
00127 
00128 void
00129 NPN_Status(NPP instance, const char* message)
00130 {
00131     CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
00132 }
00133 
00134 const char*
00135 NPN_UserAgent(NPP instance)
00136 {
00137     return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
00138 }
00139 
00140 void*
00141 NPN_MemAlloc(uint32 size)
00142 {
00143     return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
00144 }
00145 
00146 void NPN_MemFree(void* ptr)
00147 {
00148     CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
00149 }
00150 
00151 uint32 NPN_MemFlush(uint32 size)
00152 {
00153     return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
00154 }
00155 
00156 void NPN_ReloadPlugins(NPBool reloadPages)
00157 {
00158     CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
00159 }
00160 
00161 JRIEnv* NPN_GetJavaEnv()
00162 {
00163     return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
00164 }
00165 
00166 jref NPN_GetJavaPeer(NPP instance)
00167 {
00168     return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
00169                        instance);
00170 }
00171 
00172 /* nzc additions: */
00173 void NPN_ForceRedraw(NPP instance)
00174 {
00175     CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw,
00176                 instance);
00177 }
00178 
00179 
00180 void NPN_InvalidateRect(NPP instance, NPRect* invalidRect)
00181 {
00182     CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect,instance,invalidRect);
00183 }
00184 
00185 
00186 
00187 
00188 /***********************************************************************
00189  *
00190  * Wrapper functions : Netscape Navigator -> plugin
00191  *
00192  * These functions let the plugin developer just create the APIs
00193  * as documented and defined in npapi.h, without needing to 
00194  * install those functions in the function table or worry about
00195  * setting up globals for 68K plugins.
00196  *
00197  ***********************************************************************/
00198 
00199 NPError
00200 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
00201         int16 argc, char* argn[], char* argv[], NPSavedData* saved)
00202 {
00203     NPError ret;
00204     PLUGINDEBUGSTR("New");
00205     ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
00206     return ret; 
00207 }
00208 
00209 NPError
00210 Private_Destroy(NPP instance, NPSavedData** save)
00211 {
00212     PLUGINDEBUGSTR("Destroy");
00213     return NPP_Destroy(instance, save);
00214 }
00215 
00216 NPError
00217 Private_SetWindow(NPP instance, NPWindow* window)
00218 {
00219     NPError err;
00220     PLUGINDEBUGSTR("SetWindow");
00221     err = NPP_SetWindow(instance, window);
00222     return err;
00223 }
00224 
00225 NPError
00226 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
00227             NPBool seekable, uint16* stype)
00228 {
00229     NPError err;
00230     PLUGINDEBUGSTR("NewStream");
00231     err = NPP_NewStream(instance, type, stream, seekable, stype);
00232     return err;
00233 }
00234 
00235 int32
00236 Private_WriteReady(NPP instance, NPStream* stream)
00237 {
00238     unsigned int result;
00239     PLUGINDEBUGSTR("WriteReady");
00240     result = NPP_WriteReady(instance, stream);
00241     return result;
00242 }
00243 
00244 int32
00245 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
00246         void* buffer)
00247 {
00248     unsigned int result;
00249     PLUGINDEBUGSTR("Write");
00250     result = NPP_Write(instance, stream, offset, len, buffer);
00251     return result;
00252 }
00253 
00254 void
00255 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
00256 {
00257     PLUGINDEBUGSTR("StreamAsFile");
00258     NPP_StreamAsFile(instance, stream, fname);
00259 }
00260 
00261 
00262 NPError
00263 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
00264 {
00265     NPError err;
00266     PLUGINDEBUGSTR("DestroyStream");
00267     err = NPP_DestroyStream(instance, stream, reason);
00268     return err;
00269 }
00270 
00271 
00272 void
00273 Private_Print(NPP instance, NPPrint* platformPrint)
00274 {
00275     PLUGINDEBUGSTR("Print");
00276     NPP_Print(instance, platformPrint);
00277 }
00278 
00279 /*********************************************************************** 
00280  *
00281  * These functions are located automagically by netscape.
00282  *
00283  ***********************************************************************/
00284 
00285 /*
00286  * NP_GetMIMEDescription
00287  *  - Netscape needs to know about this symbol
00288  *  - Netscape uses the return value to identify when an object instance
00289  *    of this plugin should be created.
00290  */
00291 char *
00292 NP_GetMIMEDescription(void)
00293 {
00294     return NPP_GetMIMEDescription();
00295 }
00296 
00297 /*
00298  * NP_GetValue [optional]
00299  *  - Netscape needs to know about this symbol.
00300  *  - Interfaces with plugin to get values for predefined variables
00301  *    that the navigator needs.
00302  */
00303 NPError
00304 NP_GetValue(void *future, NPPVariable variable, void *value)
00305 {
00306     return NPP_GetValue(future, variable, value);
00307 }
00308 
00309 /*
00310  * NP_Initialize
00311  *  - Netscape needs to know about this symbol.
00312  *  - It calls this function after looking up its symbol before it
00313  *    is about to create the first ever object of this kind.
00314  *
00315  * PARAMETERS
00316  *    nsTable   - The netscape function table. If developers just use these
00317  *        wrappers, they dont need to worry about all these function
00318  *        tables.
00319  * RETURN
00320  *    pluginFuncs
00321  *      - This functions needs to fill the plugin function table
00322  *        pluginFuncs and return it. Netscape Navigator plugin
00323  *        library will use this function table to call the plugin.
00324  *
00325  */
00326 NPError
00327 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
00328 {
00329     NPError err = NPERR_NO_ERROR;
00330 
00331     PRBool supportsXEmbed = PR_FALSE;
00332     NPNToolkitType toolkit = 0;
00333 
00334     PLUGINDEBUGSTR("NP_Initialize");
00335     
00336     /* validate input parameters */
00337 
00338     if ((nsTable == NULL) || (pluginFuncs == NULL))
00339         err = NPERR_INVALID_FUNCTABLE_ERROR;
00340     
00341     /*
00342      * Check the major version passed in Netscape's function table.
00343      * We won't load if the major version is newer than what we expect.
00344      * Also check that the function tables passed in are big enough for
00345      * all the functions we need (they could be bigger, if Netscape added
00346      * new APIs, but that's OK with us -- we'll just ignore them).
00347      *
00348      */
00349 
00350     if (err == NPERR_NO_ERROR) {
00351         if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
00352             err = NPERR_INCOMPATIBLE_VERSION_ERROR;
00353         if (nsTable->size < sizeof(NPNetscapeFuncs))
00354             err = NPERR_INVALID_FUNCTABLE_ERROR;
00355         if (pluginFuncs->size < sizeof(NPPluginFuncs))      
00356             err = NPERR_INVALID_FUNCTABLE_ERROR;
00357     }
00358         
00359     
00360     if (err == NPERR_NO_ERROR) {
00361         /*
00362          * Copy all the fields of Netscape function table into our
00363          * copy so we can call back into Netscape later.  Note that
00364          * we need to copy the fields one by one, rather than assigning
00365          * the whole structure, because the Netscape function table
00366          * could actually be bigger than what we expect.
00367          */
00368         gNetscapeFuncs.version       = nsTable->version;
00369         gNetscapeFuncs.size          = nsTable->size;
00370         gNetscapeFuncs.posturl       = nsTable->posturl;
00371         gNetscapeFuncs.geturl        = nsTable->geturl;
00372 
00373         /* nzc added this: inside mozilla code, this is
00374          * rerouted to _geturlnotify, which in its turn is
00375          * rerouted to the proper cpp call */
00376 /*      gNetscapeFuncs.geturlnotify        = nsTable->geturlnotify; */
00377         
00378         gNetscapeFuncs.requestread   = nsTable->requestread;
00379         gNetscapeFuncs.newstream     = nsTable->newstream;
00380         gNetscapeFuncs.write         = nsTable->write;
00381         gNetscapeFuncs.destroystream = nsTable->destroystream;
00382         gNetscapeFuncs.status        = nsTable->status;
00383         gNetscapeFuncs.uagent        = nsTable->uagent;
00384         gNetscapeFuncs.memalloc      = nsTable->memalloc;
00385         gNetscapeFuncs.memfree       = nsTable->memfree;
00386         gNetscapeFuncs.memflush      = nsTable->memflush;
00387         gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
00388         gNetscapeFuncs.getJavaEnv    = nsTable->getJavaEnv;
00389         gNetscapeFuncs.getJavaPeer   = nsTable->getJavaPeer;
00390         gNetscapeFuncs.getvalue      = nsTable->getvalue;
00391 
00392         /* moz has them... */
00393         gNetscapeFuncs.forceredraw   = nsTable->forceredraw;
00394         gNetscapeFuncs.invalidaterect= nsTable->invalidaterect;
00395 
00396         
00397         /*
00398          * Set up the plugin function table that Netscape will use to
00399          * call us.  Netscape needs to know about our version and size
00400          * and have a UniversalProcPointer for every function we
00401          * implement.
00402          */
00403         pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
00404         pluginFuncs->size       = sizeof(NPPluginFuncs);
00405         pluginFuncs->newp       = NewNPP_NewProc(Private_New);
00406         pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
00407         pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
00408         pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
00409         pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
00410         pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
00411         pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
00412         pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
00413         pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
00414         pluginFuncs->event      = NULL;
00415         pluginFuncs->javaClass  = NULL;
00416 
00417         err = CallNPN_GetValueProc(gNetscapeFuncs.getvalue, NULL,
00418                     NPNVSupportsXEmbedBool,
00419                     (void *)&supportsXEmbed);
00420         
00421         if (err != NPERR_NO_ERROR || supportsXEmbed != PR_TRUE)
00422             return NPERR_INCOMPATIBLE_VERSION_ERROR;
00423         
00424         err = CallNPN_GetValueProc(gNetscapeFuncs.getvalue, NULL,
00425                     NPNVToolkit,
00426                     (void *)&toolkit);
00427         
00428         if (err != NPERR_NO_ERROR || toolkit != NPNVGtk2)
00429             return NPERR_INCOMPATIBLE_VERSION_ERROR;
00430     
00431 
00432 
00433         err = NPP_Initialize();
00434     }
00435     
00436     return err;
00437 }
00438 
00439 /*
00440  * NP_Shutdown [optional]
00441  *  - Netscape needs to know about this symbol.
00442  *  - It calls this function after looking up its symbol after
00443  *    the last object of this kind has been destroyed.
00444  *
00445  */
00446 NPError
00447 NP_Shutdown(void)
00448 {
00449     PLUGINDEBUGSTR("NP_Shutdown");
00450     NPP_Shutdown();
00451     return NPERR_NO_ERROR;
00452 }