Exemple #1
0
        private static void AddGamePadInstance(int deviceId)
        {
            if (GamePad.ConnectedGamePads == GamePad.GamepadMaxCount)
            {
                return;
            }

            Console.WriteLine("GamePad Added");

            // Clear the error buffer. We're about to do a LOT of dangerous stuff.
            SDL_ClearError();

            int which = GamePad.ConnectedGamePads++;

            // Open the device!
            _gamepadDevices[which] = SDL_GameControllerOpen(deviceId);

            // We use this when dealing with GUID initialization.
            IntPtr thisJoystick = SDL_GameControllerGetJoystick(_gamepadDevices[which]);

            int thisInstance = SDL_JoystickInstanceID(thisJoystick);

            GamepadInstances.Add(thisInstance, which);

            // Start with a fresh state.
            GamepadStates[which]             = new GamePadState();
            GamepadStates[which].IsConnected = true;

            // Initialize the haptics for the joystick, if applicable.
            bool hasRumble = SDL_GameControllerRumble(
                _gamepadDevices[which],
                0,
                0,
                0
                ) == 0;

            GamePadCapabilities caps = new GamePadCapabilities();

            caps.IsConnected = true;
            caps.GamePadType = GamepadTypes[(int)SDL_JoystickGetType(thisJoystick)];
            caps.HasAButton  = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonA
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasBButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonB
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasXButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonX
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasYButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonY
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasBackButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonBack
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasBigButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonGuide
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasStartButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonStart
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasLeftStickButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonLeftstick
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasRightStickButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonRightstick
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasLeftShoulderButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonLeftshoulder
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasRightShoulderButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonRightshoulder
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasDPadUpButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonDpadUp
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasDPadDownButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonDpadDown
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasDPadLeftButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonDpadLeft
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasDPadRightButton = SDL_GameControllerGetBindForButton(
                _gamepadDevices[which],
                SdlGameControllerButton.SdlControllerButtonDpadRight
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasLeftXThumbStick = SDL_GameControllerGetBindForAxis(
                _gamepadDevices[which],
                SdlGameControllerAxis.SdlControllerAxisLeftx
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasLeftYThumbStick = SDL_GameControllerGetBindForAxis(
                _gamepadDevices[which],
                SdlGameControllerAxis.SdlControllerAxisLefty
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasRightXThumbStick = SDL_GameControllerGetBindForAxis(
                _gamepadDevices[which],
                SdlGameControllerAxis.SdlControllerAxisRightx
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasRightYThumbStick = SDL_GameControllerGetBindForAxis(
                _gamepadDevices[which],
                SdlGameControllerAxis.SdlControllerAxisRighty
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasLeftTrigger = SDL_GameControllerGetBindForAxis(
                _gamepadDevices[which],
                SdlGameControllerAxis.SdlControllerAxisTriggerleft
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasRightTrigger = SDL_GameControllerGetBindForAxis(
                _gamepadDevices[which],
                SdlGameControllerAxis.SdlControllerAxisTriggerright
                ).bindType != SdlGameControllerBindType.SdlControllerBindtypeNone;

            caps.HasLeftVibrationMotor  = hasRumble;
            caps.HasRightVibrationMotor = hasRumble;

            caps.HasVoiceSupport = false;
            GamepadCaps[which]   = caps;

            ushort vendor  = SDL_JoystickGetVendor(thisJoystick);
            ushort product = SDL_JoystickGetProduct(thisJoystick);

            if (vendor == 0x00 && product == 0x00)
            {
                GamepadGuids[which] = "xinput";
            }
            else
            {
                GamepadGuids[which] = string.Format(
                    "{0:x2}{1:x2}{2:x2}{3:x2}",
                    vendor & 0xFF,
                    vendor >> 8,
                    product & 0xFF,
                    product >> 8
                    );
            }

            // Initialize light bar
            if (RunningPlatform == RunningPlatform.Linux &&
                (GamepadGuids[which].Equals("4c05c405") ||
                 GamepadGuids[which].Equals("4c05cc09")))
            {
                // Get all of the individual PS4 LED instances
                List <string> ledList = new List <string>();
                string[]      dirs    = Directory.GetDirectories("/sys/class/leds/");
                foreach (string dir in dirs)
                {
                    if (dir.EndsWith("blue") &&
                        (dir.Contains("054C:05C4") ||
                         dir.Contains("054C:09CC")))
                    {
                        ledList.Add(dir.Substring(0, dir.LastIndexOf(':') + 1));
                    }
                }
                // Find how many of these are already in use
                int numLights = 0;
                for (int i = 0; i < GamepadLightBars.Length; i += 1)
                {
                    if (!string.IsNullOrEmpty(GamepadLightBars[i]))
                    {
                        numLights += 1;
                    }
                }
                // If all are not already in use, use the first unused light
                if (numLights < ledList.Count)
                {
                    GamepadLightBars[which] = ledList[numLights];
                }
            }
        }