1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
* Copyright (C) 2011, 2012 Dan Nicholson <dbn.lists@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#include <config.h>
#include "evbp-viewer.h"
#include "evbp-mime.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <npapi.h>
#include <npfunctions.h>
#include <prtypes.h>
#include <glib.h>
#include <gtk/gtk.h>
#if GTK_MAJOR_VERSION > 2
#include <gtk/gtkx.h>
#endif
#include <gdk/gdkx.h>
#include <evince-document.h>
#include <evince-view.h>
#define EVBP_VERSION PACKAGE_VERSION
#define EVBP_NAME "Evince Plugin"
typedef struct evbp_priv {
NPP npp;
GtkWidget *plug;
GtkWidget *viewer;
} evbp_priv_t;
static char *evbp_mime_string = NULL;
static char *evbp_description = NULL;
static const NPNetscapeFuncs *npn_funcs = NULL;
static gboolean ev_is_initialized = FALSE;
static void
no_debug(const gchar *domain, GLogLevelFlags level,
const gchar *message, gpointer data)
{
}
static void
setup_logging(void)
{
if (!getenv("EVBP_DEBUG"))
g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, no_debug, NULL);
}
static gboolean
ev_initialize(void)
{
g_debug("%s", __func__);
/* Some backends don't handle repeated init */
if (ev_is_initialized) {
g_debug("evince already initialized");
return TRUE;
} else
g_debug("initializing evince");
/* Init g_type and g_thread asap */
g_type_init();
if (!g_thread_supported())
g_thread_init(NULL);
/* Initialize evince */
if (!ev_init()) {
g_critical("evince failed to initialize");
return FALSE;
}
ev_is_initialized = TRUE;
return TRUE;
}
static gboolean
evbp_initialize(void)
{
g_debug("%s", __func__);
g_debug("Initializing plugin version %s built with evince-%u.%u.%u",
EVBP_VERSION, EV_MAJOR_VERSION, EV_MINOR_VERSION,
EV_MICRO_VERSION);
if (!ev_initialize())
return FALSE;
#ifdef ENABLE_NLS
/* Initialize the i18n stuff */
bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);
#endif
/* Create the description string */
evbp_description = g_strdup_printf("Evince Broswer Plugin %s"
" (using Evince %u)",
EVBP_VERSION,
EV_MAJOR_VERSION);
/* Initialize gtk and evince icons */
gtk_init(NULL, NULL);
ev_stock_icons_init();
return TRUE;
}
static void
evbp_shutdown(void)
{
g_debug("%s", __func__);
if (!ev_is_initialized)
return;
g_free(evbp_description);
ev_shutdown();
ev_stock_icons_shutdown();
ev_is_initialized = FALSE;
}
static NPError
evbp_new(NPMIMEType pluginType, NPP instance, uint16_t mode,
int16_t argc, char *argn[], char *argv[],
NPSavedData *saved)
{
evbp_priv_t *priv;
g_debug("%s", __func__);
/* create new private data */
priv = npn_funcs->memalloc(sizeof(*priv));
if (!priv) {
g_critical("failed to allocate plugin private");
return NPERR_OUT_OF_MEMORY_ERROR;
}
memset(priv, 0, sizeof(*priv));
/* link the structures */
priv->npp = instance;
instance->pdata = priv;
/* create new viewer */
priv->viewer = evbp_viewer_new();
gtk_widget_show(priv->viewer);
return NPERR_NO_ERROR;
}
static NPError
evbp_destroy(NPP instance, NPSavedData **save)
{
evbp_priv_t *priv = instance->pdata;
g_debug("%s", __func__);
if (priv->plug)
gtk_widget_destroy(priv->plug);
npn_funcs->memfree(instance->pdata);
instance->pdata = NULL;
return NPERR_NO_ERROR;
}
static NPError
evbp_set_window(NPP instance, NPWindow *window)
{
evbp_priv_t *priv = instance->pdata;
g_debug("%s", __func__);
/* Create a plug from the window id or just resize the existing one
* appropriately. */
if (priv->plug) {
g_debug("resizing existing plug to %ux%u", window->width,
window->height);
gtk_window_resize(GTK_WINDOW(priv->plug), window->width,
window->height);
} else {
Window id = (Window)window->window;
g_debug("plugging into window id %lu", (unsigned long)id);
priv->plug = gtk_plug_new(id);
gtk_container_add(GTK_CONTAINER(priv->plug), priv->viewer);
gtk_widget_show(priv->plug);
}
return NPERR_NO_ERROR;
}
static NPError
evbp_new_stream(NPP instance, NPMIMEType type, NPStream *stream,
NPBool seekable, uint16_t *stype)
{
g_debug("%s", __func__);
/* fetch the url to a local file only */
*stype = NP_ASFILEONLY;
return NPERR_NO_ERROR;
}
static NPError
evbp_destroy_stream(NPP instance, NPStream *stream, NPReason reason)
{
g_debug("%s", __func__);
return NPERR_NO_ERROR;
}
static int32_t
evbp_write_ready(NPP instance, NPStream *stream)
{
g_debug("%s", __func__);
return 0;
}
static int32_t
evbp_write(NPP instance, NPStream *stream, int32_t offset, int32_t len,
void *buffer)
{
g_debug("%s", __func__);
return 0;
}
static void
evbp_stream_as_file(NPP instance, NPStream *stream, const char *fname)
{
evbp_priv_t *priv = instance->pdata;
GFile *file;
gchar *uri;
GError *error = NULL;
g_debug("%s", __func__);
if (!g_file_test(fname, G_FILE_TEST_IS_REGULAR)) {
g_warning("filename \"%s\" does not exist or is not a regular file",
fname);
return;
}
/* convert the filename to a uri */
file = g_file_new_for_commandline_arg(fname);
uri = g_file_get_uri(file);
g_object_unref(file);
/* load the document */
if (!evbp_viewer_load_uri(EVBP_VIEWER(priv->viewer), uri, &error)) {
g_warning("failed to load document \"%s\": %s\n", uri,
error->message);
g_error_free(error);
return;
}
}
static void
evbp_print(NPP instance, NPPrint *platformPrint)
{
g_debug("%s", __func__);
}
static int16_t
evbp_handle_event(NPP instance, void *event)
{
g_debug("%s", __func__);
return 0;
}
static void
evbp_url_notify(NPP instance, const char *url, NPReason reason,
void *notifyData)
{
g_debug("%s", __func__);
}
static NPError
evbp_get_value(NPP instance, NPPVariable variable, void *value)
{
NPError err = NPERR_NO_ERROR;
g_debug("%s", __func__);
switch (variable) {
case NPPVpluginNameString:
*((char **)value) = EVBP_NAME;
break;
case NPPVpluginDescriptionString:
*((char **)value) = evbp_description;
break;
case NPPVpluginNeedsXEmbed:
/* Require XEmbed */
*((PRBool *)value) = PR_TRUE;
break;
case NPPVpluginWantsAllNetworkStreams:
/* Don't require all network streams */
*((NPBool *)value) = false;
break;
default:
g_warning("cannot get value for NPPVariable %d", variable);
err = NPERR_GENERIC_ERROR;
}
return err;
}
static NPError
evbp_set_value(NPP instance, NPNVariable variable, void *value)
{
g_debug("%s", __func__);
return NPERR_GENERIC_ERROR;
}
static NPError
save_npn_funcs(const NPNetscapeFuncs *funcs)
{
g_debug("%s", __func__);
/* Check for valid structure and compatibility */
if (!funcs) {
g_critical("NPNetscapeFuncs NULL");
return NPERR_INVALID_FUNCTABLE_ERROR;
}
if (funcs->version >> 8 > NP_VERSION_MAJOR) {
g_critical("incompatible NPAPI version %d", funcs->version >> 8);
return NPERR_INCOMPATIBLE_VERSION_ERROR;
}
if (funcs->size < sizeof(npn_funcs)) {
g_critical("size of NPN funtion table does not match NPNetscapeFuncs");
return NPERR_INVALID_FUNCTABLE_ERROR;
}
npn_funcs = funcs;
return NPERR_NO_ERROR;
}
static NPError
fill_npp_funcs(NPPluginFuncs *funcs)
{
g_debug("%s", __func__);
if (!funcs) {
g_critical("NPPluginFuncs NULL");
return NPERR_INVALID_FUNCTABLE_ERROR;
}
/* Fill the browser's table */
funcs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
funcs->size = sizeof(*funcs);
funcs->newp = evbp_new;
funcs->destroy = evbp_destroy;
funcs->setwindow = evbp_set_window;
funcs->newstream = evbp_new_stream;
funcs->destroystream = evbp_destroy_stream;
funcs->asfile = evbp_stream_as_file;
funcs->writeready = evbp_write_ready;
funcs->write = evbp_write;
funcs->print = evbp_print;
funcs->event = evbp_handle_event;
funcs->urlnotify = evbp_url_notify;
funcs->getvalue = evbp_get_value;
funcs->setvalue = evbp_set_value;
return NPERR_NO_ERROR;
}
NPError
NP_Initialize(NPNetscapeFuncs *nfuncs, NPPluginFuncs *pfuncs)
{
NPError ret;
PRBool xembed = PR_FALSE;
NPNToolkitType toolkit = 0;
g_debug("%s", __func__);
ret = save_npn_funcs(nfuncs);
if (ret != NPERR_NO_ERROR) {
g_critical("failed to initialize NPNetscapeFuncs");
return ret;
}
ret = fill_npp_funcs(pfuncs);
if (ret != NPERR_NO_ERROR) {
g_critical("failed to initialize NPPluginFuncs");
return ret;
}
/* Check that the browser supports XEmbed */
ret = npn_funcs->getvalue(NULL, NPNVSupportsXEmbedBool,
(void *)&xembed);
if (ret != NPERR_NO_ERROR || xembed != PR_TRUE) {
g_critical("browser does not support XEmbed");
return NPERR_INCOMPATIBLE_VERSION_ERROR;
}
/* Check that the toolkit is Gtk2 */
ret = npn_funcs->getvalue(NULL, NPNVToolkit, (void *)&toolkit);
if (ret != NPERR_NO_ERROR || toolkit != NPNVGtk2) {
g_critical("toolkit is not Gtk2");
return NPERR_INCOMPATIBLE_VERSION_ERROR;
}
if (!evbp_initialize()) {
g_critical("failed to initialize plugin");
return NPERR_MODULE_LOAD_FAILED_ERROR;
}
return NPERR_NO_ERROR;
}
char *
NP_GetPluginVersion(void)
{
setup_logging();
g_debug("%s", __func__);
return EVBP_VERSION;
}
const char *
NP_GetMIMEDescription(void)
{
GString *mimes;
setup_logging();
g_debug("%s", __func__);
if (!ev_initialize()) {
g_critical("failed to initialize evince; can't get MIME types");
return NULL;
}
mimes = evbp_mime_get_description();
if (evbp_mime_string)
g_free(evbp_mime_string);
evbp_mime_string = g_string_free(mimes, FALSE);
return evbp_mime_string;
}
NPError
NP_Shutdown(void)
{
g_debug("%s", __func__);
if (evbp_mime_string) {
g_free(evbp_mime_string);
evbp_mime_string = NULL;
}
npn_funcs = NULL;
evbp_shutdown();
return NPERR_NO_ERROR;
}
NPError
NP_GetValue(void *instance, NPPVariable variable, void *value)
{
g_debug("%s", __func__);
return evbp_get_value(NULL, variable, value);
}
|