summaryrefslogtreecommitdiffstats
path: root/bl
blob: 7db821e45ce410b83655d0b26a97e1e81e717d81 (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
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
#!/bin/sh

# This software is released into the public domain.
# It is provided "as is", without warranties or conditions of any kind.
# Anyone is free to use, modify, redistribute and do anything with this software.

# backlight brightness configuration utility
#
# Requirements: POSIX sh, backlight in /sys/class/backlight/*,
#               write permission for $BACKLIGHT/brightness

set -eu

sys_backlight="/sys/class/backlight"

get_bldir() {
	bldir=
	if [ -n "${BACKLIGHT:-}" ]; then
		bldir="$sys_backlight/$BACKLIGHT"
	else
		for f in "$sys_backlight"/*; do
			if [ "$f" = "$sys_backlight/*" ]; then
				printf "cannot find a backlight in %s\n" "$sys_backlight" >&2
				exit 1
			elif [ -n "$bldir" ]; then
				printf "multiple backlights found, choose one with \$BACKLIGHT\n" >&2
				exit 1
			fi
			bldir="$f"
			BACKLIGHT="${f##*/}"
		done
	fi
}

read_num() {
	read -r data <"$1"
	if ! is_num "$data"; then
		printf "expected a number reading '%s', but got '%s'\n" "$1" "$data" >&2
		exit 1
	fi
	printf %d "$data"
}

is_num() {
	case "$1" in
		[0-9]) return 0 ;;
		[0-9][0-9]) return 0 ;;
		[0-9][0-9][0-9]) return 0 ;;
		[0-9][0-9][0-9][0-9]) return 0 ;;
		[0-9][0-9][0-9][0-9][0-9]) return 0 ;;
		[0-9][0-9][0-9][0-9][0-9][0-9][0-9]) return 0 ;;
		[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) return 0 ;;
		[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) return 0 ;;
		# hopefully it's not a >2^32 number
	esac
	return 1
}

get_brightness() {
	raw_max="$(read_num "$bldir/max_brightness")"
	raw_curr="$(read_num "$bldir/brightness")"
	max=100
	curr=$(( (raw_curr + (raw_max / 2 / max)) * max / raw_max ))

	# brightness 0 tends to completely turn off the backlight
	raw_min=1
	case "$BACKLIGHT" in
		nvidia_[0-9]*) raw_min=0 ;; # nvidia_0 at 0 is still not off
	esac
}

get_raw_new() {
	op="$1"
	val="$2"
	raw_new=
	new=

	if ! is_num "$val"; then
		printf "expected a number for brightness, but got '%s'\n" "$val" >&2
		exit 2
	fi

	case "$op" in
		+) : $(( new = curr + val )) ;;
		-) : $(( new = curr - val )) ;;
		"") : $(( new = val )) ;;
		=+) : $(( raw_new = raw_curr + val )) ;;
		=-) : $(( raw_new = raw_curr - val )) ;;
		=) : $(( raw_new = val )) ;;
		*)
			printf "invalid operator '%s'\n" "$op" >&2
			exit 2
			;;
	esac
	if [ -n "$new" ]; then
		: $(( raw_new = new * raw_max / max ))
	fi
	if [ -n "$raw_new" ]; then
		if [ "$raw_new" -gt "$raw_max" ]; then
			raw_new="$raw_max"
		fi
		if [ "$raw_new" -lt "$raw_min" ]; then
			raw_new="$raw_min"
		fi
	fi
}

set_brightness() {
	get_raw_new "$1" "$2"
	printf %d "$raw_new" > "$bldir/brightness"
}

usage() {
	prog="$(basename "$0")"
	printf "%s: usage: %s OPERATOR\n" "$prog" "$prog"
	printf "\n"
	printf "Operators:\n"
	printf "  (none)     print current brightness in percentages\n"
	printf "  PERC       set brightness to PERC percentages of max\n"
	printf "  +PERC      increase brightness by PERC percentage points\n"
	printf "  -PERC      decrease brightness by PERC percentage points\n"
	printf "  =          print current brightness raw value\n"
	printf "  VAL        set brightness to VAL raw value\n"
	printf "  =+VAL      increase brightness by VAL raw value\n"
	printf "  =-VAL      decrease brightness by VAL raw value\n"
	printf "\n"
	printf "Environment variables:\n"
	printf "  BACKLIGHT  the backlight in %s/ to use\n" "$sys_backlight"
	printf "             (unnecessary if only one exists)\n"
}

main() {
	if [ $# -gt 1 ]; then
		usage >&2
		exit 2
	fi
	if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
		usage
		exit
	fi
	get_bldir
	get_brightness
	op="${1:-}"
	case "$op" in
		"") printf "%d\n" "$curr" ;;
		+[0-9]*)  set_brightness +    "${op#+}" ;;
		-[0-9]*)  set_brightness -    "${op#-}" ;;
		[0-9]*)   set_brightness ""   "$op" ;;
		=) printf "%d\n" "$raw_curr" ;;
		=+[0-9]*) set_brightness "=+" "${op#=+}" ;;
		#+=[0-9]*) set_brightness "=+" "${op#+=}" ;;
		=-[0-9]*) set_brightness "=-" "${op#=-}" ;;
		#-=[0-9]*) set_brightness "=-" "${op#-=}" ;;
		=[0-9]*)  set_brightness "="  "${op#=}" ;;
		*)
			printf "invalid operator: '%s'\n" "$op" >&2
			exit 2
	esac
}

main "$@"