list windows: window names v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 06 Apr 2021 00:32:53 +0200
branchv_0
changeset 9 c8d681753bc2
parent 8 b498160cc2ab
child 10 803beead60c4
list windows: window names
src/X11Command.h
--- a/src/X11Command.h	Mon Apr 05 18:09:25 2021 +0200
+++ b/src/X11Command.h	Tue Apr 06 00:32:53 2021 +0200
@@ -263,14 +263,21 @@
 	}
 
 	relpipe::common::type::StringX fetchWindowName(const Display& display, Window window) {
-		relpipe::common::type::StringX name;
-		// TODO: this does not work → use XGetWMName(), XmbTextPropertyToTextList(), XFreeStringList() instead of XFetchName()
-		// char* rawName = nullptr;
-		// if (XFetchName(display.display, window, &rawName) && rawName) 
-		// name = convertor.from_bytes(rawName); // bad encoding, std::range_error – wstring_convert::from_bytes
-		// std::cout << "\n\n\n>>> " << rawName << " <<<\n\n\n"; // bad encoding, not UTF-8
-		//XFree(rawName);
-		return name;
+		std::string name;
+
+		XTextProperty property;
+		if (XGetWMName(display.display, window, &property) && property.nitems > 0) {
+			int count = 0;
+			int result;
+			char** list = nullptr;
+			result = XmbTextPropertyToTextList(display.display, &property, &list, &count);
+			if ((result == Success || result > 0) && list) for (int i = 0; i < count; i++) name.append(list[i]);
+			// else name = (char*) property.value; // TODO: use property.value?
+			XFreeStringList(list);
+		}
+		XFree(property.value);
+
+		return convertor.from_bytes(name);
 	}
 
 	void listWindow(const Display& display, Window window, relpipe::common::type::Integer level, Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {