forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huawei_mic.go
120 lines (108 loc) · 2.86 KB
/
huawei_mic.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
package keybinding
import (
"os"
"path/filepath"
"github.com/linuxdeepin/go-dbus-factory/com.deepin.daemon.helper.backlight"
"pkg.deepin.io/lib/pulse"
)
const huaweiMicLedName = "huawei::mic"
type huaweiMicLedWorkaround struct {
backlightHelper *backlight.Backlight
pulseCtx *pulse.Context
defaultSourceName string
defaultSourceMute bool
quit chan struct{}
}
func (c *AudioController) initHuaweiMicLedWorkaround(backlightHelper *backlight.Backlight) {
fileInfo, err := os.Stat(filepath.Join("/sys/class/leds", huaweiMicLedName))
if err != nil {
return
}
if fileInfo.IsDir() {
c.huaweiMicLedWorkaround = &huaweiMicLedWorkaround{}
c.huaweiMicLedWorkaround.backlightHelper = backlightHelper
c.huaweiMicLedWorkaround.quit = make(chan struct{})
c.huaweiMicLedWorkaround.initPulseCtx()
}
}
func (h *huaweiMicLedWorkaround) destroy() {
close(h.quit)
}
func (h *huaweiMicLedWorkaround) initPulseCtx() {
h.pulseCtx = pulse.GetContext()
if h.pulseCtx != nil {
h.defaultSourceName = h.pulseCtx.GetDefaultSource()
for _, source := range h.pulseCtx.GetSourceList() {
if source.Name == h.defaultSourceName {
h.defaultSourceMute = source.Mute
h.setHuaWeiMicLed(source.Mute)
break
}
}
eventChan := make(chan *pulse.Event, 100)
h.pulseCtx.AddEventChan(eventChan)
go func() {
for {
select {
case ev := <-eventChan:
switch ev.Facility {
case pulse.FacilityServer:
h.handlePulseServerEvent()
case pulse.FacilitySource:
h.handlePulseSourceEvent(ev.Type, ev.Index)
}
case <-h.quit:
return
}
}
}()
}
}
func (h *huaweiMicLedWorkaround) handlePulseServerEvent() {
logger.Debug("[Event] server")
defaultSourceName := h.pulseCtx.GetDefaultSource()
if h.defaultSourceName != defaultSourceName {
// default source changed
sources := h.pulseCtx.GetSourceList()
for _, source := range sources {
if source.Name == h.defaultSourceName {
if h.defaultSourceMute != source.Mute {
// mute changed
h.setHuaWeiMicLed(source.Mute)
h.defaultSourceMute = source.Mute
}
break
}
}
}
}
func (h *huaweiMicLedWorkaround) handlePulseSourceEvent(eType int, idx uint32) {
switch eType {
case pulse.EventTypeChange, pulse.EventTypeNew:
logger.Debugf("[Event] source #%d changed", idx)
source, err := h.pulseCtx.GetSource(idx)
if err != nil {
logger.Warning(err)
return
}
if source.Name == h.defaultSourceName {
// is default source
if h.defaultSourceMute != source.Mute {
// mute changed
h.setHuaWeiMicLed(source.Mute)
h.defaultSourceMute = source.Mute
}
}
}
}
func (h *huaweiMicLedWorkaround) setHuaWeiMicLed(mute bool) {
logger.Debug("setHuaWeiMicLed", mute)
var val int32
if mute {
val = 1
}
err := h.backlightHelper.SetBrightness(0, backlightTypeKeyboard, huaweiMicLedName, val)
if err != nil {
logger.Warning(err)
}
}