diff dep/animia/src/win/quartz.cc @ 197:c4ca035c565d

*: misc. patches
author Paper <mrpapersonic@gmail.com>
date Fri, 08 Dec 2023 11:19:54 -0500
parents 50108040d792
children bc1ae1810855
line wrap: on
line diff
--- a/dep/animia/src/win/quartz.cc	Thu Dec 07 16:28:11 2023 -0500
+++ b/dep/animia/src/win/quartz.cc	Fri Dec 08 11:19:54 2023 -0500
@@ -61,6 +61,22 @@
 	return osx::util::StringFromCFString(bundle_id, result);
 }
 
+template<typename T>
+static bool CFDictionaryGetValue(CFDictionaryRef thedict, CFStringRef key, T& out) {
+	CFTypeRef data = nullptr;
+	if (!CFDictionaryGetValueIfPresent(thedict, key, reinterpret_cast<const void**>(&data)) || !data)
+		return false;
+
+	if constexpr (std::is_arithmetic<T>::value)
+		osx::util::GetCFNumber(reinterpret_cast<CFNumberRef>(data), out);
+	else if constexpr (std::is_same<T, std::string>::value)
+		osx::util::StringFromCFString(reinterpret_cast<CFStringRef>(data), out);
+	else
+		return false;
+
+	return true;
+}
+
 bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) {
 	if (!window_proc)
 		return false;
@@ -77,34 +93,21 @@
 
 		Process proc;
 		{
-			{
-				CFNumberRef num = nullptr;
-				if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowOwnerPID"), reinterpret_cast<const void**>(&num)) && num)
-					osx::util::GetCFNumber(num, proc.pid);
-			}
-			{
-				CFStringRef str = nullptr;
-				if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowOwnerName"), reinterpret_cast<const void**>(&str)) && str)
-					osx::util::StringFromCFString(str, proc.name);
-			}
-			if (proc.name.empty())
+			CFDictionaryGetValue(window, CFSTR("kCGWindowOwnerPID"), proc.pid);
+			if (!CFDictionaryGetValue(window, CFSTR("kCGWindowOwnerName"), proc.name))
 				osx::util::GetProcessName(proc.pid, proc.name);
 		}
 
 		Window win;
 		{
-			{
-				CFNumberRef num = nullptr;
-				if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowNumber"), reinterpret_cast<const void**>(&num)) && num)
-					osx::util::GetCFNumber(num, win.id);
-			}
+			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.
-				CFStringRef str = nullptr;
-				if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowName"), reinterpret_cast<const void**>(&str)) && str)
-					osx::util::StringFromCFString(str, win.class_name);
+				CFDictionaryGetValue(window, CFSTR("kCGWindowName"), win.class_name);
 			}
+
 			GetWindowTitle(win.id, win.text);
 		}