Published: Sun 01 May 2011
In Tech .
tags: n900 open-source python
After a few months of owning a N900 finally came the itch to write an
app for it -- using of course Python! The Maemo documentation is
excellent, I'm only putting my notes down here for future reference.
First off, making sure to know which version of Maemo is on the phone:
Settings -> About: Maemo 5.
Setting up
The documentation at http://maemo.org/development/ is truly excellent.
Of interest to us to begin with is installing the
SDK .
The wiki has been kept up to date with tips and links to solutions of
potential problems at the stage where you might encounter them, making
any issue a non-event.
Note: It turns out that the SDK is optional to develop with Python. If
you would prefer not to set this all up, you can simply move your Python
files to the phone or write the code there directly. Personally I found
having a full SDK on the laptop to be really handy to try things out
quickly, and to tweak and understand what the final result will look
like.
Getting started, every time
The SDK docs lists the various commands that may work for your distro
and system specifics, which is awesome but can be a bit dense to read
through and find again later on. Here's what I do on Debian unstable:
Xephyr :2 -host-cursor -screen 800x480x16 -dpi 96 -ac &
and:
/scratchbox/login
export DISPLAY=:2
af-sb-init.sh start
Important: Don't forget to run `af-sb-init.sh stop` at the end, or
say, if you messed your Xephyr command and need to restart it -- you
will need to stop this script before attempting to start the emulator
environment again. Otherwise there will be D-Bus conflicts and things
won't work.
Hello, world
After setting up the SDK, the documentation recommends you go learn
about packaging next -- I kept that one for later, it's more interesting
to build something first!
By following the documentation you can find the Hello world for Python,
which does work on the phone with no hassle and without stopping by the
SDK: Developer
Guide
-> Programming
Environments
-> PyMaemo quick
start.
If you'd like to work in scratchbox (quicker turnaround!), you can go
back to the Maemo SDK on your machine and follow again the instructions
to set up a Python development environment.
Hello world, again
The same Python-related packages need to be installed into the dev
environment, within scratchbox.
nano /etc/apt/sources.list
deb http://repository.maemo.org/extras-devel/ fremantle free non-free
apt-get update
apt-get install maemo-python-env (and qemacs :o))
Note that on the phone, you would install maemo-python-device-env (cf.
Quick Start
Guide ), while when
working in scratchbox you should install maemo-python-env instead
(cf. Python
support )
qemacs -nw hello.py
python2.5 hello.py
Make sure you're calling `python2.5` explicitly or the program will
fail with "ImportError: No module named gtk ". While working with
Hildon I use this command instead, which you'll learn when doing the UI
Tutorial.
run-standalone.sh python2.5 hello.py
Because of the various warnings messing up my scratchbox screen, I ended
up using emacs from outside of scratchbox. The files you're working with
are located at /scratchbox/users/<username>/home/<username>.
Once you can run the Hello world program, the next step is to go through
the handy UI tutorial ,
which teaches a lot about Hildon and GTK.
I would also recommend installing the devhelp packages for:
PyGTK (python-gtk2-doc),
the PyGTK tutorial (python-gtk2-tutorial), and
the hildon API (libhildon-1-doc) (also available
online ). The API
reference is in C but after doing the tutorial it's easy enough to
guess the Python names. Note that although it's still handy, the
Debian package does not appear to be the very latest version, e.g.
HildonSizeType isn't documented.
I'm really happy with that setup, and it's lovely to get a nice looking
UI working so quickly and easily. This is reconciling me with GUI
programming -- it looks nice and because of the small screen, there
isn't a ton of repetitive boilerplate to write. I can't wait to be far
along enough in that project, that I have an excuse to learn again about
Debian packaging! :-)