DeviceFactory.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile$
00027  * Date modified: $Date: 2005-06-28 16:25:24 -0500 (Tue, 28 Jun 2005) $
00028  * Version:       $Revision: 17789 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <gadget/gadgetConfig.h>
00034 
00035 #include <iomanip>
00036 #include <typeinfo>
00037 
00038 #include <vpr/vpr.h>
00039 #include <jccl/Config/ConfigElement.h>
00040 #include <gadget/Type/DeviceFactory.h>
00041 #include <gadget/Type/DeviceConstructor.h>
00042 
00043 // Sims
00044 #include <gadget/Devices/Sim/SimAnalog.h>
00045 #include <gadget/Devices/Sim/SimDigital.h>
00046 #include <gadget/Devices/Sim/SimPosition.h>
00047 #include <gadget/Devices/Sim/SimGloveGesture.h>
00048 //#include <gadget/Devices/Sim/SimKeyboardDigital.h>
00049 #include <gadget/Devices/Sim/SimRelativePosition.h>
00050 #include <gadget/Devices/Sim/SimSetablePosition.h>
00051 #include <gadget/Devices/Sim/SimDigitalGlove.h>
00052 #include <gadget/Devices/KeyboardMouseDevice/KeyboardMouseDevice.h>
00053 
00054 #if defined(VPR_OS_Windows)
00055 #  include <gadget/Devices/KeyboardMouseDevice/InputWindowWin32.h>
00056 #elif defined(VPR_OS_Darwin) && ! defined(GADGET_USE_X11)
00057 #  include <gadget/Devices/KeyboardMouseDevice/InputWindowOSX.h>
00058 #else
00059 //#  include <jccl/RTRC/DependencyManager.h>
00060 #  include <gadget/Devices/KeyboardMouseDevice/InputWindowXWin.h>
00061 #endif
00062 
00063 /* Physical devices */
00064 #ifdef STATIC_DRIVERS
00065 
00066 /* PThread Dependant Driver */
00067 #ifdef GADGET_HAVE_DTK
00068 #   include <gadget/Devices/Open/DTK/DTK.h>
00069 #endif
00070 
00071 #endif /* STATIC_DRIVERS */
00072 
00073 #include <gadget/Util/Debug.h>
00074 
00075 namespace gadget
00076 {
00077 
00078 // Initialize the singleton ptr
00079 vprSingletonImpWithInitFunc(DeviceFactory, loadKnownDevices);
00080 
00084 void DeviceFactory::loadKnownDevices()
00085 {
00086    gadget::InputManager* input_mgr = gadget::InputManager::instance();
00087 
00088 #ifdef STATIC_DRIVERS
00089 
00090 #ifdef GADGET_HAVE_DTK
00091    DeviceConstructor<DTK>* dtk_wrapper = new DeviceConstructor<DTK>;
00092    if( (NULL == dtk_wrapper))
00093    {
00094       vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL) << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n" << vprDEBUG_FLUSH;
00095    }
00096 #endif
00097 
00098 #endif /* STATIC_DRIVERS */
00099 
00100    // XXX: Memory leaks!
00101    DeviceConstructor<SimAnalog>* sim_analog = new DeviceConstructor<SimAnalog>(input_mgr);
00102    DeviceConstructor<SimDigital>* sim_digital = new DeviceConstructor<SimDigital>(input_mgr);
00103    DeviceConstructor<SimPosition>* sim_position = new DeviceConstructor<SimPosition>(input_mgr);
00104    //DeviceConstructor<SimKeyboardDigital>* sim_keyboard_digital = new DeviceConstructor<SimKeyboardDigital>(input_mgr);
00105    DeviceConstructor<SimSetablePosition>* sim_setable = new DeviceConstructor<SimSetablePosition>(input_mgr);
00106    DeviceConstructor<SimRelativePosition>* sim_relative = new DeviceConstructor<SimRelativePosition>(input_mgr);
00107 
00108    DeviceConstructor<SimGloveGesture>* sim_glove = new DeviceConstructor<SimGloveGesture>(input_mgr);
00109    DeviceConstructor<SimDigitalGlove>* simpinch_glove = new DeviceConstructor<SimDigitalGlove>(input_mgr);
00110 
00111    if( (NULL == sim_analog)   ||
00112        (NULL == sim_digital)  ||
00113        (NULL == sim_position) ||
00114        (NULL == sim_glove)    ||
00115        (NULL == simpinch_glove) ||
00116        (NULL == sim_setable) ||
00117        (NULL == sim_relative) )
00118    {
00119       vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL) << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n" << vprDEBUG_FLUSH;
00120    }
00121 
00122    DeviceConstructor<KeyboardMouseDevice>* keyboard_device =
00123       new DeviceConstructor<KeyboardMouseDevice>(input_mgr);
00124    if( (NULL == keyboard_device))
00125    {
00126       vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00127          << clrOutBOLD(clrRED,"ERROR:") << "Failed to load the known device KeyboardMouseDevice."
00128          << std::endl << vprDEBUG_FLUSH;
00129    }
00130 #if defined(VPR_OS_Windows)
00131    DeviceConstructor<InputWindowWin32>* key_win32 =
00132       new DeviceConstructor<InputWindowWin32>(input_mgr);
00133    if( (NULL == key_win32))
00134    {
00135       vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00136          << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n"
00137          << vprDEBUG_FLUSH;
00138    }
00139 #elif defined(VPR_OS_Darwin) && ! defined(GADGET_USE_X11)
00140    DeviceConstructor<InputWindowOSX>* osx_keyboard =
00141       new DeviceConstructor<InputWindowOSX>(input_mgr);
00142    if( (NULL == osx_keyboard) )
00143    {
00144       vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00145          << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n"
00146          << vprDEBUG_FLUSH;
00147    }
00148 #else
00149    DeviceConstructor<InputWindowXWin>* xwin_key =
00150       new DeviceConstructor<InputWindowXWin>(input_mgr);
00151    if( (NULL == xwin_key) )
00152    {
00153       vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00154          << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n"
00155          << vprDEBUG_FLUSH;
00156    }
00157 #endif
00158 
00159 }
00160 
00161 void DeviceFactory::registerDevice(DeviceConstructorBase* constructor)
00162 {
00163    vprASSERT(constructor != NULL);
00164    mConstructors.push_back(constructor);     // Add the constructor to the list
00165    vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
00166       << "gadget::DeviceFactory: Registered: "
00167       << std::setiosflags(std::ios::right) << std::setw(25)
00168       << std::setfill(' ') << constructor->getElementType()
00169       << std::setiosflags(std::ios::right)
00170       //<< "   :" << (void*)constructor
00171       << "  type: " << typeid(*constructor).name() << std::endl
00172       << vprDEBUG_FLUSH;
00173 }
00174 
00175 // Simply query all device constructors registered looking
00176 // for one that knows how to load the device
00177 bool DeviceFactory::recognizeDevice(jccl::ConfigElementPtr element)
00178 {
00179    return ! (findConstructor(element) == -1);
00180 }
00181 
00185 Input* DeviceFactory::loadDevice(jccl::ConfigElementPtr element)
00186 {
00187    vprASSERT(recognizeDevice(element));
00188 
00189    int index(findConstructor(element));
00190 
00191    Input* new_dev;
00192    DeviceConstructorBase* constructor = mConstructors[index];
00193 
00194    vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
00195       << "gadget::DeviceFactory::loadDevice: Loading device: "
00196       << element->getID() << "  with: " << typeid(*constructor).name()
00197       << std::endl << vprDEBUG_FLUSH;
00198 
00199    new_dev = constructor->createDevice(element);
00200    return new_dev;
00201 }
00202 
00203 int DeviceFactory::findConstructor(jccl::ConfigElementPtr element)
00204 {
00205    const std::string element_type(element->getID());
00206 
00207    for ( unsigned int i = 0; i < mConstructors.size(); ++i )
00208    {
00209       // Get next constructor
00210       DeviceConstructorBase* construct = mConstructors[i];
00211       vprASSERT(construct != NULL);
00212 
00213       if(construct->getElementType() == element_type)
00214       {
00215          return i;
00216       }
00217    }
00218 
00219    return -1;
00220 }
00221 
00222 
00223 void DeviceFactory::debugDump()
00224 {
00225    vprDEBUG_OutputGuard(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL,
00226       std::string("gadget::DeviceFactory::debugDump\n"),
00227       std::string("------ END DUMP ------\n"));
00228    vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL) << "num constructors:"
00229                                    << mConstructors.size() << "\n"
00230                                    << vprDEBUG_FLUSH;
00231 
00232    for(unsigned int cNum=0;cNum<mConstructors.size();cNum++)
00233    {
00234       DeviceConstructorBase* dev_constr = mConstructors[cNum];
00235       vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL)
00236          << cNum << ": Constructor:" << (void*)dev_constr
00237          << "   type:" << typeid(*dev_constr).name() << "\n" << vprDEBUG_FLUSH;
00238       vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL)
00239          << "   recog:" << dev_constr->getElementType() << "\n"
00240          << vprDEBUG_FLUSH;
00241    }
00242 }
00243 
00244 } // End of gadget namespace

Generated on Thu Jan 4 10:41:55 2007 for Gadgeteer by  doxygen 1.5.1