SteamController: Start controller with delay after Resume

This commit is contained in:
Kamil Trzciński 2023-01-05 11:47:18 +01:00
parent 2626e9bacf
commit 99369f24e4
3 changed files with 58 additions and 5 deletions

View file

@ -12,24 +12,32 @@ namespace SteamController
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)
@ -45,10 +53,11 @@ namespace SteamController
if (!Enabled || !Steam.Updated)
{
try { Thread.Sleep(100); }
catch (ThreadInterruptedException) { }
ThreadSleep(100);
}
}
X360.Stop();
}
public void Stop()
@ -62,5 +71,18 @@ namespace SteamController
thread = null;
}
}
private bool ThreadSleep(int delayMs)
{
try
{
Thread.Sleep(delayMs);
return true;
}
catch (ThreadInterruptedException)
{
return false;
}
}
}
}

View file

@ -1,5 +1,6 @@
using CommonHelpers;
using ExternalHelpers;
using Microsoft.Win32;
using System.ComponentModel;
using System.Diagnostics;
@ -10,6 +11,8 @@ namespace SteamController
public const String Title = "Steam Controller";
public static readonly String TitleWithVersion = Title + " v" + Application.ProductVersion.ToString();
public const int ControllerDelayAfterResumeMs = 1000;
Container components = new Container();
NotifyIcon notifyIcon;
StartupManager startupManager = new StartupManager(Title);
@ -147,6 +150,24 @@ namespace SteamController
};
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)
@ -201,6 +222,7 @@ namespace SteamController
public void Dispose()
{
Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
notifyIcon.Visible = false;
context.Stop();
using (context) { }

View file

@ -28,6 +28,15 @@ namespace SteamController.Devices
using (client) { }
}
public void Start()
{
}
public void Stop()
{
lock (this) { Fail(); }
}
internal bool Tick()
{
if (this.device is not null)