Skip to content

Commit

Permalink
Include Mute indicator, Thread fix
Browse files Browse the repository at this point in the history
  • Loading branch information
infra223 committed Dec 22, 2022
1 parent 4f25cc0 commit 4621108
Show file tree
Hide file tree
Showing 16 changed files with 456 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Inno Setup LogiLockLED/Setup LogiLockLED.iss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{37738065-07D8-4618-9F70-62F502D902BB}
AppName=LogiLockLED
AppVersion=1.4
AppVersion=1.5
;AppVerName=LogiLockLED 1.0
AppPublisherURL=https://github.com/infra223/LogiLockLED
AppSupportURL=https://github.com/infra223/LogiLockLED
Expand Down
12 changes: 12 additions & 0 deletions LogiLockLED/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
<setting name="LedController" serializeAs="String">
<value>OpenRGB</value>
</setting>
<setting name="MuteOnColour" serializeAs="String">
<value>Red</value>
</setting>
<setting name="MuteOffColour" serializeAs="String">
<value>White</value>
</setting>
<setting name="EnableMute" serializeAs="String">
<value>False</value>
</setting>
<setting name="MuteIndicatorKey" serializeAs="String">
<value>Mute</value>
</setting>
</LogiLockLED.Properties.Settings>
</userSettings>
</configuration>
351 changes: 232 additions & 119 deletions LogiLockLED/ConfigurationForm.Designer.cs

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion LogiLockLED/ConfigurationForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public ConfigurationForm(ref LedSettings settings)
cbOSDPosition.DataSource = Enum.GetValues(typeof(OSDPosition));
cbOSDPosition.SelectedIndex = 0;

cbMuteIndicatorKey.Items.Clear();
cbMuteIndicatorKey.Items.Add(LockKey.Mute);
cbMuteIndicatorKey.Items.Add(LockKey.PrtSc);
cbMuteIndicatorKey.Items.Add(LockKey.F12);
cbMuteIndicatorKey.Items.Add(LockKey.Num_Asterisk);
cbMuteIndicatorKey.SelectedIndex = 0;

PopulateSettingsToUI();
}

Expand Down Expand Up @@ -49,6 +56,11 @@ private void PopulateSettingsToUI()
btnScrollOffColour.BackColor = ledSettings.ScrollOffColor;
btnScrollOnColour.BackColor = ledSettings.ScrollOnColor;

cbEnableMute.Checked = ledSettings.EnableMute;
btnMuteOffColour.BackColor = ledSettings.MuteOffColor;
btnMuteOnColour.BackColor = ledSettings.MuteOnColor;
cbMuteIndicatorKey.SelectedItem = ledSettings.MuteIndicatorKey;

cbAutoStartApp.Checked = ledSettings.AutoStartApp;

cbOsdEnabled.Checked = ledSettings.OsdEnabled;
Expand Down Expand Up @@ -90,7 +102,9 @@ private void ApplySettings()
ledSettings.EnableCaps = cbEnableCaps.Checked;
ledSettings.EnableNum = cbEnableNum.Checked;
ledSettings.EnableScroll = cbEnableScroll.Checked;

ledSettings.EnableMute = cbEnableMute.Checked;

ledSettings.MuteIndicatorKey = (LockKey)cbMuteIndicatorKey.SelectedItem;

ledSettings.OsdEnabled = cbOsdEnabled.Checked;
ledSettings.OsdPosition = (OSDPosition)cbOSDPosition.SelectedItem;
Expand All @@ -103,6 +117,8 @@ private void ApplySettings()
ledSettings.CapsOnColor = btnCapsOnColour.BackColor;
ledSettings.NumOffColor = btnNumOffColour.BackColor;
ledSettings.NumOnColor = btnNumOnColour.BackColor;
ledSettings.MuteOffColor = btnMuteOffColour.BackColor;
ledSettings.MuteOnColor = btnMuteOnColour.BackColor;
ledSettings.ScrollOffColor = btnScrollOffColour.BackColor;
ledSettings.ScrollOnColor = btnScrollOnColour.BackColor;
ledSettings.OsdTextColor = btnOsdTxtColour.BackColor == Color.Black ? Color.FromArgb(3, 3, 3) : btnOsdTxtColour.BackColor;
Expand Down
4 changes: 2 additions & 2 deletions LogiLockLED/IndicatorPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ private void hideTimer_Tick(object sender, EventArgs e)
if (this.Visible)
{
lblLockText.Text = " ";
this.Hide();
hideTimer.Stop();
this.Hide();
}
hideTimer.Stop();
}

public void ShowLockState(LockKey lockKey, bool state)
Expand Down
2 changes: 1 addition & 1 deletion LogiLockLED/LedController/ILedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace LogiLockLED
{
public enum LockKey { Caps, Num, Scroll };
public enum LockKey { Caps, Num, Scroll, Mute, PrtSc, F12, Num_Asterisk};

interface ILedController
{
Expand Down
18 changes: 17 additions & 1 deletion LogiLockLED/LedController/LogitechSDKController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,28 @@ public bool SetLockKeyColor(LockKey key, System.Drawing.Color color)
keyCode = keyboardNames.SCROLL_LOCK;
break;

case LockKey.Mute:
keyCode = keyboardNames.NUM_ASTERISK;
break;

case LockKey.PrtSc:
keyCode = keyboardNames.PRINT_SCREEN;
break;

case LockKey.F12:
keyCode = keyboardNames.F12;
break;

case LockKey.Num_Asterisk:
keyCode = keyboardNames.NUM_ASTERISK;
break;

default:
return false;
}

return LogitechGSDK.LogiLedSetLightingForKeyWithKeyName(keyCode, color.R * 100 / 255, color.G * 100 / 255, color.B * 100 / 255);
}
}

}
}
22 changes: 19 additions & 3 deletions LogiLockLED/LedController/OpenRgbController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace LogiLockLED
{
public class OpenRgbController : ILedController, IDisposable
{
private OpenRGBClient _client;
private IOpenRGBClient _client;

public bool Initialise()
{
Expand Down Expand Up @@ -57,6 +57,22 @@ public bool SetLockKeyColor(LockKey key, System.Drawing.Color color)
keyName = "Key: Scroll Lock";
break;

case LockKey.Mute:
keyName = "Key: Media Mute";
break;

case LockKey.PrtSc:
keyName = "Key: Print Screen";
break;

case LockKey.F12:
keyName = "Key: F12";
break;

case LockKey.Num_Asterisk:
keyName = "Key: Number Pad *";
break;

}

var ledNumIndex = device.Leds.ToList().FindIndex(a => a.Name == keyName);
Expand All @@ -82,12 +98,12 @@ public bool SetLockKeyColor(LockKey key, System.Drawing.Color color)

public void Shutdown()
{
_client.Disconnect();
//_client.Disconnect();
}

public void Dispose()
{
_client.Dispose();
(_client as IDisposable)?.Dispose();
}
}
}
16 changes: 16 additions & 0 deletions LogiLockLED/LedSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ public class LedSettings
public bool EnableCaps { get; set; }
public bool EnableNum { get; set; }
public bool EnableScroll { get; set; }
public bool EnableMute { get; set; }
public LockKey MuteIndicatorKey{ get; set; }

public Color CapsOnColor { get; set; }
public Color CapsOffColor { get; set; }
public Color NumOnColor { get; set; }
public Color NumOffColor { get; set; }
public Color ScrollOnColor { get; set; }
public Color ScrollOffColor { get; set; }
public Color MuteOnColor { get; set; }
public Color MuteOffColor { get; set; }


public bool OsdEnabled { get; set; }
public Font OsdFont { get; set; }
Expand Down Expand Up @@ -56,13 +61,19 @@ public void LoadSettings()
EnableCaps = Properties.Settings.Default.EnableCaps;
EnableNum =Properties.Settings.Default.EnableNum;
EnableScroll = Properties.Settings.Default.EnableScroll;
EnableMute = Properties.Settings.Default.EnableMute;

LockKey muteIndicatorKey;
MuteIndicatorKey = (Enum.TryParse<LockKey>(Properties.Settings.Default.MuteIndicatorKey, out muteIndicatorKey) ? muteIndicatorKey : LockKey.Mute);

CapsOnColor = Properties.Settings.Default.CapsOnColour;
CapsOffColor = Properties.Settings.Default.CapsOffColour;
NumOnColor = Properties.Settings.Default.NumOnColour;
NumOffColor = Properties.Settings.Default.NumOffColour;
ScrollOnColor = Properties.Settings.Default.ScrollOnColour;
ScrollOffColor = Properties.Settings.Default.ScrollOffColour;
MuteOnColor = Properties.Settings.Default.MuteOnColour;
MuteOffColor = Properties.Settings.Default.MuteOffColour;

AutoStartApp = GetAutoStartSetting();
OsdEnabled = Properties.Settings.Default.OsdEnabled;
Expand Down Expand Up @@ -100,13 +111,18 @@ public void SaveSettings()
Properties.Settings.Default.EnableCaps = EnableCaps;
Properties.Settings.Default.EnableNum = EnableNum;
Properties.Settings.Default.EnableScroll = EnableScroll;
Properties.Settings.Default.EnableMute = EnableMute;

Properties.Settings.Default.MuteIndicatorKey = MuteIndicatorKey.ToString();

Properties.Settings.Default.CapsOnColour = CapsOnColor;
Properties.Settings.Default.CapsOffColour = CapsOffColor;
Properties.Settings.Default.NumOnColour = NumOnColor;
Properties.Settings.Default.NumOffColour = NumOffColor;
Properties.Settings.Default.ScrollOnColour = ScrollOnColor;
Properties.Settings.Default.ScrollOffColour = ScrollOffColor;
Properties.Settings.Default.MuteOnColour = MuteOnColor;
Properties.Settings.Default.MuteOffColour = MuteOffColor;

Properties.Settings.Default.OsdEnabled = OsdEnabled;
Properties.Settings.Default.OsdFont = OsdFont;
Expand Down
60 changes: 37 additions & 23 deletions LogiLockLED/LedThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using NAudio.CoreAudioApi;

namespace LogiLockLED
{
public class LedThread
public class LedThread : IDisposable
{
private MMDeviceEnumerator _mmDeviceEnumerator;

private LedSettings _ledSettings;
private static List<Thread> _threads = new List<Thread>();
private Thread _thread;
private bool _stopThread;
private bool _refreshRequired = false;
Expand All @@ -24,9 +28,11 @@ public class LedThread
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);

public LedThread(ref LedSettings settings)
{
public LedThread(LedSettings settings)
{
_ledSettings = settings;
_mmDeviceEnumerator = new MMDeviceEnumerator();

_refreshTimer = new System.Timers.Timer(1000);
_refreshTimer.Elapsed += refreshTimerEvent;
_refreshTimer.Start();
Expand All @@ -49,13 +55,16 @@ public void StartThread()

if (_ledSettings.EnableKeyLockLEDs)
{
_ledApiInit = _ledController.Initialise();


if (_thread == null || _thread.ThreadState != ThreadState.Running)
{
_ledApiInit = _ledController.Initialise();
_thread = new Thread(ThreadMain);
_thread.Name = "LED Update Thread";
_stopThread = false;
_thread.Start();
_threads.Add(_thread);
}
}
}
Expand All @@ -80,24 +89,7 @@ public void StopThread()
_ledApiInit = false;
}

}

public void UpdateSettings(LedSettings settings)
{
_ledSettings = settings;
if (_ledSettings.EnableKeyLockLEDs)
{
if (_thread == null || _thread.ThreadState != ThreadState.Running)
{
StartThread();
}
Refresh();
}
else
{
StopThread();
}
}
}

public void Refresh()
{
Expand All @@ -109,13 +101,17 @@ private void ThreadMain()
bool prevCapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) == 0;
bool prevNumLock = (((ushort)GetKeyState(0x90)) & 0xffff) == 0;
bool prevScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) == 0;
MMDevice _mmDevice = _mmDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
bool prevMuteState = _mmDevice.AudioEndpointVolume.Mute;

bool firstLoop = true;

while (!_stopThread)
{
bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;
bool MuteState = _mmDevice.AudioEndpointVolume.Mute;

if ((_refreshRequired || prevNumLock != NumLock))
{
Expand Down Expand Up @@ -159,6 +155,20 @@ private void ThreadMain()
}
}

if ((_refreshRequired || prevMuteState != MuteState))
{
prevMuteState = MuteState;

if (!firstLoop && !_refreshRequired)
KeylockUpdated?.Invoke(this, new KeylockChangeArgs(LockKey.Mute, MuteState));

if (_ledSettings.EnableMute)
{
var col = MuteState ? _ledSettings.MuteOnColor : _ledSettings.MuteOffColor;
setKeyColor(_ledSettings.MuteIndicatorKey, col, _refreshRequired);
}
}

_refreshRequired = false;
firstLoop = false;
Thread.Sleep(50);
Expand All @@ -178,7 +188,11 @@ private bool setKeyColor(LockKey key, System.Drawing.Color col, bool forceSet =
return _ledController.SetLockKeyColor(key, col);
}


public void Dispose()
{
_mmDeviceEnumerator.Dispose();
(_ledController as IDisposable)?.Dispose();
}
}


Expand Down
11 changes: 9 additions & 2 deletions LogiLockLED/LogiLockLED.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<AutorunEnabled>true</AutorunEnabled>
<ApplicationRevision>3</ApplicationRevision>
<ApplicationVersion>1.4.0.%2a</ApplicationVersion>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.5.1.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
Expand Down Expand Up @@ -78,6 +78,12 @@
<StartupObject>LogiLockLED.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="NAudio.Core, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Core.2.1.0\lib\netstandard2.0\NAudio.Core.dll</HintPath>
</Reference>
<Reference Include="NAudio.Wasapi, Version=2.1.0.0, Culture=neutral, PublicKeyToken=e279aa5131008a41, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Wasapi.2.1.0\lib\netstandard2.0\NAudio.Wasapi.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -127,6 +133,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="LogiLockLED_TemporaryKey.pfx" />
<None Include="packages.config" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
Loading

0 comments on commit 4621108

Please sign in to comment.