Smack Your Mac
The Sudden Motion Sensor (SMS) is Apple’s patent-pending motion-based hardware- and data-protection system used in their notebook computer systems. Apple introduced the system 1 January 2005 in its refreshed PowerBook line, and included it in the iBook line 26 July 2005. Since that time, Apple has included the system in all of their portable systems, now the MacBook and MacBook Pro (as of October 2006).
SmacBook Pro is a hack for Apple’s sudden motion sensor using Fancy Desktop Manager to switch between virtual desktop. Basically you need to add customized code in order to use the Desktop Manager just by tapping your MacBook left and right to switch between desktops. The following codes describes the changes to be made.
Steps are excerpts from SmackBook Pro Website:
Desktop Manager patch
First, you’ll need to be able to control Desktop Manager from outside. Cocoa has a nice class called NSDistributedNotificationCenter which can handle simple message passing between applications. DM uses a notification center internally to send “next desktop”, “previous desktop” commands to itself, so the patch just replaces the normal version with the distributed version. I added the following to WorkspaceController.m, inside the init method:
[[NSDistributedNotificationCenter defaultCenter] addObserver: self
selector: @selector(selectNextWorkspace)
name: NOTIFICATION_NEXTWORKSPACE
object: nil
];[[NSDistributedNotificationCenter defaultCenter] addObserver: self
selector: @selector(selectPreviousWorkspace)
name: NOTIFICATION_PREVWORKSPACE
object: nil
];
To control all the windows in the system, an application needs to have access to the CGSUniversalConnection; only one application can have that at a given time, and the Dock tends to win the battle. Desktop Manager sneakily uses mach_inject to inject its own code into the Dock application. There’s still no x86 version of the sweet, sweet mach_inject library (Wolf! Help us!), so not all the Desktop Manager features will not work on Intel.
Reading the sensor
For the actual sensor reading, I used amstracker by Amit Singh. He does not allow redistribution, so you’ll have to get it directly from the source.
notify: Remote control for Desktop Manager
This 15-line program lets you send random notifications to any running application.
#import
int main (int argc, const char * argv[]) {
[[NSAutoreleasePool alloc] init];if(argc != 2) {
NSLog(@”Use: %s notification-name”, argv[0]);
return 1;
}[[NSDistributedNotificationCenter defaultCenter]
postNotificationName: [NSString stringWithCString: argv[1]]
object: nil];
return 0;
}
Compile it with gcc -framework Foundation -o notify notify.m, then call it with ./notify SwitchToNextWorkspace.
Actual motion interpreter
You can do arbitrarily fancy stuff here; this 10-line Perl script just checks to see if the horizontal acceleration exceeds some threshold in either direction, and calls the notify program.
use strict;
my $stable;
open F,”./AMSTracker -s -u0.01 |”;
while() {
my @a = /(-?\d+)/g;
print, next if @a != 3;# we get a signed short written as two unsigned bytes
$a[0] += 256 if $a[0] < 0;
my $x = $a[1]*256 + $a[0];if(abs($x) < 20) {
$stable++;
}if(abs($x) > 30 && $stable > 30) {
$stable = 0;
my $foo = $x < 0 ? 'Prev' : 'Next';
system "./notify SwitchTo${foo}Workspace\n";
}
}
Summary
Here’s a self-contained zip file of the above patches.
1. Install and run the patched DesktopManager
2. Download and unzip the zip file above (I just updated it with a binary version of notify, so you’ll have to re-download it)
3. Download amstracker from osxbook.com (I’m not allowed to redistribute it)
4. Place the amstracker binary in the directory where you unzipped smackbook.zip.
5. You should have a directory with the files amstracker, notify and smack.pl. From the terminal, in that directory, run the command perl smack.pl
Bookmark This!








I am Filipino Web Developer, focusing on PHP in LAMP framework. As a kid, I spent a lot of my time exploring computers and computer games from Atari to PS, from INTEL 80286 - CoreDuo. I am happily married, with two kids. Currently working in Japan as an IT Engineer.