mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Added windowprofile-conditions and lua-variables to check the currently active window
This commit is contained in:
parent
83d42084d4
commit
4c76153da2
@ -464,6 +464,7 @@
|
||||
<Compile Include="Profiles\Lua\Events\LuaDeviceDrawingEventArgs.cs" />
|
||||
<Compile Include="Profiles\Lua\Events\LuaDeviceUpdatingEventArgs.cs" />
|
||||
<Compile Include="Profiles\Lua\Brushes\LuaBrushWrapper.cs" />
|
||||
<Compile Include="Profiles\Lua\LuaActiveWindowWrapper.cs" />
|
||||
<Compile Include="Profiles\Lua\LuaDrawWrapper.cs" />
|
||||
<Compile Include="Profiles\Lua\Events\LuaEventsWrapper.cs" />
|
||||
<Compile Include="Profiles\Lua\LuaKeyboardWrapper.cs" />
|
||||
@ -566,6 +567,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
|
||||
@ -66,7 +68,7 @@ namespace Artemis.Modules.Effects.WindowsProfile
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region CPU
|
||||
|
||||
private void SetupCpu()
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
12
Artemis/Artemis/Profiles/Lua/LuaActiveWindowWrapper.cs
Normal file
12
Artemis/Artemis/Profiles/Lua/LuaActiveWindowWrapper.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Artemis.Utilities;
|
||||
using MoonSharp.Interpreter;
|
||||
|
||||
namespace Artemis.Profiles.Lua
|
||||
{
|
||||
[MoonSharpUserData]
|
||||
public class LuaActiveWindowWrapper
|
||||
{
|
||||
public string ProcessName => ActiveWindowHelper.ActiveWindowProcessName;
|
||||
public string WindowTitle => ActiveWindowHelper.ActiveWindowWindowTitle;
|
||||
}
|
||||
}
|
||||
@ -25,6 +25,7 @@ namespace Artemis.Profiles.Lua
|
||||
public static LuaKeyboardWrapper LuaKeyboardWrapper { get; private set; }
|
||||
public static LuaMouseWrapper LuaMouseWrapper { get; set; }
|
||||
public static LuaEventsWrapper LuaEventsWrapper { get; private set; }
|
||||
public static LuaActiveWindowWrapper LuaActiveWindowWrapper { get; private set; }
|
||||
|
||||
public static void SetupLua(ProfileModel profileModel, KeyboardProvider keyboardProvider)
|
||||
{
|
||||
@ -41,6 +42,7 @@ namespace Artemis.Profiles.Lua
|
||||
LuaKeyboardWrapper = new LuaKeyboardWrapper(keyboardProvider);
|
||||
LuaMouseWrapper = new LuaMouseWrapper();
|
||||
LuaEventsWrapper = new LuaEventsWrapper();
|
||||
LuaActiveWindowWrapper = new LuaActiveWindowWrapper();
|
||||
|
||||
LuaScript.Options.DebugPrint = LuaPrint;
|
||||
LuaScript.Globals["Profile"] = LuaProfileWrapper;
|
||||
@ -48,6 +50,7 @@ namespace Artemis.Profiles.Lua
|
||||
LuaScript.Globals["Brushes"] = LuaBrushWrapper;
|
||||
LuaScript.Globals["Keyboard"] = LuaKeyboardWrapper;
|
||||
LuaScript.Globals["Mouse"] = LuaMouseWrapper;
|
||||
LuaScript.Globals["ActiveWindow"] = LuaActiveWindowWrapper;
|
||||
|
||||
if (ProfileModel == null)
|
||||
return;
|
||||
@ -99,6 +102,7 @@ namespace Artemis.Profiles.Lua
|
||||
LuaKeyboardWrapper?.Dispose();
|
||||
LuaKeyboardWrapper = null;
|
||||
LuaMouseWrapper = null;
|
||||
LuaActiveWindowWrapper = null;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
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