-
Notifications
You must be signed in to change notification settings - Fork 0
/
TVFoxApp.cs
478 lines (368 loc) · 19.3 KB
/
TVFoxApp.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// !! // TVFox - https://github.com/FoxCouncil/TVFox
// *.-". // MIT License
// | | // Copyright 2020 The Fox Council
using DirectShowLib;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using TVFox.Properties;
using TVFox.Windows;
using static TVFox.Enums;
namespace TVFox
{
public class TVFoxApp : ApplicationContext
{
public const string DEVICE_DETECTION_STRING_KEY = "Game Capture ";
private readonly Control _syncForm = new Control();
private readonly NotifyIcon _trayIcon;
private readonly Timer _timer;
private readonly AboutForm _aboutForm;
private ToolStripMenuItem _contextMenuSettings;
private ToolStripMenuItem _contextMenuShowHideWindow;
private ToolStripMenuItem _contextMenuFullscreenWindow;
private ToolStripMenuItem _contextMenuMute;
private ToolStripMenuItem _contextMenuAlwaysOnTop;
private ToolStripMenuItem _contextMenuBorderless;
private ToolStripMenuItem _contextMenuSignalDetection;
private ToolStripMenuItem _contextMenuVideoInfoData;
private ToolStripMenuItem _contextMenuDebug;
private ToolStripMenuItem _contextMenuDebugShowFps;
private ToolStripMenuItem _contextMenuSettingDimensionLock;
private ToolStripMenuItem _contextMenuSettingRatioLock;
private ToolStripMenuItem _contextMenuSettingRunOnStartup;
private ToolStripMenuItem _contextMenuSettingSourceDevice;
private ToolStripMenuItem _contextMenuSettingSourceFormat;
private ToolStripMenuItem _contextMenuSettingSourceFramerate;
private ToolStripMenuItem _contextMenuSettingSourceResolution;
public static Dictionary<Guid, string> MediaStubTypeDictionary { get; set; }
public TVFoxAppState CurrentState { get; private set; } = TVFoxAppState.FirstStart;
public VideoFormState CurrentWindowState { get; } = VideoFormState.FirstStart;
public static VideoForm VideoWindow { get; private set; }
public static DsDevice CurrentInputVideoDevice { get; set; }
public static DsDevice CurrentInputAudioDevice { get; set; }
public static IBaseFilter CurrentInputVideoSignalFilter { get; set; }
public static ContextMenuStrip ContextMenuStrip { get; private set; }
public TVFoxApp()
{
MediaStubTypeDictionary = new Dictionary<Guid, string>();
foreach (var subtype in typeof(MediaSubType).GetFields())
{
var value = (Guid)subtype.GetValue(null);
if (!MediaStubTypeDictionary.ContainsKey(value))
{
MediaStubTypeDictionary.Add(value, subtype.Name);
}
}
MediaStubTypeDictionary.Add(new Guid("{34363258-0000-0010-8000-00aa00389b71}"), "X264");
CurrentInputVideoSignalFilter = DirectShowHelper.CreateFilter(FilterCategory.VideoInputDevice, CurrentInputVideoDevice.Name);
SetupContextMenus();
_syncForm.Show();
_syncForm.CreateControl();
_trayIcon = new NotifyIcon { Text = Application.ProductName, Icon = Resources.TvFox, Visible = true, ContextMenuStrip = ContextMenuStrip };
_trayIcon.DoubleClick += (cSender, cArgs) => ToggleWindow();
_timer = new Timer { Interval = 50 };
_timer.Tick += (cSender, cArgs) => CheckSignalState();
_timer.Start();
_aboutForm = new AboutForm();
Application.ApplicationExit += (sender, args) =>
{
_trayIcon.Visible = false;
};
}
private void ToggleWindow()
{
if (VideoWindow != null)
{
if (VideoWindow.Visible)
{
VideoWindow.Hide();
}
else
{
VideoWindow.Show();
}
}
UpdateContextMenu();
}
private void SignalProcess()
{
var oldLocation = Settings.Default.WindowPosition;
var oldSize = Settings.Default.WindowSize;
VideoWindow = new VideoForm(CurrentInputVideoDevice, CurrentInputAudioDevice)
{
BackColor = Color.Black,
Icon = Resources.TvFox,
ShowInTaskbar = true,
Text = Application.ProductName,
KeyPreview = true,
};
VideoWindow.ChangeFormat(Settings.Default.Frametime);
VideoWindow.VisibleChanged += (sender, args) => {
if (VideoWindow.Visible)
{
Utilities.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED);
}
else
{
Utilities.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}
UpdateContextMenu();
};
VideoWindow.FullscreenChanged += () =>
{
UpdateContextMenu();
};
VideoWindow.MuteChanged += () =>
{
UpdateContextMenu();
};
VideoWindow.Show();
VideoWindow.FullscreenSet(Settings.Default.Fullscreen);
VideoWindow.Location = oldLocation;
VideoWindow.ClientSize = oldSize;
UpdateContextMenu();
}
private void SignalDispose()
{
if (VideoWindow != null)
{
if (!VideoWindow.IsDisposed)
{
VideoWindow.ShouldClose = true;
VideoWindow.Close();
}
VideoWindow = null;
}
UpdateContextMenu();
}
private void SetupContextMenus()
{
ContextMenuStrip = new ContextMenuStrip();
ContextMenuStrip.Items.Add(_contextMenuShowHideWindow = new ToolStripMenuItem { Text = "Show &Window", Enabled = false });
ContextMenuStrip.Items.Add("-");
ContextMenuStrip.Items.Add(_contextMenuFullscreenWindow = new ToolStripMenuItem { Text = "&Fullscreen", Enabled = false });
_contextMenuFullscreenWindow.Click += (sender, args) => VideoWindow?.ToggleFullscreen();
ContextMenuStrip.Items.Add(_contextMenuMute = new ToolStripMenuItem { Text = "&Mute", Enabled = false });
_contextMenuMute.Click += (sender, args) => VideoWindow?.ToggleMute();
ContextMenuStrip.Items.Add(_contextMenuAlwaysOnTop = new ToolStripMenuItem { Text = "Always On Top", Checked = Settings.Default.AlwaysOnTop });
_contextMenuAlwaysOnTop.Click += (sender, args) =>
{
Utilities.SetAlwaysOnTop(VideoWindow?.Handle, _contextMenuAlwaysOnTop.Checked = Settings.Default.AlwaysOnTop = !Settings.Default.AlwaysOnTop);
Settings.Default.Save();
};
ContextMenuStrip.Items.Add(_contextMenuBorderless = new ToolStripMenuItem { Text = "Borderless", Checked = VideoWindow?.FormBorderStyle == FormBorderStyle.None });
_contextMenuBorderless.Click += (sender, args) =>
{
if (VideoWindow?.FormBorderStyle == FormBorderStyle.None)
{
Settings.Default.BorderStyle = VideoWindow.FormBorderStyle = FormBorderStyle.Sizable;
}
else if (VideoWindow?.FormBorderStyle == FormBorderStyle.Sizable)
{
Settings.Default.BorderStyle = VideoWindow.FormBorderStyle = FormBorderStyle.None;
}
Settings.Default.Save();
_contextMenuBorderless.Checked = VideoWindow?.FormBorderStyle == FormBorderStyle.None;
};
ContextMenuStrip.Items.Add("-");
ContextMenuStrip.Items.Add(_contextMenuSettings = new ToolStripMenuItem { Text = "&Settings" });
ContextMenuStrip.Items.Add("-");
ContextMenuStrip.Items.Add(_contextMenuSignalDetection = new ToolStripMenuItem { Text = "No Signal Detected", Enabled = false });
ContextMenuStrip.Items.Add(_contextMenuVideoInfoData = new ToolStripMenuItem { Text = "N/A", Enabled = false });
ContextMenuStrip.Items.Add("-");
ContextMenuStrip.Items.Add("&About", null, (sender, args) => _aboutForm.ShowDialog());
ContextMenuStrip.Items.Add("E&xit", null, (cSender, cArgs) => Application.Exit());
ContextMenuStrip.Items[0].Click += (cSender, cArgs) => ToggleWindow();
ContextMenuStrip.VisibleChanged += (sender, args) =>
{
if (VideoWindow != null && VideoWindow.IsFullscreen)
{
Utilities.SetMouseVisibility(ContextMenuStrip.Visible);
}
};
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingSourceDevice = new ToolStripMenuItem { Enabled = false });
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingSourceResolution = new ToolStripMenuItem { Enabled = false });
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingSourceFramerate = new ToolStripMenuItem { Enabled = false });
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingSourceFormat = new ToolStripMenuItem { Enabled = false });
_contextMenuSettings.DropDownItems.Add("-");
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingDimensionLock = new ToolStripMenuItem { Text = "Source Dimension Lock", Checked = Settings.Default.SourceDemensionLock });
_contextMenuSettingDimensionLock.Click += (sender, args) =>
{
_contextMenuSettingDimensionLock.Checked = Settings.Default.SourceDemensionLock = !Settings.Default.SourceDemensionLock;
Settings.Default.Save();
VideoWindow?.HandleWindowResize();
};
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingRatioLock = new ToolStripMenuItem { Text = "Keep Source Ratio", Checked = Settings.Default.SourceRatioLock });
_contextMenuSettingRatioLock.Click += (sender, args) =>
{
_contextMenuSettingRatioLock.Checked = Settings.Default.SourceRatioLock = !Settings.Default.SourceRatioLock;
Settings.Default.Save();
VideoWindow?.HandleWindowResize();
};
_contextMenuSettings.DropDownItems.Add(_contextMenuSettingRunOnStartup = new ToolStripMenuItem { Text = "Run On Windows Startup", Checked = Settings.Default.RunOnStartup });
_contextMenuSettingRunOnStartup.Click += (sender, args) =>
{
_contextMenuSettingRunOnStartup.Checked = Settings.Default.RunOnStartup = !Settings.Default.RunOnStartup;
Settings.Default.Save();
RunOnStartupToggle();
};
_contextMenuSettings.DropDownItems.Add("-");
_contextMenuSettings.DropDownItems.Add(_contextMenuDebug = new ToolStripMenuItem { Text = "&Debug" });
_contextMenuDebug.DropDownItems.Add(_contextMenuDebugShowFps = new ToolStripMenuItem { Text = "Display FPS", Checked = Settings.Default.ShowFps });
_contextMenuDebugShowFps.Click += (sender, args) =>
{
_contextMenuDebugShowFps.Checked = Settings.Default.ShowFps = !Settings.Default.ShowFps;
Settings.Default.Save();
VideoWindow.overlayTopLeft.Visible = Settings.Default.ShowFps;
};
}
private void UpdateContextMenu()
{
var hasSignal = CurrentState == TVFoxAppState.Signal;
var formVisible = (VideoWindow?.Visible).GetValueOrDefault(false);
var sourceSizeStr = $"{VideoWindow?.SourceSize.Width}x{VideoWindow?.SourceSize.Height} ({VideoWindow?.SourceAspectRatio})";
_contextMenuShowHideWindow.Text = formVisible ? "Hide Window" : "Show Window";
_contextMenuShowHideWindow.Enabled = hasSignal;
_contextMenuFullscreenWindow.Checked = (VideoWindow?.IsFullscreen).GetValueOrDefault(false);
_contextMenuFullscreenWindow.Enabled = hasSignal && formVisible;
_contextMenuMute.Checked = (VideoWindow?.IsMuted).GetValueOrDefault(false);
_contextMenuMute.Enabled = hasSignal && formVisible;
_contextMenuBorderless.Enabled = _contextMenuAlwaysOnTop.Enabled = (VideoWindow?.Visible).GetValueOrDefault(false) && !(VideoWindow?.IsFullscreen).GetValueOrDefault(false);
_contextMenuSettingSourceDevice.Enabled = false;
_contextMenuSettingSourceDevice.Text = $"Device ({CurrentInputVideoDevice.Name})";
_contextMenuSettingSourceResolution.Enabled = false; // hasSignal;
_contextMenuSettingSourceResolution.Text = hasSignal ? $"Resolution ({sourceSizeStr})" : "Resolution";
_contextMenuSettingSourceResolution.DropDownItems.Clear();
_contextMenuSettingSourceFramerate.Enabled = false; // hasSignal;
_contextMenuSettingSourceFramerate.Text = hasSignal ? $"Framerate ({VideoWindow?.SourceFramerate:F} fps)" : "Framerate";
_contextMenuSettingSourceFramerate.DropDownItems.Clear();
_contextMenuSettingSourceFormat.Enabled = false; // hasSignal;
_contextMenuSettingSourceFormat.Text = hasSignal ? $"Format ({VideoWindow?.SourceFormat})" : "Format";
_contextMenuSettingSourceFormat.DropDownItems.Clear();
_contextMenuDebug.Enabled = hasSignal;
if (hasSignal && VideoWindow != null)
{
foreach (var format in VideoWindow.SupportedFormats.Keys)
{
_contextMenuSettingSourceFormat.DropDownItems.Add(new ToolStripMenuItem { Text = format, Checked = format == VideoWindow.SourceFormat, Enabled = false });
}
var currentFormatSupported = VideoWindow.SupportedFormats[VideoWindow.SourceFormat];
var foundSizes = new HashSet<Size>();
foreach (var subFormat in currentFormatSupported)
{
if (foundSizes.Contains(subFormat.Item1))
{
continue;
}
_contextMenuSettingSourceResolution.DropDownItems.Add(new ToolStripMenuItem { Text = $"{subFormat.Item1.Width}x{subFormat.Item1.Height}", Checked = subFormat.Item1 == VideoWindow.SourceSize });
foundSizes.Add(subFormat.Item1);
}
if (_contextMenuSettingSourceResolution.DropDownItems.Count == 1)
{
_contextMenuSettingSourceResolution.DropDownItems.Clear();
_contextMenuSettingSourceResolution.Enabled = false;
}
var index = 0;
foreach (var frameRate in ValueConstants.FrameRates)
{
var frameRateOption = new ToolStripMenuItem { Text = $"{frameRate:F} fps", Checked = Equals(frameRate, VideoWindow.SourceFramerate), Tag = ValueConstants.FrameTimes[index] };
frameRateOption.Click += (sender, args) => VideoWindow.ChangeFormat((float)((ToolStripMenuItem)sender).Tag);
_contextMenuSettingSourceFramerate.DropDownItems.Add(frameRateOption);
index++;
}
}
_contextMenuSignalDetection.Text = hasSignal ? "Signal Detected:" : "No Signal Detected";
var videoStateString = VideoWindow == null ? "NO SIGNAL" : $"{sourceSizeStr} {VideoWindow.SourceFramerate:F0}fps";
_contextMenuVideoInfoData.Text = hasSignal && VideoWindow != null ? videoStateString : "N/A";
if (VideoWindow != null)
{
VideoWindow.Text = $"TVFox - {videoStateString} - https://github.com/FoxCouncil/TVFox";
}
RunOnStartupCheck();
}
private void CheckSignalState()
{
var videoDecoderInterface = CurrentInputVideoSignalFilter as IAMAnalogVideoDecoder;
var signalDetected = false;
videoDecoderInterface?.get_HorizontalLocked(out signalDetected);
if (signalDetected && CurrentState != TVFoxAppState.Signal)
{
CurrentState = TVFoxAppState.Signal;
SignalProcess();
}
else if (!signalDetected && CurrentState != TVFoxAppState.NoSignal)
{
CurrentState = TVFoxAppState.NoSignal;
SignalDispose();
}
}
private void RunOnStartupCheck()
{
var runOnStartupList = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
var value = (string) runOnStartupList?.GetValue(Application.ProductName);
if (value != null && value == Application.ExecutablePath)
{
Settings.Default.RunOnStartup = true;
}
else
{
Settings.Default.RunOnStartup = false;
}
Settings.Default.Save();
_contextMenuSettingRunOnStartup.Checked = Settings.Default.RunOnStartup;
}
private static void RunOnStartupToggle()
{
var runOnStartupList = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (Settings.Default.RunOnStartup)
{
runOnStartupList?.SetValue(Application.ProductName, Application.ExecutablePath.Replace(".dll", ".exe"));
}
else
{
runOnStartupList?.DeleteValue(Application.ProductName);
}
}
private static bool FindHardware()
{
var videoDeviceList = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (videoDeviceList.Length == 0)
{
return false;
}
CurrentInputVideoDevice = videoDeviceList.FirstOrDefault(cDevice => cDevice.Name.StartsWith(DEVICE_DETECTION_STRING_KEY));
if (CurrentInputVideoDevice == null)
{
return false;
}
var audioDeviceList = DsDevice.GetDevicesOfCat(FilterCategory.AudioInputDevice);
if (audioDeviceList.Length == 0)
{
return false;
}
CurrentInputAudioDevice = audioDeviceList.FirstOrDefault(cDevice => cDevice.Name.StartsWith(DEVICE_DETECTION_STRING_KEY));
if (CurrentInputAudioDevice == null)
{
return false;
}
return true;
}
[STAThread]
private static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!FindHardware())
{
MessageBox.Show("Could not find compatible hardware. Please try again.", "TvFox Error");
return;
}
Application.Run(new TVFoxApp());
}
}
}