瀏覽代碼

Simplify parsing of default gamepad mappings

The outer glfwUpdateGamepadMappings function is now bypassed when
parsing the default gamepad mappings.  The data in mappings.h does not
contain any comments or line breaks.  Also this is done before joystick
support has been initialized, so there is no need to look for matching
devices.

Finally, the array of default mappings is pre-allocated.  This has no
measurable performance impact but does generate a lot of calls, which
won't be nice for a user provided custom allocator to deal with.
Camilla Löwy 4 年之前
父節點
當前提交
201400b974
共有 5 個文件被更改,包括 19 次插入19 次删除
  1. 2 15
      src/init.c
  2. 16 0
      src/input.c
  3. 1 0
      src/internal.h
  4. 0 2
      src/mappings.h
  5. 0 2
      src/mappings.h.in

+ 2 - 15
src/init.c

@@ -28,7 +28,6 @@
 //========================================================================
 
 #include "internal.h"
-#include "mappings.h"
 
 #include <string.h>
 #include <stdlib.h>
@@ -256,24 +255,12 @@ GLFWAPI int glfwInit(void)
 
     _glfwPlatformSetTls(&_glfw.errorSlot, &_glfwMainThreadError);
 
+    _glfwInitGamepadMappings();
+
     _glfw.initialized = GLFW_TRUE;
     _glfw.timer.offset = _glfwPlatformGetTimerValue();
 
     glfwDefaultWindowHints();
-
-    {
-        int i;
-
-        for (i = 0;  _glfwDefaultMappings[i];  i++)
-        {
-            if (!glfwUpdateGamepadMappings(_glfwDefaultMappings[i]))
-            {
-                terminate();
-                return GLFW_FALSE;
-            }
-        }
-    }
-
     return GLFW_TRUE;
 }
 

+ 16 - 0
src/input.c

@@ -28,6 +28,7 @@
 //========================================================================
 
 #include "internal.h"
+#include "mappings.h"
 
 #include <assert.h>
 #include <float.h>
@@ -412,6 +413,21 @@ void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value)
 //////                       GLFW internal API                      //////
 //////////////////////////////////////////////////////////////////////////
 
+// Adds the built-in set of gamepad mappings
+//
+void _glfwInitGamepadMappings(void)
+{
+    size_t i;
+    const size_t count = sizeof(_glfwDefaultMappings) / sizeof(char*);
+    _glfw.mappings = calloc(count, sizeof(_GLFWmapping));
+
+    for (i = 0;  i < count;  i++)
+    {
+        if (parseMapping(&_glfw.mappings[_glfw.mappingCount], _glfwDefaultMappings[i]))
+            _glfw.mappingCount++;
+    }
+}
+
 // Returns an available joystick object with arrays and name allocated
 //
 _GLFWjoystick* _glfwAllocJoystick(const char* name,

+ 1 - 0
src/internal.h

@@ -781,6 +781,7 @@ void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size);
 void _glfwFreeGammaArrays(GLFWgammaramp* ramp);
 void _glfwSplitBPP(int bpp, int* red, int* green, int* blue);
 
+void _glfwInitGamepadMappings(void);
 _GLFWjoystick* _glfwAllocJoystick(const char* name,
                                   const char* guid,
                                   int axisCount,

+ 0 - 2
src/mappings.h

@@ -997,7 +997,5 @@ const char* _glfwDefaultMappings[] =
 "03000000120c0000100e000011010000,ZEROPLUS P4 Gamepad,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Linux,",
 "03000000120c0000101e000011010000,ZEROPLUS P4 Wired Gamepad,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Linux,",
 #endif // GLFW_BUILD_LINUX_MAPPINGS
-
-NULL
 };
 

+ 0 - 2
src/mappings.h.in

@@ -78,7 +78,5 @@ const char* _glfwDefaultMappings[] =
 #if defined(GLFW_BUILD_LINUX_MAPPINGS)
 @GLFW_LINUX_MAPPINGS@
 #endif // GLFW_BUILD_LINUX_MAPPINGS
-
-NULL
 };