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