annotate dep/animia/src/util/osx.cc @ 162:61b76c7b656a

dep/animia: add os x launchservices method
author Paper <mrpapersonic@gmail.com>
date Fri, 17 Nov 2023 16:49:57 -0500
parents
children 44c5e6dd9488
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /* A wrapper around multiple LaunchServices things */
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include "animia/util/osx.h"
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <CoreFoundation/CoreFoundation.h>
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <iostream>
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 namespace animia::internal::osx::util {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 #define RDSymbolNameStr(symbol) (CFSTR("_"#symbol))
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 static constexpr int kLaunchServicesMagicConstant = -2; // or -1, dunno the difference
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 typedef CFDictionaryRef (*LSCopyApplicationInformationSpec)(int, CFTypeRef, CFArrayRef);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 static LSCopyApplicationInformationSpec LSCopyApplicationInformation = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 static LSASNCreateWithPidSpec LSASNCreateWithPid = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 static CFStringRef (kLSDisplayNameKey) = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 static CFStringRef (kLSPIDKey) = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 static CFStringRef (kLaunchServicesBundleID) = CFSTR("com.apple.LaunchServices");
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 static bool FindLaunchServicesPrivateSymbols() {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 CFBundleRef launch_services_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 if (!launch_services_bundle)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 LSCopyApplicationInformation = (LSCopyApplicationInformationSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, RDSymbolNameStr(LSCopyApplicationInformation));
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 if (!LSCopyApplicationInformation)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 LSASNCreateWithPid = (LSASNCreateWithPidSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, RDSymbolNameStr(LSASNCreateWithPid));
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 if (!LSASNCreateWithPid)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 kLSDisplayNameKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, RDSymbolNameStr(kLSDisplayNameKey));
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 if (!kLSDisplayNameKey)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 kLSPIDKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, RDSymbolNameStr(kLSPIDKey));
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 if (!kLSPIDKey)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 bool LaunchServicesGetProcessName(pid_t pid, std::string& result) {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 if (!LSCopyApplicationInformation || !LSASNCreateWithPid)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 if (!FindLaunchServicesPrivateSymbols())
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 CFArrayRef request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 CFDictionaryRef dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 CFRelease(request_array);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 if (!dictionary)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 CFStringRef str;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&str) || !str) {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 CFRelease(dictionary);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 CFRetain(str);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 CFRelease(dictionary);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 result.reserve(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8) + 1);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 if (!CFStringGetCString(str, &result.front(), result.length(), result.length())) {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 CFRelease(str);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 CFRelease(str);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 }