libGizmod/GizmoLinuxInputDevice.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #include "GizmoLinuxInputDevice.hpp"
00030 #include "GizmoKeyDefs.hpp"
00031 #include "../libH/Debug.hpp"
00032 #include "../libH/Exception.hpp"
00033 #include "../libH/UtilTime.hpp"
00034 #include "../libH/Util.hpp"
00035 #include <boost/python/list.hpp>
00036 #include <boost/python/object.hpp>
00037 #include <boost/python/detail/api_placeholder.hpp>
00038 #include <boost/python/extract.hpp>
00039 #include <boost/bind.hpp>
00040 #include <list>
00041 
00042 using namespace std;
00043 using namespace boost;
00044 using namespace boost::python;
00045 using namespace H;
00046 using namespace Gizmod;
00047 
00049 // Type Defs
00051 
00053 // Construction
00055 
00059 GizmoLinuxInputDevice::GizmoLinuxInputDevice(const H::DeviceInfo & DeviceInfo) {
00060         mDeviceInfo = DeviceInfo;
00061         mSendNullEvents = false;
00062         mLastEventTime = 0;
00063         mMinTimeBetweenEvents = 0;
00064 }
00065 
00069 GizmoLinuxInputDevice::GizmoLinuxInputDevice() {
00070         mSendNullEvents = false;
00071         mLastEventTime = 0;
00072         mMinTimeBetweenEvents = 0;
00073 }
00074 
00078 GizmoLinuxInputDevice::~GizmoLinuxInputDevice() {
00079 }
00080 
00082 // Class Body
00084 
00090 void GizmoLinuxInputDevice::buildInputEventsVectorFromBuffer(std::vector<struct input_event> & EventVector, H::DynamicBuffer<char> const & Buffer) {
00091         DynamicBufferConverter<char, struct input_event>::convert(EventVector, Buffer);
00092 }
00093 
00108 bool GizmoLinuxInputDevice::createEventRaw(int Type, int Code, int Value) {
00109         struct input_event ev[2];
00110         memset(&ev, 0, sizeof(struct input_event) * 2);
00111         ev[0].type = Type;
00112         ev[0].code = Code;
00113         ev[0].value = Value;
00114         if (write(mDeviceInfo.FileDescriptor, &ev, sizeof(struct input_event) * 2) == -1)
00115                 return false;
00116         return true;
00117 }
00118 
00133 bool GizmoLinuxInputDevice::createEventPress(int Type, int Code) {
00134         if (!createEventRaw(Type, Code, 1))
00135                 return false;
00136         if (!createEventRaw(Type, Code, 0))
00137                 return false;
00138         return true;
00139 }
00140 
00157 bool GizmoLinuxInputDevice::createEventPressMod(int Type, int Code, boost::python::object Modifiers) {
00158         for (int lp = 0; lp < len(Modifiers); lp ++) 
00159                 if (!createEventRaw(Type, extract<int>(Modifiers[lp]), 1))
00160                         return false;
00161         
00162         if (!createEventRaw(Type, Code, 1))
00163                 return false;
00164         if (!createEventRaw(Type, Code, 0))
00165                 return false;
00166         
00167         for (int lp = 0; lp < len(Modifiers); lp ++) 
00168                 if (!createEventRaw(Type, extract<int>(Modifiers[lp]), 0))
00169                         return false;
00170         
00171         return true;
00172 }
00173 
00189 bool GizmoLinuxInputDevice::createEvents(int Type, int Code, int Value, int NumWrites) {
00190         struct input_event ev;
00191         memset(&ev, 0, sizeof(struct input_event));
00192         ev.type = Type;
00193         ev.code = Code;
00194         ev.value = Value;
00195         for (int lp = 0; lp < NumWrites; lp ++) 
00196                 if (write(mDeviceInfo.FileDescriptor, &ev, sizeof(struct input_event)) == -1)
00197                         return false;
00198         memset(&ev, 0, sizeof(struct input_event));
00199         if (write(mDeviceInfo.FileDescriptor, &ev, sizeof(struct input_event)) == -1)
00200                 return false;
00201         return true;
00202 }
00203 
00208 bool GizmoLinuxInputDevice::getSendNullEvents() const {
00209         return mSendNullEvents;
00210 }
00211 
00222 bool GizmoLinuxInputDevice::grabExclusiveAccess(bool Grab) {
00223         if (ioctl(mDeviceInfo.FileDescriptor, EVIOCGRAB, Grab ? 1 : 0)) {
00224                 cerr << "Device [" << mDeviceInfo.DeviceName <<"] Exclusive Access Grab Failed!" << endl;
00225                 return false;
00226         }
00227 
00228         cdbg << "Device [" << mDeviceInfo.DeviceName <<"] Exclusive Access Granted" << endl;
00229         return true;
00230 }
00231 
00236 bool GizmoLinuxInputDevice::processEvent() {
00237         if (UtilTime::getTicks() - mLastEventTime < mMinTimeBetweenEvents)
00238                 return false;
00239         mLastEventTime = UtilTime::getTicks();
00240         return true;
00241 }
00242 
00249 bool GizmoLinuxInputDevice::remapKey(int CurCode, int NewCode) {
00250         int codes[2];
00251         codes[0] = CurCode;
00252         codes[1] = NewCode;
00253         if (ioctl(mDeviceInfo.FileDescriptor, EVIOCSKEYCODE, codes)) {
00254                 cerr << "Device [" << mDeviceInfo.DeviceName <<"] Failed to Remap Key [" << CurCode << "]" << " to [" << NewCode << "]" << endl;
00255                 return false;
00256         }
00257 
00258         cdbg << "Device [" << mDeviceInfo.DeviceName <<"] Key [" << CurCode << "]" << " Remapped to [" << NewCode << "]" << endl;
00259         return true;
00260 }
00261 
00269 void GizmoLinuxInputDevice::setMinimumTimeBetweenEvents(float Seconds) {
00270         mMinTimeBetweenEvents = (long) (Seconds * 1000000.0f);
00271 }
00272 
00277 void GizmoLinuxInputDevice::setSendNullEvents(bool SendNull) {
00278         mSendNullEvents = SendNull;
00279 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3