Skip to content

Commit

Permalink
updated screenshot permission check to handle grim
Browse files Browse the repository at this point in the history
  • Loading branch information
manangulati9 committed Oct 11, 2024
1 parent bd6b60f commit 5561426
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions normcap/screengrab/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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


Expand All @@ -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.")
Expand Down

0 comments on commit 5561426

Please sign in to comment.