コード例 #1
0
ファイル: AppHelper.cs プロジェクト: akemimadoka/x360ce
        public static DeviceEffectItem[] GetDeviceEffects(Joystick device)
        {
            var items = new List <DeviceEffectItem>();

            if (device == null)
            {
                return(items.ToArray());
            }
            // Check if device supports force feedback.
            var forceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback);

            if (!forceFeedback)
            {
                return(items.ToArray());
            }
            lock (XInput.XInputLock)
            {
                // Unload xinput.
                var isLoaded = XInput.IsLoaded;
                if (isLoaded)
                {
                    XInput.FreeLibrary();
                }
                // Must reaquire device in exclusive mode to get effects.
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current.Handle, CooperativeLevel.Foreground | CooperativeLevel.Exclusive);
                IList <EffectInfo> effects = new List <EffectInfo>();
                try
                {
                    device.Acquire();
                    effects = device.GetEffects(EffectType.All);
                }
                catch (Exception)
                {
                }
                foreach (var eff in effects)
                {
                    items.Add(new DeviceEffectItem()
                    {
                        Name              = eff.Name,
                        StaticParameters  = eff.StaticParameters,
                        DynamicParameters = eff.DynamicParameters,
                    });
                }
                // Reaquire device in non exclusive mode.
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current.Handle, CooperativeLevel.Background | CooperativeLevel.NonExclusive);
                // If XInput was loaded then...
                if (isLoaded)
                {
                    Exception error;
                    XInput.ReLoadLibrary(XInput.LibraryName, out error);
                }
            }
            return(items.ToArray());
        }
コード例 #2
0
        void ShowDeviceInfo(Joystick device)
        {
            if (device == null)
            {
                // clean everything here.
                SetValue(DeviceProductNameTextBox, "");
                SetValue(DeviceProductGuidTextBox, "");
                SetValue(DeviceInstanceGuidTextBox, "");
                DiCapFfStateTextBox.Text = string.Empty;
                DiCapAxesTextBox.Text    = string.Empty;
                DiCapButtonsTextBox.Text = string.Empty;
                DiCapDPadsTextBox.Text   = string.Empty;
                DiEffectsTable.Rows.Clear();
                return;
            }
            lock (MainForm.XInputLock)
            {
                var isLoaded = XInput.IsLoaded;
                if (isLoaded)
                {
                    XInput.FreeLibrary();
                }
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current, CooperativeLevel.Foreground | CooperativeLevel.Exclusive);
                effects = new List <EffectInfo>();
                try
                {
                    device.Acquire();
                    var forceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback);
                    forceFeedbackState = forceFeedback ? "YES" : "NO";
                    effects            = device.GetEffects(EffectType.All);
                }
                catch (Exception)
                {
                    forceFeedbackState = "ERROR";
                }
                DiEffectsTable.Rows.Clear();
                foreach (var eff in effects)
                {
                    DiEffectsTable.Rows.Add(new object[] {
                        eff.Name,
                        ((EffectParameterFlags)eff.StaticParameters).ToString(),
                        ((EffectParameterFlags)eff.DynamicParameters).ToString()
                    });
                }
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current, CooperativeLevel.Background | CooperativeLevel.NonExclusive);
                if (isLoaded)
                {
                    Exception error;
                    XInput.ReLoadLibrary(XInput.LibraryName, out error);
                }
            }
            DiCapFfStateTextBox.Text = forceFeedbackState;
            DiCapButtonsTextBox.Text = device.Capabilities.ButtonCount.ToString();
            DiCapDPadsTextBox.Text   = device.Capabilities.PovCount.ToString();
            var objects = device.GetObjects(DeviceObjectTypeFlags.All).OrderBy(x => x.Offset).ToArray();

            DiObjectsTable.Rows.Clear();
            var           og         = typeof(SharpDX.DirectInput.ObjectGuid);
            var           guidFileds = og.GetFields().Where(x => x.FieldType == typeof(Guid));
            List <Guid>   guids      = guidFileds.Select(x => (Guid)x.GetValue(og)).ToList();
            List <string> names      = guidFileds.Select(x => x.Name).ToList();

            foreach (var o in objects)
            {
                DiObjectsTable.Rows.Add(new object[] {
                    o.Offset,
                    o.ObjectId.InstanceNumber,
                    o.Usage,
                    o.Name,
                    o.Aspect,
                    guids.Contains(o.ObjectType) ?  names[guids.IndexOf(o.ObjectType)] : o.ObjectType.ToString(),
                    o.ObjectId.Flags,
                });
            }
            var actuators = objects.Where(x => x.ObjectId.Flags.HasFlag(DeviceObjectTypeFlags.ForceFeedbackActuator));

            ActuatorsTextBox.Text = actuators.Count().ToString();
            var di           = device.Information;
            var slidersCount = objects.Where(x => x.ObjectType.Equals(SharpDX.DirectInput.ObjectGuid.Slider)).Count();

            DiCapAxesTextBox.Text = (device.Capabilities.AxeCount - slidersCount).ToString();
            SlidersTextBox.Text   = slidersCount.ToString();
            // Update pid and vid always so they wont be overwritten by load settings.
            short vid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 0);
            short pid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 2);

            SetValue(DeviceVidTextBox, "0x{0}", vid.ToString("X4"));
            SetValue(DevicePidTextBox, "0x{0}", pid.ToString("X4"));
            SetValue(DeviceProductNameTextBox, di.ProductName);
            SetValue(DeviceProductGuidTextBox, di.ProductGuid.ToString());
            SetValue(DeviceInstanceGuidTextBox, di.InstanceGuid.ToString());
            SetValue(DeviceTypeTextBox, di.Type.ToString());
        }
コード例 #3
0
ファイル: DirectInputControl.cs プロジェクト: roget/x360ce
        void ShowDeviceInfo(Joystick device, DeviceInfo dInfo)
        {
            if (device == null)
            {
                // clean everything here.
                SetValue(DeviceProductNameTextBox, "");
                SetValue(DeviceVendorNameTextBox, "");
                SetValue(DeviceProductGuidTextBox, "");
                SetValue(DeviceInstanceGuidTextBox, "");
                DiCapFfStateTextBox.Text = string.Empty;
                DiCapAxesTextBox.Text    = string.Empty;
                DiCapButtonsTextBox.Text = string.Empty;
                DiCapDPadsTextBox.Text   = string.Empty;
                DiEffectsTable.Rows.Clear();
                return;
            }
            lock (MainForm.XInputLock)
            {
                var isLoaded = XInput.IsLoaded;
                if (isLoaded)
                {
                    XInput.FreeLibrary();
                }
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current, CooperativeLevel.Foreground | CooperativeLevel.Exclusive);
                effects = new List <EffectInfo>();
                try
                {
                    device.Acquire();
                    var forceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback);
                    forceFeedbackState = forceFeedback ? "YES" : "NO";
                    effects            = device.GetEffects(EffectType.All);
                }
                catch (Exception)
                {
                    forceFeedbackState = "ERROR";
                }
                DiEffectsTable.Rows.Clear();
                foreach (var eff in effects)
                {
                    DiEffectsTable.Rows.Add(new object[] {
                        eff.Name,
                        ((EffectParameterFlags)eff.StaticParameters).ToString(),
                        ((EffectParameterFlags)eff.DynamicParameters).ToString()
                    });
                }
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current, CooperativeLevel.Background | CooperativeLevel.NonExclusive);
                if (isLoaded)
                {
                    Exception error;
                    XInput.ReLoadLibrary(XInput.LibraryName, out error);
                }
            }
            DiCapFfStateTextBox.Text = forceFeedbackState;
            DiCapButtonsTextBox.Text = device.Capabilities.ButtonCount.ToString();
            DiCapDPadsTextBox.Text   = device.Capabilities.PovCount.ToString();
            var objects = AppHelper.GetDeviceObjects(device);

            DiObjectsDataGridView.DataSource = objects;
            var actuators = objects.Where(x => x.Flags.HasFlag(DeviceObjectTypeFlags.ForceFeedbackActuator));

            ActuatorsTextBox.Text = actuators.Count().ToString();
            var di           = device.Information;
            var slidersCount = objects.Where(x => x.GuidValue.Equals(SharpDX.DirectInput.ObjectGuid.Slider)).Count();

            DiCapAxesTextBox.Text = (device.Capabilities.AxeCount - slidersCount).ToString();
            SlidersTextBox.Text   = slidersCount.ToString();
            // Update PID and VID always so they wont be overwritten by load settings.
            short vid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 0);
            short pid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 2);

            SetValue(DeviceVidTextBox, "0x{0}", vid.ToString("X4"));
            SetValue(DevicePidTextBox, "0x{0}", pid.ToString("X4"));
            SetValue(DeviceProductNameTextBox, di.ProductName);
            SetValue(DeviceVendorNameTextBox, dInfo == null ? "" : dInfo.Manufacturer);
            SetValue(DeviceProductGuidTextBox, di.ProductGuid.ToString());
            SetValue(DeviceInstanceGuidTextBox, di.InstanceGuid.ToString());
            SetValue(DeviceTypeTextBox, di.Type.ToString());
        }
コード例 #4
0
        void ShowDeviceInfo(Joystick device, UserDevice dInfo)
        {
            if (device == null)
            {
                // clean everything here.
                AppHelper.SetText(DiCapFfStateTextBox, "");
                AppHelper.SetText(DiCapButtonsTextBox, "");
                AppHelper.SetText(DiCapPovsTextBox, "");
                AppHelper.SetText(ActuatorsTextBox, "");
                AppHelper.SetText(DiCapAxesTextBox, "");
                AppHelper.SetText(DiSlidersTextBox, "");
                AppHelper.SetText(DeviceVidTextBox, "");
                AppHelper.SetText(DevicePidTextBox, "");
                AppHelper.SetText(DeviceRevTextBox, "");
                AppHelper.SetText(DeviceProductNameTextBox, "");
                AppHelper.SetText(DeviceVendorNameTextBox, "");
                AppHelper.SetText(DeviceProductGuidTextBox, "");
                AppHelper.SetText(DeviceInstanceGuidTextBox, "");
                AppHelper.SetText(DeviceTypeTextBox, "");
                if (DiEffectsTable.Rows.Count > 0)
                {
                    DiEffectsTable.Rows.Clear();
                }
                return;
            }
            lock (XInput.XInputLock)
            {
                // This must be done for the first time device is connected in order to retrieve
                // Force feedback information.
                // XInput must be unloaded in case it tries to lock the device exclusivly.
                var isLoaded = XInput.IsLoaded;
                if (isLoaded)
                {
                    XInput.FreeLibrary();
                }
                var forceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback);
                forceFeedbackState = forceFeedback ? "YES" : "NO";
                if (DiEffectsTable.Rows.Count > 0)
                {
                    DiEffectsTable.Rows.Clear();
                }
                // If device supports force feedback then...
                if (forceFeedback)
                {
                    // Must reaquire device in exclusive mode to get effects.
                    device.Unacquire();
                    device.SetCooperativeLevel(MainForm.Current.Handle, CooperativeLevel.Foreground | CooperativeLevel.Exclusive);
                    IList <EffectInfo> effects = new List <EffectInfo>();
                    try
                    {
                        device.Acquire();
                        effects = device.GetEffects(EffectType.All);
                    }
                    catch (Exception)
                    {
                        forceFeedbackState = "ERROR";
                    }
                    foreach (var eff in effects)
                    {
                        DiEffectsTable.Rows.Add(
                            eff.Name,
                            eff.StaticParameters.ToString(),
                            eff.DynamicParameters.ToString()
                            );
                    }
                }
                // Reaquire device in non exclusive mode.
                device.Unacquire();
                device.SetCooperativeLevel(MainForm.Current.Handle, CooperativeLevel.Background | CooperativeLevel.NonExclusive);
                if (isLoaded)
                {
                    Exception error;
                    XInput.ReLoadLibrary(XInput.LibraryName, out error);
                }
            }
            AppHelper.SetText(DiCapFfStateTextBox, forceFeedbackState);
            AppHelper.SetText(DiCapButtonsTextBox, device.Capabilities.ButtonCount.ToString());
            AppHelper.SetText(DiCapPovsTextBox, device.Capabilities.PovCount.ToString());
            var di      = device.Information;
            var objects = TestDeviceHelper.ProductGuid.Equals(di.ProductGuid)
                                ? TestDeviceHelper.GetDeviceObjects()
                                : AppHelper.GetDeviceObjects(device);

            DiObjectsDataGridView.DataSource = objects;
            var actuators = objects.Where(x => x.Flags.HasFlag(DeviceObjectTypeFlags.ForceFeedbackActuator));

            AppHelper.SetText(ActuatorsTextBox, actuators.Count().ToString());
            var slidersCount = objects.Where(x => x.Type.Equals(SharpDX.DirectInput.ObjectGuid.Slider)).Count();

            // https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.dijoystate2(v=vs.85).aspx
            AppHelper.SetText(DiCapAxesTextBox, (device.Capabilities.AxeCount - slidersCount).ToString());
            AppHelper.SetText(DiSlidersTextBox, slidersCount.ToString());
            // Update PID and VID always so they wont be overwritten by load settings.
            short vid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 0);
            short pid = BitConverter.ToInt16(di.ProductGuid.ToByteArray(), 2);

            AppHelper.SetText(DeviceVidTextBox, "0x{0:X4}", vid);
            AppHelper.SetText(DevicePidTextBox, "0x{0:X4}", pid);
            AppHelper.SetText(DeviceProductNameTextBox, di.ProductName);
            AppHelper.SetText(DeviceVendorNameTextBox, dInfo == null ? "" : dInfo.DevManufacturer);
            AppHelper.SetText(DeviceRevTextBox, "0x{0:X4}", dInfo == null ? 0 : dInfo.DevRevision);
            AppHelper.SetText(DeviceProductGuidTextBox, di.ProductGuid.ToString());
            AppHelper.SetText(DeviceInstanceGuidTextBox, di.InstanceGuid.ToString());
            AppHelper.SetText(DeviceTypeTextBox, di.Type.ToString());
        }