LogitechLCDTool Released, Some Developer Notes...
Developer — 3 Jan 2007 13:38 — 1960 days ago

I just finished the first version of LogitechLCDTool, which provides the missing Mac support for some features of Logitech’s Z-10 USB speaker system. Go check it out if you run these speakers on a Mac, or if you didn’t buy them for lack of Mac support...

Here are some notes which might be interesting to developers.

There are also some infos in this blog post by Derrick Story over at oreillynet.com.

What the program does is render a tiny black-and-white HTML page in a WebView and then capture a bitmap representation of that and send it to the speaker display over the USB bus. It also listens for USB button events and runs some AppleScripts in response or forwards the events into the WebKit JavaScript environment in case a script in there is listening.

The program also injects a proxy object into the JavaScript environment. The proxy provides methods which the JavaScript code can call back to request services from the host application. One set of methods offers the ability to compile and run AppleScript code, so it’s really an interesting combination of Objective-C, JavaScript and AppleScript which provides a lot of flexibility.

It was a pleasure to implement this, the scripting support in WebKit that combines the JavaScript and Objective-C worlds is very elegant and well-documented. It is a great way to provide extensibility in an application, especially if you need to provide user-customizable visual results.

Getting a bitmap representation worked well after I used NSBitmapImageRep’s initWithFocusedViewRect: method which produced a faithful reproduction of the bitmapped, aliased fonts as visible on the screen. I first tried loading the result of NSView’s dataWithPDFInsideRect: into a new NSImage but that resulted in anti-aliased fonts and changed geometry.

The most difficult part was figuring out how to send data to a USB device using the USB HID API. I found the terminology confusing and there doesn’t seem to be example code for data output, just input. A piece of code from a Rogue Amoeba engineer helped me a lot.

I was forced to use the HID API because direct USB communication was prevented by the fact that Mac OS X recognizes the HID devices and grabs exclusive access. It seems I could have changed this with a code-less kernel extension, but I wanted a clean, self-contained application so the USB HID API was the logical choice.

After some funky noise patterns on the LCD screen, some Mac OS X kernel panics caused by unplugging the USB cable and some Z-10 reboots to fix speaker crashes caused by malformed data packets, I finally got my images displayed on the speaker. After that I wrapped the raw C API in more convenient Objective-C code and transformed the USB HID queue events to NSNotifications to isolate the rest of the program from the USB APIs.

The “USB Prober” application included in Apple’s developer tools helped me a lot, especially the “Details” drawer in its “IORegistry” tab.

Before I published the first version, I realized that I might follow up with quite a few bug-fix updates in the first few weeks so tried the Sparkle auto-update framework by Andy Matuschak for the first time. I could not believe how easy it was to get it working. I was literally running my first auto-update 15 minutes after downloading the framework from the Sparkle website and starting to read its documentation. If you distribute Cocoa applications, you really need to look at this...


Comments
Posted by Igor on 20 Feb 2007 19:24

Great! Thank you!
(everything fine on a dual g5)

Posted by Ron on 18 Oct 2007 15:37

This is a great tool and it convinced me to buy the Z-10's for my macbook.

The duration of the track showed up as NaN, so I changed the code to this:

setText('duration', formatTime(itunes['duration'].split(',',1)));

Powered By blojsom