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