changeset 299:246017a7907a

dep/animone: clean up OS X code GetProcessName() really belongs in fd.cc after removing the stupid unnecessary LaunchServices code that was stolen from... some library :)
author Paper <paper@paper.us.eu.org>
date Mon, 13 May 2024 14:15:47 -0400
parents dec4d3c9a909
children 8eb0cfe59992
files dep/animone/include/animone/fd/xnu.h dep/animone/src/fd.cc dep/animone/src/fd/xnu.cc dep/animone/src/win/quartz.cc
diffstat 4 files changed, 45 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/dep/animone/include/animone/fd/xnu.h	Mon May 13 03:28:42 2024 -0400
+++ b/dep/animone/include/animone/fd/xnu.h	Mon May 13 14:15:47 2024 -0400
@@ -11,6 +11,7 @@
 
 bool EnumerateOpenProcesses(process_proc_t process_proc);
 bool EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc);
+bool GetProcessName(pid_t pid, std::string& result);
 
 } // namespace animone::internal::xnu
 
--- a/dep/animone/src/fd.cc	Mon May 13 03:28:42 2024 -0400
+++ b/dep/animone/src/fd.cc	Mon May 13 14:15:47 2024 -0400
@@ -10,7 +10,6 @@
 
 #ifdef MACOSX
 #	include "animone/fd/xnu.h"
-#	include "animone/util/osx.h"
 #endif
 
 #ifdef BSD
@@ -75,7 +74,7 @@
 #endif
 
 #ifdef MACOSX
-	success ^= osx::util::GetProcessName(pid, name);
+	success ^= xnu::GetProcessName(pid, name);
 #endif
 
 #ifdef BSD
--- a/dep/animone/src/fd/xnu.cc	Mon May 13 03:28:42 2024 -0400
+++ b/dep/animone/src/fd/xnu.cc	Mon May 13 14:15:47 2024 -0400
@@ -91,4 +91,44 @@
 	return true;
 }
 
+static bool GetProcessNameFromProcPidPath(pid_t pid, std::string& result) {
+	result.assign(PROC_PIDPATHINFO_MAXSIZE, '\0');
+
+	int ret = proc_pidpath(pid, result.data(), result.size() * sizeof(char));
+	if (ret <= 0)
+		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;
+
+	result.erase(0, last_slash + 1);
+	return true;
+}
+
+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 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);
+	return true;
+}
+
+bool GetProcessName(pid_t pid, std::string& result) {
+	if (GetProcessNameFromProcName(pid, result))
+		return true;
+
+	if (GetProcessNameFromProcPidPath(pid, result))
+		return true;
+
+	return false;
+}
+
 } // namespace animone::internal::xnu
--- a/dep/animone/src/win/quartz.cc	Mon May 13 03:28:42 2024 -0400
+++ b/dep/animone/src/win/quartz.cc	Mon May 13 14:15:47 2024 -0400
@@ -5,8 +5,8 @@
  * but it *does* require an Objective-C runtime.
  */
 #include "animone/win/quartz.h"
+#include "animone/fd.h"
 #include "animone.h"
-#include "animone/util/osx.h"
 
 #include <objc/message.h>
 #include <objc/runtime.h>
@@ -268,7 +268,7 @@
 		{
 			CFDictionaryGetValue(window, CFSTR("kCGWindowOwnerPID"), proc.pid);
 			if (!CFDictionaryGetValue(window, CFSTR("kCGWindowOwnerName"), proc.name))
-				osx::util::GetProcessName(proc.pid, proc.name);
+				fd::GetProcessName(proc.pid, proc.name);
 		}
 
 		Window win;
@@ -276,8 +276,7 @@
 			CFDictionaryGetValue(window, CFSTR("kCGWindowNumber"), win.id);
 
 			if (!GetProcessBundleIdentifier(proc.pid, win.class_name))
-				// Fallback to the Quartz window name, which is unlikely to be filled, but it
-				// *could* be.
+				/* XXX is this right? */
 				CFDictionaryGetValue(window, CFSTR("kCGWindowName"), win.class_name);
 
 			GetWindowTitle(win.id, proc.pid, win.text);