Example #1
0
		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);
		}
Example #2
0
		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;
				}
			}
		}
Example #3
0
		public static void LoadTempProfile(int device, string name, bool launchprogram, ControlService control)
		{
			m_Config.LoadProfile(device, launchprogram, control, appdatapath + @"\Profiles\" + name + ".xml");
			tempprofilename[device] = name;
		}
Example #4
0
		public static void LoadProfile(int device, bool launchprogram, ControlService control)
		{
			m_Config.LoadProfile(device, launchprogram, control);
			tempprofilename[device] = string.Empty;
		}
Example #5
0
		public bool LoadProfile(int device, bool launchprogram, ControlService control, string propath = null)
		{
			string rootname = "DS4Windows";
			bool missingSetting = false;
			string profilepath = string.IsNullOrEmpty(propath) ? Global.appdatapath + @"\Profiles\" + profilePath[device] + ".xml" : propath;
			if (File.Exists(profilepath))
			{
				XmlNode Item;

				m_Xdoc.Load(profilepath);
				if (m_Xdoc.SelectSingleNode(rootname) == null)
				{
					rootname = "ScpControl";
					missingSetting = true;
				}
				if (device < 4)
				{
					DS4LightBar.forcelight[device] = false;
					DS4LightBar.forcedFlash[device] = 0;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/flushHIDQueue");
					bool.TryParse(Item.InnerText, out flushHIDQueue[device]);
				}
				catch
				{
					missingSetting = true;
				} //rootname = }

				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/idleDisconnectTimeout");
					int.TryParse(Item.InnerText, out idleDisconnectTimeout[device]);
				}
				catch
				{
					missingSetting = true;
				}
				//New method for saving color
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/Color");
					string[] colors = !string.IsNullOrEmpty(Item.InnerText) ? Item.InnerText.Split(',') : new string[0];
					m_Leds[device].red = byte.Parse(colors[0]);
					m_Leds[device].green = byte.Parse(colors[1]);
					m_Leds[device].blue = byte.Parse(colors[2]);
				}
				catch
				{
					missingSetting = true;
				}
				if (m_Xdoc.SelectSingleNode("/" + rootname + "/Color") == null)
				{
					//Old method of color saving
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/Red");
						byte.TryParse(Item.InnerText, out m_Leds[device].red);
					}
					catch
					{
						missingSetting = true;
					}
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/Green");
						byte.TryParse(Item.InnerText, out m_Leds[device].green);
					}
					catch
					{
						missingSetting = true;
					}

					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/Blue");
						byte.TryParse(Item.InnerText, out m_Leds[device].blue);
					}
					catch
					{
						missingSetting = true;
					}
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/RumbleBoost");
					byte.TryParse(Item.InnerText, out rumble[device]);
				}
				catch
				{
					missingSetting = true;
				}

				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ledAsBatteryIndicator");
					bool.TryParse(Item.InnerText, out ledAsBattery[device]);
				}
				catch
				{
					missingSetting = true;
				}

				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/FlashType");
					byte.TryParse(Item.InnerText, out flashType[device]);
				}
				catch
				{
					missingSetting = true;
				}

				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/flashBatteryAt");
					int.TryParse(Item.InnerText, out flashAt[device]);
				}
				catch
				{
					missingSetting = true;
				}

				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/touchSensitivity");
					byte.TryParse(Item.InnerText, out touchSensitivity[device]);
				}
				catch
				{
					missingSetting = true;
				}
				//New method for saving color
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LowColor");
					string[] colors = !string.IsNullOrEmpty(Item.InnerText) ? Item.InnerText.Split(',') : new string[0];
					m_LowLeds[device].red = byte.Parse(colors[0]);
					m_LowLeds[device].green = byte.Parse(colors[1]);
					m_LowLeds[device].blue = byte.Parse(colors[2]);
				}
				catch
				{
					missingSetting = true;
				}
				if (m_Xdoc.SelectSingleNode("/" + rootname + "/LowColor") == null)
				{
					//Old method of color saving
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LowRed");
						byte.TryParse(Item.InnerText, out m_LowLeds[device].red);
					}
					catch
					{
						missingSetting = true;
					}
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LowGreen");
						byte.TryParse(Item.InnerText, out m_LowLeds[device].green);
					}
					catch
					{
						missingSetting = true;
					}
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LowBlue");
						byte.TryParse(Item.InnerText, out m_LowLeds[device].blue);
					}
					catch
					{
						missingSetting = true;
					}
				}
				//New method for saving color
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ChargingColor");
					string[] colors = !string.IsNullOrEmpty(Item.InnerText) ? Item.InnerText.Split(',') : new string[0];

					m_ChargingLeds[device].red = byte.Parse(colors[0]);
					m_ChargingLeds[device].green = byte.Parse(colors[1]);
					m_ChargingLeds[device].blue = byte.Parse(colors[2]);
				}
				catch
				{
					missingSetting = true;
				}
				if (m_Xdoc.SelectSingleNode("/" + rootname + "/ChargingColor") == null)
				{
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ChargingRed");
						byte.TryParse(Item.InnerText, out m_ChargingLeds[device].red);
					}
					catch
					{
						missingSetting = true;
					}
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ChargingGreen");
						byte.TryParse(Item.InnerText, out m_ChargingLeds[device].green);
					}
					catch
					{
						missingSetting = true;
					}
					try
					{
						Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ChargingBlue");
						byte.TryParse(Item.InnerText, out m_ChargingLeds[device].blue);
					}
					catch
					{
						missingSetting = true;
					}
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/FlashColor");
					string[] colors = !string.IsNullOrEmpty(Item.InnerText) ? Item.InnerText.Split(',') : new string[0];
					m_FlashLeds[device].red = byte.Parse(colors[0]);
					m_FlashLeds[device].green = byte.Parse(colors[1]);
					m_FlashLeds[device].blue = byte.Parse(colors[2]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/touchpadJitterCompensation");
					bool.TryParse(Item.InnerText, out touchpadJitterCompensation[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/lowerRCOn");
					bool.TryParse(Item.InnerText, out lowerRCOn[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/tapSensitivity");
					byte.TryParse(Item.InnerText, out tapSensitivity[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/doubleTap");
					bool.TryParse(Item.InnerText, out doubleTap[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/scrollSensitivity");
					int.TryParse(Item.InnerText, out scrollSensitivity[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LeftTriggerMiddle");
					byte.TryParse(Item.InnerText, out l2Deadzone[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/RightTriggerMiddle");
					byte.TryParse(Item.InnerText, out r2Deadzone[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ButtonMouseSensitivity");
					int.TryParse(Item.InnerText, out buttonMouseSensitivity[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/Rainbow");
					double.TryParse(Item.InnerText, out rainbow[device]);
				}
				catch
				{
					rainbow[device] = 0;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LSDeadZone");
					int.TryParse(Item.InnerText, out LSDeadzone[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/RSDeadZone");
					int.TryParse(Item.InnerText, out RSDeadzone[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/SXDeadZone");
					double.TryParse(Item.InnerText, out SXDeadzone[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/SZDeadZone");
					double.TryParse(Item.InnerText, out SZDeadzone[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/Sensitivity");
					string[] s = Item.InnerText.Split(',');
					double.TryParse(s[0], out LSSens[device]);
					double.TryParse(s[1], out RSSens[device]);
					double.TryParse(s[2], out l2Sens[device]);
					double.TryParse(s[3], out r2Sens[device]);
					double.TryParse(s[4], out SXSens[device]);
					double.TryParse(s[5], out SZSens[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ChargingType");
					int.TryParse(Item.InnerText, out chargingType[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/MouseAcceleration");
					bool.TryParse(Item.InnerText, out mouseAccel[device]);
				}
				catch
				{
					missingSetting = true;
				}
				int shiftM = 0;
				if (m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftModifier") != null)
				{
					int.TryParse(m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftModifier").InnerText, out shiftM);
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LaunchProgram");
					launchProgram[device] = Item.InnerText;
					if (launchprogram && (launchProgram[device] != string.Empty))
					{
						Process.Start(launchProgram[device]);
					}
				}
				catch
				{
					launchProgram[device] = string.Empty;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/DinputOnly");
					bool.TryParse(Item.InnerText, out dinputOnly[device]);
					if (device < 4)
					{
						if (dinputOnly[device])
						{
							control.x360Bus.Unplug(device);
						}
						else if ((control.DS4Controllers[device] != null) && control.DS4Controllers[device].IsAlive())
						{
							control.x360Bus.Plugin(device);
						}
					}
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/StartTouchpadOff");
					bool.TryParse(Item.InnerText, out startTouchpadOff[device]);
					if (startTouchpadOff[device])
					{
						control.StartTPOff(device);
					}
				}
				catch
				{
					startTouchpadOff[device] = false;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/UseTPforControls");
					bool.TryParse(Item.InnerText, out useTPforControls[device]);
				}
				catch
				{
					useTPforControls[device] = false;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/UseSAforMouse");
					bool.TryParse(Item.InnerText, out useSAforMouse[device]);
				}
				catch
				{
					useSAforMouse[device] = false;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/SATriggers");
					sATriggers[device] = Item.InnerText;
				}
				catch
				{
					sATriggers[device] = string.Empty;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/GyroSensitivity");
					int.TryParse(Item.InnerText, out gyroSensitivity[device]);
				}
				catch
				{
					gyroSensitivity[device] = 100;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/GyroInvert");
					int.TryParse(Item.InnerText, out gyroInvert[device]);
				}
				catch
				{
					gyroInvert[device] = 0;
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/LSCurve");
					int.TryParse(Item.InnerText, out lsCurve[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/RSCurve");
					int.TryParse(Item.InnerText, out rsCurve[device]);
				}
				catch
				{
					missingSetting = true;
				}
				try
				{
					Item = m_Xdoc.SelectSingleNode("/" + rootname + "/ProfileActions");
					profileActions[device].Clear();
					if (!string.IsNullOrEmpty(Item.InnerText))
					{
						profileActions[device].AddRange(Item.InnerText.Split('/'));
					}
				}
				catch
				{
					profileActions[device].Clear();
					missingSetting = true;
				}

				foreach (DS4ControlSettings dcs in ds4settings[device])
				{
					dcs.Reset();
				}

				DS4KeyType keyType;
				ushort wvk;

				XmlNode ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/Control/Button");
				if (ParentItem != null)
				{
					foreach (XmlNode item in ParentItem.ChildNodes)
					{
						UpdateDS4CSetting(device, item.Name, false, getX360ControlsByName(item.InnerText), string.Empty, DS4KeyType.None);
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/Control/Macro");
				if (ParentItem != null)
				{
					foreach (XmlNode item in ParentItem.ChildNodes)
					{
						string[] skeys;
						int[] keys;
						if (!string.IsNullOrEmpty(item.InnerText))
						{
							skeys = item.InnerText.Split('/');
							keys = new int[skeys.Length];
						}
						else
						{
							skeys = new string[0];
							keys = new int[0];
						}
						for (int i = 0; i < keys.Length; i++)
						{
							keys[i] = int.Parse(skeys[i]);
						}
						UpdateDS4CSetting(device, item.Name, false, keys, string.Empty, DS4KeyType.None);
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/Control/Key");
				if (ParentItem != null)
				{
					foreach (XmlNode item in ParentItem.ChildNodes)
					{
						if (ushort.TryParse(item.InnerText, out wvk))
						{
							UpdateDS4CSetting(device, item.Name, false, wvk, string.Empty, DS4KeyType.None);
						}
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/Control/Extras");
				if (ParentItem != null)
				{
					foreach (XmlNode item in ParentItem.ChildNodes)
					{
						if (item.InnerText != string.Empty)
						{
							UpdateDS4CExtra(device, item.Name, false, item.InnerText);
						}
						else
						{
							ParentItem.RemoveChild(item);
						}
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/Control/KeyType");
				if (ParentItem != null)
				{
					foreach (XmlNode item in ParentItem.ChildNodes)
					{
						if (item == null)
						{
							continue;
						}
						keyType = DS4KeyType.None;
						if (item.InnerText.Contains(DS4KeyType.ScanCode.ToString()))
						{
							keyType |= DS4KeyType.ScanCode;
						}
						if (item.InnerText.Contains(DS4KeyType.Toggle.ToString()))
						{
							keyType |= DS4KeyType.Toggle;
						}
						if (item.InnerText.Contains(DS4KeyType.Macro.ToString()))
						{
							keyType |= DS4KeyType.Macro;
						}
						if (item.InnerText.Contains(DS4KeyType.HoldMacro.ToString()))
						{
							keyType |= DS4KeyType.HoldMacro;
						}
						if (item.InnerText.Contains(DS4KeyType.Unbound.ToString()))
						{
							keyType |= DS4KeyType.Unbound;
						}
						if (keyType != DS4KeyType.None)
						{
							UpdateDS4CKeyType(device, item.Name, false, keyType);
						}
					}
				}

				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftControl/Button");
				if (ParentItem != null)
				{
					foreach (XmlElement item in ParentItem.ChildNodes)
					{
						int shiftT = shiftM;
						if (item.HasAttribute("Trigger"))
						{
							int.TryParse(item.Attributes["Trigger"].Value, out shiftT);
						}
						UpdateDS4CSetting(device, item.Name, true, getX360ControlsByName(item.InnerText), string.Empty, DS4KeyType.None, shiftT);
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftControl/Macro");
				if (ParentItem != null)
				{
					foreach (XmlElement item in ParentItem.ChildNodes)
					{
						string[] skeys;
						int[] keys;
						if (!string.IsNullOrEmpty(item.InnerText))
						{
							skeys = item.InnerText.Split('/');
							keys = new int[skeys.Length];
						}
						else
						{
							skeys = new string[0];
							keys = new int[0];
						}
						for (int i = 0; i < keys.Length; i++)
						{
							keys[i] = int.Parse(skeys[i]);
						}
						int shiftT = shiftM;
						if (item.HasAttribute("Trigger"))
						{
							int.TryParse(item.Attributes["Trigger"].Value, out shiftT);
						}
						UpdateDS4CSetting(device, item.Name, true, keys, string.Empty, DS4KeyType.None, shiftT);
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftControl/Key");
				if (ParentItem != null)
				{
					foreach (XmlElement item in ParentItem.ChildNodes)
					{
						if (ushort.TryParse(item.InnerText, out wvk))
						{
							int shiftT = shiftM;
							if (item.HasAttribute("Trigger"))
							{
								int.TryParse(item.Attributes["Trigger"].Value, out shiftT);
							}
							UpdateDS4CSetting(device, item.Name, true, wvk, string.Empty, DS4KeyType.None, shiftT);
						}
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftControl/Extras");
				if (ParentItem != null)
				{
					foreach (XmlElement item in ParentItem.ChildNodes)
					{
						if (item.InnerText != string.Empty)
						{
							UpdateDS4CExtra(device, item.Name, true, item.InnerText);
						}
						else
						{
							ParentItem.RemoveChild(item);
						}
					}
				}
				ParentItem = m_Xdoc.SelectSingleNode("/" + rootname + "/ShiftControl/KeyType");
				if (ParentItem != null)
				{
					foreach (XmlElement item in ParentItem.ChildNodes)
					{
						if (item != null)
						{
							keyType = DS4KeyType.None;
							if (item.InnerText.Contains(DS4KeyType.ScanCode.ToString()))
							{
								keyType |= DS4KeyType.ScanCode;
							}
							if (item.InnerText.Contains(DS4KeyType.Toggle.ToString()))
							{
								keyType |= DS4KeyType.Toggle;
							}
							if (item.InnerText.Contains(DS4KeyType.Macro.ToString()))
							{
								keyType |= DS4KeyType.Macro;
							}
							if (item.InnerText.Contains(DS4KeyType.HoldMacro.ToString()))
							{
								keyType |= DS4KeyType.HoldMacro;
							}
							if (item.InnerText.Contains(DS4KeyType.Unbound.ToString()))
							{
								keyType |= DS4KeyType.Unbound;
							}
							if (keyType != DS4KeyType.None)
							{
								UpdateDS4CKeyType(device, item.Name, true, keyType);
							}
						}
					}
				}
			}

			// Only add missing settings if the actual load was graceful
			if (missingSetting)
			{
				SaveProfile(device, profilepath);
			}

			return true;
		}
Example #6
0
        private static void Main(string[] args)
        {
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("he");
            if (args.Any(s => (s == "driverinstall") || (s == "-driverinstall")))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new WelcomeDialog());
                return;
            }
            GCSettings.LatencyMode = GCLatencyMode.LowLatency;
            try
            {
                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            }
            catch
            {
                // Ignore problems raising the priority.
            }
            try
            {
                // another instance is already running if OpenExsting succeeds.
                threadComEvent = EventWaitHandle.OpenExisting(singleAppComEventName);
                threadComEvent.Set(); // signal the other instance.
                threadComEvent.Close();
                return; // return immediatly.
            }
            catch
            {
                /* don't care about errors */
            }
            // Create the Event handle
            threadComEvent = new EventWaitHandle(false, EventResetMode.AutoReset, singleAppComEventName);
            CreateInterAppComThread();
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                RootHub = new ControlService();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new DS4Form(args));
                mutex.ReleaseMutex();
            }

            // End the communication thread.
            singleAppComThread.CancelAsync();
            while (singleAppComThread.IsBusy)
            {
                Thread.Sleep(50);
            }
            threadComEvent.Close();
        }