Skip to content

Commit 13a132d

Browse files
committed
Add win32 support for listing windows
1 parent 79b144f commit 13a132d

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This project is inspired by [Game2Text] but takes a different technical approach
1010

1111
## Known limitations
1212

13-
- Currently **only supported on Linux**. Porting it to macOS and Windows should
14-
be doable by adding the ability to list windows (see `windows_list.py`, patch
13+
- Currently **only supported on Linux and Windows**. Porting it to macOS should
14+
be doable by adding the ability to list windows (see `windows_list/windows_list_osx.py`, patch
1515
welcome)
1616
- **No configuration**, only designed for my own needs so far
1717
- A bit **slow to start** due to the OCR model initialization
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
3+
if sys.platform.startswith("win"):
4+
from .windows_list_win32 import WindowsList
5+
elif sys.platform.startswith("darwin"):
6+
from .windows_list_osx import WindowsList
7+
else:
8+
from .windows_list_x11 import WindowsList
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class WindowsList:
2+
def __call__(self) -> dict[int, tuple[str, bool]]:
3+
raise NotImplementedError("OSX not supported. Patch welcome.")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import ctypes
2+
from ctypes import wintypes
3+
4+
_USER32 = ctypes.windll.user32
5+
6+
7+
class WindowsList:
8+
def __call__(self) -> dict[int, tuple[str, bool]]:
9+
self.windows = {}
10+
WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
11+
cb_worker = WNDENUMPROC(self._enum_windows_cb)
12+
_USER32.EnumWindows(cb_worker, None)
13+
return self.windows
14+
15+
def _enum_windows_cb(self, hwnd: wintypes.HWND, _: wintypes.LPARAM):
16+
"""Callback for EnumWindows.
17+
18+
See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633498(v=vs.85)
19+
"""
20+
length = _USER32.GetWindowTextLengthW(hwnd) + 1
21+
buffer = ctypes.create_unicode_buffer(length)
22+
_USER32.GetWindowTextW(hwnd, buffer, length)
23+
if _USER32.IsWindowVisible(hwnd) and len(buffer.value) > 0:
24+
self.windows[hwnd] = (buffer.value, True)
25+
return True
File renamed without changes.

0 commit comments

Comments
 (0)