Documentation/Maemo 5 Developer Guide/Application Development/Writing Control Panel Applets

= Writing control panel applets = The control panel is designed to be extendable, and items can be added to it with dynamic libraries with certain functions and desktop files describing the library.

Functions
A control panel applet (libapplet.c) needs two functions. These functions are defined in

   #include  &lt;hildon-cp-plugin/hildon-cp-plugin-interface.h&gt; 

The first and more important one, execute, is called when the applet is activated from Control Panel. Usually this function creates a dialog and waits until it is done. Any dialog created should be modal to the window in parameter. N.B. The library might be unloaded when execute returns, so no g_timeouts, gconf_notifys or such should be left when done. Gtk or osso initialization is not needed, as they are already done at this point.

   #include  &lt;hildon-cp-plugin/hildon-cp-plugin-interface.h&gt;   #include  &lt;gtk/gtk.h&gt; osso_return_t  execute  ( osso_context_t  * osso , gpointer data , gboolean user_activated ) { GtkWidget <font color="#990000"> * dialog <font color="#990000"> ; gint response <font color="#990000"> ; '' <font color="#9A1900">/* Create dialog with OK and Cancel buttons. Leave the separator out, '' '' <font color="#9A1900">	 * as we do not have any content. */ '' 	dialog <font color="#990000"> =   <font color="#000000">gtk_dialog_new_with_buttons   <font color="#990000">(  		 <font color="#FF0000">"Hello control panel"  <font color="#990000">,  		  <font color="#000000">GTK_WINDOW   <font color="#990000">( data <font color="#990000">),  		GTK_DIALOG_MODAL <font color="#990000"> | GTK_DIALOG_NO_SEPARATOR <font color="#990000">,  		GTK_STOCK_OK <font color="#990000">,  		GTK_RESPONSE_OK <font color="#990000">,  		GTK_STOCK_CANCEL <font color="#990000">,  		GTK_RESPONSE_CANCEL <font color="#990000">,  		NULL <font color="#990000">); '' <font color="#9A1900">/* ... add something to the dialog ... */ '' 	  <font color="#0000FF">if   <font color="#990000">(! user_activated <font color="#990000">) <font color="#FF0000">{ '' <font color="#9A1900">/* ... load state ... */ '' 	 <font color="#FF0000">} '' <font color="#9A1900">/* Wait until user finishes the dialog. */ '' 	response <font color="#990000"> =   <font color="#000000">gtk_dialog_run   <font color="#990000">(   <font color="#000000">GTK_DIALOG   <font color="#990000">( dialog <font color="#990000">));  <font color="#0000FF">if  <font color="#990000">( response <font color="#990000"> == GTK_RESPONSE_OK <font color="#990000">) <font color="#FF0000">{ '' <font color="#9A1900">/* ... do something with the dialog stuff ... */ '' 	 <font color="#FF0000">}  <font color="#9A1900">/* Free the dialog (and it's children) */   <font color="#000000">gtk_widget_destroy  <font color="#990000">(   <font color="#000000">GTK_WIDGET   <font color="#990000">( dialog <font color="#990000">));  <font color="#0000FF">return  OSSO_OK <font color="#990000"> ; <font color="#FF0000">} </tt>

The other function is called save_state. It is called when application using the applet is saving state. Usually this does nothing, but if support for state saving is needed, this should be used.

osso_return_t  <font color="#000000">save_state  <font color="#990000">( osso_context_t <font color="#990000"> * osso <font color="#990000">, gpointer data <font color="#990000">) <font color="#FF0000">{ '' <font color="#9A1900">/* ... save state ... */ '' 	  <font color="#0000FF">return  OSSO_OK <font color="#990000"> ; <font color="#FF0000">} </tt>

Building applet
To use the applet, it needs to be built into a dynamic library aka shared object. This can be accomplished by giving flag -shared to gcc.

[sbox-FREMANTLE_X86: ~] &gt; gcc -shared `pkg-config gtk+-2.0 libosso --libs --cflags` libapplet.c -o libapplet.so

The binary produced by gcc should be installed to path specified in hildon-control-panel pkg-config entry. This path can be obtained with pkg-config hildon-control-panel -variable=pluginlibdir. By default this is /usr/lib/hildon-control-panel.

The .desktop file
Any control panel applet needs a desktop file describing it. The file contains metadata like name, icon name and library of the applet. The applet desktop file is much like the desktop file for any other application.

Here is an example desktop file (applet.desktop) for the applet created above.

[Desktop Entry] Encoding=UTF-8 Version=1.0 Name=Control Panel Hello World Comment=A control panel example applet Type=HildonControlPanelPlugin Icon=qgn_list_cp_isetup X-control-panel-plugin=libapplet.so Categories=general

The desktop file must be installed to directory specified in pkg-config file for hildon-control-panel. This directory can be obtained by pkg-config hildon-control-panel -variable=plugindesktopentrydir. By default, this is /usr/share/applications/hildon-control-panel.