Quellcode durchsuchen

Merge branch 'master' into EGL

Camilla Berglund vor 12 Jahren
Ursprung
Commit
14e71833bd
10 geänderte Dateien mit 114 neuen und 15 gelöschten Zeilen
  1. 8 0
      include/GL/glfw3.h
  2. 1 0
      readme.html
  3. 14 4
      src/cocoa_joystick.m
  4. 1 0
      src/internal.h
  5. 22 0
      src/joystick.c
  6. 32 4
      src/win32_joystick.c
  7. 2 0
      src/win32_platform.h
  8. 21 0
      src/x11_joystick.c
  9. 1 0
      src/x11_platform.h
  10. 12 7
      tests/joysticks.c

+ 8 - 0
include/GL/glfw3.h

@@ -1539,6 +1539,14 @@ GLFWAPI int glfwGetJoystickAxes(int joy, float* axes, int numaxes);
  */
 GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons);
 
+/*! @brief Returns the name of the specified joystick.
+ *  @param[in] joy The joystick to query.
+ *  @return The UTF-8 encoded name of the joystick, or @c NULL if the joystick
+ *  is not present.
+ *  @ingroup input
+ */
+GLFWAPI const char* glfwGetJoystickName(int joy);
+
 /*! @brief Sets the clipboard to the specified string.
  *  @param[in] window The window that will own the clipboard contents.
  *  @param[in] string A UTF-8 encoded string.

+ 1 - 0
readme.html

@@ -278,6 +278,7 @@ version of GLFW.</p>
   <li>Added <code>glfwSetWindowFocusCallback</code> function and <code>GLFWwindowfocusfun</code> type for receiving window focus events</li>
   <li>Added <code>glfwSetWindowIconifyCallback</code> function and <code>GLFWwindowiconifyfun</code> type for receiving window iconification events</li>
   <li>Added <code>glfwGetClipboardString</code> and <code>glfwSetClipboardString</code> functions for interacting with the system clipboard</li>
+  <li>Added <code>glfwGetJoystickName</code> for retrieving the name of a joystick</li>
   <li>Added <code>glfwGetCurrentContext</code> function for retrieving the window whose OpenGL context is current</li>
   <li>Added <code>GLFW_SRGB_CAPABLE</code> for requesting sRGB capable framebuffers</li>
   <li>Added <code>GLFW_CLIENT_API</code>, <code>GLFW_OPENGL_API</code> and <code>GLFW_OPENGL_ES_API</code> for selecting client API</li>

+ 14 - 4
src/cocoa_joystick.m

@@ -68,7 +68,7 @@ typedef struct
 typedef struct
 {
     int present;
-    char product[256];
+    char name[256];
 
     IOHIDDeviceInterface** interface;
 
@@ -443,9 +443,9 @@ void _glfwInitJoysticks(void)
         if (refCF)
         {
             CFStringGetCString(refCF,
-                               (char*) &(joystick->product),
-                               256,
-                               CFStringGetSystemEncoding());
+                               joystick->name,
+                               sizeof(joystick->name),
+                               kCFStringEncodingUTF8);
         }
 
         joystick->numAxes = 0;
@@ -625,3 +625,13 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
     return button;
 }
 
+
+//========================================================================
+// Get joystick name
+//========================================================================
+
+const char* _glfwPlatformGetJoystickName(int joy)
+{
+    return _glfwJoysticks[joy].name;
+}
+

+ 1 - 0
src/internal.h

@@ -285,6 +285,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window);
 int _glfwPlatformGetJoystickParam(int joy, int param);
 int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes);
 int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons);
+const char* _glfwPlatformGetJoystickName(int joy);
 
 // Time input
 double _glfwPlatformGetTime(void);

+ 22 - 0
src/joystick.c

@@ -126,3 +126,25 @@ GLFWAPI int glfwGetJoystickButtons(int joy,
     return _glfwPlatformGetJoystickButtons(joy, buttons, numbuttons);
 }
 
+
+//========================================================================
+// Get joystick name
+//========================================================================
+
+GLFWAPI const char* glfwGetJoystickName(int joy)
+{
+    if (!_glfwInitialized)
+    {
+        _glfwSetError(GLFW_NOT_INITIALIZED, NULL);
+        return NULL;
+    }
+
+    if (joy < 0 || joy > GLFW_JOYSTICK_LAST)
+    {
+        _glfwSetError(GLFW_INVALID_ENUM, NULL);
+        return NULL;
+    }
+
+    return _glfwPlatformGetJoystickName(joy);
+}
+

+ 32 - 4
src/win32_joystick.c

@@ -199,15 +199,43 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
 
     if (hats > 0)
     {
-        int j;
-        int value = ji.dwPOV / 100 / 45;
-        if (value < 0 || value > 8) value = 8;
+        int j, value = ji.dwPOV / 100 / 45;
+
+        if (value < 0 || value > 8)
+            value = 8;
 
         for (j = 0; j < 4 && button < numbuttons; j++)
         {
-            buttons[button++] = directions[value] & (1 << j) ? GLFW_PRESS : GLFW_RELEASE;
+            if (directions[value] & (1 << j))
+                buttons[button] = GLFW_PRESS;
+            else
+                buttons[button] = GLFW_RELEASE;
+
+            button++;
         }
     }
 
     return button;
 }
+
+
+//========================================================================
+// Get joystick name
+//========================================================================
+
+const char* _glfwPlatformGetJoystickName(int joy)
+{
+    JOYCAPS jc;
+    const int i = joy - GLFW_JOYSTICK_1;
+
+    if (!isJoystickPresent(joy))
+        return NULL;
+
+    _glfw_joyGetDevCaps(i, &jc, sizeof(JOYCAPS));
+
+    free(_glfwLibrary.Win32.joyNames[i]);
+    _glfwLibrary.Win32.joyNames[i] = _glfwCreateUTF8FromWideString(jc.szPname);
+
+    return _glfwLibrary.Win32.joyNames[i];
+}
+

+ 2 - 0
src/win32_platform.h

@@ -183,6 +183,8 @@ typedef struct _GLFWlibraryWin32
     } winmm;
 #endif // _GLFW_NO_DLOAD_WINMM
 
+    char*                     joyNames[GLFW_JOYSTICK_LAST + 1];
+
 } _GLFWlibraryWin32;
 
 

+ 21 - 0
src/x11_joystick.c

@@ -41,6 +41,7 @@
 #include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #endif // __linux__
 
 
@@ -52,6 +53,7 @@ static int openJoystickDevice(int joy, const char* path)
 {
 #ifdef __linux__
     char numAxes, numButtons;
+    char name[256];
     int fd, version;
 
     fd = open(path, O_RDONLY | O_NONBLOCK);
@@ -69,6 +71,11 @@ static int openJoystickDevice(int joy, const char* path)
         return GL_FALSE;
     }
 
+    if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
+        strncpy(name, "Unknown", sizeof(name));
+
+    _glfwLibrary.X11.joystick[joy].name = strdup(name);
+
     ioctl(fd, JSIOCGAXES, &numAxes);
     _glfwLibrary.X11.joystick[joy].numAxes = (int) numAxes;
 
@@ -237,6 +244,7 @@ void _glfwTerminateJoysticks(void)
             close(_glfwLibrary.X11.joystick[i].fd);
             free(_glfwLibrary.X11.joystick[i].axis);
             free(_glfwLibrary.X11.joystick[i].button);
+            free(_glfwLibrary.X11.joystick[i].name);
 
             _glfwLibrary.X11.joystick[i].present = GL_FALSE;
         }
@@ -325,3 +333,16 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
     return numButtons;
 }
 
+
+//========================================================================
+// Get joystick name
+//========================================================================
+
+const char* _glfwPlatformGetJoystickName(int joy)
+{
+    if (!_glfwLibrary.X11.joystick[joy].present)
+        return NULL;
+
+    return _glfwLibrary.X11.joystick[joy].name;
+}
+

+ 1 - 0
src/x11_platform.h

@@ -209,6 +209,7 @@ typedef struct _GLFWlibraryX11
         int             numButtons;
         float*          axis;
         unsigned char*  button;
+        char*           name;
     } joystick[GLFW_JOYSTICK_LAST + 1];
 
 } _GLFWlibraryX11;

+ 12 - 7
tests/joysticks.c

@@ -37,6 +37,7 @@
 typedef struct Joystick
 {
     GLboolean present;
+    char* name;
     float* axes;
     unsigned char* buttons;
     int axis_count;
@@ -94,11 +95,11 @@ static void draw_joystick(Joystick* j, int x, int y, int width, int height)
     }
 }
 
-static void draw_joysticks(void)
+static void draw_joysticks(GLFWwindow window)
 {
     int i, width, height;
 
-    glfwGetWindowSize(glfwGetCurrentContext(), &width, &height);
+    glfwGetWindowSize(window, &width, &height);
 
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
@@ -130,6 +131,9 @@ static void refresh_joysticks(void)
         {
             int axis_count, button_count;
 
+            free(j->name);
+            j->name = strdup(glfwGetJoystickName(GLFW_JOYSTICK_1 + i));
+
             axis_count = glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_AXES);
             if (axis_count != j->axis_count)
             {
@@ -150,8 +154,8 @@ static void refresh_joysticks(void)
 
             if (!j->present)
             {
-                printf("Found joystick %i with %i axes, %i buttons\n",
-                       i + 1, j->axis_count, j->button_count);
+                printf("Found joystick %i named \'%s\' with %i axes, %i buttons\n",
+                       i + 1, j->name, j->axis_count, j->button_count);
 
                 joystick_count++;
             }
@@ -162,12 +166,13 @@ static void refresh_joysticks(void)
         {
             if (j->present)
             {
+                printf("Lost joystick %i named \'%s\'\n", i + 1, j->name);
+
+                free(j->name);
                 free(j->axes);
                 free(j->buttons);
                 memset(j, 0, sizeof(Joystick));
 
-                printf("Lost joystick %i\n", i + 1);
-
                 joystick_count--;
             }
         }
@@ -205,7 +210,7 @@ int main(void)
         glClear(GL_COLOR_BUFFER_BIT);
 
         refresh_joysticks();
-        draw_joysticks();
+        draw_joysticks(window);
 
         glfwSwapBuffers(window);
         glfwPollEvents();