-
Notifications
You must be signed in to change notification settings - Fork 0
/
intel_backlight_brightness_step.go
118 lines (106 loc) · 2.47 KB
/
intel_backlight_brightness_step.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
package main
import (
"io/ioutil"
"log"
"strconv"
"strings"
"github.com/fsnotify/fsnotify"
)
var brightness_file = "/sys/class/backlight/intel_backlight/brightness"
var max_brightness_file = "/sys/class/backlight/intel_backlight/max_brightness"
var change_percent float32 = 10
var debug = false
func read_int_from_file(filepath string) int {
b, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err)
return 0
}
s := strings.TrimRight(string(b), "\n")
number, err := strconv.Atoi(s)
if err != nil {
log.Fatal(err)
return 0
}
return number
}
func max_brightness() int {
return read_int_from_file(max_brightness_file)
}
func current_brightness() int {
return read_int_from_file(brightness_file)
}
func change_brightness(val int) {
data := []byte(strconv.Itoa(val) + "\n")
err := ioutil.WriteFile(brightness_file, data, 0764)
if err != nil {
log.Fatal(err)
}
}
func main() {
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Start listening for events.
go func() {
brightness := current_brightness()
maximum_brightness := max_brightness()
brightness_step := int(float32(maximum_brightness) * 0.01 * change_percent)
if brightness_step < 2 {
log.Fatal("calculated brightness step is ", brightness_step)
return
}
if debug {
log.Println("maximum brightness:", maximum_brightness)
log.Println("brightness:", brightness)
log.Println("step:", brightness_step)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
actual_brightness := current_brightness()
if debug {
log.Println("brightness:", actual_brightness)
}
diff := actual_brightness - brightness
if diff == 1 || diff == -1 {
if debug {
log.Println("brightness changed by 1")
}
if diff == 1 {
brightness = actual_brightness + brightness_step
if brightness > maximum_brightness {
brightness = maximum_brightness
}
} else {
brightness = actual_brightness - brightness_step
if brightness < 2 {
brightness = 1
}
}
change_brightness(brightness)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// Add a path to watcher
err = watcher.Add(brightness_file)
if err != nil {
log.Fatal(err)
}
// Block main goroutine forever.
<-make(chan struct{})
}