File:
src/joystick/hidapi/SDL_hidapi_luna.c
Checker name:
clang-diagnostic-unused-parameter
Review status:
Unreviewed
1
/*
2
Simple DirectMedia Layer
3
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
8
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
12
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
20
*/
21
22
23
24
25
26
27
28
29
30
31
/* Define this if you want to log all packets from the controller */
32
/*#define DEBUG_LUNA_PROTOCOL*/
33
34
/* Sending rumble on macOS blocks for a long time and eventually fails */
35
36
37
38
39
enum
40
{
41
SDL_GAMEPAD_BUTTON_LUNA_MICROPHONE = 11,
42
SDL_GAMEPAD_NUM_LUNA_BUTTONS,
43
};
44
45
typedef struct
46
{
47
Uint8 last_state[USB_PACKET_LENGTH];
48
} SDL_DriverLuna_Context;
49
50
static void HIDAPI_DriverLuna_RegisterHints(SDL_HintCallback callback, void *userdata)
51
{
52
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_LUNA, callback, userdata);
53
}
54
55
static void HIDAPI_DriverLuna_UnregisterHints(SDL_HintCallback callback, void *userdata)
56
{
57
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_LUNA, callback, userdata);
58
}
59
60
static SDL_bool HIDAPI_DriverLuna_IsEnabled(void)
61
{
62
return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_LUNA, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
63
}
64
65
static SDL_bool HIDAPI_DriverLuna_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
66
{
67
return SDL_IsJoystickAmazonLunaController(vendor_id, product_id);
68
}
69
70
static SDL_bool HIDAPI_DriverLuna_InitDevice(SDL_HIDAPI_Device *device)
71
{
72
SDL_DriverLuna_Context *ctx;
73
74
ctx = (SDL_DriverLuna_Context *)SDL_calloc(1, sizeof(*ctx));
75
if (!ctx) {
76
return SDL_FALSE;
77
}
78
device->context = ctx;
79
80
HIDAPI_SetDeviceName(device, "Amazon Luna Controller");
81
82
return HIDAPI_JoystickConnected(device, NULL);
83
}
84
85
static int HIDAPI_DriverLuna_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
86
{
87
return -1;
88
}
89
90
static void HIDAPI_DriverLuna_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
91
{
92
}
93
94
static SDL_bool HIDAPI_DriverLuna_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
95
{
96
SDL_DriverLuna_Context *ctx = (SDL_DriverLuna_Context *)device->context;
97
98
SDL_AssertJoysticksLocked();
99
100
SDL_zeroa(ctx->last_state);
101
102
/* Initialize the joystick capabilities */
103
joystick->nbuttons = SDL_GAMEPAD_NUM_LUNA_BUTTONS;
104
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
105
joystick->nhats = 1;
106
107
return SDL_TRUE;
108
}
109
110
static int HIDAPI_DriverLuna_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
111
{
112
113
if (device->product_id == BLUETOOTH_PRODUCT_LUNA_CONTROLLER) {
114
/* Same packet as on Xbox One controllers connected via Bluetooth */
115
Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEB };
116
117
/* Magnitude is 1..100 so scale the 16-bit input here */
118
rumble_packet[4] = (Uint8)(low_frequency_rumble / 655);
119
rumble_packet[5] = (Uint8)(high_frequency_rumble / 655);