-
Notifications
You must be signed in to change notification settings - Fork 2
/
ddcbr.sh
executable file
·107 lines (93 loc) · 2.39 KB
/
ddcbr.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
#!/usr/bin/env bash
DDC="ddcutil --noverify --bus"
BUS="5"
ADDRESS="10"
BASE_DIR="${HOME}"/ddcbr
FILE="${BASE_DIR}"/.brightness
LOCK_FILE=$BASE_DIR/.lock
NOTIF_FILE=$BASE_DIR/.notifId
function getCurrentBrightness {
if [ -f "$FILE" ]; then
current_br=$(cat "$FILE")
fi
if [[ ! "$current_br" =~ ^[0-9]+$ ]]; then
current_br=$("$DDC" "$BUS" getvcp "$ADDRESS" | awk '{print $9}' | awk -F ',' '{print $1}')
fi
}
function increaseBrightness {
getCurrentBrightness
if [ "$current_br" -le 90 ]; then
new_br=$((current_br + 10))
elif [ "$current_br" -lt 100 ]; then
new_br=100
else
sendNotification 100
return 0
fi
setNewBrightness
}
function maxBrightness {
new_br=100
setNewBrightness
}
function minBrightness {
new_br=0
setNewBrightness
}
function decreaseBrightness {
getCurrentBrightness
if [ "$current_br" -ge 10 ]; then
new_br=$((current_br - 10))
elif [ "$current_br" -gt 0 ]; then
new_br=0
else
sendNotification 0
return 0
fi
setNewBrightness
}
function setNewBrightness {
$DDC $BUS setvcp $ADDRESS $new_br
echo "$new_br" > "$FILE"
sendNotification "$new_br"
}
function sendNotification {
echo "Brightness: $1%"
if [ -f "$NOTIF_FILE" ]; then
id=$(cat "$NOTIF_FILE")
else
id=0
fi
if [[ ! "$id" =~ ^[0-9]+$ ]]; then
id=0
fi
id=$(gdbus call --session \
--dest org.freedesktop.Notifications \
--object-path /org/freedesktop/Notifications --method org.freedesktop.Notifications.Notify \
DDCBR \
"$id" \
notification-display-brightness-full \
"Brightness" \
"$1%" \
[] \
"{'type':<'int'>, 'name':<'value'>, 'value':<'$1'>, 'type':<'string'>, 'name':<'synchronous'>, 'value':<'volume'>}" 1)
id=$(echo "$id" | sed 's/[^ ]* //; s/,.//')
# Saving the notification ID allows replacing the notification if the script gets called again before the notification closes
echo "$id" > "$NOTIF_FILE"
}
# Do nothing if it is already running. Not a good way to do this.
if [ -f "$LOCK_FILE" ]; then
exit
else
touch "$LOCK_FILE"
fi
if [ "$1" = "i" ]; then
increaseBrightness
elif [ "$1" = "d" ]; then
decreaseBrightness
elif [ "$1" = "mx" ]; then
maxBrightness
elif [ "$1" = "mi" ]; then
minBrightness
fi
rm "$LOCK_FILE"