DeviceInterface.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: 2006-12-17 09:05:59 -0600 (Sun, 17 Dec 2006) $
00028  * Version:       $Revision: 19665 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <iomanip>
00034 
00035 #include <gadget/gadgetConfig.h>
00036 #include <gadget/Type/DeviceInterface.h>
00037 #include <gadget/InputManager.h>
00038 #include <gadget/Type/Proxy.h>
00039 
00040 namespace gadget
00041 {
00042 
00043 BaseDeviceInterface::BaseDeviceInterface()
00044    : mProxyPtr(NULL)
00045    , mProxyName("UnInitialized")
00046    , mNameSet(false)
00047 {
00048    // Keep reference to the interface.
00049    BaseDeviceInterface::addDevInterface(this);
00050 }
00051 
00052 BaseDeviceInterface::~BaseDeviceInterface()
00053 {
00054    // Remove it from the list of active interfaces.
00055    BaseDeviceInterface::removeDevInterface(this);
00056 }
00057 
00058 
00059 BaseDeviceInterface::BaseDeviceInterface(const BaseDeviceInterface& other)
00060    : mProxyPtr(other.mProxyPtr)
00061    , mProxyName(other.mProxyName)
00062    , mNameSet(other.mNameSet)
00063 {
00064    // Keep reference to the interface.
00065    BaseDeviceInterface::addDevInterface(this);
00066 }
00067 
00068 void BaseDeviceInterface::init(const std::string& proxyName)
00069 {
00070    mProxyName = proxyName;    // Set the name
00071    mNameSet = true;
00072    refresh();                 // Refresh the name
00073 }
00074 
00075 // NOTE: If the interface does not have an initialized mProxyName, then
00076 // don't try to refresh it.
00077 void BaseDeviceInterface::refresh()
00078 {
00079    Proxy* prev_proxy_ptr = mProxyPtr;    // Keep track of previous value
00080 
00081    // If it is not initialized, then don't try
00082    if ( ! mNameSet )
00083    {
00084       return;
00085    }
00086 
00087    mProxyPtr = InputManager::instance()->getProxy(mProxyName);
00088 
00089    if (NULL == mProxyPtr)
00090    {
00091       vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_LVL)
00092          << "WARNING: DeviceInterface::refresh: could not find proxy: "
00093          << mProxyName.c_str() << std::endl << vprDEBUG_FLUSH;
00094       vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_LVL)
00095          << "         Make sure the proxy exists in the current configuration."
00096          << std::endl << vprDEBUG_FLUSH;
00097       vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_LVL)
00098          << "   referencing device interface will be stupefied to point at dummy device."
00099          << std::endl << vprDEBUG_FLUSH;
00100    }
00101    // ASSERT: We have just gotten a valid proxy to point to
00102    else if((NULL != mProxyPtr) && (NULL == prev_proxy_ptr))
00103    {
00104       const int item_width(25+12);
00105       //const int type_width(20);
00106 
00107       //vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_STATUS_LVL)
00108       //   << "DeviceInterface now able to find proxy: "
00109       //   << mProxyName.c_str() << "               [ "
00110       //   << clrSetNORM(clrGREEN) << "OK" << clrRESET << " ]"
00111       //   << std::endl << vprDEBUG_FLUSH;
00112 
00113       //std::string device_name("");
00114       //Input* deviceptr = mProxyPtr->getProxiedInputDevice();
00115       //if (NULL != deviceptr)
00116       //{
00117       //   device_name = deviceptr->getInstanceName();
00118       //}
00119 
00120       vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_STATUS_LVL)
00121          << "DeviceInterface found proxy: "
00122          << std::setiosflags(std::ios::right)
00123          << std::setfill(' ') << std::setw(item_width) << mProxyName
00124          << std::resetiosflags(std::ios::right) << "  ";
00125       vprDEBUG_CONTnl(vprDBG_ALL,vprDBG_CONFIG_STATUS_LVL)
00126          << "[ " << clrSetNORM(clrGREEN) << "OK" << clrRESET << " ]";
00127       vprDEBUG_CONTnl(vprDBG_ALL,vprDBG_CONFIG_STATUS_LVL)
00128          << std::endl << vprDEBUG_FLUSH;
00129    }
00130 }
00131 
00132 void BaseDeviceInterface::addDevInterface(BaseDeviceInterface* dev)
00133 {
00134    mAllocatedDevices.push_back(dev);
00135 }
00136 
00137 void BaseDeviceInterface::removeDevInterface(BaseDeviceInterface* dev)
00138 {
00139    // Attempt to find the device, if found, erase it, if not, then assert
00140    std::vector<BaseDeviceInterface*>::iterator found_dev =
00141       std::find(mAllocatedDevices.begin(), mAllocatedDevices.end(), dev);
00142    vprASSERT(found_dev != mAllocatedDevices.end() &&
00143              "Tried to remove non-registered interface");
00144 
00145    if ( mAllocatedDevices.end() != found_dev )
00146    {
00147       mAllocatedDevices.erase(found_dev);
00148    }
00149 }
00150 
00151 void BaseDeviceInterface::refreshAllInterfaces()
00152 {
00153    for ( unsigned int i = 0; i < mAllocatedDevices.size(); ++i )
00154    {
00155       BaseDeviceInterface* dev = mAllocatedDevices[i];
00156       dev->refresh();
00157    }
00158 }
00159 
00160 void BaseDeviceInterface::refreshAllDevices()
00161 {
00162    vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL)
00163       << clrOutBOLD(clrYELLOW, "NOTICE:")
00164       << " gadget::BaseDeviceInterface::refreshAllDevices() has been replaced"
00165       << std::endl;
00166    vprDEBUG_NEXTnl(vprDBG_ALL, vprDBG_WARNING_LVL)
00167       << "by gadget::BaseDeviceInterface::refreshAllInterfaces()."
00168       << std::endl << vprDEBUG_FLUSH;
00169    refreshAllInterfaces();
00170 }
00171 
00172 std::vector<BaseDeviceInterface*> BaseDeviceInterface::mAllocatedDevices;
00173 
00174 } // End of gadget namespace

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