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