From 93a1d1c226e0417931b8dd20bbc9aaead4bc777a Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Fri, 7 Sep 2012 01:01:34 +0200 Subject: [PATCH 1/9] Added stubs, implemented on Linux and Cocoa. --- include/GL/glfw3.h | 1 + src/cocoa_joystick.m | 16 +++++++++++++--- src/internal.h | 1 + src/joystick.c | 22 ++++++++++++++++++++++ src/win32_joystick.c | 11 +++++++++++ src/x11_joystick.c | 18 ++++++++++++++++++ src/x11_platform.h | 1 + tests/joysticks.c | 13 +++++++++---- 8 files changed, 76 insertions(+), 7 deletions(-) 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--; } } From a4b3a18755c4236864603949fbd764122b6e122e Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 8 Nov 2012 16:06:23 +0100 Subject: [PATCH 2/9] Formatting. --- src/win32_joystick.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/win32_joystick.c b/src/win32_joystick.c index 9ffb9aac..3e342168 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -199,13 +199,19 @@ 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++; } } From 7eff6b1b1ebb0226b8f84156b473839b4a7c4d48 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 8 Nov 2012 16:26:15 +0100 Subject: [PATCH 3/9] Initial implementation of joystick name retrieval on Win32. --- src/win32_joystick.c | 13 ++++++++++++- src/win32_platform.h | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/win32_joystick.c b/src/win32_joystick.c index 3e342168..a9fe04b1 100644 --- a/src/win32_joystick.c +++ b/src/win32_joystick.c @@ -225,6 +225,17 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, const char* _glfwPlatformGetJoystickName(int joy) { - return ""; + 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 ba10039e..9ee8cc1a 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -208,6 +208,8 @@ typedef struct _GLFWlibraryWin32 } winmm; #endif // _GLFW_NO_DLOAD_WINMM + char* joyNames[GLFW_JOYSTICK_LAST + 1]; + } _GLFWlibraryWin32; From 875a1697924af99182412ff2f4748abfbd798dbf Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 8 Nov 2012 16:26:43 +0100 Subject: [PATCH 4/9] Added missing test for joystick presence. --- src/x11_joystick.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/x11_joystick.c b/src/x11_joystick.c index d9b0116b..230edcb4 100644 --- a/src/x11_joystick.c +++ b/src/x11_joystick.c @@ -340,6 +340,9 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, const char* _glfwPlatformGetJoystickName(int joy) { + if (!_glfwLibrary.X11.joystick[joy].present) + return NULL; + return _glfwLibrary.X11.joystick[joy].name; } From 7be9e87ef18f9f7b91b72cfdffdf1de0de75fbb0 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 2 Dec 2012 16:45:03 +0100 Subject: [PATCH 5/9] Removed bad use of glfwGetCurrentContext. --- tests/joysticks.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/joysticks.c b/tests/joysticks.c index c1abb5f8..9b03a3b7 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -94,11 +94,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(); @@ -205,7 +205,7 @@ int main(void) glClear(GL_COLOR_BUFFER_BIT); refresh_joysticks(); - draw_joysticks(); + draw_joysticks(window); glfwSwapBuffers(window); glfwPollEvents(); From 3a773342e804c744f7fa9c5dd21c36944ee936c8 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 2 Dec 2012 16:52:02 +0100 Subject: [PATCH 6/9] Renamed struct member to match other platforms. --- src/cocoa_joystick.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 3979a2f1..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,8 +443,8 @@ void _glfwInitJoysticks(void) if (refCF) { CFStringGetCString(refCF, - joystick->product, - sizeof(joystick->product), + joystick->name, + sizeof(joystick->name), kCFStringEncodingUTF8); } @@ -632,6 +632,6 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, const char* _glfwPlatformGetJoystickName(int joy) { - return _glfwJoysticks[joy].product; + return _glfwJoysticks[joy].name; } From 0bac5795061cdac2d5e668ab97bec68bb0a169c5 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 2 Dec 2012 16:53:28 +0100 Subject: [PATCH 7/9] Formatting. --- src/joystick.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/joystick.c b/src/joystick.c index 59107e31..9c20303d 100644 --- a/src/joystick.c +++ b/src/joystick.c @@ -136,13 +136,13 @@ GLFWAPI const char* glfwGetJoystickName(int joy) if (!_glfwInitialized) { _glfwSetError(GLFW_NOT_INITIALIZED, NULL); - return 0; + return NULL; } if (joy < 0 || joy > GLFW_JOYSTICK_LAST) { _glfwSetError(GLFW_INVALID_ENUM, NULL); - return 0; + return NULL; } return _glfwPlatformGetJoystickName(joy); From 7d9b5c012702a53c55b40f89a950fb85eeb82a88 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 2 Dec 2012 16:55:09 +0100 Subject: [PATCH 8/9] Added documentation for glfwGetJoystickName. --- include/GL/glfw3.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index ade6d9e5..966234d7 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -1534,6 +1534,13 @@ GLFWAPI int glfwGetJoystickAxes(int joy, float* axes, int numaxes); * @ingroup input */ 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. From 20c891f56691799094095d177af07dbfa3e948c4 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 2 Dec 2012 17:10:41 +0100 Subject: [PATCH 9/9] Updated changelog. --- readme.html | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.html b/readme.html index 3ad596c9..8e155e79 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