Make joystick platform code init on demand

This makes joystick support initialize the first time a joystick
function is called, including those gamepad functions that are layered
on top of joystick functions.

Related to #1284.
Related to #1646.
This commit is contained in:
Camilla Löwy
2020-07-12 18:31:29 +02:00
parent f760b124ca
commit 782e6b6cef
19 changed files with 101 additions and 49 deletions
-2
View File
@@ -551,7 +551,6 @@ int _glfwPlatformInit(void)
return GLFW_FALSE;
_glfwInitTimerNS();
_glfwInitJoysticksNS();
_glfwPollMonitorsNS();
@@ -605,7 +604,6 @@ void _glfwPlatformTerminate(void)
free(_glfw.ns.clipboardString);
_glfwTerminateNSGL();
_glfwTerminateJoysticksNS();
} // autoreleasepool
}
-4
View File
@@ -44,7 +44,3 @@ typedef struct _GLFWjoystickNS
CFMutableArrayRef hats;
} _GLFWjoystickNS;
void _glfwInitJoysticksNS(void);
void _glfwTerminateJoysticksNS(void);
+9 -5
View File
@@ -309,7 +309,7 @@ static void removeCallback(void* context,
// Initialize joystick interface
//
void _glfwInitJoysticksNS(void)
GLFWbool _glfwPlatformInitJoysticks(void)
{
CFMutableArrayRef matching;
const long usages[] =
@@ -328,7 +328,7 @@ void _glfwInitJoysticksNS(void)
if (!matching)
{
_glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create array");
return;
return GLFW_FALSE;
}
for (size_t i = 0; i < sizeof(usages) / sizeof(long); i++)
@@ -383,19 +383,23 @@ void _glfwInitJoysticksNS(void)
// Execute the run loop once in order to register any initially-attached
// joysticks
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
return GLFW_TRUE;
}
// Close all opened joystick handles
//
void _glfwTerminateJoysticksNS(void)
void _glfwPlatformTerminateJoysticks(void)
{
int jid;
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
closeJoystick(_glfw.joysticks + jid);
CFRelease(_glfw.ns.hidManager);
_glfw.ns.hidManager = NULL;
if (_glfw.ns.hidManager)
{
CFRelease(_glfw.ns.hidManager);
_glfw.ns.hidManager = NULL;
}
}
+1
View File
@@ -91,6 +91,7 @@ static void terminate(void)
_glfw.mappingCount = 0;
_glfwTerminateVulkan();
_glfwPlatformTerminateJoysticks();
_glfwPlatformTerminate();
_glfw.initialized = GLFW_FALSE;
+47
View File
@@ -43,6 +43,22 @@
#define _GLFW_JOYSTICK_BUTTON 2
#define _GLFW_JOYSTICK_HATBIT 3
// Initializes the platform joystick API if it has not been already
//
static GLFWbool initJoysticks(void)
{
if (!_glfw.joysticksInitialized)
{
if (!_glfwPlatformInitJoysticks())
{
_glfwPlatformTerminateJoysticks();
return GLFW_FALSE;
}
}
return _glfw.joysticksInitialized = GLFW_TRUE;
}
// Finds a mapping based on joystick GUID
//
static _GLFWmapping* findMapping(const char* guid)
@@ -929,6 +945,9 @@ GLFWAPI int glfwJoystickPresent(int jid)
return GLFW_FALSE;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return GLFW_FALSE;
@@ -954,6 +973,9 @@ GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count)
return NULL;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return NULL;
@@ -983,6 +1005,9 @@ GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
return NULL;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return NULL;
@@ -1016,6 +1041,9 @@ GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
return NULL;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return NULL;
@@ -1042,6 +1070,9 @@ GLFWAPI const char* glfwGetJoystickName(int jid)
return NULL;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return NULL;
@@ -1067,6 +1098,9 @@ GLFWAPI const char* glfwGetJoystickGUID(int jid)
return NULL;
}
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid;
if (!js->present)
return NULL;
@@ -1112,6 +1146,10 @@ GLFWAPI void* glfwGetJoystickUserPointer(int jid)
GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
if (!initJoysticks())
return NULL;
_GLFW_SWAP_POINTERS(_glfw.callbacks.joystick, cbfun);
return cbfun;
}
@@ -1191,6 +1229,9 @@ GLFWAPI int glfwJoystickIsGamepad(int jid)
return GLFW_FALSE;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return GLFW_FALSE;
@@ -1216,6 +1257,9 @@ GLFWAPI const char* glfwGetGamepadName(int jid)
return NULL;
}
if (!initJoysticks())
return NULL;
js = _glfw.joysticks + jid;
if (!js->present)
return NULL;
@@ -1248,6 +1292,9 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state)
return GLFW_FALSE;
}
if (!initJoysticks())
return GLFW_FALSE;
js = _glfw.joysticks + jid;
if (!js->present)
return GLFW_FALSE;
+3
View File
@@ -539,6 +539,7 @@ struct _GLFWlibrary
_GLFWmonitor** monitors;
int monitorCount;
GLFWbool joysticksInitialized;
_GLFWjoystick joysticks[GLFW_JOYSTICK_LAST + 1];
_GLFWmapping* mappings;
int mappingCount;
@@ -632,6 +633,8 @@ void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
void _glfwPlatformSetClipboardString(const char* string);
const char* _glfwPlatformGetClipboardString(void);
GLFWbool _glfwPlatformInitJoysticks(void);
void _glfwPlatformTerminateJoysticks(void);
int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode);
void _glfwPlatformUpdateGamepadGUID(char* guid);
+3 -4
View File
@@ -266,7 +266,7 @@ static int compareJoysticks(const void* fp, const void* sp)
// Initialize joystick interface
//
GLFWbool _glfwInitJoysticksLinux(void)
GLFWbool _glfwPlatformInitJoysticks(void)
{
const char* dirname = "/dev/input";
@@ -322,7 +322,7 @@ GLFWbool _glfwInitJoysticksLinux(void)
// Close all opened joystick handles
//
void _glfwTerminateJoysticksLinux(void)
void _glfwPlatformTerminateJoysticks(void)
{
int jid;
@@ -333,14 +333,13 @@ void _glfwTerminateJoysticksLinux(void)
closeJoystick(js);
}
regfree(&_glfw.linjs.regex);
if (_glfw.linjs.inotify > 0)
{
if (_glfw.linjs.watch > 0)
inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch);
close(_glfw.linjs.inotify);
regfree(&_glfw.linjs.regex);
}
}
-3
View File
@@ -55,8 +55,5 @@ typedef struct _GLFWlibraryLinux
GLFWbool dropped;
} _GLFWlibraryLinux;
GLFWbool _glfwInitJoysticksLinux(void);
void _glfwTerminateJoysticksLinux(void);
void _glfwDetectJoystickConnectionLinux(void);
+9
View File
@@ -33,6 +33,15 @@
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
int _glfwPlatformInitJoysticks(void)
{
return GLFW_TRUE;
}
void _glfwPlatformTerminateJoysticks(void)
{
}
int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode)
{
return GLFW_FALSE;
-3
View File
@@ -580,7 +580,6 @@ int _glfwPlatformInit(void)
return GLFW_FALSE;
_glfwInitTimerWin32();
_glfwInitJoysticksWin32();
_glfwPollMonitorsWin32();
return GLFW_TRUE;
@@ -607,8 +606,6 @@ void _glfwPlatformTerminate(void)
_glfwTerminateWGL();
_glfwTerminateEGL();
_glfwTerminateJoysticksWin32();
freeLibraries();
}
+4 -2
View File
@@ -493,7 +493,7 @@ static BOOL CALLBACK deviceCallback(const DIDEVICEINSTANCE* di, void* user)
// Initialize joystick interface
//
void _glfwInitJoysticksWin32(void)
GLFWbool _glfwPlatformInitJoysticks(void)
{
if (_glfw.win32.dinput8.instance)
{
@@ -505,15 +505,17 @@ void _glfwInitJoysticksWin32(void)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to create interface");
return GLFW_FALSE;
}
}
_glfwDetectJoystickConnectionWin32();
return GLFW_TRUE;
}
// Close all opened joystick handles
//
void _glfwTerminateJoysticksWin32(void)
void _glfwPlatformTerminateJoysticks(void)
{
int jid;
-3
View File
@@ -48,9 +48,6 @@ typedef struct _GLFWjoystickWin32
GUID guid;
} _GLFWjoystickWin32;
void _glfwInitJoysticksWin32(void);
void _glfwTerminateJoysticksWin32(void);
void _glfwDetectJoystickConnectionWin32(void);
void _glfwDetectJoystickDisconnectionWin32(void);
+3
View File
@@ -531,6 +531,9 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
case WM_DEVICECHANGE:
{
if (!_glfw.joysticksInitialized)
break;
if (wParam == DBT_DEVICEARRIVAL)
{
DEV_BROADCAST_HDR* dbh = (DEV_BROADCAST_HDR*) lParam;
-8
View File
@@ -1146,11 +1146,6 @@ int _glfwPlatformInit(void)
// Sync so we got all initial output events
wl_display_roundtrip(_glfw.wl.display);
#ifdef __linux__
if (!_glfwInitJoysticksLinux())
return GLFW_FALSE;
#endif
_glfwInitTimerPOSIX();
_glfw.wl.timerfd = -1;
@@ -1213,9 +1208,6 @@ int _glfwPlatformInit(void)
void _glfwPlatformTerminate(void)
{
#ifdef __linux__
_glfwTerminateJoysticksLinux();
#endif
_glfwTerminateEGL();
if (_glfw.wl.egl.handle)
{
-9
View File
@@ -1377,11 +1377,6 @@ int _glfwPlatformInit(void)
NULL);
}
#if defined(__linux__)
if (!_glfwInitJoysticksLinux())
return GLFW_FALSE;
#endif
_glfwInitTimerPOSIX();
_glfwPollMonitorsX11();
@@ -1475,10 +1470,6 @@ void _glfwPlatformTerminate(void)
_glfwTerminateEGL();
_glfwTerminateGLX();
#if defined(__linux__)
_glfwTerminateJoysticksLinux();
#endif
if (_glfw.x11.xlib.handle)
{
_glfw_dlclose(_glfw.x11.xlib.handle);