public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(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, xVal, yVal, state); } return(state.Build()); }
static public ControllerState ReadFromPacket_SNES(byte[] packet) { if (packet.Length < BUTTONS_SNES.Length) { return(null); } var 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()); }
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 = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length)); float y = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8)); state.SetAnalog("stick_x", x, SignalTool.ReadByte(packet, BUTTONS.Length)); state.SetAnalog("stick_y", y, SignalTool.ReadByte(packet, BUTTONS.Length + 8)); SignalTool.SetMouseProperties(x, y, SignalTool.ReadByte(packet, BUTTONS.Length), SignalTool.ReadByte(packet, BUTTONS.Length + 8), state); return(state.Build()); }
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); } 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()); }
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()); }
static public 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); } var 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()); }
public static ControllerStateEventArgs ReadFromPacketSNES(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packet.Length < BUTTONS_SNES.Length) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); if (packet.Length == 32 && packet[15] == 0x00 && packet[13] != 0x00) { for (int i = 0; i < BUTTONS_SNES_NTTDATA.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_SNES_NTTDATA[i])) { continue; } state.SetButton(BUTTONS_SNES_NTTDATA[i], packet[i] != 0x00); } } else { 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 (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, SignalTool.ReadByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1), SignalTool.ReadByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1), state); } } return(state.Build()); }
static public 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); } var 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()); }
static public 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()); }
static public 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); } var 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()); }
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()); }
static public ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < FRAME_HEADER_SIZE) { return(null); } var 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); }
#pragma warning disable CA1508 // Avoid dead conditional code public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(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), joyx); state.SetAnalog("stick_y", ReadStick(joyy), joyy); state.SetAnalog("stick_x2", ReadStick(joyx2), joyx2); state.SetAnalog("stick_y2", ReadStick(joyy2), joyy2); state.SetAnalog("trig_r", ReadTrigger(rtrigger), rtrigger); state.SetAnalog("trig_l", ReadTrigger(ltrigger), 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, axis2, axis1, state); state.SetButton("scroll_up", axis3 < 512); state.SetButton("scroll_down", axis3 > 512); } else if (controllerType == 0x40 && numWords == 3) { for (int i = 0; i < KEYS.Length; ++i) { if (KEYS[i] != null) { state.SetButton(KEYS[i], false); } } byte[] keycode = new byte[6]; keycode[1] = 0; for (int i = 0; i < 4; ++i) { keycode[1] |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); keycode[1] |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (KEYS[keycode[1]] != null) { state.SetButton(KEYS[keycode[1]], true); } keycode[0] = 0; for (int i = 0; i < 4; ++i) { keycode[0] |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); keycode[0] |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (KEYS[keycode[0]] != null) { state.SetButton(KEYS[keycode[0]], true); } int k = 0; for (int i = 0; i < MODIFER_KEYS.Length / 2; ++i) { if (!string.IsNullOrEmpty(MODIFER_KEYS[k])) { state.SetButton(MODIFER_KEYS[k], (packet[j] & 0x2) != 0x0); } if (!string.IsNullOrEmpty(MODIFER_KEYS[k + 1])) { state.SetButton(MODIFER_KEYS[k + 1], (packet[j + 1] & 0x1) != 0x0); } k += 2; j += 2; } keycode[5] = 0; for (int i = 0; i < 4; ++i) { keycode[5] |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); keycode[5] |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (KEYS[keycode[5]] != null) { state.SetButton(KEYS[keycode[5]], true); } keycode[4] = 0; for (int i = 0; i < 4; ++i) { keycode[4] |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); keycode[4] |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (KEYS[keycode[4]] != null) { state.SetButton(KEYS[keycode[4]], true); } keycode[3] = 0; for (int i = 0; i < 4; ++i) { keycode[3] |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); keycode[3] |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (KEYS[keycode[3]] != null) { state.SetButton(KEYS[keycode[3]], true); } keycode[2] = 0; for (int i = 0; i < 4; ++i) { keycode[2] |= (byte)(((packet[j] & 0x02) != 0 ? 1 : 0) << (7 - (i * 2))); keycode[2] |= (byte)(((packet[j + 1] & 0x01) != 0 ? 1 : 0) << (6 - (i * 2))); j += 2; } if (KEYS[keycode[2]] != null) { state.SetButton(KEYS[keycode[2]], true); } } return(state.Build()); } return(null); }
#pragma warning disable CA1508 // Avoid dead conditional code public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packet.Length == PACKET_SIZE) { 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); } SignalTool.GenerateFakeStick(state, "l_twinstick_x", "l_twinstick_y", polishedPacket[4] != 0x00, polishedPacket[3] != 0x00, polishedPacket[2] != 0x00, polishedPacket[1] != 0x00); SignalTool.GenerateFakeStick(state, "r_twinstick_x", "r_twinstick_y", polishedPacket[11] != 0x00, polishedPacket[8] != 0x00, polishedPacket[10] != 0x00, polishedPacket[12] != 0x00); if (polishedPacket[0] == 0x16) { state.SetAnalog("lstick_x", ReadStick(polishedPacket[17]), polishedPacket[17]); state.SetAnalog("lstick_y", ReadStick(polishedPacket[18]), polishedPacket[18]); state.SetAnalog("trig_r", ReadTrigger(polishedPacket[19]), polishedPacket[19]); state.SetAnalog("trig_l", ReadTrigger(polishedPacket[20]), polishedPacket[20]); } else { state.SetAnalog("lstick_x", ReadStick(128), 128); state.SetAnalog("lstick_y", ReadStick(128), 128); state.SetAnalog("trig_r", ReadTrigger(0), 0); state.SetAnalog("trig_l", ReadTrigger(0), 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), 128); state.SetAnalog("lstick_y", ReadStick(128), 128); state.SetAnalog("trig_r", ReadTrigger(0), 0); state.SetAnalog("trig_l", ReadTrigger(0), 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, xVal, yVal, state); } return(state.Build()); } else if (packet.Length == KEYBOARD_PACKET_SIZE) { ControllerStateBuilder state = new ControllerStateBuilder(); int j = 0; byte[] reconstructedPacket = new byte[KEYBOARD_POLISHED_PACKET_SIZE]; for (int i = 0; i < KEYBOARD_POLISHED_PACKET_SIZE; ++i) { reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]); j += 2; } for (int i = 0; i < KEYS.Length; ++i) { if (KEYS[i] != null) { state.SetButton(KEYS[i], (reconstructedPacket[i / 8] & (1 << (i % 8))) != 0); } } return(state.Build()); } else { return(null); } }
#pragma warning disable CA1508 // Avoid dead conditional code public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packet.Length < PACKET_SIZE_WO_RUMBLE) { return(null); } bool hasRumble = packet.Length == PACKET_SIZE; 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 || polishedPacket[0] == 0x53 || polishedPacket[0] == 0x23) { 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 (hasRumble) { for (int i = 0; i < 2; ++i) { polishedPacket[33 + i] = 0; for (byte j = 0; j < 8; ++j) { polishedPacket[33 + i] |= (byte)((packet[152 + (i * 8) + j] == 0 ? 0 : 1) << j); } } } if (polishedPacket.Length < (hasRumble ? POLISHED_PACKET_SIZE : POLISHED_PACKET_SIZE - 2)) { return(null); } if (polishedPacket[0] != 0x41 && polishedPacket[0] != 0x73 && polishedPacket[0] != 0x79 && polishedPacket[0] != 0x12 && polishedPacket[0] != 0x23 && polishedPacket[0] != 0x53) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); if (polishedPacket[0] == 0x53) { SetButtons(ANALOG_BUTTONS, polishedPacket, state); } else if (polishedPacket[0] == 0x23) { SetButtons(NEGCON_BUTTONS, polishedPacket, state); } else { SetButtons(BUTTONS, polishedPacket, state); } if (hasRumble) { state.SetAnalog("motor1", ReadAnalogButton(polishedPacket[33]), polishedPacket[33]); state.SetAnalog("motor2", ReadAnalogButton(polishedPacket[34]), polishedPacket[34]); } else { state.SetAnalog("motor1", 0, 0); state.SetAnalog("motor2", 0, 0); } if (polishedPacket[0] == 0x73 || polishedPacket[0] == 0x79 || polishedPacket[0] == 0x53) { state.SetAnalog("rstick_x", ReadStick(polishedPacket[17]), polishedPacket[17]); state.SetAnalog("rstick_y", ReadStick(polishedPacket[18]), polishedPacket[18]); state.SetAnalog("lstick_x", ReadStick(polishedPacket[19]), polishedPacket[19]); state.SetAnalog("lstick_y", ReadStick(polishedPacket[20]), polishedPacket[20]); } else if (polishedPacket[0] == 0x23) { float steering = ReadNegConSteering(polishedPacket[17]); if (steering < 0) { state.SetAnalog("l_steering", steering * -1, polishedPacket[17]); state.SetAnalog("r_steering", 0.0f, 0); } else if (steering > 0) { state.SetAnalog("l_steering", 0.0f, 0); state.SetAnalog("r_steering", steering, polishedPacket[17]); } else { state.SetAnalog("l_steering", 0.0f, 0); state.SetAnalog("r_steering", 0.0f, 0); } state.SetAnalog("I", ReadAnalogButton(polishedPacket[18]), polishedPacket[18]); state.SetAnalog("II", ReadAnalogButton(polishedPacket[19]), polishedPacket[19]); state.SetAnalog("l1", ReadAnalogButton(polishedPacket[20]), polishedPacket[20]); } else { state.SetAnalog("rstick_x", 0, 0); state.SetAnalog("rstick_y", 0, 0); state.SetAnalog("lstick_x", 0, 0); state.SetAnalog("lstick_y", 0, 0); } if (polishedPacket[0] == 0x79) { state.SetAnalog("analog_right", polishedPacket[6] != 0x00 ? ReadAnalogButton(polishedPacket[21]) : 0.0f, polishedPacket[6] != 0x00 ? polishedPacket[21] : 0); state.SetAnalog("analog_left", polishedPacket[8] != 0x00 ? ReadAnalogButton(polishedPacket[22]) : 0.0f, polishedPacket[8] != 0x00 ? polishedPacket[22] : 0); state.SetAnalog("analog_up", polishedPacket[5] != 0x00 ? ReadAnalogButton(polishedPacket[23]) : 0.0f, polishedPacket[5] != 0x00 ? polishedPacket[23] : 0); state.SetAnalog("analog_down", polishedPacket[7] != 0x00 ? ReadAnalogButton(polishedPacket[24]) : 0.0f, polishedPacket[7] != 0x00 ? polishedPacket[24] : 0); state.SetAnalog("analog_triangle", polishedPacket[13] != 0x00 ? ReadAnalogButton(polishedPacket[25]) : 0.0f, polishedPacket[13] != 0x00 ? polishedPacket[25] : 0); state.SetAnalog("analog_circle", polishedPacket[14] != 0x00 ? ReadAnalogButton(polishedPacket[26]) : 0.0f, polishedPacket[14] != 0x00 ? polishedPacket[26] : 0); state.SetAnalog("analog_x", polishedPacket[15] != 0x00 ? ReadAnalogButton(polishedPacket[27]) : 0.0f, polishedPacket[15] != 0x00 ? polishedPacket[27] : 0); state.SetAnalog("analog_square", polishedPacket[16] != 0x00 ? ReadAnalogButton(polishedPacket[28]) : 0.0f, polishedPacket[16] != 0x00 ? polishedPacket[28] : 0); state.SetAnalog("analog_l1", polishedPacket[11] != 0x00 ? ReadAnalogButton(polishedPacket[29]) : 0.0f, polishedPacket[11] != 0x00 ? polishedPacket[29] : 0); state.SetAnalog("analog_r1", polishedPacket[12] != 0x00 ? ReadAnalogButton(polishedPacket[30]) : 0.0f, polishedPacket[12] != 0x00 ? polishedPacket[30] : 0); state.SetAnalog("analog_l2", polishedPacket[9] != 0x00 ? ReadAnalogButton(polishedPacket[31]) : 0.0f, polishedPacket[9] != 0x00 ? polishedPacket[31] : 0); state.SetAnalog("analog_r2", polishedPacket[10] != 0x00 ? ReadAnalogButton(polishedPacket[32]) : 0.0f, polishedPacket[10] != 0x00 ? polishedPacket[32] : 0); } else { state.SetAnalog("analog_right", (float)(polishedPacket[6] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_left", (float)(polishedPacket[8] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_up", (float)(polishedPacket[5] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_down", (float)(polishedPacket[7] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_triangle", (float)(polishedPacket[13] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_circle", (float)(polishedPacket[14] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_x", (float)(polishedPacket[15] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_square", (float)(polishedPacket[16] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_l1", (float)(polishedPacket[11] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_r1", (float)(polishedPacket[12] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_l2", (float)(polishedPacket[9] != 0x00 ? 1.0 : 0.0), 0); state.SetAnalog("analog_r2", (float)(polishedPacket[10] != 0x00 ? 1.0 : 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, polishedPacket[17], polishedPacket[18], state); } else { SignalTool.SetMouseProperties(0, 0, 0, 0, state); } return(state.Build()); }
static public 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; var 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); } } var 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()); } }
public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packet.Length >= PACKET_SIZE) { 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); return(state.Build()); } else if (packet.Length == 2) { ControllerStateBuilder state = new ControllerStateBuilder(); state.SetAnalog("paddle", packet[0] / 256.0f, packet[0]); state.SetButton("1", packet[1] != 0); return(state.Build()); } else if (packet.Length == 4) { ControllerStateBuilder state = new ControllerStateBuilder(); sbyte x = (sbyte)packet[0]; sbyte y = (sbyte)packet[1]; if (Math.Abs(x) != 0 && Math.Abs(x) > maxX) { maxX = Math.Abs(x); } if (Math.Abs(y) != 0 && Math.Abs(y) > maxY) { maxY = Math.Abs(y); } SignalTool.SetMouseProperties(-1.0f * x / maxX, y / maxY, x, y, state); state.SetButton("1", packet[2] != 0); state.SetButton("2", packet[3] != 0); return(state.Build()); } else { return(null); } }
static public 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)); } var 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()); }