comparison src/joystick/win32/SDL_dxjoystick.c @ 2713:0906692aa6a4

Final merge of Google Summer of Code 2008 work... Force Feedback for SDL by Edgar Simo, mentored by Ryan C. Gordon
author Sam Lantinga <slouken@libsdl.org>
date Mon, 25 Aug 2008 09:55:03 +0000
parents fe19afb86473
children 50bc882455e5
comparison
equal deleted inserted replaced
2712:c4e697245676 2713:0906692aa6a4
36 #include "SDL_error.h" 36 #include "SDL_error.h"
37 #include "SDL_events.h" 37 #include "SDL_events.h"
38 #include "SDL_joystick.h" 38 #include "SDL_joystick.h"
39 #include "../SDL_sysjoystick.h" 39 #include "../SDL_sysjoystick.h"
40 #include "../SDL_joystick_c.h" 40 #include "../SDL_joystick_c.h"
41 41 #include "SDL_dxjoystick_c.h"
42 #define WIN32_LEAN_AND_MEAN
43 #include <windows.h>
44
45 #define DIRECTINPUT_VERSION 0x0500
46 #include <dinput.h>
47 #ifdef _MSC_VER
48 /* Used for the c_dfDIJoystick2 symbol (no imports are used) */
49 # pragma comment (lib, "dinput.lib")
50 #endif
51 #include <dxerr9.h> /* From DirectX SDK 9c */
52 #ifdef _MSC_VER
53 # pragma comment (lib, "dxerr9.lib")
54 #endif
55 42
56 /* an ISO hack for VisualC++ */ 43 /* an ISO hack for VisualC++ */
57 #ifdef _MSC_VER 44 #ifdef _MSC_VER
58 #define snprintf _snprintf 45 #define snprintf _snprintf
59 #endif 46 #endif
60 47
61 #define INPUT_QSIZE 32 /* Buffer up to 32 input messages */ 48 #define INPUT_QSIZE 32 /* Buffer up to 32 input messages */
62 #define MAX_JOYSTICKS 8 49 #define MAX_JOYSTICKS 8
63 #define MAX_INPUTS 256 /* each joystick can have up to 256 inputs */
64 #define AXIS_MIN -32768 /* minimum value for axis coordinate */ 50 #define AXIS_MIN -32768 /* minimum value for axis coordinate */
65 #define AXIS_MAX 32767 /* maximum value for axis coordinate */ 51 #define AXIS_MAX 32767 /* maximum value for axis coordinate */
66 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */ 52 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
67 53
68 /* external variables referenced. */ 54 /* external variables referenced. */
69 extern HINSTANCE SDL_Instance; 55 extern HWND SDL_HelperWindow;
70 extern HWND SDL_Window;
71 56
72 57
73 /* local variables */ 58 /* local variables */
74 static LPDIRECTINPUT dinput = NULL; 59 static LPDIRECTINPUT dinput = NULL;
75 extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion, 60 extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
93 Uint8 value); 78 Uint8 value);
94 static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, 79 static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
95 Uint8 button, Uint8 state); 80 Uint8 button, Uint8 state);
96 81
97 82
98 /* local types */
99 typedef enum Type
100 { BUTTON, AXIS, HAT } Type;
101
102 typedef struct input_t
103 {
104 /* DirectInput offset for this input type: */
105 DWORD ofs;
106
107 /* Button, axis or hat: */
108 Type type;
109
110 /* SDL input offset: */
111 Uint8 num;
112 } input_t;
113
114 /* The private structure used to keep track of a joystick */
115 struct joystick_hwdata
116 {
117 LPDIRECTINPUTDEVICE2 InputDevice;
118 DIDEVCAPS Capabilities;
119 int buffered;
120
121 input_t Inputs[MAX_INPUTS];
122 int NumInputs;
123 };
124
125 /* Convert a DirectInput return code to a text message */ 83 /* Convert a DirectInput return code to a text message */
126 static void 84 static void
127 SetDIerror(const char *function, HRESULT code) 85 SetDIerror(const char *function, HRESULT code)
128 { 86 {
129 SDL_SetError("%s() [%s]: %s", function, 87 SDL_SetError("%s() [%s]: %s", function,
130 DXGetErrorString9(code), DXGetErrorDescription9(code)); 88 DXGetErrorString(code), DXGetErrorDescription(code));
131 } 89 }
132 90
133 91
134 /* Function to scan the system for joysticks. 92 /* Function to scan the system for joysticks.
135 * This function should set SDL_numjoysticks to the number of available 93 * This function should set SDL_numjoysticks to the number of available
138 */ 96 */
139 int 97 int
140 SDL_SYS_JoystickInit(void) 98 SDL_SYS_JoystickInit(void)
141 { 99 {
142 HRESULT result; 100 HRESULT result;
101 HINSTANCE instance;
143 102
144 SYS_NumJoysticks = 0; 103 SYS_NumJoysticks = 0;
145 104
146 result = CoInitialize(NULL); 105 result = CoInitialize(NULL);
147 if (FAILED(result)) { 106 if (FAILED(result)) {
156 SetDIerror("CoCreateInstance", result); 115 SetDIerror("CoCreateInstance", result);
157 return (-1); 116 return (-1);
158 } 117 }
159 118
160 /* Because we used CoCreateInstance, we need to Initialize it, first. */ 119 /* Because we used CoCreateInstance, we need to Initialize it, first. */
161 result = 120 instance = GetModuleHandle(NULL);
162 IDirectInput_Initialize(dinput, SDL_Instance, DIRECTINPUT_VERSION); 121 if (instance == NULL) {
122 SDL_SetError("GetModuleHandle() failed with error code %d.",
123 GetLastError());
124 return (-1);
125 }
126 result = IDirectInput_Initialize(dinput, instance, DIRECTINPUT_VERSION);
163 127
164 if (FAILED(result)) { 128 if (FAILED(result)) {
165 SetDIerror("IDirectInput::Initialize", result); 129 SetDIerror("IDirectInput::Initialize", result);
166 return (-1); 130 return (-1);
167 } 131 }
176 } 140 }
177 141
178 static BOOL CALLBACK 142 static BOOL CALLBACK
179 EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext) 143 EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
180 { 144 {
181 memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, 145 SDL_memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
182 sizeof(DIDEVICEINSTANCE)); 146 sizeof(DIDEVICEINSTANCE));
183 SYS_NumJoysticks++; 147 SYS_NumJoysticks++;
184 148
185 if (SYS_NumJoysticks >= MAX_JOYSTICKS) 149 if (SYS_NumJoysticks >= MAX_JOYSTICKS)
186 return DIENUM_STOP; 150 return DIENUM_STOP;
187 151
206 { 170 {
207 HRESULT result; 171 HRESULT result;
208 LPDIRECTINPUTDEVICE device; 172 LPDIRECTINPUTDEVICE device;
209 DIPROPDWORD dipdw; 173 DIPROPDWORD dipdw;
210 174
211 ZeroMemory(&dipdw, sizeof(DIPROPDWORD)); 175 SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
212 dipdw.diph.dwSize = sizeof(DIPROPDWORD); 176 dipdw.diph.dwSize = sizeof(DIPROPDWORD);
213 dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); 177 dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
214 178
215 179
216 /* allocate memory for system specific hardware data */ 180 /* allocate memory for system specific hardware data */
217 joystick->hwdata = 181 joystick->hwdata =
218 (struct joystick_hwdata *) malloc(sizeof(struct joystick_hwdata)); 182 (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
219 if (joystick->hwdata == NULL) { 183 if (joystick->hwdata == NULL) {
220 SDL_OutOfMemory(); 184 SDL_OutOfMemory();
221 return (-1); 185 return (-1);
222 } 186 }
223 ZeroMemory(joystick->hwdata, sizeof(struct joystick_hwdata)); 187 SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
224 joystick->hwdata->buffered = 1; 188 joystick->hwdata->buffered = 1;
225 joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS); 189 joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
226 190
227 result = 191 result =
228 IDirectInput_CreateDevice(dinput, 192 IDirectInput_CreateDevice(dinput,
248 212
249 /* Aquire shared access. Exclusive access is required for forces, 213 /* Aquire shared access. Exclusive access is required for forces,
250 * though. */ 214 * though. */
251 result = 215 result =
252 IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata-> 216 IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->
253 InputDevice, SDL_Window, 217 InputDevice, SDL_HelperWindow,
254 DISCL_EXCLUSIVE | 218 DISCL_EXCLUSIVE |
255 DISCL_BACKGROUND); 219 DISCL_BACKGROUND);
256 if (FAILED(result)) { 220 if (FAILED(result)) {
257 SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result); 221 SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result);
258 return (-1); 222 return (-1);
633 IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice); 597 IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
634 IDirectInputDevice2_Release(joystick->hwdata->InputDevice); 598 IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
635 599
636 if (joystick->hwdata != NULL) { 600 if (joystick->hwdata != NULL) {
637 /* free system specific hardware data */ 601 /* free system specific hardware data */
638 free(joystick->hwdata); 602 SDL_free(joystick->hwdata);
639 } 603 }
640 } 604 }
641 605
642 /* Function to perform any system-specific joystick related cleanup */ 606 /* Function to perform any system-specific joystick related cleanup */
643 void 607 void