annotate dep/animia/src/util/osx.cc @ 182:c413e475f496

dep/animia: various stylistic changes
author Paper <mrpapersonic@gmail.com>
date Mon, 04 Dec 2023 13:19:54 -0500
parents e44b7c428d7c
children 649786bae914
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
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
6 #ifdef HAVE_COREFOUNDATION
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <CoreFoundation/CoreFoundation.h>
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 #endif
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 namespace animia::internal::osx::util {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
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 #ifdef HAVE_COREFOUNDATION
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
13 /* I don't want to have to call CFRelease */
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
14 template<typename T>
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
15 struct CFDeleter {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
16 using pointer = T;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
17 void operator()(pointer p) { CFRelease(p); }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
18 }
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
19
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
20 template<typename T>
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
21 typedef CFReference = std::unique_ptr<T, CFDeleter>;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
22
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
23 /* 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
24 * 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
25 * 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
26 */
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 typedef CFDictionaryRef (*LSCopyApplicationInformationSpec)(int, CFTypeRef, CFArrayRef);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 static LSCopyApplicationInformationSpec LSCopyApplicationInformation = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 static LSASNCreateWithPidSpec LSASNCreateWithPid = nullptr;
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 /* 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
34 static constexpr int kLSDefaultSessionID = -2;
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
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 static const CFStringRef kLaunchServicesBundleID = CFSTR("com.apple.LaunchServices");
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37
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
38 /* 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
39 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
40 static CFStringRef kLSPIDKey = nullptr;
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41
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
42 static bool GetLaunchServicesPrivateSymbols() {
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 CFBundleRef launch_services_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 if (!launch_services_bundle)
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 LSCopyApplicationInformation = (LSCopyApplicationInformationSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSCopyApplicationInformation"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 if (!LSCopyApplicationInformation)
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
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
51 LSASNCreateWithPid = (LSASNCreateWithPidSpec)CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSASNCreateWithPid"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 if (!LSASNCreateWithPid)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54
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 kLSDisplayNameKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSDisplayNameKey"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 if (!kLSDisplayNameKey)
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
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
59 kLSPIDKey = *(CFStringRef*)CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSPIDKey"));
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 if (!kLSPIDKey)
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
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65
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
66 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
67 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
68 if (!GetLaunchServicesPrivateSymbols())
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
71 CFReference<CFTypeRef> asn = LSASNCreateWithPid(kCFAllocatorDefault, pid);
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
73 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
74
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
75 CFReference<CFDictionaryRef> dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array.get());
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
76 if (!dictionary.get())
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
79 CFReference<CFStringRef> str;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
80 {
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
81 CFStringRef rstr;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
82 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr)
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
83 return false;
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
84 str.reset(rstr);
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
87 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1);
169
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
88
e44b7c428d7c dep/animia: add libkvm method (UNTESTED)
Paper <mrpapersonic@gmail.com>
parents: 165
diff changeset
89 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
90 return false;
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 }
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
96 #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
97
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 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
99 /* 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
100 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
101 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
102
182
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
103 /* Get the initial size of the array
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
104 *
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
105 * 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
106 * Unfortunately, I couldn't care less about handling this. :)
c413e475f496 dep/animia: various stylistic changes
Paper <mrpapersonic@gmail.com>
parents: 169
diff changeset
107 */
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
108 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
109 {
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 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
111 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
112 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
113 }
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 /* 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
116 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
117
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 /* 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
119 {
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 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
121 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
122 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
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
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 /* 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
126 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
127 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
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 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
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
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 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
133 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
134 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
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 /* 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
137 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
138 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
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 /* 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
141 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
142 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
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 /* 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
145 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
146 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
147 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
148
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 /* 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
150 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
151 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
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 /* 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
155 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
156 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
157 }
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 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
160 result.resize(2 * MAXCOMLEN);
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
161
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
162 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
163 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
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
165
8937fb7f2d66 dep/animia/osx: that's not how std::string works
Paper <mrpapersonic@gmail.com>
parents: 163
diff changeset
166 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
167 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
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
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 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
171 #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
172 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
173 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
174 #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
175
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 /* 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
177 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
178 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
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 /* 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
182 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
183 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
184 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
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
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 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
188 }
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
189
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
190 }