-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added WindowsInfo module with dimming and lock states
- Loading branch information
1 parent
fb050b8
commit 1db32ca
Showing
5 changed files
with
138 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Artemis.Core.Modules; | ||
|
||
namespace Artemis.MediaInfo.DataModels; | ||
|
||
public class WindowsInfoDataModel : DataModel | ||
{ | ||
[DataModelProperty(Name = "Session Is Locked")] | ||
public bool SessionLocked { get; set; } | ||
|
||
[DataModelProperty(Name = "Night Lights Enabled")] | ||
public bool NightLightsEnabled { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Collections.Generic; | ||
using System.Management; | ||
using System.Security.Principal; | ||
using Artemis.Core.Modules; | ||
using Artemis.MediaInfo.DataModels; | ||
using Microsoft.Win32; | ||
|
||
namespace Artemis.MediaInfo; | ||
|
||
public class WindowsInfoModule: Module<WindowsInfoDataModel> | ||
{ | ||
private const string BlueLightReductionStateKey = @"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CloudStore\\" + | ||
@"Store\\DefaultAccount\\Current\\default$windows.data.bluelightreduction.bluelightreductionstate\\" + | ||
@"windows.data.bluelightreduction.bluelightreductionstate"; | ||
|
||
private ManagementEventWatcher _nightLightStateWatcher; | ||
|
||
public override void Enable() | ||
{ | ||
StartListenNightLight(); | ||
UpdateNightLight(); | ||
StartListenLockState(); | ||
} | ||
|
||
public override void Disable() | ||
{ | ||
_nightLightStateWatcher.EventArrived -= _nightLightStateWatcher_Changed; | ||
_nightLightStateWatcher.Stop(); | ||
_nightLightStateWatcher.Dispose(); | ||
_nightLightStateWatcher = null; | ||
SystemEvents.SessionSwitch -= OnSessionSwitch; | ||
} | ||
|
||
public override void Update(double deltaTime) | ||
{ | ||
} | ||
|
||
public override List<IModuleActivationRequirement> ActivationRequirements { get; } = new(); | ||
|
||
private void StartListenLockState() | ||
{ | ||
SystemEvents.SessionSwitch += OnSessionSwitch; | ||
} | ||
|
||
private void OnSessionSwitch(object sender, SessionSwitchEventArgs e) | ||
{ | ||
DataModel.SessionLocked = e.Reason switch | ||
{ | ||
SessionSwitchReason.SessionLock => true, | ||
SessionSwitchReason.SessionUnlock => false, | ||
_ => DataModel.SessionLocked | ||
}; | ||
} | ||
|
||
private void StartListenNightLight() | ||
{ | ||
var currentUser = WindowsIdentity.GetCurrent(); | ||
ManagementScope scope = new ManagementScope("\\\\.\\root\\default"); | ||
var quertString = string.Format( | ||
"SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND KeyPath='{0}\\\\{1}' AND ValueName='{2}'", | ||
currentUser.User.Value, BlueLightReductionStateKey.Replace("\\", "\\\\"), "Data"); | ||
var query = new WqlEventQuery(quertString); | ||
_nightLightStateWatcher = new ManagementEventWatcher(scope, query); | ||
_nightLightStateWatcher.EventArrived += _nightLightStateWatcher_Changed; | ||
_nightLightStateWatcher.Start(); | ||
} | ||
|
||
private void _nightLightStateWatcher_Changed(object sender, EventArrivedEventArgs e) | ||
{ | ||
UpdateNightLight(); | ||
} | ||
|
||
private void UpdateNightLight() | ||
{ | ||
using var key = Registry.CurrentUser.OpenSubKey(BlueLightReductionStateKey); | ||
var data = key?.GetValue("Data"); | ||
if (data is null) | ||
{ | ||
DataModel.NightLightsEnabled = false; | ||
return; | ||
} | ||
|
||
var byteData = (byte[]) data; | ||
DataModel.NightLightsEnabled = byteData.Length > 24 && byteData[23] == 0x10 && byteData[24] == 0x00; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters