-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
190 lines (159 loc) · 4.02 KB
/
main.go
File metadata and controls
190 lines (159 loc) · 4.02 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"log"
"os"
"os/exec"
"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.Printf("Failed to read device, did you type the device name correctly? (you supplied %s) \n", device)
log.Fatal(err)
}
func checkCapabilities() {
exe, err := os.Executable()
if err != nil {
return // Can't check, just continue
}
cmd := exec.Command("getcap", exe)
output, err := cmd.Output()
if err != nil {
return // getcap not available or failed, just continue
}
// Check if cap_dac_override is present
if !strings.Contains(string(output), "cap_dac_override") {
fmt.Fprintf(os.Stderr, "Error: Missing required capabilities.\n")
fmt.Fprintf(os.Stderr, "The program may fail to modify brightness without proper permissions.\n")
fmt.Fprintf(os.Stderr, "Please run: sudo setcap cap_dac_override+ep %s\n\n", exe)
os.Exit(1)
}
}
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 := os.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 := os.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 := os.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 := os.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]
}
// Check capabilities for commands that modify brightness
checkCapabilities()
switch cmd {
case "help":
fmt.Print(usage)
case "--help":
fmt.Print(usage)
case "set":
setCommand()
case "inc":
incCommand()
case "dec":
decCommand()
case "ls":
listCommand()
default:
listCommand()
}
}