void UpdateDiStates() { // Get all mapped user instances. var instanceGuids = SettingsManager.Settings.Items .Where(x => x.MapTo > (int)MapTo.None) .Select(x => x.InstanceGuid).ToArray(); // Get all connected devices. var userDevices = SettingsManager.UserDevices.Items .Where(x => instanceGuids.Contains(x.InstanceGuid) && x.IsOnline) .ToArray(); for (int i = 0; i < userDevices.Count(); i++) { // Update direct input form and return actions (pressed buttons/dpads, turned axis/sliders). var ud = userDevices[i]; JoystickState state = null; // Allow if not testing or testing with option enabled. var o = SettingsManager.Options; var allow = !o.TestEnabled || o.TestGetDInputStates; var isOnline = ud != null && ud.IsOnline; if (isOnline && allow) { var device = ud.Device; if (device != null) { try { device.Acquire(); state = device.GetCurrentState(); // Fill device objects. if (ud.DeviceObjects == null) { ud.DeviceObjects = AppHelper.GetDeviceObjects(device); } if (ud.DeviceEffects == null) { ud.DeviceEffects = AppHelper.GetDeviceEffects(device); } } catch (Exception ex) { var error = ex; } } // If this is test device then... else if (TestDeviceHelper.ProductGuid.Equals(ud.ProductGuid)) { state = TestDeviceHelper.GetCurrentState(ud); // Fill device objects. if (ud.DeviceObjects == null) { ud.DeviceObjects = TestDeviceHelper.GetDeviceObjects(); } ud.DeviceEffects = new DeviceEffectItem[0]; } } ud.JoState = state ?? new JoystickState(); ud.DiState = new CustomDiState(ud.JoState); } }
private void AddDemoDevice_Click(object sender, EventArgs e) { var ud = TestDeviceHelper.NewUserDevice(); SettingsManager.UserDevices.Items.Add(ud); MainForm.Current.DHelper.UpdateDevicesEnabled = true; }
private void AddDemoDevice_Click(object sender, EventArgs e) { var ud = TestDeviceHelper.NewUserDevice(); lock (SettingsManager.UserDevices.SyncRoot) SettingsManager.UserDevices.Items.Add(ud); Global.DHelper.UpdateDevicesEnabled = true; }
private void AddDemoDeviceButton_Click(object sender, RoutedEventArgs e) { ControlsHelper.BeginInvoke(() => { var ud = TestDeviceHelper.NewUserDevice(); lock (SettingsManager.UserDevices.SyncRoot) SettingsManager.UserDevices.Items.Add(ud); Global.DHelper.UpdateDevicesEnabled = true; }); }
void UpdateDiStates() { // Get all mapped user instances. var instanceGuids = SettingsManager.Settings.Items .Where(x => x.MapTo > (int)MapTo.None) .Select(x => x.InstanceGuid).ToArray(); // Get all connected devices. var userDevices = SettingsManager.UserDevices.Items .Where(x => instanceGuids.Contains(x.InstanceGuid) && x.IsOnline) .ToArray(); for (int i = 0; i < userDevices.Count(); i++) { var ud = userDevices[i]; JoystickState state = null; // Update direct input form and return actions (pressed buttons/dpads, turned axis/sliders). var isOnline = ud != null && ud.IsOnline; if (isOnline) { var device = ud.Device; if (device != null) { try { device.Acquire(); state = device.GetCurrentState(); } catch (Exception ex) { var error = ex; } } // If this is test device then... else if (TestDeviceHelper.ProductGuid.Equals(ud.ProductGuid)) { state = TestDeviceHelper.GetCurrentState(ud); } } ud.JoState = state ?? new JoystickState(); ud.DiState = new CustomDiState(ud.JoState); } }
void UpdateDiStates(UserGame game) { // Get all mapped user instances. var instanceGuids = SettingsManager.Settings.Items .Where(x => x.MapTo > (int)MapTo.None) .Select(x => x.InstanceGuid).ToArray(); // Get all connected devices. var userDevices = SettingsManager.UserDevices.Items .Where(x => instanceGuids.Contains(x.InstanceGuid) && x.IsOnline) .ToArray(); // Acquire copy of feedbacks for processing. var feedbacks = CopyAndClearFeedbacks(); for (int i = 0; i < userDevices.Count(); i++) { // Update direct input form and return actions (pressed Buttons/DPads, turned Axis/Sliders). var ud = userDevices[i]; JoystickState state = null; // Allow if not testing or testing with option enabled. var o = SettingsManager.Options; var allow = !o.TestEnabled || o.TestGetDInputStates; var isOnline = ud != null && ud.IsOnline; if (isOnline && allow) { var device = ud.Device; if (device != null) { var exceptionData = new System.Text.StringBuilder(); try { var isVirtual = ((EmulationType)game.EmulationType).HasFlag(EmulationType.Virtual); var hasForceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback); // Exclusive mode required only if force feedback is available and device is virtual there are no info about effects. var exclusiveRequired = hasForceFeedback && (isVirtual || ud.DeviceEffects == null); // If exclusive mode is required and mode is unknown or not exclusive then... if (exclusiveRequired && (!ud.IsExclusiveMode.HasValue || !ud.IsExclusiveMode.Value)) { var flags = CooperativeLevel.Background | CooperativeLevel.Exclusive; // Reacquire device in exclusive mode. exceptionData.AppendLine("Unacquire (Exclusive)..."); device.Unacquire(); exceptionData.AppendLine("SetCooperativeLevel (Exclusive)..."); device.SetCooperativeLevel(detector.DetectorForm.Handle, flags); exceptionData.AppendLine("Acquire (Exclusive)..."); device.Acquire(); ud.IsExclusiveMode = true; } // If current mode must be non exclusive and mode is unknown or exclusive then... else if (!exclusiveRequired && (!ud.IsExclusiveMode.HasValue || ud.IsExclusiveMode.Value)) { var flags = CooperativeLevel.Background | CooperativeLevel.NonExclusive; // Reacquire device in non exclusive mode so that xinput.dll can control force feedback. exceptionData.AppendLine("Unacquire (NonExclusive)..."); device.Unacquire(); exceptionData.AppendLine("SetCooperativeLevel (Exclusive)..."); device.SetCooperativeLevel(detector.DetectorForm.Handle, flags); exceptionData.AppendLine("Acquire (Acquire)..."); device.Acquire(); ud.IsExclusiveMode = false; } exceptionData.AppendFormat("device.GetCurrentState() // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); state = device.GetCurrentState(); // Fill device objects. if (ud.DeviceObjects == null) { exceptionData.AppendFormat("AppHelper.GetDeviceObjects(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); var dos = AppHelper.GetDeviceObjects(device); ud.DeviceObjects = dos; // Update masks. ud.DiAxeMask = CustomDiState.GetJoystickAxisMask(dos, device); ud.DiSliderMask = CustomDiState.GetJoystickSlidersMask(dos, device); } if (ud.DeviceEffects == null) { exceptionData.AppendFormat("AppHelper.GetDeviceEffects(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); ud.DeviceEffects = AppHelper.GetDeviceEffects(device); } // Get PAD index this device is mapped to. var userIndex = SettingsManager.Settings.Items .Where(x => x.MapTo > (int)MapTo.None) .Where(x => x.InstanceGuid == ud.InstanceGuid) .Select(x => x.MapTo).First(); // If device support force feedback then... if (hasForceFeedback) { // Get setting related to user device. var setting = SettingsManager.Settings.Items .FirstOrDefault(x => x.MapTo == userIndex && x.InstanceGuid == ud.InstanceGuid); if (setting != null) { // Get pad setting attached to device. var ps = SettingsManager.GetPadSetting(setting.PadSettingChecksum); if (ps != null) { // If force is enabled then... if (ps.ForceEnable == "1") { if (ud.FFState == null) { ud.FFState = new Engine.ForceFeedbackState(); } // If force update supplied then... var force = feedbacks[userIndex - 1]; if (force != null || ud.FFState.Changed(ps)) { var v = new Vibration(); if (force == null) { v.LeftMotorSpeed = short.MinValue; v.RightMotorSpeed = short.MinValue; } else { v.LeftMotorSpeed = (short)ConvertHelper.ConvertRange(byte.MinValue, byte.MaxValue, short.MinValue, short.MaxValue, force.LargeMotor); v.RightMotorSpeed = (short)ConvertHelper.ConvertRange(byte.MinValue, byte.MaxValue, short.MinValue, short.MaxValue, force.SmallMotor); } // For the future: Investigate device states if force feedback is not working. // var st = ud.Device.GetForceFeedbackState(); //st == SharpDX.DirectInput.ForceFeedbackState // ud.Device.SendForceFeedbackCommand(ForceFeedbackCommand.SetActuatorsOn); exceptionData.AppendFormat("ud.FFState.SetDeviceForces(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); ud.FFState.SetDeviceForces(ud, device, ps, v); } } // If force state was created then... else if (ud.FFState != null) { // Stop device forces. exceptionData.AppendFormat("ud.FFState.StopDeviceForces(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); ud.FFState.StopDeviceForces(device); ud.FFState = null; } } } } } catch (Exception ex) { ex.Data.Add("FFInfo", exceptionData.ToString()); JocysCom.ClassLibrary.Runtime.LogHelper.Current.WriteException(ex); ud.IsExclusiveMode = null; } } // If this is test device then... else if (TestDeviceHelper.ProductGuid.Equals(ud.ProductGuid)) { // Fill device objects. if (ud.DeviceObjects == null) { var dos = TestDeviceHelper.GetDeviceObjects(); ud.DeviceObjects = dos; // Update masks. ud.DiAxeMask = 0x1 | 0x2 | 0x4 | 0x8; ud.DiSliderMask = 0; } if (ud.DeviceEffects == null) { ud.DeviceEffects = new DeviceEffectItem[0]; } state = TestDeviceHelper.GetCurrentState(ud); } } ud.JoState = state ?? new JoystickState(); var newState = new CustomDiState(ud.JoState); var newTime = watch.ElapsedTicks; // Mouse needs special update. if (ud.Device != null && ud.Device.Information.Type == SharpDX.DirectInput.DeviceType.Mouse) { var mouseState = new CustomDiState(ud.JoState); if (ud.OldDiState == null) { // Make sure new state have zero values. for (int a = 0; a < newState.Axis.Length; a++) { mouseState.Axis[a] = -short.MinValue; } // Update sliders with delta. for (int s = 0; s < newState.Sliders.Length; s++) { mouseState.Sliders[s] = -short.MinValue; } } else { // This parts needs to be worked on. var ticks = (int)(newTime - ud.DiStateTime); // Update axis with delta. for (int a = 0; a < newState.Axis.Length; a++) { mouseState.Axis[a] = ticks * (newState.Axis[a] - ud.OldDiState.Axis[a]) - short.MinValue; } // Update sliders with delta. for (int s = 0; s < newState.Sliders.Length; s++) { mouseState.Sliders[s] = ticks * (newState.Sliders[s] - ud.OldDiState.Sliders[s]) - short.MinValue; } } // Assign unmodified state. ud.OldDiState = newState; ud.OldDiStateTime = ud.DiStateTime; ud.DiState = mouseState; } else { // Update state. ud.DiState = newState; } ud.DiStateTime = newTime; } }
void UpdateDiDevices(DirectInput manager) { if (!UpdateDevicesPending) { return; } UpdateDevicesPending = false; // Make sure that interface handle is created, before starting device updates. UserDevice[] deleteDevices; // Add connected devices. var insertDevices = new List <UserDevice>(); // List of connected devices (can be a very long operation). var devices = new List <DeviceInstance>(); // Controllers. var controllerInstances = manager.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices).ToList(); foreach (var item in controllerInstances) { devices.Add(item); } // Pointers. var pointerInstances = manager.GetDevices(DeviceClass.Pointer, DeviceEnumerationFlags.AllDevices).ToList(); foreach (var item in pointerInstances) { devices.Add(item); } // Keyboards. var keyboardInstances = manager.GetDevices(DeviceClass.Keyboard, DeviceEnumerationFlags.AllDevices).ToList(); foreach (var item in keyboardInstances) { devices.Add(item); } if (Program.IsClosing) { return; } // List of connected devices. var deviceInstanceGuid = devices.Select(x => x.InstanceGuid).ToList(); // List of current devices. var uds = SettingsManager.UserDevices.ItemsToArraySyncronized(); var currentInstanceGuids = uds.Select(x => x.InstanceGuid).ToArray(); deleteDevices = uds.Where(x => !deviceInstanceGuid.Contains(x.InstanceGuid)).ToArray(); var addedDevices = devices.Where(x => !currentInstanceGuids.Contains(x.InstanceGuid)).ToArray(); var updatedDevices = devices.Where(x => currentInstanceGuids.Contains(x.InstanceGuid)).ToArray(); // Must find better way to find Device than by Vendor ID and Product ID. DeviceInfo[] devInfos = null; DeviceInfo[] intInfos = null; if (addedDevices.Length > 0 || updatedDevices.Length > 0) { devInfos = DeviceDetector.GetDevices(); //var classes = devInfos.Select(x=>x.ClassDescription).Distinct().ToArray(); intInfos = DeviceDetector.GetInterfaces(); //var intclasses = intInfos.Select(x => x.ClassDescription).Distinct().ToArray(); } //Joystick = new Guid("6f1d2b70-d5a0-11cf-bfc7-444553540000"); //SysMouse = new Guid("6f1d2b60-d5a0-11cf-bfc7-444553540000"); //SysKeyboard = new Guid("6f1d2b61-d5a0-11cf-bfc7-444553540000"); for (int i = 0; i < addedDevices.Length; i++) { var device = addedDevices[i]; var ud = new UserDevice(); DeviceInfo hid; RefreshDevice(manager, ud, device, devInfos, intInfos, out hid); var isVirtual = false; if (hid != null) { DeviceInfo p = hid; do { p = devInfos.FirstOrDefault(x => x.DeviceId == p.ParentDeviceId); // If ViGEm hardware found then... if (p != null && VirtualDriverInstaller.ViGEmBusHardwareIds.Any(x => string.Compare(p.HardwareIds, x, true) == 0)) { isVirtual = true; break; } } while (p != null); } if (!isVirtual) { insertDevices.Add(ud); } } //if (insertDevices.Count > 0) //{ // CloudPanel.Add(CloudAction.Insert, insertDevices.ToArray(), true); //} for (int i = 0; i < updatedDevices.Length; i++) { var device = updatedDevices[i]; var ud = uds.First(x => x.InstanceGuid.Equals(device.InstanceGuid)); DeviceInfo hid; // Will refresh device and fill more values with new x360ce app if available. RefreshDevice(manager, ud, device, devInfos, intInfos, out hid); } if (Program.IsClosing) { return; } // Remove disconnected devices. for (int i = 0; i < deleteDevices.Length; i++) { lock (SettingsManager.UserDevices.SyncRoot) deleteDevices[i].IsOnline = false; } for (int i = 0; i < insertDevices.Count; i++) { var ud = insertDevices[i]; lock (SettingsManager.UserDevices.SyncRoot) SettingsManager.UserDevices.Items.Add(ud); } // Enable Test instances. TestDeviceHelper.EnableTestInstances(); RefreshDevicesCount++; var ev = DevicesUpdated; if (ev != null) { ev(this, new DInputEventArgs()); } // var game = CurrentGame; // if (game != null) // { // // Auto-configure new devices. // AutoConfigure(game); // } }
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()); }
void UpdateDiStates(UserGame game, DeviceDetector detector) { // Get all mapped user devices. var userDevices = SettingsManager.GetMappedDevices(game?.FileName); // Acquire copy of feedbacks for processing. var feedbacks = CopyAndClearFeedbacks(); for (int i = 0; i < userDevices.Count(); i++) { // Update direct input form and return actions (pressed Buttons/DPads, turned Axis/Sliders). var ud = userDevices[i]; JoystickState state = null; JoystickUpdate[] update = null; // Allow if not testing or testing with option enabled. var o = SettingsManager.Options; var allow = !o.TestEnabled || o.TestGetDInputStates; // Note: manager.IsDeviceAttached() use a lot of CPU resources. var isAttached = ud != null && ud.IsOnline; // && manager.IsDeviceAttached(ud.InstanceGuid); if (isAttached && allow) { var device = ud.Device; if (device != null) { var exceptionData = new System.Text.StringBuilder(); try { if (o.UseDeviceBufferedData && device.Properties.BufferSize == 0) { // Set BufferSize in order to use buffered data. device.Properties.BufferSize = 128; } var isVirtual = ((EmulationType)game.EmulationType).HasFlag(EmulationType.Virtual); var hasForceFeedback = device.Capabilities.Flags.HasFlag(DeviceFlags.ForceFeedback); // Exclusive mode required only if force feedback is available and device is virtual there are no info about effects. var exclusiveRequired = hasForceFeedback && (isVirtual || ud.DeviceEffects == null); // If exclusive mode is required and mode is unknown or not exclusive then... if (exclusiveRequired && (!ud.IsExclusiveMode.HasValue || !ud.IsExclusiveMode.Value)) { var flags = CooperativeLevel.Background | CooperativeLevel.Exclusive; // Reacquire device in exclusive mode. exceptionData.AppendLine("Unacquire (Exclusive)..."); device.Unacquire(); exceptionData.AppendLine("SetCooperativeLevel (Exclusive)..."); device.SetCooperativeLevel(detector.DetectorForm.Handle, flags); exceptionData.AppendLine("Acquire (Exclusive)..."); device.Acquire(); ud.IsExclusiveMode = true; } // If current mode must be non exclusive and mode is unknown or exclusive then... else if (!exclusiveRequired && (!ud.IsExclusiveMode.HasValue || ud.IsExclusiveMode.Value)) { var flags = CooperativeLevel.Background | CooperativeLevel.NonExclusive; // Reacquire device in non exclusive mode so that xinput.dll can control force feedback. exceptionData.AppendLine("Unacquire (NonExclusive)..."); device.Unacquire(); exceptionData.AppendLine("SetCooperativeLevel (Exclusive)..."); device.SetCooperativeLevel(detector.DetectorForm.Handle, flags); exceptionData.AppendLine("Acquire (Acquire)..."); device.Acquire(); ud.IsExclusiveMode = false; } exceptionData.AppendFormat("device.GetCurrentState() // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); // Polling - Retrieves data from polled objects on a DirectInput device. // Some devices require pooling (For example original "Xbox Controller S" with XBCD drivers). // If the device does not require polling, calling this method has no effect. // If a device that requires polling is not polled periodically, no new data is received from the device. // Calling this method causes DirectInput to update the device state, generate input // events (if buffered data is enabled), and set notification events (if notification is enabled). device.Poll(); if (o.UseDeviceBufferedData && device.Properties.BufferSize > 0) { // Get buffered data. update = device.GetBufferedData(); } // Get device state. state = device.GetCurrentState(); // Fill device objects. if (ud.DeviceObjects == null) { exceptionData.AppendFormat("AppHelper.GetDeviceObjects(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); var dos = AppHelper.GetDeviceObjects(device); ud.DeviceObjects = dos; // Update masks. int axisMask = 0; int actuatorMask = 0; int actuatorCount = 0; if (ud.CapType == (int)SharpDX.DirectInput.DeviceType.Mouse) { CustomDiState.GetMouseAxisMask(dos, device, out axisMask); } else { CustomDiState.GetJoystickAxisMask(dos, device, out axisMask, out actuatorMask, out actuatorCount); } ud.DiAxeMask = axisMask; // Contains information about which axis have force feedback actuator attached. ud.DiActuatorMask = actuatorMask; ud.DiActuatorCount = actuatorCount; CustomDiState.GetJoystickSlidersMask(dos, device); } if (ud.DeviceEffects == null) { exceptionData.AppendFormat("AppHelper.GetDeviceEffects(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); ud.DeviceEffects = AppHelper.GetDeviceEffects(device); } // If device support force feedback then... if (hasForceFeedback) { // Get setting related to user device. var setting = SettingsManager.UserSettings.ItemsToArraySyncronized() .FirstOrDefault(x => x.InstanceGuid == ud.InstanceGuid); // If device is mapped to controller then... if (setting != null && setting.MapTo > (int)MapTo.None) { // Get pad setting attached to device. var ps = SettingsManager.GetPadSetting(setting.PadSettingChecksum); if (ps != null) { // If force is enabled then... if (ps.ForceEnable == "1") { if (ud.FFState == null) { ud.FFState = new Engine.ForceFeedbackState(); } // If force update supplied then... var force = feedbacks[(int)setting.MapTo - 1]; if (force != null || ud.FFState.Changed(ps)) { var v = new Vibration(); if (force == null) { v.LeftMotorSpeed = short.MinValue; v.RightMotorSpeed = short.MinValue; } else { v.LeftMotorSpeed = (short)ConvertHelper.ConvertRange(byte.MinValue, byte.MaxValue, short.MinValue, short.MaxValue, force.LargeMotor); v.RightMotorSpeed = (short)ConvertHelper.ConvertRange(byte.MinValue, byte.MaxValue, short.MinValue, short.MaxValue, force.SmallMotor); } // For the future: Investigate device states if force feedback is not working. // var st = ud.Device.GetForceFeedbackState(); //st == SharpDX.DirectInput.ForceFeedbackState // ud.Device.SendForceFeedbackCommand(ForceFeedbackCommand.SetActuatorsOn); exceptionData.AppendFormat("ud.FFState.SetDeviceForces(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); ud.FFState.SetDeviceForces(ud, device, ps, v); } } // If force state was created then... else if (ud.FFState != null) { // Stop device forces. exceptionData.AppendFormat("ud.FFState.StopDeviceForces(device) // ud.IsExclusiveMode = {0}", ud.IsExclusiveMode).AppendLine(); ud.FFState.StopDeviceForces(device); ud.FFState = null; } } } } } catch (Exception ex) { var dex = ex as SharpDXException; if (dex != null && dex.ResultCode == SharpDX.DirectInput.ResultCode.InputLost) { // Ignore error. } else if (dex != null && dex.ResultCode == SharpDX.DirectInput.ResultCode.NotAcquired) { // Ignore error } else if (dex != null && dex.ResultCode == SharpDX.DirectInput.ResultCode.Unplugged) { // Ignore error } else { var cx = new DInputException("UpdateDiStates Exception", ex); cx.Data.Add("FFInfo", exceptionData.ToString()); JocysCom.ClassLibrary.Runtime.LogHelper.Current.WriteException(cx); } ud.IsExclusiveMode = null; } } // If this is test device then... else if (TestDeviceHelper.ProductGuid.Equals(ud.ProductGuid)) { // Fill device objects. if (ud.DeviceObjects == null) { var dos = TestDeviceHelper.GetDeviceObjects(); ud.DeviceObjects = dos; // Update masks. ud.DiAxeMask = 0x1 | 0x2 | 0x4 | 0x8; ud.DiSliderMask = 0; } if (ud.DeviceEffects == null) { ud.DeviceEffects = new DeviceEffectItem[0]; } state = TestDeviceHelper.GetCurrentState(ud); } } ud.JoState = state; ud.JoUpdate = update; if (state != null) { var newState = new CustomDiState(ud.JoState); var newUpdates = update?.Select(x => new CustomDiUpdate(x)).ToArray(); // If updates from buffer supplied and old state is available then... if (newUpdates != null && newUpdates.Count(x => x.Type == MapType.Button) > 1 && ud.DiState != null) { // Analyse if state must be modified. for (int b = 0; b < newState.Buttons.Length; b++) { var oldPresseed = ud.DiState.Buttons[b]; var newPresseed = newState.Buttons[b]; // If button state was not changed. if (oldPresseed == newPresseed) { // But buffer contains press then... var wasPressed = newUpdates.Count(x => x.Type == MapType.Button && x.Index == b) > 1; if (wasPressed) { // Invert state and give chance for the game to recognize the press. newState.Buttons[b] = !newState.Buttons[b]; } } } } var newTime = watch.ElapsedTicks; // Remember old state. ud.OldDiState = ud.DiState; ud.OldDiUpdates = ud.DiUpdates; ud.OldDiStateTime = ud.DiStateTime; // Update state. ud.DiState = newState; ud.DiUpdates = newUpdates; ud.DiStateTime = newTime; // Mouse needs special update. if (ud.Device != null && ud.Device.Information.Type == SharpDX.DirectInput.DeviceType.Mouse) { // If original state is missing then... if (ud.OrgDiState == null) { // Store current values. ud.OrgDiState = newState; ud.OrgDiStateTime = newTime; // Make sure new states have zero values. for (int a = 0; a < newState.Axis.Length; a++) { newState.Axis[a] = -short.MinValue; } for (int s = 0; s < newState.Sliders.Length; s++) { newState.Sliders[s] = -short.MinValue; } } var mouseState = new CustomDiState(new JoystickState()); // Clone button values. Array.Copy(newState.Buttons, mouseState.Buttons, mouseState.Buttons.Length); // //-------------------------------------------------------- // // Map mouse acceleration to axis position. Good for FPS control. // //-------------------------------------------------------- // // This parts needs to be worked on. // //var ticks = (int)(newTime - ud.DiStateTime); // // Update axis with delta. // //for (int a = 0; a < newState.Axis.Length; a++) // // mouseState.Axis[a] = ticks * (newState.Axis[a] - ud.OldDiState.Axis[a]) - short.MinValue; // // Update sliders with delta. // //for (int s = 0; s < newState.Sliders.Length; s++) // // mouseState.Sliders[s] = ticks * (newState.Sliders[s] - ud.OldDiState.Sliders[s]) - short.MinValue; //-------------------------------------------------------- // Map mouse position to axis position. Good for car wheel controls. //-------------------------------------------------------- Calc(ud.OrgDiState.Axis, newState.Axis, mouseState.Axis); Calc(ud.OrgDiState.Sliders, newState.Sliders, mouseState.Sliders); ud.DiState = mouseState; } } } }
void UpdateDiDevices() { if (!UpdateDevicesEnabled) { return; } UpdateDevicesEnabled = false; // Make sure that interface handle is created, before starting device updates. UserDevice[] deleteDevices; // Add connected devices. var insertDevices = new List <UserDevice>(); var updateDevices = new List <KeyValuePair <UserDevice, DeviceInstance> >(); // List of connected devices (can be a very long operation). var devices = new List <DeviceInstance>(); // Controllers. var controllerInstances = Manager.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices).ToList(); foreach (var item in controllerInstances) { devices.Add(item); } // Pointers. var pointerInstances = Manager.GetDevices(DeviceClass.Pointer, DeviceEnumerationFlags.AllDevices).ToList(); foreach (var item in pointerInstances) { devices.Add(item); } // Keyboards. var keyboardInstances = Manager.GetDevices(DeviceClass.Keyboard, DeviceEnumerationFlags.AllDevices).ToList(); foreach (var item in keyboardInstances) { devices.Add(item); } if (Program.IsClosing) { return; } // List of connected devices. var deviceInstanceGuid = devices.Select(x => x.InstanceGuid).ToList(); // List of current devices. var currentInstanceGuids = SettingsManager.UserDevices.Items.Select(x => x.InstanceGuid).ToArray(); deleteDevices = SettingsManager.UserDevices.Items.Where(x => !deviceInstanceGuid.Contains(x.InstanceGuid)).ToArray(); var addedDevices = devices.Where(x => !currentInstanceGuids.Contains(x.InstanceGuid)).ToArray(); var updatedDevices = devices.Where(x => currentInstanceGuids.Contains(x.InstanceGuid)).ToArray(); // Must find better way to find Device than by Vendor ID and Product ID. if (addedDevices.Length > 0) { //Joystick = new Guid("6f1d2b70-d5a0-11cf-bfc7-444553540000"); //SysMouse = new Guid("6f1d2b60-d5a0-11cf-bfc7-444553540000"); //SysKeyboard = new Guid("6f1d2b61-d5a0-11cf-bfc7-444553540000"); DeviceInfo[] devInfos = DeviceDetector.GetDevices(); DeviceInfo[] intInfos = null; // Controllers. var controllers = addedDevices .Where(x => x.Type != SharpDX.DirectInput.DeviceType.Mouse && x.Type != SharpDX.DirectInput.DeviceType.Keyboard) .Select(x => new Joystick(Manager, x.InstanceGuid)).ToArray(); // Get interfaces. var interfacePaths = controllers.Select(x => x.Properties.InterfacePath).ToArray(); intInfos = DeviceDetector.GetInterfaces(interfacePaths); for (int i = 0; i < addedDevices.Length; i++) { var device = addedDevices[i]; var ud = new UserDevice(); RefreshDevice(ud, device); DeviceInfo hid = null; DeviceInfo dev = null; if (device.IsHumanInterfaceDevice) { // Get interface info for added devices. hid = intInfos.FirstOrDefault(x => x.DevicePath == ud.Device.Properties.InterfacePath); // Get device info for added devices. dev = devInfos.FirstOrDefault(x => x.DeviceId == ud.HidDeviceId); } ud.LoadHidDeviceInfo(hid); ud.LoadDevDeviceInfo(dev); var isVirtual = false; if (hid != null) { DeviceInfo p = hid; do { p = DeviceDetector.GetParentDevice(Guid.Empty, JocysCom.ClassLibrary.Win32.DIGCF.DIGCF_ALLCLASSES, p.DeviceId); if (p != null && string.Compare(p.HardwareId, VirtualDriverInstaller.ViGEmBusHardwareId, true) == 0) { isVirtual = true; break; } } while (p != null); } if (!isVirtual) { insertDevices.Add(ud); } } } //if (insertDevices.Count > 0) //{ // CloudPanel.Add(CloudAction.Insert, insertDevices.ToArray(), true); //} for (int i = 0; i < updatedDevices.Length; i++) { var device = updatedDevices[i]; var di = SettingsManager.UserDevices.Items.First(x => x.InstanceGuid.Equals(device.InstanceGuid)); updateDevices.Add(new KeyValuePair <UserDevice, DeviceInstance>(di, device)); } if (Program.IsClosing) { return; } // Remove disconnected devices. for (int i = 0; i < deleteDevices.Length; i++) { deleteDevices[i].IsOnline = false; } for (int i = 0; i < insertDevices.Count; i++) { var ud = insertDevices[i]; SettingsManager.UserDevices.Items.Add(ud); } for (int i = 0; i < updateDevices.Count; i++) { var kv = updateDevices[i]; RefreshDevice(kv.Key, kv.Value); } // Enable Test instances. TestDeviceHelper.EnableTestInstances(); RefreshDevicesCount++; var ev = DevicesUpdated; if (ev != null) { ev(this, new DInputEventArgs()); } // var game = CurrentGame; // if (game != null) // { // // Auto-configure new devices. // AutoConfigure(game); // } }
private void AddDemoDevice_Click(object sender, EventArgs e) { var ud = TestDeviceHelper.NewUserDevice(); SettingsManager.UserDevices.Items.Add(ud); }