New unix npapi sample plugin, replaces old one. b=486423 r/sr=jst

--HG--
rename : modules/plugin/sdk/samples/basic/unix/plugin.cpp => modules/plugin/sdk/samples/basic/unix/BasicPlugin.c
rename : modules/plugin/sdk/samples/basic/unix/plugin.h => modules/plugin/sdk/samples/basic/unix/BasicPlugin.h
rename : modules/plugin/sdk/samples/basic/unix/Makefile.in => modules/plugin/sdk/samples/basic/unix/Makefile
This commit is contained in:
Josh Aas 2009-05-07 00:17:03 -04:00
parent 0d2e5f7d1a
commit b5e64e22ce
6 changed files with 311 additions and 308 deletions

View File

@ -9,8 +9,9 @@ which plugin draws inside the window.
<br><br>
<center>
<embed type="application/basic-plugin" width=600 height=40>
<embed type="application/basic-plugin" width=600 height=80>
</center>
</html>
</body>
</body>

View File

@ -0,0 +1,247 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "BasicPlugin.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#define PLUGIN_NAME "Basic Sample Plug-in"
#define PLUGIN_DESCRIPTION PLUGIN_NAME " (Mozilla SDK)"
#define PLUGIN_VERSION "1.0.0.0"
static NPNetscapeFuncs* sBrowserFuncs = NULL;
typedef struct InstanceData {
NPP npp;
NPWindow window;
} InstanceData;
static void fillPluginFunctionTable(NPPluginFuncs* pFuncs)
{
pFuncs->version = 11;
pFuncs->size = sizeof(*pFuncs);
pFuncs->newp = NPP_New;
pFuncs->destroy = NPP_Destroy;
pFuncs->setwindow = NPP_SetWindow;
pFuncs->newstream = NPP_NewStream;
pFuncs->destroystream = NPP_DestroyStream;
pFuncs->asfile = NPP_StreamAsFile;
pFuncs->writeready = NPP_WriteReady;
pFuncs->write = NPP_Write;
pFuncs->print = NPP_Print;
pFuncs->event = NPP_HandleEvent;
pFuncs->urlnotify = NPP_URLNotify;
pFuncs->getvalue = NPP_GetValue;
pFuncs->setvalue = NPP_SetValue;
}
static void drawWindow(InstanceData* instanceData, GdkDrawable* gdkWindow) {
NPWindow window = instanceData->window;
int x = window.x;
int y = window.y;
int width = window.width;
int height = window.height;
NPP npp = instanceData->npp;
if (!npp)
return;
const char* uaString = sBrowserFuncs->uagent(npp);
if (!uaString)
return;
GdkGC* gdkContext = gdk_gc_new(gdkWindow);
// draw a grey background for the plugin frame
GdkColor grey;
grey.red = grey.blue = grey.green = 32767;
gdk_gc_set_rgb_fg_color(gdkContext, &grey);
gdk_draw_rectangle(gdkWindow, gdkContext, TRUE, x, y, width, height);
// draw a 3-pixel-thick black frame around the plugin
GdkColor black;
black.red = black.green = black.blue = 0;
gdk_gc_set_rgb_fg_color(gdkContext, &black);
gdk_gc_set_line_attributes(gdkContext, 3, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_MITER);
gdk_draw_rectangle(gdkWindow, gdkContext, FALSE, x + 1, y + 1,
width - 3, height - 3);
// paint the UA string
PangoContext* pangoContext = gdk_pango_context_get();
PangoLayout* pangoTextLayout = pango_layout_new(pangoContext);
pango_layout_set_width(pangoTextLayout, (width - 10) * PANGO_SCALE);
pango_layout_set_text(pangoTextLayout, uaString, -1);
gdk_draw_layout(gdkWindow, gdkContext, x + 5, y + 5, pangoTextLayout);
g_object_unref(pangoTextLayout);
g_object_unref(gdkContext);
}
NP_EXPORT(NPError) NP_Initialize(NPNetscapeFuncs* bFuncs, NPPluginFuncs* pFuncs)
{
sBrowserFuncs = bFuncs;
fillPluginFunctionTable(pFuncs);
return NPERR_NO_ERROR;
}
NP_EXPORT(char*) NP_GetPluginVersion()
{
return PLUGIN_VERSION;
}
NP_EXPORT(char*) NP_GetMIMEDescription()
{
return "application/basic-plugin:bsc:Basic plugin";
}
NP_EXPORT(NPError) NP_GetValue(void* future, NPPVariable aVariable, void* aValue) {
switch (aVariable) {
case NPPVpluginNameString:
*((char**)aValue) = PLUGIN_NAME;
break;
case NPPVpluginDescriptionString:
*((char**)aValue) = PLUGIN_DESCRIPTION;
break;
default:
return NPERR_INVALID_PARAM;
break;
}
return NPERR_NO_ERROR;
}
NP_EXPORT(NPError) NP_Shutdown()
{
return NPERR_NO_ERROR;
}
NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved) {
// Make sure we can render this plugin
NPBool browserSupportsWindowless = false;
sBrowserFuncs->getvalue(instance, NPNVSupportsWindowless, &browserSupportsWindowless);
if (!browserSupportsWindowless) {
printf("Windowless mode not supported by the browser\n");
return NPERR_GENERIC_ERROR;
}
sBrowserFuncs->setvalue(instance, NPPVpluginWindowBool, (void*)false);
// set up our our instance data
InstanceData* instanceData = (InstanceData*)malloc(sizeof(InstanceData));
if (!instanceData)
return NPERR_OUT_OF_MEMORY_ERROR;
memset(instanceData, 0, sizeof(InstanceData));
instanceData->npp = instance;
instance->pdata = instanceData;
return NPERR_NO_ERROR;
}
NPError NPP_Destroy(NPP instance, NPSavedData** save) {
InstanceData* instanceData = (InstanceData*)(instance->pdata);
free(instanceData);
return NPERR_NO_ERROR;
}
NPError NPP_SetWindow(NPP instance, NPWindow* window) {
InstanceData* instanceData = (InstanceData*)(instance->pdata);
instanceData->window = *window;
return NPERR_NO_ERROR;
}
NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype) {
return NPERR_GENERIC_ERROR;
}
NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason) {
return NPERR_GENERIC_ERROR;
}
int32_t NPP_WriteReady(NPP instance, NPStream* stream) {
return 0;
}
int32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer) {
return 0;
}
void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname) {
}
void NPP_Print(NPP instance, NPPrint* platformPrint) {
}
int16_t NPP_HandleEvent(NPP instance, void* event) {
InstanceData *instanceData = (InstanceData*)(instance->pdata);
XEvent *nativeEvent = (XEvent*)event;
if (nativeEvent->type != GraphicsExpose)
return 0;
XGraphicsExposeEvent *expose = &nativeEvent->xgraphicsexpose;
instanceData->window.window = (void*)(expose->drawable);
GdkNativeWindow nativeWinId = (XID)(instanceData->window.window);
GdkDrawable* gdkWindow = GDK_DRAWABLE(gdk_window_foreign_new(nativeWinId));
drawWindow(instanceData, gdkWindow);
g_object_unref(gdkWindow);
return 1;
}
void NPP_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData) {
}
NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) {
return NPERR_GENERIC_ERROR;
}
NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) {
return NPERR_GENERIC_ERROR;
}

View File

@ -20,6 +20,7 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -35,46 +36,30 @@
*
* ***** END LICENSE BLOCK ***** */
#ifndef __PLUGIN_H__
#define __PLUGIN_H__
#ifndef BasicPlugin_h_
#define BasicPlugin_h_
/* Xlib/Xt stuff */
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/cursorfont.h>
// This just needs to include NPAPI headers, change the path to whatever works
// for you. Note that "XP_UNIX=1" is defined in the makefile so that the NPAPI
// headers know we're compiling for unix.
#include "../../../../base/public/npapi.h"
#include "../../../../base/public/npfunctions.h"
#include "pluginbase.h"
NPError NP_Initialize(NPNetscapeFuncs* bFuncs, NPPluginFuncs* pFuncs);
NPError NP_Shutdown();
class nsPluginInstance : public nsPluginInstanceBase
{
public:
nsPluginInstance(NPP aInstance);
virtual ~nsPluginInstance();
NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved);
NPError NPP_Destroy(NPP instance, NPSavedData** save);
NPError NPP_SetWindow(NPP instance, NPWindow* window);
NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype);
NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
int32_t NPP_WriteReady(NPP instance, NPStream* stream);
int32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer);
void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
void NPP_Print(NPP instance, NPPrint* platformPrint);
int16_t NPP_HandleEvent(NPP instance, void* event);
void NPP_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData);
NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
NPBool init(NPWindow* aWindow);
void shut();
NPBool isInitialized() {return mInitialized;}
NPError GetValue(NPPVariable variable, void *value);
NPError SetWindow(NPWindow* aWindow);
// locals
const char * getVersion();
void draw();
private:
NPP mInstance;
NPBool mInitialized;
Window mWindow;
Display *mDisplay;
Widget mXtwidget;
int mX, mY;
int mWidth, mHeight;
Visual* mVisual;
Colormap mColormap;
unsigned int mDepth;
XFontStruct *mFontInfo;
GC mGC;
};
#endif // __PLUGIN_H__
#endif // BasicPlugin_h_

View File

@ -0,0 +1,28 @@
Copyright (c) 2009 Mozilla Corporation
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the Mozilla Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -20,6 +20,7 @@
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Josh Aas <josh@mozilla.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -35,44 +36,15 @@
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
# Basic plugin requires GTK and X11
CFLAGS = -Wall -DXP_UNIX=1 -DMOZ_X11=1 -fPIC -g
include $(DEPTH)/config/autoconf.mk
npbasicplugin : BasicPlugin.o
cc $(CFLAGS) -shared BasicPlugin.o -o npbasicplugin.so
LIBRARY_NAME = npSDKbasic
BasicPlugin.o : BasicPlugin.c BasicPlugin.h
cc `pkg-config --cflags gtk+-2.0` $(CFLAGS) -c BasicPlugin.c
REQUIRES = idl \
xpcom \
nspr \
$(NULL)
clean :
rm npbasicplugin.so BasicPlugin.o
CPPSRCS = plugin.cpp
SHARED_LIBRARY_LIBS = \
$(DIST)/lib/libplugingate_s.$(LIB_SUFFIX) \
include $(topsrcdir)/config/rules.mk
DEFINES += -DMOZILLA_STRICT_API
EXTRA_DSO_LDOPTS += $(XLDFLAGS) -rdynamic -lXi -lXext -lX11 -lm -lXt
INSTALL_PLUGIN = $(CONFIG_TOOLS)/nsinstall -R
INSTALL = true
ifeq ($(OS_ARCH), UNIX)
# This needs to get defined for npapi.h on unix platforms.
DEFINES += -DXP_UNIX
endif
LOCAL_INCLUDES += -I$(srcdir)/../../include -I$(srcdir)/../../../include
install-plugin: $(SHARED_LIBRARY)
ifdef SHARED_LIBRARY
$(INSTALL_PLUGIN) $(SHARED_LIBRARY) $(DIST)/bin/plugins
endif
libs:: install-plugin

View File

@ -1,230 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "plugin.h"
#define MIME_TYPES_HANDLED "application/basic-plugin"
#define PLUGIN_NAME "Basic Example Plug-in for Mozilla"
#define MIME_TYPES_DESCRIPTION MIME_TYPES_HANDLED":bsc:"PLUGIN_NAME
#define PLUGIN_DESCRIPTION PLUGIN_NAME " (Plug-ins SDK sample)"
char* NPP_GetMIMEDescription(void)
{
return(MIME_TYPES_DESCRIPTION);
}
/////////////////////////////////////
// general initialization and shutdown
//
NPError NS_PluginInitialize()
{
return NPERR_NO_ERROR;
}
void NS_PluginShutdown()
{
}
// get values per plugin
NPError NS_PluginGetValue(NPPVariable aVariable, void *aValue)
{
NPError err = NPERR_NO_ERROR;
switch (aVariable) {
case NPPVpluginNameString:
*((char **)aValue) = PLUGIN_NAME;
break;
case NPPVpluginDescriptionString:
*((char **)aValue) = PLUGIN_DESCRIPTION;
break;
default:
err = NPERR_INVALID_PARAM;
break;
}
return err;
}
/////////////////////////////////////////////////////////////
//
// construction and destruction of our plugin instance object
//
nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
{
if(!aCreateDataStruct)
return NULL;
nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
return plugin;
}
void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
{
if(aPlugin)
delete (nsPluginInstance *)aPlugin;
}
////////////////////////////////////////
//
// nsPluginInstance class implementation
//
nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
mInstance(aInstance),
mInitialized(FALSE),
mWindow(0),
mXtwidget(0),
mFontInfo(0)
{
}
nsPluginInstance::~nsPluginInstance()
{
}
static void
xt_event_handler(Widget xtwidget, nsPluginInstance *plugin, XEvent *xevent, Boolean *b)
{
switch (xevent->type) {
case Expose:
// get rid of all other exposure events
if (plugin) {
//while(XCheckTypedWindowEvent(plugin->Display(), plugin->Window(), Expose, xevent));
plugin->draw();
}
default:
break;
}
}
void nsPluginInstance::draw()
{
unsigned int h = mHeight/2;
unsigned int w = 3 * mWidth/4;
int x = (mWidth - w)/2; // center
int y = h/2;
if (x >= 0 && y >= 0) {
GC gc = XCreateGC(mDisplay, mWindow, 0, NULL);
if (!gc)
return;
XDrawRectangle(mDisplay, mWindow, gc, x, y, w, h);
const char *string = getVersion();
if (string && *string) {
int l = strlen(string);
int fmba = mFontInfo->max_bounds.ascent;
int fmbd = mFontInfo->max_bounds.descent;
int fh = fmba + fmbd;
y += fh;
x += 32;
XDrawString(mDisplay, mWindow, gc, x, y, string, l);
}
XFreeGC(mDisplay, gc);
}
}
NPBool nsPluginInstance::init(NPWindow* aWindow)
{
if(aWindow == NULL)
return FALSE;
if (SetWindow(aWindow))
mInitialized = TRUE;
return mInitialized;
}
void nsPluginInstance::shut()
{
mInitialized = FALSE;
}
const char * nsPluginInstance::getVersion()
{
return NPN_UserAgent(mInstance);
}
NPError nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
{
NPError err = NPERR_NO_ERROR;
switch (aVariable) {
case NPPVpluginNameString:
case NPPVpluginDescriptionString:
return NS_PluginGetValue(aVariable, aValue) ;
break;
default:
err = NPERR_INVALID_PARAM;
break;
}
return err;
}
NPError nsPluginInstance::SetWindow(NPWindow* aWindow)
{
if(aWindow == NULL)
return FALSE;
mX = aWindow->x;
mY = aWindow->y;
mWidth = aWindow->width;
mHeight = aWindow->height;
if (mWindow == (Window) aWindow->window) {
// The page with the plugin is being resized.
// Save any UI information because the next time
// around expect a SetWindow with a new window id.
} else {
mWindow = (Window) aWindow->window;
NPSetWindowCallbackStruct *ws_info = (NPSetWindowCallbackStruct *)aWindow->ws_info;
mDisplay = ws_info->display;
mVisual = ws_info->visual;
mDepth = ws_info->depth;
mColormap = ws_info->colormap;
if (!mFontInfo) {
if (!(mFontInfo = XLoadQueryFont(mDisplay, "9x15")))
printf("Cannot open 9X15 font\n");
}
// add xt event handler
Widget xtwidget = XtWindowToWidget(mDisplay, mWindow);
if (xtwidget && mXtwidget != xtwidget) {
mXtwidget = xtwidget;
long event_mask = ExposureMask;
XSelectInput(mDisplay, mWindow, event_mask);
XtAddEventHandler(xtwidget, event_mask, False, (XtEventHandler)xt_event_handler, this);
}
}
draw();
return TRUE;
}