public static void MapCustom(int device, DS4State cState, DS4State MappedState, DS4StateExposed eState, Mouse tp, ControlService ctrl) { MappedState.LX = 127; MappedState.LY = 127; MappedState.RX = 127; MappedState.RY = 127; int mouseDeltaX = 0; int mouseDeltaY = 0; SyntheticState state = deviceState[device]; if ((Global.GetActions().Count > 0) && ((Global.ProfileActions[device].Count > 0) || !string.IsNullOrEmpty(Global.tempprofilename[device]))) { MapCustomAction(device, cState, MappedState, eState, tp, ctrl); } if (ctrl.DS4Controllers[device] == null) { return; } cState.CopyTo(MappedState); List<DS4Controls> cross = new List<DS4Controls>(); List<DS4Controls> circle = new List<DS4Controls>(); List<DS4Controls> square = new List<DS4Controls>(); List<DS4Controls> triangle = new List<DS4Controls>(); List<DS4Controls> options = new List<DS4Controls>(); List<DS4Controls> share = new List<DS4Controls>(); List<DS4Controls> dPadUp = new List<DS4Controls>(); List<DS4Controls> dPadDown = new List<DS4Controls>(); List<DS4Controls> dPadLeft = new List<DS4Controls>(); List<DS4Controls> dpadRight = new List<DS4Controls>(); List<DS4Controls> ps = new List<DS4Controls>(); List<DS4Controls> l1 = new List<DS4Controls>(); List<DS4Controls> r1 = new List<DS4Controls>(); List<DS4Controls> l2 = new List<DS4Controls>(); List<DS4Controls> r2 = new List<DS4Controls>(); List<DS4Controls> l3 = new List<DS4Controls>(); List<DS4Controls> r3 = new List<DS4Controls>(); List<DS4Controls> lxn = new List<DS4Controls>(); List<DS4Controls> lxp = new List<DS4Controls>(); List<DS4Controls> lyn = new List<DS4Controls>(); List<DS4Controls> lyp = new List<DS4Controls>(); List<DS4Controls> rxn = new List<DS4Controls>(); List<DS4Controls> rxp = new List<DS4Controls>(); List<DS4Controls> ryn = new List<DS4Controls>(); List<DS4Controls> ryp = new List<DS4Controls>(); DS4Controls usingExtra = DS4Controls.None; foreach (DS4ControlSettings dcs in Global.getDS4CSettings(device)) { object action = null; DS4ControlSettings.ActionType actionType = 0; DS4KeyType keyType = DS4KeyType.None; if ((dcs.shiftAction != null) && ShiftTrigger(dcs.shiftTrigger, device, cState, eState, tp)) { action = dcs.shiftAction; actionType = dcs.shiftActionType; keyType = dcs.shiftKeyType; } else if (dcs.action != null) { action = dcs.action; actionType = dcs.actionType; keyType = dcs.keyType; } if (action != null) { switch (actionType) { case DS4ControlSettings.ActionType.Macro: if (getBoolMapping(device, dcs.control, cState, eState, tp)) { resetToDefaultValue(dcs.control, MappedState); PlayMacro(device, macroControl, string.Join("/", (int[])action), dcs.control, keyType); } else if (!getBoolMapping(device, dcs.control, cState, eState, tp)) { EndMacro(device, macroControl, string.Join("/", (int[])action), dcs.control); } break; case DS4ControlSettings.ActionType.Key: ushort value = ushort.Parse(action.ToString()); if (getBoolMapping(device, dcs.control, cState, eState, tp)) { resetToDefaultValue(dcs.control, MappedState); SyntheticState.KeyPresses kp; if (!state.keyPresses.TryGetValue(value, out kp)) { state.keyPresses[value] = kp = new SyntheticState.KeyPresses(); } if (keyType.HasFlag(DS4KeyType.ScanCode)) { kp.current.scanCodeCount++; } else { kp.current.vkCount++; } if (keyType.HasFlag(DS4KeyType.Toggle)) { if (!pressedonce[value]) { kp.current.toggle = !kp.current.toggle; pressedonce[value] = true; } kp.current.toggleCount++; } kp.current.repeatCount++; } else { pressedonce[value] = false; } break; case DS4ControlSettings.ActionType.Button: int keyvalue = 0; bool isAnalog = dcs.control.ToString().Contains("LX") || dcs.control.ToString().Contains("RX") || dcs.control.ToString().Contains("LY") || dcs.control.ToString().Contains("LY") || dcs.control.ToString().Contains("R2") || dcs.control.ToString().Contains("L2") || dcs.control.ToString().Contains("Gyro"); switch (getX360ControlsByName(action.ToString())) { case X360Controls.A: cross.Add(dcs.control); break; case X360Controls.B: circle.Add(dcs.control); break; case X360Controls.X: square.Add(dcs.control); break; case X360Controls.Y: triangle.Add(dcs.control); break; case X360Controls.LB: l1.Add(dcs.control); break; case X360Controls.LS: l3.Add(dcs.control); break; case X360Controls.RB: r1.Add(dcs.control); break; case X360Controls.RS: r3.Add(dcs.control); break; case X360Controls.DpadUp: dPadUp.Add(dcs.control); break; case X360Controls.DpadDown: dPadDown.Add(dcs.control); break; case X360Controls.DpadLeft: dPadLeft.Add(dcs.control); break; case X360Controls.DpadRight: dpadRight.Add(dcs.control); break; case X360Controls.Start: options.Add(dcs.control); break; case X360Controls.Guide: ps.Add(dcs.control); break; case X360Controls.Back: share.Add(dcs.control); break; case X360Controls.LXNeg: lxn.Add(dcs.control); break; case X360Controls.LYNeg: lyn.Add(dcs.control); break; case X360Controls.RXNeg: rxn.Add(dcs.control); break; case X360Controls.RYNeg: ryn.Add(dcs.control); break; case X360Controls.LXPos: lxp.Add(dcs.control); break; case X360Controls.LYPos: lyp.Add(dcs.control); break; case X360Controls.RXPos: rxp.Add(dcs.control); break; case X360Controls.RYPos: ryp.Add(dcs.control); break; case X360Controls.LT: l2.Add(dcs.control); break; case X360Controls.RT: r2.Add(dcs.control); break; case X360Controls.LeftMouse: keyvalue = 256; if (getBoolMapping(device, dcs.control, cState, eState, tp)) { state.currentClicks.leftCount++; } break; case X360Controls.RightMouse: keyvalue = 257; if (getBoolMapping(device, dcs.control, cState, eState, tp)) { state.currentClicks.rightCount++; } break; case X360Controls.MiddleMouse: keyvalue = 258; if (getBoolMapping(device, dcs.control, cState, eState, tp)) { state.currentClicks.middleCount++; } break; case X360Controls.FourthMouse: keyvalue = 259; if (getBoolMapping(device, dcs.control, cState, eState, tp)) { state.currentClicks.fourthCount++; } break; case X360Controls.FifthMouse: keyvalue = 260; if (getBoolMapping(device, dcs.control, cState, eState, tp)) { state.currentClicks.fifthCount++; } break; case X360Controls.WUP: if (getBoolMapping(device, dcs.control, cState, eState, tp)) { if (isAnalog) { getMouseWheelMapping(device, dcs.control, cState, eState, tp, false); } else { state.currentClicks.wUpCount++; } } break; case X360Controls.WDOWN: if (getBoolMapping(device, dcs.control, cState, eState, tp)) { if (isAnalog) { getMouseWheelMapping(device, dcs.control, cState, eState, tp, true); } else { state.currentClicks.wDownCount++; } } break; case X360Controls.MouseUp: if (mouseDeltaY == 0) { mouseDeltaY = getMouseMapping(device, dcs.control, cState, eState, 0); mouseDeltaY = -Math.Abs(mouseDeltaY == -2147483648 ? 0 : mouseDeltaY); } break; case X360Controls.MouseDown: if (mouseDeltaY == 0) { mouseDeltaY = getMouseMapping(device, dcs.control, cState, eState, 1); mouseDeltaY = Math.Abs(mouseDeltaY == -2147483648 ? 0 : mouseDeltaY); } break; case X360Controls.MouseLeft: if (mouseDeltaX == 0) { mouseDeltaX = getMouseMapping(device, dcs.control, cState, eState, 2); mouseDeltaX = -Math.Abs(mouseDeltaX == -2147483648 ? 0 : mouseDeltaX); } break; case X360Controls.MouseRight: if (mouseDeltaX == 0) { mouseDeltaX = getMouseMapping(device, dcs.control, cState, eState, 3); mouseDeltaX = Math.Abs(mouseDeltaX == -2147483648 ? 0 : mouseDeltaX); } break; } if (keyType.HasFlag(DS4KeyType.Toggle)) { if (getBoolMapping(device, dcs.control, cState, eState, tp)) { resetToDefaultValue(dcs.control, MappedState); if (!pressedonce[keyvalue]) { state.currentClicks.toggle = !state.currentClicks.toggle; pressedonce[keyvalue] = true; } state.currentClicks.toggleCount++; } else { pressedonce[keyvalue] = false; } } resetToDefaultValue(dcs.control, MappedState); // erase default mappings for things that are remapped break; } } if ((usingExtra != DS4Controls.None) && (usingExtra != dcs.control)) { continue; } bool shiftE = (dcs.shiftExtras != "0,0,0,0,0,0,0,0") && !string.IsNullOrEmpty(dcs.shiftExtras) && ShiftTrigger(dcs.shiftTrigger, device, cState, eState, tp); bool regE = (dcs.extras != "0,0,0,0,0,0,0,0") && !string.IsNullOrEmpty(dcs.extras); if ((regE || shiftE) && getBoolMapping(device, dcs.control, cState, eState, tp)) { usingExtra = dcs.control; string p = shiftE ? dcs.shiftExtras : dcs.extras; string[] extraS = p.Split(','); int[] extras = new int[extraS.Length]; for (int i = 0; i < extraS.Length; i++) { int b; if (int.TryParse(extraS[i], out b)) { extras[i] = b; } } held[device] = true; try { if (!((extras[0] == extras[1]) && (extras[1] == 0))) { ctrl.SetRumble((byte)extras[0], (byte)extras[1], device); } if (extras[2] == 1) { DS4Color color = new DS4Color { red = (byte)extras[3], green = (byte)extras[4], blue = (byte)extras[5] }; DS4LightBar.forcedColor[device] = color; DS4LightBar.forcedFlash[device] = (byte)extras[6]; DS4LightBar.forcelight[device] = true; } if (extras[7] == 1) { if (oldmouse[device] == -1) { oldmouse[device] = Global.ButtonMouseSensitivity[device]; } Global.ButtonMouseSensitivity[device] = extras[8]; } } catch { // ignored } } else if ((regE || shiftE) && held[device]) { DS4LightBar.forcelight[device] = false; DS4LightBar.forcedFlash[device] = 0; Global.ButtonMouseSensitivity[device] = oldmouse[device]; oldmouse[device] = -1; ctrl.SetRumble(0, 0, device); held[device] = false; usingExtra = DS4Controls.None; } } if (macroControl[00]) { MappedState.Cross = true; } if (macroControl[01]) { MappedState.Circle = true; } if (macroControl[02]) { MappedState.Square = true; } if (macroControl[03]) { MappedState.Triangle = true; } if (macroControl[04]) { MappedState.Options = true; } if (macroControl[05]) { MappedState.Share = true; } if (macroControl[06]) { MappedState.DpadUp = true; } if (macroControl[07]) { MappedState.DpadDown = true; } if (macroControl[08]) { MappedState.DpadLeft = true; } if (macroControl[09]) { MappedState.DpadRight = true; } if (macroControl[10]) { MappedState.PS = true; } if (macroControl[11]) { MappedState.L1 = true; } if (macroControl[12]) { MappedState.R1 = true; } if (macroControl[13]) { MappedState.L2 = 255; } if (macroControl[14]) { MappedState.R2 = 255; } if (macroControl[15]) { MappedState.L3 = true; } if (macroControl[16]) { MappedState.R3 = true; } if (macroControl[17]) { MappedState.LX = 255; } if (macroControl[18]) { MappedState.LX = 0; } if (macroControl[19]) { MappedState.LY = 255; } if (macroControl[20]) { MappedState.LY = 0; } if (macroControl[21]) { MappedState.RX = 255; } if (macroControl[22]) { MappedState.RX = 0; } if (macroControl[23]) { MappedState.RY = 255; } if (macroControl[24]) { MappedState.RY = 0; } foreach (DS4Controls dc in cross) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.Cross = true; } } foreach (DS4Controls dc in circle) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.Circle = true; } } foreach (DS4Controls dc in square) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.Square = true; } } foreach (DS4Controls dc in triangle) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.Triangle = true; } } foreach (DS4Controls dc in l1) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.L1 = true; } } foreach (DS4Controls dc in l2) { if (getByteMapping(device, dc, cState, eState, tp) > 5) { MappedState.L2 = getByteMapping(device, dc, cState, eState, tp); } } foreach (DS4Controls dc in l3) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.L3 = true; } } foreach (DS4Controls dc in r1) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.R1 = true; } } foreach (DS4Controls dc in r2) { if (getByteMapping(device, dc, cState, eState, tp) > 5) { MappedState.R2 = getByteMapping(device, dc, cState, eState, tp); } } foreach (DS4Controls dc in r3) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.R3 = true; } } foreach (DS4Controls dc in dPadUp) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.DpadUp = true; } } foreach (DS4Controls dc in dpadRight) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.DpadRight = true; } } foreach (DS4Controls dc in dPadLeft) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.DpadLeft = true; } } foreach (DS4Controls dc in dPadDown) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.DpadDown = true; } } foreach (DS4Controls dc in options) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.Options = true; } } foreach (DS4Controls dc in share) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.Share = true; } } foreach (DS4Controls dc in ps) { if (getBoolMapping(device, dc, cState, eState, tp)) { MappedState.PS = true; } } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.LXNeg.ToString()), device, cState, eState, tp), DS4Controls.LXNeg)) { lxn.Add(DS4Controls.LXNeg); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.LXPos.ToString()), device, cState, eState, tp), DS4Controls.LXPos)) { lxp.Add(DS4Controls.LXPos); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.LYNeg.ToString()), device, cState, eState, tp), DS4Controls.LYNeg)) { lyn.Add(DS4Controls.LYNeg); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.LYPos.ToString()), device, cState, eState, tp), DS4Controls.LYPos)) { lyp.Add(DS4Controls.LYPos); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.RXNeg.ToString()), device, cState, eState, tp), DS4Controls.RXNeg)) { rxn.Add(DS4Controls.RXNeg); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.RXPos.ToString()), device, cState, eState, tp), DS4Controls.RXPos)) { rxp.Add(DS4Controls.RXPos); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.RYNeg.ToString()), device, cState, eState, tp), DS4Controls.RYNeg)) { ryn.Add(DS4Controls.RYNeg); } if (IfAxisIsNotModified(device, ShiftTrigger(Global.GetDS4STrigger(device, DS4Controls.RYPos.ToString()), device, cState, eState, tp), DS4Controls.RYPos)) { ryp.Add(DS4Controls.RYPos); } if (Math.Abs(MappedState.LX - 127) < 10) { if ((lxn.Count > 0) || (lxp.Count > 0)) { foreach (DS4Controls dc in lxp) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp, true)) > 5) { MappedState.LX = getXYAxisMapping(device, dc, cState, eState, tp, true); } } foreach (DS4Controls dc in lxn) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp)) > 5) { MappedState.LX = getXYAxisMapping(device, dc, cState, eState, tp); } } } } //else //MappedState.LX = cState.LX; if (Math.Abs(MappedState.LY - 127) < 10) { if ((lyn.Count > 0) || (lyp.Count > 0)) { foreach (DS4Controls dc in lyn) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp)) > 5) { MappedState.LY = getXYAxisMapping(device, dc, cState, eState, tp); } } foreach (DS4Controls dc in lyp) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp, true)) > 5) { MappedState.LY = getXYAxisMapping(device, dc, cState, eState, tp, true); } } } } //else //MappedState.LY = cState.LY; if (Math.Abs(MappedState.RX - 127) < 10) { if ((rxn.Count > 0) || (rxp.Count > 0)) { foreach (DS4Controls dc in rxn) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp)) > 5) { MappedState.RX = getXYAxisMapping(device, dc, cState, eState, tp); } } foreach (DS4Controls dc in rxp) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp, true)) > 5) { MappedState.RX = getXYAxisMapping(device, dc, cState, eState, tp, true); } } } } //else // MappedState.RX = cState.RX; if (Math.Abs(MappedState.RY - 127) < 10) { if ((ryn.Count > 0) || (ryp.Count > 0)) { foreach (DS4Controls dc in ryn) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp)) > 5) { MappedState.RY = getXYAxisMapping(device, dc, cState, eState, tp); } } foreach (DS4Controls dc in ryp) { if (Math.Abs(127 - getXYAxisMapping(device, dc, cState, eState, tp, true)) > 5) { MappedState.RY = getXYAxisMapping(device, dc, cState, eState, tp, true); } } } } // else // MappedState.RY = cState.RY; InputMethods.MoveCursorBy(mouseDeltaX, mouseDeltaY); }
public static void Commit(int device) { SyntheticState state = deviceState[device]; lock (globalState) { globalState.currentClicks.leftCount += state.currentClicks.leftCount - state.previousClicks.leftCount; globalState.currentClicks.middleCount += state.currentClicks.middleCount - state.previousClicks.middleCount; globalState.currentClicks.rightCount += state.currentClicks.rightCount - state.previousClicks.rightCount; globalState.currentClicks.fourthCount += state.currentClicks.fourthCount - state.previousClicks.fourthCount; globalState.currentClicks.fifthCount += state.currentClicks.fifthCount - state.previousClicks.fifthCount; globalState.currentClicks.wUpCount += state.currentClicks.wUpCount - state.previousClicks.wUpCount; globalState.currentClicks.wDownCount += state.currentClicks.wDownCount - state.previousClicks.wDownCount; globalState.currentClicks.toggleCount += state.currentClicks.toggleCount - state.previousClicks.toggleCount; globalState.currentClicks.toggle = state.currentClicks.toggle; if ((globalState.currentClicks.toggleCount != 0) && (globalState.previousClicks.toggleCount == 0) && globalState.currentClicks.toggle) { if ((globalState.currentClicks.leftCount != 0) && (globalState.previousClicks.leftCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_LEFTDOWN); } if ((globalState.currentClicks.rightCount != 0) && (globalState.previousClicks.rightCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_RIGHTDOWN); } if ((globalState.currentClicks.middleCount != 0) && (globalState.previousClicks.middleCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_MIDDLEDOWN); } if ((globalState.currentClicks.fourthCount != 0) && (globalState.previousClicks.fourthCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONDOWN, 1); } if ((globalState.currentClicks.fifthCount != 0) && (globalState.previousClicks.fifthCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONDOWN, 2); } } else if ((globalState.currentClicks.toggleCount != 0) && (globalState.previousClicks.toggleCount == 0) && !globalState.currentClicks.toggle) { if ((globalState.currentClicks.leftCount != 0) && (globalState.previousClicks.leftCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_LEFTUP); } if ((globalState.currentClicks.rightCount != 0) && (globalState.previousClicks.rightCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_RIGHTUP); } if ((globalState.currentClicks.middleCount != 0) && (globalState.previousClicks.middleCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_MIDDLEUP); } if ((globalState.currentClicks.fourthCount != 0) && (globalState.previousClicks.fourthCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONUP, 1); } if ((globalState.currentClicks.fifthCount != 0) && (globalState.previousClicks.fifthCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONUP, 2); } } if ((globalState.currentClicks.toggleCount == 0) && (globalState.previousClicks.toggleCount == 0)) { if ((globalState.currentClicks.leftCount != 0) && (globalState.previousClicks.leftCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_LEFTDOWN); } else if ((globalState.currentClicks.leftCount == 0) && (globalState.previousClicks.leftCount != 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_LEFTUP); } if ((globalState.currentClicks.middleCount != 0) && (globalState.previousClicks.middleCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_MIDDLEDOWN); } else if ((globalState.currentClicks.middleCount == 0) && (globalState.previousClicks.middleCount != 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_MIDDLEUP); } if ((globalState.currentClicks.rightCount != 0) && (globalState.previousClicks.rightCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_RIGHTDOWN); } else if ((globalState.currentClicks.rightCount == 0) && (globalState.previousClicks.rightCount != 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_RIGHTUP); } if ((globalState.currentClicks.fourthCount != 0) && (globalState.previousClicks.fourthCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONDOWN, 1); } else if ((globalState.currentClicks.fourthCount == 0) && (globalState.previousClicks.fourthCount != 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONUP, 1); } if ((globalState.currentClicks.fifthCount != 0) && (globalState.previousClicks.fifthCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONDOWN, 2); } else if ((globalState.currentClicks.fifthCount == 0) && (globalState.previousClicks.fifthCount != 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_XBUTTONUP, 2); } if ((globalState.currentClicks.wUpCount != 0) && (globalState.previousClicks.wUpCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_WHEEL, 100); oldnow = DateTime.UtcNow; wheel = 100; } else if ((globalState.currentClicks.wUpCount == 0) && (globalState.previousClicks.wUpCount != 0)) { wheel = 0; } if ((globalState.currentClicks.wDownCount != 0) && (globalState.previousClicks.wDownCount == 0)) { InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_WHEEL, -100); oldnow = DateTime.UtcNow; wheel = -100; } if ((globalState.currentClicks.wDownCount == 0) && (globalState.previousClicks.wDownCount != 0)) { wheel = 0; } } if (wheel != 0) //Continue mouse wheel movement { DateTime now = DateTime.UtcNow; if ((now >= oldnow + TimeSpan.FromMilliseconds(100)) && !pressagain) { oldnow = now; InputMethods.MouseEvent(InputMethods.MOUSEEVENTF_WHEEL, wheel); } } // Merge and synthesize all key presses/releases that are present in this device's mapping. // TODO what about the rest? e.g. repeat keys really ought to be on some set schedule foreach (KeyValuePair<ushort, SyntheticState.KeyPresses> kvp in state.keyPresses) { SyntheticState.KeyPresses gkp; if (globalState.keyPresses.TryGetValue(kvp.Key, out gkp)) { gkp.current.vkCount += kvp.Value.current.vkCount - kvp.Value.previous.vkCount; gkp.current.scanCodeCount += kvp.Value.current.scanCodeCount - kvp.Value.previous.scanCodeCount; gkp.current.repeatCount += kvp.Value.current.repeatCount - kvp.Value.previous.repeatCount; gkp.current.toggle = kvp.Value.current.toggle; gkp.current.toggleCount += kvp.Value.current.toggleCount - kvp.Value.previous.toggleCount; } else { gkp = new SyntheticState.KeyPresses { current = kvp.Value.current }; globalState.keyPresses[kvp.Key] = gkp; } if ((gkp.current.toggleCount != 0) && (gkp.previous.toggleCount == 0) && gkp.current.toggle) { if (gkp.current.scanCodeCount != 0) { InputMethods.PerformScKeyPress(kvp.Key); } else { InputMethods.PerformKeyPress(kvp.Key); } } else if ((gkp.current.toggleCount != 0) && (gkp.previous.toggleCount == 0) && !gkp.current.toggle) { if (gkp.previous.scanCodeCount != 0) // use the last type of VK/SC { InputMethods.PerformScKeyRelease(kvp.Key); } else { InputMethods.PerformKeyRelease(kvp.Key); } } else if ((gkp.current.vkCount + gkp.current.scanCodeCount != 0) && (gkp.previous.vkCount + gkp.previous.scanCodeCount == 0)) { if (gkp.current.scanCodeCount != 0) { oldnow = DateTime.UtcNow; InputMethods.PerformScKeyPress(kvp.Key); pressagain = false; keyshelddown = kvp.Key; } else { oldnow = DateTime.UtcNow; InputMethods.PerformKeyPress(kvp.Key); pressagain = false; keyshelddown = kvp.Key; } } else if ((gkp.current.toggleCount != 0) || (gkp.previous.toggleCount != 0) || (gkp.current.repeatCount != 0) || // repeat or SC/VK transition (gkp.previous.scanCodeCount == 0 != (gkp.current.scanCodeCount == 0))) //repeat keystroke after 500ms { if (keyshelddown == kvp.Key) { DateTime now = DateTime.UtcNow; if ((now >= oldnow + TimeSpan.FromMilliseconds(500)) && !pressagain) { oldnow = now; pressagain = true; } if (pressagain && (gkp.current.scanCodeCount != 0)) { now = DateTime.UtcNow; if ((now >= oldnow + TimeSpan.FromMilliseconds(25)) && pressagain) { oldnow = now; InputMethods.PerformScKeyPress(kvp.Key); } } else if (pressagain) { now = DateTime.UtcNow; if ((now >= oldnow + TimeSpan.FromMilliseconds(25)) && pressagain) { oldnow = now; InputMethods.PerformKeyPress(kvp.Key); } } } } if ((gkp.current.toggleCount != 0) || (gkp.previous.toggleCount != 0) || (gkp.current.vkCount + gkp.current.scanCodeCount != 0) || (gkp.previous.vkCount + gkp.previous.scanCodeCount == 0)) { continue; } if (gkp.previous.scanCodeCount != 0) // use the last type of VK/SC { InputMethods.PerformScKeyRelease(kvp.Key); pressagain = false; } else { InputMethods.PerformKeyRelease(kvp.Key); pressagain = false; } } globalState.SavePrevious(false); } state.SavePrevious(true); }
public static async void MapCustomAction(int device, DS4State cState, DS4State MappedState, DS4StateExposed eState, Mouse tp, ControlService ctrl) { try { foreach (string actionname in Global.ProfileActions[device]) { //DS4KeyType keyType = getShiftCustomKeyType(device, customKey.Key); SpecialAction action = Global.GetAction(actionname); int index = Global.GetActionIndexOf(actionname); //If a key or button is assigned to the trigger, a key special action is used like //a quick tap to use and hold to use the regular custom button/key bool triggerToBeTapped = (action.type == "Key") && (action.trigger.Count == 1) && (Global.GetDS4Action(device, action.trigger[0].ToString(), false) == null); if ((action.name == "null") || (index < 0)) { continue; } bool triggeractivated = true; if (action.delayTime > 0) { triggeractivated = false; bool subtriggeractivated = action.trigger.All(dc => getBoolMapping(device, dc, cState, eState, tp)); if (subtriggeractivated) { double time = action.delayTime; nowAction[device] = DateTime.UtcNow; if (nowAction[device] >= oldnowAction[device] + TimeSpan.FromSeconds(time)) { triggeractivated = true; } } else if (nowAction[device] < DateTime.UtcNow - TimeSpan.FromMilliseconds(100)) { oldnowAction[device] = DateTime.UtcNow; } } else if (triggerToBeTapped && (oldnowKeyAct[device] == DateTime.MinValue)) { triggeractivated = false; bool subtriggeractivated = action.trigger.All(dc => getBoolMapping(device, dc, cState, eState, tp)); if (subtriggeractivated) { oldnowKeyAct[device] = DateTime.UtcNow; } } else if (triggerToBeTapped && (oldnowKeyAct[device] != DateTime.MinValue)) { triggeractivated = false; bool subtriggeractivated = action.trigger.All(dc => getBoolMapping(device, dc, cState, eState, tp)); DateTime now = DateTime.UtcNow; if (!subtriggeractivated && (now <= oldnowKeyAct[device] + TimeSpan.FromMilliseconds(250))) { await Task.Delay(3); //if the button is assigned to the same key use a delay so the key down is the last action, not key up triggeractivated = true; oldnowKeyAct[device] = DateTime.MinValue; } else if (!subtriggeractivated) { oldnowKeyAct[device] = DateTime.MinValue; } } else if (action.trigger.Any(dc => !getBoolMapping(device, dc, cState, eState, tp))) { triggeractivated = false; } bool utriggeractivated = true; if ((action.type == "Key") && (action.uTrigger.Count > 0)) { if (action.uTrigger.Any(dc => !getBoolMapping(device, dc, cState, eState, tp))) { utriggeractivated = false; } if (action.pressRelease) { utriggeractivated = !utriggeractivated; } } if (triggeractivated && (action.type == "Program")) { if (actionDone[device, index]) { continue; } actionDone[device, index] = true; if (!string.IsNullOrEmpty(action.extra)) { Process.Start(action.details, action.extra); } else { Process.Start(action.details); } } else if (triggeractivated && (action.type == "Profile")) { if (actionDone[device, index] || !string.IsNullOrEmpty(Global.tempprofilename[device])) { continue; } actionDone[device, index] = true; untriggeraction[device] = action; untriggerindex[device] = index; foreach (DS4Controls dc in action.trigger) { DS4ControlSettings dcs = Global.getDS4CSetting(device, dc.ToString()); if (dcs.action == null) { continue; } if (dcs.actionType == DS4ControlSettings.ActionType.Key) { InputMethods.PerformKeyRelease(ushort.Parse(action.ToString())); } else if (dcs.actionType == DS4ControlSettings.ActionType.Macro) { int[] keys = (int[])dcs.action; foreach (int key in keys) { InputMethods.PerformKeyRelease((ushort)key); } } } Global.LoadTempProfile(device, action.details, true, ctrl); return; } else if (triggeractivated && (action.type == "Macro")) { if (!actionDone[device, index]) { DS4KeyType keyType = action.keyType; actionDone[device, index] = true; foreach (DS4Controls dc in action.trigger) { resetToDefaultValue(dc, MappedState); } PlayMacro(device, macroControl, string.Join("/", action.macro), DS4Controls.None, keyType); } else { EndMacro(device, macroControl, string.Join("/", action.macro), DS4Controls.None); } } else if (triggeractivated && (action.type == "Key")) { if ((action.uTrigger.Count != 0) && ((action.uTrigger.Count <= 0) || (untriggerindex[device] != -1) || actionDone[device, index])) { continue; } actionDone[device, index] = true; untriggerindex[device] = index; ushort key; ushort.TryParse(action.details, out key); if (action.uTrigger.Count == 0) { SyntheticState.KeyPresses kp; if (!deviceState[device].keyPresses.TryGetValue(key, out kp)) { deviceState[device].keyPresses[key] = kp = new SyntheticState.KeyPresses(); } if (action.keyType.HasFlag(DS4KeyType.ScanCode)) { kp.current.scanCodeCount++; } else { kp.current.vkCount++; } kp.current.repeatCount++; } else if (action.keyType.HasFlag(DS4KeyType.ScanCode)) { InputMethods.PerformScKeyPress(key); } else { InputMethods.PerformKeyPress(key); } } else if ((action.uTrigger.Count > 0) && utriggeractivated && (action.type == "Key")) { if ((untriggerindex[device] <= -1) || actionDone[device, index]) { continue; } actionDone[device, index] = true; untriggerindex[device] = -1; ushort key; ushort.TryParse(action.details, out key); if (action.keyType.HasFlag(DS4KeyType.ScanCode)) { InputMethods.PerformScKeyRelease(key); } else { InputMethods.PerformKeyRelease(key); } } else if (triggeractivated && (action.type == "DisconnectBT")) { DS4Device d = ctrl.DS4Controllers[device]; if (d.Charging) { continue; } d.DisconnectBluetooth(); foreach (DS4Controls dc in action.trigger) { DS4ControlSettings dcs = Global.getDS4CSetting(device, dc.ToString()); if (dcs.action != null) { if (dcs.actionType == DS4ControlSettings.ActionType.Key) { InputMethods.PerformKeyRelease((ushort)dcs.action); } else if (dcs.actionType == DS4ControlSettings.ActionType.Macro) { int[] keys = (int[])dcs.action; foreach (int key in keys) { InputMethods.PerformKeyRelease((ushort)key); } } } } return; } else if (triggeractivated && (action.type == "BatteryCheck")) { string[] dets = action.details.Split(','); if (bool.Parse(dets[1]) && !actionDone[device, index]) { Log.LogToTray("Controller " + (device + 1) + ": " + ctrl.GetDS4Battery(device), LogWarning.Yes); } if (bool.Parse(dets[2])) { DS4Device d = ctrl.DS4Controllers[device]; if (!actionDone[device, index]) { lastColor[device] = d.LightBarColor; DS4LightBar.forcelight[device] = true; } DS4Color empty = new DS4Color(byte.Parse(dets[3]), byte.Parse(dets[4]), byte.Parse(dets[5])); DS4Color full = new DS4Color(byte.Parse(dets[6]), byte.Parse(dets[7]), byte.Parse(dets[8])); DS4Color trans = Global.getTransitionedColor(empty, full, d.Battery); if (fadetimer[device] < 100) { DS4LightBar.forcedColor[device] = Global.getTransitionedColor(lastColor[device], trans, fadetimer[device] += 2); } } actionDone[device, index] = true; } else if (!triggeractivated && (action.type == "BatteryCheck")) { if (actionDone[device, index]) { fadetimer[device] = 0; DS4LightBar.forcelight[device] = false; actionDone[device, index] = false; } } else if (action.type == "XboxGameDVR") { /*if (getCustomButton(device, action.trigger[0]) != X360Controls.Unbound) getCustomButtons(device)[action.trigger[0]] = X360Controls.Unbound; if (getCustomMacro(device, action.trigger[0]) != "0") getCustomMacros(device).Remove(action.trigger[0]); if (getCustomKey(device, action.trigger[0]) != 0) getCustomMacros(device).Remove(action.trigger[0]);*/ string[] dets = action.details.Split(','); DS4Device d = ctrl.DS4Controllers[device]; //cus if (getBoolMapping(device, action.trigger[0], cState, eState, tp) && !getBoolMapping(device, action.trigger[0], d.GetPreviousState(), eState, tp)) { //pressed down pastTime = DateTime.UtcNow; if (DateTime.UtcNow <= firstTap + TimeSpan.FromMilliseconds(150)) { tappedOnce = false; secondtouchbegin = true; } else { firstTouch = true; } } else if (!getBoolMapping(device, action.trigger[0], cState, eState, tp) && getBoolMapping(device, action.trigger[0], d.GetPreviousState(), eState, tp)) { //released if (secondtouchbegin) { firstTouch = false; secondtouchbegin = false; } else if (firstTouch) { firstTouch = false; if ((DateTime.UtcNow <= pastTime + TimeSpan.FromMilliseconds(200)) && !tappedOnce) { tappedOnce = true; firstTap = DateTime.UtcNow; TimeofEnd = DateTime.UtcNow; } } } int type; string macro = string.Empty; if (tappedOnce) //single tap { if (int.TryParse(dets[0], out type)) { switch (type) { case 0: macro = "91/71/71/91"; break; case 1: macro = "91/164/82/82/164/91"; break; case 2: macro = "91/164/44/44/164/91"; break; case 3: macro = dets[3] + "/" + dets[3]; break; case 4: macro = "91/164/71/71/164/91"; break; } } if (DateTime.UtcNow - TimeofEnd > TimeSpan.FromMilliseconds(150)) { PlayMacro(device, macroControl, macro, DS4Controls.None, DS4KeyType.None); tappedOnce = false; } //if it fails the method resets, and tries again with a new tester value (gives tap a delay so tap and hold can work) } else if (firstTouch && (DateTime.UtcNow - pastTime > TimeSpan.FromMilliseconds(1000))) //helddown { if (int.TryParse(dets[1], out type)) { switch (type) { case 0: macro = "91/71/71/91"; break; case 1: macro = "91/164/82/82/164/91"; break; case 2: macro = "91/164/44/44/164/91"; break; case 3: macro = dets[3] + "/" + dets[3]; break; case 4: macro = "91/164/71/71/164/91"; break; } } PlayMacro(device, macroControl, macro, DS4Controls.None, DS4KeyType.None); firstTouch = false; } else if (secondtouchbegin) //if double tap { if (int.TryParse(dets[2], out type)) { switch (type) { case 0: macro = "91/71/71/91"; break; case 1: macro = "91/164/82/82/164/91"; break; case 2: macro = "91/164/44/44/164/91"; break; case 3: macro = dets[3] + "/" + dets[3]; break; case 4: macro = "91/164/71/71/164/91"; break; } } PlayMacro(device, macroControl, macro, DS4Controls.None, DS4KeyType.None); secondtouchbegin = false; } } else { actionDone[device, index] = false; } } } catch { return; } if (untriggeraction[device] != null) { SpecialAction action = untriggeraction[device]; int index = untriggerindex[device]; if (action.uTrigger.All(dc => getBoolMapping(device, dc, cState, eState, tp)) && (action.type == "Profile")) { if (((action.controls != action.ucontrols) || actionDone[device, index]) && (action.controls == action.ucontrols)) { return; } if (string.IsNullOrEmpty(Global.tempprofilename[device])) { return; } foreach (DS4Controls dc in action.uTrigger) { actionDone[device, index] = true; DS4ControlSettings dcs = Global.getDS4CSetting(device, dc.ToString()); if (dcs.action == null) { continue; } if (dcs.actionType == DS4ControlSettings.ActionType.Key) { InputMethods.PerformKeyRelease((ushort)dcs.action); } else if (dcs.actionType == DS4ControlSettings.ActionType.Macro) { int[] keys = (int[])dcs.action; foreach (int key in keys) { InputMethods.PerformKeyRelease((ushort)key); } } } untriggeraction[device] = null; Global.LoadProfile(device, false, ctrl); } else { actionDone[device, index] = false; } } }