2 * Plug and Play support for hid devices found through SDL2
4 * Copyright 2017 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #ifdef HAVE_SDL2_SDL_H
31 # include <SDL2/SDL.h>
34 #define NONAMELESSUNION
37 #define WIN32_NO_STATUS
43 #include "ddk/hidtypes.h"
44 #include "wine/library.h"
45 #include "wine/debug.h"
46 #include "wine/unicode.h"
48 #include "controller.h"
50 #ifdef WORDS_BIGENDIAN
51 # define LE_WORD(x) RtlUshortByteSwap(x)
53 # define LE_WORD(x) (x)
58 WINE_DEFAULT_DEBUG_CHANNEL(plugplay
);
62 #define VID_MICROSOFT 0x045e
64 static const WORD PID_XBOX_CONTROLLERS
[] = {
65 0x0202, /* Xbox Controller */
66 0x0285, /* Xbox Controller S */
67 0x0289, /* Xbox Controller S */
68 0x028e, /* Xbox360 Controller */
69 0x028f, /* Xbox360 Wireless Controller */
70 0x02d1, /* Xbox One Controller */
71 0x02dd, /* Xbox One Controller (Covert Forces/Firmware 2015) */
72 0x02e3, /* Xbox One Elite Controller */
73 0x02e6, /* Wireless XBox Controller Dongle */
74 0x02ea, /* Xbox One S Controller */
75 0x0719, /* Xbox 360 Wireless Adapter */
78 WINE_DECLARE_DEBUG_CHANNEL(hid_report
);
80 static DRIVER_OBJECT
*sdl_driver_obj
= NULL
;
82 static const WCHAR sdl_busidW
[] = {'S','D','L','J','O','Y',0};
84 static DWORD map_controllers
= 0;
87 DEFINE_GUID(GUID_DEVCLASS_SDL
, 0x463d60b5,0x802b,0x4bb2,0x8f,0xdb,0x7d,0xa9,0xb9,0x96,0x04,0xd8);
89 static void *sdl_handle
= NULL
;
92 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
93 MAKE_FUNCPTR(SDL_GetError
);
94 MAKE_FUNCPTR(SDL_Init
);
95 MAKE_FUNCPTR(SDL_JoystickEventState
);
96 MAKE_FUNCPTR(SDL_JoystickGetGUID
);
97 MAKE_FUNCPTR(SDL_JoystickGetGUIDString
);
98 MAKE_FUNCPTR(SDL_JoystickInstanceID
);
99 MAKE_FUNCPTR(SDL_JoystickName
);
100 MAKE_FUNCPTR(SDL_JoystickNumAxes
);
101 MAKE_FUNCPTR(SDL_JoystickOpen
);
102 MAKE_FUNCPTR(SDL_WaitEvent
);
103 MAKE_FUNCPTR(SDL_JoystickNumButtons
);
104 MAKE_FUNCPTR(SDL_JoystickNumBalls
);
105 MAKE_FUNCPTR(SDL_JoystickNumHats
);
106 MAKE_FUNCPTR(SDL_JoystickGetAxis
);
107 MAKE_FUNCPTR(SDL_JoystickGetHat
);
108 MAKE_FUNCPTR(SDL_IsGameController
);
109 MAKE_FUNCPTR(SDL_GameControllerGetAxis
);
110 MAKE_FUNCPTR(SDL_GameControllerName
);
111 MAKE_FUNCPTR(SDL_GameControllerOpen
);
112 MAKE_FUNCPTR(SDL_GameControllerEventState
);
113 MAKE_FUNCPTR(SDL_HapticClose
);
114 MAKE_FUNCPTR(SDL_HapticDestroyEffect
);
115 MAKE_FUNCPTR(SDL_HapticNewEffect
);
116 MAKE_FUNCPTR(SDL_HapticOpenFromJoystick
);
117 MAKE_FUNCPTR(SDL_HapticQuery
);
118 MAKE_FUNCPTR(SDL_HapticRumbleInit
);
119 MAKE_FUNCPTR(SDL_HapticRumblePlay
);
120 MAKE_FUNCPTR(SDL_HapticRumbleSupported
);
121 MAKE_FUNCPTR(SDL_HapticRunEffect
);
122 MAKE_FUNCPTR(SDL_HapticStopAll
);
123 MAKE_FUNCPTR(SDL_JoystickIsHaptic
);
124 MAKE_FUNCPTR(SDL_memset
);
126 static Uint16 (*pSDL_JoystickGetProduct
)(SDL_Joystick
* joystick
);
127 static Uint16 (*pSDL_JoystickGetProductVersion
)(SDL_Joystick
* joystick
);
128 static Uint16 (*pSDL_JoystickGetVendor
)(SDL_Joystick
* joystick
);
130 struct platform_private
132 SDL_Joystick
*sdl_joystick
;
133 SDL_GameController
*sdl_controller
;
140 int report_descriptor_size
;
141 BYTE
*report_descriptor
;
146 SDL_Haptic
*sdl_haptic
;
147 int haptic_effect_id
;
150 static inline struct platform_private
*impl_from_DEVICE_OBJECT(DEVICE_OBJECT
*device
)
152 return (struct platform_private
*)get_platform_private(device
);
155 static const BYTE REPORT_AXIS_TAIL
[] = {
156 0x16, 0x00, 0x80, /* LOGICAL_MINIMUM (-32768) */
157 0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (32767) */
158 0x36, 0x00, 0x80, /* PHYSICAL_MINIMUM (-32768) */
159 0x46, 0xff, 0x7f, /* PHYSICAL_MAXIMUM (32767) */
160 0x75, 0x10, /* REPORT_SIZE (16) */
161 0x95, 0x00, /* REPORT_COUNT (?) */
162 0x81, 0x02, /* INPUT (Data,Var,Abs) */
164 #define IDX_ABS_AXIS_COUNT 15
166 static const BYTE CONTROLLER_BUTTONS
[] = {
167 0x05, 0x09, /* USAGE_PAGE (Button) */
168 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
169 0x29, 0x0f, /* USAGE_MAXIMUM (Button 15) */
170 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
171 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
172 0x35, 0x00, /* LOGICAL_MINIMUM (0) */
173 0x45, 0x01, /* LOGICAL_MAXIMUM (1) */
174 0x95, 0x0f, /* REPORT_COUNT (15) */
175 0x75, 0x01, /* REPORT_SIZE (1) */
176 0x81, 0x02, /* INPUT (Data,Var,Abs) */
178 0x95, 0x01, /* REPORT_COUNT (1) */
179 0x75, 0x01, /* REPORT_SIZE (1) */
180 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
183 static const BYTE CONTROLLER_AXIS
[] = {
184 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
185 0x09, 0x30, /* USAGE (X) */
186 0x09, 0x31, /* USAGE (Y) */
187 0x09, 0x33, /* USAGE (RX) */
188 0x09, 0x34, /* USAGE (RY) */
189 0x16, 0x00, 0x80, /* LOGICAL_MINIMUM (-32768) */
190 0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (32767) */
191 0x36, 0x00, 0x80, /* PHYSICAL_MINIMUM (-32768) */
192 0x46, 0xff, 0x7f, /* PHYSICAL_MAXIMUM (32767) */
193 0x75, 0x10, /* REPORT_SIZE (16) */
194 0x95, 0x04, /* REPORT_COUNT (4) */
195 0x81, 0x02, /* INPUT (Data,Var,Abs) */
198 static const BYTE CONTROLLER_TRIGGERS
[] = {
199 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
200 0x09, 0x32, /* USAGE (Z) */
201 0x09, 0x35, /* USAGE (RZ) */
202 0x16, 0x00, 0x00, /* LOGICAL_MINIMUM (0) */
203 0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (32767) */
204 0x36, 0x00, 0x00, /* PHYSICAL_MINIMUM (0) */
205 0x46, 0xff, 0x7f, /* PHYSICAL_MAXIMUM (32767) */
206 0x75, 0x10, /* REPORT_SIZE (16) */
207 0x95, 0x02, /* REPORT_COUNT (2) */
208 0x81, 0x02, /* INPUT (Data,Var,Abs) */
211 static const BYTE HAPTIC_RUMBLE
[] = {
212 0x06, 0x00, 0xff, /* USAGE PAGE (vendor-defined) */
213 0x09, 0x01, /* USAGE (1) */
214 0x85, 0x00, /* REPORT_ID (0) */
216 0x95, 0x02, /* REPORT_COUNT (2) */
217 0x75, 0x08, /* REPORT_SIZE (8) */
218 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
220 0x15, 0x00, /* LOGICAL MINIMUM (0) */
221 0x25, 0xff, /* LOGICAL MAXIMUM (255) */
222 0x35, 0x00, /* PHYSICAL MINIMUM (0) */
223 0x45, 0xff, /* PHYSICAL MAXIMUM (255) */
224 0x75, 0x08, /* REPORT_SIZE (8) */
225 0x95, 0x02, /* REPORT_COUNT (2) */
226 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
228 0x95, 0x02, /* REPORT_COUNT (3) */
229 0x75, 0x08, /* REPORT_SIZE (8) */
230 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
233 static BYTE
*add_axis_block(BYTE
*report_ptr
, BYTE count
, BYTE page
, const BYTE
*usages
, BOOL absolute
)
236 memcpy(report_ptr
, REPORT_AXIS_HEADER
, sizeof(REPORT_AXIS_HEADER
));
237 report_ptr
[IDX_AXIS_PAGE
] = page
;
238 report_ptr
+= sizeof(REPORT_AXIS_HEADER
);
239 for (i
= 0; i
< count
; i
++)
241 memcpy(report_ptr
, REPORT_AXIS_USAGE
, sizeof(REPORT_AXIS_USAGE
));
242 report_ptr
[IDX_AXIS_USAGE
] = usages
[i
];
243 report_ptr
+= sizeof(REPORT_AXIS_USAGE
);
247 memcpy(report_ptr
, REPORT_AXIS_TAIL
, sizeof(REPORT_AXIS_TAIL
));
248 report_ptr
[IDX_ABS_AXIS_COUNT
] = count
;
249 report_ptr
+= sizeof(REPORT_AXIS_TAIL
);
253 memcpy(report_ptr
, REPORT_REL_AXIS_TAIL
, sizeof(REPORT_REL_AXIS_TAIL
));
254 report_ptr
[IDX_REL_AXIS_COUNT
] = count
;
255 report_ptr
+= sizeof(REPORT_REL_AXIS_TAIL
);
260 static void set_axis_value(struct platform_private
*ext
, int index
, short value
)
263 offset
= ext
->axis_start
+ index
* 2;
264 *((WORD
*)&ext
->report_buffer
[offset
]) = LE_WORD(value
);
267 static void set_ball_value(struct platform_private
*ext
, int index
, int value1
, int value2
)
270 offset
= ext
->ball_start
+ (index
* 2);
271 if (value1
> 127) value1
= 127;
272 if (value1
< -127) value1
= -127;
273 if (value2
> 127) value2
= 127;
274 if (value2
< -127) value2
= -127;
275 ext
->report_buffer
[offset
] = value1
;
276 ext
->report_buffer
[offset
+ 1] = value2
;
279 static void set_hat_value(struct platform_private
*ext
, int index
, int value
)
282 offset
= ext
->hat_start
+ index
;
285 case SDL_HAT_CENTERED
: ext
->report_buffer
[offset
] = 8; break;
286 case SDL_HAT_UP
: ext
->report_buffer
[offset
] = 0; break;
287 case SDL_HAT_RIGHTUP
: ext
->report_buffer
[offset
] = 1; break;
288 case SDL_HAT_RIGHT
: ext
->report_buffer
[offset
] = 2; break;
289 case SDL_HAT_RIGHTDOWN
: ext
->report_buffer
[offset
] = 3; break;
290 case SDL_HAT_DOWN
: ext
->report_buffer
[offset
] = 4; break;
291 case SDL_HAT_LEFTDOWN
: ext
->report_buffer
[offset
] = 5; break;
292 case SDL_HAT_LEFT
: ext
->report_buffer
[offset
] = 6; break;
293 case SDL_HAT_LEFTUP
: ext
->report_buffer
[offset
] = 7; break;
297 static int test_haptic(struct platform_private
*ext
)
300 if (pSDL_JoystickIsHaptic(ext
->sdl_joystick
))
302 ext
->sdl_haptic
= pSDL_HapticOpenFromJoystick(ext
->sdl_joystick
);
303 if (ext
->sdl_haptic
&&
304 ((pSDL_HapticQuery(ext
->sdl_haptic
) & SDL_HAPTIC_LEFTRIGHT
) != 0 ||
305 pSDL_HapticRumbleSupported(ext
->sdl_haptic
)))
307 pSDL_HapticStopAll(ext
->sdl_haptic
);
308 pSDL_HapticRumbleInit(ext
->sdl_haptic
);
309 rc
= sizeof(HAPTIC_RUMBLE
);
310 ext
->haptic_effect_id
= -1;
314 pSDL_HapticClose(ext
->sdl_haptic
);
315 ext
->sdl_haptic
= NULL
;
321 static int build_haptic(struct platform_private
*ext
, BYTE
*report_ptr
)
325 memcpy(report_ptr
, HAPTIC_RUMBLE
, sizeof(HAPTIC_RUMBLE
));
326 return (sizeof(HAPTIC_RUMBLE
));
331 static BOOL
build_report_descriptor(struct platform_private
*ext
)
334 INT i
, descript_size
;
336 INT button_count
, axis_count
, ball_count
, hat_count
;
337 static const BYTE device_usage
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_GAMEPAD
};
338 static const BYTE controller_usages
[] = {
342 HID_USAGE_GENERIC_RX
,
343 HID_USAGE_GENERIC_RY
,
344 HID_USAGE_GENERIC_RZ
,
345 HID_USAGE_GENERIC_SLIDER
,
346 HID_USAGE_GENERIC_DIAL
,
347 HID_USAGE_GENERIC_WHEEL
};
348 static const BYTE joystick_usages
[] = {
352 HID_USAGE_GENERIC_RZ
,
353 HID_USAGE_GENERIC_RX
,
354 HID_USAGE_GENERIC_RY
,
355 HID_USAGE_GENERIC_SLIDER
,
356 HID_USAGE_GENERIC_DIAL
,
357 HID_USAGE_GENERIC_WHEEL
};
359 descript_size
= sizeof(REPORT_HEADER
) + sizeof(REPORT_TAIL
);
362 /* For now lump all buttons just into incremental usages, Ignore Keys */
363 button_count
= pSDL_JoystickNumButtons(ext
->sdl_joystick
);
366 descript_size
+= sizeof(REPORT_BUTTONS
);
367 if (button_count
% 8)
368 descript_size
+= sizeof(REPORT_PADDING
);
369 report_size
= (button_count
+ 7) / 8;
372 axis_count
= pSDL_JoystickNumAxes(ext
->sdl_joystick
);
375 FIXME("Clamping joystick to 6 axis\n");
379 ext
->axis_start
= report_size
;
382 descript_size
+= sizeof(REPORT_AXIS_HEADER
);
383 descript_size
+= (sizeof(REPORT_AXIS_USAGE
) * axis_count
);
384 descript_size
+= sizeof(REPORT_AXIS_TAIL
);
385 report_size
+= (2 * axis_count
);
388 ball_count
= pSDL_JoystickNumBalls(ext
->sdl_joystick
);
389 ext
->ball_start
= report_size
;
392 if ((ball_count
*2) + axis_count
> 9)
394 FIXME("Capping ball + axis at 9\n");
395 ball_count
= (9-axis_count
)/2;
397 descript_size
+= sizeof(REPORT_AXIS_HEADER
);
398 descript_size
+= (sizeof(REPORT_AXIS_USAGE
) * ball_count
* 2);
399 descript_size
+= sizeof(REPORT_REL_AXIS_TAIL
);
400 report_size
+= (2*ball_count
);
403 hat_count
= pSDL_JoystickNumHats(ext
->sdl_joystick
);
404 ext
->hat_start
= report_size
;
407 descript_size
+= sizeof(REPORT_HATSWITCH
);
408 for (i
= 0; i
< hat_count
; i
++)
412 descript_size
+= test_haptic(ext
);
414 TRACE("Report Descriptor will be %i bytes\n", descript_size
);
415 TRACE("Report will be %i bytes\n", report_size
);
417 ext
->report_descriptor
= HeapAlloc(GetProcessHeap(), 0, descript_size
);
418 if (!ext
->report_descriptor
)
420 ERR("Failed to alloc report descriptor\n");
423 report_ptr
= ext
->report_descriptor
;
425 memcpy(report_ptr
, REPORT_HEADER
, sizeof(REPORT_HEADER
));
426 report_ptr
[IDX_HEADER_PAGE
] = device_usage
[0];
427 report_ptr
[IDX_HEADER_USAGE
] = device_usage
[1];
428 report_ptr
+= sizeof(REPORT_HEADER
);
431 report_ptr
= add_button_block(report_ptr
, 1, button_count
);
432 if (button_count
% 8)
434 BYTE padding
= 8 - (button_count
% 8);
435 report_ptr
= add_padding_block(report_ptr
, padding
);
440 if (axis_count
== 6 && button_count
>= 14)
441 report_ptr
= add_axis_block(report_ptr
, axis_count
, HID_USAGE_PAGE_GENERIC
, controller_usages
, TRUE
);
443 report_ptr
= add_axis_block(report_ptr
, axis_count
, HID_USAGE_PAGE_GENERIC
, joystick_usages
, TRUE
);
448 report_ptr
= add_axis_block(report_ptr
, ball_count
* 2, HID_USAGE_PAGE_GENERIC
, &joystick_usages
[axis_count
], FALSE
);
451 report_ptr
= add_hatswitch(report_ptr
, hat_count
);
453 report_ptr
+= build_haptic(ext
, report_ptr
);
454 memcpy(report_ptr
, REPORT_TAIL
, sizeof(REPORT_TAIL
));
456 ext
->report_descriptor_size
= descript_size
;
457 ext
->buffer_length
= report_size
;
458 ext
->report_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, report_size
);
459 if (ext
->report_buffer
== NULL
)
461 ERR("Failed to alloc report buffer\n");
462 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
466 /* Initialize axis in the report */
467 for (i
= 0; i
< axis_count
; i
++)
468 set_axis_value(ext
, i
, pSDL_JoystickGetAxis(ext
->sdl_joystick
, i
));
469 for (i
= 0; i
< hat_count
; i
++)
470 set_hat_value(ext
, i
, pSDL_JoystickGetHat(ext
->sdl_joystick
, i
));
475 static BOOL
build_mapped_report_descriptor(struct platform_private
*ext
)
478 INT i
, descript_size
;
480 descript_size
= sizeof(REPORT_HEADER
) + sizeof(REPORT_TAIL
);
481 descript_size
+= sizeof(CONTROLLER_BUTTONS
);
482 descript_size
+= sizeof(CONTROLLER_AXIS
);
483 descript_size
+= sizeof(CONTROLLER_TRIGGERS
);
484 descript_size
+= test_haptic(ext
);
487 ext
->buffer_length
= 14;
489 TRACE("Report Descriptor will be %i bytes\n", descript_size
);
490 TRACE("Report will be %i bytes\n", ext
->buffer_length
);
492 ext
->report_descriptor
= HeapAlloc(GetProcessHeap(), 0, descript_size
);
493 if (!ext
->report_descriptor
)
495 ERR("Failed to alloc report descriptor\n");
498 report_ptr
= ext
->report_descriptor
;
500 memcpy(report_ptr
, REPORT_HEADER
, sizeof(REPORT_HEADER
));
501 report_ptr
[IDX_HEADER_PAGE
] = HID_USAGE_PAGE_GENERIC
;
502 report_ptr
[IDX_HEADER_USAGE
] = HID_USAGE_GENERIC_GAMEPAD
;
503 report_ptr
+= sizeof(REPORT_HEADER
);
504 memcpy(report_ptr
, CONTROLLER_BUTTONS
, sizeof(CONTROLLER_BUTTONS
));
505 report_ptr
+= sizeof(CONTROLLER_BUTTONS
);
506 memcpy(report_ptr
, CONTROLLER_AXIS
, sizeof(CONTROLLER_AXIS
));
507 report_ptr
+= sizeof(CONTROLLER_AXIS
);
508 memcpy(report_ptr
, CONTROLLER_TRIGGERS
, sizeof(CONTROLLER_TRIGGERS
));
509 report_ptr
+= sizeof(CONTROLLER_TRIGGERS
);
510 report_ptr
+= build_haptic(ext
, report_ptr
);
511 memcpy(report_ptr
, REPORT_TAIL
, sizeof(REPORT_TAIL
));
513 ext
->report_descriptor_size
= descript_size
;
514 ext
->report_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ext
->buffer_length
);
515 if (ext
->report_buffer
== NULL
)
517 ERR("Failed to alloc report buffer\n");
518 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
522 /* Initialize axis in the report */
523 for (i
= SDL_CONTROLLER_AXIS_LEFTX
; i
< SDL_CONTROLLER_AXIS_MAX
; i
++)
524 set_axis_value(ext
, i
, pSDL_GameControllerGetAxis(ext
->sdl_controller
, i
));
529 static int compare_platform_device(DEVICE_OBJECT
*device
, void *platform_dev
)
531 SDL_JoystickID id1
= impl_from_DEVICE_OBJECT(device
)->id
;
532 SDL_JoystickID id2
= PtrToUlong(platform_dev
);
536 static NTSTATUS
get_reportdescriptor(DEVICE_OBJECT
*device
, BYTE
*buffer
, DWORD length
, DWORD
*out_length
)
538 struct platform_private
*ext
= impl_from_DEVICE_OBJECT(device
);
540 *out_length
= ext
->report_descriptor_size
;
542 if (length
< ext
->report_descriptor_size
)
543 return STATUS_BUFFER_TOO_SMALL
;
545 memcpy(buffer
, ext
->report_descriptor
, ext
->report_descriptor_size
);
547 return STATUS_SUCCESS
;
550 static NTSTATUS
get_string(DEVICE_OBJECT
*device
, DWORD index
, WCHAR
*buffer
, DWORD length
)
552 struct platform_private
*ext
= impl_from_DEVICE_OBJECT(device
);
553 const char* str
= NULL
;
557 case HID_STRING_ID_IPRODUCT
:
558 if (ext
->sdl_controller
)
559 str
= pSDL_GameControllerName(ext
->sdl_controller
);
561 str
= pSDL_JoystickName(ext
->sdl_joystick
);
563 case HID_STRING_ID_IMANUFACTURER
:
566 case HID_STRING_ID_ISERIALNUMBER
:
570 ERR("Unhandled string index %i\n", index
);
574 MultiByteToWideChar(CP_ACP
, 0, str
, -1, buffer
, length
);
578 return STATUS_SUCCESS
;
581 static NTSTATUS
begin_report_processing(DEVICE_OBJECT
*device
)
583 return STATUS_SUCCESS
;
586 static NTSTATUS
set_output_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
588 struct platform_private
*ext
= impl_from_DEVICE_OBJECT(device
);
590 if (ext
->sdl_haptic
&& id
== 0)
592 WORD left
= report
[2] * 128;
593 WORD right
= report
[3] * 128;
595 if (ext
->haptic_effect_id
>= 0)
597 pSDL_HapticDestroyEffect(ext
->sdl_haptic
, ext
->haptic_effect_id
);
598 ext
->haptic_effect_id
= -1;
600 pSDL_HapticStopAll(ext
->sdl_haptic
);
601 if (left
!= 0 || right
!= 0)
603 SDL_HapticEffect effect
;
605 pSDL_memset( &effect
, 0, sizeof(SDL_HapticEffect
) );
606 effect
.type
= SDL_HAPTIC_LEFTRIGHT
;
607 effect
.leftright
.length
= -1;
608 effect
.leftright
.large_magnitude
= left
;
609 effect
.leftright
.small_magnitude
= right
;
611 ext
->haptic_effect_id
= pSDL_HapticNewEffect(ext
->sdl_haptic
, &effect
);
612 if (ext
->haptic_effect_id
>= 0)
614 pSDL_HapticRunEffect(ext
->sdl_haptic
, ext
->haptic_effect_id
, 1);
618 float i
= (float)((left
+ right
)/2.0) / 32767.0;
619 pSDL_HapticRumblePlay(ext
->sdl_haptic
, i
, -1);
623 return STATUS_SUCCESS
;
628 return STATUS_NOT_IMPLEMENTED
;
632 static NTSTATUS
get_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*read
)
635 return STATUS_NOT_IMPLEMENTED
;
638 static NTSTATUS
set_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
641 return STATUS_NOT_IMPLEMENTED
;
644 static const platform_vtbl sdl_vtbl
=
646 compare_platform_device
,
647 get_reportdescriptor
,
649 begin_report_processing
,
655 static BOOL
set_report_from_event(SDL_Event
*event
)
657 DEVICE_OBJECT
*device
;
658 struct platform_private
*private;
659 /* All the events coming in will have 'which' as a 3rd field */
660 SDL_JoystickID index
= ((SDL_JoyButtonEvent
*)event
)->which
;
662 device
= bus_find_hid_device(&sdl_vtbl
, ULongToPtr(index
));
665 ERR("Failed to find device at index %i\n",index
);
668 private = impl_from_DEVICE_OBJECT(device
);
669 if (private->sdl_controller
)
671 /* We want mapped events */
677 case SDL_JOYBUTTONDOWN
:
678 case SDL_JOYBUTTONUP
:
680 SDL_JoyButtonEvent
*ie
= &event
->jbutton
;
682 set_button_value(ie
->button
, ie
->state
, private->report_buffer
);
684 process_hid_report(device
, private->report_buffer
, private->buffer_length
);
687 case SDL_JOYAXISMOTION
:
689 SDL_JoyAxisEvent
*ie
= &event
->jaxis
;
693 set_axis_value(private, ie
->axis
, ie
->value
);
694 process_hid_report(device
, private->report_buffer
, private->buffer_length
);
698 case SDL_JOYBALLMOTION
:
700 SDL_JoyBallEvent
*ie
= &event
->jball
;
702 set_ball_value(private, ie
->ball
, ie
->xrel
, ie
->yrel
);
703 process_hid_report(device
, private->report_buffer
, private->buffer_length
);
706 case SDL_JOYHATMOTION
:
708 SDL_JoyHatEvent
*ie
= &event
->jhat
;
710 set_hat_value(private, ie
->hat
, ie
->value
);
711 process_hid_report(device
, private->report_buffer
, private->buffer_length
);
715 ERR("TODO: Process Report (0x%x)\n",event
->type
);
720 static BOOL
set_mapped_report_from_event(SDL_Event
*event
)
722 DEVICE_OBJECT
*device
;
723 struct platform_private
*private;
724 /* All the events coming in will have 'which' as a 3rd field */
725 int index
= ((SDL_ControllerButtonEvent
*)event
)->which
;
726 device
= bus_find_hid_device(&sdl_vtbl
, ULongToPtr(index
));
729 ERR("Failed to find device at index %i\n",index
);
732 private = impl_from_DEVICE_OBJECT(device
);
736 case SDL_CONTROLLERBUTTONDOWN
:
737 case SDL_CONTROLLERBUTTONUP
:
740 SDL_ControllerButtonEvent
*ie
= &event
->cbutton
;
744 case SDL_CONTROLLER_BUTTON_A
: usage
= 0; break;
745 case SDL_CONTROLLER_BUTTON_B
: usage
= 1; break;
746 case SDL_CONTROLLER_BUTTON_X
: usage
= 2; break;
747 case SDL_CONTROLLER_BUTTON_Y
: usage
= 3; break;
748 case SDL_CONTROLLER_BUTTON_LEFTSHOULDER
: usage
= 4; break;
749 case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
: usage
= 5; break;
750 case SDL_CONTROLLER_BUTTON_LEFTSTICK
: usage
= 6; break;
751 case SDL_CONTROLLER_BUTTON_RIGHTSTICK
: usage
= 7; break;
752 case SDL_CONTROLLER_BUTTON_START
: usage
= 8; break;
753 case SDL_CONTROLLER_BUTTON_BACK
: usage
= 9; break;
754 case SDL_CONTROLLER_BUTTON_GUIDE
: usage
= 10; break;
755 case SDL_CONTROLLER_BUTTON_DPAD_UP
: usage
= 11; break;
756 case SDL_CONTROLLER_BUTTON_DPAD_DOWN
: usage
= 12; break;
757 case SDL_CONTROLLER_BUTTON_DPAD_LEFT
: usage
= 13; break;
758 case SDL_CONTROLLER_BUTTON_DPAD_RIGHT
: usage
= 14; break;
760 ERR("Unknown Button %i\n",ie
->button
);
765 set_button_value(usage
, ie
->state
, private->report_buffer
);
766 process_hid_report(device
, private->report_buffer
, private->buffer_length
);
770 case SDL_CONTROLLERAXISMOTION
:
772 SDL_ControllerAxisEvent
*ie
= &event
->caxis
;
774 set_axis_value(private, ie
->axis
, ie
->value
);
775 process_hid_report(device
, private->report_buffer
, private->buffer_length
);
779 ERR("TODO: Process Report (%x)\n",event
->type
);
784 static void try_remove_device(SDL_JoystickID index
)
786 DEVICE_OBJECT
*device
= NULL
;
788 device
= bus_find_hid_device(&sdl_vtbl
, ULongToPtr(index
));
791 IoInvalidateDeviceRelations(device
, RemovalRelations
);
793 bus_remove_hid_device(device
);
796 static void try_add_device(SDL_JoystickID index
)
798 DWORD vid
= 0, pid
= 0, version
= 0;
799 DEVICE_OBJECT
*device
= NULL
;
800 WCHAR serial
[34] = {0};
802 BOOL is_xbox_gamepad
;
804 SDL_Joystick
* joystick
;
806 SDL_JoystickGUID guid
;
807 SDL_GameController
*controller
= NULL
;
809 if ((joystick
= pSDL_JoystickOpen(index
)) == NULL
)
811 WARN("Unable to open sdl device %i: %s\n", index
, pSDL_GetError());
815 if (map_controllers
&& pSDL_IsGameController(index
))
816 controller
= pSDL_GameControllerOpen(index
);
822 pid
= PID_XBOX_CONTROLLERS
[3];
827 if (pSDL_JoystickGetProductVersion
!= NULL
) {
828 vid
= pSDL_JoystickGetVendor(joystick
);
829 pid
= pSDL_JoystickGetProduct(joystick
);
830 version
= pSDL_JoystickGetProductVersion(joystick
);
835 pid
= pSDL_JoystickInstanceID(joystick
) + 1;
840 guid
= pSDL_JoystickGetGUID(joystick
);
841 pSDL_JoystickGetGUIDString(guid
, guid_str
, sizeof(guid_str
));
842 MultiByteToWideChar(CP_ACP
, 0, guid_str
, -1, serial
, sizeof(guid_str
));
846 TRACE("Found sdl game controller %i (vid %04x, pid %04x, version %u, serial %s)\n",
847 index
, vid
, pid
, version
, debugstr_w(serial
));
848 is_xbox_gamepad
= TRUE
;
852 int button_count
, axis_count
;
854 TRACE("Found sdl device %i (vid %04x, pid %04x, version %u, serial %s)\n",
855 index
, vid
, pid
, version
, debugstr_w(serial
));
857 axis_count
= pSDL_JoystickNumAxes(joystick
);
858 button_count
= pSDL_JoystickNumAxes(joystick
);
859 is_xbox_gamepad
= (axis_count
== 6 && button_count
>= 14);
862 device
= bus_create_hid_device(sdl_driver_obj
, sdl_busidW
, vid
, pid
, version
, 0, serial
, is_xbox_gamepad
, &GUID_DEVCLASS_SDL
, &sdl_vtbl
, sizeof(struct platform_private
));
867 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
868 private->sdl_joystick
= joystick
;
869 private->sdl_controller
= controller
;
872 rc
= build_mapped_report_descriptor(private);
874 rc
= build_report_descriptor(private);
877 ERR("Building report descriptor failed, removing device\n");
878 bus_remove_hid_device(device
);
879 HeapFree(GetProcessHeap(), 0, serial
);
882 IoInvalidateDeviceRelations(device
, BusRelations
);
886 WARN("Ignoring device %i\n", index
);
890 static void process_device_event(SDL_Event
*event
)
892 TRACE_(hid_report
)("Received action %x\n", event
->type
);
894 if (event
->type
== SDL_JOYDEVICEADDED
)
895 try_add_device(((SDL_JoyDeviceEvent
*)event
)->which
);
896 else if (event
->type
== SDL_JOYDEVICEREMOVED
)
897 try_remove_device(((SDL_JoyDeviceEvent
*)event
)->which
);
898 else if (event
->type
>= SDL_JOYAXISMOTION
&& event
->type
<= SDL_JOYBUTTONUP
)
899 set_report_from_event(event
);
900 else if (event
->type
>= SDL_CONTROLLERAXISMOTION
&& event
->type
<= SDL_CONTROLLERBUTTONUP
)
901 set_mapped_report_from_event(event
);
904 static DWORD CALLBACK
deviceloop_thread(void *args
)
906 HANDLE init_done
= args
;
909 if (pSDL_Init(SDL_INIT_GAMECONTROLLER
|SDL_INIT_HAPTIC
) < 0)
911 ERR("Can't Init SDL\n");
912 return STATUS_UNSUCCESSFUL
;
915 pSDL_JoystickEventState(SDL_ENABLE
);
916 pSDL_GameControllerEventState(SDL_ENABLE
);
921 while (pSDL_WaitEvent(&event
) != 0)
922 process_device_event(&event
);
924 TRACE("Device thread exiting\n");
928 NTSTATUS WINAPI
sdl_driver_init(DRIVER_OBJECT
*driver
, UNICODE_STRING
*registry_path
)
930 static const WCHAR controller_modeW
[] = {'M','a','p',' ','C','o','n','t','r','o','l','l','e','r','s',0};
931 static const UNICODE_STRING controller_mode
= {sizeof(controller_modeW
) - sizeof(WCHAR
), sizeof(controller_modeW
), (WCHAR
*)controller_modeW
};
936 TRACE("(%p, %s)\n", driver
, debugstr_w(registry_path
->Buffer
));
937 if (sdl_handle
== NULL
)
939 sdl_handle
= wine_dlopen(SONAME_LIBSDL2
, RTLD_NOW
, NULL
, 0);
941 WARN("could not load %s\n", SONAME_LIBSDL2
);
942 sdl_driver_obj
= NULL
;
943 return STATUS_UNSUCCESSFUL
;
945 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(sdl_handle, #f, NULL, 0)) == NULL){WARN("Can't find symbol %s\n", #f); goto sym_not_found;}
946 LOAD_FUNCPTR(SDL_GetError
);
947 LOAD_FUNCPTR(SDL_Init
);
948 LOAD_FUNCPTR(SDL_JoystickEventState
);
949 LOAD_FUNCPTR(SDL_JoystickGetGUID
);
950 LOAD_FUNCPTR(SDL_JoystickGetGUIDString
);
951 LOAD_FUNCPTR(SDL_JoystickInstanceID
);
952 LOAD_FUNCPTR(SDL_JoystickName
);
953 LOAD_FUNCPTR(SDL_JoystickNumAxes
);
954 LOAD_FUNCPTR(SDL_JoystickOpen
);
955 LOAD_FUNCPTR(SDL_WaitEvent
);
956 LOAD_FUNCPTR(SDL_JoystickNumButtons
);
957 LOAD_FUNCPTR(SDL_JoystickNumBalls
);
958 LOAD_FUNCPTR(SDL_JoystickNumHats
);
959 LOAD_FUNCPTR(SDL_JoystickGetAxis
);
960 LOAD_FUNCPTR(SDL_JoystickGetHat
);
961 LOAD_FUNCPTR(SDL_IsGameController
);
962 LOAD_FUNCPTR(SDL_GameControllerGetAxis
);
963 LOAD_FUNCPTR(SDL_GameControllerName
);
964 LOAD_FUNCPTR(SDL_GameControllerOpen
);
965 LOAD_FUNCPTR(SDL_GameControllerEventState
);
966 LOAD_FUNCPTR(SDL_HapticClose
);
967 LOAD_FUNCPTR(SDL_HapticDestroyEffect
);
968 LOAD_FUNCPTR(SDL_HapticNewEffect
);
969 LOAD_FUNCPTR(SDL_HapticOpenFromJoystick
);
970 LOAD_FUNCPTR(SDL_HapticQuery
);
971 LOAD_FUNCPTR(SDL_HapticRumbleInit
);
972 LOAD_FUNCPTR(SDL_HapticRumblePlay
);
973 LOAD_FUNCPTR(SDL_HapticRumbleSupported
);
974 LOAD_FUNCPTR(SDL_HapticRunEffect
);
975 LOAD_FUNCPTR(SDL_HapticStopAll
);
976 LOAD_FUNCPTR(SDL_JoystickIsHaptic
);
977 LOAD_FUNCPTR(SDL_memset
);
979 pSDL_JoystickGetProduct
= wine_dlsym(sdl_handle
, "SDL_JoystickGetProduct", NULL
, 0);
980 pSDL_JoystickGetProductVersion
= wine_dlsym(sdl_handle
, "SDL_JoystickGetProductVersion", NULL
, 0);
981 pSDL_JoystickGetVendor
= wine_dlsym(sdl_handle
, "SDL_JoystickGetVendor", NULL
, 0);
984 sdl_driver_obj
= driver
;
985 driver
->MajorFunction
[IRP_MJ_PNP
] = common_pnp_dispatch
;
986 driver
->MajorFunction
[IRP_MJ_INTERNAL_DEVICE_CONTROL
] = hid_internal_dispatch
;
988 map_controllers
= check_bus_option(registry_path
, &controller_mode
, 1);
990 if (!(events
[0] = CreateEventW(NULL
, TRUE
, FALSE
, NULL
)))
992 if (!(events
[1] = CreateThread(NULL
, 0, deviceloop_thread
, events
[0], 0, NULL
)))
994 CloseHandle(events
[0]);
998 result
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
999 CloseHandle(events
[0]);
1000 CloseHandle(events
[1]);
1001 if (result
== WAIT_OBJECT_0
)
1003 TRACE("Initialization successful\n");
1004 return STATUS_SUCCESS
;
1008 sdl_driver_obj
= NULL
;
1009 return STATUS_UNSUCCESSFUL
;
1011 wine_dlclose(sdl_handle
, NULL
, 0);
1013 return STATUS_UNSUCCESSFUL
;
1018 NTSTATUS WINAPI
sdl_driver_init(DRIVER_OBJECT
*driver
, UNICODE_STRING
*registry_path
)
1020 WARN("compiled without SDL support\n");
1021 return STATUS_NOT_IMPLEMENTED
;
1024 #endif /* SONAME_LIBSDL2 */