redragon-lights.py: Add debugging
This commit is contained in:
parent
2ea1d143d0
commit
adc87c0908
@ -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,29 +44,35 @@ 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:
|
||||||
|
if debug > 2:
|
||||||
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" 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])
|
||||||
|
if debug > 1:
|
||||||
print("Screen saver {} changed: ActiveChanged is {}".format(screensaver, screensaver_changed))
|
print("Screen saver {} changed: ActiveChanged is {}".format(screensaver, screensaver_changed))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
@ -77,6 +87,7 @@ def message_callback(session, message):
|
|||||||
time.sleep(0.25)
|
time.sleep(0.25)
|
||||||
|
|
||||||
status = bool(screensaver_iface.GetActive())
|
status = bool(screensaver_iface.GetActive())
|
||||||
|
if debug > 1:
|
||||||
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
|
# Screensaver is now active; turning the screen back on is handled here with WakeUpScreen
|
||||||
@ -91,6 +102,7 @@ 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)
|
||||||
|
if debug > 1:
|
||||||
print("Screen saver {} changed: WakeUpScreen".format(screensaver))
|
print("Screen saver {} changed: WakeUpScreen".format(screensaver))
|
||||||
kbd = setup_usb()
|
kbd = setup_usb()
|
||||||
send_packets(kbd, True)
|
send_packets(kbd, True)
|
||||||
@ -100,11 +112,13 @@ 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?')
|
||||||
|
|
||||||
|
if debug > 0:
|
||||||
print("Product: ", kbd.product)
|
print("Product: ", kbd.product)
|
||||||
print("Manufacturer: ", kbd.manufacturer)
|
print("Manufacturer: ", kbd.manufacturer)
|
||||||
|
|
||||||
@ -113,6 +127,7 @@ def setup_usb():
|
|||||||
|
|
||||||
# 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,6 +150,7 @@ def send_packets(kbd, switch):
|
|||||||
else:
|
else:
|
||||||
pattern = 0x00 # off
|
pattern = 0x00 # off
|
||||||
|
|
||||||
|
if debug > 1:
|
||||||
print("Sending pattern {} command".format(pattern))
|
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)
|
||||||
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user