annotate dep/animia/src/util/osx.cc @ 237:a7d0d543b334

*: make OS X builds succeed new script: deploy_build.sh, creates the app bundle
author Paper <paper@paper.us.eu.org>
date Fri, 19 Jan 2024 11:14:44 -0500
parents 8548dc425697
children
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 #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
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
11 /* 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
12 * 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
13 * 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
14 */
162
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
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
21 /* 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
22 static constexpr int kLSDefaultSessionID = -2;
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
23
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
24 static const CFStringRef kLaunchServicesBundleID = CFSTR("com.apple.LaunchServices");
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25
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
26 /* 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
27 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
28 static CFStringRef kLSPIDKey = nullptr;
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
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
30 static bool GetLaunchServicesPrivateSymbols() {
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 CFBundleRef launch_services_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 if (!launch_services_bundle)
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
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
35 LSCopyApplicationInformation = (LSCopyApplicationInformationSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSCopyApplicationInformation"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 if (!LSCopyApplicationInformation)
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
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
39 LSASNCreateWithPid = (LSASNCreateWithPidSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSASNCreateWithPid"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 if (!LSASNCreateWithPid)
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
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
43 kLSDisplayNameKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSDisplayNameKey"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 if (!kLSDisplayNameKey)
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
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
47 kLSPIDKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSPIDKey"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 if (!kLSPIDKey)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53
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
54 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
55 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
56 if (!GetLaunchServicesPrivateSymbols())
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
59 CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
60 if (!asn)
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
63 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
64 if (!request_array) {
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
65 CFRelease(asn);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
66 return false;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
67 }
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 CFDictionaryRef dictionary = LSCopyApplicationInformation(kLSDefaultSessionID, asn, request_array);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
70
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
71 CFRelease(request_array);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
72 CFRelease(asn);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
73
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
74 if (!dictionary)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
75 return false;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
76
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
77 {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
78 CFStringRef rstr;
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
79
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
80 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
81 return false;
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
83 if (!StringFromCFString(rstr, result)) {
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
84 CFRelease(rstr);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
85 return false;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
86 }
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
87
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
88 CFRelease(rstr);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
89 }
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
91 result.resize(result.find('\0'));
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
92
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 }
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
95
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
96 bool StringFromCFString(CFStringRef string, std::string& result) {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
97 if (!string)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
98 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
99
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
100 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1);
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents: 190
diff changeset
101 if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8))
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
102 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
103
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
104 return true;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
105 }
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
106
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 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
108 /* 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
109 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
110 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
111
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
112 /* Get the initial size of the array
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
113 *
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
114 * 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
115 * Unfortunately, I couldn't care less about handling this. :)
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
116 */
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
117 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
118 {
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 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
120 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
121 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
122 }
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
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 /* 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
125 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
126
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 /* 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
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 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
130 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
131 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
132 }
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
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 /* 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
135 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
136 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
137
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 args.resize(size);
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
139 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
140 }
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
141
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 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
143 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
144 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
145
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 /* 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
147 int argc;
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
148 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
149
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
150 /* 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
151 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
152 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
153
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 /* Find the first null character */
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
155 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
156 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
157 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
158
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 /* Find the last slash */
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
160 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
161 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
162 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
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
164 /* Return our result */
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
165 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
166 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
167 }
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
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 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
170 result.resize(2 * MAXCOMLEN);
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
171
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
172 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
173 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
174 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
175
165
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
176 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
177 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
178 }
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
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
180 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
181 #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
182 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
183 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
184 #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
185
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 /* 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
187 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
188 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
189 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
190
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 /* 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
192 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
193 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
194 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
195 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
196
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 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
198 }
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
199
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
200 }