-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuickAccessMenu.xaml.cs
671 lines (519 loc) · 21.6 KB
/
QuickAccessMenu.xaml.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
using Handheld_Hardware_Tools.Models;
using System;
using System.Windows;
using Handheld_Hardware_Tools.Classes;
using Handheld_Hardware_Tools.Classes.Controller_Object_Classes;
using Microsoft.Win32;
using System.Windows.Threading;
using Handheld_Hardware_Tools.Classes.Models;
using System.Globalization;
using System.Net.NetworkInformation;
using Wpf.Ui.Controls;
using Handheld_Hardware_Tools.Classes.Devices;
using System.Diagnostics;
using Handheld_Hardware_Tools.Classes.Wifi_AP;
using System.IO;
using Handheld_Hardware_Tools.ControllerInputPages;
using Wpf.Ui;
using System.Windows.Markup;
using System.Xml;
using Handheld_Hardware_Tools.Classes.Profiles;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Handheld_Hardware_Tools.AppWindows.WindowManager;
using Handheld_Hardware_Tools.Classes.MouseMode;
using WindowsInput;
using Handheld_Hardware_Tools.AppWindows.Guide;
using Handheld_Hardware_Tools.AppWindows.QuickActionWheel;
using Handheld_Hardware_Tools.AppWindows.MainWindow;
using System.Threading;
using Handheld_Hardware_Tools.AppWindows.OSK;
using System.Linq;
using Handheld_Hardware_Tools.AppWindows.AyaNeoFlipDSApp;
using System.Reflection.Metadata;
using System.Windows.Media;
using System.Drawing;
using System.Windows.Media.Imaging;
using Handheld_Hardware_Tools.Classes.Motherboard_Info;
using System.Threading.Tasks;
namespace Handheld_Hardware_Tools
{
public partial class QuickAccessMenu : ControllerWindow
{
public MouseMode mouseMode;
public InputSimulator inputSimulator = new InputSimulator();
public WiFiDirectHotspotManager wifiAP = null;
public ProfileManager profileManager = new ProfileManager();
public MouseKeyHook mouseKeyHook = new MouseKeyHook();
public QuickAccessMenu()
{
//subscribe mouse key events
mouseKeyHook.Subscribe();
InitializeComponent();
//run start up routines
Application.Current.Dispatcher.BeginInvoke(new Action(() => InitializeRoutines()));
//set your common items between windows (this has to do with the ControllerWindow custom class)
commonFrame = frame;
instructionFrame = frameControllerInput;
//These are for a future feature of custom formats
//FileStream xamlFile = new FileStream("Styles\\NewTheme.xaml", FileMode.Open, FileAccess.Read);
//Application.Current.Resources.MergedDictionaries.Add(((ResourceDictionary)XamlReader.Load(xamlFile)));
string picLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Styles", Application.Current.Resources["SplashWindowLogo"].ToString());
//
MainWindowLogo.Source = new BitmapImage(new Uri(picLocation));
}
public void CloseMouseMode()
{//close mouse mode, make sure to unsubscribe to controller events
if (mouseMode != null)
{
mouseMode.UnsubscribeControllerEvents();
mouseMode = null;
}
}
public void ToggleMouseMode()
{
if (mouseMode != null)
{
mouseMode.UnsubscribeControllerEvents();
mouseMode = null;
}
else
{
mouseMode = new MouseMode();
}
}
#region Set up
private void InitializeRoutines()
{
//Write log to tell app is open
Task.Run(() => Log_Writer.Instance.writeLog("Start Main Window"));
Task.Run(() => Powercfg_Management.Instance.UnhidePowerCfgSettings());
//set nav menu items from model
SetNavigationMenuItemSource();
//subscribe to events
SubscribeEvents();
//update status bar values and start dispatcher timer for statusbar
SetUpStatusBarStartDispatcherTimer();
}
public void SetNavigationMenuItemSource()
{
navigationViewListBox.ItemsSource = new NavigationViewMenuItems();
navigationViewListBox.Items.Refresh();
}
private void SubscribeEvents()
{
SubscribeControllerEvents();
//subscribe to 333333 changed (to update status bar)
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
}
public void ChangeAppLocation(bool qamRight)
{
if (qamRight)
{
System.Windows.Forms.Screen windowScreen = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(this).Handle);
double scaling = GetDPIScaling() / 100;
double leftPosition = Math.Round(windowScreen.Bounds.Width / (scaling), 0) - this.Width;
this.Left = leftPosition;
}
else
{
this.Left = 0;
}
}
public double GetDPIScaling()
{
return Math.Round(VisualTreeHelper.GetDpi(this).DpiScaleX * 100, 0);
}
public void SetAppLocationHeight()
{
//Things to consider setting location and height
//DPI scaling (need monitor resolution AND scaling to calculate height and left
//need setting preference
this.Top = 0;
Settings settings = (Settings)XML_Management.Instance.LoadXML("Settings");
System.Windows.Forms.Screen windowScreen = System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(this).Handle);
this.Width = 580;
this.MaxWidth = 580;
this.MinWidth = 580;
double scaling = GetDPIScaling()/100;
double leftPosition = Math.Round(windowScreen.Bounds.Width / (scaling),0)-this.Width;
double height = Math.Round(windowScreen.WorkingArea.Height / scaling, 0);
this.MinHeight = height;
this.MaxHeight = height;
if (settings.qamOnRightSide)
{
this.Left = leftPosition;
}
else
{
this.Left = 0;
}
}
#endregion
#region status bar updates
private DispatcherTimer statusBarDispatcherTimer = new DispatcherTimer();
private void SetUpStatusBarStartDispatcherTimer()
{
UpdateTime();
UpdatePowerStatusBar();
UpdateNetworkStatus();
UpdateControllerStatus();
statusBarDispatcherTimer.Interval = new TimeSpan(0, 0, 5);
statusBarDispatcherTimer.Tick += StatusBarDispatcherTimer_Tick;
statusBarDispatcherTimer.Start();
}
private void StatusBarDispatcherTimer_Tick(object? sender, EventArgs e)
{
//Tick for updating status bar. Separate from updatestatusbar routine so it can be updated independent of the timer
UpdateStatusBar();
}
private void UpdateStatusBar()
{
UpdateTime();
UpdatePowerStatusBar();
UpdateNetworkStatus();
UpdateControllerStatus();
}
private void UpdateControllerStatus()
{
if (controllerInput == null)
{
controllerStatusBarIcon.Visibility = Visibility.Collapsed;
return;
}
else
{
if (controllerInput.controller == null)
{
controllerStatusBarIcon.Visibility = Visibility.Collapsed;
return;
}
else
{
if (controllerInput.controller.IsConnected)
{
if (controllerStatusBarIcon.Visibility == Visibility.Collapsed)
{
controllerStatusBarIcon.Visibility = Visibility.Visible;
return;
}
}
else
{
if (controllerStatusBarIcon.Visibility == Visibility.Visible)
{
controllerStatusBarIcon.Visibility = Visibility.Collapsed;
return;
}
}
}
}
}
private void UpdateNetworkStatus()
{
//Gets internet status to display on overlay
NetworkInterface[] networkCards = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkCard in networkCards)
{
if (networkCard.OperationalStatus == OperationalStatus.Up)
{
if (networkCard.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
if (noInternetStatusBarIcon.Visibility == Visibility.Visible) { noInternetStatusBarIcon.Visibility = Visibility.Collapsed; }
return;
}
if (networkCard.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
if (wifiStatusBarIcon.Visibility == Visibility.Collapsed)
{
wifiStatusBarIcon.Visibility = Visibility.Visible;
}
if (noInternetStatusBarIcon.Visibility == Visibility.Visible) { noInternetStatusBarIcon.Visibility = Visibility.Collapsed; }
return;
}
}
}
noInternetStatusBarIcon.Visibility = Visibility.Visible;
if (wifiStatusBarIcon.Visibility == Visibility.Visible)
{
wifiStatusBarIcon.Visibility = Visibility.Collapsed;
}
}
private void UpdateTime()
{
timeStatusBar.Text = DateTime.Now.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
}
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Suspend:
Log_Writer.Instance.writeLog("about to sleep!");
break;
case PowerModes.Resume:
break;
case PowerModes.StatusChange:
UpdatePowerStatusBar();
break;
}
}
private void UpdatePowerStatusBar()
{
try
{
Classes.Models.PowerStatus ps = new Classes.Models.PowerStatus();
if (ps.powerStatus == "AC")
{
viewBoxBatteryPercentage.Visibility = Visibility.Collapsed;
batteryStatusBarIcon.Visibility = Visibility.Collapsed;
chargingStatusBarIcon.Visibility = Visibility.Collapsed;
}
else
{
if (ps.powerStatus == "Online")
{
batteryStatusBarIcon.Visibility = Visibility.Collapsed;
chargingStatusBarIcon.Visibility = Visibility.Visible;
}
else
{
batteryStatusBarIcon.Visibility = Visibility.Visible;
chargingStatusBarIcon.Visibility = Visibility.Collapsed;
}
viewBoxBatteryPercentage.Visibility = Visibility.Visible;
batteryPercentageStatusBar.Text = ps.batteryLevel+"%";
}
}
catch(Exception ex)
{
}
}
#endregion
#region Controller Navigation
public ControllerInput controllerInput = new ControllerInput();
public override void HandleControllerInputTopLevel(object? sender, controllerInputEventArgs e)
{
if(this.Visibility == Visibility.Visible)
{
//THIS IS NEW CODE, I WANT TO USE JOYSTICK ONLY ON CERTAIN OBJECTS, for this window i want the newly added
//joystick inputs with a string of Joystick_DPadLeft etc etc... to work as normal dpad inputs so im going to convert it at the object level back to DPadLeft
string action = e.Action;
//this replaces joystick identifier to make joystick to dpad work
if (action.Contains("Joystick_")) { action = action.Replace("Joystick_", ""); }
if (!controllerNavigateWindow)
{//if not navigating at the window level pass input to page level
ControllerPage a = (ControllerPage)frame.Content;
a.HandleControllerInput(action);
}
else
{//send input to window handler
HandleControllerInput(action);
}
}
}
private void HandleControllerInput(string action)
{
switch(action)
{
case "B":
ToggleWindow();
break;
case "DPadUp" or "DPadDown":
General_Functions.NavigateListView(navigationViewListBox, action);
break;
case "A" or "DPadRight":
SetControllerNavigateWindow(false);
break;
default: break;
}
}
#endregion
#region ClosingEvents
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//unsubscribe to all events to make sure it doesn't keep the app open
UnsubscribeEvents();
//stop dispatcher timer for status bar
statusBarDispatcherTimer.Stop();
//Write log to tell app is closing
Log_Writer.Instance.writeLog("App closing");
}
private void UnsubscribeEvents()
{
//unsubscribe to controller input
controllerInput.buttonPressEvent.controllerInputEvent -= HandleControllerInputTopLevel;
//unsubscribe to power changed (to update status bar)
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
}
#endregion
private void navigationViewListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
LoadPageInFrame();
}
private void LoadPageInFrame()
{
//loads page in frame when navigation menu item changes
if (navigationViewListBox.SelectedItem != null)
{
Tuple<Wpf.Ui.Common.SymbolRegular, string> tuple = (Tuple<Wpf.Ui.Common.SymbolRegular, string>)navigationViewListBox.SelectedItem;
frame.Content = General_Functions.TranslateIconToPageURI(tuple.Item1);
}
}
public void LoadPageInFrameExternal(Uri uri)
{
frame.Source = uri;
}
#region LoadedEvent
private void UiWindow_Loaded(object sender, RoutedEventArgs e)
{
//set location
}
#endregion
#region ToggleWindow
private void TasksToggleWindowOpen()
{
//Tasks to do when window is reopening
//update status bar because timer takes 5 seconds to update
UpdateStatusBar();
//Resume statusbar update timer
statusBarDispatcherTimer.Start();
//set location height
SetAppLocationHeight();
//Reload last page
LoadPageInFrame();
//determine if a full screen game is running, if so we will open the app as NON-FOCUSABLE. this makes it so that some games won't minimize
DetermineAppFocusOnFullScreenGame();
if (this.WindowState == WindowState.Minimized)
{
this.WindowState = WindowState.Normal;
}
//set app to normal state and visible
this.Visibility = Visibility.Visible;
//this.Show();
}
Process gameProcess = null;
private void DetermineAppFocusOnFullScreenGame()
{
//this code checks for full screen games
List<Process> listProcesses = new List<Process>();
Process[] pList = Process.GetProcesses();
foreach (Process p in pList)
{
if (p.MainWindowHandle != IntPtr.Zero)
{
if (ScreenProgram_Management.IsForegroundFullScreen(new HandleRef(null, p.MainWindowHandle), null) && !ScreenProgram_Management.ExcludeFullScreenProcessList.Contains(p.ProcessName))
{
//this is where we will make app non focusable
Debug.WriteLine(p.ProcessName);
gameProcess = p;
SetWindowAsNonFocusable(p.MainWindowHandle);
//SetWindowAsFocusable();
return;
}
}
}
//if no full screen game is found we end up here
SetWindowAsFocusable();
}
private void TasksToggleWindowClosed()
{
//Tasks to do when window is hiding
HandleNonNullGameProcess();
//Stop statusbar update timer
statusBarDispatcherTimer.Stop();
//unload page from frame so it needs to be reloaded
frame.Source = null;
//set app to hidden
this.Visibility = Visibility.Hidden;
}
private void HandleNonNullGameProcess()
{
//this handles if a full window app was running when the app was opened, we can check if the window is not open and reopen it for us
if (gameProcess != null)
{
if (!ScreenProgram_Management.IsForegroundFullScreen(new HandleRef(null, gameProcess.MainWindowHandle), null))
{
//the above checks if the app is still fullscreen, if not (i.e. using !) then we get here
ScreenProgram_Management.SetWindowState(gameProcess.MainWindowHandle, WindowState.Normal);
}
gameProcess = null;
}
}
public void ToggleWindow()
{
if (this.Visibility == Visibility.Hidden || this.WindowState == WindowState.Minimized)
{
TasksToggleWindowOpen();
}
else
{
TasksToggleWindowClosed();
}
}
#region making the app non focusable
//used in non focus app
private const int GWL_EXSTYLE = -20;
private const int WS_EX_NOACTIVATE = 0x08000000;
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
//used to keep track of
private IntPtr gameHandle = IntPtr.Zero;
private void SetWindowAsNonFocusable(IntPtr game)
{
//set app as non focusable
var helper = new WindowInteropHelper(this);
SetForegroundWindow(helper.Handle);
//SetWindowLong(helper.Handle, GWL_EXSTYLE, GetWindowLong(helper.Handle, GWL_EXSTYLE) | WS_EX_NOACTIVATE);
//gameHandle = game;
//set game as non focusable to see if it stops controller input
//SetWindowLong(game, GWL_EXSTYLE, GetWindowLong(game, GWL_EXSTYLE) | WS_EX_NOACTIVATE);
}
private void SetWindowAsFocusable()
{
//set app as focusable
var helper = new WindowInteropHelper(this);
SetWindowLong(helper.Handle, GWL_EXSTYLE, GetWindowLong(helper.Handle, GWL_EXSTYLE) & ~WS_EX_NOACTIVATE);
if (gameHandle != IntPtr.Zero)
{
//SetWindowLong(gameHandle, GWL_EXSTYLE, GetWindowLong(gameHandle, GWL_EXSTYLE) & ~WS_EX_NOACTIVATE);
// gameHandle = IntPtr.Zero;
}
}
#endregion
#endregion
private void Button_Click(object sender, RoutedEventArgs e)
{
ToggleWindow();
}
private void ContextMenu_Show(object sender, RoutedEventArgs e)
{
TasksToggleWindowOpen();
}
private void ContextMenu_Close(object sender, RoutedEventArgs e)
{
this.Close();
}
private void notifyIcon_LeftClick(NotifyIcon sender, RoutedEventArgs e)
{
TasksToggleWindowOpen();
}
private void quickAccessMenu_ContentRendered(object sender, EventArgs e)
{
((App)Application.Current).CancelSplashScreen();
if (String.Equals("C:\\Windows\\System32", Directory.GetCurrentDirectory(), StringComparison.OrdinalIgnoreCase))
{
this.Hide();
}
else
{
SetAppLocationHeight();
}
}
}
}