Skip to content

Commit

Permalink
Add support for locking session on Linux and refactor functions
Browse files Browse the repository at this point in the history
- Added support for locking session on Linux with various methods
- Moved lock session function to a dedicated lock_session.py file
- Updated commands.py to use the new lock_session function
  • Loading branch information
Lenochxd committed Dec 16, 2024
1 parent 08109ec commit 20acd87
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/buttons/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def handle_command(message=None):
return soundboard.playsound(*soundboard.get_params(message))

elif message.startswith("/locksession"):
subprocess.Popen("Rundll32.exe user32.dll,LockWorkStation", shell=True)
system.lock_session()

elif message.startswith("/screensaversettings"):
subprocess.Popen(
Expand Down
1 change: 1 addition & 0 deletions app/buttons/system/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .command_handler import handle_command
from .screensaver import handle_command as screensaver
from .lock_session import lock_session
30 changes: 30 additions & 0 deletions app/buttons/system/lock_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from app.utils.platform import is_windows, is_linux, is_mac

import subprocess
from app.utils.logger import log


def lock_session():
"""Lock the user's session."""
if is_windows:
# Lock workstation on Windows
subprocess.Popen("Rundll32.exe user32.dll,LockWorkStation", shell=True)

elif is_linux:
# Try various Linux locking methods
if subprocess.run("dm-tool lock", shell=True).returncode == 0:
return
elif subprocess.run("xdg-screensaver lock", shell=True).returncode == 0:
return
elif subprocess.run("gnome-screensaver-command -l", shell=True).returncode == 0:
return
elif subprocess.run("loginctl lock-session", shell=True).returncode == 0:
return
elif subprocess.run("swaylock", shell=True).returncode == 0:
return
else:
log.error("Unable to lock the session.")
raise RuntimeError("Unable to lock the session.")

else:
raise NotImplementedError("Screensaver is not implemented for this platform.")

0 comments on commit 20acd87

Please sign in to comment.