示例#1
0
        public long StartCapture(Guid joystickGuid)
        {
            long result = Errors.ERROR;

            if (!directInput.IsDeviceAttached(joystickGuid))
            {
                MessageBox.Show("Джойстик не найден, обновите список!", "Джойстик", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                joystick = new Joystick(directInput, joystickGuid);
                if (joystick != null)
                {
                    joystick.Properties.BufferSize = 128;
                    joystick.Acquire();

                    pollingThread = new Thread(new ThreadStart(PollJoystick));
                    if (pollingThread != null)
                    {
                        pollingThread.Start();
                        Connect = true;
                        result  = Errors.SUCCESS;
                    }
                }
            }

            return(result);
        }
示例#2
0
 private void AcquireJoystick()
 {
     if (mDirectInput.IsDeviceAttached(mPtmConfiguration.JoystickGuid))
     {
         try
         {
             mJoystick = new Joystick(mDirectInput, mPtmConfiguration.JoystickGuid);
             mJoystick.Acquire();
             mPtmConfiguration.JoystickAcquired = true;
             return;
         }
         catch (SharpDXException)
         {
             if (mJoystick != null)
             {
                 mJoystick.Unacquire();
                 mJoystick.Dispose();
             }
             mJoystick = null;
             return;
         }
     }
     mPtmConfiguration.JoystickAcquired = false;
     if (mJoystick != null)
     {
         mJoystick.Unacquire();
         mJoystick.Dispose();
     }
     mJoystick = null;
 }
 public JS_Input()
 {
     joystickGuid = new Guid("5f939150-07ab-11ea-8001-444553540000");
     if (directInput.IsDeviceAttached(joystickGuid))
     {
         joystickConnected = true;
         joystick          = new Joystick(directInput, joystickGuid);
         //Create buffer
         joystick.Properties.BufferSize = 128;
         // Acquire the joystick
         joystick.Acquire();
     }
 }
示例#4
0
        public ControllerDevice DetectControllers()
        {
            if (device != null && !directInput.IsDeviceAttached(device.joystick.Information.InstanceGuid))
            {
                Console.WriteLine(device.joystick.Properties.InstanceName + " Removed");
                device = null;
                worker.Abort();
                worker = null;
                Unplug(controllerNrPluggedIn);
            }
            if (device != null)
            {
                return(device);
            }

            foreach (var deviceInstance in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                Joystick joystick = new Joystick(directInput, deviceInstance.InstanceGuid);

                if (joystick.Information.ProductGuid.ToString() == "028e045e-0000-0000-0000-504944564944") //If its an emulated controller skip it
                {
                    continue;
                }

                if (joystick.Capabilities.ButtonCount < 1 && joystick.Capabilities.AxesCount < 1) //Skip if it doesn't have any button and axes
                {
                    continue;
                }

                if (joystick.Information.InstanceName != "Logitech G13 Joystick")
                {
                    continue;
                }

                Logger.Log("Found Logitech G13 Joystick");
                if (device != null && device.joystick.Information.InstanceGuid == deviceInstance.InstanceGuid) //If the device is already initialized skip it
                {
                    Console.WriteLine("Controller Already Acquired " + deviceInstance.InstanceName);
                    break;
                }

                joystick.SetCooperativeLevel(handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
                joystick.Properties.BufferSize = 128;
                joystick.Acquire();
                device = new ControllerDevice(joystick, keyb);
                Console.WriteLine("Created new instance and aquired joystick");
                break;
            }
            return(device);
        }
示例#5
0
 private void AcquireDevices()
 {
     mPttDevices.Clear();
     foreach (var deviceInstance in mDirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices))
     {
         if (mDirectInput.IsDeviceAttached(deviceInstance.InstanceGuid))
         {
             Joystick device = new Joystick(mDirectInput, deviceInstance.InstanceGuid);
             try
             {
                 device.Acquire();
                 mPttDevices.Add(device);
             }
             catch { }
         }
     }
 }
示例#6
0
        public JoystickUpdate[] GetData()
        {
            if (_controller == null)
            {
                return(null);
            }

            if (!_directInput.IsDeviceAttached(_controller.Information.InstanceGuid))
            {
                if (!Initialize())
                {
                    return(null);
                }
            }

            try
            {
                // Poll events from joystick
                _controller.Poll();
                var datas = _controller.GetBufferedData();
                foreach (var state in datas)
                {
                    Debug.WriteLine(state);
                }
                return(datas);
            }
            catch (SharpDXException e)
            {
                if (e.ResultCode.Code == ResultCode.InputLost.Code ||
                    e.ResultCode.Code == ResultCode.NotAcquired.Code)
                {
                    _controller.Acquire();
                }

                Debug.WriteLine(e.ToString());
            }

            return(null);
        }
示例#7
0
            public override GamePadState GetState()
            {
                var gamePadState = new GamePadState();

                // Get the current joystick state
                try
                {
                    if (instance == null)
                    {
                        if (directInput.IsDeviceAttached(key.Guid))
                        {
                            instance = new Joystick(directInput, key.Guid);
                        }
                        else
                        {
                            return(gamePadState);
                        }
                    }

                    // Acquire the joystick
                    instance.Acquire();

                    // Make sure that the latest state is up to date
                    instance.Poll();

                    // Get the state
                    instance.GetCurrentState(ref joystickState);
                }
                catch (SharpDX.SharpDXException)
                {
                    // If there was an exception, dispose the native instance
                    try
                    {
                        if (instance != null)
                        {
                            instance.Dispose();
                            instance = null;
                        }

                        // Return a GamePadState that specify that it is not connected
                        return(gamePadState);
                    }
                    catch (Exception)
                    {
                    }
                }

                //Console.WriteLine(joystickState);
                gamePadState.IsConnected = true;

                gamePadState.Buttons = GamePadButton.None;
                if (joystickState.Buttons[0])
                {
                    gamePadState.Buttons |= GamePadButton.X;
                }
                if (joystickState.Buttons[1])
                {
                    gamePadState.Buttons |= GamePadButton.A;
                }
                if (joystickState.Buttons[2])
                {
                    gamePadState.Buttons |= GamePadButton.B;
                }
                if (joystickState.Buttons[3])
                {
                    gamePadState.Buttons |= GamePadButton.Y;
                }
                if (joystickState.Buttons[4])
                {
                    gamePadState.Buttons |= GamePadButton.LeftShoulder;
                }
                if (joystickState.Buttons[5])
                {
                    gamePadState.Buttons |= GamePadButton.RightShoulder;
                }
                if (joystickState.Buttons[6])
                {
                    gamePadState.LeftTrigger = 1.0f;
                }
                if (joystickState.Buttons[7])
                {
                    gamePadState.RightTrigger = 1.0f;
                }

                if (joystickState.Buttons[8])
                {
                    gamePadState.Buttons |= GamePadButton.Back;
                }
                if (joystickState.Buttons[9])
                {
                    gamePadState.Buttons |= GamePadButton.Start;
                }

                if (joystickState.Buttons[10])
                {
                    gamePadState.Buttons |= GamePadButton.LeftThumb;
                }
                if (joystickState.Buttons[11])
                {
                    gamePadState.Buttons |= GamePadButton.RightThumb;
                }

                int dPadRawValue = joystickState.PointOfViewControllers[0];

                if (dPadRawValue >= 0)
                {
                    int dPadValue = dPadRawValue / 4500;
                    switch (dPadValue)
                    {
                    case 0:
                        gamePadState.Buttons |= GamePadButton.PadUp;
                        break;

                    case 1:
                        gamePadState.Buttons |= GamePadButton.PadUp;
                        gamePadState.Buttons |= GamePadButton.PadRight;
                        break;

                    case 2:
                        gamePadState.Buttons |= GamePadButton.PadRight;
                        break;

                    case 3:
                        gamePadState.Buttons |= GamePadButton.PadRight;
                        gamePadState.Buttons |= GamePadButton.PadDown;
                        break;

                    case 4:
                        gamePadState.Buttons |= GamePadButton.PadDown;
                        break;

                    case 5:
                        gamePadState.Buttons |= GamePadButton.PadDown;
                        gamePadState.Buttons |= GamePadButton.PadLeft;
                        break;

                    case 6:
                        gamePadState.Buttons |= GamePadButton.PadLeft;
                        break;

                    case 7:
                        gamePadState.Buttons |= GamePadButton.PadLeft;
                        gamePadState.Buttons |= GamePadButton.PadUp;
                        break;
                    }
                }

                // Left Thumb
                gamePadState.LeftThumb   = new Vector2(2.0f * (joystickState.X / 65535.0f - 0.5f), -2.0f * (joystickState.Y / 65535.0f - 0.5f));
                gamePadState.LeftThumb.X = ClampDeadZone(gamePadState.LeftThumb.X, GamePadAxisDeadZone);
                gamePadState.LeftThumb.Y = ClampDeadZone(gamePadState.LeftThumb.Y, GamePadAxisDeadZone);

                // Right Thumb
                gamePadState.RightThumb   = new Vector2(2.0f * (joystickState.Z / 65535.0f - 0.5f), -2.0f * (joystickState.RotationZ / 65535.0f - 0.5f));
                gamePadState.RightThumb.X = ClampDeadZone(gamePadState.RightThumb.X, GamePadAxisDeadZone);
                gamePadState.RightThumb.Y = ClampDeadZone(gamePadState.RightThumb.Y, GamePadAxisDeadZone);

                return(gamePadState);
            }
        void Joystick_Poll(object Sender, EventArgs e)
        {
            if (joystickGuid != Guid.Empty)
            {
                if (!directInput.IsDeviceAttached(joystickGuid))
                {
                    joystick.Dispose();
                    joystick     = null;
                    joystickGuid = Guid.Empty;
                    joystickEnable.Dispatcher.Invoke(() =>
                    {
                        joystickEnable.IsChecked = false;
                        joystickEnable.IsEnabled = false;
                    });
                }
            }

            JoystickState state = null;

            if (joystick != null)
            {
                joystick.Poll();
                state = joystick.GetCurrentState();
            }
            if (state != null)
            {
                if (joystic_enabled)
                {
                    if (state.Buttons[6])
                    {
                        btnZoomIn_Click(null, null);
                    }
                    else if (state.Buttons[4])
                    {
                        btnZoomOut_Click(null, null);
                    }

                    if (state.Buttons[2] && !prevCamSwitchState)
                    {
                        prevCamSwitchState = true;
                        if (currentCamera == CameraSource.RGB_CAMERA_SOURCE)
                        {
                            setCameraVideoSource(CameraSource.IR_CAMERA_SOURCE);
                        }
                        else if (currentCamera == CameraSource.IR_CAMERA_SOURCE)
                        {
                            setCameraVideoSource(CameraSource.RGB_CAMERA_SOURCE);
                        }
                    }
                    else if (!state.Buttons[2])
                    {
                        prevCamSwitchState = false;
                    }

                    if (currentMode == GimbalMode.MODE_RATE)
                    {
                        double pitch_cmd = 0.0;
                        double yaw_cmd   = 0.0;
                        if (Math.Abs(state.Y - 32768) > 30)
                        {
                            pitch_cmd = ((state.Y - 32768.0) / 65536.0 * -2.0) * 3.14 / 2;
                        }
                        else
                        {
                            pitch_cmd = 0.0;
                        }
                        if (Math.Abs(state.X - 32768) > 30)
                        {
                            yaw_cmd = ((state.X - 32768.0) / 65536.0 * -2.0) * 3.14 / 2;
                        }
                        else
                        {
                            yaw_cmd = 0.0;
                        }
                        sendGimbalCommand(pitch_cmd, 0.0, yaw_cmd);
                    }
                }
            }
        }
示例#9
0
        public ControllerDevice[] detectControllers()
        {
            for (int i = 0; i < 4; i++) //Remove disconnected controllers
            {
                if (devices[i] != null && !directInput.IsDeviceAttached(devices[i].joystick.Information.InstanceGuid))
                {
                    Console.WriteLine(devices[i].joystick.Properties.InstanceName + " Removed");
                    devices[i] = null;
                    workers[i].Abort();
                    workers[i] = null;
                    Unplug(i + 1);
                }
            }

            foreach (var deviceInstance in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                Joystick joystick = new Joystick(directInput, deviceInstance.InstanceGuid);

                if (joystick.Information.ProductGuid.ToString() == "028e045e-0000-0000-0000-504944564944") //If its an emulated controller skip it
                {
                    continue;
                }

                if (joystick.Capabilities.ButtonCount < 1 && joystick.Capabilities.AxesCount < 1) //Skip if it doesn't have any button and axes
                {
                    continue;
                }

                int spot = -1;
                for (int i = 0; i < 4; i++)
                {
                    if (devices[i] == null)
                    {
                        if (spot == -1)
                        {
                            spot = i;
                            Console.WriteLine("Open Spot " + i.ToString());
                        }
                    }
                    else if (devices[i] != null && devices[i].joystick.Information.InstanceGuid == deviceInstance.InstanceGuid) //If the device is already initialized skip it
                    {
                        Console.WriteLine("Controller Already Acquired " + i.ToString() + " " + deviceInstance.InstanceName);
                        spot = -1;
                        break;
                    }
                }

                if (spot == -1)
                {
                    continue;
                }

                if (isExclusive)
                {
                    joystick.SetCooperativeLevel(handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
                }
                else
                {
                    joystick.SetCooperativeLevel(handle, CooperativeLevel.Nonexclusive | CooperativeLevel.Background);
                }
                joystick.Properties.BufferSize = 128;
                joystick.Acquire();

                devices[spot] = new ControllerDevice(joystick, spot + 1);
                if (IsActive)
                {
                    processingData[spot] = new ContData();
                    Console.WriteLine("Plug " + spot);
                    Plugin(spot + 1);
                    int t = spot;
                    workers[spot] = new Thread(() =>
                                               { ProcessData(t); });
                    workers[spot].Start();
                }
            }
            return(devices);
        }
示例#10
0
 public bool IsAttached()
 {
     return(_directInput.IsDeviceAttached(_guid));
 }
        void UpdateDiStates(DirectInput manager, UserGame game, DeviceDetector detector)
        {
            // Get all mapped user instances.
            var instanceGuids = SettingsManager.UserSettings.ItemsToArraySyncronized()
                                .Where(x => x.MapTo > (int)MapTo.None)
                                .Select(x => x.InstanceGuid).ToArray();
            // Get all connected devices.
            var userDevices = SettingsManager.UserDevices.ItemsToArraySyncronized()
                              .Where(x => instanceGuids.Contains(x.InstanceGuid) && x.IsOnline)
                              .ToArray();
            // Acquire copy of feedbacks for processing.
            var feedbacks = CopyAndClearFeedbacks();

            for (int i = 0; i < userDevices.Count(); i++)
            {
                // Update direct input form and return actions (pressed Buttons/DPads, turned Axis/Sliders).
                var           ud    = userDevices[i];
                JoystickState state = null;
                // Allow if not testing or testing with option enabled.
                var o          = SettingsManager.Options;
                var allow      = !o.TestEnabled || o.TestGetDInputStates;
                var isAttached = ud != null && ud.IsOnline && manager.IsDeviceAttached(ud.InstanceGuid);
                if (isAttached && allow)
                {
                    var device = ud.Device;
                    if (device != null)
                    {
                        var exceptionData = new System.Text.StringBuilder();
                        try
                        {
                            // Set BufferSize in order to use buffered data.
                            //device.Properties.BufferSize = 128;
                            var isVirtual        = ((EmulationType)game.EmulationType).HasFlag(EmulationType.Virtual);
                            var hasForceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback);
                            // Exclusive mode required only if force feedback is available and device is virtual there are no info about effects.
                            var exclusiveRequired = hasForceFeedback && (isVirtual || ud.DeviceEffects == null);
                            // If exclusive mode is required and mode is unknown or not exclusive then...
                            if (exclusiveRequired && (!ud.IsExclusiveMode.HasValue || !ud.IsExclusiveMode.Value))
                            {
                                var flags = CooperativeLevel.Background | CooperativeLevel.Exclusive;
                                // Reacquire device in exclusive mode.
                                exceptionData.AppendLine("Unacquire (Exclusive)...");
                                device.Unacquire();
                                exceptionData.AppendLine("SetCooperativeLevel (Exclusive)...");
                                device.SetCooperativeLevel(detector.DetectorForm.Handle, flags);
                                exceptionData.AppendLine("Acquire (Exclusive)...");
                                device.Acquire();
                                ud.IsExclusiveMode = true;
                            }
                            // If current mode must be non exclusive and mode is unknown or exclusive then...
                            else if (!exclusiveRequired && (!ud.IsExclusiveMode.HasValue || ud.IsExclusiveMode.Value))
                            {
                                var flags = CooperativeLevel.Background | CooperativeLevel.NonExclusive;
                                // Reacquire device in non exclusive mode so that xinput.dll can control force feedback.
                                exceptionData.AppendLine("Unacquire (NonExclusive)...");
                                device.Unacquire();
                                exceptionData.AppendLine("SetCooperativeLevel (Exclusive)...");
                                device.SetCooperativeLevel(detector.DetectorForm.Handle, flags);
                                exceptionData.AppendLine("Acquire (Acquire)...");
                                device.Acquire();
                                ud.IsExclusiveMode = false;
                            }
                            exceptionData.AppendFormat("device.GetCurrentState() // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine();
                            // Polling - Retrieves data from polled objects on a DirectInput device.
                            // Some devices require pooling (For example original "Xbox Controller S" with XBCD drivers).
                            // If the device does not require polling, calling this method has no effect.
                            // If a device that requires polling is not polled periodically, no new data is received from the device.
                            // Calling this method causes DirectInput to update the device state, generate input
                            // events (if buffered data is enabled), and set notification events (if notification is enabled).
                            // Get buffered data.
                            //device.Poll();
                            //var datas = device.GetBufferedData();
                            device.Poll();
                            // Get device state.
                            state = device.GetCurrentState();

                            // Fill device objects.
                            if (ud.DeviceObjects == null)
                            {
                                exceptionData.AppendFormat("AppHelper.GetDeviceObjects(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine();
                                var dos = AppHelper.GetDeviceObjects(device);
                                ud.DeviceObjects = dos;
                                // Update masks.
                                int axisMask      = 0;
                                int actuatorMask  = 0;
                                int actuatorCount = 0;
                                if (ud.CapType == (int)SharpDX.DirectInput.DeviceType.Mouse)
                                {
                                    CustomDiState.GetMouseAxisMask(dos, device, out axisMask);
                                }
                                else
                                {
                                    CustomDiState.GetJoystickAxisMask(dos, device, out axisMask, out actuatorMask, out actuatorCount);
                                }
                                ud.DiAxeMask = axisMask;
                                // Contains information about which axis have force feedback actuator attached.
                                ud.DiActuatorMask  = actuatorMask;
                                ud.DiActuatorCount = actuatorCount;
                                CustomDiState.GetJoystickSlidersMask(dos, device);
                            }
                            if (ud.DeviceEffects == null)
                            {
                                exceptionData.AppendFormat("AppHelper.GetDeviceEffects(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine();
                                ud.DeviceEffects = AppHelper.GetDeviceEffects(device);
                            }
                            // If device support force feedback then...
                            if (hasForceFeedback)
                            {
                                // Get setting related to user device.
                                var setting = SettingsManager.UserSettings.ItemsToArraySyncronized()
                                              .FirstOrDefault(x => x.InstanceGuid == ud.InstanceGuid);
                                // If device is mapped to controller then...
                                if (setting != null && setting.MapTo > (int)MapTo.None)
                                {
                                    // Get pad setting attached to device.
                                    var ps = SettingsManager.GetPadSetting(setting.PadSettingChecksum);
                                    if (ps != null)
                                    {
                                        // If force is enabled then...
                                        if (ps.ForceEnable == "1")
                                        {
                                            if (ud.FFState == null)
                                            {
                                                ud.FFState = new Engine.ForceFeedbackState();
                                            }
                                            // If force update supplied then...
                                            var force = feedbacks[(int)setting.MapTo - 1];
                                            if (force != null || ud.FFState.Changed(ps))
                                            {
                                                var v = new Vibration();
                                                if (force == null)
                                                {
                                                    v.LeftMotorSpeed  = short.MinValue;
                                                    v.RightMotorSpeed = short.MinValue;
                                                }
                                                else
                                                {
                                                    v.LeftMotorSpeed  = (short)ConvertHelper.ConvertRange(byte.MinValue, byte.MaxValue, short.MinValue, short.MaxValue, force.LargeMotor);
                                                    v.RightMotorSpeed = (short)ConvertHelper.ConvertRange(byte.MinValue, byte.MaxValue, short.MinValue, short.MaxValue, force.SmallMotor);
                                                }
                                                // For the future: Investigate device states if force feedback is not working.
                                                // var st = ud.Device.GetForceFeedbackState();
                                                //st == SharpDX.DirectInput.ForceFeedbackState
                                                // ud.Device.SendForceFeedbackCommand(ForceFeedbackCommand.SetActuatorsOn);
                                                exceptionData.AppendFormat("ud.FFState.SetDeviceForces(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine();
                                                ud.FFState.SetDeviceForces(ud, device, ps, v);
                                            }
                                        }
                                        // If force state was created then...
                                        else if (ud.FFState != null)
                                        {
                                            // Stop device forces.
                                            exceptionData.AppendFormat("ud.FFState.StopDeviceForces(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine();
                                            ud.FFState.StopDeviceForces(device);
                                            ud.FFState = null;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            var dex = ex as SharpDXException;
                            if (dex != null && dex.ResultCode == SharpDX.DirectInput.ResultCode.InputLost)
                            {
                                // Ignore error.
                            }
                            else if (dex != null && dex.ResultCode == SharpDX.DirectInput.ResultCode.NotAcquired)
                            {
                                // Ignore error
                            }
                            else if (dex != null && dex.ResultCode == SharpDX.DirectInput.ResultCode.Unplugged)
                            {
                                // Ignore error
                            }
                            else
                            {
                                var cx = new DInputException("UpdateDiStates Exception", ex);
                                cx.Data.Add("FFInfo", exceptionData.ToString());
                                JocysCom.ClassLibrary.Runtime.LogHelper.Current.WriteException(cx);
                            }
                            ud.IsExclusiveMode = null;
                        }
                    }
                    // If this is test device then...
                    else if (TestDeviceHelper.ProductGuid.Equals(ud.ProductGuid))
                    {
                        // Fill device objects.
                        if (ud.DeviceObjects == null)
                        {
                            var dos = TestDeviceHelper.GetDeviceObjects();
                            ud.DeviceObjects = dos;
                            // Update masks.
                            ud.DiAxeMask    = 0x1 | 0x2 | 0x4 | 0x8;
                            ud.DiSliderMask = 0;
                        }
                        if (ud.DeviceEffects == null)
                        {
                            ud.DeviceEffects = new DeviceEffectItem[0];
                        }
                        state = TestDeviceHelper.GetCurrentState(ud);
                    }
                }
                ud.JoState = state;
                // Update only if state available.
                if (state != null)
                {
                    var newState = new CustomDiState(ud.JoState);
                    var newTime  = watch.ElapsedTicks;
                    // Remember old state.
                    ud.OldDiState     = ud.DiState;
                    ud.OldDiStateTime = ud.DiStateTime;
                    // Update state.
                    ud.DiState     = newState;
                    ud.DiStateTime = newTime;
                    // Mouse needs special update.
                    if (ud.Device != null && ud.Device.Information.Type == SharpDX.DirectInput.DeviceType.Mouse)
                    {
                        // If original state is missing then...
                        if (ud.OrgDiState == null)
                        {
                            // Store current values.
                            ud.OrgDiState     = newState;
                            ud.OrgDiStateTime = newTime;
                            // Make sure new states have zero values.
                            for (int a = 0; a < newState.Axis.Length; a++)
                            {
                                newState.Axis[a] = -short.MinValue;
                            }
                            for (int s = 0; s < newState.Sliders.Length; s++)
                            {
                                newState.Sliders[s] = -short.MinValue;
                            }
                        }
                        var mouseState = new CustomDiState(new JoystickState());
                        // Clone button values.
                        Array.Copy(newState.Buttons, mouseState.Buttons, mouseState.Buttons.Length);

                        //	//--------------------------------------------------------
                        //	// Map mouse acceleration to axis position. Good for FPS control.
                        //	//--------------------------------------------------------

                        //	// This parts needs to be worked on.
                        //	//var ticks = (int)(newTime - ud.DiStateTime);
                        //	// Update axis with delta.
                        //	//for (int a = 0; a < newState.Axis.Length; a++)
                        //	//	mouseState.Axis[a] = ticks * (newState.Axis[a] - ud.OldDiState.Axis[a]) - short.MinValue;
                        //	// Update sliders with delta.
                        //	//for (int s = 0; s < newState.Sliders.Length; s++)
                        //	//	mouseState.Sliders[s] = ticks * (newState.Sliders[s] - ud.OldDiState.Sliders[s]) - short.MinValue;

                        //--------------------------------------------------------
                        // Map mouse position to axis position. Good for car wheel controls.
                        //--------------------------------------------------------
                        Calc(ud.OrgDiState.Axis, newState.Axis, mouseState.Axis);
                        Calc(ud.OrgDiState.Sliders, newState.Sliders, mouseState.Sliders);
                        ud.DiState = mouseState;
                    }
                }
            }
        }
示例#12
0
        public bool GetInput(int i, bool isButton)
        {
            JoystickState previousState, state;

            try
            {
                previousState = joystick.GetCurrentState();
                state         = previousState;
            }
            catch
            {
                MessageBox.Show("The controller was disconnected, Please plug it back and retry");

                controllerIsConnected = false;
                return(false);
            }

            int?inputNumber = null;
            int?inputValue  = null;

            while (inputValue == null)
            {
                //to know if someone is dumb enough to disconnect it while pressing the button because it's funny
                if (!directInput.IsDeviceAttached(joystick.Information.InstanceGuid))
                {
                    MessageBox.Show("The controller was disconnected, Please plug it back and retry");

                    controllerIsConnected = false;
                    return(false);
                }
                else
                {
                    joystick.Poll();
                    state = joystick.GetCurrentState();
                }
                for (int i2 = 0; i2 < state.Buttons.Length; i2++)
                {
                    if (state.Buttons[i2] == true && state.Buttons[i2] != previousState.Buttons[i2])
                    {
                        inputNumber = null;
                        inputValue  = i2;
                        break;
                    }
                }
                for (int i3 = 0; i3 < state.PointOfViewControllers.Length; i3++)
                {
                    if (state.PointOfViewControllers[i3] != previousState.PointOfViewControllers[i3])
                    {
                        inputNumber = i3;
                        inputValue  = state.PointOfViewControllers[i3];
                        break;
                    }
                }

                previousState = state;
            }

            if (isButton)
            {
                ButtonsNumber[i] = inputNumber;
                ButtonsValues[i] = inputValue;
            }
            else
            {
                DPadNumber[i] = inputNumber;
                DPadValues[i] = inputValue;
            }
            MessageBox.Show("Saved the input");

            /*
             * previousState = joystick.GetCurrentState();
             * while (DPadValues[i] == null)
             * {
             *  if (!directInput.IsDeviceAttached(joystick.Information.InstanceGuid))
             *  {
             *      Console.WriteLine("controller disconnected oof");
             *      Thread.Sleep(5000);
             *  }
             *  joystick.Poll();
             *  state = joystick.GetCurrentState();
             *
             *  for (int i2 = 0; i2 < state.PointOfViewControllers.Length; i2++)
             *  {
             *      if (state.PointOfViewControllers[i2] != previousState.PointOfViewControllers[i2])
             *      {
             *          DPadNumber[i] = i2;
             *          DPadValues[i] = state.PointOfViewControllers[i2];
             *          break;
             *      }
             *  }
             *  for (int i4 = 0; i4 < state.Buttons.Length; i4++)
             *  {
             *      if (state.Buttons[i4] == true && state.Buttons[i4] != previousState.Buttons[i4])
             *      {
             *          DPadNumber[i] = null;
             *          DPadValues[i] = i4;
             *          break;
             *      }
             *  }
             *  if (DPadValues[i] != null)
             *  {
             *      Console.WriteLine(XDPad[i] + " = " + "DPad " + DPadNumber[i]);
             *      Console.WriteLine(state.PointOfViewControllers[0] + " " + state.PointOfViewControllers[1] + " " + state.PointOfViewControllers[2] + " " + state.PointOfViewControllers[3]);
             *
             *      Console.WriteLine(DPadValues[i]);
             *  }
             *  previousState = state;
             * }
             * Thread.Sleep(727);*/

            // Save the f*****g object
            string json = JsonConvert.SerializeObject(new DController());

            File.WriteAllText("DInput.txt", json);

            return(true);
        }
示例#13
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if ((conductor == null) || (calibrating))
            {
                return;
            }

            if (skipCheck > 0)
            {
                --skipCheck;
                return;
            }

            if (reAcquireJoystick || (!directInput.IsDeviceAttached(joystickGuid)))
            {
                if (joystick != null)
                {
                    joystick.Unacquire();
                }

                IList <DeviceInstance> deviceInstance = directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices);
                if (deviceInstance.Count > 0)
                {
                    joystickGuid = deviceInstance[0].InstanceGuid;
                }
                else
                {
                    // don't check for another second
                    skipCheck = 100;
                    return;
                }

                reAcquireJoystick = false;

                // Instantiate the joystick
                joystick = new Joystick(directInput, joystickGuid);

                // Set BufferSize in order to use buffered data.
                joystick.Properties.BufferSize = 128;

                // Acquire the joystick
                joystick.Acquire();

                // allow values to stabilize before using them!
                skipCount = 50;
            }

            float tmp;

            JoystickUpdate[] datas = null;
            try
            {
                joystick.Poll();
                datas = joystick.GetBufferedData();
            }
            catch (Exception)
            {
                reAcquireJoystick = true;
                joystick.Unacquire();
                joystick = null;
            }

            if (skipCount-- > 0)
            {
                return;
            }

            if (datas == null)
            {
                return;
            }

            foreach (JoystickUpdate state in datas)
            {
                if (state.Offset == JoystickOffset.X)
                {
                    guageXAxis.Value = tmp = state.Value / 65535.0f;
                    conductor.Set((string)comboBoxXAxis.SelectedItem, checkBoxInvertXAxis.Checked? (1.0f - tmp) : tmp);
                }
                if (state.Offset == JoystickOffset.Y)
                {
                    guageYAxis.Value = tmp = state.Value / 65535.0f;
                    conductor.Set((string)comboBoxYAxis.SelectedItem, checkBoxInvertYAxis.Checked ? (1.0f - tmp) : tmp);
                }
                if (state.Offset == JoystickOffset.Sliders0)
                {
                    guageRudder.Value = tmp = state.Value / 65535.0f;
                    conductor.Set((string)comboBoxRudder.SelectedItem, checkBoxInvertRudder.Checked ? (1.0f - tmp) : tmp);
                }
                if (state.Offset == JoystickOffset.Z)
                {
                    guageThrottle.Value = tmp = state.Value / 65535.0f;
                    conductor.Set((string)comboBoxThrottle.SelectedItem, checkBoxInvertThrottle.Checked ? (1.0f - tmp) : tmp);
                }
                if (state.Offset == JoystickOffset.RotationZ)
                {
                    guageTwist.Value = tmp = state.Value / 65535.0f;
                    conductor.Set((string)comboBoxTwist.SelectedItem, checkBoxInvertTwist.Checked ? (1.0f - tmp) : tmp);
                }
                if (state.Offset == JoystickOffset.RotationX)
                {
                    guageRotation.Value = tmp = state.Value / 65535.0f;
                    conductor.Set((string)comboBoxRotation.SelectedItem, checkBoxInvertRotation.Checked ? (1.0f - tmp) : tmp);
                }
                if (state.Offset == JoystickOffset.Buttons0)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[0])
                        {
                            conductor.Set((string)comboBox0.SelectedItem, true);
                            buttons[0] = true;
                        }

                        value0.Text      = "1";
                        value0.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[0])
                        {
                            if (checkBoxRevert0.Checked)
                            {
                                conductor.Set((string)comboBox0.SelectedItem, false);
                            }
                            buttons[0] = false;
                        }
                        value0.Text      = "0";
                        value0.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons1)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[1])
                        {
                            conductor.Set((string)comboBox1.SelectedItem, true);
                            buttons[1] = true;
                        }

                        value1.Text      = "1";
                        value1.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[1])
                        {
                            if (checkBoxRevert1.Checked)
                            {
                                conductor.Set((string)comboBox1.SelectedItem, false);
                            }
                            buttons[1] = false;
                        }
                        value1.Text      = "0";
                        value1.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons2)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[2])
                        {
                            conductor.Set((string)comboBox2.SelectedItem, true);
                            buttons[2] = true;
                        }

                        value2.Text      = "1";
                        value2.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[2])
                        {
                            if (checkBoxRevert2.Checked)
                            {
                                conductor.Set((string)comboBox2.SelectedItem, false);
                            }
                            buttons[2] = false;
                        }
                        value2.Text      = "0";
                        value2.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons3)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[3])
                        {
                            conductor.Set((string)comboBox3.SelectedItem, true);
                            buttons[3] = true;
                        }

                        value3.Text      = "1";
                        value3.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[3])
                        {
                            if (checkBoxRevert3.Checked)
                            {
                                conductor.Set((string)comboBox3.SelectedItem, false);
                            }
                            buttons[3] = false;
                        }
                        value3.Text      = "0";
                        value3.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons4)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[4])
                        {
                            conductor.Set((string)comboBox4.SelectedItem, true);
                            buttons[4] = true;
                        }

                        value4.Text      = "1";
                        value4.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[4])
                        {
                            if (checkBoxRevert4.Checked)
                            {
                                conductor.Set((string)comboBox4.SelectedItem, false);
                            }
                            buttons[4] = false;
                        }
                        value4.Text      = "0";
                        value4.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons5)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[5])
                        {
                            conductor.Set((string)comboBox5.SelectedItem, true);
                            buttons[5] = true;
                        }

                        value5.Text      = "1";
                        value5.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[5])
                        {
                            if (checkBoxRevert5.Checked)
                            {
                                conductor.Set((string)comboBox5.SelectedItem, false);
                            }
                            buttons[5] = false;
                        }
                        value5.Text      = "0";
                        value5.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons6)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[6])
                        {
                            conductor.Set((string)comboBox6.SelectedItem, true);
                            buttons[6] = true;
                        }

                        value6.Text      = "1";
                        value6.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[6])
                        {
                            if (checkBoxRevert6.Checked)
                            {
                                conductor.Set((string)comboBox6.SelectedItem, false);
                            }
                            buttons[6] = false;
                        }
                        value6.Text      = "0";
                        value6.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons7)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[7])
                        {
                            conductor.Set((string)comboBox7.SelectedItem, true);
                            buttons[7] = true;
                        }

                        value7.Text      = "1";
                        value7.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[7])
                        {
                            if (checkBoxRevert7.Checked)
                            {
                                conductor.Set((string)comboBox7.SelectedItem, false);
                            }
                            buttons[7] = false;
                        }
                        value7.Text      = "0";
                        value7.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons8)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[8])
                        {
                            conductor.Set((string)comboBox8.SelectedItem, true);
                            buttons[8] = true;
                        }

                        value8.Text      = "1";
                        value8.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[8])
                        {
                            if (checkBoxRevert8.Checked)
                            {
                                conductor.Set((string)comboBox8.SelectedItem, false);
                            }
                            buttons[8] = false;
                        }
                        value8.Text      = "0";
                        value8.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons9)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[9])
                        {
                            conductor.Set((string)comboBox9.SelectedItem, true);
                            buttons[9] = true;
                        }

                        value9.Text      = "1";
                        value9.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[9])
                        {
                            if (checkBoxRevert9.Checked)
                            {
                                conductor.Set((string)comboBox9.SelectedItem, false);
                            }
                            buttons[9] = false;
                        }
                        value9.Text      = "0";
                        value9.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons10)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[10])
                        {
                            conductor.Set((string)comboBox10.SelectedItem, true);
                            buttons[10] = true;
                        }

                        value10.Text      = "1";
                        value10.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[10])
                        {
                            if (checkBoxRevert10.Checked)
                            {
                                conductor.Set((string)comboBox10.SelectedItem, false);
                            }
                            buttons[10] = false;
                        }
                        value10.Text      = "0";
                        value10.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons11)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[11])
                        {
                            conductor.Set((string)comboBox11.SelectedItem, true);
                            buttons[11] = true;
                        }

                        value11.Text      = "1";
                        value11.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[11])
                        {
                            if (checkBoxRevert11.Checked)
                            {
                                conductor.Set((string)comboBox11.SelectedItem, false);
                            }
                            buttons[11] = false;
                        }
                        value11.Text      = "0";
                        value11.BackColor = Color.Red;
                    }
                }
                if (state.Offset == JoystickOffset.Buttons12)
                {
                    if (state.Value != 0)
                    {
                        if (!buttons[12])
                        {
                            conductor.Set((string)comboBox12.SelectedItem, true);
                            buttons[12] = true;
                        }

                        value12.Text      = "1";
                        value12.BackColor = Color.Green;
                    }
                    else
                    {
                        if (buttons[12])
                        {
                            if (checkBoxRevert12.Checked)
                            {
                                conductor.Set((string)comboBox12.SelectedItem, false);
                            }
                            buttons[12] = false;
                        }
                        value12.Text      = "0";
                        value12.BackColor = Color.Red;
                    }
                }
            }
        }
示例#14
0
        public List <ControllerDevice> detectControllers()
        {
            deviceCount = getDeviceCount()[0];
            int allDeviceCount = getDeviceCount()[1];

            Console.WriteLine("Detected {0} attached controllers out of {1} controllers in total.", deviceCount, allDeviceCount);

            for (int i = 0; i < devices.Count(); i++) //Remove disconnected controllers
            {
                if (devices[i] != null && !directInput.IsDeviceAttached(devices[i].joystick.Information.InstanceGuid))
                {
                    Console.WriteLine("{0} removed.", devices[i].joystick.Properties.InstanceName);
                    devices[i] = null;
                    if (i < workers.Count())
                    {
                        workers[i].Abort();
                        workers[i] = null;
                        Unplug(i + 1);
                    }
                }
            }

            Resize(ref devices, deviceCount);

            int skip = 0;

            foreach (var deviceInstance in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                Joystick joystick = new Joystick(directInput, deviceInstance.InstanceGuid);

                if (joystick.Information.ProductGuid.ToString() == "028e045e-0000-0000-0000-504944564944") //If its an emulated controller skip it
                {
                    skip++;
                    continue;
                }

                if (joystick.Capabilities.ButtonCount < 1 && joystick.Capabilities.AxesCount < 1) //Skip if it doesn't have any button and axes
                {
                    skip++;
                    continue;
                }

                int spot = -1;
                for (int i = 0; i < deviceCount; i++)
                {
                    if (devices[i] == null)
                    {
                        if (spot == -1)
                        {
                            spot = i;
                            Console.WriteLine("Slot {0} is empty.", i);
                        }
                    }
                    else if (devices[i] != null && devices[i].joystick.Information.InstanceGuid == deviceInstance.InstanceGuid) //If the device is already initialized skip it
                    {
                        Console.WriteLine("Device {0} in Slot {1} already aquired. ", deviceInstance.InstanceName, i);
                        spot = -1;
                        break;
                    }
                }

                if (spot == -1)
                {
                    continue;
                }

                devices[spot] = new ControllerDevice(joystick, spot + 1);
                Console.WriteLine("Device {0} assigned to slot {1}.", devices[spot].name, spot);

                joystick.Properties.BufferSize = 128;
                joystick.Acquire();

                /*if (IsActive)       // seems unnecessary
                 * {
                 *  processingData[spot] = new ContData();
                 *  Console.WriteLine("Plug " + spot);
                 *  Plugin(spot + 1);
                 *  int t = spot;
                 *  workers[spot] = new Thread(() =>
                 *  { ProcessData(t); });
                 *  workers[spot].Start();
                 * }*/
            }
            Console.WriteLine("Skipped {0} device(s).", skip);
            return(devices);
        }