about summary refs log tree commit diff stats
path: root/pkgs/by-name/br/brightness/brightness.sh
blob: 3d6b2335b4a3aed6a88128a15948f9b82e8b2bc8 (plain) (blame)
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
#!/usr/bin/env sh

die() {
    echo "ERROR: $1"
    exit 1
}

help() {
    cat <<EOF
This is a system brightness manager

USAGE:
    $NAME set VALUE

OPTIONS:
    --help   | -h
                                Output this help and exit.

COMMANDS:
    set VALUE
                                Set the brightness to the specified percentage.

ARGUMENTS:
    VALUE := [[seq 0 100]]
                                The amount to increase/decrease the brightness. In percentage
EOF
}

BACKLIGHT="/sys/class/backlight/$BACKLIGHT_NAME"

brightness() {
    perc="$1"

    max="$(cat "$BACKLIGHT/max_brightness")"
    new="$(echo | awk --assign=perc="$perc" '{printf (perc / 100)}')"
    output="$(echo | awk --assign=new="$new" --assign=max="$max" '{printf max * new}')"
    echo "$output" >"$BACKLIGHT/brightness"
}

for arg in "$@"; do
    case "$arg" in
    "--help" | "-h")
        help
        exit 0
        ;;
    esac
done

[ "$(id --user)" != 0 ] && die "This script requires root (as it needs to write into '$BACKLIGHT/brightness')"

case "$1" in
"set")
    shift 1

    if [ -n "$1" ]; then
        value="$1"
    else
        die "No value specified"
    fi

    brightness "$value"
    ;;
*)
    die "The command '$1' does not exist! See '--help' for a list"
    ;;
esac

# vim: ft=sh