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
33 #include <sys/types.h>
42 #define WIN32_NO_STATUS
49 #include "ddk/hidtypes.h"
50 #include "ddk/hidsdi.h"
53 #include "wine/debug.h"
55 #include "wine/unixlib.h"
57 #include "unix_private.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(hid
);
63 static pthread_mutex_t sdl_cs
= PTHREAD_MUTEX_INITIALIZER
;
64 static struct sdl_bus_options options
;
66 static void *sdl_handle
= NULL
;
67 static UINT quit_event
= -1;
68 static struct list event_queue
= LIST_INIT(event_queue
);
69 static struct list device_list
= LIST_INIT(device_list
);
71 #define MAKE_FUNCPTR(f) static typeof(f) * p##f = NULL
72 MAKE_FUNCPTR(SDL_GetError
);
73 MAKE_FUNCPTR(SDL_Init
);
74 MAKE_FUNCPTR(SDL_JoystickClose
);
75 MAKE_FUNCPTR(SDL_JoystickEventState
);
76 MAKE_FUNCPTR(SDL_JoystickGetGUID
);
77 MAKE_FUNCPTR(SDL_JoystickGetGUIDString
);
78 MAKE_FUNCPTR(SDL_JoystickInstanceID
);
79 MAKE_FUNCPTR(SDL_JoystickName
);
80 MAKE_FUNCPTR(SDL_JoystickNumAxes
);
81 MAKE_FUNCPTR(SDL_JoystickOpen
);
82 MAKE_FUNCPTR(SDL_WaitEventTimeout
);
83 MAKE_FUNCPTR(SDL_JoystickNumButtons
);
84 MAKE_FUNCPTR(SDL_JoystickNumBalls
);
85 MAKE_FUNCPTR(SDL_JoystickNumHats
);
86 MAKE_FUNCPTR(SDL_JoystickGetAxis
);
87 MAKE_FUNCPTR(SDL_JoystickGetHat
);
88 MAKE_FUNCPTR(SDL_IsGameController
);
89 MAKE_FUNCPTR(SDL_GameControllerClose
);
90 MAKE_FUNCPTR(SDL_GameControllerGetAxis
);
91 MAKE_FUNCPTR(SDL_GameControllerGetButton
);
92 MAKE_FUNCPTR(SDL_GameControllerName
);
93 MAKE_FUNCPTR(SDL_GameControllerOpen
);
94 MAKE_FUNCPTR(SDL_GameControllerEventState
);
95 MAKE_FUNCPTR(SDL_HapticClose
);
96 MAKE_FUNCPTR(SDL_HapticDestroyEffect
);
97 MAKE_FUNCPTR(SDL_HapticGetEffectStatus
);
98 MAKE_FUNCPTR(SDL_HapticNewEffect
);
99 MAKE_FUNCPTR(SDL_HapticOpenFromJoystick
);
100 MAKE_FUNCPTR(SDL_HapticPause
);
101 MAKE_FUNCPTR(SDL_HapticQuery
);
102 MAKE_FUNCPTR(SDL_HapticRumbleInit
);
103 MAKE_FUNCPTR(SDL_HapticRumblePlay
);
104 MAKE_FUNCPTR(SDL_HapticRumbleStop
);
105 MAKE_FUNCPTR(SDL_HapticRumbleSupported
);
106 MAKE_FUNCPTR(SDL_HapticRunEffect
);
107 MAKE_FUNCPTR(SDL_HapticSetGain
);
108 MAKE_FUNCPTR(SDL_HapticStopAll
);
109 MAKE_FUNCPTR(SDL_HapticStopEffect
);
110 MAKE_FUNCPTR(SDL_HapticUnpause
);
111 MAKE_FUNCPTR(SDL_HapticUpdateEffect
);
112 MAKE_FUNCPTR(SDL_JoystickIsHaptic
);
113 MAKE_FUNCPTR(SDL_GameControllerAddMapping
);
114 MAKE_FUNCPTR(SDL_RegisterEvents
);
115 MAKE_FUNCPTR(SDL_PushEvent
);
116 MAKE_FUNCPTR(SDL_GetTicks
);
117 static int (*pSDL_JoystickRumble
)(SDL_Joystick
*joystick
, Uint16 low_frequency_rumble
, Uint16 high_frequency_rumble
, Uint32 duration_ms
);
118 static int (*pSDL_JoystickRumbleTriggers
)(SDL_Joystick
*joystick
, Uint16 left_rumble
, Uint16 right_rumble
, Uint32 duration_ms
);
119 static Uint16 (*pSDL_JoystickGetProduct
)(SDL_Joystick
* joystick
);
120 static Uint16 (*pSDL_JoystickGetProductVersion
)(SDL_Joystick
* joystick
);
121 static Uint16 (*pSDL_JoystickGetVendor
)(SDL_Joystick
* joystick
);
122 static SDL_JoystickType (*pSDL_JoystickGetType
)(SDL_Joystick
* joystick
);
123 static const char *(*pSDL_JoystickGetSerial
)(SDL_Joystick
* joystick
);
125 /* internal bits for extended rumble support, SDL_Haptic types are 16-bits */
126 #define WINE_SDL_JOYSTICK_RUMBLE 0x40000000 /* using SDL_JoystickRumble API */
127 #define WINE_SDL_HAPTIC_RUMBLE 0x80000000 /* using SDL_HapticRumble API */
129 #define EFFECT_SUPPORT_HAPTICS (SDL_HAPTIC_LEFTRIGHT|WINE_SDL_HAPTIC_RUMBLE|WINE_SDL_JOYSTICK_RUMBLE)
130 #define EFFECT_SUPPORT_PHYSICAL (SDL_HAPTIC_CONSTANT|SDL_HAPTIC_RAMP|SDL_HAPTIC_SINE|SDL_HAPTIC_TRIANGLE| \
131 SDL_HAPTIC_SAWTOOTHUP|SDL_HAPTIC_SAWTOOTHDOWN|SDL_HAPTIC_SPRING|SDL_HAPTIC_DAMPER|SDL_HAPTIC_INERTIA| \
132 SDL_HAPTIC_FRICTION|SDL_HAPTIC_CUSTOM)
136 struct unix_device unix_device
;
138 SDL_Joystick
*sdl_joystick
;
139 SDL_GameController
*sdl_controller
;
143 DWORD effect_support
;
144 SDL_Haptic
*sdl_haptic
;
145 int haptic_effect_id
;
147 int effect_state
[256];
152 static inline struct sdl_device
*impl_from_unix_device(struct unix_device
*iface
)
154 return CONTAINING_RECORD(iface
, struct sdl_device
, unix_device
);
157 static struct sdl_device
*find_device_from_id(SDL_JoystickID id
)
159 struct sdl_device
*impl
;
161 LIST_FOR_EACH_ENTRY(impl
, &device_list
, struct sdl_device
, unix_device
.entry
)
162 if (impl
->id
== id
&& impl
->axis_offset
== 0) return impl
;
167 static struct sdl_device
*find_device_from_id_and_axis(SDL_JoystickID id
, int axis
)
169 struct sdl_device
*impl
;
171 LIST_FOR_EACH_ENTRY(impl
, &device_list
, struct sdl_device
, unix_device
.entry
)
173 USHORT count
= impl
->unix_device
.hid_device_state
.abs_axis_count
;
174 if (impl
->id
== id
&& impl
->axis_offset
<= axis
&& impl
->axis_offset
+ count
> axis
)
181 static void set_hat_value(struct unix_device
*iface
, int index
, int value
)
186 case SDL_HAT_CENTERED
: break;
187 case SDL_HAT_DOWN
: y
= 1; break;
188 case SDL_HAT_RIGHTDOWN
: y
= x
= 1; break;
189 case SDL_HAT_RIGHT
: x
= 1; break;
190 case SDL_HAT_RIGHTUP
: x
= 1; y
= -1; break;
191 case SDL_HAT_UP
: y
= -1; break;
192 case SDL_HAT_LEFTUP
: x
= y
= -1; break;
193 case SDL_HAT_LEFT
: x
= -1; break;
194 case SDL_HAT_LEFTDOWN
: x
= -1; y
= 1; break;
196 hid_device_set_hatswitch_x(iface
, index
, x
);
197 hid_device_set_hatswitch_y(iface
, index
, y
);
200 static BOOL
descriptor_add_haptic(struct sdl_device
*impl
)
205 if (impl
->axis_offset
> 0 || !pSDL_JoystickIsHaptic(impl
->sdl_joystick
) ||
206 !(impl
->sdl_haptic
= pSDL_HapticOpenFromJoystick(impl
->sdl_joystick
)))
207 impl
->effect_support
= 0;
210 impl
->effect_support
= pSDL_HapticQuery(impl
->sdl_haptic
);
211 if (!(impl
->effect_support
& EFFECT_SUPPORT_HAPTICS
) &&
212 pSDL_HapticRumbleSupported(impl
->sdl_haptic
) &&
213 pSDL_HapticRumbleInit(impl
->sdl_haptic
) == 0)
214 impl
->effect_support
|= WINE_SDL_HAPTIC_RUMBLE
;
217 if (impl
->axis_offset
== 0 && pSDL_JoystickRumble
&& !pSDL_JoystickRumble(impl
->sdl_joystick
, 0, 0, 0))
218 impl
->effect_support
|= WINE_SDL_JOYSTICK_RUMBLE
;
220 if (impl
->effect_support
& EFFECT_SUPPORT_HAPTICS
)
222 if (!hid_device_add_haptics(&impl
->unix_device
))
226 if ((impl
->effect_support
& EFFECT_SUPPORT_PHYSICAL
))
228 /* SDL_HAPTIC_SQUARE doesn't exist */
229 if (impl
->effect_support
& SDL_HAPTIC_SINE
) usages
[count
++] = PID_USAGE_ET_SINE
;
230 if (impl
->effect_support
& SDL_HAPTIC_TRIANGLE
) usages
[count
++] = PID_USAGE_ET_TRIANGLE
;
231 if (impl
->effect_support
& SDL_HAPTIC_SAWTOOTHUP
) usages
[count
++] = PID_USAGE_ET_SAWTOOTH_UP
;
232 if (impl
->effect_support
& SDL_HAPTIC_SAWTOOTHDOWN
) usages
[count
++] = PID_USAGE_ET_SAWTOOTH_DOWN
;
233 if (impl
->effect_support
& SDL_HAPTIC_SPRING
) usages
[count
++] = PID_USAGE_ET_SPRING
;
234 if (impl
->effect_support
& SDL_HAPTIC_DAMPER
) usages
[count
++] = PID_USAGE_ET_DAMPER
;
235 if (impl
->effect_support
& SDL_HAPTIC_INERTIA
) usages
[count
++] = PID_USAGE_ET_INERTIA
;
236 if (impl
->effect_support
& SDL_HAPTIC_FRICTION
) usages
[count
++] = PID_USAGE_ET_FRICTION
;
237 if (impl
->effect_support
& SDL_HAPTIC_CONSTANT
) usages
[count
++] = PID_USAGE_ET_CONSTANT_FORCE
;
238 if (impl
->effect_support
& SDL_HAPTIC_RAMP
) usages
[count
++] = PID_USAGE_ET_RAMP
;
240 if (!hid_device_add_physical(&impl
->unix_device
, usages
, count
))
244 impl
->haptic_effect_id
= -1;
245 for (i
= 0; i
< ARRAY_SIZE(impl
->effect_ids
); ++i
) impl
->effect_ids
[i
] = -1;
249 static NTSTATUS
build_joystick_report_descriptor(struct unix_device
*iface
)
251 const USAGE_AND_PAGE device_usage
= {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_JOYSTICK
};
252 static const USAGE_AND_PAGE absolute_usages
[] =
254 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_X
},
255 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_Y
},
256 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_Z
},
257 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_RX
},
258 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_RY
},
259 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_RZ
},
260 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_SLIDER
},
261 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_DIAL
},
263 static const USAGE_AND_PAGE relative_usages
[] =
265 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_X
},
266 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_Y
},
267 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_RX
},
268 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_RY
},
269 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_Z
},
270 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_RZ
},
271 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_SLIDER
},
272 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_DIAL
},
273 {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_WHEEL
},
275 struct sdl_device
*impl
= impl_from_unix_device(iface
);
276 int i
, button_count
, axis_count
, ball_count
, hat_count
;
277 USAGE_AND_PAGE physical_usage
;
279 axis_count
= pSDL_JoystickNumAxes(impl
->sdl_joystick
);
280 if (options
.split_controllers
) axis_count
= min(6, axis_count
- impl
->axis_offset
);
281 if (axis_count
> ARRAY_SIZE(absolute_usages
))
283 FIXME("More than %zu absolute axes found, ignoring.\n", ARRAY_SIZE(absolute_usages
));
284 axis_count
= ARRAY_SIZE(absolute_usages
);
287 ball_count
= pSDL_JoystickNumBalls(impl
->sdl_joystick
);
288 if (ball_count
> ARRAY_SIZE(relative_usages
) / 2)
290 FIXME("More than %zu relative axes found, ignoring.\n", ARRAY_SIZE(relative_usages
));
291 ball_count
= ARRAY_SIZE(relative_usages
) / 2;
294 if (impl
->axis_offset
== 0)
296 hat_count
= pSDL_JoystickNumHats(impl
->sdl_joystick
);
297 button_count
= pSDL_JoystickNumButtons(impl
->sdl_joystick
);
305 if (!pSDL_JoystickGetType
) physical_usage
= device_usage
;
306 else switch (pSDL_JoystickGetType(impl
->sdl_joystick
))
308 case SDL_JOYSTICK_TYPE_ARCADE_PAD
:
309 case SDL_JOYSTICK_TYPE_ARCADE_STICK
:
310 case SDL_JOYSTICK_TYPE_DANCE_PAD
:
311 case SDL_JOYSTICK_TYPE_DRUM_KIT
:
312 case SDL_JOYSTICK_TYPE_GUITAR
:
313 case SDL_JOYSTICK_TYPE_UNKNOWN
:
314 physical_usage
.UsagePage
= HID_USAGE_PAGE_GENERIC
;
315 physical_usage
.Usage
= HID_USAGE_GENERIC_JOYSTICK
;
317 case SDL_JOYSTICK_TYPE_GAMECONTROLLER
:
318 physical_usage
.UsagePage
= HID_USAGE_PAGE_GENERIC
;
319 physical_usage
.Usage
= HID_USAGE_GENERIC_GAMEPAD
;
321 case SDL_JOYSTICK_TYPE_WHEEL
:
322 physical_usage
.UsagePage
= HID_USAGE_PAGE_SIMULATION
;
323 physical_usage
.Usage
= HID_USAGE_SIMULATION_AUTOMOBILE_SIMULATION_DEVICE
;
325 case SDL_JOYSTICK_TYPE_FLIGHT_STICK
:
326 case SDL_JOYSTICK_TYPE_THROTTLE
:
327 physical_usage
.UsagePage
= HID_USAGE_PAGE_SIMULATION
;
328 physical_usage
.Usage
= HID_USAGE_SIMULATION_FLIGHT_SIMULATION_DEVICE
;
332 if (!hid_device_begin_report_descriptor(iface
, &device_usage
))
333 return STATUS_NO_MEMORY
;
335 if (!hid_device_begin_input_report(iface
, &physical_usage
))
336 return STATUS_NO_MEMORY
;
338 for (i
= 0; i
< axis_count
; i
++)
340 if (!hid_device_add_axes(iface
, 1, absolute_usages
[i
].UsagePage
,
341 &absolute_usages
[i
].Usage
, FALSE
, -32768, 32767))
342 return STATUS_NO_MEMORY
;
345 for (i
= 0; i
< ball_count
; i
++)
347 if (!hid_device_add_axes(iface
, 2, relative_usages
[2 * i
].UsagePage
,
348 &relative_usages
[2 * i
].Usage
, TRUE
, INT32_MIN
, INT32_MAX
))
349 return STATUS_NO_MEMORY
;
352 if (hat_count
&& !hid_device_add_hatswitch(iface
, hat_count
))
353 return STATUS_NO_MEMORY
;
355 if (button_count
&& !hid_device_add_buttons(iface
, HID_USAGE_PAGE_BUTTON
, 1, button_count
))
356 return STATUS_NO_MEMORY
;
358 if (!hid_device_end_input_report(iface
))
359 return STATUS_NO_MEMORY
;
361 if (!descriptor_add_haptic(impl
))
362 return STATUS_NO_MEMORY
;
364 if (!hid_device_end_report_descriptor(iface
))
365 return STATUS_NO_MEMORY
;
367 /* Initialize axis in the report */
368 for (i
= 0; i
< axis_count
; i
++)
369 hid_device_set_abs_axis(iface
, i
, pSDL_JoystickGetAxis(impl
->sdl_joystick
, i
));
370 for (i
= 0; i
< hat_count
; i
++)
371 set_hat_value(iface
, i
, pSDL_JoystickGetHat(impl
->sdl_joystick
, i
));
373 return STATUS_SUCCESS
;
376 static NTSTATUS
build_controller_report_descriptor(struct unix_device
*iface
)
378 const USAGE_AND_PAGE device_usage
= {.UsagePage
= HID_USAGE_PAGE_GENERIC
, .Usage
= HID_USAGE_GENERIC_GAMEPAD
};
379 static const USAGE left_axis_usages
[] = {HID_USAGE_GENERIC_X
, HID_USAGE_GENERIC_Y
};
380 static const USAGE right_axis_usages
[] = {HID_USAGE_GENERIC_RX
, HID_USAGE_GENERIC_RY
};
381 static const USAGE trigger_axis_usages
[] = {HID_USAGE_GENERIC_Z
, HID_USAGE_GENERIC_RZ
};
382 struct sdl_device
*impl
= impl_from_unix_device(iface
);
383 ULONG i
, button_count
= SDL_CONTROLLER_BUTTON_MAX
- 1;
386 C_ASSERT(SDL_CONTROLLER_AXIS_MAX
== 6);
388 if (!hid_device_begin_report_descriptor(iface
, &device_usage
))
389 return STATUS_NO_MEMORY
;
391 if (!hid_device_begin_input_report(iface
, &device_usage
))
392 return STATUS_NO_MEMORY
;
394 if (!hid_device_add_axes(iface
, 2, HID_USAGE_PAGE_GENERIC
, left_axis_usages
,
395 FALSE
, -32768, 32767))
396 return STATUS_NO_MEMORY
;
398 if (!hid_device_add_axes(iface
, 2, HID_USAGE_PAGE_GENERIC
, right_axis_usages
,
399 FALSE
, -32768, 32767))
400 return STATUS_NO_MEMORY
;
402 if (!hid_device_add_axes(iface
, 2, HID_USAGE_PAGE_GENERIC
, trigger_axis_usages
,
404 return STATUS_NO_MEMORY
;
406 if (!hid_device_add_hatswitch(iface
, 1))
407 return STATUS_NO_MEMORY
;
409 if (!hid_device_add_buttons(iface
, HID_USAGE_PAGE_BUTTON
, 1, button_count
))
410 return STATUS_NO_MEMORY
;
412 if (!hid_device_end_input_report(iface
))
413 return STATUS_NO_MEMORY
;
415 if (!descriptor_add_haptic(impl
))
416 return STATUS_NO_MEMORY
;
418 if (!hid_device_end_report_descriptor(iface
))
419 return STATUS_NO_MEMORY
;
421 /* Initialize axis in the report */
422 for (i
= SDL_CONTROLLER_AXIS_LEFTX
; i
< SDL_CONTROLLER_AXIS_MAX
; i
++)
423 hid_device_set_abs_axis(iface
, i
, pSDL_GameControllerGetAxis(impl
->sdl_controller
, i
));
425 state
= pSDL_GameControllerGetButton(impl
->sdl_controller
, SDL_CONTROLLER_BUTTON_DPAD_UP
);
426 hid_device_move_hatswitch(iface
, 0, 0, state
? -1 : +1);
427 state
= pSDL_GameControllerGetButton(impl
->sdl_controller
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
);
428 hid_device_move_hatswitch(iface
, 0, 0, state
? +1 : -1);
429 state
= pSDL_GameControllerGetButton(impl
->sdl_controller
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
);
430 hid_device_move_hatswitch(iface
, 0, state
? -1 : +1, 0);
431 state
= pSDL_GameControllerGetButton(impl
->sdl_controller
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
);
432 hid_device_move_hatswitch(iface
, 0, state
? +1 : -1, 0);
434 return STATUS_SUCCESS
;
437 static void sdl_device_destroy(struct unix_device
*iface
)
441 static NTSTATUS
sdl_device_start(struct unix_device
*iface
)
443 struct sdl_device
*impl
= impl_from_unix_device(iface
);
446 pthread_mutex_lock(&sdl_cs
);
448 if (impl
->sdl_controller
) status
= build_controller_report_descriptor(iface
);
449 else status
= build_joystick_report_descriptor(iface
);
450 impl
->started
= !status
;
452 pthread_mutex_unlock(&sdl_cs
);
457 static void sdl_device_stop(struct unix_device
*iface
)
459 struct sdl_device
*impl
= impl_from_unix_device(iface
);
461 pSDL_JoystickClose(impl
->sdl_joystick
);
462 if (impl
->sdl_controller
) pSDL_GameControllerClose(impl
->sdl_controller
);
463 if (impl
->sdl_haptic
) pSDL_HapticClose(impl
->sdl_haptic
);
465 pthread_mutex_lock(&sdl_cs
);
466 impl
->started
= FALSE
;
467 list_remove(&impl
->unix_device
.entry
);
468 pthread_mutex_unlock(&sdl_cs
);
471 static NTSTATUS
sdl_device_haptics_start(struct unix_device
*iface
, UINT duration_ms
,
472 USHORT rumble_intensity
, USHORT buzz_intensity
,
473 USHORT left_intensity
, USHORT right_intensity
)
475 struct sdl_device
*impl
= impl_from_unix_device(iface
);
477 TRACE("iface %p, duration_ms %u, rumble_intensity %u, buzz_intensity %u, left_intensity %u, right_intensity %u.\n",
478 iface
, duration_ms
, rumble_intensity
, buzz_intensity
, left_intensity
, right_intensity
);
480 if (!(impl
->effect_support
& EFFECT_SUPPORT_HAPTICS
)) return STATUS_NOT_SUPPORTED
;
482 if (impl
->effect_support
& WINE_SDL_JOYSTICK_RUMBLE
)
484 pSDL_JoystickRumble(impl
->sdl_joystick
, rumble_intensity
, buzz_intensity
, duration_ms
);
485 if (pSDL_JoystickRumbleTriggers
)
486 pSDL_JoystickRumbleTriggers(impl
->sdl_joystick
, left_intensity
, right_intensity
, duration_ms
);
488 else if (impl
->effect_support
& SDL_HAPTIC_LEFTRIGHT
)
490 SDL_HapticEffect effect
;
492 memset(&effect
, 0, sizeof(SDL_HapticEffect
));
493 effect
.type
= SDL_HAPTIC_LEFTRIGHT
;
494 effect
.leftright
.length
= duration_ms
;
495 effect
.leftright
.large_magnitude
= rumble_intensity
;
496 effect
.leftright
.small_magnitude
= buzz_intensity
;
498 if (impl
->haptic_effect_id
>= 0)
499 pSDL_HapticDestroyEffect(impl
->sdl_haptic
, impl
->haptic_effect_id
);
500 impl
->haptic_effect_id
= pSDL_HapticNewEffect(impl
->sdl_haptic
, &effect
);
501 if (impl
->haptic_effect_id
>= 0)
502 pSDL_HapticRunEffect(impl
->sdl_haptic
, impl
->haptic_effect_id
, 1);
504 else if (impl
->effect_support
& WINE_SDL_HAPTIC_RUMBLE
)
506 float magnitude
= (rumble_intensity
+ buzz_intensity
) / 2.0 / 32767.0;
507 pSDL_HapticRumblePlay(impl
->sdl_haptic
, magnitude
, duration_ms
);
510 return STATUS_SUCCESS
;
513 static NTSTATUS
sdl_device_haptics_stop(struct unix_device
*iface
)
515 struct sdl_device
*impl
= impl_from_unix_device(iface
);
517 TRACE("iface %p.\n", iface
);
519 if (impl
->effect_support
& WINE_SDL_JOYSTICK_RUMBLE
)
521 pSDL_JoystickRumble(impl
->sdl_joystick
, 0, 0, 0);
522 if (pSDL_JoystickRumbleTriggers
)
523 pSDL_JoystickRumbleTriggers(impl
->sdl_joystick
, 0, 0, 0);
525 else if (impl
->effect_support
& SDL_HAPTIC_LEFTRIGHT
)
526 pSDL_HapticStopAll(impl
->sdl_haptic
);
527 else if (impl
->effect_support
& WINE_SDL_HAPTIC_RUMBLE
)
528 pSDL_HapticRumbleStop(impl
->sdl_haptic
);
530 return STATUS_SUCCESS
;
533 static NTSTATUS
sdl_device_physical_device_control(struct unix_device
*iface
, USAGE control
)
535 struct sdl_device
*impl
= impl_from_unix_device(iface
);
538 TRACE("iface %p, control %#04x.\n", iface
, control
);
542 case PID_USAGE_DC_ENABLE_ACTUATORS
:
543 pSDL_HapticSetGain(impl
->sdl_haptic
, 100);
544 InterlockedOr(&impl
->effect_flags
, EFFECT_STATE_ACTUATORS_ENABLED
);
545 return STATUS_SUCCESS
;
546 case PID_USAGE_DC_DISABLE_ACTUATORS
:
547 pSDL_HapticSetGain(impl
->sdl_haptic
, 0);
548 InterlockedAnd(&impl
->effect_flags
, ~EFFECT_STATE_ACTUATORS_ENABLED
);
549 return STATUS_SUCCESS
;
550 case PID_USAGE_DC_STOP_ALL_EFFECTS
:
551 pSDL_HapticStopAll(impl
->sdl_haptic
);
552 return STATUS_SUCCESS
;
553 case PID_USAGE_DC_DEVICE_RESET
:
554 pSDL_HapticStopAll(impl
->sdl_haptic
);
555 for (i
= 0; i
< ARRAY_SIZE(impl
->effect_ids
); ++i
)
557 if (impl
->effect_ids
[i
] < 0) continue;
558 pSDL_HapticDestroyEffect(impl
->sdl_haptic
, impl
->effect_ids
[i
]);
559 impl
->effect_ids
[i
] = -1;
561 return STATUS_SUCCESS
;
562 case PID_USAGE_DC_DEVICE_PAUSE
:
563 pSDL_HapticPause(impl
->sdl_haptic
);
564 InterlockedOr(&impl
->effect_flags
, EFFECT_STATE_DEVICE_PAUSED
);
565 return STATUS_SUCCESS
;
566 case PID_USAGE_DC_DEVICE_CONTINUE
:
567 pSDL_HapticUnpause(impl
->sdl_haptic
);
568 InterlockedAnd(&impl
->effect_flags
, ~EFFECT_STATE_DEVICE_PAUSED
);
569 return STATUS_SUCCESS
;
572 return STATUS_NOT_SUPPORTED
;
575 static NTSTATUS
sdl_device_physical_device_set_gain(struct unix_device
*iface
, BYTE percent
)
577 struct sdl_device
*impl
= impl_from_unix_device(iface
);
579 TRACE("iface %p, percent %#x.\n", iface
, percent
);
581 pSDL_HapticSetGain(impl
->sdl_haptic
, percent
);
583 return STATUS_SUCCESS
;
586 static NTSTATUS
sdl_device_physical_effect_control(struct unix_device
*iface
, BYTE index
,
587 USAGE control
, BYTE iterations
)
589 struct sdl_device
*impl
= impl_from_unix_device(iface
);
590 int id
= impl
->effect_ids
[index
];
592 TRACE("iface %p, index %u, control %04x, iterations %u.\n", iface
, index
, control
, iterations
);
594 if (impl
->effect_ids
[index
] < 0) return STATUS_UNSUCCESSFUL
;
598 case PID_USAGE_OP_EFFECT_START_SOLO
:
599 pSDL_HapticStopAll(impl
->sdl_haptic
);
601 case PID_USAGE_OP_EFFECT_START
:
602 pSDL_HapticRunEffect(impl
->sdl_haptic
, id
, (iterations
== 0xff ? SDL_HAPTIC_INFINITY
: iterations
));
604 case PID_USAGE_OP_EFFECT_STOP
:
605 pSDL_HapticStopEffect(impl
->sdl_haptic
, id
);
609 return STATUS_SUCCESS
;
612 static NTSTATUS
set_effect_type_from_usage(SDL_HapticEffect
*effect
, USAGE type
)
616 case PID_USAGE_ET_SINE
:
617 effect
->type
= SDL_HAPTIC_SINE
;
618 return STATUS_SUCCESS
;
619 case PID_USAGE_ET_TRIANGLE
:
620 effect
->type
= SDL_HAPTIC_TRIANGLE
;
621 return STATUS_SUCCESS
;
622 case PID_USAGE_ET_SAWTOOTH_UP
:
623 effect
->type
= SDL_HAPTIC_SAWTOOTHUP
;
624 return STATUS_SUCCESS
;
625 case PID_USAGE_ET_SAWTOOTH_DOWN
:
626 effect
->type
= SDL_HAPTIC_SAWTOOTHDOWN
;
627 return STATUS_SUCCESS
;
628 case PID_USAGE_ET_SPRING
:
629 effect
->type
= SDL_HAPTIC_SPRING
;
630 return STATUS_SUCCESS
;
631 case PID_USAGE_ET_DAMPER
:
632 effect
->type
= SDL_HAPTIC_DAMPER
;
633 return STATUS_SUCCESS
;
634 case PID_USAGE_ET_INERTIA
:
635 effect
->type
= SDL_HAPTIC_INERTIA
;
636 return STATUS_SUCCESS
;
637 case PID_USAGE_ET_FRICTION
:
638 effect
->type
= SDL_HAPTIC_FRICTION
;
639 return STATUS_SUCCESS
;
640 case PID_USAGE_ET_CONSTANT_FORCE
:
641 effect
->type
= SDL_HAPTIC_CONSTANT
;
642 return STATUS_SUCCESS
;
643 case PID_USAGE_ET_RAMP
:
644 effect
->type
= SDL_HAPTIC_RAMP
;
645 return STATUS_SUCCESS
;
646 case PID_USAGE_ET_CUSTOM_FORCE_DATA
:
647 effect
->type
= SDL_HAPTIC_CUSTOM
;
648 return STATUS_SUCCESS
;
650 return STATUS_NOT_SUPPORTED
;
654 static NTSTATUS
sdl_device_physical_effect_update(struct unix_device
*iface
, BYTE index
,
655 struct effect_params
*params
)
657 struct sdl_device
*impl
= impl_from_unix_device(iface
);
658 int id
= impl
->effect_ids
[index
];
659 SDL_HapticEffect effect
= {0};
663 TRACE("iface %p, index %u, params %p.\n", iface
, index
, params
);
665 if (params
->effect_type
== PID_USAGE_UNDEFINED
) return STATUS_SUCCESS
;
666 if ((status
= set_effect_type_from_usage(&effect
, params
->effect_type
))) return status
;
668 /* The first direction we get from PID is in polar coordinate space, so we need to
669 * remove 90° to make it match SDL spherical coordinates. */
670 direction
= (params
->direction
[0] - 9000) % 36000;
672 switch (params
->effect_type
)
674 case PID_USAGE_ET_SINE
:
675 case PID_USAGE_ET_SQUARE
:
676 case PID_USAGE_ET_TRIANGLE
:
677 case PID_USAGE_ET_SAWTOOTH_UP
:
678 case PID_USAGE_ET_SAWTOOTH_DOWN
:
679 effect
.periodic
.length
= (params
->duration
== 0xffff ? SDL_HAPTIC_INFINITY
: params
->duration
);
680 effect
.periodic
.delay
= params
->start_delay
;
681 effect
.periodic
.button
= params
->trigger_button
;
682 effect
.periodic
.interval
= params
->trigger_repeat_interval
;
683 effect
.periodic
.direction
.type
= SDL_HAPTIC_SPHERICAL
;
684 effect
.periodic
.direction
.dir
[0] = direction
;
685 effect
.periodic
.direction
.dir
[1] = params
->direction
[1];
686 effect
.periodic
.period
= params
->periodic
.period
;
687 effect
.periodic
.magnitude
= (params
->periodic
.magnitude
* params
->gain_percent
) / 100;
688 effect
.periodic
.offset
= params
->periodic
.offset
;
689 effect
.periodic
.phase
= params
->periodic
.phase
;
690 effect
.periodic
.attack_length
= params
->envelope
.attack_time
;
691 effect
.periodic
.attack_level
= params
->envelope
.attack_level
;
692 effect
.periodic
.fade_length
= params
->envelope
.fade_time
;
693 effect
.periodic
.fade_level
= params
->envelope
.fade_level
;
696 case PID_USAGE_ET_SPRING
:
697 case PID_USAGE_ET_DAMPER
:
698 case PID_USAGE_ET_INERTIA
:
699 case PID_USAGE_ET_FRICTION
:
700 effect
.condition
.length
= (params
->duration
== 0xffff ? SDL_HAPTIC_INFINITY
: params
->duration
);
701 effect
.condition
.delay
= params
->start_delay
;
702 effect
.condition
.button
= params
->trigger_button
;
703 effect
.condition
.interval
= params
->trigger_repeat_interval
;
704 effect
.condition
.direction
.type
= SDL_HAPTIC_SPHERICAL
;
705 effect
.condition
.direction
.dir
[0] = direction
;
706 effect
.condition
.direction
.dir
[1] = params
->direction
[1];
707 if (params
->condition_count
>= 1)
709 effect
.condition
.right_sat
[0] = params
->condition
[0].positive_saturation
;
710 effect
.condition
.left_sat
[0] = params
->condition
[0].negative_saturation
;
711 effect
.condition
.right_coeff
[0] = params
->condition
[0].positive_coefficient
;
712 effect
.condition
.left_coeff
[0] = params
->condition
[0].negative_coefficient
;
713 effect
.condition
.deadband
[0] = params
->condition
[0].dead_band
;
714 effect
.condition
.center
[0] = params
->condition
[0].center_point_offset
;
716 if (params
->condition_count
>= 2)
718 effect
.condition
.right_sat
[1] = params
->condition
[1].positive_saturation
;
719 effect
.condition
.left_sat
[1] = params
->condition
[1].negative_saturation
;
720 effect
.condition
.right_coeff
[1] = params
->condition
[1].positive_coefficient
;
721 effect
.condition
.left_coeff
[1] = params
->condition
[1].negative_coefficient
;
722 effect
.condition
.deadband
[1] = params
->condition
[1].dead_band
;
723 effect
.condition
.center
[1] = params
->condition
[1].center_point_offset
;
727 case PID_USAGE_ET_CONSTANT_FORCE
:
728 effect
.constant
.length
= (params
->duration
== 0xffff ? SDL_HAPTIC_INFINITY
: params
->duration
);
729 effect
.constant
.delay
= params
->start_delay
;
730 effect
.constant
.button
= params
->trigger_button
;
731 effect
.constant
.interval
= params
->trigger_repeat_interval
;
732 effect
.constant
.direction
.type
= SDL_HAPTIC_SPHERICAL
;
733 effect
.constant
.direction
.dir
[0] = direction
;
734 effect
.constant
.direction
.dir
[1] = params
->direction
[1];
735 effect
.constant
.level
= (params
->constant_force
.magnitude
* params
->gain_percent
) / 100;
736 effect
.constant
.attack_length
= params
->envelope
.attack_time
;
737 effect
.constant
.attack_level
= params
->envelope
.attack_level
;
738 effect
.constant
.fade_length
= params
->envelope
.fade_time
;
739 effect
.constant
.fade_level
= params
->envelope
.fade_level
;
742 /* According to the SDL documentation, ramp effect doesn't
743 * support SDL_HAPTIC_INFINITY. */
744 case PID_USAGE_ET_RAMP
:
745 effect
.ramp
.length
= params
->duration
;
746 effect
.ramp
.delay
= params
->start_delay
;
747 effect
.ramp
.button
= params
->trigger_button
;
748 effect
.ramp
.interval
= params
->trigger_repeat_interval
;
749 effect
.ramp
.direction
.type
= SDL_HAPTIC_SPHERICAL
;
750 effect
.ramp
.direction
.dir
[0] = params
->direction
[0];
751 effect
.ramp
.direction
.dir
[1] = params
->direction
[1];
752 effect
.ramp
.start
= (params
->ramp_force
.ramp_start
* params
->gain_percent
) / 100;
753 effect
.ramp
.end
= (params
->ramp_force
.ramp_end
* params
->gain_percent
) / 100;
754 effect
.ramp
.attack_length
= params
->envelope
.attack_time
;
755 effect
.ramp
.attack_level
= params
->envelope
.attack_level
;
756 effect
.ramp
.fade_length
= params
->envelope
.fade_time
;
757 effect
.ramp
.fade_level
= params
->envelope
.fade_level
;
760 case PID_USAGE_ET_CUSTOM_FORCE_DATA
:
761 FIXME("not implemented!\n");
765 if (id
< 0) impl
->effect_ids
[index
] = pSDL_HapticNewEffect(impl
->sdl_haptic
, &effect
);
766 else pSDL_HapticUpdateEffect(impl
->sdl_haptic
, id
, &effect
);
768 return STATUS_SUCCESS
;
771 static const struct hid_device_vtbl sdl_device_vtbl
=
776 sdl_device_haptics_start
,
777 sdl_device_haptics_stop
,
778 sdl_device_physical_device_control
,
779 sdl_device_physical_device_set_gain
,
780 sdl_device_physical_effect_control
,
781 sdl_device_physical_effect_update
,
784 static void check_device_effects_state(struct sdl_device
*impl
)
786 struct unix_device
*iface
= &impl
->unix_device
;
787 struct hid_effect_state
*effect_state
= &iface
->hid_physical
.effect_state
;
788 ULONG effect_flags
= InterlockedOr(&impl
->effect_flags
, 0);
791 if (!impl
->sdl_haptic
) return;
792 if (!(impl
->effect_support
& EFFECT_SUPPORT_PHYSICAL
)) return;
794 for (i
= 0; i
< ARRAY_SIZE(impl
->effect_ids
); ++i
)
796 if (impl
->effect_ids
[i
] == -1) continue;
797 if (!(impl
->effect_support
& SDL_HAPTIC_STATUS
)) ret
= 1;
798 else ret
= pSDL_HapticGetEffectStatus(impl
->sdl_haptic
, impl
->effect_ids
[i
]);
799 if (impl
->effect_state
[i
] == ret
) continue;
800 impl
->effect_state
[i
] = ret
;
801 hid_device_set_effect_state(iface
, i
, effect_flags
| (ret
== 1 ? EFFECT_STATE_EFFECT_PLAYING
: 0));
802 bus_event_queue_input_report(&event_queue
, iface
, effect_state
->report_buf
, effect_state
->report_len
);
806 static void check_all_devices_effects_state(void)
808 static UINT last_ticks
= 0;
809 UINT ticks
= pSDL_GetTicks();
810 struct sdl_device
*impl
;
812 if (ticks
- last_ticks
< 10) return;
815 pthread_mutex_lock(&sdl_cs
);
816 LIST_FOR_EACH_ENTRY(impl
, &device_list
, struct sdl_device
, unix_device
.entry
)
817 check_device_effects_state(impl
);
818 pthread_mutex_unlock(&sdl_cs
);
821 static BOOL
set_report_from_joystick_event(struct sdl_device
*impl
, SDL_Event
*event
)
823 struct unix_device
*iface
= &impl
->unix_device
;
824 struct hid_device_state
*state
= &iface
->hid_device_state
;
826 if (impl
->sdl_controller
) return TRUE
; /* use controller events instead */
830 case SDL_JOYBUTTONDOWN
:
831 case SDL_JOYBUTTONUP
:
833 SDL_JoyButtonEvent
*ie
= &event
->jbutton
;
835 hid_device_set_button(iface
, ie
->button
, ie
->state
);
836 bus_event_queue_input_report(&event_queue
, iface
, state
->report_buf
, state
->report_len
);
839 case SDL_JOYAXISMOTION
:
841 SDL_JoyAxisEvent
*ie
= &event
->jaxis
;
843 hid_device_set_abs_axis(iface
, ie
->axis
, ie
->value
);
844 bus_event_queue_input_report(&event_queue
, iface
, state
->report_buf
, state
->report_len
);
847 case SDL_JOYBALLMOTION
:
849 SDL_JoyBallEvent
*ie
= &event
->jball
;
851 hid_device_set_rel_axis(iface
, 2 * ie
->ball
, ie
->xrel
);
852 hid_device_set_rel_axis(iface
, 2 * ie
->ball
+ 1, ie
->yrel
);
853 bus_event_queue_input_report(&event_queue
, iface
, state
->report_buf
, state
->report_len
);
856 case SDL_JOYHATMOTION
:
858 SDL_JoyHatEvent
*ie
= &event
->jhat
;
860 set_hat_value(iface
, ie
->hat
, ie
->value
);
861 bus_event_queue_input_report(&event_queue
, iface
, state
->report_buf
, state
->report_len
);
865 ERR("TODO: Process Report (0x%x)\n",event
->type
);
868 check_device_effects_state(impl
);
872 static BOOL
set_report_from_controller_event(struct sdl_device
*impl
, SDL_Event
*event
)
874 struct unix_device
*iface
= &impl
->unix_device
;
875 struct hid_device_state
*state
= &iface
->hid_device_state
;
879 case SDL_CONTROLLERBUTTONDOWN
:
880 case SDL_CONTROLLERBUTTONUP
:
882 SDL_ControllerButtonEvent
*ie
= &event
->cbutton
;
885 switch ((button
= ie
->button
))
887 case SDL_CONTROLLER_BUTTON_DPAD_UP
:
888 hid_device_move_hatswitch(iface
, 0, 0, ie
->state
? -1 : +1);
890 case SDL_CONTROLLER_BUTTON_DPAD_DOWN
:
891 hid_device_move_hatswitch(iface
, 0, 0, ie
->state
? +1 : -1);
893 case SDL_CONTROLLER_BUTTON_DPAD_LEFT
:
894 hid_device_move_hatswitch(iface
, 0, ie
->state
? -1 : +1, 0);
896 case SDL_CONTROLLER_BUTTON_DPAD_RIGHT
:
897 hid_device_move_hatswitch(iface
, 0, ie
->state
? +1 : -1, 0);
899 case SDL_CONTROLLER_BUTTON_LEFTSHOULDER
: button
= 4; break;
900 case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
: button
= 5; break;
901 case SDL_CONTROLLER_BUTTON_BACK
: button
= 6; break;
902 case SDL_CONTROLLER_BUTTON_START
: button
= 7; break;
903 case SDL_CONTROLLER_BUTTON_LEFTSTICK
: button
= 8; break;
904 case SDL_CONTROLLER_BUTTON_RIGHTSTICK
: button
= 9; break;
905 case SDL_CONTROLLER_BUTTON_GUIDE
: button
= 10; break;
908 hid_device_set_button(iface
, button
, ie
->state
);
909 bus_event_queue_input_report(&event_queue
, iface
, state
->report_buf
, state
->report_len
);
912 case SDL_CONTROLLERAXISMOTION
:
914 SDL_ControllerAxisEvent
*ie
= &event
->caxis
;
916 hid_device_set_abs_axis(iface
, ie
->axis
, ie
->value
);
917 bus_event_queue_input_report(&event_queue
, iface
, state
->report_buf
, state
->report_len
);
921 ERR("TODO: Process Report (%x)\n",event
->type
);
924 check_device_effects_state(impl
);
928 static void sdl_add_device(unsigned int index
)
930 struct device_desc desc
=
933 .manufacturer
= {'S','D','L',0},
934 .serialnumber
= {'0','0','0','0',0},
936 struct sdl_device
*impl
;
938 SDL_Joystick
* joystick
;
940 SDL_GameController
*controller
= NULL
;
941 const char *product
, *sdl_serial
;
942 char guid_str
[33], buffer
[ARRAY_SIZE(desc
.product
)];
943 int axis_count
, axis_offset
;
945 if ((joystick
= pSDL_JoystickOpen(index
)) == NULL
)
947 WARN("Unable to open sdl device %i: %s\n", index
, pSDL_GetError());
951 if (options
.map_controllers
&& pSDL_IsGameController(index
))
952 controller
= pSDL_GameControllerOpen(index
);
954 if (controller
) product
= pSDL_GameControllerName(controller
);
955 else product
= pSDL_JoystickName(joystick
);
956 if (!product
) product
= "Joystick";
958 id
= pSDL_JoystickInstanceID(joystick
);
960 if (pSDL_JoystickGetProductVersion
!= NULL
) {
961 desc
.vid
= pSDL_JoystickGetVendor(joystick
);
962 desc
.pid
= pSDL_JoystickGetProduct(joystick
);
963 desc
.version
= pSDL_JoystickGetProductVersion(joystick
);
968 desc
.pid
= pSDL_JoystickInstanceID(joystick
) + 1;
972 if (pSDL_JoystickGetSerial
&& (sdl_serial
= pSDL_JoystickGetSerial(joystick
)))
974 ntdll_umbstowcs(sdl_serial
, strlen(sdl_serial
) + 1, desc
.serialnumber
, ARRAY_SIZE(desc
.serialnumber
));
978 /* Overcooked! All You Can Eat only adds controllers with unique serial numbers
979 * Prefer keeping serial numbers unique over keeping them consistent across runs */
980 pSDL_JoystickGetGUIDString(pSDL_JoystickGetGUID(joystick
), guid_str
, sizeof(guid_str
));
981 snprintf(buffer
, sizeof(buffer
), "%s.%d", guid_str
, index
);
982 TRACE("Making up serial number for %s: %s\n", product
, buffer
);
983 ntdll_umbstowcs(buffer
, strlen(buffer
) + 1, desc
.serialnumber
, ARRAY_SIZE(desc
.serialnumber
));
988 desc
.is_gamepad
= TRUE
;
993 int button_count
= pSDL_JoystickNumButtons(joystick
);
994 axis_count
= pSDL_JoystickNumAxes(joystick
);
995 desc
.is_gamepad
= (axis_count
== 6 && button_count
>= 14);
998 for (axis_offset
= 0; axis_offset
< axis_count
; axis_offset
+= (options
.split_controllers
? 6 : axis_count
))
1000 if (!axis_offset
) strcpy(buffer
, product
);
1001 else snprintf(buffer
, ARRAY_SIZE(buffer
), "%s %d", product
, axis_offset
/ 6);
1002 ntdll_umbstowcs(buffer
, strlen(buffer
) + 1, desc
.product
, ARRAY_SIZE(desc
.product
));
1004 TRACE("%s id %d, axis_offset %u, desc %s.\n", controller
? "controller" : "joystick", id
, axis_offset
, debugstr_device_desc(&desc
));
1006 if (!(impl
= hid_device_create(&sdl_device_vtbl
, sizeof(struct sdl_device
)))) return;
1007 list_add_tail(&device_list
, &impl
->unix_device
.entry
);
1008 impl
->sdl_joystick
= joystick
;
1009 impl
->sdl_controller
= controller
;
1011 impl
->axis_offset
= axis_offset
;
1013 bus_event_queue_device_created(&event_queue
, &impl
->unix_device
, &desc
);
1017 static void process_device_event(SDL_Event
*event
)
1019 struct sdl_device
*impl
;
1022 TRACE("Received action %x\n", event
->type
);
1024 pthread_mutex_lock(&sdl_cs
);
1026 if (event
->type
== SDL_JOYDEVICEADDED
)
1027 sdl_add_device(((SDL_JoyDeviceEvent
*)event
)->which
);
1028 else if (event
->type
== SDL_JOYDEVICEREMOVED
)
1030 id
= ((SDL_JoyDeviceEvent
*)event
)->which
;
1031 impl
= find_device_from_id(id
);
1032 if (impl
) bus_event_queue_device_removed(&event_queue
, &impl
->unix_device
);
1033 else WARN("Failed to find device with id %d\n", id
);
1035 else if (event
->type
== SDL_JOYAXISMOTION
&& options
.split_controllers
)
1037 id
= event
->jaxis
.which
;
1038 impl
= find_device_from_id_and_axis(id
, event
->jaxis
.axis
);
1039 if (!impl
) WARN("Failed to find device with id %d for axis %d\n", id
, event
->jaxis
.axis
);
1040 else if (!impl
->started
) WARN("Device %p with id %d is stopped, ignoring event %#x\n", impl
, id
, event
->type
);
1043 event
->jaxis
.axis
-= impl
->axis_offset
;
1044 set_report_from_joystick_event(impl
, event
);
1047 else if (event
->type
>= SDL_JOYAXISMOTION
&& event
->type
<= SDL_JOYBUTTONUP
)
1049 id
= ((SDL_JoyButtonEvent
*)event
)->which
;
1050 impl
= find_device_from_id(id
);
1051 if (!impl
) WARN("Failed to find device with id %d\n", id
);
1052 else if (!impl
->started
) WARN("Device %p with id %d is stopped, ignoring event %#x\n", impl
, id
, event
->type
);
1053 else set_report_from_joystick_event(impl
, event
);
1055 else if (event
->type
>= SDL_CONTROLLERAXISMOTION
&& event
->type
<= SDL_CONTROLLERBUTTONUP
)
1057 id
= ((SDL_ControllerButtonEvent
*)event
)->which
;
1058 impl
= find_device_from_id(id
);
1059 if (!impl
) WARN("Failed to find device with id %d\n", id
);
1060 else if (!impl
->started
) WARN("Device %p with id %d is stopped, ignoring event %#x\n", impl
, id
, event
->type
);
1061 else set_report_from_controller_event(impl
, event
);
1064 pthread_mutex_unlock(&sdl_cs
);
1067 NTSTATUS
sdl_bus_init(void *args
)
1069 const char *mapping
;
1072 TRACE("args %p\n", args
);
1074 options
= *(struct sdl_bus_options
*)args
;
1076 if (!(sdl_handle
= dlopen(SONAME_LIBSDL2
, RTLD_NOW
)))
1078 WARN("could not load %s\n", SONAME_LIBSDL2
);
1079 return STATUS_UNSUCCESSFUL
;
1081 #define LOAD_FUNCPTR(f) \
1082 if ((p##f = dlsym(sdl_handle, #f)) == NULL) \
1084 WARN("could not find symbol %s\n", #f); \
1087 LOAD_FUNCPTR(SDL_GetError
);
1088 LOAD_FUNCPTR(SDL_Init
);
1089 LOAD_FUNCPTR(SDL_JoystickClose
);
1090 LOAD_FUNCPTR(SDL_JoystickEventState
);
1091 LOAD_FUNCPTR(SDL_JoystickGetGUID
);
1092 LOAD_FUNCPTR(SDL_JoystickGetGUIDString
);
1093 LOAD_FUNCPTR(SDL_JoystickInstanceID
);
1094 LOAD_FUNCPTR(SDL_JoystickName
);
1095 LOAD_FUNCPTR(SDL_JoystickNumAxes
);
1096 LOAD_FUNCPTR(SDL_JoystickOpen
);
1097 LOAD_FUNCPTR(SDL_WaitEventTimeout
);
1098 LOAD_FUNCPTR(SDL_JoystickNumButtons
);
1099 LOAD_FUNCPTR(SDL_JoystickNumBalls
);
1100 LOAD_FUNCPTR(SDL_JoystickNumHats
);
1101 LOAD_FUNCPTR(SDL_JoystickGetAxis
);
1102 LOAD_FUNCPTR(SDL_JoystickGetHat
);
1103 LOAD_FUNCPTR(SDL_IsGameController
);
1104 LOAD_FUNCPTR(SDL_GameControllerClose
);
1105 LOAD_FUNCPTR(SDL_GameControllerGetAxis
);
1106 LOAD_FUNCPTR(SDL_GameControllerGetButton
);
1107 LOAD_FUNCPTR(SDL_GameControllerName
);
1108 LOAD_FUNCPTR(SDL_GameControllerOpen
);
1109 LOAD_FUNCPTR(SDL_GameControllerEventState
);
1110 LOAD_FUNCPTR(SDL_HapticClose
);
1111 LOAD_FUNCPTR(SDL_HapticDestroyEffect
);
1112 LOAD_FUNCPTR(SDL_HapticGetEffectStatus
);
1113 LOAD_FUNCPTR(SDL_HapticNewEffect
);
1114 LOAD_FUNCPTR(SDL_HapticOpenFromJoystick
);
1115 LOAD_FUNCPTR(SDL_HapticPause
);
1116 LOAD_FUNCPTR(SDL_HapticQuery
);
1117 LOAD_FUNCPTR(SDL_HapticRumbleInit
);
1118 LOAD_FUNCPTR(SDL_HapticRumblePlay
);
1119 LOAD_FUNCPTR(SDL_HapticRumbleStop
);
1120 LOAD_FUNCPTR(SDL_HapticRumbleSupported
);
1121 LOAD_FUNCPTR(SDL_HapticRunEffect
);
1122 LOAD_FUNCPTR(SDL_HapticSetGain
);
1123 LOAD_FUNCPTR(SDL_HapticStopAll
);
1124 LOAD_FUNCPTR(SDL_HapticStopEffect
);
1125 LOAD_FUNCPTR(SDL_HapticUnpause
);
1126 LOAD_FUNCPTR(SDL_HapticUpdateEffect
);
1127 LOAD_FUNCPTR(SDL_JoystickIsHaptic
);
1128 LOAD_FUNCPTR(SDL_GameControllerAddMapping
);
1129 LOAD_FUNCPTR(SDL_RegisterEvents
);
1130 LOAD_FUNCPTR(SDL_PushEvent
);
1131 LOAD_FUNCPTR(SDL_GetTicks
);
1133 pSDL_JoystickRumble
= dlsym(sdl_handle
, "SDL_JoystickRumble");
1134 pSDL_JoystickRumbleTriggers
= dlsym(sdl_handle
, "SDL_JoystickRumbleTriggers");
1135 pSDL_JoystickGetProduct
= dlsym(sdl_handle
, "SDL_JoystickGetProduct");
1136 pSDL_JoystickGetProductVersion
= dlsym(sdl_handle
, "SDL_JoystickGetProductVersion");
1137 pSDL_JoystickGetVendor
= dlsym(sdl_handle
, "SDL_JoystickGetVendor");
1138 pSDL_JoystickGetType
= dlsym(sdl_handle
, "SDL_JoystickGetType");
1139 pSDL_JoystickGetSerial
= dlsym(sdl_handle
, "SDL_JoystickGetSerial");
1141 if (pSDL_Init(SDL_INIT_GAMECONTROLLER
| SDL_INIT_HAPTIC
) < 0)
1143 ERR("could not init SDL: %s\n", pSDL_GetError());
1147 if ((quit_event
= pSDL_RegisterEvents(1)) == -1)
1149 ERR("error registering quit event\n");
1153 pSDL_JoystickEventState(SDL_ENABLE
);
1154 pSDL_GameControllerEventState(SDL_ENABLE
);
1156 /* Process mappings */
1157 if (pSDL_GameControllerAddMapping
)
1159 if ((mapping
= getenv("SDL_GAMECONTROLLERCONFIG")))
1161 TRACE("Setting environment mapping %s\n", debugstr_a(mapping
));
1162 if (pSDL_GameControllerAddMapping(mapping
) < 0)
1163 WARN("Failed to add environment mapping %s\n", pSDL_GetError());
1165 else for (i
= 0; i
< options
.mappings_count
; ++i
)
1167 TRACE("Setting registry mapping %s\n", debugstr_a(options
.mappings
[i
]));
1168 if (pSDL_GameControllerAddMapping(options
.mappings
[i
]) < 0)
1169 WARN("Failed to add registry mapping %s\n", pSDL_GetError());
1173 return STATUS_SUCCESS
;
1176 dlclose(sdl_handle
);
1178 return STATUS_UNSUCCESSFUL
;
1181 NTSTATUS
sdl_bus_wait(void *args
)
1183 struct bus_event
*result
= args
;
1186 /* cleanup previously returned event */
1187 bus_event_cleanup(result
);
1191 if (bus_event_queue_pop(&event_queue
, result
)) return STATUS_PENDING
;
1192 if (pSDL_WaitEventTimeout(&event
, 10) != 0) process_device_event(&event
);
1193 else check_all_devices_effects_state();
1194 } while (event
.type
!= quit_event
);
1196 TRACE("SDL main loop exiting\n");
1197 bus_event_queue_destroy(&event_queue
);
1198 dlclose(sdl_handle
);
1200 return STATUS_SUCCESS
;
1203 NTSTATUS
sdl_bus_stop(void *args
)
1207 if (!sdl_handle
) return STATUS_SUCCESS
;
1209 event
.type
= quit_event
;
1210 if (pSDL_PushEvent(&event
) != 1)
1212 ERR("error pushing quit event\n");
1213 return STATUS_UNSUCCESSFUL
;
1216 return STATUS_SUCCESS
;
1221 NTSTATUS
sdl_bus_init(void *args
)
1223 WARN("SDL support not compiled in!\n");
1224 return STATUS_NOT_IMPLEMENTED
;
1227 NTSTATUS
sdl_bus_wait(void *args
)
1229 WARN("SDL support not compiled in!\n");
1230 return STATUS_NOT_IMPLEMENTED
;
1233 NTSTATUS
sdl_bus_stop(void *args
)
1235 WARN("SDL support not compiled in!\n");
1236 return STATUS_NOT_IMPLEMENTED
;
1239 #endif /* SONAME_LIBSDL2 */