From adc87c09087b9b6a923b1c2c15d7dd81d0bdbfcc Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 24 Mar 2024 10:45:06 +0200 Subject: [PATCH] redragon-lights.py: Add debugging --- redragon-lights.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/redragon-lights.py b/redragon-lights.py index 58c411a..0b42277 100755 --- a/redragon-lights.py +++ b/redragon-lights.py @@ -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,30 +44,36 @@ 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: - pprint.pprint(message) + 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]) - print("Screen saver {} changed: ActiveChanged is {}".format(screensaver, screensaver_changed)) + if debug > 1: + 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 @@ -77,7 +87,8 @@ def message_callback(session, message): time.sleep(0.25) status = bool(screensaver_iface.GetActive()) - print("Screen saver {} changed. GetActive: {}".format(screensaver, status)) + 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 if status: @@ -91,7 +102,8 @@ def message_callback(session, message): try: # Delay slightly to avoid conflicting with ActiveChanged's GetActive() above time.sleep(0.25) - print("Screen saver {} changed: WakeUpScreen".format(screensaver)) + if debug > 1: + print("Screen saver {} changed: WakeUpScreen".format(screensaver)) kbd = setup_usb() send_packets(kbd, True) except: @@ -100,19 +112,22 @@ 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?') - print("Product: ", kbd.product) - print("Manufacturer: ", kbd.manufacturer) + if debug > 0: + print("Product: ", kbd.product) + print("Manufacturer: ", kbd.manufacturer) return kbd # Send packet/s def send_packets(kbd, switch): + global debug if kbd is None: setup_usb() @@ -135,7 +150,8 @@ def send_packets(kbd, switch): else: pattern = 0x00 # off - print("Sending pattern {} command".format(pattern)) + if debug > 1: + 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) @@ -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()