home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / plugins / javatest / npshell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  17.1 KB  |  623 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*
  19.  * npshell.c
  20.  *
  21.  * Netscape Client Plugin API
  22.  * - Function that need to be implemented by plugin developers
  23.  *
  24.  * This file defines a "shell" plugin that plugin developers can use
  25.  * as the basis for a real plugin.  This shell just provides empty
  26.  * implementations of all functions that the plugin can implement
  27.  * that will be called by Netscape (the NPP_xxx methods defined in 
  28.  * npapi.h). 
  29.  *
  30.  * dp Suresh <dp@netscape.com>
  31.  * Modified by Warren Harris to test Java stuff. <warren@netscape.com>
  32.  *
  33.  *----------------------------------------------------------------------
  34.  * PLUGIN DEVELOPERS:
  35.  *    Implement your plugins here.
  36.  *    A sample text plugin is implemented here.
  37.  *    All the sample plugin does is displays any file with a
  38.  *    ".txt" extension in a scrolled text window. It uses Motif.
  39.  *----------------------------------------------------------------------
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include "prosdep.h"    /* include first to get the right #defines */
  44. #include "npapi.h"
  45.  
  46. #include "JavaTestPlugin.h"
  47. #include "java_lang_Class.h"
  48. #include "java_lang_String.h"
  49. #ifdef EMBEDDED_FRAMES
  50. #include "java_awt_EmbeddedFrame.h"
  51. #include "java_awt_Component.h"
  52. #include "java_awt_Color.h"
  53. #include "java_awt_Window.h"
  54. #endif /* EMBEDDED_FRAMES */
  55.  
  56. #define TEXT_PLUGIN
  57.  
  58. #ifdef TEXT_PLUGIN
  59. #include <Xm/Xm.h>
  60. #include <Xm/Form.h>
  61. #include <Xm/Text.h>
  62. #endif /* TEXT_PLUGIN */
  63.  
  64. /***********************************************************************
  65.  * Instance state information about the plugin.
  66.  *
  67.  * PLUGIN DEVELOPERS:
  68.  *    Use this struct to hold per-instance information that you'll
  69.  *    need in the various functions in this file.
  70.  ***********************************************************************/
  71.  
  72. typedef struct _PluginInstance
  73. {
  74.     uint16 mode;
  75.     Window window;
  76.     Display *display;
  77.     uint32 x, y;
  78.     uint32 width, height;
  79.  
  80. #ifdef TEXT_PLUGIN
  81.     Widget text;        /* Text widget */
  82. #endif /* TEXT_PLUGIN */
  83. } PluginInstance;
  84.  
  85.  
  86. /***********************************************************************
  87.  *
  88.  * Empty implementations of plugin API functions
  89.  *
  90.  * PLUGIN DEVELOPERS:
  91.  *    You will need to implement these functions as required by your
  92.  *    plugin.
  93.  *
  94.  ***********************************************************************/
  95.  
  96. char*
  97. NPP_GetMIMEDescription(void)
  98. {
  99. #ifdef TEXT_PLUGIN
  100.     return("text/plain:.txt");
  101. #else
  102.     return("application/x-data:.sample");
  103. #endif
  104. }
  105.  
  106. #define PLUGIN_NAME        "Java Test Plugin"
  107. #define PLUGIN_DESCRIPTION    "Tests java natives and reflection stuff."
  108.  
  109. NPError
  110. NPP_GetValue(void *future, NPPVariable variable, void *value)
  111. {
  112.     NPError err = NPERR_NO_ERROR;
  113.     if (variable == NPPVpluginNameString)
  114.         *((char **)value) = PLUGIN_NAME;
  115.     else if (variable == NPPVpluginDescriptionString)
  116.         *((char **)value) = PLUGIN_DESCRIPTION;
  117.     else
  118.         err = NPERR_GENERIC_ERROR;
  119.  
  120.     return err;
  121. }
  122.  
  123. JRIEnv* env;
  124. JRIGlobalRef classString;
  125. JRIGlobalRef classColor;
  126. JRIGlobalRef classFrame;
  127. JRIGlobalRef classJavaTestPlugin;
  128.  
  129. NPError
  130. NPP_Initialize(void)
  131. {
  132.     return NPERR_NO_ERROR;
  133. }
  134.  
  135. jref
  136. NPP_GetJavaClass(void)
  137. {
  138.     jref myClass;
  139.  
  140.     env = NPN_GetJavaEnv();
  141.     myClass = init_JavaTestPlugin(env);
  142.  
  143.     /* other classes I'll need: */
  144.     classString = JRI_NewGlobalRef(env, init_java_lang_String(env));
  145. #ifdef EMBEDDED_FRAMES
  146.     classColor = JRI_NewGlobalRef(env, init_java_awt_Color(env));
  147.     classFrame = JRI_NewGlobalRef(env, java_awt_EmbeddedFrame__init(env));
  148. #endif
  149.     classJavaTestPlugin = JRI_NewGlobalRef(env, myClass);
  150.  
  151.     return myClass;
  152. }
  153.  
  154. void
  155. NPP_Shutdown(void)
  156. {
  157. }
  158.  
  159.  
  160. NPError 
  161. NPP_New(NPMIMEType pluginType,
  162.     NPP instance,
  163.     uint16 mode,
  164.     int16 argc,
  165.     char* argn[],
  166.     char* argv[],
  167.     NPSavedData* saved)
  168. {
  169.         NPError result = NPERR_NO_ERROR;
  170.         PluginInstance* This;
  171.  
  172.     if (instance == NULL)
  173.         return NPERR_INVALID_INSTANCE_ERROR;
  174.         
  175.     instance->pdata = NPN_MemAlloc(sizeof(PluginInstance));
  176.     
  177.     This = (PluginInstance*) instance->pdata;
  178.  
  179.     if (This == NULL)
  180.         return NPERR_OUT_OF_MEMORY_ERROR;
  181.  
  182.     {
  183.         /* mode is NP_EMBED, NP_FULL, or NP_BACKGROUND (see npapi.h) */
  184.         This->mode = mode;
  185.         This->window = (Window) 0;
  186.  
  187.         /* PLUGIN DEVELOPERS:
  188.          *    Initialize fields of your plugin
  189.          *    instance data here.  If the NPSavedData is non-
  190.          *    NULL, you can use that data (returned by you from
  191.          *    NPP_Destroy to set up the new plugin instance).
  192.          */
  193.     }
  194.  
  195.     /* Java Test */
  196.     {
  197.         jref plugin;
  198.         jref fact;
  199.         int val;
  200.  
  201.         plugin = JRI_GetGlobalRef(env, classJavaTestPlugin);
  202.         val = JavaTestPlugin_fact(env, plugin, 10);    /* static method */
  203.         if (JRI_ExceptionOccurred(env)) {
  204.             fprintf(stderr, "fact failed\n");
  205.             result = NPERR_INVALID_INSTANCE_ERROR;
  206.             goto done;
  207.         }
  208.         printf("Plugin: Java sez: fact(10) = %d\n", val);
  209.  
  210.         fact = NPN_GetJavaPeer(instance);
  211.         if (JRI_ExceptionOccurred(env)) {
  212.             fprintf(stderr, "couldn't create a JavaTestPlugin object\n");
  213.             result = NPERR_INVALID_INSTANCE_ERROR;
  214.             goto done;
  215.         }
  216.         printf("Plugin: setting foo field to %d\n", 11111);
  217.         set_JavaTestPlugin_foo(env, fact, 11111);
  218.         printf("Plugin: reading foo field as %ld\n",
  219.                get_JavaTestPlugin_foo(env, fact));
  220.         printf("Plugin: calling into java to test plugin native methods\n");
  221.         JavaTestPlugin_testPluginCalls(env, fact);
  222.         printf("Plugin: reading foo field as %ld\n",
  223.                get_JavaTestPlugin_foo(env, fact));
  224.         if (JRI_ExceptionOccurred(env)) {
  225.             fprintf(stderr, "test returned an error\n");
  226.             result = NPERR_INVALID_INSTANCE_ERROR;
  227.             goto done;
  228.         }
  229.  
  230.       done:
  231.         if (JRI_ExceptionOccurred(env)) {
  232.             JRI_ExceptionDescribe(env);
  233.             JRI_ExceptionClear(env);
  234.         }
  235.     }
  236.     return result;
  237. }
  238.  
  239.  
  240. NPError 
  241. NPP_Destroy(NPP instance, NPSavedData** save)
  242. {
  243.     PluginInstance* This;
  244.  
  245.     if (instance == NULL)
  246.         return NPERR_INVALID_INSTANCE_ERROR;
  247.  
  248.     This = (PluginInstance*) instance->pdata;
  249.  
  250.     /* PLUGIN DEVELOPERS:
  251.      *    If desired, call NP_MemAlloc to create a
  252.      *    NPSavedDate structure containing any state information
  253.      *    that you want restored if this plugin instance is later
  254.      *    recreated.
  255.      */
  256.  
  257.     if (This != NULL) {
  258.         NPN_MemFree(instance->pdata);
  259.         instance->pdata = NULL;
  260.     }
  261.  
  262.     return NPERR_NO_ERROR;
  263. }
  264.  
  265.  
  266.  
  267. NPError 
  268. NPP_SetWindow(NPP instance, NPWindow* window)
  269. {
  270.     NPError result = NPERR_NO_ERROR;
  271.     PluginInstance* This;
  272.  
  273.     if (instance == NULL)
  274.         return NPERR_INVALID_INSTANCE_ERROR;
  275.  
  276.     This = (PluginInstance*) instance->pdata;
  277.  
  278.     /*
  279.      * PLUGIN DEVELOPERS:
  280.      *    Before setting window to point to the
  281.      *    new window, you may wish to compare the new window
  282.      *    info to the previous window (if any) to note window
  283.      *    size changes, etc.
  284.      */
  285.     
  286.     This->window = (Window) window->window;
  287.     This->x = window->x;
  288.     This->y = window->y;
  289.     This->width = window->width;
  290.     This->height = window->height;
  291.     This->display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;
  292.  
  293. #ifdef TEXT_PLUGIN
  294.     {
  295.         Widget netscape_widget;
  296.         Widget form;
  297.         Arg av[20];
  298.         int ac;
  299.  
  300.         netscape_widget = XtWindowToWidget(This->display, This->window);
  301.  
  302.         ac = 0;
  303.         XtSetArg(av[ac], XmNx, 0); ac++;
  304.         XtSetArg(av[ac], XmNy, 0); ac++;
  305.         XtSetArg(av[ac], XmNwidth, This->width); ac++;
  306.         XtSetArg(av[ac], XmNheight, This->height); ac++;
  307.         XtSetArg(av[ac], XmNborderWidth, 0); ac++;
  308.         XtSetArg(av[ac], XmNnoResize, True); ac++;
  309.         form = XmCreateForm(netscape_widget, "pluginForm",
  310.                     av, ac);
  311.  
  312.         ac = 0;
  313.         XtSetArg(av[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
  314.         XtSetArg(av[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
  315.         XtSetArg(av[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
  316.         XtSetArg(av[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
  317.         XtSetArg (av[ac], XmNeditMode, XmMULTI_LINE_EDIT); ac++;
  318.         This->text = XmCreateScrolledText (form, "pluginText", av, ac);
  319.  
  320.         XtManageChild(This->text);
  321.         XtManageChild(form);
  322.  
  323. #ifdef EMBEDDED_FRAMES
  324.     {
  325.         JavaStatus err;
  326.         jref frame;
  327.  
  328.         /* Create a window! */
  329.         frame = java_awt_EmbeddedFrame_make_1(env, 
  330.             JRI_GetGlobalRef(env, classFrame), form);
  331.         if ((err = JRI_GetError(env)) != JavaStatusOk) {
  332.         fprintf(stderr, "couldn't create an EmbeddedFrame (%d)\n", err);
  333.         result = NPERR_INVALID_INSTANCE_ERROR;
  334.         goto done;
  335.         }
  336.         fprintf(stderr, "frame = %p\n", frame);
  337.  
  338.         java_awt_Component_resize(env, frame, 30, 30);
  339.         java_awt_Component_setBackground(env, frame,
  340.         java_awt_Color_red_get(env, JRI_GetGlobalRef(env, classColor)));
  341.         
  342.         java_awt_Window_show(env, frame);
  343.         if ((err = JRI_GetError(env)) != JavaStatusOk) {
  344.         fprintf(stderr, "couldn't show EmbeddedFrame (%d)\n", err);
  345.         result = NPERR_INVALID_INSTANCE_ERROR;
  346.         goto done;
  347.         }
  348.         fprintf(stderr, "frame shown\n");
  349.  
  350.       done:
  351.     }
  352. #endif
  353.     }
  354. #endif /* TEXT_PLUGIN */
  355.  
  356.     return result;
  357. }
  358.  
  359.  
  360. NPError 
  361. NPP_NewStream(NPP instance,
  362.           NPMIMEType type,
  363.           NPStream *stream, 
  364.           NPBool seekable,
  365.           uint16 *stype)
  366. {
  367.     PluginInstance* This;
  368.  
  369.     if (instance == NULL)
  370.         return NPERR_INVALID_INSTANCE_ERROR;
  371.  
  372.     This = (PluginInstance*) instance->pdata;
  373.  
  374.     return NPERR_NO_ERROR;
  375. }
  376.  
  377.  
  378. /* PLUGIN DEVELOPERS:
  379.  *    These next 2 functions are directly relevant in a plug-in which
  380.  *    handles the data in a streaming manner. If you want zero bytes
  381.  *    because no buffer space is YET available, return 0. As long as
  382.  *    the stream has not been written to the plugin, Navigator will
  383.  *    continue trying to send bytes.  If the plugin doesn't want them,
  384.  *    just return some large number from NPP_WriteReady(), and
  385.  *    ignore them in NPP_Write().  For a NP_ASFILE stream, they are
  386.  *    still called but can safely be ignored using this strategy.
  387.  */
  388.  
  389. int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
  390.                    * mode so we can take any size stream in our
  391.                    * write call (since we ignore it) */
  392.  
  393. int32 
  394. NPP_WriteReady(NPP instance, NPStream *stream)
  395. {
  396.     PluginInstance* This;
  397.     if (instance != NULL)
  398.         This = (PluginInstance*) instance->pdata;
  399.  
  400.     /* Number of bytes ready to accept in NPP_Write() */
  401.     return STREAMBUFSIZE;
  402. }
  403.  
  404.  
  405. int32 
  406. NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
  407. {
  408.     if (instance != NULL)
  409.     {
  410.         PluginInstance* This = (PluginInstance*) instance->pdata;
  411. #ifdef TEXT_PLUGIN
  412.         char *cbuf;
  413.         XmTextPosition pos = 0;
  414.  
  415.         cbuf = (char *) NPN_MemAlloc(len + 1);
  416.         if (!cbuf) return 0;
  417.         memcpy(cbuf, ((char *)buffer)+offset, len);
  418.         cbuf[len] = '\0';
  419.         XtVaGetValues(This->text, XmNcursorPosition, &pos, 0);
  420.         XmTextInsert(This->text, pos, cbuf);
  421.         NPN_MemFree(cbuf);
  422. #endif /* TEXT_PLUGIN */
  423.     }
  424.  
  425.     return len;        /* The number of bytes accepted */
  426. }
  427.  
  428.  
  429. NPError 
  430. NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason)
  431. {
  432.     PluginInstance* This;
  433.  
  434.     if (instance == NULL)
  435.         return NPERR_INVALID_INSTANCE_ERROR;
  436.     This = (PluginInstance*) instance->pdata;
  437.  
  438.     return NPERR_NO_ERROR;
  439. }
  440.  
  441.  
  442. void 
  443. NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
  444. {
  445.     PluginInstance* This;
  446.     if (instance != NULL)
  447.         This = (PluginInstance*) instance->pdata;
  448. }
  449.  
  450.  
  451. void 
  452. NPP_Print(NPP instance, NPPrint* printInfo)
  453. {
  454.     if(printInfo == NULL)
  455.         return;
  456.  
  457.     if (instance != NULL) {
  458.         PluginInstance* This = (PluginInstance*) instance->pdata;
  459.     
  460.         if (printInfo->mode == NP_FULL) {
  461.             /*
  462.              * PLUGIN DEVELOPERS:
  463.              *    If your plugin would like to take over
  464.              *    printing completely when it is in full-screen mode,
  465.              *    set printInfo->pluginPrinted to TRUE and print your
  466.              *    plugin as you see fit.  If your plugin wants Netscape
  467.              *    to handle printing in this case, set
  468.              *    printInfo->pluginPrinted to FALSE (the default) and
  469.              *    do nothing.  If you do want to handle printing
  470.              *    yourself, printOne is true if the print button
  471.              *    (as opposed to the print menu) was clicked.
  472.              *    On the Macintosh, platformPrint is a THPrint; on
  473.              *    Windows, platformPrint is a structure
  474.              *    (defined in npapi.h) containing the printer name, port,
  475.              *    etc.
  476.              */
  477.  
  478.             void* platformPrint =
  479.                 printInfo->print.fullPrint.platformPrint;
  480.             NPBool printOne =
  481.                 printInfo->print.fullPrint.printOne;
  482.             
  483.             /* Do the default*/
  484.             printInfo->print.fullPrint.pluginPrinted = FALSE;
  485.         }
  486.         else {    /* If not fullscreen, we must be embedded */
  487.             /*
  488.              * PLUGIN DEVELOPERS:
  489.              *    If your plugin is embedded, or is full-screen
  490.              *    but you returned false in pluginPrinted above, NPP_Print
  491.              *    will be called with mode == NP_EMBED.  The NPWindow
  492.              *    in the printInfo gives the location and dimensions of
  493.              *    the embedded plugin on the printed page.  On the
  494.              *    Macintosh, platformPrint is the printer port; on
  495.              *    Windows, platformPrint is the handle to the printing
  496.              *    device context.
  497.              */
  498.  
  499.             NPWindow* printWindow =
  500.                 &(printInfo->print.embedPrint.window);
  501.             void* platformPrint =
  502.                 printInfo->print.embedPrint.platformPrint;
  503.         }
  504.     }
  505. }
  506.  
  507. /******************************************************************************/
  508.  
  509. /* public native doPluginStuff(Ljava/lang/String;I)C */
  510. jchar
  511. JavaTestPlugin_doPluginStuff(JRIEnv* env, struct JavaTestPlugin* self,
  512.                              struct java_lang_String * s, jint i)
  513. {
  514.     const jchar* chars = JRI_GetStringChars(env, s);
  515.     return chars[0];
  516. }
  517.  
  518. /* public native native1()V */
  519. void
  520. JavaTestPlugin_native1(JRIEnv* env,struct JavaTestPlugin* self)
  521. {
  522.     fprintf(stderr, "Plugin: native1 called!\n");
  523. }
  524.  
  525. /* public native native2(B)B */
  526. jbyte
  527. JavaTestPlugin_native2(JRIEnv* env,struct JavaTestPlugin* self,jbyte b)
  528. {
  529.     return b + 4;
  530. }
  531.  
  532. /* public native native3(C)C */
  533. jchar
  534. JavaTestPlugin_native3(JRIEnv* env,struct JavaTestPlugin* self,jchar c)
  535. {
  536.     return c + 1;
  537. }
  538.  
  539. /* public native native4(S)S */
  540. jshort
  541.  
  542. JavaTestPlugin_native4(JRIEnv* env,struct JavaTestPlugin* self,jshort s)
  543. {
  544.     return s + 1;
  545. }
  546.  
  547. /* public native native5(I)I */
  548. jint
  549. JavaTestPlugin_native5(JRIEnv* env,struct JavaTestPlugin* self,jint i)
  550. {
  551.     return i + 1;
  552. }
  553.  
  554. /* public native native6(J)J */
  555. jlong
  556. JavaTestPlugin_native6(JRIEnv* env,struct JavaTestPlugin* self,jlong l)
  557. {
  558.     jlong result, tmp;
  559.     jlong_I2L(tmp, 65536);
  560.     jlong_ADD(result, l, tmp);
  561.     return result;
  562. }
  563.  
  564. /* public native native7(F)F */
  565. jfloat
  566. JavaTestPlugin_native7(JRIEnv* env,struct JavaTestPlugin* self,jfloat f)
  567. {
  568.     return f + 1.0;
  569. }
  570.  
  571. /* public native native8(D)D */
  572. jdouble
  573. JavaTestPlugin_native8(JRIEnv* env,struct JavaTestPlugin* self,jdouble d)
  574. {
  575.     return d + 1e10;
  576. }
  577.  
  578. /* public native native9(Ljava/lang/String;ISCB)Ljava/lang/String; */
  579. struct java_lang_String *
  580. JavaTestPlugin_native9(JRIEnv* env, struct JavaTestPlugin* self,
  581.                        struct java_lang_String * s, jint j, jlong x,
  582.                        jfloatArray floats, jbooleanArray bools)
  583. {
  584.     jref clazz, ba;
  585.     jbool* b;
  586.     char* c;
  587.     jsize len = JRI_GetFloatArrayLength(env, floats);
  588.     jfloat* f = JRI_GetFloatArrayData(env, floats);
  589.     jsize i;
  590.     for (i = 0; i < len; i++) {
  591.         fprintf(stderr, "float %d: %f", i, f[i]);
  592.         f[i] *= 2.2;
  593.         fprintf(stderr, " => %f\n", f[i]);
  594.     }
  595.     len = JRI_GetBooleanArrayLength(env, bools);
  596.     b = JRI_GetBooleanArrayData(env, bools);
  597.     for (i = 0; i < len; i++) {
  598.         fprintf(stderr, "boolean %d: %d\n", i, b[i]);
  599.     }
  600.     ba = JRI_FindClass(env, JRISigArray(JRISigBoolean));
  601.     c = JRI_CopyStringUTF(env, java_lang_Class_getName(ba, env));
  602.     fprintf(stderr, "class [b => %p (%s)\n", enamed NPN_GetJavaInstance & Plugin.getNativeInstance -> NPN_GetJavaPeer & Plugin.getPeer.
  603.     free(c);
  604.     
  605.     clazz = JRI_GetObjectClass(env, shorts);
  606.     c = JRI_CopyStringUTF(env, java_lang_Class_getName(env, clazz));
  607.     fprintf(stderr, "class of shorts => %p (%s)\n", clazz, c);
  608.     free(c);
  609.     
  610.     na = JRI_NewShortArray(env, 2, ss);
  611.     clazz = JRI_GetObjectClass(env, na);
  612.     c = JRI_CopyStringUTF(env, java_lang_Class_getName(env, clazz));
  613.     fprintf(stderr, "class of the new short array => %p (%s)\n", clazz, c);
  614.  
  615.     /* can we call methods with double results properly */
  616.     d = JavaTestPlugin_native8(env, self, 2e10);
  617.     fprintf(stderr, "calling native8(2e10) => %lf %s\n", d, 
  618.             (d == 3e10 ? "ok" : "error"));
  619.  
  620.     /* can we call a system function */
  621.     return java_lang_String_substring(env, s, j);
  622. }
  623.