Skip to content

Commit

Permalink
SteamController: Start controller with delay after Resume
Browse files Browse the repository at this point in the history
  • Loading branch information
ayufan committed Jan 5, 2023
1 parent edca166 commit c05f8f2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
32 changes: 27 additions & 5 deletions SteamController/ContextThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ public partial class Context : IDisposable

public int UpdatesPerSec { get; private set; }

public bool Start()
public bool Start(int? startDelayMs = null)
{
if (thread is not null)
return false;

UpdatesPerSec = 0;
threadRunning = true;
thread = new Thread(ThreadLoop);
thread.Start();
thread.Start(startDelayMs);
return true;
}

private void ThreadLoop(object? obj)
private void ThreadLoop(object? startDelayMs)
{
if (startDelayMs is int)
{
ThreadSleep((int)startDelayMs);
}

var stopwatch = new Stopwatch();
stopwatch.Start();
int updates = 0;
var nextReset = stopwatch.Elapsed.Add(UpdateResetInterval);

X360.Start();

while (threadRunning)
{
if (nextReset < stopwatch.Elapsed)
Expand All @@ -45,10 +53,11 @@ private void ThreadLoop(object? obj)

if (!Enabled || !Steam.Updated)
{
try { Thread.Sleep(100); }
catch (ThreadInterruptedException) { }
ThreadSleep(100);
}
}

X360.Stop();
}

public void Stop()
Expand All @@ -62,5 +71,18 @@ public void Stop()
thread = null;
}
}

private bool ThreadSleep(int delayMs)
{
try
{
Thread.Sleep(delayMs);
return true;
}
catch (ThreadInterruptedException)
{
return false;
}
}
}
}
22 changes: 22 additions & 0 deletions SteamController/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommonHelpers;
using ExternalHelpers;
using Microsoft.Win32;
using System.ComponentModel;
using System.Diagnostics;

Expand All @@ -10,6 +11,8 @@ internal class Controller : IDisposable
public const String Title = "Steam Controller";
public static readonly String TitleWithVersion = Title + " v" + Application.ProductVersion.ToString();

public const int ControllerDelayAfterResumeMs = 1000;

public static readonly Dictionary<String, Profiles.Profile> PreconfiguredUserProfiles = new Dictionary<String, Profiles.Profile>()
{
{ "*.desktop.cs", new Profiles.Predefined.DesktopProfile() { Name = "Desktop" } },
Expand Down Expand Up @@ -177,6 +180,24 @@ public Controller()
};

context.Start();

Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
}

private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Log.TraceLine("SystemEvents_PowerModeChanged: {0}", e.Mode);

switch (e.Mode)
{
case PowerModes.Suspend:
context.Stop();
break;

case PowerModes.Resume:
context.Start(ControllerDelayAfterResumeMs);
break;
}
}

private void ContextStateUpdate_Tick(object? sender, EventArgs e)
Expand Down Expand Up @@ -231,6 +252,7 @@ private void ContextStateUpdate_Tick(object? sender, EventArgs e)

public void Dispose()
{
Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
notifyIcon.Visible = false;
context.Stop();
using (context) { }
Expand Down
9 changes: 9 additions & 0 deletions SteamController/Devices/Xbox360Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public void Dispose()
using (client) { }
}

public void Start()
{
}

public void Stop()
{
lock (this) { Fail(); }
}

internal bool Tick()
{
if (this.device is not null)
Expand Down

0 comments on commit c05f8f2

Please sign in to comment.