summaryrefslogtreecommitdiffstats
path: root/bin/bright
blob: 791e0df4d290577c583afc5c6f7491412ea9f144 (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
#!/bin/sh

set -eu

STEPS=8
MIN=2
max=

get_b()
{
	local b=

	b=$(brightnessctl --class=backlight get) || return 1
	printf '%.0f\n' $(printf \
		'scale = 20;'$(: \
		)'((%d - 1) * sqrt((%d - %d) * (%d - (2 * %d - %d)))) / '$(: \
		)'(%d - %d)\n' \
		${STEPS} ${MIN} ${b} ${b} ${max} ${MIN} ${max} ${MIN} | bc)

	return 0
}

set_b()
{
	local b="${1}"
	shift 1

	brightnessctl --class=backlight set $(printf '%.0f\n' $(printf \
		'scale = 20;'$(: \
		)'%d + (1 - sqrt(1 - (%d / (%d - 1))^2)) * (%d - %d)\n' \
		${MIN} ${b} ${STEPS} ${max} ${MIN} | bc)) || return 1

	return 0
}

usage()
{
	printf 'Usage: %s {-|+}\n' "${0}"

	return 0
}

main()
{
	local dir=
	local b=

	if [ ${#} -ne 1 ]; then
		usage 1>&2
		return 1
	fi
	dir="${1}"
	shift 1

	case "${dir}" in
		-|+);;
		*)
			usage 1>&2
			return 1
			;;
	esac

	max=$(brightnessctl --class=backlight max)

	b=$(get_b) || return 1
	: $((b += 1))
	: $((b ${dir}= 1))
	[ ${b} -gt 8 ] && b=8
	[ ${b} -lt 1 ] && b=1
	: $((b -= 1))
	set_b ${b} || return 1

	return 0
}

main "${@}"