[ Content | Sidebar ]

Archives for programming

Using SLIME with Chicken Scheme

December 12th, 2010

Common Lisp programmers are blessed with the awesome SLIME development environment for Emacs; but sadly Schemers have nothing comparable. There are a few SLIME backends for various Schemes around, in varying states of completeness, but as far as I can tell none for my Scheme implementation of choice: Chicken Scheme.

Therefore I present swank-chicken: a SWANK server written in Chicken Scheme. (SWANK is the protocol SLIME uses to talk to the inferior Lisp process.) I’ve been hacking it together over the past week or two and it’s now reached the point where it is more or less usable. Click on the screenshot below for an example:

Supported features so far:

  • REPL support with input/output (i.e. read and write work as expected)
  • Complile region and load file commands (e.g. C-c C-l, C-c C-c)
  • Basic SLDB support with back trace
  • Parameter argument hints in the minibuffer

Huge swathes of SLIME features are obviously missing at the moment, but the most useful ones are there. In particular, I think it’s at least as functional as the normal inferior-scheme mode. The most significant omission at the moment is support for interrupting Scheme evaluation from Emacs – C-c C-c at the REPL will currently kill the backend process completely. Naïvely this should be implemented in the same way as the debugger-abort command: by calling the top-level continuation. However, perhaps unsurprisingly, calling a continuation from a Scheme SIGINT handler turns out to be a bad idea. So if you have a runaway process you must kill and restart it with C-c C-c M-x slime-restart-inferior-lisp.

UPDATE: some of these issues have been fixed.

To get started clone the git repository using:

git clone git://github.com/nickg/swank-chicken.git

Alternatively just download swank-chicken.scm and chicken-slime.el.

First we need to install a few Chicken extensions:

chicken-install format symbol-utils

Obviously you also need to install SLIME: I’ve been using the latest code from CVS but older versions should work too.

Next, place chicken-slime.el somewhere in your Emacs load-path and add the following to your .emacs:

(autoload 'chicken-slime "chicken-slime" "SWANK backend for Chicken" t)
(setq swank-chicken-path "/path/to/swank-chicken.scm")
 
(add-hook 'scheme-mode-hook
          (lambda ()
            (slime-mode t)))

Now you should be able to use SLIME commands in a Scheme buffer. To start a Scheme REPL do M-x chicken-slime.

That’s it! Let me know if you have any problems/suggestions. I intend to do some more work on this and package it up as an Egg. The code is available under the MIT license.

Qemu resizing blurriness

September 25th, 2010

Are you annoyed when Qemu randomly resizes its window causing all the text to be scaled and blurred? Maybe it’s just my window manager, but here is a trivial patch to disable that annoying feature:

--- sdl.c.old   2010-09-25 13:30:48.000000000 +0100
+++ sdl.c       2010-09-25 13:30:50.000000000 +0100
@@ -102,7 +102,7 @@
 
     //    printf("resizing to %d %d\n", w, h);
 
-    flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_RESIZABLE;
+    flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
     if (gui_fullscreen)
         flags |= SDL_FULLSCREEN;
     if (gui_noframe)

Cowshell

July 2nd, 2010

Blodgett just reminded me of another useful tool we made a few years ago: cowshell! Cowshell provides a more user-friendly interface to the Linux shell by wrapping the output in multi-coloured cows. Here is an example session:

Cowshell is a Perl script that you can acquire here. You will need cowsay and maybe some Perl modules for it to work.

My first WordPress plugin

June 15th, 2010

I’ve written a WordPress plugin to generate a browser for all the NextGEN Gallery albums that you have. You can see it in action on my photos page.

To install you must be using the NGG plugin (obviously) and then just place ngg-album-browse.php in your wp-content/plugins folder. Activate it, then you can use the following shortcode to insert it in a page or post (without the spaces):

[ albumbrowser ]

You can also display albums in a random order:

[ albumbrowser order="random" ]

Giraffify

May 22nd, 2010

A few years ago blodgett and I developed a useful tool for visualising what would happen if you grafted someone’s face onto a giraffe. We implemented this ground-breaking tool in GIMP script-fu and promptly forgot about it. I only rediscovered it recently when clearing out my home directory. Here is the program’s best guess at what our beloved leader would look like were he a giraffe:

In the process of scraping off the bit rot from giraffify I remembered why we didn’t develop it further: although script-fu is joyous Scheme the interface to the GIMP is aggravating to say the least. Perhaps this is why they now have Python-fu.

If you want to perform your own giraffification simulations you need the following files:

  • giraffify.scm – place this in ~/.gimp-2.6/scripts
  • giraffe.jpg or another picture of a giraffe – place this in your home directory or GIMP working directory

To use, open your source image in the GIMP, select the “free select tool” and mark the border of the target face. Then go to “Script-Fu” then “Shiny” and the “Giraffify” and behold the startling results of giraffification!

Cute objcopy hack

May 7th, 2010

I learned a useful trick today for embedding arbitrary data in an ELF executable. This is an easy way bundle RAM disks and other resources when you’re running on bare hardware.

The first step is to turn our binary data into an ELF object file:

echo yahyahyah > yah
objcopy -I binary -O elf32-i386 -B i386 \
    --rename-section .data=.yah yah yah.o

The -I and -O mean we are converting from raw binary to our target format elf32-i386. The -B option sets the architecture field of the output file: this is required by ld. By default the binary data is placed in the .data section; --rename-section changes this to a name we can refer to in our linker script. You can omit this flag if you don’t care about the layout of your executable.

nick@pickle:~$ objdump -x yah.o 
 
yah.o:     file format elf32-i386
yah.o
architecture: i386, flags 0x00000010:
HAS_SYMS
start address 0x00000000
 
Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .yah          0000000a  00000000  00000000  00000034  2**0
                  CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
00000000 l    d  .yah   00000000 .yah
00000000 g       .yah   00000000 _binary_yah_start
0000000a g       .yah   00000000 _binary_yah_end
0000000a g       *ABS*  00000000 _binary_yah_size

You can see objcopy has created three symbols which we can use to access the data at runtime like so:

extern void _binary_yah_start, _binary_yah_end;
extern void _binary_yah_size;
 
void *yah = &_binary_yah_start;
size_t yah_size = (size_t)&_binary_yah_size;

By renaming the output section we can place the data wherever we like using a linker script like this:

.yah : AT(some_address)
{
    *(.yah)
    . = ALIGN(4096);   /* Optional */
}

What fun! Hopefully someone will find it useful.

Syncing SanDisk/GMPC cover art

May 5th, 2010

Do you have a SanDisk MP3 player? Do you use GMPC? Do you read this blog? I suspect that set is very small but if you are in this demographic here is a treat for you: a Ruby script to copy the cover art downloaded by GMPC onto your SanDisk device.

Here it is: sandisk-cover-sync.rb

Just run it with your device mounted under /media and it will figure out the rest.

It should be easy to hack to work with other devices or Amarok, etc.

Some thoughts on trees

February 27th, 2010

I’ve been wondering for a while now what is the best way to add trees to Train Game. Trees seem fairly important for a train game seeing as trains mostly travel through the countryside where there are trees. (Except for my daily commute to work through Slough where there are no green things.)

Anyway my first attempt was to use flat quads with an alpha-blended tree textured onto them. These are commonly called “billboards” if you didn’t know. The quads are then oriented to always point at the camera, hopefully giving the illusion of a fully 3D tree. While the maths to calculate the billboard orientation is a bit tricky, these trees are incredibly cheap at runtime. Blodgett was kind enough to use its artistic talents to produce some tree pictures for me:

While these trees look quite good from a distance, when you get up close and especially when look from above the rotation and obvious flatness is a bit disconcerting.

So next I tried a completely different approach to tree rendering. Inspired by examples of procedural plant generation e.g. here and here I implemented an L-system tree generator – albeit a much simpler one than in those examples. Here’s a screenshot showing some examples of trunks and branches:

I think these look rather good. Unfortunately there are two problems: rendering them is incredibly slow, and I found it very difficult to generate decent looking leaves. Since trees are only scenery and I’d like to have a lot of them on the screen at once, it’s not really acceptable to have them use more of the render-cycle budget than the train itself. Simplifying the branch structure and using square leaves, plus some OpenGL performance tricks, gets them running at a reasonable speed, although they don’t look nearly as good:

Incidentally, these screenshots are showing the new map editor which ditches FLTK and replaces it with my own home-made XML GUI toolkit. I’m rather pleased with it design-wise and it plays much nicer with the rest of the game (it renders directly onto an OpenGL texture).

Anyway, back to trees and my conclusion from the L-system experiment is that I didn’t like them that much, they were expensive to render, and perhaps most importantly, they didn’t fit in with the rest of the artwork in the game (in the “childishly cartoonish” style). So I abandoned the code in a git branch and went for the simplest option: make some models of trees.

Thus far I’ve made two tree models – a pine tree and an apple tree – both around 80 polygons. They are very cheap to render – cheaper in fact than the billboard trees since I have an optimised VBO-based mesh renderer.

I really do like these trees! I think they’re cute but I’m a little worried other people will think they’re daft. Anyway, I’m sticking with them for the time being.

In a month or so I will tidy up the code in git and release another demo/preview version. Hopefully there will be some useful features by then ;). In the meantime you could look at my gitweb if you’re curious or clone http://www.nickg.me.uk/~nick/git/traingame.git.

Thank you muchly to blodgett for assisting with tree evaluation!

Valentine’s day

February 14th, 2010

Today is Valentine’s day. Valentine’s day is all about love, and the internationally recognised symbol of love is the stylised heart. I love sed. This is probably why I spent this evening alone writing a filter to format text as ASCII hearts:

nick@pickle:~$ sed --posix -n -f hearts.sed < poem
     Shall        I comp
   are thee t   o a summer
 's day? Thou  art more love
ly and more temperate. Rough 
winds do shake the darling bu
ds of May, And summer's lease
  hath all too short a date.
    Sometime too hot the e
     ye of heaven shines
       , And often is 
         his gold co
           mplexio
             n d
              i
     mmed;        And ev
   ery fair f   rom fair s
 ometime decli nes, By chanc
e, or nature's changing cours
e untrimmed. But thy eternal 
summer shall not fade Nor los
 e possession of that fair t
   hou ow'st; Nor shall de
     ath brag thou wand'
       rest in his sha
         de, When in
            eterna
             l l
              i
     nes to        time 
   thou grow'   st, So lon
 g as men can  breathe or ey
es can see, So long lives thi
s, and this gives life to the
e............................
 ...........................
   .......................
     ...................
       ...............
         ...........
           .......
             ...
              .

You can download the script here: hearts.sed.

As far as I can tell, girls are mad keen on standards compliance. So I went out of my way to make this script POSIX compliant.

I’ve reproduced this labour of love below. As you can see, it demonstrates the elegance and practicality of sed as general purpose programming language:

: gather
/^[[:space:]]*$/d
s/[[:space:]]\{1,\}/ /g
/.\{251\}/{
  s/^\(.\{6\}\)\(.\{6\}\)/     \1       \2\n1/;P
  s/^.*\n1\(.\{10\}\)\(.\{10\}\)/   \1   \2\n2/;P
  s/^.*\n2\(.\{13\}\)\(.\{13\}\)/ \1 \2\n3/;P
  s/^.*\n3\(.\{29\}\)/\1\n4/;P
  s/^.*\n4\(.\{29\}\)/\1\n5/;P
  s/^.*\n5\(.\{29\}\)/\1\n6/;P
  s/^.*\n6\(.\{27\}\)/ \1\n7/;P
  s/^.*\n7\(.\{23\}\)/   \1\n8/;P
  s/^.*\n8\(.\{19\}\)/     \1\n9/;P
  s/^.*\n9\(.\{15\}\)/       \1\nA/;P
  s/^.*\nA\(.\{11\}\)/         \1\nB/;P
  s/^.*\nB\(.\{7\}\)/           \1\nC/;P
  s/^.*\nC\(.\{3\}\)/             \1\nD/;P
  s/^.*\nD\(.\)/              \1\n/;P
  $ { d; q }
  D
}
$ { s/$/\./; b gather }
N
s/\n\n*/ /g
b gather

Adding similar songs from LastFM to your MPD queue

November 30th, 2009

Here’s a cute hack which uses LastFM to find songs similar to the one currently playing in MPD and add them to your playlist: more-like-this.rb.

To use it just run more-like-this while MPD is playing and 10 similar songs will be appended to the playlist. Give it a numeric argument to add a different number.

You’ll need a few Ruby gems for this to work:

gem install librmpd scrobbler

So, yes, I’ve finally switched from Amarok to MPD. Sadly after the greatness of Amarok 1.4, each new release takes giant backward steps in usability – at least for my use cases :(. GMPC is OK as a client, and I’m really liking the programability.