//call this on call back when something is beeing plugged in or unplugged void InitializeJoystickIfPossible() { // try to dispose of the old joystick if (m_joystick != null) { m_joystick.Dispose(); m_joystick = null; SetJoystickConnected(false); m_joystickType = null; } if (m_joystick == null) { // Joystick disabled? if (m_joystickInstanceName == null) return; // Try to grab the joystick with the correct instance name var attachedDevices = MyDirectInput.DirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly); foreach (var device in attachedDevices) { if (device.InstanceName != m_joystickInstanceName) continue; try { //device.Type m_joystick = new Joystick(MyDirectInput.DirectInput, device.InstanceGuid); m_joystickType = device.Type; //MethodInfo setCooperativeLevel = typeof(Device).GetMethod("SetCooperativeLevel", // new[] // { // typeof (IntPtr), // typeof (CooperativeLevel) // }); m_joystick.SetCooperativeLevel(m_windowHandle, CooperativeLevel.NonExclusive | CooperativeLevel.Background); //// Workaround for not need to reference System.Windows.Forms //setCooperativeLevel.Invoke(m_joystick, // new object[] // { // m_windowHandle, // CooperativeLevel.NonExclusive | CooperativeLevel.Background // }); break; } catch (SharpDX.SharpDXException) { } } // load and acquire joystick // both joystick and xbox 360 gamepad are treated as joystick device by slimdx if (m_joystick != null) { int sliderCount = 0; m_joystickXAxisSupported = m_joystickYAxisSupported = m_joystickZAxisSupported = false; m_joystickRotationXAxisSupported = m_joystickRotationYAxisSupported = m_joystickRotationZAxisSupported = false; m_joystickSlider1AxisSupported = m_joystickSlider2AxisSupported = false; foreach (DeviceObjectInstance doi in m_joystick.GetObjects()) { if ((doi.ObjectId.Flags & DeviceObjectTypeFlags.Axis) != 0) { // set range 0..65535 for each axis m_joystick.GetObjectPropertiesById(doi.ObjectId).Range = new InputRange(0, 65535); // find out which axes are supported if (doi.ObjectType == ObjectGuid.XAxis) m_joystickXAxisSupported = true; else if (doi.ObjectType == ObjectGuid.YAxis) m_joystickYAxisSupported = true; else if (doi.ObjectType == ObjectGuid.ZAxis) m_joystickZAxisSupported = true; else if (doi.ObjectType == ObjectGuid.RxAxis) m_joystickRotationXAxisSupported = true; else if (doi.ObjectType == ObjectGuid.RyAxis) m_joystickRotationYAxisSupported = true; else if (doi.ObjectType == ObjectGuid.RzAxis) m_joystickRotationZAxisSupported = true; else if (doi.ObjectType == ObjectGuid.Slider) { sliderCount++; if (sliderCount >= 1) m_joystickSlider1AxisSupported = true; if (sliderCount >= 2) m_joystickSlider2AxisSupported = true; } } } // acquire the device try { m_joystick.Acquire(); SetJoystickConnected(true); } catch (SharpDX.SharpDXException) { } } } }
void ShowDeviceInfo(Device device) { if (device == null) { // clean everything here. SetValue(DeviceProductNameTextBox, ""); SetValue(DeviceProductGuidTextBox, ""); SetValue(DeviceInstanceGuidTextBox, ""); DiCapFfLabel.Text = string.Empty; DiCapAxesLabel.Text = string.Empty; DiCapButtonsLabel.Text = string.Empty; DiCapDPadsLabel.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) XInput.ReLoadLibrary(XInput.LibraryName); } DiCapFfLabel.Text = string.Format("Force Feedback: {0}", forceFeedbackState); DiCapAxesLabel.Text = string.Format("Axes: {0}", device.Capabilities.AxeCount); DiCapButtonsLabel.Text = string.Format("Buttons: {0}", device.Capabilities.ButtonCount); DiCapDPadsLabel.Text = string.Format("D-Pads: {0}", device.Capabilities.PovCount); var di = device.Information; // 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()); }