- Blog -

(A cheesy homepage for Justin Collins)
Tiny App Launcher

Story

I just upgraded my lab desktop to Mandriva 2008.1. As usual, my first action was to install Xfce. That all worked well until I hit my usual Alt+F2 to launch a program. Nothing happened. I tried again. Still nothing. So I checked out the keyboard shortcuts, thinking they might be messed up. After actually finding where Alt+F2 gets set (there are a few different places in Xfce where keybindings are created), I saw it was indeed set up to run xfrun4, Xfce’s little run dialog.

I headed to the console (another aggravation: I am used to using gnome-terminal for my console, because I like its tabs and general feel. But it is way too slow for some reason and, even more importantly, I cannot figure out how to make the cursor stop blinking. It drives me nuts) and ran xfrun4 from there. Result: segfault

After poking around to see if there are any other little things like xfrun4 (something I have done before, actually), I gave up and wrote my own.

Requirements

Code
#!/bin/env ruby
require "rubygems"
require "zerenity/entry"
require "zerenity/warning"

loop do
        begin
                program = Zerenity.Entry(:title => "Run")
                if program
                        exec program
                end
                break
        rescue Interrupt
                break
        rescue Errno::ENOENT
                Zerenity.Warning(:text => "Program not found.",
                                 :title => "Error")
        rescue Exception => e
                Zerenity.Warning(:text => e.inspect, :title => "Error")
        end
end

Thrilling Conclusion

I dropped this in a file in /usr/bin/ and changed the keybinding…and that’s it. Works well enough, though it would be nice to have autocomplete/history stuff. But Zerenity does not have that (yet).


My Favorite Kind of Error

Don’t you just love seeing things like this?

[Interpreter] sig_pipe(): Shouldn't get here ??

The Light at the End of a Tunnel Sorta

So I mentioned the shared whiteboard thing previously. I have been facing some serious headaches over one of the projects in particular. I don’t want to mention the name, but it’s been a real pain.

Why has it been such a problem? Well, let us see…first of all, it is not just one project, but two: a modified Java KVM and a modified J2ME compiler, two separate projects by two different people. I was able to track down the modified KVM code, which has been both a good and a horrible thing (horrible because it’s lead me down twisty paths I would not have known existed otherwise). However, I do not have the source for the modified compiler. This means that, while I can peer into the inner workings of the KVM and even poke around a bit, I cannot recompile the entire thing. This means no adding libraries or features not already present, because if I write it in C, the (modified) compiler may not compile them and if I do it in Java the KVM may not run it.

However, I am getting ahead of myself. What was the beginning of the issue? Quite simply, I needed to write an application with a GUI. Didn’t seem like a problem. But it is, because the project I need to use underneath is an extension of J2ME, and not the fancy MIDP version, but the tiny, teensy weensy CLDC version. That means no GUI.

BUT! I was in luck, because the person who extended the KVM (Kilobyte Virtual Machine…the J2ME VM from Sun) provided a nice little IPC interface to use with external programs. Cool! Oh, wait…the API is in C?? Why?? (Note: Probably because after working to an extend a virtual machine written in C, writing some shared memory IPC stuff was likely a cinch.) Java would have been so much more convenient…

But, wait again. I don’t need to write the GUI in C, though. Doesn’t Ruby have nice C bindings? Reads up on those… Yes, cool. Oh, wait, there’s a neato thing called SWIG which will even do most of the work for me? Neat! So I spent a while getting that all working, including writing wrapper methods (in C) and everything. I got to the point where Ruby would load the C libraries and even run them. But then I hit more trouble.

Most of the library is nice low-level handling of semaphores. And it was not working properly. I believe I traced this to the shared memory not being written correctly, thus giving me bogus semaphore IDs. However…I can’t change that part. I was able to mess around with the IPC code, because it was thankfully separate from the rest of the VM (that is, the API was). But it’s no good if the VM itself needs changed.

So I finally gave up. On the IPC stuff anyways. I looked at what I had available and decided, no matter how ridiculous it is, it’s better to be ridiculous than not working at all.

Before I explain the silliness I ended up with this evening, let me explain a bit how these VMs work. You do not simply run programs on them. Oh, no. First you start the VM (including setting up a virtual network topology), then you can either ‘inject’ applications into the system, or register services at particular VMs, which the applications can visit.

Also, the only kind of I/O really available in the limited world of J2ME is printing out to stdin and stderr. That’s pretty much it (in this implementation). I couldn’t open sockets, read files, or anything. At least, I couldn’t figure out how to.

So, what did I do? Well, something like this: I have a GUI written in Ruby and Tk, which will launch the VM and attach a file handle to the stdout of the VM. It then registers a ‘Whiteboard’ service, which will receive messages from other VMs regarding what’s happening on the remote whiteboards. So far, so good. Now, if the VM wishes to communicate to the GUI, it can do so by simply using a System.out.println(). Good enough, Ruby makes it easy to parse and deal with such things. But what about from the GUI to the VM? Well…what it does is ‘inject’ an application into the system, passing any information it needs to as command line arguments. Yes. That is right. Command line arguments for IPC.

Amazingly…it works. It’s going to take a little more tweaking, but I now have more to show for approximately two hours of work than the past days I’ve spent fighting with C APIs and semaphores and accessing shared memory and digging through J2ME documentation for hope.


UI in Java and Ruby

For my research, I need to implement a shared whiteboard, which is a fairly common example of collaborative software. I’m doing it with three different projects handling the communication side of it. Since two of these can be directly tied in with Java, it made since to start with a Java UI. The third, for some inexplicable reason, chose to implement IPC through a C interface. So, for that one, I’ll be writing the UI using Ruby.

I have completed a basic prototype for both, which just implements the ‘drawing’ part of the whiteboard. They have exactly the same amount of functionality (draw lines, save the history) and do it basically the same way (start line on mouse click, save points and draw as the mouse moves). I didn’t try to make these as short as possible, just as equivalent as possible.

The Java version, using AWT, is 95 lines long. The Ruby version, using Tk, is 50 lines long. Not too big of a difference, considering how short they are.

However…each line in Java seems quite a bit longer than the Ruby version. What if we compare based on the number of characters, rather than the number of lines? Hmmmm…the Java version comes out with 2831 characters, while the Ruby version only has 978! That is a lot less typing! (A third, basically).

Oh, well. Sigh…

Edit: Oops, I forgot I also wrote a convenience class for the Java version that stored the history. But it wasn’t really necessary and I did not do so in the Ruby version, so the comparison above is still accurate, I think.


Constants in Ruby
In ruby, constants are more like variables that yell at you.
-- Eric Hodel, Ruby Mailing List

Sqwee and Unicode

As someone quite correctly pointed out, Sqwee wasn’t supporting Unicode very well. In fact, not really at all. You could put it in, but then things got weird. You might not think this would affect me much (I am generally a pretty ASCII kind of guy), but what happens when I want to do a little mathematical notation? Perhaps I need to write down {x, y} ∪ {z, y} for some reason. That would make Sqwee mildly blow up.

So I tentatively started poking into the Unicode world, expecting at any moment to have a dragon fry me to a crisp or to fall into some large crevasse. Interestingly, neither happened, but it was a little bit of a journey.

First, I found out all I needed to do was add a nice little meta-tag:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

Simple enough, right? I tried it. Didn’t help a bit. Manually switching the encoding (in Firefox) did work, though. After examining the headers using Live Headers, it was clear the content type was still being sent as iso-8895-1.

I figured this was due to the Lighttpd webserver not setting the content type correctly, so I went about trying to change that. I thought it would be possible to change it in the mimetypes:

mimetype.assign             = (
  ".html" => "text/html; content-type: utf-8" 
)

I saw this suggested somewhere, but it didn’t help.

Then I read that in Prototype Unicode doesn’t work with “get” and only with “post”, so I tried that:

 new Ajax.Updater("main", "index.rhtml", {
           method: "post",
           //...
        });

But it didn’t help either!

At this point I was getting somewhat frustrated, but then I remembered that eruby generates its own headers. Perhaps that was the problem?

Indeed it was! Fortunately, it is possible to set the content type via a commandline switch:
usage: ./eruby [switches] [inputfile]

  -d, --debug           set debugging flags (set $DEBUG to true)
  -K[kcode]             specifies KANJI (Japanese) code-set
  -M[mode]              specifies runtime mode
                          f: filter mode
                          c: CGI mode
                          n: NPH-CGI mode
  -C [charset]          specifies charset parameter for Content-Type
  -n, --noheader        disables CGI header output
  -v, --verbose         enables verbose mode
  --version             print version information and exit
Now I was in business! I went ahead and changed the Lighttpd config file to add in the appropriate switches:

cgi.assign = (".rhtml" => "/home/justin/sqwee/bin/eruby -Ku -C utf-8")
Unfortunately, this doesn’t work at all, because Lighttpd uses stat to check if the specified program exists and the options mess it up. To work around this, I needed to create a little script to launch eruby with the appropriate options:

#!/bin/env ruby
require 'pathname'
exec("#{Pathname.new(__FILE__).dirname}/eruby -KU -C utf-8 #{ARGV.join(' ')}")

That worked perfectly, and suddenly Sqwee had Unicode! That means time for a new release.


More Sqwee

Yes, I’m going talk more about Sqwee. I think it’s mostly because I’m excited about writing software with real version numbers… just kidding.

I’m still working on a good description of it. It’s incredibly simple stuff, but kind of hard to explain. It’s like a wiki, without lots of revisions and multiple users. So it’s like a personal wiki. But it is oriented towards being quick to install and quick to use, while retaining the flexibility for people to use it as they like. That’s why I sort of have the ‘prepackaged’ and ‘no frills’ versions, for running on a desktop and a shared server, respectively.

Anyways, enough blabbing about that stuff. I have been using Sqwee to take notes for a couple weeks now, and it actually works pretty well. But I noticed a couple of things that bugged me quite a bit:

First, if you are editing a page (taking notes) and you want to save it, but keep editing, you have to click ‘save’ and then ‘edit’ again. So I added the ‘quick save’ feature which will save the page but keep you in editing mode.

But then…how do you know it has really saved it and there wasn’t some problem? Or maybe it takes a long time to save or load a page…it would be nice to know when that kind of thing is “working on it” and when it has failed. So what I do now is have the button you press “gray out” while it is work in, then come back when it is complete. It works well when things are slow, but isn’t annoying when they are fast.

Anyhow, the point is that version 0.2.1 is out and I’m excited about the changes. Which, I forgot to mention, include the ability to search.


My first official software release – Sqwee

Yes, so. That project I previously called “MyWiki” has been rebranded as Sqwee – Simple Quick Web Editing Engine. Which is silly, but I like the acronym.

This project came about because I was using Pimki for keeping track of various thoughts and things, but accessing my home server remotely started to become a pain (stupid ISP, I imagine). It became such a pain that I got a real hosting plan with Site5 (link over there somewhere) and this nifty domain name.

But! I didn’t think Site5 would appreciate me running Pimki because it is tied up with WEBrick and therefore had to be running as a background thing and use a non-standard port. I looked around for something Ruby-based (like Pimki) but easy to set up on a shared host.

Sadly, I didn’t find anything that I liked. So, as these stories usually go, I made my own. I had some specific goals in mind:

  • Easy to install
  • Fast
  • Simple design
  • Quick creation and editing of pages

Out of that came the very minimalist Sqwee. It’s like a wiki, but only in that you can dynamically create and edit (and delete) pages. There are no accounts, revision tracking, or other bells and whistles (on account of wanting to keep it fast). It doesn’t even have password protection built in…but that’s because it would be a pain and any web server will have that already.

There are some planned features, but the idea is to not let anything slow down page rendering or creation. In fact, pages are basically stored as static HTML. The only “rendering” is putting the header, navigation section, and the content together, like many websites do.

This was my first time doing AJAX stuff, so I went ahead and used the Prototype library to get going. It’s pretty nice. So that means nice, quick creation and editing of pages. I like it.

Well, that’s probably enough rambling about Sqwee. If you are curious, see the website or check out the demo.


Updates to Schme2Lisp

Alright, first of all, a word of caution. A big word of caution: Since LISP does not have a false value, but uses NIL or (), those are considered to be false in LISP. In Scheme, however, () is NOT FALSE (but true). So when this little script converts from #f to NIL, be careful.

I only say this because I just spent a bunch of time trying to figure out where the translation went wrong, because I was relying on () being true.

Another thing to look out for is the list-ref -> nth conversion, which may not always be correct. I can’t remember.

Changes to Scheme2Lisp:

  • Very important: equal? -> equal (not eql)
  • Added: (define (bob)) -> (defun bob ())
  • Added: (list-ref) -> (nth)

GUIs with Shoes. GUI Shoes. Something like that.

Shoes is a shiny, tiny new GUI library under development. I was peripherally aware of Shoes already, but then I went looking for an easy way to write a GUI for a small application that I could develop easily and also have run under OS X (on my girlfriend’s laptop…I don’t have a Mac).

So I took a look at Shoes again to see what was up with it. What’s nice? Well, it’s simple…sort of. It let’s you do a lot with a little, which is good. It’s also awesome that you can run it on Linux, OS X, and Windows (XP and Vista). What’s bad is that it is rather unclear what you can do and how you can do it so far. I was trying to look through the sample programs and get up to speed as quickly as possible, but wasn’t really getting it. Documentation is really scarce, so I was bummed. I didn’t really want to spend the (very small amount of) money to order the Shoes book

Then, as if in response to my sadness, _why graciously uploaded the Shoes reference page. Whee! Now I am in business. I’m also using KirbyBase for this, which is a nice light-weight, text-based DBMS.