Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
kdeconnect-kde
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Weixuan Xiao
kdeconnect-kde
Commits
2be37aca
Commit
2be37aca
authored
Jul 30, 2019
by
Weixuan Xiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve code style in macOS system volume plugin
parent
5fadeabc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
59 deletions
+77
-59
plugins/systemvolume/systemvolumeplugin-macos.cpp
plugins/systemvolume/systemvolumeplugin-macos.cpp
+76
-58
plugins/systemvolume/systemvolumeplugin-macos.h
plugins/systemvolume/systemvolumeplugin-macos.h
+1
-1
No files found.
plugins/systemvolume/systemvolumeplugin-macos.cpp
View file @
2be37aca
...
...
@@ -35,9 +35,9 @@ Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SYSTEMVOLUME, "kdeconnect.plugin.systemvolu
class
MacOSCoreAudioDevice
{
private:
AudioDeviceID
deviceId
;
QString
description
;
bool
isStereo
;
AudioDeviceID
m_
deviceId
;
QString
m_
description
;
bool
m_
isStereo
;
friend
class
SystemvolumePlugin
;
public:
...
...
@@ -55,54 +55,73 @@ public:
static
const
AudioObjectPropertyAddress
kAudioHardwarePropertyAddress
=
{
kAudioHardwarePropertyDevices
,
kAudioObjectPropertyScopeGlobal
,
kAudioObjectPropertyElementMaster
};
kAudioObjectPropertyElementMaster
};
static
const
AudioObjectPropertyAddress
kAudioStreamPropertyAddress
=
{
kAudioDevicePropertyStreams
,
kAudioDevicePropertyScopeOutput
,
kAudioObjectPropertyElementMaster
};
kAudioObjectPropertyElementMaster
};
static
const
AudioObjectPropertyAddress
kAudioMasterVolumePropertyAddress
=
{
kAudioDevicePropertyVolumeScalar
,
kAudioDevicePropertyScopeOutput
,
kAudioObjectPropertyElementMaster
};
kAudioObjectPropertyElementMaster
};
static
const
AudioObjectPropertyAddress
kAudioLeftVolumePropertyAddress
=
{
kAudioDevicePropertyVolumeScalar
,
kAudioDevicePropertyScopeOutput
,
1
};
1
};
static
const
AudioObjectPropertyAddress
kAudioRightVolumePropertyAddress
=
{
kAudioDevicePropertyVolumeScalar
,
kAudioDevicePropertyScopeOutput
,
2
};
2
};
static
const
AudioObjectPropertyAddress
kAudioMasterMutedPropertyAddress
=
{
kAudioDevicePropertyMute
,
kAudioDevicePropertyScopeOutput
,
kAudioObjectPropertyElementMaster
};
kAudioObjectPropertyElementMaster
};
static
const
AudioObjectPropertyAddress
kAudioMasterDataSourcePropertyAddress
=
{
kAudioDevicePropertyDataSource
,
kAudioDevicePropertyScopeOutput
,
kAudioObjectPropertyElementMaster
};
kAudioDevicePropertyDataSource
,
kAudioDevicePropertyScopeOutput
,
kAudioObjectPropertyElementMaster
};
OSStatus
onVolumeChanged
(
AudioObjectID
object
,
UInt32
num
_a
ddresses
,
const
AudioObjectPropertyAddress
addresses
[],
void
*
context
)
OSStatus
onVolumeChanged
(
AudioObjectID
object
,
UInt32
num
A
ddresses
,
const
AudioObjectPropertyAddress
addresses
[],
void
*
context
)
{
Q_UNUSED
(
object
);
Q_UNUSED
(
addresses
);
Q_UNUSED
(
numAddresses
);
SystemvolumePlugin
*
plugin
=
(
SystemvolumePlugin
*
)
context
;
plugin
->
updateDeviceVolume
(
object
);
return
noErr
;
}
OSStatus
onMutedChanged
(
AudioObjectID
object
,
UInt32
num
_a
ddresses
,
const
AudioObjectPropertyAddress
addresses
[],
void
*
context
)
OSStatus
onMutedChanged
(
AudioObjectID
object
,
UInt32
num
A
ddresses
,
const
AudioObjectPropertyAddress
addresses
[],
void
*
context
)
{
Q_UNUSED
(
object
);
Q_UNUSED
(
addresses
);
Q_UNUSED
(
numAddresses
);
SystemvolumePlugin
*
plugin
=
(
SystemvolumePlugin
*
)
context
;
plugin
->
updateDeviceMuted
(
object
);
return
noErr
;
}
OSStatus
onOutputSourceChanged
(
AudioObjectID
object
,
UInt32
num
_a
ddresses
,
const
AudioObjectPropertyAddress
addresses
[],
void
*
context
)
OSStatus
onOutputSourceChanged
(
AudioObjectID
object
,
UInt32
num
A
ddresses
,
const
AudioObjectPropertyAddress
addresses
[],
void
*
context
)
{
Q_UNUSED
(
object
);
Q_UNUSED
(
addresses
);
Q_UNUSED
(
numAddresses
);
SystemvolumePlugin
*
plugin
=
(
SystemvolumePlugin
*
)
context
;
plugin
->
sendSinkList
();
return
noErr
;
...
...
@@ -169,8 +188,8 @@ std::vector<AudioObjectID> GetAllOutputAudioDeviceIDs() {
if
(
size
==
0
)
return
{};
size_t
device
_c
ount
=
size
/
sizeof
(
AudioObjectID
);
std
::
vector
<
AudioObjectID
>
deviceIds
(
device
_c
ount
);
size_t
device
C
ount
=
size
/
sizeof
(
AudioObjectID
);
std
::
vector
<
AudioObjectID
>
deviceIds
(
device
C
ount
);
result
=
AudioObjectGetPropertyData
(
kAudioObjectSystemObject
,
&
kAudioHardwarePropertyAddress
,
0
,
NULL
,
&
size
,
deviceIds
.
data
());
if
(
result
!=
noErr
)
{
qCDebug
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
...
...
@@ -195,8 +214,7 @@ std::vector<AudioObjectID> GetAllOutputAudioDeviceIDs() {
}
SystemvolumePlugin
::
SystemvolumePlugin
(
QObject
*
parent
,
const
QVariantList
&
args
)
:
KdeConnectPlugin
(
parent
,
args
)
,
sinksMap
()
:
KdeConnectPlugin
(
parent
,
args
),
m_sinksMap
()
{}
bool
SystemvolumePlugin
::
receivePacket
(
const
NetworkPacket
&
np
)
...
...
@@ -206,12 +224,12 @@ bool SystemvolumePlugin::receivePacket(const NetworkPacket& np)
}
else
{
QString
name
=
np
.
get
<
QString
>
(
QStringLiteral
(
"name"
));
if
(
sinksMap
.
contains
(
name
))
{
if
(
m_
sinksMap
.
contains
(
name
))
{
if
(
np
.
has
(
QStringLiteral
(
"volume"
)))
{
sinksMap
[
name
]
->
setVolume
(
np
.
get
<
int
>
(
QStringLiteral
(
"volume"
))
/
100.0
);
m_
sinksMap
[
name
]
->
setVolume
(
np
.
get
<
int
>
(
QStringLiteral
(
"volume"
))
/
100.0
);
}
if
(
np
.
has
(
QStringLiteral
(
"muted"
)))
{
sinksMap
[
name
]
->
setMuted
(
np
.
get
<
bool
>
(
QStringLiteral
(
"muted"
)));
m_
sinksMap
[
name
]
->
setMuted
(
np
.
get
<
bool
>
(
QStringLiteral
(
"muted"
)));
}
}
}
...
...
@@ -224,11 +242,11 @@ void SystemvolumePlugin::sendSinkList()
QJsonDocument
document
;
QJsonArray
array
;
if
(
!
sinksMap
.
empty
())
{
for
(
MacOSCoreAudioDevice
*
sink
:
sinksMap
)
{
if
(
!
m_
sinksMap
.
empty
())
{
for
(
MacOSCoreAudioDevice
*
sink
:
m_
sinksMap
)
{
delete
sink
;
}
sinksMap
.
clear
();
m_
sinksMap
.
clear
();
}
std
::
vector
<
AudioObjectID
>
deviceIds
=
GetAllOutputAudioDeviceIDs
();
...
...
@@ -236,9 +254,9 @@ void SystemvolumePlugin::sendSinkList()
for
(
AudioDeviceID
deviceId
:
deviceIds
)
{
MacOSCoreAudioDevice
*
audioDevice
=
new
MacOSCoreAudioDevice
(
deviceId
);
audioDevice
->
description
=
translateDeviceSource
(
deviceId
);
audioDevice
->
m_
description
=
translateDeviceSource
(
deviceId
);
sinksMap
.
insert
(
QStringLiteral
(
"default-"
)
+
QString
::
number
(
deviceId
),
audioDevice
);
m_
sinksMap
.
insert
(
QStringLiteral
(
"default-"
)
+
QString
::
number
(
deviceId
),
audioDevice
);
// Add volume change listener
AudioObjectAddPropertyListener
(
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
&
onVolumeChanged
,
(
void
*
)
this
);
...
...
@@ -255,7 +273,7 @@ void SystemvolumePlugin::sendSinkList()
QJsonObject
sinkObject
{
{
QStringLiteral
(
"name"
),
QStringLiteral
(
"default-"
)
+
QString
::
number
(
deviceId
)},
{
QStringLiteral
(
"muted"
),
audioDevice
->
isMuted
()},
{
QStringLiteral
(
"description"
),
audioDevice
->
description
},
{
QStringLiteral
(
"description"
),
audioDevice
->
m_
description
},
{
QStringLiteral
(
"volume"
),
audioDevice
->
volume
()
*
100
},
{
QStringLiteral
(
"maxVolume"
),
100
}
};
...
...
@@ -277,8 +295,8 @@ void SystemvolumePlugin::connected()
void
SystemvolumePlugin
::
updateDeviceMuted
(
AudioDeviceID
deviceId
)
{
for
(
MacOSCoreAudioDevice
*
sink
:
sinksMap
)
{
if
(
sink
->
deviceId
==
deviceId
)
{
for
(
MacOSCoreAudioDevice
*
sink
:
m_
sinksMap
)
{
if
(
sink
->
m_
deviceId
==
deviceId
)
{
NetworkPacket
np
(
PACKET_TYPE_SYSTEMVOLUME
);
np
.
set
<
bool
>
(
QStringLiteral
(
"muted"
),
(
bool
)(
sink
->
isMuted
()));
np
.
set
<
int
>
(
QStringLiteral
(
"volume"
),
(
int
)(
sink
->
volume
()
*
100
));
...
...
@@ -292,8 +310,8 @@ void SystemvolumePlugin::updateDeviceMuted(AudioDeviceID deviceId)
void
SystemvolumePlugin
::
updateDeviceVolume
(
AudioDeviceID
deviceId
)
{
for
(
MacOSCoreAudioDevice
*
sink
:
sinksMap
)
{
if
(
sink
->
deviceId
==
deviceId
)
{
for
(
MacOSCoreAudioDevice
*
sink
:
m_
sinksMap
)
{
if
(
sink
->
m_
deviceId
==
deviceId
)
{
NetworkPacket
np
(
PACKET_TYPE_SYSTEMVOLUME
);
np
.
set
<
int
>
(
QStringLiteral
(
"volume"
),
(
int
)(
sink
->
volume
()
*
100
));
np
.
set
<
QString
>
(
QStringLiteral
(
"name"
),
QStringLiteral
(
"default-"
)
+
QString
::
number
(
deviceId
));
...
...
@@ -304,8 +322,8 @@ void SystemvolumePlugin::updateDeviceVolume(AudioDeviceID deviceId)
qCDebug
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Device"
<<
deviceId
<<
"not found while update volume"
;
}
MacOSCoreAudioDevice
::
MacOSCoreAudioDevice
(
AudioDeviceID
_
deviceId
)
:
deviceId
(
_
deviceId
)
MacOSCoreAudioDevice
::
MacOSCoreAudioDevice
(
AudioDeviceID
deviceId
)
:
m_deviceId
(
deviceId
)
{
updateType
();
}
...
...
@@ -313,19 +331,19 @@ MacOSCoreAudioDevice::MacOSCoreAudioDevice(AudioDeviceID _deviceId)
MacOSCoreAudioDevice
::~
MacOSCoreAudioDevice
()
{
// Volume listener
AudioObjectRemovePropertyListener
(
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
AudioObjectRemovePropertyListener
(
m_
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
&
onVolumeChanged
,
(
void
*
)
this
);
AudioObjectRemovePropertyListener
(
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
AudioObjectRemovePropertyListener
(
m_
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
&
onVolumeChanged
,
(
void
*
)
this
);
AudioObjectRemovePropertyListener
(
deviceId
,
&
kAudioRightVolumePropertyAddress
,
AudioObjectRemovePropertyListener
(
m_
deviceId
,
&
kAudioRightVolumePropertyAddress
,
&
onVolumeChanged
,
(
void
*
)
this
);
// Muted listener
AudioObjectRemovePropertyListener
(
deviceId
,
&
kAudioMasterMutedPropertyAddress
,
AudioObjectRemovePropertyListener
(
m_
deviceId
,
&
kAudioMasterMutedPropertyAddress
,
&
onMutedChanged
,
(
void
*
)
this
);
// Data source listener
AudioObjectRemovePropertyListener
(
deviceId
,
&
kAudioMasterDataSourcePropertyAddress
,
AudioObjectRemovePropertyListener
(
m_
deviceId
,
&
kAudioMasterDataSourcePropertyAddress
,
&
onOutputSourceChanged
,
(
void
*
)
this
);
}
...
...
@@ -333,36 +351,36 @@ void MacOSCoreAudioDevice::setVolume(float volume)
{
OSStatus
result
;
if
(
deviceId
==
kAudioObjectUnknown
)
{
if
(
m_
deviceId
==
kAudioObjectUnknown
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to set volume of Unknown Device"
;
return
;
}
if
(
isStereo
)
{
result
=
AudioObjectSetPropertyData
(
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
0
,
NULL
,
sizeof
(
volume
),
&
volume
);
result
=
AudioObjectSetPropertyData
(
deviceId
,
&
kAudioRightVolumePropertyAddress
,
0
,
NULL
,
sizeof
(
volume
),
&
volume
);
if
(
m_
isStereo
)
{
result
=
AudioObjectSetPropertyData
(
m_
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
0
,
NULL
,
sizeof
(
volume
),
&
volume
);
result
=
AudioObjectSetPropertyData
(
m_
deviceId
,
&
kAudioRightVolumePropertyAddress
,
0
,
NULL
,
sizeof
(
volume
),
&
volume
);
}
else
{
result
=
AudioObjectSetPropertyData
(
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
0
,
NULL
,
sizeof
(
volume
),
&
volume
);
result
=
AudioObjectSetPropertyData
(
m_
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
0
,
NULL
,
sizeof
(
volume
),
&
volume
);
}
if
(
result
!=
noErr
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to set volume of Device"
<<
deviceId
<<
"to"
<<
volume
;
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to set volume of Device"
<<
m_
deviceId
<<
"to"
<<
volume
;
}
}
void
MacOSCoreAudioDevice
::
setMuted
(
bool
muted
)
{
if
(
deviceId
==
kAudioObjectUnknown
)
{
if
(
m_
deviceId
==
kAudioObjectUnknown
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to mute an Unknown Device"
;
return
;
}
UInt32
mutedValue
=
muted
?
1
:
0
;
OSStatus
result
=
AudioObjectSetPropertyData
(
deviceId
,
&
kAudioMasterMutedPropertyAddress
,
0
,
NULL
,
sizeof
(
mutedValue
),
&
mutedValue
);
OSStatus
result
=
AudioObjectSetPropertyData
(
m_
deviceId
,
&
kAudioMasterMutedPropertyAddress
,
0
,
NULL
,
sizeof
(
mutedValue
),
&
mutedValue
);
if
(
result
!=
noErr
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to set muted state of Device"
<<
deviceId
<<
"to"
<<
muted
;
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to set muted state of Device"
<<
m_
deviceId
<<
"to"
<<
muted
;
}
}
...
...
@@ -370,7 +388,7 @@ float MacOSCoreAudioDevice::volume()
{
OSStatus
result
;
if
(
deviceId
==
kAudioObjectUnknown
)
{
if
(
m_
deviceId
==
kAudioObjectUnknown
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to get volume of Unknown Device"
;
return
0.0
;
}
...
...
@@ -378,16 +396,16 @@ float MacOSCoreAudioDevice::volume()
float
volume
=
0.0
;
UInt32
volumeDataSize
=
sizeof
(
volume
);
if
(
isStereo
)
{
if
(
m_
isStereo
)
{
// Try to get steoreo device volume
result
=
AudioObjectGetPropertyData
(
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
0
,
NULL
,
&
volumeDataSize
,
&
volume
);
result
=
AudioObjectGetPropertyData
(
m_
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
0
,
NULL
,
&
volumeDataSize
,
&
volume
);
}
else
{
// Try to get master volume
result
=
AudioObjectGetPropertyData
(
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
0
,
NULL
,
&
volumeDataSize
,
&
volume
);
result
=
AudioObjectGetPropertyData
(
m_
deviceId
,
&
kAudioMasterVolumePropertyAddress
,
0
,
NULL
,
&
volumeDataSize
,
&
volume
);
}
if
(
result
!=
noErr
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to get volume of Device"
<<
deviceId
;
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to get volume of Device"
<<
m_
deviceId
;
return
0.0
;
}
...
...
@@ -396,7 +414,7 @@ float MacOSCoreAudioDevice::volume()
bool
MacOSCoreAudioDevice
::
isMuted
()
{
if
(
deviceId
==
kAudioObjectUnknown
)
{
if
(
m_
deviceId
==
kAudioObjectUnknown
)
{
qWarning
(
KDECONNECT_PLUGIN_SYSTEMVOLUME
)
<<
"Unable to get muted state of an Unknown Device"
;
return
false
;
}
...
...
@@ -404,7 +422,7 @@ bool MacOSCoreAudioDevice::isMuted()
UInt32
muted
=
0
;
UInt32
muteddataSize
=
sizeof
(
muted
);
AudioObjectGetPropertyData
(
deviceId
,
&
kAudioMasterMutedPropertyAddress
,
0
,
NULL
,
&
muteddataSize
,
&
muted
);
AudioObjectGetPropertyData
(
m_
deviceId
,
&
kAudioMasterMutedPropertyAddress
,
0
,
NULL
,
&
muteddataSize
,
&
muted
);
return
muted
==
1
;
}
...
...
@@ -414,11 +432,11 @@ void MacOSCoreAudioDevice::updateType()
// Try to get volume from left channel to check if it's a stereo device
float
volume
=
0.0
;
UInt32
volumeDataSize
=
sizeof
(
volume
);
OSStatus
result
=
AudioObjectGetPropertyData
(
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
0
,
NULL
,
&
volumeDataSize
,
&
volume
);
OSStatus
result
=
AudioObjectGetPropertyData
(
m_
deviceId
,
&
kAudioLeftVolumePropertyAddress
,
0
,
NULL
,
&
volumeDataSize
,
&
volume
);
if
(
result
==
noErr
)
{
isStereo
=
true
;
m_
isStereo
=
true
;
}
else
{
isStereo
=
false
;
m_
isStereo
=
false
;
}
}
...
...
plugins/systemvolume/systemvolumeplugin-macos.h
View file @
2be37aca
...
...
@@ -46,7 +46,7 @@ public:
void
updateDeviceVolume
(
AudioDeviceID
deviceId
);
void
updateDeviceMuted
(
AudioDeviceID
deviceId
);
private:
QMap
<
QString
,
MacOSCoreAudioDevice
*>
sinksMap
;
QMap
<
QString
,
MacOSCoreAudioDevice
*>
m_
sinksMap
;
};
#endif // SYSTEMVOLUMEPLUGINMACOS_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment