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