annotate src/joystick/win32/SDL_dxjoystick.c @ 1661:281d3f4870e5 SDL-1.3

Moved DirectInput joystick code to 1.3 branch
author Sam Lantinga <slouken@libsdl.org>
date Sun, 21 May 2006 17:27:13 +0000
parents
children 782fd950bd46
rev   line source
1661
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 1997-2006 Sam Lantinga
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 #include "SDL_config.h"
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 #ifdef SDL_JOYSTICK_DINPUT
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 /* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 * A. Formiga's WINMM driver.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 *
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 * Hats and sliders are completely untested; the app I'm writing this for mostly
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 * doesn't use them and I don't own any joysticks with them.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 *
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 * We don't bother to use event notification here. It doesn't seem to work
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 * with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 * let it return 0 events. */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 #include "SDL_error.h"
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 #include "SDL_events.h"
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 #include "SDL_joystick.h"
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 #include "../SDL_sysjoystick.h"
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 #include "../SDL_joystick_c.h"
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 #define WIN32_LEAN_AND_MEAN
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 #include <windows.h>
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 #define DIRECTINPUT_VERSION 0x0500
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 #include <dinput.h>
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 #define INPUT_QSIZE 32 /* Buffer up to 32 input messages */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 extern HINSTANCE SDL_Instance;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 extern int DX5_Load();
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 extern void DX5_Unload();
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 extern HRESULT (WINAPI *DInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT *ppDI, LPUNKNOWN punkOuter);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 static LPDIRECTINPUT dinput = NULL;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 #define MAX_JOYSTICKS 8
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 #define MAX_INPUTS 256 /* each joystick can have up to 256 inputs */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 #define AXIS_MIN -32768 /* minimum value for axis coordinate */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 #define AXIS_MAX 32767 /* maximum value for axis coordinate */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 typedef enum Type { BUTTON, AXIS, HAT } Type;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 /* array to hold joystick ID values */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 static int SYS_NumJoysticks;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 extern HWND SDL_Window;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 typedef struct input_t
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 /* DirectInput offset for this input type: */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 DWORD ofs;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 /* Button, axis or hat: */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 Type type;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 /* SDL input offset: */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 Uint8 num;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 } input_t;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 /* The private structure used to keep track of a joystick */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 struct joystick_hwdata
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 LPDIRECTINPUTDEVICE2 InputDevice;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 int buffered;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 input_t Inputs[MAX_INPUTS];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 int NumInputs;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 };
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 /* Convert a DirectInput return code to a text message */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 static void SetDIerror(char *function, int code)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 static char *error;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 static char errbuf[1024];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 errbuf[0] = 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 switch (code) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 case DIERR_GENERIC:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 error = "Undefined error!";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 case DIERR_OLDDIRECTINPUTVERSION:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 error = "Your version of DirectInput needs upgrading";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 case DIERR_INVALIDPARAM:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 error = "Invalid parameters";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 case DIERR_OUTOFMEMORY:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 error = "Out of memory";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 case DIERR_DEVICENOTREG:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 error = "Device not registered";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 case DIERR_NOINTERFACE:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 error = "Interface not supported";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 case DIERR_NOTINITIALIZED:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 error = "Device not initialized";
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 default:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 sprintf(errbuf, "%s: Unknown DirectInput error: 0x%x",
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 function, code);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 if ( ! errbuf[0] ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 sprintf(errbuf, "%s: %s", function, error);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 SDL_SetError("%s", errbuf);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 return;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 VOID* pContext )
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, sizeof(DIDEVICEINSTANCE));
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 SYS_NumJoysticks++;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 if( SYS_NumJoysticks >= MAX_JOYSTICKS )
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 return DIENUM_STOP;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 return DIENUM_CONTINUE;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147 static BOOL CALLBACK DIJoystick_EnumDevObjectsProc(LPCDIDEVICEOBJECTINSTANCE dev,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 LPVOID pvRef)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 SDL_Joystick *joystick = (SDL_Joystick*)pvRef;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151 HRESULT result;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
153 const int SupportedMask = DIDFT_BUTTON | DIDFT_POV | DIDFT_AXIS;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154 if(!(dev->dwType & SupportedMask))
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155 return DIENUM_CONTINUE; /* unsupported */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
156
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157 in->ofs = dev->dwOfs;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159 if(dev->dwType & DIDFT_BUTTON) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 in->type = BUTTON;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161 in->num = joystick->nbuttons;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 joystick->nbuttons++;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
163 } else if(dev->dwType & DIDFT_POV) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
164 in->type = HAT;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
165 in->num = joystick->nhats;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
166 joystick->nhats++;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
167 } else { /* dev->dwType & DIDFT_AXIS */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168 DIPROPRANGE diprg;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
169 DIPROPDWORD dilong;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
170
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
171 in->type = AXIS;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
172 in->num = joystick->naxes;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
173
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
174 diprg.diph.dwSize = sizeof(diprg);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 diprg.diph.dwHeaderSize = sizeof(diprg.diph);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176 diprg.diph.dwObj = dev->dwOfs;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
177 diprg.diph.dwHow = DIPH_BYOFFSET;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 diprg.lMin = AXIS_MIN;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
179 diprg.lMax = AXIS_MAX;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_RANGE, &diprg.diph);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
182 if ( result != DI_OK )
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183 return DIENUM_CONTINUE; /* don't use this axis */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 /* Set dead zone to 0. */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186 dilong.diph.dwSize = sizeof(dilong);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187 dilong.diph.dwHeaderSize = sizeof(dilong.diph);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 dilong.diph.dwObj = dev->dwOfs;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
189 dilong.diph.dwHow = DIPH_BYOFFSET;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 dilong.dwData = 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191 result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice, DIPROP_DEADZONE, &dilong.diph);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
192 if ( result != DI_OK )
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193 return DIENUM_CONTINUE; /* don't use this axis */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195 joystick->naxes++;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
196 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
197
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 joystick->hwdata->NumInputs++;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
199
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
200 if(joystick->hwdata->NumInputs == MAX_INPUTS)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 return DIENUM_STOP; /* too many */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
202
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 return DIENUM_CONTINUE;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206 /* Function to scan the system for joysticks.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
207 * This function should set SDL_numjoysticks to the number of available
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
208 * joysticks. Joystick 0 should be the system default joystick.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209 * It should return 0, or -1 on an unrecoverable fatal error.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211 int SDL_SYS_JoystickInit(void)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
212 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 HRESULT result;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
215 SYS_NumJoysticks = 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
216
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217 /* Create the DirectInput object */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218 if ( DX5_Load() < 0 ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 SDL_SetError("Couldn't load DirectInput");
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
221 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
222 result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
223 &dinput, NULL);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 if ( result != DI_OK ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225 DX5_Unload();
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226 SetDIerror("DirectInputCreate", result);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
227 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 result = IDirectInput_EnumDevices(dinput,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231 DIDEVTYPE_JOYSTICK,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 EnumJoysticksCallback,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233 NULL,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234 DIEDFL_ATTACHEDONLY );
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236 return SYS_NumJoysticks;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
237 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
238
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
239 /* Function to get the device-dependent name of a joystick */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
240 const char *SDL_SYS_JoystickName(int index)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
241 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 /***-> test for invalid index ? */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243 return(SYS_Joystick[index].tszProductName);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
244 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 /* Function to open a joystick for use.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247 The joystick to open is specified by the index field of the joystick.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
248 This should fill the nbuttons and naxes fields of the joystick structure.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 It returns 0, or -1 if there is an error.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250 */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
252 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
253 HRESULT result;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
254 LPDIRECTINPUTDEVICE device;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
255
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
256 /* allocate memory for system specific hardware data */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
257 joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
258 if (joystick->hwdata == NULL)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260 SDL_OutOfMemory();
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
261 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
264 joystick->hwdata->buffered = 1;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
265
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
266 result = IDirectInput_CreateDevice(dinput, &SYS_Joystick[joystick->index].guidInstance,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
267 &device, NULL);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
268 if ( result != DI_OK ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269 SetDIerror("DirectInput::CreateDevice", result);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
270 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
271 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
272
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
273 result = IDirectInputDevice_QueryInterface(device,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 &IID_IDirectInputDevice2, (LPVOID *)&joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275 IDirectInputDevice_Release(device);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
276 if ( result != DI_OK ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
277 SetDIerror("DirectInputDevice::QueryInterface", result);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
278 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
279 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
280
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
281 result = IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->InputDevice, SDL_Window,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
282 DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
283 if ( result != DI_OK ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
284 SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
285 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
286 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
287
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
288 result = IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice, &c_dfDIJoystick);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
289 if ( result != DI_OK ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
290 SetDIerror("DirectInputDevice::SetDataFormat", result);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
291 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
292 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
293
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
294 IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
295 DIJoystick_EnumDevObjectsProc,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
296 joystick,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
297 DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
298
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
299 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300 DIPROPDWORD dipdw;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
301 memset(&dipdw, 0, sizeof(dipdw));
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
302 dipdw.diph.dwSize = sizeof(dipdw);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303 dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
304 dipdw.diph.dwObj = 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
305 dipdw.diph.dwHow = DIPH_DEVICE;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
306 dipdw.dwData = INPUT_QSIZE;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
307 result = IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
308 DIPROP_BUFFERSIZE, &dipdw.diph);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
309
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
310 if ( result == DI_POLLEDDEVICE )
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
311 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
312 /* This device doesn't support buffering, so we're forced
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
313 * to use less reliable polling. */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
314 joystick->hwdata->buffered = 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
315 } else if ( result != DI_OK ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
316 SetDIerror("DirectInputDevice::SetProperty", result);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
317 return(-1);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
318 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
319 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
320
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
321 return(0);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
322 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
323
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
324 static Uint8 TranslatePOV(DWORD value)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
325 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
326 const int HAT_VALS[] = {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
327 SDL_HAT_UP,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
328 SDL_HAT_UP | SDL_HAT_RIGHT,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
329 SDL_HAT_RIGHT,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
330 SDL_HAT_DOWN | SDL_HAT_RIGHT,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
331 SDL_HAT_DOWN,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
332 SDL_HAT_DOWN | SDL_HAT_LEFT,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
333 SDL_HAT_LEFT,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
334 SDL_HAT_UP | SDL_HAT_LEFT
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
335 };
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
336
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
337 if(LOWORD(value) == 0xFFFF)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
338 return SDL_HAT_CENTERED;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
339
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
340 /* Round the value up: */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
341 value += 4500 / 2;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
342 value %= 36000;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
343 value /= 4500;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
344
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
345 if(value >= 8)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
346 return SDL_HAT_CENTERED; /* shouldn't happen */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
347
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
348 return HAT_VALS[value];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
349 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
350
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
351 /* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
352 * do it. */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
353 static int SDL_PrivateJoystickAxis_Int(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
354 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
355 if(joystick->axes[axis] != value)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
356 return SDL_PrivateJoystickAxis(joystick, axis, value);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
357 return 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
358 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
359
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
360 static int SDL_PrivateJoystickHat_Int(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
361 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
362 if(joystick->hats[hat] != value)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
363 return SDL_PrivateJoystickHat(joystick, hat, value);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
364 return 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
365 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
366
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
367 static int SDL_PrivateJoystickButton_Int(SDL_Joystick *joystick, Uint8 button, Uint8 state)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
368 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
369 if(joystick->buttons[button] != state)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
370 return SDL_PrivateJoystickButton(joystick, button, state);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
371 return 0;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
372 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
373
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
374 /* Function to update the state of a joystick - called as a device poll.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
375 * This function shouldn't update the joystick structure directly,
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
376 * but instead should call SDL_PrivateJoystick*() to deliver events
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
377 * and update joystick device state.
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
378 */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
379 void SDL_SYS_JoystickUpdate_Polled(SDL_Joystick *joystick)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
380 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
381 DIJOYSTATE state;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
382 HRESULT result;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
383 int i;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
384
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
385 result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
386 if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
387 IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
388 result = IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice, sizeof(state), &state);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
389 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
390
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
391 /* Set each known axis, button and POV. */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
392 for(i = 0; i < joystick->hwdata->NumInputs; ++i)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
393 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
394 const input_t *in = &joystick->hwdata->Inputs[i];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
395
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
396 switch(in->type)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
397 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
398 case AXIS:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
399 switch(in->ofs)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
400 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
401 case DIJOFS_X: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lX); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
402 case DIJOFS_Y: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lY); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
403 case DIJOFS_Z: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lZ); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
404 case DIJOFS_RX: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRx); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
405 case DIJOFS_RY: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRy); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
406 case DIJOFS_RZ: SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.lRz); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
407 case DIJOFS_SLIDER(0): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
408 case DIJOFS_SLIDER(1): SDL_PrivateJoystickAxis_Int(joystick, in->num, (Sint16)state.rglSlider[0]); break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
409 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
410
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
411 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
412
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
413 case BUTTON:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
414 SDL_PrivateJoystickButton_Int(joystick, in->num, (Uint8) (state.rgbButtons[in->ofs - DIJOFS_BUTTON0]?SDL_PRESSED:SDL_RELEASED));
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
415 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
416 case HAT:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
417 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
418 Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
419 SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
420 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
421 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
422 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
423 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
424 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
425
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
426 void SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick *joystick)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
427 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
428 int i;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
429 HRESULT result;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
430 DWORD numevents;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
431 DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
432
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
433 numevents = INPUT_QSIZE;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
434 result = IDirectInputDevice2_GetDeviceData(
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
435 joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
436 evtbuf, &numevents, 0);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
437 if ( result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED ) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
438 IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
439 result = IDirectInputDevice2_GetDeviceData(
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
440 joystick->hwdata->InputDevice, sizeof(DIDEVICEOBJECTDATA),
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
441 evtbuf, &numevents, 0);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
442 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
443
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
444 /* Handle the events */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
445 if ( result != DI_OK )
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
446 return;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
447
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
448 for(i = 0; i < (int) numevents; ++i)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
449 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
450 int j;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
451
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
452 for(j = 0; j < joystick->hwdata->NumInputs; ++j)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
453 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
454 const input_t *in = &joystick->hwdata->Inputs[j];
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
455
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
456 if(evtbuf[i].dwOfs != in->ofs)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
457 continue;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
458
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
459 switch(in->type)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
460 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
461 case AXIS:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
462 SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)evtbuf[i].dwData);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
463 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
464 case BUTTON:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
465 SDL_PrivateJoystickButton(joystick, in->num, (Uint8) (evtbuf[i].dwData?SDL_PRESSED:SDL_RELEASED));
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
466 break;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
467 case HAT:
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
468 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
469 Uint8 pos = TranslatePOV(evtbuf[i].dwData);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
470 SDL_PrivateJoystickHat(joystick, in->num, pos);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
471 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
472 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
473 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
474 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
475 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
476
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
477 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
478 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
479 HRESULT result;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
480
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
481 result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
482 if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
483 IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
484 IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
485 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
486
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
487 if(joystick->hwdata->buffered)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
488 SDL_SYS_JoystickUpdate_Buffered(joystick);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
489 else
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
490 SDL_SYS_JoystickUpdate_Polled(joystick);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
491 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
492
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
493 /* Function to close a joystick after use */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
494 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
495 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
496 IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
497 IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
498
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
499 if (joystick->hwdata != NULL) {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
500 /* free system specific hardware data */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
501 free(joystick->hwdata);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
502 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
503 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
504
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
505 /* Function to perform any system-specific joystick related cleanup */
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
506 void SDL_SYS_JoystickQuit(void)
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
507 {
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
508 IDirectInput_Release(dinput);
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
509 dinput = NULL;
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
510 DX5_Unload();
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
511 }
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
512
281d3f4870e5 Moved DirectInput joystick code to 1.3 branch
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
513 #endif /* SDL_JOYSTICK_DINPUT */