diff options
| author | Joel de Vahl <joel@stalverk80.se> | 2009-01-12 20:04:14 +0000 |
|---|---|---|
| committer | Joel de Vahl <joel@stalverk80.se> | 2009-01-12 20:04:14 +0000 |
| commit | 1d6668583a23b45724e9b107604c0dbfae433f10 (patch) | |
| tree | f1d9dea6a5002f58e93999c9dff8357f4ddd9abf /src | |
| parent | baaf1bcf0d7422e967ed4c8638927d57ca8df45d (diff) | |
| download | zcatch-1d6668583a23b45724e9b107604c0dbfae433f10.tar.gz zcatch-1d6668583a23b45724e9b107604c0dbfae433f10.zip | |
initial server launcher for osx
Diffstat (limited to 'src')
| -rw-r--r-- | src/osxlaunch/client.h (renamed from src/osxlaunch/SDLMain.h) | 0 | ||||
| -rw-r--r-- | src/osxlaunch/client.m (renamed from src/osxlaunch/SDLMain.m) | 107 | ||||
| -rw-r--r-- | src/osxlaunch/server.m | 114 |
3 files changed, 118 insertions, 103 deletions
diff --git a/src/osxlaunch/SDLMain.h b/src/osxlaunch/client.h index 4683df57..4683df57 100644 --- a/src/osxlaunch/SDLMain.h +++ b/src/osxlaunch/client.h diff --git a/src/osxlaunch/SDLMain.m b/src/osxlaunch/client.m index a1e0d156..c6e6203e 100644 --- a/src/osxlaunch/SDLMain.m +++ b/src/osxlaunch/client.m @@ -6,10 +6,9 @@ */ #import <SDL.h> -#import "SDLMain.h" +#import "client.h" #import <sys/param.h> /* for MAXPATHLEN */ #import <unistd.h> -#include <Carbon/Carbon.h> /* For some reaon, Apple removed setAppleMenu from the headers in 10.4, but the method still is there and works. To avoid warnings, we declare @@ -337,105 +336,14 @@ static void CustomApplicationMain (int argc, char **argv) @end - - #ifdef main # undef main #endif -@interface ServerView : NSTextView -{ - NSTask *task; - NSFileHandle *file; -} -- (void)listenTo: (NSTask*)t; -@end - -@implementation ServerView -- (void)listenTo: (NSTask*)t; -{ - NSPipe *pipe; - task = t; - pipe = [NSPipe pipe]; - [task setStandardOutput: pipe]; - file = [pipe fileHandleForReading]; - - [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(outputNotification:) name: NSFileHandleReadCompletionNotification object: file]; - - [file readInBackgroundAndNotify]; -} - -- (void) outputNotification: (NSNotification *) notification -{ - NSData *data = [[[notification userInfo] objectForKey: NSFileHandleNotificationDataItem] retain]; - NSString *string = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding]; - - NSRange end = NSMakeRange([[self string] length], 0); - - [self replaceCharactersInRange: end withString: string]; - end.location += [string length]; - [self scrollRangeToVisible: end]; - - [string release]; - [file readInBackgroundAndNotify]; -} - --(void)windowWillClose:(NSNotification *)notification -{ - [task terminate]; - [NSApp terminate:self]; -} -@end - -void runServer() -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSApp = [NSApplication sharedApplication]; - NSBundle* mainBundle = [NSBundle mainBundle]; - NSTask *task; - task = [[NSTask alloc] init]; - [task setCurrentDirectoryPath: [mainBundle resourcePath]]; - - // run server - NSWindow *window; - ServerView *view; - NSRect graphicsRect; - - graphicsRect = NSMakeRect(100.0, 1000.0, 600.0, 400.0); - - window = [[NSWindow alloc] - initWithContentRect: graphicsRect - styleMask: NSTitledWindowMask - | NSClosableWindowMask - | NSMiniaturizableWindowMask - backing: NSBackingStoreBuffered - defer: NO]; - - [window setTitle: @"Teewars Server"]; - - view = [[[ServerView alloc] initWithFrame: graphicsRect] autorelease]; - [view setEditable: NO]; - - [window setContentView: view]; - [window setDelegate: view]; - [window makeKeyAndOrderFront: nil]; - - [view listenTo: task]; - [task setLaunchPath: [mainBundle pathForAuxiliaryExecutable: @"teeworlds_srv"]]; - [task launch]; - [NSApp run]; - [task terminate]; - - [NSApp release]; - [pool release]; -} - /* Main entry point to executable - should *not* be SDL_main! */ int main (int argc, char **argv) { - UInt32 mod = GetCurrentKeyModifiers(); - /* Copy the arguments into a global variable */ /* This is passed if we are launched by double-clicking */ if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { @@ -453,19 +361,12 @@ int main (int argc, char **argv) gFinderLaunch = NO; } - if(mod & optionKey) - { - runServer(); - } - else - { #if SDL_USE_NIB_FILE - [SDLApplication poseAsClass:[NSApplication class]]; - NSApplicationMain (argc, argv); + [SDLApplication poseAsClass:[NSApplication class]]; + NSApplicationMain (argc, argv); #else - CustomApplicationMain (argc, argv); + CustomApplicationMain (argc, argv); #endif - } return 0; } diff --git a/src/osxlaunch/server.m b/src/osxlaunch/server.m new file mode 100644 index 00000000..263fc408 --- /dev/null +++ b/src/osxlaunch/server.m @@ -0,0 +1,114 @@ +#import <Cocoa/Cocoa.h> + +@interface ServerView : NSTextView +{ + NSTask *task; + NSFileHandle *file; +} +- (void)listenTo: (NSTask*)t; +@end + +@implementation ServerView +- (void)listenTo: (NSTask*)t; +{ + NSPipe *pipe; + task = t; + pipe = [NSPipe pipe]; + [task setStandardOutput: pipe]; + file = [pipe fileHandleForReading]; + + [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(outputNotification:) name: NSFileHandleReadCompletionNotification object: file]; + + [file readInBackgroundAndNotify]; +} + +- (void) outputNotification: (NSNotification *) notification +{ + NSData *data = [[[notification userInfo] objectForKey: NSFileHandleNotificationDataItem] retain]; + NSString *string = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding]; + NSAttributedString *attrstr = [[NSAttributedString alloc] initWithString: string]; + + [[self textStorage] beginEditing]; + [[self textStorage] appendAttributedString: attrstr]; + int length = [[self textStorage] length]; + NSRange range = NSMakeRange(length, 0); + [self scrollRangeToVisible: range]; + [[self textStorage] endEditing]; + + [attrstr release]; + [string release]; + [file readInBackgroundAndNotify]; +} + +-(void)windowWillClose:(NSNotification *)notification +{ + [task terminate]; + [NSApp terminate:self]; +} +@end + +void runServer() +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSApp = [NSApplication sharedApplication]; + NSBundle* mainBundle = [NSBundle mainBundle]; + NSTask *task; + task = [[NSTask alloc] init]; + [task setCurrentDirectoryPath: [mainBundle resourcePath]]; + + // get a server config + NSOpenPanel* openDlg = [NSOpenPanel openPanel]; + [openDlg setCanChooseFiles:YES]; + + if([openDlg runModalForDirectory:nil file:nil] != NSOKButton) + return; + + NSArray* filenames = [openDlg filenames]; + if([filenames count] != 1) + return; + + NSString* filename = [filenames objectAtIndex: 0]; + NSArray* arguments = [NSArray arrayWithObjects: @"-f", filename, nil]; + + // run server + NSWindow *window; + ServerView *view; + NSRect graphicsRect; + + graphicsRect = NSMakeRect(100.0, 1000.0, 600.0, 400.0); + + window = [[NSWindow alloc] + initWithContentRect: graphicsRect + styleMask: NSTitledWindowMask + | NSClosableWindowMask + | NSMiniaturizableWindowMask + backing: NSBackingStoreBuffered + defer: NO]; + + [window setTitle: @"Teeworlds Server"]; + + view = [[[ServerView alloc] initWithFrame: graphicsRect] autorelease]; + [view setEditable: NO]; + [view setRulerVisible: YES]; + + [window setContentView: view]; + [window setDelegate: view]; + [window makeKeyAndOrderFront: nil]; + + [view listenTo: task]; + [task setLaunchPath: [mainBundle pathForAuxiliaryExecutable: @"teeworlds_srv"]]; + [task setArguments: arguments]; + [task launch]; + [NSApp run]; + [task terminate]; + + [NSApp release]; + [pool release]; +} + +int main (int argc, char **argv) +{ + runServer(); + + return 0; +} |