Writing apps for the N900, part 1

Ever since the N900 was announced, I’ve had people ask me whether it’s difficult, or in some cases even possible, to write apps for it.   Well, it is possible, and it’s pretty easy: let’s write a simple app together and you’ll see.  We’ll make a basic client for the social bookmarking site reddit, and I’ll call it ræddit, because I have no imagination.

First, a bit of handwaving. I’m not here to teach you C, or Glib programming, or GTK programming.  There are plenty of good tutorials about those already, and I’ll assume you know a bit about them.  In keeping with this, we won’t be writing the client’s back end; we’ll just assume that there’s a Glib-friendly client library for reddit already in existence.  Oh, look, a handy Glib-friendly client library just conveniently sprang into existence for us: source, headers.

Learn to love Scratchbox. If you’re doing any development for the N900, you’ll run into a cross-compiler toolkit called scratchbox.  It’s packaged in most major distros, so install it before you go any further.  There are two reasons you’d use scratchbox:

  • In case you don’t have an N900 handy, you can run an entire Maemo environment inside scratchbox.  But we’ll assume you have an N900 lying around, because otherwise we’d have to explain how to set up the desktop environment inside scratchbox, and that would be a bit of a diversion.  (On the other hand, this would be a good subject for a future post in this series.)
  • You can also use scratchbox just for running a compiler, in order to make binaries.  This is what we’ll do.

My First Compile. So first of all, let’s not bother with any of the graphical stuff, or with packaging.  Let’s just write a program that dumps the contents of reddit’s front page out to standard output, and then let’s run it on your phone.  Here’s a pretty minimal program to do that: raeddit-01.c.  Put that in your scratchbox, along with the reddit client library linked to above, and at the scratchbox prompt type:

apt-get install libcurl3-dev libjson-glib-dev

gcc `pkg-config --cflags --libs glib-2.0 hildon-1 json-glib-1.0 libcurl dbus-glib-1` raeddit-01.c greddit.c -o raeddit-01

There are some extra libraries listed in the pkg-config statement which we’ll be needing later.

Caveat: You may currently find a difficulty getting the correct packages installed to compile against json-glib; as of the time of writing there was a version dependency problem in the Maemo repository between libjson-glib-dev and libjson-glib-1.0-0. I solved it in a rather ugly way by downloading the package directly and doing dpkg -i --force-depends libjson-glib-dev_0.6.2-5_armel.deb. Your mileage may vary. I hope they’ve fixed this by the time you read this.  If not, you know what to do.

So anyway, you should now have a binary called raeddit-01.

Now, either find out your trusty N900’s wifi IP address, or hook it up via USB, in which case its IP address is 192.168.2.15.  Give the command

scp raeddit-01 root@192.168.2.15:

(The default root password is well-known.)  Your binary is now on the phone in the directory /root.  Make sure the phone is connected to the Internet, fire up a terminal on the device, and type

/root/raeddit-01

If you see the current contents of reddit’s front page printed out on the terminal, you’re doing fine:
the output of raeddit-01

First steps into Hildon. So, now we have a very basic reddit client.  Let’s think about making it pretty.

What we want is a series of buttons going down the page that look something like this:

sketchy diagram

Eventually we’ll need to make a custom widget for that, but fortunately there’s a standard Maemo widget called a HildonButton which holds two labels, a title and an optional subtitle, which is close enough to what we want for a basic tutorial.  It looks like this:

A button

We can just stick a bunch of these into a GtkVBox.  But we also want the user to be able to flick through them with her finger, and there’s a specialised Maemo container widget to allow that: it’s called HildonPannableArea. So the GtkVBox will live inside one of those.

So here’s raeddit-02.c (diff to raeddit-01.c), which knows how to put up a window and fill it with buttons.  The buttons don’t go anywhere, but at least we’re getting somewhere!  Compile it, transfer it to the phone, and run it again:

Hildon application showing reddit content

Making the buttons into links. So next we need to make the buttons go to the various sites.  Now, there’s two important things you have to consider when porting or building an app on the N900.  One of them, which we’ve just mentioned, is that sometimes there are widgets which don’t exist on the desktop, because they work in ways which are better adapted to life on a phone.  The other is that there’s a whole ecosystem of programs on the device with which we must play nicely.  One such program is the web browser, and right now that’s exactly the program we need to talk to.

The polite way to ask the browser to show a particular page is by using a DBus call.  Here’s raeddit-03.c (diff to raeddit-02.c), which knows how to do that.  Compile it, transfer it to the phone, and run it again. I would show you a screenshot, but it looks just the same as the last version. And it works.

And now we have a first approximation to the program we wanted, which is at least good enough to call version 0.01!

Where can this go next?

Launcher, including raeddit

  1. The program needs to live in a package, so that it can be installed easily, so that it can have a little icon in the launch menu, and so that it can be uploaded to maemo-extras and perhaps one day to Maemo Select.  We’ll be looking at that next time.
  2. There are lots of things you might want to do with a post on reddit, beyond just viewing the site it links to.  So rather than the buttons going directly to the sites, it would be better if they popped up one of those rather stylish menus saying things like “Go to site”, “Go to comments page”, “Vote up”, “Vote down”, and so on.  (That kind of menu is called a HildonTouchSelector).  We’ll do that the time after next.
  3. Clearly the page should refresh every so often.
  4. The download should really be done asynchronously.
  5. The buttons should really be custom widgets that look more like our mockup.
  6. Many posts on reddit consist only of a picture, and launching the browser for these is probably overkill.  We could do these in-process with a GdkPixbuf.  This should be another option on the popup menu, “View image”, so that people still had the option of using the browser.
  7. Also for picture posts, we could display them inline in the custom widgets, as is done on the website.
  8. Of course, it would be lovely if you could use the client to log in, and vote posts up or down.
  9. Also, the ability to post links would be pretty useful.
  10. There should be an app menu with some useful buttons on it.
  11. The client should support subreddits.
  12. It would be extra fun to have this as a home applet rather than an app.
  13. There should be a way to sort posts by newness, hotness, and so on.  (Yes, I noticed after I wrote it that the current version sorts posts backwards.)
  14. It should be possible to hide posts you’ve already seen.
  15. There should be a “share” button, to share posts you like with other people by email or twitter/identica, and so on.
  16. It would be a nice touch if there were gestures to access the most common options on the popup menu (slide your finger from left to right across a post to open the associated link, and so on.)
  17. I can imagine adding a new addressbook field called “reddit username”, and being able to single-tap open a contact’s reddit user page from their contact information.
  18. The iPhone app has a thing where it goes to a random page if you shake it.  We could do that…
  19. …your idea here…

Let me know where you’d like this series to go in future.  I’m speaking only as a fascinated individual here, and I’d also welcome any corrections you have, as well as any questions about things I left unclear.  If this whets your appetite, you can find out a lot more detail in the official developer guide, or the official porting guide.

My last night in Helsinki

It’s my last night in Helsinki, at least this time around, so I thought I’d do a bit more exploring. Helsinki on the ground never seems to bear much resemblance to the map. Streets seem to vanish and re-materialise again later. Daf had recommended a place called Dong Bei Hu for dinner. After some wandering, I found it, and asked for a table for one. The staff looked me up and down and said they were full; I don’t know whether I was too scruffy to eat there or whether they were actually full, or both. So I was a bit hungry, and wishing someone would hurry up and port Urbanspoon to the N900. (Yes, I know I could have used the website, but I would also have been wishing Urbanspoon covered Finland in the first place. And I had forgotten eat.fi. More fool me.)

But very soon, I found the cathedral, which made up for being hungry:

It’s rather large and very beautiful. I went inside, but it seemed rude to take pictures of that. There was a café in the crypt, but they close at 17:00, for some reason, just when people might be wanting to have dinner there.

On the way back, I went into an Indian restaurant but nobody came to help me and I left after a minute or so. Then I found a Chinese place called Tang Dynasty who were helpful, and I ate there.

After that I came back to the hotel and started coding. I have to be at the airport at five, so I’m planning to stay up. Unfortunately, the fire alarm went off: it wasn’t an actual fire, but I’m on the ninth floor and I’m too sensible to use the lifts, so it was a long walk. The fire brigade came and walked around: they wear black uniforms with the city arms on the back (a crowned boat).

Ladies and gentlemen, this IS Maemo number five

I can finally tell you what I’ve spent the last year working on, and why I’ve been commuting back and forth to Helsinki. It’s a phone that runs Linux (a Debian-based distro called Maemo), and its name is the N900. I’ve been playing with it for months and I love it. Not only is it a phone and a Linux box in your pocket, it also runs robotfindskitten.

Quesadillas and theming

Day: Woke up, went to the gym, came back, worked, and ~plexq made us some rather good quesadillas.  ~Fin made some lovely tag icons for marnanel.org to go along with the existing ones from rosequoll.

Theming: I would like to re-examine the Metacity theme format for GNOME 3.  To this end, I’ve been working on CSS theming for Metacity.  I have a reasonable first pass at it written, and several of the standard themes converted (and several more to do).  But it occurs to me that the people who would like to create themes and the people who would be willing to download and compile several experimental libraries are not necessarily the same.  To this end, I’ve created a wiki to demonstrate the system.  If you write CSS on a page whose name begins with Borders: it will display as the result of rendering that CSS.  There is a tour, which you should take first.

Please feel free to create your own themes, either from scratch or by making copies of the existing ones.  I was going to finish off all the sample themes before announcing this, and also to make a theme wizard that would put together a theme for you from parts (to show how the system is well-adapted to use with editors), but I decided it was better to release early, release often.

Of course, now that I’ve announced this, it’s sure to be vandalised; I’ll try to keep an eye on it, but if you could revert any vandalism you see, I’d appreciate it.  Let me know what you think of the system, either way.

Top fives: Ask me for my top five favourites of anything and I’ll try to give a sensible answer.  So far I’ve been asked for cities, words, puddings, foods, beverages, heresies, books, and Tetris shapes.  Since there are only seven Tetris shapes, that’s rather an easy question: the answer is all of them except “S” and “Z”.

Avvy

When I was about fourteen my parents bought a PC to replace our ageing BBC Micro. There was an adventure game called Leisure Suit Larry which I had heard a lot about and was quite anxious to play. Once I had figured out how to win it, I set about cloning it, and in two years, with the help of my brother Mark who drew all the graphics, I produced a vaguely similar game about a Roman trying to escape Pompeii. It was released eighteen years ago next month. I’m not very proud of the code, though if I find a way of reading 5¼” disks I may open-source it. I’m not very proud of the story, either: I based the story around Roman farces, and though I thought it was quite funny when I was fifteen, the humour strikes me as juvenile and sexist now.

When I had done with that game, I wrote something better. This was Avalot, released sixteen years ago, where approximately the same cast of characters found themselves in the Middle Ages. I had no computer science training at the time, and was unaware of the concept of a virtual machine, but Avalot took several steps towards being a game program written on a general-purpose engine (the engine was called Avalanche). If I had had the opportunity to produce the planned third game of the series, it would presumably have been entirely abstracted. Avalot featured actual puzzles worthy of the name: I’m rather proud of the one about the onion. And I made some attempt to patch up the problems with the story which had been introduced in the first game, as well; Avalot’s wife had a little more to her personality than simply the nagging character from farce. It wasn’t perfect in the least, but it was light years ahead of the original game. Of course, by that time the standard of professional games had moved on, and Avalot looked dated even when it was released.

Both games were released as shareware, and neither made any appreciable amount of money. I think we all had dinner out once on the takings from the first game, and the second game didn’t even make that. But occasionally I still get fanmail.

Sometimes I wonder whether I should resurrect the Avvy franchise as Z-machine games, and whether that would give me a chance to finally fix the immaturity of the original code and of the original humour. I’m not sure, really.

Venite prandete

Rio and I spent a good while today playing with Alice.  We made a table with a button which, if pressed, would cause a yellow chicken to fly over and spin around.  For her bedtime story I read her the first chapter of The Phoenix and the Carpet.

I should reiterate that, as I said at the time, I myself don’t think using WebKit in the window manager sounds like a good idea.  Since someone had raised the idea, I thought it was worth discussing, and now it certainly has been discussed.

I was trying to typeset some of my work the other day (for some reason), and I noticed how odd it looks to set sonnets in a sans-serif font…

Remember all the old familiar faces?
Helvetica’s the nicest of the lot.
Gill Sans and Johnston take the second places;
It seems as though the serif has been shot.
Verdana has its own intrinsic glories;
The fairest text that ever left my desk
Was set in these– for essays or for stories.
But using them for sonnets?  That’s grotesque.
And gravestones are a special case as well:
A mortal lack of serif fonts would be
A certain kind of typographic hell
With Comic Sans for all eternity.
In death, the Roman lettering is best.
May flights of serifs sing thee to thy rest.

WebKit question

Further to my earlier post, there has been some more talk over at the Metacity blog about styling windows with CSS.  There seem to be a few possible ways to implement this:

  • use libccss or plain libcroco for parsing, render it ourselves in GTK.  Not as crazy as it sounds because we’re only interested in a fairly small subset of CSS.  We could make this an external library and let other window managers use it as well.
  • use libccss for parsing rendering; this doesn’t yet work entirely
  • use nbtk for rendering; this doesn’t yet work entirely
  • use WebKit for rendering.  This would save work with rendering, but I’m a little wary of embedding a browser into the window manager.

I was also concerned that using WebKit would not allow us to restrict the CSS, which is something we need to do a great deal:

  • reinterpret all URLs to prevent themes from calling home, and to provide pixmap colourisation
  • allow references to theme colours (“GtkForegroundSelected”, etc)
  • not allow dangerous properties to be set at all (it should not be possible for a theme to set min-height on the frame, etc)

Now someone from KDE says that this could be a great opportunity for cross-desktop interoperability: Metacity and Kwin could share theme formats.  But apparently KWin already pulls in a WebKit dependency for other reasons, and the KDE person is planning to implement using WebKit anyway, and thinks that we should too.

I don’t know WebKit very well (and searching isn’t helping), but I thought some of you probably do.  So what I would like to know is:

  1. Am I being over-cautious about the footprint implications of an optional dependency on WebKit?
  2. Is it possible to heavily vet the CSS which WebKit reads?  I know WebKit allows host applications to modify the document in memory, but how easy is it to modify the styles?

The cap of maintenance

Cap of MaintenanceDoes a good farmer neglect a crop he has planted?
Does a good teacher overlook even the most humble student?
Does a good father allow a single child to starve?
Does a good programmer refuse to maintain his code?

The Tao of Programming, 5.4

I like to make things. I like it a lot. It’s one of my favourite things in the world. I also like to maintain things: not quite as much, but still a lot. It’s fun as well as being a responsible thing to do.

The problem is this: the one leads inexorably to the other. If you spend thirty hours on a project, and you do it well enough that the project succeeds, you will spend three hundred hours maintaining it. Now, you only have so much time. If you spend a quarter of your time making things and the rest maintaining them, eventually you will have to stop making things in order to have enough time to maintain the things you have already made. You will have to take off the wizard’s hat and spend the rest of your life wearing only the cap of maintenance. And that’s a shame, because while maintaining things is fun, creating new things is more fun, and more like play; it would be a shame for it to vanish completely from your life. Note that I haven’t said I’m talking about software here. I am, partly, but it applies to other endeavours as well: I’m also thinking of fiction and poetry.

This whole line of thinking started the other day because the users of four of my old projects (very reasonably) started asking for certain fixes, at the same time as I had some ideas for new projects: I always have ideas for new projects, probably more ideas than I have years left in my life. I began to realise that not only did I not have time to work on any new projects in themselves, I wouldn’t have time to maintain them if I did.

This is also part of why I’m so excited both about gnome-shell and about the ideas which came up for Metacity at Gran Canaria; I’ve been only fixing bugs in Metacity, rather than adding new features, for over a year now. And perhaps that’s necessary; I like the idea of projects asymptotically approaching perfection. But it doesn’t solve the problem of decreasing creation opportunity over time.

So what are the solutions? There aren’t any good ones.

  • Refuse to maintain old projects. This is silly and irresponsible.
  • Obsolete the project eventually. This is sometimes necessary, but it’s irresponsible to do it because you don’t have enough time to write fun code, rather than for technical reasons.
  • Spend your whole time working on new projects that nobody will use, either because you don’t distribute them or because they are of interest only to a deliberately tiny group of people. I think this was my unconscious strategy in software for years before I got into GNOME and discovered that writing things which were useful to people was actually far more fun. It’s still my strategy for poetry and fiction: I write things and perhaps put them up on my blog and then never even try to do anything with them to share them with the world.
  • Pay someone to look after it. This rather requires that your idea makes money and that you’re willing to found a company to run it. However, you need business and leadership skills, which isn’t true of me, and the thing you’re making needs to be capable of making money, which isn’t true of anything I’ve ever invented for myself.
  • Get someone else to maintain what you make. The first piece of free software I wrote which was actually useful to people was Gnusto, and that was a maintenance nightmare because it used lots of rarely-touched parts of the Mozilla API which kept changing at no notice. Fortunately someone else picked it up and turned it into Parchment, making it something more wonderful than it had ever been. I suppose there must be people out there who enjoy doing maintenance more than making things, but perhaps not: finding people who are willing to take on your stuff is not really a trivial task. Of course, someone else will have to take over maintenance of your projects one day anyway, unless you happen to be immortal.

Is there a sustainable way to balance the desire to make new things with the ongoing responsibility to maintain them?

Photo © Jeff/Godfrey, cc-by-nc.