src/X11Handler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 30 Mar 2021 20:26:08 +0200
branchv_0
changeset 4 e5baa07d6e60
parent 3 2c4e533e9e33
child 5 dbf093b8b9ac
permissions -rw-r--r--
use CurrentTime (0) as default delay
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <stdexcept>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    28
#include <X11/extensions/XTest.h>
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    29
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    30
#include <relpipe/common/type/typedefs.h>
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/TypeId.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/reader/handlers/AttributeMetadata.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "Configuration.h"
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace relpipe {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace out {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace x11 {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler {
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    43
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    44
	class Display {
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    45
	public:
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
		::Display* display = nullptr;
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
		// TODO: more OOP
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    48
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
		virtual ~Display() {
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
			if (display) XCloseDisplay(display);
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    51
		}
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
	};
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
	class Event {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
	public:
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    57
		enum class Type {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    58
			KEY,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
			BUTTON,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
			MOTION,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
			UNKNOWN
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
		};
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
		enum class State {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
			PRESSED,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
			RELEASED,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
			UNKNOWN
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    68
		};
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    69
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    70
		Type type = Type::UNKNOWN;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    71
		State state = State::UNKNOWN;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    72
		int key = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    73
		int button = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    74
		unsigned long screen = 0;
4
e5baa07d6e60 use CurrentTime (0) as default delay
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
		unsigned long delay = CurrentTime;
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    76
		int x = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    77
		int y = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    78
	} currentEvent;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    79
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    80
	Display display;
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	Configuration& configuration;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    83
	std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    84
	relpipe::common::type::Integer attributeIndex = 0;
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
public:
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    87
	X11Handler(Configuration& configuration) : configuration(configuration) {
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
		display.display = XOpenDisplay(nullptr);
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
		if (display.display) {
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    93
			if (this->attributes.empty()) {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    94
				this->attributes = attributes;
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    95
			} else {
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    96
				throw std::logic_error("Only a single relation can be converted to the X11 format.");
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    97
			}
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		} else {
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    99
			throw std::invalid_argument("Unable to open display. Please check the $DISPLAY variable.");
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		}
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   101
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
	void attribute(const relpipe::common::type::StringX& value) override {
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   105
		if (attributes[attributeIndex].getAttributeName() == L"device") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   106
			// TODO: maybe add some filtering based on device ID
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   107
		} else if (attributes[attributeIndex].getAttributeName() == L"type") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   108
			if (value == L"key") currentEvent.type = Event::Type::KEY;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   109
			else if (value == L"button") currentEvent.type = Event::Type::BUTTON;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
			else if (value == L"motion") currentEvent.type = Event::Type::MOTION;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   111
			else currentEvent.type == Event::Type::UNKNOWN; // TODO: throw exception or print warning?
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   112
		} else if (attributes[attributeIndex].getAttributeName() == L"state") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   113
			if (value == L"pressed") currentEvent.state = Event::State::PRESSED;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
			else if (value == L"released") currentEvent.state = Event::State::RELEASED;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
			else currentEvent.state == Event::State::UNKNOWN; // TODO: throw exception or print warning?
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
		} else if (attributes[attributeIndex].getAttributeName() == L"button") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   117
			currentEvent.button = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   118
		} else if (attributes[attributeIndex].getAttributeName() == L"key") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   119
			currentEvent.key = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   120
		} else if (attributes[attributeIndex].getAttributeName() == L"delay") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   121
			currentEvent.delay = stoul(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   122
		} else if (attributes[attributeIndex].getAttributeName() == L"screen") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   123
			currentEvent.screen = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   124
		} else if (attributes[attributeIndex].getAttributeName() == L"x") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   125
			currentEvent.x = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   126
		} else if (attributes[attributeIndex].getAttributeName() == L"y") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   127
			currentEvent.y = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   128
		} else {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   129
			// ignore other attributes
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   132
		attributeIndex++;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   133
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   134
		if (attributeIndex % attributes.size() == 0) {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   135
			if (currentEvent.type == Event::Type::KEY) {
4
e5baa07d6e60 use CurrentTime (0) as default delay
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   136
				XTestFakeKeyEvent(display.display, currentEvent.key, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   137
			} else if (currentEvent.type == Event::Type::BUTTON) {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   138
				XTestFakeButtonEvent(display.display, currentEvent.button, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   139
			} else if (currentEvent.type == Event::Type::MOTION) {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   140
				XTestFakeMotionEvent(display.display, currentEvent.screen, currentEvent.x, currentEvent.y, currentEvent.delay);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   141
			} else {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   142
				std::wcerr << L"Unsupported event" << std::endl;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   143
			}
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   144
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   145
			attributeIndex = 0;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   146
			currentEvent = Event();
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
	void endOfPipe() {
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   151
		// XTestDiscard(display.display); // TODO: wait before discard?
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
};
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   155
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
}