merge the formfield patch from ooo-build
[ooovba.git] / np_sdk / mozsrc / npunix.c
blobbc1424a654461e287eff67db459637e3dee2a6fb
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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.
13 * The Original Code is mozilla.org code.
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.
20 * Contributor(s):
21 * Stephen Mak <smak@sun.com>
25 * npunix.c
27 * Netscape Client Plugin API
28 * - Wrapper function to interface with the Netscape Navigator
30 * dp Suresh <dp@netscape.com>
32 *----------------------------------------------------------------------
33 * PLUGIN DEVELOPERS:
34 * YOU WILL NOT NEED TO EDIT THIS FILE.
35 *----------------------------------------------------------------------
38 #define XP_UNIX 1
40 #include <stdio.h>
41 #include "npapi.h"
42 #include "npupp.h"
45 * Define PLUGIN_TRACE to have the wrapper functions print
46 * messages to stderr whenever they are called.
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
57 /***********************************************************************
59 * Globals
61 ***********************************************************************/
63 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
66 /***********************************************************************
68 * Wrapper functions : plugin calling Netscape Navigator
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.
74 ***********************************************************************/
76 void
77 NPN_Version(int* plugin_major, int* plugin_minor,
78 int* netscape_major, int* netscape_minor)
80 *plugin_major = NP_VERSION_MAJOR;
81 *plugin_minor = NP_VERSION_MINOR;
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;
89 NPError
90 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
92 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
93 instance, variable, r_value);
96 NPError
97 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
99 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
100 instance, variable, value);
103 NPError
104 NPN_GetURL(NPP instance, const char* url, const char* window)
106 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
109 NPError
110 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
112 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
115 NPError
116 NPN_PostURL(NPP instance, const char* url, const char* window,
117 uint32 len, const char* buf, NPBool file)
119 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
120 url, window, len, buf, file);
123 NPError
124 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
125 const char* buf, NPBool file, void* notifyData)
127 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
128 instance, url, window, len, buf, file, notifyData);
131 NPError
132 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
134 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
135 stream, rangeList);
138 NPError
139 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
140 NPStream** stream_ptr)
142 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
143 type, window, stream_ptr);
146 int32
147 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
149 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
150 stream, len, buffer);
153 NPError
154 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
156 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
157 instance, stream, reason);
160 void
161 NPN_Status(NPP instance, const char* message)
163 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
166 const char*
167 NPN_UserAgent(NPP instance)
169 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
172 void*
173 NPN_MemAlloc(uint32 size)
175 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
178 void NPN_MemFree(void* ptr)
180 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
183 uint32 NPN_MemFlush(uint32 size)
185 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
188 void NPN_ReloadPlugins(NPBool reloadPages)
190 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
193 #ifdef OJI
194 JRIEnv* NPN_GetJavaEnv()
196 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
199 jref NPN_GetJavaPeer(NPP instance)
201 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
202 instance);
204 #endif
206 void
207 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
209 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
210 invalidRect);
213 void
214 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
216 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
217 invalidRegion);
220 void
221 NPN_ForceRedraw(NPP instance)
223 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
228 /***********************************************************************
230 * Wrapper functions : Netscape Navigator -> plugin
232 * These functions let the plugin developer just create the APIs
233 * as documented and defined in npapi.h, without needing to
234 * install those functions in the function table or worry about
235 * setting up globals for 68K plugins.
237 ***********************************************************************/
239 NPError
240 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
241 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
243 NPError ret;
244 PLUGINDEBUGSTR("New");
245 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
246 return ret;
249 NPError
250 Private_Destroy(NPP instance, NPSavedData** save)
252 PLUGINDEBUGSTR("Destroy");
253 return NPP_Destroy(instance, save);
256 NPError
257 Private_SetWindow(NPP instance, NPWindow* window)
259 NPError err;
260 PLUGINDEBUGSTR("SetWindow");
261 err = NPP_SetWindow(instance, window);
262 return err;
265 NPError
266 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
267 NPBool seekable, uint16* stype)
269 NPError err;
270 PLUGINDEBUGSTR("NewStream");
271 err = NPP_NewStream(instance, type, stream, seekable, stype);
272 return err;
275 int32
276 Private_WriteReady(NPP instance, NPStream* stream)
278 unsigned int result;
279 PLUGINDEBUGSTR("WriteReady");
280 result = NPP_WriteReady(instance, stream);
281 return result;
284 int32
285 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
286 void* buffer)
288 unsigned int result;
289 PLUGINDEBUGSTR("Write");
290 result = NPP_Write(instance, stream, offset, len, buffer);
291 return result;
294 void
295 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
297 PLUGINDEBUGSTR("StreamAsFile");
298 NPP_StreamAsFile(instance, stream, fname);
302 NPError
303 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
305 NPError err;
306 PLUGINDEBUGSTR("DestroyStream");
307 err = NPP_DestroyStream(instance, stream, reason);
308 return err;
311 void
312 Private_URLNotify(NPP instance, const char* url,
313 NPReason reason, void* notifyData)
316 PLUGINDEBUGSTR("URLNotify");
317 NPP_URLNotify(instance, url, reason, notifyData);
322 void
323 Private_Print(NPP instance, NPPrint* platformPrint)
325 PLUGINDEBUGSTR("Print");
326 NPP_Print(instance, platformPrint);
329 #ifdef OJI
330 JRIGlobalRef
331 Private_GetJavaClass(void)
333 jref clazz = NPP_GetJavaClass();
334 if (clazz) {
335 JRIEnv* env = NPN_GetJavaEnv();
336 return JRI_NewGlobalRef(env, clazz);
338 return NULL;
340 #endif
342 /***********************************************************************
344 * These functions are located automagically by netscape.
346 ***********************************************************************/
349 * NP_GetMIMEDescription
350 * - Netscape needs to know about this symbol
351 * - Netscape uses the return value to identify when an object instance
352 * of this plugin should be created.
354 char *
355 NP_GetMIMEDescription(void)
357 return NPP_GetMIMEDescription();
361 * NP_GetValue [optional]
362 * - Netscape needs to know about this symbol.
363 * - Interfaces with plugin to get values for predefined variables
364 * that the navigator needs.
366 NPError
367 NP_GetValue(NPP future, NPPVariable variable, void *value)
369 return NPP_GetValue(future, variable, value);
373 * NP_Initialize
374 * - Netscape needs to know about this symbol.
375 * - It calls this function after looking up its symbol before it
376 * is about to create the first ever object of this kind.
378 * PARAMETERS
379 * nsTable - The netscape function table. If developers just use these
380 * wrappers, they dont need to worry about all these function
381 * tables.
382 * RETURN
383 * pluginFuncs
384 * - This functions needs to fill the plugin function table
385 * pluginFuncs and return it. Netscape Navigator plugin
386 * library will use this function table to call the plugin.
389 NPError
390 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
392 NPError err = NPERR_NO_ERROR;
394 PLUGINDEBUGSTR("NP_Initialize");
396 /* validate input parameters */
398 if ((nsTable == NULL) || (pluginFuncs == NULL))
399 err = NPERR_INVALID_FUNCTABLE_ERROR;
402 * Check the major version passed in Netscape's function table.
403 * We won't load if the major version is newer than what we expect.
404 * Also check that the function tables passed in are big enough for
405 * all the functions we need (they could be bigger, if Netscape added
406 * new APIs, but that's OK with us -- we'll just ignore them).
410 if (err == NPERR_NO_ERROR) {
411 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
412 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
413 if (nsTable->size < sizeof(NPNetscapeFuncs))
414 err = NPERR_INVALID_FUNCTABLE_ERROR;
415 if (pluginFuncs->size < sizeof(NPPluginFuncs))
416 err = NPERR_INVALID_FUNCTABLE_ERROR;
420 if (err == NPERR_NO_ERROR) {
422 * Copy all the fields of Netscape function table into our
423 * copy so we can call back into Netscape later. Note that
424 * we need to copy the fields one by one, rather than assigning
425 * the whole structure, because the Netscape function table
426 * could actually be bigger than what we expect.
428 gNetscapeFuncs.version = nsTable->version;
429 gNetscapeFuncs.size = nsTable->size;
430 gNetscapeFuncs.posturl = nsTable->posturl;
431 gNetscapeFuncs.geturl = nsTable->geturl;
432 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
433 gNetscapeFuncs.requestread = nsTable->requestread;
434 gNetscapeFuncs.newstream = nsTable->newstream;
435 gNetscapeFuncs.write = nsTable->write;
436 gNetscapeFuncs.destroystream = nsTable->destroystream;
437 gNetscapeFuncs.status = nsTable->status;
438 gNetscapeFuncs.uagent = nsTable->uagent;
439 gNetscapeFuncs.memalloc = nsTable->memalloc;
440 gNetscapeFuncs.memfree = nsTable->memfree;
441 gNetscapeFuncs.memflush = nsTable->memflush;
442 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
443 #ifdef OJI
444 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
445 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
446 #endif
447 gNetscapeFuncs.getvalue = nsTable->getvalue;
450 * Set up the plugin function table that Netscape will use to
451 * call us. Netscape needs to know about our version and size
452 * and have a UniversalProcPointer for every function we
453 * implement.
455 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
456 pluginFuncs->size = sizeof(NPPluginFuncs);
457 pluginFuncs->newp = NewNPP_NewProc(Private_New);
458 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
459 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
460 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
461 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
462 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
463 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
464 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
465 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
466 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(Private_URLNotify);
467 pluginFuncs->event = NULL;
468 #ifdef OJI
469 pluginFuncs->javaClass = Private_GetJavaClass();
470 #endif
472 err = NPP_Initialize();
475 return err;
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.
485 NPError
486 NP_Shutdown(void)
488 PLUGINDEBUGSTR("NP_Shutdown");
489 NPP_Shutdown();
490 return NPERR_NO_ERROR;