protected void moveStick(Direction x, Direction y, Xbox360Controller controller)
        {
            if (controller != null && (x != Direction.Down || y != Direction.Down))
            {
                for (short i = 0; i < 32767; i++)
                {
                    Xbox360Report report = new Xbox360Report();

                    if (x == Direction.Up)
                    {
                        report.LeftThumbX = i;
                    }
                    else if (x == Direction.Down)
                    {
                        report.LeftThumbX = (short)(i * -1);
                    }

                    if (y == Direction.Up)
                    {
                        report.LeftThumbY = i;
                    }
                    else if (y == Direction.Down)
                    {
                        report.LeftThumbY = (short)(i * -1);
                    }

                    controller.SendReport(report);
                }

                for (short i = 32767; i > 0; i--)
                {
                    Xbox360Report report = new Xbox360Report();

                    if (x == Direction.Up)
                    {
                        report.LeftThumbX = i;
                    }
                    else if (x == Direction.Down)
                    {
                        report.LeftThumbX = (short)(i * -1);
                    }

                    if (y == Direction.Up)
                    {
                        report.LeftThumbY = i;
                    }
                    else if (y == Direction.Down)
                    {
                        report.LeftThumbY = (short)(i * -1);
                    }

                    controller.SendReport(report);
                }
            }
        }
Exemple #2
0
        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();
        }
        public void Update(short lX, short lY, short rX, short rY, ControllerButtons buttons)
        {
            // Axes
            iReport.LeftThumbX  = lX;
            iReport.LeftThumbY  = lY;
            iReport.RightThumbX = rX;
            iReport.RightThumbY = rY;

            // Face buttons
            iReport.SetButtonState(Xbox360Buttons.Y, (buttons & ControllerButtons.BTN_Button1) != 0);
            iReport.SetButtonState(Xbox360Buttons.B, (buttons & ControllerButtons.BTN_Button2) != 0);
            iReport.SetButtonState(Xbox360Buttons.A, (buttons & ControllerButtons.BTN_Button3) != 0);
            iReport.SetButtonState(Xbox360Buttons.X, (buttons & ControllerButtons.BTN_Button4) != 0);

            // POV buttons
            iReport.SetButtonState(Xbox360Buttons.Up, (buttons & ControllerButtons.BTN_POV_Up) != 0);
            iReport.SetButtonState(Xbox360Buttons.Down, (buttons & ControllerButtons.BTN_POV_Down) != 0);
            iReport.SetButtonState(Xbox360Buttons.Left, (buttons & ControllerButtons.BTN_POV_Left) != 0);
            iReport.SetButtonState(Xbox360Buttons.Right, (buttons & ControllerButtons.BTN_POV_Right) != 0);

            // L buttons
            iReport.SetButtonState(Xbox360Buttons.LeftShoulder, (buttons & ControllerButtons.BTN_L1) != 0);
            iReport.LeftTrigger = ((buttons & ControllerButtons.BTN_L2) == ControllerButtons.BTN_L2) ? byte.MaxValue : byte.MinValue;
            iReport.SetButtonState(Xbox360Buttons.LeftThumb, (buttons & ControllerButtons.BTN_L3) != 0);
            // R buttons
            iReport.SetButtonState(Xbox360Buttons.RightShoulder, (buttons & ControllerButtons.BTN_R1) != 0);
            iReport.RightTrigger = ((buttons & ControllerButtons.BTN_R2) == ControllerButtons.BTN_R2) ? byte.MaxValue : byte.MinValue;
            iReport.SetButtonState(Xbox360Buttons.RightThumb, (buttons & ControllerButtons.BTN_R3) != 0);

            // Start/Select
            iReport.SetButtonState(Xbox360Buttons.Start, (buttons & ControllerButtons.BTN_Start) != 0);
            iReport.SetButtonState(Xbox360Buttons.Back, (buttons & ControllerButtons.BTN_Select) != 0);

            ctrl.SendReport(iReport);
        }
Exemple #4
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();
        }
        private int ReceiveRaw()
        {
            if (handle == IntPtr.Zero)
            {
                return(-2);
            }
            HIDapi.hid_set_nonblocking(handle, 1);
            byte[] raw_buf = new byte[report_len];
            int    ret     = HIDapi.hid_read_timeout(handle, raw_buf, new UIntPtr(report_len), 5000);

            if (ret > 0)
            {
                // Process packets as soon as they come
                for (int n = 0; n < 3; n++)
                {
                    ExtractIMUValues(raw_buf, n);

                    byte lag = (byte)Math.Max(0, raw_buf[1] - ts_en - 3);
                    if (n == 0)
                    {
                        Timestamp += (ulong)lag * 5000;                         // add lag once
                        ProcessButtonsAndStick(raw_buf);

                        int newbat = battery;
                        battery = (raw_buf[2] >> 4) / 2;
                        if (newbat != battery)
                        {
                            BatteryChanged();
                        }
                    }
                    Timestamp += 5000;                     // 5ms difference

                    packetCounter++;
                    if (Program.server != null)
                    {
                        Program.server.NewReportIncoming(this);
                    }

                    //this.SendPackage(); // JC Send Pack

                    if (xin != null)
                    {
                        xin.SendReport(report);
                    }
                }

                if (ts_en == raw_buf[1])
                {
                    form.AppendTextBox("Duplicate timestamp enqueued.\r\n");
                    DebugPrint(string.Format("Duplicate timestamp enqueued. TS: {0:X2}", ts_en), DebugType.THREADING);
                }
                ts_en = raw_buf[1];
                DebugPrint(string.Format("Enqueue. Bytes read: {0:D}. Timestamp: {1:X2}", ret, raw_buf[1]), DebugType.THREADING);
            }
            return(ret);
        }
        public override void OnBuffer(byte[] keyBuffer, byte[] lTrigger, byte[] rTrigger, byte[] lRemoteX, byte[] lRemoteY, byte[] rRemoteX, byte[] rRemoteY)
        {
            Xbox360Report controllerReport = new Xbox360Report();

            OnKeyArray(keyBuffer, controllerReport);
            OnLeftRemote(lRemoteX, lRemoteY, controllerReport);
            OnRightRemote(rRemoteX, rRemoteY, controllerReport);
            OnLTrigger(lTrigger, controllerReport);
            OnRTrigger(rTrigger, controllerReport);
            myController.SendReport(controllerReport);
        }
Exemple #7
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);
        }
        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);
            }
        }
Exemple #9
0
        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.SetButtonsFull(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:
                DS4Windows.VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, DS4Windows.VJoyFeeder.HID_USAGES.HID_USAGE_X);
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1Y:
            case SASteeringWheelEmulationAxisType.VJoy2Y:
                DS4Windows.VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, DS4Windows.VJoyFeeder.HID_USAGES.HID_USAGE_Y);
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1Z:
            case SASteeringWheelEmulationAxisType.VJoy2Z:
                DS4Windows.VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, DS4Windows.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;
            }

            cont.SendReport(report);
        }
Exemple #10
0
 private void DeactivateLogoButton(object state) //State parameter is not used because we want the most recent report
 {
     _LogoButtonActive = false;
     XInputReport.SetButtonState((Xbox360Buttons)0x0400, _LogoButtonActive);
     _Target.SendReport(XInputReport);
 }
        private void UpdateXInputDevice()
        {
            Xbox360Report report = MapToXInputDevice();

            emnulatedController.SendReport(report);
        }
Exemple #12
0
 public void SendReport()
 {
     _controller.SendReport(_report);
 }
Exemple #13
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();
        }
Exemple #14
0
        protected override void OnMessage(MessageEventArgs e)
        {
            string button = e.Data.Split('/')[0];
            string value  = e.Data.Split('/')[1];

            // Joysticks
            if (button == "0" || button == "1")
            {
                string[] percentages = value.Split(';');
                switch (button)
                {
                case "0":
                    report.SetAxis(Xbox360Axes.LeftThumbX, (short)(32767 * int.Parse(percentages[0]) / 100));
                    report.SetAxis(Xbox360Axes.LeftThumbY, (short)(32767 * int.Parse(percentages[1]) / 100));
                    break;

                case "1":
                    report.SetAxis(Xbox360Axes.RightThumbX, (short)(32767 * int.Parse(percentages[0]) / 100));
                    report.SetAxis(Xbox360Axes.RightThumbY, (short)(32767 * int.Parse(percentages[1]) / 100));
                    break;
                }
            }
            // LT/RT
            else if (button == "6" || button == "7")
            {
                // If pressed, max value. Else, 0
                short pressed = (value == "1") ? short.MaxValue : (short)0;
                switch (button)
                {
                case "8":
                    report.SetAxis(Xbox360Axes.LeftTrigger, short.MaxValue);
                    break;

                case "9":
                    report.SetAxis(Xbox360Axes.RightTrigger, short.MaxValue);
                    break;
                }
            }
            // Buttons
            else
            {
                // If value is 1, button is pressed
                bool pressed = (value == "1") ? true : false;
                switch (button)
                {
                case "2":
                    report.SetButtonState(Xbox360Buttons.A, pressed);
                    break;

                case "3":
                    report.SetButtonState(Xbox360Buttons.X, pressed);
                    break;

                case "4":
                    report.SetButtonState(Xbox360Buttons.B, pressed);
                    break;

                case "5":
                    report.SetButtonState(Xbox360Buttons.Y, pressed);
                    break;

                case "8":
                    report.SetButtonState(Xbox360Buttons.LeftShoulder, pressed);
                    break;

                case "9":
                    report.SetButtonState(Xbox360Buttons.RightShoulder, pressed);
                    break;

                case "10":
                    report.SetButtonState(Xbox360Buttons.Left, pressed);
                    break;

                case "11":
                    report.SetButtonState(Xbox360Buttons.Up, pressed);
                    break;

                case "12":
                    report.SetButtonState(Xbox360Buttons.Right, pressed);
                    break;

                case "13":
                    report.SetButtonState(Xbox360Buttons.Down, pressed);
                    break;

                case "14":
                    report.SetButtonState(Xbox360Buttons.LeftThumb, pressed);
                    break;

                case "15":
                    report.SetButtonState(Xbox360Buttons.RightThumb, pressed);
                    break;

                case "16":
                    report.SetButtonState(Xbox360Buttons.Start, pressed);
                    break;

                case "17":
                    report.SetButtonState(Xbox360Buttons.Guide, pressed);
                    break;
                }
            }


            controller.SendReport(report);
        }
Exemple #15
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();
        }