forked from SergKolo/sergrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_monitor.sh
128 lines (110 loc) · 3.2 KB
/
source_monitor.sh
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
#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: 1047481448@qq.com
# Date: June 18th 2016
# Purpose: Script that remembers and sets brightness
# depending on power sources
#
# Written for: http://askubuntu.com/q/788383/295286
# Tested on: Ubuntu 16.04 LTS , Ubuntu Kylin 16.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#
# Permission to use, copy, modify, and distribute this software is hereby granted
# without fee, provided that the copyright notice above and this permission statement
# appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
ARGV0="$0"
ARGC=$#
wait_ac_connect()
{
while ! on_ac_power ; do : ; sleep 0.25 ; done
$VERBOSE && echo "<<< adapter plugged in"
}
wait_ac_disconnect()
{
while on_ac_power ; do : ; sleep 1.25 ; done
$VERBOSE && echo "<<< adapter unplugged"
}
change_brightness()
{
qdbus org.gnome.SettingsDaemon \
/org/gnome/SettingsDaemon/Power \
org.gnome.SettingsDaemon.Power.Screen.SetPercentage "$1"
}
get_brightness()
{
qdbus org.gnome.SettingsDaemon \
/org/gnome/SettingsDaemon/Power \
org.gnome.SettingsDaemon.Power.Screen.GetPercentage
}
print_usage()
{
cat <<EOF
source_monitor.sh [-a INT] [-b INT] [-v] [-h]
-a set initial brightness on AC adapter
-b set initial brightness on batter
-v enable verbose output
-h prints this help text
Copyright Serg Kolo , 2016
EOF
}
parse_args()
{
# boiler-pate code for reusing, options may change
local OPTIND opt # OPTIND must be there,
# opt can be renamed to anything
# no leading : means errors reported(which is what i want)
# : after letter means options takes args, no : - no args
while getopts "a:b:vh" opt
do
case ${opt} in
a) AC_PERCENTAGE="${OPTARG}"
;;
b) BAT_PERCENTAGE="${OPTARG}"
;;
v) VERBOSE=true
;;
h) print_usage && exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND-1))
}
main()
{
# default values, if -a,-b, or -v options given
# they will be changed
local BAT_PERCENTAGE=50
local AC_PERCENTAGE=90
local VERBOSE=false # for debugging
parse_args "$@"
while true
do
if on_ac_power ; then
wait_ac_disconnect
AC_PERCENTAGE=$(($(get_brightness)+1)) # too long to explain why +1
# just try it for yourself
sleep 0.25
change_brightness "$BAT_PERCENTAGE" > /dev/null
else
wait_ac_connect
BAT_PERCENTAGE=$(($(get_brightness)+1))
sleep 0.25
change_brightness "$AC_PERCENTAGE" > /dev/null
fi
sleep 0.25
done
}
main "$@"