FreeRDP
Loading...
Searching...
No Matches
Utils.m
1/*
2 Utility functions
3
4 Copyright 2013 Thincast Technologies GmbH, Authors: Martin Fleisz, Dorian Johnson
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9 */
10
11#import "Utils.h"
12#import "OrderedDictionary.h"
13#import "TSXAdditions.h"
14
15#import <freerdp/input.h>
16#import <freerdp/version.h>
17#import <freerdp/config.h>
18
19#include <sys/types.h>
20#include <sys/sysctl.h>
21#include <sys/socket.h>
22#include <ifaddrs.h>
23#include <net/if_dl.h>
24
25BOOL ScanHostNameAndPort(NSString *address, NSString **host, unsigned short *port)
26{
27 *host = @"";
28 *port = 0;
29
30 if (![address length])
31 return NO;
32
33 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"rdp://%@", address]];
34
35 if (!url || ![[url host] length])
36 return NO;
37
38 *host = [url host];
39 *port = [[url port] unsignedShortValue];
40 return YES;
41}
42
43#pragma mark -
44#pragma mark Working with Screen Resolutions
45
46NSString *LocalizedFitScreen()
47{
48 return NSLocalizedString(@"Automatic", @"Screen resolution selector: Automatic resolution "
49 @"(Full Screen on iPad, reasonable size on iPhone)");
50}
51
52NSString *LocalizedFitScreen75()
53{
54 return NSLocalizedString(@"Automatic (75%)",
55 @"Screen resolution selector: Automatic resolution at 75% of the "
56 @"screen's pixel size (larger text)");
57}
58
59NSString *LocalizedFitScreen50()
60{
61 return NSLocalizedString(@"Automatic (50%)",
62 @"Screen resolution selector: Automatic resolution at 50% of the "
63 @"screen's pixel size (larger text)");
64}
65
66BOOL IsFitScreenOption(TSXScreenOptions type)
67{
68 return (type == TSXScreenOptionFitScreen || type == TSXScreenOptionFitScreen75 ||
69 type == TSXScreenOptionFitScreen50);
70}
71
72CGFloat FitScreenScale(TSXScreenOptions type)
73{
74 switch (type)
75 {
76 case TSXScreenOptionFitScreen75:
77 return 0.75f;
78 case TSXScreenOptionFitScreen50:
79 return 0.5f;
80 default:
81 return 1.0f;
82 }
83}
84
85NSString *LocalizedCustom()
86{
87 return NSLocalizedString(@"Custom", @"Screen resolution selector: Custom");
88}
89
90BOOL ScanScreenResolution(NSString *description, int *width, int *height, TSXScreenOptions *type)
91{
92 *height = 0;
93 *width = 0;
94 *type = TSXScreenOptionFixed;
95
96 if ([description isEqualToString:LocalizedFitScreen()])
97 {
98 *type = TSXScreenOptionFitScreen;
99 return YES;
100 }
101 else if ([description isEqualToString:LocalizedFitScreen75()])
102 {
103 *type = TSXScreenOptionFitScreen75;
104 return YES;
105 }
106 else if ([description isEqualToString:LocalizedFitScreen50()])
107 {
108 *type = TSXScreenOptionFitScreen50;
109 return YES;
110 }
111 else if ([description isEqualToString:LocalizedCustom()])
112 {
113 *type = TSXScreenOptionCustom;
114 return YES;
115 }
116
117 NSArray *resolution_components = [description
118 componentsSeparatedByCharactersInSet:[NSCharacterSet
119 characterSetWithCharactersInString:@"x*×"]];
120
121 if ([resolution_components count] != 2)
122 return NO;
123
124 *width = [[resolution_components objectAtIndex:0] intValue];
125 *height = [[resolution_components objectAtIndex:1] intValue];
126 return YES;
127}
128
129NSString *ScreenResolutionDescription(TSXScreenOptions type, int width, int height)
130{
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();
139
140 return [NSString stringWithFormat:@"%dx%d", width, height];
141}
142
143NSDictionary *SelectionForColorSetting()
144{
145 OrderedDictionary *dict = [OrderedDictionary dictionaryWithCapacity:3];
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")];
156 return dict;
157}
158
159NSArray *ResolutionModes()
160{
161 NSArray *array =
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];
175 return array;
176}
177
178#pragma mark Working with Security Protocols
179
180NSString *LocalizedAutomaticSecurity()
181{
182 return NSLocalizedString(@"Automatic", @"Automatic protocol security selection");
183}
184
185NSString *ProtocolSecurityDescription(TSXProtocolSecurityOptions type)
186{
187 if (type == TSXProtocolSecurityNLA)
188 return @"NLA";
189 else if (type == TSXProtocolSecurityTLS)
190 return @"TLS";
191 else if (type == TSXProtocolSecurityRDP)
192 return @"RDP";
193
194 return LocalizedAutomaticSecurity();
195}
196
197BOOL ScanProtocolSecurity(NSString *description, TSXProtocolSecurityOptions *type)
198{
199 *type = TSXProtocolSecurityRDP;
200
201 if ([description isEqualToString:@"NLA"])
202 {
203 *type = TSXProtocolSecurityNLA;
204 return YES;
205 }
206 else if ([description isEqualToString:@"TLS"])
207 {
208 *type = TSXProtocolSecurityTLS;
209 return YES;
210 }
211 else if ([description isEqualToString:@"RDP"])
212 {
213 *type = TSXProtocolSecurityRDP;
214 return YES;
215 }
216 else if ([description isEqualToString:LocalizedAutomaticSecurity()])
217 {
218 *type = TSXProtocolSecurityAutomatic;
219 return YES;
220 }
221
222 return NO;
223}
224
225NSDictionary *SelectionForSecuritySetting()
226{
227 OrderedDictionary *dict = [OrderedDictionary dictionaryWithCapacity:4];
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)];
236 return dict;
237}
238
239#pragma mark -
240#pragma mark Bookmarks
241
242#import "Bookmark.h"
243
244NSMutableArray *FilterBookmarks(NSArray *bookmarks, NSArray *filter_words)
245{
246 NSMutableArray *matching_items = [NSMutableArray array];
247 NSArray *searched_keys = [NSArray
248 arrayWithObjects:@"label", @"params.hostname", @"params.username", @"params.domain", nil];
249
250 for (ComputerBookmark *cur_bookmark in bookmarks)
251 {
252 double match_score = 0.0;
253
254 for (int i = 0; i < [searched_keys count]; i++)
255 {
256 NSString *val = [cur_bookmark valueForKeyPath:[searched_keys objectAtIndex:i]];
257
258 if (![val isKindOfClass:[NSString class]] || ![val length])
259 continue;
260
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);
266 }
267
268 if (match_score > 0.001)
269 [matching_items
270 addObject:[NSDictionary
271 dictionaryWithObjectsAndKeys:cur_bookmark, @"bookmark",
272 [NSNumber numberWithFloat:match_score],
273 @"score", nil]];
274 }
275
276 [matching_items
277 sortUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
278 return [[obj2 objectForKey:@"score"] compare:[obj1 objectForKey:@"score"]];
279 }];
280 return matching_items;
281}
282
283NSMutableArray *FilterHistory(NSArray *history, NSString *filterStr)
284{
285 NSMutableArray *result = [NSMutableArray array];
286
287 for (NSString *item in history)
288 {
289 if ([item rangeOfString:filterStr].location != NSNotFound)
290 [result addObject:item];
291 }
292
293 return result;
294}
295
296#pragma mark Version Info
297NSString *TSXAppFullVersion()
298{
299 return [NSString stringWithUTF8String:FREERDP_GIT_REVISION];
300}
301
302#pragma mark iPad/iPhone detection
303
304BOOL IsPad()
305{
306 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
307}
308
309BOOL IsPhone()
310{
311 return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone);
312}
313
314// set mouse buttons swapped flag
315static BOOL g_swap_mouse_buttons = NO;
316void SetSwapMouseButtonsFlag(BOOL swapped)
317{
318 g_swap_mouse_buttons = swapped;
319}
320
321// set invert scrolling flag
322static BOOL g_invert_scrolling = NO;
323void SetInvertScrollingFlag(BOOL invert)
324{
325 g_invert_scrolling = invert;
326}
327
328// return event value for left mouse button
329int GetLeftMouseButtonClickEvent(BOOL down)
330{
331 if (g_swap_mouse_buttons)
332 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
333 else
334 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
335}
336
337// return event value for right mouse button
338int GetRightMouseButtonClickEvent(BOOL down)
339{
340 if (g_swap_mouse_buttons)
341 return (PTR_FLAGS_BUTTON1 | (down ? PTR_FLAGS_DOWN : 0));
342 else
343 return (PTR_FLAGS_BUTTON2 | (down ? PTR_FLAGS_DOWN : 0));
344}
345
346// get mouse move event
347int GetMouseMoveEvent()
348{
349 return (PTR_FLAGS_MOVE);
350}
351
352// return mouse wheel event
353int GetMouseWheelEvent(BOOL down)
354{
355 if (g_invert_scrolling)
356 down = !down;
357
358 if (down)
359 return (PTR_FLAGS_WHEEL | PTR_FLAGS_WHEEL_NEGATIVE | (0x0088));
360 else
361 return (PTR_FLAGS_WHEEL | (0x0078));
362}
363
364// scrolling gesture detection delta
365CGFloat GetScrollGestureDelta()
366{
367 return 10.0f;
368}
369
370#pragma mark System Info functions
371
372NSString *TSXGetPrimaryMACAddress(NSString *sep)
373{
374 NSString *macaddress = @"";
375 struct ifaddrs *addrs;
376
377 if (getifaddrs(&addrs) < 0)
378 {
379 NSLog(@"getPrimaryMACAddress: getifaddrs failed.");
380 return macaddress;
381 }
382
383 for (struct ifaddrs *cursor = addrs; cursor != nullptr; cursor = cursor->ifa_next)
384 {
385 if (strcmp(cursor->ifa_name, "en0"))
386 continue;
387
388 if ((cursor->ifa_addr->sa_family == AF_LINK) &&
389 (((struct sockaddr_dl *)cursor->ifa_addr)->sdl_type == 0x6 /*IFT_ETHER*/))
390 {
391 struct sockaddr_dl *dlAddr = (struct sockaddr_dl *)cursor->ifa_addr;
392
393 if (dlAddr->sdl_alen != 6)
394 continue;
395
396 unsigned char *base = (unsigned char *)&dlAddr->sdl_data[dlAddr->sdl_nlen];
397 macaddress = [NSString hexStringFromData:base
398 ofSize:6
399 withSeparator:sep
400 afterNthChar:1];
401 break;
402 }
403 }
404
405 freeifaddrs(addrs);
406 return macaddress;
407}
408
409BOOL TSXDeviceHasJailBreak()
410{
411 if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Applications/Cydia.app/"])
412 return YES;
413
414 if ([[NSFileManager defaultManager] fileExistsAtPath:@"/etc/apt/"])
415 return YES;
416
417 return NO;
418}
419
420NSString *TSXGetPlatform()
421{
422 size_t size;
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];
427 free(machine);
428 return platform;
429}