summaryrefslogtreecommitdiffstats
path: root/examples/stored-settings
blob: 95180b6bd65729412506f0513446debba7da1227 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- python -*-

# This example demonstrates how settings can be stored
# and be transition from later.


# This file is dual-licensed under GNU General Public License
# version 3 and GNU Free Documentation License version 1.3.


# 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 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
# 
# Permission is granted to copy, distribute and/or modify this document
# under the terms of the GNU Free Documentation License, Version 1.3
# or any later version published by the Free Software Foundation;
# with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
# You should have received a copy of the GNU General Public License
# along with this software package.  If not, see <http://www.gnu.org/licenses/>.

import os
import time


uses_adhoc_opts = True
'''
:bool  `True` if the configuration script parses the ad-hoc settings
'''


# Get --temperature from Blueshift ad-hoc settigns
temperature_to = int(parser.opts['--temperature'][0])

# Get old temperature setting
temperature_from = 6500
def env_(var, default):
    if var not in os.environ:
        return default
    rc = os.environ[var]
    if rc == '':
        rc = default
    return rc
storage_file = '/dev/shm/.blueshift-conf-%s-%s' % (env_('DISPLAY', 'tty'), os.environ['USER'])
if os.path.exists(storage_file):
    with open(storage_file, 'rb') as file:
        temperature_from = int(file.read().decode('utf-8', 'replace').split('\n')[0])

# Store new temperature
with open(storage_file, 'wb') as file:
    file.write(str(temperature_to).encode('utf-8'))
    file.flush()

# Calculate transition time
fadein_time = abs(temperature_to - temperature_from) * 5 / (6500 - 3700)
fadein_steps = fadein_time * 10

# Function for setting the colour temperature
def adjust(alpha):
    temp = temperature_to * alpha + temperature_from * (1 - alpha)
    start_over()
    temperature(temp, lambda t : divide_by_maximum(cmf_10deg(t)))
    (drm if ttymode else randr)(0)

# Perform transition
if not (panicgate or temperature_to == temperature_from):
    signal.signal(signal.SIGTERM, signal_SIGTERM)
    dtime = fadein_time / fadein_steps
    df = 1 / fadein_steps
    trans = 0
    while running:
        try:
            trans += df
            if trans > 1:
                break
            adjust(trans)
            time.sleep(dtime)
        except KeyboardInterrupt:
            running = False
adjust(1)