mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Merge pull request #239 from DarthAffe/development
Added new layer-contitions to check for the active window to resolve #219
This commit is contained in:
commit
4d69f3982b
@ -610,6 +610,7 @@
|
||||
<Compile Include="Settings\OverlaySettings.cs" />
|
||||
<Compile Include="Styles\DropTargetAdorners\DropTargetMetroHighlightAdorner.cs" />
|
||||
<Compile Include="Styles\DropTargetAdorners\DropTargetMetroInsertionAdorner.cs" />
|
||||
<Compile Include="Utilities\ActiveWindowHelper.cs" />
|
||||
<Compile Include="Utilities\ColorHelpers.cs" />
|
||||
<Compile Include="Utilities\Converters\JsonConverters.cs" />
|
||||
<Compile Include="Utilities\Converters\NinjectCustomConverter.cs" />
|
||||
|
||||
@ -90,11 +90,15 @@ namespace Artemis
|
||||
ContractResolver = _kernel.Get<NinjectContractResolver>()
|
||||
};
|
||||
JsonConvert.DefaultSettings = () => settings;
|
||||
|
||||
//TODO DarthAffe 17.12.2016: Is this the right location for this?
|
||||
ActiveWindowHelper.Initialize();
|
||||
}
|
||||
|
||||
protected override void OnExit(object sender, EventArgs e)
|
||||
{
|
||||
_kernel.Dispose();
|
||||
ActiveWindowHelper.Dispose();
|
||||
base.OnExit(sender, e);
|
||||
}
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ namespace Artemis.Modules.Effects.WindowsProfile
|
||||
Performance = new PerformanceDataModel();
|
||||
CurrentTime = new CurrentTime();
|
||||
Keyboard = new KbDataModel();
|
||||
ActiveWindow = new ActiveWindow();
|
||||
}
|
||||
|
||||
public CpuDataModel Cpu { get; set; }
|
||||
@ -22,6 +23,7 @@ namespace Artemis.Modules.Effects.WindowsProfile
|
||||
public GooglePlayMusic GooglePlayMusic { get; set; }
|
||||
public CurrentTime CurrentTime { get; set; }
|
||||
public KbDataModel Keyboard { get; set; }
|
||||
public ActiveWindow ActiveWindow { get; set; }
|
||||
}
|
||||
|
||||
[MoonSharpUserData]
|
||||
@ -110,4 +112,11 @@ namespace Artemis.Modules.Effects.WindowsProfile
|
||||
public bool CapsLock { get; set; }
|
||||
public bool ScrollLock { get; set; }
|
||||
}
|
||||
|
||||
[MoonSharpUserData]
|
||||
public class ActiveWindow
|
||||
{
|
||||
public string ProcessName { get; set; }
|
||||
public string WindowTitle { get; set; }
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ using Artemis.DAL;
|
||||
using Artemis.Managers;
|
||||
using Artemis.Models;
|
||||
using Artemis.Profiles.Layers.Models;
|
||||
using Artemis.Utilities;
|
||||
using Newtonsoft.Json;
|
||||
using SpotifyAPI.Local;
|
||||
|
||||
@ -52,6 +53,7 @@ namespace Artemis.Modules.Effects.WindowsProfile
|
||||
UpdateMusicPlayers(dataModel);
|
||||
UpdateDay(dataModel);
|
||||
UpdateKeyStates(dataModel);
|
||||
UpdateActiveWindow(dataModel);
|
||||
}
|
||||
|
||||
#region Current Time
|
||||
@ -249,6 +251,12 @@ namespace Artemis.Modules.Effects.WindowsProfile
|
||||
dataModel.Keyboard.ScrollLock = ((ushort)GetKeyState(0x91) & 0xffff) != 0;
|
||||
}
|
||||
|
||||
private void UpdateActiveWindow(WindowsProfileDataModel dataModel)
|
||||
{
|
||||
dataModel.ActiveWindow.ProcessName = ActiveWindowHelper.ActiveWindowProcessName;
|
||||
dataModel.ActiveWindow.WindowTitle = ActiveWindowHelper.ActiveWindowWindowTitle;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -70,6 +70,8 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight
|
||||
properties.AmbientLightBrush = new DrawingBrush(new ImageDrawing
|
||||
(BitmapSource.Create(width, height, 96, 96, ScreenCaptureManager.LastCapturePixelFormat, null, _lastData,
|
||||
stride), new Rect(0, 0, width, height)));
|
||||
|
||||
layerModel.ApplyProperties(true);
|
||||
}
|
||||
|
||||
public void Draw(LayerModel layerModel, DrawingContext c)
|
||||
|
||||
110
Artemis/Artemis/Utilities/ActiveWindowHelper.cs
Normal file
110
Artemis/Artemis/Utilities/ActiveWindowHelper.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Artemis.Utilities
|
||||
{
|
||||
public static class ActiveWindowHelper
|
||||
{
|
||||
#region DLL-Imports
|
||||
|
||||
private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constants
|
||||
|
||||
private const uint WINEVENT_OUTOFCONTEXT = 0;
|
||||
private const uint EVENT_SYSTEM_FOREGROUND = 3;
|
||||
|
||||
private const int MAX_TITLE_LENGTH = 256;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
// DarthAffe 17.12.2016: We need to keep a reference to this or it might get collected by the garbage collector and cause some random crashes afterwards.
|
||||
private static WinEventDelegate _activeWindowChangedDelegate;
|
||||
private static IntPtr _winEventHook;
|
||||
|
||||
public static string ActiveWindowProcessName { get; private set; }
|
||||
public static string ActiveWindowWindowTitle { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static void ActiveWindowChanged(IntPtr hWinEventHook, uint eventType,
|
||||
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
||||
{
|
||||
ActiveWindowProcessName = GetActiveWindowProcessName(hwnd) ?? string.Empty;
|
||||
ActiveWindowWindowTitle = GetActiveWindowTitle(hwnd) ?? string.Empty;
|
||||
}
|
||||
|
||||
private static string GetActiveWindowProcessName(IntPtr hwnd)
|
||||
{
|
||||
try
|
||||
{
|
||||
uint pid;
|
||||
GetWindowThreadProcessId(hwnd, out pid);
|
||||
return System.Diagnostics.Process.GetProcessById((int)pid).ProcessName;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetActiveWindowTitle(IntPtr hwnd)
|
||||
{
|
||||
try
|
||||
{
|
||||
StringBuilder buffer = new StringBuilder(MAX_TITLE_LENGTH);
|
||||
return GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH) > 0 ? buffer.ToString() : null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_winEventHook != IntPtr.Zero) return;
|
||||
|
||||
_activeWindowChangedDelegate = ActiveWindowChanged;
|
||||
_winEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _activeWindowChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
|
||||
}
|
||||
catch { /* catch'em all - I don't want it to crash here */ }
|
||||
}
|
||||
|
||||
public static void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_winEventHook == IntPtr.Zero) return;
|
||||
|
||||
UnhookWinEvent(_winEventHook);
|
||||
_activeWindowChangedDelegate = null;
|
||||
_winEventHook = IntPtr.Zero;
|
||||
}
|
||||
catch { /* catch'em all - I don't want it to crash here */ }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user