From 2ea1d143d049c7dea455c8cfd758f480711fd15f Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 24 Mar 2024 03:17:30 +0200 Subject: [PATCH] redragon-lights.py: Distinguish between screen on and off a little more clearly --- redragon-lights.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/redragon-lights.py b/redragon-lights.py index 98586ac..58c411a 100755 --- a/redragon-lights.py +++ b/redragon-lights.py @@ -7,6 +7,7 @@ import dbus.mainloop.glib import gi.repository.GLib import pprint import sys +import time import usb.core #import traceback @@ -59,22 +60,42 @@ def message_callback(session, message): pprint.pprint(message) # not supported in org.freedesktop.ScreenSaver: # Error org.freedesktop.DBus.Error.NotSupported: This method is not part of the idle inhibition specification: https://specifications.freedesktop.org/idle-inhibit-spec/latest/ - if message.get_member() == "ActiveChanged": - screensaver_changed = bool(message.get_args_list()[0]) - #print("Screen saver {} changed. ActiveChanged: {}".format(screensaver, screensaver_changed)) + if message.get_member() == "ActiveChanged" or message.get_member() == "WakeUpScreen": + try: + screensaver_changed = bool(message.get_args_list()[0]) + print("Screen saver {} changed: ActiveChanged is {}".format(screensaver, screensaver_changed)) + except: + pass # Capture the GetActive method call in a try/except, or the screensavers that # don't support GetActive will block those that do try: screensaver_path = '/{0}'.format(screensaver.replace('.', '/')) screensaver_obj = session.get_object(screensaver, screensaver_path) screensaver_iface = dbus.Interface(screensaver_obj, screensaver) + + # Delay to allow for a more accurate reading of GetActive() + time.sleep(0.25) + status = bool(screensaver_iface.GetActive()) - #print("Screen saver {} changed. GetActive: {}".format(screensaver, status)) - kbd = setup_usb() - send_packets(kbd, not status) + print("Screen saver {} changed. GetActive: {}".format(screensaver, status)) + + # Screensaver is now active; turning the screen back on is handled here with WakeUpScreen + if status: + kbd = setup_usb() + send_packets(kbd, not status) except: continue + # Always wake keyboard when screen is woken + if message.get_member() == "WakeUpScreen": + try: + # Delay slightly to avoid conflicting with ActiveChanged's GetActive() above + time.sleep(0.25) + print("Screen saver {} changed: WakeUpScreen".format(screensaver)) + kbd = setup_usb() + send_packets(kbd, True) + except: + continue # Set up USB @@ -114,7 +135,7 @@ def send_packets(kbd, switch): else: pattern = 0x00 # off - print("Sending pattern {} command".format(switch)) + print("Sending pattern {} command".format(pattern)) # This message tweaks the pattern between OFF (0x0) and the white shadow_disappear pattern (0x9) # with brightness 1/4 and speed 3/4 (I think)