Skip to content

Commit

Permalink
Update Quick Tools (#197)
Browse files Browse the repository at this point in the history
* prevent crash on null ry

* implement QuickProfilesPage

* Quicktools performance, add MHz unit for GPU, align sliders width.

* disable umc on default profile

* do not automatically enable freshly created profile

* improve foreground detection

* quick profile page now works

* add ProfileUpdated to QuickProfilesPage

* improve process manager

* improve process manager

* oups

* more work

* Preliminary UpdateSelectedProfile() update.

* Cleanup profile variables to from GUI

* add missing IsDefault to default profile

* update ModernWpf

* increase release buffer delay to prevent keys from going through the hook

* fix profiles page

* add todo notes on input manager

* greatly improve inputs manager

Co-authored-by: CasperH <[email protected]>
  • Loading branch information
Valkirie and CasperH2O authored Jul 11, 2022
1 parent c70bec6 commit b07c0cc
Show file tree
Hide file tree
Showing 26 changed files with 643 additions and 203 deletions.
9 changes: 5 additions & 4 deletions ControllerCommon/Devices/AYANEO2021.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using WindowsInput.Events;
using System.Collections.Generic;
using WindowsInput.Events;

namespace ControllerCommon.Devices
{
Expand Down Expand Up @@ -29,10 +30,10 @@ public AYANEO2021() : base()
{ 'Z', 'Y' },
};

listeners.Add("WIN key", new ChordClick(KeyCode.LWin));
listeners.Add("WIN key", new List<KeyCode>() { KeyCode.LWin });
//listeners.Add("TM key", new ChordClick(KeyCode.RAlt, KeyCode.RControlKey, KeyCode.Delete)); // Conflicts with OS
listeners.Add("ESC key", new ChordClick(KeyCode.Escape));
listeners.Add("KB key", new ChordClick(KeyCode.RControlKey, KeyCode.LWin)); // Conflicts with Ayaspace when installed
listeners.Add("ESC key", new List<KeyCode>() { KeyCode.Escape });
listeners.Add("KB key", new List<KeyCode>() { KeyCode.RControlKey, KeyCode.LWin }); // Conflicts with Ayaspace when installed
}
}
}
7 changes: 4 additions & 3 deletions ControllerCommon/Devices/AYANEONEXT.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using WindowsInput.Events;
using System.Collections.Generic;
using WindowsInput.Events;

namespace ControllerCommon.Devices
{
Expand Down Expand Up @@ -29,8 +30,8 @@ public AYANEONEXT() : base()
{ 'Z', 'Y' },
};

listeners.Add("Custom key BIG", new ChordClick(KeyCode.RControlKey, KeyCode.LWin, KeyCode.F12));
listeners.Add("Custom key Small", new ChordClick(KeyCode.LWin, KeyCode.D));
listeners.Add("Custom key BIG", new List<KeyCode>() { KeyCode.RControlKey, KeyCode.LWin, KeyCode.F12 });
listeners.Add("Custom key Small", new List<KeyCode>() { KeyCode.LWin, KeyCode.D });
}
}
}
2 changes: 1 addition & 1 deletion ControllerCommon/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class Device
public OneEuroSettings oneEuroSettings = new OneEuroSettings(0.002d, 0.008d);

// trigger specific settings
public Dictionary<string, ChordClick> listeners = new();
public Dictionary<string, List<KeyCode>> listeners = new();

private static Device device;
public static Device GetDefault()
Expand Down
7 changes: 4 additions & 3 deletions ControllerCommon/Devices/OneXPlayerMiniAMD.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System.Collections.Generic;
using System.Numerics;
using WindowsInput.Events;

namespace ControllerCommon.Devices
Expand Down Expand Up @@ -31,8 +32,8 @@ public OneXPlayerMiniAMD() : base()
{ 'Z', 'Y' },
};

listeners.Add("Keyboard key", new ChordClick(KeyCode.LWin, KeyCode.RControlKey, KeyCode.O));
listeners.Add("Function key", new ChordClick(KeyCode.LWin, KeyCode.D));
listeners.Add("Keyboard key", new List<KeyCode>() { KeyCode.LWin, KeyCode.RControlKey, KeyCode.O });
listeners.Add("Function key", new List<KeyCode>() { KeyCode.LWin, KeyCode.D });
}
}
}
7 changes: 4 additions & 3 deletions ControllerCommon/Devices/OneXPlayerMiniIntel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System.Collections.Generic;
using System.Numerics;
using WindowsInput.Events;

namespace ControllerCommon.Devices
Expand Down Expand Up @@ -32,8 +33,8 @@ public OneXPlayerMiniIntel() : base()
{ 'Z', 'Y' },
};

listeners.Add("Keyboard key", new ChordClick(KeyCode.LWin, KeyCode.RControlKey, KeyCode.O));
listeners.Add("Function key", new ChordClick(KeyCode.LWin, KeyCode.D));
listeners.Add("Keyboard key", new List<KeyCode>() { KeyCode.LWin, KeyCode.RControlKey, KeyCode.O });
listeners.Add("Function key", new List<KeyCode>() { KeyCode.LWin, KeyCode.D });
}
}
}
3 changes: 3 additions & 0 deletions ControllerCommon/Processor/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ protected override void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)

public override void SetTDPLimit(string type, double limit)
{
if (ry == IntPtr.Zero)
return;

// 15W : 15000
limit *= 1000;

Expand Down
1 change: 1 addition & 0 deletions ControllerCommon/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public Profile(string path) : this()
this.executable = AppProperties["FileName"];
this.name = ProductName;
this.path = this.fullpath = path;
this.isEnabled = false;
}

public float GetSensiviy()
Expand Down
4 changes: 3 additions & 1 deletion ControllerCommon/Resources/Default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"name": "Default",
"path": "",
"executable": "Default.exe",
"whitelisted": false
"isDefault": true,
"whitelisted": false,
"umc_enabled": false
}
25 changes: 21 additions & 4 deletions ControllerCommon/Utils/ProcessUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
Expand Down Expand Up @@ -65,21 +67,23 @@ public static IntPtr GetforegroundWindow()
public class FindHostedProcess
{
public ProcessDiagnosticInfo Process { get; private set; }
int attempt = 0;

public FindHostedProcess()
public FindHostedProcess(IntPtr foregroundProcessID)
{
try
{
var foregroundProcessID = WinAPIFunctions.GetforegroundWindow();

if (foregroundProcessID == IntPtr.Zero)
return;

Process = ProcessDiagnosticInfo.TryGetForProcessId((uint)WinAPIFunctions.GetWindowProcessId(foregroundProcessID));

// Get real process
if (Process.ExecutableFileName == "ApplicationFrameHost.exe")
while (Process.ExecutableFileName == "ApplicationFrameHost.exe" && attempt < 10)
{
EnumChildWindows(foregroundProcessID, ChildWindowCallback, IntPtr.Zero);
Thread.Sleep(500);
}
}
catch (Exception)
{
Expand All @@ -94,6 +98,7 @@ private bool ChildWindowCallback(IntPtr hwnd, IntPtr lparam)
if (process.ExecutableFileName != "ApplicationFrameHost.exe")
Process = process;

attempt++;
return true;
}
}
Expand All @@ -111,6 +116,18 @@ public static string GetActiveWindowTitle()
return null;
}

public static string GetWindowTitle(IntPtr handle)
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);

if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}

public static Dictionary<string, string> GetAppProperties(string filePath1)
{
Dictionary<string, string> AppProperties = new Dictionary<string, string>();
Expand Down
4 changes: 4 additions & 0 deletions ControllerService/ControllerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ private void OnClientConnected(object sender)

internal void ProfileUpdated(Profile profile, bool backgroundtask)
{
// skip if not enabled
if (!profile.isEnabled)
return;

// skip if current profile
if (profile == ControllerService.profile)
return;
Expand Down
3 changes: 3 additions & 0 deletions HandheldCompanion/HandheldCompanion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,9 @@
<Page Update="Views\Pages\AboutPage.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Views\QuickPages\QuickProfilesPage.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Views\QuickPages\QuickSettingsPage.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
Expand Down
57 changes: 44 additions & 13 deletions HandheldCompanion/Managers/InputsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public InputsManager()
UpdateTimer = new MultimediaTimer(10);
UpdateTimer.Tick += UpdateReport;

ResetTimer = new MultimediaTimer(10) { AutoReset = false };
ResetTimer.Tick += (sender, e) => { ReleaseBuffer(); };
ResetTimer = new MultimediaTimer(20) { AutoReset = false };
ResetTimer.Tick += ReleaseBuffer;

m_GlobalHook = Hook.GlobalEvents();
m_InputSimulator = new InputSimulator();
Expand All @@ -86,30 +86,59 @@ private void InjectModifiers(KeyEventArgsExt args)
}
}

private int KeyIndex;
private bool KeyUsed;

private void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
{
ResetTimer.Stop();
KeyUsed = false;

if (TriggerLock)
return;

KeyEventArgsExt args = (KeyEventArgsExt)e;
args.SuppressKeyPress = true;

// search for modifiers (improve me)
TriggerBuffer.Add(args);
// todo: implement a key index and only catch the key if it's part of a chord at the specific index
// key0: suppress if is first key of any chords
// key0+n: always suppress. if is not n key of any chords, release buffer
foreach (List<KeyCode> chord in MainWindow.handheldDevice.listeners.Values)
{
if (KeyIndex >= chord.Count)
continue;

KeyCode chordKey = chord[KeyIndex];
KeyCode hookKey = (KeyCode)args.KeyValue;
if (chordKey == hookKey)
{
KeyUsed = true;
KeyIndex++;
break; // leave loop
}
}

// if key is used or previous key was, we need to maintain key(s) order
if (KeyUsed || KeyIndex > 0)
{
args.SuppressKeyPress = true;

if (args.IsKeyUp && args.IsExtendedKey)
InjectModifiers(args);
// add key to buffer
TriggerBuffer.Add(args);

if (args.IsKeyUp && args.IsExtendedKey)
InjectModifiers(args);
}
else
return;

// search for matching triggers
foreach (var pair in MainWindow.handheldDevice.listeners)
{
string listener = pair.Key;
ChordClick chord = pair.Value;
List<KeyCode> chord = pair.Value;

// compare ordered enumerable
var chord_keys = chord.Keys.OrderBy(key => key);
var chord_keys = chord.OrderBy(key => key);
var buffer_keys = GetBufferKeys().OrderBy(key => key);

if (Enumerable.SequenceEqual(chord_keys, buffer_keys))
Expand Down Expand Up @@ -158,7 +187,7 @@ private void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
}
else
{
TriggerInputs inputs = new TriggerInputs(TriggerInputsType.Keyboard, string.Join(",", chord.Keys), listener);
TriggerInputs inputs = new TriggerInputs(TriggerInputsType.Keyboard, string.Join(",", chord), listener);
Triggers[TriggerListener] = inputs;

if (args.IsKeyUp)
Expand All @@ -167,7 +196,6 @@ private void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
TriggerListener = string.Empty;
}
}

return;
}
}
Expand All @@ -189,11 +217,14 @@ private string GetTriggerFromName(string name)
return "";
}

private void ReleaseBuffer()
private void ReleaseBuffer(object? sender, EventArgs e)
{
if (TriggerBuffer.Count == 0)
return;

// reset index
KeyIndex = 0;

try
{
TriggerLock = true;
Expand Down Expand Up @@ -263,7 +294,7 @@ public void Start()
foreach (var pair in MainWindow.handheldDevice.listeners)
{
string listener = pair.Key;
ChordClick chord = pair.Value;
List<KeyCode> chord = pair.Value;

prevKeyUp[listener] = TIME_BURST;
prevKeyDown[listener] = TIME_BURST;
Expand Down
Loading

0 comments on commit b07c0cc

Please sign in to comment.