src/X11Handler.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 02 Apr 2021 18:34:52 +0200
branchv_0
changeset 9 08c913c9662f
parent 8 6f5616572273
permissions -rw-r--r--
debug print of devices + ignore unsupported relations
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 <stdexcept>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    27
#include <X11/extensions/XTest.h>
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    28
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    29
#include <relpipe/common/type/typedefs.h>
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/TypeId.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/AttributeMetadata.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Configuration.h"
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace relpipe {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace out {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace x11 {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
class X11Handler : public relpipe::reader::handlers::RelationalReaderStringHandler {
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    42
9
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    43
	enum class RelationType {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    44
		UNSUPPORTED,
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    45
		DEVICES,
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    46
		EVENTS,
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    47
	};
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    48
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
	class Display {
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
	public:
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    51
		::Display* display = nullptr;
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
		// TODO: more OOP
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
		virtual ~Display() {
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
			if (display) XCloseDisplay(display);
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
		}
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
	};
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
	class Event {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
	public:
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
		enum class Type {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
			KEY,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
			BUTTON,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
			MOTION,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
			UNKNOWN
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
		};
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
		enum class State {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    70
			PRESSED,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    71
			RELEASED,
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    72
			UNKNOWN
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    73
		};
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    74
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    75
		Type type = Type::UNKNOWN;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    76
		State state = State::UNKNOWN;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    77
		int key = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    78
		int button = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    79
		unsigned long screen = 0;
4
e5baa07d6e60 use CurrentTime (0) as default delay
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
		unsigned long delay = CurrentTime;
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    81
		int x = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    82
		int y = -1;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    83
	} currentEvent;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    84
9
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    85
	class Device {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    86
	public:
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    87
		relpipe::common::type::Integer id = -1;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    88
		relpipe::common::type::StringX name;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    89
		relpipe::common::type::StringX type;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    90
	} currentDevice;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    91
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
	Display display;
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	Configuration& configuration;
9
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    94
	RelationType relationType = RelationType::UNSUPPORTED;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    95
	relpipe::common::type::StringX relationName;
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    96
	std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    97
	relpipe::common::type::Integer attributeIndex = 0;
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
public:
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   100
	X11Handler(Configuration& configuration) : configuration(configuration) {
2
6851c08c6260 open X display
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   101
		display.display = XOpenDisplay(nullptr);
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 startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
9
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   105
		this->attributes = attributes;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   106
		this->relationName = name;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   107
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   108
		if (relationName == L"x11_input_device") {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   109
			this->relationType = RelationType::DEVICES;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   110
		} else if (relationName == L"x11_input_event") {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   111
			this->relationType = RelationType::EVENTS;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   112
			if (display.display == nullptr) 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
   113
		} else {
9
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   114
			this->relationType = RelationType::UNSUPPORTED;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   115
			std::wcerr << L"Unsupported relation: " << relationName << std::endl;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   116
			// TODO: throw exception?
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
1
043a3e2e5f0c namespaces clean up
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   120
	void attribute(const relpipe::common::type::StringX& value) override {
9
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   121
		if (relationType == RelationType::DEVICES) attributeOfDevice(value);
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   122
		else if (relationType == RelationType::EVENTS) attributeOfEvent(value);
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   123
		else attributeOfUnsupported(value);
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   124
	}
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   125
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   126
private:
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   127
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   128
	void attributeOfDevice(const relpipe::common::type::StringX& value) {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   129
		if (attributes[attributeIndex].getAttributeName() == L"id") currentDevice.id = stol(value);
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   130
		else if (attributes[attributeIndex].getAttributeName() == L"name") currentDevice.name = value;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   131
		else if (attributes[attributeIndex].getAttributeName() == L"type") currentDevice.type = value;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   132
		// else: ignore other attributes
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   133
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   134
		attributeIndex++;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   135
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   136
		if (attributeIndex % attributes.size() == 0) {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   137
			if (configuration.debug) std::wcerr << L"Device: id = „" << currentDevice.id << L"“ name = „" << currentDevice.name << L"“ type = „" << currentDevice.type << L"“" << std::endl;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   138
			attributeIndex = 0;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   139
			currentEvent = Event();
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   140
		}
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   141
	}
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   142
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   143
	void attributeOfUnsupported(const relpipe::common::type::StringX& value) {
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   144
		if (configuration.debug) std::wcerr << L"Unsupported relation „" << relationName << L"“ has attribute „" << value << L"“" << std::endl;
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   145
	}
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   146
08c913c9662f debug print of devices + ignore unsupported relations
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   147
	void attributeOfEvent(const relpipe::common::type::StringX& value) {
8
6f5616572273 clean-up, comments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   148
		// TODO: strictly expect correct data types (integers)?
6f5616572273 clean-up, comments
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   149
		// TODO: shorter codes (k/b/m, p/r) instead of names or both?
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   150
		if (attributes[attributeIndex].getAttributeName() == L"device") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   151
			// 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
   152
		} else if (attributes[attributeIndex].getAttributeName() == L"type") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   153
			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
   154
			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
   155
			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
   156
			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
   157
		} else if (attributes[attributeIndex].getAttributeName() == L"state") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   158
			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
   159
			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
   160
			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
   161
		} else if (attributes[attributeIndex].getAttributeName() == L"button") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   162
			currentEvent.button = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   163
		} else if (attributes[attributeIndex].getAttributeName() == L"key") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   164
			currentEvent.key = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   165
		} else if (attributes[attributeIndex].getAttributeName() == L"delay") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   166
			currentEvent.delay = stoul(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   167
		} else if (attributes[attributeIndex].getAttributeName() == L"screen") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   168
			currentEvent.screen = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   169
		} else if (attributes[attributeIndex].getAttributeName() == L"x") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   170
			currentEvent.x = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   171
		} else if (attributes[attributeIndex].getAttributeName() == L"y") {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   172
			currentEvent.y = stol(value);
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   173
		} else {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   174
			// ignore other attributes
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   177
		attributeIndex++;
6
3407386d1f60 do XFlush() after each record in order to immediately process events and avoid buffering/delays/lagging
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   178
5
dbf093b8b9ac add boolean options: --debug and --dry-run
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   179
		bool debug = configuration.debug;
dbf093b8b9ac add boolean options: --debug and --dry-run
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   180
		bool run = !configuration.dryRun;
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   181
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   182
		if (attributeIndex % attributes.size() == 0) {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   183
			if (currentEvent.type == Event::Type::KEY) {
7
93d9c4fce585 better debug output formatting: XML
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   184
				if (debug) std::wcerr << L"<key    x=\"" << currentEvent.x << L"\" y=\"" << currentEvent.y << L"\"    code=\"" << currentEvent.key << L"\" state=\"" << (currentEvent.state == Event::State::PRESSED ? L"pressed" : L"released") << L"\"/>" << std::endl;
5
dbf093b8b9ac add boolean options: --debug and --dry-run
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   185
				if (run) 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
   186
			} else if (currentEvent.type == Event::Type::BUTTON) {
7
93d9c4fce585 better debug output formatting: XML
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   187
				if (debug) std::wcerr << L"<button x=\"" << currentEvent.x << L"\" y=\"" << currentEvent.y << L"\"    code=\"" << currentEvent.button << L"\" state=\"" << (currentEvent.state == Event::State::PRESSED ? L"pressed" : L"released") << L"\"/>" << std::endl;
5
dbf093b8b9ac add boolean options: --debug and --dry-run
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   188
				if (run) XTestFakeButtonEvent(display.display, currentEvent.button, currentEvent.state == Event::State::PRESSED, currentEvent.delay);
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   189
			} else if (currentEvent.type == Event::Type::MOTION) {
7
93d9c4fce585 better debug output formatting: XML
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   190
				if (debug) std::wcerr << L"<motion x=\"" << currentEvent.x << L"\" y=\"" << currentEvent.y << L"\"/>" << std::endl;
5
dbf093b8b9ac add boolean options: --debug and --dry-run
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   191
				if (run) XTestFakeMotionEvent(display.display, currentEvent.screen, currentEvent.x, currentEvent.y, currentEvent.delay);
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   192
			} else {
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   193
				std::wcerr << L"Unsupported event" << std::endl;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   194
			}
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   195
6
3407386d1f60 do XFlush() after each record in order to immediately process events and avoid buffering/delays/lagging
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   196
			XFlush(display.display);
3
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   197
			attributeIndex = 0;
2c4e533e9e33 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   198
			currentEvent = Event();
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
	void endOfPipe() {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
};
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   209
}