Esempio n. 1
0
        public void ButtonDown(int Index, X360Buttons Button)
        {
            if (!IsRunning)
            {
                return;
            }

            var current = controllers[Index];
            var next    = new X360Controller(current);

            next.Buttons |= Button;

            // RB + Start = Guide
            if (next.Buttons.HasFlag(X360Buttons.RightBumper | X360Buttons.Start))
            {
                next.Buttons   &= ~(X360Buttons.RightBumper | X360Buttons.Start);
                next.Buttons   |= X360Buttons.Logo;
                DidEmulateGuide = true;
            }

            if (current.Buttons == next.Buttons)
            {
                return;
            }

            controllers[Index] = next;
            InvokeChange(Index);
            if (Program.IsDebug)
            {
                System.Console.WriteLine($"Controller #{Index + 1} {Button} Button Down");
            }
        }
Esempio n. 2
0
        public void ButtonUp(int Index, X360Buttons Button)
        {
            if (!IsRunning)
            {
                return;
            }

            var current = controllers[Index];
            var next    = new X360Controller(current);

            next.Buttons &= ~Button;

            if (DidEmulateGuide && Button == X360Buttons.RightBumper || Button == X360Buttons.Start)
            {
                next.Buttons   &= ~(X360Buttons.Logo | X360Buttons.RightBumper | X360Buttons.Start);
                DidEmulateGuide = false;
            }

            if (current.Buttons == next.Buttons)
            {
                return;
            }

            controllers[Index] = next;
            InvokeChange(Index);
            if (Program.IsDebug)
            {
                System.Console.WriteLine($"Controller #{Index + 1} {Button} Button Up");
            }
        }
Esempio n. 3
0
 private static async void ImpulseRelease(X360Buttons b)
 {
     await Task.Run(() =>
     {
         Thread.Sleep(10);
         m_Ctrlr.Buttons &= ~b;
         m_Bus.Report(2, m_Ctrlr.GetReport());
     });
 }
Esempio n. 4
0
        static void applyInputs()
        {
            // Logic goes here

            gamepad.Buttons &= ~previousInputs;
            previousInputs   = gamepad.Buttons;

            scp.Report(gamepadIndex, gamepad.GetReport());
        }
Esempio n. 5
0
 /// <summary>
 /// Generates a new X360Controller object. Optionally, you can specify the initial state of the controller.
 /// </summary>
 /// <param name="buttons">The pressed buttons. Use like flags (i.e. (X360Buttons.A | X360Buttons.X) would be mean both A and X are pressed).</param>
 /// <param name="leftTrigger">Left trigger analog input. 0 to 255.</param>
 /// <param name="rightTrigger">Right trigger analog input. 0 to 255.</param>
 /// <param name="leftStickX">Left stick X-axis. -32,768 to 32,767.</param>
 /// <param name="leftStickY">Left stick Y-axis. -32,768 to 32,767.</param>
 /// <param name="rightStickX">Right stick X-axis. -32,768 to 32,767.</param>
 /// <param name="rightStickY">Right stick Y-axis. -32,768 to 32,767.</param>
 public X360Controller(X360Buttons buttons, byte leftTrigger, byte rightTrigger, short leftStickX, short leftStickY, short rightStickX, short rightStickY)
 {
     Buttons      = buttons;
     LeftTrigger  = leftTrigger;
     RightTrigger = rightTrigger;
     LeftStickX   = leftStickX;
     LeftStickY   = leftStickY;
     RightStickX  = rightStickX;
     RightStickY  = rightStickY;
 }
Esempio n. 6
0
        public static void SetButton(this X360Controller controller, X360Buttons buttons, bool value)
        {
            var v = controller.Buttons & ~buttons;

            if (value)
            {
                v |= buttons;
            }
            controller.Buttons = v;
        }
        private static void SetBtn(X360Controller x, X360Buttons btn, string state)
        {
            int s = int.Parse(state);

            if (s == 1)
            {
                x.Buttons |= btn;
            }
            else
            {
                x.Buttons &= ~btn;
            }
        }
Esempio n. 8
0
        protected void ClickButton(X360Buttons button, int down, int msleep)
        {
            Console.WriteLine("Clicking: " + button.ToString() + " down: " + down + " sleep " + msleep);
            controller.Buttons = button;
            SendtoController(controller);

            Thread.Sleep(down);

            controller.Buttons &= ~button;
            //controller.Buttons = X360Buttons.None;
            SendtoController(controller);

            Thread.Sleep(msleep);
        }
Esempio n. 9
0
        private void input_thread(HidDevice Device, ScpBus scpBus, int index)
        {
            scpBus.PlugIn(index);
            X360Controller controller = new X360Controller();
            int            timeout    = 10;
            //long last_changed = 0;
            //long last_mi_button = 0;
            bool ss_button_pressed = false;
            bool ss_button_held    = false;

            while (Running)
            {
                HidDeviceData data         = Device.Read(timeout);
                var           currentState = data.Data;
                //bool changed = false;
                if (data.Status == HidDeviceData.ReadStatus.Success && currentState.Length >= 10 && currentState[0] == 3)
                {
                    // NOTE: Console.WriteLine is blocking. If main thread sends a WriteLine while we do a WriteLine here, we're boned and will miss reports!
                    //Console.WriteLine(Program.ByteArrayToHexString(currentState));

                    X360Buttons Buttons = X360Buttons.None;
                    if ((currentState[3] & 64) != 0)
                    {
                        Buttons |= X360Buttons.A;
                    }
                    if ((currentState[3] & 32) != 0)
                    {
                        Buttons |= X360Buttons.B;
                    }
                    if ((currentState[3] & 16) != 0)
                    {
                        Buttons |= X360Buttons.X;
                    }
                    if ((currentState[3] & 8) != 0)
                    {
                        Buttons |= X360Buttons.Y;
                    }
                    if ((currentState[3] & 4) != 0)
                    {
                        Buttons |= X360Buttons.LeftBumper;
                    }
                    if ((currentState[3] & 2) != 0)
                    {
                        Buttons |= X360Buttons.RightBumper;
                    }
                    if ((currentState[3] & 1) != 0)
                    {
                        Buttons |= X360Buttons.LeftStick;
                    }
                    if ((currentState[2] & 128) != 0)
                    {
                        Buttons |= X360Buttons.RightStick;
                    }
                    ss_button_pressed = (currentState[2] & 1) != 0;
                    // [2] & 2 == Assistant, [2] & 1 == Screenshot

                    switch (currentState[1])
                    {
                    default:
                        break;

                    case 0:
                        Buttons |= X360Buttons.Up;
                        break;

                    case 1:
                        Buttons |= X360Buttons.UpRight;
                        break;

                    case 2:
                        Buttons |= X360Buttons.Right;
                        break;

                    case 3:
                        Buttons |= X360Buttons.DownRight;
                        break;

                    case 4:
                        Buttons |= X360Buttons.Down;
                        break;

                    case 5:
                        Buttons |= X360Buttons.DownLeft;
                        break;

                    case 6:
                        Buttons |= X360Buttons.Left;
                        break;

                    case 7:
                        Buttons |= X360Buttons.UpLeft;
                        break;
                    }

                    if ((currentState[2] & 32) != 0)
                    {
                        Buttons |= X360Buttons.Start;
                    }
                    if ((currentState[2] & 64) != 0)
                    {
                        Buttons |= X360Buttons.Back;
                    }

                    if ((currentState[2] & 16) != 0)
                    {
                        //last_mi_button = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                        Buttons |= X360Buttons.Logo;
                    }


                    //if (controller.Buttons != Buttons)
                    {
                        //changed = true;
                        controller.Buttons = Buttons;
                    }

                    // Note: The HID reports do not allow stick values of 00.
                    // This seems to make sense: 0x80 is center, so usable values are:
                    // 0x01 to 0x7F and 0x81 to 0xFF.
                    // For our purposes I believe this is undesirable. Subtract 1 from negative
                    // values to allow maxing out the stick values.
                    // TODO: Get an Xbox controller and verify this is standard behavior.
                    for (int i = 4; i <= 7; ++i)
                    {
                        if (currentState[i] <= 0x7F && currentState[i] > 0x00)
                        {
                            currentState[i] -= 0x01;
                        }
                    }

                    ushort LeftStickXunsigned = (ushort)(currentState[4] << 8 | (currentState[4] << 1 & 255));
                    if (LeftStickXunsigned == 0xFFFE)
                    {
                        LeftStickXunsigned = 0xFFFF;
                    }
                    short LeftStickX = (short)(LeftStickXunsigned - 0x8000);

                    //if (LeftStickX != controller.LeftStickX)
                    {
                        //	changed = true;
                        controller.LeftStickX = LeftStickX;
                    }

                    ushort LeftStickYunsigned = (ushort)(currentState[5] << 8 | (currentState[5] << 1 & 255));
                    if (LeftStickYunsigned == 0xFFFE)
                    {
                        LeftStickYunsigned = 0xFFFF;
                    }
                    short LeftStickY = (short)(-LeftStickYunsigned + 0x7FFF);
                    if (LeftStickY == -1)
                    {
                        LeftStickY = 0;
                    }
                    //if (LeftStickY != controller.LeftStickY)
                    {
                        //	changed = true;
                        controller.LeftStickY = LeftStickY;
                    }

                    ushort RightStickXunsigned = (ushort)(currentState[6] << 8 | (currentState[6] << 1 & 255));
                    if (RightStickXunsigned == 0xFFFE)
                    {
                        RightStickXunsigned = 0xFFFF;
                    }
                    short RightStickX = (short)(RightStickXunsigned - 0x8000);

                    //if (RightStickX != controller.RightStickX)
                    {
                        //	changed = true;
                        controller.RightStickX = RightStickX;
                    }

                    ushort RightStickYunsigned = (ushort)(currentState[7] << 8 | (currentState[7] << 1 & 255));
                    if (RightStickYunsigned == 0xFFFE)
                    {
                        RightStickYunsigned = 0xFFFF;
                    }
                    short RightStickY = (short)(-RightStickYunsigned + 0x7FFF);
                    if (RightStickY == -1)
                    {
                        RightStickY = 0;
                    }

                    //if (RightStickY != controller.RightStickY)
                    {
                        //	changed = true;
                        controller.RightStickY = RightStickY;
                    }

                    //if (controller.LeftTrigger != currentState[8])
                    {
                        //	changed = true;
                        controller.LeftTrigger = currentState[8];
                    }

                    //if (controller.RightTrigger != currentState[9])
                    {
                        //	changed = true;
                        controller.RightTrigger = currentState[9];
                    }
                }

                /*
                 * if (data.Status == HidDeviceData.ReadStatus.WaitTimedOut || (!changed && ((last_changed + timeout) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))))
                 * {
                 *      changed = true;
                 * }*/

                //if (changed)
                {
                    //Console.WriteLine("changed");
                    //Console.WriteLine((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
                    byte[] outputReport = new byte[8];
                    scpBus.Report(index, controller.GetReport(), outputReport);

                    if (outputReport[1] == 0x08)
                    {
                        byte bigMotor   = outputReport[3];
                        byte smallMotor = outputReport[4];

                        if (smallMotor != Vibration[3] || Vibration[1] != bigMotor)
                        {
                            // We only need to take the mutex if we're modifying the data
                            rumble_mutex.WaitOne();
                            Vibration[1] = bigMotor;
                            Vibration[3] = smallMotor;
                            rumble_mutex.ReleaseMutex();
                            rumbleWaitHandle.Set();
                        }
                    }

                    //last_changed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                }

                if (ss_button_pressed && !ss_button_held)
                {
                    ss_button_held = true;
                    try
                    {
                        // TODO: Allow configuring this keybind.
                        ssThread = new Thread(() => System.Windows.Forms.SendKeys.SendWait("^+Z"));
                        ssThread.Start();
                    }
                    catch
                    {
                    }
                }
                else if (ss_button_held && !ss_button_pressed)
                {
                    ss_button_held = false;
                }
            }
        }
Esempio n. 10
0
        private void input_thread(HidDevice Device, ScpBus scpBus, int index)
        {
            scpBus.PlugIn(index);
            X360Controller controller     = new X360Controller();
            int            timeout        = 30;
            long           last_changed   = 0;
            long           last_mi_button = 0;

            while (true)
            {
                HidDeviceData data         = Device.Read(timeout);
                var           currentState = data.Data;
                bool          changed      = false;
                if (data.Status == HidDeviceData.ReadStatus.Success && currentState.Length >= 21 && currentState[0] == 4)
                {
                    //Console.WriteLine(Program.ByteArrayToHexString(currentState));
                    X360Buttons Buttons = X360Buttons.None;
                    if ((currentState[1] & 1) != 0)
                    {
                        Buttons |= X360Buttons.A;
                    }
                    if ((currentState[1] & 2) != 0)
                    {
                        Buttons |= X360Buttons.B;
                    }
                    if ((currentState[1] & 8) != 0)
                    {
                        Buttons |= X360Buttons.X;
                    }
                    if ((currentState[1] & 16) != 0)
                    {
                        Buttons |= X360Buttons.Y;
                    }
                    if ((currentState[1] & 64) != 0)
                    {
                        Buttons |= X360Buttons.LeftBumper;
                    }
                    if ((currentState[1] & 128) != 0)
                    {
                        Buttons |= X360Buttons.RightBumper;
                    }

                    if ((currentState[2] & 32) != 0)
                    {
                        Buttons |= X360Buttons.LeftStick;
                    }
                    if ((currentState[2] & 64) != 0)
                    {
                        Buttons |= X360Buttons.RightStick;
                    }

                    if (currentState[4] != 15)
                    {
                        if (currentState[4] == 0 || currentState[4] == 1 || currentState[4] == 7)
                        {
                            Buttons |= X360Buttons.Up;
                        }
                        if (currentState[4] == 4 || currentState[4] == 3 || currentState[4] == 5)
                        {
                            Buttons |= X360Buttons.Down;
                        }
                        if (currentState[4] == 6 || currentState[4] == 5 || currentState[4] == 7)
                        {
                            Buttons |= X360Buttons.Left;
                        }
                        if (currentState[4] == 2 || currentState[4] == 1 || currentState[4] == 3)
                        {
                            Buttons |= X360Buttons.Right;
                        }
                    }

                    if ((currentState[2] & 8) != 0)
                    {
                        Buttons |= X360Buttons.Start;
                    }
                    if ((currentState[2] & 4) != 0)
                    {
                        Buttons |= X360Buttons.Back;
                    }



                    if ((currentState[20] & 1) != 0)
                    {
                        last_mi_button = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                        Buttons       |= X360Buttons.Logo;
                    }
                    if (last_mi_button != 0)
                    {
                        Buttons |= X360Buttons.Logo;
                    }


                    if (controller.Buttons != Buttons)
                    {
                        changed            = true;
                        controller.Buttons = Buttons;
                    }

                    short LeftStickX = (short)((Math.Max(-127.0, currentState[5] - 128) / 127) * 32767);
                    if (LeftStickX != controller.LeftStickX)
                    {
                        changed = true;
                        controller.LeftStickX = LeftStickX;
                    }

                    short LeftStickY = (short)((Math.Max(-127.0, currentState[6] - 128) / 127) * -32767);
                    if (LeftStickY != controller.LeftStickY)
                    {
                        changed = true;
                        controller.LeftStickY = LeftStickY;
                    }

                    short RightStickX = (short)((Math.Max(-127.0, currentState[7] - 128) / 127) * 32767);
                    if (RightStickX != controller.RightStickX)
                    {
                        changed = true;
                        controller.RightStickX = RightStickX;
                    }

                    short RightStickY = (short)((Math.Max(-127.0, currentState[8] - 128) / 127) * -32767);
                    if (RightStickY != controller.RightStickY)
                    {
                        changed = true;
                        controller.RightStickY = RightStickY;
                    }

                    if (controller.LeftTrigger != currentState[11])
                    {
                        changed = true;
                        controller.LeftTrigger = currentState[11];
                    }

                    if (controller.RightTrigger != currentState[12])
                    {
                        changed = true;
                        controller.RightTrigger = currentState[12];
                    }
                }

                if (data.Status == HidDeviceData.ReadStatus.WaitTimedOut || (!changed && ((last_changed + timeout) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))))
                {
                    changed = true;
                }

                if (changed)
                {
                    //Console.WriteLine("changed");
                    //Console.WriteLine((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
                    byte[] outputReport = new byte[8];
                    scpBus.Report(index, controller.GetReport(), outputReport);

                    if (outputReport[1] == 0x08)
                    {
                        byte bigMotor   = outputReport[3];
                        byte smallMotor = outputReport[4];
                        rumble_mutex.WaitOne();
                        if (bigMotor != Vibration[2] || Vibration[1] != smallMotor)
                        {
                            Vibration[1] = smallMotor;
                            Vibration[2] = bigMotor;
                        }
                        rumble_mutex.ReleaseMutex();
                    }

                    if (last_mi_button != 0)
                    {
                        if ((last_mi_button + 100) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))
                        {
                            last_mi_button      = 0;
                            controller.Buttons ^= X360Buttons.Logo;
                        }
                    }

                    last_changed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                }
            }
        }
Esempio n. 11
0
        private void input_thread(HidDevice Device, ScpBus scpBus, int index)
        {
            scpBus.PlugIn(index);
            X360Controller controller   = new X360Controller();
            int            timeout      = 30;
            long           last_changed = 0;

            //long last_mi_button = 0;
            while (true)
            {
                HidDeviceData data         = Device.Read(timeout);
                var           currentState = data.Data;
                bool          changed      = false;

                string str = Program.ByteArrayToHexString(currentState);
                if (!string.IsNullOrEmpty(str))
                {
                    Console.WriteLine(Program.ByteArrayToHexString(currentState));
                }

                if (data.Status == HidDeviceData.ReadStatus.Success && currentState.Length >= 10 && currentState[0] == 0x02)
                {
                    // Console.WriteLine(Program.ByteArrayToHexString(currentState));
                    X360Buttons Buttons = X360Buttons.None;
                    if ((currentState[1] & 0x01) != 0)
                    {
                        Buttons |= X360Buttons.A;
                    }
                    if ((currentState[1] & 0x02) != 0)
                    {
                        Buttons |= X360Buttons.B;
                    }
                    if ((currentState[1] & 0x08) != 0)
                    {
                        Buttons |= X360Buttons.X;
                    }
                    if ((currentState[1] & 0x10) != 0)
                    {
                        Buttons |= X360Buttons.Y;
                    }
                    if ((currentState[1] & 0x40) != 0)
                    {
                        Buttons |= X360Buttons.LeftBumper;
                    }
                    if ((currentState[1] & 0x80) != 0)
                    {
                        Buttons |= X360Buttons.RightBumper;
                    }

                    if ((currentState[2] & 0x20) != 0)
                    {
                        Buttons |= X360Buttons.LeftStick;
                    }
                    if ((currentState[2] & 0x40) != 0)
                    {
                        Buttons |= X360Buttons.RightStick;
                    }

                    if (currentState[3] != 0x0F)
                    {
                        if (currentState[3] == 0 || currentState[3] == 1 || currentState[3] == 7)
                        {
                            Buttons |= X360Buttons.Up;
                        }
                        if (currentState[3] == 4 || currentState[3] == 3 || currentState[3] == 5)
                        {
                            Buttons |= X360Buttons.Down;
                        }
                        if (currentState[3] == 6 || currentState[3] == 5 || currentState[3] == 7)
                        {
                            Buttons |= X360Buttons.Left;
                        }
                        if (currentState[3] == 2 || currentState[3] == 1 || currentState[3] == 3)
                        {
                            Buttons |= X360Buttons.Right;
                        }
                    }

                    if ((currentState[2] & 0x04) != 0)
                    {
                        Buttons |= X360Buttons.Start;
                    }
                    if ((currentState[2] & 0x08) != 0)
                    {
                        Buttons |= X360Buttons.Back;
                    }

                    if ((currentState[2] & 0x10) != 0)
                    {
                        Buttons |= X360Buttons.Logo;
                    }
                    //按下Fuze Logo键一下是不触发任何按键的,0x10是短按并松开fuze键时触发的按键
                    if ((currentState[2] & 0x01) != 0)
                    {
                        Buttons |= X360Buttons.Logo;
                    }
                    //长按会触发0x01按键,在GNU/Linux系统下,会触发关机键

                    /*
                     * //if ((currentState[20] & 1) != 0)
                     * //{
                     * //    last_mi_button = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                     * //    Buttons |= X360Buttons.Logo;
                     * //}
                     * //if (last_mi_button != 0) Buttons |= X360Buttons.Logo;
                     */

                    if (controller.Buttons != Buttons)
                    {
                        changed            = true;
                        controller.Buttons = Buttons;
                    }

                    short LeftStickX = (short)((Math.Max(-127.0, currentState[4] - 128) / 127) * 32767);
                    if (LeftStickX == -32767)
                    {
                        LeftStickX = -32768;
                    }

                    if (LeftStickX != controller.LeftStickX)
                    {
                        changed = true;
                        controller.LeftStickX = LeftStickX;
                    }

                    short LeftStickY = (short)((Math.Max(-127.0, currentState[5] - 128) / 127) * -32767);
                    if (LeftStickY == -32767)
                    {
                        LeftStickY = -32768;
                    }

                    if (LeftStickY != controller.LeftStickY)
                    {
                        changed = true;
                        controller.LeftStickY = LeftStickY;
                    }

                    short RightStickX = (short)((Math.Max(-127.0, currentState[6] - 128) / 127) * 32767);
                    if (RightStickX == -32767)
                    {
                        RightStickX = -32768;
                    }

                    if (RightStickX != controller.RightStickX)
                    {
                        changed = true;
                        controller.RightStickX = RightStickX;
                    }

                    short RightStickY = (short)((Math.Max(-127.0, currentState[7] - 128) / 127) * -32767);
                    if (RightStickY == -32767)
                    {
                        RightStickY = -32768;
                    }

                    if (RightStickY != controller.RightStickY)
                    {
                        changed = true;
                        controller.RightStickY = RightStickY;
                    }

                    if (controller.LeftTrigger != currentState[8])
                    {
                        changed = true;
                        controller.LeftTrigger = currentState[8];
                    }

                    if (controller.RightTrigger != currentState[9])
                    {
                        changed = true;
                        controller.RightTrigger = currentState[9];
                    }
                }

                if (data.Status == HidDeviceData.ReadStatus.WaitTimedOut || (!changed && ((last_changed + timeout) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))))
                {
                    changed = true;
                }

                if (changed)
                {
                    //Console.WriteLine("changed");
                    //Console.WriteLine((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
                    byte[] outputReport = new byte[8];
                    scpBus.Report(index, controller.GetReport(), outputReport);

                    //TODO: 震动
                    //if (outputReport[1] == 0x08)
                    //{
                    //    byte bigMotor = outputReport[3];
                    //    byte smallMotor = outputReport[4];
                    //    rumble_mutex.WaitOne();
                    //    if (bigMotor != Vibration[2] || Vibration[1] != smallMotor)
                    //    {
                    //        Vibration[1] = smallMotor;
                    //        Vibration[2] = bigMotor;
                    //    }
                    //    rumble_mutex.ReleaseMutex();
                    //}

                    /*
                     * //if (last_mi_button != 0)
                     * //{
                     * //    if ((last_mi_button + 100) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))
                     * //    {
                     * //        last_mi_button = 0;
                     * //        controller.Buttons ^= X360Buttons.Logo;
                     * //    }
                     * //}
                     *
                     * //last_changed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                     */
                }
            }
        }