-
Notifications
You must be signed in to change notification settings - Fork 0
/
sctd
executable file
·260 lines (231 loc) · 6.87 KB
/
sctd
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/sh
# Copyright (c) 2017 Aaron Bieber <aaron@bolddaemon.com>
# Copryright (c) 2017 Inokentiy Babushkin <twk@twki.de>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#config file
CONFIG="${XDG_CONFIG_HOME:-~/.config}/sctd/sctdrc"
is_int() {
printf %d "$1" >/dev/null 2>&1
}
is_float() {
[ -z "${1##*.*}" ] && printf %f "$1" >/dev/null 2>&1
}
unfloat_num() {
float_num="$1"
whole=${float_num%%.*}
fractional=${float_num##*.}
fraclen=${#fractional}
divider=10
# adjust the divider to lenght of fractional digits
[ "$fraclen" -ge 2 ] && divider="$divider"0
# trim the fractional to 2 digits
if [ "$fraclen" -gt 2 ]; then
chars_to_trim=$((fraclen-2))
num_pattern=[0-9]
# build pattern
count=1
while [ "$count" -le "$chars_to_trim" ]; do
chars_pattern="$chars_pattern""$num_pattern"
count=$((count+1))
done
# remove extra fractional digits
fractional="${fractional%%$chars_pattern}"
fi
assembled="$whole""$fractional"
# math
scaled="$((assembled*60/divider))"
printf '%s\n' "$scaled"
}
scale_time() {
result=0
case "$1" in
*[0-9]) # just pass the num
if is_int "$1"; then
result="$1"
fi
;;
*[Ss]) # just strip last char
stripped=${1%%[Ss]}
if is_int "$stripped"; then
result="$stripped"
fi
;;
*[Mm]) # scale to seconds
stripped=${1%%[Mm]}
if is_float "$stripped"; then
unfloated=$(unfloat_num $stripped)
result="$unfloated"
else
if is_int "$stripped"; then
result="$((stripped*60))"
fi
fi
;;
*[Hh]) # scale to seconds
stripped=${1%%[Hh]}
if is_float "$stripped"; then
unfloated=$(unfloat_num $stripped)
result="$((unfloated*60))"
else
if is_int "$stripped"; then
result="$((stripped*60*60))"
fi
fi
;;
esac
printf '%s\n' "$result"
}
loadConfig () {
# defaults, in case config doesn't contain them
# default value of max temp is same as the default screen color temp of x11
DEF_TEMP_MIN=4500
DEF_TEMP_MAX=6500
DEF_INTERVAL=60
# load config if it exists
[ -f "$CONFIG" ] && . "$CONFIG"
[ "$DBGOUT" = 1 ] && echo "loaded config: $CONFIG"
# check values
# TEMP_MIN
if [ -z "$TEMP_MIN" ]; then
TEMP_MIN="$DEF_TEMP_MIN"
else
is_int "$TEMP_MIN" || TEMP_MIN="$DEF_TEMP_MIN"
fi
[ "$DBGOUT" = 1 ] && echo "temp MIN: $TEMP_MIN"
# TEMP_MAX
if [ -z "$TEMP_MAX" ]; then
TEMP_MAX="$DEF_TEMP_MAX"
else
is_int "$TEMP_MAX" || TEMP_MAX="$DEF_TEMP_MAX"
fi
[ "$DBGOUT" = 1 ] && echo "temp MAX: $TEMP_MAX"
# INTERVAL
if [ -z "$INTERVAL" ]; then
INTERVAL="$DEF_INTERVAL"
else
INTERVAL=$(scale_time $INTERVAL)
fi
# cut off interval to 60 seconds
if [ "$INTERVAL" -lt "$DEF_INTERVAL" ]; then
INTERVAL="$DEF_INTERVAL"
fi
[ "$DBGOUT" = 1 ] && echo "update INTERVAL: $INTERVAL"
# calculate increment
INCREMENT=$(( (TEMP_MAX - TEMP_MIN) / 720 ))
# 12 hours is 720 minutes
# we cycle 10 times per second, so the interval has to be scaled
INTERVAL=$(( INTERVAL * 10 ))
}
# use the right sct executable
SCT=$(command -v sct)
[ -z "$SCT" ] && SCT=$(command -v xsct)
[ -z "$SCT" ] && SCT=$(command -v waysct)
if [ ! -e "$SCT" ]; then
echo "Please install sct!"
exit 1;
fi
# set HM var to current time of the day in minutes
setHM () {
H=$(date +"%H" | sed -e 's/^0//')
M=$(date +"%M" | sed -e 's/^0//')
HM=$((H*60 + M))
}
# calculate temperature according to current time
setTEMP() {
# if time is above mid day 12:00
if [ $HM -gt 720 ]; then
TEMP=$(( TEMP_MIN + INCREMENT * (1440 - HM) ))
else
TEMP=$(( TEMP_MIN + INCREMENT * HM ))
fi
}
# set color temperature
tick() {
# run setHM for current time
setHM
# check the state of the toggle
# 1 will use the max temperature
# 0 will us the dynamically calculated value
[ "$TOGGLE_STATE" = 1 ] && SET_TEMP=max
[ "$TOGGLE_STATE" = 0 ] && SET_TEMP=dyn
[ "$DBGOUT" = 1 ] && echo "temp setting: $SET_TEMP"
# temp min is an option here but currently unused
case "$SET_TEMP" in
max) TEMP="$TEMP_MAX" ;;
dyn) setTEMP ;;
min) TEMP="$TEMP_MIN" ;;
esac
[ "$DBGOUT" = 1 ] && echo "time is: $HM"
[ "$DBGOUT" = 1 ] && echo "temperature is: $TEMP"
$SCT $TEMP
}
outHandler () {
NO_CONTINUE=1
[ "$DBGOUT" = 1 ] && echo "exiting on signal: $1"
[ "$DBGOUT" = 1 ] && echo "temperature is: $TEMP_MAX"
$SCT $TEMP_MAX
}
# handle unexpected exits and termination
trap 'outHandler "INT"' INT
trap 'outHandler "TERM"' TERM
hupHandler () {
loadConfig
tick
}
# get signal HUP
trap hupHandler HUP
TOGGLE_STATE=0
toggleSwitch () {
case "$TOGGLE_STATE" in
0) TOGGLE_STATE=1 ;;
1) TOGGLE_STATE=0 ;;
esac
[ "$DBGOUT" = 1 ] && echo "toggle state: $TOGGLE_STATE"
tick
}
# get signal USR1
trap toggleSwitch USR1
# input parsing
while [ "$#" -gt 0 ]; do
case "$1" in
debug) DBGOUT=1 ;;
oneshot) ONESHOT=1 ;;
*) echo "${0}: error, invalid argument: ${1}" ;;
esac
shift
done
[ "$DBGOUT" = 1 ] && echo "${0}"
loadConfig
# do we run as a one shot?
if [ "$ONESHOT" = 1 ]; then
tick
else
count=0
# firs color temp change
tick
while [ -z "$NO_CONTINUE" ]; do
# is the count of cycle iterations the same as the interval?
if [ "$count" = "$INTERVAL" ]; then
tick
# reset the count to 0
count=0
fi
# ingrement the count
count=$(( count + 1 ))
# the duty cycle of this daemon is 10 iterations per second
# this is fast enough to feel responsive to signales, yet not hog
# resources, mainly cpu
sleep 0.1
done
fi