From 8073cafe398d80e4c9d3a93f463bf70018e9eb15 Mon Sep 17 00:00:00 2001 From: lenemter Date: Wed, 12 Jul 2023 13:48:49 +0900 Subject: [PATCH] Handle sound keybindings in the Wingpanel indicator --- meson.build | 1 + src/DBus.vala | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Indicator.vala | 10 +++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/DBus.vala diff --git a/meson.build b/meson.build index d6ee3852..96b2e5a7 100644 --- a/meson.build +++ b/meson.build @@ -42,6 +42,7 @@ config_file = configure_file( shared_module( meson.project_name(), config_file, + 'src/DBus.vala', 'src/Device.vala', 'src/Indicator.vala', 'src/Widgets/DisplayWidget.vala', diff --git a/src/DBus.vala b/src/DBus.vala new file mode 100644 index 00000000..36c3d91a --- /dev/null +++ b/src/DBus.vala @@ -0,0 +1,43 @@ +/* + * Copyright 2023 elementary, Inc. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +[DBus (name = "io.elementary.wingpanel.sound")] +public class Sound.DBus : GLib.Object { + private static DBus? instance; + private static Indicator indicator; + + [DBus (visible = false)] + public static void init (Indicator _indicator) { + indicator = _indicator; + + Bus.own_name (SESSION, "io.elementary.wingpanel.sound", NONE, + (connection) => { + if (instance == null) { + instance = new DBus (); + } + + try { + connection.register_object ("/io/elementary/wingpanel/sound", instance); + } catch (Error e) { + warning (e.message); + } + }, + () => {}, + () => warning ("Could not acquire name") + ); + } + + public void volume_up () throws DBusError, IOError { + indicator.handle_change (1.0, false); + } + + public void volume_down () throws DBusError, IOError { + indicator.handle_change (-1.0, false); + } + + public void mute () throws DBusError, IOError { + indicator.dbus_handle_mute (); + } +} \ No newline at end of file diff --git a/src/Indicator.vala b/src/Indicator.vala index fa0e33b5..b3ba815c 100644 --- a/src/Indicator.vala +++ b/src/Indicator.vala @@ -64,6 +64,8 @@ public class Sound.Indicator : Wingpanel.Indicator { var gnome_settings = new GLib.Settings ("org.gnome.settings-daemon.plugins.media-keys"); gnome_settings.bind ("volume-step", this, "volume-step", SettingsBindFlags.DEFAULT); + DBus.init (this); + visible = true; // Prevent a race that skips automatic resource loading @@ -441,7 +443,7 @@ public class Sound.Indicator : Wingpanel.Indicator { return false; } - private void handle_change (double change, bool is_mic) { + public void handle_change (double change, bool is_mic) { double v; if (is_mic) { @@ -469,6 +471,12 @@ public class Sound.Indicator : Wingpanel.Indicator { notify_change (is_mic); } + public void dbus_handle_mute () { + volume_control.toggle_mute (); + + notify_change (false); + } + public override void opened () { open = true;