-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlimeVR.cs
157 lines (134 loc) · 5.3 KB
/
SlimeVR.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using VRDeclutter.Interop;
namespace VRDeclutter
{
public class SlimeVR
{
private readonly MainWindow _mainWindow;
private readonly Thread _watcherThread;
public bool HidingEnabled = true;
public bool IsHidden;
public bool IsWatching;
public Process Process;
public List<IntPtr> WindowHandles = new();
public SlimeVR(MainWindow mainWindow)
{
_mainWindow = mainWindow;
_watcherThread = new Thread(WatcherWork) { IsBackground = true };
_watcherThread.SetApartmentState(ApartmentState.STA);
new Thread(delegate(object? o)
{
AttachToSlimeVR(); // Wait for SlimeVR to be open
}).Start();
}
public void AttachToSlimeVR()
{
// Wait for at least one process to appear
while (!GetWindowHandles(out WindowHandles)) Thread.Sleep(1000); // CPU usage "optimization"
// Get Java Process from Window Handle
foreach (var windowHandle in WindowHandles)
{
NativeMethods.GetWindowThreadProcessId(windowHandle, out uint windowProcessId);
Process process = Process.GetProcessById((int)windowProcessId);
if (process.MainModule != null && process.MainModule.ModuleName == "java.exe")
{
Process = process;
break;
}
}
Process.EnableRaisingEvents = true;
Process.Exited += OnProcessExited;
// Start Watcher
IsWatching = true;
if (_watcherThread.IsAlive == false)
_watcherThread.Start();
}
// Reset window attachment
private void OnProcessExited(object? sender, EventArgs e)
{
IsWatching = false;
HidingEnabled = true;
new Thread(delegate(object? o)
{
AttachToSlimeVR(); // Wait for SlimeVR to be open
}).Start();
}
public void ShowWindows()
{
if (IsHidden)
{
foreach (var windowHandle in WindowHandles)
{
if (NativeMethods.IsWindowMinimized(windowHandle))
// Hide SlimeVR Window
NativeMethods.ShowNormalWindow(windowHandle);
Thread.Sleep(100); // So Windows UI doesn't glitch
}
IsHidden = false;
}
}
public void HideWindows()
{
if (!IsHidden)
{
foreach (var windowHandle in WindowHandles)
// Hide SlimeVR Window
NativeMethods.HideNormalWindow(windowHandle);
IsHidden = true;
}
}
public bool GetWindowHandles(out List<IntPtr> intPtr)
{
intPtr = new List<IntPtr>();
List<string> windowTitles = new List<string>();
foreach (IntPtr window in NativeMethods.GetWindows()) // Get all windows
{
int titleLength = NativeMethods.GetWindowTextLength(window); // Get window title length
StringBuilder buffer = new StringBuilder(titleLength);
NativeMethods.GetWindowText(window, buffer, titleLength + 1); // Get full window title
string className = NativeMethods.GetClassName(window); // Get class name
string windowTitle = buffer.ToString();
if (!string.IsNullOrWhiteSpace(windowTitle))
windowTitles.Add(windowTitle);
if (buffer.ToString().StartsWith("SlimeVR Server") &&
(className == "SunAwtFrame") | (className == "ConsoleWindowClass")) intPtr.Add(window);
}
if (intPtr.Count > 1)
return true;
return false;
}
private void WatcherWork()
{
while (true)
{
if (IsWatching)
{
// If hide on minimize is enabled
if (_mainWindow.Settings.SlimeVrActionSetting == Settings.ActionSetting.MinimizeHide)
// Check for window state
foreach (var windowHandle in WindowHandles)
if (NativeMethods.IsWindowMinimized(windowHandle))
{
// Hide SlimeVR Window
NativeMethods.HideNormalWindow(windowHandle);
IsHidden = true;
}
// If autohide is enabled
if (_mainWindow.Settings.SlimeVrActionSetting == Settings.ActionSetting.AutoHide && HidingEnabled)
foreach (var windowHandle in WindowHandles)
{
// Hide SlimeVR Window
NativeMethods.HideNormalWindow(windowHandle);
IsHidden = true;
}
}
// CPU Usage "optimization"
Thread.Sleep(1);
}
}
}
}