Friday, April 10, 2009

GTK-server and Erlang

I just stumbled across GTK-server in the last couple of days (occasionally /prog/ is of some use), and it struck me (as it has others) that this would make a good companion to Erlang for GUI applications. Of course, this was just before discovering that the new Erlang 5.7/R13 beta release has an experimental wxWidgets binding included; but for the moment, not only is the GTK binding rather more mature, it also has a more immediately obvious architecture for use in an Erlang application. The process separation also avoids any shared-memory issues that having a direct FFI style binding with opaque pointers could be prone to.

The general structure of a full program would be

  • spawn a process that launches the GTK-server executable
  • in a short while, construct the UI widgets
  • run the main loop as a tail-recursive call that handles events. If there are long-running activities, they should be separate processes with the usual message passing.

The following sample isn't quite like that because there is nothing calling back to it. The full implementation it would need to have non-blocking TCP, interleaved with whatever incoming messages needed to forward to the UI process. It would also benefit from some better data structure than the flat argument list here to loop/4...

A finished application would also need some real error handling, but that's possibly overkill for a proof-of-concept.

You can use fifos instead quite happily. On Windows, it looks like this:

with the rest of the changes being systematic.

Post a Comment