pidgin 2.14.14dev
|
The libpurple signals interface is used for general event notification, such as plugins being loaded or unloaded, allowing the GUI frontend to respond appropriately to changing internal data. Unfortunately, its use is not at all obvious from the information in the header files. This document uses code snippets from the Pidgin/libpurple plugin systems to illustrate the proper use of signals.
Signals in libpurple are very similar to those in GTK+. When certain events happen, a named signal is "emitted" from a certain object. Emitting the signal triggers a series of callbacks that have been "connected" to that signal for that object. These callbacks take appropriate action in response to the signal.
The first step of using a signal is registering it with libpurple so that callbacks may be connected to it. This is done using purple_signal_register() Here is a slightly modified example from purple_plugins_init
in libpurple/plugin.c
:
A reference to the object from which this signal is emitted, and to which potential callbacks should be connected. In this case, it will be the entire plugin module emitting the signal.
Unique identifier for the signal itself.
The rest of the arguments specify the form of the callback function.
purple_marshal_VOID__POINTER
represents the callback function prototype, not including a "data" argument, explained later. The form is purple_marshal_RETURNVALUETYPE__ARG1TYPE_ARG2TYPE_ETC
. See signals.h for more possible types.
In this case, the callback will have the form
If purple_marshal_BOOLEAN__POINTER_POINTER_POINTER
were specified, it would be:
The void
*data
argument at the end of each callback function provides the data argument given to purple_signal_connect() .
In our case, this is NULL, meaning "returns void".
The number of arguments (not including data
) that the callback function will take.
purple_value_new(PURPLE_TYPE_SUBTYPE,PURPLE_SUBTYPE_PLUGIN)
specifies that the first argument given to the callback will be a PurplePlugin*
. You will need as many "type of argument" arguments to purple_signal_register() as you specified in "Number of arguments" above.
Once the signal is registered, you can connect callbacks to it. First, you must define a callback function, such as this one from gtkplugin.c :
Note that the callback function prototype matches that specified in the call to purple_signal_register() above.
Once the callback function is defined, you can connect it to the signal. Again from gtkplugin.c , in pidgin_plugin_dialog_show()
:
The first two arguments ("What to connect to") specify the object emitting the signal (the plugin module) and what signal to listen for ("plugin-load").
The object receiving the signal is plugin_dialog
, the Pidgin plugins dialog. When plugin_dialog
is deleted, then purple_signals_disconnect_by_handle(plugin_dialog)
should be called to remove all signal connections it is associated with.
The callback function is given using a helper macro, and finally the data
argument to be passed to plugin_load_cb
is given as event_view
, a pointer to the GTK widget that plugin_load_cb
needs to update.
Connecting callbacks to signals is all well and good, but how do you "fire" the signal and trigger the callback? At some point, you must "emit" the signal, which immediately calls all connected callbacks.
As seen in purple_plugin_load()
in plugin.c :
This causes the signal "plugin-load" to be emitted from the plugin module (given by purple_plugins_get_handle()
), with the newly loaded plugin as the argument to pass to any registered callback functions.
In our example, plugin_load_cb
is called immediately as
and does whatever it does.