Exemple #1
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != PACKET_SIZE && packet.Length != MOUSE_PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (packet.Length == PACKET_SIZE)
            {
                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }
                state.SetButton("1", packet[5] != 0);
                state.SetButton("2", packet[6] != 0);
            }
            else if (packet.Length == MOUSE_PACKET_SIZE)
            {
                for (int i = 0; i < MOUSE_BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(MOUSE_BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(MOUSE_BUTTONS[i], packet[i] != 0x00);
                }

                bool xSign = packet[4] != 0;
                bool ySign = packet[5] != 0;
                bool xOver = packet[6] != 0;
                bool yOver = packet[7] != 0;

                byte xVal = SignalTool.ReadByteBackwards(packet, 8);
                byte yVal = SignalTool.ReadByteBackwards(packet, 16);

                float x = ReadMouse(xSign, xOver, xVal);
                float y = ReadMouse(ySign, yOver, yVal);

                SignalTool.SetMouseProperties(x, y, xVal, yVal, state);
            }

            return(state.Build());
        }
Exemple #2
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }
            float x = 0;
            float y = 0;

            if (packet[3] != 0x00)
            {
                x = 1;
            }
            else if (packet[2] != 0x00)
            {
                x = -1;
            }

            if (packet[0] != 0x00)
            {
                y = 1;
            }
            else if (packet[1] != 0x00)
            {
                y = -1;
            }

            if (y != 0 || x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(y, x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1.0)
                {
                    x = x1;
                    y = y1;
                }
            }

            state.SetAnalog("x", x, 0);
            state.SetAnalog("y", y, 0);

            state.SetButton("1", packet[5] == 0 && packet[6] == 0 && packet[7] == 0 && packet[8] != 0);
            state.SetButton("2", packet[5] == 0 && packet[6] == 0 && packet[7] != 0 && packet[8] == 0);
            state.SetButton("3", packet[5] != 0 && packet[6] == 0 && packet[7] == 0 && packet[8] != 0);
            state.SetButton("4", packet[5] != 0 && packet[6] != 0 && packet[7] != 0 && packet[8] == 0);
            state.SetButton("5", packet[5] == 0 && packet[6] != 0 && packet[7] != 0 && packet[8] == 0);
            state.SetButton("6", packet[5] != 0 && packet[6] == 0 && packet[7] == 0 && packet[8] == 0);
            state.SetButton("7", packet[5] == 0 && packet[6] == 0 && packet[7] != 0 && packet[8] != 0);
            state.SetButton("8", packet[5] == 0 && packet[6] != 0 && packet[7] != 0 && packet[8] != 0);
            state.SetButton("9", packet[5] == 0 && packet[6] != 0 && packet[7] == 0 && packet[8] == 0);
            state.SetButton("star", packet[5] == 0 && packet[6] != 0 && packet[7] == 0 && packet[8] != 0);
            state.SetButton("0", packet[5] != 0 && packet[6] != 0 && packet[7] == 0 && packet[8] == 0);
            state.SetButton("pound", packet[5] != 0 && packet[6] == 0 && packet[7] != 0 && packet[8] == 0);
            state.SetButton("purple", packet[5] != 0 && packet[6] != 0 && packet[7] == 0 && packet[8] != 0);
            state.SetButton("blue", packet[5] != 0 && packet[6] == 0 && packet[7] != 0 && packet[8] != 0);

            if (packet.Length == PACKET_SIZE)
            {
                for (int i = 0; i < 64; ++i)
                {
                    state.SetButton("E" + i.ToString(CultureInfo.CurrentCulture), i == (packet[10] - 11));
                }
            }
            else if (packet.Length == ROLLER_PACKET_SIZE)
            {
                for (int i = 0; i < ROLLER_BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(ROLLER_BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(ROLLER_BUTTONS[i], packet[i] != 0x00);
                }

                if (y_packet != null)
                {
                    for (int i = 0; i < Y_BUTTONS.Length; ++i)
                    {
                        if (string.IsNullOrEmpty(Y_BUTTONS[i]))
                        {
                            continue;
                        }

                        state.SetButton(Y_BUTTONS[i], y_packet[i] != 0x00);
                    }

                    float x_2 = 0;
                    float y_2 = 0;

                    if (y_packet[3] != 0x00)
                    {
                        x_2 = 1;
                    }
                    else if (y_packet[2] != 0x00)
                    {
                        x_2 = -1;
                    }

                    if (y_packet[0] != 0x00)
                    {
                        y_2 = 1;
                    }
                    else if (y_packet[1] != 0x00)
                    {
                        y_2 = -1;
                    }

                    if (y_2 != 0 || x_2 != 0)
                    {
                        // point on the unit circle at the same angle
                        double radian = Math.Atan2(y_2, x_2);
                        float  x1_2   = (float)Math.Cos(radian);
                        float  y1_2   = (float)Math.Sin(radian);

                        // Don't let magnitude exceed the unit circle
                        if (Math.Sqrt(Math.Pow(x_2, 2) + Math.Pow(y_2, 2)) > 1.0)
                        {
                            x_2 = x1_2;
                            y_2 = y1_2;
                        }
                    }

                    state.SetAnalog("x_2", x_2, 0);
                    state.SetAnalog("y_2", y_2, 0);

                    state.SetButton("1_2", y_packet[5] == 0 && y_packet[6] == 0 && y_packet[7] == 0 && y_packet[8] != 0);
                    state.SetButton("2_2", y_packet[5] == 0 && y_packet[6] == 0 && y_packet[7] != 0 && y_packet[8] == 0);
                    state.SetButton("3_2", y_packet[5] != 0 && y_packet[6] == 0 && y_packet[7] == 0 && y_packet[8] != 0);
                    state.SetButton("4_2", y_packet[5] != 0 && y_packet[6] != 0 && y_packet[7] != 0 && y_packet[8] == 0);
                    state.SetButton("5_2", y_packet[5] == 0 && y_packet[6] != 0 && y_packet[7] != 0 && y_packet[8] == 0);
                    state.SetButton("6_2", y_packet[5] != 0 && y_packet[6] == 0 && y_packet[7] == 0 && y_packet[8] == 0);
                    state.SetButton("7_2", y_packet[5] == 0 && y_packet[6] == 0 && y_packet[7] != 0 && y_packet[8] != 0);
                    state.SetButton("8_2", y_packet[5] == 0 && y_packet[6] != 0 && y_packet[7] != 0 && y_packet[8] != 0);
                    state.SetButton("9_2", y_packet[5] == 0 && y_packet[6] != 0 && y_packet[7] == 0 && y_packet[8] == 0);
                    state.SetButton("star_2", y_packet[5] == 0 && y_packet[6] != 0 && y_packet[7] == 0 && y_packet[8] != 0);
                    state.SetButton("0_2", y_packet[5] != 0 && y_packet[6] != 0 && y_packet[7] == 0 && y_packet[8] == 0);
                    state.SetButton("pound_2", y_packet[5] != 0 && y_packet[6] == 0 && y_packet[7] != 0 && y_packet[8] == 0);
                    state.SetButton("purple_2", y_packet[5] != 0 && y_packet[6] != 0 && y_packet[7] == 0 && y_packet[8] != 0);
                    state.SetButton("blue_2", y_packet[5] != 0 && y_packet[6] == 0 && y_packet[7] != 0 && y_packet[8] != 0);

                    for (int i = 0; i < Y_ROLLER_BUTTONS.Length; ++i)
                    {
                        if (string.IsNullOrEmpty(Y_ROLLER_BUTTONS[i]))
                        {
                            continue;
                        }

                        state.SetButton(Y_ROLLER_BUTTONS[i], y_packet[i] != 0x00);
                    }
                }

                sbyte roller_y = y_packet != null ? (sbyte)SignalTool.ReadByteBackwards(y_packet, 10) : (sbyte)0;
                sbyte roller_x = (sbyte)SignalTool.ReadByteBackwards(packet, 10);

                if (Math.Abs(roller_x) != 0 && Math.Abs(roller_x) > maxX)
                {
                    maxX = Math.Abs(roller_x);
                }

                if (Math.Abs(roller_y) != 0 && Math.Abs(roller_y) > maxY)
                {
                    maxY = Math.Abs(roller_y);
                }

                SignalTool.SetMouseProperties(-1.0f * roller_x / maxX, -1.0f * roller_y / maxY, roller_x, roller_y, state);
            }

            return(state.Build());
        }
Exemple #3
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            ControllerStateBuilder state = null;

            if (packet.Length == 13)
            {
                return(Classic.ReadFromPacket(packet));
            }
            else if (packet.Length == 6)
            {
                for (int i = 0; i < BUTTONS_AMIGA_ANALOG.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS_AMIGA_ANALOG[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS_AMIGA_ANALOG[i], packet[i] != 0x00);
                }

                state.SetAnalog("y", (((packet[4] >> 4) | (packet[5])) - 15.0f) / 15.0f, (packet[4] >> 4) | (packet[5]));
                state.SetAnalog("x", AmigaAnalogXAxisData, AmigaAnalogXAxisDataRaw);
            }
            if (packet.Length == BUTTONS_CD32.Length)
            {
                state = new ControllerStateBuilder();

                for (int i = 0; i < BUTTONS_CD32.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS_CD32[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS_CD32[i], (packet[i] & 0b10000000) == 0x00);
                }

                state.SetButton("up", (packet[8] & 0b00000001) == 0);
                state.SetButton("down", (packet[0] & 0b00000100) == 0);
                state.SetButton("left", (packet[0] & 0b00001000) == 0);
                state.SetButton("right", (packet[0] & 0b00010000) == 0);
            }
            else if (packet.Length == BUTTONS_CDTV_REMOTE.Length)
            {
                int checksum        = (packet[33] >> 4) | packet[34];
                int checkedCheckSum = 0;
                for (int i = 0; i < 33; ++i)
                {
                    checkedCheckSum += packet[i] == 0 ? 0 : 1;
                }

                if (checksum == checkedCheckSum)
                {
                    state = new ControllerStateBuilder();

                    for (int i = 0; i < BUTTONS_CDTV_REMOTE.Length; ++i)
                    {
                        if (string.IsNullOrEmpty(BUTTONS_CDTV_REMOTE[i]))
                        {
                            continue;
                        }

                        state.SetButton(BUTTONS_CDTV_REMOTE[i], packet[i] != 0x00);
                    }

                    float x    = 0;
                    int   xRaw = 0;
                    float y    = 0;
                    int   yRaw = 0;

                    if (packet[0] != 0x00)
                    {
                        x    = -0.25f;
                        xRaw = packet[0];
                    }
                    else if (packet[2] != 0x00)
                    {
                        x    = 0.25f;
                        xRaw = packet[2];
                    }

                    if (packet[1] != 0x00)
                    {
                        y    = 0.25f;
                        yRaw = packet[1];
                    }
                    else if (packet[3] != 0x00)
                    {
                        y    = -0.25f;
                        yRaw = packet[3];
                    }

                    SignalTool.SetMouseProperties(x, y, xRaw, yRaw, state, .25f);
                }
            }
            else if (packet.Length == BUTTONS_CDTV_JOYSTICK.Length && packet[0] == 0)
            {
                int checksum        = (packet[24] >> 4) | packet[25];
                int checkedCheckSum = 0;
                for (int i = 0; i < 24; ++i)
                {
                    checkedCheckSum += packet[i] == 0 ? 0 : 1;
                }

                if (checksum == checkedCheckSum)
                {
                    state = new ControllerStateBuilder();

                    for (int i = 0; i < BUTTONS_CDTV_JOYSTICK.Length; ++i)
                    {
                        if (string.IsNullOrEmpty(BUTTONS_CDTV_JOYSTICK[i]))
                        {
                            continue;
                        }

                        state.SetButton(BUTTONS_CDTV_JOYSTICK[i], packet[i] != 0x00);
                    }

                    SignalTool.FakeAnalogStick(packet[6], packet[5], packet[4], packet[3], state, "x", "y");
                    SignalTool.FakeAnalogStick(packet[12], packet[11], packet[10], packet[9], state, "Joy2x", "Joy2y");
                }
            }
            else if (packet.Length == 26 && packet[0] == 1)
            {
                int checksum        = (packet[24] >> 4) | packet[25];
                int checkedCheckSum = 0;
                for (int i = 0; i < 24; ++i)
                {
                    checkedCheckSum += packet[i] == 0 ? 0 : 1;
                }

                if (checksum == checkedCheckSum)
                {
                    state = new ControllerStateBuilder();

                    state.SetButton("left_button", packet[2] == 0x00);
                    state.SetButton("right_button", packet[1] == 0x00);

                    sbyte xVal = (sbyte)SignalTool.ReadByte(packet, 3);
                    sbyte yVal = (sbyte)SignalTool.ReadByte(packet, 11);

                    SignalTool.SetMouseProperties(xVal / -128.0f, yVal / 128.0f, xVal, yVal, state);
                }
            }
            else if (packet.Length == 19)
            {
                state = new ControllerStateBuilder();

                state.SetButton("left_button", packet[0] != 0x00);
                state.SetButton("right_button", packet[2] != 0x00);

                sbyte xVal = (sbyte)SignalTool.ReadByteBackwards(packet, 3);
                sbyte yVal = (sbyte)SignalTool.ReadByteBackwards(packet, 11);

                SignalTool.SetMouseProperties(xVal / -128.0f, yVal / 128.0f, xVal, yVal, state);
            }
            else if (packet.Length == 36)
            {
                byte[] reconstructedPacket = new byte[18];

                int j = 0;
                for (int i = 0; i < 18; ++i)
                {
                    reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]);
                    j += 2;
                }

                byte[] polishedPacket = new byte[128];

                int checksum = 0;
                for (int i = 0; i < 16; ++i)
                {
                    checksum += reconstructedPacket[i];
                    for (int k = 0; k < 8; ++k)
                    {
                        polishedPacket[(i * 8) + k] = (byte)((reconstructedPacket[i] & (1 << k)) != 0 ? 1 : 0);
                    }
                }

                short sentChecksum = (short)((reconstructedPacket[17] << 8) | reconstructedPacket[16]);
                if (checksum == sentChecksum)
                {
                    state = new ControllerStateBuilder();

                    for (int i = 0; i < 128; ++i)
                    {
                        string scanCode = i.ToString("X", CultureInfo.CurrentCulture).ToUpper(CultureInfo.CurrentUICulture);;
                        state.SetButton(scanCode, polishedPacket[i] != 0x00);
                    }
                }
            }

            return(state?.Build());
        }