merged tag ooo/OOO330_m14
[LibreOffice.git] / np_sdk / mozsrc / npunix.c
blob55d0fa5316d22eb1bca826f4b512f44d970da829
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Stephen Mak <smak@sun.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * npunix.c
43 * Netscape Client Plugin API
44 * - Wrapper function to interface with the Netscape Navigator
46 * dp Suresh <dp@netscape.com>
48 *----------------------------------------------------------------------
49 * PLUGIN DEVELOPERS:
50 * YOU WILL NOT NEED TO EDIT THIS FILE.
51 *----------------------------------------------------------------------
54 #define XP_UNIX 1
56 #include <stdio.h>
57 #include "npapi.h"
58 #include "npupp.h"
61 * Define PLUGIN_TRACE to have the wrapper functions print
62 * messages to stderr whenever they are called.
65 #ifdef PLUGIN_TRACE
66 #include <stdio.h>
67 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
68 #else
69 #define PLUGINDEBUGSTR(msg)
70 #endif
73 /***********************************************************************
75 * Globals
77 ***********************************************************************/
79 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
82 /***********************************************************************
84 * Wrapper functions : plugin calling Netscape Navigator
86 * These functions let the plugin developer just call the APIs
87 * as documented and defined in npapi.h, without needing to know
88 * about the function table and call macros in npupp.h.
90 ***********************************************************************/
92 void
93 NPN_Version(int* plugin_major, int* plugin_minor,
94 int* netscape_major, int* netscape_minor)
96 *plugin_major = NP_VERSION_MAJOR;
97 *plugin_minor = NP_VERSION_MINOR;
99 /* Major version is in high byte */
100 *netscape_major = gNetscapeFuncs.version >> 8;
101 /* Minor version is in low byte */
102 *netscape_minor = gNetscapeFuncs.version & 0xFF;
105 NPError
106 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
108 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
109 instance, variable, r_value);
112 NPError
113 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
115 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
116 instance, variable, value);
119 NPError
120 NPN_GetURL(NPP instance, const char* url, const char* window)
122 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
125 NPError
126 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
128 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
131 NPError
132 NPN_PostURL(NPP instance, const char* url, const char* window,
133 uint32 len, const char* buf, NPBool file)
135 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
136 url, window, len, buf, file);
139 NPError
140 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
141 const char* buf, NPBool file, void* notifyData)
143 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
144 instance, url, window, len, buf, file, notifyData);
147 NPError
148 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
150 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
151 stream, rangeList);
154 NPError
155 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
156 NPStream** stream_ptr)
158 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
159 type, window, stream_ptr);
162 int32
163 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
165 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
166 stream, len, buffer);
169 NPError
170 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
172 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
173 instance, stream, reason);
176 void
177 NPN_Status(NPP instance, const char* message)
179 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
182 const char*
183 NPN_UserAgent(NPP instance)
185 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
188 void*
189 NPN_MemAlloc(uint32 size)
191 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
194 void NPN_MemFree(void* ptr)
196 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
199 uint32 NPN_MemFlush(uint32 size)
201 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
204 void NPN_ReloadPlugins(NPBool reloadPages)
206 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
209 #ifdef OJI
210 JRIEnv* NPN_GetJavaEnv()
212 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
215 jref NPN_GetJavaPeer(NPP instance)
217 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
218 instance);
220 #endif
222 void
223 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
225 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
226 invalidRect);
229 void
230 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
232 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
233 invalidRegion);
236 void
237 NPN_ForceRedraw(NPP instance)
239 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
242 /***********************************************************************
244 * Wrapper functions : Netscape Navigator -> plugin
246 * These functions let the plugin developer just create the APIs
247 * as documented and defined in npapi.h, without needing to
248 * install those functions in the function table or worry about
249 * setting up globals for 68K plugins.
251 ***********************************************************************/
253 NPError
254 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
255 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
257 NPError ret;
258 PLUGINDEBUGSTR("New");
259 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
260 return ret;
263 NPError
264 Private_Destroy(NPP instance, NPSavedData** save)
266 PLUGINDEBUGSTR("Destroy");
267 return NPP_Destroy(instance, save);
270 NPError
271 Private_SetWindow(NPP instance, NPWindow* window)
273 NPError err;
274 PLUGINDEBUGSTR("SetWindow");
275 err = NPP_SetWindow(instance, window);
276 return err;
279 NPError
280 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
281 NPBool seekable, uint16* stype)
283 NPError err;
284 PLUGINDEBUGSTR("NewStream");
285 err = NPP_NewStream(instance, type, stream, seekable, stype);
286 return err;
289 int32
290 Private_WriteReady(NPP instance, NPStream* stream)
292 unsigned int result;
293 PLUGINDEBUGSTR("WriteReady");
294 result = NPP_WriteReady(instance, stream);
295 return result;
298 int32
299 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
300 void* buffer)
302 unsigned int result;
303 PLUGINDEBUGSTR("Write");
304 result = NPP_Write(instance, stream, offset, len, buffer);
305 return result;
308 void
309 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
311 PLUGINDEBUGSTR("StreamAsFile");
312 NPP_StreamAsFile(instance, stream, fname);
316 NPError
317 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
319 NPError err;
320 PLUGINDEBUGSTR("DestroyStream");
321 err = NPP_DestroyStream(instance, stream, reason);
322 return err;
325 void
326 Private_URLNotify(NPP instance, const char* url,
327 NPReason reason, void* notifyData)
330 PLUGINDEBUGSTR("URLNotify");
331 NPP_URLNotify(instance, url, reason, notifyData);
336 void
337 Private_Print(NPP instance, NPPrint* platformPrint)
339 PLUGINDEBUGSTR("Print");
340 NPP_Print(instance, platformPrint);
343 #ifdef OJI
344 JRIGlobalRef
345 Private_GetJavaClass(void)
347 jref clazz = NPP_GetJavaClass();
348 if (clazz) {
349 JRIEnv* env = NPN_GetJavaEnv();
350 return JRI_NewGlobalRef(env, clazz);
352 return NULL;
354 #endif
356 /***********************************************************************
358 * These functions are located automagically by netscape.
360 ***********************************************************************/
363 * NP_GetMIMEDescription
364 * - Netscape needs to know about this symbol
365 * - Netscape uses the return value to identify when an object instance
366 * of this plugin should be created.
368 char *
369 NP_GetMIMEDescription(void)
371 return NPP_GetMIMEDescription();
375 * NP_GetValue [optional]
376 * - Netscape needs to know about this symbol.
377 * - Interfaces with plugin to get values for predefined variables
378 * that the navigator needs.
380 NPError
381 NP_GetValue(void* future, NPPVariable variable, void *value)
383 return NPP_GetValue(future, variable, value);
387 * NP_Initialize
388 * - Netscape needs to know about this symbol.
389 * - It calls this function after looking up its symbol before it
390 * is about to create the first ever object of this kind.
392 * PARAMETERS
393 * nsTable - The netscape function table. If developers just use these
394 * wrappers, they dont need to worry about all these function
395 * tables.
396 * RETURN
397 * pluginFuncs
398 * - This functions needs to fill the plugin function table
399 * pluginFuncs and return it. Netscape Navigator plugin
400 * library will use this function table to call the plugin.
403 NPError
404 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
406 NPError err = NPERR_NO_ERROR;
408 PLUGINDEBUGSTR("NP_Initialize");
410 /* validate input parameters */
412 if ((nsTable == NULL) || (pluginFuncs == NULL))
413 err = NPERR_INVALID_FUNCTABLE_ERROR;
416 * Check the major version passed in Netscape's function table.
417 * We won't load if the major version is newer than what we expect.
418 * Also check that the function tables passed in are big enough for
419 * all the functions we need (they could be bigger, if Netscape added
420 * new APIs, but that's OK with us -- we'll just ignore them).
424 if (err == NPERR_NO_ERROR) {
425 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
426 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
427 if (nsTable->size < sizeof(NPNetscapeFuncs))
428 err = NPERR_INVALID_FUNCTABLE_ERROR;
429 if (pluginFuncs->size < sizeof(NPPluginFuncs))
430 err = NPERR_INVALID_FUNCTABLE_ERROR;
434 if (err == NPERR_NO_ERROR) {
436 * Copy all the fields of Netscape function table into our
437 * copy so we can call back into Netscape later. Note that
438 * we need to copy the fields one by one, rather than assigning
439 * the whole structure, because the Netscape function table
440 * could actually be bigger than what we expect.
442 gNetscapeFuncs.version = nsTable->version;
443 gNetscapeFuncs.size = nsTable->size;
444 gNetscapeFuncs.posturl = nsTable->posturl;
445 gNetscapeFuncs.geturl = nsTable->geturl;
446 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
447 gNetscapeFuncs.requestread = nsTable->requestread;
448 gNetscapeFuncs.newstream = nsTable->newstream;
449 gNetscapeFuncs.write = nsTable->write;
450 gNetscapeFuncs.destroystream = nsTable->destroystream;
451 gNetscapeFuncs.status = nsTable->status;
452 gNetscapeFuncs.uagent = nsTable->uagent;
453 gNetscapeFuncs.memalloc = nsTable->memalloc;
454 gNetscapeFuncs.memfree = nsTable->memfree;
455 gNetscapeFuncs.memflush = nsTable->memflush;
456 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
457 #ifdef OJI
458 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
459 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
460 #endif
461 gNetscapeFuncs.getvalue = nsTable->getvalue;
464 * Set up the plugin function table that Netscape will use to
465 * call us. Netscape needs to know about our version and size
466 * and have a UniversalProcPointer for every function we
467 * implement.
469 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
470 pluginFuncs->size = sizeof(NPPluginFuncs);
471 pluginFuncs->newp = NewNPP_NewProc(Private_New);
472 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
473 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
474 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
475 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
476 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
477 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
478 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
479 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
480 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(Private_URLNotify);
481 pluginFuncs->event = NULL;
482 #ifdef OJI
483 pluginFuncs->javaClass = Private_GetJavaClass();
484 #endif
486 err = NPP_Initialize();
489 return err;
493 * NP_Shutdown [optional]
494 * - Netscape needs to know about this symbol.
495 * - It calls this function after looking up its symbol after
496 * the last object of this kind has been destroyed.
499 NPError
500 NP_Shutdown(void)
502 PLUGINDEBUGSTR("NP_Shutdown");
503 NPP_Shutdown();
504 return NPERR_NO_ERROR;