12 #import "OrderedDictionary.h"
13 #import "TSXAdditions.h"
15 #import <freerdp/input.h>
16 #import <freerdp/version.h>
17 #import <freerdp/config.h>
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
21 #include <sys/socket.h>
23 #include <net/if_dl.h>
25 BOOL 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
46 NSString *LocalizedFitScreen()
48 return NSLocalizedString(
@"Automatic",
@"Screen resolution selector: Automatic resolution "
49 @"(Full Screen on iPad, reasonable size on iPhone)");
52 NSString *LocalizedCustom()
54 return NSLocalizedString(
@"Custom",
@"Screen resolution selector: Custom");
57 BOOL 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];
86 NSString *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];
96 NSDictionary *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")];
112 NSArray *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
131 NSString *LocalizedAutomaticSecurity()
133 return NSLocalizedString(
@"Automatic",
@"Automatic protocl security selection");
136 NSString *ProtocolSecurityDescription(TSXProtocolSecurityOptions type)
138 if (type == TSXProtocolSecurityNLA)
140 else if (type == TSXProtocolSecurityTLS)
142 else if (type == TSXProtocolSecurityRDP)
145 return LocalizedAutomaticSecurity();
148 BOOL 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;
176 NSDictionary *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
195 NSMutableArray *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;
234 NSMutableArray *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
248 NSString *TSXAppFullVersion()
250 return [NSString stringWithUTF8String:FREERDP_GIT_REVISION];
253 #pragma mark iPad/iPhone detection
257 #ifdef UI_USER_INTERFACE_IDIOM
258 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
266 #ifdef UI_USER_INTERFACE_IDIOM
267 return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
274 static BOOL g_swap_mouse_buttons = NO;
275 void SetSwapMouseButtonsFlag(BOOL swapped)
277 g_swap_mouse_buttons = swapped;
281 static BOOL g_invert_scrolling = NO;
282 void SetInvertScrollingFlag(BOOL invert)
284 g_invert_scrolling = invert;
288 int GetLeftMouseButtonClickEvent(BOOL down)
290 if (g_swap_mouse_buttons)
291 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
293 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
297 int GetRightMouseButtonClickEvent(BOOL down)
299 if (g_swap_mouse_buttons)
300 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
302 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
306 int GetMouseMoveEvent()
308 return (PTR_FLAGS_MOVE);
312 int GetMouseWheelEvent(BOOL down)
314 if (g_invert_scrolling)
318 return (PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x0088));
320 return (PTR_FLAGS_WHEEL | (0x0078));
324 CGFloat GetScrollGestureDelta()
332 NSURL *url = [[[NSURL alloc] initWithString:@"http://www.nonexistingdummyurl.com"] autorelease];
334 [NSData dataWithContentsOfURL:url];
338 #pragma mark System Info functions
340 NSString *TSXGetPrimaryMACAddress(NSString *sep)
342 NSString *macaddress =
@"";
343 struct ifaddrs *addrs;
345 if (getifaddrs(&addrs) < 0)
347 NSLog(
@"getPrimaryMACAddress: getifaddrs failed.");
351 for (
struct ifaddrs *cursor = addrs; cursor != NULL; cursor = cursor->ifa_next)
353 if (strcmp(cursor->ifa_name,
"en0"))
356 if ((cursor->ifa_addr->sa_family == AF_LINK) &&
357 (((
struct sockaddr_dl *)cursor->ifa_addr)->sdl_type == 0x6 ))
359 struct sockaddr_dl *dlAddr = (
struct sockaddr_dl *)cursor->ifa_addr;
361 if (dlAddr->sdl_alen != 6)
364 unsigned char *base = (
unsigned char *)&dlAddr->sdl_data[dlAddr->sdl_nlen];
365 macaddress = [NSString hexStringFromData:base
377 BOOL TSXDeviceHasJailBreak()
379 if ([[NSFileManager defaultManager] fileExistsAtPath:
@"/Applications/Cydia.app/"])
382 if ([[NSFileManager defaultManager] fileExistsAtPath:
@"/etc/apt/"])
388 NSString *TSXGetPlatform()
391 sysctlbyname(
"hw.machine", NULL, &size, NULL, 0);
392 char *machine = malloc(size);
393 sysctlbyname(
"hw.machine", machine, &size, NULL, 0);
394 NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];