2023-01-13 00:02:50 +01:00
|
|
|
/*
|
2018-10-10 16:08:37 +13:00
|
|
|
* Copyright (C) 2018 Canonical Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pulse/pulseaudio.h>
|
|
|
|
#include <gvc-mixer-control.h>
|
|
|
|
|
2018-11-19 12:57:52 +13:00
|
|
|
#include "cc-sound-resources.h"
|
2018-10-10 16:08:37 +13:00
|
|
|
#include "cc-volume-slider.h"
|
|
|
|
|
|
|
|
struct _CcVolumeSlider
|
|
|
|
{
|
2023-01-06 19:02:58 +01:00
|
|
|
GtkWidget parent_instance;
|
2018-10-10 16:08:37 +13:00
|
|
|
|
2023-01-15 19:35:20 +01:00
|
|
|
GtkButton *mute_button;
|
2018-10-10 16:08:37 +13:00
|
|
|
GtkAdjustment *volume_adjustment;
|
|
|
|
GtkScale *volume_scale;
|
|
|
|
|
2019-08-13 11:11:05 +12:00
|
|
|
gboolean is_amplified;
|
|
|
|
GvcMixerControl *mixer_control;
|
2018-10-10 16:08:37 +13:00
|
|
|
GvcMixerStream *stream;
|
2023-01-15 20:07:04 +01:00
|
|
|
CcStreamType type;
|
2018-10-10 16:08:37 +13:00
|
|
|
guint notify_volume_handler_id;
|
|
|
|
guint notify_is_muted_handler_id;
|
|
|
|
};
|
|
|
|
|
2023-01-06 19:02:58 +01:00
|
|
|
G_DEFINE_TYPE (CcVolumeSlider, cc_volume_slider, GTK_TYPE_WIDGET)
|
2018-10-10 16:08:37 +13:00
|
|
|
|
2019-12-18 11:32:00 +01:00
|
|
|
static void
|
|
|
|
update_volume_icon (CcVolumeSlider *self)
|
|
|
|
{
|
|
|
|
const gchar *icon_name = NULL;
|
|
|
|
gdouble volume, fraction;
|
|
|
|
|
|
|
|
volume = gtk_adjustment_get_value (self->volume_adjustment);
|
|
|
|
fraction = (100.0 * volume) / gtk_adjustment_get_upper (self->volume_adjustment);
|
|
|
|
|
2023-01-15 20:07:04 +01:00
|
|
|
switch (self->type)
|
|
|
|
{
|
|
|
|
case CC_STREAM_TYPE_INPUT:
|
|
|
|
if (fraction == 0.0)
|
|
|
|
icon_name = "microphone-sensitivity-muted-symbolic";
|
|
|
|
else if (fraction < 30.0)
|
|
|
|
icon_name = "microphone-sensitivity-low-symbolic";
|
|
|
|
else if (fraction > 30.0 && fraction < 70.0)
|
|
|
|
icon_name = "microphone-sensitivity-medium-symbolic";
|
|
|
|
else
|
|
|
|
icon_name = "microphone-sensitivity-high-symbolic";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CC_STREAM_TYPE_OUTPUT:
|
|
|
|
if (fraction == 0.0)
|
|
|
|
icon_name = "audio-volume-muted-symbolic";
|
|
|
|
else if (fraction < 30.0)
|
|
|
|
icon_name = "audio-volume-low-symbolic";
|
|
|
|
else if (fraction > 30.0 && fraction < 70.0)
|
|
|
|
icon_name = "audio-volume-medium-symbolic";
|
|
|
|
else
|
|
|
|
icon_name = "audio-volume-high-symbolic";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2019-12-18 11:32:00 +01:00
|
|
|
|
2022-07-20 14:05:11 +02:00
|
|
|
gtk_button_set_icon_name (GTK_BUTTON (self->mute_button), icon_name);
|
2019-12-18 11:32:00 +01:00
|
|
|
}
|
|
|
|
|
2019-09-02 15:38:10 +01:00
|
|
|
static void
|
|
|
|
volume_changed_cb (CcVolumeSlider *self)
|
|
|
|
{
|
|
|
|
gdouble volume, rounded;
|
|
|
|
|
|
|
|
if (self->stream == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
volume = gtk_adjustment_get_value (self->volume_adjustment);
|
|
|
|
rounded = round (volume);
|
|
|
|
|
2023-01-15 19:35:20 +01:00
|
|
|
// If the stream is muted, unmute it
|
|
|
|
if (gvc_mixer_stream_get_is_muted (self->stream))
|
|
|
|
gvc_mixer_stream_change_is_muted (self->stream, FALSE);
|
2019-09-02 15:38:10 +01:00
|
|
|
|
|
|
|
if (gvc_mixer_stream_set_volume (self->stream, (pa_volume_t) rounded))
|
|
|
|
gvc_mixer_stream_push_volume (self->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notify_volume_cb (CcVolumeSlider *self)
|
|
|
|
{
|
|
|
|
g_signal_handlers_block_by_func (self->volume_adjustment, volume_changed_cb, self);
|
2023-01-15 19:35:20 +01:00
|
|
|
gtk_adjustment_set_value (self->volume_adjustment, gvc_mixer_stream_get_volume (self->stream));
|
|
|
|
g_signal_handlers_unblock_by_func (self->volume_adjustment, volume_changed_cb, self);
|
2019-09-02 15:38:10 +01:00
|
|
|
|
2021-02-20 19:45:27 +01:00
|
|
|
update_volume_icon (self);
|
2019-09-02 15:38:10 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 11:11:05 +12:00
|
|
|
static void
|
|
|
|
update_ranges (CcVolumeSlider *self)
|
|
|
|
{
|
|
|
|
gdouble vol_max_norm;
|
|
|
|
|
|
|
|
if (self->mixer_control == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vol_max_norm = gvc_mixer_control_get_vol_max_norm (self->mixer_control);
|
|
|
|
|
|
|
|
gtk_scale_clear_marks (self->volume_scale);
|
|
|
|
if (self->is_amplified)
|
|
|
|
{
|
|
|
|
gtk_adjustment_set_upper (self->volume_adjustment, gvc_mixer_control_get_vol_max_amplified (self->mixer_control));
|
|
|
|
gtk_scale_add_mark (self->volume_scale,
|
|
|
|
vol_max_norm,
|
|
|
|
GTK_POS_BOTTOM,
|
|
|
|
C_("volume", "100%"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_adjustment_set_upper (self->volume_adjustment, vol_max_norm);
|
|
|
|
}
|
|
|
|
gtk_adjustment_set_page_increment (self->volume_adjustment, vol_max_norm / 100.0);
|
2018-10-10 16:08:37 +13:00
|
|
|
|
2019-09-02 15:38:10 +01:00
|
|
|
if (self->stream)
|
|
|
|
notify_volume_cb (self);
|
2018-10-10 16:08:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-01-15 19:35:20 +01:00
|
|
|
mute_cb (GtkWidget *widget,
|
|
|
|
const char *action_name,
|
|
|
|
GVariant *parameter)
|
2018-10-10 16:08:37 +13:00
|
|
|
{
|
2023-01-15 19:35:20 +01:00
|
|
|
CcVolumeSlider *self = CC_VOLUME_SLIDER (widget);
|
|
|
|
|
2018-10-10 16:08:37 +13:00
|
|
|
if (self->stream == NULL)
|
|
|
|
return;
|
|
|
|
|
2023-01-15 19:37:46 +01:00
|
|
|
if (!gvc_mixer_stream_get_is_muted (self->stream) &&
|
|
|
|
gvc_mixer_stream_get_volume (self->stream) == 0.0)
|
|
|
|
{
|
|
|
|
gdouble default_volume = gvc_mixer_control_get_vol_max_norm (self->mixer_control) * 0.25;
|
|
|
|
|
|
|
|
if (gvc_mixer_stream_set_volume (self->stream, (pa_volume_t) default_volume))
|
|
|
|
gvc_mixer_stream_push_volume (self->stream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gvc_mixer_stream_change_is_muted (self->stream, !gvc_mixer_stream_get_is_muted (self->stream));
|
|
|
|
}
|
2018-10-10 16:08:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notify_is_muted_cb (CcVolumeSlider *self)
|
|
|
|
{
|
2023-01-15 19:35:20 +01:00
|
|
|
if (gvc_mixer_stream_get_is_muted (self->stream))
|
|
|
|
{
|
|
|
|
g_signal_handlers_block_by_func (self->volume_adjustment, volume_changed_cb, self);
|
|
|
|
gtk_adjustment_set_value (self->volume_adjustment, 0.0);
|
|
|
|
g_signal_handlers_unblock_by_func (self->volume_adjustment, volume_changed_cb, self);
|
|
|
|
|
|
|
|
update_volume_icon (self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify_volume_cb (self);
|
|
|
|
}
|
2018-10-10 16:08:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc_volume_slider_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
CcVolumeSlider *self = CC_VOLUME_SLIDER (object);
|
|
|
|
|
2023-01-06 19:02:58 +01:00
|
|
|
g_clear_pointer ((GtkWidget **) &self->volume_scale, gtk_widget_unparent);
|
|
|
|
g_clear_pointer ((GtkWidget **) &self->mute_button, gtk_widget_unparent);
|
|
|
|
|
2019-08-13 11:11:05 +12:00
|
|
|
g_clear_object (&self->mixer_control);
|
2018-10-10 16:08:37 +13:00
|
|
|
g_clear_object (&self->stream);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (cc_volume_slider_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cc_volume_slider_class_init (CcVolumeSliderClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->dispose = cc_volume_slider_dispose;
|
|
|
|
|
2018-11-19 12:57:52 +13:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/sound/cc-volume-slider.ui");
|
2018-10-10 16:08:37 +13:00
|
|
|
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcVolumeSlider, mute_button);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcVolumeSlider, volume_adjustment);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcVolumeSlider, volume_scale);
|
|
|
|
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, volume_changed_cb);
|
2023-01-06 19:02:58 +01:00
|
|
|
|
2023-01-15 19:35:20 +01:00
|
|
|
gtk_widget_class_install_action (widget_class, "volume-slider.mute", NULL, mute_cb);
|
|
|
|
|
2023-01-06 19:02:58 +01:00
|
|
|
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
|
2018-10-10 16:08:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cc_volume_slider_init (CcVolumeSlider *self)
|
|
|
|
{
|
2023-01-06 19:02:58 +01:00
|
|
|
GtkLayoutManager *box_layout;
|
|
|
|
|
2018-11-19 12:57:52 +13:00
|
|
|
g_resources_register (cc_sound_get_resource ());
|
2018-10-10 16:08:37 +13:00
|
|
|
|
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
2023-01-06 19:02:58 +01:00
|
|
|
|
|
|
|
box_layout = gtk_widget_get_layout_manager (GTK_WIDGET (self));
|
|
|
|
gtk_box_layout_set_spacing (GTK_BOX_LAYOUT (box_layout), 6);
|
2019-08-13 11:11:05 +12:00
|
|
|
}
|
2018-10-10 16:08:37 +13:00
|
|
|
|
2019-08-13 11:11:05 +12:00
|
|
|
void
|
|
|
|
cc_volume_slider_set_mixer_control (CcVolumeSlider *self,
|
|
|
|
GvcMixerControl *mixer_control)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CC_IS_VOLUME_SLIDER (self));
|
|
|
|
|
|
|
|
g_set_object (&self->mixer_control, mixer_control);
|
|
|
|
|
|
|
|
update_ranges (self);
|
2018-10-10 16:08:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cc_volume_slider_set_stream (CcVolumeSlider *self,
|
2019-06-14 18:07:45 +03:00
|
|
|
GvcMixerStream *stream,
|
|
|
|
CcStreamType type)
|
2018-10-10 16:08:37 +13:00
|
|
|
{
|
|
|
|
g_return_if_fail (CC_IS_VOLUME_SLIDER (self));
|
|
|
|
|
|
|
|
if (self->stream != NULL)
|
|
|
|
{
|
|
|
|
g_signal_handler_disconnect (self->stream, self->notify_volume_handler_id);
|
|
|
|
self->notify_volume_handler_id = 0;
|
|
|
|
g_signal_handler_disconnect (self->stream, self->notify_is_muted_handler_id);
|
|
|
|
self->notify_is_muted_handler_id = 0;
|
|
|
|
}
|
|
|
|
g_clear_object (&self->stream);
|
|
|
|
|
2023-01-15 20:07:04 +01:00
|
|
|
self->type = type;
|
2019-06-14 18:07:45 +03:00
|
|
|
|
2018-10-10 16:08:37 +13:00
|
|
|
if (stream != NULL)
|
|
|
|
{
|
|
|
|
self->stream = g_object_ref (stream);
|
|
|
|
|
|
|
|
self->notify_volume_handler_id = g_signal_connect_object (stream,
|
|
|
|
"notify::volume",
|
|
|
|
G_CALLBACK (notify_volume_cb),
|
|
|
|
self, G_CONNECT_SWAPPED);
|
|
|
|
self->notify_is_muted_handler_id = g_signal_connect_object (stream,
|
|
|
|
"notify::is-muted",
|
|
|
|
G_CALLBACK (notify_is_muted_cb),
|
|
|
|
self, G_CONNECT_SWAPPED);
|
|
|
|
notify_volume_cb (self);
|
|
|
|
notify_is_muted_cb (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
sound: update the volume-slider after getting a valid stream
In the ubuntu 20.04, we met an issue about the output volume-slider
on the machine with the legacy HDA audio driver, the output device
is the Speaker first (analog-stereo pa sink), then we connect a hdmi
monitor, the HDMI audio is in the output combo-box, we select the
hdmi audio (hdmi-stereo pa sink) from the combo-box, the hdmi audio
becomes the active output device now, we adjust the output volume from
the volume-slider, the slider UI is changed, but the output sound
is not changed with the UI.
The root cause is when the speaker is active, the pulseaudio only
keeps the analog-stereo sink, the sink hdmi-stereo is unlinked, when
users select the hdmi audio from UI, the pulseaudio will unlink
analo-stereo sink and create hdmi-stereo sink, but before hdmi-stereo
is created, the output_device_changed_cb() is called and
gvc_mixer_control_get_stream_from_device() returns a NULL since the
hdmi-stereo sink is not created yet in the pulseaudio. Because stream
is NULL, the output_volume_slider->stream is NULL, users can't change
the output volume via the volume-slider.
To fix it, we add a output_volume_slider->stream check in the
device_update_cb(), if it is NULL, get the stream and set it to
volume-slider. In this function, the sink hdmi-stereo is created
already, so the stream is not NULL. And this change also applies to
input as well.
Signed-off-by: Hui Wang <hui.wang@canonical.com>
2021-07-29 17:08:08 +08:00
|
|
|
GvcMixerStream *
|
|
|
|
cc_volume_slider_get_stream (CcVolumeSlider *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CC_IS_VOLUME_SLIDER (self), NULL);
|
|
|
|
|
|
|
|
return self->stream;
|
|
|
|
}
|
|
|
|
|
2018-10-10 16:08:37 +13:00
|
|
|
void
|
|
|
|
cc_volume_slider_set_is_amplified (CcVolumeSlider *self,
|
|
|
|
gboolean is_amplified)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CC_IS_VOLUME_SLIDER (self));
|
|
|
|
|
2019-08-13 11:11:05 +12:00
|
|
|
self->is_amplified = is_amplified;
|
|
|
|
|
|
|
|
update_ranges (self);
|
2018-10-10 16:08:37 +13:00
|
|
|
}
|