redragon-lights.py: Add debugging

This commit is contained in:
Timothy Allen 2024-03-24 10:45:06 +02:00
parent 2ea1d143d0
commit adc87c0908

View File

@ -15,6 +15,7 @@ import usb.core
vendor=0x258a
product=0x0049
debug = 0
screensaver_list = [
'org.gnome.ScreenSaver',
'org.cinnamon.ScreenSaver',
@ -23,6 +24,7 @@ screensaver_list = [
]
def main():
global debug
parser = argparse.ArgumentParser(
description='Adjust backlight for Redgradon Horus K621',
add_help=True,
@ -33,6 +35,8 @@ def main():
default=None, help='Turn keyboard backlight off')
parser.add_argument('--screensaver', '-s', dest='screensaver', action='store_true',
default=None, help='Turn keyboard backlight off')
parser.add_argument('--verbose', '-v', action='count',
default=0, help='Print more verbose information (can be specified multiple times)')
args = parser.parse_args()
@ -40,29 +44,35 @@ def main():
parser.print_help()
sys.exit(1)
debug = args.verbose
if args.screensaver:
dbus_loop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session = dbus.SessionBus(mainloop=dbus_loop)
for screensaver in screensaver_list:
session.add_match_string_non_blocking("interface='{}'".format(screensaver))
#session.add_match_string_non_blocking("interface='org.gnome.Mutter.IdleMonitor'")
session.add_message_filter(message_callback)
session.add_message_filter(screensaver_message_callback)
loop = gi.repository.GLib.MainLoop()
loop.run()
else:
kbd = setup_usb()
send_packets(kbd, args.switch)
def message_callback(session, message):
def screensaver_message_callback(session, message):
global debug
for screensaver in screensaver_list:
if message.get_interface() == screensaver:
if debug > 2:
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" or message.get_member() == "WakeUpScreen":
try:
screensaver_changed = bool(message.get_args_list()[0])
if debug > 1:
print("Screen saver {} changed: ActiveChanged is {}".format(screensaver, screensaver_changed))
except:
pass
@ -77,6 +87,7 @@ def message_callback(session, message):
time.sleep(0.25)
status = bool(screensaver_iface.GetActive())
if debug > 1:
print("Screen saver {} changed. GetActive: {}".format(screensaver, status))
# Screensaver is now active; turning the screen back on is handled here with WakeUpScreen
@ -91,6 +102,7 @@ def message_callback(session, message):
try:
# Delay slightly to avoid conflicting with ActiveChanged's GetActive() above
time.sleep(0.25)
if debug > 1:
print("Screen saver {} changed: WakeUpScreen".format(screensaver))
kbd = setup_usb()
send_packets(kbd, True)
@ -100,11 +112,13 @@ def message_callback(session, message):
# Set up USB
def setup_usb():
global debug
kbd = usb.core.find(idVendor=vendor, idProduct=product)
if kbd is None:
raise ValueError('Keyboard not found; perhaps switch to USB mode?')
if debug > 0:
print("Product: ", kbd.product)
print("Manufacturer: ", kbd.manufacturer)
@ -113,6 +127,7 @@ def setup_usb():
# Send packet/s
def send_packets(kbd, switch):
global debug
if kbd is None:
setup_usb()
@ -135,6 +150,7 @@ def send_packets(kbd, switch):
else:
pattern = 0x00 # off
if debug > 1:
print("Sending pattern {} command".format(pattern))
# This message tweaks the pattern between OFF (0x0) and the white shadow_disappear pattern (0x9)
@ -159,11 +175,11 @@ def send_packets(kbd, switch):
if e.errno == 16:
print("This issue needs more investigation... it may simply be previous invalid commands")
# May need to reconfigure the device, or detach/reattach kernel driver, either for interface 0 or 0 _and_ 1
# if detaching/reattaching, swallow errors with try/except
elif e.errno == 2:
print("Try unplugging and replugging the keyboard...")
sys.exit()
if __name__ == '__main__':
#atexit.register(cleanup)
main()