FRAppliance 101

From AwkwardTV

Jump to: navigation, search

<Google>WIKI</Google>

This is the first in a likely series of tutorials on writing Apple TV Finder.app plugins. The basic setup of Xcode and the development environment will be described, and a simple "Hello World" plugin will be created.

Contents

Prerequisites

Note: These instructions are written for a PowerPC Macintosh. Some steps, especially regarding build architecture settings, may not be necessary on an Intel Mac but are still recommended.

Preparing the Environment

These steps only need to be performed once.

  1. Install Xcode and especially the Mac OS X 10.4 Universal (10.4u) SDK.
  2. Copy the BackRow.framework and iPhotoAccess.framework from /System/Library/PrivateFrameworks on the Apple TV to some place on your computer. I recommend placing these in the same folder your project folder will be created.

Installing the Headers

The headers need to be placed in the appropriate location for the compiler to find them. BackRow.framework depends on some private headers inside QuartzComposer.framework, so we have to put headers in there too.

  1. Open the BackRow.framework folder, and in there extract BRPrivateHeaders (http://ericiii.net/sa/appletv/BRPrivateHeaders.zip) to the PrivateHeaders folder. (The ZIP file contains a PrivateHeaders folder, so just move that there.)
  2. In /Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Quartz.framework/Frameworks/QuartzComposer.framework, extract QCPrivateHeaders (http://ericiii.net/sa/appletv/QCPrivateHeaders.zip) to the PrivateHeaders folder (same deal as above).
  3. In /Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Quartz.framework/Frameworks/QuartzComposer.framework/Headers/QuartzComposer.h, add this line to the end:
    #import <QuartzComposer/QuartzComposerPrivate.h>

Creating the Plugin

Now the fun stuff starts, actually creating the plugin.

Making the Project

Launch Xcode, and create a new Cocoa Bundle project (File > New Project...). Name it "HelloWorld".

At this point, you have an empty project with no code. You now need to specify the frameworks to link to.

Adding Frameworks

In the Frameworks and Libraries group, drag BackRow.framework and iPhotoAccess.framework to the Linked Frameworks group. In the dialog that pops up, make sure "Copy items into destination group's folder" is unchecked and choose a Reference Type of "Default". In "Add to Targets," the HelloWorld target should be checked. Click "Add".

At this point, you should have three frameworks listed underneath "Linked Frameworks:" BackRow.framework, iPhotoAccess.framework, and Cocoa.framework.

Creating the Plugin

Open InfoPlist.strings (for English) and add the following lines:

 CFBundleName = "Hello, World!";

The CFBundleName is the label that will be given in the main menu.

The Main Appliance Class

Now you finally write some code! Right click on the Classes group and choose Add > New File..., under Cocoa add an Objective-C class and give it a name of "HelloWorldAppliance". Check "Also create HelloWorldAppliance.h", make sure you're adding to project HelloWorldTest, and the HelloWorld target is checked. Click on "Finish".

Open HelloWorldAppliance.h and change it so that your HelloWorldAppliance class inherits from BRAppliance. Change "NSObject" in the line beginning with "@interface" to read "BRAppliance".

You also need to add the following line to the header, just underneath the existing "#import" line:

 #import <BackRow/BackRow.h>

Open HelloWorldAppliance.m now, and in order to bypass the plugin whitelist, you must put the following code in the HelloWorldAppliance implementation. Add this code before the @end line:

// Override to allow FrontRow to load custom appliance plugins
+ (NSString *) className {
    // this function creates an NSString from the contents of the
    // struct objc_class, which means using this will not call this
    // function recursively, and it'll also return the *real* class
    // name.
    NSString * className = NSStringFromClass( self );
	
	// new method based on the BackRow NSException subclass, which conveniently provides us a backtrace
	// method!
	NSRange result = [[BRBacktracingException backtrace] rangeOfString:@"_loadApplianceInfoAtPath:"];
	
	if(result.location != NSNotFound) {
		NSLog(@"+[%@ className] called for whitelist check, so I'm lying, m'kay?", className);
		className = @"RUIMoviesAppliance";
	}
	
	return className;
}

When the menu item is selected, the -applianceControllerWithScene: method is called on your BRAppliance subclass. This method needs to return a controller for your display. (Note: I'll write more on how the displays actually work in the future, but it's essentially a stack of controllers that are pushed to go deeper in the hierarchy and popped to go back.)

The following implementation uses the BRAlertController class to display a simple "Hello, World" message when the item is selected.

- (id)applianceControllerWithScene:(id)scene {
	BRAlertController *alert = [BRAlertController alertOfType:0
               titled:@"Hello, World!"
               primaryText:@"Hello from my first Apple TV plugin!"
               secondaryText:nil
               withScene:scene];
	return alert;
}

Building the Plugin

This isn't very hard, but you have to set some settings first.

Build Settings

Before you can properly build, you must adjust some build settings to build for the Apple TV environment.

Under the "Targets" group on the left side of the Xcode project window, right click on the "HelloWorld" target and choose "Get Info."

On the "Properties" tab, change Identifier to something unique, such as "com.apple.frontrow.appliance.org.awkwardtv.HelloWorldAppliance". Change the Creator to "fnrw", and set Principal Class to "HelloWorldAppliance" (this must match the BRAppliance subclass for your plugin). Note: As of the Apple TV 1.1 update, the bundle identifier must begin with "com.apple.frontrow.appliance." See June 20 2007 Patch (aka the YouTube Patch) for details.

On the "Build" tab, set configuration to "All Configurations" and then set the build settings as follows:

In the "Architectures" collection, double click on the Architectures setting and make sure only Intel is checked. This is a value of "i386".

In "Build Locations," make sure the SDK Path is set to "/Developer/SDKs/MacOSX10.4u.sdk" (it should be by default).

In "Linking," make sure ZeroLink is unchecked.

In "Packaging," set the Wrapper Extension to "frappliance".

In "GCC 4.0 - Language" (underneath "GNU C/C++ Compiler 4.0"), make sure "Enable Objective-C Exceptions" is checked.

Building

At this point, you can just click Build to build the plugin. The compiled version will appear in build/Debug/HelloWorldTest.frappliance in your project folder. The whitelist workaround causes some warnings when building, but those can be ignored.

Running the Plugin

To run it, just copy the built version to /System/Library/CoreServices/Finder.app/Contents/PlugIns on the Apple TV. I really recommend at least creating a symlink to the frappliance in the frontrow home directory, if not directly mounting the project from your development system, but both are beyond the scope of the tutorial.

After it's in place on the Apple TV, you need to restart Finder (either with killall Finder if you have killall installed, otherwise use ps awx|grep Finder to get the PID and then kill PID.)

Now when you choose the "Hello, World!" menu item on the Apple TV, you should see the message!

Congratulations!

If you've successfully made it this far, you've created your first plugin!

Submit it to http://plugins.awkwardtv.org/ so that others can find it.

A completed xcode project can be found at http://ericiii.net/sa/appletv/FRApplianceTutorial/HelloWorld.zip. This is set up for the two frameworks are in the same folder as the HelloWorld project folder.

You can also try your hand at a Front Row Screen Saver using what you've learned here.

Revision History

  • June 20, 2007
    • Update for Apple TV 1.1 update. Example project archive does not have the changes incorporated.
  • April 4, 2007
    • Changed how the BackRow headers are dealt with, to be more proper and all the headers are converted.
    • Replaced the whitelist bypass with a cleaner method of doing so (that doesn't require ExceptionHandling.framework)
Personal tools