2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2010-10-20 19:53:23 +00:00
|
|
|
const Clutter = imports.gi.Clutter;
|
2010-07-23 00:39:44 +00:00
|
|
|
const Lang = imports.lang;
|
2012-10-18 17:42:17 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
2010-07-23 00:39:44 +00:00
|
|
|
const Gvc = imports.gi.Gvc;
|
|
|
|
const St = imports.gi.St;
|
2012-12-20 02:57:26 +00:00
|
|
|
const Signals = imports.signals;
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2017-06-09 14:06:39 +00:00
|
|
|
const Main = imports.ui.main;
|
2010-07-23 00:39:44 +00:00
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
2013-04-23 20:57:43 +00:00
|
|
|
const Slider = imports.ui.slider;
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2017-07-18 17:47:27 +00:00
|
|
|
var VOLUME_NOTIFY_ID = 1;
|
2011-02-15 18:23:36 +00:00
|
|
|
|
2012-08-26 14:05:46 +00: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-23 00:39:44 +00:00
|
|
|
|
2012-08-26 14:05:46 +00:00
|
|
|
_mixerControl = new Gvc.MixerControl({ name: 'GNOME Shell Volume Control' });
|
|
|
|
_mixerControl.open();
|
|
|
|
|
|
|
|
return _mixerControl;
|
|
|
|
}
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var StreamSlider = new Lang.Class({
|
2012-12-20 02:57:26 +00:00
|
|
|
Name: 'StreamSlider',
|
2012-08-26 14:05:46 +00:00
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
_init: function(control) {
|
2012-12-20 02:57:26 +00:00
|
|
|
this._control = control;
|
2012-08-26 14:05:46 +00:00
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
this.item = new PopupMenu.PopupBaseMenuItem({ activate: false });
|
2012-10-18 17:42:17 +00:00
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
this._slider = new Slider.Slider(0);
|
2012-12-20 02:57:26 +00:00
|
|
|
this._slider.connect('value-changed', Lang.bind(this, this._sliderChanged));
|
2012-12-21 18:28:40 +00:00
|
|
|
this._slider.connect('drag-end', Lang.bind(this, this._notifyVolumeChange));
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
this._icon = new St.Icon({ style_class: 'popup-menu-icon' });
|
2013-07-16 00:00:41 +00:00
|
|
|
this.item.actor.add(this._icon);
|
|
|
|
this.item.actor.add(this._slider.actor, { expand: true });
|
2013-06-12 03:39:56 +00:00
|
|
|
this.item.actor.connect('button-press-event', Lang.bind(this, function(actor, event) {
|
2013-11-29 18:17:34 +00:00
|
|
|
return this._slider.startDragging(event);
|
2013-06-12 03:39:56 +00:00
|
|
|
}));
|
2013-08-22 11:15:00 +00:00
|
|
|
this.item.actor.connect('key-press-event', Lang.bind(this, function(actor, event) {
|
|
|
|
return this._slider.onKeyPressEvent(actor, event);
|
|
|
|
}));
|
2013-06-12 03:39:56 +00:00
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
this._stream = null;
|
|
|
|
},
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
get stream() {
|
|
|
|
return this._stream;
|
|
|
|
},
|
2012-09-04 15:55:26 +00:00
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
set stream(stream) {
|
|
|
|
if (this._stream) {
|
|
|
|
this._disconnectStream(this._stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._stream = stream;
|
|
|
|
|
|
|
|
if (this._stream) {
|
|
|
|
this._connectStream(this._stream);
|
|
|
|
this._updateVolume();
|
|
|
|
} else {
|
|
|
|
this.emit('stream-updated');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateVisibility();
|
|
|
|
},
|
|
|
|
|
|
|
|
_disconnectStream: function(stream) {
|
|
|
|
stream.disconnect(this._mutedChangedId);
|
|
|
|
this._mutedChangedId = 0;
|
|
|
|
stream.disconnect(this._volumeChangedId);
|
|
|
|
this._volumeChangedId = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
_connectStream: function(stream) {
|
|
|
|
this._mutedChangedId = stream.connect('notify::is-muted', Lang.bind(this, this._updateVolume));
|
|
|
|
this._volumeChangedId = stream.connect('notify::volume', Lang.bind(this, this._updateVolume));
|
|
|
|
},
|
|
|
|
|
|
|
|
_shouldBeVisible: function() {
|
|
|
|
return this._stream != null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateVisibility: function() {
|
|
|
|
let visible = this._shouldBeVisible();
|
2013-04-23 20:57:43 +00:00
|
|
|
this.item.actor.visible = visible;
|
2012-05-22 22:27:06 +00:00
|
|
|
},
|
|
|
|
|
2012-11-04 15:53:23 +00:00
|
|
|
scroll: function(event) {
|
2013-11-29 18:17:34 +00:00
|
|
|
return this._slider.scroll(event);
|
2010-10-20 19:53:23 +00:00
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
setValue: function(value) {
|
|
|
|
// piggy-back off of sliderChanged
|
|
|
|
this._slider.setValue(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
_sliderChanged: function(slider, value, property) {
|
|
|
|
if (!this._stream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let volume = value * this._control.get_vol_max_norm();
|
|
|
|
let prevMuted = this._stream.is_muted;
|
|
|
|
if (volume < 1) {
|
|
|
|
this._stream.volume = 0;
|
|
|
|
if (!prevMuted)
|
|
|
|
this._stream.change_is_muted(true);
|
2011-03-28 14:10:11 +00:00
|
|
|
} else {
|
2012-12-20 02:57:26 +00:00
|
|
|
this._stream.volume = volume;
|
|
|
|
if (prevMuted)
|
|
|
|
this._stream.change_is_muted(false);
|
|
|
|
}
|
|
|
|
this._stream.push_volume();
|
|
|
|
},
|
|
|
|
|
2012-12-21 18:28:40 +00:00
|
|
|
_notifyVolumeChange: function() {
|
|
|
|
global.cancel_theme_sound(VOLUME_NOTIFY_ID);
|
2012-11-04 18:53:49 +00:00
|
|
|
global.play_theme_sound(VOLUME_NOTIFY_ID,
|
|
|
|
'audio-volume-change',
|
|
|
|
_("Volume changed"),
|
|
|
|
Clutter.get_current_event ());
|
2012-12-21 18:28:40 +00:00
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_updateVolume: function() {
|
|
|
|
let muted = this._stream.is_muted;
|
|
|
|
this._slider.setValue(muted ? 0 : (this._stream.volume / this._control.get_vol_max_norm()));
|
|
|
|
this.emit('stream-updated');
|
|
|
|
},
|
|
|
|
|
|
|
|
getIcon: function() {
|
|
|
|
if (!this._stream)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let volume = this._stream.volume;
|
|
|
|
if (this._stream.is_muted || volume <= 0) {
|
|
|
|
return 'audio-volume-muted-symbolic';
|
|
|
|
} else {
|
|
|
|
let n = Math.floor(3 * volume / this._control.get_vol_max_norm()) + 1;
|
|
|
|
if (n < 2)
|
|
|
|
return 'audio-volume-low-symbolic';
|
|
|
|
if (n >= 3)
|
|
|
|
return 'audio-volume-high-symbolic';
|
|
|
|
return 'audio-volume-medium-symbolic';
|
2011-03-28 14:10:11 +00:00
|
|
|
}
|
2017-06-09 14:06:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getLevel: function() {
|
|
|
|
if (!this._stream)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return 100 * this._stream.volume / this._control.get_vol_max_norm();
|
2012-12-20 02:57:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Signals.addSignalMethods(StreamSlider.prototype);
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var OutputStreamSlider = new Lang.Class({
|
2012-12-20 02:57:26 +00:00
|
|
|
Name: 'OutputStreamSlider',
|
|
|
|
Extends: StreamSlider,
|
|
|
|
|
2013-08-21 16:24:30 +00:00
|
|
|
_init: function(control) {
|
|
|
|
this.parent(control);
|
|
|
|
this._slider.actor.accessible_name = _("Volume");
|
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_connectStream: function(stream) {
|
|
|
|
this.parent(stream);
|
|
|
|
this._portChangedId = stream.connect('notify::port', Lang.bind(this, this._portChanged));
|
|
|
|
this._portChanged();
|
2010-07-23 00:39:44 +00:00
|
|
|
},
|
|
|
|
|
2012-10-18 17:42:17 +00: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
|
2013-02-01 19:19:02 +00:00
|
|
|
if (sink.get_ports().length > 0)
|
|
|
|
return sink.get_port().port.indexOf('headphone') >= 0;
|
2012-10-18 17:42:17 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_disconnectStream: function(stream) {
|
|
|
|
this.parent(stream);
|
|
|
|
stream.disconnect(this._portChangedId);
|
|
|
|
this._portChangedId = 0;
|
2012-10-18 17:42:17 +00:00
|
|
|
},
|
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
_updateSliderIcon: function() {
|
|
|
|
this._icon.icon_name = (this._hasHeadphones ?
|
|
|
|
'audio-headphones-symbolic' :
|
|
|
|
'audio-speakers-symbolic');
|
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_portChanged: function() {
|
|
|
|
let hasHeadphones = this._findHeadphones(this._stream);
|
|
|
|
if (hasHeadphones != this._hasHeadphones) {
|
|
|
|
this._hasHeadphones = hasHeadphones;
|
2013-04-23 20:57:43 +00:00
|
|
|
this._updateSliderIcon();
|
2010-07-23 00:39:44 +00:00
|
|
|
}
|
2012-12-20 02:57:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var InputStreamSlider = new Lang.Class({
|
2012-12-20 02:57:26 +00:00
|
|
|
Name: 'InputStreamSlider',
|
|
|
|
Extends: StreamSlider,
|
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
_init: function(control) {
|
|
|
|
this.parent(control);
|
2013-08-21 16:24:30 +00:00
|
|
|
this._slider.actor.accessible_name = _("Microphone");
|
2012-12-20 02:57:26 +00:00
|
|
|
this._control.connect('stream-added', Lang.bind(this, this._maybeShowInput));
|
|
|
|
this._control.connect('stream-removed', Lang.bind(this, this._maybeShowInput));
|
2013-04-23 20:57:43 +00:00
|
|
|
this._icon.icon_name = 'audio-input-microphone-symbolic';
|
2010-07-23 00:39:44 +00:00
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_connectStream: function(stream) {
|
|
|
|
this.parent(stream);
|
|
|
|
this._maybeShowInput();
|
2010-07-23 00:39:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_maybeShowInput: function() {
|
|
|
|
// only show input widgets if any application is recording audio
|
|
|
|
let showInput = false;
|
|
|
|
let recordingApps = this._control.get_source_outputs();
|
2012-12-20 02:57:26 +00:00
|
|
|
if (this._stream && recordingApps) {
|
2010-10-20 19:15:24 +00:00
|
|
|
for (let i = 0; i < recordingApps.length; i++) {
|
|
|
|
let outputStream = recordingApps[i];
|
2010-07-23 00:39:44 +00: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 22:53:14 +00:00
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
this._showInput = showInput;
|
|
|
|
this._updateVisibility();
|
2010-07-23 00:39:44 +00:00
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_shouldBeVisible: function() {
|
|
|
|
return this.parent() && this._showInput;
|
|
|
|
}
|
|
|
|
});
|
2010-07-23 00:39:44 +00:00
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var VolumeMenu = new Lang.Class({
|
2012-12-20 02:57:26 +00:00
|
|
|
Name: 'VolumeMenu',
|
|
|
|
Extends: PopupMenu.PopupMenuSection,
|
|
|
|
|
|
|
|
_init: function(control) {
|
|
|
|
this.parent();
|
|
|
|
|
|
|
|
this.hasHeadphones = false;
|
|
|
|
|
|
|
|
this._control = control;
|
|
|
|
this._control.connect('state-changed', Lang.bind(this, this._onControlStateChanged));
|
|
|
|
this._control.connect('default-sink-changed', Lang.bind(this, this._readOutput));
|
|
|
|
this._control.connect('default-source-changed', Lang.bind(this, this._readInput));
|
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
this._output = new OutputStreamSlider(this._control);
|
2012-12-20 02:57:26 +00:00
|
|
|
this._output.connect('stream-updated', Lang.bind(this, function() {
|
|
|
|
this.emit('icon-changed');
|
|
|
|
}));
|
|
|
|
this.addMenuItem(this._output.item);
|
|
|
|
|
2013-04-23 20:57:43 +00:00
|
|
|
this._input = new InputStreamSlider(this._control);
|
2012-12-20 02:57:26 +00:00
|
|
|
this.addMenuItem(this._input.item);
|
|
|
|
|
|
|
|
this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
|
|
|
|
this._onControlStateChanged();
|
2010-07-23 00:39:44 +00:00
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
scroll: function(event) {
|
2013-11-29 18:17:34 +00:00
|
|
|
return this._output.scroll(event);
|
2012-12-20 02:57:26 +00:00
|
|
|
},
|
2012-12-20 03:02:04 +00:00
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_onControlStateChanged: function() {
|
|
|
|
if (this._control.get_state() == Gvc.MixerControlState.READY) {
|
|
|
|
this._readInput();
|
|
|
|
this._readOutput();
|
2012-12-20 03:02:04 +00:00
|
|
|
} else {
|
2012-12-20 02:57:26 +00:00
|
|
|
this.emit('icon-changed');
|
2012-12-20 03:02:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-12-20 02:57:26 +00:00
|
|
|
_readOutput: function() {
|
|
|
|
this._output.stream = this._control.get_default_sink();
|
|
|
|
},
|
|
|
|
|
|
|
|
_readInput: function() {
|
|
|
|
this._input.stream = this._control.get_default_source();
|
|
|
|
},
|
|
|
|
|
|
|
|
getIcon: function() {
|
|
|
|
return this._output.getIcon();
|
2017-06-09 14:06:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getLevel: function() {
|
|
|
|
return this._output.getLevel();
|
2012-08-26 14:05:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-18 17:41:25 +00:00
|
|
|
var Indicator = new Lang.Class({
|
2012-08-26 14:05:46 +00:00
|
|
|
Name: 'VolumeIndicator',
|
2013-06-06 21:27:25 +00:00
|
|
|
Extends: PanelMenu.SystemIndicator,
|
2012-08-26 14:05:46 +00:00
|
|
|
|
|
|
|
_init: function() {
|
2013-06-06 21:27:25 +00:00
|
|
|
this.parent();
|
|
|
|
|
2013-07-19 10:05:47 +00:00
|
|
|
this._primaryIndicator = this._addIndicator();
|
2012-08-26 14:05:46 +00:00
|
|
|
|
|
|
|
this._control = getMixerControl();
|
|
|
|
this._volumeMenu = new VolumeMenu(this._control);
|
2012-12-20 03:02:04 +00:00
|
|
|
this._volumeMenu.connect('icon-changed', Lang.bind(this, function(menu) {
|
|
|
|
let icon = this._volumeMenu.getIcon();
|
2013-06-06 21:27:25 +00:00
|
|
|
|
|
|
|
if (icon != null) {
|
|
|
|
this.indicators.show();
|
|
|
|
this._primaryIndicator.icon_name = icon;
|
|
|
|
} else {
|
|
|
|
this.indicators.hide();
|
|
|
|
}
|
2012-08-26 14:05:46 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
this.menu.addMenuItem(this._volumeMenu);
|
|
|
|
|
2013-06-06 21:27:25 +00:00
|
|
|
this.indicators.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
|
2012-08-26 14:05:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onScrollEvent: function(actor, event) {
|
2017-06-09 14:06:39 +00:00
|
|
|
let result = this._volumeMenu.scroll(event);
|
|
|
|
if (result == Clutter.EVENT_PROPAGATE || this.menu.actor.mapped)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
let gicon = new Gio.ThemedIcon({ name: this._volumeMenu.getIcon() });
|
|
|
|
let level = this._volumeMenu.getLevel();
|
|
|
|
Main.osdWindowManager.show(-1, gicon, null, level);
|
|
|
|
return result;
|
2010-07-23 00:39:44 +00:00
|
|
|
}
|
2011-11-20 14:38:48 +00:00
|
|
|
});
|