Skip to content

Commit 9dd5a91

Browse files
authored
Merge pull request #23 from ColleagueRiley/master
review internal api
2 parents 5dce552 + d9753eb commit 9dd5a91

14 files changed

Lines changed: 722 additions & 326 deletions

File tree

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ ifeq ($(PLATFORM),Windows)
3939
EXT = dll
4040
LDFLAGS = -shared
4141
# change later
42-
BACKEND ?= WINMM
42+
BACKEND ?= windows
4343

44-
ifeq ($(BACKEND),WINMM)
44+
ifeq ($(BACKEND),windows)
45+
LIBS =
46+
CFLAGS += -I./src/windows/
47+
SOURCES += $(wildcard src/windows/*.c)
48+
else ifeq ($(BACKEND),WINMM)
4549
LIBS = -lwinmm
4650
CFLAGS += -I./src/winmm/
4751
SOURCES += $(wildcard src/winmm/*.c)
4852
endif
53+
54+
CFLAGS += -D BUILD_LIBTYPE_SHARED
4955
endif
5056

5157
TARGET = $(OUTDIR)/libminigamepad.$(EXT) \
@@ -71,7 +77,7 @@ $(OUTDIR)/%.o: $(SOURCES) | $(OUTDIR)
7177
$(CC) $(CFLAGS) -c $< -o $@
7278

7379
$(EXAMPLES): %: %.c
74-
$(CC) $(CFLAGS) -I. $< $(LIBS) -L./build -lminigamepad -o $(OUTDIR)/$@
80+
$(CC) -static $(CFLAGS) -I. $< $(LIBS) -L./build -lminigamepad -o $(OUTDIR)/$@
7581

7682
$(LIBEVDEV_OBJECTS): %.o: %.c
7783
$(CC) -fPIC -c $< -o $@

examples/gamepadlist.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33

44
int main(void) {
55
mg_gamepads gamepads = {0};
6-
mg_gamepads_fetch(&gamepads);
6+
mg_gamepads_init(&gamepads);
77

8-
size_t joystick_num = gamepads.num;
9-
10-
for (size_t i = 0; i < joystick_num; i++) {
11-
mg_gamepad *gamepad = mg_gamepads_at(&gamepads, i);
12-
printf("%s\n", mg_gamepad_get_name(gamepad));
8+
for (mg_gamepad* cur = mg_gamepad_get_head(&gamepads); cur; cur = mg_gamepad_iterate(cur)) {
9+
printf("%s\n", cur->name);
1310
}
1411

1512
mg_gamepads_free(&gamepads);

examples/gamepadquery.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ void clear(void) {
1515
int main(void) {
1616
mg_gamepads gamepads = {0};
1717

18-
size_t idx = 0;
19-
2018
clear();
2119

2220
mg_gamepads_init(&gamepads);
23-
mg_gamepad *gamepad = mg_gamepads_at(&gamepads, idx);
21+
mg_gamepad* gamepad = gamepads.head;
2422

2523
for (;;) {
2624
while(mg_gamepads_update(&gamepads, NULL));
@@ -30,10 +28,13 @@ int main(void) {
3028
if (!mg_gamepad_is_connected(gamepad)) {
3129
clear();
3230
mg_gamepads_fetch(&gamepads);
33-
gamepad = mg_gamepads_at(&gamepads, idx);
31+
32+
gamepad = gamepad->next;
33+
if (gamepad == NULL)
34+
break;
3435
}
3536

36-
printf(" Gamepad: %-25s\n", mg_gamepad_get_name(gamepad));
37+
printf(" Gamepad: %-25s\n", gamepad->name);
3738
size_t gamepad_button_num = gamepad->button_num;
3839
for (size_t i = 0; i < gamepad_button_num; i++) {
3940
mg_gamepad_btn btn = gamepad->buttons[i].key;

examples/gamepadrumble.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
#include <time.h>
66
#include <unistd.h>
77

8-
#ifndef __WIN32
8+
#ifndef _WIN32
99
#include <pthread.h>
1010
#else
1111
#include <windows.h>
12+
#include <processthreadsapi.h>
13+
#include <handleapi.h>
14+
#include <synchapi.h>
1215
#endif
1316

1417
mg_gamepads gamepads = {0};
1518
mg_gamepad *gamepad;
1619

1720
double axis_value;
1821

19-
#ifndef __WIN32
22+
#ifndef _WIN32
2023
pthread_t thread_id;
2124
__attribute__((__noreturn__)) void *rumble_thread(void *arg);
2225
#else
@@ -33,7 +36,7 @@ int main(void) {
3336

3437
gamepad = mg_gamepads_at(&gamepads, 0);
3538

36-
#ifndef __WIN32
39+
#ifndef _WIN32
3740
pthread_create(&thread_id, NULL, &rumble_thread, NULL);
3841
#else
3942
CreateThread(NULL, // default security attributes
@@ -51,16 +54,16 @@ int main(void) {
5154

5255
mg_gamepads_free(&gamepads);
5356

54-
#ifndef __WIN32
57+
#ifndef _WIN32
5558
pthread_cancel(thread_id);
5659
#else
57-
CloseHandle(thread_id);
60+
CloseHandle(thread_handle);
5861
#endif
5962

6063
return 0;
6164
}
6265

63-
#ifndef __WIN32
66+
#ifndef _WIN32
6467
__attribute__((__noreturn__)) void *rumble_thread(void *arg) {
6568
#else
6669
__declspec(noreturn) DWORD WINAPI rumble_thread(LPVOID arg) {
@@ -71,7 +74,7 @@ __declspec(noreturn) DWORD WINAPI rumble_thread(LPVOID arg) {
7174
mg_gamepad_rumble(gamepad, (uint16_t)axis_value / 2, (uint16_t)axis_value,
7275
1000);
7376
printf("\rVibration: %0.2f", axis_value);
74-
#ifndef __WIN32
77+
#ifndef _WIN32
7578
fflush(stdout);
7679
sleep(1);
7780
#else

include/minigamepad.h

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
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.
95118
struct 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

Comments
 (0)