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