Permalink
Browse files

first

  • Loading branch information...
probablycorey committed Feb 16, 2010
0 parents commit 666934743e74ec40365213f5e282362a571fdd56
Showing with 656 additions and 0 deletions.
  1. +29 −0 Simulator.h
  2. +123 −0 Simulator.m
  3. +117 −0 WaxSim.m
  4. +254 −0 WaxSim.xcodeproj/project.pbxproj
  5. +7 −0 WaxSim_Prefix.pch
  6. +126 −0 iPhoneSimulatorRemoteClient/iPhoneSimulatorRemoteClient.h
@@ -0,0 +1,29 @@
+//
+// Simulator.h
+// Sim
+//
+// Created by ProbablyInteractive on 7/28/09.
+// Copyright 2009 Probably Interactive. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+#import "iPhoneSimulatorRemoteClient.h"
+
+@class DTiPhoneSimulatorSystemRoot;
+
+@interface Simulator : NSObject <DTiPhoneSimulatorSessionDelegate> {
+ NSString *_appPath;
+ DTiPhoneSimulatorSystemRoot *_sdk;
+ DTiPhoneSimulatorSession* _session;
+ NSArray *_args;
+}
+
+@property (nonatomic, readonly) DTiPhoneSimulatorSession* session;
+
++ (NSArray *)availableSDKs;
+
+- (id)initWithAppPath:(NSString *)appPath sdk:(NSString *)sdk args:(NSArray *)args;
+- (int)launch;
+- (void)end;
+
+@end
@@ -0,0 +1,123 @@
+//
+//
+// Sim
+//
+// Created by ProbablyInteractive on 7/28/09.
+// Copyright 2009 Probably Interactive. All rights reserved.
+//
+
+#import "Simulator.h"
+
+#include <sys/param.h>
+#include <objc/runtime.h>
+
+#define WaxLog(format, args...) \
+ printf("%s\n", [[NSString stringWithFormat:(format), ## args] UTF8String])
+
+@implementation Simulator
+
+@synthesize session=_session;
+
+- (id)initWithAppPath:(NSString *)appPath sdk:(NSString *)sdk args:(NSArray *)args {
+ self = [super init];
+
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+ if (![appPath isAbsolutePath]) {
+ appPath = [[fileManager currentDirectoryPath] stringByAppendingPathComponent:appPath];
+ }
+
+ _appPath = [appPath retain];
+
+ if (![fileManager fileExistsAtPath:_appPath]) {
+ WaxLog(@"App path '%@' does not exist!", _appPath);
+ exit(EXIT_FAILURE);
+ }
+
+ if (!sdk) _sdk = [[DTiPhoneSimulatorSystemRoot defaultRoot] retain];
+ else {
+ _sdk = [[DTiPhoneSimulatorSystemRoot rootWithSDKVersion:sdk] retain];
+ }
+
+ if (!_sdk) {
+ WaxLog(@"Unknown sdk '%@'", sdk);
+ WaxLog(@"Available sdks are...");
+ for (id root in [DTiPhoneSimulatorSystemRoot knownRoots]) {
+ WaxLog(@" %@", [root sdkVersion]);
+ }
+
+ exit(EXIT_FAILURE);
+ }
+
+ _args = [args retain];
+
+ return self;
+}
+
++ (NSArray *)availableSDKs {
+ NSMutableArray *sdks = [NSMutableArray array];
+ for (id root in [DTiPhoneSimulatorSystemRoot knownRoots]) {
+ [sdks addObject:[root sdkVersion]];
+ }
+
+ return sdks;
+}
+
+- (int)launch {
+ WaxLog(@"Launching '%@' on'%@'", _appPath, [_sdk sdkDisplayName]);
+
+ DTiPhoneSimulatorApplicationSpecifier *appSpec = [DTiPhoneSimulatorApplicationSpecifier specifierWithApplicationPath:_appPath];
+ if (!appSpec) {
+ WaxLog(@"Could not load application specifier for '%@'", _appPath);
+ return EXIT_FAILURE;
+ }
+
+ DTiPhoneSimulatorSystemRoot *sdkRoot = [DTiPhoneSimulatorSystemRoot defaultRoot];
+
+ DTiPhoneSimulatorSessionConfig *config = [[DTiPhoneSimulatorSessionConfig alloc] init];
+ [config setApplicationToSimulateOnStart:appSpec];
+ [config setSimulatedSystemRoot:sdkRoot];
+ [config setSimulatedApplicationShouldWaitForDebugger:NO];
+ [config setSimulatedApplicationLaunchArgs:_args];
+ [config setSimulatedApplicationLaunchEnvironment:[[NSProcessInfo processInfo] environment]];
+ [config setLocalizedClientName:@"WaxSim"];
+
+ // Make the simulator output to the current STDOUT & STDERR
+ char path[MAXPATHLEN];
+ fcntl(STDOUT_FILENO, F_GETPATH, &path);
+ [config setSimulatedApplicationStdOutPath:[NSString stringWithUTF8String:path]];
+
+ fcntl(STDERR_FILENO, F_GETPATH, &path);
+ [config setSimulatedApplicationStdErrPath:[NSString stringWithUTF8String:path]];
+
+ _session = [[DTiPhoneSimulatorSession alloc] init];
+ [_session setDelegate:self];
+ [_session setSimulatedApplicationPID:[NSNumber numberWithInt:35]];
+
+ NSError *error;
+ if (![_session requestStartWithConfig:config timeout:30 error:&error]) {
+ WaxLog(@"Could not start simulator session: %@", [error localizedDescription]);
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}
+
+- (void)end {
+ [_session requestEndWithTimeout:0];
+}
+
+// DTiPhoneSimulatorSession Delegate
+// ---------------------------------
+- (void)session:(DTiPhoneSimulatorSession *)session didStart:(BOOL)started withError:(NSError *)error {
+ if (!started) {
+ WaxLog(@"Session failed to start. %@", [error localizedDescription]);
+ exit(EXIT_FAILURE);
+ }
+}
+
+- (void)session:(DTiPhoneSimulatorSession *)session didEndWithError:(NSError *)error {
+ WaxLog(@"Session ended with error. %@", [error localizedDescription]);
+ return exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+@end
117 WaxSim.m
@@ -0,0 +1,117 @@
+#import <AppKit/AppKit.h>
+#import "iPhoneSimulatorRemoteClient.h"
+#import "Simulator.h"
+#import "termios.h"
+
+struct termios savedTermios;
+
+void printUsage();
+int hasInput();
+void ttyRaw();
+void ttyReset();
+
+int main(int argc, char *argv[]) {
+ int c;
+ char *sdk = nil;
+ char *appPath = nil;
+ NSMutableArray *additionalArgs = [NSMutableArray array];
+
+ while ((c = getopt(argc, argv, "s:ah")) != -1) {
+ switch(c) {
+ case 's':
+ sdk = optarg;
+ break;
+ case 'a':
+ fprintf(stdout, "Available SDK Versions.\n", optopt);
+ for (NSString *sdkVersion in [Simulator availableSDKs]) {
+ fprintf(stderr, " %s\n", [sdkVersion UTF8String]);
+ }
+ return 1;
+ case 'h':
+ printUsage();
+ return 1;
+ case '?':
+ if (optopt == 's') {
+ fprintf(stderr, "Option -%c requires an argument.\n", optopt);
+ printUsage();
+ }
+ else {
+ fprintf(stderr, "Unknown option `-%c'.\n", optopt);
+ printUsage();
+ }
+ return 1;
+ break;
+ default:
+ abort ();
+ }
+
+ }
+
+ if (argc > optind) {
+ appPath = argv[optind];
+
+ // Additional args are sent to app
+ for (int i = optind + 1; i < argc; i++) {
+ [additionalArgs addObject:[NSString stringWithUTF8String:argv[i]]];
+ }
+ }
+ else {
+ fprintf(stderr, "No app-path was specified!\n");
+ printUsage();
+ return 1;
+ }
+
+ ttyRaw();
+ atexit(ttyReset);
+
+ NSString *sdkString = sdk ? [NSString stringWithUTF8String:sdk] : nil;
+ NSString *appPathString = [NSString stringWithUTF8String:appPath];
+ Simulator *simulator = [[Simulator alloc] initWithAppPath:appPathString sdk:sdkString args:additionalArgs];
+ [simulator launch];
+
+ while ([[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:-1]]) {
+ int r;
+ unsigned char c;
+
+ if (hasInput()) {
+ if ((r = read(0, &c, sizeof(c))) < 0) continue; // error
+
+ printf("REBOOT %c\n", c);
+ }
+ }
+
+ return 0;
+}
+
+void printUsage() {
+ fprintf(stderr, "usage: waxsim [options] app-path\n");
+ fprintf(stderr, "example: waxsim -s 2.2 /path/to/app.app\n");
+ fprintf(stderr, "Available options are:\n");
+ fprintf(stderr, "\t-s sdk\tVersion number of sdk to use (-s 3.1)\n");
+ fprintf(stderr, "\t-a \tAvailable SDK's\n");
+ fprintf(stderr, "\t-h \tPrints out this wonderful documentation!\n");
+}
+
+int hasInput() {
+ struct timeval tv = { 0L, 0L };
+ fd_set fds;
+ FD_SET(STDIN_FILENO, &fds);
+ return select(1, &fds, NULL, NULL, &tv);
+}
+
+void ttyRaw() {
+ struct termios buf;
+ tcgetattr(STDIN_FILENO, &buf);
+
+ savedTermios = buf;
+
+ buf.c_lflag &= ~ICANON;
+ buf.c_cc[VMIN] = 0;
+ buf.c_cc[VTIME] = 0;
+
+ tcsetattr(STDIN_FILENO, TCSANOW, &buf);
+}
+
+void ttyReset() {
+ tcsetattr(STDIN_FILENO, TCSANOW, &savedTermios);
+}
Oops, something went wrong.

0 comments on commit 6669347

Please sign in to comment.