|
21
|
1 |
// |
|
|
2 |
// Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
|
|
3 |
// |
|
|
4 |
// This program is free software; you can redistribute it and/or modify |
|
|
5 |
// it under the terms of the GNU General Public License as published by |
|
|
6 |
// the Free Software Foundation; either version 3 of the License, or |
|
|
7 |
// (at your option) any later version. |
|
|
8 |
// |
|
|
9 |
// This program is distributed in the hope that it will be useful, |
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
12 |
// GNU General Public License for more details. |
|
|
13 |
// |
|
|
14 |
// You should have received a copy of the GNU General Public License |
|
|
15 |
// along with this program; if not, write to the Free Software |
|
|
16 |
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
17 |
|
|
|
18 |
#include "plugin.h" |
|
|
19 |
|
|
|
20 |
#include <cstring> |
|
|
21 |
#include <iostream> |
|
|
22 |
#include <sstream> |
|
|
23 |
#define D(s) /*std::cerr << s << std::endl;*/ |
|
|
24 |
|
|
|
25 |
#include <set> |
|
|
26 |
|
|
|
27 |
#define MIME_TYPES_HANDLED "application/x-tuio" |
|
|
28 |
// The name must be this value to get flash movies that check the |
|
|
29 |
// plugin version to load. |
|
|
30 |
#define PLUGIN_NAME "TUIO Client" |
|
|
31 |
#define MIME_TYPES_DESCRIPTION MIME_TYPES_HANDLED":tuio:"PLUGIN_NAME |
|
|
32 |
|
|
|
33 |
#define PLUGIN_DESCRIPTION "TUIO Client plugin" |
|
|
34 |
|
|
|
35 |
extern NPNetscapeFuncs NPNFuncs; |
|
|
36 |
|
|
|
37 |
NPBool plugInitialized = FALSE; |
|
|
38 |
|
|
|
39 |
static std::set<nsPluginInstance*> instances; |
|
|
40 |
|
|
|
41 |
void tuio_callback(TuioEventData data) |
|
|
42 |
{ |
|
|
43 |
std::set<nsPluginInstance*>::iterator iter; |
|
|
44 |
for (iter = instances.begin(); iter != instances.end(); iter++) { |
|
|
45 |
(*iter)->event(data); |
|
|
46 |
} |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
void |
|
|
50 |
PR_CALLBACK Destructor(void * /* data */) |
|
|
51 |
{ |
|
|
52 |
#if 0 |
|
|
53 |
/* |
|
|
54 |
* We don't actually free the storage since it's actually allocated |
|
|
55 |
* on the stack. Normally, this would not be the case and this is |
|
|
56 |
* the opportunity to free whatever. |
|
|
57 |
*/ |
|
|
58 |
PR_Free(data); |
|
|
59 |
#endif |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/// \brief Return the MIME Type description for this plugin. |
|
|
63 |
char* |
|
|
64 |
NPP_GetMIMEDescription(void) |
|
|
65 |
{ |
|
|
66 |
return const_cast<char *>(MIME_TYPES_DESCRIPTION); |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
// |
|
|
70 |
// general initialization and shutdown |
|
|
71 |
// |
|
|
72 |
|
|
|
73 |
/// \brief Initialize the plugin |
|
|
74 |
/// |
|
|
75 |
/// This C++ function gets called once when the plugin is loaded, |
|
|
76 |
/// regardless of how many instantiations there is actually playing |
|
|
77 |
/// movies. So this is where all the one time only initialization |
|
|
78 |
/// stuff goes. |
|
|
79 |
NPError |
|
|
80 |
NS_PluginInitialize() |
|
|
81 |
{ |
|
|
82 |
if ( plugInitialized ) |
|
|
83 |
{ |
|
|
84 |
return NPERR_NO_ERROR; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
plugInitialized = TRUE; |
|
|
88 |
|
|
25
|
89 |
tuio_start(80); |
|
21
|
90 |
|
|
|
91 |
return NPERR_NO_ERROR; |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
/// \brief Shutdown the plugin |
|
|
95 |
/// |
|
|
96 |
/// This C++ function gets called once when the plugin is being |
|
|
97 |
/// shutdown, regardless of how many instantiations actually are |
|
|
98 |
/// playing movies. So this is where all the one time only |
|
|
99 |
/// shutdown stuff goes. |
|
|
100 |
void |
|
|
101 |
NS_PluginShutdown() |
|
|
102 |
{ |
|
|
103 |
#if 0 |
|
|
104 |
if (!plugInitialized) |
|
|
105 |
{ |
|
|
106 |
#if GNASH_PLUGIN_DEBUG > 1 |
|
|
107 |
std::cout << "Plugin already shut down" << std::endl; |
|
|
108 |
#endif |
|
|
109 |
return; |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
plugInitialized = FALSE; |
|
|
113 |
#endif |
|
|
114 |
|
|
|
115 |
tuio_stop(); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
/// \brief Retrieve values from the plugin for the Browser |
|
|
121 |
/// |
|
|
122 |
/// This C++ function is called by the browser to get certain |
|
|
123 |
/// information is needs from the plugin. This information is the |
|
|
124 |
/// plugin name, a description, etc... |
|
|
125 |
NPError |
|
|
126 |
NS_PluginGetValue(NPPVariable aVariable, void *aValue) |
|
|
127 |
{ |
|
|
128 |
NPError err = NPERR_NO_ERROR; |
|
|
129 |
|
|
|
130 |
switch (aVariable) |
|
|
131 |
{ |
|
|
132 |
case NPPVpluginNameString: |
|
|
133 |
*static_cast<const char **> (aValue) = PLUGIN_NAME; |
|
|
134 |
break; |
|
|
135 |
|
|
|
136 |
// This becomes the description field you see below the opening |
|
|
137 |
// text when you type about:plugins and in |
|
|
138 |
// navigator.plugins["Shockwave Flash"].description, used in |
|
|
139 |
// many flash version detection scripts. |
|
|
140 |
case NPPVpluginDescriptionString: |
|
|
141 |
*static_cast<const char **>(aValue) = PLUGIN_DESCRIPTION; |
|
|
142 |
break; |
|
|
143 |
|
|
|
144 |
case NPPVpluginNeedsXEmbed: |
|
|
145 |
*(int*)aValue = PR_TRUE; |
|
|
146 |
break; |
|
|
147 |
|
|
|
148 |
case NPPVpluginTimerInterval: |
|
|
149 |
|
|
|
150 |
case NPPVpluginKeepLibraryInMemory: |
|
|
151 |
|
|
|
152 |
default: |
|
|
153 |
err = NPERR_INVALID_PARAM; |
|
|
154 |
break; |
|
|
155 |
} |
|
|
156 |
return err; |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
/// \brief construct our plugin instance object |
|
|
160 |
/// |
|
|
161 |
/// This instantiates a new object via a C++ function used by the |
|
|
162 |
/// browser. |
|
|
163 |
nsPluginInstanceBase * |
|
|
164 |
NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct) |
|
|
165 |
{ |
|
|
166 |
if(!aCreateDataStruct) return NULL; |
|
|
167 |
|
|
|
168 |
return new nsPluginInstance(aCreateDataStruct); |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
/// \brief destroy our plugin instance object |
|
|
172 |
/// |
|
|
173 |
/// This destroys our instantiated object via a C++ function used by the |
|
|
174 |
/// browser. |
|
|
175 |
void |
|
|
176 |
NS_DestroyPluginInstance(nsPluginInstanceBase* aPlugin) |
|
|
177 |
{ |
|
|
178 |
delete static_cast<nsPluginInstance *> (aPlugin); |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
// |
|
|
182 |
// nsPluginInstance class implementation |
|
|
183 |
// |
|
|
184 |
|
|
|
185 |
/// \brief Constructor |
|
|
186 |
nsPluginInstance::nsPluginInstance(nsPluginCreateData* data) |
|
|
187 |
: |
|
|
188 |
nsPluginInstanceBase(), |
|
|
189 |
_instance(data->instance), |
|
|
190 |
_port(3333), |
|
|
191 |
_callback("tuio_callback") |
|
|
192 |
{ |
|
|
193 |
for (size_t i=0, n=data->argc; i<n; ++i) |
|
|
194 |
{ |
|
|
195 |
std::string name, val; |
|
|
196 |
|
|
|
197 |
if (data->argn[i]) |
|
|
198 |
{ |
|
|
199 |
name = data->argn[i]; |
|
|
200 |
} |
|
|
201 |
|
|
|
202 |
if (data->argv[i]) |
|
|
203 |
{ |
|
|
204 |
val = data->argv[i]; |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
else if ( ! strstr(name.c_str(), "callback") ) |
|
|
208 |
{ |
|
|
209 |
_callback = val; |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
else if ( ! strstr(name.c_str(), "port") ) |
|
|
213 |
{ |
|
|
214 |
_port = atoi(val.c_str()); |
|
|
215 |
} |
|
|
216 |
} |
|
|
217 |
|
|
|
218 |
instances.insert(this); |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
/// \brief Destructor |
|
|
222 |
nsPluginInstance::~nsPluginInstance() |
|
|
223 |
{ |
|
|
224 |
instances.erase(this); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
/// \brief Initialize an instance of the plugin object |
|
|
228 |
/// |
|
|
229 |
/// This methods initializes the plugin object, and is called for |
|
|
230 |
/// every movie that gets played. This is where the movie playing |
|
|
231 |
/// specific initialization goes. |
|
|
232 |
NPBool |
|
|
233 |
nsPluginInstance::init(NPWindow* aWindow) |
|
|
234 |
{ |
|
|
235 |
D("[ns] init"); |
|
|
236 |
return TRUE; |
|
|
237 |
} |
|
|
238 |
|
|
|
239 |
/// \brief Shutdown an instantiated object |
|
|
240 |
/// |
|
|
241 |
/// This shuts down an object, and is called for every movie that gets |
|
|
242 |
/// played. This is where the movie playing specific shutdown code |
|
|
243 |
/// goes. |
|
|
244 |
void |
|
|
245 |
nsPluginInstance::shut() |
|
|
246 |
{ |
|
|
247 |
D("[ns] shut"); |
|
|
248 |
} |
|
|
249 |
|
|
|
250 |
NPError |
|
|
251 |
nsPluginInstance::SetWindow(NPWindow* aWindow) |
|
|
252 |
{ |
|
|
253 |
D("[ns] SetWindow"); |
|
|
254 |
|
|
|
255 |
if(!aWindow) |
|
|
256 |
{ |
|
|
257 |
return NPERR_INVALID_PARAM; |
|
|
258 |
} |
|
|
259 |
|
|
|
260 |
return NPERR_NO_ERROR; |
|
|
261 |
} |
|
|
262 |
|
|
|
263 |
NPError |
|
|
264 |
nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue) |
|
|
265 |
{ |
|
|
266 |
return NS_PluginGetValue(aVariable, aValue); |
|
|
267 |
} |
|
|
268 |
|
|
|
269 |
/// \brief Write a status message |
|
|
270 |
/// |
|
|
271 |
/// This writes a status message to the status line at the bottom of |
|
|
272 |
/// the browser window and the console firefox was started from. |
|
|
273 |
NPError |
|
|
274 |
nsPluginInstance::WriteStatus(const char *msg) const |
|
|
275 |
{ |
|
|
276 |
NPN_Status(_instance, msg); |
|
|
277 |
std::cout << msg << std::endl; |
|
|
278 |
|
|
|
279 |
return NPERR_NO_ERROR; |
|
|
280 |
} |
|
|
281 |
|
|
|
282 |
NPError |
|
|
283 |
nsPluginInstance::NewStream(NPMIMEType /*type*/, NPStream* stream, |
|
|
284 |
NPBool /*seekable*/, uint16* /*stype*/) |
|
|
285 |
{ |
|
|
286 |
D("[ns] NewStream"); |
|
|
287 |
return NPERR_NO_ERROR; |
|
|
288 |
} |
|
|
289 |
|
|
|
290 |
NPError |
|
|
291 |
nsPluginInstance::DestroyStream(NPStream* /*stream*/, NPError /*reason*/) |
|
|
292 |
{ |
|
|
293 |
D("[ns] DestroyStream"); |
|
|
294 |
return NPERR_NO_ERROR; |
|
|
295 |
} |
|
|
296 |
|
|
|
297 |
int32_t |
|
|
298 |
nsPluginInstance::WriteReady(NPStream* /* stream */ ) |
|
|
299 |
{ |
|
|
300 |
D("[ns] WriteReady"); |
|
|
301 |
return 0x0fffffff; |
|
|
302 |
} |
|
|
303 |
|
|
|
304 |
int32_t |
|
|
305 |
nsPluginInstance::Write(NPStream* /*stream*/, int32_t /*offset*/, int32_t len, |
|
|
306 |
void* buffer) |
|
|
307 |
{ |
|
|
308 |
D("[ns] Write: len=" << len); |
|
|
309 |
return len; |
|
|
310 |
} |
|
|
311 |
|
|
|
312 |
typedef struct { |
|
|
313 |
NPP instance; |
|
|
314 |
TuioEventData data; |
|
|
315 |
} Event; |
|
|
316 |
|
|
|
317 |
void test(void* ev) |
|
|
318 |
{ |
|
|
319 |
D("ev=" << ev); |
|
|
320 |
Event* event = (Event*)ev; |
|
|
321 |
D("event=" << event); |
|
|
322 |
std::stringstream ss; |
|
|
323 |
ss << "javascript:tuio_callback("; |
|
|
324 |
ss << event->data.type << ", "; |
|
|
325 |
ss << event->data.sid << ", "; |
|
|
326 |
ss << event->data.fid << ", "; |
|
|
327 |
ss << event->data.x << ", "; |
|
|
328 |
ss << event->data.y << ", "; |
|
25
|
329 |
ss << event->data.z << ", "; |
|
27
|
330 |
ss << event->data.a << ", "; |
|
|
331 |
if(event->data.code == NULL || event->data.code == "") |
|
|
332 |
ss << "\"\");"; |
|
|
333 |
else |
|
|
334 |
ss << "\"" << event->data.code << "\"" << ");"; |
|
|
335 |
|
|
21
|
336 |
NPN_GetURL(event->instance, ss.str().c_str(), "_self"); |
|
|
337 |
|
|
|
338 |
delete event; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
void nsPluginInstance::event(TuioEventData data) |
|
|
342 |
{ |
|
|
343 |
D("[event] callback: type=" << data.type |
|
27
|
344 |
<< ", sid=" << data.sid << ", fid=" << data.fid |
|
|
345 |
<< ", x=" << data.x << ", y=" << data.y |
|
|
346 |
<< ", z=" << data.z << ", a=" << data.a |
|
|
347 |
<< ", code=" << "\"" << data.code << "\""); |
|
|
348 |
|
|
21
|
349 |
Event* ev = new Event(); |
|
|
350 |
ev->instance = _instance; |
|
|
351 |
ev->data = data; |
|
|
352 |
|
|
|
353 |
NPN_PluginThreadAsyncCall(_instance, test, ev); |
|
|
354 |
} |
|
|
355 |
|
|
|
356 |
// Local Variables: |
|
|
357 |
// mode: C++ |
|
|
358 |
// indent-tabs-mode: t |
|
|
359 |
// End: |