/
SmartActivate.c
116 lines (99 loc) · 3.75 KB
/
SmartActivate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "SmartActivate.h"
#define useLog 0
CFDictionaryRef getProcessInfo(CFStringRef targetCreator, CFStringRef targetName, CFStringRef targetIdentifier) {
/* find an applicarion process specified by theSignature(creator type) from runnning process.
if target application can be found, get information of the process and return as a result
*/
CFIndex nKey = 3;
CFMutableArrayRef keyList = CFArrayCreateMutable(NULL,nKey,&kCFTypeArrayCallBacks);
CFArrayAppendValue(keyList, CFSTR("FileCreator"));
CFArrayAppendValue(keyList, CFSTR("CFBundleIdentifier"));
CFArrayAppendValue(keyList, CFSTR("CFBundleName"));
CFIndex firstKeyIndex;
if (targetCreator != NULL) {
firstKeyIndex = 0;
}
else if (targetIdentifier != NULL) {
firstKeyIndex = 1;
}
else {
firstKeyIndex = 2;
}
if (targetCreator == NULL) targetCreator = (CFStringRef)kCFNull;
if (targetName == NULL) targetName = (CFStringRef)kCFNull;
if (targetIdentifier == NULL) targetIdentifier = (CFStringRef)kCFNull;
CFMutableArrayRef valueList = CFArrayCreateMutable(NULL, nKey,&kCFTypeArrayCallBacks);
CFArrayAppendValue(valueList, targetCreator);
CFArrayAppendValue(valueList, targetIdentifier);
CFArrayAppendValue(valueList, targetName);
CFStringRef dictKey = CFArrayGetValueAtIndex(keyList, firstKeyIndex);
CFArrayRemoveValueAtIndex(keyList, firstKeyIndex);
CFStringRef targetValue = CFArrayGetValueAtIndex(valueList, firstKeyIndex);
CFArrayRemoveValueAtIndex(valueList, firstKeyIndex);
Boolean isFound = false;
ProcessSerialNumber psn = {kNoProcess, kNoProcess};
CFDictionaryRef pDict = NULL;
CFComparisonResult isSameStr;
OSErr err = GetNextProcess(&psn);
while( err == noErr) {
pDict = ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask);
CFStringRef dictValue = CFDictionaryGetValue(pDict, dictKey);
if (dictValue != NULL) {
isSameStr = CFStringCompare(dictValue,targetValue,0);
if (isSameStr == kCFCompareEqualTo){
isFound = true;
for (CFIndex i=0; i < 2; i++) {
CFStringRef secondValue = CFArrayGetValueAtIndex(valueList,i);
if (secondValue != (CFStringRef)kCFNull) {
CFStringRef nextKey = CFArrayGetValueAtIndex(keyList, i);
dictValue = CFDictionaryGetValue(pDict,nextKey);
isSameStr = CFStringCompare(dictValue,secondValue,0);
if (isSameStr != kCFCompareEqualTo) {
isFound = false;
break;
}
}
}
if (isFound) break;
}
}
CFRelease(pDict);
err = GetNextProcess (&psn);
}
CFRelease(keyList);
CFRelease(valueList);
if (isFound) {
#if useLog
printf("getProcessInfo found PSN high:%d, low:%d \n", psn.highLongOfPSN, psn.lowLongOfPSN);
#endif
CFMutableDictionaryRef p_dict_psn = CFDictionaryCreateMutableCopy (NULL, 0, pDict);
CFDictionaryAddValue(p_dict_psn, CFSTR("PSNLow"),
CFNumberCreate(NULL, kCFNumberSInt32Type, &(psn.lowLongOfPSN)));
CFDictionaryAddValue(p_dict_psn, CFSTR("PSNHigh"),
CFNumberCreate(NULL, kCFNumberSInt32Type, &(psn.highLongOfPSN)));
CFRelease(pDict);
return p_dict_psn;
}
else{
return nil;
}
}
ProcessSerialNumber getPSNFromDict(CFDictionaryRef pDict) {
ProcessSerialNumber psn;
CFNumberGetValue(CFDictionaryGetValue(pDict,CFSTR("PSNLow")),kCFNumberSInt32Type,&(psn.lowLongOfPSN));
CFNumberGetValue(CFDictionaryGetValue(pDict,CFSTR("PSNHigh")),kCFNumberSInt32Type,&(psn.highLongOfPSN));
return psn;
}
OSStatus activateForProcessInfo(CFDictionaryRef pDict) {
ProcessSerialNumber psn = getPSNFromDict(pDict);
return SetFrontProcessWithOptions(&psn,kSetFrontProcessFrontWindowOnly);
}
OSStatus activateSelf() {
ProcessSerialNumber psn;
OSErr err = GetCurrentProcess(&psn);
OSStatus result = err;
if (err == noErr) {
result = SetFrontProcessWithOptions(&psn,kSetFrontProcessFrontWindowOnly);
}
return result;
}