src/X11Handler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 01 Apr 2021 18:55:32 +0200
branchv_0
changeset 6 3407386d1f60
parent 5 dbf093b8b9ac
child 7 93d9c4fce585
permissions -rw-r--r--
do XFlush() after each record in order to immediately process events and avoid buffering/delays/lagging

/**
 * Relational pipes
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#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>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include "Configuration.h"

namespace relpipe {
namespace out {
namespace x11 {

class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler {
private:

	class Display {
	public:
		::Display* display = nullptr;
		// TODO: more OOP

		virtual ~Display() {
			if (display) XCloseDisplay(display);
		}
	};

	class Event {
	public:

		enum class Type {
			KEY,
			BUTTON,
			MOTION,
			UNKNOWN
		};

		enum class State {
			PRESSED,
			RELEASED,
			UNKNOWN
		};

		Type type = Type::UNKNOWN;
		State state = State::UNKNOWN;
		int key = -1;
		int button = -1;
		unsigned long screen = 0;
		unsigned long delay = CurrentTime;
		int x = -1;
		int y = -1;
	} currentEvent;

	Display display;
	Configuration& configuration;
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
	std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
	relpipe::common::type::Integer attributeIndex = 0;
public:

	X11Handler(Configuration& configuration) : configuration(configuration) {
		display.display = XOpenDisplay(nullptr);
	}

	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
		if (display.display) {
			if (this->attributes.empty()) {
				this->attributes = attributes;
			} else {
				throw std::logic_error("Only a single relation can be converted to the X11 format.");
			}
		} else {
			throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable.");
		}

	}

	void attribute(const relpipe::common::type::StringX& value) override {
		if (attributes[attributeIndex].getAttributeName() == L"device") {
			// TODO: maybe add some filtering based on device ID
		} else if (attributes[attributeIndex].getAttributeName() == L"type") {
			if (value == L"key") currentEvent.type = Event::Type::KEY;
			else if (value == L"button") currentEvent.type = Event::Type::BUTTON;
			else if (value == L"motion") currentEvent.type = Event::Type::MOTION;
			else currentEvent.type == Event::Type::UNKNOWN; // TODO: throw exception or print warning?
		} else if (attributes[attributeIndex].getAttributeName() == L"state") {
			if (value == L"pressed") currentEvent.state = Event::State::PRESSED;
			else if (value == L"released") currentEvent.state = Event::State::RELEASED;
			else currentEvent.state == Event::State::UNKNOWN; // TODO: throw exception or print warning?
		} else if (attributes[attributeIndex].getAttributeName() == L"button") {
			currentEvent.button = stol(value);
		} else if (attributes[attributeIndex].getAttributeName() == L"key") {
			currentEvent.key = stol(value);
		} else if (attributes[attributeIndex].getAttributeName() == L"delay") {
			currentEvent.delay = stoul(value);
		} else if (attributes[attributeIndex].getAttributeName() == L"screen") {
			currentEvent.screen = stol(value);
		} else if (attributes[attributeIndex].getAttributeName() == L"x") {
			currentEvent.x = stol(value);
		} else if (attributes[attributeIndex].getAttributeName() == L"y") {
			currentEvent.y = stol(value);
		} else {
			// ignore other attributes
		}

		attributeIndex++;

		bool debug = configuration.debug;
		bool run = !configuration.dryRun;

		if (attributeIndex % attributes.size() == 0) {
			if (currentEvent.type == Event::Type::KEY) {
				if (debug) std::wcerr << L"KEY:    x = " << currentEvent.x << L" y = " << currentEvent.y << L"    key = " << currentEvent.key << L" state = " << (currentEvent.state == Event::State::PRESSED ? L"pressed" : L"released") << std::endl;
				if (run) XTestFakeKeyEvent(display.display, currentEvent.key, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
			} else if (currentEvent.type == Event::Type::BUTTON) {
				if (debug) std::wcerr << L"BUTTON: x = " << currentEvent.x << L" y = " << currentEvent.y << L" button = " << currentEvent.button << L" state = " << (currentEvent.state == Event::State::PRESSED ? L"pressed" : L"released") << std::endl;
				if (run) XTestFakeButtonEvent(display.display, currentEvent.button, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
			} else if (currentEvent.type == Event::Type::MOTION) {
				if (debug) std::wcerr << L"MOTION: x = " << currentEvent.x << L" y = " << currentEvent.y << std::endl;
				if (run) XTestFakeMotionEvent(display.display, currentEvent.screen, currentEvent.x, currentEvent.y, currentEvent.delay);
			} else {
				std::wcerr << L"Unsupported event" << std::endl;
			}

			XFlush(display.display);
			attributeIndex = 0;
			currentEvent = Event();
		}
	}

	void endOfPipe() {
		// XTestDiscard(display.display); // TODO: wait before discard?
	}

};

}
}
}