open X display v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 28 Mar 2021 19:09:44 +0200
branchv_0
changeset 2 6851c08c6260
parent 1 043a3e2e5f0c
child 3 2c4e533e9e33
open X display
src/X11Handler.h
--- a/src/X11Handler.h	Sun Mar 28 18:57:10 2021 +0200
+++ b/src/X11Handler.h	Sun Mar 28 19:09:44 2021 +0200
@@ -25,6 +25,8 @@
 #include <codecvt>
 #include <stdexcept>
 
+#include <X11/extensions/XTest.h>
+
 #include <relpipe/common/type/typedefs.h>
 #include <relpipe/reader/TypeId.h>
 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
@@ -38,24 +40,42 @@
 
 class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler {
 private:
-	std::ostream& output;
+
+	class Display {
+	public:
+		::Display* display = nullptr;
+		// TODO: more OOP
+
+		virtual ~Display() {
+			if (display) XCloseDisplay(display);
+		}
+	};
+
+	Display display;
+	std::ostream& output; // TODO: remove
 	Configuration& configuration;
-	const char QUOTE = '"';
+	const char QUOTE = '"'; // TODO: remove
 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
 	std::vector<relpipe::reader::handlers::AttributeMetadata> firstAttributes;
 	relpipe::common::type::Integer valueCount = 0;
 public:
 
 	X11Handler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
+		display.display = XOpenDisplay(nullptr);
 	}
 
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
-		if (firstAttributes.empty()) {
-			firstAttributes = attributes;
-			if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
+		if (display.display) {
+			if (firstAttributes.empty()) {
+				firstAttributes = attributes;
+				if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
+			} else {
+				throw std::logic_error("Only a single relation can be converted to the X11 format.");
+			}
 		} else {
-			throw std::logic_error("Only a single relation can be converted to the X11 format.");
+			throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable.");
 		}
+
 	}
 
 	void attribute(const relpipe::common::type::StringX& value) override {
@@ -75,6 +95,8 @@
 		} else {
 			output << "\r\n";
 			valueCount = 0;
+			// XTestFakeKeyEvent(display.display, 38, true, 0);
+			// XTestFakeKeyEvent(display.display, 38, false, 0);
 		}
 	}