annotate dep/animia/src/util/osx.cc @ 163:44c5e6dd9488

dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
author Paper <mrpapersonic@gmail.com>
date Sat, 18 Nov 2023 00:47:40 -0500
parents 61b76c7b656a
children 8937fb7f2d66
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
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
3 #ifdef HAVE_COREFOUNDATION
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #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
5 #endif
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 namespace animia::internal::osx::util {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8
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
9 #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
10 /* All of these LaunchServices things use *internal functions* that are subject
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
11 ** to change. Granted, it's not very likely that these will change very much
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 ** because I'm fairly sure Apple uses them lots in their own internal code.
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
13 */
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14
163
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
15 /* from RDProcess */
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 typedef CFDictionaryRef (*LSCopyApplicationInformationSpec)(int, CFTypeRef, CFArrayRef);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 static LSCopyApplicationInformationSpec LSCopyApplicationInformation = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 static LSASNCreateWithPidSpec LSASNCreateWithPid = nullptr;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21
163
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
22 /* retrieved from LaunchServicesSPI.h in WebKit */
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
23 static constexpr int kLSDefaultSessionID = -2;
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
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 CFArrayRef request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 CFDictionaryRef dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array);
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 CFRelease(request_array);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 if (!dictionary)
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 CFStringRef str;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&str) || !str) {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 CFRelease(dictionary);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 CFRetain(str);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 CFRelease(dictionary);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 result.reserve(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str), kCFStringEncodingUTF8) + 1);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 if (!CFStringGetCString(str, &result.front(), result.length(), result.length())) {
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 CFRelease(str);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 return false;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 }
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 CFRelease(str);
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 return true;
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 }
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
89 #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
90
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
91 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
92 /* 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
93 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
94 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
95
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 /* Get the initial size of the array */
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 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
98 {
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 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
100 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
101 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
102 }
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
103
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
104 /* 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
105 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
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 /* 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
108 {
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
109 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
110 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
111 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
112 }
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 /* 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
115 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
116 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
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 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
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
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 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
122 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
123 return false;
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
124
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
125 /* 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
126 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
127 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
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 /* 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
130 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
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 /* 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
134 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
135 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
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 /* 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
139 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
140 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
141 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
142
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
143 /* 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
144 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
145 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
146 }
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
147
44c5e6dd9488 dep/animia/osx: move GetProcessName to util/osx so we can use it in quartz
Paper <mrpapersonic@gmail.com>
parents: 162
diff changeset
148 static bool GetProcessNameFromKernel(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
149 result.reserve(2*MAXCOMLEN);
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 if (!proc_name(pid, &result.front(), result.length()))
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 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
152
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 result.shrink_to_fit();
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 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
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
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 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
158 #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
159 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
160 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
161 #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
162
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 /* 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
164 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
165 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
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 /* 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
169 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
170 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
171 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
172 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
173
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 }
162
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
176
61b76c7b656a dep/animia: add os x launchservices method
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
177 }