diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index 1e90a5bc..295ff3ee 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -566,6 +566,7 @@ GLFWAPI void glfwSetScrollCallback(GLFWscrollfun cbfun); GLFWAPI int glfwGetJoystickParam(int joy, int param); GLFWAPI int glfwGetJoystickAxes(int joy, float* axes, int numaxes); GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons); +GLFWAPI const char* glfwGetJoystickName(int joy); /* Clipboard */ GLFWAPI void glfwSetClipboardString(GLFWwindow window, const char* string); diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 7eac7f91..5728f12f 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -443,9 +443,9 @@ void _glfwInitJoysticks(void) if (refCF) { CFStringGetCString(refCF, - (char*) &(joystick->product), - 256, - CFStringGetSystemEncoding()); + joystick->product, + sizeof(joystick->product), + 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].product; +} + diff --git a/src/internal.h b/src/internal.h index 39aeb4fa..509a0076 100644 --- a/src/internal.h +++ b/src/internal.h @@ -274,6 +274,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..59107e31 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 0; + } + + if (joy < 0 || joy > GLFW_JOYSTICK_LAST) + { + _glfwSetError(GLFW_INVALID_ENUM, NULL); + return 0; + } + + return _glfwPlatformGetJoystickName(joy); +} + diff --git a/src/win32_joystick.c b/src/win32_joystick.c index a51773d3..9ffb9aac 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -211,3 +211,14 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, return button; } + + +//======================================================================== +// Get joystick name +//======================================================================== + +const char* _glfwPlatformGetJoystickName(int joy) +{ + return ""; +} + diff --git a/src/x11_joystick.c b/src/x11_joystick.c index 1895dc6a..bf9a7bc1 100644 --- a/src/x11_joystick.c +++ b/src/x11_joystick.c @@ -40,6 +40,7 @@ #include #include +#include #endif // _GLFW_USE_LINUX_JOYSTICKS @@ -51,6 +52,7 @@ static int openJoystickDevice(int joy, const char* path) { #ifdef _GLFW_USE_LINUX_JOYSTICKS char numAxes, numButtons; + char name[256]; int fd, version; fd = open(path, O_RDONLY | O_NONBLOCK); @@ -68,6 +70,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; @@ -214,6 +221,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; } @@ -302,3 +310,13 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, return numButtons; } + +//======================================================================== +// Get joystick name +//======================================================================== + +const char* _glfwPlatformGetJoystickName(int joy) +{ + return _glfwLibrary.X11.joystick[joy].name; +} + diff --git a/src/x11_platform.h b/src/x11_platform.h index 75beb745..4b154fc4 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -240,6 +240,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 40202ce7..f2ba7452 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; @@ -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--; } }