|
10 | 10 | #import <CoreImage/CoreImage.h>
|
11 | 11 | #import <AppKit/AppKit.h>
|
12 | 12 |
|
13 |
| -void ScanQRCodeOnScreen(void) { |
| 13 | +void ScanQRCodeOnScreen(void) { |
| 14 | + /* check system version and permission status */ |
| 15 | + if (@available(macOS 10.12, *)) { |
| 16 | + BOOL hasPermission = CGPreflightScreenCaptureAccess(); |
| 17 | + NSLog(@"Screen Recording Permission Status: %@", hasPermission ? @"Granted" : @"Not Granted"); |
| 18 | + |
| 19 | + if (!hasPermission) { |
| 20 | + NSLog(@"Requesting Screen Recording Permission..."); |
| 21 | + CGRequestScreenCaptureAccess(); |
| 22 | + |
| 23 | + /* check permission status after request */ |
| 24 | + hasPermission = CGPreflightScreenCaptureAccess(); |
| 25 | + NSLog(@"Screen Recording Permission Status After Request: %@", hasPermission ? @"Granted" : @"Not Granted"); |
| 26 | + |
| 27 | + if (!hasPermission) { |
| 28 | + NSLog(@"Screen Recording Permission Denied"); |
| 29 | + |
| 30 | + /* send notification about permission missing */ |
| 31 | + [[NSNotificationCenter defaultCenter] |
| 32 | + postNotificationName:@"NOTIFY_FOUND_SS_URL" |
| 33 | + object:nil |
| 34 | + userInfo:@{ |
| 35 | + @"urls": @[], |
| 36 | + @"source": @"qrcode", |
| 37 | + @"error": @"Screen Recording permission required. Please grant permission in System Preferences and restart ShadowsocksX-NG" |
| 38 | + }]; |
| 39 | + |
| 40 | + /* open system privacy settings */ |
| 41 | + [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture"]]; |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + NSLog(@"Proceeding with screen capture..."); |
| 47 | + } |
| 48 | + |
14 | 49 | /* displays[] Quartz display ID's */
|
15 | 50 | CGDirectDisplayID *displays = nil;
|
16 |
| - |
17 |
| - CGError err = CGDisplayNoErr; |
18 | 51 | CGDisplayCount dspCount = 0;
|
19 | 52 |
|
20 |
| - /* How many active displays do we have? */ |
21 |
| - err = CGGetActiveDisplayList(0, NULL, &dspCount); |
| 53 | + /* variables for collecting scan information */ |
| 54 | + NSMutableDictionary *scanInfo = [NSMutableDictionary dictionary]; |
| 55 | + NSMutableArray *foundSSUrls = [NSMutableArray array]; |
| 56 | + NSMutableArray *foundQRCodes = [NSMutableArray array]; |
22 | 57 |
|
23 |
| - /* If we are getting an error here then their won't be much to display. */ |
24 |
| - if(err != CGDisplayNoErr) |
25 |
| - { |
26 |
| - NSLog(@"Could not get active display count (%d)\n", err); |
| 58 | + /* How many active displays do we have? */ |
| 59 | + CGError err = CGGetActiveDisplayList(0, NULL, &dspCount); |
| 60 | + |
| 61 | + if(err != CGDisplayNoErr) { |
| 62 | + [[NSNotificationCenter defaultCenter] |
| 63 | + postNotificationName:@"NOTIFY_FOUND_SS_URL" |
| 64 | + object:nil |
| 65 | + userInfo:@{ |
| 66 | + @"urls": @[], |
| 67 | + @"source": @"qrcode", |
| 68 | + @"error": @"Failed to get display list" |
| 69 | + }]; |
27 | 70 | return;
|
28 | 71 | }
|
29 | 72 |
|
| 73 | + scanInfo[@"displayCount"] = @(dspCount); |
| 74 | + NSLog(@"Found %d displays", dspCount); |
| 75 | + |
30 | 76 | /* Allocate enough memory to hold all the display IDs we have. */
|
31 | 77 | displays = calloc((size_t)dspCount, sizeof(CGDirectDisplayID));
|
32 | 78 |
|
33 | 79 | // Get the list of active displays
|
34 |
| - err = CGGetActiveDisplayList(dspCount, |
35 |
| - displays, |
36 |
| - &dspCount); |
37 |
| - |
38 |
| - /* More error-checking here. */ |
39 |
| - if(err != CGDisplayNoErr) |
40 |
| - { |
41 |
| - NSLog(@"Could not get active display list (%d)\n", err); |
| 80 | + err = CGGetActiveDisplayList(dspCount, displays, &dspCount); |
| 81 | + |
| 82 | + if(err != CGDisplayNoErr) { |
| 83 | + free(displays); |
| 84 | + [[NSNotificationCenter defaultCenter] |
| 85 | + postNotificationName:@"NOTIFY_FOUND_SS_URL" |
| 86 | + object:nil |
| 87 | + userInfo:@{ |
| 88 | + @"urls": @[], |
| 89 | + @"source": @"qrcode", |
| 90 | + @"error": @"Failed to get display information" |
| 91 | + }]; |
42 | 92 | return;
|
43 | 93 | }
|
44 | 94 |
|
45 |
| - NSMutableArray* foundSSUrls = [NSMutableArray array]; |
46 |
| - |
47 | 95 | CIDetector *detector = [CIDetector detectorOfType:@"CIDetectorTypeQRCode"
|
48 |
| - context:nil |
49 |
| - options:@{ CIDetectorAccuracy:CIDetectorAccuracyHigh }]; |
| 96 | + context:nil |
| 97 | + options:@{ CIDetectorAccuracy:CIDetectorAccuracyHigh }]; |
50 | 98 |
|
51 |
| - for (unsigned int displaysIndex = 0; displaysIndex < dspCount; displaysIndex++) |
52 |
| - { |
53 |
| - /* Make a snapshot image of the current display. */ |
| 99 | + int totalQRCodesFound = 0; |
| 100 | + int validSSUrlsFound = 0; |
| 101 | + |
| 102 | + for (unsigned int displaysIndex = 0; displaysIndex < dspCount; displaysIndex++) { |
54 | 103 | CGImageRef image = CGDisplayCreateImage(displays[displaysIndex]);
|
55 | 104 | NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image]];
|
| 105 | + |
| 106 | + /* count total QR codes found */ |
| 107 | + totalQRCodesFound += (int)features.count; |
| 108 | + |
56 | 109 | for (CIQRCodeFeature *feature in features) {
|
57 |
| - NSLog(@"%@", feature.messageString); |
58 |
| - if ( [feature.messageString hasPrefix:@"ss://"] ) |
59 |
| - { |
| 110 | + NSLog(@"Found QR Code: %@", feature.messageString); |
| 111 | + [foundQRCodes addObject:feature.messageString]; |
| 112 | + |
| 113 | + if ([feature.messageString hasPrefix:@"ss://"]) { |
60 | 114 | NSURL *url = [NSURL URLWithString:feature.messageString];
|
61 | 115 | if (url) {
|
62 | 116 | [foundSSUrls addObject:url];
|
| 117 | + validSSUrlsFound++; |
63 | 118 | }
|
64 | 119 | }
|
65 | 120 | }
|
66 |
| - CGImageRelease(image); |
| 121 | + CGImageRelease(image); |
67 | 122 | }
|
68 | 123 |
|
69 | 124 | free(displays);
|
70 | 125 |
|
| 126 | + /* prepare notification information */ |
| 127 | + NSString *notificationTitle; |
| 128 | + NSString *notificationSubtitle; |
| 129 | + NSString *notificationBody; |
| 130 | + |
| 131 | + if (totalQRCodesFound == 0) { |
| 132 | + notificationTitle = [NSString stringWithFormat:@"Scanned %d displays", dspCount]; |
| 133 | + notificationSubtitle = @"No QR codes found"; |
| 134 | + notificationBody = @"Try adjusting the QR code position on your screen"; |
| 135 | + } else if (validSSUrlsFound == 0) { |
| 136 | + notificationTitle = [NSString stringWithFormat:@"Found %d QR code(s)", totalQRCodesFound]; |
| 137 | + notificationSubtitle = @"No valid Shadowsocks URLs"; |
| 138 | + notificationBody = @"QR codes found are not Shadowsocks configuration"; |
| 139 | + } else { |
| 140 | + notificationTitle = [NSString stringWithFormat:@"Found %d Shadowsocks URL(s)", validSSUrlsFound]; |
| 141 | + notificationSubtitle = [NSString stringWithFormat:@"Scanned %d displays, found %d QR codes", dspCount, totalQRCodesFound]; |
| 142 | + notificationBody = @"Processing Shadowsocks configuration..."; |
| 143 | + } |
| 144 | + |
71 | 145 | [[NSNotificationCenter defaultCenter]
|
72 | 146 | postNotificationName:@"NOTIFY_FOUND_SS_URL"
|
73 | 147 | object:nil
|
74 |
| - userInfo: @{ @"urls": foundSSUrls, |
75 |
| - @"source": @"qrcode" |
76 |
| - } |
77 |
| - ]; |
| 148 | + userInfo:@{ |
| 149 | + @"urls": foundSSUrls, |
| 150 | + @"source": @"qrcode", |
| 151 | + @"title": notificationTitle, |
| 152 | + @"subtitle": notificationSubtitle, |
| 153 | + @"body": notificationBody, |
| 154 | + @"scanInfo": @{ |
| 155 | + @"displayCount": @(dspCount), |
| 156 | + @"totalQRCodes": @(totalQRCodesFound), |
| 157 | + @"validURLs": @(validSSUrlsFound) |
| 158 | + } |
| 159 | + }]; |
78 | 160 | }
|
79 | 161 |
|
80 | 162 | NSImage* createQRImage(NSString *string, NSSize size) {
|
|
0 commit comments