11 #import "ios_freerdp.h"
12 #import "ios_freerdp_ui.h"
13 #import "ios_freerdp_events.h"
15 #import "RDPSession.h"
18 #import "ConnectionParams.h"
20 NSString *TSXSessionDidDisconnectNotification =
@"TSXSessionDidDisconnect";
21 NSString *TSXSessionDidFailToConnectNotification =
@"TSXSessionDidFailToConnect";
23 @interface RDPSession (Private)
25 - (void)runSessionFinished:(NSNumber *)result;
29 - (void)sessionWillConnect;
30 - (void)sessionDidConnect;
31 - (void)sessionDidDisconnect;
32 - (void)sessionDidFailToConnect:(
int)reason;
33 - (void)sessionBitmapContextWillChange;
34 - (void)sessionBitmapContextDidChange;
39 @synthesize delegate = _delegate, params = _params, toolbarVisible = _toolbar_visible,
40 uiRequestCompleted = _ui_request_completed, bookmark = _bookmark;
47 static BOOL addArgument(
int *argc,
char ***argv,
const char *fmt, ...)
51 char **tmp = realloc(*argv, (*argc + 1) *
sizeof(
char *));
59 vasprintf(&arg, fmt, ap);
61 (*argv)[*argc - 1] = arg;
65 static BOOL addFlag(
int *argc,
char ***argv,
const char *str, BOOL flag)
67 return addArgument(argc, argv,
"%s%s", flag ?
"+" :
"-", str);
70 static void freeArguments(
int argc,
char **argv)
72 for (
int i = 0; i < argc; i++)
85 if (!(
self = [super init]))
89 [NSException raise:NSInvalidArgumentException
90 format:@"%s: params may not be nil.", __func__];
92 _bookmark = [bookmark retain];
93 _params = [[bookmark params] copy];
94 _name = [[bookmark label] retain];
96 _toolbar_visible = YES;
97 _freerdp = ios_freerdp_new();
98 _ui_request_completed = [[NSCondition alloc] init];
99 BOOL connected_via_3g = ![bookmark conntectedViaWLAN];
101 if (!addArgument(&argc, &argv,
"iFreeRDP"))
104 if (!addArgument(&argc, &argv,
"/gdi:sw"))
111 if ([_params hasValueForKey:
@"colors"])
112 if (!addArgument(&argc, &argv,
"/bpp:%d",
113 [_params intForKey:
@"colors" with3GEnabled:connected_via_3g]))
116 if ([_params hasValueForKey:
@"port"])
117 if (!addArgument(&argc, &argv,
"/port:%d", [_params intForKey:
@"port"]))
120 if ([_params boolForKey:
@"console"])
121 if (!addArgument(&argc, &argv,
"/admin"))
124 if (!addArgument(&argc, &argv,
"/v:%s", [_params UTF8StringForKey:
@"hostname"]))
128 if ([[_params StringForKey:
@"username"] length])
130 if (!addArgument(&argc, &argv,
"/u:%s", [_params UTF8StringForKey:
@"username"]))
134 if ([[_params StringForKey:
@"password"] length])
136 if (!addArgument(&argc, &argv,
"/p:%s", [_params UTF8StringForKey:
@"password"]))
140 if ([[_params StringForKey:
@"domain"] length])
142 if (!addArgument(&argc, &argv,
"/d:%s", [_params UTF8StringForKey:
@"domain"]))
146 if ([[_params StringForKey:
@"working_directory"] length])
148 if (!addArgument(&argc, &argv,
"/shell-dir:%s",
149 [_params UTF8StringForKey:
@"working_directory"]))
153 if ([[_params StringForKey:
@"remote_program"] length])
155 if (!addArgument(&argc, &argv,
"/shell:%s", [_params UTF8StringForKey:
@"remote_program"]))
160 if ([_params boolForKey:
@"perf_remotefx" with3GEnabled:connected_via_3g])
161 if (!addArgument(&argc, &argv,
"/rfx"))
164 if ([_params boolForKey:
@"perf_gfx" with3GEnabled:connected_via_3g])
165 if (!addArgument(&argc, &argv,
"/gfx"))
168 if ([_params boolForKey:
@"perf_h264" with3GEnabled:connected_via_3g])
169 if (!addArgument(&argc, &argv,
"/gfx-h264"))
172 if (![_params boolForKey:
@"perf_remotefx" with3GEnabled:connected_via_3g] &&
173 ![_params boolForKey:
@"perf_gfx" with3GEnabled:connected_via_3g] &&
174 ![_params boolForKey:
@"perf_h264" with3GEnabled:connected_via_3g])
175 if (!addArgument(&argc, &argv,
"/nsc"))
178 if (!addFlag(&argc, &argv,
"bitmap-cache", TRUE))
181 if (!addFlag(&argc, &argv,
"wallpaper",
182 [_params boolForKey:
@"perf_show_desktop" with3GEnabled:connected_via_3g]))
185 if (!addFlag(&argc, &argv,
"window-drag",
186 [_params boolForKey:
@"perf_window_dragging" with3GEnabled:connected_via_3g]))
189 if (!addFlag(&argc, &argv,
"menu-anims",
190 [_params boolForKey:
@"perf_menu_animation" with3GEnabled:connected_via_3g]))
193 if (!addFlag(&argc, &argv,
"themes",
194 [_params boolForKey:
@"perf_windows_themes" with3GEnabled:connected_via_3g]))
197 if (!addFlag(&argc, &argv,
"fonts",
198 [_params boolForKey:
@"perf_font_smoothing" with3GEnabled:connected_via_3g]))
201 if (!addFlag(&argc, &argv,
"aero",
202 [_params boolForKey:
@"perf_desktop_composition" with3GEnabled:connected_via_3g]))
205 if ([_params hasValueForKey:
@"width"])
206 if (!addArgument(&argc, &argv,
"/w:%d", [_params intForKey:
@"width"]))
209 if ([_params hasValueForKey:
@"height"])
210 if (!addArgument(&argc, &argv,
"/h:%d", [_params intForKey:
@"height"]))
214 switch ([_params intForKey:
@"security"])
216 case TSXProtocolSecurityNLA:
217 if (!addArgument(&argc, &argv,
"/sec:NLA"))
222 case TSXProtocolSecurityTLS:
223 if (!addArgument(&argc, &argv,
"/sec:TLS"))
228 case TSXProtocolSecurityRDP:
229 if (!addArgument(&argc, &argv,
"/sec:RDP"))
239 if ([_params boolForKey:
@"enable_tsg_settings"])
241 if (!addArgument(&argc, &argv,
"/g:%s", [_params UTF8StringForKey:
@"tsg_hostname"]))
244 if (!addArgument(&argc, &argv,
"/gp:%d", [_params intForKey:
@"tsg_port"]))
247 if (!addArgument(&argc, &argv,
"/gu:%s", [_params intForKey:
@"tsg_username"]))
250 if (!addArgument(&argc, &argv,
"/gp:%s", [_params intForKey:
@"tsg_password"]))
253 if (!addArgument(&argc, &argv,
"/gd:%s", [_params intForKey:
@"tsg_domain"]))
258 if (!addArgument(&argc, &argv,
"/kbd:%d", 0x409))
262 freerdp_client_settings_parse_command_line(_freerdp->context->settings, argc, argv, FALSE);
267 freeArguments(argc, argv);
268 [
self mfi]->session =
self;
271 freeArguments(argc, argv);
278 [
self setDelegate:nil];
282 [_ui_request_completed release];
283 ios_freerdp_free(_freerdp);
287 - (CGContextRef)bitmapContext
289 return [
self mfi]->bitmap_context;
293 #pragma mark Connecting and disconnecting
298 rdpSettings *settings = _freerdp->context->settings;
303 CGSize size = CGSizeZero;
305 if ([[
self delegate] respondsToSelector:
@selector(sizeForFitScreenForSession:)])
306 size = [[
self delegate] sizeForFitScreenForSession:self];
308 if (!CGSizeEqualToSize(CGSizeZero, size))
310 [_params setInt:size.width forKey:@"width"];
311 [_params setInt:size.height forKey:@"height"];
326 [
self performSelectorInBackground:@selector(runSession) withObject:nil];
331 mfInfo *mfi = [
self mfi];
332 ios_events_send(mfi, [NSDictionary dictionaryWithObject:
@"disconnect" forKey:
@"type"]);
334 if (mfi->connection_state == TSXConnectionConnecting)
337 [
self sessionDidDisconnect];
342 - (TSXConnectionState)connectionState
344 return [
self mfi]->connection_state;
381 #pragma mark Input events
383 - (void)sendInputEvent:(NSDictionary *)eventDescriptor
385 if ([
self mfi]->connection_state == TSXConnectionConnected)
386 ios_events_send([
self mfi], eventDescriptor);
390 #pragma mark Server events (main thread)
392 - (void)setNeedsDisplayInRectAsValue:(NSValue *)rect_value
394 if ([[
self delegate] respondsToSelector:
@selector(session:needsRedrawInRect:)])
395 [[
self delegate] session:self needsRedrawInRect:[rect_value CGRectValue]];
399 #pragma mark interface functions
401 - (UIImage *)getScreenshotWithSize:(CGSize)size
403 NSAssert([
self mfi]->bitmap_context != nil,
404 @"Screenshot requested while having no valid RDP drawing context");
405 CGImageRef cgImage = CGBitmapContextCreateImage([
self mfi]->bitmap_context);
406 UIGraphicsBeginImageContext(size);
407 CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, size.height);
408 CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
409 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height),
411 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
412 UIGraphicsEndImageContext();
413 CGImageRelease(cgImage);
417 - (rdpSettings *)getSessionParams
419 return _freerdp->context->settings;
422 - (NSString *)sessionName
430 @implementation RDPSession (Private)
434 return MFI_FROM_INSTANCE(_freerdp);
440 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
442 [
self performSelectorOnMainThread:@selector(sessionWillConnect)
445 int result_code = ios_run_freerdp(_freerdp);
446 [
self mfi]->connection_state = TSXConnectionDisconnected;
447 [
self performSelectorOnMainThread:@selector(runSessionFinished:)
448 withObject:[NSNumber numberWithInt:result_code]
454 - (void)runSessionFinished:(NSNumber *)result
456 int result_code = [result intValue];
460 case MF_EXIT_CONN_CANCELED:
461 [
self sessionDidDisconnect];
464 case MF_EXIT_LOGON_TIMEOUT:
465 case MF_EXIT_CONN_FAILED:
466 [
self sessionDidFailToConnect:result_code];
469 case MF_EXIT_SUCCESS:
471 [
self sessionDidDisconnect];
477 #pragma mark Session management (main thread)
479 - (void)sessionWillConnect
481 if ([[
self delegate] respondsToSelector:
@selector(sessionWillConnect:)])
482 [[
self delegate] sessionWillConnect:self];
485 - (void)sessionDidConnect
487 if ([[
self delegate] respondsToSelector:
@selector(sessionDidConnect:)])
488 [[
self delegate] sessionDidConnect:self];
491 - (void)sessionDidFailToConnect:(
int)reason
493 [[NSNotificationCenter defaultCenter]
494 postNotificationName:TSXSessionDidFailToConnectNotification
497 if ([[
self delegate] respondsToSelector:
@selector(session:didFailToConnect:)])
498 [[
self delegate] session:self didFailToConnect:reason];
501 - (void)sessionDidDisconnect
503 [[NSNotificationCenter defaultCenter] postNotificationName:TSXSessionDidDisconnectNotification
506 if ([[
self delegate] respondsToSelector:
@selector(sessionDidDisconnect:)])
507 [[
self delegate] sessionDidDisconnect:self];
510 - (void)sessionBitmapContextWillChange
512 if ([[
self delegate] respondsToSelector:
@selector(sessionBitmapContextWillChange:)])
513 [[
self delegate] sessionBitmapContextWillChange:self];
516 - (void)sessionBitmapContextDidChange
518 if ([[
self delegate] respondsToSelector:
@selector(sessionBitmapContextDidChange:)])
519 [[
self delegate] sessionBitmapContextDidChange:self];
522 - (void)sessionRequestsAuthenticationWithParams:(NSMutableDictionary *)params
524 if ([[
self delegate] respondsToSelector:
@selector(session:requestsAuthenticationWithParams:)])
525 [[
self delegate] session:self requestsAuthenticationWithParams:params];
528 - (void)sessionVerifyCertificateWithParams:(NSMutableDictionary *)params
530 if ([[
self delegate] respondsToSelector:
@selector(session:verifyCertificateWithParams:)])
531 [[
self delegate] session:self verifyCertificateWithParams:params];
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.