annotate dep/animia/src/util/osx.cc @ 189:649786bae914

*: etc. code cleanup I've removed most macros and stuff dep/animia: [UNTESTED] use raw C++ instead of Objective-C++
author Paper <mrpapersonic@gmail.com>
date Wed, 06 Dec 2023 19:42:33 -0500
parents c413e475f496
children 2d5823df870f
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
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 namespace animia::internal::osx::util {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7
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
8 #ifdef HAVE_COREFOUNDATION
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
9 /* 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
10 * 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
11 * 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
12 */
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 typedef CFDictionaryRef (*LSCopyApplicationInformationSpec)(int, CFTypeRef, CFArrayRef);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 static LSCopyApplicationInformationSpec LSCopyApplicationInformation = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 static LSASNCreateWithPidSpec LSASNCreateWithPid = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
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
19 /* 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
20 static constexpr int kLSDefaultSessionID = -2;
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
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 static const CFStringRef kLaunchServicesBundleID = CFSTR("com.apple.LaunchServices");
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
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 /* 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
25 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
26 static CFStringRef kLSPIDKey = nullptr;
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
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
28 static bool GetLaunchServicesPrivateSymbols() {
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 CFBundleRef launch_services_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 if (!launch_services_bundle)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32
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
33 LSCopyApplicationInformation = (LSCopyApplicationInformationSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSCopyApplicationInformation"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 if (!LSCopyApplicationInformation)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36
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
37 LSASNCreateWithPid = (LSASNCreateWithPidSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSASNCreateWithPid"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 if (!LSASNCreateWithPid)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
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
41 kLSDisplayNameKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSDisplayNameKey"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 if (!kLSDisplayNameKey)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44
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
45 kLSPIDKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSPIDKey"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 if (!kLSPIDKey)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 return false;
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 return true;
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
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
52 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
53 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
54 if (!GetLaunchServicesPrivateSymbols())
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
57 CFReference<CFTypeRef> asn = LSASNCreateWithPid(kCFAllocatorDefault, pid);
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
59 CFReference<CFArrayRef> request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL);
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
61 CFReference<CFDictionaryRef> dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array.get());
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
62 if (!dictionary.get())
162
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
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
65 CFReference<CFStringRef> str;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
66 {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
67 CFStringRef rstr;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
68 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
69 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
70 str.reset(rstr);
162
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
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
73 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1);
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
74
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
75 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length()))
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
76 return false;
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
78 result.resize(result.find('\0'));
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
79
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 }
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
82
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
83 bool StringFromCFString(CFStringRef string, std::string& result) {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
84 if (!string)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
85 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
86
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
87 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
88 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length()))
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
89 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
90
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
91 return true;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents: 182
diff changeset
92 }
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
93 #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
94
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
95 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
96 /* 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
97 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
98 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
99
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
100 /* Get the initial size of the array
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
101 *
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
102 * 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
103 * Unfortunately, I couldn't care less about handling this. :)
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
104 */
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
105 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
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 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
108 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
109 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
110 }
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
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 /* 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
113 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
114
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
115 /* 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
116 {
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 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
118 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
119 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
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
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 /* 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
123 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
124 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
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 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
127 }
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 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
130 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
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 /* 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
134 int 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 memcpy(&argc, &args.front(), sizeof(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
136
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 /* 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
138 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
139 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
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 /* Find the first null character */
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 size_t null_pos = args.find('\0', sizeof(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
143 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
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 /* Find the last slash */
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 size_t last_slash = args.rfind('/', null_pos);
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 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
149 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
150
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 /* Return our 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
152 result = args.substr(last_slash + 1, null_pos - last_slash - 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
153 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
154 }
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 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
157 result.resize(2 * MAXCOMLEN);
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
158
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
159 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
160 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
161 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
162
165
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
163 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
164 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
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
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 static bool GetProcessName(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
168 #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
169 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
170 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
171 #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
172
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
173 /* 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
174 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
175 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
176 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
177
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 /* 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
179 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
180 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
181 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
182 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
183
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 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
185 }
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
186
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
187 }