Refactoring

This commit is contained in:
Serge
2023-05-21 12:27:52 +02:00
parent d818405e04
commit 4ed9675d99
12 changed files with 683 additions and 632 deletions

View File

@@ -6,6 +6,7 @@ using System.Management;
using System.Reflection;
using System.Security.Principal;
using Tools;
using static NativeMethods;
namespace GHelper
{
@@ -20,7 +21,6 @@ namespace GHelper
};
public static AsusACPI? acpi;
public static AppConfig config = new AppConfig();
public static SettingsForm settingsForm = new SettingsForm();
@@ -30,7 +30,7 @@ namespace GHelper
private static long lastTheme;
private static long lastAdmin;
private static bool isOptimizationRunning = OptimizationService.IsRunning();
public static EventDispatcher eventDispatcher;
private static PowerLineStatus isPlugged = PowerLineStatus.Unknown;
@@ -44,7 +44,7 @@ namespace GHelper
Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;
Debug.WriteLine(CultureInfo.CurrentUICulture);
//Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr");
//Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("es");
CheckProcesses();
@@ -65,7 +65,7 @@ namespace GHelper
}
Logger.WriteLine("------------");
Logger.WriteLine("App launched: " + config.GetModel() + " :" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + (IsUserAdministrator() ? "A" : ""));
Logger.WriteLine("App launched: " + AppConfig.GetModel() + " :" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + (IsUserAdministrator() ? "A" : ""));
Application.EnableVisualStyles();
@@ -75,7 +75,7 @@ namespace GHelper
trayIcon.MouseClick += TrayIcon_MouseClick;
acpi.SubscribeToEvents(WatcherEventArrived);
eventDispatcher = new EventDispatcher(ds);
settingsForm.InitAura();
settingsForm.InitMatrix();
@@ -88,16 +88,9 @@ namespace GHelper
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
// Subscribing for monitor power on events
var settingGuid = new NativeMethods.PowerSettingGuid();
PowerSettingGuid settingGuid = new NativeMethods.PowerSettingGuid();
unRegPowerNotify = NativeMethods.RegisterPowerSettingNotification(ds, settingGuid.ConsoleDisplayState, NativeMethods.DEVICE_NOTIFY_WINDOW_HANDLE);
// CTRL + SHIFT + F5 to cycle profiles
Keys keybind_profile = (config.getConfig("keybind_profile") != -1) ? (Keys)config.getConfig("keybind_profile") : Keys.F5;
if (keybind_profile != 0)
{
var ghk = new KeyHandler(KeyHandler.SHIFT | KeyHandler.CTRL, keybind_profile, ds);
ghk.Register();
}
if (Environment.CurrentDirectory.Trim('\\') == Application.StartupPath.Trim('\\') || action.Length > 0)
@@ -105,7 +98,7 @@ namespace GHelper
SettingsToggle(action);
}
if (!isOptimizationRunning) AsusUSB.RunListener(HandleEvent);
Application.Run();
@@ -149,7 +142,7 @@ namespace GHelper
isPlugged = SystemInformation.PowerStatus.PowerLineStatus;
Logger.WriteLine("AutoSetting for " + isPlugged.ToString());
settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit"));
settingsForm.SetBatteryChargeLimit(AppConfig.getConfig("charge_limit"));
settingsForm.AutoPerformance();
bool switched = settingsForm.AutoGPUMode();
@@ -173,191 +166,8 @@ namespace GHelper
}
static void LaunchProcess(string command = "")
{
string executable = command.Split(' ')[0];
string arguments = command.Substring(executable.Length).Trim();
try
{
Process proc = Process.Start(executable, arguments);
}
catch
{
Logger.WriteLine("Failed to run " + command);
}
}
static void CustomKey(string configKey = "m3")
{
string command = config.getConfigString(configKey + "_custom");
int intKey;
try
{
intKey = Convert.ToInt32(command, 16);
}
catch
{
intKey = -1;
}
if (intKey > 0)
NativeMethods.KeyPress(intKey);
else
LaunchProcess(command);
}
static void KeyProcess(string name = "m3")
{
string action = config.getConfigString(name);
if (action is null || action.Length <= 1)
{
if (name == "m4")
action = "ghelper";
if (name == "fnf4")
action = "aura";
if (name == "m3" && !isOptimizationRunning)
action = "micmute";
}
switch (action)
{
case "mute":
NativeMethods.KeyPress(NativeMethods.VK_VOLUME_MUTE);
break;
case "play":
NativeMethods.KeyPress(NativeMethods.VK_MEDIA_PLAY_PAUSE);
break;
case "screenshot":
NativeMethods.KeyPress(NativeMethods.VK_SNAPSHOT);
break;
case "screen":
NativeMethods.TurnOffScreen(Program.settingsForm.Handle);
break;
case "miniled":
settingsForm.BeginInvoke(settingsForm.ToogleMiniled);
break;
case "aura":
settingsForm.BeginInvoke(settingsForm.CycleAuraMode);
break;
case "performance":
settingsForm.BeginInvoke(settingsForm.CyclePerformanceMode);
break;
case "ghelper":
settingsForm.BeginInvoke(delegate
{
SettingsToggle();
});
break;
case "custom":
CustomKey(name);
break;
case "micmute":
using (var enumerator = new MMDeviceEnumerator())
{
var commDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications);
bool muteStatus = !commDevice.AudioEndpointVolume.Mute;
commDevice.AudioEndpointVolume.Mute = muteStatus;
settingsForm.BeginInvoke(settingsForm.RunToast, muteStatus ? "Muted" : "Unmuted", muteStatus ? ToastIcon.MicrophoneMute : ToastIcon.Microphone);
}
break;
default:
break;
}
}
static void TabletMode()
{
bool touchpadState, tabletState;
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\PrecisionTouchPad\Status", false))
{
touchpadState = (key?.GetValue("Enabled")?.ToString() == "1");
}
tabletState = acpi.DeviceGet(AsusACPI.TabletState) > 0;
Logger.WriteLine("Tablet: " + tabletState + " Touchpad: " + touchpadState);
if ((tabletState && touchpadState) || (!tabletState && !touchpadState)) AsusUSB.TouchpadToggle();
}
static void HandleEvent(int EventID)
{
switch (EventID)
{
case 124: // M3
KeyProcess("m3");
return;
case 56: // M4 / Rog button
KeyProcess("m4");
return;
case 174: // FN+F5
settingsForm.BeginInvoke(settingsForm.CyclePerformanceMode);
return;
case 179: // FN+F4
KeyProcess("fnf4");
return;
case 189: // Tablet mode
TabletMode();
return;
}
if (isOptimizationRunning) return;
// Asus Optimization service Events
int brightness = config.getConfig("keyboard_brightness");
switch (EventID)
{
case 197: // FN+F2
brightness = Math.Max(0, brightness - 1);
config.setConfig("keyboard_brightness", brightness);
AsusUSB.ApplyBrightness(brightness);
settingsForm.BeginInvoke(settingsForm.RunToast, "Backlight", ToastIcon.BacklightDown);
break;
case 196: // FN+F3
brightness = Math.Min(3, brightness + 1);
config.setConfig("keyboard_brightness", brightness);
AsusUSB.ApplyBrightness(brightness);
settingsForm.BeginInvoke(settingsForm.RunToast, "Backlight", ToastIcon.BacklightUp);
break;
case 16: // FN+F7
ScreenBrightness.Adjust(-10);
settingsForm.BeginInvoke(settingsForm.RunToast, "Brightness", ToastIcon.BrightnessDown);
break;
case 32: // FN+F8
ScreenBrightness.Adjust(+10);
settingsForm.BeginInvoke(settingsForm.RunToast, "Brightness", ToastIcon.BrightnessUp);
break;
case 107: // FN+F10
AsusUSB.TouchpadToggle();
settingsForm.BeginInvoke(settingsForm.RunToast, "Touchpad", ToastIcon.Touchpad);
break;
case 108: // FN+F11
Application.SetSuspendState(PowerState.Suspend, true, true);
break;
}
}
static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{
if (e.NewEvent is null) return;
int EventID = int.Parse(e.NewEvent["EventID"].ToString());
Logger.WriteLine("WMI event " + EventID);
HandleEvent(EventID);
}
static void SettingsToggle(string action = "")
public static void SettingsToggle(string action = "")
{
if (settingsForm.Visible)
settingsForm.Hide();