about summary refs log tree commit diff
path: root/src/osxlaunch/main.m
blob: 5f3d6ab6521a20b4485afd643b2cf6854e989eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#import <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h>

@interface ServerView : NSTextView
{
	NSFileHandle *file;
}
- (void)listenTo: (NSFileHandle*)f;
@end

@implementation ServerView
- (void)listenTo: (NSFileHandle*)f;
{
	file = f;

	[[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
{
    [NSApp terminate:self];
}
@end

int main(int argc, char **argv)
{
	UInt32 mod = GetCurrentKeyModifiers();

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSApp = [NSApplication sharedApplication];
	NSBundle* mainBundle = [NSBundle mainBundle];
	NSTask *task;
    task = [[NSTask alloc] init];
	[task setCurrentDirectoryPath: [mainBundle resourcePath]]; 
	NSPipe *pipe;
	NSFileHandle *file;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    file = [pipe fileHandleForReading];


	if(mod & optionKey)
	{
		// 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: file];
		[task setLaunchPath: [mainBundle pathForAuxiliaryExecutable: @"teewars_srv"]];
		[task launch];
		[NSApp run];
		[task terminate];
	}
	else
	{
		// run client
		[task setLaunchPath: [mainBundle pathForAuxiliaryExecutable: @"teewars"]];
		[task launch];
		[task waitUntilExit];
	}



    [NSApp release];
    [pool release];
    return(EXIT_SUCCESS);
}