public Gamepad(GamepadConfiguration config, uint gamepadId) { _config = config; _vJoy = new vJoy(); _gamepadId = gamepadId; if (!_vJoy.vJoyEnabled()) { Log(@"vJoy driver not enabled: Failed Getting vJoy attributes."); return; } uint dllVer = 0, drvVer = 0; var match = _vJoy.DriverMatch(ref dllVer, ref drvVer); if (!match) { Log($@"Version of Driver ({drvVer:X}) does NOT match DLL Version ({dllVer:X})\n"); return; } var status = _vJoy.GetVJDStatus(_gamepadId); if ((status == VjdStat.VJD_STAT_OWN) || ((status == VjdStat.VJD_STAT_FREE) && !_vJoy.AcquireVJD(_gamepadId))) { Log($@"Failed to acquire vJoy device number {_gamepadId}.\n"); return; } ResetGamepad(_gamepadId); }
private static async Task SaveConfigurationToFile(GamepadConfiguration configuration, string filename) { var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); using (var outputFile = new StreamWriter(desktopPath + $"\\{filename}.mficonfiguration")) { await outputFile.WriteAsync(configuration.GetJsonRepresentation()); } }
public Gamepad(GamepadConfiguration config, VGenWrapper vGenWrapper, HidDeviceLoader hidDeviceLoader) { _config = config; _vGenWrapper = vGenWrapper; _hidDeviceLoader = hidDeviceLoader; _virtualMappings = new Dictionary <XInputGamepadButtons, XInputGamepadButtons>(); foreach (var virtualMapping in _config.Mapping.VirtualKeysItems.Where(item => item.DestinationItem != null)) { var virtualPattern = virtualMapping.SourceKeys .Where(sourceKey => sourceKey != null) .Aggregate((XInputGamepadButtons)0, (current, sourceKey) => current | sourceKey.Value); // ReSharper disable once PossibleInvalidOperationException _virtualMappings[virtualPattern] = (XInputGamepadButtons)virtualMapping.DestinationItem; } }
public void Refresh() { _currentGamepadThread?.Abort(); _currentGamepadThread = new Thread(async() => { Thread.CurrentThread.IsBackground = true; var selectedConfigFile = Settings.Default.SelectedConfigFile; var selectedHidDevice = Settings.Default.SelectedHidDevice; var vJoyId = Settings.Default.SelectedJoyId; if (selectedConfigFile == string.Empty || selectedHidDevice == string.Empty) { Log("Configuration incomplete"); return; } ; GamepadConfiguration configuration = null; HidDeviceRepresentation hidDeviceRepresentation = null; try { configuration = await GetConfigFromFilePath(selectedConfigFile); hidDeviceRepresentation = JsonConvert.DeserializeObject <HidDeviceRepresentation>(selectedHidDevice); } catch (Exception ex) { Log($"Error while reading configuration: {ex.Message}"); } Log($"Using {hidDeviceRepresentation}, vJoy {vJoyId}, configuration file: {selectedConfigFile}"); SetupGamepad(hidDeviceRepresentation, vJoyId, configuration); }); _currentGamepadThread.Start(); }
private IObservable <uint> StartSingle(GamepadConfiguration gamepadConfiguration) { return(Observable.Create <uint>(observer => { var gamepad = new Gamepad(gamepadConfiguration, _vGenWrapper, _hidManager.HidDeviceLoader); gamepad.ErrorOccuredEvent += Gamepad_ErrorOccuredEvent; if (gamepad.Start()) { observer.OnNext(gamepadConfiguration.GamepadId); } else { gamepad.Dispose(); observer.OnError(GamepadStartException); } return Disposable.Create(() => { gamepad.Dispose(); gamepad.ErrorOccuredEvent -= Gamepad_ErrorOccuredEvent; }); })); }
public void SetupGamepad(HidDeviceRepresentation hidDeviceRepresentation, uint gamePadId, GamepadConfiguration config) { try { var device = _hidDeviceLoader.GetDevices( hidDeviceRepresentation.VendorId, hidDeviceRepresentation.ProductId, hidDeviceRepresentation.ProductVersion, hidDeviceRepresentation.SerialNumber ).First(); if (device == null) { Log(@"Failed to open device."); return; } HidStream stream; if (!device.TryOpen(out stream)) { Log("Failed to open device."); return; } var gamepad = new Gamepad(config, gamePadId); gamepad.ErrorOccuredEvent += Gamepad_ErrorOccuredEvent; Log("Successfully initialized gamepad"); using (stream) { while (true) { var bytes = new byte[device.MaxInputReportLength]; int count; try { count = stream.Read(bytes, 0, bytes.Length); } catch (TimeoutException) { continue; } catch (Exception ex) { Log(ex.Message); break; } if (count > 0) { gamepad.UpdateState(bytes); } } } } catch (Exception ex) { Log(ex.Message); } }
private GamepadConfiguration GamepadConfiguration_Micorsoft_Xbox(bool isXbox360) { var config = new GamepadConfiguration(); config.dpad_POV_Index = 0; config.dpadMode = DeviceDPadMode.POV; config.triggerButtonMode = DeviceTriggerButtonMode.Virtual; // primary buttons config.button1.index = 0; config.button2.index = 1; config.button3.index = 2; config.button4.index = 3; config.button1.name = "A"; config.button2.name = "B"; config.button3.name = "X"; config.button4.name = "Y"; // dpad config.dpadLeft.name = "Left"; config.dpadRight.name = "Right"; config.dpadDown.name = "Down"; config.dpadUp.name = "Up"; // options config.menu.index = 7; config.back.index = 6; config.menu.name = isXbox360 ? "Start" : "Menu"; config.back.name = "Back"; if (!isXbox360) // only some controllers supply this { config.home.index = 10; config.home.name = "Xbox"; } // bumbers config.bumperLeft.index = 4; config.bumperRight.index = 5; config.bumperLeft.name = "BL"; config.bumperRight.name = "BR"; // trigger buttons config.triggerButtonLeft.name = "TBL"; config.triggerButtonRight.name = "TBR"; // joystick buttons config.joystickButtonLeft.index = 8; config.joystickButtonRight.index = 9; config.joystickButtonLeft.name = "JBL"; config.joystickButtonRight.name = "JBR"; // triggers config.triggerLeft_Axis1D.updateMode = Axis1DUpdateMode.Positive; config.triggerRight_Axis1D.updateMode = Axis1DUpdateMode.Negitive; config.triggerLeft_Axis1D.name = "TL"; config.triggerRight_Axis1D.name = "TR"; config.axis1DMaps = new DeviceMapConfig_Axis1D[2]; config.axis1DMaps[0].axis1D_Src = 2; config.axis1DMaps[0].axis1D_Dst = GamepadConfiguration.MapConfig_Axis1D.triggerLeft; config.axis1DMaps[1].axis1D_Src = 2; config.axis1DMaps[1].axis1D_Dst = GamepadConfiguration.MapConfig_Axis1D.triggerRight; // joysticks config.joystickLeft_Axis2D.name = "JL"; config.joystickRight_Axis2D.name = "JR"; config.axis2DMaps = new DeviceMapConfig_Axis2D[2]; config.axis2DMaps[0].invertAxisY = true; config.axis2DMaps[0].axis1D_X_Src = 0; config.axis2DMaps[0].axis1D_Y_Src = 1; config.axis2DMaps[0].axis2D_Dst = GamepadConfiguration.MapConfig_Axis2D.joystickLeft; config.axis2DMaps[1].invertAxisY = true; config.axis2DMaps[1].axis1D_X_Src = 3; config.axis2DMaps[1].axis1D_Y_Src = 4; config.axis2DMaps[1].axis2D_Dst = GamepadConfiguration.MapConfig_Axis2D.joystickRight; return(config); }
private GamepadConfiguration GamepadConfiguration_Micorsoft_Xbox() { var config = new GamepadConfiguration(); // primary buttons config.button1.index = 0; config.button2.index = 1; config.button3.index = 2; config.button4.index = 3; config.button1.name = "A"; config.button2.name = "B"; config.button3.name = "X"; config.button4.name = "Y"; // dpad config.dpadLeft.index = 4; config.dpadRight.index = 5; config.dpadDown.index = 6; config.dpadUp.index = 7; config.dpadLeft.name = "Left"; config.dpadRight.name = "Right"; config.dpadDown.name = "Down"; config.dpadUp.name = "Up"; // options config.menu.index = 8; config.back.index = 9; config.menu.name = "Menu"; config.back.name = "Back"; // bumbers config.bumperLeft.index = 10; config.bumperRight.index = 11; config.bumperLeft.name = "BL"; config.bumperRight.name = "BR"; // trigger buttons config.triggerButtonLeft.index = 12; config.triggerButtonRight.index = 13; config.triggerButtonLeft.name = "TBL"; config.triggerButtonRight.name = "TBR"; // joystick buttons config.joystickButtonLeft.index = 14; config.joystickButtonRight.index = 15; config.joystickButtonLeft.name = "JBL"; config.joystickButtonRight.name = "JBR"; // triggers config.triggerLeft_Axis1D.index = 0; config.triggerRight_Axis1D.index = 1; config.triggerLeft_Axis1D.name = "TL"; config.triggerRight_Axis1D.name = "TR"; // triggers config.joystickLeft_Axis2D.index = 0; config.joystickRight_Axis2D.index = 1; config.joystickLeft_Axis2D.name = "JL"; config.joystickRight_Axis2D.name = "JR"; return(config); }