| 1 |
/* |
| 2 |
* This file is part of budgie-desktop |
| 3 |
* |
| 4 |
* Copyright Budgie Desktop Developers |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
public class MediaControlsRavenPlugin : Budgie.RavenPlugin, Peas.ExtensionBase { |
| 13 |
public Budgie.RavenWidget new_widget_instance(string uuid, GLib.Settings? settings) { |
| 14 |
return new MediaControlsRavenWidget(uuid, settings); |
| 15 |
} |
| 16 |
|
| 17 |
public bool supports_settings() { |
| 18 |
return false; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public class MediaControlsRavenWidget : Budgie.RavenWidget { |
| 23 |
private MprisDBusImpl impl; |
| 24 |
|
| 25 |
private HashTable<string, MprisClientWidget> ifaces; |
| 26 |
private Gtk.Box? content = null; |
| 27 |
private StartListening? placeholder = null; |
| 28 |
|
| 29 |
private int our_width = 250; |
| 30 |
|
| 31 |
public MediaControlsRavenWidget(string uuid, GLib.Settings? settings) { |
| 32 |
initialize(uuid, settings); |
| 33 |
|
| 34 |
content = new Gtk.Box(Gtk.Orientation.VERTICAL, 8); |
| 35 |
add(content); |
| 36 |
|
| 37 |
placeholder = new StartListening(); |
| 38 |
placeholder.get_style_context().add_class("raven-header"); |
| 39 |
content.pack_start(placeholder, false, false, 0); |
| 40 |
|
| 41 |
ifaces = new HashTable<string, MprisClientWidget>(str_hash, str_equal); |
| 42 |
|
| 43 |
setup_dbus.begin(); |
| 44 |
|
| 45 |
size_allocate.connect(on_size_allocate); |
| 46 |
show_all(); |
| 47 |
} |
| 48 |
|
| 49 |
void on_size_allocate() { |
| 50 |
int w = get_allocated_width(); |
| 51 |
if (w > our_width) { |
| 52 |
our_width = w; |
| 53 |
|
| 54 |
// Notify every client of the updated size. Idle needs to be used |
| 55 |
// to prevent any 'queue_resize' triggered from being ignored |
| 56 |
Idle.add(notify_clients_on_width_change); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
bool notify_clients_on_width_change() { |
| 61 |
var iter = HashTableIter<string,MprisClientWidget>(ifaces); |
| 62 |
MprisClientWidget? widget = null; |
| 63 |
while (iter.next(null, out widget)) { |
| 64 |
widget.update_width(our_width); |
| 65 |
} |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add an interface handler/widget to known list and UI |
| 71 |
* |
| 72 |
* @param name DBUS name (object path) |
| 73 |
* @param iface The constructed MprisClient instance |
| 74 |
*/ |
| 75 |
void add_iface(string name, MprisClient iface) { |
| 76 |
MprisClientWidget widg = new MprisClientWidget(iface, our_width); |
| 77 |
widg.show_all(); |
| 78 |
if (content.get_children().index(placeholder) != -1) { |
| 79 |
content.remove(placeholder); |
| 80 |
} |
| 81 |
content.pack_start(widg, false, false, 0); |
| 82 |
ifaces.insert(name, widg); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Destroy an interface handler and remove from UI |
| 87 |
* |
| 88 |
* @param name DBUS name to remove handler for |
| 89 |
*/ |
| 90 |
void destroy_iface(string name) { |
| 91 |
var widg = ifaces[name]; |
| 92 |
if (widg != null) { |
| 93 |
content.remove(widg); |
| 94 |
ifaces.remove(name); |
| 95 |
} |
| 96 |
|
| 97 |
if (ifaces.size() == 0) { |
| 98 |
content.pack_start(placeholder, false, false, 0); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void on_name_owner_changed(string? n, string? o, string? ne) { |
| 103 |
if (!n.has_prefix("org.mpris.MediaPlayer2.")) { |
| 104 |
return; |
| 105 |
} |
| 106 |
if (o == "") { |
| 107 |
new_iface.begin(n, (o, r) => { |
| 108 |
var iface = new_iface.end(r); |
| 109 |
if (iface != null) { |
| 110 |
add_iface(n, iface); |
| 111 |
} |
| 112 |
}); |
| 113 |
} else { |
| 114 |
Idle.add(() => { |
| 115 |
destroy_iface(n); |
| 116 |
return false; |
| 117 |
}); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Do basic dbus initialisation |
| 123 |
*/ |
| 124 |
public async void setup_dbus() { |
| 125 |
try { |
| 126 |
impl = yield Bus.get_proxy(BusType.SESSION, "org.freedesktop.DBus", "/org/freedesktop/DBus"); |
| 127 |
var names = yield impl.list_names(); |
| 128 |
|
| 129 |
/* Search for existing players (launched prior to our start) */ |
| 130 |
foreach (var name in names) { |
| 131 |
if (name.has_prefix("org.mpris.MediaPlayer2.")) { |
| 132 |
var iface = yield new_iface(name); |
| 133 |
if (iface != null) { |
| 134 |
add_iface(name, iface); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/* Also check for new mpris clients coming up while we're up */ |
| 140 |
impl.name_owner_changed.connect(on_name_owner_changed); |
| 141 |
} catch (Error e) { |
| 142 |
warning("Failed to initialise dbus: %s", e.message); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private class StartListening : Gtk.Box { |
| 148 |
private AppInfo? music_app = null; // Our current default music player that handles audio/ogg |
| 149 |
private bool has_music_player; // Private bool of whether or not we have a music player installed |
| 150 |
private Gtk.Button start_listening; // Our button to start listening to music |
| 151 |
|
| 152 |
public StartListening() { |
| 153 |
Object(orientation: Gtk.Orientation.VERTICAL, spacing: 8); |
| 154 |
|
| 155 |
var label = new Gtk.Label(_("No apps are currently playing audio.")) { |
| 156 |
wrap = true, |
| 157 |
wrap_mode = Pango.WrapMode.WORD_CHAR, |
| 158 |
max_width_chars = 1, |
| 159 |
justify = Gtk.Justification.CENTER, |
| 160 |
hexpand = true, |
| 161 |
}; |
| 162 |
label.margin_top = 4; |
| 163 |
|
| 164 |
start_listening = new Gtk.Button.with_label(_("Play some music")); |
| 165 |
start_listening.halign = Gtk.Align.CENTER; |
| 166 |
start_listening.margin_bottom = 4; |
| 167 |
start_listening.hexpand = false; |
| 168 |
|
| 169 |
pack_start(label, false, false, 0); |
| 170 |
pack_start(start_listening, false, false, 0); |
| 171 |
|
| 172 |
var monitor = AppInfoMonitor.get(); // Get our AppInfoMonitor, which monitors the app info database for changes |
| 173 |
monitor.changed.connect(check_music_support); // Recheck music support |
| 174 |
|
| 175 |
start_listening.clicked.connect(launch_music_player); |
| 176 |
|
| 177 |
check_music_support(); // Do our initial check |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* check_music_support will check if we have an application that supports vorbis. |
| 182 |
* We're checking for vorbis since it's more likely the end user has open source vorbis support than alternative codecs like MP3 |
| 183 |
*/ |
| 184 |
private void check_music_support() { |
| 185 |
music_app = AppInfo.get_default_for_type("audio/vorbis", false); |
| 186 |
has_music_player = (music_app != null); |
| 187 |
start_listening.set_visible(has_music_player); // Set the visibility of the button based on if we have a music player |
| 188 |
} |
| 189 |
|
| 190 |
private void launch_music_player() { |
| 191 |
if (music_app == null) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
try { |
| 196 |
music_app.launch(null, null); |
| 197 |
} catch (Error e) { |
| 198 |
warning("Unable to launch %s: %s", music_app.get_name(), e.message); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
[ModuleInit] |
| 204 |
public void peas_register_types(TypeModule module) { |
| 205 |
// boilerplate - all modules need this |
| 206 |
var objmodule = module as Peas.ObjectModule; |
| 207 |
objmodule.register_extension_type(typeof(Budgie.RavenPlugin), typeof(MediaControlsRavenPlugin)); |
| 208 |
} |