
| Sample Code |
Introduction
Developing applications that run on Moblin is easy if
you are familiar with Linux, GTK+ development, and you have already
created a target device's build-environment with
Moblin Image Creator.
Once you have created a target, make sure you have the right libraries and tools installed so that you can easily develop applications. First, add the developers-tools functional set (fsest) to your target with Moblin Image Creator. You should also have the libosso-dev package installed. For more information about necessary libraries, and adding fsets, click here. Now you can begin developing C/C++ applications that use the Hildon Application Framework.
There are several things to do to create an application for Moblin:This guide helps you create a simple application that displays "Hello World" and a timestamp of when the application was started. We will then add a File menu containing the Quit option and a toolbar with a Quit button. Next, we will show you how to integrate this application with the Hildon desktop environment. We will show you how to add it to the Home Screen menu and use the libraries that allow only one instance of the program to be running. After that we will modify the menubar and the toolbar for a better fit with the Hildon desktop environment.
Our "Hello World!" Samples
You can download all of our hello-world samples, compile
them, and see how they work. To learn how to build them step-by-step,
you can skip to the next
section.

You should see "Hello World! v 1.0", "Hello World! v 2.0", and "Hello World! v 3.0" entries on the Home Screen in categories Mobile and All. Launch the applications from the Home Screen and see what functionality and differences they have.
Close the Xephyr window, uninstall, and clean by typing:Now, let's take the step-by-step approach.
Compiling a Basic C GTK+ Application
Start the terminal session from Moblin Image Creator for your target by selecting it in the Targets section and clicking the Terminal button. You can create a directory for your source wherever you would like. For this example, we are using the directory /usr/src/hello.
Adding the Application Icon to the Home
Screen
To add the Hello World icon to the Moblin home screen, you must create
a desktop entry file for your application. The file, which we will call
hello-world-1.desktop, has all the information necessary to display the
application's icon as a point of entry on the home screen. The name of
the file must end with the extension ".desktop", and the file must be
placed in the /usr/share/mobile-basic-flash/applications/ directory on
the target file-system.
For more information about the desktop entry file, refer to the Desktop Entry Specification at http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html.
Start the Xephyr window:You should now see "Hello World! v 1.0" entry on the home screen in categories Mobile and All. The only problem is that every time you click the icon, a new instance of hello-world launches! You can verify this by comparing the timestamps in applications. For mobile devices, it is optimal for applications to have only one instance running. Let's fix this in the next section.
Registering An Application to the D-Bus
Create the hello-world-2.c File
The D-Bus is the message system that helps applications communicate
with each other and the system by registering services offered to
others. The Hildon framework includes LibOSSO, a wrapper layer that
assists D-Bus for easier usage. By registering an application to the
D-Bus service, the Hildon framework only allows a single instance of
the application. For mobile devices, it is optimal for applications
to have only one instance running.
For more information, refer to D-Bus and LibOSSO documents.
Now we are going to add LibOSSO functionality to our Hello World application. You can copy file hello-world-1.c to hello-world-2.c and modify it, or use hello-world-2.c from the sample code.
We wrote the handler function that will process the message "top_application" and make the application visible. To connect to D-Bus and receive the system messages, we must call the LibOSSO API functions osso_initialize(...) and osso_rpc_set_default_cb_f(...). See the libosso.h file or http://maemo.org for more information on all osso_ functions.
We will also add one line to our main(...) function:Now the application connects to the D-Bus session bus and will bring itself to the front when necessary by calling gtk_window_present(...).
Create D-Bus service file org.moblin.helloworld2.service
To launch the Hildon application and connect it to D-Bus services,
you need to create the D-Bus service file. This file should be
placed in /usr/share/dbus-1/services/ directory in the target
file-system and end with the extension ".service" For more
information, you can search
http://maemo.org for the X-Osso-Service.
| Name | The application's D-Bus service name. This needs to be a unique value for each application that matches the value used in the application initialization code in the call to the osso_initialize(...) function. Usually the name starts with the application provider URL following the application name to minimize the possibility of conflicts with other service files. No dashes - or underscores _ are allowed in this name. That is why we changed this name to helloworld2 from the application name hello-world-2. The name of the D-Bus service file should match this name with the extension ".service", which is why it became org.moblin.helloworld2[1] |
| Exec | The full path of application binary to launch[1]. |
Update Desktop File
So that our D-Bus registered application behaves correctly, our
desktop entry file needs to have one more name/value tag added.
You should now see the "Hello World! v 2.0" icon on the home screen. Launch the application and take note of the timestamp.
Here's the cool part: Switch back to the home screen without closing the application, and click on "Hello World! v 2.0" icon again. The application launches with the same timestamp! We can now verify that adding LibOSSO causes the system to bring forward our already running instance of hello-world-2.

Figure 4-1.
See! Same timestamp!
Hildon Program, Window, Menu and
Toolbar
You have probably already noticed that applications written or
modified for mobile devices have no menubars and have bigger icons
on toolbars. Let's copy our hello-world-2.c to hello-world-3.c and
modify it to take advantage of the Hildon library specially
created for mobile devices.
HildonProgram is the main instance of a Hildon-based application. It is inherited from GObject and is the base widget in the Hildon Framework. HildonWindow is the top main window of a Hildon-based application. It is derived from the GtkWindow and provides additional commodities specific to the Hildon framework.
Create D-Bus service file org.moblin.helloworld3.service
Make sure you update the D-Bus service file to reflect the new name of hello-world-3. This file should be placed in the /usr/share/dbus-1/services/ directory in the target file-system:Create Desktop File hello-world-3.desktop
You also need a new desktop entry file. This file should be placed in /usr/share/mobile-basic-flash/applications/ directory in the target file-system:You should see now "Hello World! v 3.0" entry on the Home Screen in categories Mobile and All. Launch the application from the Home Screen. Notice that it has a new type of menu and a bigger toolbar at the bottom.

If you click the application title, a menu with item "File" and the "Quit" appears. Shortcut Ctrl+Q works with the latest Hildon library update. On normal desktop systems, we can take advantages of mnemonic keyboard navigation, by, for example, typing "Alt-F" to open the File menu. This doesn't work like this in our Hildon app, but pressing F when the menu is activated opens the File submenu.
The Makefile
This is the Makefile that we use to compile all source files and install necessary files in proper locations:Cool Trick!
If the Name parameter in the desktop entry file matches the
application title exactly (set by gtk_window_set_title(...) or
g_set_application_name(...)) then we can write an application that
will only launch a singleton instance without using LibOSSO.
You should now see the "Hello World! (v 1.0)" icon on the home screen. Launch the application and take note of the timestamp. Switch back to the home screen without closing the application, and click on "Hello World! v 1.0" icon again. Our application launches with the same timestamp. We have a singleton instance.
Reference
Documents
The following documents were used as references in the writing of this
document. Refer to these documents for further details.