@@ -38,50 +38,89 @@ void MediaController::initializeMprisInterface() {
3838 }
3939}
4040
41- void MediaController::handleEarDetection (const QString &status) {
41+ void MediaController::handleEarDetection (const QString &status)
42+ {
43+ if (earDetectionBehavior == Disabled)
44+ {
45+ LOG_DEBUG (" Ear detection is disabled, ignoring status" );
46+ return ;
47+ }
48+
4249 bool primaryInEar = false ;
4350 bool secondaryInEar = false ;
4451
4552 QStringList parts = status.split (" , " );
46- if (parts.size () == 2 ) {
53+ if (parts.size () == 2 )
54+ {
4755 primaryInEar = parts[0 ].contains (" In Ear" );
4856 secondaryInEar = parts[1 ].contains (" In Ear" );
4957 }
5058
5159 LOG_DEBUG (" Ear detection status: primaryInEar="
5260 << primaryInEar << " , secondaryInEar=" << secondaryInEar
5361 << " , isAirPodsActive=" << isActiveOutputDeviceAirPods ());
54- if (primaryInEar || secondaryInEar) {
62+
63+ // First handle playback pausing based on selected behavior
64+ bool shouldPause = false ;
65+ bool shouldResume = false ;
66+
67+ if (earDetectionBehavior == PauseWhenOneRemoved)
68+ {
69+ shouldPause = !primaryInEar || !secondaryInEar;
70+ shouldResume = primaryInEar && secondaryInEar;
71+ }
72+ else if (earDetectionBehavior == PauseWhenBothRemoved)
73+ {
74+ shouldPause = !primaryInEar && !secondaryInEar;
75+ shouldResume = primaryInEar || secondaryInEar;
76+ }
77+
78+ if (shouldPause && isActiveOutputDeviceAirPods ())
79+ {
80+ QProcess process;
81+ process.start (" playerctl" , QStringList () << " status" );
82+ process.waitForFinished ();
83+ QString playbackStatus = process.readAllStandardOutput ().trimmed ();
84+ LOG_DEBUG (" Playback status: " << playbackStatus);
85+ if (playbackStatus == " Playing" )
86+ {
87+ pause ();
88+ }
89+ }
90+
91+ // Then handle device profile switching
92+ if (primaryInEar || secondaryInEar)
93+ {
5594 LOG_INFO (" At least one AirPod is in ear" );
5695 activateA2dpProfile ();
57- } else {
58- LOG_INFO (" Both AirPods are out of ear" );
59- removeAudioOutputDevice ();
60- }
6196
62- if (primaryInEar && secondaryInEar) {
63- if (wasPausedByApp && isActiveOutputDeviceAirPods ()) {
97+ // Resume if conditions are met and we previously paused
98+ if (shouldResume && wasPausedByApp && isActiveOutputDeviceAirPods ())
99+ {
64100 int result = QProcess::execute (" playerctl" , QStringList () << " play" );
65101 LOG_DEBUG (" Executed 'playerctl play' with result: " << result);
66- if (result == 0 ) {
102+ if (result == 0 )
103+ {
67104 LOG_INFO (" Resumed playback via Playerctl" );
68105 wasPausedByApp = false ;
69- } else {
70- LOG_ERROR (" Failed to resume playback via Playerctl" );
71106 }
72- }
73- } else {
74- if (isActiveOutputDeviceAirPods ()) {
75- QProcess process;
76- process.start (" playerctl" , QStringList () << " status" );
77- process.waitForFinished ();
78- QString playbackStatus = process.readAllStandardOutput ().trimmed ();
79- LOG_DEBUG (" Playback status: " << playbackStatus);
80- if (playbackStatus == " Playing" ) {
81- pause ();
107+ else
108+ {
109+ LOG_ERROR (" Failed to resume playback via Playerctl" );
82110 }
83111 }
84112 }
113+ else
114+ {
115+ LOG_INFO (" Both AirPods are out of ear" );
116+ removeAudioOutputDevice ();
117+ }
118+ }
119+
120+ void MediaController::setEarDetectionBehavior (EarDetectionBehavior behavior)
121+ {
122+ earDetectionBehavior = behavior;
123+ LOG_INFO (" Set ear detection behavior to: " << behavior);
85124}
86125
87126void MediaController::followMediaChanges () {
0 commit comments