1 
2 //          Copyright 2018 - 2021 Michael D. Parker
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module bindbc.sdl.bind.sdljoystick;
8 
9 import bindbc.sdl.config;
10 import bindbc.sdl.bind.sdlstdinc : SDL_bool;
11 
12 struct SDL_Joystick;
13 
14 struct SDL_JoystickGUID {
15     ubyte[16] data;
16 }
17 
18 alias SDL_JoystickID = int;
19 
20 enum : ubyte {
21     SDL_HAT_CENTERED = 0x00,
22     SDL_HAT_UP = 0x01,
23     SDL_HAT_RIGHT = 0x02,
24     SDL_HAT_DOWN = 0x04,
25     SDL_HAT_LEFT = 0x08,
26     SDL_HAT_RIGHTUP = (SDL_HAT_RIGHT|SDL_HAT_UP),
27     SDL_HAT_RIGHTDOWN = (SDL_HAT_RIGHT|SDL_HAT_DOWN),
28     SDL_HAT_LEFTUP = (SDL_HAT_LEFT|SDL_HAT_UP),
29     SDL_HAT_LEFTDOWN = (SDL_HAT_LEFT|SDL_HAT_DOWN),
30 }
31 
32 static if(sdlSupport >= SDLSupport.sdl204) {
33     enum SDL_JoystickPowerLevel {
34         SDL_JOYSTICK_POWER_UNKNOWN = -1,
35         SDL_JOYSTICK_POWER_EMPTY,
36         SDL_JOYSTICK_POWER_LOW,
37         SDL_JOYSTICK_POWER_MEDIUM,
38         SDL_JOYSTICK_POWER_FULL,
39         SDL_JOYSTICK_POWER_WIRED,
40         SDL_JOYSTICK_POWER_MAX
41     }
42     mixin(expandEnum!SDL_JoystickPowerLevel);
43 }
44 
45 static if(sdlSupport >= SDLSupport.sdl2014) enum SDL_IPHONE_MAX_GFORCE = 5.0;
46 
47 static if(sdlSupport >= SDLSupport.sdl206) {
48     enum SDL_JoystickType {
49         SDL_JOYSTICK_TYPE_UNKNOWN,
50         SDL_JOYSTICK_TYPE_GAMECONTROLLER,
51         SDL_JOYSTICK_TYPE_WHEEL,
52         SDL_JOYSTICK_TYPE_ARCADE_STICK,
53         SDL_JOYSTICK_TYPE_FLIGHT_STICK,
54         SDL_JOYSTICK_TYPE_DANCE_PAD,
55         SDL_JOYSTICK_TYPE_GUITAR,
56         SDL_JOYSTICK_TYPE_DRUM_KIT,
57         SDL_JOYSTICK_TYPE_ARCADE_PAD,
58         SDL_JOYSTICK_TYPE_THROTTLE,
59     }
60     mixin(expandEnum!SDL_JoystickType);
61 
62     enum {
63         SDL_JOYSTICK_AXIS_MAX = 32767,
64         SDL_JOYSTICK_AXIS_MIN = -32768,
65     }
66 }
67 
68 static if(staticBinding) {
69     extern(C) @nogc nothrow {
70         int SDL_NumJoysticks();
71         const(char)* SDL_JoystickNameForIndex(int device_index);
72         SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index);
73         SDL_Joystick* SDL_JoystickOpen(int device_index);
74         const(char)* SDL_JoystickName(SDL_Joystick* joystick);
75         SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick* joystick);
76         void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char* pszGUID, int cbGUID);
77         SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const(char)* );
78         SDL_bool SDL_JoystickGetAttached(SDL_Joystick* joystick);
79         SDL_JoystickID SDL_JoystickInstanceID(SDL_Joystick* joystick);
80         int SDL_JoystickNumAxes(SDL_Joystick* joystick);
81         int SDL_JoystickNumBalls(SDL_Joystick* joystick);
82         int SDL_JoystickNumHats(SDL_Joystick* joystick);
83         int SDL_JoystickNumButtons(SDL_Joystick* joystick);
84         void SDL_JoystickUpdate();
85         int SDL_JoystickEventState(int state);
86         short SDL_JoystickGetAxis(SDL_Joystick* joystick, int axis);
87         ubyte SDL_JoystickGetHat(SDL_Joystick* joystick, int hat);
88         int SDL_JoystickGetBall(SDL_Joystick* joystick, int ball, int* dx, int* dy);
89         ubyte SDL_JoystickGetButton(SDL_Joystick* joystick, int button);
90         void SDL_JoystickClose(SDL_Joystick* joystick);
91 
92         static if(sdlSupport >= SDLSupport.sdl204) {
93             SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick* joystick);
94             SDL_Joystick* SDL_JoystickFromInstanceID(SDL_JoystickID instance_id);
95         }
96         static if(sdlSupport >= SDLSupport.sdl206) {
97             SDL_bool SDL_JoystickGetAxisInitialState(SDL_Joystick* joystick, int axis, short* state);
98             ushort SDL_JoystickGetDeviceProduct(int device_index);
99             ushort SDL_JoystickGetDeviceProductVersion(int device_index);
100             SDL_JoystickType SDL_JoystickGetDeviceType(int device_index);
101             SDL_JoystickType SDL_JoystickGetDeviceInstanceID(int device_index);
102             ushort SDL_JoystickGetDeviceVendor(int device_index);
103             ushort SDL_JoystickGetProduct(SDL_Joystick* joystick);
104             ushort SDL_JoystickGetProductVersion(SDL_Joystick* joystick);
105             SDL_JoystickType SDL_JoystickGetType(SDL_Joystick* joystick);
106             ushort SDL_JoystickGetVendor(SDL_Joystick* joystick);
107         }
108         static if(sdlSupport >= SDLSupport.sdl207) {
109             void SDL_LockJoysticks();
110             void SDL_UnlockJoysticks();
111         }
112         static if(sdlSupport >= SDLSupport.sdl209) {
113             int SDL_JoystickRumble(SDL_Joystick* joystick, ushort low_frequency_rumble, ushort high_frequency_rumble, uint duration_ms);
114         }
115         static if(sdlSupport >= SDLSupport.sdl2010) {
116             int SDL_JoystickGetDevicePlayerIndex(int device_index);
117             int SDL_JoystickGetPlayerIndex(SDL_Joystick* joystick);
118         }
119         static if(sdlSupport >= SDLSupport.sdl2012) {
120             SDL_Joystick* SDL_JoystickFromPlayerIndex(int);
121             void SDL_JoystickSetPlayerIndex(SDL_Joystick* joystick,int);
122         }
123         static if(sdlSupport >= SDLSupport.sdl2012) {
124             int SDL_JoystickAttachVirtual(SDL_JoystickType type, int naxes, int nbuttons, int nhats);
125             int SDL_JoystickDetachVirtual(int device_index);
126             SDL_bool SDL_JoystickIsVirtual(int device_index);
127             int SDL_JoystickSetVirtualAxis(SDL_Joystick* joystick, int axis, short value);
128             int SDL_JoystickSetVirtualButton(SDL_Joystick* joystick, int button, ubyte value);
129             int SDL_JoystickSetVirtualHat(SDL_Joystick* joystick, int hat, ubyte value);
130             const(char)* SDL_JoystickGetSerial(SDL_Joystick* joystick);
131             int SDL_JoystickRumbleTriggers(SDL_Joystick* joystick, ushort left_rumble, ushort right_rumble, uint duration_ms);
132             SDL_bool SDL_JoystickHasLED(SDL_Joystick* joystick);
133             int SDL_JoystickSetLED(SDL_Joystick* joystick, ubyte red, ubyte green, ubyte blue);
134         }
135     }
136 }
137 else {
138     extern(C) @nogc nothrow {
139         alias pSDL_NumJoysticks = int function();
140         alias pSDL_JoystickNameForIndex = const(char)* function(int device_index);
141         alias pSDL_JoystickGetDeviceGUID = SDL_JoystickGUID function(int device_index);
142         alias pSDL_JoystickOpen = SDL_Joystick* function(int device_index);
143         alias pSDL_JoystickName = const(char)* function(SDL_Joystick* joystick);
144         alias pSDL_JoystickGetGUID = SDL_JoystickGUID function(SDL_Joystick* joystick);
145         alias pSDL_JoystickGetGUIDString = void function(SDL_JoystickGUID guid, char* pszGUID, int cbGUID);
146         alias pSDL_JoystickGetGUIDFromString = SDL_JoystickGUID function(const(char)* pchGUID);
147         alias pSDL_JoystickGetAttached = SDL_bool function(SDL_Joystick* joystick);
148         alias pSDL_JoystickInstanceID = SDL_JoystickID function(SDL_Joystick* joystick);
149         alias pSDL_JoystickNumAxes = int function(SDL_Joystick* joystick);
150         alias pSDL_JoystickNumBalls = int function(SDL_Joystick* joystick);
151         alias pSDL_JoystickNumHats = int function(SDL_Joystick* joystick);
152         alias pSDL_JoystickNumButtons = int function(SDL_Joystick* joystick);
153         alias pSDL_JoystickUpdate = void function();
154         alias pSDL_JoystickEventState = int function(int state);
155         alias pSDL_JoystickGetAxis = short function(SDL_Joystick* joystick, int axis);
156         alias pSDL_JoystickGetHat = ubyte function(SDL_Joystick* joystick, int hat);
157         alias pSDL_JoystickGetBall = int function(SDL_Joystick* joystick, int ball, int* dx, int* dy);
158         alias pSDL_JoystickGetButton = ubyte function(SDL_Joystick* joystick, int button);
159         alias pSDL_JoystickClose = void function(SDL_Joystick* joystick);
160     }
161     __gshared {
162         pSDL_NumJoysticks SDL_NumJoysticks;
163         pSDL_JoystickNameForIndex SDL_JoystickNameForIndex;
164         pSDL_JoystickGetDeviceGUID SDL_JoystickGetDeviceGUID;
165         pSDL_JoystickOpen SDL_JoystickOpen;
166         pSDL_JoystickName SDL_JoystickName;
167         pSDL_JoystickGetGUID SDL_JoystickGetGUID;
168         pSDL_JoystickGetGUIDString SDL_JoystickGetGUIDString;
169         pSDL_JoystickGetGUIDFromString SDL_JoystickGetGUIDFromString;
170         pSDL_JoystickGetAttached SDL_JoystickGetAttached;
171         pSDL_JoystickInstanceID SDL_JoystickInstanceID;
172         pSDL_JoystickNumAxes SDL_JoystickNumAxes;
173         pSDL_JoystickNumBalls SDL_JoystickNumBalls;
174         pSDL_JoystickNumHats SDL_JoystickNumHats;
175         pSDL_JoystickNumButtons SDL_JoystickNumButtons;
176         pSDL_JoystickUpdate SDL_JoystickUpdate;
177         pSDL_JoystickEventState SDL_JoystickEventState;
178         pSDL_JoystickGetAxis SDL_JoystickGetAxis;
179         pSDL_JoystickGetHat SDL_JoystickGetHat;
180         pSDL_JoystickGetBall SDL_JoystickGetBall;
181         pSDL_JoystickGetButton SDL_JoystickGetButton;
182         pSDL_JoystickClose SDL_JoystickClose;
183     }
184     static if(sdlSupport >= SDLSupport.sdl204) {
185         extern(C) @nogc nothrow {
186             alias pSDL_JoystickCurrentPowerLevel = SDL_JoystickPowerLevel function(SDL_Joystick* joystick);
187             alias pSDL_JoystickFromInstanceID = SDL_Joystick* function(SDL_JoystickID instance_id);
188         }
189         __gshared {
190             pSDL_JoystickCurrentPowerLevel SDL_JoystickCurrentPowerLevel;
191             pSDL_JoystickFromInstanceID SDL_JoystickFromInstanceID;
192         }
193     }
194     static if(sdlSupport >= SDLSupport.sdl206) {
195         extern(C) @nogc nothrow {
196             alias pSDL_JoystickGetAxisInitialState = SDL_bool function(SDL_Joystick* joystick, int axis, short* state);
197             alias pSDL_JoystickGetDeviceProduct = ushort function(int device_index);
198             alias pSDL_JoystickGetDeviceProductVersion = ushort function(int device_index);
199             alias pSDL_JoystickGetDeviceType = SDL_JoystickType function(int device_index);
200             alias pSDL_JoystickGetDeviceInstanceID = SDL_JoystickType function(int device_index);
201             alias pSDL_JoystickGetDeviceVendor = ushort function(int device_index);
202             alias pSDL_JoystickGetProduct = ushort function(SDL_Joystick* joystick);
203             alias pSDL_JoystickGetProductVersion = ushort function(SDL_Joystick* joystick);
204             alias pSDL_JoystickGetType = SDL_JoystickType function(SDL_Joystick* joystick);
205             alias pSDL_JoystickGetVendor = ushort function(SDL_Joystick* joystick);
206         }
207         __gshared {
208             pSDL_JoystickGetAxisInitialState SDL_JoystickGetAxisInitialState;
209             pSDL_JoystickGetDeviceProduct SDL_JoystickGetDeviceProduct;
210             pSDL_JoystickGetDeviceProductVersion SDL_JoystickGetDeviceProductVersion;
211             pSDL_JoystickGetDeviceType SDL_JoystickGetDeviceType;
212             pSDL_JoystickGetDeviceInstanceID SDL_JoystickGetDeviceInstanceID;
213             pSDL_JoystickGetDeviceVendor SDL_JoystickGetDeviceVendor;
214             pSDL_JoystickGetProduct SDL_JoystickGetProduct;
215             pSDL_JoystickGetProductVersion SDL_JoystickGetProductVersion;
216             pSDL_JoystickGetType SDL_JoystickGetType;
217             pSDL_JoystickGetVendor SDL_JoystickGetVendor;
218         }
219     }
220     static if(sdlSupport >= SDLSupport.sdl207) {
221         extern(C) @nogc nothrow {
222             alias pSDL_LockJoysticks = void function();
223             alias pSDL_UnlockJoysticks = void function();
224         }
225         __gshared {
226             pSDL_LockJoysticks SDL_LockJoysticks;
227             pSDL_UnlockJoysticks SDL_UnlockJoysticks;
228         }
229     }
230     static if(sdlSupport >= SDLSupport.sdl209) {
231         extern(C) @nogc nothrow {
232             alias pSDL_JoystickRumble = int function(SDL_Joystick* joystick, ushort low_frequency_rumble, ushort high_frequency_rumble, uint duration_ms);
233         }
234         __gshared {
235             pSDL_JoystickRumble SDL_JoystickRumble;
236         }
237     }
238     static if(sdlSupport >= SDLSupport.sdl2010) {
239         extern(C) @nogc nothrow {
240             alias pSDL_JoystickGetDevicePlayerIndex = int function(int device_index);
241             alias pSDL_JoystickGetPlayerIndex = int function(SDL_Joystick* joystick);
242         }
243         __gshared {
244             pSDL_JoystickGetDevicePlayerIndex SDL_JoystickGetDevicePlayerIndex;
245             pSDL_JoystickGetPlayerIndex SDL_JoystickGetPlayerIndex;
246         }
247     }
248     static if(sdlSupport >= SDLSupport.sdl2012) {
249         extern(C) @nogc nothrow {
250             alias pSDL_JoystickFromPlayerIndex = SDL_Joystick* function(int);
251             alias pSDL_JoystickSetPlayerIndex = void function(SDL_Joystick* joystick,int);
252         }
253         __gshared {
254             pSDL_JoystickFromPlayerIndex SDL_JoystickFromPlayerIndex;
255             pSDL_JoystickSetPlayerIndex SDL_JoystickSetPlayerIndex;
256         }
257     }
258     static if(sdlSupport >= SDLSupport.sdl2014) {
259         extern(C) @nogc nothrow {
260             alias pSDL_JoystickAttachVirtual = int function(SDL_JoystickType type, int naxes, int nbuttons, int nhats);
261             alias pSDL_JoystickDetachVirtual = int function(int device_index);
262             alias pSDL_JoystickIsVirtual = SDL_bool function(int device_index);
263             alias pSDL_JoystickSetVirtualAxis = int function(SDL_Joystick* joystick, int axis, short value);
264             alias pSDL_JoystickSetVirtualButton = int function(SDL_Joystick* joystick, int button, ubyte value);
265             alias pSDL_JoystickSetVirtualHat = int function(SDL_Joystick* joystick, int hat, ubyte value);
266             alias pSDL_JoystickGetSerial = const(char)* function(SDL_Joystick* joystick);
267             alias pSDL_JoystickRumbleTriggers = int function(SDL_Joystick* joystick, ushort left_rumble, ushort right_rumble, uint duration_ms);
268             alias pSDL_JoystickHasLED = SDL_bool function(SDL_Joystick* joystick);
269             alias pSDL_JoystickSetLED = int function(SDL_Joystick* joystick, ubyte red, ubyte green, ubyte blue);
270         }
271         __gshared {
272             pSDL_JoystickAttachVirtual SDL_JoystickAttachVirtual;
273             pSDL_JoystickDetachVirtual SDL_JoystickDetachVirtual;
274             pSDL_JoystickIsVirtual SDL_JoystickIsVirtual;
275             pSDL_JoystickSetVirtualAxis SDL_JoystickSetVirtualAxis;
276             pSDL_JoystickSetVirtualButton SDL_JoystickSetVirtualButton;
277             pSDL_JoystickSetVirtualHat SDL_JoystickSetVirtualHat;
278             pSDL_JoystickGetSerial SDL_JoystickGetSerial;
279             pSDL_JoystickRumbleTriggers SDL_JoystickRumbleTriggers;
280             pSDL_JoystickHasLED SDL_JoystickHasLED;
281             pSDL_JoystickSetLED SDL_JoystickSetLED;
282         }
283     }
284 }