00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_DI_EVENT_EMITTER_H
00027 #define NL_DI_EVENT_EMITTER_H
00028
00029
00030
00031 #include "nel/misc/types_nl.h"
00032
00033
00034 #ifdef NL_OS_WINDOWS
00035
00036
00037 #define DIRECTINPUT_VERSION 0x0800
00038
00039 #include "nel/misc/input_device_server.h"
00040 #include "nel/misc/input_device_manager.h"
00041 #include "nel/misc/event_emitter.h"
00042 #include "nel/misc/smart_ptr.h"
00043 #include "nel/misc/events.h"
00044 #include "nel/misc/rect.h"
00045 #include "nel/misc/game_device.h"
00046 #include <windows.h>
00047 #include <dinput.h>
00048
00049
00050
00051 namespace NLMISC
00052 {
00053
00054
00055 class CWinEventEmitter;
00056 class CDIKeyboard;
00057 class CDIMouse;
00058 struct IMouseDevice;
00059 struct IKeyboardDevice;
00060
00061
00062 struct EDirectInput : public EInputDevice
00063 {
00064 EDirectInput(const char *reason) : EInputDevice(reason) {}
00065 };
00066
00067 struct EDirectInputLibNotFound : public EDirectInput
00068 {
00069 EDirectInputLibNotFound() : EDirectInput("can't found the direct input dll") {}
00070 };
00071
00072 struct EDirectInputInitFailed : public EDirectInput
00073 {
00074 EDirectInputInitFailed() : EDirectInput("Direct input initialization failed") {}
00075 };
00076
00077 struct EDirectInputCooperativeLevelFailed : public EDirectInput
00078 {
00079 EDirectInputCooperativeLevelFailed() : EDirectInput("Direct Input Device Cooperative level couldn't be set") {}
00080 };
00081
00082
00083
00084 struct CDIEvent : public IInputDeviceEvent
00085 {
00086
00087 virtual bool operator < (const IInputDeviceEvent &ide) const
00088 {
00089
00090 return Datas.dwTimeStamp < (safe_cast<const CDIEvent *>(&ide))->Datas.dwTimeStamp;
00091 }
00092 DIDEVICEOBJECTDATA Datas;
00093 };
00094
00103 class CDIEventEmitter : public IEventEmitter, public IInputDeviceManager
00104 {
00105 public:
00113 static CDIEventEmitter *create(HINSTANCE hinst, HWND hwnd, CWinEventEmitter *we);
00114 ~CDIEventEmitter();
00115 public:
00116
00118 void poll(CEventServer *server = NULL);
00119
00121
00122
00125 virtual IMouseDevice *getMouseDevice() throw(EInputDevice);
00127 virtual void releaseMouse();
00132 virtual IKeyboardDevice *getKeyboardDevice() throw(EInputDevice);
00134 virtual void releaseKeyboard();
00135
00136 virtual void enumerateGameDevice(TDeviceDescVect &descs) throw(EInputDevice);
00137
00138 virtual IGameDevice *createGameDevice(const std::string &instanceName) throw(EInputDevice);
00139
00140 virtual void releaseGameDevice(IGameDevice *);
00142
00144 virtual void submitEvents(CEventServer &server);
00145
00146
00147 TMouseButton buildButtonsFlags() const;
00148
00149 TMouseButton buildKeyboardButtonFlags() const
00150 {
00151 return (TMouseButton) (buildButtonsFlags() & (ctrlButton|shiftButton|altButton));
00152 }
00153
00154
00155
00156
00157 private:
00158 typedef HRESULT (WINAPI * TPDirectInput8Create) (HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID* ppvOut, LPUNKNOWN punkOuter);
00159
00160 class CDIEventServer : CEventServer
00161 {
00162 friend class CDIEventEmitter;
00163 public:
00164 void setServer (CEventServer *server)
00165 {
00166 _Server = server;
00167 }
00168 private:
00169 bool pumpEvent(CEvent *event)
00170 {
00171 CEventServer::pumpEvent(event);
00172 _Server->postEvent (event);
00173 return false;
00174 }
00175 private:
00176 CEventServer *_Server;
00177 };
00178 private:
00179 HWND _hWnd;
00180 TMouseButton _ButtonsFlags;
00181 NLMISC::CRefPtr<CWinEventEmitter> _WE;
00182 static HMODULE _DirectInputLibHandle;
00183 static TPDirectInput8Create _PDirectInput8Create;
00184 static uint _NumCreatedInterfaces;
00185 private:
00186 static bool loadLib();
00187 static void unloadLib();
00188
00189 private:
00190 CDIEventServer _InternalServer;
00191 CInputDeviceServer _DeviceServer;
00192 IDirectInput8 *_DInput8;
00193 CDIMouse *_Mouse;
00194 CDIKeyboard *_Keyboard;
00195 private:
00196 CDIEventEmitter(HWND hwnd, CWinEventEmitter *we);
00197 };
00198
00199
00200
00201 }
00202
00203 #endif // NL_WINDOWS
00204
00205
00206 #endif // NL_DX_EVENT_EMITTER_H
00207
00208