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 *LocalizedFitScreen75()
54 return NSLocalizedString(
@"Automatic (75%)",
55 @"Screen resolution selector: Automatic resolution at 75% of the "
56 @"screen's pixel size (larger text)");
59NSString *LocalizedFitScreen50()
61 return NSLocalizedString(
@"Automatic (50%)",
62 @"Screen resolution selector: Automatic resolution at 50% of the "
63 @"screen's pixel size (larger text)");
66BOOL IsFitScreenOption(TSXScreenOptions type)
68 return (type == TSXScreenOptionFitScreen || type == TSXScreenOptionFitScreen75 ||
69 type == TSXScreenOptionFitScreen50);
72CGFloat FitScreenScale(TSXScreenOptions type)
76 case TSXScreenOptionFitScreen75:
78 case TSXScreenOptionFitScreen50:
85NSString *LocalizedCustom()
87 return NSLocalizedString(
@"Custom",
@"Screen resolution selector: Custom");
90BOOL ScanScreenResolution(NSString *description,
int *width,
int *height, TSXScreenOptions *type)
94 *type = TSXScreenOptionFixed;
96 if ([description isEqualToString:LocalizedFitScreen()])
98 *type = TSXScreenOptionFitScreen;
101 else if ([description isEqualToString:LocalizedFitScreen75()])
103 *type = TSXScreenOptionFitScreen75;
106 else if ([description isEqualToString:LocalizedFitScreen50()])
108 *type = TSXScreenOptionFitScreen50;
111 else if ([description isEqualToString:LocalizedCustom()])
113 *type = TSXScreenOptionCustom;
117 NSArray *resolution_components = [description
118 componentsSeparatedByCharactersInSet:[NSCharacterSet
119 characterSetWithCharactersInString:@"x*×"]];
121 if ([resolution_components count] != 2)
124 *width = [[resolution_components objectAtIndex:0] intValue];
125 *height = [[resolution_components objectAtIndex:1] intValue];
129NSString *ScreenResolutionDescription(TSXScreenOptions type,
int width,
int height)
131 if (type == TSXScreenOptionFitScreen)
132 return LocalizedFitScreen();
133 else if (type == TSXScreenOptionFitScreen75)
134 return LocalizedFitScreen75();
135 else if (type == TSXScreenOptionFitScreen50)
136 return LocalizedFitScreen50();
137 else if (type == TSXScreenOptionCustom)
138 return LocalizedCustom();
140 return [NSString stringWithFormat:@"%dx%d", width, height];
143NSDictionary *SelectionForColorSetting()
146 [dict setValue:[NSNumber numberWithInt:8]
147 forKey:NSLocalizedString(@"Palette Color (8 Bit)", @"8 bit color selection")];
148 [dict setValue:[NSNumber numberWithInt:15]
149 forKey:NSLocalizedString(@"High Color (15 Bit)", @"15 bit color selection")];
150 [dict setValue:[NSNumber numberWithInt:16]
151 forKey:NSLocalizedString(@"High Color (16 Bit)", @"16 bit color selection")];
152 [dict setValue:[NSNumber numberWithInt:24]
153 forKey:NSLocalizedString(@"True Color (24 Bit)", @"24 bit color selection")];
154 [dict setValue:[NSNumber numberWithInt:32]
155 forKey:NSLocalizedString(@"Highest Quality (32 Bit)", @"32 bit color selection")];
159NSArray *ResolutionModes()
162 [NSArray arrayWithObjects:ScreenResolutionDescription(TSXScreenOptionFitScreen, 0, 0),
163 ScreenResolutionDescription(TSXScreenOptionFitScreen75, 0, 0),
164 ScreenResolutionDescription(TSXScreenOptionFitScreen50, 0, 0),
165 ScreenResolutionDescription(TSXScreenOptionFixed, 640, 480),
166 ScreenResolutionDescription(TSXScreenOptionFixed, 800, 600),
167 ScreenResolutionDescription(TSXScreenOptionFixed, 1024, 768),
168 ScreenResolutionDescription(TSXScreenOptionFixed, 1280, 1024),
169 ScreenResolutionDescription(TSXScreenOptionFixed, 1440, 900),
170 ScreenResolutionDescription(TSXScreenOptionFixed, 1440, 1050),
171 ScreenResolutionDescription(TSXScreenOptionFixed, 1600, 1200),
172 ScreenResolutionDescription(TSXScreenOptionFixed, 1920, 1080),
173 ScreenResolutionDescription(TSXScreenOptionFixed, 1920, 1200),
174 ScreenResolutionDescription(TSXScreenOptionCustom, 0, 0), nil];
178#pragma mark Working with Security Protocols
180NSString *LocalizedAutomaticSecurity()
182 return NSLocalizedString(
@"Automatic",
@"Automatic protocol security selection");
185NSString *ProtocolSecurityDescription(TSXProtocolSecurityOptions type)
187 if (type == TSXProtocolSecurityNLA)
189 else if (type == TSXProtocolSecurityTLS)
191 else if (type == TSXProtocolSecurityRDP)
194 return LocalizedAutomaticSecurity();
197BOOL ScanProtocolSecurity(NSString *description, TSXProtocolSecurityOptions *type)
199 *type = TSXProtocolSecurityRDP;
201 if ([description isEqualToString:
@"NLA"])
203 *type = TSXProtocolSecurityNLA;
206 else if ([description isEqualToString:
@"TLS"])
208 *type = TSXProtocolSecurityTLS;
211 else if ([description isEqualToString:
@"RDP"])
213 *type = TSXProtocolSecurityRDP;
216 else if ([description isEqualToString:LocalizedAutomaticSecurity()])
218 *type = TSXProtocolSecurityAutomatic;
225NSDictionary *SelectionForSecuritySetting()
228 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityAutomatic]
229 forKey:ProtocolSecurityDescription(TSXProtocolSecurityAutomatic)];
230 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityRDP]
231 forKey:ProtocolSecurityDescription(TSXProtocolSecurityRDP)];
232 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityTLS]
233 forKey:ProtocolSecurityDescription(TSXProtocolSecurityTLS)];
234 [dict setValue:[NSNumber numberWithInt:TSXProtocolSecurityNLA]
235 forKey:ProtocolSecurityDescription(TSXProtocolSecurityNLA)];
240#pragma mark Bookmarks
244NSMutableArray *FilterBookmarks(NSArray *bookmarks, NSArray *filter_words)
246 NSMutableArray *matching_items = [NSMutableArray array];
247 NSArray *searched_keys = [NSArray
248 arrayWithObjects:@"label", @"params.hostname", @"params.username", @"params.domain", nil];
252 double match_score = 0.0;
254 for (
int i = 0; i < [searched_keys count]; i++)
256 NSString *val = [cur_bookmark valueForKeyPath:[searched_keys objectAtIndex:i]];
258 if (![val isKindOfClass:[NSString
class]] || ![val length])
261 for (NSString *word in filter_words)
262 if ([val rangeOfString:word
263 options:(NSCaseInsensitiveSearch | NSWidthInsensitiveSearch)]
264 .location != NSNotFound)
265 match_score += (1.0 / [filter_words count]) * pow(2, [searched_keys count] - i);
268 if (match_score > 0.001)
270 addObject:[NSDictionary
271 dictionaryWithObjectsAndKeys:cur_bookmark,
@"bookmark",
272 [NSNumber numberWithFloat:match_score],
277 sortUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
278 return [[obj2 objectForKey:@"score"] compare:[obj1 objectForKey:@"score"]];
280 return matching_items;
283NSMutableArray *FilterHistory(NSArray *history, NSString *filterStr)
285 NSMutableArray *result = [NSMutableArray array];
287 for (NSString *item in history)
289 if ([item rangeOfString:filterStr].location != NSNotFound)
290 [result addObject:item];
296#pragma mark Version Info
297NSString *TSXAppFullVersion()
299 return [NSString stringWithUTF8String:FREERDP_GIT_REVISION];
302#pragma mark iPad/iPhone detection
306 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
311 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone);
315static BOOL g_swap_mouse_buttons = NO;
316void SetSwapMouseButtonsFlag(BOOL swapped)
318 g_swap_mouse_buttons = swapped;
322static BOOL g_invert_scrolling = NO;
323void SetInvertScrollingFlag(BOOL invert)
325 g_invert_scrolling = invert;
329int GetLeftMouseButtonClickEvent(BOOL down)
331 if (g_swap_mouse_buttons)
332 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
334 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
338int GetRightMouseButtonClickEvent(BOOL down)
340 if (g_swap_mouse_buttons)
341 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
343 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
347int GetMouseMoveEvent()
349 return (PTR_FLAGS_MOVE);
353int GetMouseWheelEvent(BOOL down)
355 if (g_invert_scrolling)
359 return (PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x0088));
361 return (PTR_FLAGS_WHEEL | (0x0078));
365CGFloat GetScrollGestureDelta()
370#pragma mark System Info functions
372NSString *TSXGetPrimaryMACAddress(NSString *sep)
374 NSString *macaddress =
@"";
375 struct ifaddrs *addrs;
377 if (getifaddrs(&addrs) < 0)
379 NSLog(
@"getPrimaryMACAddress: getifaddrs failed.");
383 for (
struct ifaddrs *cursor = addrs; cursor !=
nullptr; cursor = cursor->ifa_next)
385 if (strcmp(cursor->ifa_name,
"en0"))
388 if ((cursor->ifa_addr->sa_family == AF_LINK) &&
389 (((
struct sockaddr_dl *)cursor->ifa_addr)->sdl_type == 0x6 ))
391 struct sockaddr_dl *dlAddr = (
struct sockaddr_dl *)cursor->ifa_addr;
393 if (dlAddr->sdl_alen != 6)
396 unsigned char *base = (
unsigned char *)&dlAddr->sdl_data[dlAddr->sdl_nlen];
397 macaddress = [NSString hexStringFromData:base
409BOOL TSXDeviceHasJailBreak()
411 if ([[NSFileManager defaultManager] fileExistsAtPath:
@"/Applications/Cydia.app/"])
414 if ([[NSFileManager defaultManager] fileExistsAtPath:
@"/etc/apt/"])
420NSString *TSXGetPlatform()
423 sysctlbyname(
"hw.machine",
nullptr, &size,
nullptr, 0);
424 char *machine = malloc(size);
425 sysctlbyname(
"hw.machine", machine, &size,
nullptr, 0);
426 NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];