Created settings schema.
[AdvancedVolumeMixer.git] / extension.js
blob77aaef5c32e5bcae247d32e4e69734af39d63781
1 // extension.js
2 // vi: et sw=2
3 //
4 // Advanced Volume Mixer
5 // Control programs' volume from gnome volume mixer applet.
6 //
7 // Author: Harry Karvonen <harry.karvonen@gmail.com>
8 //
10 const Clutter = imports.gi.Clutter;
11 const Lang = imports.lang;
12 const Gvc = imports.gi.Gvc;
13 const Signals = imports.signals;
14 const St = imports.gi.St;
16 const Main = imports.ui.main;
17 const PopupMenu = imports.ui.popupMenu;
20 let advMixer;
23 function AdvPopupSwitchMenuItem() {
24   this._init.apply(this, arguments);
28 AdvPopupSwitchMenuItem.prototype = {
29   __proto__: PopupMenu.PopupSwitchMenuItem.prototype,
31   _init: function(text, active, gicon, params) {
32     PopupMenu.PopupSwitchMenuItem.prototype._init.call(
33       this,
34       " " + text + "  ",
35       active,
36       params
37     );
39     this._icon = new St.Icon({
40       gicon:        gicon,
41       style_class: "adv-volume-icon"
42     });
44     // Rebuild switch
45     this.removeActor(this._statusBin);
46     this.removeActor(this.label)
48     // Horizontal box
49     let labelBox = new St.BoxLayout({vertical: false});
51     labelBox.add(this._icon,
52                 {expand: false, x_fill: false, x_align: St.Align.START});
53     labelBox.add(this.label,
54                  {expand: false, x_fill: false, x_align: St.Align.START});
55     labelBox.add(this._statusBin,
56                  {expand: true, x_fill: true, x_align: St.Align.END});
57             
58     this.addActor(labelBox, {span: -1, expand: true });
59   }
63 function AdvMixer(mixer) {
64   this._init(mixer);
68 AdvMixer.prototype = {
69   _init: function(mixer) {
70     this._mixer = mixer;
71     this._control = mixer._control;
72     this._separator = new PopupMenu.PopupSeparatorMenuItem();
73     this._items = {};
74     this._outputs = {};
75     this._outputMenu = new PopupMenu.PopupSubMenuMenuItem(_("Volume"));
77     this._mixer.menu.addMenuItem(this._separator, 1);
79     this._streamAddedId = this._control.connect(
80       "stream-added",
81       Lang.bind(this, this._streamAdded)
82     );
83     this._streamRemovedId = this._control.connect(
84       "stream-removed",
85       Lang.bind(this, this._streamRemoved)
86     );
87     this._defaultSinkChangedId = this._control.connect(
88       "default-sink-changed",
89       Lang.bind(this, this._defaultSinkChanged)
90     );
92     // Change Volume title
93     let title = this._mixer._volumeMenu.firstMenuItem.firstMenuItem;
94     title.destroy();
96     this._mixer._volumeMenu.firstMenuItem.addMenuItem(this._outputMenu, 0);
97     this._outputMenu.actor.show();
99     // Add streams
100     let streams = this._control.get_streams();
101     for (let i = 0; i < streams.length; i++) {
102       this._streamAdded(this._control, streams[i].id);
103     }
105     if (this._control.get_default_sink() != null) {
106       this._defaultSinkChanged(
107         this._control,
108         this._control.get_default_sink().id
109       );
110     }
111   },
114   _streamAdded: function(control, id) {
115     if (id in this._items) {
116       return;
117     }
119     if (id in this._outputs) {
120       return;
121     }
123     let stream = control.lookup_stream_id(id);
125     if (stream["is-event-stream"]) {
126       // Do nothing
127     } else if (stream instanceof Gvc.MixerSinkInput) {
128       let slider = new PopupMenu.PopupSliderMenuItem(
129         stream.volume / this._control.get_vol_max_norm()
130       );
131       let title = new AdvPopupSwitchMenuItem(
132         stream.name || stream.description,
133         !stream.is_muted,
134         stream.get_gicon(),
135         {activate: false}
136       );
138       this._items[id] = {
139         slider: slider,
140         title: title
141       };
143       slider.connect(
144         "value-changed",
145         Lang.bind(this, this._sliderValueChanged, stream.id)
146       );
148       title.actor.connect(
149         "button-release-event",
150         Lang.bind(this, this._titleToggleState, stream.id)
151       );
153       title.actor.connect(
154         "key-press-event",
155         Lang.bind(this, this._titleToggleState, stream.id)
156       );
158       stream.connect(
159         "notify::volume",
160         Lang.bind(this, this._notifyVolume, stream.id)
161       );
163       stream.connect(
164         "notify::is-muted",
165         Lang.bind(this, this._notifyIsMuted, stream.id)
166       );
168       this._mixer.menu.addMenuItem(this._items[id]["slider"], 2);
169       this._mixer.menu.addMenuItem(this._items[id]["title"], 2);
170     } else if (stream instanceof Gvc.MixerSink) {
171       let output = new PopupMenu.PopupMenuItem(stream.description);
173       output.connect(
174         "activate",
175         function (item, event) { control.set_default_sink(stream); }
176       );
178       this._outputMenu.menu.addMenuItem(output);
180       this._outputs[id] = output;
181     }
182   },
184   _streamRemoved: function(control, id) {
185     if (id in this._items) {
186       this._items[id]["slider"].destroy();
187       this._items[id]["title"].destroy();
188       delete this._items[id];
189     }
191     if (id in this._outputs) {
192       this._outputs[id].destroy();
193       delete this._outputs[id];
194     }
195   },
197   _defaultSinkChanged: function(control, id) {
198     for (let output in this._outputs) {
199       this._outputs[output].setShowDot(output == id);
200     }
201   },
203   _sliderValueChanged: function(slider, value, id) {
204     let stream = this._control.lookup_stream_id(id);
205     let volume = value * this._control.get_vol_max_norm();
207     stream.volume = volume;
208     stream.push_volume();
209   },
211   _titleToggleState: function(title, event, id) {
212     if (event.type() == Clutter.EventType.KEY_PRESS) {
213       let symbol = event.get_key_symbol();
215       if (symbol != Clutter.KEY_space && symbol != Clutter.KEY_Return) {
216         return false;
217       }
218     }
220     let stream = this._control.lookup_stream_id(id);
222     stream.change_is_muted(!stream.is_muted);
224     return true;
225   },
227   _notifyVolume: function(object, param_spec, id) {
228     let stream = this._control.lookup_stream_id(id);
230     this._items[id]["slider"].setValue(stream.volume / this._control.get_vol_max_norm());
231   },
233   _notifyIsMuted: function(object, param_spec, id) {
234     let stream = this._control.lookup_stream_id(id);
236     this._items[id]["title"].setToggleState(!stream.is_muted);
237   },
239   destroy: function() {
240     this._control.disconnect(this._streamAddedId);
241     this._control.disconnect(this._streamRemovedId);
242     this._control.disconnect(this._defaultSinkChangedId);
244     this._separator.destroy();
245     delete this._separator;
247     // Restore Volume label
248     this._outputMenu.destroy();
249     delete this._outputMenu;
251     let title = new PopupMenu.PopupMenuItem(_("Volume"), {reactive: false });
252     this._mixer._volumeMenu.firstMenuItem.addMenuItem(title, 0);
253     title.actor.show();
255     // remove application streams
256     for (let id in this._items) {
257       this._streamRemoved(this._control, id);
258     }
260     this.emit("destroy");
261   }
265 Signals.addSignalMethods(AdvMixer.prototype);
268 function main() {
269   init();
270   enable();
274 function init() {
278 function enable() {
279   if (Main.panel.statusArea['volume'] && !advMixer) {
280     advMixer = new AdvMixer(Main.panel.statusArea["volume"]);
281   }
285 function disable() {
286   if (advMixer) {
287     advMixer.destroy();
288     advMixer = null;
289   }