1- // SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
1+ // SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
22//
33// SPDX-License-Identifier: GPL-3.0-or-later
44
@@ -11,6 +11,7 @@ import (
1111 "strings"
1212 "sync"
1313
14+ "github.com/linuxdeepin/go-lib/pulse"
1415 "github.com/linuxdeepin/go-lib/xdg/basedir"
1516)
1617
@@ -25,8 +26,10 @@ type PortConfig struct {
2526}
2627
2728type CardConfig struct {
28- Name string
29- Ports map [string ]* PortConfig // Name => PortConfig
29+ Name string
30+ Ports map [string ]* PortConfig // Name => PortConfig
31+ DefaultOutputPort string // 默认输出端口名称,手动设置时记录
32+ DefaultInputPort string // 默认输入端口名称,手动设置时记录
3033}
3134
3235type MuteConfig struct {
@@ -77,8 +80,10 @@ func NewMuteConfig() *MuteConfig {
7780
7881func NewCardConfig (name string ) * CardConfig {
7982 return & CardConfig {
80- Name : name ,
81- Ports : make (map [string ]* PortConfig ),
83+ Name : name ,
84+ Ports : make (map [string ]* PortConfig ),
85+ DefaultOutputPort : "" ,
86+ DefaultInputPort : "" ,
8287 }
8388}
8489
@@ -286,6 +291,47 @@ func (ck *ConfigKeeper) SetMuteAll(mute bool) {
286291 ck .Save ()
287292}
288293
294+ // SetDefaultPort 设置声卡的默认端口
295+ func (ck * ConfigKeeper ) SetDefaultPort (cardName string , portName string , direction int ) {
296+ ck .mu .Lock ()
297+ defer ck .mu .Unlock ()
298+
299+ cardConfig , ok := ck .Cards [cardName ]
300+ if ! ok {
301+ logger .Warningf ("card %s not found in config" , cardName )
302+ return
303+ }
304+
305+ if direction == pulse .DirectionSink {
306+ cardConfig .DefaultOutputPort = portName
307+ } else if direction == pulse .DirectionSource {
308+ cardConfig .DefaultInputPort = portName
309+ } else {
310+ logger .Warningf ("unexpected direction %d of port <%s:%s>" ,
311+ direction , cardName , portName )
312+ return
313+ }
314+ ck .Save ()
315+ }
316+
317+ // GetDefaultPort 获取声卡的默认端口
318+ func (ck * ConfigKeeper ) GetDefaultPort (cardName string , direction int ) string {
319+ ck .mu .Lock ()
320+ defer ck .mu .Unlock ()
321+
322+ cardConfig , ok := ck .Cards [cardName ]
323+ if ! ok {
324+ return ""
325+ }
326+
327+ if direction == pulse .DirectionSink {
328+ return cardConfig .DefaultOutputPort
329+ } else if direction == pulse .DirectionSource {
330+ return cardConfig .DefaultInputPort
331+ }
332+ return ""
333+ }
334+
289335func (card * CardConfig ) UpdatePortConfig (portConfig * PortConfig ) {
290336 card .Ports [portConfig .Name ] = portConfig
291337}
0 commit comments