Skip to content

Commit

Permalink
media-keys: Add microphone mute key support
Browse files Browse the repository at this point in the history
Pressing the microphone mute button now toggles the mute status.

Fix #175

Signed-off-by: Zhang Xianwei <zhang.xianwei8@zte.com.cn>
  • Loading branch information
zhangxianwei8 authored and lukefromdc committed Dec 5, 2018
1 parent 3e77bd1 commit 2830e71
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 25 deletions.
Expand Up @@ -55,6 +55,11 @@
<summary>Turn the volume up quietly</summary>
<description>Binding to raise the system volume quietly.</description>
</key>
<key name="mic-mute" type="s">
<default>'XF86AudioMicMute'</default>
<summary>Microphone mute/unmute</summary>
<description>Binding to mute/unmute the microphone.</description>
</key>
<key name="power" type="s">
<default>'&lt;Control&gt;&lt;Alt&gt;Delete'</default>
<summary>Shut down</summary>
Expand Down
2 changes: 2 additions & 0 deletions plugins/media-keys/acme.h
Expand Up @@ -35,6 +35,7 @@ enum {
MUTE_QUIET_KEY,
VOLUME_DOWN_QUIET_KEY,
VOLUME_UP_QUIET_KEY,
MIC_MUTE_KEY,
POWER_KEY,
EJECT_KEY,
HOME_KEY,
Expand Down Expand Up @@ -80,6 +81,7 @@ static struct {
{ MUTE_QUIET_KEY, "volume-mute-quiet", NULL, NULL },
{ VOLUME_DOWN_QUIET_KEY, "volume-down-quiet", NULL, NULL },
{ VOLUME_UP_QUIET_KEY, "volume-up-quiet", NULL, NULL },
{ MIC_MUTE_KEY, "mic-mute", NULL, NULL },
{ POWER_KEY, "power", NULL, NULL },
{ EJECT_KEY, "eject", NULL, NULL },
{ HOME_KEY, "home", NULL, NULL },
Expand Down
101 changes: 88 additions & 13 deletions plugins/media-keys/msd-media-keys-manager.c
Expand Up @@ -73,7 +73,9 @@ struct _MsdMediaKeysManagerPrivate
/* Volume bits */
MateMixerContext *context;
MateMixerStream *stream;
MateMixerStream *source_stream;
MateMixerStreamControl *control;
MateMixerStreamControl *source_control;
#endif
GtkWidget *dialog;
GSettings *settings;
Expand Down Expand Up @@ -635,15 +637,21 @@ update_dialog (MsdMediaKeysManager *manager,
guint volume,
gboolean muted,
gboolean sound_changed,
gboolean quiet)
gboolean quiet,
gboolean is_mic)
{
if (muted)
volume = 0.0;

dialog_init (manager);

msd_media_keys_window_set_volume_muted (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
muted);
if (is_mic)
msd_media_keys_window_set_mic_muted (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
muted);
else
msd_media_keys_window_set_volume_muted (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
muted);

msd_media_keys_window_set_volume_level (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
volume);

Expand All @@ -652,7 +660,7 @@ update_dialog (MsdMediaKeysManager *manager,
dialog_show (manager);

#ifdef HAVE_LIBCANBERRA
if (quiet == FALSE && sound_changed != FALSE && muted == FALSE)
if (quiet == FALSE && sound_changed != FALSE && muted == FALSE && is_mic == FALSE)
ca_gtk_play_for_widget (manager->priv->dialog, 0,
CA_PROP_EVENT_ID, "audio-volume-change",
CA_PROP_EVENT_DESCRIPTION, "Volume changed through key press",
Expand All @@ -675,14 +683,21 @@ do_sound_action (MsdMediaKeysManager *manager,
guint volume_min, volume_max;
guint volume_step;
guint volume_last;
MateMixerStreamControl *control;

if (manager->priv->control == NULL)
gboolean is_input_control =
type == MIC_MUTE_KEY ? TRUE : FALSE;
if (is_input_control)
control = manager->priv->source_control;
else
control = manager->priv->control;
if (control == NULL)
return;

/* Theoretically the volume limits might be different for different
* streams, also the minimum might not always start at 0 */
volume_min = mate_mixer_stream_control_get_min_volume (manager->priv->control);
volume_max = mate_mixer_stream_control_get_normal_volume (manager->priv->control);
volume_min = mate_mixer_stream_control_get_min_volume (control);
volume_max = mate_mixer_stream_control_get_normal_volume (control);

volume_step = g_settings_get_int (manager->priv->settings, "volume-step");
if (volume_step <= 0 ||
Expand All @@ -693,12 +708,13 @@ do_sound_action (MsdMediaKeysManager *manager,
volume_step = (volume_max - volume_min) * volume_step / 100;

volume = volume_last =
mate_mixer_stream_control_get_volume (manager->priv->control);
mate_mixer_stream_control_get_volume (control);
muted = muted_last =
mate_mixer_stream_control_get_mute (manager->priv->control);
mate_mixer_stream_control_get_mute (control);

switch (type) {
case MUTE_KEY:
case MIC_MUTE_KEY:
muted = !muted;
break;
case VOLUME_DOWN_KEY:
Expand All @@ -723,13 +739,14 @@ do_sound_action (MsdMediaKeysManager *manager,
}

if (muted != muted_last) {
if (mate_mixer_stream_control_set_mute (manager->priv->control, muted))
if (mate_mixer_stream_control_set_mute (control, muted))
sound_changed = TRUE;
else
muted = muted_last;
}
if (volume != mate_mixer_stream_control_get_volume (manager->priv->control)) {
if (mate_mixer_stream_control_set_volume (manager->priv->control, volume))

if (volume != mate_mixer_stream_control_get_volume (control)) {
if (mate_mixer_stream_control_set_volume (control, volume))
sound_changed = TRUE;
else
volume = volume_last;
Expand All @@ -739,7 +756,8 @@ do_sound_action (MsdMediaKeysManager *manager,
CLAMP (100 * volume / (volume_max - volume_min), 0, 100),
muted,
sound_changed,
quiet);
quiet,
is_input_control);
}

static void
Expand Down Expand Up @@ -775,12 +793,45 @@ update_default_output (MsdMediaKeysManager *manager)
g_debug ("Default output stream unset");
}

static void
update_default_input (MsdMediaKeysManager *manager)
{
MateMixerStream *stream;
MateMixerStreamControl *control = NULL;

stream = mate_mixer_context_get_default_input_stream (manager->priv->context);
if (stream != NULL)
control = mate_mixer_stream_get_default_control (stream);

if (stream == manager->priv->source_stream)
return;

g_clear_object (&manager->priv->source_stream);
g_clear_object (&manager->priv->source_control);

if (control != NULL) {
MateMixerStreamControlFlags flags = mate_mixer_stream_control_get_flags (control);

/* Do not use the stream if it is not possible to mute it or
* change the volume */
if (!(flags & MATE_MIXER_STREAM_CONTROL_MUTE_WRITABLE))
return;

manager->priv->source_stream = g_object_ref (stream);
manager->priv->source_control = g_object_ref (control);
g_debug ("Default input stream updated to %s",
mate_mixer_stream_get_name (stream));
} else
g_debug ("Default input stream unset");
}

static void
on_context_state_notify (MateMixerContext *context,
GParamSpec *pspec,
MsdMediaKeysManager *manager)
{
update_default_output (manager);
update_default_input (manager);
}

static void
Expand All @@ -791,6 +842,14 @@ on_context_default_output_notify (MateMixerContext *context,
update_default_output (manager);
}

static void
on_context_default_input_notify (MateMixerContext *context,
GParamSpec *pspec,
MsdMediaKeysManager *manager)
{
update_default_input (manager);
}

static void
on_context_stream_removed (MateMixerContext *context,
const gchar *name,
Expand All @@ -805,6 +864,15 @@ on_context_stream_removed (MateMixerContext *context,
g_clear_object (&manager->priv->control);
}
}
if (manager->priv->source_stream != NULL) {
MateMixerStream *stream =
mate_mixer_context_get_stream (manager->priv->context, name);

if (stream == manager->priv->source_stream) {
g_clear_object (&manager->priv->source_stream);
g_clear_object (&manager->priv->source_control);
}
}
}
#endif /* HAVE_LIBMATEMIXER */

Expand Down Expand Up @@ -1102,6 +1170,7 @@ do_action (MsdMediaKeysManager *manager,
case MUTE_KEY:
case VOLUME_DOWN_KEY:
case VOLUME_UP_KEY:
case MIC_MUTE_KEY:
#ifdef HAVE_LIBMATEMIXER
do_sound_action (manager, type, FALSE);
#endif
Expand Down Expand Up @@ -1398,6 +1467,10 @@ msd_media_keys_manager_start (MsdMediaKeysManager *manager, GError **error)
"notify::default-output-stream",
G_CALLBACK (on_context_default_output_notify),
manager);
g_signal_connect (manager->priv->context,
"notify::default-input-stream",
G_CALLBACK (on_context_default_input_notify),
manager);
g_signal_connect (manager->priv->context,
"stream-removed",
G_CALLBACK (on_context_stream_removed),
Expand Down Expand Up @@ -1483,7 +1556,9 @@ msd_media_keys_manager_stop (MsdMediaKeysManager *manager)

#ifdef HAVE_LIBMATEMIXER
g_clear_object (&priv->stream);
g_clear_object (&priv->source_stream);
g_clear_object (&priv->control);
g_clear_object (&priv->source_control);
g_clear_object (&priv->context);
#endif

Expand Down
88 changes: 76 additions & 12 deletions plugins/media-keys/msd-media-keys-window.c
Expand Up @@ -43,6 +43,8 @@ struct MsdMediaKeysWindowPrivate
char *description;

guint volume_muted : 1;
guint mic_muted : 1;
guint is_mic :1;
int volume_level;

GtkImage *image;
Expand Down Expand Up @@ -98,10 +100,16 @@ action_changed (MsdMediaKeysWindow *window)
volume_controls_set_visible (window, TRUE);
description_label_set_visible (window, FALSE);

if (window->priv->volume_muted) {
window_set_icon_name (window, "audio-volume-muted");
if (window->priv->is_mic) {
if (window->priv->mic_muted)
window_set_icon_name (window, "microphone-sensitivity-muted");
else
window_set_icon_name (window, "microphone-sensitivity-high");
} else {
window_set_icon_name (window, "audio-volume-high");
if (window->priv->volume_muted)
window_set_icon_name (window, "audio-volume-muted");
else
window_set_icon_name (window, "audio-volume-high");
}

break;
Expand Down Expand Up @@ -148,6 +156,20 @@ volume_muted_changed (MsdMediaKeysWindow *window)
}
}

static void
mic_muted_changed (MsdMediaKeysWindow *window)
{
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));

if (!msd_osd_window_is_composited (MSD_OSD_WINDOW (window))) {
if (window->priv->mic_muted) {
window_set_icon_name (window, "microphone-sensitivity-muted");
} else {
window_set_icon_name (window, "microphone-sensitivity-high");
}
}
}

void
msd_media_keys_window_set_action (MsdMediaKeysWindow *window,
MsdMediaKeysWindowAction action)
Expand All @@ -159,6 +181,17 @@ msd_media_keys_window_set_action (MsdMediaKeysWindow *window,
window->priv->action = action;
action_changed (window);
} else {
if (window->priv->is_mic) {
if (window->priv->mic_muted)
window_set_icon_name (window, "microphone-sensitivity-muted");
else
window_set_icon_name (window, "microphone-sensitivity-high");
} else {
if (window->priv->volume_muted)
window_set_icon_name (window, "audio-volume-muted");
else
window_set_icon_name (window, "audio-volume-high");
}
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
}
}
Expand Down Expand Up @@ -195,6 +228,20 @@ msd_media_keys_window_set_volume_muted (MsdMediaKeysWindow *window,
window->priv->volume_muted = muted;
volume_muted_changed (window);
}
window->priv->is_mic = FALSE;
}

void
msd_media_keys_window_set_mic_muted (MsdMediaKeysWindow *window,
gboolean muted)
{
g_return_if_fail (MSD_IS_MEDIA_KEYS_WINDOW (window));

if (window->priv->mic_muted != muted) {
window->priv->mic_muted = muted;
mic_muted_changed (window);
}
window->priv->is_mic = TRUE;
}

void
Expand Down Expand Up @@ -379,18 +426,35 @@ render_speaker (MsdMediaKeysWindow *window,
"audio-volume-low",
"audio-volume-medium",
"audio-volume-high",
"microphone-sensitivity-muted",
"microphone-sensitivity-low",
"microphone-sensitivity-medium",
"microphone-sensitivity-high",
NULL
};

if (window->priv->volume_muted) {
n = 0;
if (!window->priv->is_mic) {
if (window->priv->volume_muted) {
n = 0;
} else {
/* select volume image */
n = 3 * window->priv->volume_level / 100 + 1;
if (n < 1) {
n = 1;
} else if (n > 3) {
n = 3;
}
}
} else {
/* select image */
n = 3 * window->priv->volume_level / 100 + 1;
if (n < 1) {
n = 1;
} else if (n > 3) {
n = 3;
if (window->priv->mic_muted) {
n = 4;
} else {
/* select microphone image */
n = 3 * window->priv->volume_level / 100 + 5;
if (n < 5) {
n = 5;
} else if (n > 7) {
n = 7;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/media-keys/msd-media-keys-window.h
Expand Up @@ -65,6 +65,8 @@ void msd_media_keys_window_set_action (MsdMediaKeysWindo
void msd_media_keys_window_set_action_custom (MsdMediaKeysWindow *window,
const char *icon_name,
const char *description);
void msd_media_keys_window_set_mic_muted (MsdMediaKeysWindow *window,
gboolean muted);
void msd_media_keys_window_set_volume_muted (MsdMediaKeysWindow *window,
gboolean muted);
void msd_media_keys_window_set_volume_level (MsdMediaKeysWindow *window,
Expand Down

0 comments on commit 2830e71

Please sign in to comment.