Skip to content

Commit 2830e71

Browse files
zhangxianwei8lukefromdc
authored andcommitted
media-keys: Add microphone mute key support
Pressing the microphone mute button now toggles the mute status. Fix #175 Signed-off-by: Zhang Xianwei <zhang.xianwei8@zte.com.cn>
1 parent 3e77bd1 commit 2830e71

File tree

5 files changed

+173
-25
lines changed

5 files changed

+173
-25
lines changed

data/org.mate.SettingsDaemon.plugins.media-keys.gschema.xml.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
<summary>Turn the volume up quietly</summary>
5656
<description>Binding to raise the system volume quietly.</description>
5757
</key>
58+
<key name="mic-mute" type="s">
59+
<default>'XF86AudioMicMute'</default>
60+
<summary>Microphone mute/unmute</summary>
61+
<description>Binding to mute/unmute the microphone.</description>
62+
</key>
5863
<key name="power" type="s">
5964
<default>'&lt;Control&gt;&lt;Alt&gt;Delete'</default>
6065
<summary>Shut down</summary>

plugins/media-keys/acme.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum {
3535
MUTE_QUIET_KEY,
3636
VOLUME_DOWN_QUIET_KEY,
3737
VOLUME_UP_QUIET_KEY,
38+
MIC_MUTE_KEY,
3839
POWER_KEY,
3940
EJECT_KEY,
4041
HOME_KEY,
@@ -80,6 +81,7 @@ static struct {
8081
{ MUTE_QUIET_KEY, "volume-mute-quiet", NULL, NULL },
8182
{ VOLUME_DOWN_QUIET_KEY, "volume-down-quiet", NULL, NULL },
8283
{ VOLUME_UP_QUIET_KEY, "volume-up-quiet", NULL, NULL },
84+
{ MIC_MUTE_KEY, "mic-mute", NULL, NULL },
8385
{ POWER_KEY, "power", NULL, NULL },
8486
{ EJECT_KEY, "eject", NULL, NULL },
8587
{ HOME_KEY, "home", NULL, NULL },

plugins/media-keys/msd-media-keys-manager.c

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ struct _MsdMediaKeysManagerPrivate
7373
/* Volume bits */
7474
MateMixerContext *context;
7575
MateMixerStream *stream;
76+
MateMixerStream *source_stream;
7677
MateMixerStreamControl *control;
78+
MateMixerStreamControl *source_control;
7779
#endif
7880
GtkWidget *dialog;
7981
GSettings *settings;
@@ -635,15 +637,21 @@ update_dialog (MsdMediaKeysManager *manager,
635637
guint volume,
636638
gboolean muted,
637639
gboolean sound_changed,
638-
gboolean quiet)
640+
gboolean quiet,
641+
gboolean is_mic)
639642
{
640643
if (muted)
641644
volume = 0.0;
642645

643646
dialog_init (manager);
644647

645-
msd_media_keys_window_set_volume_muted (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
646-
muted);
648+
if (is_mic)
649+
msd_media_keys_window_set_mic_muted (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
650+
muted);
651+
else
652+
msd_media_keys_window_set_volume_muted (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
653+
muted);
654+
647655
msd_media_keys_window_set_volume_level (MSD_MEDIA_KEYS_WINDOW (manager->priv->dialog),
648656
volume);
649657

@@ -652,7 +660,7 @@ update_dialog (MsdMediaKeysManager *manager,
652660
dialog_show (manager);
653661

654662
#ifdef HAVE_LIBCANBERRA
655-
if (quiet == FALSE && sound_changed != FALSE && muted == FALSE)
663+
if (quiet == FALSE && sound_changed != FALSE && muted == FALSE && is_mic == FALSE)
656664
ca_gtk_play_for_widget (manager->priv->dialog, 0,
657665
CA_PROP_EVENT_ID, "audio-volume-change",
658666
CA_PROP_EVENT_DESCRIPTION, "Volume changed through key press",
@@ -675,14 +683,21 @@ do_sound_action (MsdMediaKeysManager *manager,
675683
guint volume_min, volume_max;
676684
guint volume_step;
677685
guint volume_last;
686+
MateMixerStreamControl *control;
678687

679-
if (manager->priv->control == NULL)
688+
gboolean is_input_control =
689+
type == MIC_MUTE_KEY ? TRUE : FALSE;
690+
if (is_input_control)
691+
control = manager->priv->source_control;
692+
else
693+
control = manager->priv->control;
694+
if (control == NULL)
680695
return;
681696

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

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

695710
volume = volume_last =
696-
mate_mixer_stream_control_get_volume (manager->priv->control);
711+
mate_mixer_stream_control_get_volume (control);
697712
muted = muted_last =
698-
mate_mixer_stream_control_get_mute (manager->priv->control);
713+
mate_mixer_stream_control_get_mute (control);
699714

700715
switch (type) {
701716
case MUTE_KEY:
717+
case MIC_MUTE_KEY:
702718
muted = !muted;
703719
break;
704720
case VOLUME_DOWN_KEY:
@@ -723,13 +739,14 @@ do_sound_action (MsdMediaKeysManager *manager,
723739
}
724740

725741
if (muted != muted_last) {
726-
if (mate_mixer_stream_control_set_mute (manager->priv->control, muted))
742+
if (mate_mixer_stream_control_set_mute (control, muted))
727743
sound_changed = TRUE;
728744
else
729745
muted = muted_last;
730746
}
731-
if (volume != mate_mixer_stream_control_get_volume (manager->priv->control)) {
732-
if (mate_mixer_stream_control_set_volume (manager->priv->control, volume))
747+
748+
if (volume != mate_mixer_stream_control_get_volume (control)) {
749+
if (mate_mixer_stream_control_set_volume (control, volume))
733750
sound_changed = TRUE;
734751
else
735752
volume = volume_last;
@@ -739,7 +756,8 @@ do_sound_action (MsdMediaKeysManager *manager,
739756
CLAMP (100 * volume / (volume_max - volume_min), 0, 100),
740757
muted,
741758
sound_changed,
742-
quiet);
759+
quiet,
760+
is_input_control);
743761
}
744762

745763
static void
@@ -775,12 +793,45 @@ update_default_output (MsdMediaKeysManager *manager)
775793
g_debug ("Default output stream unset");
776794
}
777795

796+
static void
797+
update_default_input (MsdMediaKeysManager *manager)
798+
{
799+
MateMixerStream *stream;
800+
MateMixerStreamControl *control = NULL;
801+
802+
stream = mate_mixer_context_get_default_input_stream (manager->priv->context);
803+
if (stream != NULL)
804+
control = mate_mixer_stream_get_default_control (stream);
805+
806+
if (stream == manager->priv->source_stream)
807+
return;
808+
809+
g_clear_object (&manager->priv->source_stream);
810+
g_clear_object (&manager->priv->source_control);
811+
812+
if (control != NULL) {
813+
MateMixerStreamControlFlags flags = mate_mixer_stream_control_get_flags (control);
814+
815+
/* Do not use the stream if it is not possible to mute it or
816+
* change the volume */
817+
if (!(flags & MATE_MIXER_STREAM_CONTROL_MUTE_WRITABLE))
818+
return;
819+
820+
manager->priv->source_stream = g_object_ref (stream);
821+
manager->priv->source_control = g_object_ref (control);
822+
g_debug ("Default input stream updated to %s",
823+
mate_mixer_stream_get_name (stream));
824+
} else
825+
g_debug ("Default input stream unset");
826+
}
827+
778828
static void
779829
on_context_state_notify (MateMixerContext *context,
780830
GParamSpec *pspec,
781831
MsdMediaKeysManager *manager)
782832
{
783833
update_default_output (manager);
834+
update_default_input (manager);
784835
}
785836

786837
static void
@@ -791,6 +842,14 @@ on_context_default_output_notify (MateMixerContext *context,
791842
update_default_output (manager);
792843
}
793844

845+
static void
846+
on_context_default_input_notify (MateMixerContext *context,
847+
GParamSpec *pspec,
848+
MsdMediaKeysManager *manager)
849+
{
850+
update_default_input (manager);
851+
}
852+
794853
static void
795854
on_context_stream_removed (MateMixerContext *context,
796855
const gchar *name,
@@ -805,6 +864,15 @@ on_context_stream_removed (MateMixerContext *context,
805864
g_clear_object (&manager->priv->control);
806865
}
807866
}
867+
if (manager->priv->source_stream != NULL) {
868+
MateMixerStream *stream =
869+
mate_mixer_context_get_stream (manager->priv->context, name);
870+
871+
if (stream == manager->priv->source_stream) {
872+
g_clear_object (&manager->priv->source_stream);
873+
g_clear_object (&manager->priv->source_control);
874+
}
875+
}
808876
}
809877
#endif /* HAVE_LIBMATEMIXER */
810878

@@ -1102,6 +1170,7 @@ do_action (MsdMediaKeysManager *manager,
11021170
case MUTE_KEY:
11031171
case VOLUME_DOWN_KEY:
11041172
case VOLUME_UP_KEY:
1173+
case MIC_MUTE_KEY:
11051174
#ifdef HAVE_LIBMATEMIXER
11061175
do_sound_action (manager, type, FALSE);
11071176
#endif
@@ -1398,6 +1467,10 @@ msd_media_keys_manager_start (MsdMediaKeysManager *manager, GError **error)
13981467
"notify::default-output-stream",
13991468
G_CALLBACK (on_context_default_output_notify),
14001469
manager);
1470+
g_signal_connect (manager->priv->context,
1471+
"notify::default-input-stream",
1472+
G_CALLBACK (on_context_default_input_notify),
1473+
manager);
14011474
g_signal_connect (manager->priv->context,
14021475
"stream-removed",
14031476
G_CALLBACK (on_context_stream_removed),
@@ -1483,7 +1556,9 @@ msd_media_keys_manager_stop (MsdMediaKeysManager *manager)
14831556

14841557
#ifdef HAVE_LIBMATEMIXER
14851558
g_clear_object (&priv->stream);
1559+
g_clear_object (&priv->source_stream);
14861560
g_clear_object (&priv->control);
1561+
g_clear_object (&priv->source_control);
14871562
g_clear_object (&priv->context);
14881563
#endif
14891564

plugins/media-keys/msd-media-keys-window.c

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ struct MsdMediaKeysWindowPrivate
4343
char *description;
4444

4545
guint volume_muted : 1;
46+
guint mic_muted : 1;
47+
guint is_mic :1;
4648
int volume_level;
4749

4850
GtkImage *image;
@@ -98,10 +100,16 @@ action_changed (MsdMediaKeysWindow *window)
98100
volume_controls_set_visible (window, TRUE);
99101
description_label_set_visible (window, FALSE);
100102

101-
if (window->priv->volume_muted) {
102-
window_set_icon_name (window, "audio-volume-muted");
103+
if (window->priv->is_mic) {
104+
if (window->priv->mic_muted)
105+
window_set_icon_name (window, "microphone-sensitivity-muted");
106+
else
107+
window_set_icon_name (window, "microphone-sensitivity-high");
103108
} else {
104-
window_set_icon_name (window, "audio-volume-high");
109+
if (window->priv->volume_muted)
110+
window_set_icon_name (window, "audio-volume-muted");
111+
else
112+
window_set_icon_name (window, "audio-volume-high");
105113
}
106114

107115
break;
@@ -148,6 +156,20 @@ volume_muted_changed (MsdMediaKeysWindow *window)
148156
}
149157
}
150158

159+
static void
160+
mic_muted_changed (MsdMediaKeysWindow *window)
161+
{
162+
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
163+
164+
if (!msd_osd_window_is_composited (MSD_OSD_WINDOW (window))) {
165+
if (window->priv->mic_muted) {
166+
window_set_icon_name (window, "microphone-sensitivity-muted");
167+
} else {
168+
window_set_icon_name (window, "microphone-sensitivity-high");
169+
}
170+
}
171+
}
172+
151173
void
152174
msd_media_keys_window_set_action (MsdMediaKeysWindow *window,
153175
MsdMediaKeysWindowAction action)
@@ -159,6 +181,17 @@ msd_media_keys_window_set_action (MsdMediaKeysWindow *window,
159181
window->priv->action = action;
160182
action_changed (window);
161183
} else {
184+
if (window->priv->is_mic) {
185+
if (window->priv->mic_muted)
186+
window_set_icon_name (window, "microphone-sensitivity-muted");
187+
else
188+
window_set_icon_name (window, "microphone-sensitivity-high");
189+
} else {
190+
if (window->priv->volume_muted)
191+
window_set_icon_name (window, "audio-volume-muted");
192+
else
193+
window_set_icon_name (window, "audio-volume-high");
194+
}
162195
msd_osd_window_update_and_hide (MSD_OSD_WINDOW (window));
163196
}
164197
}
@@ -195,6 +228,20 @@ msd_media_keys_window_set_volume_muted (MsdMediaKeysWindow *window,
195228
window->priv->volume_muted = muted;
196229
volume_muted_changed (window);
197230
}
231+
window->priv->is_mic = FALSE;
232+
}
233+
234+
void
235+
msd_media_keys_window_set_mic_muted (MsdMediaKeysWindow *window,
236+
gboolean muted)
237+
{
238+
g_return_if_fail (MSD_IS_MEDIA_KEYS_WINDOW (window));
239+
240+
if (window->priv->mic_muted != muted) {
241+
window->priv->mic_muted = muted;
242+
mic_muted_changed (window);
243+
}
244+
window->priv->is_mic = TRUE;
198245
}
199246

200247
void
@@ -379,18 +426,35 @@ render_speaker (MsdMediaKeysWindow *window,
379426
"audio-volume-low",
380427
"audio-volume-medium",
381428
"audio-volume-high",
429+
"microphone-sensitivity-muted",
430+
"microphone-sensitivity-low",
431+
"microphone-sensitivity-medium",
432+
"microphone-sensitivity-high",
382433
NULL
383434
};
384-
385-
if (window->priv->volume_muted) {
386-
n = 0;
435+
if (!window->priv->is_mic) {
436+
if (window->priv->volume_muted) {
437+
n = 0;
438+
} else {
439+
/* select volume image */
440+
n = 3 * window->priv->volume_level / 100 + 1;
441+
if (n < 1) {
442+
n = 1;
443+
} else if (n > 3) {
444+
n = 3;
445+
}
446+
}
387447
} else {
388-
/* select image */
389-
n = 3 * window->priv->volume_level / 100 + 1;
390-
if (n < 1) {
391-
n = 1;
392-
} else if (n > 3) {
393-
n = 3;
448+
if (window->priv->mic_muted) {
449+
n = 4;
450+
} else {
451+
/* select microphone image */
452+
n = 3 * window->priv->volume_level / 100 + 5;
453+
if (n < 5) {
454+
n = 5;
455+
} else if (n > 7) {
456+
n = 7;
457+
}
394458
}
395459
}
396460

plugins/media-keys/msd-media-keys-window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void msd_media_keys_window_set_action (MsdMediaKeysWindo
6565
void msd_media_keys_window_set_action_custom (MsdMediaKeysWindow *window,
6666
const char *icon_name,
6767
const char *description);
68+
void msd_media_keys_window_set_mic_muted (MsdMediaKeysWindow *window,
69+
gboolean muted);
6870
void msd_media_keys_window_set_volume_muted (MsdMediaKeysWindow *window,
6971
gboolean muted);
7072
void msd_media_keys_window_set_volume_level (MsdMediaKeysWindow *window,

0 commit comments

Comments
 (0)