Skip to content

Commit

Permalink
Initial commit of sources of iphonesim-example-20081226 sources
Browse files Browse the repository at this point in the history
  • Loading branch information
landonf committed Jul 25, 2009
0 parents commit 87c3e76
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CC=gcc

SIM_PRIVATE_FRAMEWORK_DIR=/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/PrivateFrameworks
CFLAGS=-Wall -Werror -I. -ISource
LDFLAGS=-framework AppKit -F${SIM_PRIVATE_FRAMEWORK_DIR} -framework iPhoneSimulatorRemoteClient -Wl,-rpath -Wl,${SIM_PRIVATE_FRAMEWORK_DIR}
OBJS= \
Source/main.o \
Source/nsprintf.o \
Source/iPhoneSimExample.o

.SUFFIXES: .m
.m.o:
${CC} ${CFLAGS} -c $< -o $@

all: iphonesim

iphonesim: ${OBJS}
${CC} -o $@ ${OBJS} ${LDFLAGS}

clean:
rm -f ${OBJS} iphonesim
30 changes: 30 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
A command-line utility for managing the iPhone Simulator.

Example:
./iphonesim launch ~/Project/build/Debug-iphonesimulator/Project.app

Author: Landon Fuller <landonf@plausiblelabs.com>

Copyright (c) 2008-2009 Plausible Labs Cooperative, Inc.
All rights reserved.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions Source/iPhoneSimExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Author: Landon Fuller <landonf@plausiblelabs.com>
* Copyright (c) 2008 Plausible Labs Cooperative, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import <iPhoneSimulatorRemoteClient/iPhoneSimulatorRemoteClient.h>

@interface iPhoneSimExample : NSObject <DTiPhoneSimulatorSessionDelegate> {
@private
}

- (void) runWithArgc: (int) argc argv: (char **) argv;

@end
160 changes: 160 additions & 0 deletions Source/iPhoneSimExample.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Author: Landon Fuller <landonf@plausiblelabs.com>
* Copyright (c) 2008 Plausible Labs Cooperative, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#import "iPhoneSimExample.h"
#import "nsprintf.h"


/**
* A simple usage example for the iPhoneSimulatorRemoteClient
* framework.
*/
@implementation iPhoneSimExample

/**
* Print usage.
*/
- (void) printUsage {
fprintf(stderr, "Usage: iphonesim <options> <command> ...\n");
fprintf(stderr, "Commands:\n");
fprintf(stderr, " showsdks\n");
fprintf(stderr, " launch <application path>\n");
}


/**
* List available SDK roots.
*/
- (int) showSDKs {
NSArray *roots = [DTiPhoneSimulatorSystemRoot knownRoots];

nsprintf(@"Simulator SDK Roots:\n");
for (DTiPhoneSimulatorSystemRoot *root in roots) {
nsfprintf(stderr, @"'%@' (%@)\n\t%@\n", [root sdkDisplayName], [root sdkVersion], [root sdkRootPath]);
}

return EXIT_SUCCESS;
}

- (void) session: (DTiPhoneSimulatorSession *) session didEndWithError: (NSError *) error {
nsprintf(@"Seesion did end with error %@\n", error);

if (error != nil)
exit(EXIT_FAILURE);

exit(EXIT_SUCCESS);
}


- (void) session: (DTiPhoneSimulatorSession *) session didStart: (BOOL) started withError: (NSError *) error {
if (started) {
nsprintf(@"Session started\n");
} else {
nsprintf(@"Session could not be started: %@", error);
exit(EXIT_FAILURE);
}
}


/**
* Launch the given Simulator binary.
*/
- (int) launchApp: (NSString *) path {
DTiPhoneSimulatorApplicationSpecifier *appSpec;
DTiPhoneSimulatorSystemRoot *sdkRoot;
DTiPhoneSimulatorSessionConfig *config;
DTiPhoneSimulatorSession *session;
NSError *error;

/* Create the app specifier */
appSpec = [DTiPhoneSimulatorApplicationSpecifier specifierWithApplicationPath: path];
if (appSpec == nil) {
nsprintf(@"Could not load application specification for %s", path);
return EXIT_FAILURE;
}
nsprintf(@"App Spec: %@\n", appSpec);

/* Load the default SDK root */
sdkRoot = [DTiPhoneSimulatorSystemRoot defaultRoot];
nsprintf(@"SDK Root: %@\n", sdkRoot);

/* Set up the session configuration */
config = [[[DTiPhoneSimulatorSessionConfig alloc] init] autorelease];
[config setApplicationToSimulateOnStart: appSpec];
[config setSimulatedSystemRoot: sdkRoot];
[config setSimulatedApplicationShouldWaitForDebugger: NO];

[config setSimulatedApplicationLaunchArgs: [NSArray array]];
[config setSimulatedApplicationLaunchEnvironment: [NSDictionary dictionary]];

[config setLocalizedClientName: @"iPhoneSimExample"];

/* Start the session */
session = [[[DTiPhoneSimulatorSession alloc] init] autorelease];
[session setDelegate: self];
[session setSimulatedApplicationPID: [NSNumber numberWithInt: 35]];

if (![session requestStartWithConfig: config timeout: 10 error: &error]) {
nsprintf(@"Could not start simulator session: %@", error);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}


/**
* Execute 'main'
*/
- (void) runWithArgc: (int) argc argv: (char **) argv {
/* Read the command */
if (argc < 2) {
[self printUsage];
exit(EXIT_FAILURE);
}

if (strcmp(argv[1], "showsdks") == 0) {
exit([self showSDKs]);
}
else if (strcmp(argv[1], "launch") == 0) {
/* Requires an additional argument */
if (argc < 3) {
fprintf(stderr, "Missing application path argument\n");
[self printUsage];
exit(EXIT_FAILURE);
}

/* Don't exit, adds to runloop */
[self launchApp: [NSString stringWithUTF8String: argv[2]]];
} else {
fprintf(stderr, "Unknow command\n");
[self printUsage];
exit(EXIT_FAILURE);
}
}

@end
48 changes: 48 additions & 0 deletions Source/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Author: Landon Fuller <landonf@plausiblelabs.com>
* Copyright (c) 2008 Plausible Labs Cooperative, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#import <AppKit/AppKit.h>

#import "iPhoneSimExample.h"

/*
* Runs the iPhoneSimExample backed by a main runloop.
*/
int main (int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
iPhoneSimExample *sim;

/* Execute command line handler */
sim = [[iPhoneSimExample alloc] init];
[sim runWithArgc: argc argv: argv];

/* Run the loop to handle added input sources, if any */
[[NSRunLoop mainRunLoop] run];

[pool release];
return 0;
}
3 changes: 3 additions & 0 deletions Source/nsprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int nsvfprintf (FILE *stream, NSString *format, va_list args);
int nsfprintf (FILE *stream, NSString *format, ...);
int nsprintf (NSString *format, ...);
42 changes: 42 additions & 0 deletions Source/nsprintf.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* NSLog() clone, but writes to arbitrary output stream
*/

#import <Foundation/Foundation.h>
#import <stdio.h>

int nsvfprintf (FILE *stream, NSString *format, va_list args) {
int retval;

NSString *str = (NSString *) CFStringCreateWithFormatAndArguments(NULL, NULL, (CFStringRef) format, args);
retval = fprintf(stream, "%s", [str UTF8String]);
[str release];

return retval;
}

int nsfprintf (FILE *stream, NSString *format, ...) {
va_list ap;
int retval;

va_start(ap, format);
{
retval = nsvfprintf(stream, format, ap);
}
va_end(ap);

return retval;
}

int nsprintf (NSString *format, ...) {
va_list ap;
int retval;

va_start(ap, format);
{
retval = nsvfprintf(stderr, format, ap);
}
va_end(ap);

return retval;
}
Loading

0 comments on commit 87c3e76

Please sign in to comment.