forked from Codeusa/Borderless-Gaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBorderlessGaming.cs
266 lines (229 loc) · 9.02 KB
/
BorderlessGaming.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using BorderlessGaming.Common;
using BorderlessGaming.Utilities;
using BorderlessGaming.Forms;
using BorderlessGaming.WindowsAPI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BorderlessGaming
{
/// <summary>
/// Basically the Controller for the app, everything is just tightly coupled
/// </summary>
public class BorderlessGaming
{
public const string HiddenFile = "HiddenProcesses.json";
public const string FavoritesFile = "Favorites.json";
public static readonly string DataPath;
static BorderlessGaming()
{
DataPath = Tools.GetDataPath();
}
private readonly MainWindow window;
private readonly Favorites _favorites;
private readonly HiddenProcesses _hiddenProcesses;
private readonly ProcessDetailsList _processDetails;
private readonly Windows windows;
private CancellationTokenSource workerTaskToken;
public Favorites Favorites { get { return _favorites; } }
public HiddenProcesses HiddenProcesses { get { return _hiddenProcesses; } }
public ProcessDetailsList Processes { get { return _processDetails; } }
public bool AutoHandleFavorites { get; set; }
public BorderlessGaming(MainWindow window)
{
this.window = window;
_favorites = new Favorites(Path.Combine(DataPath, FavoritesFile));
_hiddenProcesses = new HiddenProcesses(Path.Combine(DataPath, HiddenFile));
_processDetails = new ProcessDetailsList();
windows = new Windows();
AutoHandleFavorites = true;
}
public void Start()
{
workerTaskToken = new CancellationTokenSource();
Task.Factory.StartNew(DoMainWork, workerTaskToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
/// <summary>
/// Update the processlist and process the favorites
/// Invoke this method in a task or it will block
/// </summary>
private void DoMainWork()
{
while (!workerTaskToken.IsCancellationRequested)
{
// update the processlist
UpdateProcesses();
if (AutoHandleFavorites)
{
// check favorites against the cache
foreach (var pd in _processDetails)
{
try
{
foreach (var favProcess in Favorites)
{
if (favProcess.Matches(pd))
{
RemoveBorder(pd, favProcess);
}
}
}
catch
{
continue;
}
}
}
Task.WaitAll(Task.Delay(20000));
}
}
private object updateLock = new object();
private void UpdateProcesses()
{
// Note: additional try/catch blocks were added here to prevent stalls when Windows is put into
// suspend or hibernation.
try
{
if (!AutoHandleFavorites)
{
MainWindow frm = MainWindow.ext();
if (frm != null)
if ((frm.WindowState == FormWindowState.Minimized) || (!frm.Visible))
return;
}
}
catch { } // swallow any exceptions in attempting to check window minimize/visibility state
lock (updateLock)
{
// check existing processes for changes (auto-prune)
for (int i = 0; i < _processDetails.Count;)
{
try
{
var pd = _processDetails[i];
var shouldBePruned = pd.ProcessHasExited;
if (!shouldBePruned)
{
var currentTitle = "";
if (!pd.NoAccess)
{
// 2 or 10 seconds until window title timeout, depending on slow-window detection mode
Tools.StartMethodMultithreadedAndWait(() => { currentTitle = Native.GetWindowTitle(pd.WindowHandle); }, (AppEnvironment.SettingValue("SlowWindowDetection", false)) ? 10 : 2);
shouldBePruned = shouldBePruned || (pd.WindowTitle != currentTitle);
}
}
if (shouldBePruned)
{
if (pd.MadeBorderless)
HandlePrunedProcess(pd);
_processDetails.RemoveAt(i);
}
else
i++;
}
catch
{
// swallow any exceptions and move to the next item in the array
i++;
}
}
// add new process windows
try
{
windows.QueryProcessesWithWindows((pd) =>
{
if (_hiddenProcesses.IsHidden(pd.Proc.ProcessName))
return;
if (!_processDetails.Select(p => p.Proc.Id).Contains(pd.Proc.Id) ||
!_processDetails.Select(p => p.WindowTitle).Contains(pd.WindowTitle))
_processDetails.Add(pd);
}, _processDetails.WindowPtrSet);
}
catch { } // swallow any exceptions in attempting to add new windows
// update window
window.lblUpdateStatus.Text = "Right-click for more options. Last updated " + DateTime.Now.ToString();
}
}
public Task RefreshProcesses()
{
lock (updateLock)
{
_processDetails.ClearProcesses();
}
return Task.Factory.StartNew(UpdateProcesses);
}
/// <summary>
/// Handle a removed process
/// </summary>
/// <param name="pd"></param>
private void HandlePrunedProcess(ProcessDetails pd)
{
// If we made this process borderless at some point, then check for a favorite that matches and undo
// some stuff to Windows.
foreach (var fav in _favorites)
{
if (fav.Matches(pd))
{
if (fav.HideWindowsTaskbar)
Manipulation.ToggleWindowsTaskbarVisibility(Tools.Boolstate.True);
if (fav.HideMouseCursor)
Manipulation.ToggleMouseCursorVisibility(window, Tools.Boolstate.True);
}
}
}
/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public void RemoveBorder(ProcessDetails pd, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
{
if(favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
{
//Wait 10 seconds before removing the border.
Task task = new Task(() => RemoveBorder(pd, favDetails, true));
task.Wait(TimeSpan.FromSeconds(10));
}
// If a Favorite screen exists, use the Rect from that, instead
if (null != favDetails && null != favDetails.favScreen)
{
RemoveBorder_ToSpecificRect(pd, favDetails.favScreen, favDetails, overrideTimeout);
return;
}
Manipulation.MakeWindowBorderless(pd, window, pd.WindowHandle, new Rectangle(), favDetails ?? _favorites.FromProcessDetails(pd));
}
/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public void RemoveBorder_ToSpecificScreen(IntPtr hWnd, Screen screen, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
{
if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
{
//Wait 10 seconds before removing the border.
Task task = new Task(() => RemoveBorder_ToSpecificScreen(hWnd, screen, favDetails, true));
task.Wait(TimeSpan.FromSeconds(10));
}
var pd = _processDetails.FromHandle(hWnd);
Manipulation.MakeWindowBorderless(pd, window, hWnd, screen.Bounds, favDetails ?? _favorites.FromProcessDetails(pd));
}
/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public void RemoveBorder_ToSpecificRect(IntPtr hWnd, Rectangle targetFrame, Favorites.Favorite favDetails = null, Boolean overrideTimeout = false)
{
if (favDetails != null && favDetails.DelayBorderless == true && overrideTimeout == false)
{
//Wait 10 seconds before removing the border.
Task task = new Task(() => RemoveBorder_ToSpecificRect(hWnd, targetFrame, favDetails, true));
task.Wait(TimeSpan.FromSeconds(10));
}
var pd = _processDetails.FromHandle(hWnd);
Manipulation.MakeWindowBorderless(pd, window, hWnd, targetFrame, favDetails ?? _favorites.FromProcessDetails(pd));
}
}
}