public void UpdateButtonStatus(byte LX, byte LY, byte RX, byte RY, byte L2, byte R2, byte Button_DPad_State, byte Button2_Status) { controller.SetAxisValue(Xbox360Axis.LeftThumbX, LeftThumbX = (short)((LX << 8) - 32768)); controller.SetAxisValue(Xbox360Axis.LeftThumbY, LeftThumbY = (short)~((LY << 8) - 32768)); controller.SetAxisValue(Xbox360Axis.RightThumbX, RightThumbX = (short)((RX << 8) - 32768)); controller.SetAxisValue(Xbox360Axis.RightThumbY, RightThumbY = (short)~((RY << 8) - 32768)); controller.SetSliderValue(Xbox360Slider.LeftTrigger, LeftTrigger = L2); controller.SetSliderValue(Xbox360Slider.RightTrigger, RightTrigger = R2); // controller.SetButtonState(Xbox360Button.Y, Y = (Button_DPad_State & (1 << 7)) > 0); controller.SetButtonState(Xbox360Button.B, B = (Button_DPad_State & (1 << 6)) > 0); controller.SetButtonState(Xbox360Button.A, A = (Button_DPad_State & (1 << 5)) > 0); controller.SetButtonState(Xbox360Button.X, X = (Button_DPad_State & (1 << 4)) > 0); int dPad = Button_DPad_State & 0x0F; controller.SetButtonState(Xbox360Button.Up, Up = (dPad == 0 || dPad == 1 || dPad == 7)); controller.SetButtonState(Xbox360Button.Right, Right = (dPad == 1 || dPad == 2 || dPad == 3)); controller.SetButtonState(Xbox360Button.Down, Down = (dPad == 3 || dPad == 4 || dPad == 5)); controller.SetButtonState(Xbox360Button.Left, Left = (dPad == 5 || dPad == 6 || dPad == 7)); // controller.SetButtonState(Xbox360Button.RightThumb, RightThumb = (Button2_Status & (1 << 7)) > 0); controller.SetButtonState(Xbox360Button.LeftThumb, LeftThumb = (Button2_Status & (1 << 6)) > 0); controller.SetButtonState(Xbox360Button.Start, Start = (Button2_Status & (1 << 5)) > 0); //Option controller.SetButtonState(Xbox360Button.Back, Back = (Button2_Status & (1 << 4)) > 0); //Share controller.SetButtonState(Xbox360Button.RightShoulder, RightShoulder = (Button2_Status & (1 << 1)) > 0); controller.SetButtonState(Xbox360Button.LeftShoulder, LeftShoulder = (Button2_Status & (1 << 0)) > 0); controller.SubmitReport(); }
internal void UpdateState(StateMapping mapping) { _controller.ResetReport(); _controller.SetButtonState(Xbox360Button.A, mapping.A); _controller.SetButtonState(Xbox360Button.B, mapping.B); _controller.SetButtonState(Xbox360Button.X, mapping.X); _controller.SetButtonState(Xbox360Button.Y, mapping.Y); _controller.SetButtonState(Xbox360Button.Back, mapping.Back); _controller.SetButtonState(Xbox360Button.Start, mapping.Start); _controller.SetButtonState(Xbox360Button.Guide, mapping.Guide); _controller.SetButtonState(Xbox360Button.LeftShoulder, mapping.LeftShoulder); _controller.SetButtonState(Xbox360Button.RightShoulder, mapping.RightShoulder); _controller.SetButtonState(Xbox360Button.LeftThumb, mapping.LeftThumb); _controller.SetButtonState(Xbox360Button.RightThumb, mapping.RightThumb); _controller.SetAxisValue(Xbox360Axis.LeftThumbX, mapping.LeftThumbX); _controller.SetAxisValue(Xbox360Axis.LeftThumbY, mapping.LeftThumbY); _controller.SetAxisValue(Xbox360Axis.RightThumbX, mapping.RightThumbX); _controller.SetAxisValue(Xbox360Axis.RightThumbY, mapping.RightThumbY); _controller.SetSliderValue(Xbox360Slider.LeftTrigger, mapping.LeftTrigger); _controller.SetSliderValue(Xbox360Slider.RightTrigger, mapping.RightTrigger); _controller.SetButtonState(Xbox360Button.Up, mapping.Up); _controller.SetButtonState(Xbox360Button.Down, mapping.Down); _controller.SetButtonState(Xbox360Button.Left, mapping.Left); _controller.SetButtonState(Xbox360Button.Right, mapping.Right); _controller.SubmitReport(); }
void SetReport(IXbox360Controller controller, GamepadState state) { PreprocessState(state); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Up, state.Up); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Down, state.Down); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Left, state.Left); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Right, state.Right); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.LeftThumbX, (short)state.LX); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.LeftThumbY, (short)state.LY); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.RightThumbX, (short)state.RX); controller.SetAxisValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Axis.RightThumbY, (short)state.RY); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.A, state.A); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.B, state.B); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.X, state.X); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Y, state.Y); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Back, state.Select); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.Start, state.Start); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.LeftShoulder, state.L1); controller.SetSliderValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Slider.LeftTrigger, (byte)(state.L2 ? 255 : 0)); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.LeftThumb, state.L3); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.RightShoulder, state.R1); controller.SetSliderValue(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Slider.RightTrigger, (byte)(state.R2 ? 255 : 0)); controller.SetButtonState(Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360Button.RightThumb, state.R3); }
private void DoUpdateInput(OutputControllerXbox360InputState new_state) { xbox_controller.SetButtonState(Xbox360Button.LeftThumb, new_state.thumb_stick_left); xbox_controller.SetButtonState(Xbox360Button.RightThumb, new_state.thumb_stick_right); xbox_controller.SetButtonState(Xbox360Button.Y, new_state.y); xbox_controller.SetButtonState(Xbox360Button.X, new_state.x); xbox_controller.SetButtonState(Xbox360Button.B, new_state.b); xbox_controller.SetButtonState(Xbox360Button.A, new_state.a); xbox_controller.SetButtonState(Xbox360Button.Start, new_state.start); xbox_controller.SetButtonState(Xbox360Button.Back, new_state.back); xbox_controller.SetButtonState(Xbox360Button.Guide, new_state.guide); xbox_controller.SetButtonState(Xbox360Button.Up, new_state.dpad_up); xbox_controller.SetButtonState(Xbox360Button.Right, new_state.dpad_right); xbox_controller.SetButtonState(Xbox360Button.Down, new_state.dpad_down); xbox_controller.SetButtonState(Xbox360Button.Left, new_state.dpad_left); xbox_controller.SetButtonState(Xbox360Button.LeftShoulder, new_state.shoulder_left); xbox_controller.SetButtonState(Xbox360Button.RightShoulder, new_state.shoulder_right); xbox_controller.SetAxisValue(Xbox360Axis.LeftThumbX, new_state.axis_left_x); xbox_controller.SetAxisValue(Xbox360Axis.LeftThumbY, new_state.axis_left_y); xbox_controller.SetAxisValue(Xbox360Axis.RightThumbX, new_state.axis_right_x); xbox_controller.SetAxisValue(Xbox360Axis.RightThumbY, new_state.axis_right_y); xbox_controller.SetSliderValue(Xbox360Slider.LeftTrigger, new_state.trigger_left); xbox_controller.SetSliderValue(Xbox360Slider.RightTrigger, new_state.trigger_right); xbox_controller.SubmitReport(); current_state = new_state; }
private void SetValueIfNeeded(Xbox360Slider slider, double?value) { if (value.HasValue) { var newValue = (byte)(value.Value * byte.MaxValue); controller.SetSliderValue(slider, newValue); } }
public void InputReportReceived(IDualShockDevice device, IInputReport report) { _target.ResetReport(); //This may be able to be optimized, look into later... // Convert report to DS3 format and store latest report for this device var ds3Report = (DualShock3InputReport)report; if (device.DeviceIndex == 0) { _Nav0Report = ds3Report; } if (device.DeviceIndex == 1) { _Nav1Report = ds3Report; } // Only combine reports and submit if we've seen input from each controller at least once if (_Nav0Report != null && _Nav1Report != null) { // Map buttons from Navigation #1 into input report _target.SetAxisValue(Xbox360Axis.LeftThumbX, Scale(_Nav0Report[DualShock3Axes.LeftThumbX], false)); _target.SetAxisValue(Xbox360Axis.LeftThumbY, Scale(_Nav0Report[DualShock3Axes.LeftThumbY], true)); _target.SetAxisValue(Xbox360Axis.RightThumbX, Scale(_Nav1Report[DualShock3Axes.LeftThumbX], false)); _target.SetAxisValue(Xbox360Axis.RightThumbY, Scale(_Nav1Report[DualShock3Axes.LeftThumbY], true)); _target.SetSliderValue(Xbox360Slider.LeftTrigger, _Nav0Report[DualShock3Axes.LeftTrigger]); _target.SetSliderValue(Xbox360Slider.RightTrigger, _Nav1Report[DualShock3Axes.LeftTrigger]); foreach (var button in _btnMap0.Where(m => _Nav0Report.EngagedButtons.Contains(m.Key)) .Select(m => m.Value)) { _target.SetButtonState(button, true); } foreach (var button in _btnMap1.Where(m => _Nav1Report.EngagedButtons.Contains(m.Key)) .Select(m => m.Value)) { _target.SetButtonState(button, true); } _target.SubmitReport(); } }
private void input_thread() { target360.Connect(); bool ss_button_pressed = false; bool ss_button_held = false; _stream.ReadTimeout = Timeout.Infinite; byte[] data = new byte[_device.GetMaxInputReportLength()]; while (running) { int read = 0; try { read = _stream.Read(data); } catch (TimeoutException e) { read = 0; } catch { unplug(joinInputThread: false); } if (read > 0) { if (data[0] == 0x03 && read == 10) { target360.ResetReport(); if ((data[3] & 64) != 0) { target360.SetButtonState(Xbox360Button.A, true); } if ((data[3] & 32) != 0) { target360.SetButtonState(Xbox360Button.B, true); } if ((data[3] & 16) != 0) { target360.SetButtonState(Xbox360Button.X, true); } if ((data[3] & 8) != 0) { target360.SetButtonState(Xbox360Button.Y, true); } if ((data[3] & 4) != 0) { target360.SetButtonState(Xbox360Button.LeftShoulder, true); } if ((data[3] & 2) != 0) { target360.SetButtonState(Xbox360Button.RightShoulder, true); } if ((data[3] & 1) != 0) { target360.SetButtonState(Xbox360Button.LeftThumb, true); } if ((data[2] & 128) != 0) { target360.SetButtonState(Xbox360Button.RightThumb, true); } ss_button_pressed = (data[2] & 1) != 0; //assistant_button_pressed = ( currentState[2] & 2 ) != 0; // [2] & 2 == Assistant, [2] & 1 == Screenshot switch (data[1]) { default: break; case 0: target360.SetButtonState(Xbox360Button.Up, true); break; case 1: target360.SetButtonState(Xbox360Button.Up, true); target360.SetButtonState(Xbox360Button.Right, true); break; case 2: target360.SetButtonState(Xbox360Button.Right, true); break; case 3: target360.SetButtonState(Xbox360Button.Down, true); target360.SetButtonState(Xbox360Button.Right, true); break; case 4: target360.SetButtonState(Xbox360Button.Down, true); break; case 5: target360.SetButtonState(Xbox360Button.Down, true); target360.SetButtonState(Xbox360Button.Left, true); break; case 6: target360.SetButtonState(Xbox360Button.Left, true); break; case 7: target360.SetButtonState(Xbox360Button.Up, true); target360.SetButtonState(Xbox360Button.Left, true); break; } if ((data[2] & 32) != 0) { target360.SetButtonState(Xbox360Button.Start, true); } if ((data[2] & 64) != 0) { target360.SetButtonState(Xbox360Button.Back, true); } if ((data[2] & 16) != 0) { target360.SetButtonState(Xbox360Button.Guide, true); } // Note: The HID reports do not allow stick values of 00. // This seems to make sense: 0x80 is center, so usable values are: // 0x01 to 0x7F and 0x81 to 0xFF. // For our purposes I believe this is undesirable. Subtract 1 from negative // values to allow maxing out the stick values. // TODO: Get an Xbox controller and verify this is standard behavior. for (int i = 4; i <= 7; ++i) { if (data[i] <= 0x7F && data[i] > 0x00) { data[i] -= 0x01; } } ushort LeftStickXunsigned = (ushort)(data[4] << 8 | (data[4] << 1 & 255)); if (LeftStickXunsigned == 0xFFFE) { LeftStickXunsigned = 0xFFFF; } short LeftStickX = (short)(LeftStickXunsigned - 0x8000); ushort LeftStickYunsigned = (ushort)(data[5] << 8 | (data[5] << 1 & 255)); if (LeftStickYunsigned == 0xFFFE) { LeftStickYunsigned = 0xFFFF; } short LeftStickY = (short)(-LeftStickYunsigned + 0x7FFF); if (LeftStickY == -1) { LeftStickY = 0; } ushort RightStickXunsigned = (ushort)(data[6] << 8 | (data[6] << 1 & 255)); if (RightStickXunsigned == 0xFFFE) { RightStickXunsigned = 0xFFFF; } short RightStickX = (short)(RightStickXunsigned - 0x8000); ushort RightStickYunsigned = (ushort)(data[7] << 8 | (data[7] << 1 & 255)); if (RightStickYunsigned == 0xFFFE) { RightStickYunsigned = 0xFFFF; } short RightStickY = (short)(-RightStickYunsigned + 0x7FFF); if (RightStickY == -1) { RightStickY = 0; } target360.SetAxisValue(Xbox360Axis.LeftThumbX, LeftStickX); target360.SetAxisValue(Xbox360Axis.LeftThumbY, LeftStickY); target360.SetAxisValue(Xbox360Axis.RightThumbX, RightStickX); target360.SetAxisValue(Xbox360Axis.RightThumbY, RightStickY); target360.SetSliderValue(Xbox360Slider.LeftTrigger, data[8]); target360.SetSliderValue(Xbox360Slider.RightTrigger, data[9]); target360.SubmitReport(); } } if (ss_button_pressed && !ss_button_held) { ss_button_held = true; try { // TODO: Allow configuring this keybind. ssThread = new Thread(() => System.Windows.Forms.SendKeys.SendWait("^+Z")); ssThread.Start(); } catch { } } else if (ss_button_held && !ss_button_pressed) { ss_button_held = false; } } target360.Disconnect(); }
static void Main(string[] args) { ViGEmClient client = new ViGEmClient(); IXbox360Controller controller = client.CreateXbox360Controller(); controller.AutoSubmitReport = false; controller.Connect(); using (Host server = new Host()) { server.InitializeServer(6789, 2); Event netEvent; while (true) { bool polled = false; while (!polled) { if (server.CheckEvents(out netEvent) == false) { if (server.Service(15, out netEvent) == false) { break; } polled = true; } switch (netEvent.Type) { case EventType.None: break; case EventType.Connect: Console.WriteLine("Client connected - ID: " + netEvent.ChannelID + ", IP: " + netEvent.Peer.Host); break; case EventType.Disconnect: Console.WriteLine("Client disconnected - ID: " + netEvent.ChannelID + ", IP: " + netEvent.Peer.Host); break; case EventType.Receive: Console.WriteLine("Packet received from - ID: " + netEvent.ChannelID + ", IP: " + netEvent.Peer.Host + ", Channel ID: " + netEvent.ChannelID + ", Data length: " + netEvent.Packet.Length); int t; Console.WriteLine(System.Text.Encoding.Default.GetString(netEvent.Packet.GetBytes())); if (Int32.TryParse(System.Text.Encoding.Default.GetString(netEvent.Packet.GetBytes()), out t)) { bool press = t < 20; if (!keyboard_mode) { if (t % 20 < 14) { controller.SetButtonState(t % 20, press); } else if (t % 20 == 14) { controller.SetSliderValue(Xbox360Slider.LeftTrigger, press ? (byte)0xFF : (byte)0x00); } else if (t % 20 == 15) { controller.SetSliderValue(Xbox360Slider.RightTrigger, press ? (byte)0xFF : (byte)0x00); } controller.SubmitReport(); } else { switch (t % 20) { case 0: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.D4).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.D4).Invoke(); } break; case 1: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.D5).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.D5).Invoke(); } break; case 2: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.D6).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.D6).Invoke(); } break; case 3: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.D7).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.D7).Invoke(); } break; case 4: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.R).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.R).Invoke(); } break; case 5: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.T).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.T).Invoke(); } break; case 6: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.Y).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.Y).Invoke(); } break; case 7: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.U).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.U).Invoke(); } break; case 8: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.F).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.F).Invoke(); } break; case 9: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.G).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.G).Invoke(); } break; case 10: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.H).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.H).Invoke(); } break; case 11: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.J).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.J).Invoke(); } break; case 12: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.V).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.V).Invoke(); } break; case 13: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.B).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.B).Invoke(); } break; case 14: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.N).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.N).Invoke(); } break; case 15: if (press) { WindowsInput.Simulate.Events().Hold(WindowsInput.Events.KeyCode.M).Invoke(); } else { WindowsInput.Simulate.Events().Release(WindowsInput.Events.KeyCode.M).Invoke(); } break; } } } netEvent.Packet.Dispose(); break; } } } server.Flush(); } controller.Disconnect(); }
public virtual bool Report(byte[] input, byte[] output) { byte id = (byte)(input[4] - 1); if (targets.Count <= id || targets[id] == null) { return(false); } IXbox360Controller controller = targets[id]; controller.ResetReport(); if ((input[10] & 32) != 0) { controller.SetButtonState(Xbox360Button.Back, true); } if ((input[10] & 64) != 0) { controller.SetButtonState(Xbox360Button.LeftThumb, true); } if ((input[10] & 128) != 0) { controller.SetButtonState(Xbox360Button.RightThumb, true); } if ((input[10] & 16) != 0) { controller.SetButtonState(Xbox360Button.Start, true); } if ((input[10] & 1) != 0) { controller.SetButtonState(Xbox360Button.Up, true); } if ((input[10] & 2) != 0) { controller.SetButtonState(Xbox360Button.Down, true); } if ((input[10] & 4) != 0) { controller.SetButtonState(Xbox360Button.Left, true); } if ((input[10] & 8) != 0) { controller.SetButtonState(Xbox360Button.Right, true); } if ((input[11] & 1) != 0) { controller.SetButtonState(Xbox360Button.LeftShoulder, true); } if ((input[11] & 2) != 0) { controller.SetButtonState(Xbox360Button.RightShoulder, true); } if ((input[11] & 128) != 0) { controller.SetButtonState(Xbox360Button.Y, true); } if ((input[11] & 32) != 0) { controller.SetButtonState(Xbox360Button.B, true); } if ((input[11] & 16) != 0) { controller.SetButtonState(Xbox360Button.A, true); } if ((input[11] & 64) != 0) { controller.SetButtonState(Xbox360Button.X, true); } if ((input[11] & 4) != 0) { controller.SetButtonState(Xbox360Button.Guide, true); } controller.SetSliderValue(Xbox360Slider.LeftTrigger, input[12]); controller.SetSliderValue(Xbox360Slider.RightTrigger, input[13]); controller.SetAxisValue(Xbox360Axis.LeftThumbX, BitConverter.ToInt16(input, 14)); controller.SetAxisValue(Xbox360Axis.LeftThumbY, BitConverter.ToInt16(input, 16)); controller.SetAxisValue(Xbox360Axis.RightThumbX, BitConverter.ToInt16(input, 18)); controller.SetAxisValue(Xbox360Axis.RightThumbY, BitConverter.ToInt16(input, 20)); controller.SubmitReport(); return(true); }
private void DeviceWorker() { _Logger.Information("Starting worker thread for {Device}", _Device); DisableReEnableDevice(); // Open HID device to read input from the gamepad _Logger.Information("Opening HID device {Device}", _Device); _Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive); ExclusiveMode = true; // If exclusive mode is not available, retry in shared mode. if (!_Device.IsOpen) { _Logger.Warning("Cannot access HID device in exclusive mode, retrying in shared mode: {Device}", _Device); _Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); ExclusiveMode = false; } if (_Device.IsOpen) { // Init Xiaomi Gamepad vibration _Device.WriteFeatureData(new byte[] { 0x20, 0x00, 0x00 }); // Connect the virtual Xbox360 gamepad try { _Logger.Information("Connecting to ViGEm client"); _Target.Connect(); } catch (VigemAlreadyConnectedException e) { _Logger.Warning(e, "ViGEm client was already opened, closing and reopening it"); _Target.Disconnect(); _Target.Connect(); } Started?.Invoke(this, EventArgs.Empty); HidReport hidReport; while (!_CTS.Token.IsCancellationRequested) { // Is device has been closed, exit the loop if (!_Device.IsOpen) { break; } // Otherwise read a report hidReport = _Device.ReadReport(1000); if (hidReport.ReadStatus == HidDeviceData.ReadStatus.WaitTimedOut) { continue; } else if (hidReport.ReadStatus != HidDeviceData.ReadStatus.Success) { _Logger.Error("Cannot read HID report for device {Device}, got {Report}", _Device, hidReport.ReadStatus); break; } var data = hidReport.Data; /* * [0] Buttons state, 1 bit per button * [1] Buttons state, 1 bit per button * [2] 0x00 * [3] D-Pad * [4] Left thumb, X axis * [5] Left thumb, Y axis * [6] Right thumb, X axis * [7] Right thumb, Y axis * [8] 0x00 * [9] 0x00 * [10] L trigger * [11] R trigger * [12] Accelerometer axis 1 * [13] Accelerometer axis 1 * [14] Accelerometer axis 2 * [15] Accelerometer axis 2 * [16] Accelerometer axis 3 * [17] Accelerometer axis 3 * [18] Battery level * [19] MI button */ lock (_Target) { _Target.SetButtonState(Xbox360Button.A, GetBit(data[0], 0)); _Target.SetButtonState(Xbox360Button.B, GetBit(data[0], 1)); _Target.SetButtonState(Xbox360Button.X, GetBit(data[0], 3)); _Target.SetButtonState(Xbox360Button.Y, GetBit(data[0], 4)); _Target.SetButtonState(Xbox360Button.LeftShoulder, GetBit(data[0], 6)); _Target.SetButtonState(Xbox360Button.RightShoulder, GetBit(data[0], 7)); _Target.SetButtonState(Xbox360Button.Back, GetBit(data[1], 2)); _Target.SetButtonState(Xbox360Button.Start, GetBit(data[1], 3)); _Target.SetButtonState(Xbox360Button.LeftThumb, GetBit(data[1], 5)); _Target.SetButtonState(Xbox360Button.RightThumb, GetBit(data[1], 6)); // Reset Hat switch status, as is set to 15 (all directions set, impossible state) _Target.SetButtonState(Xbox360Button.Up, false); _Target.SetButtonState(Xbox360Button.Left, false); _Target.SetButtonState(Xbox360Button.Down, false); _Target.SetButtonState(Xbox360Button.Right, false); if (data[3] < 8) { var btns = HatSwitches[data[3]]; // Hat Switch is a number from 0 to 7, where 0 is Up, 1 is Up-Left, etc. foreach (var b in btns) { _Target.SetButtonState(b, true); } } // Analog axis _Target.SetAxisValue(Xbox360Axis.LeftThumbX, MapAnalog(data[4])); _Target.SetAxisValue(Xbox360Axis.LeftThumbY, MapAnalog(data[5], true)); _Target.SetAxisValue(Xbox360Axis.RightThumbX, MapAnalog(data[6])); _Target.SetAxisValue(Xbox360Axis.RightThumbY, MapAnalog(data[7], true)); // Triggers _Target.SetSliderValue(Xbox360Slider.LeftTrigger, data[10]); _Target.SetSliderValue(Xbox360Slider.RightTrigger, data[11]); // Logo ("home") button if (GetBit(data[19], 0)) { _Target.SetButtonState(Xbox360Button.Guide, true); Task.Delay(200).ContinueWith(DelayedReleaseGuideButton); } // Update battery level BatteryLevel = data[18]; _Target.SubmitReport(); } } // Disconnect the virtual Xbox360 gamepad // Let Dispose handle that, otherwise it will rise a NotPluggedIn exception _Logger.Information("Disconnecting ViGEm client"); _Target.Disconnect(); // Close the HID device _Logger.Information("Closing HID device {Device}", _Device); _Device.CloseDevice(); DisableReEnableDevice(); } else { _Logger.Error("Cannot open HID device {Device}", _Device); Ended?.Invoke(this, EventArgs.Empty); return; } _Logger.Information("Exiting worker thread for {0}", _Device.ToString()); Ended?.Invoke(this, EventArgs.Empty); }
public void UpdateController(IXbox360Controller controller) { // Only update controller if it is plugged in. if (IsPlugged) { controller.SetButtonState(Xbox360Button.A, BUTTON_B); // Button layouts on Xbox controllers are swapped from Nintendo layout controller.SetButtonState(Xbox360Button.B, BUTTON_A); controller.SetButtonState(Xbox360Button.X, BUTTON_Y); controller.SetButtonState(Xbox360Button.Y, BUTTON_X); controller.SetButtonState(Xbox360Button.Left, DPAD_LEFT); controller.SetButtonState(Xbox360Button.Right, DPAD_RIGHT); controller.SetButtonState(Xbox360Button.Up, DPAD_UP); controller.SetButtonState(Xbox360Button.Down, DPAD_DOWN); controller.SetButtonState(Xbox360Button.Start, BUTTON_START); // If enabled, the triggers act as digital buttons. if (ApplicationSettings.EnableDigitalPress) { if ((ANALOG_LEFT / 255f) > ApplicationSettings.TriggerDeadzone) { controller.SetButtonState(Xbox360Button.LeftShoulder, true); } else { controller.SetButtonState(Xbox360Button.LeftShoulder, false); } if ((ANALOG_RIGHT / 255f) > ApplicationSettings.TriggerDeadzone) { controller.SetButtonState(Xbox360Button.RightShoulder, true); } else { controller.SetButtonState(Xbox360Button.RightShoulder, false); } if (BUTTON_Z) { controller.SetSliderValue(Xbox360Slider.RightTrigger, 255); } else { controller.SetSliderValue(Xbox360Slider.RightTrigger, 0); } } // Use normal triggerThreshold/deadzone values else { controller.SetSliderValue(Xbox360Slider.LeftTrigger, ANALOG_LEFT); controller.SetSliderValue(Xbox360Slider.RightTrigger, ANALOG_RIGHT); controller.SetButtonState(Xbox360Button.RightShoulder, BUTTON_Z); } // Check if calibration has been done. If not generate it if (_Calibration.CurrentState == CalibrationState.Calibrating) { _Calibration.GenerateCalibrations(); } // Full calibration scales generated if (_Calibration.CurrentState == CalibrationState.Calibrated) { LeftStickX = Math.Round(LEFT_STICK_X * _Calibration.LeftStickCalibration[0] - _Calibration.LeftStickCalibration[2]); LeftStickY = Math.Round(LEFT_STICK_Y * _Calibration.LeftStickCalibration[1] - _Calibration.LeftStickCalibration[3]); CStickX = Math.Round(C_STICK_X * _Calibration.CStickCalibration[0] - _Calibration.CStickCalibration[2]); CStickY = Math.Round(C_STICK_Y * _Calibration.CStickCalibration[1] - _Calibration.CStickCalibration[3]); } // Stick centers generated else if (_Calibration.CurrentState == CalibrationState.SticksCentered) { LeftStickX = LEFT_STICK_X + _Calibration.StickCenters[0]; LeftStickY = LEFT_STICK_Y + _Calibration.StickCenters[1]; CStickX = C_STICK_X + _Calibration.StickCenters[2]; CStickY = C_STICK_Y + _Calibration.StickCenters[3]; } // No calibration done else { LeftStickX = LEFT_STICK_X; LeftStickY = LEFT_STICK_Y; CStickX = C_STICK_X; CStickY = C_STICK_Y; } LeftStickX = Extensions.ByteToShort((byte)Extensions.Clamp(LeftStickX, 0, 255)); // ensure stick values are within byte range LeftStickY = Extensions.ByteToShort((byte)Extensions.Clamp(LeftStickY, 0, 255)); CStickX = Extensions.ByteToShort((byte)Extensions.Clamp(CStickX, 0, 255)); CStickY = Extensions.ByteToShort((byte)Extensions.Clamp(CStickY, 0, 255)); if (GetDeadzone((float)LeftStickX, (float)LeftStickY)) // This means they are within the deadzone, take no input { LeftStickX = 0; LeftStickY = 0; } if (GetDeadzone((float)CStickX, (float)CStickY)) { CStickX = 0; CStickY = 0; } controller.SetAxisValue(Xbox360Axis.LeftThumbX, (short)LeftStickX); controller.SetAxisValue(Xbox360Axis.LeftThumbY, (short)LeftStickY); controller.SetAxisValue(Xbox360Axis.RightThumbX, (short)CStickX); controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)CStickY); controller.FeedbackReceived += Controller_FeedbackReceived; controller.SubmitReport(); } }