public JoystickCapture(IntPtr handle) { SlimDX.DirectInput.DirectInput directInput = new SlimDX.DirectInput.DirectInput(); foreach (DeviceInstance device in (IEnumerable <DeviceInstance>)directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { try { this.joystick = new Joystick(directInput, device.InstanceGuid); this.joystick.SetCooperativeLevel(handle, CooperativeLevel.Foreground | CooperativeLevel.Exclusive); break; } catch (DirectInputException ex) { } } if (this.joystick == null) { return; } foreach (DeviceObjectInstance deviceObjectInstance in (IEnumerable <DeviceObjectInstance>) this.joystick.GetObjects()) { if ((deviceObjectInstance.ObjectType & ObjectDeviceType.Axis) != ObjectDeviceType.All) { this.joystick.GetObjectPropertiesById((int)deviceObjectInstance.ObjectType).SetRange(-1000, 1000); } } this.joystick.Acquire(); }
public bool ScanForControllers() { bool foundSome = false; var list = manager.GetDevices(DI.DeviceClass.GameController, DI.DeviceEnumerationFlags.AttachedOnly); foreach (DI.DeviceInstance d in list) { bool found = false; foreach (var cc in Controllers) { if (cc.Device.Information.InstanceGuid == d.InstanceGuid) { found = true; break; } } if (found) { continue; } Console.WriteLine("Found gamepad " + d.InstanceName + "..."); Controllers.Add(new InputController(this, new DI.Joystick(manager, d.InstanceGuid), Controllers.Count)); foundSome = true; } return(foundSome); }
public TabMateDevice() { // Find the device /// The DirectInput interface SlimDX.DirectInput.DirectInput input = new SlimDX.DirectInput.DirectInput(); /// For every device connected using DirectInput that: /// - is a "game controller" /// - and is connected foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly)) { /// Get the TabMate from the input interface, and bind its ref via the guid tab_mate = new Joystick(input, device.InstanceGuid); break; } event_timer.Elapsed += Event_timer_Elapsed; event_timer.Enabled = true; event_timer.Interval = 1; }
/// <summary> /// Gets the current available DirectInput devices. /// </summary> /// <returns>List of devices</returns> public IEnumerable <DirectDevice> GetInputDevices() { var directDevices = new List <DirectDevice>(); var deviceInstances = directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly); foreach (var deviceInstance in deviceInstances) { var joystick = new Joystick(directInput, deviceInstance.InstanceGuid); if (joystick.Information.ProductGuid.ToString() == EMULATED_ID || (joystick.Capabilities.AxesCount < 1 && joystick.Capabilities.ButtonCount < 1)) { joystick.Dispose(); continue; } joystick.Properties.BufferSize = 128; directDevices.Add(new DirectDevice(deviceInstance, joystick)); } return(directDevices); }
private static void Scan() { _directInput = new SlimDX.DirectInput.DirectInput(); try { while (_isActive) { var getDevices = Stopwatch.StartNew(); IList <Joystick> list; string footprint; bool updated; if (_directInput == null) { list = new List <Joystick>(0); footprint = string.Empty; updated = _staticDataFootprint != footprint; } else { try { var devices = _directInput?.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly); footprint = devices?.Select(x => x.InstanceGuid).JoinToString(';'); updated = _staticDataFootprint != footprint; list = updated ? devices?.Select(x => { var existing = _staticData?.FirstOrDefault(y => y.Information.InstanceGuid == x.InstanceGuid); if (existing != null) { return(existing); } var result = new Joystick(_directInput, x.InstanceGuid); if (result.Capabilities == null) { // We don’t really need a check here, but we need to access .Capabilities here, in a background // thread, because it might take a while to get the data which will be needed later. throw new Exception("Never happens"); } return(result); }).ToArray() : _staticData; } catch (Exception e) { // TODO: Try to re-initiate scanning later? Logging.Error(e); list = new List <Joystick>(0); footprint = string.Empty; updated = _staticDataFootprint != footprint; DisposeHelper.Dispose(ref _directInput); } } getDevices.Stop(); _scanTime = getDevices.Elapsed; if (updated) { _staticData?.ApartFrom(list).DisposeEverything(); _staticDataFootprint = footprint; _staticData = list; lock (Instances) { for (var i = Instances.Count - 1; i >= 0; i--) { Instances[i].RaiseUpdate(list); } } } else { UpdateScanTime(); } Thread.Sleep(OptionMinRescanPeriod + getDevices.Elapsed); int count; lock (Instances) { count = Instances.Count; } if (count == 0) { lock (ThreadSync) { Monitor.Wait(ThreadSync); } } } } finally { DisposeHelper.Dispose(ref _directInput); } }
private void RescanDevices() { IList <DeviceInstance> devices; try { if (_directInput == null) { _directInput = new SlimDX.DirectInput.DirectInput(); } devices = _directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly); } catch (Exception e) { Logging.Error(e); devices = new List <DeviceInstance>(); DisposeHelper.Dispose(ref _directInput); } if (OptionIgnoreControlsFilter != null) { devices = devices.Where(x => OptionIgnoreControlsFilter.Test(x.ProductName)).ToList(); } var footprint = GetFootprint(devices); if (footprint == _devicesFootprint) { return; } _skip = true; try { var newDevices = devices.Select((x, i) => Devices.FirstOrDefault(y => y.Same(x)) ?? DirectInputDevice.Create(_directInput, x, i)).NonNull().ToList(); _devicesFootprint = GetFootprint(newDevices.Select(x => x.Device)); foreach (var entry in Entries.OfType <BaseEntry <DirectInputAxle> >()) { var current = entry.Input?.Device; if (current is PlaceholderInputDevice) { var replacement = newDevices.FirstOrDefault(x => x.Same(current)); if (replacement != null) { entry.Input = replacement.GetAxle(entry.Input.Id); } } else if (current is DirectInputDevice && !newDevices.Contains(current)) { entry.Input = GetPlaceholderDevice((DirectInputDevice)current).GetAxle(entry.Input.Id); } } foreach (var entry in Entries.OfType <BaseEntry <DirectInputButton> >()) { var current = entry.Input?.Device; if (current is PlaceholderInputDevice) { var replacement = newDevices.FirstOrDefault(x => x.Same(current)); if (replacement != null) { entry.Input = replacement.GetButton(entry.Input.Id); } } else if (current is DirectInputDevice && !newDevices.Contains(current)) { entry.Input = GetPlaceholderDevice((DirectInputDevice)current).GetButton(entry.Input.Id); } } foreach (var device in Devices.ApartFrom(newDevices)) { for (var i = 0; i < device.Buttons.Length; i++) { device.Buttons[i].PropertyChanged -= DeviceButtonEventHandler; } for (var i = 0; i < device.Axles.Length; i++) { device.Axles[i].PropertyChanged -= DeviceAxleEventHandler; } device.Dispose(); } Devices.ReplaceEverythingBy(newDevices); UpdatePlaceholders(); foreach (var device in newDevices) { ProductGuids[device.DisplayName] = device.Id; for (var i = 0; i < device.Buttons.Length; i++) { device.Buttons[i].PropertyChanged += DeviceButtonEventHandler; } for (var i = 0; i < device.Axles.Length; i++) { device.Axles[i].PropertyChanged += DeviceAxleEventHandler; } } } finally { _skip = false; UpdateWheelHShifterDevice(); } }