Saving some space

🐚

The png file format has become the de-facto lossless file format for pixmaps. One of interesting aspects of this format, is that a given image can be compressed in multiple ways, and that most tools only use one. This means that given the right tools, png files can be made smaller without any loss of quality. On Mac OS X, it is very easy to find all the png files in one’s home directory and reduce their size, assuming your have the Developer Tools and Mac ports installed:

sudo port install optipng
mdfind -0 -onlyin ~ "kMDItemContentType=public.png" | xargs -n 5 -0 optipng -preserve

The first line install the optipng tool.
The second line involves three command-line tools:

  • mdfind searches for all files of type public.png in the home directory (~) separating each item with a null character
  • xargs takes the file paths in bunches of 5 (completely arbitrary value) and passes them as parameters to the next command:
  • optipng compresses the files, preserving the file attributes

To be honest I did not bother checking how much space this save me, I just wanted to play around with mdfind… Adding the parameter -o 3 to optipng could save more space, but increase the processing time. Technically you could run this command on the System directory to compress all the png files there, but I don’t think this would be wise. I realise the same technique could be used for jpeg files using jpegtran, if I have some time, I’ll explain how to do it.

Use old style Next-Step icons for commercial e-mail

Screen Capture iTunes and Doodle Logo in Mail.app

One feature of Mail.app on OS X that I like is that it shows the picture associated with the sender of an e-mail. The normal way of associating a picture with an e-mail is using the Address Book. While this makes sense for the humans I’m interacting with, I don’t want to add an address book entry for no-reply@flattr.com.

The good news is, there is an old Next-Step era mechanism that still works, even under Mac OS X 10.7: just add a tiff file with a name e-mail.tiff into the folder ~/Library/Images/People, restart Mail.app and you are done. So for instance the Flattr logo goes into file ~/Library/Images/People/no-reply@flattr.com.tiff. If you want to reuse the icon of a desktop application, just open the application’s package (context menu) and go down into the Resources folder and open the Application-name.ics file with Preview.app copy paste the high-resolution icon into a new file and save it as tiff.

While there is no way to have a file match multiple e-mail addresses, you can avoid duplicating files using symbolic or hard links. On the other hand, those images, contrary to the ones added in address book can contain an α-channel (transparency).

Use Emoticons to distinguish shells

Python shell with Snake Emoji in the Title

One application that has been updated in Mac OS X Lion is the venerable terminal.app which finally supports 256 colours. While playing around with emoji characters, I realised they were quite useful to mark different terminals. I typically have multiple windows open with local and remote shells, along with a python interpreter. Previously I used the background colour of the terminal to distinguish the various contexts, but now I also add a relevant emoji in the title. Among the useful characters for this use

  • Shell 🐚
  • Snake (Python) 🐍
  • Camel 🐫
  • Computer 💻

To add the character to a terminal’s title, just go into Terminal/Preferences, select the Settings Icon and the Window tab. In the Title item, you can enter the emoticon in the title text.

xkbgrowl

A few weeks back, I posted about a way to setup Growl to display X11 keyboard notifications. While the technique I described works, it was not really satisfying. So I decided to write a small program that would offer the same functionality (and a few others), the result is xkbgrowl.

μ-FAQ

How to I trigger a notification?
Type xkbbell "message" in a shell.
Why did you write this?
Because I wanted to
Why not use Growl network notifications?
Sending Growl network from a generic unix box requires two things: to know the address of the target Mac and that said Mac has an open network port. The first is a configuration problem, the second is a security issue. I could setup an additional ssh tunnel, but this is done automatically for the X11 connection…
Why not use XXX
There are many open source frameworks to do notifications. At a low level, Dbus seems to be the standard, at a higher level there are frameworks like Mumbles. My solution does not require to install a single thing on any Unix box, it uses software that has been there since 1995, and it uses the standard.

This small program basically connects to the X11 server and listens to X11 keyboard bell events and translates them to growl notifications. Compared to the script version, there are a few additional features:

  • No dependencies besides growl.
  • No text escaping or buffer issues
  • The volume value is translated into a Growl priority.
  • If an X11 window is associated with the event: the window name is prefixed to the notification text and the window’s icon is used as the notification icon.
  • Simple to use, just enter xkbgrowl &. The program will exit when the X11 servers quits.

The code is now hosted on google code, please go there for more information


As usual, if your computer catches fire, or any bad thing happens on your machine because you used this software, its your own fault. This code happens to work on my machine, but was not really tested on any other settings.

Post Unix OS

The most interesting technical read of last week was undoubtedly . While for consumers the new feature set is very modest, having a software company release a new version of an operation system with mostly bug fixes and performance improvements is something very interesting. I wish others would follow this example. Most engineer know that I you give them enough time, they can improve the code tremendously, most very often this time is not given.

Besides optimizations, Snow Leopard introduces a set of new technologies related to performance. As single core performance is not improving much anymore, this means more parallelism. is a operating system level work-queue management framework. The C language and its variants have been extended to support closures. Those closures can then dispatched to various work-queues. Instead of using a lock to protect a given resource, a serial work queue is created and associated with the resource. Where this gets interesting is that the closures might not run on the CPU: closures are compiled to LLVM code, which might be dispatched on the GPU using Open CL.

Taken separately, none of these technologies is really innovative – there have been numerous projects and language that implemented them. The caveat is that they only solved part of the problem and never managed to gain much traction outside of specialized markets. Here we have an operating system that lets the programmer handle the machine as if it were a small cluster. Code is compiled into some virtual machine code and sent to various processing queues. The number of processors, and their precise instruction sets are irrelevant. While on OS X, the code is only dispatched on the CPU and the GPU, this architecture can be used for sending processing to other machines or add-on processing cards. The grand plan of the Cell processor, where processing could be sent from your mobile phone could sent processing tasks to your TV set might still be relevant. The coolest bit is that most of those technologies are free: Apple open-sourced GCD, LLVM is open source, Open CL is a standard handled by the same group as Open GL.

What is also interesting is that Apple is incrementally replacing traditional part of the operating system with more modern versions. In previous versions Launchd replaced the traditional dæmons, DTrace was used to replaced various system probes. Now Apple has started to replace the tool-chain: clang is a new C-compiler that generates LLVM code, it currently only support C and objective-C, but C++ is in the works. While gcc is the traditional compiler it has many problems, its error handling is horrible, the generated code is far from optimal, more importantly its code base is monolithic and old, this means it is difficult to improve it in any way. Finally gcc is not designed in part as a library (say for integration into an IDE), the GPL license is also a deterrent for such adoption. My hope is that clang will have the same effect on the compiler world that Webkit had on web browser: make things move forward.

xkbbell & xkbevd

X11 Logo ⓒ MIT

So I started working with Eclipse on Linux. I was hoping to redefine the keyboard shortcuts to use the meta key, as I’m connecting remotely with a Mac OS X laptop and X11. I though it would be an easy task. I was wrong. In KDE applications, this just requires you to select the action, type in the new shortcut and it is done. With gtk2 this does not work. I checked with xev, the key events are the expected one: left-meta. I searched online, no useful information, just other lost users and the nagging feeling that Gnome does not honor the xmod file. Changing the keyboard settings in the gnome-control-center has no effect, this might be related to the fact I’m running a KDE desktop, but I have hardly the choice, on my virtual machine Gnome just crashes. I tried reproducing the issue on my laptop but installing gnome-terminal and launching result in the program dying horribly on launch, probably because there is no gnome desktop running, and plain X11 is way too scary. I have not solved the issue (getting rid of all things gtk would probably be the sanest solution), but digging around I stumbled on a interesting X11 extension: X Keyboard.

I have written a small binary program called xbkgrowl that does the same thing than the script presented here. It does not require any configuration file and does a few other things, link priority translation. Please check it out

In short, this extension can be used to control the lights and modifiers on the keyboard, it also gives a way of sending notifications to the X11 server. This would be something useful when I’m working on a remote Linux box, streaming back sounds is wasteful and far from stable. On my Mac, I have Growl installed so it would be really convenient to be able to trigger growl notifications. There are versions of Growl, or Growl-like project under linux, and Growl has network support, but this requires a fixed IP adresses, an open port on my Mac, and installing software or at least writing some script. But actually by using the X keyboard dæmon and an old command line you can do this using the X11 connection and without installing anything on the Linux side.

  • Install growlnotify on Mac OS X.
  • Create a configuration file xkbevd.cf with the following content:
    Bell()  shell "/usr/local/bin/growlnotify --message \"$(name)\" --appIcon X11; true "
    
  • Start the X11 server (you can just start xlogo)
  • Start the X keyboard dæmon with the following command xkbevd -cfg xkbevd.cf -bg. This will start the program in background using the configuration defined in xkbevd.cf.
  • You can now trigger a notification using the command xkbbell message, for instance xkbbell "lasers offline". This should work both locally and on remote machines.
    This only works if the X11 display on your Unix box point back to the X11 server on your Mac. You can test this by launching xlogo on the Unix box, it should display the logo on the Mac’s screen. If you have connected using ssh, make sure you selected the -Y option.

Some notes:

  • This is an old protocol (1995) which has never been seriously supported. Use at your own risk.
  • The xkbevd program, is buggy. In particular, there seems to be some issue with the memory buffer used for command substitution, no end marker is inserted, so when a long message is followed by a short one, the short command will have the last characters from the long one at its end. This is why the second command is true, which ignores all its arguments.
  • While the xkbbell command recognises many options, only the volume flag seems to be carried to the server. The volume is in the $(volume) variable, with the value of 50 added.
  • The notifications are carried over NX, but the text message is discarded.
  • There are some standard bell names defined in the header files, but any arbitrary ASCII string seems to work.
  • Instead of growl, you can also use the Mac OS X command say. In that case, the configuration file would be:
    Bell()  shell "/usr/bin/say \"$(name)\" ; true "
    
  • It is possible to define different actions for different messages, so for instance.
    Bell()  shell "/usr/local/bin/growlnotify --message \"$(name)\" --appIcon X11; true "
    Bell(Warning) shell "/usr/bin/say Warning
    

I more or less fixed the gtk issue with xmodmap, but this is really a kludge.

Much ado about nothing

Like many other computer geeks, I’m a regular reader of web-sites like slashdot, or osnews, not to mention a few technical blogs. It is a good source of news, and there are often good technical links, but politics and ideology have the annoying tendency to come to the foreground.

The kernel of the operating system Mac OS X is open-source, that is the source code can be downloaded and compiled freely. There are a myriad of different open-source projects which are distributed under different licences, with very strong debates about which licenses are more free, better, etc. I must admit that my interest in those debates is very limited.

One of the latest scandal in this online community was the fact that Apple did not distribute the source code for the Intel version of Mac OS X. People reacted very violently, which was kind of strange because Mac OS X’s kernel, Darwin, dit not gather so much interest when it was open. But post were posted, and blog entries written decrying the fact that Apple reneged their committement to open-source, that it was the begining of the end. The closing of the web site open-darwin, which hosted most of the Apple-related open-source project fueling the whole debate…

Nobody seemed to take the common sense approach: Apple had just moved their main OS to another architecture (intel), and their source code tree would probably be a mess. As the OS contains proprietary bits, some licenced from another companies, the lawyers of the company would probably not want anything published until everything had been checked. Not to mention engineers who would like to have some time to clean up things.

Of course, if you took the common sense explanation, the fact that the source code of the kernel is now available for intel and a new web site hosting Mac-related open source projects opened would not be a real surprise…