ddnet/src/macos/server.mm

123 lines
3.2 KiB
Plaintext
Raw Normal View History

#import <Cocoa/Cocoa.h>
@interface ServerView : NSTextView
{
NSTask *pTask;
NSFileHandle *pFile;
}
- (void)listenTo: (NSTask *)t;
@end
@implementation ServerView
- (void)listenTo: (NSTask *)t
{
NSPipe *pPipe;
pTask = t;
pPipe = [NSPipe pipe];
[pTask setStandardOutput: pPipe];
pFile = [pPipe fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(outputNotification:) name: NSFileHandleReadCompletionNotification object: pFile];
[pFile readInBackgroundAndNotify];
}
- (void) outputNotification: (NSNotification *) notification
{
NSData *pData = [[[notification userInfo] objectForKey: NSFileHandleNotificationDataItem] retain];
NSString *pString = [[NSString alloc] initWithData: pData encoding: NSASCIIStringEncoding];
NSAttributedString *pAttrStr = [[NSAttributedString alloc] initWithString: pString];
[[self textStorage] appendAttributedString: pAttrStr];
int length = [[self textStorage] length];
NSRange range = NSMakeRange(length, 0);
[self scrollRangeToVisible: range];
[pAttrStr release];
[pString release];
[pFile readInBackgroundAndNotify];
}
-(void)windowWillClose:(NSNotification *)notification
{
[pTask terminate];
[NSApp terminate:self];
}
@end
void runServer()
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
NSApp = [NSApplication sharedApplication];
NSBundle *pMainBundle = [NSBundle mainBundle];
NSTask *pTask;
pTask = [[NSTask alloc] init];
[pTask setCurrentDirectoryPath: [pMainBundle resourcePath]]; // NOLINT(clang-analyzer-nullability.NullablePassedToNonnull)
NSArray *pArguments = [NSArray new];
NSAlert *pAlert = [[[NSAlert alloc] init] autorelease];
[pAlert setMessageText: @"Run DDNet Server"];
[pAlert addButtonWithTitle: @"Use default config"];
[pAlert addButtonWithTitle: @"Select config"];
[pAlert addButtonWithTitle: @"Cancel"];
switch([pAlert runModal])
{
case NSAlertFirstButtonReturn:
break;
case NSAlertThirdButtonReturn:
return;
case NSAlertSecondButtonReturn:
// get a server config
NSOpenPanel *pOpenDlg = [NSOpenPanel openPanel];
[pOpenDlg setCanChooseFiles:YES];
if([pOpenDlg runModal] != NSModalResponseOK)
return;
NSString *pFileName = [[pOpenDlg URL] path];
pArguments = [NSArray arrayWithObjects: @"-f", pFileName, nil];
break;
}
// run server
NSWindow *pWindow;
ServerView *pView;
NSRect GraphicsRect;
GraphicsRect = NSMakeRect(100.0, 1000.0, 600.0, 400.0);
pWindow = [[NSWindow alloc]
initWithContentRect: GraphicsRect
Fix new Obj-C deprecation warnings src/macos/server.mm:74:29: warning: 'NSOKButton' is deprecated: first deprecated in macOS 10.10 [-Wdeprecated-declarations] if([openDlg runModal] != NSOKButton) ^~~~~~~~~~ NSModalResponseOK /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPanel.h:51:5: note: 'NSOKButton' has been explicitly marked deprecated here NSOKButton API_DEPRECATED_WITH_REPLACEMENT("NSModalResponseOK", macos(10.0,10.10)) = NSModalResponseOK, ^ src/macos/server.mm:90:14: warning: 'NSTitledWindowMask' is deprecated: first deprecated in macOS 10.12 [-Wdeprecated-declarations] styleMask: NSTitledWindowMask ^~~~~~~~~~~~~~~~~~ NSWindowStyleMaskTitled /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSWindow.h:885:32: note: 'NSTitledWindowMask' has been explicitly marked deprecated here static const NSWindowStyleMask NSTitledWindowMask API_DEPRECATED_WITH_REPLACEMENT("NSWindowStyleMaskTitled", macos(10.0,10.12)) = NSWindowStyleMaskTitled; ^ src/macos/server.mm:91:5: warning: 'NSClosableWindowMask' is deprecated: first deprecated in macOS 10.12 [-Wdeprecated-declarations] | NSClosableWindowMask ^~~~~~~~~~~~~~~~~~~~ NSWindowStyleMaskClosable /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSWindow.h:886:32: note: 'NSClosableWindowMask' has been explicitly marked deprecated here static const NSWindowStyleMask NSClosableWindowMask API_DEPRECATED_WITH_REPLACEMENT("NSWindowStyleMaskClosable", macos(10.0,10.12)) = NSWindowStyleMaskClosable; ^ src/macos/server.mm:92:5: warning: 'NSMiniaturizableWindowMask' is deprecated: first deprecated in macOS 10.12 [-Wdeprecated-declarations] | NSMiniaturizableWindowMask ^~~~~~~~~~~~~~~~~~~~~~~~~~ NSWindowStyleMaskMiniaturizable /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSWindow.h:887:32: note: 'NSMiniaturizableWindowMask' has been explicitly marked deprecated here static const NSWindowStyleMask NSMiniaturizableWindowMask API_DEPRECATED_WITH_REPLACEMENT("NSWindowStyleMaskMiniaturizable", macos(10.0,10.12)) = NSWindowStyleMaskMiniaturizable; ^ 4 warnings generated.
2022-05-25 15:02:55 +00:00
styleMask: NSWindowStyleMaskTitled
| NSWindowStyleMaskClosable
| NSWindowStyleMaskMiniaturizable
backing: NSBackingStoreBuffered
defer: NO];
[pWindow setTitle: @"DDNet Server"];
pView = [[[ServerView alloc] initWithFrame: GraphicsRect] autorelease];
[pView setEditable: NO];
[pView setRulerVisible: YES];
[pWindow setContentView: pView];
[pWindow setDelegate: (id<NSWindowDelegate>)pView];
[pWindow makeKeyAndOrderFront: nil];
[pView listenTo: pTask];
[pTask setLaunchPath: [pMainBundle pathForAuxiliaryExecutable: @"DDNet-Server"]];
[pTask setArguments: pArguments];
[pTask launch];
[NSApp run];
[pTask terminate];
[NSApp release];
[pPool release];
}
int main(int argc, char **argv)
{
runServer();
return 0;
}