FreeRDP/client/Mac/cli/AppDelegate.m

258 lines
6.4 KiB
Mathematica
Raw Normal View History

//
// AppDelegate.m
// MacClient2
//
// Created by Benoît et Kathy on 2013-05-08.
//
//
#import "AppDelegate.h"
#import "MacFreeRDP/mfreerdp.h"
#import "MacFreeRDP/mf_client.h"
#import "MacFreeRDP/MRDPView.h"
#import <freerdp/client/cmdline.h>
static AppDelegate* _singleDelegate = nil;
void AppDelegate_EmbedWindowEventHandler(void* context, EmbedWindowEventArgs* e);
void AppDelegate_ConnectionResultEventHandler(void* context, ConnectionResultEventArgs* e);
void AppDelegate_ErrorInfoEventHandler(void* ctx, ErrorInfoEventArgs* e);
void mac_set_view_size(rdpContext* context, MRDPView* view);
@implementation AppDelegate
- (void)dealloc
{
2013-06-23 04:27:23 +04:00
[super dealloc];
}
@synthesize window = window;
@synthesize context = context;
- (void) applicationDidFinishLaunching:(NSNotification*)aNotification
{
2013-06-23 04:27:23 +04:00
int status;
2013-06-23 06:55:51 +04:00
mfContext* mfc;
2013-06-23 04:27:23 +04:00
_singleDelegate = self;
2013-06-23 04:27:23 +04:00
[self CreateContext];
status = [self ParseCommandLineArguments];
2013-06-23 06:55:51 +04:00
mfc = (mfContext*) context;
mfc->view = (void*) mrdpView;
2013-06-23 04:27:23 +04:00
if (status < 0)
{
}
else
{
PubSub_SubscribeConnectionResult(context->pubSub, AppDelegate_ConnectionResultEventHandler);
PubSub_SubscribeErrorInfo(context->pubSub, AppDelegate_ErrorInfoEventHandler);
PubSub_SubscribeEmbedWindow(context->pubSub, AppDelegate_EmbedWindowEventHandler);
2013-06-23 06:55:51 +04:00
freerdp_client_start(context);
2013-06-23 04:27:23 +04:00
}
}
- (void) applicationWillTerminate:(NSNotification*)notification
{
NSLog(@"Stopping...\n");
freerdp_client_stop(context);
2013-06-23 04:27:23 +04:00
[mrdpView releaseResources];
2013-12-11 23:31:54 +04:00
_singleDelegate = nil;
NSLog(@"Stopped.\n");
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
2013-06-23 04:27:23 +04:00
return YES;
}
- (int) ParseCommandLineArguments
{
2013-06-23 04:27:23 +04:00
int i;
int length;
2013-06-23 06:55:51 +04:00
int status;
2013-06-23 04:27:23 +04:00
char* cptr;
2013-06-23 08:18:55 +04:00
NSArray* args = [[NSProcessInfo processInfo] arguments];
context->argc = (int) [args count];
context->argv = malloc(sizeof(char*) * context->argc);
i = 0;
2013-06-23 08:18:55 +04:00
for (NSString* str in args)
2013-06-23 04:27:23 +04:00
{
/* filter out some arguments added by XCode */
if ([str isEqualToString:@"YES"])
continue;
if ([str isEqualToString:@"-NSDocumentRevisionsDebugMode"])
continue;
length = (int) ([str length] + 1);
cptr = (char*) malloc(length);
2013-06-23 04:27:23 +04:00
strcpy(cptr, [str UTF8String]);
context->argv[i++] = cptr;
2013-06-23 04:27:23 +04:00
}
context->argc = i;
status = freerdp_client_settings_parse_command_line(context->settings, context->argc, context->argv);
status = freerdp_client_settings_command_line_status_print(context->settings, status, context->argc, context->argv);
2013-06-23 04:27:23 +04:00
return status;
}
- (void) CreateContext
{
RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
2013-06-23 04:27:23 +04:00
ZeroMemory(&clientEntryPoints, sizeof(RDP_CLIENT_ENTRY_POINTS));
clientEntryPoints.Size = sizeof(RDP_CLIENT_ENTRY_POINTS);
clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
2013-06-23 04:27:23 +04:00
RdpClientEntry(&clientEntryPoints);
2013-06-23 04:27:23 +04:00
context = freerdp_client_context_new(&clientEntryPoints);
}
- (void) ReleaseContext
{
mfContext* mfc;
MRDPView* view;
mfc = (mfContext*) context;
view = (MRDPView*) mfc->view;
[view releaseResources];
[view release];
mfc->view = nil;
2013-06-23 08:18:55 +04:00
freerdp_client_context_free(context);
2013-06-23 04:27:23 +04:00
context = nil;
}
/** *********************************************************************
* called when we fail to connect to a RDP server - Make sure this is called from the main thread.
***********************************************************************/
- (void) rdpConnectError : (NSString*) withMessage
{
NSString* message = withMessage ? withMessage : @"Error connecting to server";
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:message];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
/** *********************************************************************
* just a terminate selector for above call
***********************************************************************/
- (void) alertDidEnd:(NSAlert *)a returnCode:(NSInteger)rc contextInfo:(void *)ci
{
[NSApp terminate:nil];
}
@end
void AppDelegate_EmbedWindowEventHandler(void* ctx, EmbedWindowEventArgs* e)
{
rdpContext* context = (rdpContext*) ctx;
2013-12-11 23:31:54 +04:00
if (_singleDelegate)
{
mfContext* mfc = (mfContext*) context;
_singleDelegate->mrdpView = mfc->view;
if (_singleDelegate->window)
{
[[_singleDelegate->window contentView] addSubview:mfc->view];
}
mac_set_view_size(context, mfc->view);
2013-12-11 23:31:54 +04:00
}
}
/** *********************************************************************
* On connection error, display message and quit application
***********************************************************************/
void AppDelegate_ConnectionResultEventHandler(void* ctx, ConnectionResultEventArgs* e)
{
NSLog(@"ConnectionResult event result:%d\n", e->result);
if (_singleDelegate)
{
if (e->result != 0)
{
NSString* message = nil;
if (connectErrorCode == AUTHENTICATIONERROR)
{
2013-12-18 22:00:16 +04:00
message = [NSString stringWithFormat:@"%@", @"Authentication failure, check credentials."];
}
// Making sure this should be invoked on the main UI thread.
[_singleDelegate performSelectorOnMainThread:@selector(rdpConnectError:) withObject:message waitUntilDone:FALSE];
}
}
}
void AppDelegate_ErrorInfoEventHandler(void* ctx, ErrorInfoEventArgs* e)
{
NSLog(@"ErrorInfo event code:%d\n", e->code);
if (_singleDelegate)
{
// Retrieve error message associated with error code
NSString* message = nil;
if (e->code != ERRINFO_NONE)
{
const char* errorMessage = freerdp_get_error_info_string(e->code);
message = [[NSString alloc] initWithUTF8String:errorMessage];
}
// Making sure this should be invoked on the main UI thread.
[_singleDelegate performSelectorOnMainThread:@selector(rdpConnectError:) withObject:message waitUntilDone:TRUE];
[message release];
}
}
void mac_set_view_size(rdpContext* context, MRDPView* view)
{
// set client area to specified dimensions
NSRect innerRect;
innerRect.origin.x = 0;
innerRect.origin.y = 0;
innerRect.size.width = context->settings->DesktopWidth;
innerRect.size.height = context->settings->DesktopHeight;
[view setFrame:innerRect];
// calculate window of same size, but keep position
NSRect outerRect = [[view window] frame];
outerRect.size = [[view window] frameRectForContentRect:innerRect].size;
// we are not in RemoteApp mode, disable larger than resolution
[[view window] setContentMaxSize:innerRect.size];
// set window to given area
[[view window] setFrame:outerRect display:YES];
2013-12-11 23:31:54 +04:00
if (context->settings->Fullscreen)
[[view window] toggleFullScreen:nil];
}