-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (139 loc) · 3.36 KB
/
main.go
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
162
163
164
165
166
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
const (
sysfsDir = "/sys/class/backlight"
usage = `backlight. Simple program to control your backlights.
Usage:
backlight ls Lists all devices and their values
backlight set <device> <val> Sets the brightness of a device to a specific value
backlight dec <device> <val> Decreases the brightness of a device by a specific value
backlight inc <device> <val> Increases the brightness of a device by a specific value
backlight help Displays this help message :)
`
)
func deviceError(device string, err error) {
fmt.Println(fmt.Sprintf("Failed to read device, did you type the device name correctly? (you supplied %s) \n", device))
log.Fatal(err)
}
func setBrightness(device string, val int) {
max := getMaxBrightness(device)
if val > max {
val = max
}
if val < 0 {
val = 0
}
filename := fmt.Sprintf("%s/%s/brightness", sysfsDir, device)
err := ioutil.WriteFile(filename, []byte(strconv.Itoa(val)), 0777)
if err != nil {
fmt.Print("Failed to write to sysfs, do you have the correct permissions? \n\n")
panic(err)
}
}
func getMaxBrightness(device string) int {
filename := fmt.Sprintf("%s/%s/max_brightness", sysfsDir, device)
bytes, err := ioutil.ReadFile(filename)
if err != nil {
deviceError(device, err)
}
val, err := strconv.Atoi(strings.Trim(string(bytes), "\n "))
if err != nil {
log.Fatal("Non-integer value in max_brightness read from system", err)
}
return val
}
func getBrightness(device string) int {
filename := fmt.Sprintf("%s/%s/brightness", sysfsDir, device)
bytes, err := ioutil.ReadFile(filename)
if err != nil {
deviceError(device, err)
}
val, err := strconv.Atoi(strings.Trim(string(bytes), "\n "))
if err != nil {
log.Fatal("Non-integer value in brightness read from system", err)
}
return val
}
/*
Commands
*/
func setCommand() {
if len(os.Args) < 4 {
fmt.Println(`Missing arguments.`)
os.Exit(1)
}
device := os.Args[2]
val, err := strconv.Atoi(strings.Trim(os.Args[3], "\n "))
if err != nil {
log.Fatalln("Invalid value given, must be an integer.")
}
setBrightness(device, val)
}
func incCommand() {
if len(os.Args) < 4 {
fmt.Println(`Missing arguments.`)
os.Exit(1)
}
device := os.Args[2]
val, err := strconv.Atoi(strings.Trim(os.Args[3], "\n "))
if err != nil {
log.Fatalln("Invalid value given, must be an integer.")
}
current := getBrightness(device)
newVal := current + val
setBrightness(device, newVal)
}
func decCommand() {
if len(os.Args) < 4 {
fmt.Println(`Missing arguments.`)
os.Exit(1)
}
device := os.Args[2]
val, err := strconv.Atoi(strings.Trim(os.Args[3], "\n "))
if err != nil {
log.Fatalln("Invalid value given, must be an integer.")
}
current := getBrightness(device)
newVal := current - val
setBrightness(device, newVal)
}
func listCommand() {
files, err := ioutil.ReadDir(sysfsDir)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
brightness := getBrightness(f.Name())
max := getMaxBrightness(f.Name())
fmt.Println(f.Name(), brightness, "/", max)
}
}
func main() {
cmd := ""
if len(os.Args) > 1 {
cmd = os.Args[1]
}
switch cmd {
case "help":
fmt.Println(usage)
case "--help":
fmt.Println(usage)
case "set":
setCommand()
case "inc":
incCommand()
case "dec":
decCommand()
case "ls":
listCommand()
default:
listCommand()
}
}