redragon-lights.py: Distinguish between screen on and off a little more clearly

This commit is contained in:
Timothy Allen 2024-03-24 03:17:30 +02:00
parent f88957ea0d
commit 2ea1d143d0

View File

@ -7,6 +7,7 @@ import dbus.mainloop.glib
import gi.repository.GLib import gi.repository.GLib
import pprint import pprint
import sys import sys
import time
import usb.core import usb.core
#import traceback #import traceback
@ -59,22 +60,42 @@ def message_callback(session, message):
pprint.pprint(message) pprint.pprint(message)
# not supported in org.freedesktop.ScreenSaver: # 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/ # 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": if message.get_member() == "ActiveChanged" or message.get_member() == "WakeUpScreen":
try:
screensaver_changed = bool(message.get_args_list()[0]) screensaver_changed = bool(message.get_args_list()[0])
#print("Screen saver {} changed. ActiveChanged: {}".format(screensaver, screensaver_changed)) 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 # Capture the GetActive method call in a try/except, or the screensavers that
# don't support GetActive will block those that do # don't support GetActive will block those that do
try: try:
screensaver_path = '/{0}'.format(screensaver.replace('.', '/')) screensaver_path = '/{0}'.format(screensaver.replace('.', '/'))
screensaver_obj = session.get_object(screensaver, screensaver_path) screensaver_obj = session.get_object(screensaver, screensaver_path)
screensaver_iface = dbus.Interface(screensaver_obj, screensaver) 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()) status = bool(screensaver_iface.GetActive())
#print("Screen saver {} changed. GetActive: {}".format(screensaver, 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() kbd = setup_usb()
send_packets(kbd, not status) send_packets(kbd, not status)
except: except:
continue 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 # Set up USB
@ -114,7 +135,7 @@ def send_packets(kbd, switch):
else: else:
pattern = 0x00 # off 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) # 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) # with brightness 1/4 and speed 3/4 (I think)