-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathapp_detector.py
More file actions
294 lines (254 loc) · 10.4 KB
/
Copy pathapp_detector.py
File metadata and controls
294 lines (254 loc) · 10.4 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
"""
Foreground application detector — polls the active window and fires
a callback when the foreground app changes.
Windows: GetForegroundWindow + QueryFullProcessImageNameW (with UWP resolution).
macOS: NSWorkspace.sharedWorkspace().frontmostApplication().
"""
import os
import sys
import threading
import time
# ==================================================================
# Platform-specific get_foreground_exe()
# ==================================================================
if sys.platform == "win32":
import ctypes
import ctypes.wintypes as wt
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
MAX_PATH = 260
user32.GetForegroundWindow.restype = wt.HWND
user32.GetWindowThreadProcessId.argtypes = [wt.HWND, ctypes.POINTER(wt.DWORD)]
user32.GetWindowThreadProcessId.restype = wt.DWORD
kernel32.OpenProcess.argtypes = [wt.DWORD, wt.BOOL, wt.DWORD]
kernel32.OpenProcess.restype = wt.HANDLE
kernel32.CloseHandle.argtypes = [wt.HANDLE]
kernel32.CloseHandle.restype = wt.BOOL
kernel32.QueryFullProcessImageNameW.argtypes = [
wt.HANDLE, wt.DWORD,
ctypes.c_wchar_p, ctypes.POINTER(wt.DWORD),
]
kernel32.QueryFullProcessImageNameW.restype = wt.BOOL
user32.FindWindowExW.argtypes = [wt.HWND, wt.HWND, wt.LPCWSTR, wt.LPCWSTR]
user32.FindWindowExW.restype = wt.HWND
user32.GetClassNameW.argtypes = [wt.HWND, ctypes.c_wchar_p, ctypes.c_int]
user32.GetClassNameW.restype = ctypes.c_int
WNDENUMPROC = ctypes.WINFUNCTYPE(wt.BOOL, wt.HWND, wt.LPARAM)
user32.EnumChildWindows.argtypes = [wt.HWND, WNDENUMPROC, wt.LPARAM]
user32.EnumChildWindows.restype = wt.BOOL
user32.EnumWindows.argtypes = [WNDENUMPROC, wt.LPARAM]
user32.EnumWindows.restype = wt.BOOL
user32.IsWindowVisible.argtypes = [wt.HWND]
user32.IsWindowVisible.restype = wt.BOOL
user32.GetWindowTextW.argtypes = [wt.HWND, ctypes.c_wchar_p, ctypes.c_int]
user32.GetWindowTextW.restype = ctypes.c_int
user32.GetWindowTextLengthW.argtypes = [wt.HWND]
user32.GetWindowTextLengthW.restype = ctypes.c_int
def _get_window_title(hwnd) -> str:
length = user32.GetWindowTextLengthW(hwnd)
if not length:
return ""
buf = ctypes.create_unicode_buffer(length + 1)
user32.GetWindowTextW(hwnd, buf, length + 1)
return buf.value
def _path_from_pid(pid: int) -> str | None:
hproc = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not hproc:
return None
try:
buf = ctypes.create_unicode_buffer(MAX_PATH)
size = wt.DWORD(MAX_PATH)
if kernel32.QueryFullProcessImageNameW(hproc, 0, buf, ctypes.byref(size)):
return buf.value
finally:
kernel32.CloseHandle(hproc)
return None
def _resolve_uwp_child(hwnd) -> str | None:
host_pid = wt.DWORD()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(host_pid))
result = [None]
def _enum_cb(child_hwnd, _lparam):
child_pid = wt.DWORD()
user32.GetWindowThreadProcessId(child_hwnd, ctypes.byref(child_pid))
if child_pid.value != host_pid.value:
exe_path = _path_from_pid(child_pid.value)
if exe_path and os.path.basename(exe_path).lower() != "applicationframehost.exe":
result[0] = exe_path
return False
return True
user32.EnumChildWindows(hwnd, WNDENUMPROC(_enum_cb), 0)
return result[0]
# Window classes that belong to genuine explorer.exe usage
_EXPLORER_CLASSES = frozenset({
"CabinetWClass", # File Explorer windows
"Shell_TrayWnd", # Taskbar
"Shell_SecondaryTrayWnd", # Taskbar on secondary monitors
"Progman", # Desktop
"WorkerW", # Desktop worker
})
def _get_window_class(hwnd) -> str:
cls = ctypes.create_unicode_buffer(256)
user32.GetClassNameW(hwnd, cls, 256)
return cls.value
def _find_uwp_app_global() -> str | None:
"""Enumerate all top-level windows to find a UWP app behind an overlay."""
result = [None]
def _enum_cb(hwnd, _lparam):
if not user32.IsWindowVisible(hwnd):
return True
pid = wt.DWORD()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
if not pid.value:
return True
exe_path = _path_from_pid(pid.value)
if exe_path and os.path.basename(exe_path).lower() == "applicationframehost.exe":
real = _resolve_uwp_child(hwnd)
if real:
result[0] = real
return False
return True
user32.EnumWindows(WNDENUMPROC(_enum_cb), 0)
return result[0]
def get_foreground_exe() -> str | None:
"""Return the foreground app path on Windows, or None."""
hwnd = user32.GetForegroundWindow()
if not hwnd:
return None
pid = wt.DWORD()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
if pid.value == 0:
return None
exe_path = _path_from_pid(pid.value)
if not exe_path:
return None
exe_lower = os.path.basename(exe_path).lower()
if exe_lower == "applicationframehost.exe":
real = _resolve_uwp_child(hwnd)
# If we can't resolve the real app (e.g. fullscreen UWP),
# return None so the detector keeps the last known profile.
return real
if exe_lower == "explorer.exe":
wc = _get_window_class(hwnd)
if wc not in _EXPLORER_CLASSES:
title = _get_window_title(hwnd)
print(f"[AppDetect] FG: explorer.exe class={wc} title='{title}'")
real = _resolve_uwp_child(hwnd)
if real:
return real
real = _find_uwp_app_global()
return real # None keeps last profile
return exe_path
elif sys.platform == "darwin":
import functools
try:
import objc as _objc
except ImportError as exc:
raise ImportError(
"PyObjC is required on macOS. Run "
"`python -m pip install -r requirements.txt`."
) from exc
def _autoreleased(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
with _objc.autorelease_pool():
return fn(*args, **kwargs)
return wrapper
@_autoreleased
def get_foreground_exe() -> str | None:
"""Return a stable app identifier for the frontmost app on macOS."""
try:
from AppKit import NSWorkspace
app = NSWorkspace.sharedWorkspace().frontmostApplication()
if app is None:
return None
ident = app.bundleIdentifier()
if ident:
return ident
url = app.executableURL()
if url:
return os.path.basename(url.path())
return app.localizedName()
except Exception:
return None
elif sys.platform == "linux":
import subprocess as _subprocess
_WAYLAND = os.environ.get("XDG_SESSION_TYPE", "").lower() == "wayland"
_KDE = "KDE" in os.environ.get("XDG_CURRENT_DESKTOP", "").upper()
def _pid_to_exe(pid: int) -> str | None:
try:
return os.readlink(f"/proc/{pid}/exe")
except OSError:
return None
def _get_foreground_xdotool() -> str | None:
"""X11: use xdotool."""
try:
result = _subprocess.run(
["xdotool", "getactivewindow", "getwindowpid"],
capture_output=True, text=True, timeout=1,
)
if result.returncode == 0 and result.stdout.strip():
return _pid_to_exe(int(result.stdout.strip()))
except (FileNotFoundError, ValueError, OSError, _subprocess.TimeoutExpired):
pass
return None
def _get_foreground_kdotool() -> str | None:
"""KDE Wayland: use kdotool."""
try:
result = _subprocess.run(
["kdotool", "getactivewindow", "getwindowpid"],
capture_output=True, text=True, timeout=1,
)
if result.returncode == 0 and result.stdout.strip():
return _pid_to_exe(int(result.stdout.strip()))
except (FileNotFoundError, ValueError, OSError, _subprocess.TimeoutExpired):
pass
return None
def get_foreground_exe() -> str | None:
"""Return the foreground app executable path on Linux."""
if _WAYLAND:
if _KDE:
exe = _get_foreground_kdotool()
if exe:
return exe
# Fall back to xdotool so XWayland apps still work when
# kdotool is unavailable or cannot resolve the active window.
return _get_foreground_xdotool()
# GNOME / other Wayland compositors: not yet supported
return None
return _get_foreground_xdotool()
else:
def get_foreground_exe() -> str | None:
return None
class AppDetector:
"""
Polls the foreground window every *interval* seconds.
Calls ``on_change(exe_name: str)`` when the foreground app changes.
"""
def __init__(self, on_change, interval: float = 0.3):
self._on_change = on_change
self._interval = interval
self._last_exe: str | None = None
self._stop = threading.Event()
self._thread: threading.Thread | None = None
def start(self):
if self._thread and self._thread.is_alive():
return
self._stop.clear()
self._thread = threading.Thread(target=self._poll, daemon=True, name="AppDetector")
self._thread.start()
def stop(self):
self._stop.set()
if self._thread:
self._thread.join(timeout=2)
# ------------------------------------------------------------------
def _poll(self):
while not self._stop.is_set():
try:
exe = get_foreground_exe()
if exe and exe != self._last_exe:
self._last_exe = exe
self._on_change(exe)
except Exception:
pass
self._stop.wait(self._interval)