Exemplo n.º 1
0
        public unsafe override bool SetEnableAutocenter(bool enable)
        {
            IDirectInputDevice8 *directInputDevice =
                ((DirectInputJoystickInputDevice)Device).directInputDevice;

            DIPROPDWORD dipdw = new DIPROPDWORD();

            dipdw.diph.dwSize       = (uint)sizeof(DIPROPDWORD);
            dipdw.diph.dwHeaderSize = (uint)sizeof(DIPROPHEADER);
            dipdw.diph.dwObj        = 0;
            dipdw.diph.dwHow        = DInput.DIPH_DEVICE;
            dipdw.dwData            = (uint)((enable) ? 1 : 0);

            GUID *centerProp = (GUID *)DInput.getDIPROP_AUTOCENTER();
            int   hr         = IDirectInputDevice8.SetProperty(directInputDevice, centerProp, ref dipdw.diph);

            if (Wrapper.FAILED(hr))
            {
                Log.Warning("DirectInputForceFeedbackController: " +
                            "Cannot change autocenter property for \"{0}\".", Device.Name);
                return(false);
            }

            return(true);
        }
        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);
        }