1 #ifndef LIBDENG_DIRECTINPUT_H
2 #define LIBDENG_DIRECTINPUT_H
3 
4 #ifdef __CLIENT__
5 
6 #define WIN32_LEAN_AND_MEAN
7 #define DIRECTINPUT_VERSION 0x0800
8 
9 //#define _WIN32_WINNT 0x0501
10 
11 #include <windows.h>
12 #include <dinput.h>
13 #include <assert.h>
14 
15 #define I_SAFE_RELEASE(d) { if(d) { (d)->Release(); (d) = NULL; } }
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /**
22  * Attempt to initialize an application-global interface to DirectInput. First
23  * the version 8 interface (if available on the host system) and if unsuccessful,
24  * then the older version 3 interface.
25  *
26  * @note The caller must ensure that COM has been initialized else this will
27  *       fail and false will be returned.
28  *
29  * @return  @c true if an interface was initialized successfully.
30  */
31 int DirectInput_Init(void);
32 
33 /**
34  * Shutdown the open DirectInput interface if initialized.
35  */
36 void DirectInput_Shutdown(void);
37 
38 /**
39  * Retrieve a handle to the version 8 interface.
40  * @return  Interface instance handle or @c NULL if not initialized.
41  */
42 LPDIRECTINPUT8 DirectInput_IVersion8();
43 
44 /**
45  * Retrieve a handle to the version 3 interface.
46  * @return  Interface instance handle or @c NULL if not initialized.
47  */
48 LPDIRECTINPUT DirectInput_IVersion3();
49 
50 /**
51  * Releases and then destroys a DirectInput device.
52  * @param dev  Address of the device instance to be destroyed. Can be @c NULL.
53  */
54 void DirectInput_KillDevice(LPDIRECTINPUTDEVICE8* dev);
55 
56 /**
57  * Retrieve a plain text explanation of a DirectInput error code suitable for
58  * printing to the error log/displaying to the user.
59  *
60  * @param hr  DirectInput Error code to be translated.
61  * @return  Plain text explanation. Always returns a valid cstring.
62  */
63 const char* DirectInput_ErrorMsg(HRESULT hr);
64 
65 #ifdef __cplusplus
66 } // extern "C"
67 #endif
68 
69 #ifdef __cplusplus
70 /**
71  * A handy adaptor for manipulating a DIPROPDWORD structure.
72  */
73 struct DIPropDWord : DIPROPDWORD
74 {
75     DIPropDWord(DWORD how=0, DWORD object=0, DWORD data=0)
76     {
77         initHeader();
78         setHow(how);
79         setObject(object);
80         setData(data);
81     }
82 
83     operator DIPROPHEADER*() { return &diph; }
84     operator const DIPROPHEADER*() const { return &diph; }
85 
setHowDIPropDWord86     inline DIPropDWord& setHow(DWORD how)    { diph.dwHow = how;  return *this; }
setObjectDIPropDWord87     inline DIPropDWord& setObject(DWORD obj) { diph.dwObj = obj;  return *this; }
setDataDIPropDWord88     inline DIPropDWord& setData(DWORD data)  {     dwData = data; return *this; }
89 
90 private:
initHeaderDIPropDWord91     void initHeader()
92     {
93         diph.dwSize = sizeof(DIPROPDWORD);
94         diph.dwHeaderSize = sizeof(diph);
95     }
96 };
97 
98 /**
99  * A handy adaptor for manipulating a DIPROPRANGE structure.
100  */
101 struct DIPropRange : DIPROPRANGE
102 {
103     DIPropRange(DWORD how=0, DWORD object=0, int min=0, int max=0)
104     {
105         initHeader();
106         setHow(how);
107         setObject(object);
108         setMin(min);
109         setMax(max);
110     }
111 
112     operator DIPROPHEADER*() { return &diph; }
113     operator const DIPROPHEADER*() const { return &diph; }
114 
setHowDIPropRange115     inline DIPropRange& setHow(DWORD how)    { diph.dwHow = how; return *this; }
setObjectDIPropRange116     inline DIPropRange& setObject(DWORD obj) { diph.dwObj = obj; return *this; }
setMinDIPropRange117     inline DIPropRange& setMin(int min)      {       lMin = min; return *this; }
setMaxDIPropRange118     inline DIPropRange& setMax(int max)      {       lMax = max; return *this; }
119 
120 private:
initHeaderDIPropRange121     void initHeader()
122     {
123         diph.dwSize = sizeof(DIPROPRANGE);
124         diph.dwHeaderSize = sizeof(diph);
125     }
126 };
127 #endif // __cplusplus
128 
129 #endif // __CLIENT__
130 
131 #endif // LIBDENG_DIRECTINPUT_H
132