From 34116c60139084a4dba3b8d6e9c9df20efd17f88 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sat, 4 Jul 2015 23:59:45 -0400 Subject: Add a small save/load feature --- xsft.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/xsft.py b/xsft.py index 0f411c9..0912f1b 100755 --- a/xsft.py +++ b/xsft.py @@ -3,6 +3,7 @@ # Licensed under the GNU GPLv3+ import math +import os import subprocess from gi.repository import Gtk @@ -77,23 +78,24 @@ def temperature_to_gamma(kelvin): class MyWindow(Gtk.Window): - def __init__(self): + def __init__(self, conf): Gtk.Window.__init__(self, title="xrandr-slightly-fewer-tears") self.set_default_size(640, -1) + self.conf = conf + self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, border_width=10) self.add(self.box) self.box.add(Gtk.Label('Brightness')) - self.brightness = self.add_hscale(20, 100) - self.brightness.set_value(80) + self.brightness = self.add_hscale(20, 100, conf.brightness) self.box.add(Gtk.Label('Temperature (K)')) - self.temperature = self.add_hscale(1000, 25000) - self.temperature.set_value(5500) + self.temperature = self.add_hscale(1000, 25000, conf.temperature) - def add_hscale(self, min_val, max_val): + def add_hscale(self, min_val, max_val, def_val): scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, digits=0) scale.set_range(min_val, max_val) + scale.set_value(def_val) scale.connect('value-changed', lambda _: self.update()) self.box.add(scale) return scale @@ -103,13 +105,53 @@ class MyWindow(Gtk.Window): if brightness < 0.2: brightness = 0.2 - set_brightness_and_gamma(get_connected_outputs(), - brightness, - temperature_to_gamma(self.temperature.get_value())) + gamma = temperature_to_gamma(self.temperature.get_value()) + set_brightness_and_gamma(get_connected_outputs(), brightness, gamma) + + self.conf.brightness = self.brightness.get_value() + self.conf.temperature = self.temperature.get_value() + self.conf.save() + + +class Config(object): + def __init__(self): + self.pardir = os.path.join(os.getenv('HOME'), + '.config', + 'xrandr-slightly-fewer-tears') + self.path = os.path.join(self.pardir, 'xsft.conf') + + # Defaults + self.brightness = 80 + self.temperature = 5500 + + try: + self.load() + except IOError: + pass + + def load(self): + with open(self.path) as conf_file: + for line in conf_file.readlines(): + parts = line.split('=') + key = parts[0] + val = float(parts[1]) + if key == 'brightness': + self.brightness = val + if key == 'temperature': + self.temperature = val + + def save(self): + if not os.path.isdir(self.pardir): + os.mkdir(self.pardir) + with open(self.path, 'w') as conf_file: + for key in ('brightness', 'temperature'): + val = getattr(self, key) + conf_file.write('{}={}\n'.format(key, val)) def main(): - win = MyWindow() + conf = Config() + win = MyWindow(conf) win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main() -- cgit