Using spark on your hackintosh for volume key binding a VoodooHDA PrefPane applescript

I had 2 more issues with my hackintosh.

1) I want the standard volume control buttons on my laptop to control the volume like a normal macbook.

2) I want to be able to open up VoodooHDA pref pane so I can adjust mixer settings. This will be the function key right next to the volume controls.

How to do awesome keybinding has been something I’ve been looking for a long time, and I found it: An awesome open source key bind program called Spark. It does exactly what you’d expect and much, much more.

With spark, problem 1 is quickly solved.

Problem 2 requires binding the key to an applescript to pop open the pref pane, which I’ve pasted below:

tell application "System Preferences"
    set current pane to pane "org.voodoo.VoodooHDA"
end tell

And problem 2 is solved now as well. Hell yeah!

Writing your own kext kernel extension for mac os x

Direct copypasta of

I generally like using Xcode. It typically does the job, and usually even makes the job easier. It shields the user from having to worry about mundane details that are all too common when building software. However, for the same reason it can increase a developer’s productivity, it can also make it more difficult to understand what’s actually going on. For example, Xcode has a template to create a Generic Kernel Extension. The template compiles with no modifications. The resulting kernel extension (KEXT) can then be loaded and unloaded without having to write one line of code. This is super cool, but it also hides some of the details of what a KEXT really is. So, today we’ll write a “Hello, World” KEXT from scratch without the help of Xcode

(note the issues above do not apply to Xcode alone, rather they apply to almost all IDEs)

Well, let’s just jump right in. Basically, a KEXT is a bundle on Mac OS X (it’s also a “package”), which means it is a directory structure with some predefined form and an Info.plist file. Our sample KEXT will be named “MyKext.kext”, and it will be in a directory structure that looks like this:

$ find MyKext.kext

To start off, we need to make the basic directory structure.

$ cd
$ mkdir -p MyKext.kext/Contents/MacOS
$ cd MyKext.kext/Contents/MacOS

Now we can start writing our code. Our code will contain two main routines: MyKextStart() and MyKextStop(), which are called when the KEXT is loaded and unloaded respectively. It will also contain some required bookkeeping code that’s needed in order to make our compiled binary proper. Our start and stop routines look like:

// File: mykext.c
#include <libkern/libkern.h>
#include <mach/mach_types.h>

kern_return_t MyKextStart(kmod_info_t *ki, void *d) {
printf("Hello, World!\n");

kern_return_t MyKextStop(kmod_info_t *ki, void *d) {
printf("Goodbye, World!\n");

... more to come in a minute

After these two methods (in the same mykext.c file) we need to put the required bookkeeping stuff.

extern kern_return_t _start(kmod_info_t *ki, void *data);
extern kern_return_t _stop(kmod_info_t *ki, void *data);

KMOD_EXPLICIT_DECL(net.unixjunkie.kext.MyKext, "1.0.0d1", _start, _stop)
__private_extern__ kmod_start_func_t *_realmain = MyKextStart;
__private_extern__ kmod_stop_func_t *_antimain = MyKextStop;
__private_extern__ int _kext_apple_cc = __APPLE_CC__;

This stuff basically declares some needed structures, and it also sets up our routines (MyKextStart() andMyKextStop()) so that they’re called on load and unload, by assigning them to the _realmain and _antimainsymbols respectively.

OK, now comes the tricky part: the compile. KEXTs are compiled statically, they can only use certain headers that are available in the kernel, and they can’t link with the standard C library. These requirements basically translate into a gcc command like the following:

$ gcc -static mykext.c -o MyKext -fno-builtin -nostdlib -lkmod -r -mlong-branch -I/System/Library/Frameworks/Kernel.framework/Headers -Wall

If the planets are properly aligned, you won’t get any errors or warnings, and you’ll end up with a Mach-O object file in the current directory named MyKext. This is your actual compiled KEXT. (At this point, it’s fun to inspect this file using otool. For example, otool -hV MyKext, and otool -l MyKext. Read the man page for otool(1) for more details here.)

Now, the last thing we need to do (before we actually load this thing up) is to give our KEXT an Info.plist. The easiest way to do this is to copy another KEXT’s Info.plist file, and change the names of a few things. For this example, I’m going to copy /System/Library/Extensions/webdav_fs.kext/Contents/Info.plist.

$ cd ..
$ pwd
$ cp /System/Library/Extensions/webdav_fs.kext/Contents/Info.plist .

Now, you’ll need to edit the file and change the value of the “CFBundleExecutable” key to MyKext, and the value of “CFBundleIdentifier” to net.unixjunkie.kext.MyKext (or whatever you set that value to in your mykext.c file).

Okay, it’s show time. To load any KEXT, all files in the KEXT must be owned by root and be in group wheel. The files must also have certain permissions in order to load. Here’s the steps to load the KEXT.

$ cd /tmp
$ sudo -s
# cp -rp ~/MyKext.kext .
# chown -R root:wheel MyKext.kext
# chmod -R 0644 MyKext.kext
# kextload -v MyKext.kext
kextload: extension MyKext.kext appears to be valid
kextload: loading extension MyKext.kext
kextload: sending 1 personality to the kernel
kextload: MyKext.kext loaded successfully
# tail -1 /var/log/system.log
Dec 15 20:15:47 jgm-mac kernel[0]: Hello, World!

We can see that our MyKextStart() was called. Now let’s unload it and see what happens.

# kextunload -v MyKext.kext
kextunload: unload kext MyKext.kext succeeded

Wahoo! It looks like we made a kernel extension with our own 10 fingers, and it worked! :-) 

That was fun. Check out Amit Singh’s Mac OS X Internals book for more cool bits about what that required “bookkeeping” stuff was in our source file, and why it’s required.

How to access the contents of the iphone hard disk drive

You may want to see and access the files you’re creating in your Documents or tmp directories while coding with iOS. Directly accessing those folders with Finder turns out to be rather difficult.

It was recommended to me in #iphonedev (freenode) to do the following in order to view the files in iTunes:

1) In Xcode, open up your <appname>-Info.plist file and add a new string by Ctrl-clicking an existing row and clicking “add row”:

The string should be set to: ”Application supports iTunes file sharing”, it should be a Boolean, and the value should be YES

2) Open up iTunes and access it from the Apps section of your device.

So I was doing it this way, and received this dreaded message: 

My iPhone is associated with a different mac… And I don’t want to delete everything just to access it from my development machine. To get around this, you will need to spend money on an application called Phone Disk.

Phone Disk gives you direct access to your root directory of your iPhone. It worked perfectly for what I needed.

The trial allows you to transfer up to 100MB. A license is $9.99. You can find it here:

How to design ios interfaces without using interface builder

Now that my app has achieved the functionality requested by my client—naturally he begins to talk about interface design.

I am a coder, but I am not a graphic designer. So how can we allow the budget-client, who may not be using Mac—and doesn’t want to figure out how to use Interface Builder anyway—to create great interfaces?

How to change an interface button title from within ios code

  1. Open your Nib file (.xib) for your view controller.

  2. Click on the button that you want to be able to change the label for, and from the Utilities panel (Appears on the far right when you enable it in Xcode view) and go to the connections section.

  3. You probably already have a connection (Touch Up Inside or something else) to some method in your File’s Owner (Your controller class). In order to be able to access the attributes of the button itself from within the class, we need to make a new Referencing Outlet to this button.

To create a new referencing outlet for this button, click inside the circle for “New Referencing Outlet” and drag (connect) to your controller’s header file. Give it a name, like myButton.

Congratulations! You can now access and potentially modify the attributes of your button from within your iOS program.

  1. You can now make use of the following source code, to change the button title (when an action is hit, for example):
[myButton setTitle:"New Label" forState:UIControlStateNormal];

There are other UIControl states that you can use, but this was sufficient for me.

get current date time in iOS

This was copied directly from a blog post of Murat Yilmaz 

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnitNSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger week = [dateComponents week];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

How to add a framework to your project in xcode 4

When I googled for this, I got false information — so here is how you do it:

1) First click the Blue Xcode icon signifying your project on the lefthand navigation pane.

Confused? The highlighted thing: 

2) Now look right, to the next pane. Make sure the project name under TARGETS is selected, not PROJECTS.

Looks like this: 

3) Next pane on the right, you’ll see a few things at the top, “Summary”, “Info”, “Build Settings”, “Build Phases”, “Build Rules” … You want to click on Build Phases.

You will see a few expandable sections now.

4) Expand the section labeled Link Binary With Libraries

You should now be seeing something that looks like this:

5) Click the + button at the bottom-left of that Link Binary With Libraries section

Select the library you want, for example, AVFoundation.framework and click Add.

You’ll notice the library/framework has appeared in your navigation pane at the left.

You can go ahead and drag that into the Frameworks folder.

6) In order to utilize the framework, you will need to #import it into your source code.

For AVFoundation.framework, I simply put the following line in my main.m, right under the existing #import for UIKit

#import <AVFoundation/AVFoundation.h>

And that worked for me.

Upgraded to xcode 4 could not insert new outlet

Are you following along on Apple’s “Your First iOS Application” guide, found here ?

Did you upgrade to Xcode 4 mid way? Now when you try to create an outlet, and it says it “could not insert new outlet” in scary red letters; something about not know what class you are looking for.

But… my application builds! What the hell?

Confusing, yes, but this worked for me:

1) Go to your helloWorldAppDelegate.m file

2) You’ll see this code:

@synthesize myViewController;

or you might see

@synthesize myViewController=_myViewController;

3) If it is like the first example, change it to look like the 2nd one. If it is like the 2nd one, change it to look like the first.

4) Make sure the memory release is correct in your dealloc method at the bottom of the same file. If you changed the @synthesize to look like the 2nd example, make sure the release looks as such:

[_myViewController release];

And vice versa.

5) Build, simulate. If it works, try making the connection again from your nib file to your header file. At this point it worked for me.

Oddly enough, once it worked, I was able to reverse the changes in the helloWordAppDelegate.m file without any problems. Clearly this was just a bug in transitioning from Xcode 3 to Xcode 4.

Keep in mind that the two different ways of synthesizing that class variable is simply a matter of stylistic concern. In the earlier pages of the guide, it is explained that the underscore in `_myViewController` is to “remind you that you’re not supposed to access class variables directly “

Learning ios development for free

Learning iOS Development for Free

There are a lot of great lectures on learning iOS development on the iTunesU network free for download. But it’s slow going and I’d rather code than watch lectures about it.

This guy has a bunch of links and this is where I’ve started a few days ago with iOS.


“Can you recommend a good book for getting started with Objective-C?”

I get this question a lot, but there’s no need to spend any money or even to spend time digesting an entire book. I always point people to the Apple docs — the documentation is very modular and (mostly) very reliable.