Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Implements <see cref="IXOutputInterface.Plugin(int)"/>
        /// </summary>
        /// <param name="controllerCount">number of controller</param>
        /// <returns>If it was successful</returns>
        public bool Plugin(int controllerCount)
        {
            var controller = new Xbox360Controller(client);

            controller.Connect();
            controllers.Add(controllerCount, controller);
            return(true);
        }
Ejemplo n.º 3
0
        protected override void OnOpen()
        {
            Xbox360Controller newController = new Xbox360Controller(Globals.client);

            newController.FeedbackReceived += connectClientToPort;

            newController.Connect();
        }
Ejemplo n.º 4
0
 internal void Activate()
 {
     try
     {
         emnulatedController.Connect();
         activated = true;
     }
     catch (Exception ex)
     { }
 }
Ejemplo n.º 5
0
        public Keyboard2Xinput(String mappingFile)
        {
            config = new Config(mappingFile);

            InitializeAxesDict();
            InitializeButtonsDict();
            log.Debug("initialize dicts done.");

            // start enabled?
            String startEnabledStr = config.getCurrentMapping()["startup"]["enabled"];

            // only start disabled if explicitly configured as such
            if ((startEnabledStr != null) && ("false".Equals(startEnabledStr.ToLower())))
            {
                enabled = false;
            }

            // try to init ViGEm
            try
            {
                client = new ViGEmClient();
            }
            catch (VigemBusNotFoundException e)
            {
                throw new ViGEmBusNotFoundException("ViGEm bus not found, please make sure ViGEm is correctly installed.", e);
            }
            // create pads
            controllers = new List <Xbox360Controller>(config.PadCount);
            reports     = new List <Xbox360Report>(config.PadCount);
            for (int i = 1; i <= config.PadCount; i++)
            {
                Xbox360Controller controller = new Xbox360Controller(client);
                controllers.Add(controller);
                controller.FeedbackReceived +=
                    (sender, eventArgs) => Console.WriteLine(
                        $"LM: {eventArgs.LargeMotor}, " +
                        $"SM: {eventArgs.SmallMotor}, " +
                        $"LED: {eventArgs.LedNumber}");

                controller.Connect();
                reports.Add(new Xbox360Report());
                Thread.Sleep(1000);
            }
            // the pressed buttons (to avoid sending reports if the pressed buttons haven't changed)
            pressedButtons = new List <ISet <Xbox360Buttons> >(config.PadCount);
            for (int i = 0; i < config.PadCount; i++)
            {
                pressedButtons.Add(new HashSet <Xbox360Buttons>());
            }
            listeners = new List <StateListener>();
        }
Ejemplo n.º 6
0
        public bool Initialize(uint id)
        {
            if (id <= 0 || (id > 15 && id < 1001) || id > 1004)
            {
                throw new ArgumentException("Invalid ID!", "id");
            }

            client = new ViGEmClient();
            ctrl   = new Xbox360Controller(client);

            ctrl.Connect();

            Initialized = true;
            return(true);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            var client = new ViGEmClient();

            var x360 = new Xbox360Controller(client);
            var id   = x360.ProductId;

            x360.FeedbackReceived +=
                (sender, eventArgs) => Console.WriteLine(
                    eventArgs.ToString() +
                    $"LM: {eventArgs.LargeMotor}, " +
                    $"SM: {eventArgs.SmallMotor}, " +
                    $"LED: {eventArgs.LedNumber}");
            x360.Connect();
            var id2 = x360.ProductId;

            Console.ReadLine();
        }
Ejemplo n.º 8
0
 public override bool Initialize(Action <Exception> FailedCallback)
 {
     try
     {
         myViGEmClient = new ViGEmClient();
         myController  = new Xbox360Controller(myViGEmClient);
         myController.Connect();
         myController.FeedbackReceived += MyController_FeedbackReceived;
         return(true);
     }
     catch (Exception ex)
     {
         if (ex is Nefarius.ViGEm.Client.Exceptions.VigemBusNotFoundException)
         {
             FailedCallback.Invoke(new Exception("没有找到ViGEm虚拟手柄驱动!"));
         }
         else
         {
             FailedCallback.Invoke(ex);
         }
         return(false);
     }
 }
Ejemplo n.º 9
0
 protected override void AcquireTarget()
 {
     target = new Xbox360Controller(_client);
     target.Connect();
 }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        public void DoProcessing(IProgress <string> message, IProgress <string> xUpdate, IProgress <string> rawXUpdate, IProgress <string> yUpdate, IProgress <string> rawYUpdate, Joystick joystick, Keyboard keyboard, CancellationToken cancellationToken)
        {
            ViGEmClient               client            = new ViGEmClient();
            Xbox360Controller         controller        = new Xbox360Controller(client);
            ControllerBindingsStorage bindingStorage    = new ControllerBindingsStorage();
            Dictionary <int, int>     bindings          = bindingStorage.load();
            Xbox360Axes?              analogStickAxisX  = null;
            Xbox360Axes?              analogStickAxisY  = null;
            Xbox360Axes?              digitalStickAxisX = null;
            Xbox360Axes?              digitalStickAxisY = null;
            Xbox360Buttons?           joystickButton0   = null;
            bool leftTriggerLastPressed       = false;
            bool leftTriggerPressed           = false;
            bool rightTriggerLastPressed      = false;
            bool rightTriggerPressed          = false;
            bool digitalStickUpLastPressed    = false;
            bool digitalStickUpPressed        = false;
            bool digitalStickDownLastPressed  = false;
            bool digitalStickDownPressed      = false;
            bool digitalStickLeftLastPressed  = false;
            bool digitalStickLeftPressed      = false;
            bool digitalStickRightLastPressed = false;
            bool digitalStickRightPressed     = false;

            foreach (KeyValuePair <int, int> binding in bindings)
            {
                if (binding.Key == (int)Xbox360StorageButtons.AnalogStick)
                {
                    AnalogStickBinding analogStickBinding = (AnalogStickBinding)Enum.Parse(typeof(AnalogStickBinding), binding.Value.ToString());

                    switch (analogStickBinding)
                    {
                    case AnalogStickBinding.LeftStick:
                        analogStickAxisX  = Xbox360Axes.LeftThumbX;
                        analogStickAxisY  = Xbox360Axes.LeftThumbY;
                        digitalStickAxisX = Xbox360Axes.RightThumbX;
                        digitalStickAxisY = Xbox360Axes.RightThumbY;

                        break;

                    case AnalogStickBinding.RightStick:
                        analogStickAxisX  = Xbox360Axes.RightThumbX;
                        analogStickAxisY  = Xbox360Axes.RightThumbY;
                        digitalStickAxisX = Xbox360Axes.LeftThumbX;
                        digitalStickAxisY = Xbox360Axes.LeftThumbY;

                        break;
                    }
                }

                if (binding.Value == (int)Key.JoystickButton)
                {
                    joystickButton0 = (Xbox360Buttons)Enum.Parse(typeof(Xbox360Buttons), binding.Key.ToString());
                }
            }

            controller.Connect();
            message.Report("Virtual Xbox 360 controller created!");

            Xbox360Report report = new Xbox360Report();

            while (true)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    controller.Disconnect();
                    return;
                }

                joystick.Poll();

                JoystickUpdate[] updates = joystick.GetBufferedData();

                foreach (JoystickUpdate update in updates)
                {
                    int value = (update.Value - 32767);

                    if (update.Offset == JoystickOffset.X || update.Offset == JoystickOffset.Y || update.Offset == JoystickOffset.Buttons0)
                    {
                        if (analogStickAxisX != null && analogStickAxisX.HasValue && analogStickAxisY != null && analogStickAxisY.HasValue)
                        {
                            if (update.Offset == JoystickOffset.X)
                            {
                                short x = getConstrainedValue(value);

                                report.SetAxis(analogStickAxisX.Value, x);
                                xUpdate.Report(x.ToString());
                                rawXUpdate.Report((update.Value - 32767).ToString());
                            }

                            if (update.Offset == JoystickOffset.Y)
                            {
                                short y = getConstrainedValue(value * -1);

                                report.SetAxis(analogStickAxisY.Value, y);
                                yUpdate.Report(y.ToString());
                                rawYUpdate.Report(((update.Value - 32767) * -1).ToString());
                            }
                        }

                        if (update.Offset == JoystickOffset.Buttons0 && joystickButton0 != null && joystickButton0.HasValue)
                        {
                            bool buttonValue = update.Value > 0 ? true : false;

                            report.SetButtonState(joystickButton0.Value, buttonValue);
                        }
                    }

                    message.Report(update.Offset.ToString() + ": " + value.ToString());
                }

                keyboard.Poll();

                KeyboardUpdate[] keyboardUpdates = keyboard.GetBufferedData();

                foreach (KeyboardUpdate keyboardUpdate in keyboardUpdates)
                {
                    foreach (KeyValuePair <int, int> binding in bindings)
                    {
                        if ((int)keyboardUpdate.Key == binding.Value)
                        {
                            Xbox360Buttons?button = null;

                            switch (binding.Key)
                            {
                            case (int)Xbox360StorageButtons.Guide:
                                button = Xbox360Buttons.Guide;
                                break;

                            case (int)Xbox360StorageButtons.Start:
                                button = Xbox360Buttons.Start;
                                break;

                            case (int)Xbox360StorageButtons.Back:
                                button = Xbox360Buttons.Back;
                                break;

                            case (int)Xbox360StorageButtons.LeftShoulder:
                                button = Xbox360Buttons.LeftShoulder;
                                break;

                            case (int)Xbox360StorageButtons.RightShoulder:
                                button = Xbox360Buttons.RightShoulder;
                                break;

                            case (int)Xbox360StorageButtons.LeftTrigger:
                                leftTriggerPressed = keyboardUpdate.Value == 128 ? true : false;
                                break;

                            case (int)Xbox360StorageButtons.RightTrigger:
                                rightTriggerPressed = keyboardUpdate.Value == 128 ? true : false;
                                break;

                            case (int)Xbox360StorageButtons.A:
                                button = Xbox360Buttons.A;
                                break;

                            case (int)Xbox360StorageButtons.B:
                                button = Xbox360Buttons.B;
                                break;

                            case (int)Xbox360StorageButtons.X:
                                button = Xbox360Buttons.X;
                                break;

                            case (int)Xbox360StorageButtons.Y:
                                button = Xbox360Buttons.Y;
                                break;

                            case (int)Xbox360StorageButtons.Up:
                                button = Xbox360Buttons.Up;
                                break;

                            case (int)Xbox360StorageButtons.Down:
                                button = Xbox360Buttons.Down;
                                break;

                            case (int)Xbox360StorageButtons.Left:
                                button = Xbox360Buttons.Left;
                                break;

                            case (int)Xbox360StorageButtons.Right:
                                button = Xbox360Buttons.Right;
                                break;

                            case (int)Xbox360StorageButtons.LeftThumb:
                                button = Xbox360Buttons.LeftThumb;
                                break;

                            case (int)Xbox360StorageButtons.RightThumb:
                                button = Xbox360Buttons.RightThumb;
                                break;

                            case (int)Xbox360StorageButtons.DigitalStickUp:
                                digitalStickUpPressed = keyboardUpdate.Value == 128 ? true : false;
                                break;

                            case (int)Xbox360StorageButtons.DigitalStickDown:
                                digitalStickDownPressed = keyboardUpdate.Value == 128 ? true : false;
                                break;

                            case (int)Xbox360StorageButtons.DigitalStickLeft:
                                digitalStickLeftPressed = keyboardUpdate.Value == 128 ? true : false;
                                break;

                            case (int)Xbox360StorageButtons.DigitalStickRight:
                                digitalStickRightPressed = keyboardUpdate.Value == 128 ? true : false;
                                break;
                            }

                            if (button != null && button.HasValue)
                            {
                                bool pressed = keyboardUpdate.Value == 128 ? true : false;

                                report.SetButtonState(button.Value, pressed);
                            }

                            message.Report("Keyboard - Key: " + keyboardUpdate.Key + " Value: " + keyboardUpdate.Value);
                        }
                    }
                }

                if (leftTriggerPressed != leftTriggerLastPressed)
                {
                    if (leftTriggerPressed)
                    {
                        report.LeftTrigger = 255;
                    }
                    else
                    {
                        report.LeftTrigger = 0;
                    }

                    leftTriggerLastPressed = leftTriggerPressed;
                }

                if (rightTriggerPressed != rightTriggerLastPressed)
                {
                    if (rightTriggerPressed)
                    {
                        report.RightTrigger = 255;
                    }
                    else
                    {
                        report.RightTrigger = 0;
                    }

                    rightTriggerLastPressed = rightTriggerPressed;
                }

                if (digitalStickUpPressed != digitalStickUpLastPressed)
                {
                    if (digitalStickUpPressed)
                    {
                        report.SetAxis(digitalStickAxisY.Value, 32767);
                    }
                    else
                    {
                        report.SetAxis(digitalStickAxisY.Value, 0);
                    }

                    digitalStickUpLastPressed = digitalStickUpPressed;
                }

                if (digitalStickDownPressed != digitalStickDownLastPressed)
                {
                    if (digitalStickDownPressed)
                    {
                        report.SetAxis(digitalStickAxisY.Value, -32768);
                    }
                    else
                    {
                        report.SetAxis(digitalStickAxisY.Value, 0);
                    }

                    digitalStickDownLastPressed = digitalStickDownPressed;
                }

                if (digitalStickLeftPressed != digitalStickLeftLastPressed)
                {
                    if (digitalStickLeftPressed)
                    {
                        report.SetAxis(digitalStickAxisX.Value, -32768);
                    }
                    else
                    {
                        report.SetAxis(digitalStickAxisX.Value, 0);
                    }

                    digitalStickLeftLastPressed = digitalStickLeftPressed;
                }

                if (digitalStickRightPressed != digitalStickRightLastPressed)
                {
                    if (digitalStickRightPressed)
                    {
                        report.SetAxis(digitalStickAxisX.Value, 32767);
                    }
                    else
                    {
                        report.SetAxis(digitalStickAxisX.Value, 0);
                    }

                    digitalStickRightLastPressed = digitalStickRightPressed;
                }

                controller.SendReport(report);
                Thread.Sleep(1);
            }
        }
Ejemplo n.º 12
0
 public override void Connect() => cont.Connect();
Ejemplo n.º 13
0
        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());
        }
Ejemplo n.º 14
0
        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();
        }
Ejemplo n.º 15
0
        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();
        }
Ejemplo n.º 16
0
 public override void Connect()
 {
     cont.Connect();
     connected = true;
 }
Ejemplo n.º 17
0
 public Xb360()
 {
     _controller = new Xbox360Controller(Client.ViGEmClient);
     _report     = new Xbox360Report();
     _controller.Connect();
 }