blob: f9af879db6b0a92f0604ab3714b3b0580a9183fd [file] [log] [blame]
Grace Klobafbe47c02009-05-14 17:31:45 -07001/*
2 * Copyright 2008, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
Grace Klobafbe47c02009-05-14 17:31:45 -070025
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040026#include "AnimationPlugin.h"
27
Grace Klobafbe47c02009-05-14 17:31:45 -070028#include <math.h>
29#include <string.h>
30
31extern NPNetscapeFuncs* browser;
32extern ANPLogInterfaceV0 gLogI;
33extern ANPCanvasInterfaceV0 gCanvasI;
34extern ANPPaintInterfaceV0 gPaintI;
35extern ANPPathInterfaceV0 gPathI;
Derek Sollenberger349bc372011-01-04 16:14:34 -050036extern ANPSystemInterfaceV0 gSystemI;
Derek Sollenberger1e64f022011-01-07 14:11:29 -050037extern ANPWindowInterfaceV1 gWindowI;
Grace Klobafbe47c02009-05-14 17:31:45 -070038
Ben Murdoch3b6f45d2010-05-12 14:09:26 +010039static uint16_t rnd16(float x, int inset) {
Grace Klobafbe47c02009-05-14 17:31:45 -070040 int ix = (int)roundf(x) + inset;
41 if (ix < 0) {
42 ix = 0;
43 }
Ben Murdoch3b6f45d2010-05-12 14:09:26 +010044 return static_cast<uint16_t>(ix);
Grace Klobafbe47c02009-05-14 17:31:45 -070045}
46
Derek Sollenberger0c763b62009-08-11 10:05:46 -040047///////////////////////////////////////////////////////////////////////////////
48
Derek Sollenberger349bc372011-01-04 16:14:34 -050049BallAnimation::BallAnimation(NPP inst) : SurfaceSubPlugin(inst) {
Derek Sollenberger0c763b62009-08-11 10:05:46 -040050 //register for touch events
51 ANPEventFlags flags = kTouch_ANPEventFlag;
52 NPError err = browser->setvalue(inst, kAcceptEvents_ANPSetValue, &flags);
53 if (err != NPERR_NO_ERROR) {
Derek Sollenbergere62ce172009-11-30 11:52:06 -050054 gLogI.log(kError_ANPLogType, "Error selecting input events.");
Derek Sollenberger0c763b62009-08-11 10:05:46 -040055 }
Derek Sollenberger349bc372011-01-04 16:14:34 -050056
57 gLogI.log(kError_ANPLogType, "Starting Rendering Thread");
58
59 //start a thread and do your drawing there
60 m_renderingThread = new AnimationThread(inst);
61 m_renderingThread->incStrong(inst);
62 m_renderingThread->run("AnimationThread");
Derek Sollenberger0c763b62009-08-11 10:05:46 -040063}
64
65BallAnimation::~BallAnimation() {
Derek Sollenberger349bc372011-01-04 16:14:34 -050066 m_renderingThread->requestExitAndWait();
67 destroySurface();
Derek Sollenberger0c763b62009-08-11 10:05:46 -040068}
Grace Klobafbe47c02009-05-14 17:31:45 -070069
Derek Sollenberger21f39912009-07-09 09:19:39 -040070bool BallAnimation::supportsDrawingModel(ANPDrawingModel model) {
Derek Sollenberger349bc372011-01-04 16:14:34 -050071 return (model == kOpenGL_ANPDrawingModel);
Derek Sollenberger21f39912009-07-09 09:19:39 -040072}
73
Derek Sollenberger349bc372011-01-04 16:14:34 -050074jobject BallAnimation::getSurface() {
Derek Sollenberger0c763b62009-08-11 10:05:46 -040075
Derek Sollenberger349bc372011-01-04 16:14:34 -050076 if (m_surface) {
77 return m_surface;
Grace Klobafbe47c02009-05-14 17:31:45 -070078 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040079
Derek Sollenberger349bc372011-01-04 16:14:34 -050080 // load the appropriate java class and instantiate it
81 JNIEnv* env = NULL;
82 if (gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
83 gLogI.log(kError_ANPLogType, " ---- getSurface: failed to get env");
84 return NULL;
85 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040086
Derek Sollenberger349bc372011-01-04 16:14:34 -050087 const char* className = "com.android.sampleplugin.AnimationSurface";
88 jclass fullScreenClass = gSystemI.loadJavaClass(inst(), className);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040089
Derek Sollenberger349bc372011-01-04 16:14:34 -050090 if(!fullScreenClass) {
91 gLogI.log(kError_ANPLogType, " ---- getSurface: failed to load class");
92 return NULL;
93 }
94
95 jmethodID constructor = env->GetMethodID(fullScreenClass, "<init>", "(Landroid/content/Context;)V");
96 jobject fullScreenSurface = env->NewObject(fullScreenClass, constructor, m_context);
97
98 if(!fullScreenSurface) {
99 gLogI.log(kError_ANPLogType, " ---- getSurface: failed to construct object");
100 return NULL;
101 }
102
103 gLogI.log(kError_ANPLogType, " ---- object %p", fullScreenSurface);
104
105 m_surface = env->NewGlobalRef(fullScreenSurface);
106 return m_surface;
107}
108
109void BallAnimation::destroySurface() {
110 JNIEnv* env = NULL;
111 if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
112 env->DeleteGlobalRef(m_surface);
113 m_surface = NULL;
114 }
Grace Klobafbe47c02009-05-14 17:31:45 -0700115}
116
Derek Sollenbergerc654bce2009-07-20 10:03:58 -0400117void BallAnimation::showEntirePluginOnScreen() {
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400118 NPP instance = this->inst();
119 PluginObject *obj = (PluginObject*) instance->pdata;
120 NPWindow *window = obj->window;
121
Derek Sollenberger1e64f022011-01-07 14:11:29 -0500122 // log the current visible rect
123 ANPRectI visibleRect = gWindowI.visibleRect(instance);
124 gLogI.log(kDebug_ANPLogType, "Current VisibleRect: (%d,%d,%d,%d)",
125 visibleRect.left, visibleRect.top, visibleRect.right, visibleRect.bottom);
126
Derek Sollenbergerc654bce2009-07-20 10:03:58 -0400127 ANPRectI visibleRects[1];
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400128
Derek Sollenbergerc654bce2009-07-20 10:03:58 -0400129 visibleRects[0].left = 0;
130 visibleRects[0].top = 0;
131 visibleRects[0].right = window->width;
132 visibleRects[0].bottom = window->height;
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400133
Derek Sollenbergerc654bce2009-07-20 10:03:58 -0400134 gWindowI.setVisibleRects(instance, visibleRects, 1);
135 gWindowI.clearVisibleRects(instance);
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400136}
137
Ben Murdoch3b6f45d2010-05-12 14:09:26 +0100138int16_t BallAnimation::handleEvent(const ANPEvent* evt) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400139 NPP instance = this->inst();
Grace Klobafbe47c02009-05-14 17:31:45 -0700140
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400141 switch (evt->eventType) {
142 case kDraw_ANPEventType:
143 switch (evt->data.draw.model) {
Derek Sollenberger349bc372011-01-04 16:14:34 -0500144 case kOpenGL_ANPDrawingModel: {
145 //send the width and height to the rendering thread
146 int width = evt->data.draw.data.surface.width;
147 int height = evt->data.draw.data.surface.height;
148 gLogI.log(kError_ANPLogType, "New Dimensions (%d,%d)", width, height);
149 m_renderingThread->setDimensions(width, height);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400150 return 1;
Derek Sollenberger349bc372011-01-04 16:14:34 -0500151 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400152 default:
Derek Sollenberger349bc372011-01-04 16:14:34 -0500153 return 0; // unknown drawing model
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400154 }
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400155 case kTouch_ANPEventType:
156 if (kDown_ANPTouchAction == evt->data.touch.action) {
Derek Sollenbergerc654bce2009-07-20 10:03:58 -0400157 showEntirePluginOnScreen();
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400158 }
Derek Sollenberger349bc372011-01-04 16:14:34 -0500159 else if (kDoubleTap_ANPTouchAction == evt->data.touch.action) {
160 browser->geturl(inst(), "javascript:alert('Detected double tap event.')", 0);
161 gWindowI.requestFullScreen(inst());
162 }
163 return 1;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400164 default:
165 break;
Grace Klobafbe47c02009-05-14 17:31:45 -0700166 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400167 return 0; // unknown or unhandled event
Grace Klobafbe47c02009-05-14 17:31:45 -0700168}