## Writing Totem Plugins

Writing Totem Plugins — brief tutorial on writing Totem plugins

## Introduction

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

### Locations

Totem plugins can either be installed in the system path (e.g. `/usr/lib/totem/plugins/`), or in a user's home directory (e.g. `~/.local/share/totem/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/totem/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/totem/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 Totem) changes. If the plugin does not have its own website, Totem's website (`https://wiki.gnome.org/Apps/Videos`) 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 TOTEM_TYPE_FOOBAR_PLUGIN        (totem_foobar_plugin_get_type ())
    #define TOTEM_FOOBAR_PLUGIN(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPlugin))
    #define TOTEM_FOOBAR_PLUGIN_CLASS(k)        (G_TYPE_CHECK_CLASS_CAST((k), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPluginClass))
    #define TOTEM_IS_FOOBAR_PLUGIN(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), TOTEM_TYPE_FOOBAR_PLUGIN))
    #define TOTEM_IS_FOOBAR_PLUGIN_CLASS(k)     (G_TYPE_CHECK_CLASS_TYPE ((k), TOTEM_TYPE_FOOBAR_PLUGIN))
    #define TOTEM_FOOBAR_PLUGIN_GET_CLASS(o)    (G_TYPE_INSTANCE_GET_CLASS ((o), TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPluginClass))

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

    TOTEM_PLUGIN_REGISTER (TOTEM_TYPE_FOOBAR_PLUGIN, TotemFoobarPlugin, totem_foobar_plugin);

    static void
    impl_activate (PeasActivatable *plugin)
    {
        TotemFoobarPlugin *self = TOTEM_FOOBAR_PLUGIN (plugin);
        TotemFoobarPluginPrivate *priv = self->priv;
        TotemObject *totem = 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
         * Totem 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)
    {
        TotemFoobarPlugin *self = TOTEM_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 Totem'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 Totem 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 Totem interface.

Note that plugins can be activated and deactivated (e.g. from Totem's plugin manager) many times during one Totem 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 Totem 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.