Writing Xplayer Plugins

## Writing Xplayer Plugins Writing Xplayer Plugins — brief tutorial on writing Xplayer plugins |

## Introduction

Xplayer is extensible by means of small, dynamically-loadable plugins, which add functionality wanted by some but not others.

### Locations

Xplayer plugins can either be installed in the system path (e.g. `/usr/lib/xplayer/plugins/`), or in a user's home directory (e.g. `~/.local/share/xplayer/plugins/`). In either case, each plugin resides in a subdirectory named after the plugin itself.

In addition, each plugin needs a `.plugin` index file, residing inside the plugin directory. This gives the code name of the plugin, as well as some metadata about the plugin such as its human-readable name, description and author.

Example 1. Example Plugin Directory

A system-installed plugin called `subtitle-downloader` would reside in `/usr/lib/xplayer/plugins/subtitle-downloader`, and would (at a minimum) have the following files:

- `subtitle-downloader.plugin`
- `libsubtitle-downloader.so`

If installed in a user's home directory, it would reside in `~/.local/share/xplayer/plugins/subtitle-downloader` and have the same files.

### The `.plugin` File

The file should use the following template:

    [Plugin]
    	Module=plugin-name
    	IAge=plugin interface age (starting at 1)
    	Builtin=true or false
    	Name=Human-Readable Plugin Name
    	Description=Simple sentence describing the plugin's functionality.
    	Authors=Plugin Author Name
    	Copyright=Copyright © year Copyright Holder
    	Website=http://plugin/website/

Most of the values in the template are fairly self-explanatory. One thing to note is that the plugin name should be in lowercase, and contain no spaces. The plugin interface age should start at `1`, and only be incremented when the binary interface of the plugin (as used by Xplayer) changes. If the plugin does not have its own website, Xplayer's website (`http://projects.gnome.org/xplayer/`) can be used.

The library file containing the plugin's code should be named `lib`plugin-name`.so` (for C, or other compiled language, plugins) or ``pluginname`.py` (for Python plugins).

### Writing a Plugin

Writing a plugin in C is a matter of creating a new GObject which inherits from PeasExtensionBase and which implements PeasActivatable. The following code will create a simple plugin called `foobar`:

Example 2. Example Plugin Code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    #define XPLAYER_TYPE_FOOBAR_PLUGIN      (xplayer_foobar_plugin_get_type ())
    #define XPLAYER_FOOBAR_PLUGIN(o)            (G_TYPE_CHECK_INSTANCE_CAST ((o), XPLAYER_TYPE_FOOBAR_PLUGIN, XplayerFoobarPlugin))
    #define XPLAYER_FOOBAR_PLUGIN_CLASS(k)      (G_TYPE_CHECK_CLASS_CAST((k), XPLAYER_TYPE_FOOBAR_PLUGIN, XplayerFoobarPluginClass))
    #define XPLAYER_IS_FOOBAR_PLUGIN(o)     (G_TYPE_CHECK_INSTANCE_TYPE ((o), XPLAYER_TYPE_FOOBAR_PLUGIN))
    #define XPLAYER_IS_FOOBAR_PLUGIN_CLASS(k)       (G_TYPE_CHECK_CLASS_TYPE ((k), XPLAYER_TYPE_FOOBAR_PLUGIN))
    #define XPLAYER_FOOBAR_PLUGIN_GET_CLASS(o)  (G_TYPE_INSTANCE_GET_CLASS ((o), XPLAYER_TYPE_FOOBAR_PLUGIN, XplayerFoobarPluginClass))

    typedef struct {
        /* Plugin private member variables */
    } XplayerFoobarPluginPrivate;

    XPLAYER_PLUGIN_REGISTER (XPLAYER_TYPE_FOOBAR_PLUGIN, XplayerFoobarPlugin, xplayer_foobar_plugin);

    static void
    impl_activate (PeasActivatable *plugin)
    {
        XplayerFoobarPlugin *self = XPLAYER_FOOBAR_PLUGIN (plugin);
        XplayerFoobarPluginPrivate *priv = self->priv;
        XplayerObject *xplayer = g_object_get_data (G_OBJECT (plugin), "object");

        /* Initialise resources, connect to events, create menu items and UI, etc., here.
         * Note that impl_activate() and impl_deactivate() can be called multiple times in one
         * Xplayer instance, though impl_activate() will always be followed by impl_deactivate() before
         * it is called again. Similarly, impl_deactivate() cannot be called twice in succession. */
    }

    static void
    impl_deactivate (PeasActivatable *plugin)
    {
        XplayerFoobarPlugin *self = XPLAYER_FOOBAR_PLUGIN (plugin);

        /* Destroy resources created in impl_activate() here. e.g. Disconnect from signals
         * and remove menu entries and UI. */
    }

Once resources have been created, and the plugin has been connected to Xplayer's UI in the `impl_activate` function, the plugin is free to go about its tasks as appropriate. If the user deactivates the plugin, or Xplayer decides to deactivate it, the `impl_deactivate` will be called. The plugin should free any resources grabbed or allocated in the `impl_activate` function, and remove itself from the Xplayer interface.

Note that plugins can be activated and deactivated (e.g. from Xplayer's plugin manager) many times during one Xplayer session, so the `impl_activate` and `impl_deactivate` functions must be able to cope with this.

Any of the API documented in the rest of the Xplayer API reference can be used by plugins. Python plugins are written in the same way as C plugins, and are similarly implemented as classes derived from PeasExtensionBase and implementing PeasActivatable.