~ubuntu-branches/ubuntu/utopic/gnome-chemistry-utils/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/716961_fix_plugin_mime_typo.patch/mozilla-plugin/moz-plugin.c

  • Committer: Package Import Robot
  • Author(s): Logan Rosen
  • Date: 2014-01-21 14:29:57 UTC
  • mfrom: (2.1.29 sid)
  • Revision ID: package-import@ubuntu.com-20140121142957-7ey4iswdlks1ayhx
Tags: 0.14.6-1ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/control: Build-depend on firefox-dev instead of xulrunner-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Gnome Chemisty Utils
3
 
 * moz-plugin.c
4
 
 *
5
 
 * Copyright (C) 2002-2011 Jean Bréfort <jean.brefort@normalesup.org>
6
 
 *
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.
11
 
 *
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.
16
 
 *
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
20
 
 * USA
21
 
 */
22
 
 
23
 
#include <config.h>
24
 
#include "npapi.h"
25
 
#ifdef HAVE_NPFUNCTIONS_H
26
 
#   include "npfunctions.h"
27
 
#else
28
 
#   include "npupp.h"
29
 
#endif
30
 
#include <unistd.h>
31
 
#include <string.h>
32
 
#include <glib.h>
33
 
 
34
 
static NPNetscapeFuncs mozilla_funcs;
35
 
static int pid = 0;
36
 
static int to_pipe = 0;
37
 
 
38
 
typedef struct {
39
 
 
40
 
        char *url;      /* The URL that this instance displays */
41
 
 
42
 
        char *mime_type;        /* The mime type that this instance displays */
43
 
 
44
 
        int width, height;      /* The size of the display area */
45
 
 
46
 
        unsigned long moz_xid;  /* The XID of the window mozilla has for us */
47
 
 
48
 
/*      int argc;
49
 
        char **args;
50
 
        pthread_t thread;*/
51
 
        NPP instance;
52
 
 
53
 
        NPStream *m_Stream;
54
 
        bool m_ExpectingStream;
55
 
} ChemPlugin;
56
 
 
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[],
60
 
#else
61
 
                                 G_GNUC_UNUSED uint16 mode, uint16 argc, char *argn[], char *argv[],
62
 
#endif
63
 
                                 G_GNUC_UNUSED NPSavedData *saved)
64
 
{
65
 
        ChemPlugin *plugin;
66
 
        char buf [32];
67
 
        int i;
68
 
 
69
 
        if (instance == NULL)
70
 
                return NPERR_INVALID_INSTANCE_ERROR;
71
 
 
72
 
        instance->pdata = mozilla_funcs.memalloc (sizeof (ChemPlugin));
73
 
        plugin = (ChemPlugin *) instance->pdata;
74
 
 
75
 
        if (plugin == NULL)
76
 
                return NPERR_OUT_OF_MEMORY_ERROR;
77
 
        memset (plugin, 0, sizeof (ChemPlugin));
78
 
 
79
 
        plugin->instance = instance;
80
 
 
81
 
        if (pid == 0) {
82
 
                int p[2];
83
 
                char *argv[2];
84
 
                argv[0] = LIBEXECDIR"/chem-viewer";
85
 
                argv[1] = NULL;
86
 
 
87
 
                if (pipe (p) < 0) {
88
 
                        perror ("pipe creation");
89
 
                        return NPERR_INVALID_INSTANCE_ERROR;
90
 
                }
91
 
                if ((pid = fork()) < 0) {
92
 
                        perror ("fork");
93
 
                        return NPERR_INVALID_INSTANCE_ERROR;
94
 
                } else if (pid > 0) {
95
 
                        close (p[0]);
96
 
                        to_pipe = p[1];
97
 
                } else {
98
 
                        close (p[1]);
99
 
                        if (p[0] != STDIN_FILENO){
100
 
                                if (dup2 (p[0], STDIN_FILENO) != STDIN_FILENO) {
101
 
                                        perror("dup2 (stdin)");
102
 
                                }
103
 
                                close (p[0]);
104
 
                        }
105
 
                        if (execvp (argv[0], argv) < 0) {
106
 
                                perror ("execvp");
107
 
                        }
108
 
                }
109
 
        }
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);
115
 
        i = 0;
116
 
        while (i < argc && strcmp (argn[i++], "PARAM"));
117
 
        for (; i < argc; i++) {
118
 
                if (argn[i] == NULL || argv[i] == NULL)
119
 
                        break;
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);
124
 
        }
125
 
        write (to_pipe, "end\n", 4);
126
 
        plugin->m_Stream = NULL;
127
 
        plugin->m_ExpectingStream = true;
128
 
        return NPERR_NO_ERROR;
129
 
}
130
 
 
131
 
static NPError ChemDestroy (NPP instance, G_GNUC_UNUSED NPSavedData **save)
132
 
{
133
 
        ChemPlugin *plugin;
134
 
        char buf[32];
135
 
 
136
 
        if (instance == NULL)
137
 
                return NPERR_INVALID_INSTANCE_ERROR;
138
 
 
139
 
        plugin = (ChemPlugin *) instance->pdata;
140
 
        if (plugin == NULL)
141
 
                return NPERR_NO_ERROR;
142
 
 
143
 
        write (to_pipe, "kill\n", 5);
144
 
        snprintf (buf, 32, "%p\n", instance);
145
 
        write (to_pipe, buf, strlen (buf));
146
 
 
147
 
        mozilla_funcs.memfree (instance->pdata);
148
 
        instance->pdata = NULL;
149
 
        return NPERR_NO_ERROR;
150
 
}
151
 
 
152
 
static NPError ChemSetWindow (NPP instance, NPWindow *window)
153
 
{
154
 
        char buf[32];
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;
161
 
}
162
 
 
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)
166
 
#else
167
 
                      G_GNUC_UNUSED NPBool seekable, uint16 *stype)
168
 
#endif
169
 
{
170
 
        if (!stream || !stream->url)
171
 
                return NPERR_GENERIC_ERROR;
172
 
 
173
 
        ChemPlugin *plugin;
174
 
 
175
 
        if (instance == NULL)
176
 
                return NPERR_INVALID_INSTANCE_ERROR;
177
 
 
178
 
        plugin = (ChemPlugin *) instance->pdata;
179
 
        if (plugin == NULL)
180
 
                return NPERR_INVALID_INSTANCE_ERROR;
181
 
 
182
 
        if (plugin->m_Stream || !plugin->m_ExpectingStream)
183
 
                return mozilla_funcs.destroystream (instance, stream, NPRES_DONE);
184
 
 
185
 
        plugin->m_ExpectingStream = false;
186
 
        plugin->m_Stream = stream;
187
 
 
188
 
        *stype = NP_ASFILEONLY;
189
 
 
190
 
        return NPERR_NO_ERROR;
191
 
}
192
 
 
193
 
static NPError ChemDestroyStream (G_GNUC_UNUSED NPP instance, G_GNUC_UNUSED NPStream* stream, G_GNUC_UNUSED NPReason reason)
194
 
{
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;
202
 
}
203
 
 
204
 
static void ChemPrint (G_GNUC_UNUSED NPP instance, G_GNUC_UNUSED NPPrint *platformPrint)
205
 
{
206
 
// TODO: implement !!!
207
 
}
208
 
 
209
 
static void ChemStreamAsFile (NPP instance, G_GNUC_UNUSED NPStream *stream, const char *fname)
210
 
{
211
 
        char buf[32];
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);
217
 
}
218
 
 
219
 
static NPError ChemGetValue (NPP instance, NPPVariable variable, void *value)
220
 
{
221
 
        if (instance == NULL)
222
 
                return NPERR_INVALID_INSTANCE_ERROR;
223
 
 
224
 
        /* See NPPVariable in npapi.h */
225
 
        switch (variable) {
226
 
        case NPPVpluginNameString:
227
 
                * ((char const **) value) = "Gnome Chemistry Utils "VERSION".";
228
 
                break;
229
 
        case NPPVpluginDescriptionString:
230
 
                *((char const **) value) =  "Gnome Chemistry Utils "VERSION". Chemical structures display.";
231
 
                break;
232
 
        case NPPVpluginNeedsXEmbed:
233
 
                *((NPBool *) value) = TRUE;
234
 
                break;
235
 
        case NPPVpluginScriptableNPObject:
236
 
        value = NULL;
237
 
                break;
238
 
        default:
239
 
                return NPERR_INVALID_PARAM;
240
 
                break;
241
 
        }
242
 
 
243
 
        return NPERR_NO_ERROR;
244
 
}
245
 
 
246
 
NPError NP_GetValue (G_GNUC_UNUSED void *future, NPPVariable variable, void *value)
247
 
{
248
 
        switch (variable) {
249
 
        case NPPVpluginNameString:
250
 
                *((char **) value) = "Gnome Chemistry Utils";
251
 
                break;
252
 
        case NPPVpluginDescriptionString:
253
 
                *((char **) value) =
254
 
                        "Gnome Chemistry Utils "VERSION". "
255
 
                        "Chemical structures display.";
256
 
                break;
257
 
        default:
258
 
                return NPERR_GENERIC_ERROR;
259
 
        }
260
 
        return NPERR_NO_ERROR;
261
 
}
262
 
 
263
 
#ifdef MOZILLA_USES_CONST_CHAR
264
 
char const *NP_GetMIMEDescription (void)
265
 
#else
266
 
char *NP_GetMIMEDescription (void)
267
 
#endif
268
 
{
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;");
278
 
}
279
 
 
280
 
/* This is called to initialise the plugin
281
 
 */
282
 
NPError NP_Initialize(NPNetscapeFuncs *mozFuncs, NPPluginFuncs *pluginFuncs) {
283
 
        if (mozFuncs == NULL || pluginFuncs == NULL)
284
 
                return NPERR_INVALID_FUNCTABLE_ERROR;
285
 
 
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;
292
 
 
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;
310
 
#else
311
 
 
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);
325
 
#endif
326
 
        pluginFuncs->javaClass  = NULL;
327
 
 
328
 
        return NPERR_NO_ERROR;
329
 
}
330
 
 
331
 
NPError NP_Shutdown(void)
332
 
{
333
 
        /* stop the server and close all resources */
334
 
        write (to_pipe, "halt\n", 5);
335
 
        pid = 0;
336
 
        close (to_pipe);
337
 
        to_pipe = 0;
338
 
        return NPERR_NO_ERROR;
339
 
}