forked from KoKuToru/koku-xinput-wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xinput.cpp
executable file
·315 lines (277 loc) · 10.8 KB
/
xinput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "xinput.h"
#include "main.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_haptic.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <iomanip>
#include <sstream>
#define WINE_XINPUT_AXES 8
#define MAX_XINPUT_BUTTONS 14
using namespace std;
bool active = true;
struct Sgamepad_sdl
{
SDL_Joystick *joystick;
SDL_GameController* controller;
SDL_Haptic *haptic;
int haptic_effects[2];
};
static vector<Sgamepad_sdl> gamepads_sdl;
static const unsigned int xbuttons[MAX_XINPUT_BUTTONS] = {XINPUT_GAMEPAD_START, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB, XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT, XINPUT_GAMEPAD_DPAD_UP};
static const SDL_GameControllerButton sdlbuttons[MAX_XINPUT_BUTTONS] = {SDL_CONTROLLER_BUTTON_START, SDL_CONTROLLER_BUTTON_BACK, SDL_CONTROLLER_BUTTON_LEFTSTICK, SDL_CONTROLLER_BUTTON_RIGHTSTICK, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, SDL_CONTROLLER_BUTTON_A, SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_X, SDL_CONTROLLER_BUTTON_Y, SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_UP};
GUID GUID_NULL = {0,0,0,{0,0,0,0,0,0,0,0}};
void GamepadInitSDL()
{
static bool inited = false;
if (inited)
{
return;
}
inited = true;
//init:
SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_HAPTIC|SDL_INIT_GAMECONTROLLER);
SDL_JoystickEventState(SDL_IGNORE);
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
for(int i = 0; i < SDL_NumJoysticks(); ++i)
{
SDL_Joystick* joy = SDL_JoystickOpen(i);
if (joy)
{
Sgamepad_sdl new_gamepad;
SDL_GameController* controller = SDL_GameControllerOpen(i);
if (controller) {
new_gamepad.joystick = joy;
new_gamepad.controller = controller;
new_gamepad.haptic = 0;
//check for haptic
new_gamepad.haptic = SDL_HapticOpenFromJoystick(new_gamepad.joystick);
if (new_gamepad.haptic != 0)
{
//start haptic effects
SDL_HapticEffect effect[2];
memset(&effect, 0, sizeof(SDL_HapticEffect)*2);
effect[0].type = SDL_HAPTIC_SINE; //constant somehow don't work, or I don't undestand what it does..
effect[0].periodic.direction.type = SDL_HAPTIC_CARTESIAN;
effect[0].periodic.direction.dir[0] = 1;
effect[0].periodic.period = 0;
effect[0].periodic.magnitude = 0;
effect[0].periodic.length = SDL_HAPTIC_INFINITY;
effect[1] = effect[0];
effect[1].periodic.direction.dir[0] = -1;
effect[1].periodic.magnitude = 0;
new_gamepad.haptic_effects[0] = SDL_HapticNewEffect(new_gamepad.haptic, &(effect[0]));
new_gamepad.haptic_effects[1] = SDL_HapticNewEffect(new_gamepad.haptic, &(effect[1]));
SDL_HapticRunEffect(new_gamepad.haptic, new_gamepad.haptic_effects[0], 1);
SDL_HapticRunEffect(new_gamepad.haptic, new_gamepad.haptic_effects[1], 1);
}
gamepads_sdl.push_back(new_gamepad);
}
}
}
XINPUT_VIBRATION welcome_vibration;
welcome_vibration.wLeftMotorSpeed = 65535;
welcome_vibration.wRightMotorSpeed = 0;
XInputSetState(0, &welcome_vibration);
SDL_Delay(250);
welcome_vibration.wLeftMotorSpeed = 0;
welcome_vibration.wRightMotorSpeed = 0;
XInputSetState(0, &welcome_vibration);
SDL_Delay(250);
welcome_vibration.wLeftMotorSpeed = 0;
welcome_vibration.wRightMotorSpeed = 65535;
XInputSetState(0, &welcome_vibration);
SDL_Delay(250);
welcome_vibration.wLeftMotorSpeed = 0;
welcome_vibration.wRightMotorSpeed = 0;
XInputSetState(0, &welcome_vibration);
}
void WINAPI XInputEnable(bool enable)
{
if (!enable)
{
XINPUT_VIBRATION stop_vibration;
stop_vibration.wLeftMotorSpeed = 0;
stop_vibration.wRightMotorSpeed = 0;
for(int i = 0; i < 4; ++i)
{
XInputSetState(i, &stop_vibration);
}
}
active = enable;
}
unsigned WINAPI XInputGetAudioDeviceIds(unsigned dwUserIndex, short* pRenderDeviceId, unsigned *pRenderCount, short* pCaptureDeviceId, unsigned *pCaptureCount)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
/*
If there is no headset connected to the controller,
the function will also retrieve ERROR_SUCCESS with NULL as the values
for pRenderDeviceId and pCaptureDeviceId.
*/
if (pRenderCount)
{
*pRenderDeviceId = 0;
}
if (pCaptureDeviceId)
{
*pCaptureDeviceId = 0;
}
return ERROR_SUCCESS;
}
unsigned WINAPI XInputGetBatteryInformation(unsigned dwUserIndex, char devType, XINPUT_BATTERY_INFORMATION *pBatteryInformation)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
if (pBatteryInformation)
{
if (devType == BATTERY_DEVTYPE_GAMEPAD)
{
//sorry no real battery check
pBatteryInformation->BatteryType = BATTERY_TYPE_WIRED;
pBatteryInformation->BatteryLevel = BATTERY_LEVEL_FULL;
}
else
{
pBatteryInformation->BatteryType = BATTERY_TYPE_DISCONNECTED;
pBatteryInformation->BatteryLevel = BATTERY_LEVEL_EMPTY;
}
}
return ERROR_SUCCESS;
}
unsigned WINAPI XInputGetCapabilities(unsigned dwUserIndex, unsigned dwFlags, XINPUT_CAPABILITIES *pCapabilities)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
if (pCapabilities)
{
pCapabilities->Type = XINPUT_DEVTYPE_GAMEPAD;
pCapabilities->SubType = XINPUT_DEVSUBTYPE_GAMEPAD;
pCapabilities->Flags = (gamepads_sdl[dwUserIndex].haptic != 0)?XINPUT_CAPS_FFB_SUPPORTED:0;
pCapabilities->Gamepad.wButtons = 0xFFFF;
pCapabilities->Gamepad.bLeftTrigger = 255;
pCapabilities->Gamepad.bRightTrigger = 255;
pCapabilities->Gamepad.sThumbLX = 32767;
pCapabilities->Gamepad.sThumbLY = 32767;
pCapabilities->Gamepad.sThumbRX = 32767;
pCapabilities->Gamepad.sThumbRY = 32767;
if (gamepads_sdl[dwUserIndex].haptic != 0)
{
pCapabilities->Vibration.wLeftMotorSpeed = 65535;
pCapabilities->Vibration.wRightMotorSpeed = 65535;
}
else
{
pCapabilities->Vibration.wLeftMotorSpeed = 0;
pCapabilities->Vibration.wRightMotorSpeed = 0;
}
}
return ERROR_SUCCESS;
}
unsigned WINAPI XInputGetDSoundAudioDeviceGuids(unsigned dwUserIndex, GUID* pDSoundRenderGuid, GUID* pDSoundCaptureGuid)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
/*
If there is no headset connected to the controller,
the function also retrieves ERROR_SUCCESS with GUID_NULL as the values
for pDSoundRenderGuid and pDSoundCaptureGuid.
*/
if (pDSoundRenderGuid) {
*pDSoundRenderGuid = GUID_NULL;
}
if (pDSoundCaptureGuid) {
*pDSoundCaptureGuid = GUID_NULL;
}
return ERROR_SUCCESS;
}
unsigned WINAPI XInputGetKeystroke(unsigned dwUserIndex, unsigned dwReserved, XINPUT_KEYSTROKE* pKeystroke)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
//If no new keys have been pressed, the return value is ERROR_EMPTY.
return ERROR_EMPTY;
}
unsigned WINAPI XInputGetState(unsigned dwUserIndex, XINPUT_STATE *pState)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
SDL_JoystickUpdate();
SDL_GameControllerUpdate();
//set data
if (pState)
{
SDL_GameController* controller = gamepads_sdl[dwUserIndex].controller;
for (int j = 0; j < MAX_XINPUT_BUTTONS; j++) {
Uint8 result = SDL_GameControllerGetButton(controller, sdlbuttons[j]);
//printf("result for %d: %d\n", j, result);
if (result) {
pState->Gamepad.wButtons |= xbuttons[j];
}
else {
pState->Gamepad.wButtons &= ~(xbuttons[j]);
}
}
short ly = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
short ry = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY);
pState->Gamepad.sThumbLX = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
pState->Gamepad.sThumbLY = (ly == 0 ? 0 : -ly -1);
pState->Gamepad.sThumbRX = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX);
pState->Gamepad.sThumbRY = ry == 0 ? 0 : -ry -1;
pState->Gamepad.bLeftTrigger = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
pState->Gamepad.bRightTrigger = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
static int dwPacketNumber = 0;
pState->dwPacketNumber = ++dwPacketNumber;
}
return ERROR_SUCCESS;
}
unsigned WINAPI XInputSetState(unsigned dwUserIndex, XINPUT_VIBRATION *pVibration)
{
GamepadInitSDL();
if (dwUserIndex >= gamepads_sdl.size())
{
return ERROR_DEVICE_NOT_CONNECTED;
}
if (active && pVibration)
{
if (gamepads_sdl[dwUserIndex].haptic != 0)
{
SDL_HapticEffect effect[2];
memset(&effect, 0, sizeof(SDL_HapticEffect)*2);
effect[0].type = SDL_HAPTIC_SINE;
effect[0].periodic.direction.type = SDL_HAPTIC_CARTESIAN;
effect[0].periodic.direction.dir[0] = 1;
effect[0].periodic.period = 0;
effect[0].periodic.magnitude = pVibration->wLeftMotorSpeed>>1;
effect[0].periodic.length = SDL_HAPTIC_INFINITY;
effect[1] = effect[0];
effect[1].periodic.direction.dir[0] = -1;
effect[1].periodic.magnitude = pVibration->wRightMotorSpeed>>1;
SDL_HapticUpdateEffect(gamepads_sdl[dwUserIndex].haptic, gamepads_sdl[dwUserIndex].haptic_effects[0], &(effect[0]));
SDL_HapticUpdateEffect(gamepads_sdl[dwUserIndex].haptic, gamepads_sdl[dwUserIndex].haptic_effects[1], &(effect[1]));
}
}
return ERROR_SUCCESS;
}