Auto-locking My Computer When I Walk Away

The other day, while I was wating for several GB to transfer over the network at work, I finally got around to setting something that’s been dancing at the back of my mind for a while: computer-based proximity detection using Bluetooth.

I have a Treo 650.  It has Bluetooth.  I also have a USB Bluetooth Adapter.  I originally planned to carry the bluetooth adapter around and hook it up to different computers whenever I wanted to talk to the Treo, but I’ve only been using it at work, so I’ve been leaving the adapter connected to my Linux computer at work.  The thought occurred to me that I could use the Bluetooth adapter to see whether my phone was nearby and do things based on that information.  At least to start, I decided to have the computer lock itself when I wasn’t around.

I have the BlueZ Bluetooth stack installed.  (On Debian, that’s the bluez-utils package.)  They include a l2ping program, but that establishes a full Bluetooth connection with the device, which makes my Treo turn on the screen, play a little sound, and show a pop-up dialog.  That’s a little intrusive for something that I want checked several times a minute.  Some people use hcitool rssi to find out the strength of the phone’s (or other device’s) Bluetooth signal.  That also requires a full Bluetooth connection.  I ended up using hcitool name, which returns the name of the device if it’s found and nothing if it’s not.  More importantly, it doesn’t cause the Treo to do anything but silently send its response, and it works even if the Treo screen is off.

So I now have a stupid little shell script that looks like this:

#!/bin/sh

PHONE_ADDR=01:23:45:67:89:AB
PHONE_NAME="glamdring"
WAIT_TIME=15

while true; do
  if [ "$(hcitool name $PHONE_ADDR)" \!= "$PHONE_NAME" ]; then
    xscreensaver-command -lock
  fi
  sleep $WAIT_TIME
done

There are programs for Windows that do similar things.  Possibly one of the simplest is Blue Lock, which is also open-source (and written in Delphi).  I’m probably just going to write a simple Windows program to listen on the network for a message from my Linux computer to tell it to lock the screen.


New Site Hosting

In the interests of better site availability and less Comcast AUP-breaking, I’ve finally gotten around to outsourcing my website hosting.  I’m currently at NearlyFreeSpeech.net, a webhost committed to the twin goals of free speech and affordable web hosting.

How free is their speech?  Read their Abuse page:

“A NearlyFreeSpeech.NET member site is defaming me or otherwise injuring me civilly.”

Please forward a copy of your legal finding from a court of competent jurisdiction to our contact address. If you have not yet obtained such a finding, a preliminary injunction or court order is also sufficient.

If you are not able to obtain the above, you will need to work directly with the site operator to resolve your differences. We will have to fall back on our members’ contractual assertion that the content they upload is legitimate and therefore we will not be able to get involved

How affordable is their hosting?  You pay only for the bandwidth and storage that you actually use: $1 per gigabyte of bandwidth and $0.01 per megabyte-month of storage.  (Plus the bandwidth cost goes down the more you use.)

They support a variety of CGI scripting languages, including C, PHP, Perl, Python, and Ruby.  Oh, but also Fortran, Tcl, Lisp, Scheme, OCaml, and Haskell.

We’ll see how it goes, but I think I’ll like it here.


Java Reflection

I’m only a few weeks into my Java class and I’m already annoyed at the language.  I’m completely willing to ascribe this to newbieness, where I’m just not working with what the language gives me, but the metaobject stuff in Java seems a bit painful.

I’m working on a project for the class where I have to accept input in several different units of heat (BTUs, calories, and joules) and output the measurement in joules.  I’ve made an abstract base class for the various units and created concrete classes for each unit the program has to read.  I’d like to just have a list of available classes and have my program enumerate them automatically (rather than hardcoding the behavior for each), but the way I would normally think about doing this is painful in Java.

In Delphi, I’d do something like this:

type
  THeatUnits = class
   public
    constructor Create(Value : Real); virtual;
    class function GetUnitsName : String; virtual; abstract;
    function ConvertToJoules : Real; virtual; abstract;
  end;
    
  THeatUnitsClass = class of THeatUnits;

  TBTUs = class(THeatUnits);
  TCalories = class(THeatUnits);
  TJoules = class(THeatUnits);

const
  availableUnits : array [1..3] of THeatUnitsClass
                 = (TBTUs, TCalories, TJoules);

... 

procedure DoStuff(Index : Integer; Value : Real);
  var
    Units : THeatUnits;
begin
  Units := availableUnits[Index].Create(Value);
  // Now do things polymorphically with Units. 
end;

Delphi’s class types often seem like a quick hack to me, but they beat what Java does in the same situation.  For one thing, there doesn’t seem to really be a class type in the same way that Delphi does it.  There are instead objects of type Class.  As far as I can tell, the best way to get one of those is to call Class.forName("ClassName").  But the painful part is that there’s no specialization of class types at compile time, so the Java code equivalent to my Units := availableUnits[Index].Create(Value); above would be something like this:

static final AVAILABLE_UNITS = new String[] ("BTUs", "Calories", "Joules");

public void doStuff(int index, double value) {
  Class unitClass = Class.forName(AVAILABLE_UNITS[index]);
  Constructor unitConstructor = unitClass.getConstructor(new Class[] (double.class));
  HeatUnits units = (HeatUnits)unitConstructor.newInstance(new Object[] (new Double(value)));
  // now do things polymorphically with units. 
}

(Common Lisp, of course, would be more succinct than Delphi, because everything is first-class; I would probably do something like this:

(defclass heat-units () ())

(defgeneric get-units-name (unit-class))
(defgeneric convert-to-joules (unit-class))

(defclass btus (heat-units) ())
(defclass calories (heat-units) ())
(defclass joules (heat-units) ())

(defparameter +available-units+ #(btus calories joules))

(defun do-stuff (index value)
  (let ((units (make-instance (svref +available-units+ index)
                              :value value)))
    ;; Now do things polymorphically with units. 
    ))

)

As I said, though, I think this is just an artifact of not thinking in Java to the appropriate degree.  Most of Java’s reflection stuff seems set up to be useful at run-time while Delphi’s run-time reflection is much uglier than Java’s.  And I think I’m going to approach my Java problem from a different direction, with an enum and a factory method.  I was still struck by how annoyingly wordy (and not entirely typesafe) my first approach turned out to be in Java.


Pumpkin Pie

This is from smakelijke gerechten, a cookbook assembled by the First Reformed Church in Oak Harbor, Washington in 1971.  My grandmother’s sister belonged to the church, and the cookbook was a gift for my grandmother’s birthday.

  • 2 eggs, slightly beaten
  • 1-3/4 cups pumpkin puree (1 can)
  • 3/4 cup sugar
  • 1/2 teaspoon salt
  • 1 teaspoon cinnamon
  • 1/2 teaspoon ground ginger
  • 1/4 teaspoon ground cloves
  • 1-2/3 cups evaporated milk (1 12-oz can)
  • 1 9" pie crust, unbaked

Preheat oven to 425°F.  Mix all ingredients.  Pour into pie crust.  Bake at 425°F for 15 minutes, then reduce temperature to 350°F and bake for 45 more minutes or until a toothpick inserted in center comes out clean.


Grandma's Potato Salad

While I was down in Texas for Thanksgiving, I got my grandmother to give me a couple of her recipes.  Unfortunately, she was so familiar with the ingredients that she didn’t have any measurements more specific than “‘Til it tastes right,” so that’s all I can provide at the moment.  I plan to make the recipe a few more times myself and figure out what measurements work well.

  • 3-4 lb. potatoes
  • 4 eggs
  • 1 medium white onion, diced
  • mayonnaise
  • pickle juice
  • curry powder
  • salt
  • pepper
  • yellow mustard
  • paprika

Scrub and boil the potatoes.  Hard boil the eggs.  When the potatoes are done, let them cool then peel them—the skins should rub right off.  Cut the potatoes into roughly 1" pieces.  Dice the eggs.  Mix together potatoes, eggs, and onion.

In a separate bowl, mix the mayonnaise and pickle juice until the mixture is the right consistency.  Add the curry powder, salt, pepper, and mustard to taste.

Mix the dressing with the potatoes, then sprinkle paprika over the salad.