2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-07-22 20:39:44 -04:00
|
|
|
|
2010-10-20 15:53:23 -04:00
|
|
|
const Clutter = imports.gi.Clutter;
|
2010-07-22 20:39:44 -04:00
|
|
|
const Lang = imports.lang;
|
2012-10-18 13:42:17 -04:00
|
|
|
const Gio = imports.gi.Gio;
|
2010-07-22 20:39:44 -04:00
|
|
|
const Gvc = imports.gi.Gvc;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2010-10-20 15:53:23 -04:00
|
|
|
const VOLUME_ADJUSTMENT_STEP = 0.05; /* Volume adjustment step in % */
|
2010-07-22 20:39:44 -04:00
|
|
|
|
2011-02-15 13:23:36 -05:00
|
|
|
const VOLUME_NOTIFY_ID = 1;
|
|
|
|
|
2012-08-26 10:05:46 -04:00
|
|
|
// Each Gvc.MixerControl is a connection to PulseAudio,
|
|
|
|
// so it's better to make it a singleton
|
|
|
|
let _mixerControl;
|
|
|
|
function getMixerControl() {
|
|
|
|
if (_mixerControl)
|
|
|
|
return _mixerControl;
|
2010-07-22 20:39:44 -04:00
|
|
|
|
2012-08-26 10:05:46 -04:00
|
|
|
_mixerControl = new Gvc.MixerControl({ name: 'GNOME Shell Volume Control' });
|
|
|
|
_mixerControl.open();
|
|
|
|
|
|
|
|
return _mixerControl;
|
|
|
|
}
|
2010-07-22 20:39:44 -04:00
|
|
|
|
2012-08-26 10:05:46 -04:00
|
|
|
const VolumeMenu = new Lang.Class({
|
|
|
|
Name: 'VolumeMenu',
|
|
|
|
Extends: PopupMenu.PopupMenuSection,
|
|
|
|
|
|
|
|
_init: function(control) {
|
|
|
|
this.parent();
|
|
|
|
|
2012-10-18 13:42:17 -04:00
|
|
|
this.hasHeadphones = false;
|
|
|
|
|
2012-08-26 10:05:46 -04:00
|
|
|
this._control = control;
|
2011-03-28 10:10:11 -04:00
|
|
|
this._control.connect('state-changed', Lang.bind(this, this._onControlStateChanged));
|
2010-07-22 20:39:44 -04:00
|
|
|
this._control.connect('default-sink-changed', Lang.bind(this, this._readOutput));
|
|
|
|
this._control.connect('default-source-changed', Lang.bind(this, this._readInput));
|
|
|
|
this._control.connect('stream-added', Lang.bind(this, this._maybeShowInput));
|
|
|
|
this._control.connect('stream-removed', Lang.bind(this, this._maybeShowInput));
|
2011-03-11 10:45:11 -05:00
|
|
|
this._volumeMax = this._control.get_vol_max_norm();
|
2010-07-22 20:39:44 -04:00
|
|
|
|
|
|
|
this._output = null;
|
|
|
|
this._outputVolumeId = 0;
|
|
|
|
this._outputMutedId = 0;
|
2011-09-07 08:11:10 -04:00
|
|
|
/* Translators: This is the label for audio volume */
|
2010-11-16 09:22:38 -05:00
|
|
|
this._outputTitle = new PopupMenu.PopupMenuItem(_("Volume"), { reactive: false });
|
2011-07-19 19:45:29 -04:00
|
|
|
this._outputSlider = new PopupMenu.PopupSliderMenuItem(0);
|
2010-07-22 20:39:44 -04:00
|
|
|
this._outputSlider.connect('value-changed', Lang.bind(this, this._sliderChanged, '_output'));
|
|
|
|
this._outputSlider.connect('drag-end', Lang.bind(this, this._notifyVolumeChange));
|
2012-08-26 10:05:46 -04:00
|
|
|
this.addMenuItem(this._outputTitle);
|
|
|
|
this.addMenuItem(this._outputSlider);
|
2010-07-22 20:39:44 -04:00
|
|
|
|
2012-08-26 10:05:46 -04:00
|
|
|
this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
2010-07-22 20:39:44 -04:00
|
|
|
|
|
|
|
this._input = null;
|
|
|
|
this._inputVolumeId = 0;
|
|
|
|
this._inputMutedId = 0;
|
2010-11-16 09:22:38 -05:00
|
|
|
this._inputTitle = new PopupMenu.PopupMenuItem(_("Microphone"), { reactive: false });
|
2011-07-19 19:45:29 -04:00
|
|
|
this._inputSlider = new PopupMenu.PopupSliderMenuItem(0);
|
2010-07-22 20:39:44 -04:00
|
|
|
this._inputSlider.connect('value-changed', Lang.bind(this, this._sliderChanged, '_input'));
|
|
|
|
this._inputSlider.connect('drag-end', Lang.bind(this, this._notifyVolumeChange));
|
2012-08-26 10:05:46 -04:00
|
|
|
this.addMenuItem(this._inputTitle);
|
|
|
|
this.addMenuItem(this._inputSlider);
|
2012-09-04 11:55:26 -04:00
|
|
|
|
2012-09-21 21:15:44 -04:00
|
|
|
this._onControlStateChanged();
|
2012-05-22 18:27:06 -04:00
|
|
|
},
|
|
|
|
|
2012-11-04 10:53:23 -05:00
|
|
|
scroll: function(event) {
|
|
|
|
this._outputSlider.scroll(event);
|
2010-10-20 15:53:23 -04:00
|
|
|
},
|
|
|
|
|
2011-03-28 10:10:11 -04:00
|
|
|
_onControlStateChanged: function() {
|
|
|
|
if (this._control.get_state() == Gvc.MixerControlState.READY) {
|
|
|
|
this._readOutput();
|
|
|
|
this._readInput();
|
2012-09-21 21:15:44 -04:00
|
|
|
this._maybeShowInput();
|
2011-03-28 10:10:11 -04:00
|
|
|
} else {
|
2012-12-19 22:02:04 -05:00
|
|
|
this.emit('icon-changed');
|
2011-03-28 10:10:11 -04:00
|
|
|
}
|
2010-07-22 20:39:44 -04:00
|
|
|
},
|
|
|
|
|
2012-10-18 13:42:17 -04:00
|
|
|
_findHeadphones: function(sink) {
|
|
|
|
// This only works for external headphones (e.g. bluetooth)
|
|
|
|
if (sink.get_form_factor() == 'headset' ||
|
|
|
|
sink.get_form_factor() == 'headphone')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// a bit hackish, but ALSA/PulseAudio have a number
|
|
|
|
// of different identifiers for headphones, and I could
|
|
|
|
// not find the complete list
|
|
|
|
let port = sink.get_port();
|
|
|
|
if (port)
|
|
|
|
return port.port.indexOf('headphone') >= 0;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_portChanged: function() {
|
|
|
|
this.hasHeadphones = this._findHeadphones(this._output);
|
|
|
|
this.emit('headphones-changed');
|
|
|
|
},
|
|
|
|
|
2010-07-22 20:39:44 -04:00
|
|
|
_readOutput: function() {
|
|
|
|
if (this._outputVolumeId) {
|
|
|
|
this._output.disconnect(this._outputVolumeId);
|
|
|
|
this._output.disconnect(this._outputMutedId);
|
2012-10-18 13:42:17 -04:00
|
|
|
this._output.disconnect(this._outputPortId);
|
2010-07-22 20:39:44 -04:00
|
|
|
this._outputVolumeId = 0;
|
|
|
|
this._outputMutedId = 0;
|
2012-10-18 13:42:17 -04:00
|
|
|
this._outputPortId = 0;
|
2010-07-22 20:39:44 -04:00
|
|
|
}
|
|
|
|
this._output = this._control.get_default_sink();
|
|
|
|
if (this._output) {
|
2012-12-19 22:04:29 -05:00
|
|
|
this._outputMutedId = this._output.connect('notify::is-muted', Lang.bind(this, this._updateVolume, '_output'));
|
|
|
|
this._outputVolumeId = this._output.connect('notify::volume', Lang.bind(this, this._updateVolume, '_output'));
|
2012-10-18 13:42:17 -04:00
|
|
|
this._outputPortId = this._output.connect('notify::port', Lang.bind(this, this._portChanged));
|
|
|
|
|
2012-12-19 22:04:29 -05:00
|
|
|
this._updateVolume(null, null, '_output');
|
2012-10-18 13:42:17 -04:00
|
|
|
this._portChanged();
|
2010-07-22 20:39:44 -04:00
|
|
|
} else {
|
2012-10-18 13:42:17 -04:00
|
|
|
this.hasHeadphones = false;
|
2010-11-16 09:22:38 -05:00
|
|
|
this._outputSlider.setValue(0);
|
2012-12-19 22:02:04 -05:00
|
|
|
this.emit('icon-changed');
|
2010-07-22 20:39:44 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_readInput: function() {
|
|
|
|
if (this._inputVolumeId) {
|
|
|
|
this._input.disconnect(this._inputVolumeId);
|
|
|
|
this._input.disconnect(this._inputMutedId);
|
|
|
|
this._inputVolumeId = 0;
|
|
|
|
this._inputMutedId = 0;
|
|
|
|
}
|
|
|
|
this._input = this._control.get_default_source();
|
|
|
|
if (this._input) {
|
2012-12-19 22:04:29 -05:00
|
|
|
this._inputMutedId = this._input.connect('notify::is-muted', Lang.bind(this, this._updateVolume, '_input'));
|
|
|
|
this._inputVolumeId = this._input.connect('notify::volume', Lang.bind(this, this._updateVolume, '_input'));
|
|
|
|
this._updateVolume(null, null, '_input');
|
2010-07-22 20:39:44 -04:00
|
|
|
} else {
|
2010-11-16 09:22:38 -05:00
|
|
|
this._inputTitle.actor.hide();
|
2010-07-22 20:39:44 -04:00
|
|
|
this._inputSlider.actor.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_maybeShowInput: function() {
|
|
|
|
// only show input widgets if any application is recording audio
|
|
|
|
let showInput = false;
|
|
|
|
let recordingApps = this._control.get_source_outputs();
|
2010-10-20 15:15:24 -04:00
|
|
|
if (this._input && recordingApps) {
|
|
|
|
for (let i = 0; i < recordingApps.length; i++) {
|
|
|
|
let outputStream = recordingApps[i];
|
2010-07-22 20:39:44 -04:00
|
|
|
let id = outputStream.get_application_id();
|
|
|
|
// but skip gnome-volume-control and pavucontrol
|
|
|
|
// (that appear as recording because they show the input level)
|
|
|
|
if (!id || (id != 'org.gnome.VolumeControl' && id != 'org.PulseAudio.pavucontrol')) {
|
|
|
|
showInput = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-16 18:53:14 -04:00
|
|
|
|
|
|
|
this._inputTitle.actor.visible = showInput;
|
|
|
|
this._inputSlider.actor.visible = showInput;
|
2010-07-22 20:39:44 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_sliderChanged: function(slider, value, property) {
|
|
|
|
if (this[property] == null) {
|
|
|
|
log ('Volume slider changed for %s, but %s does not exist'.format(property, property));
|
|
|
|
return;
|
|
|
|
}
|
2011-08-31 06:46:57 -04:00
|
|
|
let volume = value * this._volumeMax;
|
2010-11-16 09:22:38 -05:00
|
|
|
let prev_muted = this[property].is_muted;
|
|
|
|
if (volume < 1) {
|
|
|
|
this[property].volume = 0;
|
|
|
|
if (!prev_muted)
|
|
|
|
this[property].change_is_muted(true);
|
|
|
|
} else {
|
|
|
|
this[property].volume = volume;
|
|
|
|
if (prev_muted)
|
|
|
|
this[property].change_is_muted(false);
|
|
|
|
}
|
2010-07-22 20:39:44 -04:00
|
|
|
this[property].push_volume();
|
|
|
|
},
|
|
|
|
|
|
|
|
_notifyVolumeChange: function() {
|
2011-02-15 13:23:36 -05:00
|
|
|
global.cancel_theme_sound(VOLUME_NOTIFY_ID);
|
|
|
|
global.play_theme_sound(VOLUME_NOTIFY_ID, 'audio-volume-change');
|
2010-07-22 20:39:44 -04:00
|
|
|
},
|
|
|
|
|
2012-12-19 22:02:04 -05:00
|
|
|
getIcon: function() {
|
|
|
|
if (!this._output)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let volume = this._output.volume;
|
|
|
|
if (this._output.is_muted || volume <= 0) {
|
|
|
|
return 'audio-volume-muted-symbolic';
|
|
|
|
} else {
|
|
|
|
let n = Math.floor(3 * volume / this._volumeMax) + 1;
|
|
|
|
if (n < 2)
|
|
|
|
return 'audio-volume-low-symbolic';
|
|
|
|
if (n >= 3)
|
|
|
|
return 'audio-volume-high-symbolic';
|
|
|
|
return 'audio-volume-medium-symbolic';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-12-19 22:04:29 -05:00
|
|
|
_updateVolume: function(object, param_spec, property) {
|
2010-07-22 20:39:44 -04:00
|
|
|
let muted = this[property].is_muted;
|
2010-11-16 09:22:38 -05:00
|
|
|
let slider = this[property+'Slider'];
|
2011-08-31 06:46:57 -04:00
|
|
|
slider.setValue(muted ? 0 : (this[property].volume / this._volumeMax));
|
2012-12-19 22:02:04 -05:00
|
|
|
if (property == '_output')
|
|
|
|
this.emit('icon-changed');
|
2012-08-26 10:05:46 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Indicator = new Lang.Class({
|
|
|
|
Name: 'VolumeIndicator',
|
|
|
|
Extends: PanelMenu.SystemStatusButton,
|
|
|
|
|
|
|
|
_init: function() {
|
2012-08-30 15:53:25 -04:00
|
|
|
this.parent('audio-volume-muted-symbolic', _("Volume"));
|
2012-08-26 10:05:46 -04:00
|
|
|
|
|
|
|
this._control = getMixerControl();
|
|
|
|
this._volumeMenu = new VolumeMenu(this._control);
|
2012-12-19 22:02:04 -05:00
|
|
|
this._volumeMenu.connect('icon-changed', Lang.bind(this, function(menu) {
|
|
|
|
let icon = this._volumeMenu.getIcon();
|
|
|
|
this._hasPulseAudio = icon != null;
|
2012-08-26 10:05:46 -04:00
|
|
|
this.setIcon(icon);
|
|
|
|
this._syncVisibility();
|
|
|
|
}));
|
2012-10-18 13:42:17 -04:00
|
|
|
this._volumeMenu.connect('headphones-changed', Lang.bind(this, function() {
|
|
|
|
this._syncVisibility();
|
|
|
|
}));
|
|
|
|
|
|
|
|
this._headphoneIcon = this.addIcon(new Gio.ThemedIcon({ name: 'headphones-symbolic' }));
|
|
|
|
this._headphoneIcon.visible = false;
|
2012-08-26 10:05:46 -04:00
|
|
|
|
|
|
|
this.menu.addMenuItem(this._volumeMenu);
|
|
|
|
|
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
this.menu.addSettingsAction(_("Sound Settings"), 'gnome-sound-panel.desktop');
|
|
|
|
|
|
|
|
this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
|
|
|
|
},
|
|
|
|
|
|
|
|
_syncVisibility: function() {
|
2012-09-01 08:42:53 -04:00
|
|
|
this.actor.visible = this._hasPulseAudio;
|
2012-12-19 22:45:16 -05:00
|
|
|
this._headphoneIcon.visible = this._volumeMenu.hasHeadphones;
|
2012-08-26 10:05:46 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_onScrollEvent: function(actor, event) {
|
2012-11-04 10:53:23 -05:00
|
|
|
this._volumeMenu.scroll(event);
|
2010-07-22 20:39:44 -04:00
|
|
|
}
|
2011-11-20 09:38:48 -05:00
|
|
|
});
|