summaryrefslogtreecommitdiff
path: root/.local/bin/backlightctl
blob: d3f6280c0dca292632992e98bb4631eff012b585 (plain)
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
#!/bin/sh

# Get the correct sub-directory for your backlight device
root_path="/sys/class/backlight/"
device="$(find "$root_path" | tail -n 1)"

max="$(cat "$device/max_brightness")"

# What percentage to increase/decrease by when no argument is given
default_step=10
# What percentage to set to when no argument is given
default_set=50

to_percent() {
	printf "%d" "$((100 * $1 / $max))"
}

to_value() {
	printf "%d" "$((max * $1 / 100))"
}

get_status() {
	cat "$device/brightness"
}

get_percent() {
	to_percent "$(get_status)"
}

# Add $1 to current brightness. Adjust if out of bounds (0-$max)
# $1 is converted to a value between 0-$max within this function
get_total() {
	value="$(to_value "$1")"
	printf "%d" "$(($(get_status) + value))"
}

helpmsg="Usage: backlightctl -[FLAG] [PERCENTAGE]
  -i, --increase: Increase brightness by n (default 10%)
  -d, --decrease: Decrease brightness by n (default 10%)
  -s, --set: Set brightness to specific value (default 50%)
  -g  --get: Get current brightness
  -q, --quiet: Don't send any notification (for use in dwmblocks)
  -h, --help: Print this help message
"

case "$1" in

-i | --increase)
	[ -z "$2" ] && step="$default_step" || step="$2"
	get_total "$step" >"$device/brightness"
	;;

-d | --decrease)
	[ -z "$2" ] && step="$default_step" || step="$2"
	get_total "-$step" >"$device/brightness"
	;;

-s | --set)
	[ -z "$2" ] && set="$default_set" || set="$2"
	to_value "$set" >"$device/brightness"
	;;

-g | --get)
	printf "󰃠 %s\n" "$(get_status)%"
	;;

-h | --help)
	printf "%s" "$helpmsg"
	;;
*)
	printf "\033[31mInvalid option: $1\033[0m\n%s" "$helpmsg" && exit 1
	;;
esac

# Send notification
case "$@" in
*-q* | *--quiet*)
	exit 0
	;;
*)
	notify-send.sh -t 1000 --replace-file /tmp/bl-notif -a "󰃠 Brightness:" "$(get_percent)%"
	;;
esac