Last active
October 20, 2019 18:47
-
-
Save averne/12e50448aba5852ac996911a4a7e63b8 to your computer and use it in GitHub Desktop.
Keyboard backlight timeout script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Needs packages python-dbus & python-gobject | |
# ~/scripts/kbd_idle.sh: | |
# !/bin/sh | |
# cd ~/scripts | |
# exec python3 kbd_idle.py & | |
# Add to kde autostart | |
import sys | |
from time import sleep | |
import threading | |
import dbus | |
from dbus.mainloop.glib import DBusGMainLoop | |
from gi.repository import GLib | |
IDLE_TIMEOUT = 10 # Seconds | |
brightness = 0 | |
def kbd_brightness_change_handler(x): | |
global brightness | |
brightness = x | |
def signal_listener_thread(loop): | |
loop.run() | |
def main(argc, argv): | |
DBusGMainLoop(set_as_default=True) | |
loop = GLib.MainLoop() | |
sesbus = dbus.SessionBus() | |
sysbus = dbus.SystemBus() | |
ss_proxy = sesbus.get_object("org.freedesktop.ScreenSaver", "/ScreenSaver") | |
ss_iface = dbus.Interface(ss_proxy, "org.freedesktop.ScreenSaver") | |
ksm_proxy = sesbus.get_object("org.kde.ksmserver", "/KSMServer") | |
ksm_iface = dbus.Interface(ksm_proxy, "org.kde.KSMServerInterface") | |
bl_proxy = sysbus.get_object("org.freedesktop.UPower", "/org/freedesktop/UPower/KbdBacklight") | |
bl_iface = dbus.Interface(bl_proxy, "org.freedesktop.UPower.KbdBacklight") | |
bl_iface.connect_to_signal("BrightnessChanged", kbd_brightness_change_handler) | |
receiver = threading.Thread(target=signal_listener_thread, args=[loop]) | |
receiver.start() | |
global brightness | |
brightness = bl_iface.GetBrightness() | |
prev_brightness = 0 | |
shutdown = False | |
try: | |
while not ksm_iface.isShuttingDown(): | |
sleep(0.5) | |
idle_time = ss_iface.GetSessionIdleTime() / 1000 # ms | |
if not shutdown and idle_time > IDLE_TIMEOUT: | |
prev_brightness = bl_iface.GetBrightness() | |
bl_iface.SetBrightness(0) | |
shutdown = True | |
elif shutdown and idle_time < IDLE_TIMEOUT: | |
if not brightness: | |
bl_iface.SetBrightness(prev_brightness) | |
shutdown = False | |
except (SystemExit, KeyboardInterrupt): | |
pass | |
loop.quit() | |
receiver.join() | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main(len(sys.argv), sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment