private static void Main(string[] args) { var x360 = new Xbox360Controller(); x360.FeedbackReceived += (sender, eventArgs) => Console.WriteLine( $"LM: {eventArgs.LargeMotor}, " + $"SM: {eventArgs.SmallMotor}, " + $"LED: {eventArgs.LedNumber}"); x360.PlugIn(); var report = new Xbox360Report(); report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B); report.SetAxis(Xbox360Axes.LeftTrigger, 0xFF); report.SetAxis(Xbox360Axes.RightTrigger, 0xFF); x360.SendReport(report); Console.ReadKey(); var ds4 = new DualShock4Controller(); ds4.FeedbackReceived += (sender, eventArgs) => Console.WriteLine( $"LM: {eventArgs.LargeMotor}, " + $"SM: {eventArgs.SmallMotor}, "); ds4.PlugIn(); Console.ReadKey(); }
private static void Main(string[] args) { var client = new ViGEmClient(); #if X360 var x360 = new Xbox360Controller(client); x360.FeedbackReceived += (sender, eventArgs) => Console.WriteLine( $"LM: {eventArgs.LargeMotor}, " + $"SM: {eventArgs.SmallMotor}, " + $"LED: {eventArgs.LedNumber}"); x360.Connect(); var report = new Xbox360Report(); report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B); report.SetAxis(Xbox360Axes.LeftTrigger, 0xFF); report.SetAxis(Xbox360Axes.RightTrigger, 0xFF); x360.SendReport(report); #endif var ds4 = new DualShock4Controller(client); ds4.Connect(); var report = new DualShock4Report(); report.SetButtons(DualShock4Buttons.Cross); ds4.SendReport(report); /* * var x360 = new Xbox360Controller(); * x360.FeedbackReceived += * (sender, eventArgs) => Console.WriteLine( * $"LM: {eventArgs.LargeMotor}, " + * $"SM: {eventArgs.SmallMotor}, " + * $"LED: {eventArgs.LedNumber}"); * x360.PlugIn(); * * var report = new Xbox360Report(); * report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B); * report.SetAxis(Xbox360Axes.LeftTrigger, 0xFF); * report.SetAxis(Xbox360Axes.RightTrigger, 0xFF); * * x360.SendReport(report); * * Console.ReadKey(); * * var ds4 = new DualShock4Controller(); * ds4.FeedbackReceived += * (sender, eventArgs) => Console.WriteLine( * $"LM: {eventArgs.LargeMotor}, " + * $"SM: {eventArgs.SmallMotor}, "); * ds4.PlugIn(); */ Console.ReadKey(); }
public void OnKeyArray(byte[] buffer, Xbox360Report controllerReport) { _holdKeyCache[0] = 0; _holdKeyCache[1] = 0; _holdKeyCache[2] = 0; _holdKeyCache[3] = 0; for (int i = 0; i < buffer.Length; i++) { int target; if (this.config.Keymap.TryGetValue(buffer[i], out target)) { if (!HoldKeyCache.Contains((Xbox360Buttons)target)) { ParseKey((Xbox360Buttons)target, controllerReport); } _holdKeyCache[i] = (Xbox360Buttons)target; } } foreach (var _hk in HoldKeyCache) { if (!_holdKeyCache.Contains(_hk)) { ParseKey(_hk, controllerReport, 2); } } controllerReport.SetButtons(_holdKeyCache.Where(k => k > 0).ToArray()); HoldKeyCache = _holdKeyCache; }
private void DeviceWorker() { Console.WriteLine("Starting worker thread for {0}", _Device.ToString()); // Open HID device to read input from the gamepad _Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); // Init Xiaomi Gamepad vibration _Device.WriteFeatureData(new byte[] { 0x20, 0x00, 0x00 }); // Connect the virtual Xbox360 gamepad try { _Target.Connect(); } catch (VigemAlreadyConnectedException e) { _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) { Console.WriteLine("Device {0}: error while reading HID report, {1}", _Device.ToString(), hidReport.ReadStatus.ToString()); 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 (_Report) { _Report.SetButtonState(Xbox360Buttons.A, GetBit(data[0], 0)); _Report.SetButtonState(Xbox360Buttons.B, GetBit(data[0], 1)); _Report.SetButtonState(Xbox360Buttons.X, GetBit(data[0], 3)); _Report.SetButtonState(Xbox360Buttons.Y, GetBit(data[0], 4)); _Report.SetButtonState(Xbox360Buttons.LeftShoulder, GetBit(data[0], 6)); _Report.SetButtonState(Xbox360Buttons.RightShoulder, GetBit(data[0], 7)); _Report.SetButtonState(Xbox360Buttons.Back, GetBit(data[1], 2)); _Report.SetButtonState(Xbox360Buttons.Start, GetBit(data[1], 3)); _Report.SetButtonState(Xbox360Buttons.LeftThumb, GetBit(data[1], 5)); _Report.SetButtonState(Xbox360Buttons.RightThumb, GetBit(data[1], 6)); // Reset Hat switch status, as is set to 15 (all directions set, impossible state) _Report.SetButtonState(Xbox360Buttons.Up, false); _Report.SetButtonState(Xbox360Buttons.Left, false); _Report.SetButtonState(Xbox360Buttons.Down, false); _Report.SetButtonState(Xbox360Buttons.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. _Report.SetButtons(btns); } // Analog axis _Report.SetAxis(Xbox360Axes.LeftThumbX, MapAnalog(data[4])); _Report.SetAxis(Xbox360Axes.LeftThumbY, MapAnalog(data[5], true)); _Report.SetAxis(Xbox360Axes.RightThumbX, MapAnalog(data[6])); _Report.SetAxis(Xbox360Axes.RightThumbY, MapAnalog(data[7], true)); // Triggers _Report.SetAxis(Xbox360Axes.LeftTrigger, data[10]); _Report.SetAxis(Xbox360Axes.RightTrigger, data[11]); // Logo ("home") button if (GetBit(data[19], 0)) { _Report.SetButtonState((Xbox360Buttons)0x0400, true); Task.Delay(200).ContinueWith(DelayedReleaseGuideButton); } // Update battery level BatteryLevel = data[18]; _Target.SendReport(_Report); } } // Disconnect the virtual Xbox360 gamepad // Let Dispose handle that, otherwise it will rise a NotPluggedIn exception //_Target.Disconnect(); // Close the HID device _Device.CloseDevice(); Console.WriteLine("Exiting worker thread for {0}", _Device.ToString()); Ended?.Invoke(this, EventArgs.Empty); }
private void DeviceWorker() { Console.WriteLine("Starting worker thread for {0}", _Device.ToString()); // Open HID device to read input from the gamepad _Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); // Init Xiaomi Gamepad vibration _Device.WriteFeatureData(new byte[] { 0x20, 0x00, 0x00 }); // Connect the virtual Xbox360 gamepad try { _Target.Connect(); } catch (VigemAlreadyConnectedException e) { _Target.Disconnect(); _Target.Connect(); } HidReport hidReport; Xbox360Report xInputReport = new Xbox360Report(); 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) { Console.WriteLine("Device {0}: error while reading HID report, {1}", _Device.ToString(), hidReport.ReadStatus.ToString()); break; } var data = hidReport.Data; xInputReport.SetButtonState(Xbox360Buttons.A, GetBit(data[0], 0)); xInputReport.SetButtonState(Xbox360Buttons.B, GetBit(data[0], 1)); xInputReport.SetButtonState(Xbox360Buttons.X, GetBit(data[0], 3)); xInputReport.SetButtonState(Xbox360Buttons.Y, GetBit(data[0], 4)); xInputReport.SetButtonState(Xbox360Buttons.LeftShoulder, GetBit(data[0], 6)); xInputReport.SetButtonState(Xbox360Buttons.RightShoulder, GetBit(data[0], 7)); xInputReport.SetButtonState(Xbox360Buttons.Back, GetBit(data[1], 2)); xInputReport.SetButtonState(Xbox360Buttons.Start, GetBit(data[1], 3)); xInputReport.SetButtonState(Xbox360Buttons.LeftThumb, GetBit(data[1], 5)); xInputReport.SetButtonState(Xbox360Buttons.RightThumb, GetBit(data[1], 6)); // Reset Hat switch status, as is set to 15 (all directions set, impossible state) xInputReport.SetButtonState(Xbox360Buttons.Up, false); xInputReport.SetButtonState(Xbox360Buttons.Left, false); xInputReport.SetButtonState(Xbox360Buttons.Down, false); xInputReport.SetButtonState(Xbox360Buttons.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. xInputReport.SetButtons(btns); } // Analog axis xInputReport.SetAxis(Xbox360Axes.LeftThumbX, MapAnalog(data[4])); xInputReport.SetAxis(Xbox360Axes.LeftThumbY, MapAnalog(data[5], true)); xInputReport.SetAxis(Xbox360Axes.RightThumbX, MapAnalog(data[6])); xInputReport.SetAxis(Xbox360Axes.RightThumbY, MapAnalog(data[7], true)); // Triggers xInputReport.SetAxis(Xbox360Axes.LeftTrigger, data[10]); xInputReport.SetAxis(Xbox360Axes.RightTrigger, data[11]); // Logo ("home") button if (GetBit(data[19], 0)) { _LogoButtonActive = true; _LogoButtonTimer = _LogoButtonTimer ?? new Timer(DeactivateLogoButton, null, Timeout.Infinite, Timeout.Infinite); _LogoButtonTimer.Change(200, Timeout.Infinite); } xInputReport.SetButtonState((Xbox360Buttons)0x0400, _LogoButtonActive); XInputReport = xInputReport; _Target.SendReport(xInputReport); } // Disconnect the virtual Xbox360 gamepad // Let Dispose handle that, otherwise it will rise a NotPluggedIn exception //_Target.Disconnect(); // Close the HID device _Device.CloseDevice(); Console.WriteLine("Exiting worker thread for {0}", _Device.ToString()); }
private static void Main(string[] args) { var client = new ViGEmClient(); #if X360 var x360 = new Xbox360Controller(client); x360.FeedbackReceived += (sender, eventArgs) => Console.WriteLine( $"LM: {eventArgs.LargeMotor}, " + $"SM: {eventArgs.SmallMotor}, " + $"LED: {eventArgs.LedNumber}"); x360.Connect(); var report = new Xbox360Report(); report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B); report.SetAxis(Xbox360Axes.LeftTrigger, 0xFF); report.SetAxis(Xbox360Axes.RightTrigger, 0xFF); x360.SendReport(report); #endif Console.Title = "ViGEm Benchmark - CLOSE THIS WINDOW TO STOP!"; Console.WriteLine(Console.Title); var controllers = new ObservableCollection <DualShock4Controller>(); controllers.CollectionChanged += (sender, eventArgs) => { switch (eventArgs.Action) { case NotifyCollectionChangedAction.Add: Console.WriteLine("Added Controller"); break; case NotifyCollectionChangedAction.Remove: Console.WriteLine("Removed Controller"); if (((ObservableCollection <DualShock4Controller>)sender).Count == 0) { Console.WriteLine(" > Finished! Press any key to exit!"); } break; } }; for (int i = 0; i < 500; i++) { Console.WriteLine($" > Spawning device {i}"); Task.Run(() => { var ds4 = new DualShock4Controller(client); try { lock (controllers) controllers.Add(ds4); ds4.Connect(); var report = new DualShock4Report(); report.SetButtons(DualShock4Buttons.Cross); ds4.SendReport(report); Thread.Sleep(1000); ds4.Disconnect(); } finally { lock (controllers) controllers.Remove(ds4); } }); Thread.Sleep(20); } /* * var x360 = new Xbox360Controller(); * x360.FeedbackReceived += * (sender, eventArgs) => Console.WriteLine( * $"LM: {eventArgs.LargeMotor}, " + * $"SM: {eventArgs.SmallMotor}, " + * $"LED: {eventArgs.LedNumber}"); * x360.PlugIn(); * * var report = new Xbox360Report(); * report.SetButtons(Xbox360Buttons.A, Xbox360Buttons.B); * report.SetAxis(Xbox360Axes.LeftTrigger, 0xFF); * report.SetAxis(Xbox360Axes.RightTrigger, 0xFF); * * x360.SendReport(report); * * Console.ReadKey(); * * var ds4 = new DualShock4Controller(); * ds4.FeedbackReceived += * (sender, eventArgs) => Console.WriteLine( * $"LM: {eventArgs.LargeMotor}, " + * $"SM: {eventArgs.SmallMotor}, "); * ds4.PlugIn(); */ Console.ReadKey(); }
static void Main(string[] args) { var client = new ViGEmClient(); var x360 = new Xbox360Controller(client); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings["bindAddr"]), UInt16.Parse(ConfigurationManager.AppSettings["bindPort"])); UdpClient newsock = new UdpClient(ipep); IPEndPoint senderep = new IPEndPoint(IPAddress.Any, 0); x360.FeedbackReceived += (sender, eventArgs) => { }; x360.Connect(); Console.WriteLine("Controller connected."); Console.WriteLine("Listening on {0}", ipep); var report = new Xbox360Report(); var lastRecv = DateTime.Now; float tt = 0; int second_counter = 0; while (true) { var dt_t = DateTime.Now - lastRecv; lastRecv = DateTime.Now; float dt = dt_t.Milliseconds / 1000.0f; InputStatus status; unsafe { fixed(byte *data = &newsock.Receive(ref senderep)[0]) { InputStatus *s = (InputStatus *)data; status = *s; } } report.Buttons = 0; for (int i = 0; i < 32; ++i) { if ((status.kHeld & (1 << i)) != 0) { Xbox360Buttons?button = X3DSMap[i]; if (button != null) { report.SetButtons(button.Value); } } } if (status.touch.px > 0 && status.touch.py > 0) { if (status.touch.px < 320 / 3) { report.SetButtons(Xbox360Buttons.LeftThumb); } else if (status.touch.px > 320 / 3 * 2) { report.SetButtons(Xbox360Buttons.RightThumb); } else { report.SetButtons(Xbox360Buttons.LeftThumb, Xbox360Buttons.RightThumb); } } report.SetAxis(Xbox360Axes.LeftThumbX, (short)((float)status.pad.dx * Int16.MaxValue / 160.0f)); report.SetAxis(Xbox360Axes.LeftThumbY, (short)((float)status.pad.dy * Int16.MaxValue / 160.0f)); report.SetAxis(Xbox360Axes.RightThumbX, (short)((float)status.cstick.dx * Int16.MaxValue / 160.0f)); report.SetAxis(Xbox360Axes.RightThumbY, (short)((float)status.cstick.dy * Int16.MaxValue / 160.0f)); if ((status.kHeld & (1 << 14)) != 0) { report.SetAxis(Xbox360Axes.LeftTrigger, Int16.MaxValue); } else { report.SetAxis(Xbox360Axes.LeftTrigger, Int16.MinValue); } if ((status.kHeld & (1 << 15)) != 0) { report.SetAxis(Xbox360Axes.RightTrigger, Int16.MaxValue); } else { report.SetAxis(Xbox360Axes.RightTrigger, Int16.MinValue); } x360.SendReport(report); second_counter++; tt += dt; if (tt > 1.0f) { Console.WriteLine("{0} packet received", second_counter); tt = 0; second_counter = 0; } } Console.ReadKey(); }
public override void ConvertAndSendReport(DS4State state, int device) { Xbox360Buttons tempButtons = 0; unchecked { if (state.Share) { tempButtons |= Xbox360Buttons.Back; } if (state.L3) { tempButtons |= Xbox360Buttons.LeftThumb; } if (state.R3) { tempButtons |= Xbox360Buttons.RightThumb; } if (state.Options) { tempButtons |= Xbox360Buttons.Start; } if (state.DpadUp) { tempButtons |= Xbox360Buttons.Up; } if (state.DpadRight) { tempButtons |= Xbox360Buttons.Right; } if (state.DpadDown) { tempButtons |= Xbox360Buttons.Down; } if (state.DpadLeft) { tempButtons |= Xbox360Buttons.Left; } if (state.L1) { tempButtons |= Xbox360Buttons.LeftShoulder; } if (state.R1) { tempButtons |= Xbox360Buttons.RightShoulder; } if (state.Triangle) { tempButtons |= Xbox360Buttons.Y; } if (state.Circle) { tempButtons |= Xbox360Buttons.B; } if (state.Cross) { tempButtons |= Xbox360Buttons.A; } if (state.Square) { tempButtons |= Xbox360Buttons.X; } if (state.PS) { tempButtons |= Xbox360Buttons.Guide; } report.SetButtons(tempButtons); } report.LeftTrigger = state.L2; report.RightTrigger = state.R2; SASteeringWheelEmulationAxisType steeringWheelMappedAxis = Global.GetSASteeringWheelEmulationAxis(device); switch (steeringWheelMappedAxis) { case SASteeringWheelEmulationAxisType.None: report.LeftThumbX = AxisScale(state.LX, false); report.LeftThumbY = AxisScale(state.LY, true); report.RightThumbX = AxisScale(state.RX, false); report.RightThumbY = AxisScale(state.RY, true); break; case SASteeringWheelEmulationAxisType.LX: report.LeftThumbX = (short)state.SASteeringWheelEmulationUnit; report.LeftThumbY = AxisScale(state.LY, true); report.RightThumbX = AxisScale(state.RX, false); report.RightThumbY = AxisScale(state.RY, true); break; case SASteeringWheelEmulationAxisType.LY: report.LeftThumbX = AxisScale(state.LX, false); report.LeftThumbY = (short)state.SASteeringWheelEmulationUnit; report.RightThumbX = AxisScale(state.RX, false); report.RightThumbY = AxisScale(state.RY, true); break; case SASteeringWheelEmulationAxisType.RX: report.LeftThumbX = AxisScale(state.LX, false); report.LeftThumbY = AxisScale(state.LY, true); report.RightThumbX = (short)state.SASteeringWheelEmulationUnit; report.RightThumbY = AxisScale(state.RY, true); break; case SASteeringWheelEmulationAxisType.RY: report.LeftThumbX = AxisScale(state.LX, false); report.LeftThumbY = AxisScale(state.LY, true); report.RightThumbX = AxisScale(state.RX, false); report.RightThumbY = (short)state.SASteeringWheelEmulationUnit; break; case SASteeringWheelEmulationAxisType.L2R2: report.LeftTrigger = report.RightTrigger = 0; if (state.SASteeringWheelEmulationUnit >= 0) { report.LeftTrigger = (byte)state.SASteeringWheelEmulationUnit; } else { report.RightTrigger = (byte)state.SASteeringWheelEmulationUnit; } goto case SASteeringWheelEmulationAxisType.None; case SASteeringWheelEmulationAxisType.VJoy1X: case SASteeringWheelEmulationAxisType.VJoy2X: VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, VJoyFeeder.HID_USAGES.HID_USAGE_X); goto case SASteeringWheelEmulationAxisType.None; case SASteeringWheelEmulationAxisType.VJoy1Y: case SASteeringWheelEmulationAxisType.VJoy2Y: VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, VJoyFeeder.HID_USAGES.HID_USAGE_Y); goto case SASteeringWheelEmulationAxisType.None; case SASteeringWheelEmulationAxisType.VJoy1Z: case SASteeringWheelEmulationAxisType.VJoy2Z: VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, VJoyFeeder.HID_USAGES.HID_USAGE_Z); goto case SASteeringWheelEmulationAxisType.None; default: // Should never come here but just in case use the NONE case as default handler.... goto case SASteeringWheelEmulationAxisType.None; } Controller.SendReport(report); }