12#import "OrderedDictionary.h"
13#import "TSXAdditions.h"
15#import <freerdp/input.h>
16#import <freerdp/version.h>
17#import <freerdp/config.h>
20#include <sys/sysctl.h>
21#include <sys/socket.h>
25BOOL ScanHostNameAndPort(NSString *address, NSString **host,
unsigned short *port)
30 if (![address length])
33 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"rdp://%@", address]];
35 if (!url || ![[url host] length])
39 *port = [[url port] unsignedShortValue];
44#pragma mark Working with Screen Resolutions
46NSString *LocalizedFitScreen()
48 return NSLocalizedString(
@"Automatic",
@"Screen resolution selector: Automatic resolution "
49 @"(Full Screen on iPad, reasonable size on iPhone)");
52NSString *LocalizedCustom()
54 return NSLocalizedString(
@"Custom",
@"Screen resolution selector: Custom");
57BOOL ScanScreenResolution(NSString *description,
int *width,
int *height, TSXScreenOptions *type)
61 *type = TSXScreenOptionFixed;
63 if ([description isEqualToString:LocalizedFitScreen()])
65 *type = TSXScreenOptionFitScreen;
68 else if ([description isEqualToString:LocalizedCustom()])
70 *type = TSXScreenOptionCustom;
74 NSArray *resolution_components = [description
75 componentsSeparatedByCharactersInSet:[NSCharacterSet
76 characterSetWithCharactersInString:@"x*×"]];
78 if ([resolution_components count] != 2)
81 *width = [[resolution_components objectAtIndex:0] intValue];
82 *height = [[resolution_components objectAtIndex:1] intValue];
86NSString *ScreenResolutionDescription(TSXScreenOptions type,
int width,
int height)
88 if (type == TSXScreenOptionFitScreen)
89 return LocalizedFitScreen();
90 else if (type == TSXScreenOptionCustom)
91 return LocalizedCustom();
93 return [NSString stringWithFormat:@"%dx%d", width, height];
96NSDictionary *SelectionForColorSetting()
99 [dict setValue:[NSNumber numberWithInt:8]
100 forKey:NSLocalizedString(@"Palette Color (8 Bit)", @"8 bit color selection")];
101 [dict setValue:[NSNumber numberWithInt:15]
102 forKey:NSLocalizedString(@"High Color (15 Bit)", @"15 bit color selection")];
103 [dict setValue:[NSNumber numberWithInt:16]
104 forKey:NSLocalizedString(@"High Color (16 Bit)", @"16 bit color selection")];
105 [dict setValue:[NSNumber numberWithInt:24]
106 forKey:NSLocalizedString(@"True Color (24 Bit)", @"24 bit color selection")];
107 [dict setValue:[NSNumber numberWithInt:32]
108 forKey:NSLocalizedString(@"Highest Quality (32 Bit)", @"32 bit color selection")];
112NSArray *ResolutionModes()
115 [NSArray arrayWithObjects:ScreenResolutionDescription(TSXScreenOptionFitScreen, 0, 0),
116 ScreenResolutionDescription(TSXScreenOptionFixed, 640, 480),
117 ScreenResolutionDescription(TSXScreenOptionFixed, 800, 600),
118 ScreenResolutionDescription(TSXScreenOptionFixed, 1024, 768),
119 ScreenResolutionDescription(TSXScreenOptionFixed, 1280, 1024),
120 ScreenResolutionDescription(TSXScreenOptionFixed, 1440, 900),
121 ScreenResolutionDescription(TSXScreenOptionFixed, 1440, 1050),
122 ScreenResolutionDescription(TSXScreenOptionFixed, 1600, 1200),
123 ScreenResolutionDescription(TSXScreenOptionFixed, 1920, 1080),
124 ScreenResolutionDescription(TSXScreenOptionFixed, 1920, 1200),
125 ScreenResolutionDescription(TSXScreenOptionCustom, 0, 0), nil];
129#pragma mark Working with Security Protocols
131NSString *LocalizedAutomaticSecurity()
133 return NSLocalizedString(
@"Automatic",
@"Automatic protocol security selection");
136NSString *ProtocolSecurityDescription(TSXProtocolSecurityOptions type)
138 if (type == TSXProtocolSecurityNLA)
140 else if (type == TSXProtocolSecurityTLS)
142 else if (type == TSXProtocolSecurityRDP)
145 return LocalizedAutomaticSecurity();
148BOOL ScanProtocolSecurity(NSString *description, TSXProtocolSecurityOptions *type)
150 *type = TSXProtocolSecurityRDP;
152 if ([description isEqualToString:
@"NLA"])
154 *type = TSXProtocolSecurityNLA;
157 else if ([description isEqualToString:
@"TLS"])
159 *type = TSXProtocolSecurityTLS;
162 else if ([description isEqualToString:
@"RDP"])
164 *type = TSXProtocolSecurityRDP;
167 else if ([description isEqualToString:LocalizedAutomaticSecurity()])
169 *type = TSXProtocolSecurityAutomatic;
176NSDictionary *SelectionForSecuritySetting()
179 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityAutomatic]
180 forKey:ProtocolSecurityDescription(TSXProtocolSecurityAutomatic)];
181 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityRDP]
182 forKey:ProtocolSecurityDescription(TSXProtocolSecurityRDP)];
183 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityTLS]
184 forKey:ProtocolSecurityDescription(TSXProtocolSecurityTLS)];
185 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityNLA]
186 forKey:ProtocolSecurityDescription(TSXProtocolSecurityNLA)];
191#pragma mark Bookmarks
195NSMutableArray *FilterBookmarks(NSArray *bookmarks, NSArray *filter_words)
197 NSMutableArray *matching_items = [NSMutableArray array];
198 NSArray *searched_keys = [NSArray
199 arrayWithObjects:@"label", @"params.hostname", @"params.username", @"params.domain", nil];
203 double match_score = 0.0;
205 for (
int i = 0; i < [searched_keys count]; i++)
207 NSString *val = [cur_bookmark valueForKeyPath:[searched_keys objectAtIndex:i]];
209 if (![val isKindOfClass:[NSString class]] || ![val length])
212 for (NSString *word in filter_words)
213 if ([val rangeOfString:word
214 options:(NSCaseInsensitiveSearch | NSWidthInsensitiveSearch)]
215 .location != NSNotFound)
216 match_score += (1.0 / [filter_words count]) * pow(2, [searched_keys count] - i);
219 if (match_score > 0.001)
221 addObject:[NSDictionary
222 dictionaryWithObjectsAndKeys:cur_bookmark, @"bookmark",
223 [NSNumber numberWithFloat:match_score],
228 sortUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
229 return [[obj2 objectForKey:@"score"] compare:[obj1 objectForKey:@"score"]];
231 return matching_items;
234NSMutableArray *FilterHistory(NSArray *history, NSString *filterStr)
236 NSMutableArray *result = [NSMutableArray array];
238 for (NSString *item in history)
240 if ([item rangeOfString:filterStr].location != NSNotFound)
241 [result addObject:item];
247#pragma mark Version Info
248NSString *TSXAppFullVersion()
250 return [NSString stringWithUTF8String:FREERDP_GIT_REVISION];
253#pragma mark iPad/iPhone detection
257 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
262 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone);
266static BOOL g_swap_mouse_buttons = NO;
267void SetSwapMouseButtonsFlag(BOOL swapped)
269 g_swap_mouse_buttons = swapped;
273static BOOL g_invert_scrolling = NO;
274void SetInvertScrollingFlag(BOOL invert)
276 g_invert_scrolling = invert;
280int GetLeftMouseButtonClickEvent(BOOL down)
282 if (g_swap_mouse_buttons)
283 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
285 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
289int GetRightMouseButtonClickEvent(BOOL down)
291 if (g_swap_mouse_buttons)
292 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
294 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
298int GetMouseMoveEvent()
300 return (PTR_FLAGS_MOVE);
304int GetMouseWheelEvent(BOOL down)
306 if (g_invert_scrolling)
310 return (PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x0088));
312 return (PTR_FLAGS_WHEEL | (0x0078));
316CGFloat GetScrollGestureDelta()
321#pragma mark System Info functions
323NSString *TSXGetPrimaryMACAddress(NSString *sep)
325 NSString *macaddress =
@"";
326 struct ifaddrs *addrs;
328 if (getifaddrs(&addrs) < 0)
330 NSLog(
@"getPrimaryMACAddress: getifaddrs failed.");
334 for (
struct ifaddrs *cursor = addrs; cursor !=
nullptr; cursor = cursor->ifa_next)
336 if (strcmp(cursor->ifa_name,
"en0"))
339 if ((cursor->ifa_addr->sa_family == AF_LINK) &&
340 (((
struct sockaddr_dl *)cursor->ifa_addr)->sdl_type == 0x6 ))
342 struct sockaddr_dl *dlAddr = (
struct sockaddr_dl *)cursor->ifa_addr;
344 if (dlAddr->sdl_alen != 6)
347 unsigned char *base = (
unsigned char *)&dlAddr->sdl_data[dlAddr->sdl_nlen];
348 macaddress = [NSString hexStringFromData:base
360BOOL TSXDeviceHasJailBreak()
362 if ([[NSFileManager defaultManager] fileExistsAtPath:
@"/Applications/Cydia.app/"])
365 if ([[NSFileManager defaultManager] fileExistsAtPath:
@"/etc/apt/"])
371NSString *TSXGetPlatform()
374 sysctlbyname(
"hw.machine",
nullptr, &size,
nullptr, 0);
375 char *machine = malloc(size);
376 sysctlbyname(
"hw.machine", machine, &size,
nullptr, 0);
377 NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];