KeyboardMouseDevice.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-01-01 14:59:16 -0600 (Sat, 01 Jan 2005) $
00028  * Version:       $Revision: 16524 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <gadget/Util/Debug.h>
00034 #include <gadget/Devices/KeyboardMouseDevice/KeyboardMouseDevice.h>
00035 #include <jccl/Config/ConfigElement.h>
00036 
00037 namespace gadget
00038 {
00039 vprSingletonImp(KeyboardMouseDevice::KeyboardMouseDeviceRegistry);
00040 
00041 std::string KeyboardMouseDevice::getElementType()
00042 {
00043    return std::string("keyboard_mouse_device");
00044 }
00045 
00046 bool KeyboardMouseDevice::config(jccl::ConfigElementPtr e)
00047 {
00048    unsigned required_definition_ver(1);
00049 
00050    if(e->getVersion() < required_definition_ver)
00051    {
00052       vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
00053          << clrOutBOLD(clrRED, "ERROR")
00054          << ": [gadget::KeyboardMouseDevice::config()] Element named '"
00055          << e->getName() << "'" << std::endl << vprDEBUG_FLUSH;
00056       vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
00057          << "is version " << e->getVersion()
00058          << ", but we require at least version " << required_definition_ver
00059          << ". Ignoring.\n" << vprDEBUG_FLUSH;
00060       return false;
00061    }
00062 
00063    if ( ! (Input::config(e) && KeyboardMouse::config(e) ) )
00064    {
00065       return false;
00066    }
00067 
00068    mMouseSensitivity = e->getProperty<float>("mouse_sensitivity");
00069    if (0.0f == mMouseSensitivity)
00070    {
00071       mMouseSensitivity = 0.5;
00072    }
00073 
00074    for ( int i = 0; i < gadget::LAST_KEY; ++i )
00075    {
00076       mRealkeys[i] = mKeys[i] = mCurKeys[i];
00077    }
00078 
00079    KeyboardMouseDeviceRegistry::KeyboardMouseDeviceInfo event_source_info;
00080    event_source_info.mSourceName = e->getName();
00081    event_source_info.mKeyboardMouseDevice = this;
00082 
00083    KeyboardMouseDeviceRegistry::instance()->addKeyboardMouseDevice(e->getName(), event_source_info);
00084 
00085    return true;
00086 }
00087 
00088 void KeyboardMouseDevice::updateData()
00089 {
00090 vpr::Guard<vpr::Mutex> guard(mKeysLock);      // Lock access to the mKeys array
00091 
00092 // Scale mouse values based on sensitivity
00093    mKeys[gadget::MOUSE_POSX] = int(float(mKeys[gadget::MOUSE_POSX]) * mMouseSensitivity);
00094    mKeys[gadget::MOUSE_NEGX] = int(float(mKeys[gadget::MOUSE_NEGX]) * mMouseSensitivity);
00095    mKeys[gadget::MOUSE_POSY] = int(float(mKeys[gadget::MOUSE_POSY]) * mMouseSensitivity);
00096    mKeys[gadget::MOUSE_NEGY] = int(float(mKeys[gadget::MOUSE_NEGY]) * mMouseSensitivity);
00097 
00098    /*
00099    vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00100       << "gadget::KeyboardMouseDevice::updateData:" << mInstName << " -- "
00101       << "mouse_keys: px:" << mKeys[gadget::MOUSE_POSX]
00102       << " nx:" << mKeys[gadget::MOUSE_NEGX]
00103       << " py:" << mKeys[gadget::MOUSE_POSY]
00104       << " ny:" << mKeys[gadget::MOUSE_NEGY]
00105       << std::endl << vprDEBUG_FLUSH;
00106    */
00107 
00108    // Copy over values
00109    for ( unsigned int i = 0; i < gadget::LAST_KEY; ++i )
00110    {
00111       mCurKeys[i] = mKeys[i];
00112    }
00113 
00114    // Re-initialize the mKeys based on current key state in realKeys
00115    // Set the initial state of the mKey key counts based on the current state
00116    // of the system this is to ensure that if a key is still down, we get at
00117    // least one event for it.
00118    for ( unsigned int j = 0; j < gadget::LAST_KEY; ++j )
00119    {
00120       mKeys[j] = mRealkeys[j];
00121    }
00122 
00123    updateEventQueue();
00124 }
00126 bool KeyboardMouseDevice::KeyboardMouseDeviceRegistry::addKeyboardMouseDevice(const std::string& name,
00127                         KeyboardMouseDevice::KeyboardMouseDeviceRegistry::KeyboardMouseDeviceInfo winInfo)
00128 {
00129    event_source_map_t::iterator found_window = mKeyboardMouseDeviceMap.find(name);
00130    if(found_window != mKeyboardMouseDeviceMap.end())
00131    {
00132       vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_WARNING_LVL)
00133          << "Warning: Attempted to add event source twice. named '"
00134          << name << "'. Ignoring second.\n" << vprDEBUG_FLUSH;
00135       return false;
00136    }
00137    else
00138    {
00139       vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
00140          << "Adding event source: '" << name << "'" << std::endl << vprDEBUG_FLUSH;
00141       mKeyboardMouseDeviceMap[name] = winInfo;
00142       return true;
00143    }
00144 }
00145 
00147 void KeyboardMouseDevice::KeyboardMouseDeviceRegistry::removeKeyboardMouseDevice(const std::string& name)
00148 {
00149    unsigned num_erased = mKeyboardMouseDeviceMap.erase(name);
00150    if(0 == num_erased)
00151    {
00152       vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_WARNING_LVL)
00153          << "Warning: Attempted to remove event source not found. named '"
00154          << name << "'. \n" << vprDEBUG_FLUSH;
00155    }
00156 }
00157 
00158 bool KeyboardMouseDevice::KeyboardMouseDeviceRegistry::getKeyboardMouseDevice(const std::string& name,
00159                                                 KeyboardMouseDeviceInfo& winInfo)
00160 {
00161    event_source_map_t::iterator found_window = mKeyboardMouseDeviceMap.find(name);
00162    if(found_window != mKeyboardMouseDeviceMap.end())
00163    {
00164       winInfo = (*found_window).second;
00165       return true;
00166    }
00167    else
00168    {
00169       return false;
00170    }
00171 }
00172 
00173 }

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