Skip to content

Commit 87c7104

Browse files
committed
ctypes method
1 parent 7eaa397 commit 87c7104

3 files changed

Lines changed: 34 additions & 29 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Python
1717
uses: actions/setup-python@v5
1818
with:
19-
python-version: '3.13'
19+
python-version: "3.13"
2020

2121
- name: Create virtual environment
2222
run: |
@@ -66,7 +66,7 @@ jobs:
6666
- name: Set up Python
6767
uses: actions/setup-python@v5
6868
with:
69-
python-version: '3.13'
69+
python-version: "3.13"
7070

7171
- name: Create virtual environment
7272
run: |

src/core/logic/app_tracker.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _start_tracking(self):
6161

6262
def _fetch_app_names(self):
6363
apps = []
64-
seen_names = ["AppUsageGUI", "Python"]
64+
seen_names = ["AppUsageGUI", "Python", "Python3"]
6565

6666
for process in psutil.process_iter(["pid", "name"]):
6767
try:
@@ -133,6 +133,9 @@ def start_filter_reset(self, refresh=False, update_pids=False):
133133
self.temp_reset_thread.start()
134134

135135
def _reset_excluded_pids(self, refresh, update_pids):
136+
logger.info(
137+
f"Resetting excluded PIDs (refresh={refresh}, update_pids={update_pids})"
138+
)
136139
global EXCLUDED_APP_PIDS
137140
global INCLUDED_APP_PIDS
138141
EXCLUDED_APP_PIDS = []
@@ -184,30 +187,37 @@ def _update_excluded_apps(self):
184187
def _has_gui(self, process_id):
185188
if os.name == "nt":
186189
try:
187-
p = psutil.Process(process_id)
188-
exe_path = p.exe().lower()
189-
name = p.name().lower()
190-
191-
# Known GUI apps in System32 to KEEP
192-
GUI_WHITELIST = {
193-
"notepad.exe",
194-
"mspaint.exe",
195-
"calc.exe",
196-
"explorer.exe",
197-
"taskmgr.exe",
198-
"winver.exe",
199-
}
190+
# Not the ideal method, but until pywinauto supports free-threaded builds, this is the best we can do
191+
import ctypes
192+
from ctypes import wintypes
200193

201-
if (
202-
exe_path.startswith("C:\\Windows\\System32\\")
203-
and name not in GUI_WHITELIST
204-
):
205-
logger.warning("SYSTEM32")
206-
return False
194+
found_window = [False]
195+
196+
# Define the callback function type
197+
WNDENUMPROC = ctypes.WINFUNCTYPE(
198+
wintypes.BOOL, wintypes.HWND, wintypes.LPARAM
199+
)
200+
201+
def enum_callback(hwnd, lparam):
202+
# Get window PID
203+
window_pid = wintypes.DWORD()
204+
ctypes.windll.user32.GetWindowThreadProcessId(
205+
hwnd, ctypes.byref(window_pid)
206+
)
207207

208-
return p.exe()
208+
# Check if this window belongs to our PID and is visible
209+
if window_pid.value == process_id:
210+
if ctypes.windll.user32.IsWindowVisible(hwnd):
211+
found_window[0] = True
212+
return False # Stop enumerating
209213

210-
except (RuntimeError, psutil.NoSuchProcess, psutil.AccessDenied):
214+
return True # Continue enumerating
215+
216+
callback = WNDENUMPROC(enum_callback)
217+
ctypes.windll.user32.EnumWindows(callback, 0)
218+
return found_window[0]
219+
220+
except RuntimeError:
211221
logger.warning(f"Process {process_id} not found or inaccessible")
212222
return True
213223
elif sys.platform == "darwin":

src/core/utils/tk_utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,15 @@ def center(win):
6969

7070
# Get actual window and screen sizes
7171
width = win.winfo_width()
72-
logger.info(f"width={width}")
7372
height = win.winfo_height()
74-
logger.info(f"height={height}")
7573
screen_width = win.winfo_screenwidth()
76-
logger.info(f"screen_width={screen_width}")
7774
screen_height = win.winfo_screenheight()
78-
logger.info(f"screen_height={screen_height}")
7975

8076
# Convert percentage offsets to pixels
8177

8278
# Compute center position
8379
x = int((screen_width / 2) - (width / 2))
8480
y = int((screen_height / 2) - (height / 2))
85-
logger.info(f"x={x}, y={y}")
8681

8782
win.geometry(f"{width}x{height}+{x}+{y}")
8883

0 commit comments

Comments
 (0)