microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
af80c8ee50697aae53886fa72bf64e2e856f14a4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/autoShell/AutoShell.cs

1398lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System;
5using System.Collections;
6using System.Collections.Generic;
7using System.Diagnostics;
8using System.IO;
9using System.Linq;
10using System.Reflection;
11using System.Runtime.InteropServices;
12using System.Text;
13using System.Threading.Tasks;
14using System.Windows.Controls;
15using Microsoft.VisualBasic;
16using Microsoft.WindowsAPICodePack.Shell;
17using Newtonsoft.Json;
18using Newtonsoft.Json.Linq;
19using static autoShell.AutoShell;
20
21
22namespace autoShell;
23
24internal partial class AutoShell
25{
26 // create a map of friendly names to executable paths
27 static Hashtable s_friendlyNameToPath = [];
28 static Hashtable s_friendlyNameToId = [];
29 static double s_savedVolumePct = 0.0;
30
31 static IServiceProvider10 s_shell;
32 static IVirtualDesktopManager s_virtualDesktopManager;
33 static IVirtualDesktopManagerInternal s_virtualDesktopManagerInternal;
34 static IVirtualDesktopManagerInternal_BUGBUG s_virtualDesktopManagerInternal_BUGBUG;
35 static IApplicationViewCollection s_applicationViewCollection;
36 static IVirtualDesktopPinnedApps s_virtualDesktopPinnedApps;
37
38
39 /// <summary>
40 /// Constructor used to get system wide information required for specific commands.
41 /// </summary>
42 static AutoShell()
43 {
44 // get current user name
45 string userName = Environment.UserName;
46 SortedList<string, string> sortedList = new SortedList<string, string>
47 {
48 { "chrome", "chrome.exe" },
49 { "power point", "C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE" },
50 { "powerpoint", "C:\\Program Files\\Microsoft Office\\root\\Office16\\POWERPNT.EXE" },
51 { "word", "C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE" },
52 { "winword", "C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE" },
53 { "excel", "C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE" },
54 { "outlook", "C:\\Program Files\\Microsoft Office\\root\\Office16\\OUTLOOK.EXE" },
55 { "visual studio", "devenv.exe" },
56 { "visual studio code", "C:\\Users\\" + userName + "\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe" },
57 { "edge", "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" },
58 { "microsoft edge", "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe" },
59 { "notepad", "C:\\Windows\\System32\\notepad.exe" },
60 { "paint", "mspaint.exe" },
61 { "calculator", "calc.exe" },
62 { "file explorer", "C:\\Windows\\explorer.exe" },
63 { "control panel", "C:\\Windows\\System32\\control.exe" },
64 { "task manager", "C:\\Windows\\System32\\Taskmgr.exe" },
65 { "cmd", "C:\\Windows\\System32\\cmd.exe" },
66 { "powershell", "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" },
67 { "snipping tool", "C:\\Windows\\System32\\SnippingTool.exe" },
68 { "magnifier", "C:\\Windows\\System32\\Magnify.exe" },
69 { "paint 3d", "C:\\Program Files\\WindowsApps\\Microsoft.MSPaint_10.1807.18022.0_x64__8wekyb3d8bbwe\\"},
70 { "m365 copilot", "C:\\Program Files\\WindowsApps\\Microsoft.MicrosoftOfficeHub_19.2512.45041.0_x64__8wekyb3d8bbwe\\M365Copilot.exe" },
71 { "copilot", "C:\\Program Files\\WindowsApps\\Microsoft.MicrosoftOfficeHub_19.2512.45041.0_x64__8wekyb3d8bbwe\\M365Copilot.exe" },
72 { "spotify", "C:\\Program Files\\WindowsApps\\SpotifyAB.SpotifyMusic_1.279.427.0_x64__zpdnekdrzrea0\\spotify.exe" },
73 };
74
75 // add the entries to the hashtable
76 foreach (var kvp in sortedList)
77 {
78 s_friendlyNameToPath.Add(kvp.Key, kvp.Value);
79 }
80
81 var installedApps = GetAllInstalledAppsIds();
82 foreach (var kvp in installedApps)
83 {
84 s_friendlyNameToId.Add(kvp.Key, kvp.Value);
85 }
86
87 // Load the installed themes
88 LoadThemes();
89
90 // Desktop management
91 s_shell = (IServiceProvider10)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_ImmersiveShell));
92 s_virtualDesktopManagerInternal = (IVirtualDesktopManagerInternal)s_shell.QueryService(CLSID_VirtualDesktopManagerInternal, typeof(IVirtualDesktopManagerInternal).GUID);
93 s_virtualDesktopManagerInternal_BUGBUG = (IVirtualDesktopManagerInternal_BUGBUG)s_shell.QueryService(CLSID_VirtualDesktopManagerInternal, typeof(IVirtualDesktopManagerInternal).GUID);
94 s_virtualDesktopManager = (IVirtualDesktopManager)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_VirtualDesktopManager));
95 s_applicationViewCollection = (IApplicationViewCollection)s_shell.QueryService(typeof(IApplicationViewCollection).GUID, typeof(IApplicationViewCollection).GUID);
96 s_virtualDesktopPinnedApps = (IVirtualDesktopPinnedApps)s_shell.QueryService(CLSID_VirtualDesktopPinnedApps, typeof(IVirtualDesktopPinnedApps).GUID);
97 }
98
99 /// <summary>
100 /// Program entry point
101 /// </summary>
102 /// <param name="args">Any command line arguments</param>
103 static void Main(string[] args)
104 {
105 string rawCmdLine = Marshal.PtrToStringUni(GetCommandLineW());
106
107 // if there are command line args let's execute those one at a time and then exit
108 // user can specify a single JSON object command or an array of them on the command line
109 if (args.Length > 0)
110 {
111 string exe = $"\"{Environment.ProcessPath}\"";
112 string cmdLine = rawCmdLine.Replace(exe, "");
113
114 if (cmdLine.StartsWith(exe, StringComparison.OrdinalIgnoreCase))
115 {
116 cmdLine = cmdLine[exe.Length..];
117 }
118 else if (cmdLine.StartsWith(Path.GetFileName(Environment.ProcessPath), StringComparison.OrdinalIgnoreCase))
119 {
120 cmdLine = cmdLine[Path.GetFileName(Environment.ProcessPath).Length..];
121 }
122 else if (cmdLine.StartsWith(Path.GetFileNameWithoutExtension(Environment.ProcessPath), StringComparison.OrdinalIgnoreCase))
123 {
124 cmdLine = cmdLine[Path.GetFileNameWithoutExtension(Environment.ProcessPath).Length..];
125 }
126
127 try
128 {
129 JArray commands = JArray.Parse(cmdLine);
130 foreach (JObject jo in commands.Children<JObject>())
131 {
132 execLine(jo);
133 }
134 }
135 catch (JsonReaderException)
136 {
137 execLine(JObject.Parse(cmdLine));
138 }
139
140 // exit
141 return;
142 }
143
144 // run in interactive mode, keep accepting commands until we get the shutdown command
145 bool quit = false;
146 while (!quit)
147 {
148 try
149 {
150 // read a line from the console
151 string line = Console.ReadLine();
152
153 // if stdin is closed (e.g., piped input finished), exit
154 if (line == null)
155 {
156 break;
157 }
158
159 // parse the line as a json object with one or more command keys (with values as parameters)
160 JObject root = JObject.Parse(line);
161
162 // execute the line
163 quit = execLine(root);
164 }
165 catch (Exception ex)
166 {
167 LogError(ex);
168 }
169 }
170 }
171
172 internal static void LogError(Exception ex)
173 {
174 Debug.WriteLine(ex);
175 ConsoleColor previousColor = Console.ForegroundColor;
176 Console.ForegroundColor = ConsoleColor.Red;
177 Console.WriteLine("Error: " + ex.Message);
178 Console.ForegroundColor = previousColor;
179 }
180
181 internal static void LogWarning(string message)
182 {
183 Debug.WriteLine(message);
184 ConsoleColor previousColor = Console.ForegroundColor;
185 Console.ForegroundColor = ConsoleColor.Yellow;
186 Console.WriteLine("Warning: " + message);
187 Console.ForegroundColor = previousColor;
188 }
189
190 static SortedList<string, string> GetAllInstalledAppsIds()
191 {
192 // GUID taken from https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
193 var FOLDERID_AppsFolder = new Guid("{1e87508d-89c2-42f0-8a7e-645a0f50ca58}");
194 ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(FOLDERID_AppsFolder);
195 var appIds = new SortedList<string, string>();
196
197 foreach (var app in (IKnownFolder)appsFolder)
198 {
199 string appName = app.Name.ToLowerInvariant();
200 if (appIds.ContainsKey(appName))
201 {
202 Debug.WriteLine("Key has multiple values: " + appName);
203 }
204 else
205 {
206 // The ParsingName property is the AppUserModelID
207 appIds.Add(appName, app.ParsingName);
208 }
209 }
210
211 return appIds;
212 }
213
214 static void SetMasterVolume(int pct)
215 {
216 // Using Windows Core Audio API via COM interop
217 try
218 {
219 var deviceEnumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();
220 deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice device);
221 var audioEndpointVolumeGuid = typeof(IAudioEndpointVolume).GUID;
222 device.Activate(ref audioEndpointVolumeGuid, 0, IntPtr.Zero, out object obj);
223 var audioEndpointVolume = (IAudioEndpointVolume)obj;
224 audioEndpointVolume.GetMasterVolumeLevelScalar(out float currentVolume);
225 s_savedVolumePct = currentVolume * 100.0;
226 audioEndpointVolume.SetMasterVolumeLevelScalar(pct / 100.0f, Guid.Empty);
227 }
228 catch (Exception ex)
229 {
230 Debug.WriteLine("Failed to set volume: " + ex.Message);
231 }
232 }
233
234 static void RestoreMasterVolume()
235 {
236 // Using Windows Core Audio API via COM interop
237 try
238 {
239 var deviceEnumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();
240 deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice device);
241 var audioEndpointVolumeGuid = typeof(IAudioEndpointVolume).GUID;
242 device.Activate(ref audioEndpointVolumeGuid, 0, IntPtr.Zero, out object obj);
243 var audioEndpointVolume = (IAudioEndpointVolume)obj;
244 audioEndpointVolume.SetMasterVolumeLevelScalar((float)(s_savedVolumePct / 100.0), Guid.Empty);
245 }
246 catch (Exception ex)
247 {
248 Debug.WriteLine("Failed to restore volume: " + ex.Message);
249 }
250 }
251
252 static void SetMasterMute(bool mute)
253 {
254 // Using Windows Core Audio API via COM interop
255 try
256 {
257 var deviceEnumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();
258 deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice device);
259 var audioEndpointVolumeGuid = typeof(IAudioEndpointVolume).GUID;
260 device.Activate(ref audioEndpointVolumeGuid, 0, IntPtr.Zero, out object obj);
261 var audioEndpointVolume = (IAudioEndpointVolume)obj;
262 audioEndpointVolume.GetMute(out bool currentMute);
263 Debug.WriteLine("Current Mute:" + currentMute);
264 audioEndpointVolume.SetMute(mute, Guid.Empty);
265 }
266 catch (Exception ex)
267 {
268 Debug.WriteLine("Failed to set mute: " + ex.Message);
269 }
270 }
271
272 static string ResolveProcessNameFromFriendlyName(string friendlyName)
273 {
274 string path = (string)s_friendlyNameToPath[friendlyName.ToLowerInvariant()];
275 if (path != null)
276 {
277 return Path.GetFileNameWithoutExtension(path);
278 }
279 else
280 {
281 return friendlyName;
282 }
283 }
284
285 static IntPtr FindProcessWindowHandle(string processName)
286 {
287 processName = ResolveProcessNameFromFriendlyName(processName);
288 Process[] processes = Process.GetProcessesByName(processName);
289 // loop through the processes that match the name; raise the first one that has a main window
290 foreach (Process p in processes)
291 {
292 if (p.MainWindowHandle != IntPtr.Zero)
293 {
294 return p.MainWindowHandle;
295 }
296 }
297
298 // Try to find by window title if we haven't found it and bring it forward
299 return FindWindowByTitle(processName).hWnd;
300 }
301
302 // given part of a process name, raise the window of that process to the top level
303 static void RaiseWindow(string processName)
304 {
305 processName = ResolveProcessNameFromFriendlyName(processName);
306 Process[] processes = Process.GetProcessesByName(processName);
307 // loop through the processes that match the name; raise the first one that has a main window
308 foreach (Process p in processes)
309 {
310 if (p.MainWindowHandle != IntPtr.Zero)
311 {
312 SetForegroundWindow(p.MainWindowHandle);
313 Interaction.AppActivate(p.Id);
314 return;
315 }
316 }
317
318 // this means all the applications processes are running in the background. This happens for edge and chrome browsers.
319 string path = (string)s_friendlyNameToPath[processName];
320 if (path != null)
321 {
322 Process.Start(path);
323 }
324 else
325 {
326 // Try to find by window title if we haven't found it and bring it forward
327 (nint hWnd1, int pid) = FindWindowByTitle(processName);
328
329 if (hWnd1 != nint.Zero)
330 {
331 SetForegroundWindow(hWnd1);
332 Interaction.AppActivate(pid);
333 }
334 }
335 }
336
337 static void MaximizeWindow(string processName)
338 {
339 processName = ResolveProcessNameFromFriendlyName(processName);
340 Process[] processes = Process.GetProcessesByName(processName);
341 // loop through the processes that match the name; raise the first one that has a main window
342 foreach (Process p in processes)
343 {
344 if (p.MainWindowHandle != IntPtr.Zero)
345 {
346 uint WM_SYSCOMMAND = 0x112;
347 uint SC_MAXIMIZE = 0xf030;
348 SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, IntPtr.Zero);
349 SetForegroundWindow(p.MainWindowHandle);
350 Interaction.AppActivate(p.Id);
351 return;
352 }
353 }
354
355 // if we haven't found what we are looking for let's enumerate the top level windows and try that way
356 (nint hWnd, int pid) = FindWindowByTitle(processName);
357 if (hWnd != nint.Zero)
358 {
359 uint WM_SYSCOMMAND = 0x112;
360 uint SC_MAXIMIZE = 0xf030;
361 SendMessage(hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, IntPtr.Zero);
362 SetForegroundWindow(hWnd);
363 Interaction.AppActivate(pid);
364 }
365 }
366
367 static void MinimizeWindow(string processName)
368 {
369 processName = ResolveProcessNameFromFriendlyName(processName);
370 Process[] processes = Process.GetProcessesByName(processName);
371 // loop through the processes that match the name; raise the first one that has a main window
372 foreach (Process p in processes)
373 {
374 if (p.MainWindowHandle != IntPtr.Zero)
375 {
376 uint WM_SYSCOMMAND = 0x112;
377 uint SC_MINIMIZE = 0xF020;
378 SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_MINIMIZE, IntPtr.Zero);
379 break;
380 }
381 }
382
383 // if we haven't found what we are looking for let's enumerate the top level windows and try that way
384 (nint hWnd, int pid) = FindWindowByTitle(processName);
385 if (hWnd != nint.Zero)
386 {
387 uint WM_SYSCOMMAND = 0x112;
388 uint SC_MINIMIZE = 0xF020;
389 SendMessage(hWnd, WM_SYSCOMMAND, SC_MINIMIZE, IntPtr.Zero);
390 SetForegroundWindow(hWnd);
391 Interaction.AppActivate(pid);
392 }
393 }
394
395 static void TileWindowPair(string processName1, string processName2)
396 {
397 // find both processes
398 // TODO: Update this to account for UWP apps (e.g. calculator). UWPs are hosted by ApplicationFrameHost.exe
399 processName1 = ResolveProcessNameFromFriendlyName(processName1);
400 Process[] processes1 = Process.GetProcessesByName(processName1);
401 IntPtr hWnd1 = IntPtr.Zero;
402 IntPtr hWnd2 = IntPtr.Zero;
403 int pid1 = -1;
404 int pid2 = -1;
405
406 foreach (Process p in processes1)
407 {
408 if (p.MainWindowHandle != IntPtr.Zero)
409 {
410 hWnd1 = p.MainWindowHandle;
411 pid1 = p.Id;
412 break;
413 }
414 }
415
416 // If no process found by name, search by window title
417 if (hWnd1 == IntPtr.Zero)
418 {
419 (hWnd1, pid1) = FindWindowByTitle(processName1);
420 }
421
422 processName2 = ResolveProcessNameFromFriendlyName(processName2);
423 Process[] processes2 = Process.GetProcessesByName(processName2);
424 foreach (Process p in processes2)
425 {
426 if (p.MainWindowHandle != IntPtr.Zero)
427 {
428 hWnd2 = p.MainWindowHandle;
429 pid2 = p.Id;
430 break;
431 }
432 }
433
434 // If no process found by name, search by window title
435 if (hWnd2 == IntPtr.Zero)
436 {
437 (hWnd2, pid2) = FindWindowByTitle(processName2);
438 }
439
440 if (hWnd1 != IntPtr.Zero && hWnd2 != IntPtr.Zero)
441 {
442 // TODO: handle multiple monitors
443 // get the screen size
444 IntPtr desktopHandle = GetDesktopWindow();
445 RECT desktopRect = new RECT();
446 GetWindowRect(desktopHandle, ref desktopRect);
447 // get the dimensions of the taskbar
448 // find the taskbar window
449 IntPtr taskbarHandle = IntPtr.Zero;
450 IntPtr hWnd = IntPtr.Zero;
451 while ((hWnd = FindWindowEx(IntPtr.Zero, hWnd, "Shell_TrayWnd", null)) != IntPtr.Zero)
452 {
453 // find the taskbar window's child
454 taskbarHandle = FindWindowEx(hWnd, IntPtr.Zero, "ReBarWindow32", null);
455 if (taskbarHandle != IntPtr.Zero)
456 {
457 break;
458 }
459 }
460 if (hWnd == IntPtr.Zero)
461 {
462 Debug.WriteLine("Taskbar not found");
463 return;
464 }
465 else
466 {
467 RECT taskbarRect = new RECT();
468 GetWindowRect(hWnd, ref taskbarRect);
469 Debug.WriteLine("Taskbar Rect: " + taskbarRect.Left + ", " + taskbarRect.Top + ", " + taskbarRect.Right + ", " + taskbarRect.Bottom);
470 // TODO: handle left, top, right and nonexistant taskbars
471 // subtract the taskbar height from the screen height
472 desktopRect.Bottom -= (int)((taskbarRect.Bottom - taskbarRect.Top) / 2);
473 }
474 // set the window positions using the shellRect and making sure the windows are visible
475 int halfwidth = (desktopRect.Right - desktopRect.Left) / 2;
476 IntPtr HWND_TOP = IntPtr.Zero;
477 uint showWindow = 0x40;
478 SetWindowPos(hWnd1, HWND_TOP, desktopRect.Left, desktopRect.Top, halfwidth, desktopRect.Bottom, showWindow);
479 SetForegroundWindow(hWnd1);
480 Interaction.AppActivate(pid1);
481 SetWindowPos(hWnd2, HWND_TOP, desktopRect.Left + halfwidth, desktopRect.Top, halfwidth, desktopRect.Bottom, showWindow);
482 SetForegroundWindow(hWnd2);
483 Interaction.AppActivate(pid2);
484 }
485 }
486
487 /// <summary>
488 /// Finds a top-level window by searching for a partial match in the window title.
489 /// </summary>
490 /// <param name="titleSearch">The text to search for in window titles (case-insensitive).</param>
491 /// <returns>A tuple containing the window handle and process ID, or (IntPtr.Zero, -1) if not found.</returns>
492 static (IntPtr hWnd, int pid) FindWindowByTitle(string titleSearch)
493 {
494 IntPtr foundHandle = IntPtr.Zero;
495 int foundPid = -1;
496 StringBuilder windowTitle = new StringBuilder(256);
497
498 EnumWindows((hWnd, lParam) =>
499 {
500 // Only consider visible windows
501 if (!IsWindowVisible(hWnd))
502 {
503 return true; // Continue enumeration
504 }
505
506 // Get window title
507 int length = GetWindowText(hWnd, windowTitle, windowTitle.Capacity);
508 if (length > 0)
509 {
510 string title = windowTitle.ToString();
511 // Case-insensitive partial match
512 if (title.Contains(titleSearch, StringComparison.OrdinalIgnoreCase))
513 {
514 foundHandle = hWnd;
515 GetWindowThreadProcessId(hWnd, out uint pid);
516 foundPid = (int)pid;
517 return false; // Stop enumeration
518 }
519 }
520 return true; // Continue enumeration
521 }, IntPtr.Zero);
522
523 return (foundHandle, foundPid);
524 }
525
526 // given a friendly name, check if it's running and if not, start it; if it's running raise it to the top level
527 static void OpenApplication(string friendlyName)
528 {
529 // check to see if the application is running
530 Process[] processes = Process.GetProcessesByName(friendlyName);
531 if (processes.Length == 0)
532 {
533 // if not, start it
534 Debug.WriteLine("Starting " + friendlyName);
535 string path = (string)s_friendlyNameToPath[friendlyName.ToLowerInvariant()];
536 if (path != null)
537 {
538 try
539 {
540 Process.Start(path);
541 }
542 catch (System.ComponentModel.Win32Exception)
543 {
544 // alternate start method
545 Process.Start(friendlyName);
546 }
547 }
548 else
549 {
550 string appModelUserID = (string)s_friendlyNameToId[friendlyName.ToLowerInvariant()];
551 if (appModelUserID != null)
552 {
553 try
554 {
555 Process.Start("explorer.exe", @" shell:appsFolder\" + appModelUserID);
556 }
557 catch { }
558 }
559 }
560 }
561 else
562 {
563 // if so, raise it to the top level
564 Debug.WriteLine("Raising " + friendlyName);
565 RaiseWindow(friendlyName);
566 }
567 }
568
569 // close application
570 static void CloseApplication(string friendlyName)
571 {
572 // check to see if the application is running
573 string processName = ResolveProcessNameFromFriendlyName(friendlyName);
574 Process[] processes = Process.GetProcessesByName(processName);
575 if (processes.Length != 0)
576 {
577 // if so, close it
578 Debug.WriteLine("Closing " + friendlyName);
579 foreach (Process p in processes)
580 {
581 if (p.MainWindowHandle != IntPtr.Zero)
582 {
583 p.CloseMainWindow();
584 }
585 }
586 }
587 }
588
589 private static void SetDesktopWallpaper(string imagePath)
590 {
591 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imagePath, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
592 }
593
594 /// <summary>
595 /// Creates virtual desktops from a JSON array of desktop names.
596 /// </summary>
597 /// <param name="jsonValue">JSON array containing desktop names, e.g., ["Work", "Personal", "Gaming"]</param>
598 static void CreateDesktop(string jsonValue)
599 {
600 try
601 {
602 // Parse the JSON array of desktop names
603 JArray desktopNames = JArray.Parse(jsonValue);
604
605 if (desktopNames == null || desktopNames.Count == 0)
606 {
607 desktopNames = ["desktop X"];
608 }
609
610 if (s_virtualDesktopManagerInternal == null)
611 {
612 Debug.WriteLine($"Failed to get Virtual Desktop Manager Internal");
613 return;
614 }
615
616 foreach (JToken desktopNameToken in desktopNames)
617 {
618 string desktopName = desktopNameToken.ToString();
619
620
621 try
622 {
623 // Create a new virtual desktop
624 IVirtualDesktop newDesktop = s_virtualDesktopManagerInternal.CreateDesktop();
625
626 if (newDesktop != null)
627 {
628 // Set the desktop name (Windows 10 build 20231+ / Windows 11)
629 try
630 {
631 // TODO: debug & get working
632 // Works in .NET framework but not .NET
633 //s_virtualDesktopManagerInternal_BUGBUG.SetDesktopName(newDesktop, desktopName);
634 //Debug.WriteLine($"Created virtual desktop: {desktopName}");
635 }
636 catch (Exception ex2)
637 {
638 // Older Windows version - name setting not supported
639 Debug.WriteLine($"Created virtual desktop (naming not supported on this Windows version): {ex2.Message}");
640 }
641 }
642 }
643 catch (Exception ex)
644 {
645 Debug.WriteLine($"Failed to create desktop '{desktopName}': {ex.Message}");
646 }
647 }
648 }
649 catch (JsonException ex)
650 {
651 Debug.WriteLine($"Failed to parse desktop names JSON: {ex.Message}");
652 }
653 catch (Exception ex)
654 {
655 Debug.WriteLine($"Error creating desktops: {ex.Message}");
656 }
657 }
658
659 static void SwitchDesktop(string desktopIdentifier)
660 {
661 if (!int.TryParse(desktopIdentifier, out int index))
662 {
663 // Try to find the desktop by name
664 s_virtualDesktopManagerInternal.SwitchDesktop(FindDesktopByName(desktopIdentifier));
665 }
666 else
667 {
668 SwitchDesktop(index);
669 }
670 }
671
672 static void SwitchDesktop(int index)
673 {
674 s_virtualDesktopManagerInternal.GetDesktops(out IObjectArray desktops);
675 desktops.GetAt(index, typeof(IVirtualDesktop).GUID, out object od);
676
677 // BUGBUG: different windows versions use different COM interfaces
678 // Different Windows versions use different COM interfaces for desktop switching
679 // Windows 11 22H2 (build 22621) and later use the updated interface
680 if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22621))
681 {
682 // Use the BUGBUG interface for Windows 11 22H2+
683 s_virtualDesktopManagerInternal_BUGBUG.SwitchDesktopWithAnimation((IVirtualDesktop)od);
684 }
685 else if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000))
686 {
687 // Windows 11 21H2 (build 22000)
688 s_virtualDesktopManagerInternal.SwitchDesktopWithAnimation((IVirtualDesktop)od);
689 }
690 else
691 {
692 // Windows 10 - use the original interface
693 s_virtualDesktopManagerInternal.SwitchDesktopAndMoveForegroundView((IVirtualDesktop)od);
694 }
695
696 Marshal.ReleaseComObject(desktops);
697 }
698
699 static void BumpDesktopIndex(int bump)
700 {
701 IVirtualDesktop desktop = s_virtualDesktopManagerInternal.GetCurrentDesktop();
702 int index = GetDesktopIndex(desktop);
703 int count = s_virtualDesktopManagerInternal.GetCount();
704
705 if (index == -1)
706 {
707 Debug.WriteLine("Undable to get the index of the current desktop");
708 return;
709 }
710
711 index += bump;
712
713 if (index > count)
714 {
715 index = 0;
716 }
717 else if (index < 0)
718 {
719 index = count - 1;
720 }
721
722 SwitchDesktop(index);
723 }
724
725 static IVirtualDesktop FindDesktopByName(string name)
726 {
727 int count = s_virtualDesktopManagerInternal.GetCount();
728
729 s_virtualDesktopManagerInternal.GetDesktops(out IObjectArray desktops);
730 for (int i = 0; i < count; i++)
731 {
732 desktops.GetAt(i, typeof(IVirtualDesktop).GUID, out object od);
733
734 if (string.Equals(((IVirtualDesktop)od).GetName(), name, StringComparison.OrdinalIgnoreCase))
735 {
736 Marshal.ReleaseComObject(desktops);
737 return (IVirtualDesktop)od;
738 }
739 }
740
741 Marshal.ReleaseComObject(desktops);
742
743 return null;
744 }
745
746 static int GetDesktopIndex(IVirtualDesktop desktop)
747 {
748 int index = -1;
749 int count = s_virtualDesktopManagerInternal.GetCount();
750
751 s_virtualDesktopManagerInternal.GetDesktops(out IObjectArray desktops);
752 for (int i = 0; i < count; i++)
753 {
754 desktops.GetAt(i, typeof(IVirtualDesktop).GUID, out object od);
755
756 if (desktop.GetId() == ((IVirtualDesktop)od).GetId())
757 {
758 Marshal.ReleaseComObject(desktops);
759 return i;
760 }
761 }
762
763 Marshal.ReleaseComObject(desktops);
764
765 return -1;
766 }
767
768 /// <summary>
769 ///
770 /// </summary>
771 /// <param name="value"></param>
772 /// <remarks>Currently not working correction, returns ACCESS_DENIED // TODO: investigate</remarks>
773 static void MoveWindowToDesktop(JToken value)
774 {
775 string process = value.SelectToken("process").ToString();
776 string desktop = value.SelectToken("desktop").ToString();
777 if (string.IsNullOrEmpty(process))
778 {
779 Debug.WriteLine("No process name supplied");
780 return;
781 }
782
783 if (string.IsNullOrEmpty(desktop))
784 {
785 Debug.WriteLine("No desktop id supplied");
786 return;
787 }
788
789 IntPtr hWnd = FindProcessWindowHandle(process);
790
791 if (int.TryParse(desktop, out int desktopIndex))
792 {
793 s_virtualDesktopManagerInternal.GetDesktops(out IObjectArray desktops);
794 if (desktopIndex < 1 || desktopIndex > s_virtualDesktopManagerInternal.GetCount())
795 {
796 Debug.WriteLine("Desktop index out of range");
797 Marshal.ReleaseComObject(desktops);
798 return;
799 }
800 desktops.GetAt(desktopIndex - 1, typeof(IVirtualDesktop).GUID, out object od);
801 Guid g = ((IVirtualDesktop)od).GetId();
802 s_virtualDesktopManager.MoveWindowToDesktop(hWnd, ref g);
803 Marshal.ReleaseComObject(desktops);
804 return;
805 }
806
807 IVirtualDesktop ivd = FindDesktopByName(desktop);
808 if (ivd is not null)
809 {
810 Guid desktopGuid = ivd.GetId();
811 s_virtualDesktopManager.MoveWindowToDesktop(hWnd, ref desktopGuid);
812 }
813 }
814
815 static void PinWindow(string processName)
816 {
817 IntPtr hWnd = FindProcessWindowHandle(processName);
818
819 if (hWnd != IntPtr.Zero)
820 {
821 s_applicationViewCollection.GetViewForHwnd(hWnd, out IApplicationView view);
822
823 if (view is not null)
824 {
825 s_virtualDesktopPinnedApps.PinView((IApplicationView)view);
826 }
827 }
828 else
829 {
830 Console.WriteLine($"The window handle for '{processName}' could not be found");
831 }
832 }
833
834 static IVirtualDesktopManagerInternal GetVirtualDesktopManagerInternal()
835 {
836 try
837 {
838 IServiceProvider shellServiceProvider = (IServiceProvider)Activator.CreateInstance(
839 Type.GetTypeFromCLSID(CLSID_ImmersiveShell));
840
841 shellServiceProvider.QueryService(
842 CLSID_VirtualDesktopManagerInternal,
843 typeof(IVirtualDesktopManagerInternal).GUID,
844 out object objVirtualDesktopManagerInternal);
845
846 return (IVirtualDesktopManagerInternal)objVirtualDesktopManagerInternal;
847 }
848 catch
849 {
850 return null;
851 }
852 }
853
854 static bool execLine(JObject root)
855 {
856 var quit = false;
857 foreach (var kvp in root)
858 {
859 string key = kvp.Key;
860 string value = kvp.Value.ToString();
861 switch (key)
862 {
863 case "launchProgram":
864 OpenApplication(value);
865 break;
866 case "closeProgram":
867 CloseApplication(value);
868 break;
869 case "maximize":
870 MaximizeWindow(value);
871 break;
872 case "minimize":
873 MinimizeWindow(value);
874 break;
875 case "switchTo":
876 RaiseWindow(value);
877 break;
878 case "quit":
879 quit = true;
880 break;
881 case "tile":
882 string[] apps = value.Split(',');
883 if (apps.Length == 2)
884 {
885 TileWindowPair(apps[0], apps[1]);
886 }
887 break;
888 case "volume":
889 int pct = 0;
890 if (int.TryParse(value, out pct))
891 {
892 SetMasterVolume(pct);
893 }
894 break;
895 case "restoreVolume":
896 RestoreMasterVolume();
897 break;
898 case "mute":
899 bool mute = false;
900 if (bool.TryParse(value, out mute))
901 {
902 SetMasterMute(mute);
903 }
904 break;
905 case "listAppNames":
906 var installedApps = GetAllInstalledAppsIds();
907 Console.WriteLine(JsonConvert.SerializeObject(installedApps.Keys));
908 break;
909 case "setWallpaper":
910 SetDesktopWallpaper(value);
911 break;
912 case "applyTheme":
913 bool result = ApplyTheme(value);
914 break;
915 case "listThemes":
916 var themes = GetInstalledThemes();
917 Console.WriteLine(JsonConvert.SerializeObject(themes));
918 break;
919 case "setThemeMode":
920 // value can be "light", "dark", "toggle", or boolean
921 if (value.Equals("toggle", StringComparison.OrdinalIgnoreCase))
922 {
923 ToggleLightDarkMode();
924 }
925 else
926 {
927 bool useLightMode;
928 if (bool.TryParse(value, out useLightMode))
929 {
930 SetLightDarkMode(useLightMode);
931 }
932 else if (value.Equals("light", StringComparison.OrdinalIgnoreCase))
933 {
934 SetLightDarkMode(true);
935 }
936 else if (value.Equals("dark", StringComparison.OrdinalIgnoreCase))
937 {
938 SetLightDarkMode(false);
939 }
940 }
941 break;
942 case "createDesktop":
943 CreateDesktop(value);
944 break;
945 case "switchDesktop":
946 SwitchDesktop(value);
947 break;
948 case "nextDesktop":
949 BumpDesktopIndex(1);
950 break;
951 case "previousDesktop":
952 BumpDesktopIndex(-1);
953 break;
954 case "moveWindowToDesktop":
955 MoveWindowToDesktop(kvp.Value);
956 break;
957 case "pinWindow":
958 PinWindow(value);
959 break;
960 case "toggleNotifications":
961 ShellExecute(IntPtr.Zero, "open", "ms-actioncenter:", null, null, 1);
962 break;
963 case "debug":
964 Debugger.Launch();
965 break;
966 case "toggleAirplaneMode":
967 SetAirplaneMode(bool.Parse(value));
968 break;
969 case "listWifiNetworks":
970 ListWifiNetworks();
971 break;
972 case "connectWifi":
973 JObject netInfo = JObject.Parse(value);
974 string ssid = netInfo.Value<string>("ssid");
975 string password = netInfo["password"] is not null ? netInfo.Value<string>("password") : "";
976 ConnectToWifi(ssid, password);
977 break;
978 case "disconnectWifi":
979 DisconnectFromWifi();
980 break;
981 case "setTextSize":
982 if (int.TryParse(value, out int textSizePct))
983 {
984 SetTextSize(textSizePct);
985 }
986 break;
987 default:
988 Debug.WriteLine("Unknown command: " + key);
989 break;
990 }
991 }
992 return quit;
993 }
994
995 /// <summary>
996 /// Sets the airplane mode state using the Radio Management API.
997 /// </summary>
998 /// <param name="enable">True to enable airplane mode, false to disable.</param>
999 static void SetAirplaneMode(bool enable)
1000 {
1001 IRadioManager radioManager = null;
1002 try
1003 {
1004 // Create the Radio Management API COM object
1005 Type radioManagerType = Type.GetTypeFromCLSID(CLSID_RadioManagementAPI);
1006 if (radioManagerType == null)
1007 {
1008 Debug.WriteLine("Failed to get Radio Management API type");
1009 return;
1010 }
1011
1012 object obj = Activator.CreateInstance(radioManagerType);
1013 radioManager = (IRadioManager)obj;
1014
1015 if (radioManager == null)
1016 {
1017 Debug.WriteLine("Failed to create Radio Manager instance");
1018 return;
1019 }
1020
1021 // Get current state (for logging)
1022 int hr = radioManager.GetSystemRadioState(out int currentState, out int _, out int _);
1023 if (hr < 0)
1024 {
1025 Debug.WriteLine($"Failed to get system radio state: HRESULT 0x{hr:X8}");
1026 return;
1027 }
1028
1029 // currentState: 0 = airplane mode ON (radios off), 1 = airplane mode OFF (radios on)
1030 bool airplaneModeCurrentlyOn = currentState == 0;
1031 Debug.WriteLine($"Current airplane mode state: {(airplaneModeCurrentlyOn ? "on" : "off")}");
1032
1033 // Set the new state
1034 // bEnabled: 0 = turn airplane mode ON (disable radios), 1 = turn airplane mode OFF (enable radios)
1035 int newState = enable ? 0 : 1;
1036 hr = radioManager.SetSystemRadioState(newState);
1037 if (hr < 0)
1038 {
1039 Debug.WriteLine($"Failed to set system radio state: HRESULT 0x{hr:X8}");
1040 return;
1041 }
1042
1043 Debug.WriteLine($"Airplane mode set to: {(enable ? "on" : "off")}");
1044 }
1045 catch (COMException ex)
1046 {
1047 Debug.WriteLine($"COM Exception setting airplane mode: {ex.Message} (HRESULT: 0x{ex.HResult:X8})");
1048 }
1049 catch (Exception ex)
1050 {
1051 Debug.WriteLine($"Failed to set airplane mode: {ex.Message}");
1052 }
1053 finally
1054 {
1055 if (radioManager != null)
1056 {
1057 Marshal.ReleaseComObject(radioManager);
1058 }
1059 }
1060 }
1061
1062 /// <summary>
1063 /// Lists all WiFi networks currently in range.
1064 /// </summary>
1065 static void ListWifiNetworks()
1066 {
1067 IntPtr clientHandle = IntPtr.Zero;
1068 IntPtr wlanInterfaceList = IntPtr.Zero;
1069 IntPtr networkList = IntPtr.Zero;
1070
1071 try
1072 {
1073 // Open WLAN handle
1074 int result = WlanOpenHandle(2, IntPtr.Zero, out uint negotiatedVersion, out clientHandle);
1075 if (result != 0)
1076 {
1077 Debug.WriteLine($"Failed to open WLAN handle: {result}");
1078 return;
1079 }
1080
1081 // Enumerate wireless interfaces
1082 result = WlanEnumInterfaces(clientHandle, IntPtr.Zero, out wlanInterfaceList);
1083 if (result != 0)
1084 {
1085 Debug.WriteLine($"Failed to enumerate WLAN interfaces: {result}");
1086 return;
1087 }
1088
1089 WLAN_INTERFACE_INFO_LIST interfaceList = Marshal.PtrToStructure<WLAN_INTERFACE_INFO_LIST>(wlanInterfaceList);
1090
1091 if (interfaceList.dwNumberOfItems == 0)
1092 {
1093 Console.WriteLine("[]");
1094 return;
1095 }
1096
1097 var allNetworks = new List<object>();
1098
1099 for (int i = 0; i < interfaceList.dwNumberOfItems; i++)
1100 {
1101 WLAN_INTERFACE_INFO interfaceInfo = interfaceList.InterfaceInfo[i];
1102
1103 // Scan for networks (trigger a refresh)
1104 WlanScan(clientHandle, ref interfaceInfo.InterfaceGuid, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1105
1106 // Small delay to allow scan to complete
1107 System.Threading.Thread.Sleep(100);
1108
1109 // Get available networks
1110 result = WlanGetAvailableNetworkList(clientHandle, ref interfaceInfo.InterfaceGuid, 0, IntPtr.Zero, out networkList);
1111 if (result != 0)
1112 {
1113 Debug.WriteLine($"Failed to get network list: {result}");
1114 continue;
1115 }
1116
1117 WLAN_AVAILABLE_NETWORK_LIST availableNetworkList = Marshal.PtrToStructure<WLAN_AVAILABLE_NETWORK_LIST>(networkList);
1118
1119 IntPtr networkPtr = networkList + 8; // Skip dwNumberOfItems and dwIndex
1120
1121 for (int j = 0; j < availableNetworkList.dwNumberOfItems; j++)
1122 {
1123 WLAN_AVAILABLE_NETWORK network = Marshal.PtrToStructure<WLAN_AVAILABLE_NETWORK>(networkPtr);
1124
1125 string ssid = Encoding.ASCII.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
1126
1127 if (!string.IsNullOrEmpty(ssid))
1128 {
1129 allNetworks.Add(new
1130 {
1131 SSID = ssid,
1132 SignalQuality = network.wlanSignalQuality,
1133 Secured = network.bSecurityEnabled,
1134 Connected = (network.dwFlags & 1) != 0 // WLAN_AVAILABLE_NETWORK_CONNECTED
1135 });
1136 }
1137
1138 networkPtr += Marshal.SizeOf<WLAN_AVAILABLE_NETWORK>();
1139 }
1140
1141 if (networkList != IntPtr.Zero)
1142 {
1143 WlanFreeMemory(networkList);
1144 networkList = IntPtr.Zero;
1145 }
1146 }
1147
1148 // Remove duplicates and sort by signal strength
1149 var uniqueNetworks = allNetworks
1150 .GroupBy(n => ((dynamic)n).SSID)
1151 .Select(g => g.OrderByDescending(n => ((dynamic)n).SignalQuality).First())
1152 .OrderByDescending(n => ((dynamic)n).SignalQuality)
1153 .ToList();
1154
1155 Console.WriteLine(JsonConvert.SerializeObject(uniqueNetworks));
1156 }
1157 catch (Exception ex)
1158 {
1159 Debug.WriteLine($"Error listing WiFi networks: {ex.Message}");
1160 Console.WriteLine("[]");
1161 }
1162 finally
1163 {
1164 if (networkList != IntPtr.Zero)
1165 WlanFreeMemory(networkList);
1166 if (wlanInterfaceList != IntPtr.Zero)
1167 WlanFreeMemory(wlanInterfaceList);
1168 if (clientHandle != IntPtr.Zero)
1169 WlanCloseHandle(clientHandle, IntPtr.Zero);
1170 }
1171 }
1172
1173 /// <summary>
1174 /// Connects to a WiFi network by name (SSID). If the network requires a password and one is provided,
1175 /// it will create a temporary profile. For networks with existing profiles, it connects using the profile.
1176 /// </summary>
1177 /// <param name="ssid">The SSID of the network to connect to.</param>
1178 /// <param name="password">Optional password for secured networks.</param>
1179 static void ConnectToWifi(string ssid, string password = null)
1180 {
1181 IntPtr clientHandle = IntPtr.Zero;
1182 IntPtr wlanInterfaceList = IntPtr.Zero;
1183
1184 try
1185 {
1186 // Open WLAN handle
1187 int result = WlanOpenHandle(2, IntPtr.Zero, out uint negotiatedVersion, out clientHandle);
1188 if (result != 0)
1189 {
1190 LogWarning($"Failed to open WLAN handle: {result}");
1191 return;
1192 }
1193
1194 // Enumerate wireless interfaces
1195 result = WlanEnumInterfaces(clientHandle, IntPtr.Zero, out wlanInterfaceList);
1196 if (result != 0)
1197 {
1198 LogWarning($"Failed to enumerate WLAN interfaces: {result}");
1199 return;
1200 }
1201
1202 WLAN_INTERFACE_INFO_LIST interfaceList = Marshal.PtrToStructure<WLAN_INTERFACE_INFO_LIST>(wlanInterfaceList);
1203
1204 if (interfaceList.dwNumberOfItems == 0)
1205 {
1206 LogWarning("No wireless interfaces found.");
1207 return;
1208 }
1209
1210 // Use the first available wireless interface
1211 WLAN_INTERFACE_INFO interfaceInfo = interfaceList.InterfaceInfo[0];
1212
1213 // If password is provided, create a profile and connect
1214 if (!string.IsNullOrEmpty(password))
1215 {
1216 string profileXml = GenerateWifiProfileXml(ssid, password);
1217
1218 result = WlanSetProfile(clientHandle, ref interfaceInfo.InterfaceGuid, 0, profileXml, null, true, IntPtr.Zero, out uint reasonCode);
1219 if (result != 0)
1220 {
1221 LogWarning($"Failed to set WiFi profile: {result}, reason: {reasonCode}");
1222 return;
1223 }
1224 }
1225
1226 // Set up connection parameters
1227 WLAN_CONNECTION_PARAMETERS connectionParams = new WLAN_CONNECTION_PARAMETERS
1228 {
1229 wlanConnectionMode = WLAN_CONNECTION_MODE.wlan_connection_mode_profile,
1230 strProfile = ssid,
1231 pDot11Ssid = IntPtr.Zero,
1232 pDesiredBssidList = IntPtr.Zero,
1233 dot11BssType = DOT11_BSS_TYPE.dot11_BSS_type_any,
1234 dwFlags = 0
1235 };
1236
1237 result = WlanConnect(clientHandle, ref interfaceInfo.InterfaceGuid, ref connectionParams, IntPtr.Zero);
1238 if (result != 0)
1239 {
1240 LogWarning($"Failed to connect to WiFi network '{ssid}': {result}");
1241 return;
1242 }
1243
1244 Debug.WriteLine($"Successfully initiated connection to WiFi network: {ssid}");
1245 Console.WriteLine($"Connecting to WiFi network: {ssid}");
1246 }
1247 catch (Exception ex)
1248 {
1249 LogError(ex);
1250 }
1251 finally
1252 {
1253 if (wlanInterfaceList != IntPtr.Zero)
1254 WlanFreeMemory(wlanInterfaceList);
1255 if (clientHandle != IntPtr.Zero)
1256 WlanCloseHandle(clientHandle, IntPtr.Zero);
1257 }
1258 }
1259
1260 /// <summary>
1261 /// Generates a WiFi profile XML for WPA2-Personal (PSK) networks.
1262 /// </summary>
1263 static string GenerateWifiProfileXml(string ssid, string password)
1264 {
1265 // Convert SSID to hex
1266 string ssidHex = BitConverter.ToString(Encoding.UTF8.GetBytes(ssid)).Replace("-", "");
1267
1268 return $@"<?xml version=""1.0""?>
1269<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
1270 <name>{ssid}</name>
1271 <SSIDConfig>
1272 <SSID>
1273 <hex>{ssidHex}</hex>
1274 <name>{ssid}</name>
1275 </SSID>
1276 </SSIDConfig>
1277 <connectionType>ESS</connectionType>
1278 <connectionMode>auto</connectionMode>
1279 <MSM>
1280 <security>
1281 <authEncryption>
1282 <authentication>WPA2PSK</authentication>
1283 <encryption>AES</encryption>
1284 <useOneX>false</useOneX>
1285 </authEncryption>
1286 <sharedKey>
1287 <keyType>passPhrase</keyType>
1288 <protected>false</protected>
1289 <keyMaterial>{password}</keyMaterial>
1290 </sharedKey>
1291 </security>
1292 </MSM>
1293</WLANProfile>";
1294 }
1295
1296 /// <summary>
1297 /// Disconnects from the currently connected WiFi network.
1298 /// </summary>
1299 /// <summary>
1300 /// Sets the system text scaling factor (percentage).
1301 /// </summary>
1302 /// <param name="percentage">The text scaling percentage (100-225).</param>
1303 static void SetTextSize(int percentage)
1304 {
1305 try
1306 {
1307 if (percentage == -1)
1308 {
1309 percentage = new Random().Next(100, 225 + 1);
1310 }
1311
1312 // Clamp the percentage to valid range
1313 if (percentage < 100)
1314 {
1315 percentage = 100;
1316 }
1317 else if (percentage > 225)
1318 {
1319 percentage = 225;
1320 }
1321
1322 // Open the Settings app to the ease of access page
1323 Process.Start(new ProcessStartInfo
1324 {
1325 FileName = "ms-settings:easeofaccess",
1326 UseShellExecute = true
1327 });
1328
1329 // Use UI Automation to navigate and set the text size
1330 UIAutomation.SetTextSizeViaUIAutomation(percentage);
1331 }
1332 catch (Exception ex)
1333 {
1334 LogError(ex);
1335 }
1336 }
1337
1338 static void DisconnectFromWifi()
1339 {
1340 IntPtr clientHandle = IntPtr.Zero;
1341 IntPtr wlanInterfaceList = IntPtr.Zero;
1342
1343 try
1344 {
1345 // Open WLAN handle
1346 int result = WlanOpenHandle(2, IntPtr.Zero, out uint negotiatedVersion, out clientHandle);
1347 if (result != 0)
1348 {
1349 LogWarning($"Failed to open WLAN handle: {result}");
1350 return;
1351 }
1352
1353 // Enumerate wireless interfaces
1354 result = WlanEnumInterfaces(clientHandle, IntPtr.Zero, out wlanInterfaceList);
1355 if (result != 0)
1356 {
1357 LogWarning($"Failed to enumerate WLAN interfaces: {result}");
1358 return;
1359 }
1360
1361 WLAN_INTERFACE_INFO_LIST interfaceList = Marshal.PtrToStructure<WLAN_INTERFACE_INFO_LIST>(wlanInterfaceList);
1362
1363 if (interfaceList.dwNumberOfItems == 0)
1364 {
1365 LogWarning("No wireless interfaces found.");
1366 return;
1367 }
1368
1369 // Disconnect from all wireless interfaces
1370 for (int i = 0; i < interfaceList.dwNumberOfItems; i++)
1371 {
1372 WLAN_INTERFACE_INFO interfaceInfo = interfaceList.InterfaceInfo[i];
1373
1374 result = WlanDisconnect(clientHandle, ref interfaceInfo.InterfaceGuid, IntPtr.Zero);
1375 if (result != 0)
1376 {
1377 LogWarning($"Failed to disconnect from WiFi on interface {i}: {result}");
1378 }
1379 else
1380 {
1381 Debug.WriteLine($"Successfully disconnected from WiFi on interface: {interfaceInfo.strInterfaceDescription}");
1382 Console.WriteLine("Disconnected from WiFi");
1383 }
1384 }
1385 }
1386 catch (Exception ex)
1387 {
1388 LogError(ex);
1389 }
1390 finally
1391 {
1392 if (wlanInterfaceList != IntPtr.Zero)
1393 WlanFreeMemory(wlanInterfaceList);
1394 if (clientHandle != IntPtr.Zero)
1395 WlanCloseHandle(clientHandle, IntPtr.Zero);
1396 }
1397 }
1398}
1399