src/X11Handler.h
branchv_0
changeset 2 6851c08c6260
parent 1 043a3e2e5f0c
child 3 2c4e533e9e33
equal deleted inserted replaced
1:043a3e2e5f0c 2:6851c08c6260
    23 #include <sstream>
    23 #include <sstream>
    24 #include <locale>
    24 #include <locale>
    25 #include <codecvt>
    25 #include <codecvt>
    26 #include <stdexcept>
    26 #include <stdexcept>
    27 
    27 
       
    28 #include <X11/extensions/XTest.h>
       
    29 
    28 #include <relpipe/common/type/typedefs.h>
    30 #include <relpipe/common/type/typedefs.h>
    29 #include <relpipe/reader/TypeId.h>
    31 #include <relpipe/reader/TypeId.h>
    30 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    32 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    31 #include <relpipe/reader/handlers/AttributeMetadata.h>
    33 #include <relpipe/reader/handlers/AttributeMetadata.h>
    32 
    34 
    36 namespace out {
    38 namespace out {
    37 namespace x11 {
    39 namespace x11 {
    38 
    40 
    39 class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    41 class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    40 private:
    42 private:
    41 	std::ostream& output;
    43 
       
    44 	class Display {
       
    45 	public:
       
    46 		::Display* display = nullptr;
       
    47 		// TODO: more OOP
       
    48 
       
    49 		virtual ~Display() {
       
    50 			if (display) XCloseDisplay(display);
       
    51 		}
       
    52 	};
       
    53 
       
    54 	Display display;
       
    55 	std::ostream& output; // TODO: remove
    42 	Configuration& configuration;
    56 	Configuration& configuration;
    43 	const char QUOTE = '"';
    57 	const char QUOTE = '"'; // TODO: remove
    44 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
    58 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
    45 	std::vector<relpipe::reader::handlers::AttributeMetadata> firstAttributes;
    59 	std::vector<relpipe::reader::handlers::AttributeMetadata> firstAttributes;
    46 	relpipe::common::type::Integer valueCount = 0;
    60 	relpipe::common::type::Integer valueCount = 0;
    47 public:
    61 public:
    48 
    62 
    49 	X11Handler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
    63 	X11Handler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
       
    64 		display.display = XOpenDisplay(nullptr);
    50 	}
    65 	}
    51 
    66 
    52 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    67 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    53 		if (firstAttributes.empty()) {
    68 		if (display.display) {
    54 			firstAttributes = attributes;
    69 			if (firstAttributes.empty()) {
    55 			if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
    70 				firstAttributes = attributes;
       
    71 				if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
       
    72 			} else {
       
    73 				throw std::logic_error("Only a single relation can be converted to the X11 format.");
       
    74 			}
    56 		} else {
    75 		} else {
    57 			throw std::logic_error("Only a single relation can be converted to the X11 format.");
    76 			throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable.");
    58 		}
    77 		}
       
    78 
    59 	}
    79 	}
    60 
    80 
    61 	void attribute(const relpipe::common::type::StringX& value) override {
    81 	void attribute(const relpipe::common::type::StringX& value) override {
    62 		valueCount++;
    82 		valueCount++;
    63 
    83 
    73 		if (valueCount % firstAttributes.size()) {
    93 		if (valueCount % firstAttributes.size()) {
    74 			output << ",";
    94 			output << ",";
    75 		} else {
    95 		} else {
    76 			output << "\r\n";
    96 			output << "\r\n";
    77 			valueCount = 0;
    97 			valueCount = 0;
       
    98 			// XTestFakeKeyEvent(display.display, 38, true, 0);
       
    99 			// XTestFakeKeyEvent(display.display, 38, false, 0);
    78 		}
   100 		}
    79 	}
   101 	}
    80 
   102 
    81 	void endOfPipe() {
   103 	void endOfPipe() {
    82 		output.flush();
   104 		output.flush();