00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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);
00091
00092
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
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 for ( unsigned int i = 0; i < gadget::LAST_KEY; ++i )
00110 {
00111 mCurKeys[i] = mKeys[i];
00112 }
00113
00114
00115
00116
00117
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 }