/// <summary>
        /// ctor and init
        /// </summary>
        /// <param name="device">A DXInput device</param>
        /// <param name="hwnd">The WinHandle of the main window</param>
        /// <param name="joystickNum">The 0.. n-1 Joystick from DX enum</param>
        public DevJoystick(SharpDX.DirectInput.Joystick device, Control hwnd)
        {
            //      log.DebugFormat( "JoystickCls ctor - Entry with {0}", device.Information.ProductName );

            m_device = device;
            m_hwnd   = hwnd;
            // Set BufferSize in order to use buffered data.
            m_device.Properties.BufferSize = 128;

            //      log.Debug( "Get JS Objects" );
            try {
                // Set the data format to the c_dfDIJoystick pre-defined format.
                //m_device.SetDataFormat( DeviceDataFormat.Joystick );
                // Set the cooperative level for the device.
                m_device.SetCooperativeLevel(m_hwnd, CooperativeLevel.NonExclusive | CooperativeLevel.Background);
                // Enumerate all the objects on the device.
                foreach (DeviceObjectInstance d in m_device.GetObjects( ))
                {
                    // For axes that are returned, set the DIPROP_RANGE property for the
                    // enumerated axis in order to scale min/max values.
                    if ((0 != (d.ObjectId.Flags & DeviceObjectTypeFlags.Axis)))
                    {
                        // Set the range for the axis.
                        m_device.Properties.Range = new InputRange(-1000, +1000);
                    }
                }
                StartEventHandling( );
            }
            catch (Exception ex) {
                Console.WriteLine("Get JS Objects failed:\n{0}", ex);
            }
            DevJoystick.RegisteredDevices++;
        }
        /// <summary>
        /// ctor and init
        /// </summary>
        /// <param name="device">A DXInput device</param>
        /// <param name="hwnd">The WinHandle of the main window</param>
        /// <param name="joystickNum">The 0.. n-1 Joystick from DX enum</param>
        /// <param name="panel">The respective JS panel to show the properties</param>
        /// <param name="tabIndex">The Tab index in the GUI</param>
        public JoystickCls(SharpDX.DirectInput.Joystick device, Control hwnd, int joystickNum, UC_JoyPanel panel, int tabIndex)
        {
            log.DebugFormat("JoystickCls ctor - Entry with {0}", device.Information.ProductName);

            m_device         = device;
            m_hwnd           = hwnd;
            m_joystickNumber = joystickNum;     // this remains fixed
            m_xmlInstance    = joystickNum + 1; // initial assignment (is 1 based..)
            m_jPanel         = panel;
            MyTabPageIndex   = tabIndex;
            Activated_low    = false;

            m_senseLimit = AppConfiguration.AppConfig.jsSenseLimit; // can be changed in the app.config file if it is still too little

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

            m_jPanel.Caption      = m_device.Properties.ProductName;
            m_jPanel.nAxis        = AxisCount.ToString( );
            m_jPanel.nButtons     = ButtonCount.ToString( );
            m_jPanel.nPOVs        = POVCount.ToString( );
            m_jPanel.JsAssignment = 0; // default is no assignment

            m_ignoreButtons = new bool[m_state.Buttons.Length];
            ResetButtons(m_ignoreButtons);

            m_modifierButtons = new bool[m_state.Buttons.Length];
            ResetButtons(m_modifierButtons);

            log.Debug("Get JS Objects");
            try {
                // Set the data format to the c_dfDIJoystick pre-defined format.
                //m_device.SetDataFormat( DeviceDataFormat.Joystick );
                // Set the cooperative level for the device.
                m_device.SetCooperativeLevel(m_hwnd, CooperativeLevel.NonExclusive | CooperativeLevel.Background);
                // Enumerate all the objects on the device.
                foreach (DeviceObjectInstance d in m_device.GetObjects( ))
                {
                    // For axes that are returned, set the DIPROP_RANGE property for the
                    // enumerated axis in order to scale min/max values.
                    if ((0 != (d.ObjectId.Flags & DeviceObjectTypeFlags.Axis)))
                    {
                        // Set the range for the axis.
                        m_device.Properties.Range = new InputRange(-1000, +1000);
                    }
                    // Update the controls to reflect what objects the device supports.
                    UpdateControls(d);
                }
            }
            catch (Exception ex) {
                log.Error("Get JS Objects failed", ex);
            }

            ApplySettings_low( ); // get whatever is needed here from Settings
            JoystickCls.RegisteredDevices++;

            Activated_low = true;
        }
        public InputDeviceJoystickWindows(DirectInput di, DeviceInstance d, bool paxison)
        {
            jsi = new InputDeviceIdentity()
            {
                Instanceguid = d.InstanceGuid, Productguid = d.ProductGuid, Name = d.InstanceName.RemoveTrailingCZeros()
            };

            axisevents = paxison;

            stick = new SharpDX.DirectInput.Joystick(di, d.InstanceGuid);
            stick.SetNotification(eventhandle);
            stick.Acquire();

            axispresent = new bool[AxisCount];
            axisvalue   = Enumerable.Repeat(AxisNullValue, AxisCount).ToArray();

            Capabilities c = stick.Capabilities;

            butstate = new bool[c.ButtonCount];

            povvalue    = Enumerable.Repeat(POVNotPressed, c.PovCount).ToArray();
            slidercount = 0;

            DeviceProperties p = stick.Properties;

            jsi.VendorId  = p.VendorId;
            jsi.ProductId = p.ProductId;

            //   string s = p.PortDisplayName;

            System.Diagnostics.Debug.WriteLine("JOY {0} {1} but {2} pov {3}", jsi.Name, jsi.Productguid, butstate.Length, povvalue.Length);

            foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
            {
                if ((deviceObject.ObjectId.Flags & DeviceObjectTypeFlags.Axis) != 0)
                {
                    System.Guid guid = deviceObject.ObjectType;
                    //System.Diagnostics.Debug.WriteLine("  {0} {1} {2} {3} {4}", jsi.Name, deviceObject.UsagePage, deviceObject.Usage, deviceObject.Offset, guid.ToString());

                    if (guid == ObjectGuid.XAxis)
                    {
                        axispresent[(int)Axis.X] = true;
                    }
                    else if (guid == ObjectGuid.YAxis)
                    {
                        axispresent[(int)Axis.Y] = true;
                    }
                    else if (guid == ObjectGuid.ZAxis)
                    {
                        axispresent[(int)Axis.Z] = true;
                    }
                    else if (guid == ObjectGuid.RxAxis)
                    {
                        axispresent[(int)Axis.RX] = true;
                    }
                    else if (guid == ObjectGuid.RyAxis)
                    {
                        axispresent[(int)Axis.RY] = true;
                    }
                    else if (guid == ObjectGuid.RzAxis)
                    {
                        axispresent[(int)Axis.RZ] = true;
                    }
                    else if (guid == ObjectGuid.Slider)
                    {
                        int axisentry = (int)Axis.U + slidercount;
                        if (axisentry < AxisCount)
                        {
                            axispresent[axisentry] = true;
                            slidercount++;      // must be sliders, only ones left with axis
                            //System.Diagnostics.Debug.WriteLine("Slider " + slidercount);
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Unknown Axis " + guid.ToString());
                    }

                    ObjectProperties o = stick.GetObjectPropertiesById(deviceObject.ObjectId);
                    o.Range = new InputRange(AxisMinRange, AxisMaxRange);
                }
            }
        }
Exemple #4
0
        public bool AcquireJoystick(Guid guid)
        {
            try
            {
                if (_joystick != null)
                {
                    _joystick.Unacquire();
                    _joystick = null;
                }

                var dinput = new DirectInput();
                foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly))
                {
                    if (device.InstanceGuid == guid)
                    {
                        _joystick = new SharpDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    }
                }

                if (_joystick != null)
                {
                    foreach (DeviceObjectInstance deviceObject in _joystick.GetObjects())
                    {
                        //if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        switch (deviceObject.ObjectId.Flags)
                        {
                        case DeviceObjectTypeFlags.Axis:
                        case DeviceObjectTypeFlags.AbsoluteAxis:
                        case DeviceObjectTypeFlags.RelativeAxis:
                            var ir = _joystick.GetObjectPropertiesById(deviceObject.ObjectId);
                            if (ir != null)
                            {
                                try
                                {
                                    ir.Range = new InputRange(-100, 100);
                                }
                                catch (Exception ex)
                                {
                                    MainForm.LogExceptionToFile(ex);
                                }
                            }
                            break;
                        }
                    }

                    _joystick.Acquire();

                    var cps = _joystick.Capabilities;
                    AxisCount = cps.AxeCount;

                    UpdateStatus();
                }
            }
            catch (Exception ex)
            {
                MainForm.LogExceptionToFile(ex);
                return(false);
            }

            return(true);
        }
        /// <summary>Gets a list of the attached joysticks.</summary>
        /// <returns>A list of joysticks.</returns>
        private List<DI.Joystick> GetAttachedJoysticks()
        {
            List<DI.Joystick> joysticks = new List<DI.Joystick>();

            foreach(DI.DeviceInstance device in _directInput.GetDevices(DI.DeviceClass.GameControl, DI.DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    DI.Joystick joystick = new DI.Joystick(_directInput, device.InstanceGuid);

                    joystick.Acquire();

                    IList<DI.DeviceObjectInstance> deviceObjects = joystick.GetObjects();
                    for(int i = 0; i < deviceObjects.Count; i++)
                    {
                        DI.DeviceObjectInstance deviceObjectInstance = deviceObjects[i];

                        if((deviceObjectInstance.ObjectId.Flags & DI.DeviceObjectTypeFlags.Axis) != 0)
                            joystick.GetObjectPropertiesById(deviceObjectInstance.ObjectId).Range = new DI.InputRange(-1000, 1000);
                    }

                    joysticks.Add(joystick);
                }
                catch(SharpDXException)
                {
                }
            }

            return joysticks;
        }
        /// <summary>
        /// Function to retrieve the capabilities from the DirectInput joystick.
        /// </summary>
        /// <param name="joystick">The DirectInput joystick to evaluate.</param>
        public void GetDeviceCaps(DI.Joystick joystick)
        {
            var defaults   = new Dictionary <GamingDeviceAxis, int>(new GorgonGamingDeviceAxisEqualityComparer());
            var axisRanges = new Dictionary <GamingDeviceAxis, GorgonRange>(new GorgonGamingDeviceAxisEqualityComparer());

            ProductID      = joystick.Properties.ProductId;
            ManufacturerID = joystick.Properties.VendorId;
            ButtonCount    = joystick.Capabilities.ButtonCount;
            POVCount       = joystick.Capabilities.PovCount;
            Capabilities   = POVCount > 0 ? GamingDeviceCapabilityFlags.SupportsPOV : GamingDeviceCapabilityFlags.None;

            IList <DI.DeviceObjectInstance> axisInfo = joystick.GetObjects(DI.DeviceObjectTypeFlags.Axis);

            foreach (DI.DeviceObjectInstance axis in axisInfo)
            {
                var usage = (HIDUsage)axis.Usage;
                DI.ObjectProperties properties = joystick.GetObjectPropertiesById(axis.ObjectId);

                // Skip this axis if retrieving the properties results in failure.
                if (properties == null)
                {
                    continue;
                }

                var range    = new GorgonRange(properties.Range.Minimum, properties.Range.Maximum);
                int midPoint = ((range.Range + 1) / 2) + range.Minimum;

                switch (usage)
                {
                case HIDUsage.X:
                    AxisMappings[GamingDeviceAxis.XAxis] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.XAxis]   = range;
                    defaults[GamingDeviceAxis.XAxis]     = range.Minimum < 0 ? 0 : midPoint;
                    break;

                case HIDUsage.Y:
                    AxisMappings[GamingDeviceAxis.YAxis] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.YAxis]   = range;
                    defaults[GamingDeviceAxis.YAxis]     = range.Minimum < 0 ? 0 : midPoint;
                    break;

                case HIDUsage.Slider:
                    AxisMappings[GamingDeviceAxis.Throttle] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.Throttle]   = range;
                    defaults[GamingDeviceAxis.Throttle]     = 0;
                    Capabilities |= GamingDeviceCapabilityFlags.SupportsThrottle;
                    break;

                case HIDUsage.Z:
                    AxisMappings[GamingDeviceAxis.ZAxis] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.ZAxis]   = range;
                    defaults[GamingDeviceAxis.ZAxis]     = range.Minimum < 0 ? 0 : midPoint;
                    Capabilities |= GamingDeviceCapabilityFlags.SupportsZAxis;
                    break;

                case HIDUsage.RelativeX:
                    AxisMappings[GamingDeviceAxis.XAxis2] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.XAxis2]   = range;
                    Capabilities |= GamingDeviceCapabilityFlags.SupportsSecondaryXAxis;
                    defaults[GamingDeviceAxis.XAxis2] = midPoint;
                    break;

                case HIDUsage.RelativeY:
                    AxisMappings[GamingDeviceAxis.YAxis2] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.YAxis2]   = range;
                    Capabilities |= GamingDeviceCapabilityFlags.SupportsSecondaryYAxis;
                    defaults[GamingDeviceAxis.YAxis2] = midPoint;
                    break;

                case HIDUsage.RelativeZ:
                    AxisMappings[GamingDeviceAxis.ZAxis2] = axis.ObjectId;
                    axisRanges[GamingDeviceAxis.ZAxis2]   = range;
                    Capabilities |= GamingDeviceCapabilityFlags.SupportsRudder;
                    defaults[GamingDeviceAxis.ZAxis2] = 0;
                    break;
                }
            }

            AxisInfo = new GorgonGamingDeviceAxisList <GorgonGamingDeviceAxisInfo>(
                axisRanges.Select(item => new GorgonGamingDeviceAxisInfo(item.Key, item.Value, defaults[item.Key])));
        }