changeset 214:8d35061e7505

dep/animia: win/x11: simplify getting strings
author Paper <mrpapersonic@gmail.com>
date Sun, 07 Jan 2024 11:19:28 -0500
parents 58e81b42a0d6
children 031a257ee019
files dep/animia/src/win/x11.cc
diffstat 1 files changed, 11 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/dep/animia/src/win/x11.cc	Sun Jan 07 11:11:27 2024 -0500
+++ b/dep/animia/src/win/x11.cc	Sun Jan 07 11:19:28 2024 -0500
@@ -20,19 +20,23 @@
 typedef ::Display XDisplay;
 typedef ::Atom XAtom;
 
-/* should return UTF8_STRING or STRING */
-static bool GetWindowPropertyAsString(XDisplay* display, XWindow window, XAtom atom, std::string& result, XAtom reqtype = AnyPropertyType) {
-	if (atom == None || reqtype == None)
+/* should return UTF8_STRING or STRING. this means we are not
+ * *guaranteed* a UTF-8 string back.
+*/
+static bool GetWindowPropertyAsString(XDisplay* display, XWindow window, XAtom atom, std::string& result) {
+	if (atom == None)
 		return false;
 
+	XAtom Atom_UTF8_STRING = ::XInternAtom(display, "UTF8_STRING", False);
+
 	int format;
 	unsigned long leftover_bytes, num_of_items;
 	XAtom type;
 	unsigned char* data;
 
-	int status = ::XGetWindowProperty(display, window, atom, 0L, (~0L), False, reqtype,
+	int status = ::XGetWindowProperty(display, window, atom, 0L, (~0L), False, AnyPropertyType,
 	                                  &type, &format, &num_of_items, &leftover_bytes, &data);
-	if (status != Success || !(reqtype == AnyPropertyType || type == reqtype) || !num_of_items)
+	if (status != Success || !(type == Atom_UTF8_STRING || type == XA_STRING) || !num_of_items)
 		return false;
 
 	result = std::string((char*)data, num_of_items);
@@ -91,13 +95,10 @@
 }
 
 static bool FetchName(XDisplay* display, XWindow window, std::string& result) {
-	/* TODO: Check if XInternAtom created None or not... */
-	if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "_NET_WM_NAME", True),
-		                          result, ::XInternAtom(display, "UTF8_STRING", True)))
+	if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "_NET_WM_NAME", True), result))
 		return true;
 
-	if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "WM_NAME", True),
-		                           result, XA_STRING))
+	if (GetWindowPropertyAsString(display, window, ::XInternAtom(display, "WM_NAME", True), result))
 		return true;
 
 	/* Fallback to XGetWMName() */
@@ -147,11 +148,6 @@
 
 		if (status < Success || !num_of_items)
 			return false;
-
-		if (format != 32) {
-			::XFree(data);
-			return false;
-		}
 	}
 
 	XWindow* arr = (XWindow*)data;