Toggle audio sink/source on icon click

A click on the mic/speaker icon now mutes/unmutes the sink/source.

Implements https://github.com/scorpion-26/gBar/issues/81
This commit is contained in:
scorpion-26 2024-03-26 22:09:38 +01:00
parent 2974233a5e
commit c9bc79c956
4 changed files with 57 additions and 4 deletions

View file

@ -286,8 +286,8 @@ namespace Bar
Widget* audioSlider; Widget* audioSlider;
Widget* micSlider; Widget* micSlider;
Text* audioIcon; Button* audioIcon;
Text* micIcon; Button* micIcon;
void OnChangeVolumeSink(Slider&, double value) void OnChangeVolumeSink(Slider&, double value)
{ {
System::SetVolumeSink(value); System::SetVolumeSink(value);
@ -315,6 +315,18 @@ namespace Bar
System::SetVolumeSource(micVolume); System::SetVolumeSource(micVolume);
} }
void OnToggleSink(Button& button)
{
System::AudioInfo info = System::GetAudioInfo();
System::SetMutedSink(!info.sinkMuted);
}
void OnToggleSource(Button& button)
{
System::AudioInfo info = System::GetAudioInfo();
System::SetMutedSource(!info.sourceMuted);
}
TimerResult UpdateAudio(Widget&) TimerResult UpdateAudio(Widget&)
{ {
System::AudioInfo info = System::GetAudioInfo(); System::AudioInfo info = System::GetAudioInfo();
@ -344,7 +356,7 @@ namespace Bar
} }
else else
{ {
((Slider*)micSlider)->SetValue(info.sinkVolume); ((Slider*)micSlider)->SetValue(info.sourceVolume);
} }
if (info.sourceMuted) if (info.sourceMuted)
{ {
@ -605,18 +617,20 @@ namespace Bar
Utils::SetTransform(*box, {-1, false, SideToAlignment(side)}); Utils::SetTransform(*box, {-1, false, SideToAlignment(side)});
box->SetOrientation(Utils::GetOrientation()); box->SetOrientation(Utils::GetOrientation());
{ {
auto icon = Widget::Create<Text>(); auto icon = Widget::Create<Button>();
icon->SetAngle(Utils::GetAngle()); icon->SetAngle(Utils::GetAngle());
switch (type) switch (type)
{ {
case AudioType::Input: case AudioType::Input:
icon->SetClass("mic-icon"); icon->SetClass("mic-icon");
icon->SetText(Config::Get().speakerHighIcon); icon->SetText(Config::Get().speakerHighIcon);
icon->OnClick(DynCtx::OnToggleSource);
DynCtx::micIcon = icon.get(); DynCtx::micIcon = icon.get();
break; break;
case AudioType::Output: case AudioType::Output:
icon->SetClass("audio-icon"); icon->SetClass("audio-icon");
icon->SetText(Config::Get().micHighIcon); icon->SetText(Config::Get().micHighIcon);
icon->OnClick(DynCtx::OnToggleSink);
if (!RotatedIcons()) if (!RotatedIcons())
Utils::SetTransform(*icon, {-1, true, Alignment::Fill, 0, 6}); Utils::SetTransform(*icon, {-1, true, Alignment::Fill, 0, 6});
DynCtx::audioIcon = icon.get(); DynCtx::audioIcon = icon.get();

View file

@ -234,6 +234,34 @@ namespace PulseAudio
system(cmd.c_str()); system(cmd.c_str());
} }
inline void SetMutedSink(bool muted)
{
if (muted)
{
LOG("Audio: Mute sink");
system("pamixer --mute");
}
else
{
LOG("Audio: Unmute sink");
system("pamixer --unmute");
}
}
inline void SetMutedSource(bool muted)
{
if (muted)
{
LOG("Audio: Mute source");
system("pamixer --default-source --mute");
}
else
{
LOG("Audio: Unmute source");
system("pamixer --default-source --unmute");
}
}
inline void Shutdown() inline void Shutdown()
{ {
pa_mainloop_free(mainLoop); pa_mainloop_free(mainLoop);

View file

@ -484,6 +484,15 @@ namespace System
PulseAudio::SetVolumeSource(volume); PulseAudio::SetVolumeSource(volume);
} }
void SetMutedSink(bool muted)
{
PulseAudio::SetMutedSink(muted);
}
void SetMutedSource(bool muted)
{
PulseAudio::SetMutedSource(muted);
}
#ifdef WITH_WORKSPACES #ifdef WITH_WORKSPACES
void PollWorkspaces(const std::string& monitor, uint32_t numWorkspaces) void PollWorkspaces(const std::string& monitor, uint32_t numWorkspaces)
{ {

View file

@ -85,6 +85,8 @@ namespace System
AudioInfo GetAudioInfo(); AudioInfo GetAudioInfo();
void SetVolumeSink(double volume); void SetVolumeSink(double volume);
void SetVolumeSource(double volume); void SetVolumeSource(double volume);
void SetMutedSink(bool muted);
void SetMutedSource(bool muted);
#ifdef WITH_WORKSPACES #ifdef WITH_WORKSPACES
enum class WorkspaceStatus enum class WorkspaceStatus