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
@@ -18,6 +18,7 @@ import (
1818 ofdbus "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.dbus"
1919 login1 "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.login1"
2020 "github.com/linuxdeepin/go-lib/dbusutil"
21+ "github.com/linuxdeepin/go-lib/strv"
2122)
2223
2324const (
@@ -38,18 +39,23 @@ const (
3839)
3940
4041const (
41- devIconComputer = "computer"
42- devIconPhone = "phone"
43- devIconModem = "modem"
44- devIconNetworkWireless = "network-wireless"
45- devIconAudioCard = "audio-card"
46- devIconCameraVideo = "camera-video"
47- devIconCameraPhoto = "camera-photo"
48- devIconPrinter = "printer"
49- devIconInputGaming = "input-gaming"
50- devIconInputKeyboard = "input-keyboard"
51- devIconInputTablet = "input-tablet"
52- devIconInputMouse = "input-mouse"
42+ devIconComputer = "computer"
43+ devIconPhone = "phone"
44+ devIconModem = "modem"
45+ devIconNetworkWireless = "network-wireless"
46+ devIconAudioCard = "audio-card"
47+ devIconCameraVideo = "camera-video"
48+ devIconCameraPhoto = "camera-photo"
49+ devIconPrinter = "printer"
50+ devIconInputGaming = "input-gaming"
51+ devIconInputKeyboard = "input-keyboard"
52+ devIconInputTablet = "input-tablet"
53+ devIconInputMouse = "input-mouse"
54+ devIconAudioHeadset = "audio-headset"
55+ devIconAudioHeadPhones = "audio-headphones"
56+ devIconVideoDisplay = "video-display"
57+ devIconMultimediaPlayer = "multimedia-player"
58+ devIconScanner = "scanner"
5359)
5460
5561const (
@@ -1013,15 +1019,11 @@ func (b *SysBluetooth) autoConnectPairedDevice(devPath dbus.ObjectPath, adapterP
10131019 return nil
10141020 }
10151021
1016- switch device .Icon {
1017- // 只自动连接一个这些图标的设备
1018- case devIconAudioCard , devIconInputKeyboard , devIconInputMouse , devIconInputTablet :
1019- connectedDevice := b .findFirstConnectedDeviceByIcon (device .Icon )
1020- if connectedDevice != nil {
1021- logger .Debugf ("there is already a connected %v, icon: %v, do not auto connect it: %v" ,
1022- connectedDevice , device .Icon , device )
1023- return nil
1024- }
1022+ connectedDevice := b .getConflictingConnectedDevice (device .Icon )
1023+ if connectedDevice != nil {
1024+ logger .Debugf ("there is already a connected %v, icon: %v, do not auto connect it: %v" ,
1025+ connectedDevice , device .Icon , device )
1026+ return nil
10251027 }
10261028
10271029 logger .Debug ("auto connect paired" , device )
@@ -1126,20 +1128,39 @@ func (b *SysBluetooth) getConnectedDeviceByAddress(address string) *device {
11261128 return nil
11271129}
11281130
1129- func (b * SysBluetooth ) findFirstConnectedDeviceByIcon (icon string ) * device {
1131+ func (b * SysBluetooth ) findFirstConnectedDeviceByIcons (icon strv. Strv ) * device {
11301132 b .connectedMu .Lock ()
11311133 defer b .connectedMu .Unlock ()
11321134
11331135 for _ , devices := range b .connectedDevices {
11341136 for _ , dev := range devices {
1135- if dev .Icon == icon {
1137+ if icon . Contains ( dev .Icon ) {
11361138 return dev
11371139 }
11381140 }
11391141 }
11401142 return nil
11411143}
11421144
1145+ // getConflictingConnectedDevice 检查是否已有冲突的已连接设备
1146+ // 对于输入设备(键盘、鼠标、平板),只允许连接一个同类型设备
1147+ // 对于音频设备,只允许连接一个音频设备
1148+ // getConflictingConnectedDevice 检查是否已有冲突的已连接设备
1149+ // 对于输入设备(键盘、鼠标、平板),只允许连接一个同类型设备
1150+ // 对于音频设备,只允许连接一个音频设备
1151+ // 返回已连接的冲突设备,如果没有冲突则返回nil
1152+ func (b * SysBluetooth ) getConflictingConnectedDevice (deviceIcon string ) * device {
1153+ switch deviceIcon {
1154+ // 只自动连接一个这些图标的设备
1155+ case devIconInputKeyboard , devIconInputMouse , devIconInputTablet :
1156+ return b .findFirstConnectedDeviceByIcons (strv.Strv {deviceIcon })
1157+ // 这些可能都是音频设备,只连接一个
1158+ case devIconAudioCard , devIconAudioHeadset , devIconAudioHeadPhones :
1159+ return b .findFirstConnectedDeviceByIcons (strv.Strv {devIconAudioCard , devIconAudioHeadset , devIconAudioHeadPhones })
1160+ }
1161+ return nil
1162+ }
1163+
11431164func (b * SysBluetooth ) setAutoConnectFinishedStatus (adapterPath dbus.ObjectPath , status bool ) {
11441165 adapter , err := b .getAdapter (adapterPath )
11451166 if err != nil {
0 commit comments