Skip to content

Commit 1a1e727

Browse files
committed
Changes to keep compatibility with unsupported laptop models.
1 parent ad150d8 commit 1a1e727

20 files changed

Lines changed: 117 additions & 49 deletions

LenovoLegionToolkit.Lib.Automation/AutomationProcessor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class AutomationProcessor(
2323
GameAutoListener gameAutoListener,
2424
ProcessAutoListener processAutoListener,
2525
TimeAutoListener timeAutoListener,
26-
TimeInveralAutoListener timeInveralAutoListener,
26+
TimeIntervalAutoListener timeIntervalAutoListener,
2727
UserInactivityAutoListener userInactivityAutoListener,
2828
WiFiAutoListener wifiAutoListener)
2929
{
@@ -270,9 +270,9 @@ private async void TimeAutoListener_Changed(object? sender, TimeAutoListener.Cha
270270
await ProcessEvent(e).ConfigureAwait(false);
271271
}
272272

273-
private async void TimeIntervalAutoListener_Changed(object? sender, int interval)
273+
private async void TimeIntervalAutoListener_Changed(object? sender, TimeIntervalAutoListener.ChangedEventArgs args)
274274
{
275-
var e = new TimeIntervalAutomationEvent { Interval = interval };
275+
var e = new TimeIntervalAutomationEvent(args.Interval);
276276
await ProcessEvent(e).ConfigureAwait(false);
277277
}
278278

@@ -321,7 +321,7 @@ private async Task UpdateListenersAsync()
321321
await gameAutoListener.UnsubscribeChangedAsync(GameAutoListener_Changed).ConfigureAwait(false);
322322
await processAutoListener.UnsubscribeChangedAsync(ProcessAutoListener_Changed).ConfigureAwait(false);
323323
await timeAutoListener.UnsubscribeChangedAsync(TimeAutoListener_Changed).ConfigureAwait(false);
324-
await timeInveralAutoListener.UnsubscribeChangedAsync(TimeIntervalAutoListener_Changed).ConfigureAwait(false);
324+
await timeIntervalAutoListener.UnsubscribeChangedAsync(TimeIntervalAutoListener_Changed).ConfigureAwait(false);
325325
await userInactivityAutoListener.UnsubscribeChangedAsync(UserInactivityAutoListener_Changed).ConfigureAwait(false);
326326
await wifiAutoListener.UnsubscribeChangedAsync(WiFiAutoListener_Changed).ConfigureAwait(false);
327327

LenovoLegionToolkit.Lib.Automation/Pipeline/Triggers/TimeIntervalAutomationPipelineTrigger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task<bool> IsMatchingState()
4747
return false;
4848
}
4949

50-
public void UpdateEnvironment(ref AutomationEnvironment environment)
50+
public void UpdateEnvironment(AutomationEnvironment environment)
5151
{
5252
environment.ACInterval = ACInterval;
5353
environment.DCInterval = DCInterval;

LenovoLegionToolkit.Lib.Automation/Steps/DisplayBrightnessAutomationStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public Task RunAsync(AutomationContext context, AutomationEnvironment environmen
1919
return _controller.SetBrightnessAsync(Brightness);
2020
}
2121

22-
public override IAutomationStep DeepCopy() => new DisplayBrightnessAutomationStep(State);
22+
IAutomationStep IAutomationStep.DeepCopy() => new DisplayBrightnessAutomationStep(Brightness);
2323
}

LenovoLegionToolkit.Lib.Automation/Steps/ProcessAutomationStep.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using LenovoLegionToolkit.Lib.System;
56
using Newtonsoft.Json;
@@ -17,7 +18,7 @@ public class ProcessAutomationStep : IAutomationStep
1718

1819
public Task<bool> IsSupportedAsync() => Task.FromResult(true);
1920

20-
public async Task RunAsync(AutomationEnvironment _)
21+
public async Task RunAsync(AutomationContext context, AutomationEnvironment environment, CancellationToken token)
2122
{
2223

2324
if (State.Processes == null)

LenovoLegionToolkit.Lib.Automation/Steps/ProcessorTDPAutomationStep.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using LenovoLegionToolkit.Lib.Automation.Utils;
45
using Newtonsoft.Json;
@@ -18,7 +19,7 @@ public class ProcessorTDPAutomationStep : IAutomationStep
1819

1920
public Task<bool> IsSupportedAsync() => Task.FromResult(true);
2021

21-
public async Task RunAsync(AutomationEnvironment _)
22+
public async Task RunAsync(AutomationContext context, AutomationEnvironment environment, CancellationToken token)
2223
{
2324
await _manager.InitializeAsync();
2425

LenovoLegionToolkit.Lib.Automation/Utils/ProcessorManager.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace LenovoLegionToolkit.Lib.Automation.Utils;
1212
public class ProcessorManager
1313
{
1414
private readonly ProcessorController _controller = IoCContainer.Resolve<ProcessorController>();
15-
private readonly TimeIntervalAutoListener _timeIntervalListener;
15+
private readonly TimeIntervalAutoListener _timeIntervalAutoListener;
1616

1717
private readonly AsyncLock _ioLock = new();
1818

@@ -26,9 +26,9 @@ public class ProcessorManager
2626
private double _slow;
2727
private bool _useMSR;
2828

29-
public ProcessorManager(TimeIntervalAutoListener timeIntervalListener)
29+
public ProcessorManager(TimeIntervalAutoListener timeIntervalAutoListener)
3030
{
31-
_timeIntervalListener = timeIntervalListener ?? throw new ArgumentNullException(nameof(timeIntervalListener));
31+
_timeIntervalAutoListener = timeIntervalAutoListener ?? throw new ArgumentNullException(nameof(timeIntervalAutoListener));
3232
// initialize processor
3333
_controller = _controller.GetCurrent();
3434
}
@@ -44,7 +44,7 @@ public async Task InitializeAsync()
4444
using (await _ioLock.LockAsync().ConfigureAwait(false))
4545
{
4646
//_timeIntervalListener.Changed += TimeIntervalListener_Changed;
47-
await _timeIntervalListener.SubscribeChangedAsync(TimeIntervalListener_Changed).ConfigureAwait(false);
47+
await _timeIntervalAutoListener.SubscribeChangedAsync(TimeIntervalAutoListener_Changed).ConfigureAwait(false);
4848

4949
await StopAsync().ConfigureAwait(false);
5050
}
@@ -58,13 +58,24 @@ public Task StartAsync(double stapm, double fast, double slow, bool useMSR, int
5858
_useMSR = useMSR;
5959

6060
if (Log.Instance.IsTraceEnabled)
61-
Log.Instance.Trace($"Starting time interval listener...");
61+
Log.Instance.Trace($"Setting processor TDP/power limits...");
6262

63-
_timeIntervalListener.StartAsync(interval*1000);
63+
MaintainTDP(_stapm, _fast, _slow, _useMSR).ConfigureAwait(false);
6464

6565
if (Log.Instance.IsTraceEnabled)
66-
Log.Instance.Trace($"Stopped time interval listener...");
66+
Log.Instance.Trace($"Processor TDP/power limits set");
6767

68+
if (interval > 0)
69+
{
70+
if (Log.Instance.IsTraceEnabled)
71+
Log.Instance.Trace($"Starting time interval listener...");
72+
73+
_timeIntervalAutoListener.StartAsync(interval * 1000);
74+
75+
if (Log.Instance.IsTraceEnabled)
76+
Log.Instance.Trace($"Started time interval listener.");
77+
}
78+
6879
return Task.CompletedTask;
6980
}
7081

@@ -73,10 +84,10 @@ public Task StopAsync()
7384
if (Log.Instance.IsTraceEnabled)
7485
Log.Instance.Trace($"Stopping time interval listener...");
7586

76-
_timeIntervalListener.StopNowAsync();
87+
_timeIntervalAutoListener.StopNowAsync();
7788

7889
if (Log.Instance.IsTraceEnabled)
79-
Log.Instance.Trace($"Stopped time interval listener...");
90+
Log.Instance.Trace($"Stopped time interval listener.");
8091

8192
_stapm = 0;
8293
_fast = 0;
@@ -177,7 +188,7 @@ public Task MaintainTDP(double stapm, double fast, double slow, bool useMSR)
177188
return Task.CompletedTask;
178189
}
179190

180-
private async void TimeIntervalListener_Changed(object? sender, int interval)
191+
private async void TimeIntervalAutoListener_Changed(object? sender, TimeIntervalAutoListener.ChangedEventArgs e)
181192
{
182193
await MaintainTDP(_stapm, _fast, _slow, _useMSR).ConfigureAwait(false);
183194
}

LenovoLegionToolkit.Lib/AutoListeners/TimeIntervalAutoListener.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
namespace LenovoLegionToolkit.Lib.AutoListeners;
77

8-
public class TimeIntervalAutoListener : AbstractAutoListener<int>
8+
public class TimeIntervalAutoListener : AbstractAutoListener<TimeIntervalAutoListener.ChangedEventArgs>
99
{
10+
public class ChangedEventArgs(int interval) : EventArgs
11+
{
12+
public int Interval { get; } = interval;
13+
}
14+
1015
private readonly Timer _timer;
1116

1217
public TimeIntervalAutoListener()
@@ -60,5 +65,5 @@ public Task StopNowAsync()
6065
return Task.CompletedTask;
6166
}
6267

63-
private void Timer_Elapsed(object? sender, ElapsedEventArgs e) => RaiseChanged((int)_timer.Interval);
68+
private void Timer_Elapsed(object? sender, ElapsedEventArgs e) => RaiseChanged(new ChangedEventArgs((int)_timer.Interval));
6469
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
using LenovoLegionToolkit.Lib.System.Management;
3+
4+
namespace LenovoLegionToolkit.Lib.Controllers;
5+
6+
public class DisplayBrightnessController
7+
{
8+
public Task SetBrightnessAsync(int brightness) => WMI.WmiMonitorBrightnessMethods.WmiSetBrightness(brightness, 1);
9+
}

LenovoLegionToolkit.Lib/Controllers/PowerPlanController.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime;
45
using System.Runtime.InteropServices;
56
using System.Threading.Tasks;
67
using LenovoLegionToolkit.Lib.Extensions;
@@ -59,7 +60,11 @@ public async Task SetPowerPlanAsync(PowerModeState powerModeState, bool alwaysAc
5960
if (Log.Instance.IsTraceEnabled)
6061
Log.Instance.Trace($"Power plan for power mode {powerModeState} was not found in settings");
6162

62-
powerPlanId = DefaultPowerPlan;
63+
if (DefaultPowerModes.TryGetValue(powerModeState, out var defaultPowerPlanId))
64+
powerPlanId = defaultPowerPlanId;
65+
else
66+
throw new InvalidOperationException("Unknown state");
67+
6368
isDefault = true;
6469
}
6570

@@ -104,6 +109,20 @@ public async Task SetPowerPlanAsync(PowerModeState powerModeState, bool alwaysAc
104109
Log.Instance.Trace($"Power plan {powerPlanToActivate.Guid} activated. [name={powerPlanToActivate.Name}]");
105110
}
106111

112+
public PowerModeState[] GetMatchingPowerModes(Guid powerPlanGuid)
113+
{
114+
var powerModes = new Dictionary<PowerModeState, Guid>(DefaultPowerModes);
115+
116+
foreach (var kv in settings.Store.PowerPlans)
117+
{
118+
powerModes[kv.Key] = kv.Value;
119+
}
120+
121+
return powerModes.Where(kv => kv.Value == powerPlanGuid)
122+
.Select(kv => kv.Key)
123+
.ToArray();
124+
}
125+
107126
public void SetPowerPlanParameter(PowerPlan powerPlan, Brightness brightness)
108127
{
109128
PInvoke.PowerWriteACValueIndex(NullSafeHandle.Null, powerPlan.Guid, PInvoke.GUID_VIDEO_SUBGROUP, PInvokeExtensions.DISPLAY_BRIGTHNESS_SETTING_GUID, brightness.Value);

LenovoLegionToolkit.Lib/Features/AbstractWmiFeature.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,44 @@
44

55
namespace LenovoLegionToolkit.Lib.Features;
66

7-
public abstract class AbstractWmiFeature<T>(
8-
Func<Task<int>> getValue,
9-
Func<int, Task> setValue,
10-
Func<Task<int>>? isSupported = null,
11-
int offset = 0)
12-
: IFeature<T>
13-
where T : struct, Enum, IComparable
7+
public abstract class AbstractWmiFeature<T> : IFeature<T> where T : struct, Enum, IComparable
148
{
15-
public async Task<bool> IsSupportedAsync()
9+
private readonly Func<Task<int>> _getValue;
10+
private readonly Func<int, Task> _setValue;
11+
public readonly Func<Task<int>>? _isSupported;
12+
private readonly int _offset;
13+
14+
protected AbstractWmiFeature(Func<Task<int>> getValue, Func<int, Task> setValue, Func<Task<int>>? isSupported = null, int offset = 0)
15+
{
16+
_getValue = getValue;
17+
_setValue = setValue;
18+
_isSupported = isSupported;
19+
_offset = offset;
20+
}
21+
22+
public virtual async Task<bool> IsSupportedAsync()
1623
{
1724
try
1825
{
19-
if (isSupported is null)
26+
if (_isSupported is null)
2027
return true;
2128

22-
return await isSupported().ConfigureAwait(false) > 0;
29+
return await _isSupported().ConfigureAwait(false) > 0;
2330
}
2431
catch
2532
{
2633
return false;
2734
}
2835
}
36+
2937
public virtual Task<T[]> GetAllStatesAsync() => Task.FromResult(Enum.GetValues<T>());
3038

3139
public virtual async Task<T> GetStateAsync()
3240
{
3341
if (Log.Instance.IsTraceEnabled)
3442
Log.Instance.Trace($"Getting state... [feature={GetType().Name}]");
3543

36-
var internalResult = await getValue().ConfigureAwait(false);
44+
var internalResult = await _getValue().ConfigureAwait(false);
3745
var result = FromInternal(internalResult);
3846

3947
if (Log.Instance.IsTraceEnabled)
@@ -48,13 +56,13 @@ public virtual async Task SetStateAsync(T state)
4856
if (Log.Instance.IsTraceEnabled)
4957
Log.Instance.Trace($"Setting state to {state}... [feature={GetType().Name}]");
5058

51-
await setValue(ToInternal(state)).ConfigureAwait(false);
59+
await _setValue(ToInternal(state)).ConfigureAwait(false);
5260

5361
if (Log.Instance.IsTraceEnabled)
5462
Log.Instance.Trace($"Set state to {state} [feature={GetType().Name}]");
5563
}
5664

57-
private int ToInternal(T state) => (int)(object)state + offset;
65+
private int ToInternal(T state) => (int)(object)state + _offset;
5866

59-
private T FromInternal(int state) => (T)(object)(state - offset);
67+
private T FromInternal(int state) => (T)(object)(state - _offset);
6068
}

0 commit comments

Comments
 (0)