Skip to content

Commit c05f8f2

Browse files
committed
SteamController: Start controller with delay after Resume
1 parent edca166 commit c05f8f2

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

SteamController/ContextThread.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,32 @@ public partial class Context : IDisposable
1212

1313
public int UpdatesPerSec { get; private set; }
1414

15-
public bool Start()
15+
public bool Start(int? startDelayMs = null)
1616
{
1717
if (thread is not null)
1818
return false;
1919

20+
UpdatesPerSec = 0;
2021
threadRunning = true;
2122
thread = new Thread(ThreadLoop);
22-
thread.Start();
23+
thread.Start(startDelayMs);
2324
return true;
2425
}
2526

26-
private void ThreadLoop(object? obj)
27+
private void ThreadLoop(object? startDelayMs)
2728
{
29+
if (startDelayMs is int)
30+
{
31+
ThreadSleep((int)startDelayMs);
32+
}
33+
2834
var stopwatch = new Stopwatch();
2935
stopwatch.Start();
3036
int updates = 0;
3137
var nextReset = stopwatch.Elapsed.Add(UpdateResetInterval);
3238

39+
X360.Start();
40+
3341
while (threadRunning)
3442
{
3543
if (nextReset < stopwatch.Elapsed)
@@ -45,10 +53,11 @@ private void ThreadLoop(object? obj)
4553

4654
if (!Enabled || !Steam.Updated)
4755
{
48-
try { Thread.Sleep(100); }
49-
catch (ThreadInterruptedException) { }
56+
ThreadSleep(100);
5057
}
5158
}
59+
60+
X360.Stop();
5261
}
5362

5463
public void Stop()
@@ -62,5 +71,18 @@ public void Stop()
6271
thread = null;
6372
}
6473
}
74+
75+
private bool ThreadSleep(int delayMs)
76+
{
77+
try
78+
{
79+
Thread.Sleep(delayMs);
80+
return true;
81+
}
82+
catch (ThreadInterruptedException)
83+
{
84+
return false;
85+
}
86+
}
6587
}
6688
}

SteamController/Controller.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CommonHelpers;
22
using ExternalHelpers;
3+
using Microsoft.Win32;
34
using System.ComponentModel;
45
using System.Diagnostics;
56

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

14+
public const int ControllerDelayAfterResumeMs = 1000;
15+
1316
public static readonly Dictionary<String, Profiles.Profile> PreconfiguredUserProfiles = new Dictionary<String, Profiles.Profile>()
1417
{
1518
{ "*.desktop.cs", new Profiles.Predefined.DesktopProfile() { Name = "Desktop" } },
@@ -177,6 +180,24 @@ public Controller()
177180
};
178181

179182
context.Start();
183+
184+
Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
185+
}
186+
187+
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
188+
{
189+
Log.TraceLine("SystemEvents_PowerModeChanged: {0}", e.Mode);
190+
191+
switch (e.Mode)
192+
{
193+
case PowerModes.Suspend:
194+
context.Stop();
195+
break;
196+
197+
case PowerModes.Resume:
198+
context.Start(ControllerDelayAfterResumeMs);
199+
break;
200+
}
180201
}
181202

182203
private void ContextStateUpdate_Tick(object? sender, EventArgs e)
@@ -231,6 +252,7 @@ private void ContextStateUpdate_Tick(object? sender, EventArgs e)
231252

232253
public void Dispose()
233254
{
255+
Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
234256
notifyIcon.Visible = false;
235257
context.Stop();
236258
using (context) { }

SteamController/Devices/Xbox360Controller.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ public void Dispose()
2828
using (client) { }
2929
}
3030

31+
public void Start()
32+
{
33+
}
34+
35+
public void Stop()
36+
{
37+
lock (this) { Fail(); }
38+
}
39+
3140
internal bool Tick()
3241
{
3342
if (this.device is not null)

0 commit comments

Comments
 (0)