input_device_server.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000-2002 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #include "stdmisc.h"
00025 #include "nel/misc/input_device_server.h"
00026 #include "nel/misc/input_device.h"
00027 
00028 
00029 
00030 namespace NLMISC
00031 {
00032 //=======================================================================
00033 void    CInputDeviceServer::registerDevice(IInputDevice *device)
00034 {
00035     nlassert(!isDevice(device));
00036     _Devices.push_back(device);
00037 }
00038 
00039 //=======================================================================
00040 void    CInputDeviceServer::removeDevice(IInputDevice *device)
00041 {
00042     TDeviceCont::iterator it = std::find(_Devices.begin(), _Devices.end(), device);
00043     nlassert(it != _Devices.end());
00044     _Devices.erase(it);
00045 }
00046 
00047 //=======================================================================
00048 bool    CInputDeviceServer::isDevice(IInputDevice *device) const
00049 {
00050     TDeviceCont::const_iterator it = std::find(_Devices.begin(), _Devices.end(), device);
00051     return it != _Devices.end();
00052 }
00053 
00054 //=======================================================================
00055 // Predicate to compare vents dates
00056 struct CInputDeviceEventLess
00057 {
00058     bool operator()(const IInputDeviceEvent *lhs, const IInputDeviceEvent *rhs) const
00059     {
00060         return *lhs < *rhs;
00061     }
00062 };
00063 
00064 //=======================================================================
00065 void    CInputDeviceServer::poll(CEventServer *server)
00066 {
00067     nlassert(_Events.empty());
00068     TDeviceCont::iterator deviceIt;
00069     for (deviceIt = _Devices.begin(); deviceIt != _Devices.end(); ++deviceIt)
00070     {
00071         (*deviceIt)->begin(server);
00072         (*deviceIt)->poll(this);
00073     }
00074     // Sort the messages to get the right dates.
00075     std::sort(_Events.begin(), _Events.end(), CInputDeviceEventLess());
00076     // submit the result to the server
00077     IInputDevice *lastVisitedDevice = NULL;
00078     TEventCont::iterator eventIt;
00079     for (eventIt = _Events.begin(); eventIt != _Events.end(); ++eventIt)
00080     {
00081         // see if this message is from a previous device then the last we visited.
00082         if (lastVisitedDevice && (*eventIt)->Emitter != lastVisitedDevice)
00083         {
00084             // yes, tells that a transition occured
00085             lastVisitedDevice->transitionOccured(server, *eventIt);
00086             lastVisitedDevice = (*eventIt)->Emitter;
00087         }
00088         nlassert((*eventIt)->Emitter != NULL);
00089         (*eventIt)->Emitter->submit(*eventIt, server);
00090     }
00091     //
00092     for (deviceIt = _Devices.begin(); deviceIt != _Devices.end(); ++deviceIt)
00093     {
00094         (*deviceIt)->transitionOccured(server, NULL);
00095     }
00096     // delete the messages
00097     for (eventIt = _Events.begin(); eventIt != _Events.end(); ++eventIt)
00098     {
00099         delete *eventIt;
00100     }
00101     //
00102     _Events.clear();
00103 }
00104 
00105 //=======================================================================
00106 void    CInputDeviceServer::submitEvent(IInputDeviceEvent *deviceEvent)
00107 {
00108     _Events.push_back(deviceEvent);
00109 }
00110 
00111 
00112 } // NLMISC

Generated on Thu Jan 7 08:26:34 2010 for NeL by  doxygen 1.6.1