Example #1
0
        public static ControllerState ReadFromPacket(byte[] 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);
            }

            state.SetAnalog("lstick_x", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length)));
            state.SetAnalog("lstick_y", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8)));
            state.SetAnalog("cstick_x", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 16)));
            state.SetAnalog("cstick_y", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 24)));
            state.SetAnalog("trig_l", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 32)));
            state.SetAnalog("trig_r", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 40)));

            return(state.Build());
        }
Example #2
0
        public static ControllerState ReadFromPacket(byte[] 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 = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length));
            float y = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8));

            state.SetAnalog("stick_x", x);
            state.SetAnalog("stick_y", y);

            SignalTool.SetMouseProperties(x, y, state);

            return(state.Build());
        }
Example #3
0
        public static ControllerState ReadFromPacket_SNES(byte[] packet)
        {
            if (packet.Length < BUTTONS_SNES.Length)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

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

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

            if (state != null && packet.Length == 32 && packet[15] != 0x00)
            {
                float y = (float)(SignalTool.ReadByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1)) / 127;
                float x = (float)(SignalTool.ReadByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1)) / 127;
                SignalTool.SetMouseProperties(x, y, state);
            }

            return(state.Build());
        }
Example #4
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < 1)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (packet[0] == 0)
            {
                if (packet.Length < PACKET_SIZE)
                {
                    return(null);
                }

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

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }
            }
            else
            {
                if (packet.Length < MOUSE_PACKET_SIZE)
                {
                    return(null);
                }

                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 ySign = packet[11] != 0;
                byte yVal  = SignalTool.ReadByte(packet, 14, 7);
                bool xSign = packet[21] != 0;
                byte xVal  = SignalTool.ReadByte(packet, 24, 7);

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

                SignalTool.SetMouseProperties(x, y, state);
            }
            return(state.Build());
        }
Example #5
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            polishedPacket[0] = (byte)((packet[0] >> 4) | packet[1]);
            polishedPacket[1] = (byte)(packet[9] == 0 ? 0 : 1);
            polishedPacket[2] = (byte)(packet[17] == 0 ? 0 : 1);

            for (int i = 18; i < 29; ++i)
            {
                polishedPacket[i - 15] = (byte)(packet[i] == 0 ? 0 : 1);
            }

            for (byte i = 0; i < 7; ++i)
            {
                polishedPacket[14] |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i);
            }

            for (byte i = 0; i < 7; ++i)
            {
                polishedPacket[15] |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

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

                state.SetButton(BUTTONS[i], polishedPacket[i] == 0x00);
            }

            float x = 0;
            float y = 0;

            if (packet[29] == 1) // This is the "Has Data" bit.  Reset the mouse on cached results.
            {
                y = ReadMouse(polishedPacket[14]);
                x = ReadMouse(polishedPacket[15]);
            }
            SignalTool.SetMouseProperties(x, y, state);

            return(state.Build());
        }
Example #6
0
        public static ControllerState ReadFromPacket(byte[] 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, state);
            }

            return(state.Build());
        }
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            var state = new ControllerStateBuilder();

            switch (packet.Length)
            {
            // Standard 64 bit packet size
            case PACKET_SIZE:
                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }
                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }
                state.SetAnalog("lstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length)));
                state.SetAnalog("lstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8)));
                state.SetAnalog("cstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length + 16)));
                state.SetAnalog("cstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 24)));
                state.SetAnalog("trig_l", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 32)));
                state.SetAnalog("trig_r", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 40)));
                break;

            // Packets are written as bytes when writing from the NicoHood API, so we're looking for a packet size of 8 (interpreted as bytes)
            case NICOHOOD_PACKET_SIZE:
                for (int i = 0; i < 16; i++)
                {
                    if (string.IsNullOrEmpty(NICOHOOD_BUTTONS[i]))
                    {
                        continue;
                    }
                    int bitPacket = (packet[i / 8] >> (i % 8)) & 0x1;
                    state.SetButton(NICOHOOD_BUTTONS[i], bitPacket != 0x00);
                }
                state.SetAnalog("lstick_x", readStick(packet[2]));
                state.SetAnalog("lstick_y", readStick(packet[3]));
                state.SetAnalog("cstick_x", readStick(packet[4]));
                state.SetAnalog("cstick_y", readStick(packet[5]));
                state.SetAnalog("trig_l", readTrigger(packet[6]));
                state.SetAnalog("trig_r", readTrigger(packet[7]));
                break;

            default:
                return(null);
            }

            return(state.Build());
        }
Example #8
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            state.SetAnalog("stick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length)));
            state.SetAnalog("stick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8)));

            return(state.Build());
        }
Example #9
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            polishedPacket[0] = 0;
            byte j = 8;

            for (byte i = 0; i < 8; ++i)
            {
                j--;
                polishedPacket[0] |= (byte)((packet[i] == 0 ? 0 : 1) << j);
            }

            if (polishedPacket[0] != 0x02 && polishedPacket[0] != 0x16 && polishedPacket[0] != 0xFF)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (polishedPacket[0] != 0xFF)
            {
                for (byte i = 0; i < 16; ++i)
                {
                    polishedPacket[i + 1] = packet[i + 8] == 0 ? (byte)1 : (byte)0;
                }

                byte numExtraBytes = 0;
                if (polishedPacket[0] == 0x16)
                {
                    numExtraBytes = 4;
                }

                for (int i = 0; i < numExtraBytes; ++i)
                {
                    polishedPacket[17 + i] = 0;
                    j = 8;
                    for (byte k = 0; k < 8; ++k)
                    {
                        j--;
                        polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8 + k)] == 0 ? 0 : 1) << j);
                    }
                }

                if (polishedPacket.Length < POLISHED_PACKET_SIZE)
                {
                    return(null);
                }

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

                    state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
                }

                if (polishedPacket[0] == 0x16)
                {
                    state.SetAnalog("lstick_x", ReadStick(polishedPacket[17]));
                    state.SetAnalog("lstick_y", ReadStick(polishedPacket[18]));
                    state.SetAnalog("trig_r", ReadTrigger(polishedPacket[19]));
                    state.SetAnalog("trig_l", ReadTrigger(polishedPacket[20]));
                }
                else
                {
                    state.SetAnalog("lstick_x", ReadStick(128));
                    state.SetAnalog("lstick_y", ReadStick(128));
                    state.SetAnalog("trig_r", ReadTrigger(0));
                    state.SetAnalog("trig_l", ReadTrigger(0));
                }
            }
            else
            {
                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], false);
                }

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

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

                state.SetAnalog("lstick_x", ReadStick(128));
                state.SetAnalog("lstick_y", ReadStick(128));
                state.SetAnalog("trig_r", ReadTrigger(0));
                state.SetAnalog("trig_l", ReadTrigger(0));

                byte xVal = 0;
                j = 8;
                for (byte k = 0; k < 8; ++k)
                {
                    j--;
                    xVal |= (byte)((packet[16 + k] == 0 ? 0 : 1) << j);
                }
                byte yVal = 0;
                j = 8;
                for (byte k = 0; k < 8; ++k)
                {
                    j--;
                    yVal |= (byte)((packet[24 + k] == 0 ? 0 : 1) << j);
                }

                float x = ReadMouse(packet[11] != 0, packet[9] != 0, xVal);
                float y = ReadMouse(packet[10] != 0, packet[8] != 0, yVal);

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

            return(state.Build());
        }
Example #10
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            ControllerStateBuilder state = null;

            if (packet.Length == 13)
            {
                return(Classic.ReadFromPacket(packet));
            }
            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;
                    float y = 0;

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

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

                    SignalTool.SetMouseProperties(x, y, 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, 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, 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").ToUpper();;
                        state.SetButton(scanCode, polishedPacket[i] != 0x00);
                    }
                }
            }

            return(state?.Build());
        }
Example #11
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            polishedPacket[0] = 0;
            for (byte i = 0; i < 8; ++i)
            {
                polishedPacket[0] |= (byte)((packet[i] == 0 ? 0 : 1) << i);
            }

            for (byte i = 0; i < 16; ++i)
            {
                polishedPacket[i + 1] = packet[i + 8];
            }

            int nextNumBytes = 0;

            if (polishedPacket[0] == 0x73 || polishedPacket[0] == 0x79)
            {
                nextNumBytes = 4;
            }
            else if (polishedPacket[0] == 0x12)
            {
                nextNumBytes = 2;
            }

            for (int i = 0; i < nextNumBytes; ++i)
            {
                polishedPacket[17 + i] = 0;
                for (byte j = 0; j < 8; ++j)
                {
                    polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8 + j)] == 0 ? 0 : 1) << j);
                }
            }

            if (polishedPacket[0] == 0x79)
            {
                for (int i = 0; i < 12; ++i)
                {
                    polishedPacket[21 + i] = 0;
                    for (byte j = 0; j < 8; ++j)
                    {
                        polishedPacket[21 + i] |= (byte)((packet[56 + (i * 8 + j)] == 0 ? 0 : 1) << j);
                    }
                }
            }

            if (polishedPacket.Length < POLISHED_PACKET_SIZE)
            {
                return(null);
            }

            if (polishedPacket[0] != 0x41 && polishedPacket[0] != 0x73 && polishedPacket[0] != 0x79 && polishedPacket[0] != 0x12)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

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

                state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }

            if (polishedPacket[0] == 0x73 || polishedPacket[0] == 0x79)
            {
                state.SetAnalog("rstick_x", ReadStick(polishedPacket[17]));
                state.SetAnalog("rstick_y", ReadStick(polishedPacket[18]));
                state.SetAnalog("lstick_x", ReadStick(polishedPacket[19]));
                state.SetAnalog("lstick_y", ReadStick(polishedPacket[20]));
            }
            else
            {
                state.SetAnalog("rstick_x", 0);
                state.SetAnalog("rstick_y", 0);
                state.SetAnalog("lstick_x", 0);
                state.SetAnalog("lstick_y", 0);
            }

            if (polishedPacket[0] == 0x79)
            {
                state.SetAnalog("analog_right", polishedPacket[6] != 0x00 ? ReadAnalogButton(polishedPacket[21]) : 0.0f);
                state.SetAnalog("analog_left", polishedPacket[8] != 0x00 ? ReadAnalogButton(polishedPacket[22]) : 0.0f);
                state.SetAnalog("analog_up", polishedPacket[5] != 0x00 ? ReadAnalogButton(polishedPacket[23]) : 0.0f);
                state.SetAnalog("analog_down", polishedPacket[7] != 0x00 ? ReadAnalogButton(polishedPacket[24]) : 0.0f);

                state.SetAnalog("analog_triangle", polishedPacket[13] != 0x00 ? ReadAnalogButton(polishedPacket[25]) : 0.0f);
                state.SetAnalog("analog_circle", polishedPacket[14] != 0x00 ? ReadAnalogButton(polishedPacket[26]) : 0.0f);
                state.SetAnalog("analog_x", polishedPacket[15] != 0x00 ? ReadAnalogButton(polishedPacket[27]) : 0.0f);
                state.SetAnalog("analog_square", polishedPacket[16] != 0x00 ? ReadAnalogButton(polishedPacket[28]) : 0.0f);

                state.SetAnalog("analog_l1", polishedPacket[11] != 0x00 ? ReadAnalogButton(polishedPacket[29]) : 0.0f);
                state.SetAnalog("analog_r1", polishedPacket[12] != 0x00 ? ReadAnalogButton(polishedPacket[30]) : 0.0f);
                state.SetAnalog("analog_l2", polishedPacket[9] != 0x00 ? ReadAnalogButton(polishedPacket[31]) : 0.0f);
                state.SetAnalog("analog_r2", polishedPacket[10] != 0x00 ? ReadAnalogButton(polishedPacket[32]) : 0.0f);
            }
            else
            {
                state.SetAnalog("analog_right", (float)(polishedPacket[6] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_left", (float)(polishedPacket[8] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_up", (float)(polishedPacket[5] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_down", (float)(polishedPacket[7] != 0x00 ? 1.0 : 0.0));

                state.SetAnalog("analog_triangle", (float)(polishedPacket[13] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_circle", (float)(polishedPacket[14] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_x", (float)(polishedPacket[15] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_square", (float)(polishedPacket[16] != 0x00 ? 1.0 : 0.0));

                state.SetAnalog("analog_l1", (float)(polishedPacket[11] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_r1", (float)(polishedPacket[12] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_l2", (float)(polishedPacket[9] != 0x00 ? 1.0 : 0.0));
                state.SetAnalog("analog_r2", (float)(polishedPacket[10] != 0x00 ? 1.0 : 0.0));
            }

            if (polishedPacket[0] == 0x12)
            {
                float x = ReadMouse(polishedPacket[10] == 0x00, polishedPacket[17]);
                float y = ReadMouse(polishedPacket[9] == 0x00, polishedPacket[18]);
                SignalTool.SetMouseProperties(x, y, state);
            }
            else
            {
                SignalTool.SetMouseProperties(0, 0, state);
            }

            return(state.Build());
        }
Example #12
0
File: CDi.cs Project: edru/RetroSpy
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] cleanedData = new byte[12];
            for (int i = 0; i < 24; i = i + 2)
            {
                cleanedData[i / 2] = (byte)((packet[i]) | ((packet[i + 1]) >> 4));
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

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

                state.SetButton(BUTTONS[i], cleanedData[i] != 0x00);
            }

            // Set double 1 buttons
            state.SetButton("wired-1a", cleanedData[4] != 0x00);
            state.SetButton("wireless-1a", cleanedData[10] != 0x00);

            // Handle 3 overriding other pushes
            if (cleanedData[4] != 0x00 && cleanedData[5] != 0x00)
            {
                state.SetButton("wired-3of3", true);
                state.SetButton("wired-1of3", false);
                state.SetButton("wired-1aof3", false);
                state.SetButton("wired-2of3", false);
            }
            else
            {
                state.SetButton("wired-3of3", false);
                state.SetButton("wired-1of3", cleanedData[4] != 0x00);
                state.SetButton("wired-1aof3", cleanedData[4] != 0x00);
                state.SetButton("wired-2of3", cleanedData[5] != 0x00);
            }

            state.SetButton("wireless-1a", cleanedData[10] != 0x00);

            state.SetAnalog("wired-analog_right", ReadAnalogButton(cleanedData[3]));
            state.SetAnalog("wired-analog_left", ReadAnalogButton(cleanedData[2]));
            state.SetAnalog("wired-analog_up", ReadAnalogButton(cleanedData[0]));
            state.SetAnalog("wired-analog_down", ReadAnalogButton(cleanedData[1]));

            state.SetAnalog("wireless-analog_right", ReadAnalogButton(cleanedData[9]));
            state.SetAnalog("wireless-analog_left", ReadAnalogButton(cleanedData[8]));
            state.SetAnalog("wireless-analog_up", ReadAnalogButton(cleanedData[6]));
            state.SetAnalog("wireless-analog_down", ReadAnalogButton(cleanedData[7]));

            float x = 0;
            float y = 0;

            if (cleanedData[2] > 0)
            {
                x = -1 * ReadAnalogButton(cleanedData[2]);
            }
            else if (cleanedData[3] > 0)
            {
                x = ReadAnalogButton(cleanedData[3]);
            }

            if (cleanedData[0] > 0)
            {
                y = ReadAnalogButton(cleanedData[0]);
            }
            else if (cleanedData[1] > 0)
            {
                y = -1 * ReadAnalogButton(cleanedData[1]);
            }

            state.SetAnalog("stick_x", x);
            state.SetAnalog("stick_y", y);
            SignalTool.SetMouseProperties(x, y, state);

            return(state.Build());
        }
Example #13
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet.Length == PACKET_SIZE)
            {
                byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

                polishedPacket[0] = (byte)((packet[0] >> 4) | packet[1]);
                polishedPacket[1] = (byte)(packet[9] == 0 ? 0 : 1);
                polishedPacket[2] = (byte)(packet[17] == 0 ? 0 : 1);

                for (int i = 18; i < 29; ++i)
                {
                    polishedPacket[i - 15] = (byte)(packet[i] == 0 ? 0 : 1);
                }

                for (byte i = 0; i < 7; ++i)
                {
                    polishedPacket[14] |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i);
                }

                for (byte i = 0; i < 7; ++i)
                {
                    polishedPacket[15] |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i);
                }

                for (int i = 29; i < 61; i += 2)
                {
                    polishedPacket[(i / 2) + 2] = (byte)((packet[i] >> 4) | packet[i + 1]);
                }

                ControllerStateBuilder state = new ControllerStateBuilder();

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

                    state.SetButton(BUTTONS[i], polishedPacket[i] == 0x00);
                }

                float y = ReadMouse(polishedPacket[14]);
                float x = ReadMouse(polishedPacket[15]);

                SignalTool.SetMouseProperties(x, y, polishedPacket[14], polishedPacket[15], state);

                for (int i = 0; i < KEYBOARD_CODES.Length; ++i)
                {
                    if (KEYBOARD_CODES[i] != null)
                    {
                        state.SetButton(KEYBOARD_CODES[i], (polishedPacket[(i / 8) + 16] & (1 << (i % 8))) != 0);
                    }
                }

                return(state.Build());
            }
            else if (packet.Length == JOYSTICK_PACKET_SIZE)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                byte x = 0, y = 0;

                for (byte i = 0; i < 8; ++i)
                {
                    x |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i);
                }

                for (byte i = 0; i < 8; ++i)
                {
                    y |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i);
                }

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

                    state.SetButton(JOYSTICK_BUTTONS[i], packet[i + 18] == 0x00);
                }

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

                return(state.Build());
            }
            else if (packet.Length == TABLET_PACKET_SIZE)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                if (packet[34] != 0)
                {
                    byte x = 0, y = 0, x1 = 0, y1 = 0;

                    for (byte i = 0; i < 8; ++i)
                    {
                        x |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i);
                    }

                    for (byte i = 0; i < 8; ++i)
                    {
                        x1 |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i);
                    }

                    for (byte i = 0; i < 8; ++i)
                    {
                        y |= (byte)((packet[i + 18] == 0 ? 0 : 1) << i);
                    }

                    for (byte i = 0; i < 8; ++i)
                    {
                        y1 |= (byte)((packet[i + 26] == 0 ? 0 : 1) << i);
                    }

                    SetTablet(((x & 0b00011111) * 32) + (x1 >> 3),
                              ((y & 0b00011111) * 128) + (y1 >> 1), state);

                    state.SetButton("Button", (y & 0b10000000) != 0);
                }

                return(state.Build());
            }
            else
            {
                return(null);
            }
        }
Example #14
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < FRAME_HEADER_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            int j = 0;

            byte numWords = 0;

            for (int i = 0; i < 4; ++i)
            {
                numWords |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                numWords |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                j        += 2;
            }

            // Skip sender and receiver address
            j += 16;

            byte dcCommand = 0;

            for (int i = 0; i < 4; ++i)
            {
                dcCommand |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                dcCommand |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                j         += 2;
            }

            if (dcCommand == 8 && numWords >= 1)
            {
                uint controllerType = 0;
                for (int i = 0; i < 2; i++)
                {
                    for (int k = 0; k < 4; ++k)
                    {
                        controllerType |= (uint)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (k * 2) + (i * 8)));
                        controllerType |= (uint)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8)));
                        j += 2;
                    }
                }

                j += 16;

                if (controllerType == 1 && numWords == 3)
                {
                    byte ltrigger = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        ltrigger |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                        ltrigger |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                        j        += 2;
                    }

                    byte rtrigger = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        rtrigger |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                        rtrigger |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                        j        += 2;
                    }

                    int k = 0;
                    for (int i = 0; i < BUTTONS.Length / 2; ++i)
                    {
                        if (!string.IsNullOrEmpty(BUTTONS[k]))
                        {
                            state.SetButton(BUTTONS[k], (packet[j] & 0x2) == 0x0);
                        }

                        if (!string.IsNullOrEmpty(BUTTONS[k + 1]))
                        {
                            state.SetButton(BUTTONS[k + 1], (packet[j + 1] & 0x1) == 0x0);
                        }

                        k += 2;
                        j += 2;
                    }

                    byte joyy2 = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        joyy2 |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                        joyy2 |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                        j     += 2;
                    }

                    byte joyx2 = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        joyx2 |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                        joyx2 |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                        j     += 2;
                    }

                    byte joyy = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        joyy |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                        joyy |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                        j    += 2;
                    }

                    byte joyx = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        joyx |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2)));
                        joyx |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2)));
                        j    += 2;
                    }

                    state.SetAnalog("stick_x", ReadStick(joyx));
                    state.SetAnalog("stick_y", ReadStick(joyy));
                    state.SetAnalog("stick_x2", ReadStick(joyx2));
                    state.SetAnalog("stick_y2", ReadStick(joyy2));
                    state.SetAnalog("trig_r", ReadTrigger(rtrigger));
                    state.SetAnalog("trig_l", ReadTrigger(ltrigger));
                }
                else if (controllerType == 0x200 && numWords == 6)
                {
                    j += 24;

                    int k = 0;
                    for (int i = 0; i < MOUSE_BUTTONS.Length / 2; ++i)
                    {
                        if (!string.IsNullOrEmpty(MOUSE_BUTTONS[k]))
                        {
                            state.SetButton(MOUSE_BUTTONS[k], (packet[j] & 0x2) == 0x0);
                        }

                        if (!string.IsNullOrEmpty(MOUSE_BUTTONS[k + 1]))
                        {
                            state.SetButton(MOUSE_BUTTONS[k + 1], (packet[j + 1] & 0x1) == 0x0);
                        }

                        k += 2;
                        j += 2;
                    }

                    ushort axis1 = 0;
                    for (int i = 1; i >= 0; --i)
                    {
                        for (k = 0; k < 4; ++k)
                        {
                            axis1 |= (ushort)(((packet[j] & 0x02) != 0 ? 1 : 0) << ((7 - k * 2) + (i * 8)));
                            axis1 |= (ushort)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8)));
                            j     += 2;
                        }
                    }

                    ushort axis2 = 0;
                    for (int i = 1; i >= 0; --i)
                    {
                        for (k = 0; k < 4; ++k)
                        {
                            axis2 |= (ushort)(((packet[j] & 0x02) != 0 ? 1 : 0) << ((7 - k * 2) + (i * 8)));
                            axis2 |= (ushort)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8)));
                            j     += 2;
                        }
                    }

                    j += 16;

                    ushort axis3 = 0;
                    for (int i = 1; i >= 0; --i)
                    {
                        for (k = 0; k < 4; ++k)
                        {
                            axis3 |= (ushort)(((packet[j] & 0x02) != 0 ? 1 : 0) << ((7 - k * 2) + (i * 8)));
                            axis3 |= (ushort)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (k * 2) + (i * 8)));
                            j     += 2;
                        }
                    }

                    float x = (axis2 - 512) / 512.0f;
                    float y = -1 * (axis1 - 512) / 512.0f;

                    SignalTool.SetMouseProperties(x, y, state);

                    state.SetButton("scroll_up", axis3 < 512);
                    state.SetButton("scroll_down", axis3 > 512);
                }
                return(state.Build());
            }

            return(null);
        }
Example #15
0
        public static ControllerState ReadFromPacket_FMTowns(byte[] packet)
        {
            if (packet.Length != 9 && packet.Length != 70)
            {
                return(null);
            }

            if (packet.Length == 9)
            {
                byte[] polishedPacket = new byte[BUTTONS_FMTOWNS.Length];

                if (packet[0] != 0 && packet[1] != 0)
                {
                    packet[0]         = packet[1] = 0;
                    polishedPacket[9] = 1;
                }

                if (packet[2] != 0 && packet[3] != 0)
                {
                    packet[2]          = packet[3] = 0;
                    polishedPacket[10] = 1;
                }

                for (int i = 0; i < packet.Length; ++i)
                {
                    polishedPacket[i] = packet[i];
                }

                return(ReadPacketButtons(polishedPacket, BUTTONS_FMTOWNS));
            }
            else
            {
                int    j = 0;
                byte[] reconstructedPacket = new byte[34];
                for (int i = 0; i < 34; ++i)
                {
                    reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]);
                    j += 2;
                }

                byte[] polishedPacket = new byte[256];

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

                ControllerStateBuilder state = new ControllerStateBuilder();

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

                    state.SetButton(SCANCODES_FMTOWNS[i], polishedPacket[i] != 0x00);
                }

                SignalTool.SetMouseProperties(((sbyte)reconstructedPacket[33]) / -128.0f, ((sbyte)reconstructedPacket[32]) / 128.0f, state);

                state.SetButton("left", packet[68] == 1);
                state.SetButton("right", packet[69] == 1);

                return(state.Build());
            }
        }