JoystickDevice<Sdl2JoystickDetails> OpenJoystick(int id)
        {
            JoystickDevice<Sdl2JoystickDetails> joystick = null;
            int num_axes = 0;
            int num_buttons = 0;
            int num_hats = 0;
            int num_balls = 0;
	        int is_haptic = 0;

            IntPtr handle = SDL.JoystickOpen(id);
            if (handle != IntPtr.Zero)
            {
                num_axes = SDL.JoystickNumAxes(handle);
                num_buttons = SDL.JoystickNumButtons(handle);
                num_hats = SDL.JoystickNumHats(handle);
                num_balls = SDL.JoystickNumBalls(handle);
	            is_haptic = SDL.JoystickIsHaptic(handle);

	            			
                joystick = new JoystickDevice<Sdl2JoystickDetails>(id, num_axes, num_buttons);
                joystick.Description = SDL.JoystickName(handle);
                joystick.Details.Handle = handle;
                joystick.Details.Guid = SDL.JoystickGetGUID(handle).ToGuid();
                joystick.Details.HatCount = num_hats;
                joystick.Details.BallCount = num_balls;
	            joystick.Details.IsHaptic = is_haptic > 0;

				if (is_haptic > 0)
				{
					Debug.Print("[SDL2] Joystick device {0} supports haptics", id);

					IntPtr haptic = SDL.HapticOpenFromJoystick(handle);
					if (haptic != IntPtr.Zero)
					{
						joystick.Details.Haptic = haptic;

						if (SDL.HapticEffectSupported(haptic, ref leftRightHapticEffect) == 1)
						{
							supportedHapticEffect = HapticEffectType.LeftAndRightMotor;
							SDL.HapticNewEffect(haptic, ref leftRightHapticEffect);
						}
						else if (SDL.HapticRumbleSupported(haptic) == 1)
						{
							supportedHapticEffect = HapticEffectType.Simple;
							if (SDL.HapticRumbleInit(haptic) < 0)
							{
								Debug.Print("[SDL2] Couldn't initialize rumble for joystick device {0}", id);
							}
						}
						else
						{
							SDL.HapticClose(haptic);
							joystick.Details.IsHaptic = false;
						}
					}
				}

                Debug.Print("[SDL2] Joystick device {0} opened successfully. ", id);
                Debug.Print("\t\t'{0}' has {1} axes, {2} buttons, {3} hats, {4} balls",
                    joystick.Description, joystick.Axis.Count, joystick.Button.Count,
                    joystick.Details.HatCount, joystick.Details.BallCount);
            }
            else
            {
                Debug.Print("[SDL2] Failed to open joystick device {0}", id);
            }

            return joystick;
        }
Esempio n. 2
0
        JoystickDevice <Sdl2JoystickDetails> OpenJoystick(int id)
        {
            JoystickDevice <Sdl2JoystickDetails> joystick = null;
            int num_axes    = 0;
            int num_buttons = 0;
            int num_hats    = 0;
            int num_balls   = 0;
            int is_haptic   = 0;

            IntPtr handle = SDL.JoystickOpen(id);

            if (handle != IntPtr.Zero)
            {
                num_axes    = SDL.JoystickNumAxes(handle);
                num_buttons = SDL.JoystickNumButtons(handle);
                num_hats    = SDL.JoystickNumHats(handle);
                num_balls   = SDL.JoystickNumBalls(handle);
                is_haptic   = SDL.JoystickIsHaptic(handle);


                joystick                   = new JoystickDevice <Sdl2JoystickDetails>(id, num_axes, num_buttons);
                joystick.Description       = SDL.JoystickName(handle);
                joystick.Details.Handle    = handle;
                joystick.Details.Guid      = SDL.JoystickGetGUID(handle).ToGuid();
                joystick.Details.HatCount  = num_hats;
                joystick.Details.BallCount = num_balls;
                joystick.Details.IsHaptic  = is_haptic > 0;

                if (is_haptic > 0)
                {
                    Debug.Print("[SDL2] Joystick device {0} supports haptics", id);

                    IntPtr haptic = SDL.HapticOpenFromJoystick(handle);
                    if (haptic != IntPtr.Zero)
                    {
                        joystick.Details.Haptic = haptic;

                        if (SDL.HapticEffectSupported(haptic, ref leftRightHapticEffect) == 1)
                        {
                            supportedHapticEffect = HapticEffectType.LeftAndRightMotor;
                            SDL.HapticNewEffect(haptic, ref leftRightHapticEffect);
                        }
                        else if (SDL.HapticRumbleSupported(haptic) == 1)
                        {
                            supportedHapticEffect = HapticEffectType.Simple;
                            if (SDL.HapticRumbleInit(haptic) < 0)
                            {
                                Debug.Print("[SDL2] Couldn't initialize rumble for joystick device {0}", id);
                            }
                        }
                        else
                        {
                            SDL.HapticClose(haptic);
                            joystick.Details.IsHaptic = false;
                        }
                    }
                }

                Debug.Print("[SDL2] Joystick device {0} opened successfully. ", id);
                Debug.Print("\t\t'{0}' has {1} axes, {2} buttons, {3} hats, {4} balls",
                            joystick.Description, joystick.Axis.Count, joystick.Button.Count,
                            joystick.Details.HatCount, joystick.Details.BallCount);
            }
            else
            {
                Debug.Print("[SDL2] Failed to open joystick device {0}", id);
            }

            return(joystick);
        }