1 
2 //          Copyright Michael D. Parker 2018.
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.sdlgamecontroller;
8 
9 import bindbc.sdl.config;
10 
11 import bindbc.sdl.bind.sdljoystick,
12        bindbc.sdl.bind.sdlrwops;
13 import bindbc.sdl.bind.sdlstdinc : SDL_bool;
14 
15 struct SDL_GameController;
16 
17 enum SDL_GameControllerBindType {
18     SDL_CONTROLLER_BINDTYPE_NONE = 0,
19     SDL_CONTROLLER_BINDTYPE_BUTTON,
20     SDL_CONTROLLER_BINDTYPE_AXIS,
21     SDL_CONTROLLER_BINDTYPE_HAT,
22 }
23 mixin(expandEnum!SDL_GameControllerBindType);
24 
25 struct SDL_GameControllerButtonBind {
26     SDL_GameControllerBindType bindType;
27     union value {
28         int button;
29         int axis;
30         struct hat {
31             int hat;
32             int hat_mask;
33         }
34     }
35     alias button = value.button;
36     alias axis = value.axis;
37     alias hat = value.hat;
38 }
39 
40 enum SDL_GameControllerAxis {
41     SDL_CONTROLLER_AXIS_INVALID = -1,
42     SDL_CONTROLLER_AXIS_LEFTX,
43     SDL_CONTROLLER_AXIS_LEFTY,
44     SDL_CONTROLLER_AXIS_RIGHTX,
45     SDL_CONTROLLER_AXIS_RIGHTY,
46     SDL_CONTROLLER_AXIS_TRIGGERLEFT,
47     SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
48     SDL_CONTROLLER_AXIS_MAX
49 }
50 mixin(expandEnum!SDL_GameControllerAxis);
51 
52 enum SDL_GameControllerButton {
53     SDL_CONTROLLER_BUTTON_INVALID = -1,
54     SDL_CONTROLLER_BUTTON_A,
55     SDL_CONTROLLER_BUTTON_B,
56     SDL_CONTROLLER_BUTTON_X,
57     SDL_CONTROLLER_BUTTON_Y,
58     SDL_CONTROLLER_BUTTON_BACK,
59     SDL_CONTROLLER_BUTTON_GUIDE,
60     SDL_CONTROLLER_BUTTON_START,
61     SDL_CONTROLLER_BUTTON_LEFTSTICK,
62     SDL_CONTROLLER_BUTTON_RIGHTSTICK,
63     SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
64     SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
65     SDL_CONTROLLER_BUTTON_DPAD_UP,
66     SDL_CONTROLLER_BUTTON_DPAD_DOWN,
67     SDL_CONTROLLER_BUTTON_DPAD_LEFT,
68     SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
69     SDL_CONTROLLER_BUTTON_MAX
70 }
71 mixin(expandEnum!SDL_GameControllerButton);
72 
73 static if(sdlSupport >= SDLSupport.sdl202) {
74     @nogc nothrow
75     int SDL_GameControllerAddMappingsFromFile(const(char)* file) {
76         pragma(inline, true);
77         return SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file,"rb"),1);
78     }
79 }
80 
81 version(BindSDL_Static) {
82     extern(C) @nogc nothrow {
83         int SDL_GameControllerAddMapping(const(char)*);
84         char* SDL_GameControllerMappingForGUID(SDL_JoystickGUID);
85         char* SDL_GameControllerMapping(SDL_GameController*);
86         SDL_bool SDL_IsGameController(int);
87         const(char)* SDL_GameControllerNameForIndex(int);
88         SDL_GameController* SDL_GameControllerOpen(int);
89         const(char)* SDL_GameControllerName(SDL_GameController*);
90         SDL_bool SDL_GameControllerGetAttached(SDL_GameController*);
91         SDL_Joystick* SDL_GameControllerGetJoystick(SDL_GameController*);
92         int SDL_GameControllerEventState(int);
93         void SDL_GameControllerUpdate();
94         SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const(char)*);
95         const(char)* SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis);
96         SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController*,SDL_GameControllerAxis);
97         short SDL_GameControllerGetAxis(SDL_GameController*,SDL_GameControllerAxis);
98         SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const(char*));
99         const(char)* SDL_GameControllerGetStringForButton(SDL_GameControllerButton);
100         SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController*,SDL_GameControllerButton);
101         ubyte SDL_GameControllerGetButton(SDL_GameController*,SDL_GameControllerButton);
102         void SDL_GameControllerClose(SDL_GameController*);
103 
104         static if(sdlSupport >= SDLSupport.sdl202) {
105             int SDL_GameControllerAddMappingsFromRW(SDL_RWops*,int);
106         }
107         static if(sdlSupport >= SDLSupport.sdl204) {
108             SDL_GameController* SDL_GameControllerFromInstanceID(SDL_JoystickID);
109         }
110         static if(sdlSupport >= SDLSupport.sdl206) {
111             ushort SDL_GameControllerGetProduct(SDL_GameController*);
112             ushort SDL_GameControllerGetProductVersion(SDL_GameController*);
113             ushort SDL_GameControllerGetVendor(SDL_GameController*);
114             char* SDL_GameControllerMappingForIndex(int);
115             int SDL_GameControllerNumMappings();
116         }
117         static if(sdlSupport >= SDLSupport.sdl209) {
118             char* SDL_GameControllerMappingForDeviceIndex(int);
119             int SDL_GameControllerRumble(SDL_GameController*,ushort,ushort,uint);
120         }
121         static if(sdlSupport >= SDLSupport.sdl2010) {
122             int SDL_GameControllerGetPlayerIndex(SDL_GameController*);
123         }
124     }
125 }
126 else {
127     extern(C) @nogc nothrow {
128         alias pSDL_GameControllerAddMapping = int function(const(char)*);
129         alias pSDL_GameControllerMappingForGUID = char* function(SDL_JoystickGUID);
130         alias pSDL_GameControllerMapping = char* function(SDL_GameController*);
131         alias pSDL_IsGameController = SDL_bool function(int);
132         alias pSDL_GameControllerNameForIndex = const(char)* function(int);
133         alias pSDL_GameControllerOpen = SDL_GameController* function(int);
134         alias pSDL_GameControllerName = const(char)* function(SDL_GameController*);
135         alias pSDL_GameControllerGetAttached = SDL_bool function(SDL_GameController*);
136         alias pSDL_GameControllerGetJoystick = SDL_Joystick* function(SDL_GameController*);
137         alias pSDL_GameControllerEventState = int function(int);
138         alias pSDL_GameControllerUpdate = void function();
139         alias pSDL_GameControllerGetAxisFromString = SDL_GameControllerAxis function(const(char)*);
140         alias pSDL_GameControllerGetStringForAxis = const(char)* function(SDL_GameControllerAxis);
141         alias pSDL_GameControllerGetBindForAxis = SDL_GameControllerButtonBind function(SDL_GameController*,SDL_GameControllerAxis);
142         alias pSDL_GameControllerGetAxis = short function(SDL_GameController*,SDL_GameControllerAxis);
143         alias pSDL_GameControllerGetButtonFromString = SDL_GameControllerButton function(const(char*));
144         alias pSDL_GameControllerGetStringForButton = const(char)* function(SDL_GameControllerButton);
145         alias pSDL_GameControllerGetBindForButton = SDL_GameControllerButtonBind function(SDL_GameController*,SDL_GameControllerButton);
146         alias pSDL_GameControllerGetButton = ubyte function(SDL_GameController*,SDL_GameControllerButton);
147         alias pSDL_GameControllerClose = void function(SDL_GameController*);
148     }
149     __gshared {
150         pSDL_GameControllerAddMapping SDL_GameControllerAddMapping;
151         pSDL_GameControllerMappingForGUID SDL_GameControllerMappingForGUID;
152         pSDL_GameControllerMapping SDL_GameControllerMapping;
153         pSDL_IsGameController SDL_IsGameController;
154         pSDL_GameControllerNameForIndex SDL_GameControllerNameForIndex;
155         pSDL_GameControllerOpen SDL_GameControllerOpen;
156         pSDL_GameControllerName SDL_GameControllerName;
157         pSDL_GameControllerGetAttached SDL_GameControllerGetAttached;
158         pSDL_GameControllerGetJoystick SDL_GameControllerGetJoystick;
159         pSDL_GameControllerEventState SDL_GameControllerEventState;
160         pSDL_GameControllerUpdate SDL_GameControllerUpdate;
161         pSDL_GameControllerGetAxisFromString SDL_GameControllerGetAxisFromString;
162         pSDL_GameControllerGetStringForAxis SDL_GameControllerGetStringForAxis;
163         pSDL_GameControllerGetBindForAxis SDL_GameControllerGetBindForAxis;
164         pSDL_GameControllerGetAxis SDL_GameControllerGetAxis;
165         pSDL_GameControllerGetButtonFromString SDL_GameControllerGetButtonFromString;
166         pSDL_GameControllerGetStringForButton SDL_GameControllerGetStringForButton;
167         pSDL_GameControllerGetBindForButton SDL_GameControllerGetBindForButton;
168         pSDL_GameControllerGetButton SDL_GameControllerGetButton;
169         pSDL_GameControllerClose SDL_GameControllerClose;
170     }
171     static if(sdlSupport >= SDLSupport.sdl202) {
172         extern(C) @nogc nothrow {
173             alias pSDL_GameControllerAddMappingsFromRW = int function(SDL_RWops*,int);
174         }
175 
176         __gshared {
177             pSDL_GameControllerAddMappingsFromRW SDL_GameControllerAddMappingsFromRW;
178         }
179     }
180     static if(sdlSupport >= SDLSupport.sdl204) {
181         extern(C) @nogc nothrow {
182             alias pSDL_GameControllerFromInstanceID = SDL_GameController* function(SDL_JoystickID);
183         }
184 
185         __gshared {
186             pSDL_GameControllerFromInstanceID SDL_GameControllerFromInstanceID;
187         }
188     }
189     static if(sdlSupport >= SDLSupport.sdl206) {
190         extern(C) @nogc nothrow {
191             alias pSDL_GameControllerGetProduct = ushort function(SDL_GameController*);
192             alias pSDL_GameControllerGetProductVersion = ushort function(SDL_GameController*);
193             alias pSDL_GameControllerGetVendor = ushort function(SDL_GameController*);
194             alias pSDL_GameControllerMappingForIndex = char* function(int);
195             alias pSDL_GameControllerNumMappings = int function();
196         }
197 
198         __gshared {
199             pSDL_GameControllerGetProduct SDL_GameControllerGetProduct;
200             pSDL_GameControllerGetProductVersion SDL_GameControllerGetProductVersion;
201             pSDL_GameControllerGetVendor SDL_GameControllerGetVendor;
202             pSDL_GameControllerMappingForIndex SDL_GameControllerMappingForIndex;
203             pSDL_GameControllerNumMappings SDL_GameControllerNumMappings;
204         }
205     }
206     static if(sdlSupport >= SDLSupport.sdl209) {
207         extern(C) @nogc nothrow {
208             alias pSDL_GameControllerMappingForDeviceIndex = char* function(int);
209             alias pSDL_GameControllerRumble = int function(SDL_GameController*,ushort,ushort,uint);
210         }
211 
212         __gshared {
213             pSDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex;
214             pSDL_GameControllerRumble SDL_GameControllerRumble;
215         }
216     }
217     static if(sdlSupport >= SDLSupport.sdl2010) {
218         extern(C) @nogc nothrow {
219             alias pSDL_GameControllerGetPlayerIndex = int function(SDL_GameController*);
220         }
221         __gshared {
222             pSDL_GameControllerGetPlayerIndex SDL_GameControllerGetPlayerIndex;
223         }
224     }
225 }