Mercurial > minori
diff dep/animone/src/util/osx.cc @ 268:382b50754fe4
dep/animone: make osx code a bit less hacky
it would be nice if macos actually provided a real API for getting
window titles (outside of the accessibility api). the accessibility
API is a real mess to work with; the user has to give permission to
access it under newer versions.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 12 Apr 2024 05:21:45 -0400 |
parents | 862d0d8619f6 |
children |
line wrap: on
line diff
--- a/dep/animone/src/util/osx.cc Thu Apr 11 22:05:41 2024 -0400 +++ b/dep/animone/src/util/osx.cc Fri Apr 12 05:21:45 2024 -0400 @@ -8,167 +8,30 @@ namespace animone::internal::osx::util { -typedef CFTypeRef (*LSASNCreateWithPidSpec)(CFAllocatorRef, pid_t); -typedef CFDictionaryRef (*LSCopyApplicationInformationSpec)(int, CFTypeRef, CFArrayRef); - -/* retrieved dynamically from launchservices */ -static LSCopyApplicationInformationSpec LSCopyApplicationInformation = nullptr; -static LSASNCreateWithPidSpec LSASNCreateWithPid = nullptr; - -static CFStringRef kLSDisplayNameKey = nullptr; -static CFStringRef kLSPIDKey = nullptr; - -/* retrieved from LaunchServicesSPI.h in WebKit */ -static constexpr int kLSDefaultSessionID = -2; +static bool GetProcessNameFromProcPidPath(pid_t pid, std::string& result) { + result.assign(PROC_PIDPATHINFO_MAXSIZE, '\0'); -static const CFStringRef kLaunchServicesBundleID = CFSTR("com.apple.LaunchServices"); - -static bool GetLaunchServicesPrivateSymbols() { - CFBundleRef launch_services_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID); - if (!launch_services_bundle) - return false; - - LSCopyApplicationInformation = reinterpret_cast<LSCopyApplicationInformationSpec>( - CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSCopyApplicationInformation"))); - if (!LSCopyApplicationInformation) - return false; - - LSASNCreateWithPid = reinterpret_cast<LSASNCreateWithPidSpec>( - CFBundleGetFunctionPointerForName(launch_services_bundle, CFSTR("_LSASNCreateWithPid"))); - if (!LSASNCreateWithPid) + int ret = proc_pidpath(pid, result.data(), result.size() * sizeof(char)); + if (ret <= 0) return false; - CFStringRef* ptr_kLSDisplayNameKey = reinterpret_cast<CFStringRef*>( - CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSDisplayNameKey"))); - if (!ptr_kLSDisplayNameKey) - return false; - kLSDisplayNameKey = *ptr_kLSDisplayNameKey; - - CFStringRef* ptr_kLSPIDKey = - reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(launch_services_bundle, CFSTR("_kLSPIDKey"))); - if (!ptr_kLSPIDKey) - return false; - kLSPIDKey = *ptr_kLSPIDKey; - - return true; -} - -static bool LaunchServicesGetProcessName(pid_t pid, std::string& result) { - if (!LSCopyApplicationInformation || !LSASNCreateWithPid || !kLSDisplayNameKey || !kLSPIDKey) - if (!GetLaunchServicesPrivateSymbols()) - return false; + /* find the last slash, if there's none, we're done here */ + size_t last_slash = result.rfind('/'); + if (last_slash == std::string::npos) + return true; - /* what the hell is an `asn`? */ - CFPtr<CFTypeRef> asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); - if (!asn) - return false; - - CFPtr<CFArrayRef> request_array = CFArrayCreate(NULL, (const void**)kLSDisplayNameKey, 1, NULL); - if (!request_array) - return false; - - CFPtr<CFDictionaryRef> dictionary = - LSCopyApplicationInformation(kLSDefaultSessionID, asn.get(), request_array.get()); - if (!dictionary) - return false; - - { - /* this doesn't need to be free'd */ - CFStringRef rstr; - - if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr) - return false; - - if (!StringFromCFString(rstr, result)) - return false; - } - - result.resize(result.find('\0')); - + result.erase(0, last_slash + 1); return true; } -bool StringFromCFString(CFStringRef string, std::string& result) { - if (!string) - return false; - - result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1); - if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8)) - return false; - - return true; -} - -static bool GetProcessArgs(pid_t pid, std::string& args) { - /* sysctl shouldn't touch these, so we define them as const */ - int mib[3] = {CTL_KERN, KERN_PROCARGS2, static_cast<int>(pid)}; - const size_t mib_size = sizeof(mib) / sizeof(*mib); - - /* Get the initial size of the array - * - * NOTE: it IS possible for this value to change inbetween calls to sysctl(). - * Unfortunately, I couldn't care less about handling this. :) - * - * is that really true, though? these should be constant values. but are - * argc and argv *really* constant? - */ - size_t size; - { - int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0); - if (ret) - return false; - } - - /* Reserve the space for it in args */ - args.resize(size); - - /* Get the contents of argc and argv */ - { - int ret = sysctl((int*)mib, mib_size, &args.front(), &size, NULL, 0); - if (ret) - return false; - } - - /* Is the size big enough to hold at least argc? */ - if (size < sizeof(int)) - return false; - - args.resize(size); - return true; -} - -static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { - if (!GetProcessArgs(pid, result)) - return false; - - /* Get argc using memcpy */ - int argc = 0; - memcpy(&argc, &result.front(), sizeof(argc)); - - /* Do we even have argv[0]? */ - if (argc < 1) - return false; - - /* Find the first null character */ - 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 = result.rfind('/', null_pos); - if (last_slash == std::string::npos) - return false; - - /* Return our result */ - result = result.substr(last_slash + 1, null_pos - last_slash - 1); - return true; -} - -static bool GetProcessNameFromKernel(pid_t pid, std::string& result) { - result.resize(2 * MAXCOMLEN); +static bool GetProcessNameFromProcName(pid_t pid, std::string& result) { + result.assign(2 * MAXCOMLEN, '\0'); int size = proc_name(pid, &result.front(), result.length()); - if (!size) + + /* if size is MAXCOMLEN or 2 * MAXCOMLEN, assume + * this method won't work and our result is truncated */ + if (size <= 0 || size == MAXCOMLEN || size == 2 * MAXCOMLEN) return false; result.resize(size); @@ -176,18 +39,10 @@ } bool GetProcessName(pid_t pid, std::string& result) { - if (LaunchServicesGetProcessName(pid, result)) + if (GetProcessNameFromProcName(pid, result)) return true; - /* Try parsing the arguments, this prevents the process name being - * cut off to 2*MAXCOMLEN (32 chars) */ - if (GetProcessNameFromArgs(pid, result)) - return true; - - /* Then attempt getting it from the kernel, which results in the - * process name being cut to 32 chars (worse, 16 chars if p_name is - * unavailable) */ - if (GetProcessNameFromKernel(pid, result)) + if (GetProcessNameFromProcPidPath(pid, result)) return true; return false;