Mercurial > minori
diff 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 |
line wrap: on
line diff
--- a/dep/animia/src/util/osx.cc Wed Dec 06 19:42:33 2023 -0500 +++ b/dep/animia/src/util/osx.cc Wed Dec 06 21:26:13 2023 -0500 @@ -3,6 +3,9 @@ #include <string> #include <memory> +#include <sys/sysctl.h> +#include <libproc.h> + namespace animia::internal::osx::util { #ifdef HAVE_COREFOUNDATION @@ -54,26 +57,37 @@ if (!GetLaunchServicesPrivateSymbols()) return false; - CFReference<CFTypeRef> asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); - - CFReference<CFArrayRef> request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); - - CFReference<CFDictionaryRef> dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array.get()); - if (!dictionary.get()) + CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); + if (!asn) return false; - CFReference<CFStringRef> str; + CFArrayRef request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); + if (!request_array) { + CFRelease(asn); + return false; + } + + CFDictionaryRef dictionary = LSCopyApplicationInformation(kLSDefaultSessionID, asn, request_array); + + CFRelease(request_array); + CFRelease(asn); + + if (!dictionary) + return false; + { CFStringRef rstr; + if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr) return false; - str.reset(rstr); - } - result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); + if (!StringFromCFString(rstr, result)) { + CFRelease(rstr); + return false; + } - if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) - return false; + CFRelease(rstr); + } result.resize(result.find('\0')); @@ -84,8 +98,8 @@ if (!string) return false; - result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); - if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) + result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1); + if (!CFStringGetCString(string, &result.front(), result.length(), result.length())) return false; return true; @@ -124,6 +138,7 @@ return false; args.resize(size); + return true; } static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { @@ -132,24 +147,24 @@ /* Get argc using memcpy */ int argc; - memcpy(&argc, &args.front(), sizeof(argc)); + memcpy(&result, &result.front(), sizeof(argc)); /* Do we even have argv[0]? */ if (argc < 1) return false; /* Find the first null character */ - size_t null_pos = args.find('\0', sizeof(argc)); + size_t null_pos = result.find('\0', sizeof(argc)); if (null_pos == std::string::npos) return false; /* Find the last slash */ - size_t last_slash = args.rfind('/', null_pos); + size_t last_slash = result.rfind('/', null_pos); if (last_slash == std::string::npos) return false; /* Return our result */ - result = args.substr(last_slash + 1, null_pos - last_slash - 1); + result = result.substr(last_slash + 1, null_pos - last_slash - 1); return true; } @@ -164,7 +179,7 @@ return true; } -static bool GetProcessName(pid_t pid, std::string& result) { +bool GetProcessName(pid_t pid, std::string& result) { #ifdef HAVE_COREFOUNDATION if (LaunchServicesGetProcessName(pid, result)) return true;