front_idill/extern/fajran-npTuioClient/src/plugin.cpp
changeset 28 9ccef81f02ab
parent 27 6c08d4d7219e
equal deleted inserted replaced
27:6c08d4d7219e 28:9ccef81f02ab
    13 // 
    13 // 
    14 // You should have received a copy of the GNU General Public License
    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
    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
    16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17 
    17 
       
    18 /*
       
    19     Modified by alexandre.bastien@iri.centrepompidou.fr to manage TUIO strings.
       
    20 */
       
    21 
    18 #include "plugin.h"
    22 #include "plugin.h"
    19 
    23 
    20 #include <cstring>
    24 #include <cstring>
    21 #include <iostream>
    25 #include <iostream>
    22 #include <sstream>
    26 #include <sstream>
    38 
    42 
    39 static std::set<nsPluginInstance*> instances;
    43 static std::set<nsPluginInstance*> instances;
    40 
    44 
    41 void tuio_callback(TuioEventData data)
    45 void tuio_callback(TuioEventData data)
    42 {
    46 {
    43 	std::set<nsPluginInstance*>::iterator iter;
    47     std::set<nsPluginInstance*>::iterator iter;
    44 	for (iter = instances.begin(); iter != instances.end(); iter++) {
    48     for (iter = instances.begin(); iter != instances.end(); iter++) {
    45 		(*iter)->event(data);
    49         (*iter)->event(data);
    46 	}
    50     }
    47 }
    51 }
    48 
    52 
    49 void
    53 void
    50 PR_CALLBACK Destructor(void * /* data */)
    54 PR_CALLBACK Destructor(void * /* data */)
    51 {
    55 {
    84         return NPERR_NO_ERROR;
    88         return NPERR_NO_ERROR;
    85     }
    89     }
    86 
    90 
    87     plugInitialized = TRUE;
    91     plugInitialized = TRUE;
    88 
    92 
    89 	tuio_start(80);
    93     tuio_start(80);
    90 
    94 
    91     return NPERR_NO_ERROR;
    95     return NPERR_NO_ERROR;
    92 }
    96 }
    93 
    97 
    94 /// \brief Shutdown the plugin
    98 /// \brief Shutdown the plugin
   110     }
   114     }
   111 
   115 
   112     plugInitialized = FALSE;
   116     plugInitialized = FALSE;
   113 #endif
   117 #endif
   114 
   118 
   115 	tuio_stop();
   119     tuio_stop();
   116 }
   120 }
   117 
   121 
   118 
   122 
   119 
   123 
   120 /// \brief Retrieve values from the plugin for the Browser
   124 /// \brief Retrieve values from the plugin for the Browser
   185 /// \brief Constructor
   189 /// \brief Constructor
   186 nsPluginInstance::nsPluginInstance(nsPluginCreateData* data)
   190 nsPluginInstance::nsPluginInstance(nsPluginCreateData* data)
   187     :
   191     :
   188     nsPluginInstanceBase(),
   192     nsPluginInstanceBase(),
   189     _instance(data->instance),
   193     _instance(data->instance),
   190 	_port(3333),
   194     _port(3333),
   191 	_callback("tuio_callback")
   195     _callback("tuio_callback")
   192 {
   196 {
   193     for (size_t i=0, n=data->argc; i<n; ++i)
   197     for (size_t i=0, n=data->argc; i<n; ++i)
   194     {
   198     {
   195         std::string name, val;
   199         std::string name, val;
   196 
   200 
   213         {
   217         {
   214             _port = atoi(val.c_str());
   218             _port = atoi(val.c_str());
   215         }
   219         }
   216     }
   220     }
   217 
   221 
   218 	instances.insert(this);
   222     instances.insert(this);
   219 }
   223 }
   220 
   224 
   221 /// \brief Destructor
   225 /// \brief Destructor
   222 nsPluginInstance::~nsPluginInstance()
   226 nsPluginInstance::~nsPluginInstance()
   223 {
   227 {
   224 	instances.erase(this);
   228     instances.erase(this);
   225 }
   229 }
   226 
   230 
   227 /// \brief Initialize an instance of the plugin object
   231 /// \brief Initialize an instance of the plugin object
   228 /// 
   232 /// 
   229 /// This methods initializes the plugin object, and is called for
   233 /// This methods initializes the plugin object, and is called for
   230 /// every movie that gets played. This is where the movie playing
   234 /// every movie that gets played. This is where the movie playing
   231 /// specific initialization goes.
   235 /// specific initialization goes.
   232 NPBool
   236 NPBool
   233 nsPluginInstance::init(NPWindow* aWindow)
   237 nsPluginInstance::init(NPWindow* aWindow)
   234 {
   238 {
   235 	D("[ns] init");
   239     D("[ns] init");
   236     return TRUE;
   240     return TRUE;
   237 }
   241 }
   238 
   242 
   239 /// \brief Shutdown an instantiated object
   243 /// \brief Shutdown an instantiated object
   240 ///
   244 ///
   242 /// played. This is where the movie playing specific shutdown code
   246 /// played. This is where the movie playing specific shutdown code
   243 /// goes.
   247 /// goes.
   244 void
   248 void
   245 nsPluginInstance::shut()
   249 nsPluginInstance::shut()
   246 {
   250 {
   247 	D("[ns] shut");
   251     D("[ns] shut");
   248 }
   252 }
   249 
   253 
   250 NPError
   254 NPError
   251 nsPluginInstance::SetWindow(NPWindow* aWindow)
   255 nsPluginInstance::SetWindow(NPWindow* aWindow)
   252 {
   256 {
   253 	D("[ns] SetWindow");
   257     D("[ns] SetWindow");
   254 
   258 
   255 	if(!aWindow)
   259     if(!aWindow)
   256 	{
   260     {
   257 		return NPERR_INVALID_PARAM;
   261         return NPERR_INVALID_PARAM;
   258 	}
   262     }
   259 
   263 
   260 	return NPERR_NO_ERROR;
   264     return NPERR_NO_ERROR;
   261 }
   265 }
   262 
   266 
   263 NPError
   267 NPError
   264 nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
   268 nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
   265 {
   269 {
   266 	return NS_PluginGetValue(aVariable, aValue);
   270     return NS_PluginGetValue(aVariable, aValue);
   267 }
   271 }
   268 
   272 
   269 /// \brief Write a status message
   273 /// \brief Write a status message
   270 ///
   274 ///
   271 /// This writes a status message to the status line at the bottom of
   275 /// This writes a status message to the status line at the bottom of
   272 /// the browser window and the console firefox was started from.
   276 /// the browser window and the console firefox was started from.
   273 NPError
   277 NPError
   274 nsPluginInstance::WriteStatus(const char *msg) const
   278 nsPluginInstance::WriteStatus(const char *msg) const
   275 {
   279 {
   276 	NPN_Status(_instance, msg);
   280     NPN_Status(_instance, msg);
   277 	std::cout << msg << std::endl;
   281     std::cout << msg << std::endl;
   278 
   282 
   279 	return NPERR_NO_ERROR;
   283     return NPERR_NO_ERROR;
   280 }
   284 }
   281 
   285 
   282 NPError
   286 NPError
   283 nsPluginInstance::NewStream(NPMIMEType /*type*/, NPStream* stream,
   287 nsPluginInstance::NewStream(NPMIMEType /*type*/, NPStream* stream,
   284                             NPBool /*seekable*/, uint16* /*stype*/)
   288                             NPBool /*seekable*/, uint16* /*stype*/)
   285 {
   289 {
   286 	D("[ns] NewStream");
   290     D("[ns] NewStream");
   287 	return NPERR_NO_ERROR;
   291     return NPERR_NO_ERROR;
   288 }
   292 }
   289 
   293 
   290 NPError
   294 NPError
   291 nsPluginInstance::DestroyStream(NPStream* /*stream*/, NPError /*reason*/)
   295 nsPluginInstance::DestroyStream(NPStream* /*stream*/, NPError /*reason*/)
   292 {
   296 {
   293 	D("[ns] DestroyStream");
   297     D("[ns] DestroyStream");
   294 	return NPERR_NO_ERROR;
   298     return NPERR_NO_ERROR;
   295 }
   299 }
   296 
   300 
   297 int32_t
   301 int32_t
   298 nsPluginInstance::WriteReady(NPStream* /* stream */ )
   302 nsPluginInstance::WriteReady(NPStream* /* stream */ )
   299 {
   303 {
   300 	D("[ns] WriteReady");
   304     D("[ns] WriteReady");
   301 	return 0x0fffffff;
   305     return 0x0fffffff;
   302 }
   306 }
   303 
   307 
   304 int32_t
   308 int32_t
   305 nsPluginInstance::Write(NPStream* /*stream*/, int32_t /*offset*/, int32_t len,
   309 nsPluginInstance::Write(NPStream* /*stream*/, int32_t /*offset*/, int32_t len,
   306         void* buffer)
   310         void* buffer)
   307 {
   311 {
   308 	D("[ns] Write: len=" << len);
   312     D("[ns] Write: len=" << len);
   309 	return len;
   313     return len;
   310 }
   314 }
   311 
   315 
   312 typedef struct {
   316 typedef struct {
   313 	NPP instance;
   317     NPP instance;
   314 	TuioEventData data;
   318     TuioEventData data;
   315 } Event;
   319 } Event;
   316 
   320 
   317 void test(void* ev)
   321 void test(void* ev)
   318 {
   322 {
   319 	D("ev=" << ev);
   323     D("ev=" << ev);
   320 	Event* event = (Event*)ev;
   324     Event* event = (Event*)ev;
   321 	D("event=" << event);
   325     D("event=" << event);
   322 	std::stringstream ss;
   326     std::stringstream ss;
   323 	ss << "javascript:tuio_callback(";
   327     ss << "javascript:tuio_callback(";
   324 	ss << event->data.type << ", ";
   328     ss << event->data.type << ", ";
   325 	ss << event->data.sid << ", ";
   329     ss << event->data.sid << ", ";
   326 	ss << event->data.fid << ", ";
   330     ss << event->data.fid << ", ";
   327 	ss << event->data.x << ", ";
   331     ss << event->data.x << ", ";
   328 	ss << event->data.y << ", ";
   332     ss << event->data.y << ", ";
   329 	ss << event->data.z << ", ";
   333     ss << event->data.z << ", ";
   330 	ss << event->data.a << ", ";
   334     ss << event->data.a << ", ";
   331 	if(event->data.code == NULL || event->data.code == "")
   335     if(event->data.code == NULL || event->data.code == "")
   332 		ss << "\"\");";
   336         ss << "\"\");";
   333 	else
   337     else
   334 		ss << "\"" << event->data.code << "\"" << ");";
   338         ss << "\"" << event->data.code << "\"" << ");";
   335 	
   339     
   336 	NPN_GetURL(event->instance, ss.str().c_str(), "_self");
   340     NPN_GetURL(event->instance, ss.str().c_str(), "_self");
   337 
   341 
   338 	delete event;
   342     delete event;
   339 }
   343 }
   340 
   344 
   341 void nsPluginInstance::event(TuioEventData data)
   345 void nsPluginInstance::event(TuioEventData data)
   342 {
   346 {
   343 	D("[event] callback: type=" << data.type
   347     D("[event] callback: type=" << data.type
   344 	<< ", sid=" << data.sid << ", fid=" << data.fid
   348     << ", sid=" << data.sid << ", fid=" << data.fid
   345 	<< ", x=" << data.x << ", y=" << data.y
   349     << ", x=" << data.x << ", y=" << data.y
   346 	<< ", z=" << data.z << ", a=" << data.a
   350     << ", z=" << data.z << ", a=" << data.a
   347 	<< ", code=" << "\"" << data.code << "\"");
   351     << ", code=" << "\"" << data.code << "\"");
   348 	
   352     
   349 	Event* ev = new Event();
   353     Event* ev = new Event();
   350 	ev->instance = _instance;
   354     ev->instance = _instance;
   351 	ev->data = data;
   355     ev->data = data;
   352 
   356 
   353 	NPN_PluginThreadAsyncCall(_instance, test, ev);
   357     NPN_PluginThreadAsyncCall(_instance, test, ev);
   354 }
   358 }
   355 
   359 
   356 // Local Variables:
   360 // Local Variables:
   357 // mode: C++
   361 // mode: C++
   358 // indent-tabs-mode: t
   362 // indent-tabs-mode: t