Mercurial > minori
comparison dep/animia/src/util/osx.cc @ 190:2d5823df870f
dep/animia: finalize de-objc-ifying quartz
this also fixes up some... rather dumb mistakes in window.cc :)
HG Enter commit message. Lines beginning with 'HG:' are removed.
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 06 Dec 2023 21:26:13 -0500 |
parents | 649786bae914 |
children | 8548dc425697 |
comparison
equal
deleted
inserted
replaced
189:649786bae914 | 190:2d5823df870f |
---|---|
1 #include "animia/util/osx.h" | 1 #include "animia/util/osx.h" |
2 | 2 |
3 #include <string> | 3 #include <string> |
4 #include <memory> | 4 #include <memory> |
5 | |
6 #include <sys/sysctl.h> | |
7 #include <libproc.h> | |
5 | 8 |
6 namespace animia::internal::osx::util { | 9 namespace animia::internal::osx::util { |
7 | 10 |
8 #ifdef HAVE_COREFOUNDATION | 11 #ifdef HAVE_COREFOUNDATION |
9 /* all of these LaunchServices things use *internal functions* that are subject | 12 /* all of these LaunchServices things use *internal functions* that are subject |
52 static bool LaunchServicesGetProcessName(pid_t pid, std::string& result) { | 55 static bool LaunchServicesGetProcessName(pid_t pid, std::string& result) { |
53 if (!LSCopyApplicationInformation || !LSASNCreateWithPid) | 56 if (!LSCopyApplicationInformation || !LSASNCreateWithPid) |
54 if (!GetLaunchServicesPrivateSymbols()) | 57 if (!GetLaunchServicesPrivateSymbols()) |
55 return false; | 58 return false; |
56 | 59 |
57 CFReference<CFTypeRef> asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); | 60 CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); |
58 | 61 if (!asn) |
59 CFReference<CFArrayRef> request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); | 62 return false; |
60 | 63 |
61 CFReference<CFDictionaryRef> dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array.get()); | 64 CFArrayRef request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); |
62 if (!dictionary.get()) | 65 if (!request_array) { |
63 return false; | 66 CFRelease(asn); |
64 | 67 return false; |
65 CFReference<CFStringRef> str; | 68 } |
69 | |
70 CFDictionaryRef dictionary = LSCopyApplicationInformation(kLSDefaultSessionID, asn, request_array); | |
71 | |
72 CFRelease(request_array); | |
73 CFRelease(asn); | |
74 | |
75 if (!dictionary) | |
76 return false; | |
77 | |
66 { | 78 { |
67 CFStringRef rstr; | 79 CFStringRef rstr; |
80 | |
68 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr) | 81 if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr) |
69 return false; | 82 return false; |
70 str.reset(rstr); | 83 |
71 } | 84 if (!StringFromCFString(rstr, result)) { |
72 | 85 CFRelease(rstr); |
73 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); | 86 return false; |
74 | 87 } |
75 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) | 88 |
76 return false; | 89 CFRelease(rstr); |
90 } | |
77 | 91 |
78 result.resize(result.find('\0')); | 92 result.resize(result.find('\0')); |
79 | 93 |
80 return true; | 94 return true; |
81 } | 95 } |
82 | 96 |
83 bool StringFromCFString(CFStringRef string, std::string& result) { | 97 bool StringFromCFString(CFStringRef string, std::string& result) { |
84 if (!string) | 98 if (!string) |
85 return false; | 99 return false; |
86 | 100 |
87 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); | 101 result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1); |
88 if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) | 102 if (!CFStringGetCString(string, &result.front(), result.length(), result.length())) |
89 return false; | 103 return false; |
90 | 104 |
91 return true; | 105 return true; |
92 } | 106 } |
93 #endif // HAVE_COREFOUNDATION | 107 #endif // HAVE_COREFOUNDATION |
122 /* Is the size big enough to hold at least argc? */ | 136 /* Is the size big enough to hold at least argc? */ |
123 if (size < sizeof(int)) | 137 if (size < sizeof(int)) |
124 return false; | 138 return false; |
125 | 139 |
126 args.resize(size); | 140 args.resize(size); |
141 return true; | |
127 } | 142 } |
128 | 143 |
129 static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { | 144 static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { |
130 if (!GetProcessArgs(pid, result)) | 145 if (!GetProcessArgs(pid, result)) |
131 return false; | 146 return false; |
132 | 147 |
133 /* Get argc using memcpy */ | 148 /* Get argc using memcpy */ |
134 int argc; | 149 int argc; |
135 memcpy(&argc, &args.front(), sizeof(argc)); | 150 memcpy(&result, &result.front(), sizeof(argc)); |
136 | 151 |
137 /* Do we even have argv[0]? */ | 152 /* Do we even have argv[0]? */ |
138 if (argc < 1) | 153 if (argc < 1) |
139 return false; | 154 return false; |
140 | 155 |
141 /* Find the first null character */ | 156 /* Find the first null character */ |
142 size_t null_pos = args.find('\0', sizeof(argc)); | 157 size_t null_pos = result.find('\0', sizeof(argc)); |
143 if (null_pos == std::string::npos) | 158 if (null_pos == std::string::npos) |
144 return false; | 159 return false; |
145 | 160 |
146 /* Find the last slash */ | 161 /* Find the last slash */ |
147 size_t last_slash = args.rfind('/', null_pos); | 162 size_t last_slash = result.rfind('/', null_pos); |
148 if (last_slash == std::string::npos) | 163 if (last_slash == std::string::npos) |
149 return false; | 164 return false; |
150 | 165 |
151 /* Return our result */ | 166 /* Return our result */ |
152 result = args.substr(last_slash + 1, null_pos - last_slash - 1); | 167 result = result.substr(last_slash + 1, null_pos - last_slash - 1); |
153 return true; | 168 return true; |
154 } | 169 } |
155 | 170 |
156 static bool GetProcessNameFromKernel(pid_t pid, std::string& result) { | 171 static bool GetProcessNameFromKernel(pid_t pid, std::string& result) { |
157 result.resize(2 * MAXCOMLEN); | 172 result.resize(2 * MAXCOMLEN); |
162 | 177 |
163 result.resize(size); | 178 result.resize(size); |
164 return true; | 179 return true; |
165 } | 180 } |
166 | 181 |
167 static bool GetProcessName(pid_t pid, std::string& result) { | 182 bool GetProcessName(pid_t pid, std::string& result) { |
168 #ifdef HAVE_COREFOUNDATION | 183 #ifdef HAVE_COREFOUNDATION |
169 if (LaunchServicesGetProcessName(pid, result)) | 184 if (LaunchServicesGetProcessName(pid, result)) |
170 return true; | 185 return true; |
171 #endif // HAVE_COREFOUNDATION | 186 #endif // HAVE_COREFOUNDATION |
172 | 187 |