Repository navigation
Expand file tree
/
Copy pathsystem_controller.py
More file actions
1408 lines (1246 loc) · 54.7 KB
/
Copy pathsystem_controller.py
File metadata and controls
1408 lines (1246 loc) · 54.7 KB
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
System Controller Module for PC Remote Sentinel
Handles OS hardware metrics, webcam, screen capture/recording, power, and audio controls.
"""
import os
import sys
import time
import math
import re
import threading
import ctypes
import ctypes.wintypes
import subprocess
import winsound
from datetime import datetime, timezone, timedelta
from pathlib import Path
from typing import Dict, Any, List, Optional, Tuple
import psutil
from PIL import ImageGrab
from loguru import logger
try:
import cv2
except ImportError:
cv2 = None
class SystemController:
"""Windows PC Controller for remote administration."""
@staticmethod
def get_system_stats() -> Dict[str, Any]:
"""Collect real-time CPU, RAM, Disk, Battery, and Uptime diagnostics."""
cpu_pct = psutil.cpu_percent(interval=0.5)
per_core = psutil.cpu_percent(percpu=True)
mem = psutil.virtual_memory()
disk = psutil.disk_usage("C:\\")
# Battery / Power status
battery = psutil.sensors_battery()
battery_info = "N/A (Desktop/AC)"
if battery:
plugged = "🔌 Plugged In" if battery.power_plugged else "🔋 On Battery"
battery_info = f"{battery.percent:.0f}% ({plugged})"
# Uptime
boot_time = datetime.fromtimestamp(psutil.boot_time())
uptime_delta = datetime.now() - boot_time
hours, remainder = divmod(int(uptime_delta.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
uptime_str = f"{hours}h {minutes}m {seconds}s"
return {
"cpu_percent": cpu_pct,
"per_core": per_core,
"memory_percent": mem.percent,
"memory_used_gb": round(mem.used / (1024 ** 3), 2),
"memory_total_gb": round(mem.total / (1024 ** 3), 2),
"disk_percent": disk.percent,
"disk_free_gb": round(disk.free / (1024 ** 3), 2),
"disk_total_gb": round(disk.total / (1024 ** 3), 2),
"battery": battery_info,
"uptime": uptime_str,
"boot_time": boot_time.strftime("%Y-%m-%d %H:%M:%S"),
"session_state": SystemController.get_session_state(),
}
@staticmethod
def ensure_desktop_access() -> bool:
"""Attach thread to active Windows input desktop."""
try:
try:
ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception:
pass
hdesk = ctypes.windll.user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
ctypes.windll.user32.SetThreadDesktop(hdesk)
return True
except Exception:
pass
return False
@staticmethod
def get_session_state() -> str:
"""Detect if Windows is at interactive desktop, lock screen, or Session 0."""
try:
hdesk = ctypes.windll.user32.OpenInputDesktop(0, False, 0x0100)
if hdesk:
buf = ctypes.create_unicode_buffer(256)
size = ctypes.c_uint(0)
ctypes.windll.user32.GetUserObjectInformationW(hdesk, 2, buf, 256, ctypes.byref(size))
desk = buf.value.lower()
if desk == "default":
return "Interactive Desktop (Unlocked) 🟢"
elif desk == "winlogon":
return "Windows Login / Lock Screen 🔒"
return f"{buf.value} 🖥️"
except Exception:
pass
return "Pre-Login / Session 0 🔒"
@staticmethod
def take_screenshot(output_path: Path) -> Tuple[bool, str]:
"""Capture full desktop screenshot with graceful pre-login detection."""
try:
SystemController.ensure_desktop_access()
screenshot = ImageGrab.grab(all_screens=True)
screenshot.save(output_path, "PNG")
return True, "Screenshot captured"
except Exception as e:
err = str(e)
logger.error(f"Screenshot error: {err}")
if "screen grab failed" in err.lower():
return False, "⚠️ Screen capture unavailable: PC is currently at the pre-login lock screen (Session 0). Please log into Windows to view active desktop."
return False, f"Screenshot error: {err}"
@staticmethod
def take_webcam_photo(output_path: Path, camera_index: int = 0) -> Tuple[bool, str]:
"""Capture a single photo from webcam."""
if cv2 is None:
return False, "OpenCV (cv2) is not installed."
cap = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW)
if not cap.isOpened():
return False, "Could not access webcam device."
try:
# Let sensor adjust for 3 frames
for _ in range(5):
cap.read()
ret, frame = cap.read()
if ret and frame is not None:
cv2.imwrite(str(output_path), frame)
return True, "Photo captured"
return False, "Failed to grab webcam frame"
except Exception as e:
return False, f"Webcam error: {e}"
finally:
cap.release()
@staticmethod
def record_webcam_video(output_path: Path, duration_sec: int = 10, camera_index: int = 0) -> Tuple[bool, str]:
"""Record video clip from webcam for specified seconds."""
if cv2 is None:
return False, "OpenCV is not installed."
cap = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW)
if not cap.isOpened():
return False, "Could not open webcam."
try:
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 640)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 480)
fps = 20.0
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(str(output_path), fourcc, fps, (width, height))
end_time = time.time() + duration_sec
while time.time() < end_time:
ret, frame = cap.read()
if ret and frame is not None:
out.write(frame)
time.sleep(0.04)
out.release()
return True, f"Recorded {duration_sec}s webcam clip"
except Exception as e:
return False, f"Webcam recording failed: {e}"
finally:
cap.release()
@staticmethod
def record_screen_video(output_path: Path, duration_sec: int = 10) -> Tuple[bool, str]:
"""Record desktop screen video for specified seconds (up to 120s)."""
if cv2 is None:
return False, "OpenCV is not installed."
try:
import numpy as np
screen = ImageGrab.grab()
width, height = screen.size
scale = 1.0
if width > 1920:
scale = 1920.0 / width
width = 1920
height = int(height * scale)
if width % 2 != 0:
width -= 1
if height % 2 != 0:
height -= 1
fps = 10.0
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(str(output_path), fourcc, fps, (width, height))
end_time = time.time() + duration_sec
frame_delay = 1.0 / fps
while time.time() < end_time:
t0 = time.time()
img = ImageGrab.grab()
frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
if scale != 1.0:
frame = cv2.resize(frame, (width, height))
out.write(frame)
elapsed = time.time() - t0
if elapsed < frame_delay:
time.sleep(frame_delay - elapsed)
out.release()
return True, f"Recorded {duration_sec}s screen video"
except Exception as e:
return False, f"Screen recording failed: {e}"
# ── Power & Session Management ──────────────────────────────────────
@staticmethod
def lock_workstation() -> bool:
"""Lock Windows PC instantly using user32 API, rundll32, or session disconnect."""
try:
res = ctypes.windll.user32.LockWorkStation()
if res != 0:
logger.info("Workstation locked via ctypes user32.LockWorkStation.")
return True
except Exception as e:
logger.debug(f"Direct LockWorkStation failed: {e}")
try:
res = subprocess.run(["rundll32.exe", "user32.dll,LockWorkStation"], shell=False, timeout=3)
if res.returncode == 0:
logger.info("Workstation locked via rundll32.")
return True
except Exception as e:
logger.debug(f"rundll32 LockWorkStation failed: {e}")
try:
res = subprocess.run(["tsdiscon"], shell=False, timeout=3)
if res.returncode == 0:
logger.info("Workstation locked via tsdiscon.")
return True
except Exception as e:
logger.error(f"All workstation lock strategies failed: {e}")
return False
return False
@staticmethod
def sleep_pc() -> bool:
"""Put PC into sleep state using powrprof API and powershell fallback."""
try:
res = subprocess.run(["rundll32.exe", "powrprof.dll,SetSuspendState", "0,1,0"], shell=False, timeout=5)
if res.returncode == 0:
return True
except Exception as e:
logger.debug(f"rundll32 SetSuspendState failed: {e}")
try:
res = subprocess.run(
["powershell", "-NoProfile", "-Command", "Add-Type -Assembly System.Windows.Forms; [System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false)"],
capture_output=True,
timeout=5
)
return res.returncode == 0
except Exception as e:
logger.error(f"Sleep error: {e}")
return False
@staticmethod
def shutdown_pc(seconds: int = 0) -> bool:
"""Shutdown PC now or with timer."""
try:
subprocess.run(["shutdown", "/s", "/t", str(seconds)], check=True)
return True
except Exception as e:
logger.error(f"Shutdown error: {e}")
return False
@staticmethod
def restart_pc(seconds: int = 0) -> bool:
"""Restart PC now or with timer."""
try:
subprocess.run(["shutdown", "/r", "/t", str(seconds)], check=True)
return True
except Exception as e:
logger.error(f"Restart error: {e}")
return False
@staticmethod
def cancel_shutdown() -> bool:
"""Cancel any pending shutdown timer."""
try:
subprocess.run(["shutdown", "/a"], check=True)
return True
except Exception:
return False
@staticmethod
def turn_off_monitors() -> bool:
"""Put monitors to sleep immediately (wakes up on mouse move/key press)."""
try:
HWND_BROADCAST = 0xFFFF
WM_SYSCOMMAND = 0x0112
SC_MONITORPOWER = 0xF170
MONITOR_OFF = 2
ctypes.windll.user32.SendMessageW(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF)
return True
except Exception as e:
logger.debug(f"Direct SendMessageW monitor off failed: {e}")
try:
cmd = "Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class M { [DllImport(\"user32.dll\")] public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam); }'; [M]::SendMessage(0xffff, 0x0112, 0xf170, 2)"
res = subprocess.run(["powershell", "-NoProfile", "-Command", cmd], capture_output=True, timeout=5)
return res.returncode == 0
except Exception as e:
logger.error(f"Monitor off error: {e}")
return False
@staticmethod
def turn_on_monitors() -> bool:
"""Wake up and power on monitors immediately."""
try:
HWND_BROADCAST = 0xFFFF
WM_SYSCOMMAND = 0x0112
SC_MONITORPOWER = 0xF170
MONITOR_ON = -1
# 1. Broadcast WM_SYSCOMMAND to power on display
ctypes.windll.user32.SendMessageW(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON)
# 2. Inform power subsystem that display is required (resets idle timers)
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ctypes.windll.kernel32.SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED)
# 3. Simulate relative hardware mouse movement to wake physical display controller
MOUSEEVENTF_MOVE = 0x0001
ctypes.windll.user32.mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0)
time.sleep(0.02)
ctypes.windll.user32.mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0)
return True
except Exception as e:
logger.error(f"Monitor on error: {e}")
return False
# ── Audio & Speech Controls ─────────────────────────────────────────
@staticmethod
def control_media(action: str) -> bool:
"""
Adjust volume and media playback via Windows virtual keystrokes with extended scan codes.
Executes a clean, single hardware-level keystroke without duplicate broadcasts to prevent double-toggling.
action: 'up', 'down', 'mute', 'play_pause', 'play', 'pause', 'next', 'prev', 'stop'
"""
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
# Mapping: (VK_CODE, SCAN_CODE)
key_map = {
"up": (0xAF, 0x30), # VK_VOLUME_UP
"down": (0xAE, 0x2E), # VK_VOLUME_DOWN
"mute": (0xAD, 0x20), # VK_VOLUME_MUTE
"play_pause": (0xCD, 0x22), # VK_MEDIA_PLAY_PAUSE
"play": (0xCD, 0x22), # VK_MEDIA_PLAY_PAUSE
"pause": (0xCD, 0x22), # VK_MEDIA_PLAY_PAUSE
"next": (0xB0, 0x19), # VK_MEDIA_NEXT_TRACK
"prev": (0xB1, 0x10), # VK_MEDIA_PREV_TRACK
"stop": (0xB2, 0x24), # VK_MEDIA_STOP
}
entry = key_map.get(action.lower())
if not entry:
return False
vk, scan = entry
try:
# Simulate a single genuine extended hardware media key press
ctypes.windll.user32.keybd_event(vk, scan, KEYEVENTF_EXTENDEDKEY, 0)
time.sleep(0.05)
ctypes.windll.user32.keybd_event(vk, scan, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0)
return True
except Exception as e:
logger.error(f"Media control error: {e}")
return False
@staticmethod
def change_volume(action: str) -> bool:
"""Backward-compatible alias for control_media."""
return SystemController.control_media(action)
@staticmethod
def press_media_key(action: str) -> Tuple[bool, str]:
"""Convenience method returning (ok, msg) tuple for media key actions."""
ok = SystemController.control_media(action)
return ok, f"Media key '{action}' triggered" if ok else f"Unknown or failed media key: {action}"
@staticmethod
def get_active_window_title_fast() -> str:
"""Return title of current foreground window quickly without external imports."""
try:
SystemController.ensure_desktop_access()
user32 = ctypes.windll.user32
h = user32.GetForegroundWindow()
if h:
length = user32.GetWindowTextLengthW(h)
if length > 0:
buf = ctypes.create_unicode_buffer(length + 1)
user32.GetWindowTextW(h, buf, length + 1)
t = buf.value.strip()
if t:
return t
except Exception:
pass
return "Desktop"
@staticmethod
def switch_virtual_desktop(action: str) -> Tuple[bool, str]:
"""
Switch, create, or navigate Windows Virtual Desktops via hardware key events.
action: 'next', 'prev', 'new', 'close', 'task_view', 'show_desktop'
"""
SystemController.ensure_desktop_access()
action = action.lower().strip()
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
VK_LWIN = 0x5B
VK_CONTROL = 0x11
VK_RIGHT = 0x27
VK_LEFT = 0x25
VK_D = 0x44
VK_F4 = 0x73
VK_TAB = 0x09
if action in ("next", "right"):
primary, use_ctrl, ext, label = VK_RIGHT, True, True, "Next Desktop (Win+Ctrl+Right)"
elif action in ("prev", "previous", "left"):
primary, use_ctrl, ext, label = VK_LEFT, True, True, "Previous Desktop (Win+Ctrl+Left)"
elif action in ("new", "create"):
primary, use_ctrl, ext, label = VK_D, True, False, "New Virtual Desktop (Win+Ctrl+D)"
elif action in ("close", "remove"):
primary, use_ctrl, ext, label = VK_F4, True, False, "Close Virtual Desktop (Win+Ctrl+F4)"
elif action in ("task_view", "overview", "tab"):
primary, use_ctrl, ext, label = VK_TAB, False, False, "Task View (Win+Tab)"
elif action in ("show_desktop", "desktop", "minimize_all"):
primary, use_ctrl, ext, label = VK_D, False, False, "Show Desktop (Win+D)"
else:
return False, f"Unknown desktop action '{action}'. Valid: next, prev, new, close, task_view, show_desktop"
user32 = ctypes.windll.user32
try:
# Press Windows key
user32.keybd_event(VK_LWIN, 0, 0, 0)
time.sleep(0.02)
if use_ctrl:
user32.keybd_event(VK_CONTROL, 0, 0, 0)
time.sleep(0.02)
# Press primary key
flags = KEYEVENTF_EXTENDEDKEY if ext else 0
user32.keybd_event(primary, 0, flags, 0)
time.sleep(0.06)
# Release primary key
user32.keybd_event(primary, 0, flags | KEYEVENTF_KEYUP, 0)
time.sleep(0.03)
except Exception as e:
logger.error(f"Virtual desktop switch error: {e}")
return False, f"Error switching desktop: {e}"
finally:
# Always guarantee modifier keys are released to avoid sticky keys
if use_ctrl:
user32.keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
time.sleep(0.01)
user32.keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)
time.sleep(0.12)
active = SystemController.get_active_window_title_fast()
return True, f"Switched desktop: {label} (Active: {active})"
@staticmethod
def cycle_window(direction: str = "next") -> Tuple[bool, str]:
"""
Cycle between open application windows.
First tries to advance sequentially through open application windows (Alt+Tab equivalent).
Falls back to hardware Alt+Tab key events.
"""
SystemController.ensure_desktop_access()
direction = direction.lower().strip()
is_prev = direction in ("prev", "previous", "back", "left")
# 1. Smart sequential window list cycling
try:
wins = SystemController.get_open_windows(limit=25)
if len(wins) > 1:
cur_hwnd = ctypes.windll.user32.GetForegroundWindow()
cur_idx = -1
for i, w in enumerate(wins):
if w["hwnd"] == cur_hwnd:
cur_idx = i
break
if cur_idx == -1:
target_idx = 1 if not is_prev else len(wins) - 1
else:
target_idx = (cur_idx - 1) % len(wins) if is_prev else (cur_idx + 1) % len(wins)
target_w = wins[target_idx]
ok, _ = SystemController.focus_window(target_w["hwnd"])
if ok:
lbl = "Prev Window (Alt+Shift+Tab)" if is_prev else "Next Window (Alt+Tab)"
return True, f"{lbl} -> {target_w['title']}"
except Exception as e:
logger.debug(f"Smart cycle fallback: {e}")
# 2. Hardware Alt+Tab fallback
VK_MENU = 0x12 # Alt
VK_SHIFT = 0x10 # Shift
VK_TAB = 0x09 # Tab
KEYEVENTF_KEYUP = 0x0002
user32 = ctypes.windll.user32
try:
user32.keybd_event(VK_MENU, 0, 0, 0)
time.sleep(0.02)
if is_prev:
user32.keybd_event(VK_SHIFT, 0, 0, 0)
time.sleep(0.02)
user32.keybd_event(VK_TAB, 0, 0, 0)
time.sleep(0.05)
user32.keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0)
time.sleep(0.02)
except Exception as e:
logger.error(f"Cycle window error: {e}")
return False, f"Error cycling window: {e}"
finally:
if is_prev:
user32.keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0)
user32.keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
time.sleep(0.12)
active = SystemController.get_active_window_title_fast()
lbl = "Prev Window (Alt+Shift+Tab)" if is_prev else "Next Window (Alt+Tab)"
return True, f"Switched to {lbl} -> {active}"
@staticmethod
def get_open_windows(limit: int = 25) -> List[Dict[str, Any]]:
"""
Enumerate all active, visible top-level application windows with titles.
Includes minimized application windows so they can be brought to focus.
Filters out invisible helper / background system utility windows.
"""
SystemController.ensure_desktop_access()
user32 = ctypes.windll.user32
WNDENUMPROC = ctypes.WINFUNCTYPE(ctypes.wintypes.BOOL, ctypes.wintypes.HWND, ctypes.wintypes.LPARAM)
ignored_titles = {
"program manager", "settings", "windows input experience", "setup",
"microsoft text input application", "task switching", "nvidia geforce overlay",
"desktopwindowcontentbridge", "screen clipping host"
}
ignored_processes = {
"applicationframehost.exe", "shellexperiencehost.exe"
}
windows = []
def enum_cb(hwnd, lparam):
if not user32.IsWindowVisible(hwnd):
return True
length = user32.GetWindowTextLengthW(hwnd)
if length <= 0:
return True
buff = ctypes.create_unicode_buffer(length + 1)
user32.GetWindowTextW(hwnd, buff, length + 1)
title = buff.value.strip()
if not title or title.lower() in ignored_titles:
return True
is_iconic = bool(user32.IsIconic(hwnd))
rect = ctypes.wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rect))
w = rect.right - rect.left
h = rect.bottom - rect.top
# Skip tiny invisible helper windows if not minimized
if not is_iconic and (w < 60 or h < 60):
return True
pid = ctypes.wintypes.DWORD()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
pname = ""
try:
pname = psutil.Process(pid.value).name()
except Exception:
pass
if pname.lower() in ignored_processes and title.lower() in ("settings", "start", "search"):
return True
windows.append({
"hwnd": hwnd,
"title": title,
"process": pname,
"pid": pid.value,
"is_minimized": is_iconic,
})
return True
cb_proc = WNDENUMPROC(enum_cb)
try:
user32.EnumWindows(cb_proc, 0)
except Exception as e:
logger.error(f"EnumWindows error: {e}")
# Put active window first if found
try:
fg = user32.GetForegroundWindow()
windows.sort(key=lambda x: (x["hwnd"] != fg))
except Exception:
pass
return windows[:limit]
@staticmethod
def focus_window(target: Any) -> Tuple[bool, str]:
"""
Bring a specific application window to the foreground by HWND or partial title search.
Restores window if minimized. Uses SwitchToThisWindow and input simulation to bypass Windows focus restrictions.
"""
SystemController.ensure_desktop_access()
user32 = ctypes.windll.user32
open_wins = SystemController.get_open_windows(limit=50)
target_hwnd = None
target_title = ""
if isinstance(target, int) or (isinstance(target, str) and target.strip().isdigit()):
h_int = int(target)
for w in open_wins:
if w["hwnd"] == h_int:
target_hwnd = h_int
target_title = w["title"]
break
if not target_hwnd and user32.IsWindow(h_int):
target_hwnd = h_int
target_title = f"Window {h_int}"
elif isinstance(target, str):
q = target.lower().strip()
for w in open_wins:
if q == w["process"].lower() or q in w["title"].lower() or q in w["process"].lower():
target_hwnd = w["hwnd"]
target_title = w["title"]
break
if not target_hwnd:
return False, f"Could not find open window matching '{target}'"
try:
# 1. Restore if minimized
SW_RESTORE = 9
SW_SHOW = 5
if user32.IsIconic(target_hwnd):
user32.ShowWindow(target_hwnd, SW_RESTORE)
else:
user32.ShowWindow(target_hwnd, SW_SHOW)
# 2. Use Microsoft SwitchToThisWindow API
try:
user32.SwitchToThisWindow(target_hwnd, True)
except Exception:
pass
# 3. Simulate Alt key down/up to bypass Windows SetForegroundWindow lock
VK_MENU = 0x12
KEYEVENTF_KEYUP = 0x0002
user32.keybd_event(VK_MENU, 0, 0, 0)
user32.keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
# 4. Bring window to top and set foreground
user32.BringWindowToTop(target_hwnd)
user32.SetForegroundWindow(target_hwnd)
time.sleep(0.08)
return True, f"Focused: {target_title}"
except Exception as e:
logger.error(f"Focus window error: {e}")
return False, f"Error focusing window: {e}"
@staticmethod
def speak_text(text: str) -> bool:
"""Speak message aloud on PC speakers using Windows Speech Synthesis."""
try:
safe_text = text.replace("'", "''").replace('"', '`"')
cmd = f'Add-Type -AssemblyName System.Speech; $synth = New-Object System.Speech.Synthesis.SpeechSynthesizer; $synth.Speak("{safe_text}")'
subprocess.Popen(["powershell", "-NoProfile", "-Command", cmd])
return True
except Exception as e:
logger.error(f"TTS error: {e}")
return False
@staticmethod
def play_audio_file(file_path: Path) -> Tuple[bool, str]:
"""Play any audio file (Telegram voice note .ogg, .mp3, .wav, etc.) through PC speakers."""
try:
target_path = file_path
temp_wav = None
# Telegram voice notes are Opus in OGG; convert to PCM WAV for native Windows audio playback
if file_path.suffix.lower() != ".wav":
temp_wav = file_path.with_suffix(".temp.wav")
try:
import av
in_container = av.open(str(file_path))
out_container = av.open(str(temp_wav), mode="w", format="wav")
in_stream = in_container.streams.audio[0]
out_stream = out_container.add_stream("pcm_s16le", rate=44100, layout="stereo")
resampler = av.AudioResampler(format="s16", layout="stereo", rate=44100)
for frame in in_container.decode(in_stream):
for rf in resampler.resample(frame):
for packet in out_stream.encode(rf):
out_container.mux(packet)
for packet in out_stream.encode(None):
out_container.mux(packet)
out_container.close()
in_container.close()
target_path = temp_wav
except Exception as conv_err:
logger.warning(f"Audio conversion warning: {conv_err}")
# Play using winsound SND_FILENAME
try:
winsound.PlaySound(str(target_path), winsound.SND_FILENAME)
except Exception:
try:
import playsound3
playsound3.playsound(str(target_path))
except Exception as ps_err:
return False, f"Audio playback failed: {ps_err}"
# Clean up temporary WAV file
if temp_wav and temp_wav.exists():
try:
temp_wav.unlink()
except Exception:
pass
return True, "Audio played on PC speakers"
except Exception as e:
logger.error(f"Failed to play audio: {e}")
return False, str(e)
@staticmethod
def play_alert_siren(label: str = "Instant Alarm") -> None:
"""Play alert siren sounds and voice alert immediately."""
alarm_manager.trigger_alarm_now(label)
@staticmethod
def set_alarm(seconds: int, label: str = "Scheduled Alarm", callback=None) -> Dict[str, Any]:
"""Schedule a PC alarm timer with customizable duration and label."""
return alarm_manager.set_alarm(seconds, label, callback)
@staticmethod
def cancel_alarm() -> bool:
"""Cancel any pending countdown/scheduled alarm."""
return alarm_manager.cancel_alarm()
@staticmethod
def stop_alarm() -> bool:
"""Silence any currently ringing alarm."""
return alarm_manager.stop_alarm()
@staticmethod
def get_alarm_status() -> Dict[str, Any]:
"""Fetch real-time alarm status (idle, scheduled, or ringing)."""
return alarm_manager.get_status()
@staticmethod
def parse_alarm_time(text: str) -> Tuple[Optional[int], str]:
"""Parse customizable time inputs (relative '10m', '45s', or clock '18:30')."""
return AlarmManager.parse_time_input(text)
# ── Task Manager & Utilities ────────────────────────────────────────
@staticmethod
def list_top_processes(limit: int = 10) -> List[Dict[str, Any]]:
"""List top processes sorted by memory and active CPU consumption."""
procs = []
for p in psutil.process_iter(['pid', 'name', 'memory_percent']):
try:
info = p.info
name = info['name'] or "Unknown"
procs.append({
"pid": info['pid'],
"name": name,
"mem": round(info['memory_percent'] or 0.0, 1),
"_proc": p,
})
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
# Sort primarily by memory usage descending
procs.sort(key=lambda x: x['mem'], reverse=True)
top = procs[:limit]
# Fast sampling for active CPU usage
for item in top:
try:
item['_proc'].cpu_percent()
except Exception:
pass
time.sleep(0.1)
for item in top:
try:
item['cpu'] = round(item['_proc'].cpu_percent(), 1)
except Exception:
item['cpu'] = 0.0
del item['_proc']
return top
PROTECTED_PROCESSES = {
"csrss.exe", "smss.exe", "services.exe", "wininit.exe",
"lsass.exe", "winlogon.exe", "system", "registry"
}
@staticmethod
def kill_process(identifier: str) -> Tuple[bool, str]:
"""
Kill process by name or PID with strict safety checks to prevent
interfering with other critical processes or trading bots.
"""
target = identifier.strip()
killed = 0
my_pids = {os.getpid()}
try:
my_pids.add(os.getppid())
except Exception:
pass
# 1. Protect against blanket termination of Python interpreters by name
# to prevent terminating user trading bots or other critical Python applications.
if target.lower() in ("python.exe", "pythonw.exe", "python"):
return False, (
"🛡️ Safety Shield: Terminating Python by name ('python.exe') is restricted to prevent "
"accidentally killing other running Python applications (such as trading bots or background workers). "
"Please specify the exact Process PID instead: '/kill <PID>'."
)
# 2. Protect against terminating critical Windows system components
if target.lower() in SystemController.PROTECTED_PROCESSES:
return False, f"🛡️ Safety Shield: Cannot terminate critical Windows system process: '{target}'."
if target.isdigit():
pid = int(target)
if pid in (0, 4):
return False, f"🛡️ Safety Shield: Refusing to terminate Windows System core process (PID {pid})."
if pid in my_pids:
return False, f"🛡️ Safety Shield: Refusing to terminate Sentinel's own process (PID {pid})."
try:
p = psutil.Process(pid)
pname = p.name().lower()
if pname in SystemController.PROTECTED_PROCESSES:
return False, f"🛡️ Safety Shield: Cannot terminate critical Windows process: {p.name()} (PID {pid})."
p.kill()
return True, f"Killed process {p.name()} (PID {pid})"
except psutil.NoSuchProcess:
return False, f"No process found with PID {pid}."
except psutil.AccessDenied:
return False, f"Access denied trying to terminate PID {pid} (insufficient privileges)."
except Exception as e:
return False, f"Could not kill PID {pid}: {e}"
else:
for p in psutil.process_iter(['name', 'pid']):
try:
if p.pid in my_pids:
continue
if p.info['name'] and p.info['name'].lower() == target.lower():
p.kill()
killed += 1
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
if killed > 0:
return True, f"Terminated {killed} instance{'s' if killed > 1 else ''} of '{target}'"
return False, f"No running process found matching '{target}'"
@staticmethod
def get_clipboard() -> str:
"""Read current text contents from Windows clipboard."""
try:
import win32clipboard, win32con
win32clipboard.OpenClipboard()
try:
if win32clipboard.IsClipboardFormatAvailable(win32con.CF_UNICODETEXT):
data = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
return data if data else "[Clipboard text is empty]"
return "[Clipboard does not contain text]"
finally:
win32clipboard.CloseClipboard()
except Exception as e:
logger.error(f"Clipboard read error: {e}")
return f"[Error reading clipboard: {e}]"
@staticmethod
def set_clipboard(text: str) -> bool:
"""Copy text to Windows clipboard."""
try:
import win32clipboard, win32con
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text)
return True
finally:
win32clipboard.CloseClipboard()
except Exception as e:
logger.error(f"Clipboard write error: {e}")
return False
@staticmethod
def scan_storage_candidates(recordings_dir: Path, downloads_dir: Path) -> List[Dict[str, Any]]:
"""
Scan recordings and temporary downloads folders for cleanup candidates without deleting anything.
Returns list of metadata dicts (path, name, folder, size_bytes, size_mb, age_str).
"""
candidates = []
now = time.time()
for folder_type, folder in [("recordings", recordings_dir), ("downloads", downloads_dir)]:
if not folder.exists():
continue
for item in folder.iterdir():
if item.is_file():
try:
stat = item.stat()
age_sec = max(0, now - stat.st_mtime)
if age_sec < 60:
age_str = f"{int(age_sec)}s ago"
elif age_sec < 3600:
age_str = f"{int(age_sec / 60)}m ago"
elif age_sec < 86400:
age_str = f"{int(age_sec / 3600)}h ago"
else:
age_str = f"{int(age_sec / 86400)}d ago"
candidates.append({
"path": str(item.resolve()),
"name": item.name,
"folder": folder_type,
"size_bytes": stat.st_size,
"size_mb": round(stat.st_size / (1024 * 1024), 2),
"age_str": age_str,
})
except Exception as e:
logger.warning(f"Could not stat {item.name}: {e}")
return candidates
@staticmethod
def delete_storage_files(file_paths: List[str], allowed_dirs: List[Path]) -> Tuple[int, int]:
"""
Securely delete explicitly approved files. Validates that each file path resides
strictly within the allowed bot directories to prevent path traversal attacks.
Returns (deleted_count, freed_bytes).
"""
deleted_count = 0
freed_bytes = 0
resolved_allowed = [d.resolve() for d in allowed_dirs if d.exists()]
for path_str in file_paths:
try:
p = Path(path_str).resolve()
if not any(str(p).startswith(str(d)) for d in resolved_allowed):
logger.warning(f"Security: Refusing to delete file outside allowed dirs: {p}")
continue
if p.is_file():
sz = p.stat().st_size
p.unlink()
deleted_count += 1
freed_bytes += sz
except Exception as e:
logger.error(f"Error deleting file {path_str}: {e}")
return deleted_count, freed_bytes
@staticmethod
def cleanup_old_files(recordings_dir: Path, downloads_dir: Path, max_age_days: int = 3) -> Tuple[int, int]:
"""Delete recordings and temp files older than max_age_days. Returns (count, bytes_freed)."""
deleted_count = 0
freed_bytes = 0
now = time.time()
max_age_sec = max_age_days * 86400
for folder in [recordings_dir, downloads_dir]:
if not folder.exists():
continue
for item in folder.iterdir():
if item.is_file():
try: