Skip to content

Commit

Permalink
mika76#29 prevented sleep timer created if no power saving enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpainter committed Sep 6, 2018
1 parent f4ef2a2 commit c9c42a4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
59 changes: 41 additions & 18 deletions Mamesaver/Power/PowerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class PowerManager : IDisposable
/// Time before a <see cref="SleepTriggered"/> event is published, based on the user's power management
/// configuration.
/// </summary>
private TimeSpan _sleepTimeout;
private TimeSpan? _sleepTimeout;

/// <summary>
/// Current power source type.
Expand Down Expand Up @@ -72,24 +72,43 @@ public void Initialise()

// Identify policy for selected power type
_sleepTimeout = GetSleepTimeout(powerType.Value);
Log.Information("Connected to {powerType} power; sleeping after {min} minutes",
powerType.Value.ToString(), _sleepTimeout.TotalMinutes);

// Create a timer which fires once when the display or computer should go to sleep
_sleepTimer = new Timer { Interval = (int)_sleepTimeout.TotalMilliseconds, AutoReset = false };

_sleepTimer.Elapsed += SleepTimerTick;
_sleepTimer.Start();
if (_sleepTimeout == null)
{
Log.Information("Connected to {powerType} power; no sleep configured", powerType.Value.ToString());
}
else
{
// Create a timer which fires once when the display or computer should go to sleep
Log.Information("Connected to {powerType} power; sleeping after {min} minutes", powerType.Value.ToString(), _sleepTimeout.Value.TotalMinutes);
InitSleepTimer();
}

// Receive notifications for power state changes so the timer can be updated
_eventWatcher.Initialise();
_eventWatcher.PowerStateChanged += PowerStateChanged;
}

/// <summary>
/// Either creates or updates the sleep timer.
/// </summary>
private void InitSleepTimer()
{
if (_sleepTimeout == null) throw new InvalidOperationException("Can't create a timer with no timeout specified");

if (_sleepTimer == null)
{
_sleepTimer = new Timer { AutoReset = false };
_sleepTimer.Elapsed += SleepTimerTick;
}

_sleepTimer.Interval = (int)_sleepTimeout.Value.TotalMilliseconds;
_sleepTimer.Start();
}

/// <summary>
/// Returns the lowest of screen and PC sleep settings for a given power type.
/// </summary>
private static TimeSpan GetSleepTimeout(PowerType powerType)
private static TimeSpan? GetSleepTimeout(PowerType powerType)
{
var powerPolicy = PowerInterop.GetPowerPolicy(powerType);
return new[] { powerPolicy.VideoTimeout, powerPolicy.IdleTimeout }.Min();
Expand Down Expand Up @@ -119,16 +138,20 @@ private void PowerStateChanged(object sender, EventArgs args)
if (_currentPowerType == powerType) return;
_currentPowerType = powerType.Value;

_sleepTimer?.Stop();

// Identify policy for selected power type
_sleepTimeout = GetSleepTimeout(powerType.Value);
Log.Information("Power changed to {powerType} power; sleeping after {min} minutes",
powerType.Value.ToString(), _sleepTimeout.TotalMinutes);

// Update sleep timer on power state change due to different sleep configurations
_sleepTimer.Interval = (int)_sleepTimeout.TotalMilliseconds;

_sleepTimer.Stop();
_sleepTimer.Start();
if (_sleepTimeout == null)
{
Log.Information("Power changed to {powerType} power; no sleep configured", powerType.Value.ToString());
}
else
{
// Update sleep timer on power state change due to different sleep configurations
Log.Information("Power changed to {powerType} power; sleeping after {min} minutes", powerType.Value.ToString(), _sleepTimeout.Value.TotalMinutes);
InitSleepTimer();
}
}

/// <summary>
Expand Down
19 changes: 13 additions & 6 deletions Mamesaver/Windows/PowerInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ public static PowerPolicy GetPowerPolicy(PowerType powerType)
{
IdleTimeout =
userPolicy.IdleTimeoutAc == 0 || userPolicy.IdleAc.Action == POWER_ACTION.PowerActionNone
? TimeSpan.MaxValue
? (TimeSpan?)null
: TimeSpan.FromSeconds(userPolicy.IdleTimeoutAc),
VideoTimeout = userPolicy.VideoTimeoutAc == 0
? TimeSpan.MaxValue
? (TimeSpan?)null
: TimeSpan.FromSeconds(userPolicy.VideoTimeoutAc)
};
case PowerType.DC:
return new PowerPolicy
{
IdleTimeout =
userPolicy.IdleTimeoutDc == 0 || userPolicy.IdleDc.Action == POWER_ACTION.PowerActionNone
? TimeSpan.MaxValue
? (TimeSpan?)null
: TimeSpan.FromSeconds(userPolicy.IdleTimeoutDc),
VideoTimeout = userPolicy.VideoTimeoutDc == 0
? TimeSpan.MaxValue
? (TimeSpan?)null
: TimeSpan.FromSeconds(userPolicy.VideoTimeoutDc)
};
default:
Expand All @@ -68,7 +68,14 @@ public enum PowerType

public class PowerPolicy
{
public TimeSpan VideoTimeout { get; set; }
public TimeSpan IdleTimeout { get; set; }
/// <summary>
/// Timeout for screen to go to sleep, or <c>null</c> if no sleep is specified.
/// </summary>
public TimeSpan? VideoTimeout { get; set; }

/// <summary>
/// Timeout for PC to go to sleep, or <c>null</c> if no sleep is specified.
/// </summary>
public TimeSpan? IdleTimeout { get; set; }
}
}

0 comments on commit c9c42a4

Please sign in to comment.