[Technology] Writing a Rhythmbox Plugin: Rhythmbox DJ

I don't like being anchored to my computer, so I want it to communicate more information to me while I'm doing something else at home.  When music is playing and I want to know the artist and track name of a song, I want Rhythmbox to announce that aloud.

I previously wrote djaqua which did this, but was written in Python.  That bit rotted for me, and rather than updating it, I took an opportunity to try writing a Rhythmbox plugin in Vala.  I really like Vala.  Despite bugs, it works surprisingly well for what it tries to do, and it helps structure and simplify so much in GNOME development.  The biggest challenge I encounter with it is availability of existing bindings, which I think is important to persuading other developers to consider Vala (and GNOME), however, I can figure out how to wrap them for the most part.

The source code can currently be found here:


Disclaimer: I am neither a vala nor a Rhythmbox expert, so I may give lots of bad advice below, so please send in corrections!  For now, this has worked for me, though.

For a Rhythmbox plugin based on libpeas we want

  • a .plugin file describing it: generated from djaqua.plugin.in

  • source code: rb-djaqua-plugin.vala

  • a Makefile: generated from Makefile.am

For Rhythmbox DJ, we'll also add

  • a PeasGtkConfigurable configure window, built from a GtkBuilder file: djaqua.ui

  • a GSettings schema: org.gnome.rhythmbox.plugins.djaqua.gschema.xml)

And to make up for some non-existing bindings, we'll add these .vapis

  • libpeas-gtk-1.0.vapi

  • libspeechd.vapi

Let's start with all the smaller files and finish with the source code last.

Plugin File

The plugin file is in a  .ini like format.  We have [sections] and key=value pairs.  We want one section, "[Plugin]", and the following keys, Module, IAge, Name, Description, Authors, Copyright, and Website.  (I'm not sure which are strictly necessary; let me know if you do ;).

To manage this, we write djaqua.plugin.in 

_Name=Rhythmbox DJ
_Description=Announces tracks at start and close
Authors=Richard Schwarting <aquarichy@gmail.com>, James Livingston <doclivingston@gmail.com>
Copyright=Copyright © 2012 Richard Schwarting, James Livingston

Name and Description will be visible in Rhythmbox's Plugins menu, and so we want them to be localisable, which is why we create an .in file and prefix two those fields with _.  Since I haven't actually done much localisation before, I may not have done this step correctly. :D


Cryptic autofoo here.  We will write a Makefile.am that will define many of the directories and flags we want to compile with.

These include directories,

plugindir: set to $(PLUGINDIR)/djaqua

plugindatadir: set to $(PLUGINDATADIR)/djaqua

gtkbuilderdir: optional, when using GtkBuilder for UI, set to $(plugindatadir)


plugin_LTLIBRARIES: points to our .la, libdjaqua.la

libdjaqua_la_SOURCES: set to our .vala source file

INCLUDES: set to a whole whack of things; some probably unnecessary :)

plugin_in_file: the plugin.in file that will generate our .plugin file

gsettings_SCHEMAS: optional if you use GSettings for preferences, ours is org.gnome.rhythmbox.plugins.djaqua.gschema.xml, which is a real file. :)

gtkbuilder_DATA: optional if using GtkBuilder, ours is djaqua.ui, and it's the XML file generated by Glade 3.

plugin_DATA: an expression evaluating to our .plugin file to be.

EXTRA_DIST: additional files to distribute, including our gtkbuilder_DATA file, our plugin files, and our GSettings schema.  I might be specifying the wrong plugin file?

CLEANFILES: files that you want removed during make clean, should be most generated files, maybe not all; I might be omitting some

DISTCLEANFILES: files that you want removed during make distclean, should be all generated files that you don't absolutely need.  Mine is just set to CLEANFILES right now. :)


libdjaqua_la_LDFLAGS: Flags for linking, I use PLUGIN_LIBTOOL_FLAGS and -lspeechd (since I use speechd :D)

libdjaqua_la_CFLAGS: Any compilation flags; just using WNOERROR_CFLAGS here. 

VALAFLAGS: Since my plugin is coded in vala, I have a bunch of flags I need to pass, like --vapidir's to find custom bindings, --pkg's to specify which packages/bindnigs I will use, and for my project, --thread, --target-glib

Some rules

@GSETTINGS_RULES@: this appears on its own line.

And some targets, like

%.plugin: includes a fancy rule that deals with internationalising the plugin description from the .plugin.in.  I actually don't do that yet.

%.c %.h: generate .c and .h files from the .vala source. 

Source code: in vala

The plugin system is based on libpeas, so our plugin will be a Peas plugin.  We're going to create a class named DJAquaPlugin that extends and implements Peas.ExtensionBase, and Peas.Activatable: class DJAquaPlugin : Peas.ExtensionBase, Peas.Activatable.  I believe the order there matters.

Later, we'll also implement the PeasGtk.Configurable interface.

Within our plugin class, we'll require:

a "object" member: public GLib.Object object { owned get; construct; } This will be set by Rhythmbox after constructing your object to an Rb.Shell object, so you can access it.

an "activate" method: public void activate ()  This will be called when a user first activates your plugin, and subsequently when Rhythmbox starts and activates it.  It should feature most setup.

a "deactivate" method: public void deactivate ()

an "update_state" method: public void update_state ().  I actually didn't require this, so mine is empty.

Outside of the class, as its own method, we'll want a ModuleInit method, peas_register_type:


public void peas_register_types (GLib.TypeModule module) {

  var objectmodule = module as Peas.ObjectModule;

  objmodule.register_extension_type (typeof (Peas.Activatable), typeof (DJAquaPlugin));


The most important required method will probably be "activate".  To interact with Rhythmbox, you'll want to use the "object" member.  I set it to a new RB.Shell variable, like "RB.Shell shell = (RB.Shell)this.object".  You may also want access to the RB.ShellPlayer, which is a property within RB.Shell, so you can use "RB.ShellPlayer shell_player; shell.get ("shell-player", out shell_player);" to get a reference to it.  From there, you can use their APIs to manipulate Rhythmbox and connect to their signals to interact with play.

In "deactivate", you'll want to clean up anything you set up in activate () that rhythmbox will no longer need.  In my case, I set up a connection to Speech Dispatcher which I will then want to remove.

Rhythmbox plugin configuration: Peas.Configurable

I wanted the user to be able to control a few settings for the plugin, mostly which voice they hear.  For that, your plugin should implement PeasGtk.Configurable.  This binding isn't shipped right now, so I created mine own, which I'll submit a patch for, and for now will be available with my code.

So, our class signature becomes this: 

class DJAquaPlugin : Peas.ExtensionBase, Peas.Activatable, PeasGtk.Configurable {

and we impement the required method:

public Gtk.Widget create_configure_widget ()

There are some important points:

  • every time you open the preferences, it calls create_configure_widget and expects a new widget

  • every time you close the preferences, it destroys the widget you had previously given it

  • therefore, you cannot be clever and save a reference to your widget once created and keep returning the same one; if you try, you'll encounter memory errors since your widget will have been unallocated.

  • the widget should not be a window itself (in Glade, you can create top level widgets that are not windows)

I used Glade to design my UI with something like a Gtk.VBox as the top level widget.  That worked fine. I created a separate object to handle all the code for preferences, giving logic to the menus, etc.  I used GSettings to bind preferences to, so both my main plugin could read them, and my preferences object could manipulate them.

NOTE: To be able to locate the GtkBuilder file, I had to use RB.find_plugin_data_file (), which I needed to add a binding more.  I'll submit a patch for that.  There's autofoo in the Makefile.am that ensures it gets distributed (EXTRA_DIST I think) and will be where find_plugin_data_file looks.

Rhythmbox plugin configuration: GSettings

GSettings requires a schema file be installed, as well as code to read and set your settings.  You should have a namespaced ID for your settings.  My namespaced ID is "org.gnome.rhythmbox.plugins.djaqua".  My file is pretty simple, and looks like this

  <schema id="org.gnome.rhythmbox.plugins.djaqua" path="/org/gnome/rhythmbox/plugins/djaqua/">
    <key name="output-module" type="s">
      <summary>Speech Engine</summary>
      <description>The installed system to synthesise voice for you.
      Different systems have different sets of voices and
      quality. Some examples are flite, espeak, festival, etc.  You
      may need to install them.</description>
    <key name="voice" type="s">
      <description>The voice to speak in.</description>

I only have one schema in my schemalist, the one for my plugin.  I have two keys; these can be numbers, sets of options, strings, etc.  In my case, each key is associated with a single string value (the "s" for type indicates string).  Each key has a default value, a summary (name), and a description of what the key is for.

NOTE: while working on this, I was installing everything under ~/.local, rather than into my system.  GSettings currently only checks for setting schemas installed under XDG_DATA_DIRS (e.g. /usr/share and /usr/local/share) in a glib-2.0/schemas directory.  That's fine, but you can't extend XDG_DATA_DIRS easily in your .bash_profile in the same way you can PATH or LD_LIBRARY_PATH, etc.  Trying to do


results in an almost broken GNOME session because XDG_DATA_DIRS isn't a known, set variable right while reading .bash_profile. :(  Instead, for testing, I do


in .bash_profile.  Let me know if you have a better solution.  I intend on filing a bug some day.

For code with settings, I created a GSettings object for my schema, and connected to its "changed" signal, so if a preference changed in the preferences dialogue, I could change the plugins behaviour (e.g. change the voice).

Inside my preferences code, I created its own GSettings object (my preferences code is in its own class), and bound the Gtk.ComboBox's I use to their corresponding GSettings.  In particular, I set the "id-column" property on my Gtk.ComboBox's to be the string I needed to give Speech Dispatcher, and then bound the "active-id" property for the Gtk.ComboBox's to the settings.   It got a bit confusing, but you can see the code yourself if you need something similar.

Custom Bindings

Vala can access other compiled libraries written in other languages: hooray!  Unfortunately, it needs a .vapi file to tell it how to provide a Vala API for that library.  Boo!, but necessary.  There are parts of libpeas that are not bound yet, and libspeechd (Speech Dispatcher) was not bound at all.  So, when working in Vala, sometimes you'll have to bind additional methods, or an entire library.

I created two custom bindings

  • libpeas-gtk-1.0.vapi

  • libspeechd.vapi

I really need to submit patches for both so they can be more widely used.  :)

Writing VAPI files is not that hard.  You can look in /usr/share/vala-*/vapi to model a custom one off of those.  In the .vapi file, you generally

  • specify a namespace

  • specify enums and classes, that will want to know things like what 

    • the name of the underlying C structure is, 

    • what .h file declares its methods, 

    • what prefixes its types go by

    • names of its public members

    • names of its methods (and corresponding C names)

    • any special considerations (e.g. how to handle array parametres)

    • free, destroy, copy, and constructor functions

I don't fully understand what I'm doing when I create them, but creating a rough one and submitting it as a patch at least gets the ball rolling, and helpful people on bugzilla can point you in what needs work. :)

Keine Kommentare:

Kommentar veröffentlichen

Dieses Blog durchsuchen


#Technology #GNOME gnome gxml fedora bugs linux vala google #General firefox security gsoc GUADEC android bug xml fedora 18 javascript libxml2 programming web blogger encryption fedora 17 gdom git emacs libgdata memory mozilla open source serialisation upgrade web development API Spain containers design evolution fedora 16 fedora 20 fedora 22 fedup file systems friends future glib gnome shell internet luks music performance phone photos php podman preupgrade tablet testing typescript yum #Microblog Network Manager adb apache art automation bash brno catastrophe css data loss debian debugging deja-dup disaster docker emusic errors ext4 facebook fedora 19 gee gir gitlab gitorious gmail gobject google talk google+ gtk html libxml mail microsoft mtp mysql namespaces nautilus nextcloud owncloud picasaweb pitivi ptp python raspberry pi resizing rpm school selinux signal sms speech dispatcher systemd technology texting time management uoguelph usability video web design youtube #Tech Air Canada C Electron Element Empathy Europe GError GNOME 3 GNOME Files Go Google Play Music Grimes IRC Mac OS X Mario Kart Memento Nintendo Nintendo Switch PEAP Selenium Splatoon UI VPN Xiki accessibility advertising ai albums anaconda anonymity apple ask asus eee top automake autonomous automobiles b43 backup battery berlin bit rot broadcom browsers browsing canada canadian english cars chrome clarity comments communication compiler complaints computer computers configuration console constructive criticism cron cropping customisation dataloss dconf debug symbols design patterns desktop summit development discoverability distribution diy dnf documentation drm duplicity e-mail efficiency email english environment estate experimenting ext3 fedora 11 festival file formats firejail flac flatpak forgottotagit freedom friendship fuse galaxy nexus galton gay rights gdb german germany gimp gio gjs gnome software gnome-control-center google assistant google calendar google chrome google hangouts google reader gqe graphviz growth gtest gtg gvfs gvfs metadata hard drive hard drives hardware help hp humour ide identity instagram installation instant messaging integration intel interactivity introspection jabber java java 13 jobs kernel keyboard language language servers languages law learning lenovo letsencrypt libreoffice librpm life livecd liveusb login lsp macbook maintainership mariadb mario matrix memory leaks messaging mounting mouse netflix new zealand node nodelist numix obama oci ogg oggenc oh the humanity open open standards openoffice optimisation org-mode organisation package management packagekit paint shedding parallelism pdo perl pipelight privacy productivity progress progressive web apps pumpkin pwa pyright quality recursion redhat refactoring repairs report rhythmbox rust sandboxes scheduling screenshots self-navigating car shell sleep smartphones software software engineering speed sql ssd synergy tabs test tests themes thesis tracker travel triumf turtles tv tweak twist typing university update usb user experience valadoc video editing volunteering vpnc waf warm wayland weather web apps website wifi wiki wireless wishes work xinput xmpp xorg xpath
Powered by Blogger.