From 770350fd4c8f6c570f0d2e41fe6fe744b33e4725 Mon Sep 17 00:00:00 2001 From: bendmorris Date: Thu, 6 Feb 2014 12:34:15 -0500 Subject: [PATCH 1/2] Adding function to detect joystick names on Win/Mac/Linux. --- project/include/Display.h | 2 ++ project/src/backend/sdl/SDLStage.cpp | 4 ++++ project/src/backend/sdl2/SDL2Stage.cpp | 4 ++++ project/src/common/ExternalInterface.cpp | 14 ++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/project/include/Display.h b/project/include/Display.h index d2b39ff2d..5b069a127 100644 --- a/project/include/Display.h +++ b/project/include/Display.h @@ -437,6 +437,8 @@ public: virtual void setMultitouchActive(bool inActive) { } virtual bool getMultitouchActive() { return false; } + virtual const char *getJoystickName(int id) { return ""; } + Matrix GetFullMatrix(bool inStageScaling); bool FinishEditOnEnter(); diff --git a/project/src/backend/sdl/SDLStage.cpp b/project/src/backend/sdl/SDLStage.cpp index aa1b18e15..d4c94a466 100644 --- a/project/src/backend/sdl/SDLStage.cpp +++ b/project/src/backend/sdl/SDLStage.cpp @@ -684,6 +684,10 @@ public: double mDownX; double mDownY; + + const char *getJoystickName(int id) { + return SDL_JoystickNameForIndex(id); + } Surface *GetPrimarySurface() { diff --git a/project/src/backend/sdl2/SDL2Stage.cpp b/project/src/backend/sdl2/SDL2Stage.cpp index c10cd63f8..902b68225 100644 --- a/project/src/backend/sdl2/SDL2Stage.cpp +++ b/project/src/backend/sdl2/SDL2Stage.cpp @@ -507,6 +507,10 @@ public: double mDownX; double mDownY; + const char *getJoystickName(int id) { + return SDL_JoystickNameForIndex(id); + } + Surface *GetPrimarySurface() { diff --git a/project/src/common/ExternalInterface.cpp b/project/src/common/ExternalInterface.cpp index 791fb2c0b..6b1c773ce 100644 --- a/project/src/common/ExternalInterface.cpp +++ b/project/src/common/ExternalInterface.cpp @@ -1260,6 +1260,20 @@ value lime_stage_set_focus(value inStage,value inObject,value inDirection) } DEFINE_PRIM(lime_stage_set_focus,3); +value lime_stage_get_joystick_name(value inStage, value inId) +{ + #if (defined(HX_WINDOWS) || defined(HX_MACOS) || defined(HX_LINUX)) + Stage *stage; + if (AbstractToObject(inStage,stage)) + { + const char *joystickName = stage->getJoystickName(val_int(inId)); + return alloc_string(joystickName); + } + #endif + return alloc_null(); +} +DEFINE_PRIM(lime_stage_get_joystick_name,2); + DO_STAGE_PROP(focus_rect,FocusRect,alloc_bool,val_bool) DO_STAGE_PROP(scale_mode,ScaleMode,alloc_int,val_int) DO_STAGE_PROP(align,Align,alloc_int,val_int) From 94a0ba8f2fbb81590bd7649b61cd6a34374cb057 Mon Sep 17 00:00:00 2001 From: bendmorris Date: Thu, 6 Feb 2014 16:54:54 -0500 Subject: [PATCH 2/2] Avoid segfault when SDL_JoystickNameForIndex returns null (no joystick for that id.) --- project/include/Display.h | 2 +- project/src/common/ExternalInterface.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/include/Display.h b/project/include/Display.h index 5b069a127..d5e2bfe26 100644 --- a/project/include/Display.h +++ b/project/include/Display.h @@ -437,7 +437,7 @@ public: virtual void setMultitouchActive(bool inActive) { } virtual bool getMultitouchActive() { return false; } - virtual const char *getJoystickName(int id) { return ""; } + virtual const char *getJoystickName(int id) { return NULL; } Matrix GetFullMatrix(bool inStageScaling); bool FinishEditOnEnter(); diff --git a/project/src/common/ExternalInterface.cpp b/project/src/common/ExternalInterface.cpp index 6b1c773ce..4292daafe 100644 --- a/project/src/common/ExternalInterface.cpp +++ b/project/src/common/ExternalInterface.cpp @@ -1267,7 +1267,7 @@ value lime_stage_get_joystick_name(value inStage, value inId) if (AbstractToObject(inStage,stage)) { const char *joystickName = stage->getJoystickName(val_int(inId)); - return alloc_string(joystickName); + if (joystickName != NULL) return alloc_string(joystickName); } #endif return alloc_null();