FreeRDP/client/iOS/Models/RDPSession.m

537 lines
14 KiB
Mathematica
Raw Normal View History

/*
2016-08-05 14:14:55 +03:00
RDP Session object
2013-12-04 14:37:57 +04:00
Copyright 2013 Thincast Technologies GmbH, Authors: Martin Fleisz, Dorian Johnson
2016-08-05 14:14:55 +03:00
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
2019-11-06 17:24:51 +03:00
If a copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
*/
#import "ios_freerdp.h"
#import "ios_freerdp_ui.h"
#import "ios_freerdp_events.h"
#import "RDPSession.h"
#import "TSXTypes.h"
#import "Bookmark.h"
#import "ConnectionParams.h"
2019-11-06 17:24:51 +03:00
NSString *TSXSessionDidDisconnectNotification = @"TSXSessionDidDisconnect";
NSString *TSXSessionDidFailToConnectNotification = @"TSXSessionDidFailToConnect";
2019-11-06 17:24:51 +03:00
@interface RDPSession (Private)
- (void)runSession;
2019-11-06 17:24:51 +03:00
- (void)runSessionFinished:(NSNumber *)result;
- (mfInfo *)mfi;
// The connection thread calls these on the main thread.
- (void)sessionWillConnect;
- (void)sessionDidConnect;
- (void)sessionDidDisconnect;
- (void)sessionDidFailToConnect:(int)reason;
- (void)sessionBitmapContextWillChange;
- (void)sessionBitmapContextDidChange;
@end
@implementation RDPSession
2019-11-06 17:24:51 +03:00
@synthesize delegate = _delegate, params = _params, toolbarVisible = _toolbar_visible,
uiRequestCompleted = _ui_request_completed, bookmark = _bookmark;
+ (void)initialize
{
2016-08-05 14:14:55 +03:00
ios_init_freerdp();
}
2019-11-06 17:24:51 +03:00
static BOOL addArgument(int *argc, char ***argv, const char *fmt, ...)
2016-08-05 14:14:55 +03:00
{
va_list ap;
2019-11-06 17:24:51 +03:00
char *arg = NULL;
char **tmp = realloc(*argv, (*argc + 1) * sizeof(char *));
2016-08-05 14:14:55 +03:00
if (!tmp)
return FALSE;
*argv = tmp;
*argc = *argc + 1;
va_start(ap, fmt);
vasprintf(&arg, fmt, ap);
va_end(ap);
(*argv)[*argc - 1] = arg;
return TRUE;
}
2019-11-06 17:24:51 +03:00
static BOOL addFlag(int *argc, char ***argv, const char *str, BOOL flag)
2016-08-05 14:14:55 +03:00
{
return addArgument(argc, argv, "%s%s", flag ? "+" : "-", str);
}
2019-11-06 17:24:51 +03:00
static void freeArguments(int argc, char **argv)
2016-08-05 14:14:55 +03:00
{
int i;
for (i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
// Designated initializer.
2019-11-06 17:24:51 +03:00
- (id)initWithBookmark:(ComputerBookmark *)bookmark
{
2016-08-05 14:14:55 +03:00
int status;
2019-11-06 17:24:51 +03:00
char **argv = NULL;
2016-08-05 14:14:55 +03:00
int argc = 0;
if (!(self = [super init]))
return nil;
2016-08-05 14:14:55 +03:00
if (!bookmark)
2019-11-06 17:24:51 +03:00
[NSException raise:NSInvalidArgumentException
format:@"%s: params may not be nil.", __func__];
2016-08-05 14:14:55 +03:00
_bookmark = [bookmark retain];
_params = [[bookmark params] copy];
2016-08-05 14:14:55 +03:00
_name = [[bookmark label] retain];
_delegate = nil;
_toolbar_visible = YES;
_freerdp = ios_freerdp_new();
2016-08-05 14:14:55 +03:00
_ui_request_completed = [[NSCondition alloc] init];
BOOL connected_via_3g = ![bookmark conntectedViaWLAN];
if (!addArgument(&argc, &argv, "iFreeRDP"))
goto out_free;
if (!addArgument(&argc, &argv, "/gdi:sw"))
goto out_free;
2019-11-06 17:24:51 +03:00
// Screen Size is set on connect (we need a valid delegate in case the user choose an automatic
// screen size)
2016-08-05 14:14:55 +03:00
// Other simple numeric settings
if ([_params hasValueForKey:@"colors"])
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/bpp:%d",
[_params intForKey:@"colors" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
if ([_params hasValueForKey:@"port"])
2016-08-05 14:14:55 +03:00
if (!addArgument(&argc, &argv, "/port:%d", [_params intForKey:@"port"]))
goto out_free;
if ([_params boolForKey:@"console"])
2016-08-05 14:14:55 +03:00
if (!addArgument(&argc, &argv, "/admin"))
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/v:%s", [_params UTF8StringForKey:@"hostname"]))
2015-06-09 16:22:50 +03:00
goto out_free;
2016-08-05 14:14:55 +03:00
// String settings
if ([[_params StringForKey:@"username"] length])
2015-06-09 16:22:50 +03:00
{
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/u:%s", [_params UTF8StringForKey:@"username"]))
2015-06-09 16:22:50 +03:00
goto out_free;
}
if ([[_params StringForKey:@"password"] length])
2015-06-09 16:22:50 +03:00
{
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/p:%s", [_params UTF8StringForKey:@"password"]))
2015-06-09 16:22:50 +03:00
goto out_free;
}
if ([[_params StringForKey:@"domain"] length])
2015-06-09 16:22:50 +03:00
{
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/d:%s", [_params UTF8StringForKey:@"domain"]))
2015-06-09 16:22:50 +03:00
goto out_free;
}
2016-08-05 14:14:55 +03:00
if ([[_params StringForKey:@"working_directory"] length])
{
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/shell-dir:%s",
[_params UTF8StringForKey:@"working_directory"]))
2016-08-05 14:14:55 +03:00
goto out_free;
}
2016-08-05 14:14:55 +03:00
if ([[_params StringForKey:@"remote_program"] length])
{
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/shell:%s", [_params UTF8StringForKey:@"remote_program"]))
2016-08-05 14:14:55 +03:00
goto out_free;
}
2016-08-05 14:14:55 +03:00
// RemoteFX
if ([_params boolForKey:@"perf_remotefx" with3GEnabled:connected_via_3g])
if (!addArgument(&argc, &argv, "/rfx"))
goto out_free;
if ([_params boolForKey:@"perf_gfx" with3GEnabled:connected_via_3g])
if (!addArgument(&argc, &argv, "/gfx"))
goto out_free;
if ([_params boolForKey:@"perf_h264" with3GEnabled:connected_via_3g])
if (!addArgument(&argc, &argv, "/gfx-h264"))
goto out_free;
if (![_params boolForKey:@"perf_remotefx" with3GEnabled:connected_via_3g] &&
![_params boolForKey:@"perf_gfx" with3GEnabled:connected_via_3g] &&
![_params boolForKey:@"perf_h264" with3GEnabled:connected_via_3g])
if (!addArgument(&argc, &argv, "/nsc"))
goto out_free;
if (!addFlag(&argc, &argv, "bitmap-cache", TRUE))
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addFlag(&argc, &argv, "wallpaper",
[_params boolForKey:@"perf_show_desktop" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addFlag(&argc, &argv, "window-drag",
[_params boolForKey:@"perf_window_dragging" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addFlag(&argc, &argv, "menu-anims",
[_params boolForKey:@"perf_menu_animation" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addFlag(&argc, &argv, "themes",
[_params boolForKey:@"perf_windows_themes" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addFlag(&argc, &argv, "fonts",
[_params boolForKey:@"perf_font_smoothing" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
2019-11-06 17:24:51 +03:00
if (!addFlag(&argc, &argv, "aero",
[_params boolForKey:@"perf_desktop_composition" with3GEnabled:connected_via_3g]))
2016-08-05 14:14:55 +03:00
goto out_free;
if ([_params hasValueForKey:@"width"])
2016-08-05 14:14:55 +03:00
if (!addArgument(&argc, &argv, "/w:%d", [_params intForKey:@"width"]))
goto out_free;
if ([_params hasValueForKey:@"height"])
2016-08-05 14:14:55 +03:00
if (!addArgument(&argc, &argv, "/h:%d", [_params intForKey:@"height"]))
goto out_free;
// security
switch ([_params intForKey:@"security"])
{
case TSXProtocolSecurityNLA:
if (!addArgument(&argc, &argv, "/sec:NLA"))
goto out_free;
break;
case TSXProtocolSecurityTLS:
if (!addArgument(&argc, &argv, "/sec:TLS"))
goto out_free;
break;
case TSXProtocolSecurityRDP:
if (!addArgument(&argc, &argv, "/sec:RDP"))
goto out_free;
break;
default:
break;
}
// ts gateway settings
if ([_params boolForKey:@"enable_tsg_settings"])
{
2019-11-06 17:24:51 +03:00
if (!addArgument(&argc, &argv, "/g:%s", [_params UTF8StringForKey:@"tsg_hostname"]))
2016-08-05 14:14:55 +03:00
goto out_free;
if (!addArgument(&argc, &argv, "/gp:%d", [_params intForKey:@"tsg_port"]))
goto out_free;
if (!addArgument(&argc, &argv, "/gu:%s", [_params intForKey:@"tsg_username"]))
goto out_free;
if (!addArgument(&argc, &argv, "/gp:%s", [_params intForKey:@"tsg_password"]))
goto out_free;
if (!addArgument(&argc, &argv, "/gd:%s", [_params intForKey:@"tsg_domain"]))
goto out_free;
}
// Remote keyboard layout
2016-08-05 14:14:55 +03:00
if (!addArgument(&argc, &argv, "/kbd:%d", 0x409))
goto out_free;
2022-05-03 09:50:28 +03:00
status =
freerdp_client_settings_parse_command_line(_freerdp->context->settings, argc, argv, FALSE);
2016-08-05 14:14:55 +03:00
if (0 != status)
goto out_free;
freeArguments(argc, argv);
[self mfi]->session = self;
return self;
2015-06-09 16:22:50 +03:00
out_free:
2016-08-05 14:14:55 +03:00
freeArguments(argc, argv);
[self release];
return nil;
}
- (void)dealloc
{
[self setDelegate:nil];
2016-08-05 14:14:55 +03:00
[_bookmark release];
[_name release];
[_params release];
2016-08-05 14:14:55 +03:00
[_ui_request_completed release];
ios_freerdp_free(_freerdp);
[super dealloc];
}
- (CGContextRef)bitmapContext
{
2016-08-05 14:14:55 +03:00
return [self mfi]->bitmap_context;
}
#pragma mark -
#pragma mark Connecting and disconnecting
- (void)connect
2016-08-05 14:14:55 +03:00
{
// Set Screen Size to automatic if widht or height are still 0
2022-05-03 09:50:28 +03:00
rdpSettings *settings = _freerdp->context->settings;
2016-08-05 14:14:55 +03:00
2023-10-13 10:48:44 +03:00
if (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) == 0 ||
freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) == 0)
{
2016-08-05 14:14:55 +03:00
CGSize size = CGSizeZero;
if ([[self delegate] respondsToSelector:@selector(sizeForFitScreenForSession:)])
size = [[self delegate] sizeForFitScreenForSession:self];
if (!CGSizeEqualToSize(CGSizeZero, size))
{
[_params setInt:size.width forKey:@"width"];
[_params setInt:size.height forKey:@"height"];
2023-10-13 10:48:44 +03:00
freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, size.width);
freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, size.height);
2016-08-05 14:14:55 +03:00
}
}
2016-08-05 14:14:55 +03:00
2019-11-06 17:24:51 +03:00
// TODO: This is a hack to ensure connections to RDVH with 16bpp don't have an odd screen
// resolution width
2016-08-05 14:14:55 +03:00
// Otherwise this could result in screen corruption ..
if (freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth) <= 16)
2023-10-13 10:48:44 +03:00
{
const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) & (~1);
freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, w);
}
2016-08-05 14:14:55 +03:00
[self performSelectorInBackground:@selector(runSession) withObject:nil];
}
- (void)disconnect
{
2019-11-06 17:24:51 +03:00
mfInfo *mfi = [self mfi];
ios_events_send(mfi, [NSDictionary dictionaryWithObject:@"disconnect" forKey:@"type"]);
if (mfi->connection_state == TSXConnectionConnecting)
{
mfi->unwanted = YES;
[self sessionDidDisconnect];
return;
2016-08-05 14:14:55 +03:00
}
}
- (TSXConnectionState)connectionState
{
return [self mfi]->connection_state;
}
// suspends the session
2019-11-06 17:24:51 +03:00
- (void)suspend
{
2016-08-05 14:14:55 +03:00
if (!_suspended)
{
_suspended = YES;
// instance->update->SuppressOutput(instance->context, 0, NULL);
}
}
// resumes a previously suspended session
2019-11-06 17:24:51 +03:00
- (void)resume
{
2016-08-05 14:14:55 +03:00
if (_suspended)
{
/* RECTANGLE_16 rec;
rec.left = 0;
rec.top = 0;
2023-10-13 10:48:44 +03:00
rec.right = freerdp_settings_get_uint32(instance->settings, FreeRDP_DesktopWidth);
rec.bottom = freerdp_settings_get_uint32(instance->settings, FreeRDP_DesktopHeight);
2016-08-05 14:14:55 +03:00
*/
_suspended = NO;
// instance->update->SuppressOutput(instance->context, 1, &rec);
// [delegate sessionScreenSettingsChanged:self];
}
}
// returns YES if the session is started
2019-11-06 17:24:51 +03:00
- (BOOL)isSuspended
{
2016-08-05 14:14:55 +03:00
return _suspended;
}
#pragma mark -
#pragma mark Input events
2019-11-06 17:24:51 +03:00
- (void)sendInputEvent:(NSDictionary *)eventDescriptor
{
if ([self mfi]->connection_state == TSXConnectionConnected)
ios_events_send([self mfi], eventDescriptor);
}
#pragma mark -
#pragma mark Server events (main thread)
2019-11-06 17:24:51 +03:00
- (void)setNeedsDisplayInRectAsValue:(NSValue *)rect_value
{
if ([[self delegate] respondsToSelector:@selector(session:needsRedrawInRect:)])
[[self delegate] session:self needsRedrawInRect:[rect_value CGRectValue]];
2016-08-05 14:14:55 +03:00
}
#pragma mark -
#pragma mark interface functions
2019-11-06 17:24:51 +03:00
- (UIImage *)getScreenshotWithSize:(CGSize)size
{
2016-08-05 14:14:55 +03:00
NSAssert([self mfi]->bitmap_context != nil,
@"Screenshot requested while having no valid RDP drawing context");
CGImageRef cgImage = CGBitmapContextCreateImage([self mfi]->bitmap_context);
UIGraphicsBeginImageContext(size);
2016-08-05 14:14:55 +03:00
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
2019-11-06 17:24:51 +03:00
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height),
cgImage);
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2016-08-05 14:14:55 +03:00
CGImageRelease(cgImage);
return viewImage;
}
2019-11-06 17:24:51 +03:00
- (rdpSettings *)getSessionParams
{
2022-05-03 09:50:28 +03:00
return _freerdp->context->settings;
}
2019-11-06 17:24:51 +03:00
- (NSString *)sessionName
{
2016-08-05 14:14:55 +03:00
return _name;
}
2016-08-05 14:14:55 +03:00
@end
#pragma mark -
2019-11-06 17:24:51 +03:00
@implementation RDPSession (Private)
2019-11-06 17:24:51 +03:00
- (mfInfo *)mfi
{
return MFI_FROM_INSTANCE(_freerdp);
}
// Blocks until rdp session finishes.
- (void)runSession
{
2019-11-06 17:24:51 +03:00
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2016-08-05 14:14:55 +03:00
// Run the session
2019-11-06 17:24:51 +03:00
[self performSelectorOnMainThread:@selector(sessionWillConnect)
withObject:nil
waitUntilDone:YES];
2016-08-05 14:14:55 +03:00
int result_code = ios_run_freerdp(_freerdp);
[self mfi]->connection_state = TSXConnectionDisconnected;
2019-11-06 17:24:51 +03:00
[self performSelectorOnMainThread:@selector(runSessionFinished:)
withObject:[NSNumber numberWithInt:result_code]
waitUntilDone:YES];
2016-08-05 14:14:55 +03:00
[pool release];
}
// Main thread.
2019-11-06 17:24:51 +03:00
- (void)runSessionFinished:(NSNumber *)result
{
int result_code = [result intValue];
2016-08-05 14:14:55 +03:00
switch (result_code)
{
case MF_EXIT_CONN_CANCELED:
[self sessionDidDisconnect];
break;
2016-08-05 14:14:55 +03:00
case MF_EXIT_LOGON_TIMEOUT:
case MF_EXIT_CONN_FAILED:
[self sessionDidFailToConnect:result_code];
break;
2016-08-05 14:14:55 +03:00
case MF_EXIT_SUCCESS:
default:
2016-08-05 14:14:55 +03:00
[self sessionDidDisconnect];
break;
}
}
#pragma mark -
#pragma mark Session management (main thread)
- (void)sessionWillConnect
2016-08-05 14:14:55 +03:00
{
if ([[self delegate] respondsToSelector:@selector(sessionWillConnect:)])
[[self delegate] sessionWillConnect:self];
}
- (void)sessionDidConnect
{
2016-08-05 14:14:55 +03:00
if ([[self delegate] respondsToSelector:@selector(sessionDidConnect:)])
[[self delegate] sessionDidConnect:self];
}
- (void)sessionDidFailToConnect:(int)reason
2016-08-05 14:14:55 +03:00
{
2019-11-06 17:24:51 +03:00
[[NSNotificationCenter defaultCenter]
postNotificationName:TSXSessionDidFailToConnectNotification
object:self];
if ([[self delegate] respondsToSelector:@selector(session:didFailToConnect:)])
[[self delegate] session:self didFailToConnect:reason];
}
- (void)sessionDidDisconnect
2016-08-05 14:14:55 +03:00
{
2019-11-06 17:24:51 +03:00
[[NSNotificationCenter defaultCenter] postNotificationName:TSXSessionDidDisconnectNotification
object:self];
2016-08-05 14:14:55 +03:00
if ([[self delegate] respondsToSelector:@selector(sessionDidDisconnect:)])
[[self delegate] sessionDidDisconnect:self];
}
- (void)sessionBitmapContextWillChange
{
2019-11-06 17:24:51 +03:00
if ([[self delegate] respondsToSelector:@selector(sessionBitmapContextWillChange:)])
[[self delegate] sessionBitmapContextWillChange:self];
}
- (void)sessionBitmapContextDidChange
{
2019-11-06 17:24:51 +03:00
if ([[self delegate] respondsToSelector:@selector(sessionBitmapContextDidChange:)])
[[self delegate] sessionBitmapContextDidChange:self];
}
2019-11-06 17:24:51 +03:00
- (void)sessionRequestsAuthenticationWithParams:(NSMutableDictionary *)params
{
2019-11-06 17:24:51 +03:00
if ([[self delegate] respondsToSelector:@selector(session:requestsAuthenticationWithParams:)])
2016-08-05 14:14:55 +03:00
[[self delegate] session:self requestsAuthenticationWithParams:params];
}
2019-11-06 17:24:51 +03:00
- (void)sessionVerifyCertificateWithParams:(NSMutableDictionary *)params
{
2019-11-06 17:24:51 +03:00
if ([[self delegate] respondsToSelector:@selector(session:verifyCertificateWithParams:)])
2016-08-05 14:14:55 +03:00
[[self delegate] session:self verifyCertificateWithParams:params];
}
@end