Files
archived-g-helper/app/Program.cs
2023-05-22 11:18:57 +02:00

263 lines
8.3 KiB
C#

using Microsoft.Win32;
using NAudio.CoreAudioApi;
using System.Diagnostics;
using System.Globalization;
using System.Management;
using System.Reflection;
using System.Security.Principal;
using Tools;
using static NativeMethods;
namespace GHelper
{
static class Program
{
public static NotifyIcon trayIcon = new NotifyIcon
{
Text = "G-Helper",
Icon = Properties.Resources.standard,
Visible = true
};
public static AsusACPI? acpi;
public static SettingsForm settingsForm = new SettingsForm();
public static IntPtr unRegPowerNotify;
private static long lastAuto;
private static long lastTheme;
private static long lastAdmin;
public static InputDispatcher inputDispatcher;
private static PowerLineStatus isPlugged = PowerLineStatus.Unknown;
// The main entry point for the application
public static void Main(string[] args)
{
string action = "";
if (args.Length > 0) action = args[0];
Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;
Debug.WriteLine(CultureInfo.CurrentUICulture);
//Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("uk");
CheckProcesses();
try
{
acpi = new AsusACPI();
}
catch
{
DialogResult dialogResult = MessageBox.Show(Properties.Strings.ACPIError, Properties.Strings.StartupError, MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Process.Start(new ProcessStartInfo("https://www.asus.com/support/FAQ/1047338/") { UseShellExecute = true });
}
Application.Exit();
return;
}
Logger.WriteLine("------------");
Logger.WriteLine("App launched: " + AppConfig.GetModel() + " :" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + CultureInfo.CurrentUICulture + (IsUserAdministrator() ? "A" : ""));
Application.EnableVisualStyles();
HardwareControl.RecreateGpuControl();
var ds = settingsForm.Handle;
trayIcon.MouseClick += TrayIcon_MouseClick;
inputDispatcher = new InputDispatcher(ds);
settingsForm.InitAura();
settingsForm.InitMatrix();
settingsForm.SetStartupCheck(Startup.IsScheduled());
SetAutoModes();
// Subscribing for system power change events
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
// Subscribing for monitor power on events
PowerSettingGuid settingGuid = new NativeMethods.PowerSettingGuid();
unRegPowerNotify = NativeMethods.RegisterPowerSettingNotification(ds, settingGuid.ConsoleDisplayState, NativeMethods.DEVICE_NOTIFY_WINDOW_HANDLE);
if (Environment.CurrentDirectory.Trim('\\') == Application.StartupPath.Trim('\\') || action.Length > 0)
{
SettingsToggle(action);
}
Application.Run();
}
static void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastTheme) < 2000) return;
switch (e.Category)
{
case UserPreferenceCategory.General:
bool changed = settingsForm.InitTheme();
if (changed)
{
Debug.WriteLine("Theme Changed");
lastTheme = DateTimeOffset.Now.ToUnixTimeMilliseconds();
}
if (settingsForm.fans is not null && settingsForm.fans.Text != "")
settingsForm.fans.InitTheme();
if (settingsForm.keyb is not null && settingsForm.keyb.Text != "")
settingsForm.keyb.InitTheme();
break;
}
}
public static void SetAutoModes()
{
if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastAuto) < 3000) return;
lastAuto = DateTimeOffset.Now.ToUnixTimeMilliseconds();
isPlugged = SystemInformation.PowerStatus.PowerLineStatus;
Logger.WriteLine("AutoSetting for " + isPlugged.ToString());
settingsForm.SetBatteryChargeLimit(AppConfig.getConfig("charge_limit"));
settingsForm.AutoPerformance();
bool switched = settingsForm.AutoGPUMode();
if (!switched)
{
settingsForm.InitGPUMode();
settingsForm.AutoScreen();
}
settingsForm.AutoKeyboard();
settingsForm.matrix.SetMatrix();
}
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (SystemInformation.PowerStatus.PowerLineStatus == isPlugged) return;
Logger.WriteLine("Windows - Power Mode Changed");
SetAutoModes();
}
public static void SettingsToggle(string action = "")
{
if (settingsForm.Visible)
settingsForm.Hide();
else
{
settingsForm.Show();
settingsForm.Activate();
settingsForm.VisualiseGPUMode();
switch (action)
{
case "gpu":
Startup.ReScheduleAdmin();
settingsForm.FansToggle();
break;
case "gpurestart":
settingsForm.RestartGPU(false);
break;
}
}
}
static void TrayIcon_MouseClick(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SettingsToggle();
}
}
static void OnExit(object sender, EventArgs e)
{
trayIcon.Visible = false;
NativeMethods.UnregisterPowerSettingNotification(unRegPowerNotify);
Application.Exit();
}
static void CheckProcesses()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
if (processes.Length > 1)
{
foreach (Process process in processes)
if (process.Id != currentProcess.Id)
try
{
process.Kill();
}
catch (Exception ex)
{
Logger.WriteLine(ex.ToString());
MessageBox.Show(Properties.Strings.AppAlreadyRunningText, Properties.Strings.AppAlreadyRunning, MessageBoxButtons.OK);
Application.Exit();
return;
}
}
}
public static bool IsUserAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
public static void RunAsAdmin(string? param = null)
{
if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastAdmin) < 2000) return;
lastAdmin = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// Check if the current user is an administrator
if (!IsUserAdministrator())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = param;
startInfo.Verb = "runas";
Process.Start(startInfo);
Application.Exit();
}
}
}
}