Mercurial > minori
annotate dep/animia/src/util/osx.cc @ 198:bc1ae1810855
dep/animia: switch from using classes to global functions
the old idea was ok, but sort of hackish; this method doesn't use classes
at all, and this way (especially important!) we can do wayland stuff AND x11
at the same time, which wasn't really possible without stupid workarounds in
the other method
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 24 Dec 2023 02:59:42 -0500 |
parents | 8548dc425697 |
children | a7d0d543b334 |
rev | line source |
---|---|
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "animia/util/osx.h" |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
2 |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
3 #include <string> |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
4 #include <memory> |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
5 |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
6 #include <sys/sysctl.h> |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
7 #include <libproc.h> |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
8 |
162
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 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
11 #ifdef HAVE_COREFOUNDATION |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
12 /* all of these LaunchServices things use *internal functions* that are subject |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
13 * to change. Granted, it's not very likely that these will change very much |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
14 * because I'm fairly sure Apple uses them lots in their own internal code. |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
15 */ |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
16 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t); |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
17 typedef CFDictionaryRef (*LSCopyApplicationInformationSpec)(int, CFTypeRef, CFArrayRef); |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
19 static LSCopyApplicationInformationSpec LSCopyApplicationInformation = nullptr; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 static LSASNCreateWithPidSpec LSASNCreateWithPid = nullptr; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
21 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
22 /* retrieved from LaunchServicesSPI.h in WebKit */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
23 static constexpr int kLSDefaultSessionID = -2; |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
24 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
25 static const CFStringRef kLaunchServicesBundleID = CFSTR("com.apple.LaunchServices"); |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
27 /* retrieved dynamically */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
28 static CFStringRef kLSDisplayNameKey = nullptr; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
29 static CFStringRef kLSPIDKey = nullptr; |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
30 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
31 static bool GetLaunchServicesPrivateSymbols() { |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
32 CFBundleRef launch_services_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID); |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
33 if (!launch_services_bundle) |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
35 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
36 LSCopyApplicationInformation = (LSCopyApplicationInformationSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSCopyApplicationInformation")); |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
37 if (!LSCopyApplicationInformation) |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
38 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
40 LSASNCreateWithPid = (LSASNCreateWithPidSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSASNCreateWithPid")); |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
41 if (!LSASNCreateWithPid) |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
42 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
44 kLSDisplayNameKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSDisplayNameKey")); |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
45 if (!kLSDisplayNameKey) |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
46 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
47 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
48 kLSPIDKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSPIDKey")); |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
49 if (!kLSPIDKey) |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
51 |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
52 return true; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
53 } |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
54 |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
55 static bool LaunchServicesGetProcessName(pid_t pid, std::string& result) { |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
56 if (!LSCopyApplicationInformation || !LSASNCreateWithPid) |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
57 if (!GetLaunchServicesPrivateSymbols()) |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
60 CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
61 if (!asn) |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
62 return false; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
63 |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
64 CFArrayRef request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
65 if (!request_array) { |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
66 CFRelease(asn); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
67 return false; |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
68 } |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
69 |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
70 CFDictionaryRef dictionary = LSCopyApplicationInformation(kLSDefaultSessionID, asn, request_array); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
71 |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
72 CFRelease(request_array); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
73 CFRelease(asn); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
74 |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
75 if (!dictionary) |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
76 return false; |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
77 |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
78 { |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
79 CFStringRef rstr; |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
80 |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
81 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr) |
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
82 return false; |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
83 |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
84 if (!StringFromCFString(rstr, result)) { |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
85 CFRelease(rstr); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
86 return false; |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
87 } |
169
e44b7c428d7c
dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents:
165
diff
changeset
|
88 |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
89 CFRelease(rstr); |
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
90 } |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
91 |
182
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
92 result.resize(result.find('\0')); |
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
93 |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 return true; |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
95 } |
189 | 96 |
97 bool StringFromCFString(CFStringRef string, std::string& result) { | |
98 if (!string) | |
99 return false; | |
100 | |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
101 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1); |
194
8548dc425697
sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
190
diff
changeset
|
102 if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8)) |
189 | 103 return false; |
104 | |
105 return true; | |
106 } | |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
107 #endif // HAVE_COREFOUNDATION |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
108 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
109 static bool GetProcessArgs(pid_t pid, std::string& args) { |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
110 /* sysctl shouldn't touch these, so we define them as const */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
111 const int mib[3] = {CTL_KERN, KERN_PROCARGS2, static_cast<int>(pid)}; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
112 const size_t mib_size = sizeof(mib)/sizeof(*mib); |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
113 |
182
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
114 /* Get the initial size of the array |
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
115 * |
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
116 * NOTE: it IS possible for this value to change inbetween calls to sysctl(). |
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
117 * Unfortunately, I couldn't care less about handling this. :) |
c413e475f496
dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents:
169
diff
changeset
|
118 */ |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
119 size_t size; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
120 { |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
121 int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0); |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
122 if (ret) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
123 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
124 } |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
125 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
126 /* Reserve the space for it in args */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
127 args.resize(size); |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
128 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
129 /* Get the contents of argc and argv */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
130 { |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
131 int ret = sysctl((int*)mib, mib_size, &args.front(), &size, NULL, 0); |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
132 if (ret) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
133 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
134 } |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
135 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
136 /* Is the size big enough to hold at least argc? */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
137 if (size < sizeof(int)) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
138 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
139 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
140 args.resize(size); |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
141 return true; |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
142 } |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
143 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
144 static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
145 if (!GetProcessArgs(pid, result)) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
146 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
147 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
148 /* Get argc using memcpy */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
149 int argc; |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
150 memcpy(&result, &result.front(), sizeof(argc)); |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
151 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
152 /* Do we even have argv[0]? */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
153 if (argc < 1) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
154 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
155 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
156 /* Find the first null character */ |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
157 size_t null_pos = result.find('\0', sizeof(argc)); |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
158 if (null_pos == std::string::npos) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
159 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
160 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
161 /* Find the last slash */ |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
162 size_t last_slash = result.rfind('/', null_pos); |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
163 if (last_slash == std::string::npos) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
164 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
165 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
166 /* Return our result */ |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
167 result = result.substr(last_slash + 1, null_pos - last_slash - 1); |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
168 return true; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
169 } |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
170 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
171 static bool GetProcessNameFromKernel(pid_t pid, std::string& result) { |
165
8937fb7f2d66
dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
172 result.resize(2 * MAXCOMLEN); |
8937fb7f2d66
dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
173 |
8937fb7f2d66
dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
174 int size = proc_name(pid, &result.front(), result.length()); |
8937fb7f2d66
dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
175 if (!size) |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
176 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
177 |
165
8937fb7f2d66
dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents:
163
diff
changeset
|
178 result.resize(size); |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
179 return true; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
180 } |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
181 |
190
2d5823df870f
dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents:
189
diff
changeset
|
182 bool GetProcessName(pid_t pid, std::string& result) { |
163
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
183 #ifdef HAVE_COREFOUNDATION |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
184 if (LaunchServicesGetProcessName(pid, result)) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
185 return true; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
186 #endif // HAVE_COREFOUNDATION |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
187 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
188 /* Try parsing the arguments, this prevents the process name being |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
189 cut off to 2*MAXCOMLEN (32 chars) */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
190 if (GetProcessNameFromArgs(pid, result)) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
191 return true; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
192 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
193 /* Then attempt getting it from the kernel, which results in the |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
194 process name being cut to 32 chars (worse, 16 chars if p_name is |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
195 unavailable) */ |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
196 if (GetProcessNameFromKernel(pid, result)) |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
197 return true; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
198 |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
199 return false; |
44c5e6dd9488
dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents:
162
diff
changeset
|
200 } |
162
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
201 |
61b76c7b656a
dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
202 } |