|
| 1 | +"""Platform-specific implementations for GUI event capture. |
| 2 | +
|
| 3 | +This module provides platform-specific implementations for: |
| 4 | +- Screen capture |
| 5 | +- Input event capture |
| 6 | +- Display information (resolution, DPI, pixel ratio) |
| 7 | +
|
| 8 | +The module automatically selects the appropriate implementation based on |
| 9 | +the current platform (darwin, win32, linux). |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import sys |
| 15 | +from typing import TYPE_CHECKING |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from typing import Protocol |
| 19 | + |
| 20 | + class PlatformProvider(Protocol): |
| 21 | + """Protocol for platform-specific providers.""" |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def get_screen_dimensions() -> tuple[int, int]: |
| 25 | + """Get screen dimensions in physical pixels.""" |
| 26 | + ... |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def get_display_pixel_ratio() -> float: |
| 30 | + """Get display pixel ratio (physical/logical).""" |
| 31 | + ... |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def is_accessibility_enabled() -> bool: |
| 35 | + """Check if accessibility permissions are enabled.""" |
| 36 | + ... |
| 37 | + |
| 38 | + |
| 39 | +def get_platform() -> str: |
| 40 | + """Get the current platform identifier. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + 'darwin' for macOS, 'win32' for Windows, 'linux' for Linux. |
| 44 | + """ |
| 45 | + return sys.platform |
| 46 | + |
| 47 | + |
| 48 | +def get_platform_provider() -> "PlatformProvider": |
| 49 | + """Get the platform-specific provider for the current OS. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Platform provider instance for the current operating system. |
| 53 | +
|
| 54 | + Raises: |
| 55 | + NotImplementedError: If the platform is not supported. |
| 56 | + """ |
| 57 | + platform = get_platform() |
| 58 | + |
| 59 | + if platform == "darwin": |
| 60 | + from openadapt_capture.platform.darwin import DarwinPlatform |
| 61 | + return DarwinPlatform() |
| 62 | + elif platform == "win32": |
| 63 | + from openadapt_capture.platform.windows import WindowsPlatform |
| 64 | + return WindowsPlatform() |
| 65 | + elif platform.startswith("linux"): |
| 66 | + from openadapt_capture.platform.linux import LinuxPlatform |
| 67 | + return LinuxPlatform() |
| 68 | + else: |
| 69 | + raise NotImplementedError(f"Platform not supported: {platform}") |
| 70 | + |
| 71 | + |
| 72 | +def get_screen_dimensions() -> tuple[int, int]: |
| 73 | + """Get screen dimensions in physical pixels. |
| 74 | +
|
| 75 | + This returns the actual screenshot pixel dimensions, which may be |
| 76 | + larger than logical dimensions on HiDPI/Retina displays. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + Tuple of (width, height) in physical pixels. |
| 80 | + """ |
| 81 | + try: |
| 82 | + provider = get_platform_provider() |
| 83 | + return provider.get_screen_dimensions() |
| 84 | + except (NotImplementedError, ImportError): |
| 85 | + # Fallback to generic implementation |
| 86 | + try: |
| 87 | + from PIL import ImageGrab |
| 88 | + screenshot = ImageGrab.grab() |
| 89 | + return screenshot.size |
| 90 | + except Exception: |
| 91 | + return (1920, 1080) # Default fallback |
| 92 | + |
| 93 | + |
| 94 | +def get_display_pixel_ratio() -> float: |
| 95 | + """Get the display pixel ratio (physical/logical). |
| 96 | +
|
| 97 | + This is the ratio of physical pixels to logical pixels. |
| 98 | + For example, 2.0 for Retina displays on macOS. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + Pixel ratio (e.g., 1.0 for standard displays, 2.0 for Retina). |
| 102 | + """ |
| 103 | + try: |
| 104 | + provider = get_platform_provider() |
| 105 | + return provider.get_display_pixel_ratio() |
| 106 | + except (NotImplementedError, ImportError): |
| 107 | + return 1.0 |
| 108 | + |
| 109 | + |
| 110 | +def is_accessibility_enabled() -> bool: |
| 111 | + """Check if accessibility permissions are enabled. |
| 112 | +
|
| 113 | + On macOS, this checks if the application has accessibility permissions |
| 114 | + required for keyboard and mouse event capture. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + True if accessibility is enabled, False otherwise. |
| 118 | + """ |
| 119 | + try: |
| 120 | + provider = get_platform_provider() |
| 121 | + return provider.is_accessibility_enabled() |
| 122 | + except (NotImplementedError, ImportError): |
| 123 | + return True # Assume enabled on unknown platforms |
| 124 | + |
| 125 | + |
| 126 | +__all__ = [ |
| 127 | + "get_platform", |
| 128 | + "get_platform_provider", |
| 129 | + "get_screen_dimensions", |
| 130 | + "get_display_pixel_ratio", |
| 131 | + "is_accessibility_enabled", |
| 132 | +] |
0 commit comments