-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathbrightness.go
308 lines (265 loc) · 6.9 KB
/
brightness.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package display
import (
"encoding/json"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"github.com/linuxdeepin/startdde/wl_display/brightness"
)
type InvalidOutputNameError struct {
Name string
}
func (err InvalidOutputNameError) Error() string {
return fmt.Sprintf("invalid output name %q", err.Name)
}
func (m *Manager) saveBrightness() {
if m.settings == nil || m.settings.C == nil || m.settings.Object.C == nil {
logger.Info("[saveBrightness] object invalid, not save...")
return
}
jsonStr := jsonMarshal(m.Brightness)
logger.Info("[saveBrightness] gsettings set:", m.settings, gsKeyBrightness, jsonStr)
m.settings.SetString(gsKeyBrightness, jsonStr)
}
func (m *Manager) changeBrightness(raised bool) error {
// TODO
// return errors.New("TODO")
if m.xConn == nil {
logger.Warning("No xorg connection")
return nil
}
var step float64 = 0.05
if !raised {
step = -step
}
monitors := m.getConnectedMonitors()
for _, monitor := range monitors {
v, ok := m.Brightness[monitor.Name]
if !ok {
v = 1.0
}
var br float64
setBr := true
if blCtrl, err := brightness.GetBacklightController(monitor.ID, m.xConn); err != nil {
logger.Debugf("get output %q backlight controller failed: %v", monitor.Name, err)
} else {
max := blCtrl.MaxBrightness
cur, err := blCtrl.GetBrightness()
if err == nil {
// TODO: Some drivers will also set the brightness when the brightness up/down key is pressed
hv := float64(cur) / float64(max)
avg := (v + hv) / 2
delta := (v - hv) / avg
logger.Debugf("v: %g, hv: %g, avg: %g delta: %g", v, hv, avg, delta)
if math.Abs(delta) > 0.05 {
logger.Debug("backlight actual brightness is not set")
setBr = false
br = hv
}
}
}
if setBr {
br = v + step
if br > 1.0 {
br = 1.0
}
if br < 0.1 {
br = 0.1
}
logger.Debug("[changeBrightness] will set to:", monitor.Name, br)
err := m.doSetBrightness(br, monitor.Name)
if err != nil {
return err
}
} else {
logger.Debug("[changeBrightness] will update to:", monitor.Name, br)
err := m.doSetBrightnessFake(br, monitor.Name)
if err != nil {
return err
}
}
}
m.saveBrightness()
return nil
}
// doSetBrightnessAuxForBacklight单独处理使用F1,F2调节亮度逻辑,避免发送多次dbus信号
func (m *Manager) doSetBrightnessAuxForBacklight(fake bool, value float64, name string, isRaised bool) error {
monitors := m.getConnectedMonitors()
monitor0 := monitors.GetByName(name)
if monitor0 == nil {
return InvalidOutputNameError{Name: name}
}
monitor0.PropsMu.RLock()
enabled := monitor0.Enabled
monitor0.PropsMu.RUnlock()
var br float64
if !fake && enabled {
var step float64
var times int
if value < 0.4 {
step = 0.001
times = 50
} else if value < 0.7 {
step = 0.002
times = 25
} else if value < 0.9 {
step = 0.005
times = 10
} else {
step = 0.05
times = 1
}
if !isRaised {
step = -step
}
for i := 1; i <= times; i++ {
br = value + step*float64(i)
if br > 1.0 {
br = 1.0
}
if br < 0.1 {
br = 0.1
}
err := m.setMonitorBrightness(monitor0, br)
if err != nil {
logger.Warningf("brightness: failed to set brightness for %s: %v", name, err)
return err
}
// 防止出现多次调节亮度值不变的情况
if math.Abs(br-0.1) < 1e-5 || math.Abs(br-1.0) < 1e-5 {
break
}
}
}
value = br
oldValue := m.Brightness[name]
if oldValue == value {
return nil
}
// update brightness of the output
m.Brightness[name] = value
err := m.emitPropChangedBrightness(m.Brightness)
if err != nil {
logger.Warning(err)
}
return nil
}
func (m *Manager) getSavedBrightnessTable() (map[string]float64, error) {
value := m.settings.GetString(gsKeyBrightness)
if value == "" {
return nil, nil
}
var result map[string]float64
err := json.Unmarshal([]byte(value), &result)
if err != nil {
return nil, err
}
return result, nil
}
func (m *Manager) initBrightness() error {
brightnessTable, err := m.getSavedBrightnessTable()
if err != nil {
logger.Warning(err)
}
if brightnessTable == nil {
brightnessTable = make(map[string]float64)
}
var saved = false
monitors := m.getConnectedMonitors()
for _, monitor := range monitors {
_, ok := brightnessTable[monitor.Name]
if !ok {
// add new monitor
brightnessTable[monitor.Name] = 1
saved = true
}
}
var lightSet = false
m.brightnessMapMu.Lock()
m.Brightness = brightnessTable
m.brightnessMapMu.Unlock()
for name, v := range brightnessTable {
// set the saved brightness
if m.isConnected(name) {
err = m.doSetBrightness(v, name)
if err != nil {
logger.Warning("Failed to set default brightness:", name, err)
continue
}
if !lightSet {
lightSet = true
}
}
}
if saved {
logger.Info("Init default output brightness")
// In huawei KelvinU sometimes crash because of GObject assert failure in GSettings
m.saveBrightness()
}
if !lightSet {
return errors.New("Init default output brightness failed!")
}
return nil
}
func (m *Manager) getBrightnessSetter() string {
// NOTE: 特殊处理龙芯笔记本亮度设置问题
blDir := "/sys/class/backlight/loongson"
_, err := os.Stat(blDir)
if err == nil {
_, err := os.Stat(filepath.Join(blDir, "device/edid"))
if err != nil {
return "backlight"
}
}
return m.settings.GetString(gsKeySetter)
}
func (m *Manager) setMonitorBrightness(monitor *Monitor, value float64) error {
isBuiltin := isBuiltinOutput(monitor.Name)
logger.Debugf("brightness: setMonitorBrightness for %s, setter=%s, value=%.2f, edidBase64=%s", monitor.Name, m.getBrightnessSetter(), value, monitor.edid)
err := brightness.Set(monitor.uuid, value, m.getBrightnessSetter(), isBuiltin, monitor.edid)
return err
}
func (m *Manager) doSetBrightnessAux(fake bool, value float64, name string) error {
monitors := m.getConnectedMonitors()
monitor0 := monitors.GetByName(name)
if monitor0 == nil {
return InvalidOutputNameError{Name: name}
}
monitor0.PropsMu.RLock()
enabled := monitor0.Enabled
monitor0.PropsMu.RUnlock()
if !fake && enabled {
err := m.setMonitorBrightness(monitor0, value)
if err != nil {
logger.Warningf("failed to set brightness for %s: %v", name, err)
return err
}
}
// update brightness of the output
m.Brightness[name] = value
err := m.emitPropChangedBrightness(m.Brightness)
if err != nil {
logger.Warning(err)
}
return nil
}
func (m *Manager) doSetBrightness(value float64, name string) error {
return m.doSetBrightnessAux(false, value, name)
}
func (m *Manager) doSetBrightnessFake(value float64, name string) error {
return m.doSetBrightnessAux(true, value, name)
}
func (m *Manager) isConnected(monitorName string) bool {
monitors := m.getConnectedMonitors()
for _, monitor := range monitors {
if monitor.Name == monitorName {
return true
}
}
return false
}