/
xbacklight.go
139 lines (113 loc) · 3.3 KB
/
xbacklight.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
package xorg
import (
"encoding/binary"
"fmt"
"math"
"github.com/BurntSushi/xgb/randr"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/xprop"
)
type Backlight struct {
x *xgbutil.XUtil
resources *randr.GetScreenResourcesCurrentReply
atom xproto.Atom
}
func NewBacklight() (*Backlight, error) {
x, err := xgbutil.NewConn()
if err != nil {
return nil, err
}
if err := randr.Init(x.Conn()); err != nil {
return nil, err
}
resources, err := randr.GetScreenResourcesCurrent(x.Conn(), x.RootWin()).Reply()
if err != nil {
return nil, err
}
atom, err := xprop.Atom(x, "Backlight", false)
if err != nil {
return nil, err
}
b := &Backlight{
x: x,
resources: resources,
atom: atom,
}
return b, nil
}
// If the return is -1 then the output is not valid
func (b *Backlight) getRawBrightessForOutput(output randr.Output) (int, error) {
atomNew, err := xprop.Atom(b.x, "Backlight", false)
if err != nil {
return -1, err
}
b.atom = atomNew
var prop *randr.GetOutputPropertyReply
prop, err = randr.GetOutputProperty(b.x.Conn(), output, atomNew, xproto.AtomNone, 0, 4, false, false).Reply()
if err != nil {
// Try with legacy API
atomLegacy, err := xprop.Atom(b.x, "BACKLIGHT", false)
if err != nil {
return -1, err
}
prop, err = randr.GetOutputProperty(b.x.Conn(), output, atomLegacy, xproto.AtomNone, 0, 4, false, false).Reply()
if err != nil {
return -1, err
}
b.atom = atomLegacy
}
if prop.Type != xproto.AtomInteger ||
prop.NumItems != 1 ||
prop.Format != 32 {
return -1, fmt.Errorf("Invalid return type for getRawBrightessForOutput")
}
return int(binary.LittleEndian.Uint32(prop.Data)), nil
}
func (b *Backlight) getRawRangeForOutput(output randr.Output) (int, int, error) {
if b.atom == xproto.AtomNone {
atom, err := xprop.Atom(b.x, "Backlight", false)
if err != nil {
return 0, 0, err
}
b.atom = atom
}
query, err := randr.QueryOutputProperty(b.x.Conn(), output, b.atom).Reply()
if err != nil {
return 0, 0, err
}
if query.Range && query.Length == 2 {
return int(query.ValidValues[0]), int(query.ValidValues[1]), nil
}
return 0, 0, fmt.Errorf("Invalid return type for getRawRangeForOutput")
}
// Gets first valid value
// func (b *Backlighter) Get() (float64, error) {
// rawBacklight, err := backlightGet(b.x, b.output)
// if err != nil {
// return 0, err
// }
// return float64(rawBacklight-b.min) / float64(b.max-b.min), nil
// }
func (b *Backlight) SetAll(brightness int) {
for i := 0; i < len(b.resources.Outputs); i++ {
output := b.resources.Outputs[i]
current, err := b.getRawBrightessForOutput(output)
// Invalid output, continue
if current == -1 || err != nil {
continue
}
min, max := 0, 0
min, max, err = b.getRawRangeForOutput(output)
if err != nil {
continue
}
currentNormalized := float64(current-min) * 100 / float64(max-min)
// backlight_set
data := make([]byte, 4)
newNormalized := uint32(min) + uint32(math.Ceil(float64(brightness)*float64(max-min)/100))
binary.LittleEndian.PutUint32(data, newNormalized)
randr.ChangeOutputProperty(b.x.Conn(), output, b.atom, xproto.AtomInteger, 32, xproto.PropModeReplace, 1, data)
fmt.Printf("Xorg: Output = %d, Current = %d, New = %d\n", i, int(currentNormalized), int(newNormalized))
}
}