Last active
July 6, 2020 10:49
-
-
Save finkrer/e8ec4b1eccfec94e4d2fd1a78e0cefcb to your computer and use it in GitHub Desktop.
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/env python | |
import dbus | |
import subprocess | |
from gi.repository import GLib | |
from dbus.mainloop.glib import DBusGMainLoop | |
night_mode = False | |
backlight_threshold = 0.3 | |
backlight_property = 'BlPct' | |
themes = { | |
'light': 'org.kde.breeze.desktop', | |
'dark': 'org.kde.breezedark.desktop' | |
} | |
def main(): | |
DBusGMainLoop(set_as_default=True) | |
bus = dbus.SessionBus() | |
add_receiver(bus, handle_backlight_change) | |
loop = GLib.MainLoop().run() | |
def set_theme(name): | |
subprocess.Popen(['lookandfeeltool', '-a', themes[name]], | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL) | |
def handle_backlight_change(*args): | |
global night_mode | |
event = args[1] | |
if backlight_property not in event: | |
return | |
current_backlight = event[backlight_property] | |
is_night = current_backlight < backlight_threshold | |
if is_night and not night_mode: | |
set_theme('dark') | |
if not is_night and night_mode: | |
set_theme('light') | |
night_mode = is_night | |
def add_receiver(bus, handler): | |
bus.add_signal_receiver( | |
handler, | |
'PropertiesChanged', | |
'org.freedesktop.DBus.Properties', | |
path='/org/clight/clight' | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment