From 31a1c27735af5cdc52d433eb9a9e5a44701d4f79 Mon Sep 17 00:00:00 2001 From: chenhongtao Date: Mon, 9 Oct 2023 15:18:59 +0800 Subject: [PATCH] feat: add pause player interface Log: (cherry picked from commit 52a7dccf60ab1f9269ae92ddafacee38a077c5f9) --- audio1/audio.go | 43 ++++++++++++++++--- audio1/audio_dbusutil.go | 13 ++++++ audio1/audio_events.go | 19 ++++++++ audio1/module.go | 10 +++++ .../org.deepin.dde.daemon.audio.json | 10 +++++ 5 files changed, 90 insertions(+), 5 deletions(-) diff --git a/audio1/audio.go b/audio1/audio.go index 010e7f477..2dcb77ba0 100644 --- a/audio1/audio.go +++ b/audio1/audio.go @@ -31,6 +31,7 @@ const ( gsKeyHeadphoneOutputVolume = "headphone-output-volume" gsKeyHeadphoneUnplugAutoPause = "headphone-unplug-auto-pause" gsKeyVolumeIncrease = "volume-increase" + gsKeyReduceNoise = "reduce-input-noise" gsKeyOutputAutoSwitchCountMax = "output-auto-switch-count-max" @@ -50,6 +51,7 @@ const ( increaseMaxVolume = 1.5 normalMaxVolume = 1.0 + dsgkeyPausePlayer = "pausePlayer" dsgKeyAutoSwitchPort = "autoSwitchPort" dsgKeyBluezModeFilterList = "bluezModeFilterList" dsgKeyPortFilterList = "portFilterList" @@ -105,6 +107,7 @@ type Audio struct { // dbusutil-gen: equal=objectPathSliceEqual Sinks []dbus.ObjectPath // dbusutil-gen: equal=objectPathSliceEqual + configManagerPath dbus.ObjectPath Sources []dbus.ObjectPath DefaultSink dbus.ObjectPath DefaultSource dbus.ObjectPath @@ -117,7 +120,10 @@ type Audio struct { // dbusutil-gen: ignore IncreaseVolume gsprop.Bool `prop:"access:rw"` - ReduceNoise bool `prop:"access:rw"` + PausePlayer bool `prop:"access:rw"` + + ReduceNoise bool `prop:"access:rw"` + defaultPaCfg defaultPaConfig // 最大音量 @@ -210,6 +216,7 @@ func newAudio(service *dbusutil.Service) *Audio { a.settings.Reset(gsKeyOutputVolume) a.settings.Reset(gsKeyHeadphoneOutputVolume) a.IncreaseVolume.Bind(a.settings, gsKeyVolumeIncrease) + a.PausePlayer = false a.ReduceNoise = false a.emitPropChangedReduceNoise(a.ReduceNoise) a.headphoneUnplugAutoPause = a.settings.GetBoolean(gsKeyHeadphoneUnplugAutoPause) @@ -506,6 +513,9 @@ func (a *Audio) refershSinkInputs() { } func (a *Audio) shouldAutoPause() bool { + if !a.PausePlayer { + return false + } if a.defaultSink == nil { logger.Warning("default sink is nil") return false @@ -1668,16 +1678,15 @@ func (a *Audio) initDsgProp() error { a.systemSigLoop = dbusutil.NewSignalLoop(systemBus, 10) - var configManagerPath dbus.ObjectPath systemConnObj := systemBus.Object("org.desktopspec.ConfigManager", "/") - err = systemConnObj.Call("org.desktopspec.ConfigManager.acquireManager", 0, "org.deepin.dde.daemon", "org.deepin.dde.daemon.audio", "").Store(&configManagerPath) + err = systemConnObj.Call("org.desktopspec.ConfigManager.acquireManager", 0, "org.deepin.dde.daemon", "org.deepin.dde.daemon.audio", "").Store(&a.configManagerPath) if err != nil { logger.Warning(err) return nil } err = dbusutil.NewMatchRuleBuilder().Type("signal"). - PathNamespace(string(configManagerPath)). + PathNamespace(string(a.configManagerPath)). Interface("org.desktopspec.ConfigManager.Manager"). Member("valueChanged").Build().AddTo(systemBus) if err != nil { @@ -1686,7 +1695,7 @@ func (a *Audio) initDsgProp() error { } var val bool - systemConnObj = systemBus.Object("org.desktopspec.ConfigManager", configManagerPath) + systemConnObj = systemBus.Object("org.desktopspec.ConfigManager", a.configManagerPath) err = systemConnObj.Call("org.desktopspec.ConfigManager.Manager.value", 0, dsgKeyAutoSwitchPort).Store(&val) if err != nil { logger.Warning(err) @@ -1695,6 +1704,17 @@ func (a *Audio) initDsgProp() error { a.setEnableAutoSwitchPort(val) } + var keyPausePlayer bool + err = systemConnObj.Call("org.desktopspec.ConfigManager.Manager.value", 0, dsgkeyPausePlayer).Store(&keyPausePlayer) + if err != nil { + logger.Warning(err) + } else { + logger.Info("auto switch port:", keyPausePlayer) + a.PropsMu.Lock() + a.PausePlayer = keyPausePlayer + a.PropsMu.Unlock() + } + var ret []dbus.Variant err = systemConnObj.Call("org.desktopspec.ConfigManager.Manager.value", 0, dsgKeyBluezModeFilterList).Store(&ret) if err != nil { @@ -1792,6 +1812,19 @@ func (a *Audio) initDsgProp() error { } } + if ok && key == dsgkeyPausePlayer { + var pausePlayer bool + err = systemConnObj.Call("org.desktopspec.ConfigManager.Manager.value", 0, key).Store(&pausePlayer) + if err != nil { + logger.Warning(err) + } else { + logger.Info("pausePlayer config:", pausePlayer) + a.PropsMu.Lock() + a.PausePlayer = pausePlayer + a.emitPropChangedPausePlayer(pausePlayer) + a.PropsMu.Unlock() + } + } } }) diff --git a/audio1/audio_dbusutil.go b/audio1/audio_dbusutil.go index 43a62d3f8..b13ffeab5 100644 --- a/audio1/audio_dbusutil.go +++ b/audio1/audio_dbusutil.go @@ -123,6 +123,19 @@ func (v *Audio) emitPropChangedBluetoothAudioModeOpts(value []string) error { return v.service.EmitPropertyChanged(v, "BluetoothAudioModeOpts", value) } +func (v *Audio) setPropPausePlayer(value bool) (changed bool) { + if v.PausePlayer != value { + v.PausePlayer = value + v.emitPropChangedPausePlayer(value) + return true + } + return false +} + +func (v *Audio) emitPropChangedPausePlayer(value bool) error { + return v.service.EmitPropertyChanged(v, "PausePlayer", value) +} + func (v *Audio) setPropReduceNoise(value bool) (changed bool) { if v.ReduceNoise != value { v.ReduceNoise = value diff --git a/audio1/audio_events.go b/audio1/audio_events.go index beaa1c0ff..db6f20083 100644 --- a/audio1/audio_events.go +++ b/audio1/audio_events.go @@ -606,6 +606,25 @@ func (a *Audio) writeReduceNoise(write *dbusutil.PropertyWrite) *dbus.Error { return nil } +func (a *Audio) writeKeyPausePlayer(write *dbusutil.PropertyWrite) *dbus.Error { + pausePlayer, ok := write.Value.(bool) + if !ok { + return dbusutil.ToError(errors.New("type is not bool")) + } + systemBus, err := dbus.SystemBus() + if err != nil { + return dbus.MakeFailedError(err) + } + + systemConnObj := systemBus.Object("org.desktopspec.ConfigManager", a.configManagerPath) + err = systemConnObj.Call("org.desktopspec.ConfigManager.Manager.setValue", 0, dsgkeyPausePlayer, dbus.MakeVariant(pausePlayer)).Err + if err != nil { + return dbusutil.ToError(errors.New("dconfig Cannot set value " + dsgkeyPausePlayer)) + } + a.setPropPausePlayer(pausePlayer) + return nil +} + func (a *Audio) notifyBluezCardPortInsert(card *Card) { logger.Debugf("notify bluez card %d:%s", card.Id, card.core.Name) oldCard, err := a.oldCards.getByName(card.core.Name) diff --git a/audio1/module.go b/audio1/module.go index 142340055..7547276be 100644 --- a/audio1/module.go +++ b/audio1/module.go @@ -64,6 +64,16 @@ func (m *Module) start() error { so := service.GetServerObject(m.audio) err = so.SetWriteCallback(m.audio, "ReduceNoise", m.audio.writeReduceNoise) + if err != nil { + logger.Warning("failed to bind callback for ReduceNoise:", err) + } + + err = so.SetWriteCallback(m.audio, "PausePlayer", m.audio.writeKeyPausePlayer) + + if err != nil { + logger.Warning("failed to bind callback for PausePlayer:", err) + } + err = m.audio.syncConfig.Register() if err != nil { logger.Warning("failed to register for deepin sync:", err) diff --git a/misc/dsg-configs/org.deepin.dde.daemon.audio.json b/misc/dsg-configs/org.deepin.dde.daemon.audio.json index 870de8152..d8d0e837d 100644 --- a/misc/dsg-configs/org.deepin.dde.daemon.audio.json +++ b/misc/dsg-configs/org.deepin.dde.daemon.audio.json @@ -2,6 +2,16 @@ "magic": "dsg.config.meta", "version": "1.0", "contents": { + "pausePlayer": { + "value": false, + "serial":0, + "flags" : [], + "name": "PlayerPause", + "name[zh_CN]": "插拔声音设备暂停输出", + "description": "auto pause player", + "permissions": "readwrite", + "visibility": "private" + }, "autoSwitchPort": { "value": true, "serial": 0,