summaryrefslogtreecommitdiff
path: root/plugin/Main.c
blob: 834300ec8462cfe870bd72d4d8419b5c6530978d (plain)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/* $Xorg: Main.c,v 1.5 2001/02/09 02:05:57 xorgcvs Exp $ */
/*

Copyright 1996, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
ITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
The Open Group.

*/
/* $XFree86: xc/programs/xrx/plugin/Main.c,v 1.8tsi Exp $ */

/*
 * RX plug-in module based on the UnixTemplate file provided by Netcape.
 */

/* -*- Mode: C; tab-width: 4; -*- */
/******************************************************************************
 * Copyright 1996 Netscape Communications. All rights reserved.
 ******************************************************************************/
/*
 * UnixShell.c
 *
 * Netscape Client Plugin API
 * - Function that need to be implemented by plugin developers
 *
 * This file defines a "Template" plugin that plugin developers can use
 * as the basis for a real plugin.  This shell just provides empty
 * implementations of all functions that the plugin can implement
 * that will be called by Netscape (the NPP_xxx methods defined in 
 * npapi.h). 
 *
 * dp Suresh <dp@netscape.com>
 *
 */


#include "RxPlugin.h"
#include <X11/StringDefs.h>

#ifdef USE_MOTIF
extern WidgetClass xmLabelGadgetClass;
extern WidgetClass xmPushButtonGadgetClass;
#else
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Command.h>
#endif /* USE_MOTIF */

#include <ctype.h>
#include <stdlib.h>

/***********************************************************************
 * Utility functions to deal with list of arguments
 ***********************************************************************/

/* Free list of arguments */
static void
FreeArgs(char* argn[], char* argv[], int argc)
{
    int i;
    if (argc != 0) {
	for (i = 0; i < argc; i++) {
	    NPN_MemFree(argn[i]);
	    NPN_MemFree(argv[i]);
	}
	NPN_MemFree(argn);
	NPN_MemFree(argv);
    }
}

/* Copy list 2 to list 1 */
static NPError
CopyArgs(char** argn1[], char** argv1[], int16* argc1,
	 char* argn2[], char* argv2[], int16 argc2)
{
    char **argn, **argv;
    int i;

    argn = (char **)NPN_MemAlloc(sizeof(char *) * argc2);
    if (!argn)
	return NPERR_OUT_OF_MEMORY_ERROR;
    argv = (char **)NPN_MemAlloc(sizeof(char *) * argc2);
    if (!argv) {
	NPN_MemFree(argn);
	return NPERR_OUT_OF_MEMORY_ERROR;
    }
    memset(argn, 0, sizeof(char *) * argc2);
    memset(argv, 0, sizeof(char *) * argc2);
    for (i = 0; i < argc2; i++) {
	char *name, *value;
	name = (char *)NPN_MemAlloc(strlen(argn2[i]) + 1);
	if (!name) {
	    FreeArgs(argn, argv, i - 1);
	    return NPERR_OUT_OF_MEMORY_ERROR;
	}
	strcpy(name, argn2[i]);
	value = (char *)NPN_MemAlloc(strlen(argv2[i]) + 1);
	if (!value) {
	    NPN_MemFree(name);
	    FreeArgs(argn, argv, i - 1);
	    return NPERR_OUT_OF_MEMORY_ERROR;
	}
	strcpy(value, argv2[i]);
	argn[i] = name;
	argv[i] = value;
    }
    *argc1 = argc2;
    *argn1 = argn;
    *argv1 = argv;

    return NPERR_NO_ERROR;
}


char*
NPP_GetMIMEDescription(void)
{
    return(PLUGIN_MIME_DESCRIPTION);
}

NPError
NPP_GetValue(void *future, NPPVariable variable, void *value)
{
    NPError err = NPERR_NO_ERROR;

    switch (variable) {
    case NPPVpluginNameString:
	*((char **)value) = PLUGIN_NAME;
	break;
    case NPPVpluginDescriptionString:
	*((char **)value) = PLUGIN_DESCRIPTION;
	break;
    default:
	err = NPERR_GENERIC_ERROR;
    }
    return err;
}

jref
NPP_GetJavaClass()
{
    return NULL;
}

NPError 
NPP_New(NPMIMEType pluginType,
	NPP instance,
	uint16 mode,
	int16 argc,
	char* argn[],
	char* argv[],
	NPSavedData* saved)
{
    PluginInstance* This;

    if (instance == NULL)
	return NPERR_INVALID_INSTANCE_ERROR;

    instance->pdata = NPN_MemAlloc(sizeof(PluginInstance));
	
    This = (PluginInstance*) instance->pdata;

    if (This == NULL)
	return NPERR_OUT_OF_MEMORY_ERROR;

    This->instance = instance;
    if (argc != 0) {		/* copy the arguments list */
	if (CopyArgs(&This->argn, &This->argv, &This->argc,
		     argn, argv, argc) == NPERR_OUT_OF_MEMORY_ERROR) {
	    NPN_MemFree(This);
	    return NPERR_OUT_OF_MEMORY_ERROR;
	}
    } else {
	This->argc = 0;
	This->argn = This->argv = NULL;
    }
    This->parse_reply = 0;
    This->status = 0;
    This->dont_reparent = RxUndef;
    This->state = LOADING;
    This->status_widget = NULL;
    This->plugin_widget = NULL;
    RxpNew(This);

    return NPERR_NO_ERROR;
}


NPError 
NPP_Destroy(NPP instance, NPSavedData** save)
{
    PluginInstance* This;

    if (instance == NULL)
	return NPERR_INVALID_INSTANCE_ERROR;

    This = (PluginInstance*) instance->pdata;

    /* PLUGIN DEVELOPERS:
     *	If desired, call NP_MemAlloc to create a
     *	NPSavedDate structure containing any state information
     *	that you want restored if this plugin instance is later
     *	recreated.
     */

    if (This != NULL) {
	RxpDestroy(This);
	if (This->argc != 0)
	    FreeArgs(This->argn, This->argv, This->argc);
	if (This->query != NULL)
	    NPN_MemFree(This->query);
	NPN_MemFree(instance->pdata);
	instance->pdata = NULL;
    }

    return NPERR_NO_ERROR;
}


/* private buffer structure */
typedef struct {
    char *buf;
    uint32 size;
} RxStreamBuf;

NPError 
NPP_NewStream(NPP instance,
	      NPMIMEType type,
	      NPStream *stream, 
	      NPBool seekable,
	      uint16 *stype)
{
    PluginInstance* This;
    RxStreamBuf *streambuf;

    if (instance == NULL)
	return NPERR_INVALID_INSTANCE_ERROR;

    This = (PluginInstance*) instance->pdata;

    if (This->parse_reply != 0)
	return NPERR_NO_ERROR;

    /* malloc structure to store RX document */
    streambuf = (RxStreamBuf *) NPN_MemAlloc(sizeof(RxStreamBuf));
    if (streambuf == NULL)
	return NPERR_OUT_OF_MEMORY_ERROR;

    streambuf->buf = NULL;
    streambuf->size = 0;
    stream->pdata = (void *) streambuf;

    return NPERR_NO_ERROR;
}


/* PLUGIN DEVELOPERS:
 *	These next 2 functions are directly relevant in a plug-in which
 *	handles the data in a streaming manner. If you want zero bytes
 *	because no buffer space is YET available, return 0. As long as
 *	the stream has not been written to the plugin, Navigator will
 *	continue trying to send bytes.  If the plugin doesn't want them,
 *	just return some large number from NPP_WriteReady(), and
 *	ignore them in NPP_Write().  For a NP_ASFILE stream, they are
 *	still called but can safely be ignored using this strategy.
 */

int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
				   * mode so we can take any size stream in our
				   * write call (since we ignore it) */

int32 
NPP_WriteReady(NPP instance, NPStream *stream)
{
    return STREAMBUFSIZE;
}

int32 
NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buf)
{
    PluginInstance* This;

    if (instance == NULL)
	return len;

    This = (PluginInstance*) instance->pdata;

    if (This->parse_reply == 0) {
	/* copy stream buffer to private storage, concatenating if necessary:
	 * since Netscape doesn't provide an NPN_MemRealloc we must do
	 * a new malloc, copy, and free :-(
	 */
	RxStreamBuf *streambuf = (RxStreamBuf *) stream->pdata;
	uint32 size = streambuf->size;
	char *cbuf;

	/* if first chunk add 1 for null terminating character */
	if (size == 0)
	    size++;

	size += len;
	cbuf = (char *) NPN_MemAlloc(size);
	if (cbuf == NULL)
	    return -1;
	if (streambuf->size != 0) {
	    memcpy(cbuf, streambuf->buf, streambuf->size - 1);
	    memcpy(cbuf + streambuf->size - 1, ((char *)buf), len);
	    /* free old storage */
	    NPN_MemFree((void *) streambuf->buf);
	} else
	    memcpy(cbuf, ((char *)buf), len);
	cbuf[size - 1] = '\0';
	/* store new buffer */
	streambuf->buf = cbuf;
	streambuf->size = size;
#ifdef PLUGIN_TRACE
	fprintf(stderr, "write %s:\n", PLUGIN_NAME);
	fwrite(buf, len, 1, stderr);
	fprintf(stderr, "\n");
#endif
    } else {
	int l = len;
	if (This->parse_reply == 1) {
	    /* look for status line */
	    char *ptr = strchr(buf, '\n');
	    if (ptr != NULL && isdigit(((char *)buf)[0])) {
		This->status = (short) atoi((char *)buf);
		/* skip status line */
		l -= ptr - (char *)buf + 1;
		buf = ptr + 1;
		if (This->status != 0) {
		    fprintf(stderr,
			    "%s: Application failed to start properly\n",
			    PLUGIN_NAME);
		}
	    }
	    This->parse_reply = 2;
	}
	/* simply prints out whatever we get, and let netscape display it
	   in a dialog */
	fwrite(buf, l, 1, stderr);
    }

    return len;			/* The number of bytes accepted */
}

void
StartApplication(PluginInstance* This)
{
#ifndef NO_STARTING_STATE
    RxpSetStatusWidget(This, STARTING);
#else
    RxpSetStatusWidget(This, RUNNING);
#endif

    /* perform GET request
     * throwing away the response.
     */
    (void) NPN_GetURL(This->instance, This->query, NULL);
    This->parse_reply = 1;	/* we want to print out the answer  */
}

void
StartCB(Widget widget, XtPointer client_data, XtPointer call_data)
{
    PluginInstance* This = (PluginInstance*) client_data;
#if 0
    XtUnmapWidget(widget);
#endif
    XtDestroyWidget(widget);
    StartApplication(This);
}


void
RxpSetStatusWidget(PluginInstance* This, PluginState state)
{
    Arg args[5];
    int n;
    XrmDatabase db;
    char* return_type;
    XrmValue return_value;

    if (This->status_widget) {
	XtDestroyWidget(This->status_widget);
	This->status_widget = NULL;
    }
    if (This->plugin_widget == NULL)
	return;

    db = XtDatabase (XtDisplay (This->plugin_widget));

    if (!XrmGetResource (db, "RxPlugin_BeenHere", "RxPlugin_BeenHere",
		    &return_type, &return_value)) {

	XrmPutStringResource (&db, "*Rx_Loading.labelString", "Loading...");
	XrmPutStringResource (&db, "*Rx_Starting.labelString", "Starting...");
	XrmPutStringResource (&db, "*Rx_Start.labelString", "Start");
	XrmPutStringResource (&db, "RxPlugin_BeenHere", "YES");
    }

    n = 0;
    XtSetArg(args[n], "shadowThickness", 1); n++;
    XtSetArg(args[n], XtNwidth, This->width); n++;
    XtSetArg(args[n], XtNheight, This->height); n++;
#ifdef USE_MOTIF
    if (state == LOADING) {
	/* create a label */
	This->status_widget =
	    XtCreateManagedWidget("Rx_Loading", xmLabelGadgetClass,
		This->plugin_widget, args, n);
#ifndef NO_STARTING_STATE
    } else if (state == STARTING) {
	/* create a label */
	This->status_widget =
	    XtCreateManagedWidget("Rx_Starting", xmLabelGadgetClass,
		This->plugin_widget, args, n);
#endif
    } else if (state == WAITING) {
	/* create a push button */
	This->status_widget =
	    XtCreateManagedWidget("Rx_Start", xmPushButtonGadgetClass,
		This->plugin_widget, args, n);
	XtAddCallback(This->status_widget, "activateCallback", StartCB, This);
    } else if (state == RUNNING) {
	/* nothing else to be done */
    }
#else
    if (state == LOADING) {
	/* create a label */
	This->status_widget =
	    XtCreateManagedWidget("Rx_Loading", labelWidgetClass,
		This->plugin_widget, args, n);
#ifndef NO_STARTING_STATE
    } else if (state == STARTING) {
	/* create a label */
	This->status_widget =
	    XtCreateManagedWidget("Rx_Starting", labelWidgetClass,
		This->plugin_widget, args, n);
#endif
    } else if (state == WAITING) {
	/* create a push button */
	This->status_widget =
	    XtCreateManagedWidget("Rx_Start", commandWidgetClass,
		This->plugin_widget, args, n);
	XtAddCallback(This->status_widget, XtNcallback, StartCB, This);
    } else if (state == RUNNING) {
	/* nothing else to be done */
    }
#endif /* USE_MOTIF */
    This->state = state;
}


NPError 
NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason)
{
    PluginInstance* This;
    RxStreamBuf *streambuf = (RxStreamBuf *) stream->pdata;
    char **rx_argn, **rx_argv;
    int rx_argc;
    RxParams params;
    RxReturnParams return_params;
    NPError status = NPERR_NO_ERROR;

    if (instance == NULL)
	return NPERR_INVALID_INSTANCE_ERROR;

    This = (PluginInstance*) instance->pdata;

    if (This->parse_reply != 0) {
	fflush(stderr);
	if (This->status != 0)	/* if error occured change widget status */
	    RxpSetStatusWidget(This, WAITING);
	return NPRES_DONE;
    }

    memset(&params, 0, sizeof(RxParams));
    memset(&return_params, 0, sizeof(RxReturnParams));
    rx_argc = 0;

    if (reason != NPRES_DONE) {
	status = NPERR_GENERIC_ERROR;
	goto exit;
    }

    /* read params from stream */
    if (RxReadParams(streambuf->buf, &rx_argn, &rx_argv, &rx_argc) != 0) {
	fprintf(stderr, "%s: invalid file %s\n", PLUGIN_NAME, stream->url);
	status = NPERR_GENERIC_ERROR;
	goto exit;
    }

    RxInitializeParams(&params);

    /* parse RX params */
    if (RxParseParams(rx_argn, rx_argv, rx_argc, &params, 0) != 0) {
	fprintf(stderr, "%s: invalid RX params\n", PLUGIN_NAME);
	status = NPERR_GENERIC_ERROR;
	goto exit;
    }

    /* parse HTML params */
    if (RxParseParams(This->argn, This->argv, This->argc, &params, 0) != 0) {
	fprintf(stderr, "%s: invalid HTML params\n", PLUGIN_NAME);
	status = NPERR_GENERIC_ERROR;
	goto exit;
    }

    /* set up return parameters */
    if (RxpProcessParams(This, &params, &return_params) != 0) {
	fprintf(stderr, "%s: failed to process params\n", PLUGIN_NAME);
	status = NPERR_GENERIC_ERROR;
	goto exit;
    }

    /* make query */
    This->query = RxBuildRequest(&return_params);
    if (This->query == NULL) {
	fprintf(stderr, "%s: failed to make query\n", PLUGIN_NAME);
	status = NPERR_GENERIC_ERROR;
	goto exit;
    }

    if (params.auto_start != RxFalse) /* default is auto start */
	StartApplication(This);
    else
	RxpSetStatusWidget(This, WAITING);

exit:
    /* free all forms of params */
    FreeArgs(rx_argn, rx_argv, rx_argc);
    FreeArgs(This->argn, This->argv, This->argc);
    This->argc = 0;
    RxFreeParams(&params);
    RxFreeReturnParams(&return_params);
    /* free private storage */
    if (streambuf->buf != NULL)
	NPN_MemFree(streambuf->buf);
    NPN_MemFree(stream->pdata);

    return status;
}

void 
NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
{
    /*
    PluginInstance* This;
    if (instance != NULL)
	This = (PluginInstance*) instance->pdata;
     */
}


void 
NPP_Print(NPP instance, NPPrint* printInfo)
{
    if(printInfo == NULL)
	return;

    if (instance != NULL) {
#if 0
	PluginInstance* This = (PluginInstance*) instance->pdata;
#endif
	
	if (printInfo->mode == NP_FULL) {
	    /*
	     * PLUGIN DEVELOPERS:
	     *	If your plugin would like to take over
	     *	printing completely when it is in full-screen mode,
	     *	set printInfo->pluginPrinted to TRUE and print your
	     *	plugin as you see fit.  If your plugin wants Netscape
	     *	to handle printing in this case, set
	     *	printInfo->pluginPrinted to FALSE (the default) and
	     *	do nothing.  If you do want to handle printing
	     *	yourself, printOne is true if the print button
	     *	(as opposed to the print menu) was clicked.
	     *	On the Macintosh, platformPrint is a THPrint; on
	     *	Windows, platformPrint is a structure
	     *	(defined in npapi.h) containing the printer name, port,
	     *	etc.
	     */

	    /*
	    void* platformPrint =
		printInfo->print.fullPrint.platformPrint;
	    NPBool printOne =
		printInfo->print.fullPrint.printOne;
	     */
			
	    /* Do the default*/
	    printInfo->print.fullPrint.pluginPrinted = FALSE;
	}
#if 0
	else {	/* If not fullscreen, we must be embedded */
	    /*
	     * PLUGIN DEVELOPERS:
	     *	If your plugin is embedded, or is full-screen
	     *	but you returned false in pluginPrinted above, NPP_Print
	     *	will be called with mode == NP_EMBED.  The NPWindow
	     *	in the printInfo gives the location and dimensions of
	     *	the embedded plugin on the printed page.  On the
	     *	Macintosh, platformPrint is the printer port; on
	     *	Windows, platformPrint is the handle to the printing
	     *	device context.
	     */

	    NPWindow* printWindow =
		&(printInfo->print.embedPrint.window);
	    void* platformPrint =
		printInfo->print.embedPrint.platformPrint;
	}
#endif
    }
}