# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

input_device_server.cpp

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