-
Notifications
You must be signed in to change notification settings - Fork 2
/
init
executable file
·213 lines (167 loc) · 4.51 KB
/
init
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
#!/bin/sh
CURWDIR="$(cd $(dirname $0) && pwd)"
PWD=$CURWDIR
TITLE="屏幕设置"
PROGRAM_NAME="backlight-control"
CUSTOM_BIN="/system/apps/tp/bin/custom"
CUSTOM_CONFIG_FILE="$CURWDIR/custom.conf"
TMP_FILE="/tmp/conf"
APPS_CONFIG_DIR="/data/conf"
LAUNCHER_CONFIG_DIR="$APPS_CONFIG_DIR/launcher/conf.d"
LAUNCHER_CONFIG_FILE="$LAUNCHER_CONFIG_DIR/$PROGRAM_NAME.conf"
ICON="res/icon.png"
PRESSED_ICON="res/icon_p.png"
PID_FILE="$CURWDIR/$PROGRAM_NAME.pid"
PKILL_BIN="/usr/bin/pkill"
INSTALL_BIN="/usr/bin/install"
REMOVE="/bin/rm -f"
LUA_BIN="/system/bin/lua"
LUA_SCRIPT_PATH="/system/share/lua/5.1/tp_entry.lua"
UCI_BACKLIGHT="modou.backlight"
UCI_BRIGHTNESS="modou.backlight.brightness"
UCI_SLEEP_TIME="modou.backlight.timeout"
DEFAULT_BRIGHTNESS=100
DEFAULT_SLEEP_TIME=180
PROFILE="$PWD/conf/profile"
OPTION_TEMPLATE="$PWD/conf/option.template"
usage() {
echo "ERROR: action missing"
echo "syntax: $0 <start|stop|restart|status|config|install|uninstall>"
echo "example: $0 start"
}
start() {
echo "not implemented"
}
stop() {
echo "not implemented"
}
func_return_sleep_txt=
sleep_time_2_sleep_txt()
{
local txt=
if [ "$1" == "0" ] ; then
func_return_sleep_txt="从不"
else
local m=
let "m=sleep_time/60"
func_return_sleep_txt=$m"分钟"
fi
}
func_return_sleep_time=
sleep_txt_2_sleep_time()
{
case "$1" in
"从不" )
func_return_sleep_time=0;;
"3分钟" )
func_return_sleep_time=180;;
"10分钟" )
func_return_sleep_time=600;;
"30分钟" )
func_return_sleep_time=1800;;
* )
func_return_sleep_time=0;;
esac
}
sync_config_from_uci()
{
local brightness=`uci get $UCI_BRIGHTNESS`
local sleep_time=`uci get $UCI_SLEEP_TIME`
if [ "$brightness" == "" -o "$sleep_time" == "" ] ; then
uci set $UCI_BACKLIGHT=service
brightness=$DEFAULT_BRIGHTNESS
sleep_time=$DEFAULT_SLEEP_TIME
uci set $UCI_BRIGHTNESS=$brightness
uci set $UCI_SLEEP_TIME=$sleep_time
uci commit modou
fi
sleep_time_2_sleep_txt $sleep_time
local sleep_txt=$func_return_sleep_txt
json4sh.sh set $CURWDIR/conf/data.json "brightness" value $brightness
json4sh.sh set $CURWDIR/conf/data.json "sleep_time" value $sleep_txt
save_to_profile $sleep_txt $brightness
}
save_to_profile()
{
local sleep_txt=$1
local brightness=$2
rm $PROFILE
echo $sleep_txt > $PROFILE
echo $brightness >> $PROFILE
}
set_config()
{
sync_config_from_data_json
}
sync_config_from_data_json()
{
local brightness=`json4sh.sh get $CURWDIR/conf/data.json "brightness" value`
local sleep_txt=`json4sh.sh get $CURWDIR/conf/data.json "sleep_time" value`
sleep_txt_2_sleep_time $sleep_txt
local sleep_time=$func_return_sleep_time
uci set $UCI_BRIGHTNESS=$brightness
uci set $UCI_SLEEP_TIME=$sleep_time
uci commit modou
save_to_profile $sleep_txt $brightness
blcontrol -b $brightness -t $sleep_time
}
sync_config_from_profile()
{
local sleep_txt=`cat $PROFILE | head -n 1`
local brightness=`cat $PROFILE | tail -n 1`
sleep_txt_2_sleep_time $sleep_txt
local sleep_time=$func_return_sleep_time
uci set $UCI_BRIGHTNESS=$brightness
uci set $UCI_SLEEP_TIME=$sleep_time
uci commit modou
json4sh.sh set $CURWDIR/conf/data.json "brightness" value $brightness
json4sh.sh set $CURWDIR/conf/data.json "sleep_time" value $sleep_txt
blcontrol -b $brightness -t $sleep_time
}
run() {
$PWD/bin/generate-config-file-2 $OPTION_TEMPLATE $PROFILE
sync_config_from_profile
#custom $PWD/conf/custom.conf
}
install()
{
echo "{" > "$TMP_FILE"
echo "\"name\" : \"$TITLE\"," >> "$TMP_FILE"
echo "\"icon\" : \"$CURWDIR/$ICON\"," >> "$TMP_FILE"
echo "\"iconPressed\" : \"$CURWDIR/$PRESSED_ICON\"," >> "$TMP_FILE"
echo "\"exec\" : \"$CURWDIR/init run\"," >> "$TMP_FILE"
echo "\"msgNum\" : 4" >> "$TMP_FILE"
echo "}" >> "$TMP_FILE"
$INSTALL_BIN -d $LAUNCHER_CONFIG_DIR
mv "$TMP_FILE" "$LAUNCHER_CONFIG_FILE"
sync_config_from_uci
}
uninstall() {
$REMOVE "$LAUNCHER_CONFIG_FILE"
}
# main
if [ $# -lt 1 ]; then
usage
exit 255
fi
case "$1" in
"start" )
start;;
"stop" )
stop;;
"run" )
run;;
"restart" )
start
stop;;
"install" )
install;;
"uninstall" )
uninstall;;
"config" )
config;;
"set_config" )
set_config;;
* )
usage ;;
esac