Skip to content

Commit

Permalink
added plan switching!
Browse files Browse the repository at this point in the history
  • Loading branch information
Neuroburst committed Apr 14, 2024
1 parent 7f15b1a commit c585718
Show file tree
Hide file tree
Showing 576 changed files with 6,683 additions and 496 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bin/Release
/.vs
Binary file removed .vs/PowerTray/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .vs/PowerTray/v17/.futdcache.v2
Binary file not shown.
Binary file removed .vs/PowerTray/v17/.suo
Binary file not shown.
55 changes: 0 additions & 55 deletions .vs/PowerTray/v17/DocumentLayout.json

This file was deleted.

Binary file removed .vs/ProjectEvaluation/powertray.metadata.v7.bin
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/powertray.projects.v7.bin
Binary file not shown.
200 changes: 192 additions & 8 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,44 @@

using System.Windows.Interop;

using System.Configuration;
using LiveCharts;
using LiveCharts.Wpf;
using LiveCharts.Defaults;

using LibreHardwareMonitor.Hardware;
using System.Security.Principal;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using LibreHardwareMonitor.Hardware.Cpu;

/// TODO:
// make sure special power plan still works with usb devices

// NOTIFICAITON HAPPENS WHEN SWITCHING FROM CHARGIN TO IDLE (BAD)
// create BatteryBoost Profile if doesn't exist (make sure to add max processor state and usb savings)


/// SUFFERING:
// font is unreadable in light mode :(
// make icon auto-darkmode (doesn't work on publish)
// scroll is too sensitive
// make tooltip stay open somehow

// INFORMATION GATHERING
// figure out how to use win32 API to make it give weird information (and use same battery as kernel)
// make option for multiple batteries besides the auto-selected one

namespace PowerTray
{
public class PowerPlan
{
public readonly string Name;
public Guid Guid;

public PowerPlan(string name, Guid guid)
{
Name = name;
Guid = guid;
}
}

public partial class App : System.Windows.Application
{

Expand All @@ -51,7 +68,29 @@ public partial class App : System.Windows.Application

public enum DisplayedInfo { percentage, chargeRate, calcChargeRate };


// PowerPlans
[DllImport("PowrProf.dll")]
public static extern UInt32 PowerEnumerate(IntPtr RootPowerKey, IntPtr SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, UInt32 AcessFlags, UInt32 Index, ref Guid Buffer, ref UInt32 BufferSize);
private const uint ACCESS_SCHEME = 16;
[DllImport("PowrProf.dll")]
public static extern UInt32 PowerReadFriendlyName(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, IntPtr Buffer, ref UInt32 BufferSize);

[DllImportAttribute("powrprof.dll", EntryPoint = "PowerGetActiveScheme")]
public static extern uint PowerGetActiveScheme(IntPtr UserPowerKey, out IntPtr ActivePolicyGuid);
[DllImportAttribute("powrprof.dll", EntryPoint = "PowerSetActiveScheme")]
public static extern uint PowerSetActiveScheme(IntPtr UserPowerKey, ref Guid ActivePolicyGuid);

public static Wpf.Ui.Controls.MenuItem pwrPlans;

// Params ---
public static List<PowerPlan> plans = new List<PowerPlan>();

public static BatteryStatus? charging = null;
public static string acplanName = "Balanced";
public static string batteryPlanName = "Balanced";
public static bool notifs = true;

public static DisplayedInfo tray_display = DisplayedInfo.percentage; // (CHANGEABLE)

static float trayFontSize = 11f; // (CHANGEABLE)
Expand All @@ -64,7 +103,7 @@ public enum DisplayedInfo { percentage, chargeRate, calcChargeRate };
public static int trayRefreshRate = 1000; // in milliseconds (CHANGEABLE)
public static int batInfoRefreshRate = 1000; // in milliseconds (CHANGEABLE)

public static int graphRefreshRate = 4000; // in milliseconds (CHANGEABLE)
public static int graphRefreshRate = 2000; // in milliseconds (CHANGEABLE)

static Color chargingColor = Color.Green;
static Color highColor = Color.Black;
Expand All @@ -79,6 +118,8 @@ public enum DisplayedInfo { percentage, chargeRate, calcChargeRate };
public static uint batteryTag = 0;
public static SafeFileHandle batteryHandle = null;

public static bool auto_switch = false;

public static bool firstTime = true;
public static bool graphFirstTime = true;
public static List<int> remainChargeHistory = new List<int>();
Expand Down Expand Up @@ -234,6 +275,11 @@ public static void LoadSettings()
maxChargeHistoryLength = settings.BufferSize;
graphsHistoryLength = settings.HistoryLength;

auto_switch = settings.AutoSwitch;
notifs = settings.Notifs;
acplanName = settings.ACPlan;
batteryPlanName = settings.BatteryPlan;

trayRefreshRate = settings.TrayRefreshRate;
tray_timer.Interval = new TimeSpan(0, 0, 0, 0, trayRefreshRate);
batInfoRefreshRate = settings.BatInfoRefreshRate;
Expand All @@ -251,8 +297,114 @@ public static void LoadSettings()
trayFont = new Font(trayFontType, trayFontSize * trayFontQualityMultiplier, settings.FontStyle);
}


// Power Plans
public static string ReadFriendlyName(Guid schemeGuid)
{
uint sizeName = 1024;
IntPtr pSizeName = Marshal.AllocHGlobal((int)sizeName);

string friendlyName;
try
{
PowerReadFriendlyName(IntPtr.Zero, ref schemeGuid, IntPtr.Zero, IntPtr.Zero, pSizeName, ref sizeName);
friendlyName = Marshal.PtrToStringUni(pSizeName);
}
finally
{
Marshal.FreeHGlobal(pSizeName);
}
return (friendlyName);
}

public static void GeneratePowerPlanList()
{
plans.Clear();
foreach (Guid guidPlan in GetPlans())
{
PowerPlan plan = new PowerPlan(ReadFriendlyName(guidPlan), guidPlan);
plans.Add(plan);
}
plans = plans.OrderBy(i => i.Name).ToList();
}

private static IEnumerable<Guid> GetPlans()
{
Guid schemeGuid = Guid.Empty;
uint sizeSchemeGuid = (uint)Marshal.SizeOf(typeof(Guid));
uint schemeIndex = 0;

while (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero,
ACCESS_SCHEME, schemeIndex,
ref schemeGuid, ref sizeSchemeGuid) == 0)
{
yield return schemeGuid;
schemeIndex++;
}
}


public static Guid GetActivePlanGuid()
{
Guid ActiveScheme = Guid.Empty;
if (PowerGetActiveScheme((IntPtr)null, out IntPtr ptr) == 0)
{
ActiveScheme = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid));
Marshal.FreeHGlobal(ptr);
}
return (ActiveScheme);
}
public static void RefreshPowerPlans()
{
GeneratePowerPlanList();
if (pwrPlans != null)
{
pwrPlans.Items.Clear();
foreach (PowerPlan plan in plans)
{
Guid active_plan = GetActivePlanGuid();

var item = new Wpf.Ui.Controls.MenuItem()
{
Header = plan.Name,
IsCheckable = true,
IsChecked = plan.Guid == active_plan,
//Icon = new SymbolIcon(SymbolRegular.Settings20, 14, false),
};
item.Command = new RelayCommand<dynamic>(action => SetPlan(plan.Guid, plan.Name), canExecute => true);

pwrPlans.Items.Add(item);
}
}
if (settingsWindow != null)
{
settingsWindow.UpdatePlansList();
}

}

public static void SetPlan(Guid guid, string name)
{
if (notifs)
{
ToastContentBuilder toast = new ToastContentBuilder();
toast.AddText("Power Plan Switched");
toast.AddText("Switched to " + name + " plan");
toast.SetToastDuration(ToastDuration.Short);
toast.Show();
}
PowerSetActiveScheme(IntPtr.Zero, ref guid);
RefreshPowerPlans();
//foreach (Wpf.Ui.Controls.MenuItem menu in pwrPlans.Items)
//{
// menu.IsChecked = false;
//}
//item.IsChecked = true;
}

private void App_Startup(object sender, StartupEventArgs e)
{

// debug
SetupUnhandledExceptionHandling();

Expand Down Expand Up @@ -301,6 +453,14 @@ private void App_Startup(object sender, StartupEventArgs e)
Command = TraySwitch,
};

pwrPlans = new Wpf.Ui.Controls.MenuItem()
{
Header = "Power Plans",
Icon = new SymbolIcon(SymbolRegular.BatterySaver20, 14, false),
};

RefreshPowerPlans();

var settings = new Wpf.Ui.Controls.MenuItem()
{
Header = "Settings",
Expand All @@ -317,7 +477,7 @@ private void App_Startup(object sender, StartupEventArgs e)

var contextMenu = new ContextMenu()
{
Items = {batteryInfo, graphs, switchInfo, settings, exit }
Items = {batteryInfo, graphs, switchInfo, pwrPlans, settings, exit }
};

toolTip = new ToolTip();
Expand Down Expand Up @@ -402,7 +562,7 @@ private void UpdateTray(object sender, EventArgs e)
windowDarkMode = appdarkMode;
ApplicationThemeManager.Apply(
(appdarkMode ? ApplicationTheme.Dark : ApplicationTheme.Light));

//Style style = (Style)Resources["Style"];
}

Expand All @@ -426,6 +586,30 @@ private void UpdateTray(object sender, EventArgs e)
int remainChargeCapMwh = (int)bat_info["Remaining Charge mWh"];
int chargeRateMw = (int)bat_info["Reported Charge Rate mW"];

// plans
if (auto_switch && charging != (BatteryStatus)bat_info["Status"])
{
charging = (BatteryStatus)bat_info["Status"];
var ac = charging != BatteryStatus.Discharging;

string name = batteryPlanName;
if (ac)
{
name = acplanName;
}

Guid guid = plans[0].Guid;
foreach (PowerPlan plan in plans)
{
if (plan.Name == name)
{
guid = plan.Guid;
}
}

SetPlan(guid, name);
}

// update remainChargeHistory ---
var historyLength = remainChargeHistory.Count;
if (historyLength == 0 || remainChargeHistory[historyLength - 1] != remainChargeCapMwh)
Expand Down Expand Up @@ -615,7 +799,7 @@ private static Color LightenColor(Color color)
private static void Quit() // check if the exit button was pressed
{
trayIcon.Dispose();
System.Windows.Application.Current.Shutdown();
Current.Shutdown();
}

public static string GetCalculatedTimeLeft(int remainChargeCapMwh, int fullChargeCapMwh)
Expand Down
2 changes: 1 addition & 1 deletion Graph.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"

mc:Ignorable="d"
Title="PowerTray Graphs" Height="400" Width="700" MinHeight="350" MinWidth="350"
Title="PowerTray Graphs" Height="420" Width="680" MinHeight="350" MinWidth="350"
ExtendsContentIntoTitleBar="True"
WindowBackdropType="Mica"
WindowCornerPreference="Round"
Expand Down
6 changes: 3 additions & 3 deletions Graph.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Graph()
Values = App.chargeRateGraph,
StrokeThickness = 5,
LineSmoothness = 0.5,
PointGeometrySize = 5,
PointGeometrySize = 9,
},

new LineSeries
Expand All @@ -54,7 +54,7 @@ public Graph()
Values = App.calcChargeRateGraph,
StrokeThickness = 5,
LineSmoothness = 0.5,
PointGeometrySize = 5,
PointGeometrySize = 9,
},

new LineSeries
Expand All @@ -63,7 +63,7 @@ public Graph()
Values = App.cpuWattageGraph,
StrokeThickness = 5,
LineSmoothness = 0.5,
PointGeometrySize = 5,
PointGeometrySize = 9,
},
};

Expand Down
Loading

0 comments on commit c585718

Please sign in to comment.