Exemplo n.º 1
0
        public void Initialize(Settings settings)
        {
            // Initialize first. We are outputing to the console set up in this API
            eliteAPI = new EliteDangerousAPI();
            //eliteAPI.Logger.UseConsole(Severity.Info);

            foreach (var device in settings.Devices)
            {
                var ffDevice = new ForceFeedbackController();
                if (ffDevice.Initialize(
                        device.ProductGuid,
                        device.ProductName,
                        @".\Forces",
                        device.AutoCenter,
                        device.ForceFeedbackGain) == false)
                {
                    continue;
                }

                var deviceEvents = new DeviceEvents
                {
                    EventSettings = device.StatusEvents.ToDictionary(v => v.Event, v => v),
                    Device        = ffDevice
                };

                Devices.Add(deviceEvents);
            }

            eliteAPI.Start();

            eliteAPI.Events.AllEvent += Events_AllEvent;
        }
        /// <summary>
        /// Initialize the device
        /// </summary>
        /// <returns>Returns true if initializng was successfully</returns>
        internal bool Init()
        {
            //buttons
            Button[] buttons = new Button[2];
            buttons[0] = new Button(JoystickButtons.Button1, 0);
            buttons[1] = new Button(JoystickButtons.Button2, 1);

            //axes
            Axis[] axes = new Axis[1];
            axes[0] = new JoystickInputDevice.Axis(JoystickAxes.X, new Range(-1, 1), false);

            //povs
            POV[] povs = new POV[0];
            //povs[ 0 ] = new JoystickInputDevice.POV( JoystickPOVs.POV1 );

            //sliders
            Slider[] sliders = new Slider[0];
            //sliders[ 0 ] = new Slider( JoystickSliders.Slider1 );

            //forceFeedbackController
            ForceFeedbackController forceFeedbackController = null;

            //initialize data
            InitDeviceData(buttons, axes, povs, sliders, forceFeedbackController);

            return(true);
        }
Exemplo n.º 3
0
        //

        public unsafe DirectInputForceFeedbackRampEffect(ForceFeedbackController owner,
                                                         JoystickAxes[] axes)
            : base(owner, axes)
        {
        }
Exemplo n.º 4
0
        //

        public unsafe DirectInputForceFeedbackPeriodicEffect(ForceFeedbackController owner,
                                                             ForceFeedbackEffectTypes effectType, JoystickAxes[] axes)
            : base(owner, effectType, axes)
        {
        }
        internal unsafe bool Init()
        {
            GUID devGuid = deviceGuid;

            void */*IDirectInputDevice8*/ directInputDeviceTemp = null;

            int hr = IDirectInput.CreateDevice(
                WindowsInputDeviceManager.Instance.DirectInput,
                ref devGuid, out directInputDeviceTemp, null);

            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputJoystickDevice: Cannot create device \"{0}\" ({1}).",
                            Name, DInput.GetOutString(DInput.DXGetErrorStringW(hr)));
                return(false);
            }

            directInputDevice = (IDirectInputDevice8 *)directInputDeviceTemp;

            // get capabilities

            DIDEVCAPS caps = new DIDEVCAPS();

            caps.dwSize = (uint)sizeof(DIDEVCAPS);

            hr = IDirectInputDevice8.GetCapabilities(directInputDevice, ref caps);
            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputJoystickDevice: Cannot get device capabilities \"{0}\".", Name);
                return(false);
            }

            //buttons
            Button[] buttons = new Button[caps.dwButtons];
            for (int n = 0; n < buttons.Length; n++)
            {
                buttons[n] = new Button((JoystickButtons)n, n);
            }

            //povs
            POV[] povs = new POV[caps.dwPOVs];
            for (int n = 0; n < povs.Length; n++)
            {
                povs[n] = new JoystickInputDevice.POV((JoystickPOVs)n);
            }

            // setup

            hr = IDirectInputDevice8.SetDataFormat(directInputDevice, DInput.Get_c_dfDIJoystick2());

            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputJoystickDevice: Cannot set device data format \"{0}\".", Name);
                return(false);
            }

            hr = IDirectInputDevice8.SetCooperativeLevel(directInputDevice,
                                                         WindowsInputDeviceManager.Instance.WindowHandle,
                                                         DInput.DISCL_EXCLUSIVE | DInput.DISCL_FOREGROUND);

            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputJoystickDevice: Cannot set device " +
                            "cooperative level \"{0}\".", Name);
                return(false);
            }

            //-------------------------------------------------------------------
            // setup size for buffered input

            DIPROPDWORD dibuf = new DIPROPDWORD();

            dibuf.diph.dwSize       = (uint)sizeof(DIPROPDWORD);
            dibuf.diph.dwHeaderSize = (uint)sizeof(DIPROPHEADER);
            dibuf.diph.dwHow        = DInput.DIPH_DEVICE;
            dibuf.diph.dwObj        = 0;
            dibuf.dwData            = BufferSize;

            GUID *bufferSizeGuid = (GUID *)DInput.getDIPROP_BUFFERSIZE();

            hr = IDirectInputDevice8.SetProperty(directInputDevice, bufferSizeGuid, ref dibuf.diph);
            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputJoystickDevice: Cannot set device buffer size \"{0}\".",
                            Name);
                return(false);
            }

            deviceDataBuffer = NativeUtility.Alloc(NativeUtility.MemoryAllocationType.Utility,
                                                   sizeof(DIDEVICEOBJECTDATA) * BufferSize);

            //--------------------------------------------------------------------

            temporarySliderCount = 0;

            temporaryAxisList = new List <JoystickInputDevice.Axis>();

            tempDeviceForEnumerate = this;
            hr = IDirectInputDevice8.EnumObjects(directInputDevice, EnumDeviceObjectsHandler,
                                                 null, DInput.DIDFT_ALL);
            tempDeviceForEnumerate = null;

            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputJoystickDevice: Cannot enumerate device objects \"{0}\".",
                            Name);
                return(false);
            }

            //axes
            Axis[] axes = temporaryAxisList.ToArray();
            temporaryAxisList = null;

            //sliders
            Slider[] sliders = new Slider[temporarySliderCount];
            for (int n = 0; n < sliders.Length; n++)
            {
                sliders[n] = new JoystickInputDevice.Slider((JoystickSliders)n);
            }

            //forceFeedbackController
            ForceFeedbackController forceFeedbackController = null;

            if ((caps.dwFlags & DInput.DIDC_FORCEFEEDBACK) != 0)
            {
                forceFeedbackController = new DirectInputForceFeedbackController(directInputDevice, this);
            }

            //initialize data
            InitDeviceData(buttons, axes, povs, sliders, forceFeedbackController);

            return(true);
        }
        unsafe protected override void OnUpdateState()
        {
            int hr;

            // gain access to device
            hr = IDirectInputDevice8.Poll(directInputDevice);
            if (Wrapper.FAILED(hr))
            {
                if (hr == DInput.GetHRESULT_DIERR_INPUTLOST())
                {
                    if (!IsDeviceLost())
                    {
                        DeviceLost();
                        return;
                    }
                }

                hr = IDirectInputDevice8.Acquire(directInputDevice);
                while (hr == DInput.GetHRESULT_DIERR_INPUTLOST())
                {
                    hr = IDirectInputDevice8.Acquire(directInputDevice);
                }
                IDirectInputDevice8.Poll(directInputDevice);
            }
            else
            {
                if (IsDeviceLost())
                {
                    DeviceRestore();
                }
            }

            // get data

            uint entries = BufferSize;
            DIDEVICEOBJECTDATA *entryPtr = (DIDEVICEOBJECTDATA *)deviceDataBuffer;

            hr = IDirectInputDevice8.GetDeviceData(directInputDevice,
                                                   (uint)sizeof(DIDEVICEOBJECTDATA), entryPtr, ref entries, 0);
            if (Wrapper.FAILED(hr))
            {
                //Log.Info( "Cannot get device data for '" + Name + "' error = " +
                //   DInput.GetOutString( DInput.DXGetErrorStringW( hr ) ) );
                return;
            }

            // process data

            for (int k = 0; k < entries; k++)
            {
                switch (entryPtr->dwOfs)
                {
                case DInput.DIJOFS_X:
                case DInput.DIJOFS_Y:
                case DInput.DIJOFS_Z:
                case DInput.DIJOFS_RX:
                case DInput.DIJOFS_RY:
                case DInput.DIJOFS_RZ:
                {
                    JoystickAxes axisName = JoystickAxes.X;
                    switch (entryPtr->dwOfs)
                    {
                    case DInput.DIJOFS_X: axisName = JoystickAxes.X; break;

                    case DInput.DIJOFS_Y: axisName = JoystickAxes.Y; break;

                    case DInput.DIJOFS_Z: axisName = JoystickAxes.Z; break;

                    case DInput.DIJOFS_RX: axisName = JoystickAxes.Rx; break;

                    case DInput.DIJOFS_RY: axisName = JoystickAxes.Ry; break;

                    case DInput.DIJOFS_RZ: axisName = JoystickAxes.Rz; break;
                    }

                    Axis axis = GetAxisByName(axisName);

                    float value = (float)((int)entryPtr->dwData) / MaxRange;

                    //invert value for specific axes
                    if (axis.Name == JoystickAxes.Y || axis.Name == JoystickAxes.Ry ||
                        axis.Name == JoystickAxes.Rz)
                    {
                        value = -value;
                    }

                    axis.Value = value;

                    InputDeviceManager.Instance.SendEvent(new JoystickAxisChangedEvent(this, axis));
                }
                break;

                case DInput.DIJOFS_SLIDER00:
                {
                    Vector2F value = Sliders[0].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[0].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[0], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER01:
                {
                    Vector2F value = Sliders[0].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[0].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[0], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_SLIDER10:
                {
                    Vector2F value = Sliders[1].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[1].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[1], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER11:
                {
                    Vector2F value = Sliders[1].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[1].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[1], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_SLIDER20:
                {
                    Vector2F value = Sliders[2].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[2].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[2], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER21:
                {
                    Vector2F value = Sliders[2].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[2].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[2], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_SLIDER30:
                {
                    Vector2F value = Sliders[3].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[3].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[3], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER31:
                {
                    Vector2F value = Sliders[3].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[3].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[3], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_POV0:
                    UpdatePOV(0, entryPtr->dwData);
                    break;

                case DInput.DIJOFS_POV1:
                    UpdatePOV(1, entryPtr->dwData);
                    break;

                case DInput.DIJOFS_POV2:
                    UpdatePOV(2, entryPtr->dwData);
                    break;

                case DInput.DIJOFS_POV3:
                    UpdatePOV(3, entryPtr->dwData);
                    break;

                default:
                    if (entryPtr->dwOfs >= DInput.DIJOFS_BUTTON0 && entryPtr->dwOfs < DInput.DIJOFS_BUTTON128)
                    {
                        int  buttonIndex = (int)(entryPtr->dwOfs - DInput.DIJOFS_BUTTON0);
                        bool pressed     = ((entryPtr->dwData & 0x80) != 0);

                        Button button = Buttons[buttonIndex];
                        if (button.Pressed != pressed)
                        {
                            button.Pressed = pressed;

                            if (pressed)
                            {
                                InputDeviceManager.Instance.SendEvent(
                                    new JoystickButtonDownEvent(this, button));
                            }
                            else
                            {
                                InputDeviceManager.Instance.SendEvent(
                                    new JoystickButtonUpEvent(this, button));
                            }
                        }
                    }
                    break;
                }

                entryPtr++;
            }

            //update states for effects. effects can be destroyed inside OnUpdateState().
            if (ForceFeedbackController != null)
            {
                ForceFeedbackController.OnUpdateState();
            }
        }