forked from linuxdeepin/startdde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_effect.go
234 lines (201 loc) · 5.17 KB
/
sound_effect.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
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
dbus1 "github.com/godbus/dbus/v5"
"github.com/linuxdeepin/dde-api/soundutils"
soundthemeplayer "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.soundthemeplayer1"
"github.com/linuxdeepin/go-lib/pulse"
)
var soundThemePlayer soundthemeplayer.SoundThemePlayer
func playLoginSound() {
// NOTE: always start pulseaudio
err := startPulseAudio()
if err != nil {
logger.Warning("failed to start pulseaudio:", err)
}
markFile := filepath.Join(os.TempDir(), "startdde-login-sound-mark")
_, err = os.Stat(markFile)
if err == nil {
// already played
return
} else if !os.IsNotExist(err) {
logger.Warning(err)
}
defer func() {
err := ioutil.WriteFile(markFile, nil, 0644)
if err != nil {
logger.Warning(err)
}
}()
autoLoginUser, err := getLightDMAutoLoginUser()
if err != nil {
logger.Warning(err)
return
}
u, err := user.Current()
if err != nil {
logger.Warning(err)
return
}
if u.Username != autoLoginUser {
return
}
logger.Info("PlaySystemSound DesktopLogin")
err = soundutils.PlaySystemSound(soundutils.EventDesktopLogin, "")
if err != nil {
logger.Warning("PlaySystemSound DesktopLogin failed:", err)
}
logger.Info("PlaySystemSound DesktopLogin done")
}
func getDefaultSinkAlsaDevice() (device string, mute bool, err error) {
ctx := pulse.GetContext()
if ctx == nil {
err = errors.New("failed to get pulse.Context")
return
}
defaultSink := getPulseDefaultSink(ctx)
if defaultSink == nil {
err = errors.New("failed to get default sink")
return
}
if defaultSink.Mute {
mute = true
return
}
device, err = getSinkAlsaDevice(defaultSink)
return
}
func playLogoutSound() {
device, mute, err := getDefaultSinkAlsaDevice()
if err != nil {
logger.Warning(err)
quitPulseAudio()
return
}
if mute {
logger.Debug("default sink is mute")
quitPulseAudio()
return
}
logger.Debugf("ALSA device: %q", device)
quitPulseAudio()
err = soundThemePlayer.Play(0, soundutils.GetSoundTheme(),
soundutils.EventDesktopLogout, device)
if err != nil {
logger.Warning("SoundThemePlayer.Play err:", err)
}
}
func initSoundThemePlayer() {
sysBus, err := dbus1.SystemBus()
if err != nil {
return
}
soundThemePlayer = soundthemeplayer.NewSoundThemePlayer(sysBus)
}
const (
audioInterface = "org.deepin.dde.Audio1"
audioServiceName = audioInterface
audioPath = "/org/deepin/dde/Audio1"
)
func startPulseAudio() error {
err := exec.Command("systemctl", "--user", "--runtime", "unmask", "pulseaudio.service").Run()
if err != nil {
return err
}
err = exec.Command("systemctl", "--user", "start", "pulseaudio.service").Run()
if err != nil {
return err
}
return nil
}
func quitPulseAudio() {
logger.Debug("quit pulse audio")
sessionBus, err := dbus1.SessionBus()
if err != nil {
logger.Warning("failed to get session bus:", err)
} else {
audioObj := sessionBus.Object(audioServiceName, audioPath)
err = audioObj.Call(audioInterface+".NoRestartPulseAudio", dbus1.FlagNoAutoStart).Err
if err != nil {
logger.Warning("failed to call NoRestartPulseAudio:", err)
}
}
// mask pulseaudio.service
out, err := exec.Command("systemctl", "--user", "--runtime", "--now", "mask",
"pulseaudio.service").CombinedOutput()
if err != nil {
logger.Warningf("temp mask pulseaudio.service failed err: %v, out: %s", err, out)
}
// view status
err = exec.Command("systemctl", "--quiet", "--user", "is-active",
"pulseaudio.service").Run()
if err == nil {
logger.Warning("pulseaudio.service is still running")
} else {
logger.Debug("pulseaudio.service is stopped, err:", err)
}
}
func preparePlayShutdownSound() {
canPlay := soundutils.CanPlayEvent(soundutils.EventSystemShutdown)
var device string
if canPlay {
var mute bool
var err error
device, mute, err = getDefaultSinkAlsaDevice()
if err != nil {
logger.Warning(err)
return
}
if mute {
logger.Debug("default sink is mute")
canPlay = false
}
}
var cfg soundutils.ShutdownSoundConfig
if canPlay {
cfg = soundutils.ShutdownSoundConfig{
CanPlay: canPlay,
Theme: soundutils.GetSoundTheme(),
Event: soundutils.EventSystemShutdown,
Device: device,
}
}
logger.Debugf("set shutdown sound config: %+v", cfg)
err := soundutils.SetShutdownSoundConfig(&cfg)
if err != nil {
logger.Warning("failed to set shutdown sound config:", err)
}
}
func getPulseDefaultSink(ctx *pulse.Context) (defaultSink *pulse.Sink) {
defaultSinkName := ctx.GetDefaultSink()
for _, sink := range ctx.GetSinkList() {
if sink.Name == defaultSinkName {
defaultSink = sink
break
}
}
return
}
func getSinkAlsaDevice(sink *pulse.Sink) (string, error) {
props := sink.PropList
card := props["alsa.card"]
device := props["alsa.device"]
if card == "" || device == "" {
return "", errors.New("failed to get sink ALSA property")
}
deviceStr := fmt.Sprintf("plughw:CARD=%s,DEV=%s", card, device)
subdevice := props["alsa.subdevice"]
if subdevice != "" {
deviceStr = deviceStr + ",SUBDEV=" + subdevice
}
return deviceStr, nil
}