00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00077 std::sort(_Events.begin(), _Events.end(), CInputDeviceEventLess());
00078
00079 IInputDevice *lastVisitedDevice = NULL;
00080 TEventCont::iterator eventIt;
00081 for (eventIt = _Events.begin(); eventIt != _Events.end(); ++eventIt)
00082 {
00083
00084 if (lastVisitedDevice && (*eventIt)->Emitter != lastVisitedDevice)
00085 {
00086
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
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 }