11#ifndef __minigamepad_H
22#define __minigamepad_H
33
4+ #if defined(_WIN32 ) && !defined(MG_API )
5+ #if defined(__TINYC__ )
6+ #define __declspec (x ) __attribute__((x))
7+ #endif
8+ #if defined(BUILD_LIBTYPE_SHARED )
9+ #define MG_API __declspec(dllexport) // We are building the library as a Win32 shared library (.dll)
10+ #elif defined(USE_LIBTYPE_SHARED )
11+ #define MG_API __declspec(dllimport) // We are using the library as a Win32 shared library (.dll)
12+ #endif
13+ #elif !defined(MG_API )
14+ #if defined(BUILD_LIBTYPE_SHARED )
15+ #define MG_API __attribute__((visibility("default"))) // We are building as a Unix shared library (.so/.dylib)
16+ #endif
17+ #endif
18+
19+ #ifndef MG_API
20+ #define MG_API
21+ #endif
22+
423#include <stddef.h>
524
625#ifdef __cplusplus
@@ -33,6 +52,10 @@ typedef enum {
3352 MG_GAMEPAD_BUTTON_RIGHT_STICK ,
3453 MG_GAMEPAD_BUTTON_LEFT_SHOULDER ,
3554 MG_GAMEPAD_BUTTON_RIGHT_SHOULDER ,
55+ MG_GAMEPAD_BUTTON_DPAD_UP ,
56+ MG_GAMEPAD_BUTTON_DPAD_DOWN ,
57+ MG_GAMEPAD_BUTTON_DPAD_LEFT ,
58+ MG_GAMEPAD_BUTTON_DPAD_RIGHT ,
3659 MG_GAMEPAD_BUTTON_MISC1 , /**< Additional button (e.g. Xbox Series X share
3760 button, PS5 microphone button, Nintendo Switch
3861 Pro capture button, Amazon Luna microphone
@@ -88,54 +111,67 @@ typedef enum {
88111 MG_GAMEPAD_AXIS_MAX ,
89112} mg_gamepad_axis ;
90113
91- #define MAX_BUTTONS MG_GAMEPAD_BUTTON_MAX
92- #define MAX_AXISES MG_GAMEPAD_AXIS_MAX
114+ #define MG_MAX_BUTTONS MG_GAMEPAD_BUTTON_MAX
115+ #define MG_MAX_AXISES MG_GAMEPAD_AXIS_MAX
93116
94117/// Internal context of the gamepad, i.e. implementation details.
95118struct mg_gamepad_context_t ;
96119
97- typedef struct mg_gamepad_t {
120+ typedef struct mg_buttons {
121+ mg_gamepad_btn key ;
122+ int16_t value ;
123+ } mg_buttons ;
124+
125+ typedef struct mg_axises {
126+ mg_gamepad_axis key ;
127+ int16_t value ;
128+ int16_t deadzone ;
129+ } mg_axises ;
130+
131+ struct mg_mapping ;
132+
133+ typedef struct mg_gamepad {
98134 // Internal context for platform dependent items.
99135 struct mg_gamepad_context_t * ctx ;
100136 // Map of buttons that the controller has
101- struct {
102- mg_gamepad_btn key ;
103- int16_t value ;
104- } buttons [MAX_BUTTONS ];
137+ mg_buttons buttons [MG_MAX_BUTTONS ];
105138 // The number of buttons on the controller.
106139 size_t button_num ;
107140 // Map of axises that the controller has, + their deadzones
108141 // By default, the deadzones are 5000 for any axis that isn't the d-pad; you
109142 // are strongly encouraged to make this customizable in any program you make
110143 // with this.
111- struct {
112- mg_gamepad_axis key ;
113- int16_t value ;
114- int16_t deadzone ;
115- } axises [MAX_AXISES ];
144+
145+ mg_axises axises [MG_MAX_AXISES ];
116146 // The number of axises on the controller.
117147 size_t axis_num ;
118148
119- struct mg_gamepad_t * prev ;
120- struct mg_gamepad_t * next ;
149+ bool connected ;
150+ char name [128 ];
151+ char guid [33 ];
152+
153+ struct mg_mapping * mapping ;
154+
155+ struct mg_gamepad * prev ;
156+ struct mg_gamepad * next ;
121157} mg_gamepad ;
122158
123159/// A list of gamepads recognized by the system.
124- typedef struct mg_gamepads_t {
125- struct mg_gamepad_t __list [16 ];
126- struct mg_gamepad_t * cur ;
127- struct mg_gamepad_t * head ;
160+ typedef struct mg_gamepads {
161+ mg_gamepad __list [16 ];
162+ mg_gamepad * cur ;
163+ mg_gamepad * head ;
128164
129165 struct {
130- struct mg_gamepad_t * cur ;
131- struct mg_gamepad_t * head ;
166+ mg_gamepad * cur ;
167+ mg_gamepad * head ;
132168 } freed ;
133169
134170 size_t num ;
135171} mg_gamepads ;
136172
137173
138- typedef enum mg_gamepad_event_type_t {
174+ typedef enum mg_gamepad_event_type {
139175 MG_GAMEPAD_NONE = 0 ,
140176 MG_GAMEPAD_CONNECT ,
141177 MG_GAMEPAD_DISCONNECT ,
@@ -144,7 +180,7 @@ typedef enum mg_gamepad_event_type_t {
144180 MG_GAMEPAD_AXIS_MOVE
145181} mg_gamepad_event_type ;
146182
147- typedef struct mg_gamepad_event_t {
183+ typedef struct mg_gamepad_event {
148184 mg_gamepad_event_type type ;
149185 mg_gamepad_btn btn ;
150186 mg_gamepad_axis axis ;
@@ -153,38 +189,47 @@ typedef struct mg_gamepad_event_t {
153189} mg_gamepad_event ;
154190
155191
192+ // get the head gamepad in the linked list of gamepads
193+ MG_API mg_gamepad * mg_gamepad_get_head (mg_gamepads * gamepads );
194+
195+ // iterates to the next node in the linked list of gamepads, returns `cur`, allowing you to check if cur == NULL
196+ MG_API mg_gamepad * mg_gamepad_iterate (mg_gamepad * cur );
197+
156198/// Init the gamepads internal structure.
157- void mg_gamepads_init (mg_gamepads * gamepad );
199+ MG_API void mg_gamepads_init (mg_gamepads * gamepad );
158200
159201/// Fetch and update all gamepads and return the first event
160- bool mg_gamepads_update (mg_gamepads * gamepads , mg_gamepad_event * ev );
202+ MG_API bool mg_gamepads_update (mg_gamepads * gamepads , mg_gamepad_event * ev );
203+
204+ /// Add another mapping to the gamepads mappings
205+ MG_API int mg_update_gamepad_mappings (mg_gamepads * gamepads , const char * string );
161206
162207/// Update the gamepad's internal structure.
163208/// This needs to be called before any gamepad buttons/axises are checked if you
164209/// want the correct values.
165- bool mg_gamepad_update (mg_gamepad * gamepad , mg_gamepad_event * ev );
210+ MG_API bool mg_gamepad_update (mg_gamepad * gamepad , mg_gamepad_event * ev );
166211
167212/// Get the gamepads currently connected to the system. Returns true if a new gamepad was found
168- bool mg_gamepads_fetch (mg_gamepads * );
213+ MG_API bool mg_gamepads_fetch (mg_gamepads * );
169214/// Get the number of gamepads attached to the system.
170- size_t mg_gamepads_num (mg_gamepads * gamepads );
215+ MG_API size_t mg_gamepads_num (mg_gamepads * gamepads );
171216/// Get the game pad at the given index.
172- mg_gamepad * mg_gamepads_at (mg_gamepads * mj , size_t idx );
217+ MG_API mg_gamepad * mg_gamepads_at (mg_gamepads * mj , size_t idx );
173218/// Free gamepads.
174- void mg_gamepads_free (mg_gamepads * gamepads );
219+ MG_API void mg_gamepads_free (mg_gamepads * gamepads );
175220
176221/// Get the gamepad's name.
177- const char * mg_gamepad_get_name (mg_gamepad * gamepad );
222+ MG_API const char * mg_gamepad_get_name (mg_gamepad * gamepad );
178223/// Check if this gamepad is connected.
179- bool mg_gamepad_is_connected (mg_gamepad * gamepad );
224+ MG_API bool mg_gamepad_is_connected (mg_gamepad * gamepad );
180225
181226/// Get the name of a gamepad button.
182- const char * mg_gamepad_btn_get_name (mg_gamepad_btn );
227+ MG_API const char * mg_gamepad_btn_get_name (mg_gamepad_btn );
183228/// Get the name of a gamepad axis.
184- const char * mg_gamepad_axis_get_name (mg_gamepad_axis axis );
229+ MG_API const char * mg_gamepad_axis_get_name (mg_gamepad_axis axis );
185230
186231/// Rumble the controller
187- void mg_gamepad_rumble (mg_gamepad * gamepad , uint16_t strong_vibration , uint16_t weak_vibration , uint16_t milliseconds );
232+ MG_API void mg_gamepad_rumble (mg_gamepad * gamepad , uint16_t strong_vibration , uint16_t weak_vibration , uint16_t milliseconds );
188233
189234#ifdef __cplusplus
190235}
0 commit comments