public void OnReadFile(string filename, ref byte[] inputReport) { try { //Console.WriteLine("OnReadFile {0}", filename); //if((printFrequency++)%30 == 0) // PrintAnalogSticksAsDegrees(inputReport.Skip(1).Take(4).ToArray()); // Expect inputReport to be modified if (Injector.Callback != null) { // Parse the state var state = DualShockState.ParseFromDualshockRaw(inputReport); // Skip if state is invalid if (state == null) { return; } // Expect it to be modified Injector.Callback(ref state); // Convert it back state.ConvertToDualshockRaw(ref inputReport); } } catch (Exception ex) { Log.Logger.Error("Problem in OnReadFile: " + ex.Message); } }
public void OnReadFile(string filename, ref byte[] inputReport) { try { Log.Verbose("InjectionInterface.OnReadFile filename " + filename); //if((printFrequency++)%30 == 0) // PrintAnalogSticksAsDegrees(inputReport.Skip(1).Take(4).ToArray()); // Expect inputReport to be modified if (Injector.Callback != null) { Log.Verbose("InjectionInterface.OnReadFile Injector.Callback " + Injector.Callback.ToString()); // Parse the state DualShockState state = DualShockState.ParseFromDualshockRaw(inputReport); // Skip if state is invalid if (state == null) { return; } // Expect it to be modified Injector.Callback(ref state); // Convert it back state.ConvertToDualshockRaw(ref inputReport); } } catch (Exception ex) { ExceptionLogger.LogException("InjectionInterface.OnReadFile error", ex); } }
public static bool IsDefaultState(DualShockState state) { return((state.LX == _defaultState.LX || (state.LX >= AnalogDeadZoneMin && state.LX <= AnalogDeadZoneMax)) && (state.LY == _defaultState.LY || (state.LY >= AnalogDeadZoneMin && state.LY <= AnalogDeadZoneMax)) && (state.RX == _defaultState.RX || (state.RX >= AnalogDeadZoneMin && state.RX <= AnalogDeadZoneMax)) && (state.RY == _defaultState.RY || (state.RY >= AnalogDeadZoneMin && state.RY <= AnalogDeadZoneMax)) && state.L2 == _defaultState.L2 && state.R2 == _defaultState.R2 && state.Triangle == _defaultState.Triangle && state.Circle == _defaultState.Circle && state.Cross == _defaultState.Cross && state.Square == _defaultState.Square && state.DPad_Up == _defaultState.DPad_Up && state.DPad_Down == _defaultState.DPad_Down && state.DPad_Left == _defaultState.DPad_Left && state.DPad_Right == _defaultState.DPad_Right && state.L1 == _defaultState.L1 && state.R1 == _defaultState.R1 && state.Share == _defaultState.Share && state.Options == _defaultState.Options && state.L3 == _defaultState.L3 && state.R3 == _defaultState.R3 && state.PS == _defaultState.PS && (state.Touch1 != null && state.Touch1.IsTouched) && (state.Touch2 != null && state.Touch2.IsTouched)); }
public void OnReadFile(string filename, ref byte[] inputReport) { try { //Console.WriteLine("OnReadFile {0}", filename); // Expect inputReport to be modified if (Interceptor.Callback != null) { // Parse the state var state = DualShockState.ParseFromDualshockRaw(inputReport); // Skip if state is invalid if (state == null) { return; } // Expect it to be modified Interceptor.Callback(ref state); // Convert it back state.ConvertToDualshockRaw(ref inputReport); } } catch (Exception) { } }
public void CopyTo(DualShockState state) { state.ReportTimeStamp = ReportTimeStamp; state.LX = LX; state.LY = LY; state.RX = RX; state.RY = RY; state.L2 = L2; state.R2 = R2; state.Triangle = Triangle; state.Circle = Circle; state.Cross = Cross; state.Square = Square; state.DPad_Up = DPad_Up; state.DPad_Down = DPad_Down; state.DPad_Left = DPad_Left; state.DPad_Right = DPad_Right; state.L1 = L1; state.R3 = R3; state.Share = Share; state.Options = Options; state.R1 = R1; state.L3 = L3; state.PS = PS; state.Touch1 = Touch1 == null ? null : Touch1.Clone(); state.Touch2 = Touch2 == null ? null : Touch2.Clone(); state.TouchButton = TouchButton; state.TouchPacketCounter = TouchPacketCounter; state.FrameCounter = FrameCounter; state.Battery = Battery; state.IsCharging = IsCharging; state.AccelX = AccelX; state.AccelY = AccelY; state.AccelZ = AccelZ; state.GyroX = GyroX; state.GyroY = GyroY; state.GyroZ = GyroZ; }
public DualShockState(DualShockState state) { ReportTimeStamp = state.ReportTimeStamp; LX = state.LX; LY = state.LY; RX = state.RX; RY = state.RY; L2 = state.L2; R2 = state.R2; Triangle = state.Triangle; Circle = state.Circle; Cross = state.Cross; Square = state.Square; DPad_Up = state.DPad_Up; DPad_Down = state.DPad_Down; DPad_Left = state.DPad_Left; DPad_Right = state.DPad_Right; L1 = state.L1; R3 = state.R3; Share = state.Share; Options = state.Options; R1 = state.R1; L3 = state.L3; PS = state.PS; Touch1 = state.Touch1 == null ? null : state.Touch1.Clone(); Touch2 = state.Touch2 == null ? null : state.Touch2.Clone(); TouchButton = state.TouchButton; TouchPacketCounter = state.TouchPacketCounter; FrameCounter = state.FrameCounter; Battery = state.Battery; IsCharging = state.IsCharging; AccelX = state.AccelX; AccelY = state.AccelY; AccelZ = state.AccelZ; GyroX = state.GyroX; GyroY = state.GyroY; GyroZ = state.GyroZ; }
public static DualShockState ParseFromDualshockRaw(byte[] data) { // Validate data if (data == null) { return(null); } if (data[0] != 0x1) { return(null); } // Create empty state to fill in data var result = new DualShockState(); result.LX = data[1]; result.LY = data[2]; result.RX = data[3]; result.RY = data[4]; result.L2 = data[8]; result.R2 = data[9]; result.Triangle = (data[5] & (byte)VK.Triangle) != 0; result.Circle = (data[5] & (byte)VK.Circle) != 0; result.Cross = (data[5] & (byte)VK.Cross) != 0; result.Square = (data[5] & (byte)VK.Square) != 0; result.DPad_Up = (data[5] & (byte)VK.DPad_Up) != 0; result.DPad_Down = (data[5] & (byte)VK.DPad_Down) != 0; result.DPad_Left = (data[5] & (byte)VK.DPad_Left) != 0; result.DPad_Right = (data[5] & (byte)VK.DPad_Right) != 0; //Convert dpad into individual On/Off bits instead of a clock representation byte dpadState = 0; dpadState = (byte)( ((result.DPad_Right ? 1 : 0) << 0) | ((result.DPad_Left ? 1 : 0) << 1) | ((result.DPad_Down ? 1 : 0) << 2) | ((result.DPad_Up ? 1 : 0) << 3)); switch (dpadState) { case 0: result.DPad_Up = true; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = false; break; // ↑ case 1: result.DPad_Up = true; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = true; break; // ↑→ case 2: result.DPad_Up = false; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = true; break; // → case 3: result.DPad_Up = false; result.DPad_Down = true; result.DPad_Left = false; result.DPad_Right = true; break; // ↓→ case 4: result.DPad_Up = false; result.DPad_Down = true; result.DPad_Left = false; result.DPad_Right = false; break; // ↓ case 5: result.DPad_Up = false; result.DPad_Down = true; result.DPad_Left = true; result.DPad_Right = false; break; // ↓← case 6: result.DPad_Up = false; result.DPad_Down = false; result.DPad_Left = true; result.DPad_Right = false; break; // ← case 7: result.DPad_Up = true; result.DPad_Down = false; result.DPad_Left = true; result.DPad_Right = false; break; // ↑← case 8: result.DPad_Up = false; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = false; break; // - } bool L2Pressed = (data[6] & (byte)VK.L2) != 0; bool R2Pressed = (data[6] & (byte)VK.R2) != 0; result.L1 = (data[6] & (byte)VK.L1) != 0; result.R1 = (data[6] & (byte)VK.R1) != 0; result.Share = (data[6] & (byte)VK.Share) != 0; result.Options = (data[6] & (byte)VK.Options) != 0; result.L3 = (data[6] & (byte)VK.L3) != 0; result.R3 = (data[6] & (byte)VK.R3) != 0; result.PS = (data[7] & (byte)VK.PS) != 0; result.TouchButton = (data[7] & (byte)VK.TouchButton) != 0; result.FrameCounter = (byte)(data[7] >> 2); return(result); }
public static DualShockState ParseFromDualshockRaw(byte[] data) { // Validate data if (data == null) { return(null); } if (data[0] != 0x1) { return(null); } // Create empty state to fill in data var result = new DualShockState(); result.ReportTimeStamp = DateTime.UtcNow; // timestamp with UTC in case system time zone changes result.LX = data[1]; result.LY = data[2]; result.RX = data[3]; result.RY = data[4]; result.L2 = data[8]; result.R2 = data[9]; result.Triangle = (data[5] & (byte)VK.Triangle) != 0; result.Circle = (data[5] & (byte)VK.Circle) != 0; result.Cross = (data[5] & (byte)VK.Cross) != 0; result.Square = (data[5] & (byte)VK.Square) != 0; result.DPad_Up = (data[5] & (byte)VK.DPad_Up) != 0; result.DPad_Down = (data[5] & (byte)VK.DPad_Down) != 0; result.DPad_Left = (data[5] & (byte)VK.DPad_Left) != 0; result.DPad_Right = (data[5] & (byte)VK.DPad_Right) != 0; //Convert dpad into individual On/Off bits instead of a clock representation byte dpadState = 0; dpadState = (byte)( ((result.DPad_Right ? 1 : 0) << 0) | ((result.DPad_Left ? 1 : 0) << 1) | ((result.DPad_Down ? 1 : 0) << 2) | ((result.DPad_Up ? 1 : 0) << 3)); switch (dpadState) { case 0: result.DPad_Up = true; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = false; break; // ↑ case 1: result.DPad_Up = true; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = true; break; // ↑→ case 2: result.DPad_Up = false; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = true; break; // → case 3: result.DPad_Up = false; result.DPad_Down = true; result.DPad_Left = false; result.DPad_Right = true; break; // ↓→ case 4: result.DPad_Up = false; result.DPad_Down = true; result.DPad_Left = false; result.DPad_Right = false; break; // ↓ case 5: result.DPad_Up = false; result.DPad_Down = true; result.DPad_Left = true; result.DPad_Right = false; break; // ↓← case 6: result.DPad_Up = false; result.DPad_Down = false; result.DPad_Left = true; result.DPad_Right = false; break; // ← case 7: result.DPad_Up = true; result.DPad_Down = false; result.DPad_Left = true; result.DPad_Right = false; break; // ↑← case 8: result.DPad_Up = false; result.DPad_Down = false; result.DPad_Left = false; result.DPad_Right = false; break; // - } bool L2Pressed = (data[6] & (byte)VK.L2) != 0; bool R2Pressed = (data[6] & (byte)VK.R2) != 0; result.L1 = (data[6] & (byte)VK.L1) != 0; result.R1 = (data[6] & (byte)VK.R1) != 0; result.Share = (data[6] & (byte)VK.Share) != 0; result.Options = (data[6] & (byte)VK.Options) != 0; result.L3 = (data[6] & (byte)VK.L3) != 0; result.R3 = (data[6] & (byte)VK.R3) != 0; result.PS = (data[7] & (byte)VK.PS) != 0; result.TouchButton = (data[7] & (byte)VK.TouchButton) != 0; result.FrameCounter = (byte)(data[7] >> 2); // Charging/Battery try { result.IsCharging = (data[30] & 0x10) != 0; result.Battery = (byte)((data[30] & 0x0F) * 10); if (data[30] != priorInputReport30) { priorInputReport30 = data[30]; } } catch { throw new InterceptorException("Index out of bounds: battery"); } // Accelerometer byte[] accel = new byte[6]; Array.Copy(data, 14, accel, 0, 6); result.AccelX = (short)((ushort)(accel[2] << 8) | accel[3]); result.AccelY = (short)((ushort)(accel[0] << 8) | accel[1]); result.AccelZ = (short)((ushort)(accel[4] << 8) | accel[5]); // Gyro byte[] gyro = new byte[6]; Array.Copy(data, 20, gyro, 0, 6); result.GyroX = (short)((ushort)(gyro[0] << 8) | gyro[1]); result.GyroY = (short)((ushort)(gyro[2] << 8) | gyro[3]); result.GyroZ = (short)((ushort)(gyro[4] << 8) | gyro[5]); try { for (int touches = data[-1 + TOUCHPAD_DATA_OFFSET - 1], touchOffset = 0; touches > 0; touches--, touchOffset += 9) { //bool touchLeft = (data[1 + TOUCHPAD_DATA_OFFSET + touchOffset] + ((data[2 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0x0F) * 255) >= 1920 * 2 / 5) ? false : true; //bool touchRight = (data[1 + TOUCHPAD_DATA_OFFSET + touchOffset] + ((data[2 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0x0F) * 255) < 1920 * 2 / 5) ? false : true; byte touchID1 = (byte)(data[0 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0x7F); byte touchID2 = (byte)(data[4 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0x7F); bool isTouch1 = (data[0 + TOUCHPAD_DATA_OFFSET + touchOffset] >> 7) != 0 ? false : true; // >= 1 touch detected bool isTouch2 = (data[4 + TOUCHPAD_DATA_OFFSET + touchOffset] >> 7) != 0 ? false : true; // 2 touches detected int currentX1 = data[1 + TOUCHPAD_DATA_OFFSET + touchOffset] + ((data[2 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0x0F) * 255); int currentY1 = ((data[2 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0xF0) >> 4) + (data[3 + TOUCHPAD_DATA_OFFSET + touchOffset] * 16); int currentX2 = data[5 + TOUCHPAD_DATA_OFFSET + touchOffset] + ((data[6 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0x0F) * 255); int currentY2 = ((data[6 + TOUCHPAD_DATA_OFFSET + touchOffset] & 0xF0) >> 4) + (data[7 + TOUCHPAD_DATA_OFFSET + touchOffset] * 16); result.TouchPacketCounter = data[-1 + TOUCHPAD_DATA_OFFSET + touchOffset]; result.Touch1 = new Touch(touchID1, isTouch1, currentX1, currentY1); result.Touch2 = new Touch(touchID2, isTouch2, currentX2, currentY2); } } catch { throw new InterceptorException("Index out of bounds: touchpad"); } return(result); }