Activity tutorial

From OLPC
Revision as of 13:54, 15 September 2006 by 65.204.58.2 (talk)
Jump to: navigation, search
  This page is monitored by the OLPC team.

Here is a short tutorial about writing a sugar activity. It's a step by step guide to get you started quickly, without going in details of the activity system. Download the current example sources (Marco's original sources are here).

Write the build system

Create the autogen.sh script

You should only need to change the module name and activity file from this example.

#!/bin/sh
# Run this to generate all the initial makefiles, etc.

srcdir=`dirname $0`
test -z "$srcdir" && srcdir=.

PKG_NAME="sugar-drawing"

(test -f $srcdir/drawing.activity) || {
    echo -n "**Error**: Directory "\`$srcdir\'" does not look like the"
    echo " top-level $PKG_NAME directory"
    exit 1
}

which gnome-autogen.sh || {
    echo "You need to install gnome-common from the GNOME CVS"
    exit 1
}

REQUIRED_AUTOMAKE_VERSION=1.9 USE_GNOME2_MACROS=1 . gnome-autogen.sh

Create a configure.ac file

The first line of configure.ac specifies the name of the application, the version number, a bug reporting address, and the name of the module.

The configure.ac file contains a list of all the Makefiles in your project. You will need one Makefile for every directory in your project.

AC_INIT([Sugar Drawing],[0.1],[],[sugar-drawing])
AM_INIT_AUTOMAKE

AM_PATH_PYTHON

AC_OUTPUT([
Makefile
other_dir/Makefile
])

Other required files

Automake checks for the existence of a few required files, you have to create them even if empty:

touch NEWS README AUTHORS ChangeLog

Makefile.am

This file tells automake how to install your program.

in the Makefile.am, SUBDIRS is a space-separated list of subdirectories of the current folder, an activitydir that describes where files in the current folder will be installed, and activity_PYTHON which describes what files to install.

The backslash at the end of the activity_PYTHON lines indicates that more data exists on the following lines. (Note that the last line does not have a slash).

(fixme: exactly what does EXTRA_DIST mean?)

SUBDIRS = foo bar
activitydir = $(datadir)/sugar/activities/drawing
activity_PYTHON =               \
        __init__.py             \
        DrawingActivity.py

EXTRA_DIST = drawing.activity

install-data-local:
        sugar-setup-activity $(srcdir)/drawing.activity

Write the activity code

Write a subclass of sugar.activity.Activity. It's a GtkWindow so you can use the "add" method to insert your own widgets. The source of DrawingActivity.py demonstrates it:

import gtk

from sugar.activity.Activity import Activity

class DrawingActivity(Activity):
        def __init__(self):
                Activity.__init__(self)
                button = gtk.Button('Drawing')
                self.add(button)
                button.show()

Or if you are adapting a full application, your activity could look something like this: (I found I needed to delete the app object explicitly)

import gtk

from sugar.activity.Activity import Activity
from my_module.my_app import my_app

class DrawingActivity(Activity):
        def __init__(self):
                Activity.__init__(self)

                app = my_app.my_app()
                self.add(app.some_widget)
                app.do_show()
                self.connect('destroy',self.do_quit, app)

        def do_quit(self, event, app):
                app.do_quit()
                del app

create __init__.py

This is the package initialization file. It can be blank.

touch __init__.py

drawing.activity

Include the necessary information about the activity.

(fixme: what is default_type?)

[Activity]
name = Drawing
id = org.laptop.sugar.Drawing
python_module = drawing.DrawingActivity.DrawingActivity
default_type = _drawing_olpc._udp
show_launcher = yes

Build and install

Initialize the build system. The value of prefix depends on the path of sugar-jhbuild:

./autogen.sh --prefix=[SUGAR-JHBUILD]/build

Build and install:

./sugar-jhbuild shell
make
make install