From 5561426445903f33583fe6bfada8dc9067e55015 Mon Sep 17 00:00:00 2001 From: Manan Gulati Date: Fri, 11 Oct 2024 21:51:26 +0530 Subject: [PATCH] updated screenshot permission check to handle grim --- normcap/screengrab/permissions.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/normcap/screengrab/permissions.py b/normcap/screengrab/permissions.py index 1ce326e2b..befbaac5d 100644 --- a/normcap/screengrab/permissions.py +++ b/normcap/screengrab/permissions.py @@ -10,10 +10,11 @@ from normcap.screengrab import system_info try: - from normcap.screengrab.handlers import dbus_portal + from normcap.screengrab.handlers import dbus_portal, grim except ImportError: dbus_portal = cast(Any, None) + grim = cast(Any, None) logger = logging.getLogger(__name__) @@ -190,16 +191,19 @@ def reject_button_pressed(self) -> None: self.hide() -def _dbus_portal_has_screenshot_permission() -> bool: - if not dbus_portal: - raise ModuleNotFoundError( - "Portal permission requested, but dbus_portal could not been imported!" - ) +def _check_screenshot_permission() -> bool: result = [] - try: - result = dbus_portal.capture() - except (PermissionError, TimeoutError) as exc: - logger.warning("Screenshot permissions on Wayland seem missing.", exc_info=exc) + + if grim: + result = grim.capture() + elif dbus_portal: + try: + result = dbus_portal.capture() + except (PermissionError, TimeoutError) as exc: + logger.warning( + "Screenshot permissions on Wayland seem missing.", exc_info=exc + ) + return len(result) > 0 @@ -225,7 +229,7 @@ def has_screenshot_permission() -> bool: if sys.platform == "linux" and not system_info.has_wayland_display_manager(): return True if sys.platform == "linux" and system_info.has_wayland_display_manager(): - return _dbus_portal_has_screenshot_permission() + return _check_screenshot_permission() if sys.platform == "win32": return True raise NotImplementedError("Missing permission check for this platform.")