Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion EnergyStar/EnergyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace EnergyStar
{
public unsafe class EnergyManager
{
public static readonly HashSet<string> BypassProcessList = new HashSet<string>
public static readonly HashSet<string> DefaultBypassProcessList = new HashSet<string>
{
// Not ourselves
"EnergyStar.exe".ToLowerInvariant(),
Expand Down Expand Up @@ -44,6 +44,7 @@ public unsafe class EnergyManager
// WUDF
"WUDFRd.exe".ToLowerInvariant(),
};
public static HashSet<string> BypassProcessList = DefaultBypassProcessList;
// Speical handling needs for UWP to get the child window process
public const string UWPFrameHostApp = "ApplicationFrameHost.exe";

Expand Down Expand Up @@ -184,5 +185,15 @@ public static void ThrottleAllUserBackgroundProcesses()
Win32Api.CloseHandle(hProcess);
}
}

public static void SetBypassProcessList(IEnumerable<string> newBypassList)
{
var list = new List<string>();

list.AddRange(DefaultBypassProcessList);
list.AddRange(newBypassList);

BypassProcessList = list.Distinct().ToHashSet();
}
}
}
57 changes: 57 additions & 0 deletions EnergyStar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@ static async void HouseKeepingThreadProc()
}
}

static void OnBypassListChanged(object sender, FileSystemEventArgs e)
{
var path = e.FullPath;
if (path == null)
return;

LoadBypassList(path);
}

static void LoadBypassList(string path)
{
if (File.Exists(path))
{
var file = File.OpenText(path);
var content = file.ReadToEnd();
file.Close();

if (content != null)
{
var srcBypassList = content.Replace("\r\n", "\n").Split('\n');
var bypassList = new List<string> { };
// Remove empty items
foreach (var item in srcBypassList)
{
var trimed = item.Trim().ToLowerInvariant();
if (trimed.Length > 0)
{
bypassList.Add(trimed);
}
}

if (bypassList != null)
{
EnergyManager.SetBypassProcessList(bypassList);
Console.WriteLine("Bypass list updated");
}
}
}
}

static void Main(string[] args)
{
// Well, this program only works for Windows Version starting with Cobalt...
Expand All @@ -38,6 +78,23 @@ static void Main(string[] args)
Environment.Exit(120);
}

// Load bypass list and watch for changes.
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
var bypassListFile = Path.Combine(baseDir, "bypass.txt");
using var bypassListWatcher = new FileSystemWatcher(baseDir);

bypassListWatcher.Changed += OnBypassListChanged;
bypassListWatcher.Created += OnBypassListChanged;
bypassListWatcher.Deleted += OnBypassListChanged;
bypassListWatcher.Renamed += OnBypassListChanged;

bypassListWatcher.Filter = "bypass.txt";
bypassListWatcher.IncludeSubdirectories = true;
bypassListWatcher.EnableRaisingEvents = true;

LoadBypassList(bypassListFile);


HookManager.SubscribeToWindowEvents();
EnergyManager.ThrottleAllUserBackgroundProcesses();

Expand Down
53 changes: 53 additions & 0 deletions examples/winsw/winsw.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--
MIT License

Copyright (c) 2008-2020 Kohsuke Kawaguchi, Sun Microsystems, Inc., CloudBees,
Inc., Oleg Nenashev and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->

<!--
This is an example of a minimal Windows Service Wrapper configuration, which includes only mandatory options.

This configuration file should be placed near the WinSW executable, the name should be the same.
E.g. for myapp.exe the configuration file name should be myapp.xml

You can find more information about the configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
Full example: https://github.com/kohsuke/winsw/blob/master/examples/sample-allOptions.xml
-->
<service>

<!-- ID of the service. It should be unique across the Windows system-->
<id>energystar</id>
<!-- Display name of the service -->
<name>EnergyStar</name>
<!-- Service description -->
<description>A terrible application setting SV2 Efficiency Mode for inactive Windows apps and user background apps</description>

<!-- Path to the executable, which should be started -->
<executable>%BASE%\EnergyStar.exe</executable>

<logpath>%BASE%\logs</logpath>

<log mode="roll">
<sizeThreshold>1024</sizeThreshold>
<keepFiles>1</keepFiles>
</log>
</service>