14#import <winpr/assert.h>
15#import <freerdp/client/cmdline.h>
18void AppDelegate_ConnectionResultEventHandler(
void *context,
const ConnectionResultEventArgs *e);
19void AppDelegate_ErrorInfoEventHandler(
void *ctx,
const ErrorInfoEventArgs *e);
20void AppDelegate_EmbedWindowEventHandler(
void *context,
const EmbedWindowEventArgs *e);
21void AppDelegate_ResizeWindowEventHandler(
void *context,
const ResizeWindowEventArgs *e);
22void mac_set_view_size(rdpContext *context,
MRDPView *view);
31@synthesize window = window;
33@synthesize context = context;
35- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
39 _singleDelegate =
self;
41 status = [
self ParseCommandLineArguments];
45 mfc->view = (
void *)mrdpView;
49 NSScreen *screen = [[NSScreen screens] objectAtIndex:0];
50 NSRect screenFrame = [screen frame];
51 rdpSettings *settings = context->settings;
53 WINPR_ASSERT(settings);
58 screenFrame.size.width);
60 screenFrame.size.height);
63 PubSub_SubscribeConnectionResult(context->pubSub, AppDelegate_ConnectionResultEventHandler);
64 PubSub_SubscribeErrorInfo(context->pubSub, AppDelegate_ErrorInfoEventHandler);
65 PubSub_SubscribeEmbedWindow(context->pubSub, AppDelegate_EmbedWindowEventHandler);
66 PubSub_SubscribeResizeWindow(context->pubSub, AppDelegate_ResizeWindowEventHandler);
67 freerdp_client_start(context);
71 if (WindowTitle && WindowTitle[0])
73 winTitle = [[NSString alloc]
74 initWithFormat:@"%@", [NSString stringWithCString:WindowTitle
75 encoding:NSUTF8StringEncoding]];
81 winTitle = [[NSString alloc]
82 initWithFormat:@"%@:%u",
83 [NSString stringWithCString:name encoding:NSUTF8StringEncoding],
87 [window setTitle:winTitle];
91 [NSApp terminate:self];
95- (void)applicationWillBecomeActive:(NSNotification *)notification
100- (void)applicationWillResignActive:(NSNotification *)notification
105- (void)applicationWillTerminate:(NSNotification *)notification
107 NSLog(
@"Stopping...\n");
108 freerdp_client_stop(context);
109 [mrdpView releaseResources];
110 _singleDelegate = nil;
111 NSLog(
@"Stopped.\n");
112 [NSApp terminate:self];
115- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
120- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app
125- (int)ParseCommandLineArguments
128 NSArray *args = [[NSProcessInfo processInfo] arguments];
131 context->argc = WINPR_ASSERTING_INT_CAST(
int, [args count]);
132 context->argv = calloc(
sizeof(
char *), context->argc);
134 for (NSString *str in args)
137 if ([str isEqualToString:
@"YES"])
140 if ([str isEqualToString:
@"-NSDocumentRevisionsDebugMode"])
143 const size_t length = ([str lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
146 char *cptr = (
char *)malloc(length);
149 (void)sprintf_s(cptr, length,
"%s", [str UTF8String]);
150 context->argv[i++] = cptr;
154 const int status = freerdp_client_settings_parse_command_line(context->settings, context->argc,
155 context->argv, FALSE);
156 freerdp_client_settings_command_line_status_print(context->settings, status, context->argc,
163 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = WINPR_C_ARRAY_INIT;
165 clientEntryPoints.Size =
sizeof(RDP_CLIENT_ENTRY_POINTS);
166 clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
167 RdpClientEntry(&clientEntryPoints);
168 context = freerdp_client_context_new(&clientEntryPoints);
171- (void)ReleaseContext
177 [view exitFullScreenModeWithOptions:nil];
178 [view releaseResources];
181 freerdp_client_context_free(context);
189- (void)rdpConnectError:(NSString *)withMessage
195 [view exitFullScreenModeWithOptions:nil];
196 NSString *message = withMessage ? withMessage :
@"Error connecting to server";
197 NSAlert *alert = [[NSAlert alloc] init];
198 [alert setMessageText:message];
199 [alert beginSheetModalForWindow:[
self window]
201 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
209- (void)alertDidEnd:(NSAlert *)a returnCode:(NSInteger)rc contextInfo:(
void *)ci
211 [NSApp terminate:nil];
220void AppDelegate_ConnectionResultEventHandler(
void *ctx,
const ConnectionResultEventArgs *e)
222 rdpContext *context = (rdpContext *)ctx;
223 NSLog(
@"ConnectionResult event result:%d\n", e->result);
229 NSString *message = nil;
230 DWORD code = freerdp_get_last_error(context);
233 case FREERDP_ERROR_AUTHENTICATION_FAILED:
235 stringWithFormat:@"%@", @"Authentication failure, check credentials."];
242 [_singleDelegate performSelectorOnMainThread:@selector(rdpConnectError:)
244 waitUntilDone:FALSE];
249void AppDelegate_ErrorInfoEventHandler(
void *ctx,
const ErrorInfoEventArgs *e)
251 NSLog(
@"ErrorInfo event code:%d\n", e->code);
256 NSString *message = nil;
258 if (e->code != ERRINFO_NONE)
260 const char *errorMessage = freerdp_get_error_info_string(e->code);
261 message = [[NSString alloc] initWithUTF8String:errorMessage];
265 [_singleDelegate performSelectorOnMainThread:@selector(rdpConnectError:)
272void AppDelegate_EmbedWindowEventHandler(
void *ctx,
const EmbedWindowEventArgs *e)
274 rdpContext *context = (rdpContext *)ctx;
279 _singleDelegate->mrdpView = mfc->view;
281 if (_singleDelegate->window)
283 [[_singleDelegate->window contentView] addSubview:mfc->view];
286 dispatch_async(dispatch_get_main_queue(), ^{
287 mac_set_view_size(context, mfc->view);
292void AppDelegate_ResizeWindowEventHandler(
void *ctx,
const ResizeWindowEventArgs *e)
294 rdpContext *context = (rdpContext *)ctx;
295 (void)fprintf(stderr,
"ResizeWindowEventHandler: %d %d\n", e->width, e->height);
300 dispatch_async(dispatch_get_main_queue(), ^{
301 mac_set_view_size(context, mfc->view);
306void mac_set_view_size(rdpContext *context,
MRDPView *view)
310 innerRect.origin.x = 0;
311 innerRect.origin.y = 0;
314 [view setFrame:innerRect];
316 NSRect outerRect = [[view window] frame];
317 outerRect.size = [[view window] frameRectForContentRect:innerRect].size;
319 [[view window] setContentMaxSize:innerRect.size];
321 [[view window] setFrame:outerRect display:YES];
323 [NSApp activateIgnoringOtherApps:YES];
326 [[view window] toggleFullScreen:nil];
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.