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:
https://gitorious.org/rhythmbox-dj 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
[Plugin]
Module=djaqua
IAge=2
_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
Website=http://www.rhythmbox.org/ 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
Makefile
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)
Files,
plugin_LTLIBRARIES: points to our .la, lib
djaqua.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. :)
Flags,
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:
[ModuleInit] 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
<schemalist>
<schema id="org.gnome.rhythmbox.plugins.djaqua" path="/org/gnome/rhythmbox/plugins/djaqua/">
<key name="output-module" type="s">
<default>'espeak'</default>
<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>
<key name="voice" type="s">
<default>'MALE1'</default>
<summary>Voice</summary>
<description>The voice to speak in.</description>
</key>
</schema>
</schemalist 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
XDG_DATA_DIRS=$XDG_DATA_DIRS:$HOME/.local/share 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
XDG_DATA_DIRS=/usr/local/share:/usr/share:$HOME/.local/share 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. :)