5
* Copyright (C) 2002-2011 Jean Bréfort <jean.brefort@normalesup.org>
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 3 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25
#ifdef HAVE_NPFUNCTIONS_H
26
# include "npfunctions.h"
34
static NPNetscapeFuncs mozilla_funcs;
36
static int to_pipe = 0;
40
char *url; /* The URL that this instance displays */
42
char *mime_type; /* The mime type that this instance displays */
44
int width, height; /* The size of the display area */
46
unsigned long moz_xid; /* The XID of the window mozilla has for us */
54
bool m_ExpectingStream;
57
static NPError ChemNew (NPMIMEType mime_type, NPP instance,
58
#ifdef HAVE_NPFUNCTIONS_H
59
G_GNUC_UNUSED uint16_t mode, uint16_t argc, char *argn[], char *argv[],
61
G_GNUC_UNUSED uint16 mode, uint16 argc, char *argn[], char *argv[],
63
G_GNUC_UNUSED NPSavedData *saved)
70
return NPERR_INVALID_INSTANCE_ERROR;
72
instance->pdata = mozilla_funcs.memalloc (sizeof (ChemPlugin));
73
plugin = (ChemPlugin *) instance->pdata;
76
return NPERR_OUT_OF_MEMORY_ERROR;
77
memset (plugin, 0, sizeof (ChemPlugin));
79
plugin->instance = instance;
84
argv[0] = LIBEXECDIR"/chem-viewer";
88
perror ("pipe creation");
89
return NPERR_INVALID_INSTANCE_ERROR;
91
if ((pid = fork()) < 0) {
93
return NPERR_INVALID_INSTANCE_ERROR;
99
if (p[0] != STDIN_FILENO){
100
if (dup2 (p[0], STDIN_FILENO) != STDIN_FILENO) {
101
perror("dup2 (stdin)");
105
if (execvp (argv[0], argv) < 0) {
110
write (to_pipe, "new\n", 4);
111
snprintf (buf, 32, "%p\n", instance);
112
write (to_pipe, buf, strlen (buf));
113
write (to_pipe, mime_type, strlen ((char*) mime_type));
114
write (to_pipe, "\n", 1);
116
while (i < argc && strcmp (argn[i++], "PARAM"));
117
for (; i < argc; i++) {
118
if (argn[i] == NULL || argv[i] == NULL)
120
write (to_pipe, argn[i], strlen (argn[i]));
121
write (to_pipe, "\n", 1);
122
write (to_pipe, argv[i], strlen (argv[i]));
123
write (to_pipe, "\n", 1);
125
write (to_pipe, "end\n", 4);
126
plugin->m_Stream = NULL;
127
plugin->m_ExpectingStream = true;
128
return NPERR_NO_ERROR;
131
static NPError ChemDestroy (NPP instance, G_GNUC_UNUSED NPSavedData **save)
136
if (instance == NULL)
137
return NPERR_INVALID_INSTANCE_ERROR;
139
plugin = (ChemPlugin *) instance->pdata;
141
return NPERR_NO_ERROR;
143
write (to_pipe, "kill\n", 5);
144
snprintf (buf, 32, "%p\n", instance);
145
write (to_pipe, buf, strlen (buf));
147
mozilla_funcs.memfree (instance->pdata);
148
instance->pdata = NULL;
149
return NPERR_NO_ERROR;
152
static NPError ChemSetWindow (NPP instance, NPWindow *window)
155
write (to_pipe, "win\n", 4);
156
snprintf (buf, 32, "%p\n", instance);
157
write (to_pipe, buf, strlen (buf));
158
snprintf (buf, 32, "%p\n", window->window);
159
write (to_pipe, buf, strlen (buf));
160
return NPERR_NO_ERROR;
163
static NPError ChemNewStream (NPP instance, G_GNUC_UNUSED NPMIMEType type, G_GNUC_UNUSED NPStream *stream,
164
#ifdef HAVE_NPFUNCTIONS_H
165
G_GNUC_UNUSED NPBool seekable, uint16_t *stype)
167
G_GNUC_UNUSED NPBool seekable, uint16 *stype)
170
if (!stream || !stream->url)
171
return NPERR_GENERIC_ERROR;
175
if (instance == NULL)
176
return NPERR_INVALID_INSTANCE_ERROR;
178
plugin = (ChemPlugin *) instance->pdata;
180
return NPERR_INVALID_INSTANCE_ERROR;
182
if (plugin->m_Stream || !plugin->m_ExpectingStream)
183
return mozilla_funcs.destroystream (instance, stream, NPRES_DONE);
185
plugin->m_ExpectingStream = false;
186
plugin->m_Stream = stream;
188
*stype = NP_ASFILEONLY;
190
return NPERR_NO_ERROR;
193
static NPError ChemDestroyStream (G_GNUC_UNUSED NPP instance, G_GNUC_UNUSED NPStream* stream, G_GNUC_UNUSED NPReason reason)
195
if (instance == NULL)
196
return NPERR_INVALID_INSTANCE_ERROR;
197
ChemPlugin *plugin = (ChemPlugin *) instance->pdata;
198
if (!plugin->m_Stream || plugin->m_Stream != stream)
199
return NPERR_GENERIC_ERROR;
200
plugin->m_Stream = NULL;
201
return NPERR_NO_ERROR;
204
static void ChemPrint (G_GNUC_UNUSED NPP instance, G_GNUC_UNUSED NPPrint *platformPrint)
206
// TODO: implement !!!
209
static void ChemStreamAsFile (NPP instance, G_GNUC_UNUSED NPStream *stream, const char *fname)
212
write (to_pipe, "file\n", 5);
213
snprintf (buf, 32, "%p\n", instance);
214
write (to_pipe, buf, strlen (buf));
215
write (to_pipe, fname, strlen (fname));
216
write (to_pipe, "\n", 1);
219
static NPError ChemGetValue (NPP instance, NPPVariable variable, void *value)
221
if (instance == NULL)
222
return NPERR_INVALID_INSTANCE_ERROR;
224
/* See NPPVariable in npapi.h */
226
case NPPVpluginNameString:
227
* ((char const **) value) = "Gnome Chemistry Utils "VERSION".";
229
case NPPVpluginDescriptionString:
230
*((char const **) value) = "Gnome Chemistry Utils "VERSION". Chemical structures display.";
232
case NPPVpluginNeedsXEmbed:
233
*((NPBool *) value) = TRUE;
235
case NPPVpluginScriptableNPObject:
239
return NPERR_INVALID_PARAM;
243
return NPERR_NO_ERROR;
246
NPError NP_GetValue (G_GNUC_UNUSED void *future, NPPVariable variable, void *value)
249
case NPPVpluginNameString:
250
*((char **) value) = "Gnome Chemistry Utils";
252
case NPPVpluginDescriptionString:
254
"Gnome Chemistry Utils "VERSION". "
255
"Chemical structures display.";
258
return NPERR_GENERIC_ERROR;
260
return NPERR_NO_ERROR;
263
#ifdef MOZILLA_USES_CONST_CHAR
264
char const *NP_GetMIMEDescription (void)
266
char *NP_GetMIMEDescription (void)
269
return ("chemical/x-xyz:xyz:XYZ Coordinate Format;"
270
"chemical/x-mdl-molfile:mol:MDL Molfile;"
271
"chemical/x-pdb:pdb,ent:Protein DataBank;"
272
"chemical/x-jcamp-dx:dx,jdx:JCAMP Spectroscopic Data Exchange Format;"
273
"application/x-gcrystal:gcrystal:Crystalline structure model;"
274
"chemical/x-cif:cif:Crystallographic Information File;"
275
"application/x-gchempaint:gchempaint:2D chemical structures"
276
"chemical/x-cdx:cdx:ChemDraw binary CDX format;"
277
"chemical/x-cdxml:cdxml:ChemDraw CDXML format;");
280
/* This is called to initialise the plugin
282
NPError NP_Initialize(NPNetscapeFuncs *mozFuncs, NPPluginFuncs *pluginFuncs) {
283
if (mozFuncs == NULL || pluginFuncs == NULL)
284
return NPERR_INVALID_FUNCTABLE_ERROR;
286
if ((mozFuncs->version >> 8) > NP_VERSION_MAJOR)
287
return NPERR_INCOMPATIBLE_VERSION_ERROR;
288
if (mozFuncs->size < sizeof (NPNetscapeFuncs))
289
return NPERR_INVALID_FUNCTABLE_ERROR;
290
if (pluginFuncs->size < sizeof (NPPluginFuncs))
291
return NPERR_INVALID_FUNCTABLE_ERROR;
293
memcpy (&mozilla_funcs, mozFuncs, sizeof (NPNetscapeFuncs));
294
pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
295
pluginFuncs->size = sizeof (NPPluginFuncs);
296
#ifdef HAVE_NPFUNCTIONS_H
297
pluginFuncs->newp = (NPP_NewProcPtr)ChemNew;
298
pluginFuncs->destroy = (NPP_DestroyProcPtr) ChemDestroy;
299
pluginFuncs->setwindow = (NPP_SetWindowProcPtr) ChemSetWindow;
300
pluginFuncs->newstream = (NPP_NewStreamProcPtr) ChemNewStream;
301
pluginFuncs->destroystream = (NPP_DestroyStreamProcPtr) ChemDestroyStream;
302
pluginFuncs->asfile = (NPP_StreamAsFileProcPtr) ChemStreamAsFile;
303
pluginFuncs->writeready = NULL;
304
pluginFuncs->write = NULL;
305
pluginFuncs->print = (NPP_PrintProcPtr) ChemPrint;
306
pluginFuncs->urlnotify = NULL;
307
pluginFuncs->event = NULL;
308
pluginFuncs->getvalue = (NPP_GetValueProcPtr) ChemGetValue;
309
pluginFuncs->setvalue = NULL;//(NPP_SetValueProcPtr) ChemSetValue;
312
pluginFuncs->newp = NewNPP_NewProc (ChemNew);
313
pluginFuncs->destroy = NewNPP_DestroyProc (ChemDestroy);
314
pluginFuncs->setwindow = NewNPP_SetWindowProc (ChemSetWindow);
315
pluginFuncs->newstream = NewNPP_NewStreamProc (ChemNewStream);
316
pluginFuncs->destroystream = NewNPP_DestroyStreamProc (ChemDestroyStream);
317
pluginFuncs->asfile = NewNPP_StreamAsFileProc (ChemStreamAsFile);
318
pluginFuncs->writeready = NULL;
319
pluginFuncs->write = NULL;
320
pluginFuncs->print = NewNPP_PrintProc (ChemPrint);#endif
321
pluginFuncs->urlnotify = NULL;
322
pluginFuncs->event = NULL;
323
pluginFuncs->getvalue = NewNPP_GetValueProc (ChemGetValue);
324
pluginFuncs->setvalue = NULL;//NewNPP_SetValueProc (ChemSetValue);
326
pluginFuncs->javaClass = NULL;
328
return NPERR_NO_ERROR;
331
NPError NP_Shutdown(void)
333
/* stop the server and close all resources */
334
write (to_pipe, "halt\n", 5);
338
return NPERR_NO_ERROR;