diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h
index 5ffa8ed3..b0de880a 100644
--- a/include/GL/glfw3.h
+++ b/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.
diff --git a/readme.html b/readme.html
index 25198815..8e25c743 100644
--- a/readme.html
+++ b/readme.html
@@ -278,6 +278,7 @@ version of GLFW.
Added glfwSetWindowFocusCallback
function and GLFWwindowfocusfun
type for receiving window focus events
Added glfwSetWindowIconifyCallback
function and GLFWwindowiconifyfun
type for receiving window iconification events
Added glfwGetClipboardString
and glfwSetClipboardString
functions for interacting with the system clipboard
+ Added glfwGetJoystickName
for retrieving the name of a joystick
Added glfwGetCurrentContext
function for retrieving the window whose OpenGL context is current
Added GLFW_SRGB_CAPABLE
for requesting sRGB capable framebuffers
Added GLFW_CLIENT_API
, GLFW_OPENGL_API
and GLFW_OPENGL_ES_API
for selecting client API
diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m
index 04c9e972..e7d5cc81 100644
--- a/src/cocoa_joystick.m
+++ b/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;
+}
+
diff --git a/src/internal.h b/src/internal.h
index 4590208c..fa87e699 100644
--- a/src/internal.h
+++ b/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);
diff --git a/src/joystick.c b/src/joystick.c
index 84763951..9c20303d 100644
--- a/src/joystick.c
+++ b/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);
+}
+
diff --git a/src/win32_joystick.c b/src/win32_joystick.c
index a51773d3..a9fe04b1 100644
--- a/src/win32_joystick.c
+++ b/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];
+}
+
diff --git a/src/win32_platform.h b/src/win32_platform.h
index 758ada2c..f5e94bb5 100644
--- a/src/win32_platform.h
+++ b/src/win32_platform.h
@@ -183,6 +183,8 @@ typedef struct _GLFWlibraryWin32
} winmm;
#endif // _GLFW_NO_DLOAD_WINMM
+ char* joyNames[GLFW_JOYSTICK_LAST + 1];
+
} _GLFWlibraryWin32;
diff --git a/src/x11_joystick.c b/src/x11_joystick.c
index 93ab3e7f..230edcb4 100644
--- a/src/x11_joystick.c
+++ b/src/x11_joystick.c
@@ -41,6 +41,7 @@
#include
#include
#include
+#include
#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;
+}
+
diff --git a/src/x11_platform.h b/src/x11_platform.h
index 8c4740a0..990487fc 100644
--- a/src/x11_platform.h
+++ b/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;
diff --git a/tests/joysticks.c b/tests/joysticks.c
index c1abb5f8..2b243687 100644
--- a/tests/joysticks.c
+++ b/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();