Skip to content

Commit

Permalink
Add screensaver support for Linux and refactor screensaver functions
Browse files Browse the repository at this point in the history
- Added screensaver support for Linux, including X11 and Wayland
- Moved screensaver functions to a dedicated screensaver.py file
- Updated commands.py to use the new screensaver functions
  • Loading branch information
Lenochxd committed Dec 16, 2024
1 parent 77d0d68 commit 08109ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
11 changes: 1 addition & 10 deletions app/buttons/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from app.utils.logger import log
from app.utils.global_variables import get_global_variable
from app.utils.kill_nircmd import kill_nircmd

from app.utils.firewall import fix_firewall_permission
from .usage import extract_asked_device, get_usage
Expand Down Expand Up @@ -81,15 +80,7 @@ def handle_command(message=None):
)

elif message.startswith("/screensaver") and not message.startswith("/screensaversettings"):
if message.endswith(("on", "/screensaver", "start")):
subprocess.Popen("%windir%/system32/scrnsave.scr /s", shell=True)

elif message.endswith(("hard", "full", "black")):
subprocess.Popen('"lib/nircmd.exe" monitor off', shell=True)
kill_nircmd()

elif message.endswith(("off", "false")):
pyautogui.press("CTRL")
system.screensaver(message)

elif message.startswith("/key"):
key = message.replace("/key", "", 1).strip()
Expand Down
3 changes: 2 additions & 1 deletion app/buttons/system/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .command_handler import handle_command
from .command_handler import handle_command
from .screensaver import handle_command as screensaver
53 changes: 53 additions & 0 deletions app/buttons/system/screensaver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from app.utils.platform import is_windows, is_linux

import os
import subprocess
import pyautogui
from app.utils.kill_nircmd import kill_nircmd
from app.utils.logger import log


def handle_command(message):
if message.endswith(("on", "/screensaver", "start")):
screensaver()

elif message.endswith(("hard", "full", "black")):
screensaver(hard=True)

elif message.endswith(("off", "false")):
screensaver_off()

else:
return False
return True


def screensaver(hard=False):
if is_windows:
if hard:
subprocess.Popen('"lib/nircmd.exe" monitor off', shell=True)
kill_nircmd()
else:
subprocess.Popen("%windir%/system32/scrnsave.scr /s", shell=True)

elif is_linux:
# Try X11 (xset), then Wayland (gnome-screensaver)
if subprocess.run("xset dpms force off", shell=True).returncode == 0:
pass
elif subprocess.run("gnome-screensaver-command -a", shell=True).returncode == 0:
pass
elif subprocess.run("swaymsg 'output * dpms off'", shell=True).returncode == 0:
pass
else:
log.error("Unable to enable screensaver.")
raise RuntimeError("Unable to enable screensaver.")

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


def screensaver_off():
pyautogui.press("CTRL")
if is_linux:
os.system("gnome-screensaver-command -d")

0 comments on commit 08109ec

Please sign in to comment.