Fix SDL2 instance enumeration and mapping
authorThomas Altenburger <mister.helmut@gmail.com>
Thu, 25 Feb 2016 19:19:27 +0000 (20:19 +0100)
committerThomas Altenburger <mister.helmut@gmail.com>
Thu, 25 Feb 2016 22:05:01 +0000 (23:05 +0100)
Source/OpenTK/Platform/SDL2/Sdl2.cs
Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs

index 4690e7d9e531c0cf6451eeb94dd408eb06c21add..b41abb4709afbbd2daab5570ca7d28217e86edfa 100644 (file)
@@ -331,6 +331,10 @@ namespace OpenTK.Platform.SDL2
         [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_JoystickGetGUID", ExactSpelling = true)]
         public static extern JoystickGuid JoystickGetGUID(IntPtr joystick);
 
+        [SuppressUnmanagedCodeSecurity]
+        [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_JoystickInstanceID", ExactSpelling = true)]
+        public static extern int JoystickInstanceID(IntPtr joystick);
+
         [SuppressUnmanagedCodeSecurity]
         [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_JoystickName", ExactSpelling = true)]
         static extern IntPtr JoystickNameInternal(IntPtr joystick);
index 7623bf53c1e74944ac35087cec74e9a12dc15efa..396c336bc72df48a675ca5380fae7e6103fdb6f8 100644 (file)
@@ -42,6 +42,7 @@ namespace OpenTK.Platform.SDL2
         {
             public IntPtr Handle { get; set; }
             public Guid Guid { get; set; }
+            public int InstanceId { get; set; }
             public int PacketNumber { get; set; }
             public int HatCount { get; set; }
             public int BallCount { get; set; }
@@ -51,7 +52,6 @@ namespace OpenTK.Platform.SDL2
         }
 
         // For IJoystickDriver2 implementation
-        int last_joystick_instance = 0;
         readonly List<JoystickDevice> joysticks = new List<JoystickDevice>(4);
         readonly Dictionary<int, int> sdl_instanceid_to_joysticks = new Dictionary<int, int>();
 
@@ -98,6 +98,7 @@ namespace OpenTK.Platform.SDL2
                 joystick = new JoystickDevice<Sdl2JoystickDetails>(id, num_axes, num_buttons);
                 joystick.Description = SDL.JoystickName(handle);
                 joystick.Details.Handle = handle;
+                joystick.Details.InstanceId = SDL.JoystickInstanceID(handle);
                 joystick.Details.Guid = SDL.JoystickGetGUID(handle).ToGuid();
                 joystick.Details.HatCount = num_hats;
                 joystick.Details.BallCount = num_balls;
@@ -315,11 +316,12 @@ namespace OpenTK.Platform.SDL2
                     {
                         IntPtr handle = SDL.JoystickOpen(id);
                         if (handle != IntPtr.Zero)
-                        {
+                        {                                                        
+                            JoystickDevice<Sdl2JoystickDetails> joystick = OpenJoystick(id);
+
+                            int instance_id = joystick.Details.InstanceId;
                             int device_id = id;
-                            int instance_id = last_joystick_instance++;
 
-                            JoystickDevice<Sdl2JoystickDetails> joystick = OpenJoystick(id);
                             if (joystick != null)
                             {
                                 joystick.Details.IsConnected = true;