GLFW: Make joystick platform code init on demand

From upstream: 782e6b6cef.
This commit is contained in:
Luflosi 2020-07-23 13:46:55 +02:00
parent 78efefcea1
commit 785973bb5e
No known key found for this signature in database
GPG Key ID: 4E41E29EDCC345D0
11 changed files with 72 additions and 37 deletions

View File

@ -669,8 +669,6 @@ int _glfwPlatformInit(void)
if (!initializeTIS()) if (!initializeTIS())
return false; return false;
_glfwInitJoysticksNS();
_glfwPollMonitorsNS(); _glfwPollMonitorsNS();
return true; return true;
@ -723,7 +721,6 @@ void _glfwPlatformTerminate(void)
free(_glfw.ns.clipboardString); free(_glfw.ns.clipboardString);
_glfwTerminateNSGL(); _glfwTerminateNSGL();
_glfwTerminateJoysticksNS();
} // autoreleasepool } // autoreleasepool
} }

View File

@ -44,7 +44,3 @@ typedef struct _GLFWjoystickNS
CFMutableArrayRef hats; CFMutableArrayRef hats;
} _GLFWjoystickNS; } _GLFWjoystickNS;
void _glfwInitJoysticksNS(void);
void _glfwTerminateJoysticksNS(void);

View File

@ -296,7 +296,7 @@ static void removeCallback(void* context UNUSED,
// Initialize joystick interface // Initialize joystick interface
// //
void _glfwInitJoysticksNS(void) bool _glfwPlatformInitJoysticks(void)
{ {
CFMutableArrayRef matching; CFMutableArrayRef matching;
const long usages[] = const long usages[] =
@ -315,7 +315,7 @@ void _glfwInitJoysticksNS(void)
if (!matching) if (!matching)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create array"); _glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create array");
return; return false;
} }
for (size_t i = 0; i < sizeof(usages) / sizeof(long); i++) for (size_t i = 0; i < sizeof(usages) / sizeof(long); i++)
@ -370,19 +370,23 @@ void _glfwInitJoysticksNS(void)
// Execute the run loop once in order to register any initially-attached // Execute the run loop once in order to register any initially-attached
// joysticks // joysticks
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false); CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
return true;
} }
// Close all opened joystick handles // Close all opened joystick handles
// //
void _glfwTerminateJoysticksNS(void) void _glfwPlatformTerminateJoysticks(void)
{ {
int jid; int jid;
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++) for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
closeJoystick(_glfw.joysticks + jid); closeJoystick(_glfw.joysticks + jid);
CFRelease(_glfw.ns.hidManager); if (_glfw.ns.hidManager)
_glfw.ns.hidManager = NULL; {
CFRelease(_glfw.ns.hidManager);
_glfw.ns.hidManager = NULL;
}
} }

1
glfw/init.c vendored
View File

@ -92,6 +92,7 @@ static void terminate(void)
_glfw.mappingCount = 0; _glfw.mappingCount = 0;
_glfwTerminateVulkan(); _glfwTerminateVulkan();
_glfwPlatformTerminateJoysticks();
_glfwPlatformTerminate(); _glfwPlatformTerminate();
_glfw.initialized = false; _glfw.initialized = false;

47
glfw/input.c vendored
View File

@ -44,6 +44,22 @@
#define _GLFW_JOYSTICK_BUTTON 2 #define _GLFW_JOYSTICK_BUTTON 2
#define _GLFW_JOYSTICK_HATBIT 3 #define _GLFW_JOYSTICK_HATBIT 3
// Initializes the platform joystick API if it has not been already
//
static bool initJoysticks(void)
{
if (!_glfw.joysticksInitialized)
{
if (!_glfwPlatformInitJoysticks())
{
_glfwPlatformTerminateJoysticks();
return false;
}
}
return _glfw.joysticksInitialized = true;
}
// Finds a mapping based on joystick GUID // Finds a mapping based on joystick GUID
// //
static _GLFWmapping* findMapping(const char* guid) static _GLFWmapping* findMapping(const char* guid)
@ -1101,6 +1117,9 @@ GLFWAPI int glfwJoystickPresent(int jid)
return false; return false;
} }
if (!initJoysticks())
return false;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return false; return false;
@ -1126,6 +1145,9 @@ GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count)
return NULL; return NULL;
} }
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return NULL; return NULL;
@ -1155,6 +1177,9 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
return NULL; return NULL;
} }
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return NULL; return NULL;
@ -1188,6 +1213,9 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
return NULL; return NULL;
} }
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return NULL; return NULL;
@ -1214,6 +1242,9 @@ GLFWAPI const char* glfwGetJoystickName(int jid)
return NULL; return NULL;
} }
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return NULL; return NULL;
@ -1239,6 +1270,9 @@ GLFWAPI const char* glfwGetJoystickGUID(int jid)
return NULL; return NULL;
} }
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return NULL; return NULL;
@ -1284,6 +1318,10 @@ GLFWAPI void* glfwGetJoystickUserPointer(int jid)
GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun) GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
{ {
_GLFW_REQUIRE_INIT_OR_RETURN(NULL); _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
if (!initJoysticks())
return NULL;
_GLFW_SWAP_POINTERS(_glfw.callbacks.joystick, cbfun); _GLFW_SWAP_POINTERS(_glfw.callbacks.joystick, cbfun);
return cbfun; return cbfun;
} }
@ -1363,6 +1401,9 @@ GLFWAPI int glfwJoystickIsGamepad(int jid)
return false; return false;
} }
if (!initJoysticks())
return false;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return false; return false;
@ -1388,6 +1429,9 @@ GLFWAPI const char* glfwGetGamepadName(int jid)
return NULL; return NULL;
} }
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return NULL; return NULL;
@ -1420,6 +1464,9 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state)
return false; return false;
} }
if (!initJoysticks())
return false;
js = _glfw.joysticks + jid; js = _glfw.joysticks + jid;
if (!js->present) if (!js->present)
return false; return false;

3
glfw/internal.h vendored
View File

@ -574,6 +574,7 @@ struct _GLFWlibrary
_GLFWmonitor** monitors; _GLFWmonitor** monitors;
int monitorCount; int monitorCount;
bool joysticksInitialized;
_GLFWjoystick joysticks[GLFW_JOYSTICK_LAST + 1]; _GLFWjoystick joysticks[GLFW_JOYSTICK_LAST + 1];
_GLFWmapping* mappings; _GLFWmapping* mappings;
int mappingCount; int mappingCount;
@ -667,6 +668,8 @@ void _glfwPlatformSetPrimarySelectionString(const char* string);
const char* _glfwPlatformGetPrimarySelectionString(void); const char* _glfwPlatformGetPrimarySelectionString(void);
#endif #endif
bool _glfwPlatformInitJoysticks(void);
void _glfwPlatformTerminateJoysticks(void);
int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode); int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode);
void _glfwPlatformUpdateGamepadGUID(char* guid); void _glfwPlatformUpdateGamepadGUID(char* guid);

View File

@ -266,7 +266,7 @@ static int compareJoysticks(const void* fp, const void* sp)
// Initialize joystick interface // Initialize joystick interface
// //
bool _glfwInitJoysticksLinux(void) bool _glfwPlatformInitJoysticks(void)
{ {
const char* dirname = "/dev/input"; const char* dirname = "/dev/input";
@ -322,7 +322,7 @@ bool _glfwInitJoysticksLinux(void)
// Close all opened joystick handles // Close all opened joystick handles
// //
void _glfwTerminateJoysticksLinux(void) void _glfwPlatformTerminateJoysticks(void)
{ {
int jid; int jid;
@ -333,14 +333,13 @@ void _glfwTerminateJoysticksLinux(void)
closeJoystick(js); closeJoystick(js);
} }
regfree(&_glfw.linjs.regex);
if (_glfw.linjs.inotify > 0) if (_glfw.linjs.inotify > 0)
{ {
if (_glfw.linjs.watch > 0) if (_glfw.linjs.watch > 0)
inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch); inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch);
close(_glfw.linjs.inotify); close(_glfw.linjs.inotify);
regfree(&_glfw.linjs.regex);
} }
} }

View File

@ -55,7 +55,4 @@ typedef struct _GLFWlibraryLinux
bool dropped; bool dropped;
} _GLFWlibraryLinux; } _GLFWlibraryLinux;
bool _glfwInitJoysticksLinux(void);
void _glfwTerminateJoysticksLinux(void);
void _glfwDetectJoystickConnectionLinux(void); void _glfwDetectJoystickConnectionLinux(void);

View File

@ -33,6 +33,15 @@
////// GLFW platform API ////// ////// GLFW platform API //////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
int _glfwPlatformInitJoysticks(void)
{
return true;
}
void _glfwPlatformTerminateJoysticks(void)
{
}
int _glfwPlatformPollJoystick(_GLFWjoystick* js UNUSED, int mode UNUSED) int _glfwPlatformPollJoystick(_GLFWjoystick* js UNUSED, int mode UNUSED)
{ {
return false; return false;

8
glfw/wl_init.c vendored
View File

@ -771,11 +771,6 @@ int _glfwPlatformInit(void)
// Sync so we got all initial output events // Sync so we got all initial output events
wl_display_roundtrip(_glfw.wl.display); wl_display_roundtrip(_glfw.wl.display);
#ifdef __linux__
if (!_glfwInitJoysticksLinux())
return false;
#endif
if (!_glfw.wl.wmBase) if (!_glfw.wl.wmBase)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
@ -799,9 +794,6 @@ int _glfwPlatformInit(void)
void _glfwPlatformTerminate(void) void _glfwPlatformTerminate(void)
{ {
#ifdef __linux__
_glfwTerminateJoysticksLinux();
#endif
_glfwTerminateEGL(); _glfwTerminateEGL();
if (_glfw.wl.egl.handle) if (_glfw.wl.egl.handle)
{ {

10
glfw/x11_init.c vendored
View File

@ -653,13 +653,6 @@ int _glfwPlatformInit(void)
_glfw.x11.helperWindowHandle = createHelperWindow(); _glfw.x11.helperWindowHandle = createHelperWindow();
_glfw.x11.hiddenCursorHandle = createHiddenCursor(); _glfw.x11.hiddenCursorHandle = createHiddenCursor();
#if defined(__linux__)
if (!_glfwInitJoysticksLinux())
return false;
if (_glfw.linjs.inotify > 0)
addWatch(&_glfw.x11.eventLoopData, "joystick", _glfw.linjs.inotify, POLLIN, 1, NULL, NULL);
#endif
_glfwPollMonitorsX11(); _glfwPollMonitorsX11();
return true; return true;
} }
@ -738,9 +731,6 @@ void _glfwPlatformTerminate(void)
_glfwTerminateEGL(); _glfwTerminateEGL();
_glfwTerminateGLX(); _glfwTerminateGLX();
#if defined(__linux__)
_glfwTerminateJoysticksLinux();
#endif
finalizePollData(&_glfw.x11.eventLoopData); finalizePollData(&_glfw.x11.eventLoopData);
} }