1 /+
2 +            Copyright 2022 – 2023 Aya Partridge
3 +          Copyright 2018 - 2022 Michael D. Parker
4 + Distributed under the Boost Software License, Version 1.0.
5 +     (See accompanying file LICENSE_1_0.txt or copy at
6 +           http://www.boost.org/LICENSE_1_0.txt)
7 +/
8 module sdl.joystick;
9 
10 import bindbc.sdl.config;
11 import bindbc.sdl.codegen;
12 
13 import sdl.stdinc: SDL_bool;
14 import sdl.guid: SDL_GUID;
15 
16 struct SDL_Joystick;
17 
18 alias SDL_JoystickGUID = SDL_GUID;
19 
20 alias SDL_JoystickID = int;
21 
22 enum: ubyte{
23 	SDL_HAT_CENTERED   = 0x00,
24 	SDL_HAT_UP         = 0x01,
25 	SDL_HAT_RIGHT      = 0x02,
26 	SDL_HAT_DOWN       = 0x04,
27 	SDL_HAT_LEFT       = 0x08,
28 	SDL_HAT_RIGHTUP    = (SDL_HAT_RIGHT | SDL_HAT_UP),
29 	SDL_HAT_RIGHTDOWN  = (SDL_HAT_RIGHT | SDL_HAT_DOWN),
30 	SDL_HAT_LEFTUP     = (SDL_HAT_LEFT  | SDL_HAT_UP),
31 	SDL_HAT_LEFTDOWN   = (SDL_HAT_LEFT  | SDL_HAT_DOWN),
32 }
33 
34 static if(sdlSupport >= SDLSupport.v2_0_4){
35 	alias SDL_JoystickPowerLevel = int;
36 	enum: SDL_JoystickPowerLevel{
37 		SDL_JOYSTICK_POWER_UNKNOWN = -1,
38 		SDL_JOYSTICK_POWER_EMPTY,
39 		SDL_JOYSTICK_POWER_LOW,
40 		SDL_JOYSTICK_POWER_MEDIUM,
41 		SDL_JOYSTICK_POWER_FULL,
42 		SDL_JOYSTICK_POWER_WIRED,
43 		SDL_JOYSTICK_POWER_MAX
44 	}
45 }
46 
47 static if(sdlSupport >= SDLSupport.v2_0_14){
48 	enum SDL_IPHONE_MAX_GFORCE = 5.0;
49 }
50 
51 static if(sdlSupport >= SDLSupport.v2_24){
52 	struct SDL_VirtualJoystickDesc{
53 		ushort version_; //NOTE: the original variable's name is `version`
54 		ushort type;
55 		ushort naxes;
56 		ushort nbuttons;
57 		ushort nhats;
58 		ushort vendor_id;
59 		ushort product_id;
60 		ushort padding;
61 		uint button_mask;
62 		uint axis_mask;
63 		const(char)* name;
64 
65 		void* userdata;
66 		void function(void* userdata) Update;
67 		void function(void* userdata, int player_index) SetPlayerIndex;
68 		int function(void* userdata, ushort low_frequency_rumble, ushort high_frequency_rumble) Rumble;
69 		int function(void* userdata, ushort left_rumble, ushort right_rumble) RumbleTriggers;
70 		int function(void* userdata, ubyte red, ubyte green, ubyte blue) SetLED;
71 		int function(void* userdata, const(void)* data, int size) SendEffect;
72 	}
73 	
74 	enum ushort SDL_VIRTUAL_JOYSTICK_DESC_VERSION = 1;
75 }
76 
77 static if(sdlSupport >= SDLSupport.v2_0_6){
78 	alias SDL_JoystickType = ushort;
79 	enum: SDL_JoystickType{
80 		SDL_JOYSTICK_TYPE_UNKNOWN,
81 		SDL_JOYSTICK_TYPE_GAMECONTROLLER,
82 		SDL_JOYSTICK_TYPE_WHEEL,
83 		SDL_JOYSTICK_TYPE_ARCADE_STICK,
84 		SDL_JOYSTICK_TYPE_FLIGHT_STICK,
85 		SDL_JOYSTICK_TYPE_DANCE_PAD,
86 		SDL_JOYSTICK_TYPE_GUITAR,
87 		SDL_JOYSTICK_TYPE_DRUM_KIT,
88 		SDL_JOYSTICK_TYPE_ARCADE_PAD,
89 		SDL_JOYSTICK_TYPE_THROTTLE,
90 	}
91 	
92 	enum{
93 		SDL_JOYSTICK_AXIS_MAX = 32767,
94 		SDL_JOYSTICK_AXIS_MIN = -32768,
95 	}
96 }
97 
98 mixin(joinFnBinds((){
99 	string[][] ret;
100 	ret ~= makeFnBinds([
101 		[q{int}, q{SDL_NumJoysticks}, q{}],
102 		[q{const(char)*}, q{SDL_JoystickNameForIndex}, q{int device_index}],
103 		[q{SDL_JoystickGUID}, q{SDL_JoystickGetDeviceGUID}, q{int device_index}],
104 		[q{SDL_Joystick*}, q{SDL_JoystickOpen}, q{int device_index}],
105 		[q{const(char)*}, q{SDL_JoystickName}, q{SDL_Joystick* joystick}],
106 		[q{SDL_JoystickGUID}, q{SDL_JoystickGetGUID}, q{SDL_Joystick* joystick}],
107 		[q{void}, q{SDL_JoystickGetGUIDString}, q{SDL_JoystickGUID guid, char* pszGUID, int cbGUID}],
108 		[q{SDL_JoystickGUID}, q{SDL_JoystickGetGUIDFromString}, q{const(char)* }],
109 		[q{SDL_bool}, q{SDL_JoystickGetAttached}, q{SDL_Joystick* joystick}],
110 		[q{SDL_JoystickID}, q{SDL_JoystickInstanceID}, q{SDL_Joystick* joystick}],
111 		[q{int}, q{SDL_JoystickNumAxes}, q{SDL_Joystick* joystick}],
112 		[q{int}, q{SDL_JoystickNumBalls}, q{SDL_Joystick* joystick}],
113 		[q{int}, q{SDL_JoystickNumHats}, q{SDL_Joystick* joystick}],
114 		[q{int}, q{SDL_JoystickNumButtons}, q{SDL_Joystick* joystick}],
115 		[q{void}, q{SDL_JoystickUpdate}, q{}],
116 		[q{int}, q{SDL_JoystickEventState}, q{int state}],
117 		[q{short}, q{SDL_JoystickGetAxis}, q{SDL_Joystick* joystick, int axis}],
118 		[q{ubyte}, q{SDL_JoystickGetHat}, q{SDL_Joystick* joystick, int hat}],
119 		[q{int}, q{SDL_JoystickGetBall}, q{SDL_Joystick* joystick, int ball, int* dx, int* dy}],
120 		[q{ubyte}, q{SDL_JoystickGetButton}, q{SDL_Joystick* joystick, int button}],
121 		[q{void}, q{SDL_JoystickClose}, q{SDL_Joystick* joystick}],
122 	]);
123 	static if(sdlSupport >= SDLSupport.v2_0_4){
124 		ret ~= makeFnBinds([
125 			[q{SDL_JoystickPowerLevel}, q{SDL_JoystickCurrentPowerLevel}, q{SDL_Joystick* joystick}],
126 			[q{SDL_Joystick*}, q{SDL_JoystickFromInstanceID}, q{SDL_JoystickID instance_id}],
127 		]);
128 	}
129 	static if(sdlSupport >= SDLSupport.v2_0_6){
130 		ret ~= makeFnBinds([
131 			[q{SDL_bool}, q{SDL_JoystickGetAxisInitialState}, q{SDL_Joystick* joystick, int axis, short* state}],
132 			[q{ushort}, q{SDL_JoystickGetDeviceProduct}, q{int device_index}],
133 			[q{ushort}, q{SDL_JoystickGetDeviceProductVersion}, q{int device_index}],
134 			[q{SDL_JoystickType}, q{SDL_JoystickGetDeviceType}, q{int device_index}],
135 			[q{SDL_JoystickType}, q{SDL_JoystickGetDeviceInstanceID}, q{int device_index}],
136 			[q{ushort}, q{SDL_JoystickGetDeviceVendor}, q{int device_index}],
137 			[q{ushort}, q{SDL_JoystickGetProduct}, q{SDL_Joystick* joystick}],
138 			[q{ushort}, q{SDL_JoystickGetProductVersion}, q{SDL_Joystick* joystick}],
139 			[q{SDL_JoystickType}, q{SDL_JoystickGetType}, q{SDL_Joystick* joystick}],
140 			[q{ushort}, q{SDL_JoystickGetVendor}, q{SDL_Joystick* joystick}],
141 		]);
142 	}
143 	static if(sdlSupport >= SDLSupport.v2_0_7){
144 		ret ~= makeFnBinds([
145 			[q{void}, q{SDL_LockJoysticks}, q{}],
146 			[q{void}, q{SDL_UnlockJoysticks}, q{}],
147 		]);
148 	}
149 	static if(sdlSupport >= SDLSupport.v2_0_9){
150 		ret ~= makeFnBinds([
151 			[q{int}, q{SDL_JoystickRumble}, q{SDL_Joystick* joystick, ushort low_frequency_rumble, ushort high_frequency_rumble, uint duration_ms}],
152 		]);
153 	}
154 	static if(sdlSupport >= SDLSupport.v2_0_10){
155 		ret ~= makeFnBinds([
156 			[q{int}, q{SDL_JoystickGetDevicePlayerIndex}, q{int device_index}],
157 			[q{int}, q{SDL_JoystickGetPlayerIndex}, q{SDL_Joystick* joystick}],
158 		]);
159 	}
160 	static if(sdlSupport >= SDLSupport.v2_0_12){
161 		ret ~= makeFnBinds([
162 			[q{SDL_Joystick*}, q{SDL_JoystickFromPlayerIndex}, q{int}],
163 			[q{void}, q{SDL_JoystickSetPlayerIndex}, q{SDL_Joystick* joystick,int}],
164 		]);
165 	}
166 	static if(sdlSupport >= SDLSupport.v2_0_14){
167 		ret ~= makeFnBinds([
168 			[q{int}, q{SDL_JoystickAttachVirtual}, q{SDL_JoystickType type, int naxes, int nbuttons, int nhats}],
169 			[q{int}, q{SDL_JoystickDetachVirtual}, q{int device_index}],
170 			[q{SDL_bool}, q{SDL_JoystickIsVirtual}, q{int device_index}],
171 			[q{int}, q{SDL_JoystickSetVirtualAxis}, q{SDL_Joystick* joystick, int axis, short value}],
172 			[q{int}, q{SDL_JoystickSetVirtualButton}, q{SDL_Joystick* joystick, int button, ubyte value}],
173 			[q{int}, q{SDL_JoystickSetVirtualHat}, q{SDL_Joystick* joystick, int hat, ubyte value}],
174 			[q{const(char)*}, q{SDL_JoystickGetSerial}, q{SDL_Joystick* joystick}],
175 			[q{int}, q{SDL_JoystickRumbleTriggers}, q{SDL_Joystick* joystick, ushort left_rumble, ushort right_rumble, uint duration_ms}],
176 			[q{SDL_bool}, q{SDL_JoystickHasLED}, q{SDL_Joystick* joystick}],
177 			[q{int}, q{SDL_JoystickSetLED}, q{SDL_Joystick* joystick, ubyte red, ubyte green, ubyte blue}],
178 		]);
179 	}
180 	static if(sdlSupport >= SDLSupport.v2_0_16){
181 		ret ~= makeFnBinds([
182 			[q{int}, q{SDL_JoystickSendEffect}, q{SDL_Joystick* joystick, const(void)*data, int size}],
183 		]);
184 	}
185 	static if(sdlSupport >= SDLSupport.v2_0_18){
186 		ret ~= makeFnBinds([
187 			[q{SDL_bool}, q{SDL_JoystickHasRumble}, q{SDL_Joystick* joystick}],
188 			[q{SDL_bool}, q{SDL_JoystickHasRumbleTriggers}, q{SDL_Joystick* joystick}],
189 		]);
190 	}
191 	static if(sdlSupport >= SDLSupport.v2_24){
192 		ret ~= makeFnBinds([
193 			[q{const(char)*}, q{SDL_JoystickPathForIndex}, q{int device_index}],
194 			[q{int}, q{SDL_JoystickAttachVirtualEx}, q{const(SDL_VirtualJoystickDesc)* desc}],
195 			[q{const(char)*}, q{SDL_JoystickPath}, q{SDL_Joystick* joystick}],
196 			[q{ushort}, q{SDL_JoystickGetFirmwareVersion}, q{SDL_Joystick* joystick}],
197 		]);
198 	}
199 	static if(sdlSupport >= SDLSupport.v2_26){
200 		ret ~= makeFnBinds([
201 			[q{void}, q{SDL_GetJoystickGUIDInfo}, q{SDL_JoystickGUID guid, ushort* vendor, ushort* product, ushort* version_, ushort* crc16}],
202 		]);
203 	}
204 	return ret;
205 }()));