Skip to content

Commit

Permalink
Allow muting applications
Browse files Browse the repository at this point in the history
  • Loading branch information
leolost2605 committed Aug 26, 2023
1 parent ef2b2ba commit 2d01585
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/App.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
* SPDX-FileCopyrightText: 2023 elementary, Inc. (https://elementary.io)
*
* Authored by: Leonhard Kargl <[email protected]>
*/

public class App : Object {
public uint32 index { get; construct; }
public string name { get; construct; }
public PulseAudio.ChannelMap channel_map { get; set; }
public double volume { get; set; }
public bool muted { get; set; }

public App (uint32 index, string name) {
Object (
index: index,
name: name
);
}
}
43 changes: 38 additions & 5 deletions src/AppRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Sound.AppRow : Gtk.ListBoxRow {
private App? app;
private Gtk.Label title_label;
private Gtk.Image image;
private Gtk.Button icon_button;
private Gtk.Scale volume_scale;

construct {
Expand All @@ -23,6 +24,11 @@ public class Sound.AppRow : Gtk.ListBoxRow {
};
title_label.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);

icon_button = new Gtk.Button.from_icon_name ("audio-volume-muted") {
can_focus = false
};
icon_button.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);

volume_scale = new Gtk.Scale.with_range (HORIZONTAL, 0, 1, 0.01) {
hexpand = true,
draw_value = false
Expand All @@ -36,8 +42,9 @@ public class Sound.AppRow : Gtk.ListBoxRow {
margin_start = 6
};
grid.attach (image, 0, 0, 1, 2);
grid.attach (title_label, 1, 0);
grid.attach (volume_scale, 1, 1);
grid.attach (title_label, 1, 0, 2);
grid.attach (icon_button, 1, 1);
grid.attach (volume_scale, 2, 1);

hexpand = true;
child = grid;
Expand All @@ -49,10 +56,35 @@ public class Sound.AppRow : Gtk.ListBoxRow {

return true;
});

icon_button.clicked.connect (() => toggle_mute_application ());
}

private void update_scale () {
private void toggle_mute_application (bool? custom = null) {
if (app == null) {
return;
}

PulseAudioManager.get_default ().mute_application (app, null != null ? custom : !app.muted);
}

private void update () {
volume_scale.set_value (app.volume);

if (app.muted) {
((Gtk.Image) icon_button.image).icon_name = "audio-volume-muted";
volume_scale.sensitive = false;
} else {
volume_scale.sensitive = true;

if (volume_scale.get_value () < 0.33) {
((Gtk.Image) icon_button.image).icon_name = "audio-volume-low";
} else if (volume_scale.get_value () > 0.66) {
((Gtk.Image) icon_button.image).icon_name = "audio-volume-high";
} else {
((Gtk.Image) icon_button.image).icon_name = "audio-volume-medium";
}
}
}

public void bind_app (App app) {
Expand All @@ -68,12 +100,13 @@ public class Sound.AppRow : Gtk.ListBoxRow {
image.set_from_icon_name ("application-default-icon", Gtk.IconSize.DND);
}

app.notify["volume"].connect (update_scale);
app.notify["volume"].connect (update);
app.notify["muted"].connect (update);

volume_scale.set_value (app.volume);
}

public void unbind_app () {
app.notify["volume"].disconnect (update_scale);
app.notify["volume"].disconnect (update);
}
}
14 changes: 13 additions & 1 deletion src/PulseAudioManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ public class Sound.PulseAudioManager : GLib.Object {
app.channel_map = sink_input.channel_map;
}

if ((sink_input.mute != 0) != app.muted) {
app.muted = sink_input.mute != 0;
}

var volume = sink_input.volume.avg ().sw_to_linear ();
if (app.volume != volume) {
app.volume = volume;
Expand Down Expand Up @@ -916,7 +920,15 @@ public class Sound.PulseAudioManager : GLib.Object {

context.set_sink_input_volume (app.index, cvol, (c, success) => {
if (success != 1) {
warning ("Changing application volume failed");
warning ("Failed to change volume of application '%s'.", app.name);
}
});
}

public void mute_application (App app, bool mute) {
context.set_sink_input_mute (app.index, mute, (c, success) => {
if (success != 1) {
warning ("Failed to mute application '%s'.", app.name);
}
});
}
Expand Down

0 comments on commit 2d01585

Please sign in to comment.