@@ -309,34 +309,29 @@ typedef MG_ENUM(u8, mg_event_type) {
309309};
310310
311311#ifdef MG_LINUX
312- #include <linux/input.h>
313- #include <linux/input-event-codes.h>
312+ struct mg_input_absinfo {
313+ i32 value;
314+ i32 minimum;
315+ i32 maximum;
316+ i32 fuzz;
317+ i32 flat;
318+ i32 resolution;
319+ };
314320
315321typedef struct mg_gamepad_src {
316322 int fd;
317- u8 keyMap[KEY_CNT - BTN_MISC ];
318- u8 absMap[ABS_CNT ];
319- struct input_absinfo absInfo[ABS_CNT ];
323+ u8 keyMap[512 ];
324+ u8 absMap[64 ];
325+ struct mg_input_absinfo absInfo[64 ];
320326 char full_path[256];
321327} mg_gamepad_src;
322328#elif defined(MG_WINDOWS)
323- #define WIN32_LEAN_AND_MEAN
324- #define OEMRESOURCE
325- #include <windows.h>
326-
327- #include <xinput.h>
328- #include <dinput.h>
329-
330329typedef struct mg_gamepad_src {
331- IDirectInputDevice8* device;
332- DIDEVCAPS caps;
333- DWORD xinput_index;
330+ void* device;
331+ u32 xinput_index;
334332} mg_gamepad_src;
335333#elif defined(MG_MACOS)
336- #include <IOKit/hid/IOHIDManager.h>
337-
338334typedef struct mg_gamepad_src {
339- IOHIDDeviceRef device;
340335 void* events;
341336} mg_gamepad_src;
342337#elif defined(MG_WASM)
@@ -376,17 +371,15 @@ typedef struct mg_gamepad {
376371} mg_gamepad;
377372
378373#ifdef MG_LINUX
374+ #include <linux/input.h>
375+ #include <linux/input-event-codes.h>
376+
379377typedef struct mg_gamepads_src {
380378 int inotify, watch;
381379} mg_gamepads_src;
382380#elif defined(MG_WINDOWS)
383- typedef HRESULT (WINAPI * PFN_DirectInput8Create)(HINSTANCE,DWORD,REFIID,LPVOID*,LPUNKNOWN);
384- typedef void (*mg_proc)(void); /* function pointer equivalent of void* */
385-
386381typedef struct mg_gamepads_src {
387- HINSTANCE dinput_dll;
388- PFN_DirectInput8Create DInput8Create;
389- IDirectInput8* dinput;
382+ void* dinput;
390383} mg_gamepads_src;
391384#elif defined(MG_MACOS)
392385typedef struct mg_gamepads_src {
@@ -801,7 +794,7 @@ mg_gamepad* mg_linux_setup_gamepad(mg_gamepads* gamepads, const char* full_path)
801794 if (!isBitSet(i, absBits))
802795 continue;
803796
804- if (ioctl(fd, EVIOCGABS(i), &gamepad->src.absInfo[i]) < 0)
797+ if (ioctl(fd, EVIOCGABS(i), (struct input_absinfo*) &gamepad->src.absInfo[i]) < 0)
805798 continue;
806799
807800 gamepad->src.absMap[i] = (u8)axisCount;
@@ -1069,7 +1062,7 @@ mg_bool mg_gamepad_update_platform(mg_gamepad* gamepad, mg_events* events) {
10691062 float deadzone, event_val;
10701063
10711064 mg_axis axis = mg_get_gamepad_axis(gamepad, gamepad->src.absMap[ev.code]);
1072- const struct input_absinfo info = gamepad->src.absInfo[ev.code];
1065+ const struct mg_input_absinfo info = gamepad->src.absInfo[ev.code];
10731066 float normalized = (float)ev.value;
10741067 const float range = (float)(info.maximum - info.minimum);
10751068
@@ -1243,18 +1236,24 @@ mg_axis mg_get_gamepad_axis_platform(u32 axis) {
12431236 */
12441237
12451238#if defined(MG_WINDOWS)
1239+ #include <xinput.h>
1240+ #include <dinput.h>
1241+
1242+ typedef void (*mg_proc)(void); /* function pointer equivalent of void* */
12461243
12471244mg_gamepad* mg_xinput_list[XUSER_MAX_COUNT];
1248- typedef DWORD (WINAPI * PFN_XInputGetState)(DWORD,XINPUT_STATE*);
1249- typedef DWORD (WINAPI * PFN_XInputGetCapabilities)(DWORD,DWORD,XINPUT_CAPABILITIES*);
1250- typedef DWORD (WINAPI * PFN_XInputGetKeystroke)(DWORD, DWORD, PXINPUT_KEYSTROKE);
1245+ typedef DWORD (* PFN_XInputGetState)(DWORD,XINPUT_STATE*);
1246+ typedef DWORD (* PFN_XInputGetCapabilities)(DWORD,DWORD,XINPUT_CAPABILITIES*);
1247+ typedef DWORD (* PFN_XInputGetKeystroke)(DWORD, DWORD, PXINPUT_KEYSTROKE);
1248+ typedef HRESULT (WINAPI * PFN_DirectInput8Create)(HINSTANCE,DWORD,REFIID,LPVOID*,LPUNKNOWN);
1249+
1250+ HINSTANCE mg_xinput_dll = NULL;
1251+ HINSTANCE mg_dinput_dll = NULL;
12511252
1252- HINSTANCE xinput_dll = NULL;
12531253PFN_XInputGetState XInputGetStateSrc = NULL;
12541254PFN_XInputGetKeystroke XInputGetKeystrokeSrc = NULL;
12551255PFN_XInputGetCapabilities XInputGetCapabilitiesSrc = NULL;
1256-
1257-
1256+ PFN_DirectInput8Create DInput8CreateSrc = NULL;
12581257
12591258const GUID MG_IID_IDirectInput8W =
12601259 {0xbf798031,0x483a,0x4da2,{0xaa,0x99,0x5d,0x64,0xed,0x36,0x97,0x00}};
@@ -1336,7 +1335,7 @@ mg_bool mg_supportsXInput(const GUID* guid) {
13361335 unsigned int count = 0;
13371336 mg_size_t i;
13381337
1339- if (xinput_dll == NULL) {
1338+ if (mg_xinput_dll == NULL) {
13401339 return MG_FALSE;
13411340 }
13421341
@@ -1401,30 +1400,29 @@ BOOL CALLBACK DirectInputEnumDevicesCallback(LPCDIDEVICEINSTANCE inst, LPVOID us
14011400 gamepad = mg_gamepad_find(gamepads);
14021401 gamepad->src.device = NULL;
14031402
1404- if (FAILED(IDirectInput8_CreateDevice(gamepads->src.dinput, &inst->guidInstance, &gamepad->src.device, NULL))) {
1403+ if (FAILED(IDirectInput8_CreateDevice((IDirectInput8*) gamepads->src.dinput, &inst->guidInstance, (IDirectInputDevice8**) &gamepad->src.device, NULL))) {
14051404 mg_gamepad_release(gamepads, gamepad);
14061405 return DIENUM_CONTINUE;
14071406 }
14081407
14091408
1410- if (FAILED(IDirectInputDevice8_SetDataFormat(gamepad->src.device, &mg_dataFormat ))) {
1409+ if (FAILED(IDirectInputDevice8_SetDataFormat((IDirectInputDevice8*) gamepad->src.device, &mg_dataFormat ))) {
14111410 mg_gamepad_release(gamepads, gamepad);
14121411 return DIENUM_CONTINUE;
14131412 }
14141413
14151414 MG_MEMSET(&caps, 0, sizeof(caps));
14161415 caps.dwSize = sizeof(DIDEVCAPS);
14171416
1418- IDirectInputDevice8_GetCapabilities(gamepad->src.device, &caps);
1419- gamepad->src.caps = caps;
1417+ IDirectInputDevice8_GetCapabilities((IDirectInputDevice8*)gamepad->src.device, &caps);
14201418
14211419 MG_MEMSET(&dipd, 0, sizeof(dipd));
14221420 dipd.diph.dwSize = sizeof(dipd);
14231421 dipd.diph.dwHeaderSize = sizeof(dipd.diph);
14241422 dipd.diph.dwHow = DIPH_DEVICE;
14251423 dipd.dwData = DIPROPAXISMODE_ABS;
14261424
1427- if (FAILED(IDirectInputDevice8_SetProperty(gamepad->src.device, DIPROP_AXISMODE, &dipd.diph))) {
1425+ if (FAILED(IDirectInputDevice8_SetProperty((IDirectInputDevice8*) gamepad->src.device, DIPROP_AXISMODE, &dipd.diph))) {
14281426 mg_gamepad_release(gamepads, gamepad);
14291427 return DIENUM_CONTINUE;
14301428 }
@@ -1455,10 +1453,10 @@ BOOL CALLBACK DirectInputEnumDevicesCallback(LPCDIDEVICEINSTANCE inst, LPVOID us
14551453 DIJOYSTATE state;
14561454 MG_MEMSET(&state, 0, sizeof(state));
14571455
1458- IDirectInputDevice8_Acquire(gamepad->src.device);
1459- IDirectInputDevice8_Poll(gamepad->src.device);
1456+ IDirectInputDevice8_Acquire((IDirectInputDevice8*) gamepad->src.device);
1457+ IDirectInputDevice8_Poll((IDirectInputDevice8*) gamepad->src.device);
14601458
1461- result = IDirectInputDevice8_GetDeviceState(gamepad->src.device, sizeof(state), &state);
1459+ result = IDirectInputDevice8_GetDeviceState((IDirectInputDevice8*) gamepad->src.device, sizeof(state), &state);
14621460 if (FAILED(result)) {
14631461 mg_gamepad_release(gamepads, gamepad);
14641462 return DIENUM_CONTINUE;
@@ -1513,48 +1511,50 @@ BOOL CALLBACK DirectInputEnumDevicesCallback(LPCDIDEVICEINSTANCE inst, LPVOID us
15131511}
15141512
15151513void mg_gamepads_init_platform(mg_gamepads* gamepads) {
1516- /* init global gamepads->src.data */
1517- if (xinput_dll == NULL) {
1514+ HINSTANCE hInstance = GetModuleHandle(0);
1515+
1516+ /* init global gamepads->src.data */
1517+ if (mg_xinput_dll == NULL) {
15181518 /* load xinput dll and functions (if it's available) */
15191519 static const char* names[] = {"xinput0_4.dll", "xinput9_1_0.dll", "xinput1_2.dll", "xinput1_1.dll"};
15201520
15211521 uint32_t i;
15221522 for (i = 0; i < sizeof(names) / sizeof(const char*) && (XInputGetStateSrc == NULL || XInputGetKeystrokeSrc != NULL); i++) {
1523- xinput_dll = LoadLibraryA(names[i]);
1524- if (xinput_dll ) {
1525- XInputGetStateSrc = (PFN_XInputGetState)(mg_proc)GetProcAddress(xinput_dll , "XInputGetState");
1526- XInputGetKeystrokeSrc = (PFN_XInputGetKeystroke)(mg_proc)GetProcAddress(xinput_dll , "XInputGetKeystroke");
1527- XInputGetCapabilitiesSrc = (PFN_XInputGetCapabilities)(mg_proc)GetProcAddress(xinput_dll , "XInputGetCapabilities");
1523+ mg_xinput_dll = LoadLibraryA(names[i]);
1524+ if (mg_xinput_dll ) {
1525+ XInputGetStateSrc = (PFN_XInputGetState)(mg_proc)GetProcAddress(mg_xinput_dll , "XInputGetState");
1526+ XInputGetKeystrokeSrc = (PFN_XInputGetKeystroke)(mg_proc)GetProcAddress(mg_xinput_dll , "XInputGetKeystroke");
1527+ XInputGetCapabilitiesSrc = (PFN_XInputGetCapabilities)(mg_proc)GetProcAddress(mg_xinput_dll , "XInputGetCapabilities");
15281528 }
15291529 }
15301530
1531- if (xinput_dll ) {
1531+ if (mg_xinput_dll ) {
15321532 mg_bool b = mg_gamepads_fetch(gamepads, NULL);
15331533 MG_UNUSED(b);
15341534 }
15351535 }
15361536
1537- if (gamepads->src.dinput_dll == NULL) {
1538-
1537+ if (mg_dinput_dll == NULL && DInput8CreateSrc == NULL) {
15391538 /* load directinput dll and functions */
1540- gamepads->src.dinput_dll = LoadLibraryA("dinput8.dll");
1541- if (gamepads->src.dinput_dll) {
1542- HINSTANCE hInstance = GetModuleHandle(0);
1543- gamepads->src.DInput8Create = (PFN_DirectInput8Create)(mg_proc)GetProcAddress(gamepads->src.dinput_dll, "DirectInput8Create");
1544- if (FAILED(gamepads->src.DInput8Create(hInstance,
1545- DIRECTINPUT_VERSION,
1546- &MG_IID_IDirectInput8W,
1547- (void**) &gamepads->src.dinput,
1548- NULL)) ||
1549- FAILED(IDirectInput8_EnumDevices(gamepads->src.dinput,
1550- DI8DEVCLASS_GAMECTRL,
1551- DirectInputEnumDevicesCallback,
1552- (void*)gamepads,
1553- DIEDFL_ALLDEVICES))) {
1554- gamepads->src.dinput_dll = NULL;
1555- }
1556- }
1557- }
1539+ mg_dinput_dll = LoadLibraryA("dinput8.dll");
1540+ if (mg_dinput_dll)
1541+ DInput8CreateSrc = (PFN_DirectInput8Create)(mg_proc)GetProcAddress(mg_dinput_dll, "DirectInput8Create");
1542+ }
1543+
1544+ if (DInput8CreateSrc) {
1545+ if (FAILED(DInput8CreateSrc(hInstance,
1546+ DIRECTINPUT_VERSION,
1547+ &MG_IID_IDirectInput8W,
1548+ (void**) &gamepads->src.dinput,
1549+ NULL)) ||
1550+ FAILED(IDirectInput8_EnumDevices((IDirectInput8*)gamepads->src.dinput,
1551+ DI8DEVCLASS_GAMECTRL,
1552+ DirectInputEnumDevicesCallback,
1553+ (void*)gamepads,
1554+ DIEDFL_ALLDEVICES))) {
1555+ mg_dinput_dll = NULL;
1556+ }
1557+ }
15581558}
15591559
15601560#ifndef XINPUT_DEVSUBTYPE_FLIGHT_STICK
@@ -1589,7 +1589,7 @@ static const char* mg_xinput_gamepad_name(const XINPUT_CAPABILITIES xic) {
15891589
15901590mg_bool mg_gamepads_update_platform(mg_gamepads* gamepads, mg_events* events) {
15911591 mg_bool out = MG_FALSE;
1592- if (xinput_dll ) {
1592+ if (mg_xinput_dll ) {
15931593 DWORD dwResult, i;
15941594 for (i = 0; i < XUSER_MAX_COUNT; i++) {
15951595 mg_gamepad* gamepad = mg_xinput_list[i];
@@ -1649,7 +1649,7 @@ mg_bool mg_gamepads_update_platform(mg_gamepads* gamepads, mg_events* events) {
16491649 }
16501650
16511651 if (gamepads->src.dinput) {
1652- IDirectInput8_EnumDevices(gamepads->src.dinput,
1652+ IDirectInput8_EnumDevices((IDirectInput8*) gamepads->src.dinput,
16531653 DI8DEVCLASS_GAMECTRL,
16541654 DirectInputEnumDevicesCallback,
16551655 (void*)gamepads,
@@ -1660,16 +1660,16 @@ mg_bool mg_gamepads_update_platform(mg_gamepads* gamepads, mg_events* events) {
16601660}
16611661
16621662void mg_gamepads_free_platform(mg_gamepads* gamepads) {
1663- if (xinput_dll ) {
1664- FreeLibrary(xinput_dll );
1665- xinput_dll = NULL;
1663+ if (mg_xinput_dll ) {
1664+ FreeLibrary(mg_xinput_dll );
1665+ mg_xinput_dll = NULL;
16661666 }
16671667
1668- if (gamepads->src.dinput_dll ) {
1668+ if (mg_dinput_dll ) {
16691669 if (gamepads->src.dinput)
1670- IDirectInput8_Release(gamepads->src.dinput);
1670+ IDirectInput8_Release((IDirectInput8*) gamepads->src.dinput);
16711671
1672- FreeLibrary(gamepads->src.dinput_dll );
1672+ FreeLibrary(mg_dinput_dll );
16731673 }
16741674}
16751675
@@ -1744,18 +1744,22 @@ mg_bool mg_gamepad_update_platform(mg_gamepad* gamepad, mg_events* events) {
17441744
17451745 if (gamepad->src.device) {
17461746 u32 i;
1747- DIDEVCAPS caps = gamepad->src. caps;
1747+ DIDEVCAPS caps;
17481748 DIJOYSTATE state;
17491749 HRESULT result;
17501750 LONG axes_state[6];
17511751
1752- IDirectInputDevice8_Poll(gamepad->src.device);
1753- result = IDirectInputDevice8_GetDeviceState(gamepad->src.device, sizeof(state), &state);
1752+ MG_MEMSET(&caps, 0, sizeof(caps));
1753+ caps.dwSize = sizeof(DIDEVCAPS);
1754+
1755+ IDirectInputDevice8_GetCapabilities((IDirectInputDevice8*)gamepad->src.device, &caps);
1756+ IDirectInputDevice8_Poll((IDirectInputDevice8*)gamepad->src.device);
1757+ result = IDirectInputDevice8_GetDeviceState((IDirectInputDevice8*)gamepad->src.device, sizeof(state), &state);
17541758 if (result == DIERR_NOTACQUIRED || result == DIERR_INPUTLOST) {
1755- IDirectInputDevice8_Acquire(gamepad->src.device);
1756- IDirectInputDevice8_Poll(gamepad->src.device);
1759+ IDirectInputDevice8_Acquire((IDirectInputDevice8*) gamepad->src.device);
1760+ IDirectInputDevice8_Poll((IDirectInputDevice8*) gamepad->src.device);
17571761
1758- result = IDirectInputDevice8_GetDeviceState(gamepad->src.device, sizeof(state), &state);
1762+ result = IDirectInputDevice8_GetDeviceState((IDirectInputDevice8*) gamepad->src.device, sizeof(state), &state);
17591763 }
17601764
17611765 if (FAILED(result)) {
@@ -1811,7 +1815,7 @@ mg_bool mg_gamepad_update_platform(mg_gamepad* gamepad, mg_events* events) {
18111815
18121816void mg_gamepad_release_platform(mg_gamepad* gamepad) {
18131817 if (gamepad->src.device) {
1814- IDirectInputDevice8_Release(gamepad->src.device);
1818+ IDirectInputDevice8_Release((IDirectInputDevice8*) gamepad->src.device);
18151819 }
18161820
18171821 if (gamepad->src.xinput_index) {
@@ -1859,7 +1863,7 @@ void mg_osx_input_value_changed_callback(void *context, IOReturn result, void *s
18591863 CFIndex intValue = IOHIDValueGetIntegerValue(value);
18601864 MG_UNUSED(result); MG_UNUSED(sender);
18611865
1862- if (gamepad->src.device != device)
1866+ if ((IDirectInputDevice8*) gamepad->src.device != device)
18631867 return;
18641868
18651869 switch (usagePage) {
@@ -2024,7 +2028,7 @@ void mg_osx_device_removed_callback(void *context, IOReturn result, void *sender
20242028 }
20252029
20262030 for (cur = gamepads->list.head; cur; cur = cur->next) {
2027- if (cur->src.device == device) {
2031+ if ((IDirectInputDevice8*) cur->src.device == device) {
20282032 mg_handle_connection_event(&gamepads->events, MG_FALSE, cur);
20292033 mg_gamepad_release(gamepads, cur);
20302034 return;
0 commit comments