diff --git a/ColorControl/Config.cs b/ColorControl/Config.cs index d993a69..5e024f3 100644 --- a/ColorControl/Config.cs +++ b/ColorControl/Config.cs @@ -21,6 +21,8 @@ public class Config public int AmdPresetId_ApplyOnStartup { get; set; } + public bool FixChromeFonts { get; set; } + public Config() { DisplaySettingsDelay = 1000; @@ -29,6 +31,7 @@ public Config() FormHeight = 600; MinimizeToTray = true; CheckForUpdates = true; + FixChromeFonts = false; } } } diff --git a/ColorControl/LgService.cs b/ColorControl/LgService.cs index fca54fb..22c46c2 100644 --- a/ColorControl/LgService.cs +++ b/ColorControl/LgService.cs @@ -560,7 +560,7 @@ public async Task CheckProcesses() if (!applicableDevices.Any()) { - break; + continue; } if (_poweredOffByScreenSaver && !UserSessionInfo.UserLocalSession) diff --git a/ColorControl/MainForm.cs b/ColorControl/MainForm.cs index 425b417..b820d8c 100644 --- a/ColorControl/MainForm.cs +++ b/ColorControl/MainForm.cs @@ -96,6 +96,11 @@ public MainForm(AppContext appContext) chkFixChromeFonts.Enabled = Utils.IsChromeInstalled(); if (chkFixChromeFonts.Enabled) { + var fixInstalled = Utils.IsChromeFixInstalled(); + if (_config.FixChromeFonts && !fixInstalled) + { + Utils.ExecuteElevated(StartUpParams.ActivateChromeFontFixParam); + } chkFixChromeFonts.Checked = Utils.IsChromeFixInstalled(); } @@ -558,9 +563,8 @@ private void btnSetShortcut_Click(object sender, EventArgs e) { var shortcut = edtShortcut.Text.Trim(); - if (!string.IsNullOrWhiteSpace(shortcut) && !shortcut.Contains("+")) + if (!Utils.ValidateShortcut(shortcut)) { - MessageForms.WarningOk("Invalid shortcut. The shortcut should have modifiers and a normal key."); return; } @@ -1176,9 +1180,8 @@ private void btnApplyLg_Click(object sender, EventArgs e) private void btnSetShortcutLg_Click(object sender, EventArgs e) { var shortcut = edtShortcutLg.Text.Trim(); - if (!string.IsNullOrWhiteSpace(shortcut) && !shortcut.Contains("+")) + if (!Utils.ValidateShortcut(shortcut)) { - MessageForms.WarningOk("Invalid shortcut. The shortcut should have modifiers and a normal key."); return; } @@ -1891,6 +1894,7 @@ private void chkFixChromeFonts_CheckedChanged(object sender, EventArgs e) { if (_initialized) { + _config.FixChromeFonts = chkFixChromeFonts.Checked; if (chkFixChromeFonts.Checked) { Utils.ExecuteElevated(StartUpParams.ActivateChromeFontFixParam); @@ -1929,9 +1933,8 @@ private void btnSetShortcutScreenSaver_Click(object sender, EventArgs e) { var shortcut = edtBlankScreenSaverShortcut.Text.Trim(); - if (!string.IsNullOrWhiteSpace(shortcut) && !shortcut.Contains("+")) + if (!Utils.ValidateShortcut(shortcut)) { - MessageForms.WarningOk("Invalid shortcut. The shortcut should have modifiers and a normal key."); return; } @@ -2242,9 +2245,8 @@ private void btnSetAmdShortcut_Click(object sender, EventArgs e) { var shortcut = edtAmdShortcut.Text.Trim(); - if (!string.IsNullOrWhiteSpace(shortcut) && !shortcut.Contains("+")) + if (!Utils.ValidateShortcut(shortcut)) { - MessageForms.WarningOk("Invalid shortcut. The shortcut should have modifiers and a normal key."); return; } diff --git a/ColorControl/Utils.cs b/ColorControl/Utils.cs index e9847ba..ee59a16 100644 --- a/ColorControl/Utils.cs +++ b/ColorControl/Utils.cs @@ -68,7 +68,7 @@ public enum UserNotificationState : int public static bool ConsoleOpened { get; private set; } - [DllImport("user32.dll")] + [DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); @@ -108,6 +108,7 @@ public enum UserNotificationState : int private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); private static bool WinKeyDown = false; private static UserNotificationState LastNotificationState; + private static Keys[] KeysWithoutModifiers = new[] { Keys.F13, Keys.F14, Keys.F15, Keys.F16, Keys.F17, Keys.F18, Keys.F19, Keys.F20, Keys.F21, Keys.F22, Keys.F23, Keys.F24 }; public static Bitmap SubPixelShift(Bitmap bitmap) { @@ -527,7 +528,7 @@ public static bool TaskExists(string taskName, bool update) } } - public static void RegisterShortcut(IntPtr handle, int id, string shortcut, bool clear = false) + public static bool RegisterShortcut(IntPtr handle, int id, string shortcut, bool clear = false) { if (clear) { @@ -537,8 +538,17 @@ public static void RegisterShortcut(IntPtr handle, int id, string shortcut, bool if (!string.IsNullOrEmpty(shortcut)) { var (mods, key) = ParseShortcut(shortcut); - RegisterHotKey(handle, id, mods, key); + var result = RegisterHotKey(handle, id, mods, key); + if (!result) + { + var errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; + Logger.Error($"Could not register shortcut {shortcut}: {errorMessage}"); + } + + return result; } + + return true; } public static void BuildDropDownMenuEx(ContextMenuStrip mnuParent, string name, Type enumType, EventHandler clickEvent, object tag = null) @@ -671,6 +681,8 @@ public static string FormatKeyboardShortcut(KeyEventArgs keyEvent) //Debug.WriteLine("KD: " + e.Modifiers + ", " + e.KeyCode); + var keysWithoutModifiers = new[] { Keys.F12 }; + var shortcutString = (pressedModifiers > 0 ? pressedModifiers.ToString() : ""); if (keyEvent.KeyCode == Keys.LWin || WinKeyDown) { @@ -682,12 +694,17 @@ public static string FormatKeyboardShortcut(KeyEventArgs keyEvent) shortcutString += "Win"; } - if (!string.IsNullOrEmpty(shortcutString) && keyEvent.KeyCode != Keys.ControlKey && keyEvent.KeyCode != Keys.ShiftKey && keyEvent.KeyCode != Keys.Menu && keyEvent.KeyCode != Keys.LWin) + var empty = string.IsNullOrEmpty(shortcutString); + if ((!empty || keysWithoutModifiers.Contains(keyEvent.KeyCode)) && keyEvent.KeyCode != Keys.ControlKey && keyEvent.KeyCode != Keys.ShiftKey && keyEvent.KeyCode != Keys.Menu && keyEvent.KeyCode != Keys.LWin) { - shortcutString += " + " + keyEvent.KeyCode.ToString(); + if (!empty) + { + shortcutString += " + "; + } + shortcutString += keyEvent.KeyCode.ToString(); } - if (pressedModifiers == 0 && !WinKeyDown) + if (string.IsNullOrEmpty(shortcutString)) { keyEvent.SuppressKeyPress = true; } @@ -695,6 +712,18 @@ public static string FormatKeyboardShortcut(KeyEventArgs keyEvent) return shortcutString; } + public static bool ValidateShortcut(string shortcut) + { + var valid = string.IsNullOrWhiteSpace(shortcut) || shortcut.Contains("+") || shortcut.Contains("F12"); + + if (!valid) + { + MessageForms.WarningOk("Invalid shortcut. The shortcut should have modifiers and a normal key."); + } + + return valid; + } + public static void HandleKeyboardShortcutUp(KeyEventArgs keyEvent) { if (keyEvent.KeyCode == Keys.LWin) diff --git a/ColorControl/lgtv/LgTvConnection.cs b/ColorControl/lgtv/LgTvConnection.cs index ec672c1..41ea618 100644 --- a/ColorControl/lgtv/LgTvConnection.cs +++ b/ColorControl/lgtv/LgTvConnection.cs @@ -172,7 +172,17 @@ private void Connection_MessageReceived(MessageWebSocket sender, MessageWebSocke { try { - using (var dr = args.GetDataReader()) + var task = new Task(new Func(args.GetDataReader)); + task.Start(); + var result = task.Wait(5000); + + var dr = result ? task.Result : null; + if (!result) + { + throw new Exception("Timeout while reading response, possible disconnect"); + } + + using (dr) { dr.UnicodeEncoding = UnicodeEncoding.Utf8; var message = dr.ReadString(dr.UnconsumedBufferLength);