]> git.sesse.net Git - vlc/blob - mozilla/support/npunix.c
* ./mozilla/*: moved support files in a separate directory.
[vlc] / mozilla / support / npunix.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * The contents of this file are subject to the Mozilla Public
4  * License Version 1.1 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of
6  * the License at http://www.mozilla.org/MPL/
7  * 
8  * Software distributed under the License is distributed on an "AS
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10  * implied. See the License for the specific language governing
11  * rights and limitations under the License.
12  * 
13  * The Original Code is mozilla.org code.
14  *
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation.  Portions created by Netscape are
17  * Copyright (C) 1998 Netscape Communications Corporation. All
18  * Rights Reserved.
19  *
20  * Contributor(s): 
21  * Stephen Mak <smak@sun.com>
22  */
23
24 /*
25  * npunix.c
26  *
27  * Netscape Client Plugin API
28  * - Wrapper function to interface with the Netscape Navigator
29  *
30  * dp Suresh <dp@netscape.com>
31  *
32  *----------------------------------------------------------------------
33  * PLUGIN DEVELOPERS:
34  *  YOU WILL NOT NEED TO EDIT THIS FILE.
35  *----------------------------------------------------------------------
36  */
37
38 #define XP_UNIX 1
39
40 #include <stdio.h>
41 #include "npapi.h"
42 #include "npupp.h"
43
44 /*
45  * Define PLUGIN_TRACE to have the wrapper functions print
46  * messages to stderr whenever they are called.
47  */
48
49 #ifdef PLUGIN_TRACE
50 #include <stdio.h>
51 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
52 #else
53 #define PLUGINDEBUGSTR(msg)
54 #endif
55
56
57 /***********************************************************************
58  *
59  * Globals
60  *
61  ***********************************************************************/
62
63 static NPNetscapeFuncs   gNetscapeFuncs;    /* Netscape Function table */
64
65
66 /***********************************************************************
67  *
68  * Wrapper functions : plugin calling Netscape Navigator
69  *
70  * These functions let the plugin developer just call the APIs
71  * as documented and defined in npapi.h, without needing to know
72  * about the function table and call macros in npupp.h.
73  *
74  ***********************************************************************/
75
76 void
77 NPN_Version(int* plugin_major, int* plugin_minor,
78          int* netscape_major, int* netscape_minor)
79 {
80     *plugin_major = NP_VERSION_MAJOR;
81     *plugin_minor = NP_VERSION_MINOR;
82
83     /* Major version is in high byte */
84     *netscape_major = gNetscapeFuncs.version >> 8;
85     /* Minor version is in low byte */
86     *netscape_minor = gNetscapeFuncs.version & 0xFF;
87 }
88
89 NPError
90 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
91 {
92     return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
93                     instance, variable, r_value);
94 }
95
96 NPError
97 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
98 {
99     return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
100                     instance, variable, value);
101 }
102
103 NPError
104 NPN_GetURL(NPP instance, const char* url, const char* window)
105 {
106     return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
107 }
108
109 NPError
110 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
111 {
112     return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
113 }
114
115 NPError
116 NPN_PostURL(NPP instance, const char* url, const char* window,
117          uint32 len, const char* buf, NPBool file)
118 {
119     return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
120                     url, window, len, buf, file);
121 }
122
123 NPError
124 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
125                   const char* buf, NPBool file, void* notifyData)
126 {
127     return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
128             instance, url, window, len, buf, file, notifyData);
129 }
130
131 NPError
132 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
133 {
134     return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
135                     stream, rangeList);
136 }
137
138 NPError
139 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
140           NPStream** stream_ptr)
141 {
142     return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
143                     type, window, stream_ptr);
144 }
145
146 int32
147 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
148 {
149     return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
150                     stream, len, buffer);
151 }
152
153 NPError
154 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
155 {
156     return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
157                         instance, stream, reason);
158 }
159
160 void
161 NPN_Status(NPP instance, const char* message)
162 {
163     CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
164 }
165
166 const char*
167 NPN_UserAgent(NPP instance)
168 {
169     return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
170 }
171
172 void*
173 NPN_MemAlloc(uint32 size)
174 {
175     return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
176 }
177
178 void NPN_MemFree(void* ptr)
179 {
180     CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
181 }
182
183 uint32 NPN_MemFlush(uint32 size)
184 {
185     return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
186 }
187
188 void NPN_ReloadPlugins(NPBool reloadPages)
189 {
190     CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
191 }
192
193 JRIEnv* NPN_GetJavaEnv()
194 {
195     return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
196 }
197
198 jref NPN_GetJavaPeer(NPP instance)
199 {
200     return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
201                        instance);
202 }
203
204 void
205 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
206 {
207     CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
208         invalidRect);
209 }
210
211 void
212 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
213 {
214     CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
215         invalidRegion);
216 }
217
218 void
219 NPN_ForceRedraw(NPP instance)
220 {
221     CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
222 }
223
224
225
226 /***********************************************************************
227  *
228  * Wrapper functions : Netscape Navigator -> plugin
229  *
230  * These functions let the plugin developer just create the APIs
231  * as documented and defined in npapi.h, without needing to 
232  * install those functions in the function table or worry about
233  * setting up globals for 68K plugins.
234  *
235  ***********************************************************************/
236
237 NPError
238 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
239         int16 argc, char* argn[], char* argv[], NPSavedData* saved)
240 {
241     NPError ret;
242     PLUGINDEBUGSTR("New");
243     ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
244     return ret; 
245 }
246
247 NPError
248 Private_Destroy(NPP instance, NPSavedData** save)
249 {
250     PLUGINDEBUGSTR("Destroy");
251     return NPP_Destroy(instance, save);
252 }
253
254 NPError
255 Private_SetWindow(NPP instance, NPWindow* window)
256 {
257     NPError err;
258     PLUGINDEBUGSTR("SetWindow");
259     err = NPP_SetWindow(instance, window);
260     return err;
261 }
262
263 NPError
264 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
265             NPBool seekable, uint16* stype)
266 {
267     NPError err;
268     PLUGINDEBUGSTR("NewStream");
269     err = NPP_NewStream(instance, type, stream, seekable, stype);
270     return err;
271 }
272
273 int32
274 Private_WriteReady(NPP instance, NPStream* stream)
275 {
276     unsigned int result;
277     PLUGINDEBUGSTR("WriteReady");
278     result = NPP_WriteReady(instance, stream);
279     return result;
280 }
281
282 int32
283 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
284         void* buffer)
285 {
286     unsigned int result;
287     PLUGINDEBUGSTR("Write");
288     result = NPP_Write(instance, stream, offset, len, buffer);
289     return result;
290 }
291
292 void
293 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
294 {
295     PLUGINDEBUGSTR("StreamAsFile");
296     NPP_StreamAsFile(instance, stream, fname);
297 }
298
299
300 NPError
301 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
302 {
303     NPError err;
304     PLUGINDEBUGSTR("DestroyStream");
305     err = NPP_DestroyStream(instance, stream, reason);
306     return err;
307 }
308
309 void
310 Private_URLNotify(NPP instance, const char* url,
311                 NPReason reason, void* notifyData)
312                 
313 {
314     PLUGINDEBUGSTR("URLNotify");
315     NPP_URLNotify(instance, url, reason, notifyData);
316 }
317
318
319
320 void
321 Private_Print(NPP instance, NPPrint* platformPrint)
322 {
323     PLUGINDEBUGSTR("Print");
324     NPP_Print(instance, platformPrint);
325 }
326
327 NPError
328 Private_GetValue(NPP instance, NPPVariable variable, void *r_value)
329 {
330     PLUGINDEBUGSTR("GetValue");
331 return NPP_GetValue(instance, variable, r_value);
332 }
333
334 JRIGlobalRef
335 Private_GetJavaClass(void)
336 {
337     jref clazz = NPP_GetJavaClass();
338     if (clazz) {
339     JRIEnv* env = NPN_GetJavaEnv();
340     return JRI_NewGlobalRef(env, clazz);
341     }
342     return NULL;
343 }
344
345 /*********************************************************************** 
346  *
347  * These functions are located automagically by netscape.
348  *
349  ***********************************************************************/
350
351 /*
352  * NP_GetMIMEDescription
353  *  - Netscape needs to know about this symbol
354  *  - Netscape uses the return value to identify when an object instance
355  *    of this plugin should be created.
356  */
357 char *
358 NP_GetMIMEDescription(void)
359 {
360     return NPP_GetMIMEDescription();
361 }
362
363 /*
364  * NP_GetValue [optional]
365  *  - Netscape needs to know about this symbol.
366  *  - Interfaces with plugin to get values for predefined variables
367  *    that the navigator needs.
368  */
369 NPError
370 NP_GetValue(NPP future, NPPVariable variable, void *value)
371 {
372     return NPP_GetValue(future, variable, value);
373 }
374
375 /*
376  * NP_Initialize
377  *  - Netscape needs to know about this symbol.
378  *  - It calls this function after looking up its symbol before it
379  *    is about to create the first ever object of this kind.
380  *
381  * PARAMETERS
382  *    nsTable   - The netscape function table. If developers just use these
383  *        wrappers, they dont need to worry about all these function
384  *        tables.
385  * RETURN
386  *    pluginFuncs
387  *      - This functions needs to fill the plugin function table
388  *        pluginFuncs and return it. Netscape Navigator plugin
389  *        library will use this function table to call the plugin.
390  *
391  */
392 NPError
393 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
394 {
395     NPError err = NPERR_NO_ERROR;
396
397     PLUGINDEBUGSTR("NP_Initialize");
398     
399     /* validate input parameters */
400
401     if ((nsTable == NULL) || (pluginFuncs == NULL))
402         err = NPERR_INVALID_FUNCTABLE_ERROR;
403     
404     /*
405      * Check the major version passed in Netscape's function table.
406      * We won't load if the major version is newer than what we expect.
407      * Also check that the function tables passed in are big enough for
408      * all the functions we need (they could be bigger, if Netscape added
409      * new APIs, but that's OK with us -- we'll just ignore them).
410      *
411      */
412
413     if (err == NPERR_NO_ERROR) {
414         if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
415             err = NPERR_INCOMPATIBLE_VERSION_ERROR;
416         if (nsTable->size < sizeof(NPNetscapeFuncs))
417             err = NPERR_INVALID_FUNCTABLE_ERROR;
418         if (pluginFuncs->size < sizeof(NPPluginFuncs))      
419             err = NPERR_INVALID_FUNCTABLE_ERROR;
420     }
421         
422     
423     if (err == NPERR_NO_ERROR) {
424         /*
425          * Copy all the fields of Netscape function table into our
426          * copy so we can call back into Netscape later.  Note that
427          * we need to copy the fields one by one, rather than assigning
428          * the whole structure, because the Netscape function table
429          * could actually be bigger than what we expect.
430          */
431         gNetscapeFuncs.version       = nsTable->version;
432         gNetscapeFuncs.size          = nsTable->size;
433         gNetscapeFuncs.posturl       = nsTable->posturl;
434         gNetscapeFuncs.geturl        = nsTable->geturl;
435         gNetscapeFuncs.geturlnotify  = nsTable->geturlnotify;
436         gNetscapeFuncs.requestread   = nsTable->requestread;
437         gNetscapeFuncs.newstream     = nsTable->newstream;
438         gNetscapeFuncs.write         = nsTable->write;
439         gNetscapeFuncs.destroystream = nsTable->destroystream;
440         gNetscapeFuncs.status        = nsTable->status;
441         gNetscapeFuncs.uagent        = nsTable->uagent;
442         gNetscapeFuncs.memalloc      = nsTable->memalloc;
443         gNetscapeFuncs.memfree       = nsTable->memfree;
444         gNetscapeFuncs.memflush      = nsTable->memflush;
445         gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
446         gNetscapeFuncs.getJavaEnv    = nsTable->getJavaEnv;
447         gNetscapeFuncs.getJavaPeer   = nsTable->getJavaPeer;
448         gNetscapeFuncs.getvalue      = nsTable->getvalue;
449
450         /*
451          * Set up the plugin function table that Netscape will use to
452          * call us.  Netscape needs to know about our version and size
453          * and have a UniversalProcPointer for every function we
454          * implement.
455          */
456         pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
457         pluginFuncs->size       = sizeof(NPPluginFuncs);
458         pluginFuncs->newp       = NewNPP_NewProc(Private_New);
459         pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
460         pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
461         pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
462         pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
463         pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
464         pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
465         pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
466         pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
467         pluginFuncs->urlnotify  = NewNPP_URLNotifyProc(Private_URLNotify);
468         pluginFuncs->event      = NULL;
469         pluginFuncs->javaClass  = Private_GetJavaClass();
470         pluginFuncs->getvalue   = NewNPP_GetValueProc(Private_GetValue);
471         
472         err = NPP_Initialize();
473     }
474     
475     return err;
476 }
477
478 /*
479  * NP_Shutdown [optional]
480  *  - Netscape needs to know about this symbol.
481  *  - It calls this function after looking up its symbol after
482  *    the last object of this kind has been destroyed.
483  *
484  */
485 NPError
486 NP_Shutdown(void)
487 {
488     PLUGINDEBUGSTR("NP_Shutdown");
489     NPP_Shutdown();
490     return NPERR_NO_ERROR;
491 }