1/*
2 IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
3 consideration of your agreement to the following terms, and your use, installation,
4 modification or redistribution of this Apple software constitutes acceptance of these
5 terms.  If you do not agree with these terms, please do not use, install, modify or
6 redistribute this Apple software.
7
8 In consideration of your agreement to abide by the following terms, and subject to these
9 terms, Apple grants you a personal, non-exclusive license, under Appleâs copyrights in
10 this original Apple software (the "Apple Software"), to use, reproduce, modify and
11 redistribute the Apple Software, with or without modifications, in source and/or binary
12 forms; provided that if you redistribute the Apple Software in its entirety and without
13 modifications, you must retain this notice and the following text and disclaimers in all
14 such redistributions of the Apple Software.  Neither the name, trademarks, service marks
15 or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
16 the Apple Software without specific prior written permission from Apple. Except as expressly
17 stated in this notice, no other rights or licenses, express or implied, are granted by Apple
18 herein, including but not limited to any patent rights that may be infringed by your
19 derivative works or by other works in which the Apple Software may be incorporated.
20
21 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
22 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
24 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
25
26 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
29 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
30 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
31 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#import <WebKit/npapi.h>
35#import <WebKit/npfunctions.h>
36#import <WebKit/npruntime.h>
37
38#import <Cocoa/Cocoa.h>
39#import <Quartz/Quartz.h>
40#import <QuartzCore/QuartzCore.h>
41
42// Browser function table
43static NPNetscapeFuncs* browser;
44
45// Structure for per-instance storage
46typedef struct PluginObject
47{
48    NPP npp;
49
50    NPWindow window;
51
52    QCCompositionLayer *layer;
53    bool gold;
54} PluginObject;
55
56NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved);
57NPError NPP_Destroy(NPP instance, NPSavedData** save);
58NPError NPP_SetWindow(NPP instance, NPWindow* window);
59NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype);
60NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
61int32 NPP_WriteReady(NPP instance, NPStream* stream);
62int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer);
63void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
64void NPP_Print(NPP instance, NPPrint* platformPrint);
65int16 NPP_HandleEvent(NPP instance, void* event);
66void NPP_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData);
67NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
68NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
69
70#pragma export on
71// Mach-o entry points
72NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
73NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
74void NP_Shutdown(void);
75#pragma export off
76
77NPError NP_Initialize(NPNetscapeFuncs* browserFuncs)
78{
79    browser = browserFuncs;
80    return NPERR_NO_ERROR;
81}
82
83NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
84{
85    pluginFuncs->version = 11;
86    pluginFuncs->size = sizeof(pluginFuncs);
87    pluginFuncs->newp = NPP_New;
88    pluginFuncs->destroy = NPP_Destroy;
89    pluginFuncs->setwindow = NPP_SetWindow;
90    pluginFuncs->newstream = NPP_NewStream;
91    pluginFuncs->destroystream = NPP_DestroyStream;
92    pluginFuncs->asfile = NPP_StreamAsFile;
93    pluginFuncs->writeready = NPP_WriteReady;
94    pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
95    pluginFuncs->print = NPP_Print;
96    pluginFuncs->event = NPP_HandleEvent;
97    pluginFuncs->urlnotify = NPP_URLNotify;
98    pluginFuncs->getvalue = NPP_GetValue;
99    pluginFuncs->setvalue = NPP_SetValue;
100
101    return NPERR_NO_ERROR;
102}
103
104void NP_Shutdown(void)
105{
106
107}
108
109NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
110{
111    // Create per-instance storage
112    PluginObject *obj = (PluginObject *)malloc(sizeof(PluginObject));
113    bzero(obj, sizeof(PluginObject));
114
115    obj->npp = instance;
116    instance->pdata = obj;
117
118    // Ask the browser if it supports the Core Animation drawing model
119    NPBool supportsCoreAnimation;
120    if (browser->getvalue(instance, NPNVsupportsCoreAnimationBool, &supportsCoreAnimation) != NPERR_NO_ERROR)
121        supportsCoreAnimation = FALSE;
122
123    if (!supportsCoreAnimation)
124        return NPERR_INCOMPATIBLE_VERSION_ERROR;
125
126    // If the browser supports the Core Animation drawing model, enable it.
127    browser->setvalue(instance, NPPVpluginDrawingModel, (void *)NPDrawingModelCoreAnimation);
128
129    // If the browser supports the Cocoa event model, enable it.
130    NPBool supportsCocoa;
131    if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
132        supportsCocoa = FALSE;
133
134    if (!supportsCocoa)
135        return NPERR_INCOMPATIBLE_VERSION_ERROR;
136
137    browser->setvalue(instance, NPPVpluginEventModel, (void *)NPEventModelCocoa);
138
139    return NPERR_NO_ERROR;
140}
141
142NPError NPP_Destroy(NPP instance, NPSavedData** save)
143{
144    // Free per-instance storage
145    PluginObject *obj = instance->pdata;
146
147    [obj->layer release];
148
149    free(obj);
150
151    return NPERR_NO_ERROR;
152}
153
154NPError NPP_SetWindow(NPP instance, NPWindow* window)
155{
156    PluginObject *obj = instance->pdata;
157    obj->window = *window;
158
159    return NPERR_NO_ERROR;
160}
161
162
163NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
164{
165    *stype = NP_ASFILEONLY;
166    return NPERR_NO_ERROR;
167}
168
169NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
170{
171    return NPERR_NO_ERROR;
172}
173
174int32 NPP_WriteReady(NPP instance, NPStream* stream)
175{
176    return 0;
177}
178
179int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
180{
181    return 0;
182}
183
184void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
185{
186}
187
188void NPP_Print(NPP instance, NPPrint* platformPrint)
189{
190
191}
192
193static void handleMouseClick(PluginObject *obj, NPCocoaEvent *event)
194{
195    obj->gold = !obj->gold;
196
197    [obj->layer setValue:[NSNumber numberWithBool:obj->gold] forInputKey:@"gold"];
198}
199
200int16 NPP_HandleEvent(NPP instance, void* event)
201{
202    PluginObject *obj = instance->pdata;
203
204    NPCocoaEvent *cocoaEvent = event;
205
206    switch(cocoaEvent->type) {
207        case NPCocoaEventMouseDown:
208            handleMouseClick(obj, cocoaEvent);
209            return 1;
210    }
211
212    return 0;
213}
214
215void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
216{
217
218}
219
220NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
221{
222    PluginObject *obj = instance->pdata;
223
224    switch (variable) {
225        case NPPVpluginCoreAnimationLayer:
226            if (!obj->layer) {
227                NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.netscapecoreanimationplugin"] pathForResource:@"Composition" ofType:@"qtz"];
228
229                QCComposition *composition = [QCComposition compositionWithFile:path];
230                obj->layer = [[QCCompositionLayer compositionLayerWithComposition:composition] retain];
231            }
232
233            // Make sure to return a retained layer
234            *((CALayer **)value) = [obj->layer retain];
235
236            return NPERR_NO_ERROR;
237
238        default:
239            return NPERR_GENERIC_ERROR;
240    }
241}
242
243NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
244{
245    return NPERR_GENERIC_ERROR;
246}
247