Skip to content

Commit

Permalink
fixed process monitor issue, improve shortcuts and Chrome fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Maassoft committed Aug 29, 2021
1 parent dd9ce93 commit b85d929
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ColorControl/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class Config

public int AmdPresetId_ApplyOnStartup { get; set; }

public bool FixChromeFonts { get; set; }

public Config()
{
DisplaySettingsDelay = 1000;
Expand All @@ -29,6 +31,7 @@ public Config()
FormHeight = 600;
MinimizeToTray = true;
CheckForUpdates = true;
FixChromeFonts = false;
}
}
}
2 changes: 1 addition & 1 deletion ColorControl/LgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public async Task CheckProcesses()

if (!applicableDevices.Any())
{
break;
continue;
}

if (_poweredOffByScreenSaver && !UserSessionInfo.UserLocalSession)
Expand Down
18 changes: 10 additions & 8 deletions ColorControl/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
41 changes: 35 additions & 6 deletions ColorControl/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -682,19 +694,36 @@ 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;
}

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)
Expand Down
12 changes: 11 additions & 1 deletion ColorControl/lgtv/LgTvConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,17 @@ private void Connection_MessageReceived(MessageWebSocket sender, MessageWebSocke
{
try
{
using (var dr = args.GetDataReader())
var task = new Task<DataReader>(new Func<DataReader>(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);
Expand Down

0 comments on commit b85d929

Please sign in to comment.