-
-
Notifications
You must be signed in to change notification settings - Fork 112
Adjust Windows to match the Linux backend setup #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Microsoft Windows backend dispatcher for MSS screenshot implementations.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from typing import Any | ||
|
|
||
| from mss.base import MSS as _MSS | ||
| from mss.base import MSSImplementation | ||
| from mss.exception import ScreenShotError | ||
|
|
||
| # Re-export the GDI implementation so existing code that references | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary; MSSImplWindows was never publicly released. |
||
| # ``mss.windows.MSSImplWindows`` keeps working. | ||
| from mss.windows.gdi import MSSImplWindows | ||
|
|
||
| __all__ = ["MSS"] | ||
|
|
||
| BACKENDS = ["default"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest adding an explicit "gdi", similar to how the Linux implementation has all the backends available by explicit request. Currently, of course, this would be the same as "default". The intention, though, is that "default" will eventually be able to look at things like the Windows version to choose the backend to use. |
||
|
|
||
|
|
||
| class MSS(_MSS): | ||
| """Deprecated Microsoft Windows compatibility constructor. | ||
|
|
||
| Use :class:`mss.MSS` instead. | ||
| """ | ||
|
|
||
| def __init__(self, /, **kwargs: Any) -> None: | ||
| # TODO(jholveck): #493 Remove compatibility constructor after 10.x transition period. | ||
| warnings.warn( | ||
| "mss.windows.MSS is deprecated and will be removed in 11.0; use mss.MSS instead", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| super().__init__(**kwargs) | ||
|
|
||
|
|
||
| def choose_impl(backend: str = "default", **kwargs: Any) -> MSSImplementation: | ||
| """Return a backend-specific MSS implementation for Microsoft Windows. | ||
|
|
||
| Selects and instantiates the appropriate Windows backend based on the | ||
| ``backend`` parameter. | ||
|
|
||
| :param backend: Backend selector. Valid values: | ||
|
|
||
| - ``"default"`` (default): GDI-based backend using ``BitBlt`` and | ||
| ``CreateDIBSection`` for direct memory access to pixel data. | ||
|
|
||
| .. versionadded:: 10.3.0 Prior to this version, Windows had a single | ||
| implementation selected through :class:`mss.windows.MSS`. | ||
|
|
||
| :param kwargs: Additional keyword arguments passed to the backend class. | ||
| :returns: An MSS backend implementation. | ||
|
|
||
| .. versionadded:: 10.3.0 Prior to this version, this didn't exist: | ||
| Windows had a single implementation selected through | ||
| :class:`mss.windows.MSS`. | ||
| """ | ||
| backend = backend.lower() | ||
| if backend == "default": | ||
| return MSSImplWindows(**kwargs) | ||
| assert backend not in BACKENDS # noqa: S101 | ||
| msg = f"Backend {backend!r} not (yet?) implemented." | ||
| raise ScreenShotError(msg) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| """Windows GDI-based backend for MSS. | ||
| """GDI-based backend for MSS on Microsoft Windows. | ||
|
|
||
| Uses user32/gdi32 APIs to capture the desktop and enumerate monitors. | ||
| This implementation uses CreateDIBSection for direct memory access to pixel data. | ||
|
|
@@ -8,7 +8,6 @@ | |
|
|
||
| import ctypes | ||
| import sys | ||
| import warnings | ||
| from ctypes import POINTER, WINFUNCTYPE, Structure, WinError, _Pointer | ||
| from ctypes.wintypes import ( | ||
| BOOL, | ||
|
|
@@ -31,7 +30,6 @@ | |
| ) | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from mss.base import MSS as _MSS | ||
| from mss.base import MSSImplementation | ||
| from mss.exception import ScreenShotError | ||
|
|
||
|
|
@@ -40,25 +38,7 @@ | |
|
|
||
| from mss.models import CFunctionsErrChecked, Monitor, Monitors | ||
|
|
||
| __all__ = ("MSS",) | ||
|
|
||
| BACKENDS = ["default"] | ||
|
|
||
|
|
||
| class MSS(_MSS): | ||
| """Deprecated Windows compatibility constructor. | ||
|
|
||
| Use :class:`mss.MSS` instead. | ||
| """ | ||
|
|
||
| def __init__(self, /, **kwargs: Any) -> None: | ||
| # TODO(jholveck): #493 Remove compatibility constructor after 10.x transition period. | ||
| warnings.warn( | ||
| "mss.windows.MSS is deprecated and will be removed in 11.0; use mss.MSS instead", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
| super().__init__(**kwargs) | ||
| __all__ = ("MSSImplWindows",) | ||
|
|
||
|
|
||
| LPCRECT = POINTER(RECT) # Actually a const pointer, but ctypes has no const. | ||
|
|
@@ -128,7 +108,7 @@ class DISPLAY_DEVICEW(Structure): # noqa: N801 | |
| MONITORNUMPROC = WINFUNCTYPE(BOOL, HMONITOR, HDC, POINTER(RECT), LPARAM) | ||
|
|
||
|
|
||
| def _errcheck(result: BOOL | _Pointer, func: Callable, arguments: tuple) -> tuple: | ||
| def _errcheck(result: int | _Pointer, func: Callable, arguments: tuple) -> tuple: | ||
| """If the result is zero, raise an exception.""" | ||
| if not result: | ||
| # Notably, the errno that is in winerror may not be relevant. Use the winerror and strerror attributes | ||
|
|
@@ -183,13 +163,14 @@ def _errcheck(result: BOOL | _Pointer, func: Callable, arguments: tuple) -> tupl | |
|
|
||
|
|
||
| class MSSImplWindows(MSSImplementation): | ||
| """Multiple ScreenShots implementation for Microsoft Windows. | ||
| """Multiple ScreenShots implementation for Microsoft Windows (GDI backend). | ||
|
|
||
| This implementation uses CreateDIBSection for direct memory access to pixel data, | ||
| which eliminates the need for GetDIBits. The DIB pixel data is written directly | ||
| to system-managed memory that we can read from. | ||
|
|
||
| This has no Windows-specific constructor parameters. | ||
| This backend is selected by ``backend="default"`` and has no Windows-specific | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, I suggest making "gdi" the name for this backend, and "default" essentially an alias. |
||
| constructor parameters. | ||
|
|
||
| .. seealso:: | ||
|
|
||
|
|
@@ -209,13 +190,9 @@ class MSSImplWindows(MSSImplementation): | |
| "user32", | ||
| } | ||
|
|
||
| def __init__(self, *, backend: str = "default") -> None: | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
| if backend != "default": | ||
| msg = 'The only valid backend on this platform is "default".' | ||
| raise ScreenShotError(msg) | ||
|
|
||
| # user32 and gdi32 should not be changed after initialization. | ||
| self.user32 = ctypes.WinDLL("user32", use_last_error=True) | ||
| self.gdi32 = ctypes.WinDLL("gdi32", use_last_error=True) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to mention the internal aspects: choose_impl, MSSImplWindows, BACKENDS, or mss.windows.gdi. Discussing internal implementation details in user-facing documentation risks confusion about what users are supposed to access.
It should be sufficient to say that the Windows stuff was restructured, the existing GDI backend is still the default - and currently only - implementation, and that this was done to prepare for a DXGI implementation.