diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-02-17 01:20:03 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-02-17 01:20:03 +0100 |
commit | 73df5cdda541938e1891b650b3adfc1c317682fb (patch) | |
tree | b5f1f7c32a943cdf6fb103d3f6af7520f3abdf65 | |
parent | add tempurature mappings from redshift (diff) | |
download | blueshift-73df5cdda541938e1891b650b3adfc1c317682fb.tar.gz blueshift-73df5cdda541938e1891b650b3adfc1c317682fb.tar.bz2 blueshift-73df5cdda541938e1891b650b3adfc1c317682fb.tar.xz |
fix some minor errors
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rwxr-xr-x | src/__main__.py | 122 | ||||
-rw-r--r-- | src/blueshift_randr.pyx | 6 | ||||
-rw-r--r-- | src/curve.py | 19 | ||||
-rw-r--r-- | src/monitor.py | 7 |
4 files changed, 90 insertions, 64 deletions
diff --git a/src/__main__.py b/src/__main__.py index 083d0b0..45ba7db 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -23,7 +23,9 @@ import datetime ## Set global variables global DATADIR, i_size, o_size, r_curve, g_curve, b_curve, clip_result -global periodically, wait_period, monitor_controller, running +global periodically, wait_period, fadein_time, fadeout_time, fadein_steps, fadeout_steps +global monitor_controller, running, continuous_run +global signal_SIGTERM from solar import * @@ -40,6 +42,7 @@ def periodically(year, month, day, hour, minute, second, weekday, fade): fadeout_time = None fadein_steps = 100 fadeout_steps = 100 + start_over() if fade is None: negative(False, False, False) temperature(6500, lambda T : divide_by_maximum(series_d(T))) @@ -103,22 +106,22 @@ monitor_controller = lambda : randr() :()→void Function used by Blueshift on exit to apply reset colour curves ''' -fadein_time = 10 +fadein_time = 2 ''' :float? The number of seconds used to fade in on start, `None` for no fading ''' -fadeout_time = 10 +fadeout_time = 2 ''' :float? The number of seconds used to fade out on exit, `None` for no fading ''' -fadein_steps = 100 +fadein_steps = 10 ''' :int The number of steps in the fade in phase, if any ''' -fadeout_steps = 100 +fadeout_steps = 10 ''' :int The number of steps in the fade out phase, if any ''' @@ -129,52 +132,33 @@ running = True ''' -## Load extension and configurations via blueshiftrc -if config_file is None: - for file in ('$XDG_CONFIG_HOME/%/%rc', '$HOME/.config/%/%rc', '$HOME/.%rc', '/etc/%rc'): - file = file.replace('%', 'blueshift') - for arg in ('XDG_CONFIG_HOME', 'HOME'): - if '$' + arg in file: - if arg in os.environ: - file = file.replace('$' + arg, os.environ[arg].replace('$', '\0')) - else: - file = None - break - if file is not None: - file = file.replace('\0', '$') - if os.path.exists(file): - config_file = file - break -if config_file is not None: - code = None - with open(file, 'rb') as script: - code = script.read() - code = code.decode('utf8', 'error') + '\n' - code = compile(code, file, 'exec') - g, l = globals(), dict(locals()) - for key in l: - g[key] = l[key] - exec(code, g) -else: - print('No configuration file found') - sys.exit(1) - -## Run periodically if configured to -if periodically is not None: +def signal_SIGTERM(signum, frame): + global running + if not running: + running = False + start_over() + monitor_controller() + close_c_bindings() + sys.exit(0) + running = False + + +def continuous_run(): + ''' + Invoked to run continuously if `periodically` is not `None` + ''' + global running, wait_period, fadein_time, fadeout_time, fadein_steps, fadeout_steps def p(t, fade = None): wd = t.isocalendar()[2] periodically(t.year, t.month, t.day, t.hour, t.minute, t.second, wd, fade) + def sleep(seconds): + try: + time.sleep(seconds) + except KeyboardInterrupt: + signal_SIGTERM(0, None) - ## Catch TERM signal - def signal_SIGTERM(signum, frame): - if not running: - running = False - start_over() - monitor_controller() - close_c_bindings() - sys.exit(0) - running = False + ## Catch signals signal.signal(signal.SIGTERM, signal_SIGTERM) ## Fade in @@ -182,33 +166,73 @@ if periodically is not None: ftime = 0 if fadein_time is not None: dtime = fadein_time / fadein_steps + df = 1 / fadein_steps while running: - ftime += dtime + ftime += df if ftime > 1: break p(datetime.datetime.now(), ftime) + sleep(dtime) ## Run periodically if not early_exit: while running: p(datetime.datetime.now(), None) if running: - time.sleep(wait_period) + sleep(wait_period) ## Fade out if fadeout_time is not None: dtime = fadeout_time / fadeout_steps + df = 1 / fadeout_steps if early_exit: ftime = 1 while True: - ftime -= dtime + ftime -= df if ftime <= 0: break p(datetime.datetime.now(), -ftime) + sleep(dtime) ## Reset start_over() monitor_controller() + +## Load extension and configurations via blueshiftrc +if config_file is None: + for file in ('$XDG_CONFIG_HOME/%/%rc', '$HOME/.config/%/%rc', '$HOME/.%rc', '/etc/%rc'): + file = file.replace('%', 'blueshift') + for arg in ('XDG_CONFIG_HOME', 'HOME'): + if '$' + arg in file: + if arg in os.environ: + file = file.replace('$' + arg, os.environ[arg].replace('$', '\0')) + else: + file = None + break + if file is not None: + file = file.replace('\0', '$') + if os.path.exists(file): + config_file = file + break +if config_file is not None: + code = None + with open(file, 'rb') as script: + code = script.read() + code = code.decode('utf8', 'error') + '\n' + code = compile(code, file, 'exec') + g, l = globals(), dict(locals()) + for key in l: + g[key] = l[key] + exec(code, g) +else: + print('No configuration file found') + sys.exit(1) + + +## Run periodically if configured to +if periodically is not None: + continuous_run() + close_c_bindings() diff --git a/src/blueshift_randr.pyx b/src/blueshift_randr.pyx index e022714..d5e13b6 100644 --- a/src/blueshift_randr.pyx +++ b/src/blueshift_randr.pyx @@ -25,9 +25,9 @@ def randr_apply(unsigned long long use_crtcs, r_curve, g_curve, b_curve): if (r is NULL) or (g is NULL) or (b is NULL): raise MemoryError() for i in range(256): - r[i] = r_curve[i] - g[i] = g_curve[i] - b[i] = b_curve[i] + r[i] = r_curve[i] & 0xFFFF + g[i] = g_curve[i] & 0xFFFF + b[i] = b_curve[i] & 0xFFFF rc = blueshift_randr_apply(use_crtcs, r, g, b) free(r) free(g) diff --git a/src/curve.py b/src/curve.py index 4c05822..ece5885 100644 --- a/src/curve.py +++ b/src/curve.py @@ -115,11 +115,11 @@ def cmf_2deg(temperature): temp = min(max(1000, temperature), 40000) x, y = 0, 0 if (temp % 100) == 0: - (x, y) = cmf_2deg_cache[(temp - 1000) // 100] + (x, y) = cmf_2deg_cache[int((temp - 1000) // 100)] else: temp -= 1000 - (x1, y1) = cmf_2deg_cache[temp // 100] - (x2, y2) = cmf_2deg_cache[temp // 100 + 1] + (x1, y1) = cmf_2deg_cache[int(temp // 100)] + (x2, y2) = cmf_2deg_cache[int(temp // 100 + 1)] temp = (temp % 100) / 100 x = x1 * temp + x2 * (1 - temp) y = y1 * temp + y2 * (1 - temp) @@ -146,11 +146,11 @@ def cmf_10deg(temperature): temp = min(max(1000, temperature), 40000) x, y = 0, 0 if (temp % 100) == 0: - (x, y) = cmf_10deg_cache[(temp - 1000) // 100] + (x, y) = cmf_10deg_cache[int((temp - 1000) // 100)] else: temp -= 1000 - (x1, y1) = cmf_10deg_cache[temp // 100] - (x2, y2) = cmf_10deg_cache[temp // 100 + 1] + (x1, y1) = cmf_10deg_cache[int(temp // 100)] + (x2, y2) = cmf_10deg_cache[int(temp // 100 + 1)] temp = (temp % 100) / 100 x = x1 * temp + x2 * (1 - temp) y = y1 * temp + y2 * (1 - temp) @@ -189,11 +189,11 @@ def redshift(temperature, old_version = False, linear_interpolation = False): temp = min(max(1000, temperature), 10000 if old_version else 25100) r, g, b = 1, 1, 1 if (temp % 100) == 0: - (r, g, b) = cache[(temp - 1000) // 100] + (r, g, b) = cache[int((temp - 1000) // 100)] else: temp -= 1000 - (r1, g1, b1) = cache[temp // 100] - (r2, g2, b2) = cache[temp // 100 + 1] + (r1, g1, b1) = cache[int(temp // 100)] + (r2, g2, b2) = cache[int(temp // 100 + 1)] temp = (temp % 100) / 100 if linear_interpolation: (r, g, b) = standard_to_linear(r, g, b) @@ -216,7 +216,6 @@ def temperature(temperature, algorithm): if temperature == 6500: return (r, g, b) = algorithm(temperature) - print(r, g, b) rgb_brightness(r, g, b) diff --git a/src/monitor.py b/src/monitor.py index b2bfa84..52bc220 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -66,8 +66,11 @@ def randr(*crtcs): randr_opened = True else: sys.exit(1) - if not randr_apply(crtcs, R_curve, G_curve, B_curve) == 0: - sys.exit(1) + try: + if not randr_apply(crtcs, R_curve, G_curve, B_curve) == 0: + sys.exit(1) + except OverflowError: + pass # Happens on exit by TERM signal def print_curves(*crtcs): |