diff dep/animia/src/fd/xnu.cc @ 162:61b76c7b656a

dep/animia: add os x launchservices method
author Paper <mrpapersonic@gmail.com>
date Fri, 17 Nov 2023 16:49:57 -0500
parents 900b5b530883
children 44c5e6dd9488
line wrap: on
line diff
--- a/dep/animia/src/fd/xnu.cc	Fri Nov 17 13:09:20 2023 -0500
+++ b/dep/animia/src/fd/xnu.cc	Fri Nov 17 16:49:57 2023 -0500
@@ -1,8 +1,11 @@
 /*
-** fd/xnu.cpp
+** fd/xnu.cc
 **  - provides support for XNU (part of Darwin)
 */
 #include "animia/fd/xnu.h"
+#ifdef HAVE_COREFOUNDATION
+#	include "animia/util/osx.h"
+#endif
 #include "animia.h"
 
 #include <cassert>
@@ -28,18 +31,22 @@
 
 	/* Get the initial size of the array */
 	size_t size;
-	int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0);
-	if (ret)
-		return false;
+	{
+		int ret = sysctl((int*)mib, mib_size, nullptr, &size, nullptr, 0);
+		if (ret)
+			return false;
+	}
 
 	/* Reserve the space for it in a std::string */
 	std::string args;
 	args.resize(size);
 
 	/* Get the contents of argc and argv */
-	ret = sysctl((int*)mib, mib_size, &args.front(), &size, NULL, 0);
-	if (ret)
-		return false;
+	{
+		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))
@@ -51,8 +58,7 @@
 	int argc;
 	memcpy(&argc, &args.front(), sizeof(argc));
 
-	/* Check for a condition that, realistically, would never happen,
-	   but better to be safe than sorry */
+	/* Do we even have argv[0]? */
 	if (argc < 1)
 		return false;
 
@@ -81,7 +87,13 @@
 }
 
 static bool GetProcessName(pid_t pid, std::string& result) {
-	/* First try parsing the arguments, this prevents the process name being
+	/* Use LaunchServices */
+#ifdef HAVE_COREFOUNDATION
+	if (osx::util::LaunchServicesGetProcessName(pid, result))
+		return true;
+#endif
+
+	/* Try parsing the arguments, this prevents the process name being
 	   cut off to 2*MAXCOMLEN (32 chars) */
 	if (GetProcessNameFromArgs(pid, result))
 		return true;