summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-02-25 10:25:13 +0100
committerMattias Andrée <maandree@operamail.com>2014-02-25 10:25:13 +0100
commitcf6e22913e6db4b8bf3a5727651888cc21ecc68e (patch)
treefc835db33a8fbdaba301a0c864c6da602ada3609
parentdoc (diff)
downloadblueshift-cf6e22913e6db4b8bf3a5727651888cc21ecc68e.tar.gz
blueshift-cf6e22913e6db4b8bf3a5727651888cc21ecc68e.tar.bz2
blueshift-cf6e22913e6db4b8bf3a5727651888cc21ecc68e.tar.xz
split out adhoc mode to adhoc.py
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--Makefile2
-rwxr-xr-xsrc/__main__.py114
-rw-r--r--src/adhoc.py116
3 files changed, 137 insertions, 95 deletions
diff --git a/Makefile b/Makefile
index 4186df9..32c9763 100644
--- a/Makefile
+++ b/Makefile
@@ -60,7 +60,7 @@ FLAGS = $$($(PKGCONFIG) --cflags $(LIBS)) -std=$(STD) $(WARN) $(OPTIMISE) -fPIC
# Resource files
DATAFILES = 2deg 10deg redshift redshift_old
# Python source files
-PYFILES = __main__.py colour.py curve.py monitor.py solar.py icc.py
+PYFILES = __main__.py colour.py curve.py monitor.py solar.py icc.py adhoc.py
# Library files
CBINDINGS = $(foreach B,$(SERVER_BINDINGS),blueshift_$(B).so)
# Configuration script example files
diff --git a/src/__main__.py b/src/__main__.py
index 86c6866..3d40d6e 100755
--- a/src/__main__.py
+++ b/src/__main__.py
@@ -375,100 +375,29 @@ for opt in ('--gamma', '--brightness', '++brightness', '--temperature'):
print('%s can only be used up to two times' % opt)
sys.exit(1)
+g, l = globals(), dict(locals())
+for key in l:
+ g[key] = l[key]
settings = [gammas, rgb_brightnesses, cie_brightnesses, temperatures]
if (config_file is None) and any([doreset, location] + settings):
## Use one time configurations
- if len(parser.files) > 0:
- print('%s: warning: configuration script arguments are not supported in ad-hoc mode' % sys.argv[0])
-
- ## Determine whether we should run in continuous mode
- continuous = any(map(lambda a : (a is not None) and (len(a) == 2), settings))
- continuous = continuous or (location is not None)
-
- ## Select default settings when not specified
- d = lambda a, default : [default, default] if a is None else (a * 2 if len(a) == 1 else a)
- gammas = d(gammas, "1:1:1")
- rgb_brightnesses = d(rgb_brightnesses, "1")
- cie_brightnesses = d(cie_brightnesses, "1")
- if temperatures is None:
- temperatures = ['3700', '6500']
- elif len(temperatures) == 1:
- temperatures *= 2
-
- ## Parse string arrays into floating point matrices
- settings = [gammas, rgb_brightnesses, cie_brightnesses, temperatures, [location]]
- s = lambda f, v : f(v) if v is not None else None
- settings = [s(lambda c : [s(lambda x : [float(y) for y in x.split(':')], x) for x in c], c) for c in settings]
- [gammas, rgb_brightnesses, cie_brightnesses, temperatures, location] = settings
- location = None if location is None else location[0]
-
- ## Select method for calculating to what degree the adjustments should be applied
- alpha = lambda : 1
- if continuous:
- if location is not None:
- alpha = lambda : sun(*location)
- else:
- def alpha_():
- now = datetime.datetime.now()
- hh, mm = now.hour, now.minute + now.second / 60
- if 12 <= hh <= 22:
- return 1 - (hh - 12) / (22 - 12) - mm / 60
- if hh <= 12:
- hh += 22 - 12
- return (hh - 22) / 14 + m / 60
- alpha = alpha_
-
- ## Set monitor control
- def reduce(f, items):
- '''
- https://en.wikipedia.org/wiki/Fold_(higher-order_function)
- '''
- if len(items) < 2:
- return items
- rc = items[0]
- for i in range(1, len(items)):
- rc = f(rc, items[i])
- return rc
- output = reduce(lambda x, y : x + y, [a.split(',') for a in output])
- monitor_controller = lambda : randr(*output)
-
- def apply(dayness, pureness):
- '''
- Apply adjustments
-
- @param dayness:float The visibility of the sun
- @param pureness:float Transitioning progress, 1 for at clean state, 0 for at adjusted state
- '''
- start_over()
- interpol_ = lambda d, p, a, r : d * r + (p[0] * a + p[1] * (1 - a)) * (1 - r)
- interpol = lambda d, p : [interpol_(d, [p[0][i], p[1][i]], dayness, pureness) for i in range(len(p[0]))]
- temperature(*interpol(6500, temperatures), algorithm = lambda t : divide_by_maximum(cmf_10deg(t)))
- rgb_brightness(*interpol(1, rgb_brightnesses))
- cie_brightness(*interpol(1, cie_brightnesses))
- clip()
- gamma(*interpol(1, gammas))
- clip()
- monitor_controller()
-
- if continuous and not doreset:
- ## Continuous mode
- def periodically(year, month, day, hour, minute, second, weekday, fade):
- apply(alpha(), 0 if fade is None else 1 - abs(fade))
+ code, pathname = None, None
+ for p in sys.path:
+ p += '/adhoc.py'
+ if os.path.exists(p):
+ pathname = p
+ exit
+ if pathname is not None:
+ with open(pathname, 'rb') as script:
+ code = script.read()
else:
- ## One shot mode
- if not panicgate:
- signal.signal(signal.SIGTERM, signal_SIGTERM)
- trans = 0
- while running:
- try:
- apply(alpha(), trans if doreset else 1 - trans)
- trans += 0.05
- time.sleep(0.1)
- except KeyboardInterrupt:
- signal_SIGTERM(0, None)
- if trans >= 1:
- break
- apply(alpha(), 1 if doreset else 0)
+ import zipimport
+ importer = zipimport.zipimporter(sys.argv[0])
+ code = importer.get_data('adhoc.py')
+ pathname = sys.argv[0] + '/adhoc.py'
+ code = code.decode('utf-8', 'error') + '\n'
+ code = compile(code, pathname, 'exec')
+ exec(code, g)
else:
## Load extension and configurations via blueshiftrc
if config_file is None:
@@ -491,11 +420,8 @@ else:
code = None
with open(config_file, 'rb') as script:
code = script.read()
- code = code.decode('utf8', 'error') + '\n'
+ code = code.decode('utf-8', 'error') + '\n'
code = compile(code, config_file, 'exec')
- g, l = globals(), dict(locals())
- for key in l:
- g[key] = l[key]
exec(code, g)
else:
print('No configuration file found')
diff --git a/src/adhoc.py b/src/adhoc.py
new file mode 100644
index 0000000..a4bc7a2
--- /dev/null
+++ b/src/adhoc.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python3
+
+# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import time
+import signal
+import datetime
+
+
+## Warn if we are using configuration script arguments
+if len(parser.files) > 0:
+ print('%s: warning: configuration script arguments are not supported in ad-hoc mode' % sys.argv[0])
+
+## Determine whether we should run in continuous mode
+continuous = any(map(lambda a : (a is not None) and (len(a) == 2), settings))
+continuous = continuous or (location is not None)
+
+## Select default settings when not specified
+d = lambda a, default : [default, default] if a is None else (a * 2 if len(a) == 1 else a)
+gammas = d(gammas, "1:1:1")
+rgb_brightnesses = d(rgb_brightnesses, "1")
+cie_brightnesses = d(cie_brightnesses, "1")
+if temperatures is None:
+ temperatures = ['3700', '6500']
+elif len(temperatures) == 1:
+ temperatures *= 2
+
+## Parse string arrays into floating point matrices
+settings = [gammas, rgb_brightnesses, cie_brightnesses, temperatures, [location]]
+s = lambda f, v : f(v) if v is not None else None
+settings = [s(lambda c : [s(lambda x : [float(y) for y in x.split(':')], x) for x in c], c) for c in settings]
+[gammas, rgb_brightnesses, cie_brightnesses, temperatures, location] = settings
+location = None if location is None else location[0]
+
+## Select method for calculating to what degree the adjustments should be applied
+alpha = lambda : 1
+if continuous:
+ if location is not None:
+ alpha = lambda : sun(*location)
+ else:
+ def alpha_():
+ now = datetime.datetime.now()
+ hh, mm = now.hour, now.minute + now.second / 60
+ if 12 <= hh <= 22:
+ return 1 - (hh - 12) / (22 - 12) - mm / 60
+ if hh <= 12:
+ hh += 22 - 12
+ return (hh - 22) / 14 + m / 60
+ alpha = alpha_
+
+## Set monitor control
+def reduce(f, items):
+ '''
+ https://en.wikipedia.org/wiki/Fold_(higher-order_function)
+ '''
+ if len(items) < 2:
+ return items
+ rc = items[0]
+ for i in range(1, len(items)):
+ rc = f(rc, items[i])
+ return rc
+output = reduce(lambda x, y : x + y, [a.split(',') for a in output])
+monitor_controller = lambda : randr(*output)
+
+def apply(dayness, pureness):
+ '''
+ Apply adjustments
+
+ @param dayness:float The visibility of the sun
+ @param pureness:float Transitioning progress, 1 for at clean state, 0 for at adjusted state
+ '''
+ start_over()
+ interpol_ = lambda d, p, a, r : d * r + (p[0] * a + p[1] * (1 - a)) * (1 - r)
+ interpol = lambda d, p : [interpol_(d, [p[0][i], p[1][i]], dayness, pureness) for i in range(len(p[0]))]
+ temperature(*interpol(6500, temperatures), algorithm = lambda t : divide_by_maximum(cmf_10deg(t)))
+ rgb_brightness(*interpol(1, rgb_brightnesses))
+ cie_brightness(*interpol(1, cie_brightnesses))
+ clip()
+ gamma(*interpol(1, gammas))
+ clip()
+ monitor_controller()
+
+if continuous and not doreset:
+ ## Continuous mode
+ def periodically(year, month, day, hour, minute, second, weekday, fade):
+ apply(alpha(), 0 if fade is None else 1 - abs(fade))
+else:
+ ## One shot mode
+ if not panicgate:
+ signal.signal(signal.SIGTERM, signal_SIGTERM)
+ trans = 0
+ while running:
+ try:
+ apply(alpha(), trans if doreset else 1 - trans)
+ trans += 0.05
+ time.sleep(0.1)
+ except KeyboardInterrupt:
+ signal_SIGTERM(0, None)
+ if trans >= 1:
+ break
+ apply(alpha(), 1 if doreset else 0)
+